Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 4 | ** 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 10 | ** 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Nick Kralevich | d747129 | 2013-02-28 16:59:13 -0800 | [diff] [blame] | 18 | #include <sys/capability.h> |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 19 | #include "installd.h" |
| 20 | #include <diskusage/dirsize.h> |
| 21 | #include <selinux/android.h> |
| 22 | |
| 23 | /* Directory records that are used in execution of commands. */ |
| 24 | dir_rec_t android_data_dir; |
| 25 | dir_rec_t android_asec_dir; |
| 26 | dir_rec_t android_app_dir; |
| 27 | dir_rec_t android_app_private_dir; |
| 28 | dir_rec_t android_app_lib_dir; |
| 29 | dir_rec_t android_media_dir; |
| 30 | dir_rec_array_t android_system_dirs; |
| 31 | |
Robert Craig | 4d3fd4e | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 32 | int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 33 | { |
| 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 Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 59 | if (mkdir(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 60 | ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); |
| 61 | return -1; |
| 62 | } |
Nick Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 63 | if (chmod(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 64 | 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 Smalley | 2628820 | 2014-02-07 09:16:46 -0500 | [diff] [blame] | 95 | if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 96 | 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 Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 112 | int uninstall(const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 113 | { |
| 114 | char pkgdir[PKG_PATH_MAX]; |
| 115 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 116 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 117 | return -1; |
| 118 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 119 | remove_profile_file(pkgname); |
| 120 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 121 | /* delete contents AND directory, no exceptions */ |
| 122 | return delete_dir_contents(pkgdir, 1, NULL); |
| 123 | } |
| 124 | |
| 125 | int 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 | |
| 142 | int 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 Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 161 | ALOGE("fixing uid of non-root pkg: %s %" PRIu32 " %" PRIu32 "\n", pkgdir, s.st_uid, s.st_gid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 162 | 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 Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 179 | int delete_user_data(const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 180 | { |
| 181 | char pkgdir[PKG_PATH_MAX]; |
| 182 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 183 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 184 | return -1; |
| 185 | |
| 186 | /* delete contents, excluding "lib", but not the directory itself */ |
| 187 | return delete_dir_contents(pkgdir, 0, "lib"); |
| 188 | } |
| 189 | |
Nick Kralevich | e4e91c4 | 2013-09-20 12:45:20 -0700 | [diff] [blame] | 190 | int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 191 | { |
| 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 Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 198 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 199 | return -1; |
| 200 | } |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 201 | if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 202 | 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 Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 210 | if (mkdir(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 211 | ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); |
| 212 | return -errno; |
| 213 | } |
Nick Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 214 | if (chmod(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 215 | 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 Smalley | 2628820 | 2014-02-07 09:16:46 -0500 | [diff] [blame] | 251 | if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 252 | 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 Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 268 | int delete_user(userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 269 | { |
| 270 | char data_path[PKG_PATH_MAX]; |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 271 | if (create_user_path(data_path, userid)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 272 | return -1; |
| 273 | } |
| 274 | if (delete_dir_contents(data_path, 1, NULL)) { |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | char media_path[PATH_MAX]; |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 279 | if (create_user_media_path(media_path, userid) == -1) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | if (delete_dir_contents(media_path, 1, NULL) == -1) { |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 289 | int delete_cache(const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 290 | { |
| 291 | char cachedir[PKG_PATH_MAX]; |
| 292 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 293 | if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 294 | 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 | */ |
| 307 | int 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 Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 325 | if (create_user_path(tmpdir, 0) == 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 326 | //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 | |
| 390 | int 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 | |
| 410 | int 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 Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 426 | int get_size(const char *pkgname, userid_t userid, const char *apkpath, |
Dianne Hackborn | 8b41780 | 2013-05-01 18:55:10 -0700 | [diff] [blame] | 427 | const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 428 | 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 Hackborn | 8b41780 | 2013-05-01 18:55:10 -0700 | [diff] [blame] | 466 | if (libdirpath != NULL && libdirpath[0] != '!') { |
| 467 | d = opendir(libdirpath); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 468 | 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 Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 483 | if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 484 | 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); |
| 537 | done: |
| 538 | *_codesize = codesize; |
| 539 | *_datasize = datasize; |
| 540 | *_cachesize = cachesize; |
| 541 | *_asecsize = asecsize; |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 546 | int 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 563 | dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) + |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 564 | strlen(DALVIK_CACHE_POSTFIX) + 1; |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 565 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 566 | 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 574 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 575 | for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) { |
| 576 | if (*tmp == '/') { |
| 577 | *tmp = '@'; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name, |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 585 | const char* output_file_name) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 586 | { |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 587 | /* 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 592 | 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 Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 600 | ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 601 | 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 Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 606 | static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name, |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 607 | const char* output_file_name, const char *pkgname) |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 608 | { |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 609 | 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 Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 613 | 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 619 | char profile_file[strlen("--profile-file=") + PKG_PATH_MAX]; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 620 | |
| 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 625 | 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 Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 631 | |
| 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 636 | profile_file, |
Anwar Ghuloum | 4bc0540 | 2014-03-11 15:42:58 -0700 | [diff] [blame] | 637 | strlen(dex2oat_flags) > 0 ? dex2oat_flags : NULL, |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 638 | (char*) NULL); |
| 639 | ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno)); |
| 640 | } |
| 641 | |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 642 | static int wait_child(pid_t pid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 643 | { |
| 644 | int status; |
| 645 | pid_t got_pid; |
| 646 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 647 | 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 662 | return 0; |
| 663 | } else { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 664 | return status; /* always nonzero */ |
| 665 | } |
| 666 | } |
| 667 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 668 | int dexopt(const char *apk_path, uid_t uid, int is_public, |
| 669 | const char *pkgname) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 670 | { |
| 671 | struct utimbuf ut; |
| 672 | struct stat apk_stat, dex_stat; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 673 | char out_path[PKG_PATH_MAX]; |
Brian Carlstrom | e7a8b17 | 2013-06-30 13:17:38 -0700 | [diff] [blame] | 674 | char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX]; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 675 | char *end; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 676 | int res, zip_fd=-1, out_fd=-1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 677 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 678 | if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { |
| 679 | return -1; |
| 680 | } |
| 681 | |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 682 | /* The command to run depend on the value of persist.sys.dalvik.vm.lib */ |
Brian Carlstrom | 0c05d3a | 2014-01-30 13:14:01 -0800 | [diff] [blame] | 683 | property_get("persist.sys.dalvik.vm.lib.1", persist_sys_dalvik_vm_lib, "libdvm.so"); |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 684 | |
| 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 693 | if (create_cache_path(out_path, apk_path)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 694 | 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 Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 702 | ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 703 | return -1; |
| 704 | } |
| 705 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 706 | 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 710 | goto fail; |
| 711 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 712 | if (fchmod(out_fd, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 713 | S_IRUSR|S_IWUSR|S_IRGRP | |
| 714 | (is_public ? S_IROTH : 0)) < 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 715 | ALOGE("installd cannot chmod '%s' during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 716 | goto fail; |
| 717 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 718 | if (fchown(out_fd, AID_SYSTEM, uid) < 0) { |
| 719 | ALOGE("installd cannot chown '%s' during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 720 | goto fail; |
| 721 | } |
| 722 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 723 | // 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 729 | 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 Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 736 | ALOGE("setgid(%d) failed in installd during dexopt\n", uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 737 | exit(64); |
| 738 | } |
| 739 | if (setuid(uid) != 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 740 | ALOGE("setuid(%d) failed in installd during dexopt\n", uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 741 | 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 Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 753 | if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) { |
| 754 | ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 755 | exit(67); |
| 756 | } |
| 757 | |
Brian Carlstrom | e7a8b17 | 2013-06-30 13:17:38 -0700 | [diff] [blame] | 758 | if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) { |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 759 | run_dexopt(zip_fd, out_fd, apk_path, out_path); |
Brian Carlstrom | e7a8b17 | 2013-06-30 13:17:38 -0700 | [diff] [blame] | 760 | } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) { |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 761 | run_dex2oat(zip_fd, out_fd, apk_path, out_path, pkgname); |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 762 | } else { |
Brian Carlstrom | e7a8b17 | 2013-06-30 13:17:38 -0700 | [diff] [blame] | 763 | exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */ |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 764 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 765 | exit(68); /* only get here on exec failure */ |
| 766 | } else { |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 767 | 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 772 | goto fail; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | ut.actime = apk_stat.st_atime; |
| 777 | ut.modtime = apk_stat.st_mtime; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 778 | utime(out_path, &ut); |
| 779 | |
| 780 | close(out_fd); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 781 | close(zip_fd); |
| 782 | return 0; |
| 783 | |
| 784 | fail: |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 785 | if (out_fd >= 0) { |
| 786 | close(out_fd); |
| 787 | unlink(out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 788 | } |
| 789 | if (zip_fd >= 0) { |
| 790 | close(zip_fd); |
| 791 | } |
| 792 | return -1; |
| 793 | } |
| 794 | |
| 795 | void 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 | |
| 816 | int 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 825 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 826 | if (lstat(srcpath, statbuf) < 0) { |
| 827 | ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno)); |
| 828 | return 1; |
| 829 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 830 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 831 | 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 856 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 857 | 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 864 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 865 | if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
| 866 | ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name); |
| 867 | continue; |
| 868 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 869 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 870 | if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
| 871 | ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name); |
| 872 | continue; |
| 873 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 874 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 875 | srcpath[srcend] = dstpath[dstend] = '/'; |
| 876 | strcpy(srcpath+srcend+1, name); |
| 877 | strcpy(dstpath+dstend+1, name); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 878 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 879 | if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) { |
| 880 | res = 1; |
| 881 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 882 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 883 | // 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 886 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 887 | srcpath[srcend] = dstpath[dstend] = 0; |
| 888 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 889 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 890 | closedir(d); |
| 891 | return res; |
| 892 | } |
| 893 | |
| 894 | int 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 931 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 932 | 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); |
| 1051 | done: |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | int 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 | |
| 1112 | out: |
| 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 Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1125 | |
| 1126 | static 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 |
| 1140 | static 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 | |
| 1173 | int 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; |
| 1230 | fail: |
| 1231 | if (idmap_fd >= 0) { |
| 1232 | close(idmap_fd); |
| 1233 | unlink(idmap_path); |
| 1234 | } |
| 1235 | return -1; |
| 1236 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1237 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame^] | 1238 | int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid) |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1239 | { |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame^] | 1240 | struct dirent *entry; |
| 1241 | DIR *d; |
| 1242 | struct stat s; |
| 1243 | char *userdir; |
| 1244 | char *primarydir; |
| 1245 | char *pkgdir; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1246 | int ret = 0; |
| 1247 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame^] | 1248 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
| 1249 | unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE; |
| 1250 | |
| 1251 | if (!pkgName || !seinfo) { |
| 1252 | ALOGE("Package name or seinfo tag is null when trying to restorecon."); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1253 | return -1; |
| 1254 | } |
| 1255 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame^] | 1256 | if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) { |
| 1257 | return -1; |
| 1258 | } |
| 1259 | |
| 1260 | // Relabel for primary user. |
| 1261 | if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) { |
| 1262 | ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno)); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1263 | ret |= -1; |
| 1264 | } |
| 1265 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame^] | 1266 | if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) { |
| 1267 | free(primarydir); |
| 1268 | return -1; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1269 | } |
| 1270 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame^] | 1271 | // Relabel package directory for all secondary users. |
| 1272 | d = opendir(userdir); |
| 1273 | if (d == NULL) { |
| 1274 | free(primarydir); |
| 1275 | free(userdir); |
| 1276 | return -1; |
| 1277 | } |
| 1278 | |
| 1279 | while ((entry = readdir(d))) { |
| 1280 | if (entry->d_type != DT_DIR) { |
| 1281 | continue; |
| 1282 | } |
| 1283 | |
| 1284 | const char *user = entry->d_name; |
| 1285 | // Ignore "." and ".." |
| 1286 | if (!strcmp(user, ".") || !strcmp(user, "..")) { |
| 1287 | continue; |
| 1288 | } |
| 1289 | |
| 1290 | // user directories start with a number |
| 1291 | if (user[0] < '0' || user[0] > '9') { |
| 1292 | ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user); |
| 1293 | continue; |
| 1294 | } |
| 1295 | |
| 1296 | if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) { |
| 1297 | continue; |
| 1298 | } |
| 1299 | |
| 1300 | if (stat(pkgdir, &s) < 0) { |
| 1301 | free(pkgdir); |
| 1302 | continue; |
| 1303 | } |
| 1304 | |
| 1305 | if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) { |
| 1306 | ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno)); |
| 1307 | ret |= -1; |
| 1308 | } |
| 1309 | free(pkgdir); |
| 1310 | } |
| 1311 | |
| 1312 | closedir(d); |
| 1313 | free(primarydir); |
| 1314 | free(userdir); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1315 | return ret; |
| 1316 | } |