blob: cfb80e3ce41101e46708285af996db6ed4d2ac59 [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
Narayan Kamath1b400322014-04-11 13:17:00 +0100390int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700391{
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
Narayan Kamath1b400322014-04-11 13:17:00 +0100398 if (create_cache_path(src_dex, src, instruction_set)) return -1;
399 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700400
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
Narayan Kamath1b400322014-04-11 13:17:00 +0100410int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700411{
412 char dex_path[PKG_PATH_MAX];
413
414 if (validate_apk_path(path)) return -1;
Narayan Kamath1b400322014-04-11 13:17:00 +0100415 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700416
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,
Narayan Kamath1b400322014-04-11 13:17:00 +0100428 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
429 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700430{
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 */
Narayan Kamath1b400322014-04-11 13:17:00 +0100459 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700460 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
Narayan Kamath1b400322014-04-11 13:17:00 +0100546int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700547{
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) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100564 strlen(instruction_set) +
565 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800566
Mike Lockwood94afecf2012-10-24 10:45:23 -0700567 if (dstlen > PKG_PATH_MAX) {
568 return -1;
569 }
570
Narayan Kamath1b400322014-04-11 13:17:00 +0100571 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700572 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100573 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700574 src + 1, /* skip the leading / */
575 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800576
Narayan Kamath1b400322014-04-11 13:17:00 +0100577 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700578 if (*tmp == '/') {
579 *tmp = '@';
580 }
581 }
582
583 return 0;
584}
585
586static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800587 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700588{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800589 /* platform-specific flags affecting optimization and verification */
590 char dexopt_flags[PROPERTY_VALUE_MAX];
591 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
592 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
593
Mike Lockwood94afecf2012-10-24 10:45:23 -0700594 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
595 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
596 char zip_num[MAX_INT_LEN];
597 char odex_num[MAX_INT_LEN];
598
599 sprintf(zip_num, "%d", zip_fd);
600 sprintf(odex_num, "%d", odex_fd);
601
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700602 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700603 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
604 dexopt_flags, (char*) NULL);
605 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
606}
607
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700608static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Narayan Kamath1b400322014-04-11 13:17:00 +0100609 const char* output_file_name, const char *pkgname, const char *instruction_set)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700610{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800611 char dex2oat_flags[PROPERTY_VALUE_MAX];
612 property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, "");
613 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
614
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700615 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
616 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100617 static const unsigned int MAX_INSTRUCTION_SET_LEN = 32;
618
619 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
620 ALOGE("Instruction set %s longer than max length of %d",
621 instruction_set, MAX_INSTRUCTION_SET_LEN);
622 return;
623 }
624
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700625 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
626 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
627 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
628 char oat_location_arg[strlen("--oat-name=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100629 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
630 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700631
632 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
633 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
634 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
635 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100636 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Dave Allisond9370732014-01-30 14:19:23 -0800637 if (strcmp(pkgname, "*") != 0) {
Narayan Kamath1b400322014-04-11 13:17:00 +0100638 snprintf(profile_file_arg, sizeof(profile_file_arg), "--profile-file=%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800639 DALVIK_CACHE_PREFIX "profiles", pkgname);
640 } else {
Narayan Kamath1b400322014-04-11 13:17:00 +0100641 strcpy(profile_file_arg, "--no-profile-file");
Dave Allisond9370732014-01-30 14:19:23 -0800642 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700643
644 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
645 execl(DEX2OAT_BIN, DEX2OAT_BIN,
646 zip_fd_arg, zip_location_arg,
647 oat_fd_arg, oat_location_arg,
Narayan Kamath1b400322014-04-11 13:17:00 +0100648 profile_file_arg, instruction_set_arg,
Anwar Ghuloum4bc05402014-03-11 15:42:58 -0700649 strlen(dex2oat_flags) > 0 ? dex2oat_flags : NULL,
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700650 (char*) NULL);
651 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
652}
653
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100654static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700655{
656 int status;
657 pid_t got_pid;
658
Mike Lockwood94afecf2012-10-24 10:45:23 -0700659 while (1) {
660 got_pid = waitpid(pid, &status, 0);
661 if (got_pid == -1 && errno == EINTR) {
662 printf("waitpid interrupted, retrying\n");
663 } else {
664 break;
665 }
666 }
667 if (got_pid != pid) {
668 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
669 (int) pid, (int) got_pid, strerror(errno));
670 return 1;
671 }
672
673 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700674 return 0;
675 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700676 return status; /* always nonzero */
677 }
678}
679
Dave Allisond9370732014-01-30 14:19:23 -0800680int dexopt(const char *apk_path, uid_t uid, int is_public,
Narayan Kamath1b400322014-04-11 13:17:00 +0100681 const char *pkgname, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700682{
683 struct utimbuf ut;
684 struct stat apk_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700685 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700686 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700687 char *end;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700688 int res, zip_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700689
Mike Lockwood94afecf2012-10-24 10:45:23 -0700690 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
691 return -1;
692 }
693
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800694 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom0c05d3a2014-01-30 13:14:01 -0800695 property_get("persist.sys.dalvik.vm.lib.1", persist_sys_dalvik_vm_lib, "libdvm.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700696
697 /* Before anything else: is there a .odex file? If so, we have
698 * precompiled the apk and there is nothing to do here.
699 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800700 strcpy(out_path, apk_path);
701 end = strrchr(out_path, '.');
702 if (end != NULL) {
703 strcpy(end, ".odex");
704 if (stat(out_path, &dex_stat) == 0) {
705 return 0;
706 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700707 }
708
Narayan Kamath1b400322014-04-11 13:17:00 +0100709 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700710 return -1;
711 }
712
713 memset(&apk_stat, 0, sizeof(apk_stat));
714 stat(apk_path, &apk_stat);
715
716 zip_fd = open(apk_path, O_RDONLY, 0);
717 if (zip_fd < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700718 ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700719 return -1;
720 }
721
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700722 unlink(out_path);
723 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
724 if (out_fd < 0) {
725 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700726 goto fail;
727 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700728 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700729 S_IRUSR|S_IWUSR|S_IRGRP |
730 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700731 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700732 goto fail;
733 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700734 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
735 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700736 goto fail;
737 }
738
Dave Allisond9370732014-01-30 14:19:23 -0800739 // Create profile file if there is a package name present.
740 if (strcmp(pkgname, "*") != 0) {
741 create_profile_file(pkgname, uid);
742 }
743
744
Mike Lockwood94afecf2012-10-24 10:45:23 -0700745 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
746
747 pid_t pid;
748 pid = fork();
749 if (pid == 0) {
750 /* child -- drop privileges before continuing */
751 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700752 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700753 exit(64);
754 }
755 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700756 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700757 exit(65);
758 }
759 // drop capabilities
760 struct __user_cap_header_struct capheader;
761 struct __user_cap_data_struct capdata[2];
762 memset(&capheader, 0, sizeof(capheader));
763 memset(&capdata, 0, sizeof(capdata));
764 capheader.version = _LINUX_CAPABILITY_VERSION_3;
765 if (capset(&capheader, &capdata[0]) < 0) {
766 ALOGE("capset failed: %s\n", strerror(errno));
767 exit(66);
768 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700769 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
770 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700771 exit(67);
772 }
773
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700774 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800775 run_dexopt(zip_fd, out_fd, apk_path, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700776 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Narayan Kamath1b400322014-04-11 13:17:00 +0100777 run_dex2oat(zip_fd, out_fd, apk_path, out_path, pkgname, instruction_set);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700778 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700779 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700780 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700781 exit(68); /* only get here on exec failure */
782 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100783 res = wait_child(pid);
784 if (res == 0) {
785 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
786 } else {
787 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", apk_path, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700788 goto fail;
789 }
790 }
791
792 ut.actime = apk_stat.st_atime;
793 ut.modtime = apk_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700794 utime(out_path, &ut);
795
796 close(out_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700797 close(zip_fd);
798 return 0;
799
800fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700801 if (out_fd >= 0) {
802 close(out_fd);
803 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700804 }
805 if (zip_fd >= 0) {
806 close(zip_fd);
807 }
808 return -1;
809}
810
811void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
812 struct stat* statbuf)
813{
814 while (path[basepos] != 0) {
815 if (path[basepos] == '/') {
816 path[basepos] = 0;
817 if (lstat(path, statbuf) < 0) {
818 ALOGV("Making directory: %s\n", path);
819 if (mkdir(path, mode) == 0) {
820 chown(path, uid, gid);
821 } else {
822 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
823 }
824 }
825 path[basepos] = '/';
826 basepos++;
827 }
828 basepos++;
829 }
830}
831
832int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
833 int dstuid, int dstgid, struct stat* statbuf)
834{
835 DIR *d;
836 struct dirent *de;
837 int res;
838
839 int srcend = strlen(srcpath);
840 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -0800841
Mike Lockwood94afecf2012-10-24 10:45:23 -0700842 if (lstat(srcpath, statbuf) < 0) {
843 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
844 return 1;
845 }
Dave Allisond9370732014-01-30 14:19:23 -0800846
Mike Lockwood94afecf2012-10-24 10:45:23 -0700847 if ((statbuf->st_mode&S_IFDIR) == 0) {
848 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
849 dstuid, dstgid, statbuf);
850 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
851 if (rename(srcpath, dstpath) >= 0) {
852 if (chown(dstpath, dstuid, dstgid) < 0) {
853 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
854 unlink(dstpath);
855 return 1;
856 }
857 } else {
858 ALOGW("Unable to rename %s to %s: %s\n",
859 srcpath, dstpath, strerror(errno));
860 return 1;
861 }
862 return 0;
863 }
864
865 d = opendir(srcpath);
866 if (d == NULL) {
867 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
868 return 1;
869 }
870
871 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -0800872
Mike Lockwood94afecf2012-10-24 10:45:23 -0700873 while ((de = readdir(d))) {
874 const char *name = de->d_name;
875 /* always skip "." and ".." */
876 if (name[0] == '.') {
877 if (name[1] == 0) continue;
878 if ((name[1] == '.') && (name[2] == 0)) continue;
879 }
Dave Allisond9370732014-01-30 14:19:23 -0800880
Mike Lockwood94afecf2012-10-24 10:45:23 -0700881 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
882 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
883 continue;
884 }
Dave Allisond9370732014-01-30 14:19:23 -0800885
Mike Lockwood94afecf2012-10-24 10:45:23 -0700886 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
887 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
888 continue;
889 }
Dave Allisond9370732014-01-30 14:19:23 -0800890
Mike Lockwood94afecf2012-10-24 10:45:23 -0700891 srcpath[srcend] = dstpath[dstend] = '/';
892 strcpy(srcpath+srcend+1, name);
893 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -0800894
Mike Lockwood94afecf2012-10-24 10:45:23 -0700895 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
896 res = 1;
897 }
Dave Allisond9370732014-01-30 14:19:23 -0800898
Mike Lockwood94afecf2012-10-24 10:45:23 -0700899 // Note: we will be leaving empty directories behind in srcpath,
900 // but that is okay, the package manager will be erasing all of the
901 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -0800902
Mike Lockwood94afecf2012-10-24 10:45:23 -0700903 srcpath[srcend] = dstpath[dstend] = 0;
904 }
Dave Allisond9370732014-01-30 14:19:23 -0800905
Mike Lockwood94afecf2012-10-24 10:45:23 -0700906 closedir(d);
907 return res;
908}
909
910int movefiles()
911{
912 DIR *d;
913 int dfd, subfd;
914 struct dirent *de;
915 struct stat s;
916 char buf[PKG_PATH_MAX+1];
917 int bufp, bufe, bufi, readlen;
918
919 char srcpkg[PKG_NAME_MAX];
920 char dstpkg[PKG_NAME_MAX];
921 char srcpath[PKG_PATH_MAX];
922 char dstpath[PKG_PATH_MAX];
923 int dstuid=-1, dstgid=-1;
924 int hasspace;
925
926 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
927 if (d == NULL) {
928 goto done;
929 }
930 dfd = dirfd(d);
931
932 /* Iterate through all files in the directory, executing the
933 * file movements requested there-in.
934 */
935 while ((de = readdir(d))) {
936 const char *name = de->d_name;
937
938 if (de->d_type == DT_DIR) {
939 continue;
940 } else {
941 subfd = openat(dfd, name, O_RDONLY);
942 if (subfd < 0) {
943 ALOGW("Unable to open update commands at %s%s\n",
944 UPDATE_COMMANDS_DIR_PREFIX, name);
945 continue;
946 }
Dave Allisond9370732014-01-30 14:19:23 -0800947
Mike Lockwood94afecf2012-10-24 10:45:23 -0700948 bufp = 0;
949 bufe = 0;
950 buf[PKG_PATH_MAX] = 0;
951 srcpkg[0] = dstpkg[0] = 0;
952 while (1) {
953 bufi = bufp;
954 while (bufi < bufe && buf[bufi] != '\n') {
955 bufi++;
956 }
957 if (bufi < bufe) {
958 buf[bufi] = 0;
959 ALOGV("Processing line: %s\n", buf+bufp);
960 hasspace = 0;
961 while (bufp < bufi && isspace(buf[bufp])) {
962 hasspace = 1;
963 bufp++;
964 }
965 if (buf[bufp] == '#' || bufp == bufi) {
966 // skip comments and empty lines.
967 } else if (hasspace) {
968 if (dstpkg[0] == 0) {
969 ALOGW("Path before package line in %s%s: %s\n",
970 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
971 } else if (srcpkg[0] == 0) {
972 // Skip -- source package no longer exists.
973 } else {
974 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
975 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
976 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
977 movefileordir(srcpath, dstpath,
978 strlen(dstpath)-strlen(buf+bufp),
979 dstuid, dstgid, &s);
980 }
981 }
982 } else {
983 char* div = strchr(buf+bufp, ':');
984 if (div == NULL) {
985 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
986 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
987 } else {
988 *div = 0;
989 div++;
990 if (strlen(buf+bufp) < PKG_NAME_MAX) {
991 strcpy(dstpkg, buf+bufp);
992 } else {
993 srcpkg[0] = dstpkg[0] = 0;
994 ALOGW("Package name too long in %s%s: %s\n",
995 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
996 }
997 if (strlen(div) < PKG_NAME_MAX) {
998 strcpy(srcpkg, div);
999 } else {
1000 srcpkg[0] = dstpkg[0] = 0;
1001 ALOGW("Package name too long in %s%s: %s\n",
1002 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1003 }
1004 if (srcpkg[0] != 0) {
1005 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1006 if (lstat(srcpath, &s) < 0) {
1007 // Package no longer exists -- skip.
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 if (srcpkg[0] != 0) {
1016 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1017 if (lstat(dstpath, &s) == 0) {
1018 dstuid = s.st_uid;
1019 dstgid = s.st_gid;
1020 } else {
1021 // Destination package doesn't
1022 // exist... due to original-package,
1023 // this is normal, so don't be
1024 // noisy about it.
1025 srcpkg[0] = 0;
1026 }
1027 } else {
1028 srcpkg[0] = 0;
1029 ALOGW("Can't create path %s in %s%s\n",
1030 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1031 }
1032 }
1033 ALOGV("Transfering from %s to %s: uid=%d\n",
1034 srcpkg, dstpkg, dstuid);
1035 }
1036 }
1037 }
1038 bufp = bufi+1;
1039 } else {
1040 if (bufp == 0) {
1041 if (bufp < bufe) {
1042 ALOGW("Line too long in %s%s, skipping: %s\n",
1043 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1044 }
1045 } else if (bufp < bufe) {
1046 memcpy(buf, buf+bufp, bufe-bufp);
1047 bufe -= bufp;
1048 bufp = 0;
1049 }
1050 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1051 if (readlen < 0) {
1052 ALOGW("Failure reading update commands in %s%s: %s\n",
1053 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1054 break;
1055 } else if (readlen == 0) {
1056 break;
1057 }
1058 bufe += readlen;
1059 buf[bufe] = 0;
1060 ALOGV("Read buf: %s\n", buf);
1061 }
1062 }
1063 close(subfd);
1064 }
1065 }
1066 closedir(d);
1067done:
1068 return 0;
1069}
1070
1071int linklib(const char* pkgname, const char* asecLibDir, int userId)
1072{
1073 char pkgdir[PKG_PATH_MAX];
1074 char libsymlink[PKG_PATH_MAX];
1075 struct stat s, libStat;
1076 int rc = 0;
1077
1078 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1079 ALOGE("cannot create package path\n");
1080 return -1;
1081 }
1082 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1083 ALOGE("cannot create package lib symlink origin path\n");
1084 return -1;
1085 }
1086
1087 if (stat(pkgdir, &s) < 0) return -1;
1088
1089 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1090 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1091 return -1;
1092 }
1093
1094 if (chmod(pkgdir, 0700) < 0) {
1095 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1096 rc = -1;
1097 goto out;
1098 }
1099
1100 if (lstat(libsymlink, &libStat) < 0) {
1101 if (errno != ENOENT) {
1102 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1103 rc = -1;
1104 goto out;
1105 }
1106 } else {
1107 if (S_ISDIR(libStat.st_mode)) {
1108 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1109 rc = -1;
1110 goto out;
1111 }
1112 } else if (S_ISLNK(libStat.st_mode)) {
1113 if (unlink(libsymlink) < 0) {
1114 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1115 rc = -1;
1116 goto out;
1117 }
1118 }
1119 }
1120
1121 if (symlink(asecLibDir, libsymlink) < 0) {
1122 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1123 strerror(errno));
1124 rc = -errno;
1125 goto out;
1126 }
1127
1128out:
1129 if (chmod(pkgdir, s.st_mode) < 0) {
1130 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1131 rc = -errno;
1132 }
1133
1134 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1135 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1136 return -errno;
1137 }
1138
1139 return rc;
1140}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001141
1142static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1143{
1144 static const char *IDMAP_BIN = "/system/bin/idmap";
1145 static const size_t MAX_INT_LEN = 32;
1146 char idmap_str[MAX_INT_LEN];
1147
1148 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1149
1150 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1151 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1152}
1153
1154// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1155// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1156static int flatten_path(const char *prefix, const char *suffix,
1157 const char *overlay_path, char *idmap_path, size_t N)
1158{
1159 if (overlay_path == NULL || idmap_path == NULL) {
1160 return -1;
1161 }
1162 const size_t len_overlay_path = strlen(overlay_path);
1163 // will access overlay_path + 1 further below; requires absolute path
1164 if (len_overlay_path < 2 || *overlay_path != '/') {
1165 return -1;
1166 }
1167 const size_t len_idmap_root = strlen(prefix);
1168 const size_t len_suffix = strlen(suffix);
1169 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1170 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1171 // additions below would cause overflow
1172 return -1;
1173 }
1174 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1175 return -1;
1176 }
1177 memset(idmap_path, 0, N);
1178 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1179 char *ch = idmap_path + len_idmap_root;
1180 while (*ch != '\0') {
1181 if (*ch == '/') {
1182 *ch = '@';
1183 }
1184 ++ch;
1185 }
1186 return 0;
1187}
1188
1189int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1190{
1191 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1192
1193 int idmap_fd = -1;
1194 char idmap_path[PATH_MAX];
1195
1196 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1197 idmap_path, sizeof(idmap_path)) == -1) {
1198 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1199 goto fail;
1200 }
1201
1202 unlink(idmap_path);
1203 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1204 if (idmap_fd < 0) {
1205 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1206 goto fail;
1207 }
1208 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1209 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1210 goto fail;
1211 }
1212 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1213 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1214 goto fail;
1215 }
1216
1217 pid_t pid;
1218 pid = fork();
1219 if (pid == 0) {
1220 /* child -- drop privileges before continuing */
1221 if (setgid(uid) != 0) {
1222 ALOGE("setgid(%d) failed during idmap\n", uid);
1223 exit(1);
1224 }
1225 if (setuid(uid) != 0) {
1226 ALOGE("setuid(%d) failed during idmap\n", uid);
1227 exit(1);
1228 }
1229 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1230 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1231 exit(1);
1232 }
1233
1234 run_idmap(target_apk, overlay_apk, idmap_fd);
1235 exit(1); /* only if exec call to idmap failed */
1236 } else {
1237 int status = wait_child(pid);
1238 if (status != 0) {
1239 ALOGE("idmap failed, status=0x%04x\n", status);
1240 goto fail;
1241 }
1242 }
1243
1244 close(idmap_fd);
1245 return 0;
1246fail:
1247 if (idmap_fd >= 0) {
1248 close(idmap_fd);
1249 unlink(idmap_path);
1250 }
1251 return -1;
1252}
Robert Craige9887e42014-02-20 10:25:56 -05001253
Robert Craigda30dc72014-03-27 10:21:12 -04001254int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001255{
Robert Craigda30dc72014-03-27 10:21:12 -04001256 struct dirent *entry;
1257 DIR *d;
1258 struct stat s;
1259 char *userdir;
1260 char *primarydir;
1261 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001262 int ret = 0;
1263
Robert Craigda30dc72014-03-27 10:21:12 -04001264 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1265 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1266
1267 if (!pkgName || !seinfo) {
1268 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001269 return -1;
1270 }
1271
Robert Craigda30dc72014-03-27 10:21:12 -04001272 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1273 return -1;
1274 }
1275
1276 // Relabel for primary user.
1277 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1278 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001279 ret |= -1;
1280 }
1281
Robert Craigda30dc72014-03-27 10:21:12 -04001282 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1283 free(primarydir);
1284 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001285 }
1286
Robert Craigda30dc72014-03-27 10:21:12 -04001287 // Relabel package directory for all secondary users.
1288 d = opendir(userdir);
1289 if (d == NULL) {
1290 free(primarydir);
1291 free(userdir);
1292 return -1;
1293 }
1294
1295 while ((entry = readdir(d))) {
1296 if (entry->d_type != DT_DIR) {
1297 continue;
1298 }
1299
1300 const char *user = entry->d_name;
1301 // Ignore "." and ".."
1302 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1303 continue;
1304 }
1305
1306 // user directories start with a number
1307 if (user[0] < '0' || user[0] > '9') {
1308 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1309 continue;
1310 }
1311
1312 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1313 continue;
1314 }
1315
1316 if (stat(pkgdir, &s) < 0) {
1317 free(pkgdir);
1318 continue;
1319 }
1320
1321 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) {
1322 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1323 ret |= -1;
1324 }
1325 free(pkgdir);
1326 }
1327
1328 closedir(d);
1329 free(primarydir);
1330 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001331 return ret;
1332}