blob: 12e8cd19919faf05bc1b827857f647e3f88ce4af [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
88 if (symlink(applibdir, libsymlink) < 0) {
89 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
90 strerror(errno));
91 unlink(pkgdir);
92 return -1;
93 }
94
Stephen Smalley26288202014-02-07 09:16:46 -050095 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070096 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
97 unlink(libsymlink);
98 unlink(pkgdir);
99 return -errno;
100 }
101
102 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
244 if (symlink(applibdir, libsymlink) < 0) {
245 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
246 applibdir, strerror(errno));
247 unlink(pkgdir);
248 return -1;
249 }
250
Stephen Smalley26288202014-02-07 09:16:46 -0500251 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700252 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
253 unlink(libsymlink);
254 unlink(pkgdir);
255 return -errno;
256 }
257
258 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
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700268int delete_user(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269{
270 char data_path[PKG_PATH_MAX];
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700271 if (create_user_path(data_path, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700272 return -1;
273 }
274 if (delete_dir_contents(data_path, 1, NULL)) {
275 return -1;
276 }
277
278 char media_path[PATH_MAX];
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700279 if (create_user_media_path(media_path, userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700280 return -1;
281 }
282 if (delete_dir_contents(media_path, 1, NULL) == -1) {
283 return -1;
284 }
285
286 return 0;
287}
288
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700289int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700290{
291 char cachedir[PKG_PATH_MAX];
292
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700293 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700294 return -1;
295
296 /* delete contents, not the directory, no exceptions */
297 return delete_dir_contents(cachedir, 0, 0);
298}
299
300/* Try to ensure free_size bytes of storage are available.
301 * Returns 0 on success.
302 * This is rather simple-minded because doing a full LRU would
303 * be potentially memory-intensive, and without atime it would
304 * also require that apps constantly modify file metadata even
305 * when just reading from the cache, which is pretty awful.
306 */
307int free_cache(int64_t free_size)
308{
309 cache_t* cache;
310 int64_t avail;
311 DIR *d;
312 struct dirent *de;
313 char tmpdir[PATH_MAX];
314 char *dirpos;
315
316 avail = data_disk_free();
317 if (avail < 0) return -1;
318
319 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
320 if (avail >= free_size) return 0;
321
322 cache = start_cache_collection();
323
324 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700325 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700326 //ALOGI("adding cache files from %s\n", tmpdir);
327 add_cache_files(cache, tmpdir, "cache");
328 }
329
330 // Search for other users and add any cache files from them.
331 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
332 SECONDARY_USER_PREFIX);
333 dirpos = tmpdir + strlen(tmpdir);
334 d = opendir(tmpdir);
335 if (d != NULL) {
336 while ((de = readdir(d))) {
337 if (de->d_type == DT_DIR) {
338 const char *name = de->d_name;
339 /* always skip "." and ".." */
340 if (name[0] == '.') {
341 if (name[1] == 0) continue;
342 if ((name[1] == '.') && (name[2] == 0)) continue;
343 }
344 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
345 strcpy(dirpos, name);
346 //ALOGI("adding cache files from %s\n", tmpdir);
347 add_cache_files(cache, tmpdir, "cache");
348 } else {
349 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
350 }
351 }
352 }
353 closedir(d);
354 }
355
356 // Collect cache files on external storage for all users (if it is mounted as part
357 // of the internal storage).
358 strcpy(tmpdir, android_media_dir.path);
359 dirpos = tmpdir + strlen(tmpdir);
360 d = opendir(tmpdir);
361 if (d != NULL) {
362 while ((de = readdir(d))) {
363 if (de->d_type == DT_DIR) {
364 const char *name = de->d_name;
365 /* skip any dir that doesn't start with a number, so not a user */
366 if (name[0] < '0' || name[0] > '9') {
367 continue;
368 }
369 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
370 strcpy(dirpos, name);
371 if (lookup_media_dir(tmpdir, "Android") == 0
372 && lookup_media_dir(tmpdir, "data") == 0) {
373 //ALOGI("adding cache files from %s\n", tmpdir);
374 add_cache_files(cache, tmpdir, "cache");
375 }
376 } else {
377 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
378 }
379 }
380 }
381 closedir(d);
382 }
383
384 clear_cache_files(cache, free_size);
385 finish_cache_collection(cache);
386
387 return data_disk_free() >= free_size ? 0 : -1;
388}
389
390int move_dex(const char *src, const char *dst)
391{
392 char src_dex[PKG_PATH_MAX];
393 char dst_dex[PKG_PATH_MAX];
394
395 if (validate_apk_path(src)) return -1;
396 if (validate_apk_path(dst)) return -1;
397
398 if (create_cache_path(src_dex, src)) return -1;
399 if (create_cache_path(dst_dex, dst)) return -1;
400
401 ALOGV("move %s -> %s\n", src_dex, dst_dex);
402 if (rename(src_dex, dst_dex) < 0) {
403 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
404 return -1;
405 } else {
406 return 0;
407 }
408}
409
410int rm_dex(const char *path)
411{
412 char dex_path[PKG_PATH_MAX];
413
414 if (validate_apk_path(path)) return -1;
415 if (create_cache_path(dex_path, path)) return -1;
416
417 ALOGV("unlink %s\n", dex_path);
418 if (unlink(dex_path) < 0) {
419 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
420 return -1;
421 } else {
422 return 0;
423 }
424}
425
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700426int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700427 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700428 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
429 int64_t* _asecsize)
430{
431 DIR *d;
432 int dfd;
433 struct dirent *de;
434 struct stat s;
435 char path[PKG_PATH_MAX];
436
437 int64_t codesize = 0;
438 int64_t datasize = 0;
439 int64_t cachesize = 0;
440 int64_t asecsize = 0;
441
442 /* count the source apk as code -- but only if it's not
443 * on the /system partition and its not on the sdcard.
444 */
445 if (validate_system_app_path(apkpath) &&
446 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
447 if (stat(apkpath, &s) == 0) {
448 codesize += stat_size(&s);
449 }
450 }
451 /* count the forward locked apk as code if it is given
452 */
453 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
454 if (stat(fwdlock_apkpath, &s) == 0) {
455 codesize += stat_size(&s);
456 }
457 }
458 /* count the cached dexfile as code */
459 if (!create_cache_path(path, apkpath)) {
460 if (stat(path, &s) == 0) {
461 codesize += stat_size(&s);
462 }
463 }
464
465 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700466 if (libdirpath != NULL && libdirpath[0] != '!') {
467 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700468 if (d != NULL) {
469 dfd = dirfd(d);
470 codesize += calculate_dir_size(dfd);
471 closedir(d);
472 }
473 }
474
475 /* compute asec size if it is given
476 */
477 if (asecpath != NULL && asecpath[0] != '!') {
478 if (stat(asecpath, &s) == 0) {
479 asecsize += stat_size(&s);
480 }
481 }
482
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700483 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700484 goto done;
485 }
486
487 d = opendir(path);
488 if (d == NULL) {
489 goto done;
490 }
491 dfd = dirfd(d);
492
493 /* most stuff in the pkgdir is data, except for the "cache"
494 * directory and below, which is cache, and the "lib" directory
495 * and below, which is code...
496 */
497 while ((de = readdir(d))) {
498 const char *name = de->d_name;
499
500 if (de->d_type == DT_DIR) {
501 int subfd;
502 int64_t statsize = 0;
503 int64_t dirsize = 0;
504 /* always skip "." and ".." */
505 if (name[0] == '.') {
506 if (name[1] == 0) continue;
507 if ((name[1] == '.') && (name[2] == 0)) continue;
508 }
509 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
510 statsize = stat_size(&s);
511 }
512 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
513 if (subfd >= 0) {
514 dirsize = calculate_dir_size(subfd);
515 }
516 if(!strcmp(name,"lib")) {
517 codesize += dirsize + statsize;
518 } else if(!strcmp(name,"cache")) {
519 cachesize += dirsize + statsize;
520 } else {
521 datasize += dirsize + statsize;
522 }
523 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
524 // This is the symbolic link to the application's library
525 // code. We'll count this as code instead of data, since
526 // it is not something that the app creates.
527 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
528 codesize += stat_size(&s);
529 }
530 } else {
531 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
532 datasize += stat_size(&s);
533 }
534 }
535 }
536 closedir(d);
537done:
538 *_codesize = codesize;
539 *_datasize = datasize;
540 *_cachesize = cachesize;
541 *_asecsize = asecsize;
542 return 0;
543}
544
545
Mike Lockwood94afecf2012-10-24 10:45:23 -0700546int create_cache_path(char path[PKG_PATH_MAX], const char *src)
547{
548 char *tmp;
549 int srclen;
550 int dstlen;
551
552 srclen = strlen(src);
553
554 /* demand that we are an absolute path */
555 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
556 return -1;
557 }
558
559 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
560 return -1;
561 }
562
Dave Allisond9370732014-01-30 14:19:23 -0800563 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Mike Lockwood94afecf2012-10-24 10:45:23 -0700564 strlen(DALVIK_CACHE_POSTFIX) + 1;
Dave Allisond9370732014-01-30 14:19:23 -0800565
Mike Lockwood94afecf2012-10-24 10:45:23 -0700566 if (dstlen > PKG_PATH_MAX) {
567 return -1;
568 }
569
570 sprintf(path,"%s%s%s",
571 DALVIK_CACHE_PREFIX,
572 src + 1, /* skip the leading / */
573 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800574
Mike Lockwood94afecf2012-10-24 10:45:23 -0700575 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
576 if (*tmp == '/') {
577 *tmp = '@';
578 }
579 }
580
581 return 0;
582}
583
584static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800585 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700586{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800587 /* platform-specific flags affecting optimization and verification */
588 char dexopt_flags[PROPERTY_VALUE_MAX];
589 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
590 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
591
Mike Lockwood94afecf2012-10-24 10:45:23 -0700592 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
593 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
594 char zip_num[MAX_INT_LEN];
595 char odex_num[MAX_INT_LEN];
596
597 sprintf(zip_num, "%d", zip_fd);
598 sprintf(odex_num, "%d", odex_fd);
599
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700600 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700601 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
602 dexopt_flags, (char*) NULL);
603 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
604}
605
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700606static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Dave Allisond9370732014-01-30 14:19:23 -0800607 const char* output_file_name, const char *pkgname)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700608{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800609 char dex2oat_flags[PROPERTY_VALUE_MAX];
610 property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, "");
611 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
612
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700613 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
614 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
615 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
616 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
617 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
618 char oat_location_arg[strlen("--oat-name=") + PKG_PATH_MAX];
Dave Allisond9370732014-01-30 14:19:23 -0800619 char profile_file[strlen("--profile-file=") + PKG_PATH_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700620
621 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
622 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
623 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
624 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Dave Allisond9370732014-01-30 14:19:23 -0800625 if (strcmp(pkgname, "*") != 0) {
626 snprintf(profile_file, sizeof(profile_file), "--profile-file=%s/%s",
627 DALVIK_CACHE_PREFIX "profiles", pkgname);
628 } else {
629 strcpy(profile_file, "--no-profile-file");
630 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700631
632 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
633 execl(DEX2OAT_BIN, DEX2OAT_BIN,
634 zip_fd_arg, zip_location_arg,
635 oat_fd_arg, oat_location_arg,
Dave Allisond9370732014-01-30 14:19:23 -0800636 profile_file,
Anwar Ghuloum4bc05402014-03-11 15:42:58 -0700637 strlen(dex2oat_flags) > 0 ? dex2oat_flags : NULL,
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700638 (char*) NULL);
639 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
640}
641
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100642static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700643{
644 int status;
645 pid_t got_pid;
646
Mike Lockwood94afecf2012-10-24 10:45:23 -0700647 while (1) {
648 got_pid = waitpid(pid, &status, 0);
649 if (got_pid == -1 && errno == EINTR) {
650 printf("waitpid interrupted, retrying\n");
651 } else {
652 break;
653 }
654 }
655 if (got_pid != pid) {
656 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
657 (int) pid, (int) got_pid, strerror(errno));
658 return 1;
659 }
660
661 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700662 return 0;
663 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700664 return status; /* always nonzero */
665 }
666}
667
Dave Allisond9370732014-01-30 14:19:23 -0800668int dexopt(const char *apk_path, uid_t uid, int is_public,
669 const char *pkgname)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700670{
671 struct utimbuf ut;
672 struct stat apk_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700673 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700674 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700675 char *end;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700676 int res, zip_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700677
Mike Lockwood94afecf2012-10-24 10:45:23 -0700678 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
679 return -1;
680 }
681
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800682 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom0c05d3a2014-01-30 13:14:01 -0800683 property_get("persist.sys.dalvik.vm.lib.1", persist_sys_dalvik_vm_lib, "libdvm.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700684
685 /* Before anything else: is there a .odex file? If so, we have
686 * precompiled the apk and there is nothing to do here.
687 */
688 sprintf(out_path, "%s%s", apk_path, ".odex");
689 if (stat(out_path, &dex_stat) == 0) {
690 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700691 }
692
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700693 if (create_cache_path(out_path, apk_path)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700694 return -1;
695 }
696
697 memset(&apk_stat, 0, sizeof(apk_stat));
698 stat(apk_path, &apk_stat);
699
700 zip_fd = open(apk_path, O_RDONLY, 0);
701 if (zip_fd < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700702 ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700703 return -1;
704 }
705
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700706 unlink(out_path);
707 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
708 if (out_fd < 0) {
709 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700710 goto fail;
711 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700712 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700713 S_IRUSR|S_IWUSR|S_IRGRP |
714 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700715 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700716 goto fail;
717 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700718 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
719 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700720 goto fail;
721 }
722
Dave Allisond9370732014-01-30 14:19:23 -0800723 // Create profile file if there is a package name present.
724 if (strcmp(pkgname, "*") != 0) {
725 create_profile_file(pkgname, uid);
726 }
727
728
Mike Lockwood94afecf2012-10-24 10:45:23 -0700729 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
730
731 pid_t pid;
732 pid = fork();
733 if (pid == 0) {
734 /* child -- drop privileges before continuing */
735 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700736 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700737 exit(64);
738 }
739 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700740 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700741 exit(65);
742 }
743 // drop capabilities
744 struct __user_cap_header_struct capheader;
745 struct __user_cap_data_struct capdata[2];
746 memset(&capheader, 0, sizeof(capheader));
747 memset(&capdata, 0, sizeof(capdata));
748 capheader.version = _LINUX_CAPABILITY_VERSION_3;
749 if (capset(&capheader, &capdata[0]) < 0) {
750 ALOGE("capset failed: %s\n", strerror(errno));
751 exit(66);
752 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700753 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
754 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700755 exit(67);
756 }
757
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700758 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800759 run_dexopt(zip_fd, out_fd, apk_path, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700760 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Dave Allisond9370732014-01-30 14:19:23 -0800761 run_dex2oat(zip_fd, out_fd, apk_path, out_path, pkgname);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700762 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700763 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700764 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700765 exit(68); /* only get here on exec failure */
766 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100767 res = wait_child(pid);
768 if (res == 0) {
769 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
770 } else {
771 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", apk_path, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700772 goto fail;
773 }
774 }
775
776 ut.actime = apk_stat.st_atime;
777 ut.modtime = apk_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700778 utime(out_path, &ut);
779
780 close(out_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700781 close(zip_fd);
782 return 0;
783
784fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700785 if (out_fd >= 0) {
786 close(out_fd);
787 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700788 }
789 if (zip_fd >= 0) {
790 close(zip_fd);
791 }
792 return -1;
793}
794
795void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
796 struct stat* statbuf)
797{
798 while (path[basepos] != 0) {
799 if (path[basepos] == '/') {
800 path[basepos] = 0;
801 if (lstat(path, statbuf) < 0) {
802 ALOGV("Making directory: %s\n", path);
803 if (mkdir(path, mode) == 0) {
804 chown(path, uid, gid);
805 } else {
806 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
807 }
808 }
809 path[basepos] = '/';
810 basepos++;
811 }
812 basepos++;
813 }
814}
815
816int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
817 int dstuid, int dstgid, struct stat* statbuf)
818{
819 DIR *d;
820 struct dirent *de;
821 int res;
822
823 int srcend = strlen(srcpath);
824 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -0800825
Mike Lockwood94afecf2012-10-24 10:45:23 -0700826 if (lstat(srcpath, statbuf) < 0) {
827 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
828 return 1;
829 }
Dave Allisond9370732014-01-30 14:19:23 -0800830
Mike Lockwood94afecf2012-10-24 10:45:23 -0700831 if ((statbuf->st_mode&S_IFDIR) == 0) {
832 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
833 dstuid, dstgid, statbuf);
834 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
835 if (rename(srcpath, dstpath) >= 0) {
836 if (chown(dstpath, dstuid, dstgid) < 0) {
837 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
838 unlink(dstpath);
839 return 1;
840 }
841 } else {
842 ALOGW("Unable to rename %s to %s: %s\n",
843 srcpath, dstpath, strerror(errno));
844 return 1;
845 }
846 return 0;
847 }
848
849 d = opendir(srcpath);
850 if (d == NULL) {
851 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
852 return 1;
853 }
854
855 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -0800856
Mike Lockwood94afecf2012-10-24 10:45:23 -0700857 while ((de = readdir(d))) {
858 const char *name = de->d_name;
859 /* always skip "." and ".." */
860 if (name[0] == '.') {
861 if (name[1] == 0) continue;
862 if ((name[1] == '.') && (name[2] == 0)) continue;
863 }
Dave Allisond9370732014-01-30 14:19:23 -0800864
Mike Lockwood94afecf2012-10-24 10:45:23 -0700865 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
866 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
867 continue;
868 }
Dave Allisond9370732014-01-30 14:19:23 -0800869
Mike Lockwood94afecf2012-10-24 10:45:23 -0700870 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
871 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
872 continue;
873 }
Dave Allisond9370732014-01-30 14:19:23 -0800874
Mike Lockwood94afecf2012-10-24 10:45:23 -0700875 srcpath[srcend] = dstpath[dstend] = '/';
876 strcpy(srcpath+srcend+1, name);
877 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -0800878
Mike Lockwood94afecf2012-10-24 10:45:23 -0700879 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
880 res = 1;
881 }
Dave Allisond9370732014-01-30 14:19:23 -0800882
Mike Lockwood94afecf2012-10-24 10:45:23 -0700883 // Note: we will be leaving empty directories behind in srcpath,
884 // but that is okay, the package manager will be erasing all of the
885 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -0800886
Mike Lockwood94afecf2012-10-24 10:45:23 -0700887 srcpath[srcend] = dstpath[dstend] = 0;
888 }
Dave Allisond9370732014-01-30 14:19:23 -0800889
Mike Lockwood94afecf2012-10-24 10:45:23 -0700890 closedir(d);
891 return res;
892}
893
894int movefiles()
895{
896 DIR *d;
897 int dfd, subfd;
898 struct dirent *de;
899 struct stat s;
900 char buf[PKG_PATH_MAX+1];
901 int bufp, bufe, bufi, readlen;
902
903 char srcpkg[PKG_NAME_MAX];
904 char dstpkg[PKG_NAME_MAX];
905 char srcpath[PKG_PATH_MAX];
906 char dstpath[PKG_PATH_MAX];
907 int dstuid=-1, dstgid=-1;
908 int hasspace;
909
910 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
911 if (d == NULL) {
912 goto done;
913 }
914 dfd = dirfd(d);
915
916 /* Iterate through all files in the directory, executing the
917 * file movements requested there-in.
918 */
919 while ((de = readdir(d))) {
920 const char *name = de->d_name;
921
922 if (de->d_type == DT_DIR) {
923 continue;
924 } else {
925 subfd = openat(dfd, name, O_RDONLY);
926 if (subfd < 0) {
927 ALOGW("Unable to open update commands at %s%s\n",
928 UPDATE_COMMANDS_DIR_PREFIX, name);
929 continue;
930 }
Dave Allisond9370732014-01-30 14:19:23 -0800931
Mike Lockwood94afecf2012-10-24 10:45:23 -0700932 bufp = 0;
933 bufe = 0;
934 buf[PKG_PATH_MAX] = 0;
935 srcpkg[0] = dstpkg[0] = 0;
936 while (1) {
937 bufi = bufp;
938 while (bufi < bufe && buf[bufi] != '\n') {
939 bufi++;
940 }
941 if (bufi < bufe) {
942 buf[bufi] = 0;
943 ALOGV("Processing line: %s\n", buf+bufp);
944 hasspace = 0;
945 while (bufp < bufi && isspace(buf[bufp])) {
946 hasspace = 1;
947 bufp++;
948 }
949 if (buf[bufp] == '#' || bufp == bufi) {
950 // skip comments and empty lines.
951 } else if (hasspace) {
952 if (dstpkg[0] == 0) {
953 ALOGW("Path before package line in %s%s: %s\n",
954 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
955 } else if (srcpkg[0] == 0) {
956 // Skip -- source package no longer exists.
957 } else {
958 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
959 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
960 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
961 movefileordir(srcpath, dstpath,
962 strlen(dstpath)-strlen(buf+bufp),
963 dstuid, dstgid, &s);
964 }
965 }
966 } else {
967 char* div = strchr(buf+bufp, ':');
968 if (div == NULL) {
969 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
970 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
971 } else {
972 *div = 0;
973 div++;
974 if (strlen(buf+bufp) < PKG_NAME_MAX) {
975 strcpy(dstpkg, buf+bufp);
976 } else {
977 srcpkg[0] = dstpkg[0] = 0;
978 ALOGW("Package name too long in %s%s: %s\n",
979 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
980 }
981 if (strlen(div) < PKG_NAME_MAX) {
982 strcpy(srcpkg, div);
983 } else {
984 srcpkg[0] = dstpkg[0] = 0;
985 ALOGW("Package name too long in %s%s: %s\n",
986 UPDATE_COMMANDS_DIR_PREFIX, name, div);
987 }
988 if (srcpkg[0] != 0) {
989 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
990 if (lstat(srcpath, &s) < 0) {
991 // Package no longer exists -- skip.
992 srcpkg[0] = 0;
993 }
994 } else {
995 srcpkg[0] = 0;
996 ALOGW("Can't create path %s in %s%s\n",
997 div, UPDATE_COMMANDS_DIR_PREFIX, name);
998 }
999 if (srcpkg[0] != 0) {
1000 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1001 if (lstat(dstpath, &s) == 0) {
1002 dstuid = s.st_uid;
1003 dstgid = s.st_gid;
1004 } else {
1005 // Destination package doesn't
1006 // exist... due to original-package,
1007 // this is normal, so don't be
1008 // noisy about it.
1009 srcpkg[0] = 0;
1010 }
1011 } else {
1012 srcpkg[0] = 0;
1013 ALOGW("Can't create path %s in %s%s\n",
1014 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1015 }
1016 }
1017 ALOGV("Transfering from %s to %s: uid=%d\n",
1018 srcpkg, dstpkg, dstuid);
1019 }
1020 }
1021 }
1022 bufp = bufi+1;
1023 } else {
1024 if (bufp == 0) {
1025 if (bufp < bufe) {
1026 ALOGW("Line too long in %s%s, skipping: %s\n",
1027 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1028 }
1029 } else if (bufp < bufe) {
1030 memcpy(buf, buf+bufp, bufe-bufp);
1031 bufe -= bufp;
1032 bufp = 0;
1033 }
1034 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1035 if (readlen < 0) {
1036 ALOGW("Failure reading update commands in %s%s: %s\n",
1037 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1038 break;
1039 } else if (readlen == 0) {
1040 break;
1041 }
1042 bufe += readlen;
1043 buf[bufe] = 0;
1044 ALOGV("Read buf: %s\n", buf);
1045 }
1046 }
1047 close(subfd);
1048 }
1049 }
1050 closedir(d);
1051done:
1052 return 0;
1053}
1054
1055int linklib(const char* pkgname, const char* asecLibDir, int userId)
1056{
1057 char pkgdir[PKG_PATH_MAX];
1058 char libsymlink[PKG_PATH_MAX];
1059 struct stat s, libStat;
1060 int rc = 0;
1061
1062 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1063 ALOGE("cannot create package path\n");
1064 return -1;
1065 }
1066 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1067 ALOGE("cannot create package lib symlink origin path\n");
1068 return -1;
1069 }
1070
1071 if (stat(pkgdir, &s) < 0) return -1;
1072
1073 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1074 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1075 return -1;
1076 }
1077
1078 if (chmod(pkgdir, 0700) < 0) {
1079 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1080 rc = -1;
1081 goto out;
1082 }
1083
1084 if (lstat(libsymlink, &libStat) < 0) {
1085 if (errno != ENOENT) {
1086 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1087 rc = -1;
1088 goto out;
1089 }
1090 } else {
1091 if (S_ISDIR(libStat.st_mode)) {
1092 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1093 rc = -1;
1094 goto out;
1095 }
1096 } else if (S_ISLNK(libStat.st_mode)) {
1097 if (unlink(libsymlink) < 0) {
1098 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1099 rc = -1;
1100 goto out;
1101 }
1102 }
1103 }
1104
1105 if (symlink(asecLibDir, libsymlink) < 0) {
1106 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1107 strerror(errno));
1108 rc = -errno;
1109 goto out;
1110 }
1111
1112out:
1113 if (chmod(pkgdir, s.st_mode) < 0) {
1114 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1115 rc = -errno;
1116 }
1117
1118 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1119 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1120 return -errno;
1121 }
1122
1123 return rc;
1124}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001125
1126static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1127{
1128 static const char *IDMAP_BIN = "/system/bin/idmap";
1129 static const size_t MAX_INT_LEN = 32;
1130 char idmap_str[MAX_INT_LEN];
1131
1132 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1133
1134 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1135 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1136}
1137
1138// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1139// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1140static int flatten_path(const char *prefix, const char *suffix,
1141 const char *overlay_path, char *idmap_path, size_t N)
1142{
1143 if (overlay_path == NULL || idmap_path == NULL) {
1144 return -1;
1145 }
1146 const size_t len_overlay_path = strlen(overlay_path);
1147 // will access overlay_path + 1 further below; requires absolute path
1148 if (len_overlay_path < 2 || *overlay_path != '/') {
1149 return -1;
1150 }
1151 const size_t len_idmap_root = strlen(prefix);
1152 const size_t len_suffix = strlen(suffix);
1153 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1154 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1155 // additions below would cause overflow
1156 return -1;
1157 }
1158 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1159 return -1;
1160 }
1161 memset(idmap_path, 0, N);
1162 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1163 char *ch = idmap_path + len_idmap_root;
1164 while (*ch != '\0') {
1165 if (*ch == '/') {
1166 *ch = '@';
1167 }
1168 ++ch;
1169 }
1170 return 0;
1171}
1172
1173int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1174{
1175 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1176
1177 int idmap_fd = -1;
1178 char idmap_path[PATH_MAX];
1179
1180 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1181 idmap_path, sizeof(idmap_path)) == -1) {
1182 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1183 goto fail;
1184 }
1185
1186 unlink(idmap_path);
1187 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1188 if (idmap_fd < 0) {
1189 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1190 goto fail;
1191 }
1192 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1193 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1194 goto fail;
1195 }
1196 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1197 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1198 goto fail;
1199 }
1200
1201 pid_t pid;
1202 pid = fork();
1203 if (pid == 0) {
1204 /* child -- drop privileges before continuing */
1205 if (setgid(uid) != 0) {
1206 ALOGE("setgid(%d) failed during idmap\n", uid);
1207 exit(1);
1208 }
1209 if (setuid(uid) != 0) {
1210 ALOGE("setuid(%d) failed during idmap\n", uid);
1211 exit(1);
1212 }
1213 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1214 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1215 exit(1);
1216 }
1217
1218 run_idmap(target_apk, overlay_apk, idmap_fd);
1219 exit(1); /* only if exec call to idmap failed */
1220 } else {
1221 int status = wait_child(pid);
1222 if (status != 0) {
1223 ALOGE("idmap failed, status=0x%04x\n", status);
1224 goto fail;
1225 }
1226 }
1227
1228 close(idmap_fd);
1229 return 0;
1230fail:
1231 if (idmap_fd >= 0) {
1232 close(idmap_fd);
1233 unlink(idmap_path);
1234 }
1235 return -1;
1236}
Robert Craige9887e42014-02-20 10:25:56 -05001237
1238int restorecon_data()
1239{
1240 char *data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
1241 char *user_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
1242
1243 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE |
1244 SELINUX_ANDROID_RESTORECON_DATADATA;
1245
1246 int ret = 0;
1247
1248 if (!data_dir || !user_dir) {
1249 return -1;
1250 }
1251
1252 if (selinux_android_restorecon(data_dir, flags) < 0) {
1253 ALOGE("restorecon failed for %s: %s\n", data_dir, strerror(errno));
1254 ret |= -1;
1255 }
1256
1257 if (selinux_android_restorecon(user_dir, flags) < 0) {
1258 ALOGE("restorecon failed for %s: %s\n", user_dir, strerror(errno));
1259 ret |= -1;
1260 }
1261
1262 free(data_dir);
1263 free(user_dir);
1264 return ret;
1265}