blob: 89115704275e308bd3bd5409f42df89ebe7f7863 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070017#include <inttypes.h>
Nick Kralevichd7471292013-02-28 16:59:13 -080018#include <sys/capability.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070019#include "installd.h"
20#include <diskusage/dirsize.h>
21#include <selinux/android.h>
22
23/* Directory records that are used in execution of commands. */
24dir_rec_t android_data_dir;
25dir_rec_t android_asec_dir;
26dir_rec_t android_app_dir;
27dir_rec_t android_app_private_dir;
28dir_rec_t android_app_lib_dir;
29dir_rec_t android_media_dir;
30dir_rec_array_t android_system_dirs;
31
Robert Craig4d3fd4e2013-03-25 06:33:03 -040032int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070033{
34 char pkgdir[PKG_PATH_MAX];
35 char libsymlink[PKG_PATH_MAX];
36 char applibdir[PKG_PATH_MAX];
37 struct stat libStat;
38
39 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
40 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
41 return -1;
42 }
43
44 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
45 ALOGE("cannot create package path\n");
46 return -1;
47 }
48
49 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
50 ALOGE("cannot create package lib symlink origin path\n");
51 return -1;
52 }
53
54 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
55 ALOGE("cannot create package lib symlink dest path\n");
56 return -1;
57 }
58
Nick Kralevicha2d838a2013-01-09 16:00:35 -080059 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070060 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
61 return -1;
62 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080063 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070064 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
65 unlink(pkgdir);
66 return -1;
67 }
68
69 if (lstat(libsymlink, &libStat) < 0) {
70 if (errno != ENOENT) {
71 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
72 return -1;
73 }
74 } else {
75 if (S_ISDIR(libStat.st_mode)) {
76 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
77 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
78 return -1;
79 }
80 } else if (S_ISLNK(libStat.st_mode)) {
81 if (unlink(libsymlink) < 0) {
82 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
83 return -1;
84 }
85 }
86 }
87
Stephen Smalley26288202014-02-07 09:16:46 -050088 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070089 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
90 unlink(libsymlink);
91 unlink(pkgdir);
92 return -errno;
93 }
94
Stephen Smalley3a983892014-05-13 12:53:07 -040095 if (symlink(applibdir, libsymlink) < 0) {
96 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
97 strerror(errno));
98 unlink(pkgdir);
99 return -1;
100 }
101
Mike Lockwood94afecf2012-10-24 10:45:23 -0700102 if (chown(pkgdir, uid, gid) < 0) {
103 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
104 unlink(libsymlink);
105 unlink(pkgdir);
106 return -1;
107 }
108
109 return 0;
110}
111
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700112int uninstall(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700113{
114 char pkgdir[PKG_PATH_MAX];
115
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700116 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700117 return -1;
118
Dave Allisond9370732014-01-30 14:19:23 -0800119 remove_profile_file(pkgname);
120
Mike Lockwood94afecf2012-10-24 10:45:23 -0700121 /* delete contents AND directory, no exceptions */
122 return delete_dir_contents(pkgdir, 1, NULL);
123}
124
125int renamepkg(const char *oldpkgname, const char *newpkgname)
126{
127 char oldpkgdir[PKG_PATH_MAX];
128 char newpkgdir[PKG_PATH_MAX];
129
130 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
131 return -1;
132 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
133 return -1;
134
135 if (rename(oldpkgdir, newpkgdir) < 0) {
136 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
137 return -errno;
138 }
139 return 0;
140}
141
142int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
143{
144 char pkgdir[PKG_PATH_MAX];
145 struct stat s;
146 int rc = 0;
147
148 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
149 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
150 return -1;
151 }
152
153 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
154 ALOGE("cannot create package path\n");
155 return -1;
156 }
157
158 if (stat(pkgdir, &s) < 0) return -1;
159
160 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700161 ALOGE("fixing uid of non-root pkg: %s %" PRIu32 " %" PRIu32 "\n", pkgdir, s.st_uid, s.st_gid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700162 return -1;
163 }
164
165 if (chmod(pkgdir, 0751) < 0) {
166 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
167 unlink(pkgdir);
168 return -errno;
169 }
170 if (chown(pkgdir, uid, gid) < 0) {
171 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
172 unlink(pkgdir);
173 return -errno;
174 }
175
176 return 0;
177}
178
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700179int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180{
181 char pkgdir[PKG_PATH_MAX];
182
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700183 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184 return -1;
185
186 /* delete contents, excluding "lib", but not the directory itself */
187 return delete_dir_contents(pkgdir, 0, "lib");
188}
189
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700190int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700191{
192 char pkgdir[PKG_PATH_MAX];
193 char applibdir[PKG_PATH_MAX];
194 char libsymlink[PKG_PATH_MAX];
195 struct stat libStat;
196
197 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700198 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700199 return -1;
200 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700201 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700202 ALOGE("cannot create package lib symlink origin path\n");
203 return -1;
204 }
205 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
206 ALOGE("cannot create package lib symlink dest path\n");
207 return -1;
208 }
209
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800210 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700211 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
212 return -errno;
213 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800214 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700215 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
216 unlink(pkgdir);
217 return -errno;
218 }
219
220 if (lstat(libsymlink, &libStat) < 0) {
221 if (errno != ENOENT) {
222 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
223 unlink(pkgdir);
224 return -1;
225 }
226 } else {
227 if (S_ISDIR(libStat.st_mode)) {
228 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
229 ALOGE("couldn't delete lib directory during install for non-primary: %s",
230 libsymlink);
231 unlink(pkgdir);
232 return -1;
233 }
234 } else if (S_ISLNK(libStat.st_mode)) {
235 if (unlink(libsymlink) < 0) {
236 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
237 libsymlink);
238 unlink(pkgdir);
239 return -1;
240 }
241 }
242 }
243
Stephen Smalley26288202014-02-07 09:16:46 -0500244 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
246 unlink(libsymlink);
247 unlink(pkgdir);
248 return -errno;
249 }
250
Stephen Smalley3a983892014-05-13 12:53:07 -0400251 if (symlink(applibdir, libsymlink) < 0) {
252 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
253 applibdir, strerror(errno));
254 unlink(pkgdir);
255 return -1;
256 }
257
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258 if (chown(pkgdir, uid, uid) < 0) {
259 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
260 unlink(libsymlink);
261 unlink(pkgdir);
262 return -errno;
263 }
264
265 return 0;
266}
267
Robin Lee095c7632014-04-25 15:05:19 +0100268int create_user(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269{
Robin Lee095c7632014-04-25 15:05:19 +0100270 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700271 return -1;
272 }
273
274 return 0;
275}
276
Robin Lee095c7632014-04-25 15:05:19 +0100277int delete_user(userid_t userid)
278{
279 int status = 0;
280
281 char data_path[PKG_PATH_MAX];
282 if ((create_user_path(data_path, userid) != 0)
283 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
284 status = -1;
285 }
286
287 char media_path[PATH_MAX];
288 if ((create_user_media_path(media_path, userid) != 0)
289 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
290 status = -1;
291 }
292
293 char config_path[PATH_MAX];
294 if ((create_user_config_path(config_path, userid) != 0)
295 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
296 status = -1;
297 }
298
299 return status;
300}
301
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700302int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700303{
304 char cachedir[PKG_PATH_MAX];
305
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700306 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307 return -1;
308
309 /* delete contents, not the directory, no exceptions */
310 return delete_dir_contents(cachedir, 0, 0);
311}
312
313/* Try to ensure free_size bytes of storage are available.
314 * Returns 0 on success.
315 * This is rather simple-minded because doing a full LRU would
316 * be potentially memory-intensive, and without atime it would
317 * also require that apps constantly modify file metadata even
318 * when just reading from the cache, which is pretty awful.
319 */
320int free_cache(int64_t free_size)
321{
322 cache_t* cache;
323 int64_t avail;
324 DIR *d;
325 struct dirent *de;
326 char tmpdir[PATH_MAX];
327 char *dirpos;
328
329 avail = data_disk_free();
330 if (avail < 0) return -1;
331
332 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
333 if (avail >= free_size) return 0;
334
335 cache = start_cache_collection();
336
337 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700338 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700339 //ALOGI("adding cache files from %s\n", tmpdir);
340 add_cache_files(cache, tmpdir, "cache");
341 }
342
343 // Search for other users and add any cache files from them.
344 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
345 SECONDARY_USER_PREFIX);
346 dirpos = tmpdir + strlen(tmpdir);
347 d = opendir(tmpdir);
348 if (d != NULL) {
349 while ((de = readdir(d))) {
350 if (de->d_type == DT_DIR) {
351 const char *name = de->d_name;
352 /* always skip "." and ".." */
353 if (name[0] == '.') {
354 if (name[1] == 0) continue;
355 if ((name[1] == '.') && (name[2] == 0)) continue;
356 }
357 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
358 strcpy(dirpos, name);
359 //ALOGI("adding cache files from %s\n", tmpdir);
360 add_cache_files(cache, tmpdir, "cache");
361 } else {
362 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
363 }
364 }
365 }
366 closedir(d);
367 }
368
369 // Collect cache files on external storage for all users (if it is mounted as part
370 // of the internal storage).
371 strcpy(tmpdir, android_media_dir.path);
372 dirpos = tmpdir + strlen(tmpdir);
373 d = opendir(tmpdir);
374 if (d != NULL) {
375 while ((de = readdir(d))) {
376 if (de->d_type == DT_DIR) {
377 const char *name = de->d_name;
378 /* skip any dir that doesn't start with a number, so not a user */
379 if (name[0] < '0' || name[0] > '9') {
380 continue;
381 }
382 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
383 strcpy(dirpos, name);
384 if (lookup_media_dir(tmpdir, "Android") == 0
385 && lookup_media_dir(tmpdir, "data") == 0) {
386 //ALOGI("adding cache files from %s\n", tmpdir);
387 add_cache_files(cache, tmpdir, "cache");
388 }
389 } else {
390 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
391 }
392 }
393 }
394 closedir(d);
395 }
396
397 clear_cache_files(cache, free_size);
398 finish_cache_collection(cache);
399
400 return data_disk_free() >= free_size ? 0 : -1;
401}
402
Narayan Kamath1b400322014-04-11 13:17:00 +0100403int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700404{
405 char src_dex[PKG_PATH_MAX];
406 char dst_dex[PKG_PATH_MAX];
407
408 if (validate_apk_path(src)) return -1;
409 if (validate_apk_path(dst)) return -1;
410
Narayan Kamath1b400322014-04-11 13:17:00 +0100411 if (create_cache_path(src_dex, src, instruction_set)) return -1;
412 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700413
414 ALOGV("move %s -> %s\n", src_dex, dst_dex);
415 if (rename(src_dex, dst_dex) < 0) {
416 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
417 return -1;
418 } else {
419 return 0;
420 }
421}
422
Narayan Kamath1b400322014-04-11 13:17:00 +0100423int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700424{
425 char dex_path[PKG_PATH_MAX];
426
427 if (validate_apk_path(path)) return -1;
Narayan Kamath1b400322014-04-11 13:17:00 +0100428 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700429
430 ALOGV("unlink %s\n", dex_path);
431 if (unlink(dex_path) < 0) {
432 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
433 return -1;
434 } else {
435 return 0;
436 }
437}
438
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700439int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700440 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100441 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
442 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700443{
444 DIR *d;
445 int dfd;
446 struct dirent *de;
447 struct stat s;
448 char path[PKG_PATH_MAX];
449
450 int64_t codesize = 0;
451 int64_t datasize = 0;
452 int64_t cachesize = 0;
453 int64_t asecsize = 0;
454
455 /* count the source apk as code -- but only if it's not
456 * on the /system partition and its not on the sdcard.
457 */
458 if (validate_system_app_path(apkpath) &&
459 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
460 if (stat(apkpath, &s) == 0) {
461 codesize += stat_size(&s);
462 }
463 }
464 /* count the forward locked apk as code if it is given
465 */
466 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
467 if (stat(fwdlock_apkpath, &s) == 0) {
468 codesize += stat_size(&s);
469 }
470 }
471 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100472 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700473 if (stat(path, &s) == 0) {
474 codesize += stat_size(&s);
475 }
476 }
477
478 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700479 if (libdirpath != NULL && libdirpath[0] != '!') {
480 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700481 if (d != NULL) {
482 dfd = dirfd(d);
483 codesize += calculate_dir_size(dfd);
484 closedir(d);
485 }
486 }
487
488 /* compute asec size if it is given
489 */
490 if (asecpath != NULL && asecpath[0] != '!') {
491 if (stat(asecpath, &s) == 0) {
492 asecsize += stat_size(&s);
493 }
494 }
495
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700496 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700497 goto done;
498 }
499
500 d = opendir(path);
501 if (d == NULL) {
502 goto done;
503 }
504 dfd = dirfd(d);
505
506 /* most stuff in the pkgdir is data, except for the "cache"
507 * directory and below, which is cache, and the "lib" directory
508 * and below, which is code...
509 */
510 while ((de = readdir(d))) {
511 const char *name = de->d_name;
512
513 if (de->d_type == DT_DIR) {
514 int subfd;
515 int64_t statsize = 0;
516 int64_t dirsize = 0;
517 /* always skip "." and ".." */
518 if (name[0] == '.') {
519 if (name[1] == 0) continue;
520 if ((name[1] == '.') && (name[2] == 0)) continue;
521 }
522 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
523 statsize = stat_size(&s);
524 }
525 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
526 if (subfd >= 0) {
527 dirsize = calculate_dir_size(subfd);
528 }
529 if(!strcmp(name,"lib")) {
530 codesize += dirsize + statsize;
531 } else if(!strcmp(name,"cache")) {
532 cachesize += dirsize + statsize;
533 } else {
534 datasize += dirsize + statsize;
535 }
536 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
537 // This is the symbolic link to the application's library
538 // code. We'll count this as code instead of data, since
539 // it is not something that the app creates.
540 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
541 codesize += stat_size(&s);
542 }
543 } else {
544 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
545 datasize += stat_size(&s);
546 }
547 }
548 }
549 closedir(d);
550done:
551 *_codesize = codesize;
552 *_datasize = datasize;
553 *_cachesize = cachesize;
554 *_asecsize = asecsize;
555 return 0;
556}
557
558
Narayan Kamath1b400322014-04-11 13:17:00 +0100559int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700560{
561 char *tmp;
562 int srclen;
563 int dstlen;
564
565 srclen = strlen(src);
566
567 /* demand that we are an absolute path */
568 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
569 return -1;
570 }
571
572 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
573 return -1;
574 }
575
Dave Allisond9370732014-01-30 14:19:23 -0800576 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100577 strlen(instruction_set) +
578 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800579
Mike Lockwood94afecf2012-10-24 10:45:23 -0700580 if (dstlen > PKG_PATH_MAX) {
581 return -1;
582 }
583
Narayan Kamath1b400322014-04-11 13:17:00 +0100584 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700585 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100586 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700587 src + 1, /* skip the leading / */
588 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800589
Narayan Kamath1b400322014-04-11 13:17:00 +0100590 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700591 if (*tmp == '/') {
592 *tmp = '@';
593 }
594 }
595
596 return 0;
597}
598
599static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800600 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700601{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800602 /* platform-specific flags affecting optimization and verification */
603 char dexopt_flags[PROPERTY_VALUE_MAX];
604 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
605 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
606
Mike Lockwood94afecf2012-10-24 10:45:23 -0700607 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
608 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
609 char zip_num[MAX_INT_LEN];
610 char odex_num[MAX_INT_LEN];
611
612 sprintf(zip_num, "%d", zip_fd);
613 sprintf(odex_num, "%d", odex_fd);
614
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700615 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700616 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
617 dexopt_flags, (char*) NULL);
618 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
619}
620
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700621static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Narayan Kamath1b400322014-04-11 13:17:00 +0100622 const char* output_file_name, const char *pkgname, const char *instruction_set)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700623{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800624 char dex2oat_flags[PROPERTY_VALUE_MAX];
625 property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, "");
626 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
627
Calin Juravle57c69c32014-06-06 14:42:16 +0100628 char profiler_prop[PROPERTY_VALUE_MAX];
629 bool profiler = property_get("dalvik.vm.profiler", profiler_prop, "0")
630 && (profiler_prop[0] == '1');
631
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700632 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
633 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100634 static const unsigned int MAX_INSTRUCTION_SET_LEN = 32;
635
636 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
637 ALOGE("Instruction set %s longer than max length of %d",
638 instruction_set, MAX_INSTRUCTION_SET_LEN);
639 return;
640 }
641
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700642 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
643 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
644 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
645 char oat_location_arg[strlen("--oat-name=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100646 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
647 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700648
649 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
650 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
651 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
652 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100653 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Calin Juravle57c69c32014-06-06 14:42:16 +0100654
655 if (profiler && (strcmp(pkgname, "*") != 0)) {
656 char profile_file[PKG_PATH_MAX];
657 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800658 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100659 struct stat st;
660 if (stat(profile_file, &st) == -1) {
661 strcpy(profile_file_arg, "--no-profile-file");
662 } else {
663 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
664 }
Dave Allisond9370732014-01-30 14:19:23 -0800665 } else {
Narayan Kamath1b400322014-04-11 13:17:00 +0100666 strcpy(profile_file_arg, "--no-profile-file");
Dave Allisond9370732014-01-30 14:19:23 -0800667 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700668
669 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
670 execl(DEX2OAT_BIN, DEX2OAT_BIN,
671 zip_fd_arg, zip_location_arg,
672 oat_fd_arg, oat_location_arg,
Narayan Kamath1b400322014-04-11 13:17:00 +0100673 profile_file_arg, instruction_set_arg,
Anwar Ghuloum4bc05402014-03-11 15:42:58 -0700674 strlen(dex2oat_flags) > 0 ? dex2oat_flags : NULL,
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700675 (char*) NULL);
676 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
677}
678
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100679static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700680{
681 int status;
682 pid_t got_pid;
683
Mike Lockwood94afecf2012-10-24 10:45:23 -0700684 while (1) {
685 got_pid = waitpid(pid, &status, 0);
686 if (got_pid == -1 && errno == EINTR) {
687 printf("waitpid interrupted, retrying\n");
688 } else {
689 break;
690 }
691 }
692 if (got_pid != pid) {
693 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
694 (int) pid, (int) got_pid, strerror(errno));
695 return 1;
696 }
697
698 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700699 return 0;
700 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700701 return status; /* always nonzero */
702 }
703}
704
Dave Allisond9370732014-01-30 14:19:23 -0800705int dexopt(const char *apk_path, uid_t uid, int is_public,
Narayan Kamath1b400322014-04-11 13:17:00 +0100706 const char *pkgname, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700707{
708 struct utimbuf ut;
709 struct stat apk_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700710 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700711 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700712 char *end;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700713 int res, zip_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700714
Mike Lockwood94afecf2012-10-24 10:45:23 -0700715 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
716 return -1;
717 }
718
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800719 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom856bc782014-05-28 14:31:47 -0700720 property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700721
722 /* Before anything else: is there a .odex file? If so, we have
723 * precompiled the apk and there is nothing to do here.
724 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800725 strcpy(out_path, apk_path);
726 end = strrchr(out_path, '.');
727 if (end != NULL) {
728 strcpy(end, ".odex");
729 if (stat(out_path, &dex_stat) == 0) {
730 return 0;
731 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700732 }
733
Narayan Kamath1b400322014-04-11 13:17:00 +0100734 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700735 return -1;
736 }
737
738 memset(&apk_stat, 0, sizeof(apk_stat));
739 stat(apk_path, &apk_stat);
740
741 zip_fd = open(apk_path, O_RDONLY, 0);
742 if (zip_fd < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700743 ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700744 return -1;
745 }
746
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700747 unlink(out_path);
748 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
749 if (out_fd < 0) {
750 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700751 goto fail;
752 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700753 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700754 S_IRUSR|S_IWUSR|S_IRGRP |
755 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700756 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700757 goto fail;
758 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700759 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
760 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700761 goto fail;
762 }
763
Dave Allisond9370732014-01-30 14:19:23 -0800764 // Create profile file if there is a package name present.
765 if (strcmp(pkgname, "*") != 0) {
766 create_profile_file(pkgname, uid);
767 }
768
769
Mike Lockwood94afecf2012-10-24 10:45:23 -0700770 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
771
772 pid_t pid;
773 pid = fork();
774 if (pid == 0) {
775 /* child -- drop privileges before continuing */
776 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700777 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700778 exit(64);
779 }
780 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700781 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700782 exit(65);
783 }
784 // drop capabilities
785 struct __user_cap_header_struct capheader;
786 struct __user_cap_data_struct capdata[2];
787 memset(&capheader, 0, sizeof(capheader));
788 memset(&capdata, 0, sizeof(capdata));
789 capheader.version = _LINUX_CAPABILITY_VERSION_3;
790 if (capset(&capheader, &capdata[0]) < 0) {
791 ALOGE("capset failed: %s\n", strerror(errno));
792 exit(66);
793 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700794 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
795 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700796 exit(67);
797 }
798
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700799 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800800 run_dexopt(zip_fd, out_fd, apk_path, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700801 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Narayan Kamath1b400322014-04-11 13:17:00 +0100802 run_dex2oat(zip_fd, out_fd, apk_path, out_path, pkgname, instruction_set);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700803 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700804 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700805 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700806 exit(68); /* only get here on exec failure */
807 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100808 res = wait_child(pid);
809 if (res == 0) {
810 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
811 } else {
812 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", apk_path, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700813 goto fail;
814 }
815 }
816
817 ut.actime = apk_stat.st_atime;
818 ut.modtime = apk_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700819 utime(out_path, &ut);
820
821 close(out_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700822 close(zip_fd);
823 return 0;
824
825fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700826 if (out_fd >= 0) {
827 close(out_fd);
828 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700829 }
830 if (zip_fd >= 0) {
831 close(zip_fd);
832 }
833 return -1;
834}
835
836void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
837 struct stat* statbuf)
838{
839 while (path[basepos] != 0) {
840 if (path[basepos] == '/') {
841 path[basepos] = 0;
842 if (lstat(path, statbuf) < 0) {
843 ALOGV("Making directory: %s\n", path);
844 if (mkdir(path, mode) == 0) {
845 chown(path, uid, gid);
846 } else {
847 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
848 }
849 }
850 path[basepos] = '/';
851 basepos++;
852 }
853 basepos++;
854 }
855}
856
857int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
858 int dstuid, int dstgid, struct stat* statbuf)
859{
860 DIR *d;
861 struct dirent *de;
862 int res;
863
864 int srcend = strlen(srcpath);
865 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -0800866
Mike Lockwood94afecf2012-10-24 10:45:23 -0700867 if (lstat(srcpath, statbuf) < 0) {
868 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
869 return 1;
870 }
Dave Allisond9370732014-01-30 14:19:23 -0800871
Mike Lockwood94afecf2012-10-24 10:45:23 -0700872 if ((statbuf->st_mode&S_IFDIR) == 0) {
873 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
874 dstuid, dstgid, statbuf);
875 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
876 if (rename(srcpath, dstpath) >= 0) {
877 if (chown(dstpath, dstuid, dstgid) < 0) {
878 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
879 unlink(dstpath);
880 return 1;
881 }
882 } else {
883 ALOGW("Unable to rename %s to %s: %s\n",
884 srcpath, dstpath, strerror(errno));
885 return 1;
886 }
887 return 0;
888 }
889
890 d = opendir(srcpath);
891 if (d == NULL) {
892 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
893 return 1;
894 }
895
896 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -0800897
Mike Lockwood94afecf2012-10-24 10:45:23 -0700898 while ((de = readdir(d))) {
899 const char *name = de->d_name;
900 /* always skip "." and ".." */
901 if (name[0] == '.') {
902 if (name[1] == 0) continue;
903 if ((name[1] == '.') && (name[2] == 0)) continue;
904 }
Dave Allisond9370732014-01-30 14:19:23 -0800905
Mike Lockwood94afecf2012-10-24 10:45:23 -0700906 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
907 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
908 continue;
909 }
Dave Allisond9370732014-01-30 14:19:23 -0800910
Mike Lockwood94afecf2012-10-24 10:45:23 -0700911 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
912 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
913 continue;
914 }
Dave Allisond9370732014-01-30 14:19:23 -0800915
Mike Lockwood94afecf2012-10-24 10:45:23 -0700916 srcpath[srcend] = dstpath[dstend] = '/';
917 strcpy(srcpath+srcend+1, name);
918 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -0800919
Mike Lockwood94afecf2012-10-24 10:45:23 -0700920 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
921 res = 1;
922 }
Dave Allisond9370732014-01-30 14:19:23 -0800923
Mike Lockwood94afecf2012-10-24 10:45:23 -0700924 // Note: we will be leaving empty directories behind in srcpath,
925 // but that is okay, the package manager will be erasing all of the
926 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -0800927
Mike Lockwood94afecf2012-10-24 10:45:23 -0700928 srcpath[srcend] = dstpath[dstend] = 0;
929 }
Dave Allisond9370732014-01-30 14:19:23 -0800930
Mike Lockwood94afecf2012-10-24 10:45:23 -0700931 closedir(d);
932 return res;
933}
934
935int movefiles()
936{
937 DIR *d;
938 int dfd, subfd;
939 struct dirent *de;
940 struct stat s;
941 char buf[PKG_PATH_MAX+1];
942 int bufp, bufe, bufi, readlen;
943
944 char srcpkg[PKG_NAME_MAX];
945 char dstpkg[PKG_NAME_MAX];
946 char srcpath[PKG_PATH_MAX];
947 char dstpath[PKG_PATH_MAX];
948 int dstuid=-1, dstgid=-1;
949 int hasspace;
950
951 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
952 if (d == NULL) {
953 goto done;
954 }
955 dfd = dirfd(d);
956
957 /* Iterate through all files in the directory, executing the
958 * file movements requested there-in.
959 */
960 while ((de = readdir(d))) {
961 const char *name = de->d_name;
962
963 if (de->d_type == DT_DIR) {
964 continue;
965 } else {
966 subfd = openat(dfd, name, O_RDONLY);
967 if (subfd < 0) {
968 ALOGW("Unable to open update commands at %s%s\n",
969 UPDATE_COMMANDS_DIR_PREFIX, name);
970 continue;
971 }
Dave Allisond9370732014-01-30 14:19:23 -0800972
Mike Lockwood94afecf2012-10-24 10:45:23 -0700973 bufp = 0;
974 bufe = 0;
975 buf[PKG_PATH_MAX] = 0;
976 srcpkg[0] = dstpkg[0] = 0;
977 while (1) {
978 bufi = bufp;
979 while (bufi < bufe && buf[bufi] != '\n') {
980 bufi++;
981 }
982 if (bufi < bufe) {
983 buf[bufi] = 0;
984 ALOGV("Processing line: %s\n", buf+bufp);
985 hasspace = 0;
986 while (bufp < bufi && isspace(buf[bufp])) {
987 hasspace = 1;
988 bufp++;
989 }
990 if (buf[bufp] == '#' || bufp == bufi) {
991 // skip comments and empty lines.
992 } else if (hasspace) {
993 if (dstpkg[0] == 0) {
994 ALOGW("Path before package line in %s%s: %s\n",
995 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
996 } else if (srcpkg[0] == 0) {
997 // Skip -- source package no longer exists.
998 } else {
999 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1000 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1001 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1002 movefileordir(srcpath, dstpath,
1003 strlen(dstpath)-strlen(buf+bufp),
1004 dstuid, dstgid, &s);
1005 }
1006 }
1007 } else {
1008 char* div = strchr(buf+bufp, ':');
1009 if (div == NULL) {
1010 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1011 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1012 } else {
1013 *div = 0;
1014 div++;
1015 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1016 strcpy(dstpkg, buf+bufp);
1017 } else {
1018 srcpkg[0] = dstpkg[0] = 0;
1019 ALOGW("Package name too long in %s%s: %s\n",
1020 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1021 }
1022 if (strlen(div) < PKG_NAME_MAX) {
1023 strcpy(srcpkg, div);
1024 } else {
1025 srcpkg[0] = dstpkg[0] = 0;
1026 ALOGW("Package name too long in %s%s: %s\n",
1027 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1028 }
1029 if (srcpkg[0] != 0) {
1030 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1031 if (lstat(srcpath, &s) < 0) {
1032 // Package no longer exists -- skip.
1033 srcpkg[0] = 0;
1034 }
1035 } else {
1036 srcpkg[0] = 0;
1037 ALOGW("Can't create path %s in %s%s\n",
1038 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1039 }
1040 if (srcpkg[0] != 0) {
1041 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1042 if (lstat(dstpath, &s) == 0) {
1043 dstuid = s.st_uid;
1044 dstgid = s.st_gid;
1045 } else {
1046 // Destination package doesn't
1047 // exist... due to original-package,
1048 // this is normal, so don't be
1049 // noisy about it.
1050 srcpkg[0] = 0;
1051 }
1052 } else {
1053 srcpkg[0] = 0;
1054 ALOGW("Can't create path %s in %s%s\n",
1055 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1056 }
1057 }
1058 ALOGV("Transfering from %s to %s: uid=%d\n",
1059 srcpkg, dstpkg, dstuid);
1060 }
1061 }
1062 }
1063 bufp = bufi+1;
1064 } else {
1065 if (bufp == 0) {
1066 if (bufp < bufe) {
1067 ALOGW("Line too long in %s%s, skipping: %s\n",
1068 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1069 }
1070 } else if (bufp < bufe) {
1071 memcpy(buf, buf+bufp, bufe-bufp);
1072 bufe -= bufp;
1073 bufp = 0;
1074 }
1075 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1076 if (readlen < 0) {
1077 ALOGW("Failure reading update commands in %s%s: %s\n",
1078 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1079 break;
1080 } else if (readlen == 0) {
1081 break;
1082 }
1083 bufe += readlen;
1084 buf[bufe] = 0;
1085 ALOGV("Read buf: %s\n", buf);
1086 }
1087 }
1088 close(subfd);
1089 }
1090 }
1091 closedir(d);
1092done:
1093 return 0;
1094}
1095
1096int linklib(const char* pkgname, const char* asecLibDir, int userId)
1097{
1098 char pkgdir[PKG_PATH_MAX];
1099 char libsymlink[PKG_PATH_MAX];
1100 struct stat s, libStat;
1101 int rc = 0;
1102
1103 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1104 ALOGE("cannot create package path\n");
1105 return -1;
1106 }
1107 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1108 ALOGE("cannot create package lib symlink origin path\n");
1109 return -1;
1110 }
1111
1112 if (stat(pkgdir, &s) < 0) return -1;
1113
1114 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1115 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1116 return -1;
1117 }
1118
1119 if (chmod(pkgdir, 0700) < 0) {
1120 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1121 rc = -1;
1122 goto out;
1123 }
1124
1125 if (lstat(libsymlink, &libStat) < 0) {
1126 if (errno != ENOENT) {
1127 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1128 rc = -1;
1129 goto out;
1130 }
1131 } else {
1132 if (S_ISDIR(libStat.st_mode)) {
1133 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1134 rc = -1;
1135 goto out;
1136 }
1137 } else if (S_ISLNK(libStat.st_mode)) {
1138 if (unlink(libsymlink) < 0) {
1139 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1140 rc = -1;
1141 goto out;
1142 }
1143 }
1144 }
1145
1146 if (symlink(asecLibDir, libsymlink) < 0) {
1147 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1148 strerror(errno));
1149 rc = -errno;
1150 goto out;
1151 }
1152
1153out:
1154 if (chmod(pkgdir, s.st_mode) < 0) {
1155 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1156 rc = -errno;
1157 }
1158
1159 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1160 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1161 return -errno;
1162 }
1163
1164 return rc;
1165}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001166
1167static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1168{
1169 static const char *IDMAP_BIN = "/system/bin/idmap";
1170 static const size_t MAX_INT_LEN = 32;
1171 char idmap_str[MAX_INT_LEN];
1172
1173 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1174
1175 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1176 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1177}
1178
1179// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1180// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1181static int flatten_path(const char *prefix, const char *suffix,
1182 const char *overlay_path, char *idmap_path, size_t N)
1183{
1184 if (overlay_path == NULL || idmap_path == NULL) {
1185 return -1;
1186 }
1187 const size_t len_overlay_path = strlen(overlay_path);
1188 // will access overlay_path + 1 further below; requires absolute path
1189 if (len_overlay_path < 2 || *overlay_path != '/') {
1190 return -1;
1191 }
1192 const size_t len_idmap_root = strlen(prefix);
1193 const size_t len_suffix = strlen(suffix);
1194 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1195 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1196 // additions below would cause overflow
1197 return -1;
1198 }
1199 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1200 return -1;
1201 }
1202 memset(idmap_path, 0, N);
1203 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1204 char *ch = idmap_path + len_idmap_root;
1205 while (*ch != '\0') {
1206 if (*ch == '/') {
1207 *ch = '@';
1208 }
1209 ++ch;
1210 }
1211 return 0;
1212}
1213
1214int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1215{
1216 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1217
1218 int idmap_fd = -1;
1219 char idmap_path[PATH_MAX];
1220
1221 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1222 idmap_path, sizeof(idmap_path)) == -1) {
1223 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1224 goto fail;
1225 }
1226
1227 unlink(idmap_path);
1228 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1229 if (idmap_fd < 0) {
1230 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1231 goto fail;
1232 }
1233 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1234 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1235 goto fail;
1236 }
1237 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1238 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1239 goto fail;
1240 }
1241
1242 pid_t pid;
1243 pid = fork();
1244 if (pid == 0) {
1245 /* child -- drop privileges before continuing */
1246 if (setgid(uid) != 0) {
1247 ALOGE("setgid(%d) failed during idmap\n", uid);
1248 exit(1);
1249 }
1250 if (setuid(uid) != 0) {
1251 ALOGE("setuid(%d) failed during idmap\n", uid);
1252 exit(1);
1253 }
1254 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1255 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1256 exit(1);
1257 }
1258
1259 run_idmap(target_apk, overlay_apk, idmap_fd);
1260 exit(1); /* only if exec call to idmap failed */
1261 } else {
1262 int status = wait_child(pid);
1263 if (status != 0) {
1264 ALOGE("idmap failed, status=0x%04x\n", status);
1265 goto fail;
1266 }
1267 }
1268
1269 close(idmap_fd);
1270 return 0;
1271fail:
1272 if (idmap_fd >= 0) {
1273 close(idmap_fd);
1274 unlink(idmap_path);
1275 }
1276 return -1;
1277}
Robert Craige9887e42014-02-20 10:25:56 -05001278
Robert Craigda30dc72014-03-27 10:21:12 -04001279int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001280{
Robert Craigda30dc72014-03-27 10:21:12 -04001281 struct dirent *entry;
1282 DIR *d;
1283 struct stat s;
1284 char *userdir;
1285 char *primarydir;
1286 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001287 int ret = 0;
1288
Robert Craigda30dc72014-03-27 10:21:12 -04001289 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1290 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1291
1292 if (!pkgName || !seinfo) {
1293 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001294 return -1;
1295 }
1296
Robert Craigda30dc72014-03-27 10:21:12 -04001297 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1298 return -1;
1299 }
1300
1301 // Relabel for primary user.
1302 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1303 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001304 ret |= -1;
1305 }
1306
Robert Craigda30dc72014-03-27 10:21:12 -04001307 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1308 free(primarydir);
1309 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001310 }
1311
Robert Craigda30dc72014-03-27 10:21:12 -04001312 // Relabel package directory for all secondary users.
1313 d = opendir(userdir);
1314 if (d == NULL) {
1315 free(primarydir);
1316 free(userdir);
1317 return -1;
1318 }
1319
1320 while ((entry = readdir(d))) {
1321 if (entry->d_type != DT_DIR) {
1322 continue;
1323 }
1324
1325 const char *user = entry->d_name;
1326 // Ignore "." and ".."
1327 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1328 continue;
1329 }
1330
1331 // user directories start with a number
1332 if (user[0] < '0' || user[0] > '9') {
1333 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1334 continue;
1335 }
1336
1337 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1338 continue;
1339 }
1340
1341 if (stat(pkgdir, &s) < 0) {
1342 free(pkgdir);
1343 continue;
1344 }
1345
1346 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) {
1347 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1348 ret |= -1;
1349 }
1350 free(pkgdir);
1351 }
1352
1353 closedir(d);
1354 free(primarydir);
1355 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001356 return ret;
1357}