blob: 10244ac764e43899b80e0e70437164225df8edc9 [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
Nick Kralevichd7471292013-02-28 16:59:13 -080017#include <sys/capability.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070018#include "installd.h"
19#include <diskusage/dirsize.h>
20#include <selinux/android.h>
21
22/* Directory records that are used in execution of commands. */
23dir_rec_t android_data_dir;
24dir_rec_t android_asec_dir;
25dir_rec_t android_app_dir;
26dir_rec_t android_app_private_dir;
27dir_rec_t android_app_lib_dir;
28dir_rec_t android_media_dir;
29dir_rec_array_t android_system_dirs;
30
Robert Craig4d3fd4e2013-03-25 06:33:03 -040031int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070032{
33 char pkgdir[PKG_PATH_MAX];
34 char libsymlink[PKG_PATH_MAX];
35 char applibdir[PKG_PATH_MAX];
36 struct stat libStat;
37
38 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
39 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
40 return -1;
41 }
42
43 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
44 ALOGE("cannot create package path\n");
45 return -1;
46 }
47
48 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
49 ALOGE("cannot create package lib symlink origin path\n");
50 return -1;
51 }
52
53 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
54 ALOGE("cannot create package lib symlink dest path\n");
55 return -1;
56 }
57
Nick Kralevicha2d838a2013-01-09 16:00:35 -080058 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070059 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
60 return -1;
61 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080062 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070063 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
64 unlink(pkgdir);
65 return -1;
66 }
67
68 if (lstat(libsymlink, &libStat) < 0) {
69 if (errno != ENOENT) {
70 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
71 return -1;
72 }
73 } else {
74 if (S_ISDIR(libStat.st_mode)) {
75 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
76 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
77 return -1;
78 }
79 } else if (S_ISLNK(libStat.st_mode)) {
80 if (unlink(libsymlink) < 0) {
81 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
82 return -1;
83 }
84 }
85 }
86
87 if (symlink(applibdir, libsymlink) < 0) {
88 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
89 strerror(errno));
90 unlink(pkgdir);
91 return -1;
92 }
93
Stephen Smalley26288202014-02-07 09:16:46 -050094 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070095 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
96 unlink(libsymlink);
97 unlink(pkgdir);
98 return -errno;
99 }
100
101 if (chown(pkgdir, uid, gid) < 0) {
102 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
103 unlink(libsymlink);
104 unlink(pkgdir);
105 return -1;
106 }
107
108 return 0;
109}
110
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700111int uninstall(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112{
113 char pkgdir[PKG_PATH_MAX];
114
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700115 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700116 return -1;
117
Dave Allisond9370732014-01-30 14:19:23 -0800118 remove_profile_file(pkgname);
119
Mike Lockwood94afecf2012-10-24 10:45:23 -0700120 /* delete contents AND directory, no exceptions */
121 return delete_dir_contents(pkgdir, 1, NULL);
122}
123
124int renamepkg(const char *oldpkgname, const char *newpkgname)
125{
126 char oldpkgdir[PKG_PATH_MAX];
127 char newpkgdir[PKG_PATH_MAX];
128
129 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
130 return -1;
131 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
132 return -1;
133
134 if (rename(oldpkgdir, newpkgdir) < 0) {
135 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
136 return -errno;
137 }
138 return 0;
139}
140
141int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
142{
143 char pkgdir[PKG_PATH_MAX];
144 struct stat s;
145 int rc = 0;
146
147 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
148 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
149 return -1;
150 }
151
152 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
153 ALOGE("cannot create package path\n");
154 return -1;
155 }
156
157 if (stat(pkgdir, &s) < 0) return -1;
158
159 if (s.st_uid != 0 || s.st_gid != 0) {
160 ALOGE("fixing uid of non-root pkg: %s %lu %lu\n", pkgdir, s.st_uid, s.st_gid);
161 return -1;
162 }
163
164 if (chmod(pkgdir, 0751) < 0) {
165 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
166 unlink(pkgdir);
167 return -errno;
168 }
169 if (chown(pkgdir, uid, gid) < 0) {
170 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
171 unlink(pkgdir);
172 return -errno;
173 }
174
175 return 0;
176}
177
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700178int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700179{
180 char pkgdir[PKG_PATH_MAX];
181
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700182 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700183 return -1;
184
185 /* delete contents, excluding "lib", but not the directory itself */
186 return delete_dir_contents(pkgdir, 0, "lib");
187}
188
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700189int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700190{
191 char pkgdir[PKG_PATH_MAX];
192 char applibdir[PKG_PATH_MAX];
193 char libsymlink[PKG_PATH_MAX];
194 struct stat libStat;
195
196 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700197 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198 return -1;
199 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700200 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700201 ALOGE("cannot create package lib symlink origin path\n");
202 return -1;
203 }
204 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
205 ALOGE("cannot create package lib symlink dest path\n");
206 return -1;
207 }
208
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800209 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
211 return -errno;
212 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800213 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700214 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
215 unlink(pkgdir);
216 return -errno;
217 }
218
219 if (lstat(libsymlink, &libStat) < 0) {
220 if (errno != ENOENT) {
221 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
222 unlink(pkgdir);
223 return -1;
224 }
225 } else {
226 if (S_ISDIR(libStat.st_mode)) {
227 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
228 ALOGE("couldn't delete lib directory during install for non-primary: %s",
229 libsymlink);
230 unlink(pkgdir);
231 return -1;
232 }
233 } else if (S_ISLNK(libStat.st_mode)) {
234 if (unlink(libsymlink) < 0) {
235 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
236 libsymlink);
237 unlink(pkgdir);
238 return -1;
239 }
240 }
241 }
242
243 if (symlink(applibdir, libsymlink) < 0) {
244 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
245 applibdir, strerror(errno));
246 unlink(pkgdir);
247 return -1;
248 }
249
Stephen Smalley26288202014-02-07 09:16:46 -0500250 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700251 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
252 unlink(libsymlink);
253 unlink(pkgdir);
254 return -errno;
255 }
256
257 if (chown(pkgdir, uid, uid) < 0) {
258 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
259 unlink(libsymlink);
260 unlink(pkgdir);
261 return -errno;
262 }
263
264 return 0;
265}
266
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700267int delete_user(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700268{
269 char data_path[PKG_PATH_MAX];
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700270 if (create_user_path(data_path, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700271 return -1;
272 }
273 if (delete_dir_contents(data_path, 1, NULL)) {
274 return -1;
275 }
276
277 char media_path[PATH_MAX];
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700278 if (create_user_media_path(media_path, userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700279 return -1;
280 }
281 if (delete_dir_contents(media_path, 1, NULL) == -1) {
282 return -1;
283 }
284
285 return 0;
286}
287
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700288int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700289{
290 char cachedir[PKG_PATH_MAX];
291
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700292 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700293 return -1;
294
295 /* delete contents, not the directory, no exceptions */
296 return delete_dir_contents(cachedir, 0, 0);
297}
298
299/* Try to ensure free_size bytes of storage are available.
300 * Returns 0 on success.
301 * This is rather simple-minded because doing a full LRU would
302 * be potentially memory-intensive, and without atime it would
303 * also require that apps constantly modify file metadata even
304 * when just reading from the cache, which is pretty awful.
305 */
306int free_cache(int64_t free_size)
307{
308 cache_t* cache;
309 int64_t avail;
310 DIR *d;
311 struct dirent *de;
312 char tmpdir[PATH_MAX];
313 char *dirpos;
314
315 avail = data_disk_free();
316 if (avail < 0) return -1;
317
318 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
319 if (avail >= free_size) return 0;
320
321 cache = start_cache_collection();
322
323 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700324 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700325 //ALOGI("adding cache files from %s\n", tmpdir);
326 add_cache_files(cache, tmpdir, "cache");
327 }
328
329 // Search for other users and add any cache files from them.
330 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
331 SECONDARY_USER_PREFIX);
332 dirpos = tmpdir + strlen(tmpdir);
333 d = opendir(tmpdir);
334 if (d != NULL) {
335 while ((de = readdir(d))) {
336 if (de->d_type == DT_DIR) {
337 const char *name = de->d_name;
338 /* always skip "." and ".." */
339 if (name[0] == '.') {
340 if (name[1] == 0) continue;
341 if ((name[1] == '.') && (name[2] == 0)) continue;
342 }
343 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
344 strcpy(dirpos, name);
345 //ALOGI("adding cache files from %s\n", tmpdir);
346 add_cache_files(cache, tmpdir, "cache");
347 } else {
348 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
349 }
350 }
351 }
352 closedir(d);
353 }
354
355 // Collect cache files on external storage for all users (if it is mounted as part
356 // of the internal storage).
357 strcpy(tmpdir, android_media_dir.path);
358 dirpos = tmpdir + strlen(tmpdir);
359 d = opendir(tmpdir);
360 if (d != NULL) {
361 while ((de = readdir(d))) {
362 if (de->d_type == DT_DIR) {
363 const char *name = de->d_name;
364 /* skip any dir that doesn't start with a number, so not a user */
365 if (name[0] < '0' || name[0] > '9') {
366 continue;
367 }
368 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
369 strcpy(dirpos, name);
370 if (lookup_media_dir(tmpdir, "Android") == 0
371 && lookup_media_dir(tmpdir, "data") == 0) {
372 //ALOGI("adding cache files from %s\n", tmpdir);
373 add_cache_files(cache, tmpdir, "cache");
374 }
375 } else {
376 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
377 }
378 }
379 }
380 closedir(d);
381 }
382
383 clear_cache_files(cache, free_size);
384 finish_cache_collection(cache);
385
386 return data_disk_free() >= free_size ? 0 : -1;
387}
388
389int move_dex(const char *src, const char *dst)
390{
391 char src_dex[PKG_PATH_MAX];
392 char dst_dex[PKG_PATH_MAX];
393
394 if (validate_apk_path(src)) return -1;
395 if (validate_apk_path(dst)) return -1;
396
397 if (create_cache_path(src_dex, src)) return -1;
398 if (create_cache_path(dst_dex, dst)) return -1;
399
400 ALOGV("move %s -> %s\n", src_dex, dst_dex);
401 if (rename(src_dex, dst_dex) < 0) {
402 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
403 return -1;
404 } else {
405 return 0;
406 }
407}
408
409int rm_dex(const char *path)
410{
411 char dex_path[PKG_PATH_MAX];
412
413 if (validate_apk_path(path)) return -1;
414 if (create_cache_path(dex_path, path)) return -1;
415
416 ALOGV("unlink %s\n", dex_path);
417 if (unlink(dex_path) < 0) {
418 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
419 return -1;
420 } else {
421 return 0;
422 }
423}
424
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700425int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700426 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700427 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
428 int64_t* _asecsize)
429{
430 DIR *d;
431 int dfd;
432 struct dirent *de;
433 struct stat s;
434 char path[PKG_PATH_MAX];
435
436 int64_t codesize = 0;
437 int64_t datasize = 0;
438 int64_t cachesize = 0;
439 int64_t asecsize = 0;
440
441 /* count the source apk as code -- but only if it's not
442 * on the /system partition and its not on the sdcard.
443 */
444 if (validate_system_app_path(apkpath) &&
445 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
446 if (stat(apkpath, &s) == 0) {
447 codesize += stat_size(&s);
448 }
449 }
450 /* count the forward locked apk as code if it is given
451 */
452 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
453 if (stat(fwdlock_apkpath, &s) == 0) {
454 codesize += stat_size(&s);
455 }
456 }
457 /* count the cached dexfile as code */
458 if (!create_cache_path(path, apkpath)) {
459 if (stat(path, &s) == 0) {
460 codesize += stat_size(&s);
461 }
462 }
463
464 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700465 if (libdirpath != NULL && libdirpath[0] != '!') {
466 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700467 if (d != NULL) {
468 dfd = dirfd(d);
469 codesize += calculate_dir_size(dfd);
470 closedir(d);
471 }
472 }
473
474 /* compute asec size if it is given
475 */
476 if (asecpath != NULL && asecpath[0] != '!') {
477 if (stat(asecpath, &s) == 0) {
478 asecsize += stat_size(&s);
479 }
480 }
481
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700482 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700483 goto done;
484 }
485
486 d = opendir(path);
487 if (d == NULL) {
488 goto done;
489 }
490 dfd = dirfd(d);
491
492 /* most stuff in the pkgdir is data, except for the "cache"
493 * directory and below, which is cache, and the "lib" directory
494 * and below, which is code...
495 */
496 while ((de = readdir(d))) {
497 const char *name = de->d_name;
498
499 if (de->d_type == DT_DIR) {
500 int subfd;
501 int64_t statsize = 0;
502 int64_t dirsize = 0;
503 /* always skip "." and ".." */
504 if (name[0] == '.') {
505 if (name[1] == 0) continue;
506 if ((name[1] == '.') && (name[2] == 0)) continue;
507 }
508 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
509 statsize = stat_size(&s);
510 }
511 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
512 if (subfd >= 0) {
513 dirsize = calculate_dir_size(subfd);
514 }
515 if(!strcmp(name,"lib")) {
516 codesize += dirsize + statsize;
517 } else if(!strcmp(name,"cache")) {
518 cachesize += dirsize + statsize;
519 } else {
520 datasize += dirsize + statsize;
521 }
522 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
523 // This is the symbolic link to the application's library
524 // code. We'll count this as code instead of data, since
525 // it is not something that the app creates.
526 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
527 codesize += stat_size(&s);
528 }
529 } else {
530 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
531 datasize += stat_size(&s);
532 }
533 }
534 }
535 closedir(d);
536done:
537 *_codesize = codesize;
538 *_datasize = datasize;
539 *_cachesize = cachesize;
540 *_asecsize = asecsize;
541 return 0;
542}
543
544
Mike Lockwood94afecf2012-10-24 10:45:23 -0700545int create_cache_path(char path[PKG_PATH_MAX], const char *src)
546{
547 char *tmp;
548 int srclen;
549 int dstlen;
550
551 srclen = strlen(src);
552
553 /* demand that we are an absolute path */
554 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
555 return -1;
556 }
557
558 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
559 return -1;
560 }
561
Dave Allisond9370732014-01-30 14:19:23 -0800562 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Mike Lockwood94afecf2012-10-24 10:45:23 -0700563 strlen(DALVIK_CACHE_POSTFIX) + 1;
Dave Allisond9370732014-01-30 14:19:23 -0800564
Mike Lockwood94afecf2012-10-24 10:45:23 -0700565 if (dstlen > PKG_PATH_MAX) {
566 return -1;
567 }
568
569 sprintf(path,"%s%s%s",
570 DALVIK_CACHE_PREFIX,
571 src + 1, /* skip the leading / */
572 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800573
Mike Lockwood94afecf2012-10-24 10:45:23 -0700574 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
575 if (*tmp == '/') {
576 *tmp = '@';
577 }
578 }
579
580 return 0;
581}
582
583static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800584 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700585{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800586 /* platform-specific flags affecting optimization and verification */
587 char dexopt_flags[PROPERTY_VALUE_MAX];
588 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
589 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
590
Mike Lockwood94afecf2012-10-24 10:45:23 -0700591 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
592 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
593 char zip_num[MAX_INT_LEN];
594 char odex_num[MAX_INT_LEN];
595
596 sprintf(zip_num, "%d", zip_fd);
597 sprintf(odex_num, "%d", odex_fd);
598
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700599 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700600 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
601 dexopt_flags, (char*) NULL);
602 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
603}
604
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700605static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Dave Allisond9370732014-01-30 14:19:23 -0800606 const char* output_file_name, const char *pkgname)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700607{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800608 char dex2oat_flags[PROPERTY_VALUE_MAX];
609 property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, "");
610 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
611
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700612 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
613 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
614 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
615 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
616 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
617 char oat_location_arg[strlen("--oat-name=") + PKG_PATH_MAX];
Dave Allisond9370732014-01-30 14:19:23 -0800618 char profile_file[strlen("--profile-file=") + PKG_PATH_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700619
620 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
621 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
622 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
623 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Dave Allisond9370732014-01-30 14:19:23 -0800624 if (strcmp(pkgname, "*") != 0) {
625 snprintf(profile_file, sizeof(profile_file), "--profile-file=%s/%s",
626 DALVIK_CACHE_PREFIX "profiles", pkgname);
627 } else {
628 strcpy(profile_file, "--no-profile-file");
629 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700630
631 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
632 execl(DEX2OAT_BIN, DEX2OAT_BIN,
633 zip_fd_arg, zip_location_arg,
634 oat_fd_arg, oat_location_arg,
Dave Allisond9370732014-01-30 14:19:23 -0800635 profile_file,
Anwar Ghuloum4bc05402014-03-11 15:42:58 -0700636 strlen(dex2oat_flags) > 0 ? dex2oat_flags : NULL,
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700637 (char*) NULL);
638 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
639}
640
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100641static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700642{
643 int status;
644 pid_t got_pid;
645
Mike Lockwood94afecf2012-10-24 10:45:23 -0700646 while (1) {
647 got_pid = waitpid(pid, &status, 0);
648 if (got_pid == -1 && errno == EINTR) {
649 printf("waitpid interrupted, retrying\n");
650 } else {
651 break;
652 }
653 }
654 if (got_pid != pid) {
655 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
656 (int) pid, (int) got_pid, strerror(errno));
657 return 1;
658 }
659
660 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700661 return 0;
662 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700663 return status; /* always nonzero */
664 }
665}
666
Dave Allisond9370732014-01-30 14:19:23 -0800667int dexopt(const char *apk_path, uid_t uid, int is_public,
668 const char *pkgname)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700669{
670 struct utimbuf ut;
671 struct stat apk_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700672 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700673 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700674 char *end;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700675 int res, zip_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700676
Mike Lockwood94afecf2012-10-24 10:45:23 -0700677 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
678 return -1;
679 }
680
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800681 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom0c05d3a2014-01-30 13:14:01 -0800682 property_get("persist.sys.dalvik.vm.lib.1", persist_sys_dalvik_vm_lib, "libdvm.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700683
684 /* Before anything else: is there a .odex file? If so, we have
685 * precompiled the apk and there is nothing to do here.
686 */
687 sprintf(out_path, "%s%s", apk_path, ".odex");
688 if (stat(out_path, &dex_stat) == 0) {
689 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700690 }
691
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700692 if (create_cache_path(out_path, apk_path)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700693 return -1;
694 }
695
696 memset(&apk_stat, 0, sizeof(apk_stat));
697 stat(apk_path, &apk_stat);
698
699 zip_fd = open(apk_path, O_RDONLY, 0);
700 if (zip_fd < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700701 ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700702 return -1;
703 }
704
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700705 unlink(out_path);
706 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
707 if (out_fd < 0) {
708 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700709 goto fail;
710 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700711 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700712 S_IRUSR|S_IWUSR|S_IRGRP |
713 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700714 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700715 goto fail;
716 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700717 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
718 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700719 goto fail;
720 }
721
Dave Allisond9370732014-01-30 14:19:23 -0800722 // Create profile file if there is a package name present.
723 if (strcmp(pkgname, "*") != 0) {
724 create_profile_file(pkgname, uid);
725 }
726
727
Mike Lockwood94afecf2012-10-24 10:45:23 -0700728 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
729
730 pid_t pid;
731 pid = fork();
732 if (pid == 0) {
733 /* child -- drop privileges before continuing */
734 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700735 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700736 exit(64);
737 }
738 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700739 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700740 exit(65);
741 }
742 // drop capabilities
743 struct __user_cap_header_struct capheader;
744 struct __user_cap_data_struct capdata[2];
745 memset(&capheader, 0, sizeof(capheader));
746 memset(&capdata, 0, sizeof(capdata));
747 capheader.version = _LINUX_CAPABILITY_VERSION_3;
748 if (capset(&capheader, &capdata[0]) < 0) {
749 ALOGE("capset failed: %s\n", strerror(errno));
750 exit(66);
751 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700752 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
753 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700754 exit(67);
755 }
756
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700757 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800758 run_dexopt(zip_fd, out_fd, apk_path, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700759 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Dave Allisond9370732014-01-30 14:19:23 -0800760 run_dex2oat(zip_fd, out_fd, apk_path, out_path, pkgname);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700761 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700762 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700763 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700764 exit(68); /* only get here on exec failure */
765 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100766 res = wait_child(pid);
767 if (res == 0) {
768 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
769 } else {
770 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", apk_path, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700771 goto fail;
772 }
773 }
774
775 ut.actime = apk_stat.st_atime;
776 ut.modtime = apk_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700777 utime(out_path, &ut);
778
779 close(out_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700780 close(zip_fd);
781 return 0;
782
783fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700784 if (out_fd >= 0) {
785 close(out_fd);
786 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700787 }
788 if (zip_fd >= 0) {
789 close(zip_fd);
790 }
791 return -1;
792}
793
794void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
795 struct stat* statbuf)
796{
797 while (path[basepos] != 0) {
798 if (path[basepos] == '/') {
799 path[basepos] = 0;
800 if (lstat(path, statbuf) < 0) {
801 ALOGV("Making directory: %s\n", path);
802 if (mkdir(path, mode) == 0) {
803 chown(path, uid, gid);
804 } else {
805 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
806 }
807 }
808 path[basepos] = '/';
809 basepos++;
810 }
811 basepos++;
812 }
813}
814
815int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
816 int dstuid, int dstgid, struct stat* statbuf)
817{
818 DIR *d;
819 struct dirent *de;
820 int res;
821
822 int srcend = strlen(srcpath);
823 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -0800824
Mike Lockwood94afecf2012-10-24 10:45:23 -0700825 if (lstat(srcpath, statbuf) < 0) {
826 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
827 return 1;
828 }
Dave Allisond9370732014-01-30 14:19:23 -0800829
Mike Lockwood94afecf2012-10-24 10:45:23 -0700830 if ((statbuf->st_mode&S_IFDIR) == 0) {
831 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
832 dstuid, dstgid, statbuf);
833 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
834 if (rename(srcpath, dstpath) >= 0) {
835 if (chown(dstpath, dstuid, dstgid) < 0) {
836 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
837 unlink(dstpath);
838 return 1;
839 }
840 } else {
841 ALOGW("Unable to rename %s to %s: %s\n",
842 srcpath, dstpath, strerror(errno));
843 return 1;
844 }
845 return 0;
846 }
847
848 d = opendir(srcpath);
849 if (d == NULL) {
850 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
851 return 1;
852 }
853
854 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -0800855
Mike Lockwood94afecf2012-10-24 10:45:23 -0700856 while ((de = readdir(d))) {
857 const char *name = de->d_name;
858 /* always skip "." and ".." */
859 if (name[0] == '.') {
860 if (name[1] == 0) continue;
861 if ((name[1] == '.') && (name[2] == 0)) continue;
862 }
Dave Allisond9370732014-01-30 14:19:23 -0800863
Mike Lockwood94afecf2012-10-24 10:45:23 -0700864 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
865 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
866 continue;
867 }
Dave Allisond9370732014-01-30 14:19:23 -0800868
Mike Lockwood94afecf2012-10-24 10:45:23 -0700869 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
870 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
871 continue;
872 }
Dave Allisond9370732014-01-30 14:19:23 -0800873
Mike Lockwood94afecf2012-10-24 10:45:23 -0700874 srcpath[srcend] = dstpath[dstend] = '/';
875 strcpy(srcpath+srcend+1, name);
876 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -0800877
Mike Lockwood94afecf2012-10-24 10:45:23 -0700878 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
879 res = 1;
880 }
Dave Allisond9370732014-01-30 14:19:23 -0800881
Mike Lockwood94afecf2012-10-24 10:45:23 -0700882 // Note: we will be leaving empty directories behind in srcpath,
883 // but that is okay, the package manager will be erasing all of the
884 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -0800885
Mike Lockwood94afecf2012-10-24 10:45:23 -0700886 srcpath[srcend] = dstpath[dstend] = 0;
887 }
Dave Allisond9370732014-01-30 14:19:23 -0800888
Mike Lockwood94afecf2012-10-24 10:45:23 -0700889 closedir(d);
890 return res;
891}
892
893int movefiles()
894{
895 DIR *d;
896 int dfd, subfd;
897 struct dirent *de;
898 struct stat s;
899 char buf[PKG_PATH_MAX+1];
900 int bufp, bufe, bufi, readlen;
901
902 char srcpkg[PKG_NAME_MAX];
903 char dstpkg[PKG_NAME_MAX];
904 char srcpath[PKG_PATH_MAX];
905 char dstpath[PKG_PATH_MAX];
906 int dstuid=-1, dstgid=-1;
907 int hasspace;
908
909 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
910 if (d == NULL) {
911 goto done;
912 }
913 dfd = dirfd(d);
914
915 /* Iterate through all files in the directory, executing the
916 * file movements requested there-in.
917 */
918 while ((de = readdir(d))) {
919 const char *name = de->d_name;
920
921 if (de->d_type == DT_DIR) {
922 continue;
923 } else {
924 subfd = openat(dfd, name, O_RDONLY);
925 if (subfd < 0) {
926 ALOGW("Unable to open update commands at %s%s\n",
927 UPDATE_COMMANDS_DIR_PREFIX, name);
928 continue;
929 }
Dave Allisond9370732014-01-30 14:19:23 -0800930
Mike Lockwood94afecf2012-10-24 10:45:23 -0700931 bufp = 0;
932 bufe = 0;
933 buf[PKG_PATH_MAX] = 0;
934 srcpkg[0] = dstpkg[0] = 0;
935 while (1) {
936 bufi = bufp;
937 while (bufi < bufe && buf[bufi] != '\n') {
938 bufi++;
939 }
940 if (bufi < bufe) {
941 buf[bufi] = 0;
942 ALOGV("Processing line: %s\n", buf+bufp);
943 hasspace = 0;
944 while (bufp < bufi && isspace(buf[bufp])) {
945 hasspace = 1;
946 bufp++;
947 }
948 if (buf[bufp] == '#' || bufp == bufi) {
949 // skip comments and empty lines.
950 } else if (hasspace) {
951 if (dstpkg[0] == 0) {
952 ALOGW("Path before package line in %s%s: %s\n",
953 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
954 } else if (srcpkg[0] == 0) {
955 // Skip -- source package no longer exists.
956 } else {
957 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
958 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
959 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
960 movefileordir(srcpath, dstpath,
961 strlen(dstpath)-strlen(buf+bufp),
962 dstuid, dstgid, &s);
963 }
964 }
965 } else {
966 char* div = strchr(buf+bufp, ':');
967 if (div == NULL) {
968 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
969 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
970 } else {
971 *div = 0;
972 div++;
973 if (strlen(buf+bufp) < PKG_NAME_MAX) {
974 strcpy(dstpkg, buf+bufp);
975 } else {
976 srcpkg[0] = dstpkg[0] = 0;
977 ALOGW("Package name too long in %s%s: %s\n",
978 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
979 }
980 if (strlen(div) < PKG_NAME_MAX) {
981 strcpy(srcpkg, div);
982 } else {
983 srcpkg[0] = dstpkg[0] = 0;
984 ALOGW("Package name too long in %s%s: %s\n",
985 UPDATE_COMMANDS_DIR_PREFIX, name, div);
986 }
987 if (srcpkg[0] != 0) {
988 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
989 if (lstat(srcpath, &s) < 0) {
990 // Package no longer exists -- skip.
991 srcpkg[0] = 0;
992 }
993 } else {
994 srcpkg[0] = 0;
995 ALOGW("Can't create path %s in %s%s\n",
996 div, UPDATE_COMMANDS_DIR_PREFIX, name);
997 }
998 if (srcpkg[0] != 0) {
999 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1000 if (lstat(dstpath, &s) == 0) {
1001 dstuid = s.st_uid;
1002 dstgid = s.st_gid;
1003 } else {
1004 // Destination package doesn't
1005 // exist... due to original-package,
1006 // this is normal, so don't be
1007 // noisy about it.
1008 srcpkg[0] = 0;
1009 }
1010 } else {
1011 srcpkg[0] = 0;
1012 ALOGW("Can't create path %s in %s%s\n",
1013 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1014 }
1015 }
1016 ALOGV("Transfering from %s to %s: uid=%d\n",
1017 srcpkg, dstpkg, dstuid);
1018 }
1019 }
1020 }
1021 bufp = bufi+1;
1022 } else {
1023 if (bufp == 0) {
1024 if (bufp < bufe) {
1025 ALOGW("Line too long in %s%s, skipping: %s\n",
1026 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1027 }
1028 } else if (bufp < bufe) {
1029 memcpy(buf, buf+bufp, bufe-bufp);
1030 bufe -= bufp;
1031 bufp = 0;
1032 }
1033 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1034 if (readlen < 0) {
1035 ALOGW("Failure reading update commands in %s%s: %s\n",
1036 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1037 break;
1038 } else if (readlen == 0) {
1039 break;
1040 }
1041 bufe += readlen;
1042 buf[bufe] = 0;
1043 ALOGV("Read buf: %s\n", buf);
1044 }
1045 }
1046 close(subfd);
1047 }
1048 }
1049 closedir(d);
1050done:
1051 return 0;
1052}
1053
1054int linklib(const char* pkgname, const char* asecLibDir, int userId)
1055{
1056 char pkgdir[PKG_PATH_MAX];
1057 char libsymlink[PKG_PATH_MAX];
1058 struct stat s, libStat;
1059 int rc = 0;
1060
1061 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1062 ALOGE("cannot create package path\n");
1063 return -1;
1064 }
1065 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1066 ALOGE("cannot create package lib symlink origin path\n");
1067 return -1;
1068 }
1069
1070 if (stat(pkgdir, &s) < 0) return -1;
1071
1072 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1073 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1074 return -1;
1075 }
1076
1077 if (chmod(pkgdir, 0700) < 0) {
1078 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1079 rc = -1;
1080 goto out;
1081 }
1082
1083 if (lstat(libsymlink, &libStat) < 0) {
1084 if (errno != ENOENT) {
1085 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1086 rc = -1;
1087 goto out;
1088 }
1089 } else {
1090 if (S_ISDIR(libStat.st_mode)) {
1091 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1092 rc = -1;
1093 goto out;
1094 }
1095 } else if (S_ISLNK(libStat.st_mode)) {
1096 if (unlink(libsymlink) < 0) {
1097 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1098 rc = -1;
1099 goto out;
1100 }
1101 }
1102 }
1103
1104 if (symlink(asecLibDir, libsymlink) < 0) {
1105 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1106 strerror(errno));
1107 rc = -errno;
1108 goto out;
1109 }
1110
1111out:
1112 if (chmod(pkgdir, s.st_mode) < 0) {
1113 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1114 rc = -errno;
1115 }
1116
1117 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1118 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1119 return -errno;
1120 }
1121
1122 return rc;
1123}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001124
1125static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1126{
1127 static const char *IDMAP_BIN = "/system/bin/idmap";
1128 static const size_t MAX_INT_LEN = 32;
1129 char idmap_str[MAX_INT_LEN];
1130
1131 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1132
1133 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1134 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1135}
1136
1137// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1138// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1139static int flatten_path(const char *prefix, const char *suffix,
1140 const char *overlay_path, char *idmap_path, size_t N)
1141{
1142 if (overlay_path == NULL || idmap_path == NULL) {
1143 return -1;
1144 }
1145 const size_t len_overlay_path = strlen(overlay_path);
1146 // will access overlay_path + 1 further below; requires absolute path
1147 if (len_overlay_path < 2 || *overlay_path != '/') {
1148 return -1;
1149 }
1150 const size_t len_idmap_root = strlen(prefix);
1151 const size_t len_suffix = strlen(suffix);
1152 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1153 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1154 // additions below would cause overflow
1155 return -1;
1156 }
1157 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1158 return -1;
1159 }
1160 memset(idmap_path, 0, N);
1161 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1162 char *ch = idmap_path + len_idmap_root;
1163 while (*ch != '\0') {
1164 if (*ch == '/') {
1165 *ch = '@';
1166 }
1167 ++ch;
1168 }
1169 return 0;
1170}
1171
1172int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1173{
1174 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1175
1176 int idmap_fd = -1;
1177 char idmap_path[PATH_MAX];
1178
1179 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1180 idmap_path, sizeof(idmap_path)) == -1) {
1181 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1182 goto fail;
1183 }
1184
1185 unlink(idmap_path);
1186 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1187 if (idmap_fd < 0) {
1188 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1189 goto fail;
1190 }
1191 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1192 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1193 goto fail;
1194 }
1195 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1196 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1197 goto fail;
1198 }
1199
1200 pid_t pid;
1201 pid = fork();
1202 if (pid == 0) {
1203 /* child -- drop privileges before continuing */
1204 if (setgid(uid) != 0) {
1205 ALOGE("setgid(%d) failed during idmap\n", uid);
1206 exit(1);
1207 }
1208 if (setuid(uid) != 0) {
1209 ALOGE("setuid(%d) failed during idmap\n", uid);
1210 exit(1);
1211 }
1212 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1213 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1214 exit(1);
1215 }
1216
1217 run_idmap(target_apk, overlay_apk, idmap_fd);
1218 exit(1); /* only if exec call to idmap failed */
1219 } else {
1220 int status = wait_child(pid);
1221 if (status != 0) {
1222 ALOGE("idmap failed, status=0x%04x\n", status);
1223 goto fail;
1224 }
1225 }
1226
1227 close(idmap_fd);
1228 return 0;
1229fail:
1230 if (idmap_fd >= 0) {
1231 close(idmap_fd);
1232 unlink(idmap_path);
1233 }
1234 return -1;
1235}