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)) { |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame^] | 76 | if (delete_dir_contents(libsymlink, 1, NULL) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 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 | |
Stephen Smalley | 2628820 | 2014-02-07 09:16:46 -0500 | [diff] [blame] | 88 | if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 89 | ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno)); |
| 90 | unlink(libsymlink); |
| 91 | unlink(pkgdir); |
| 92 | return -errno; |
| 93 | } |
| 94 | |
Stephen Smalley | 3a98389 | 2014-05-13 12:53:07 -0400 | [diff] [blame] | 95 | if (symlink(applibdir, libsymlink) < 0) { |
| 96 | ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir, |
| 97 | strerror(errno)); |
| 98 | unlink(pkgdir); |
| 99 | return -1; |
| 100 | } |
| 101 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 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 | |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame^] | 179 | static int lib_dir_matcher(const char* file_name, const int is_dir) { |
| 180 | return is_dir && !strcmp(file_name, "lib"); |
| 181 | } |
| 182 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 183 | int delete_user_data(const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 184 | { |
| 185 | char pkgdir[PKG_PATH_MAX]; |
| 186 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 187 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 188 | return -1; |
| 189 | |
| 190 | /* delete contents, excluding "lib", but not the directory itself */ |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame^] | 191 | return delete_dir_contents(pkgdir, 0, &lib_dir_matcher); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Nick Kralevich | e4e91c4 | 2013-09-20 12:45:20 -0700 | [diff] [blame] | 194 | 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] | 195 | { |
| 196 | char pkgdir[PKG_PATH_MAX]; |
| 197 | char applibdir[PKG_PATH_MAX]; |
| 198 | char libsymlink[PKG_PATH_MAX]; |
| 199 | struct stat libStat; |
| 200 | |
| 201 | // Create the data dir for the package |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 202 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 203 | return -1; |
| 204 | } |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 205 | if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 206 | ALOGE("cannot create package lib symlink origin path\n"); |
| 207 | return -1; |
| 208 | } |
| 209 | if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) { |
| 210 | ALOGE("cannot create package lib symlink dest path\n"); |
| 211 | return -1; |
| 212 | } |
| 213 | |
Nick Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 214 | if (mkdir(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 215 | ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); |
| 216 | return -errno; |
| 217 | } |
Nick Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 218 | if (chmod(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 219 | ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); |
| 220 | unlink(pkgdir); |
| 221 | return -errno; |
| 222 | } |
| 223 | |
| 224 | if (lstat(libsymlink, &libStat) < 0) { |
| 225 | if (errno != ENOENT) { |
| 226 | ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno)); |
| 227 | unlink(pkgdir); |
| 228 | return -1; |
| 229 | } |
| 230 | } else { |
| 231 | if (S_ISDIR(libStat.st_mode)) { |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame^] | 232 | if (delete_dir_contents(libsymlink, 1, NULL) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 233 | ALOGE("couldn't delete lib directory during install for non-primary: %s", |
| 234 | libsymlink); |
| 235 | unlink(pkgdir); |
| 236 | return -1; |
| 237 | } |
| 238 | } else if (S_ISLNK(libStat.st_mode)) { |
| 239 | if (unlink(libsymlink) < 0) { |
| 240 | ALOGE("couldn't unlink lib directory during install for non-primary: %s", |
| 241 | libsymlink); |
| 242 | unlink(pkgdir); |
| 243 | return -1; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Stephen Smalley | 2628820 | 2014-02-07 09:16:46 -0500 | [diff] [blame] | 248 | if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 249 | ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno)); |
| 250 | unlink(libsymlink); |
| 251 | unlink(pkgdir); |
| 252 | return -errno; |
| 253 | } |
| 254 | |
Stephen Smalley | 3a98389 | 2014-05-13 12:53:07 -0400 | [diff] [blame] | 255 | if (symlink(applibdir, libsymlink) < 0) { |
| 256 | ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink, |
| 257 | applibdir, strerror(errno)); |
| 258 | unlink(pkgdir); |
| 259 | return -1; |
| 260 | } |
| 261 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 262 | if (chown(pkgdir, uid, uid) < 0) { |
| 263 | ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); |
| 264 | unlink(libsymlink); |
| 265 | unlink(pkgdir); |
| 266 | return -errno; |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 272 | int delete_user(userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 273 | { |
| 274 | char data_path[PKG_PATH_MAX]; |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 275 | if (create_user_path(data_path, userid)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 276 | return -1; |
| 277 | } |
| 278 | if (delete_dir_contents(data_path, 1, NULL)) { |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | char media_path[PATH_MAX]; |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 283 | if (create_user_media_path(media_path, userid) == -1) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 284 | return -1; |
| 285 | } |
| 286 | if (delete_dir_contents(media_path, 1, NULL) == -1) { |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 293 | int delete_cache(const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 294 | { |
| 295 | char cachedir[PKG_PATH_MAX]; |
| 296 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 297 | if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 298 | return -1; |
| 299 | |
| 300 | /* delete contents, not the directory, no exceptions */ |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame^] | 301 | return delete_dir_contents(cachedir, 0, NULL); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* Try to ensure free_size bytes of storage are available. |
| 305 | * Returns 0 on success. |
| 306 | * This is rather simple-minded because doing a full LRU would |
| 307 | * be potentially memory-intensive, and without atime it would |
| 308 | * also require that apps constantly modify file metadata even |
| 309 | * when just reading from the cache, which is pretty awful. |
| 310 | */ |
| 311 | int free_cache(int64_t free_size) |
| 312 | { |
| 313 | cache_t* cache; |
| 314 | int64_t avail; |
| 315 | DIR *d; |
| 316 | struct dirent *de; |
| 317 | char tmpdir[PATH_MAX]; |
| 318 | char *dirpos; |
| 319 | |
| 320 | avail = data_disk_free(); |
| 321 | if (avail < 0) return -1; |
| 322 | |
| 323 | ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail); |
| 324 | if (avail >= free_size) return 0; |
| 325 | |
| 326 | cache = start_cache_collection(); |
| 327 | |
| 328 | // Collect cache files for primary user. |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 329 | if (create_user_path(tmpdir, 0) == 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 330 | //ALOGI("adding cache files from %s\n", tmpdir); |
| 331 | add_cache_files(cache, tmpdir, "cache"); |
| 332 | } |
| 333 | |
| 334 | // Search for other users and add any cache files from them. |
| 335 | snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path, |
| 336 | SECONDARY_USER_PREFIX); |
| 337 | dirpos = tmpdir + strlen(tmpdir); |
| 338 | d = opendir(tmpdir); |
| 339 | if (d != NULL) { |
| 340 | while ((de = readdir(d))) { |
| 341 | if (de->d_type == DT_DIR) { |
| 342 | const char *name = de->d_name; |
| 343 | /* always skip "." and ".." */ |
| 344 | if (name[0] == '.') { |
| 345 | if (name[1] == 0) continue; |
| 346 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 347 | } |
| 348 | if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) { |
| 349 | strcpy(dirpos, name); |
| 350 | //ALOGI("adding cache files from %s\n", tmpdir); |
| 351 | add_cache_files(cache, tmpdir, "cache"); |
| 352 | } else { |
| 353 | ALOGW("Path exceeds limit: %s%s", tmpdir, name); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | closedir(d); |
| 358 | } |
| 359 | |
| 360 | // Collect cache files on external storage for all users (if it is mounted as part |
| 361 | // of the internal storage). |
| 362 | strcpy(tmpdir, android_media_dir.path); |
| 363 | dirpos = tmpdir + strlen(tmpdir); |
| 364 | d = opendir(tmpdir); |
| 365 | if (d != NULL) { |
| 366 | while ((de = readdir(d))) { |
| 367 | if (de->d_type == DT_DIR) { |
| 368 | const char *name = de->d_name; |
| 369 | /* skip any dir that doesn't start with a number, so not a user */ |
| 370 | if (name[0] < '0' || name[0] > '9') { |
| 371 | continue; |
| 372 | } |
| 373 | if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) { |
| 374 | strcpy(dirpos, name); |
| 375 | if (lookup_media_dir(tmpdir, "Android") == 0 |
| 376 | && lookup_media_dir(tmpdir, "data") == 0) { |
| 377 | //ALOGI("adding cache files from %s\n", tmpdir); |
| 378 | add_cache_files(cache, tmpdir, "cache"); |
| 379 | } |
| 380 | } else { |
| 381 | ALOGW("Path exceeds limit: %s%s", tmpdir, name); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | closedir(d); |
| 386 | } |
| 387 | |
| 388 | clear_cache_files(cache, free_size); |
| 389 | finish_cache_collection(cache); |
| 390 | |
| 391 | return data_disk_free() >= free_size ? 0 : -1; |
| 392 | } |
| 393 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 394 | int move_dex(const char *src, const char *dst, const char *instruction_set) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 395 | { |
| 396 | char src_dex[PKG_PATH_MAX]; |
| 397 | char dst_dex[PKG_PATH_MAX]; |
| 398 | |
| 399 | if (validate_apk_path(src)) return -1; |
| 400 | if (validate_apk_path(dst)) return -1; |
| 401 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 402 | if (create_cache_path(src_dex, src, instruction_set)) return -1; |
| 403 | if (create_cache_path(dst_dex, dst, instruction_set)) return -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 404 | |
| 405 | ALOGV("move %s -> %s\n", src_dex, dst_dex); |
| 406 | if (rename(src_dex, dst_dex) < 0) { |
| 407 | ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno)); |
| 408 | return -1; |
| 409 | } else { |
| 410 | return 0; |
| 411 | } |
| 412 | } |
| 413 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 414 | int rm_dex(const char *path, const char *instruction_set) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 415 | { |
| 416 | char dex_path[PKG_PATH_MAX]; |
| 417 | |
| 418 | if (validate_apk_path(path)) return -1; |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 419 | if (create_cache_path(dex_path, path, instruction_set)) return -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 420 | |
| 421 | ALOGV("unlink %s\n", dex_path); |
| 422 | if (unlink(dex_path) < 0) { |
| 423 | ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno)); |
| 424 | return -1; |
| 425 | } else { |
| 426 | return 0; |
| 427 | } |
| 428 | } |
| 429 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 430 | int get_size(const char *pkgname, userid_t userid, const char *apkpath, |
Dianne Hackborn | 8b41780 | 2013-05-01 18:55:10 -0700 | [diff] [blame] | 431 | const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 432 | const char *instruction_set, int64_t *_codesize, int64_t *_datasize, |
| 433 | int64_t *_cachesize, int64_t* _asecsize) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 434 | { |
| 435 | DIR *d; |
| 436 | int dfd; |
| 437 | struct dirent *de; |
| 438 | struct stat s; |
| 439 | char path[PKG_PATH_MAX]; |
| 440 | |
| 441 | int64_t codesize = 0; |
| 442 | int64_t datasize = 0; |
| 443 | int64_t cachesize = 0; |
| 444 | int64_t asecsize = 0; |
| 445 | |
| 446 | /* count the source apk as code -- but only if it's not |
| 447 | * on the /system partition and its not on the sdcard. |
| 448 | */ |
| 449 | if (validate_system_app_path(apkpath) && |
| 450 | strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) { |
| 451 | if (stat(apkpath, &s) == 0) { |
| 452 | codesize += stat_size(&s); |
| 453 | } |
| 454 | } |
| 455 | /* count the forward locked apk as code if it is given |
| 456 | */ |
| 457 | if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') { |
| 458 | if (stat(fwdlock_apkpath, &s) == 0) { |
| 459 | codesize += stat_size(&s); |
| 460 | } |
| 461 | } |
| 462 | /* count the cached dexfile as code */ |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 463 | if (!create_cache_path(path, apkpath, instruction_set)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 464 | if (stat(path, &s) == 0) { |
| 465 | codesize += stat_size(&s); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* add in size of any libraries */ |
Dianne Hackborn | 8b41780 | 2013-05-01 18:55:10 -0700 | [diff] [blame] | 470 | if (libdirpath != NULL && libdirpath[0] != '!') { |
| 471 | d = opendir(libdirpath); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 472 | if (d != NULL) { |
| 473 | dfd = dirfd(d); |
| 474 | codesize += calculate_dir_size(dfd); |
| 475 | closedir(d); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /* compute asec size if it is given |
| 480 | */ |
| 481 | if (asecpath != NULL && asecpath[0] != '!') { |
| 482 | if (stat(asecpath, &s) == 0) { |
| 483 | asecsize += stat_size(&s); |
| 484 | } |
| 485 | } |
| 486 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 487 | if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 488 | goto done; |
| 489 | } |
| 490 | |
| 491 | d = opendir(path); |
| 492 | if (d == NULL) { |
| 493 | goto done; |
| 494 | } |
| 495 | dfd = dirfd(d); |
| 496 | |
| 497 | /* most stuff in the pkgdir is data, except for the "cache" |
| 498 | * directory and below, which is cache, and the "lib" directory |
| 499 | * and below, which is code... |
| 500 | */ |
| 501 | while ((de = readdir(d))) { |
| 502 | const char *name = de->d_name; |
| 503 | |
| 504 | if (de->d_type == DT_DIR) { |
| 505 | int subfd; |
| 506 | int64_t statsize = 0; |
| 507 | int64_t dirsize = 0; |
| 508 | /* always skip "." and ".." */ |
| 509 | if (name[0] == '.') { |
| 510 | if (name[1] == 0) continue; |
| 511 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 512 | } |
| 513 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 514 | statsize = stat_size(&s); |
| 515 | } |
| 516 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); |
| 517 | if (subfd >= 0) { |
| 518 | dirsize = calculate_dir_size(subfd); |
| 519 | } |
| 520 | if(!strcmp(name,"lib")) { |
| 521 | codesize += dirsize + statsize; |
| 522 | } else if(!strcmp(name,"cache")) { |
| 523 | cachesize += dirsize + statsize; |
| 524 | } else { |
| 525 | datasize += dirsize + statsize; |
| 526 | } |
| 527 | } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) { |
| 528 | // This is the symbolic link to the application's library |
| 529 | // code. We'll count this as code instead of data, since |
| 530 | // it is not something that the app creates. |
| 531 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 532 | codesize += stat_size(&s); |
| 533 | } |
| 534 | } else { |
| 535 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 536 | datasize += stat_size(&s); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | closedir(d); |
| 541 | done: |
| 542 | *_codesize = codesize; |
| 543 | *_datasize = datasize; |
| 544 | *_cachesize = cachesize; |
| 545 | *_asecsize = asecsize; |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 550 | int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 551 | { |
| 552 | char *tmp; |
| 553 | int srclen; |
| 554 | int dstlen; |
| 555 | |
| 556 | srclen = strlen(src); |
| 557 | |
| 558 | /* demand that we are an absolute path */ |
| 559 | if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { |
| 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX? |
| 564 | return -1; |
| 565 | } |
| 566 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 567 | dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) + |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 568 | strlen(instruction_set) + |
| 569 | strlen(DALVIK_CACHE_POSTFIX) + 2; |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 570 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 571 | if (dstlen > PKG_PATH_MAX) { |
| 572 | return -1; |
| 573 | } |
| 574 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 575 | sprintf(path,"%s%s/%s%s", |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 576 | DALVIK_CACHE_PREFIX, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 577 | instruction_set, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 578 | src + 1, /* skip the leading / */ |
| 579 | DALVIK_CACHE_POSTFIX); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 580 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 581 | for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 582 | if (*tmp == '/') { |
| 583 | *tmp = '@'; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | 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] | 591 | const char* output_file_name) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 592 | { |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 593 | /* platform-specific flags affecting optimization and verification */ |
| 594 | char dexopt_flags[PROPERTY_VALUE_MAX]; |
| 595 | property_get("dalvik.vm.dexopt-flags", dexopt_flags, ""); |
| 596 | ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags); |
| 597 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 598 | static const char* DEX_OPT_BIN = "/system/bin/dexopt"; |
| 599 | static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig |
| 600 | char zip_num[MAX_INT_LEN]; |
| 601 | char odex_num[MAX_INT_LEN]; |
| 602 | |
| 603 | sprintf(zip_num, "%d", zip_fd); |
| 604 | sprintf(odex_num, "%d", odex_fd); |
| 605 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 606 | 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] | 607 | execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name, |
| 608 | dexopt_flags, (char*) NULL); |
| 609 | ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno)); |
| 610 | } |
| 611 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 612 | static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 613 | const char* output_file_name, const char *pkgname, const char *instruction_set) |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 614 | { |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 615 | char dex2oat_flags[PROPERTY_VALUE_MAX]; |
| 616 | property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, ""); |
| 617 | ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags); |
| 618 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 619 | static const char* DEX2OAT_BIN = "/system/bin/dex2oat"; |
| 620 | static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 621 | static const unsigned int MAX_INSTRUCTION_SET_LEN = 32; |
| 622 | |
| 623 | if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { |
| 624 | ALOGE("Instruction set %s longer than max length of %d", |
| 625 | instruction_set, MAX_INSTRUCTION_SET_LEN); |
| 626 | return; |
| 627 | } |
| 628 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 629 | char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN]; |
| 630 | char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX]; |
| 631 | char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN]; |
| 632 | char oat_location_arg[strlen("--oat-name=") + PKG_PATH_MAX]; |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 633 | char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX]; |
| 634 | char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 635 | |
| 636 | sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd); |
| 637 | sprintf(zip_location_arg, "--zip-location=%s", input_file_name); |
| 638 | sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd); |
| 639 | sprintf(oat_location_arg, "--oat-location=%s", output_file_name); |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 640 | sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 641 | if (strcmp(pkgname, "*") != 0) { |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 642 | snprintf(profile_file_arg, sizeof(profile_file_arg), "--profile-file=%s/%s", |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 643 | DALVIK_CACHE_PREFIX "profiles", pkgname); |
| 644 | } else { |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 645 | strcpy(profile_file_arg, "--no-profile-file"); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 646 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 647 | |
| 648 | ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name); |
| 649 | execl(DEX2OAT_BIN, DEX2OAT_BIN, |
| 650 | zip_fd_arg, zip_location_arg, |
| 651 | oat_fd_arg, oat_location_arg, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 652 | profile_file_arg, instruction_set_arg, |
Anwar Ghuloum | 4bc0540 | 2014-03-11 15:42:58 -0700 | [diff] [blame] | 653 | strlen(dex2oat_flags) > 0 ? dex2oat_flags : NULL, |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 654 | (char*) NULL); |
| 655 | ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno)); |
| 656 | } |
| 657 | |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 658 | static int wait_child(pid_t pid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 659 | { |
| 660 | int status; |
| 661 | pid_t got_pid; |
| 662 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 663 | while (1) { |
| 664 | got_pid = waitpid(pid, &status, 0); |
| 665 | if (got_pid == -1 && errno == EINTR) { |
| 666 | printf("waitpid interrupted, retrying\n"); |
| 667 | } else { |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | if (got_pid != pid) { |
| 672 | ALOGW("waitpid failed: wanted %d, got %d: %s\n", |
| 673 | (int) pid, (int) got_pid, strerror(errno)); |
| 674 | return 1; |
| 675 | } |
| 676 | |
| 677 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 678 | return 0; |
| 679 | } else { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 680 | return status; /* always nonzero */ |
| 681 | } |
| 682 | } |
| 683 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 684 | int dexopt(const char *apk_path, uid_t uid, int is_public, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 685 | const char *pkgname, const char *instruction_set) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 686 | { |
| 687 | struct utimbuf ut; |
| 688 | struct stat apk_stat, dex_stat; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 689 | char out_path[PKG_PATH_MAX]; |
Brian Carlstrom | e7a8b17 | 2013-06-30 13:17:38 -0700 | [diff] [blame] | 690 | char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX]; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 691 | char *end; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 692 | int res, zip_fd=-1, out_fd=-1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 693 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 694 | if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { |
| 695 | return -1; |
| 696 | } |
| 697 | |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 698 | /* The command to run depend on the value of persist.sys.dalvik.vm.lib */ |
Brian Carlstrom | 856bc78 | 2014-05-28 14:31:47 -0700 | [diff] [blame] | 699 | property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so"); |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 700 | |
| 701 | /* Before anything else: is there a .odex file? If so, we have |
| 702 | * precompiled the apk and there is nothing to do here. |
| 703 | */ |
Chih-Wei Huang | 0e8ae16 | 2014-04-28 15:47:45 +0800 | [diff] [blame] | 704 | strcpy(out_path, apk_path); |
| 705 | end = strrchr(out_path, '.'); |
| 706 | if (end != NULL) { |
| 707 | strcpy(end, ".odex"); |
| 708 | if (stat(out_path, &dex_stat) == 0) { |
| 709 | return 0; |
| 710 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 713 | if (create_cache_path(out_path, apk_path, instruction_set)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 714 | return -1; |
| 715 | } |
| 716 | |
| 717 | memset(&apk_stat, 0, sizeof(apk_stat)); |
| 718 | stat(apk_path, &apk_stat); |
| 719 | |
| 720 | zip_fd = open(apk_path, O_RDONLY, 0); |
| 721 | if (zip_fd < 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 722 | ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 723 | return -1; |
| 724 | } |
| 725 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 726 | unlink(out_path); |
| 727 | out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 728 | if (out_fd < 0) { |
| 729 | ALOGE("installd cannot open '%s' for output during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 730 | goto fail; |
| 731 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 732 | if (fchmod(out_fd, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 733 | S_IRUSR|S_IWUSR|S_IRGRP | |
| 734 | (is_public ? S_IROTH : 0)) < 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 735 | ALOGE("installd cannot chmod '%s' during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 736 | goto fail; |
| 737 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 738 | if (fchown(out_fd, AID_SYSTEM, uid) < 0) { |
| 739 | ALOGE("installd cannot chown '%s' during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 740 | goto fail; |
| 741 | } |
| 742 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 743 | // Create profile file if there is a package name present. |
| 744 | if (strcmp(pkgname, "*") != 0) { |
| 745 | create_profile_file(pkgname, uid); |
| 746 | } |
| 747 | |
| 748 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 749 | ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path); |
| 750 | |
| 751 | pid_t pid; |
| 752 | pid = fork(); |
| 753 | if (pid == 0) { |
| 754 | /* child -- drop privileges before continuing */ |
| 755 | if (setgid(uid) != 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 756 | ALOGE("setgid(%d) failed in installd during dexopt\n", uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 757 | exit(64); |
| 758 | } |
| 759 | if (setuid(uid) != 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 760 | ALOGE("setuid(%d) failed in installd during dexopt\n", uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 761 | exit(65); |
| 762 | } |
| 763 | // drop capabilities |
| 764 | struct __user_cap_header_struct capheader; |
| 765 | struct __user_cap_data_struct capdata[2]; |
| 766 | memset(&capheader, 0, sizeof(capheader)); |
| 767 | memset(&capdata, 0, sizeof(capdata)); |
| 768 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 769 | if (capset(&capheader, &capdata[0]) < 0) { |
| 770 | ALOGE("capset failed: %s\n", strerror(errno)); |
| 771 | exit(66); |
| 772 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 773 | if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) { |
| 774 | ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 775 | exit(67); |
| 776 | } |
| 777 | |
Brian Carlstrom | e7a8b17 | 2013-06-30 13:17:38 -0700 | [diff] [blame] | 778 | if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) { |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 779 | run_dexopt(zip_fd, out_fd, apk_path, out_path); |
Brian Carlstrom | e7a8b17 | 2013-06-30 13:17:38 -0700 | [diff] [blame] | 780 | } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) { |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 781 | run_dex2oat(zip_fd, out_fd, apk_path, out_path, pkgname, instruction_set); |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 782 | } else { |
Brian Carlstrom | e7a8b17 | 2013-06-30 13:17:38 -0700 | [diff] [blame] | 783 | exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */ |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 784 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 785 | exit(68); /* only get here on exec failure */ |
| 786 | } else { |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 787 | res = wait_child(pid); |
| 788 | if (res == 0) { |
| 789 | ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path); |
| 790 | } else { |
| 791 | 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] | 792 | goto fail; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | ut.actime = apk_stat.st_atime; |
| 797 | ut.modtime = apk_stat.st_mtime; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 798 | utime(out_path, &ut); |
| 799 | |
| 800 | close(out_fd); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 801 | close(zip_fd); |
| 802 | return 0; |
| 803 | |
| 804 | fail: |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 805 | if (out_fd >= 0) { |
| 806 | close(out_fd); |
| 807 | unlink(out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 808 | } |
| 809 | if (zip_fd >= 0) { |
| 810 | close(zip_fd); |
| 811 | } |
| 812 | return -1; |
| 813 | } |
| 814 | |
| 815 | void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, |
| 816 | struct stat* statbuf) |
| 817 | { |
| 818 | while (path[basepos] != 0) { |
| 819 | if (path[basepos] == '/') { |
| 820 | path[basepos] = 0; |
| 821 | if (lstat(path, statbuf) < 0) { |
| 822 | ALOGV("Making directory: %s\n", path); |
| 823 | if (mkdir(path, mode) == 0) { |
| 824 | chown(path, uid, gid); |
| 825 | } else { |
| 826 | ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); |
| 827 | } |
| 828 | } |
| 829 | path[basepos] = '/'; |
| 830 | basepos++; |
| 831 | } |
| 832 | basepos++; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | int movefileordir(char* srcpath, char* dstpath, int dstbasepos, |
| 837 | int dstuid, int dstgid, struct stat* statbuf) |
| 838 | { |
| 839 | DIR *d; |
| 840 | struct dirent *de; |
| 841 | int res; |
| 842 | |
| 843 | int srcend = strlen(srcpath); |
| 844 | int dstend = strlen(dstpath); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 845 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 846 | if (lstat(srcpath, statbuf) < 0) { |
| 847 | ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno)); |
| 848 | return 1; |
| 849 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 850 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 851 | if ((statbuf->st_mode&S_IFDIR) == 0) { |
| 852 | mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH, |
| 853 | dstuid, dstgid, statbuf); |
| 854 | ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid); |
| 855 | if (rename(srcpath, dstpath) >= 0) { |
| 856 | if (chown(dstpath, dstuid, dstgid) < 0) { |
| 857 | ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno)); |
| 858 | unlink(dstpath); |
| 859 | return 1; |
| 860 | } |
| 861 | } else { |
| 862 | ALOGW("Unable to rename %s to %s: %s\n", |
| 863 | srcpath, dstpath, strerror(errno)); |
| 864 | return 1; |
| 865 | } |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | d = opendir(srcpath); |
| 870 | if (d == NULL) { |
| 871 | ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno)); |
| 872 | return 1; |
| 873 | } |
| 874 | |
| 875 | res = 0; |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 876 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 877 | while ((de = readdir(d))) { |
| 878 | const char *name = de->d_name; |
| 879 | /* always skip "." and ".." */ |
| 880 | if (name[0] == '.') { |
| 881 | if (name[1] == 0) continue; |
| 882 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 883 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 884 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 885 | if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
| 886 | ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name); |
| 887 | continue; |
| 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 | if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
| 891 | ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name); |
| 892 | continue; |
| 893 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 894 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 895 | srcpath[srcend] = dstpath[dstend] = '/'; |
| 896 | strcpy(srcpath+srcend+1, name); |
| 897 | strcpy(dstpath+dstend+1, name); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 898 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 899 | if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) { |
| 900 | res = 1; |
| 901 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 902 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 903 | // Note: we will be leaving empty directories behind in srcpath, |
| 904 | // but that is okay, the package manager will be erasing all of the |
| 905 | // data associated with .apks that disappear. |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 906 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 907 | srcpath[srcend] = dstpath[dstend] = 0; |
| 908 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 909 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 910 | closedir(d); |
| 911 | return res; |
| 912 | } |
| 913 | |
| 914 | int movefiles() |
| 915 | { |
| 916 | DIR *d; |
| 917 | int dfd, subfd; |
| 918 | struct dirent *de; |
| 919 | struct stat s; |
| 920 | char buf[PKG_PATH_MAX+1]; |
| 921 | int bufp, bufe, bufi, readlen; |
| 922 | |
| 923 | char srcpkg[PKG_NAME_MAX]; |
| 924 | char dstpkg[PKG_NAME_MAX]; |
| 925 | char srcpath[PKG_PATH_MAX]; |
| 926 | char dstpath[PKG_PATH_MAX]; |
| 927 | int dstuid=-1, dstgid=-1; |
| 928 | int hasspace; |
| 929 | |
| 930 | d = opendir(UPDATE_COMMANDS_DIR_PREFIX); |
| 931 | if (d == NULL) { |
| 932 | goto done; |
| 933 | } |
| 934 | dfd = dirfd(d); |
| 935 | |
| 936 | /* Iterate through all files in the directory, executing the |
| 937 | * file movements requested there-in. |
| 938 | */ |
| 939 | while ((de = readdir(d))) { |
| 940 | const char *name = de->d_name; |
| 941 | |
| 942 | if (de->d_type == DT_DIR) { |
| 943 | continue; |
| 944 | } else { |
| 945 | subfd = openat(dfd, name, O_RDONLY); |
| 946 | if (subfd < 0) { |
| 947 | ALOGW("Unable to open update commands at %s%s\n", |
| 948 | UPDATE_COMMANDS_DIR_PREFIX, name); |
| 949 | continue; |
| 950 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 951 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 952 | bufp = 0; |
| 953 | bufe = 0; |
| 954 | buf[PKG_PATH_MAX] = 0; |
| 955 | srcpkg[0] = dstpkg[0] = 0; |
| 956 | while (1) { |
| 957 | bufi = bufp; |
| 958 | while (bufi < bufe && buf[bufi] != '\n') { |
| 959 | bufi++; |
| 960 | } |
| 961 | if (bufi < bufe) { |
| 962 | buf[bufi] = 0; |
| 963 | ALOGV("Processing line: %s\n", buf+bufp); |
| 964 | hasspace = 0; |
| 965 | while (bufp < bufi && isspace(buf[bufp])) { |
| 966 | hasspace = 1; |
| 967 | bufp++; |
| 968 | } |
| 969 | if (buf[bufp] == '#' || bufp == bufi) { |
| 970 | // skip comments and empty lines. |
| 971 | } else if (hasspace) { |
| 972 | if (dstpkg[0] == 0) { |
| 973 | ALOGW("Path before package line in %s%s: %s\n", |
| 974 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 975 | } else if (srcpkg[0] == 0) { |
| 976 | // Skip -- source package no longer exists. |
| 977 | } else { |
| 978 | ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg); |
| 979 | if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) && |
| 980 | !create_move_path(dstpath, dstpkg, buf+bufp, 0)) { |
| 981 | movefileordir(srcpath, dstpath, |
| 982 | strlen(dstpath)-strlen(buf+bufp), |
| 983 | dstuid, dstgid, &s); |
| 984 | } |
| 985 | } |
| 986 | } else { |
| 987 | char* div = strchr(buf+bufp, ':'); |
| 988 | if (div == NULL) { |
| 989 | ALOGW("Bad package spec in %s%s; no ':' sep: %s\n", |
| 990 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 991 | } else { |
| 992 | *div = 0; |
| 993 | div++; |
| 994 | if (strlen(buf+bufp) < PKG_NAME_MAX) { |
| 995 | strcpy(dstpkg, buf+bufp); |
| 996 | } else { |
| 997 | srcpkg[0] = dstpkg[0] = 0; |
| 998 | ALOGW("Package name too long in %s%s: %s\n", |
| 999 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 1000 | } |
| 1001 | if (strlen(div) < PKG_NAME_MAX) { |
| 1002 | strcpy(srcpkg, div); |
| 1003 | } else { |
| 1004 | srcpkg[0] = dstpkg[0] = 0; |
| 1005 | ALOGW("Package name too long in %s%s: %s\n", |
| 1006 | UPDATE_COMMANDS_DIR_PREFIX, name, div); |
| 1007 | } |
| 1008 | if (srcpkg[0] != 0) { |
| 1009 | if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) { |
| 1010 | if (lstat(srcpath, &s) < 0) { |
| 1011 | // Package no longer exists -- skip. |
| 1012 | srcpkg[0] = 0; |
| 1013 | } |
| 1014 | } else { |
| 1015 | srcpkg[0] = 0; |
| 1016 | ALOGW("Can't create path %s in %s%s\n", |
| 1017 | div, UPDATE_COMMANDS_DIR_PREFIX, name); |
| 1018 | } |
| 1019 | if (srcpkg[0] != 0) { |
| 1020 | if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) { |
| 1021 | if (lstat(dstpath, &s) == 0) { |
| 1022 | dstuid = s.st_uid; |
| 1023 | dstgid = s.st_gid; |
| 1024 | } else { |
| 1025 | // Destination package doesn't |
| 1026 | // exist... due to original-package, |
| 1027 | // this is normal, so don't be |
| 1028 | // noisy about it. |
| 1029 | srcpkg[0] = 0; |
| 1030 | } |
| 1031 | } else { |
| 1032 | srcpkg[0] = 0; |
| 1033 | ALOGW("Can't create path %s in %s%s\n", |
| 1034 | div, UPDATE_COMMANDS_DIR_PREFIX, name); |
| 1035 | } |
| 1036 | } |
| 1037 | ALOGV("Transfering from %s to %s: uid=%d\n", |
| 1038 | srcpkg, dstpkg, dstuid); |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | bufp = bufi+1; |
| 1043 | } else { |
| 1044 | if (bufp == 0) { |
| 1045 | if (bufp < bufe) { |
| 1046 | ALOGW("Line too long in %s%s, skipping: %s\n", |
| 1047 | UPDATE_COMMANDS_DIR_PREFIX, name, buf); |
| 1048 | } |
| 1049 | } else if (bufp < bufe) { |
| 1050 | memcpy(buf, buf+bufp, bufe-bufp); |
| 1051 | bufe -= bufp; |
| 1052 | bufp = 0; |
| 1053 | } |
| 1054 | readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe); |
| 1055 | if (readlen < 0) { |
| 1056 | ALOGW("Failure reading update commands in %s%s: %s\n", |
| 1057 | UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno)); |
| 1058 | break; |
| 1059 | } else if (readlen == 0) { |
| 1060 | break; |
| 1061 | } |
| 1062 | bufe += readlen; |
| 1063 | buf[bufe] = 0; |
| 1064 | ALOGV("Read buf: %s\n", buf); |
| 1065 | } |
| 1066 | } |
| 1067 | close(subfd); |
| 1068 | } |
| 1069 | } |
| 1070 | closedir(d); |
| 1071 | done: |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | int linklib(const char* pkgname, const char* asecLibDir, int userId) |
| 1076 | { |
| 1077 | char pkgdir[PKG_PATH_MAX]; |
| 1078 | char libsymlink[PKG_PATH_MAX]; |
| 1079 | struct stat s, libStat; |
| 1080 | int rc = 0; |
| 1081 | |
| 1082 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) { |
| 1083 | ALOGE("cannot create package path\n"); |
| 1084 | return -1; |
| 1085 | } |
| 1086 | if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) { |
| 1087 | ALOGE("cannot create package lib symlink origin path\n"); |
| 1088 | return -1; |
| 1089 | } |
| 1090 | |
| 1091 | if (stat(pkgdir, &s) < 0) return -1; |
| 1092 | |
| 1093 | if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { |
| 1094 | ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno)); |
| 1095 | return -1; |
| 1096 | } |
| 1097 | |
| 1098 | if (chmod(pkgdir, 0700) < 0) { |
| 1099 | ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); |
| 1100 | rc = -1; |
| 1101 | goto out; |
| 1102 | } |
| 1103 | |
| 1104 | if (lstat(libsymlink, &libStat) < 0) { |
| 1105 | if (errno != ENOENT) { |
| 1106 | ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); |
| 1107 | rc = -1; |
| 1108 | goto out; |
| 1109 | } |
| 1110 | } else { |
| 1111 | if (S_ISDIR(libStat.st_mode)) { |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame^] | 1112 | if (delete_dir_contents(libsymlink, 1, NULL) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1113 | rc = -1; |
| 1114 | goto out; |
| 1115 | } |
| 1116 | } else if (S_ISLNK(libStat.st_mode)) { |
| 1117 | if (unlink(libsymlink) < 0) { |
| 1118 | ALOGE("couldn't unlink lib dir: %s\n", strerror(errno)); |
| 1119 | rc = -1; |
| 1120 | goto out; |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | if (symlink(asecLibDir, libsymlink) < 0) { |
| 1126 | ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir, |
| 1127 | strerror(errno)); |
| 1128 | rc = -errno; |
| 1129 | goto out; |
| 1130 | } |
| 1131 | |
| 1132 | out: |
| 1133 | if (chmod(pkgdir, s.st_mode) < 0) { |
| 1134 | ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); |
| 1135 | rc = -errno; |
| 1136 | } |
| 1137 | |
| 1138 | if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { |
| 1139 | ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno)); |
| 1140 | return -errno; |
| 1141 | } |
| 1142 | |
| 1143 | return rc; |
| 1144 | } |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1145 | |
| 1146 | static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) |
| 1147 | { |
| 1148 | static const char *IDMAP_BIN = "/system/bin/idmap"; |
| 1149 | static const size_t MAX_INT_LEN = 32; |
| 1150 | char idmap_str[MAX_INT_LEN]; |
| 1151 | |
| 1152 | snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); |
| 1153 | |
| 1154 | execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); |
| 1155 | ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); |
| 1156 | } |
| 1157 | |
| 1158 | // Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) |
| 1159 | // eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap |
| 1160 | static int flatten_path(const char *prefix, const char *suffix, |
| 1161 | const char *overlay_path, char *idmap_path, size_t N) |
| 1162 | { |
| 1163 | if (overlay_path == NULL || idmap_path == NULL) { |
| 1164 | return -1; |
| 1165 | } |
| 1166 | const size_t len_overlay_path = strlen(overlay_path); |
| 1167 | // will access overlay_path + 1 further below; requires absolute path |
| 1168 | if (len_overlay_path < 2 || *overlay_path != '/') { |
| 1169 | return -1; |
| 1170 | } |
| 1171 | const size_t len_idmap_root = strlen(prefix); |
| 1172 | const size_t len_suffix = strlen(suffix); |
| 1173 | if (SIZE_MAX - len_idmap_root < len_overlay_path || |
| 1174 | SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { |
| 1175 | // additions below would cause overflow |
| 1176 | return -1; |
| 1177 | } |
| 1178 | if (N < len_idmap_root + len_overlay_path + len_suffix) { |
| 1179 | return -1; |
| 1180 | } |
| 1181 | memset(idmap_path, 0, N); |
| 1182 | snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); |
| 1183 | char *ch = idmap_path + len_idmap_root; |
| 1184 | while (*ch != '\0') { |
| 1185 | if (*ch == '/') { |
| 1186 | *ch = '@'; |
| 1187 | } |
| 1188 | ++ch; |
| 1189 | } |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | int idmap(const char *target_apk, const char *overlay_apk, uid_t uid) |
| 1194 | { |
| 1195 | ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); |
| 1196 | |
| 1197 | int idmap_fd = -1; |
| 1198 | char idmap_path[PATH_MAX]; |
| 1199 | |
| 1200 | if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, |
| 1201 | idmap_path, sizeof(idmap_path)) == -1) { |
| 1202 | ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); |
| 1203 | goto fail; |
| 1204 | } |
| 1205 | |
| 1206 | unlink(idmap_path); |
| 1207 | idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 1208 | if (idmap_fd < 0) { |
| 1209 | ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); |
| 1210 | goto fail; |
| 1211 | } |
| 1212 | if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { |
| 1213 | ALOGE("idmap cannot chown '%s'\n", idmap_path); |
| 1214 | goto fail; |
| 1215 | } |
| 1216 | if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { |
| 1217 | ALOGE("idmap cannot chmod '%s'\n", idmap_path); |
| 1218 | goto fail; |
| 1219 | } |
| 1220 | |
| 1221 | pid_t pid; |
| 1222 | pid = fork(); |
| 1223 | if (pid == 0) { |
| 1224 | /* child -- drop privileges before continuing */ |
| 1225 | if (setgid(uid) != 0) { |
| 1226 | ALOGE("setgid(%d) failed during idmap\n", uid); |
| 1227 | exit(1); |
| 1228 | } |
| 1229 | if (setuid(uid) != 0) { |
| 1230 | ALOGE("setuid(%d) failed during idmap\n", uid); |
| 1231 | exit(1); |
| 1232 | } |
| 1233 | if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { |
| 1234 | ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); |
| 1235 | exit(1); |
| 1236 | } |
| 1237 | |
| 1238 | run_idmap(target_apk, overlay_apk, idmap_fd); |
| 1239 | exit(1); /* only if exec call to idmap failed */ |
| 1240 | } else { |
| 1241 | int status = wait_child(pid); |
| 1242 | if (status != 0) { |
| 1243 | ALOGE("idmap failed, status=0x%04x\n", status); |
| 1244 | goto fail; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | close(idmap_fd); |
| 1249 | return 0; |
| 1250 | fail: |
| 1251 | if (idmap_fd >= 0) { |
| 1252 | close(idmap_fd); |
| 1253 | unlink(idmap_path); |
| 1254 | } |
| 1255 | return -1; |
| 1256 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1257 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1258 | int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid) |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1259 | { |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1260 | struct dirent *entry; |
| 1261 | DIR *d; |
| 1262 | struct stat s; |
| 1263 | char *userdir; |
| 1264 | char *primarydir; |
| 1265 | char *pkgdir; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1266 | int ret = 0; |
| 1267 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1268 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
| 1269 | unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE; |
| 1270 | |
| 1271 | if (!pkgName || !seinfo) { |
| 1272 | ALOGE("Package name or seinfo tag is null when trying to restorecon."); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1273 | return -1; |
| 1274 | } |
| 1275 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1276 | if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) { |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | // Relabel for primary user. |
| 1281 | if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) { |
| 1282 | ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno)); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1283 | ret |= -1; |
| 1284 | } |
| 1285 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1286 | if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) { |
| 1287 | free(primarydir); |
| 1288 | return -1; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1289 | } |
| 1290 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1291 | // Relabel package directory for all secondary users. |
| 1292 | d = opendir(userdir); |
| 1293 | if (d == NULL) { |
| 1294 | free(primarydir); |
| 1295 | free(userdir); |
| 1296 | return -1; |
| 1297 | } |
| 1298 | |
| 1299 | while ((entry = readdir(d))) { |
| 1300 | if (entry->d_type != DT_DIR) { |
| 1301 | continue; |
| 1302 | } |
| 1303 | |
| 1304 | const char *user = entry->d_name; |
| 1305 | // Ignore "." and ".." |
| 1306 | if (!strcmp(user, ".") || !strcmp(user, "..")) { |
| 1307 | continue; |
| 1308 | } |
| 1309 | |
| 1310 | // user directories start with a number |
| 1311 | if (user[0] < '0' || user[0] > '9') { |
| 1312 | ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user); |
| 1313 | continue; |
| 1314 | } |
| 1315 | |
| 1316 | if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) { |
| 1317 | continue; |
| 1318 | } |
| 1319 | |
| 1320 | if (stat(pkgdir, &s) < 0) { |
| 1321 | free(pkgdir); |
| 1322 | continue; |
| 1323 | } |
| 1324 | |
| 1325 | if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) { |
| 1326 | ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno)); |
| 1327 | ret |= -1; |
| 1328 | } |
| 1329 | free(pkgdir); |
| 1330 | } |
| 1331 | |
| 1332 | closedir(d); |
| 1333 | free(primarydir); |
| 1334 | free(userdir); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1335 | return ret; |
| 1336 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame^] | 1337 | |
| 1338 | static int prune_dex_exclusion_predicate(const char *file_name, const int is_dir) |
| 1339 | { |
| 1340 | // Don't exclude any directories, we want to inspect them |
| 1341 | // recusively for files. |
| 1342 | if (is_dir) { |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | // Don't exclude regular files that start with the list |
| 1348 | // of prefixes. |
| 1349 | static const char data_app_prefix[] = "data@app@"; |
| 1350 | static const char data_priv_app_prefix[] = "data@priv-app@"; |
| 1351 | if (!strncmp(file_name, data_app_prefix, sizeof(data_app_prefix) - 1) || |
| 1352 | !strncmp(file_name, data_priv_app_prefix, sizeof(data_priv_app_prefix) - 1)) { |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | // Exclude all regular files that don't start with the prefix "data@app@" or |
| 1357 | // "data@priv-app@". |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
| 1361 | int prune_dex_cache() { |
| 1362 | return delete_dir_contents(DALVIK_CACHE_PREFIX, 0, &prune_dex_exclusion_predicate); |
| 1363 | } |