blob: f37a6fbae152b24c1bdaac14521a427802324c7b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2** Copyright 2008, The Android Open Source Project
3**
4** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
7**
8** http://www.apache.org/licenses/LICENSE-2.0
9**
10** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
14** limitations under the License.
15*/
16
17#include "installd.h"
18
Kenny Root86c95842011-03-31 13:16:12 -070019int create_pkg_path_in_dir(char path[PKG_PATH_MAX],
20 const dir_rec_t* dir,
21 const char* pkgname,
22 const char* postfix)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023{
Kenny Root86c95842011-03-31 13:16:12 -070024 const size_t postfix_len = strlen(postfix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
Kenny Root86c95842011-03-31 13:16:12 -070026 const size_t pkgname_len = strlen(pkgname);
27 if (pkgname_len > PKG_NAME_MAX) {
28 return -1;
29 }
30
31 if (is_valid_package_name(pkgname) < 0) {
32 return -1;
33 }
34
35 if ((pkgname_len + dir->len + postfix_len) >= PKG_PATH_MAX) {
36 return -1;
37 }
38
39 char *dst = path;
40 size_t dst_size = PKG_PATH_MAX;
41
42 if (append_and_increment(&dst, dir->path, &dst_size) < 0
43 || append_and_increment(&dst, pkgname, &dst_size) < 0
44 || append_and_increment(&dst, postfix, &dst_size) < 0) {
45 LOGE("Error building APK path");
46 return -1;
47 }
48
49 return 0;
50}
51
52/**
53 * Create the package path name for a given package name with a postfix for
54 * a certain persona. Returns 0 on success, and -1 on failure.
55 */
56int create_pkg_path(char path[PKG_PATH_MAX],
57 const char *pkgname,
58 const char *postfix,
59 uid_t persona)
60{
61 size_t uid_len;
62 char* persona_prefix;
63 if (persona == 0) {
64 persona_prefix = PRIMARY_USER_PREFIX;
65 uid_len = 0;
66 } else {
67 persona_prefix = SECONDARY_USER_PREFIX;
68 uid_len = snprintf(NULL, 0, "%d", persona);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 }
Kenny Root86c95842011-03-31 13:16:12 -070070
71 const size_t prefix_len = android_data_dir.len + strlen(persona_prefix) + uid_len + 1 /*slash*/;
72 char prefix[prefix_len + 1];
73
74 char *dst = prefix;
75 size_t dst_size = sizeof(prefix);
76
77 if (append_and_increment(&dst, android_data_dir.path, &dst_size) < 0
78 || append_and_increment(&dst, persona_prefix, &dst_size) < 0) {
79 LOGE("Error building prefix for APK path");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 return -1;
81 }
82
Kenny Root86c95842011-03-31 13:16:12 -070083 if (persona != 0) {
84 int ret = snprintf(dst, dst_size, "%d/", persona);
85 if (ret < 0 || (size_t) ret != uid_len + 1) {
86 LOGW("Error appending UID to APK path");
87 return -1;
88 }
89 }
90
91 dir_rec_t dir;
92 dir.path = prefix;
93 dir.len = prefix_len;
94
95 return create_pkg_path_in_dir(path, &dir, pkgname, postfix);
96}
97
98/**
99 * Checks whether the package name is valid. Returns -1 on error and
100 * 0 on success.
101 */
102int is_valid_package_name(const char* pkgname) {
103 const char *x = pkgname;
Suchi Amalapurapuc028be42010-01-25 12:19:12 -0800104 int alpha = -1;
Kenny Root86c95842011-03-31 13:16:12 -0700105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 while (*x) {
107 if (isalnum(*x) || (*x == '_')) {
108 /* alphanumeric or underscore are fine */
109 } else if (*x == '.') {
110 if ((x == pkgname) || (x[1] == '.') || (x[1] == 0)) {
111 /* periods must not be first, last, or doubled */
112 LOGE("invalid package name '%s'\n", pkgname);
113 return -1;
114 }
Suchi Amalapurapuc028be42010-01-25 12:19:12 -0800115 } else if (*x == '-') {
116 /* Suffix -X is fine to let versioning of packages.
117 But whatever follows should be alphanumeric.*/
118 alpha = 1;
Kenny Root86c95842011-03-31 13:16:12 -0700119 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 /* anything not A-Z, a-z, 0-9, _, or . is invalid */
121 LOGE("invalid package name '%s'\n", pkgname);
122 return -1;
123 }
Kenny Root86c95842011-03-31 13:16:12 -0700124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 x++;
126 }
Kenny Root86c95842011-03-31 13:16:12 -0700127
Suchi Amalapurapuc028be42010-01-25 12:19:12 -0800128 if (alpha == 1) {
129 // Skip current character
130 x++;
131 while (*x) {
132 if (!isalnum(*x)) {
133 LOGE("invalid package name '%s' should include only numbers after -\n", pkgname);
134 return -1;
135 }
136 x++;
137 }
138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 return 0;
141}
142
143static int _delete_dir_contents(DIR *d, const char *ignore)
144{
145 int result = 0;
146 struct dirent *de;
147 int dfd;
148
149 dfd = dirfd(d);
150
151 if (dfd < 0) return -1;
152
153 while ((de = readdir(d))) {
154 const char *name = de->d_name;
155
156 /* skip the ignore name if provided */
157 if (ignore && !strcmp(name, ignore)) continue;
158
159 if (de->d_type == DT_DIR) {
160 int r, subfd;
161 DIR *subdir;
162
163 /* always skip "." and ".." */
164 if (name[0] == '.') {
165 if (name[1] == 0) continue;
166 if ((name[1] == '.') && (name[2] == 0)) continue;
167 }
168
169 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
170 if (subfd < 0) {
Kenny Root50871522010-08-04 09:14:01 -0700171 LOGE("Couldn't openat %s: %s\n", name, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 result = -1;
173 continue;
174 }
175 subdir = fdopendir(subfd);
176 if (subdir == NULL) {
Kenny Root50871522010-08-04 09:14:01 -0700177 LOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 close(subfd);
179 result = -1;
180 continue;
181 }
182 if (_delete_dir_contents(subdir, 0)) {
183 result = -1;
184 }
185 closedir(subdir);
186 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
Kenny Root50871522010-08-04 09:14:01 -0700187 LOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 result = -1;
189 }
190 } else {
191 if (unlinkat(dfd, name, 0) < 0) {
Kenny Root50871522010-08-04 09:14:01 -0700192 LOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 result = -1;
194 }
195 }
196 }
197
198 return result;
199}
200
201int delete_dir_contents(const char *pathname,
202 int also_delete_dir,
203 const char *ignore)
204{
205 int res = 0;
206 DIR *d;
207
208 d = opendir(pathname);
209 if (d == NULL) {
Kenny Root50871522010-08-04 09:14:01 -0700210 LOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 return -errno;
212 }
213 res = _delete_dir_contents(d, ignore);
214 closedir(d);
215 if (also_delete_dir) {
216 if (rmdir(pathname)) {
Kenny Root50871522010-08-04 09:14:01 -0700217 LOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 res = -1;
219 }
220 }
221 return res;
222}
223
224int delete_dir_contents_fd(int dfd, const char *name)
225{
226 int fd, res;
227 DIR *d;
228
229 fd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
230 if (fd < 0) {
Kenny Root50871522010-08-04 09:14:01 -0700231 LOGE("Couldn't openat %s: %s\n", name, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 return -1;
233 }
234 d = fdopendir(fd);
235 if (d == NULL) {
Kenny Root50871522010-08-04 09:14:01 -0700236 LOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 close(fd);
238 return -1;
239 }
240 res = _delete_dir_contents(d, 0);
241 closedir(d);
242 return res;
243}
Kenny Root86c95842011-03-31 13:16:12 -0700244
245/**
246 * Checks whether a path points to a system app (.apk file). Returns 0
247 * if it is a system app or -1 if it is not.
248 */
249int validate_system_app_path(const char* path) {
250 size_t i;
251
252 for (i = 0; i < android_system_dirs.count; i++) {
253 const size_t dir_len = android_system_dirs.dirs[i].len;
254 if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) {
255 if (path[dir_len] == '.' || strchr(path + dir_len, '/') != NULL) {
256 LOGE("invalid system apk path '%s' (trickery)\n", path);
257 return -1;
258 }
259 return 0;
260 }
261 }
262
263 return -1;
264}
265
266/**
267 * Get the contents of a environment variable that contains a path. Caller
268 * owns the string that is inserted into the directory record. Returns
269 * 0 on success and -1 on error.
270 */
271int get_path_from_env(dir_rec_t* rec, const char* var) {
272 const char* path = getenv(var);
273 int ret = get_path_from_string(rec, path);
274 if (ret < 0) {
275 LOGW("Problem finding value for environment variable %s\n", var);
276 }
277 return ret;
278}
279
280/**
281 * Puts the string into the record as a directory. Appends '/' to the end
282 * of all paths. Caller owns the string that is inserted into the directory
283 * record. A null value will result in an error.
284 *
285 * Returns 0 on success and -1 on error.
286 */
287int get_path_from_string(dir_rec_t* rec, const char* path) {
288 if (path == NULL) {
289 return -1;
290 } else {
291 const size_t path_len = strlen(path);
292 if (path_len <= 0) {
293 return -1;
294 }
295
296 // Make sure path is absolute.
297 if (path[0] != '/') {
298 return -1;
299 }
300
301 if (path[path_len - 1] == '/') {
302 // Path ends with a forward slash. Make our own copy.
303
304 rec->path = strdup(path);
305 if (rec->path == NULL) {
306 return -1;
307 }
308
309 rec->len = path_len;
310 } else {
311 // Path does not end with a slash. Generate a new string.
312 char *dst;
313
314 // Add space for slash and terminating null.
315 size_t dst_size = path_len + 2;
316
317 rec->path = malloc(dst_size);
318 if (rec->path == NULL) {
319 return -1;
320 }
321
322 dst = rec->path;
323
324 if (append_and_increment(&dst, path, &dst_size) < 0
325 || append_and_increment(&dst, "/", &dst_size)) {
326 LOGE("Error canonicalizing path");
327 return -1;
328 }
329
330 rec->len = dst - rec->path;
331 }
332 }
333 return 0;
334}
335
336int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) {
337 dst->len = src->len + strlen(suffix);
338 const size_t dstSize = dst->len + 1;
339 dst->path = (char*) malloc(dstSize);
340
341 if (dst->path == NULL
342 || snprintf(dst->path, dstSize, "%s%s", src->path, suffix)
343 != (ssize_t) dst->len) {
344 LOGE("Could not allocate memory to hold appended path; aborting\n");
345 return -1;
346 }
347
348 return 0;
349}
350
351/**
352 * Check whether path points to a valid path for an APK file. An ASEC
353 * directory is allowed to have one level of subdirectory names. Returns -1
354 * when an invalid path is encountered and 0 when a valid path is encountered.
355 */
356int validate_apk_path(const char *path)
357{
358 int allowsubdir = 0;
359 char *subdir = NULL;
360 size_t dir_len;
361 size_t path_len;
362
363 if (!strncmp(path, android_app_dir.path, android_app_dir.len)) {
364 dir_len = android_app_dir.len;
365 } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) {
366 dir_len = android_app_private_dir.len;
367 } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) {
368 dir_len = android_asec_dir.len;
369 allowsubdir = 1;
370 } else {
371 LOGE("invalid apk path '%s' (bad prefix)\n", path);
372 return -1;
373 }
374
375 path_len = strlen(path);
376
377 /*
378 * Only allow the path to have a subdirectory if it's been marked as being allowed.
379 */
380 if ((subdir = strchr(path + dir_len, '/')) != NULL) {
381 ++subdir;
382 if (!allowsubdir
383 || (path_len > (size_t) (subdir - path) && (strchr(subdir, '/') != NULL))) {
384 LOGE("invalid apk path '%s' (subdir?)\n", path);
385 return -1;
386 }
387 }
388
389 /*
390 * Directories can't have a period directly after the directory markers
391 * to prevent ".."
392 */
393 if (path[dir_len] == '.'
394 || (subdir != NULL && ((*subdir == '.') || (strchr(subdir, '/') != NULL)))) {
395 LOGE("invalid apk path '%s' (trickery)\n", path);
396 return -1;
397 }
398
399 return 0;
400}
401
402int append_and_increment(char** dst, const char* src, size_t* dst_size) {
403 ssize_t ret = strlcpy(*dst, src, *dst_size);
404 if (ret < 0 || (size_t) ret >= *dst_size) {
405 return -1;
406 }
407 *dst += ret;
408 *dst_size -= ret;
409 return 0;
410}