blob: a86abe1b7baca5341370eccadef8a3bbecf59c16 [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 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800688 strcpy(out_path, apk_path);
689 end = strrchr(out_path, '.');
690 if (end != NULL) {
691 strcpy(end, ".odex");
692 if (stat(out_path, &dex_stat) == 0) {
693 return 0;
694 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700695 }
696
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700697 if (create_cache_path(out_path, apk_path)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700698 return -1;
699 }
700
701 memset(&apk_stat, 0, sizeof(apk_stat));
702 stat(apk_path, &apk_stat);
703
704 zip_fd = open(apk_path, O_RDONLY, 0);
705 if (zip_fd < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700706 ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700707 return -1;
708 }
709
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700710 unlink(out_path);
711 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
712 if (out_fd < 0) {
713 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700714 goto fail;
715 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700716 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700717 S_IRUSR|S_IWUSR|S_IRGRP |
718 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700719 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700720 goto fail;
721 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700722 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
723 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700724 goto fail;
725 }
726
Dave Allisond9370732014-01-30 14:19:23 -0800727 // Create profile file if there is a package name present.
728 if (strcmp(pkgname, "*") != 0) {
729 create_profile_file(pkgname, uid);
730 }
731
732
Mike Lockwood94afecf2012-10-24 10:45:23 -0700733 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
734
735 pid_t pid;
736 pid = fork();
737 if (pid == 0) {
738 /* child -- drop privileges before continuing */
739 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700740 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700741 exit(64);
742 }
743 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700744 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700745 exit(65);
746 }
747 // drop capabilities
748 struct __user_cap_header_struct capheader;
749 struct __user_cap_data_struct capdata[2];
750 memset(&capheader, 0, sizeof(capheader));
751 memset(&capdata, 0, sizeof(capdata));
752 capheader.version = _LINUX_CAPABILITY_VERSION_3;
753 if (capset(&capheader, &capdata[0]) < 0) {
754 ALOGE("capset failed: %s\n", strerror(errno));
755 exit(66);
756 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700757 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
758 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700759 exit(67);
760 }
761
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700762 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800763 run_dexopt(zip_fd, out_fd, apk_path, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700764 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Dave Allisond9370732014-01-30 14:19:23 -0800765 run_dex2oat(zip_fd, out_fd, apk_path, out_path, pkgname);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700766 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700767 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700768 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700769 exit(68); /* only get here on exec failure */
770 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100771 res = wait_child(pid);
772 if (res == 0) {
773 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
774 } else {
775 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", apk_path, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700776 goto fail;
777 }
778 }
779
780 ut.actime = apk_stat.st_atime;
781 ut.modtime = apk_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700782 utime(out_path, &ut);
783
784 close(out_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700785 close(zip_fd);
786 return 0;
787
788fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700789 if (out_fd >= 0) {
790 close(out_fd);
791 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700792 }
793 if (zip_fd >= 0) {
794 close(zip_fd);
795 }
796 return -1;
797}
798
799void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
800 struct stat* statbuf)
801{
802 while (path[basepos] != 0) {
803 if (path[basepos] == '/') {
804 path[basepos] = 0;
805 if (lstat(path, statbuf) < 0) {
806 ALOGV("Making directory: %s\n", path);
807 if (mkdir(path, mode) == 0) {
808 chown(path, uid, gid);
809 } else {
810 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
811 }
812 }
813 path[basepos] = '/';
814 basepos++;
815 }
816 basepos++;
817 }
818}
819
820int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
821 int dstuid, int dstgid, struct stat* statbuf)
822{
823 DIR *d;
824 struct dirent *de;
825 int res;
826
827 int srcend = strlen(srcpath);
828 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -0800829
Mike Lockwood94afecf2012-10-24 10:45:23 -0700830 if (lstat(srcpath, statbuf) < 0) {
831 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
832 return 1;
833 }
Dave Allisond9370732014-01-30 14:19:23 -0800834
Mike Lockwood94afecf2012-10-24 10:45:23 -0700835 if ((statbuf->st_mode&S_IFDIR) == 0) {
836 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
837 dstuid, dstgid, statbuf);
838 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
839 if (rename(srcpath, dstpath) >= 0) {
840 if (chown(dstpath, dstuid, dstgid) < 0) {
841 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
842 unlink(dstpath);
843 return 1;
844 }
845 } else {
846 ALOGW("Unable to rename %s to %s: %s\n",
847 srcpath, dstpath, strerror(errno));
848 return 1;
849 }
850 return 0;
851 }
852
853 d = opendir(srcpath);
854 if (d == NULL) {
855 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
856 return 1;
857 }
858
859 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -0800860
Mike Lockwood94afecf2012-10-24 10:45:23 -0700861 while ((de = readdir(d))) {
862 const char *name = de->d_name;
863 /* always skip "." and ".." */
864 if (name[0] == '.') {
865 if (name[1] == 0) continue;
866 if ((name[1] == '.') && (name[2] == 0)) continue;
867 }
Dave Allisond9370732014-01-30 14:19:23 -0800868
Mike Lockwood94afecf2012-10-24 10:45:23 -0700869 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
870 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
871 continue;
872 }
Dave Allisond9370732014-01-30 14:19:23 -0800873
Mike Lockwood94afecf2012-10-24 10:45:23 -0700874 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
875 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
876 continue;
877 }
Dave Allisond9370732014-01-30 14:19:23 -0800878
Mike Lockwood94afecf2012-10-24 10:45:23 -0700879 srcpath[srcend] = dstpath[dstend] = '/';
880 strcpy(srcpath+srcend+1, name);
881 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -0800882
Mike Lockwood94afecf2012-10-24 10:45:23 -0700883 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
884 res = 1;
885 }
Dave Allisond9370732014-01-30 14:19:23 -0800886
Mike Lockwood94afecf2012-10-24 10:45:23 -0700887 // Note: we will be leaving empty directories behind in srcpath,
888 // but that is okay, the package manager will be erasing all of the
889 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -0800890
Mike Lockwood94afecf2012-10-24 10:45:23 -0700891 srcpath[srcend] = dstpath[dstend] = 0;
892 }
Dave Allisond9370732014-01-30 14:19:23 -0800893
Mike Lockwood94afecf2012-10-24 10:45:23 -0700894 closedir(d);
895 return res;
896}
897
898int movefiles()
899{
900 DIR *d;
901 int dfd, subfd;
902 struct dirent *de;
903 struct stat s;
904 char buf[PKG_PATH_MAX+1];
905 int bufp, bufe, bufi, readlen;
906
907 char srcpkg[PKG_NAME_MAX];
908 char dstpkg[PKG_NAME_MAX];
909 char srcpath[PKG_PATH_MAX];
910 char dstpath[PKG_PATH_MAX];
911 int dstuid=-1, dstgid=-1;
912 int hasspace;
913
914 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
915 if (d == NULL) {
916 goto done;
917 }
918 dfd = dirfd(d);
919
920 /* Iterate through all files in the directory, executing the
921 * file movements requested there-in.
922 */
923 while ((de = readdir(d))) {
924 const char *name = de->d_name;
925
926 if (de->d_type == DT_DIR) {
927 continue;
928 } else {
929 subfd = openat(dfd, name, O_RDONLY);
930 if (subfd < 0) {
931 ALOGW("Unable to open update commands at %s%s\n",
932 UPDATE_COMMANDS_DIR_PREFIX, name);
933 continue;
934 }
Dave Allisond9370732014-01-30 14:19:23 -0800935
Mike Lockwood94afecf2012-10-24 10:45:23 -0700936 bufp = 0;
937 bufe = 0;
938 buf[PKG_PATH_MAX] = 0;
939 srcpkg[0] = dstpkg[0] = 0;
940 while (1) {
941 bufi = bufp;
942 while (bufi < bufe && buf[bufi] != '\n') {
943 bufi++;
944 }
945 if (bufi < bufe) {
946 buf[bufi] = 0;
947 ALOGV("Processing line: %s\n", buf+bufp);
948 hasspace = 0;
949 while (bufp < bufi && isspace(buf[bufp])) {
950 hasspace = 1;
951 bufp++;
952 }
953 if (buf[bufp] == '#' || bufp == bufi) {
954 // skip comments and empty lines.
955 } else if (hasspace) {
956 if (dstpkg[0] == 0) {
957 ALOGW("Path before package line in %s%s: %s\n",
958 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
959 } else if (srcpkg[0] == 0) {
960 // Skip -- source package no longer exists.
961 } else {
962 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
963 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
964 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
965 movefileordir(srcpath, dstpath,
966 strlen(dstpath)-strlen(buf+bufp),
967 dstuid, dstgid, &s);
968 }
969 }
970 } else {
971 char* div = strchr(buf+bufp, ':');
972 if (div == NULL) {
973 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
974 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
975 } else {
976 *div = 0;
977 div++;
978 if (strlen(buf+bufp) < PKG_NAME_MAX) {
979 strcpy(dstpkg, buf+bufp);
980 } else {
981 srcpkg[0] = dstpkg[0] = 0;
982 ALOGW("Package name too long in %s%s: %s\n",
983 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
984 }
985 if (strlen(div) < PKG_NAME_MAX) {
986 strcpy(srcpkg, div);
987 } else {
988 srcpkg[0] = dstpkg[0] = 0;
989 ALOGW("Package name too long in %s%s: %s\n",
990 UPDATE_COMMANDS_DIR_PREFIX, name, div);
991 }
992 if (srcpkg[0] != 0) {
993 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
994 if (lstat(srcpath, &s) < 0) {
995 // Package no longer exists -- skip.
996 srcpkg[0] = 0;
997 }
998 } else {
999 srcpkg[0] = 0;
1000 ALOGW("Can't create path %s in %s%s\n",
1001 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1002 }
1003 if (srcpkg[0] != 0) {
1004 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1005 if (lstat(dstpath, &s) == 0) {
1006 dstuid = s.st_uid;
1007 dstgid = s.st_gid;
1008 } else {
1009 // Destination package doesn't
1010 // exist... due to original-package,
1011 // this is normal, so don't be
1012 // noisy about it.
1013 srcpkg[0] = 0;
1014 }
1015 } else {
1016 srcpkg[0] = 0;
1017 ALOGW("Can't create path %s in %s%s\n",
1018 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1019 }
1020 }
1021 ALOGV("Transfering from %s to %s: uid=%d\n",
1022 srcpkg, dstpkg, dstuid);
1023 }
1024 }
1025 }
1026 bufp = bufi+1;
1027 } else {
1028 if (bufp == 0) {
1029 if (bufp < bufe) {
1030 ALOGW("Line too long in %s%s, skipping: %s\n",
1031 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1032 }
1033 } else if (bufp < bufe) {
1034 memcpy(buf, buf+bufp, bufe-bufp);
1035 bufe -= bufp;
1036 bufp = 0;
1037 }
1038 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1039 if (readlen < 0) {
1040 ALOGW("Failure reading update commands in %s%s: %s\n",
1041 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1042 break;
1043 } else if (readlen == 0) {
1044 break;
1045 }
1046 bufe += readlen;
1047 buf[bufe] = 0;
1048 ALOGV("Read buf: %s\n", buf);
1049 }
1050 }
1051 close(subfd);
1052 }
1053 }
1054 closedir(d);
1055done:
1056 return 0;
1057}
1058
1059int linklib(const char* pkgname, const char* asecLibDir, int userId)
1060{
1061 char pkgdir[PKG_PATH_MAX];
1062 char libsymlink[PKG_PATH_MAX];
1063 struct stat s, libStat;
1064 int rc = 0;
1065
1066 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1067 ALOGE("cannot create package path\n");
1068 return -1;
1069 }
1070 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1071 ALOGE("cannot create package lib symlink origin path\n");
1072 return -1;
1073 }
1074
1075 if (stat(pkgdir, &s) < 0) return -1;
1076
1077 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1078 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1079 return -1;
1080 }
1081
1082 if (chmod(pkgdir, 0700) < 0) {
1083 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1084 rc = -1;
1085 goto out;
1086 }
1087
1088 if (lstat(libsymlink, &libStat) < 0) {
1089 if (errno != ENOENT) {
1090 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1091 rc = -1;
1092 goto out;
1093 }
1094 } else {
1095 if (S_ISDIR(libStat.st_mode)) {
1096 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1097 rc = -1;
1098 goto out;
1099 }
1100 } else if (S_ISLNK(libStat.st_mode)) {
1101 if (unlink(libsymlink) < 0) {
1102 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1103 rc = -1;
1104 goto out;
1105 }
1106 }
1107 }
1108
1109 if (symlink(asecLibDir, libsymlink) < 0) {
1110 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1111 strerror(errno));
1112 rc = -errno;
1113 goto out;
1114 }
1115
1116out:
1117 if (chmod(pkgdir, s.st_mode) < 0) {
1118 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1119 rc = -errno;
1120 }
1121
1122 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1123 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1124 return -errno;
1125 }
1126
1127 return rc;
1128}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001129
1130static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1131{
1132 static const char *IDMAP_BIN = "/system/bin/idmap";
1133 static const size_t MAX_INT_LEN = 32;
1134 char idmap_str[MAX_INT_LEN];
1135
1136 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1137
1138 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1139 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1140}
1141
1142// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1143// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1144static int flatten_path(const char *prefix, const char *suffix,
1145 const char *overlay_path, char *idmap_path, size_t N)
1146{
1147 if (overlay_path == NULL || idmap_path == NULL) {
1148 return -1;
1149 }
1150 const size_t len_overlay_path = strlen(overlay_path);
1151 // will access overlay_path + 1 further below; requires absolute path
1152 if (len_overlay_path < 2 || *overlay_path != '/') {
1153 return -1;
1154 }
1155 const size_t len_idmap_root = strlen(prefix);
1156 const size_t len_suffix = strlen(suffix);
1157 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1158 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1159 // additions below would cause overflow
1160 return -1;
1161 }
1162 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1163 return -1;
1164 }
1165 memset(idmap_path, 0, N);
1166 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1167 char *ch = idmap_path + len_idmap_root;
1168 while (*ch != '\0') {
1169 if (*ch == '/') {
1170 *ch = '@';
1171 }
1172 ++ch;
1173 }
1174 return 0;
1175}
1176
1177int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1178{
1179 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1180
1181 int idmap_fd = -1;
1182 char idmap_path[PATH_MAX];
1183
1184 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1185 idmap_path, sizeof(idmap_path)) == -1) {
1186 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1187 goto fail;
1188 }
1189
1190 unlink(idmap_path);
1191 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1192 if (idmap_fd < 0) {
1193 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1194 goto fail;
1195 }
1196 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1197 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1198 goto fail;
1199 }
1200 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1201 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1202 goto fail;
1203 }
1204
1205 pid_t pid;
1206 pid = fork();
1207 if (pid == 0) {
1208 /* child -- drop privileges before continuing */
1209 if (setgid(uid) != 0) {
1210 ALOGE("setgid(%d) failed during idmap\n", uid);
1211 exit(1);
1212 }
1213 if (setuid(uid) != 0) {
1214 ALOGE("setuid(%d) failed during idmap\n", uid);
1215 exit(1);
1216 }
1217 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1218 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1219 exit(1);
1220 }
1221
1222 run_idmap(target_apk, overlay_apk, idmap_fd);
1223 exit(1); /* only if exec call to idmap failed */
1224 } else {
1225 int status = wait_child(pid);
1226 if (status != 0) {
1227 ALOGE("idmap failed, status=0x%04x\n", status);
1228 goto fail;
1229 }
1230 }
1231
1232 close(idmap_fd);
1233 return 0;
1234fail:
1235 if (idmap_fd >= 0) {
1236 close(idmap_fd);
1237 unlink(idmap_path);
1238 }
1239 return -1;
1240}
Robert Craige9887e42014-02-20 10:25:56 -05001241
Robert Craigda30dc72014-03-27 10:21:12 -04001242int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001243{
Robert Craigda30dc72014-03-27 10:21:12 -04001244 struct dirent *entry;
1245 DIR *d;
1246 struct stat s;
1247 char *userdir;
1248 char *primarydir;
1249 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001250 int ret = 0;
1251
Robert Craigda30dc72014-03-27 10:21:12 -04001252 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1253 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1254
1255 if (!pkgName || !seinfo) {
1256 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001257 return -1;
1258 }
1259
Robert Craigda30dc72014-03-27 10:21:12 -04001260 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1261 return -1;
1262 }
1263
1264 // Relabel for primary user.
1265 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1266 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001267 ret |= -1;
1268 }
1269
Robert Craigda30dc72014-03-27 10:21:12 -04001270 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1271 free(primarydir);
1272 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001273 }
1274
Robert Craigda30dc72014-03-27 10:21:12 -04001275 // Relabel package directory for all secondary users.
1276 d = opendir(userdir);
1277 if (d == NULL) {
1278 free(primarydir);
1279 free(userdir);
1280 return -1;
1281 }
1282
1283 while ((entry = readdir(d))) {
1284 if (entry->d_type != DT_DIR) {
1285 continue;
1286 }
1287
1288 const char *user = entry->d_name;
1289 // Ignore "." and ".."
1290 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1291 continue;
1292 }
1293
1294 // user directories start with a number
1295 if (user[0] < '0' || user[0] > '9') {
1296 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1297 continue;
1298 }
1299
1300 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1301 continue;
1302 }
1303
1304 if (stat(pkgdir, &s) < 0) {
1305 free(pkgdir);
1306 continue;
1307 }
1308
1309 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) {
1310 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1311 ret |= -1;
1312 }
1313 free(pkgdir);
1314 }
1315
1316 closedir(d);
1317 free(primarydir);
1318 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001319 return ret;
1320}