1 /* 2 ** Copyright 2008, The Android Open Source Project 3 ** 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 7 ** 8 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** 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 14 ** limitations under the License. 15 */ 16 17 #include <sys/capability.h> 18 #include <linux/prctl.h> 19 20 #include "installd.h" 21 22 23 #define BUFFER_MAX 1024 /* input buffer for commands */ 24 #define TOKEN_MAX 8 /* max number of arguments in buffer */ 25 #define REPLY_MAX 256 /* largest reply allowed */ 26 27 static int do_ping(char **arg, char reply[REPLY_MAX]) 28 { 29 return 0; 30 } 31 32 static int do_install(char **arg, char reply[REPLY_MAX]) 33 { 34 return install(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]); /* pkgname, uid, gid, seinfo */ 35 } 36 37 static int do_dexopt(char **arg, char reply[REPLY_MAX]) 38 { 39 /* apk_path, uid, is_public */ 40 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2])); 41 } 42 43 static int do_move_dex(char **arg, char reply[REPLY_MAX]) 44 { 45 return move_dex(arg[0], arg[1]); /* src, dst */ 46 } 47 48 static int do_rm_dex(char **arg, char reply[REPLY_MAX]) 49 { 50 return rm_dex(arg[0]); /* pkgname */ 51 } 52 53 static int do_remove(char **arg, char reply[REPLY_MAX]) 54 { 55 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */ 56 } 57 58 static int do_rename(char **arg, char reply[REPLY_MAX]) 59 { 60 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */ 61 } 62 63 static int do_fixuid(char **arg, char reply[REPLY_MAX]) 64 { 65 return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */ 66 } 67 68 static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */ 69 { 70 return free_cache((int64_t)atoll(arg[0])); /* free_size */ 71 } 72 73 static int do_rm_cache(char **arg, char reply[REPLY_MAX]) 74 { 75 return delete_cache(arg[0], atoi(arg[1])); /* pkgname, userid */ 76 } 77 78 static int do_get_size(char **arg, char reply[REPLY_MAX]) 79 { 80 int64_t codesize = 0; 81 int64_t datasize = 0; 82 int64_t cachesize = 0; 83 int64_t asecsize = 0; 84 int res = 0; 85 86 /* pkgdir, userid, apkpath */ 87 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5], 88 &codesize, &datasize, &cachesize, &asecsize); 89 90 /* 91 * Each int64_t can take up 22 characters printed out. Make sure it 92 * doesn't go over REPLY_MAX in the future. 93 */ 94 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, 95 codesize, datasize, cachesize, asecsize); 96 return res; 97 } 98 99 static int do_rm_user_data(char **arg, char reply[REPLY_MAX]) 100 { 101 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */ 102 } 103 104 static int do_mk_user_data(char **arg, char reply[REPLY_MAX]) 105 { 106 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */ 107 } 108 109 static int do_rm_user(char **arg, char reply[REPLY_MAX]) 110 { 111 return delete_user(atoi(arg[0])); /* userid */ 112 } 113 114 static int do_movefiles(char **arg, char reply[REPLY_MAX]) 115 { 116 return movefiles(); 117 } 118 119 static int do_linklib(char **arg, char reply[REPLY_MAX]) 120 { 121 return linklib(arg[0], arg[1], atoi(arg[2])); 122 } 123 124 struct cmdinfo { 125 const char *name; 126 unsigned numargs; 127 int (*func)(char **arg, char reply[REPLY_MAX]); 128 }; 129 130 struct cmdinfo cmds[] = { 131 { "ping", 0, do_ping }, 132 { "install", 4, do_install }, 133 { "dexopt", 3, do_dexopt }, 134 { "movedex", 2, do_move_dex }, 135 { "rmdex", 1, do_rm_dex }, 136 { "remove", 2, do_remove }, 137 { "rename", 2, do_rename }, 138 { "fixuid", 3, do_fixuid }, 139 { "freecache", 1, do_free_cache }, 140 { "rmcache", 2, do_rm_cache }, 141 { "getsize", 6, do_get_size }, 142 { "rmuserdata", 2, do_rm_user_data }, 143 { "movefiles", 0, do_movefiles }, 144 { "linklib", 3, do_linklib }, 145 { "mkuserdata", 3, do_mk_user_data }, 146 { "rmuser", 1, do_rm_user }, 147 }; 148 149 static int readx(int s, void *_buf, int count) 150 { 151 char *buf = _buf; 152 int n = 0, r; 153 if (count < 0) return -1; 154 while (n < count) { 155 r = read(s, buf + n, count - n); 156 if (r < 0) { 157 if (errno == EINTR) continue; 158 ALOGE("read error: %s\n", strerror(errno)); 159 return -1; 160 } 161 if (r == 0) { 162 ALOGE("eof\n"); 163 return -1; /* EOF */ 164 } 165 n += r; 166 } 167 return 0; 168 } 169 170 static int writex(int s, const void *_buf, int count) 171 { 172 const char *buf = _buf; 173 int n = 0, r; 174 if (count < 0) return -1; 175 while (n < count) { 176 r = write(s, buf + n, count - n); 177 if (r < 0) { 178 if (errno == EINTR) continue; 179 ALOGE("write error: %s\n", strerror(errno)); 180 return -1; 181 } 182 n += r; 183 } 184 return 0; 185 } 186 187 188 /* Tokenize the command buffer, locate a matching command, 189 * ensure that the required number of arguments are provided, 190 * call the function(), return the result. 191 */ 192 static int execute(int s, char cmd[BUFFER_MAX]) 193 { 194 char reply[REPLY_MAX]; 195 char *arg[TOKEN_MAX+1]; 196 unsigned i; 197 unsigned n = 0; 198 unsigned short count; 199 int ret = -1; 200 201 // ALOGI("execute('%s')\n", cmd); 202 203 /* default reply is "" */ 204 reply[0] = 0; 205 206 /* n is number of args (not counting arg[0]) */ 207 arg[0] = cmd; 208 while (*cmd) { 209 if (isspace(*cmd)) { 210 *cmd++ = 0; 211 n++; 212 arg[n] = cmd; 213 if (n == TOKEN_MAX) { 214 ALOGE("too many arguments\n"); 215 goto done; 216 } 217 } 218 cmd++; 219 } 220 221 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { 222 if (!strcmp(cmds[i].name,arg[0])) { 223 if (n != cmds[i].numargs) { 224 ALOGE("%s requires %d arguments (%d given)\n", 225 cmds[i].name, cmds[i].numargs, n); 226 } else { 227 ret = cmds[i].func(arg + 1, reply); 228 } 229 goto done; 230 } 231 } 232 ALOGE("unsupported command '%s'\n", arg[0]); 233 234 done: 235 if (reply[0]) { 236 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply); 237 } else { 238 n = snprintf(cmd, BUFFER_MAX, "%d", ret); 239 } 240 if (n > BUFFER_MAX) n = BUFFER_MAX; 241 count = n; 242 243 // ALOGI("reply: '%s'\n", cmd); 244 if (writex(s, &count, sizeof(count))) return -1; 245 if (writex(s, cmd, count)) return -1; 246 return 0; 247 } 248 249 /** 250 * Initialize all the global variables that are used elsewhere. Returns 0 upon 251 * success and -1 on error. 252 */ 253 void free_globals() { 254 size_t i; 255 256 for (i = 0; i < android_system_dirs.count; i++) { 257 if (android_system_dirs.dirs[i].path != NULL) { 258 free(android_system_dirs.dirs[i].path); 259 } 260 } 261 262 free(android_system_dirs.dirs); 263 } 264 265 int initialize_globals() { 266 // Get the android data directory. 267 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) { 268 return -1; 269 } 270 271 // Get the android app directory. 272 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) { 273 return -1; 274 } 275 276 // Get the android protected app directory. 277 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) { 278 return -1; 279 } 280 281 // Get the android app native library directory. 282 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) { 283 return -1; 284 } 285 286 // Get the sd-card ASEC mount point. 287 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) { 288 return -1; 289 } 290 291 // Get the android media directory. 292 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) { 293 return -1; 294 } 295 296 // Take note of the system and vendor directories. 297 android_system_dirs.count = 2; 298 299 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t)); 300 if (android_system_dirs.dirs == NULL) { 301 ALOGE("Couldn't allocate array for dirs; aborting\n"); 302 return -1; 303 } 304 305 // system 306 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) { 307 free_globals(); 308 return -1; 309 } 310 311 // append "app/" to dirs[0] 312 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR); 313 android_system_dirs.dirs[0].path = system_app_path; 314 android_system_dirs.dirs[0].len = strlen(system_app_path); 315 316 // vendor 317 // TODO replace this with an environment variable (doesn't exist yet) 318 android_system_dirs.dirs[1].path = "/vendor/app/"; 319 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); 320 321 return 0; 322 } 323 324 int initialize_directories() { 325 int res = -1; 326 327 // Read current filesystem layout version to handle upgrade paths 328 char version_path[PATH_MAX]; 329 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path); 330 331 int oldVersion; 332 if (fs_read_atomic_int(version_path, &oldVersion) == -1) { 333 oldVersion = 0; 334 } 335 int version = oldVersion; 336 337 // /data/user 338 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); 339 // /data/data 340 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); 341 // /data/user/0 342 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0"); 343 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) { 344 goto fail; 345 } 346 347 // Make the /data/user directory if necessary 348 if (access(user_data_dir, R_OK) < 0) { 349 if (mkdir(user_data_dir, 0711) < 0) { 350 goto fail; 351 } 352 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { 353 goto fail; 354 } 355 if (chmod(user_data_dir, 0711) < 0) { 356 goto fail; 357 } 358 } 359 // Make the /data/user/0 symlink to /data/data if necessary 360 if (access(primary_data_dir, R_OK) < 0) { 361 if (symlink(legacy_data_dir, primary_data_dir)) { 362 goto fail; 363 } 364 } 365 366 if (version == 0) { 367 // Introducing multi-user, so migrate /data/media contents into /data/media/0 368 ALOGD("Upgrading /data/media for multi-user"); 369 370 // Ensure /data/media 371 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 372 goto fail; 373 } 374 375 // /data/media.tmp 376 char media_tmp_dir[PATH_MAX]; 377 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path); 378 379 // Only copy when upgrade not already in progress 380 if (access(media_tmp_dir, F_OK) == -1) { 381 if (rename(android_media_dir.path, media_tmp_dir) == -1) { 382 ALOGE("Failed to move legacy media path: %s", strerror(errno)); 383 goto fail; 384 } 385 } 386 387 // Create /data/media again 388 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 389 goto fail; 390 } 391 392 // /data/media/0 393 char owner_media_dir[PATH_MAX]; 394 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path); 395 396 // Move any owner data into place 397 if (access(media_tmp_dir, F_OK) == 0) { 398 if (rename(media_tmp_dir, owner_media_dir) == -1) { 399 ALOGE("Failed to move owner media path: %s", strerror(errno)); 400 goto fail; 401 } 402 } 403 404 // Ensure media directories for any existing users 405 DIR *dir; 406 struct dirent *dirent; 407 char user_media_dir[PATH_MAX]; 408 409 dir = opendir(user_data_dir); 410 if (dir != NULL) { 411 while ((dirent = readdir(dir))) { 412 if (dirent->d_type == DT_DIR) { 413 const char *name = dirent->d_name; 414 415 // skip "." and ".." 416 if (name[0] == '.') { 417 if (name[1] == 0) continue; 418 if ((name[1] == '.') && (name[2] == 0)) continue; 419 } 420 421 // /data/media/<user_id> 422 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name); 423 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 424 goto fail; 425 } 426 } 427 } 428 closedir(dir); 429 } 430 431 version = 1; 432 } 433 434 // /data/media/obb 435 char media_obb_dir[PATH_MAX]; 436 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path); 437 438 if (version == 1) { 439 // Introducing /data/media/obb for sharing OBB across users; migrate 440 // any existing OBB files from owner. 441 ALOGD("Upgrading to shared /data/media/obb"); 442 443 // /data/media/0/Android/obb 444 char owner_obb_path[PATH_MAX]; 445 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path); 446 447 // Only move if target doesn't already exist 448 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) { 449 if (rename(owner_obb_path, media_obb_dir) == -1) { 450 ALOGE("Failed to move OBB from owner: %s", strerror(errno)); 451 goto fail; 452 } 453 } 454 455 version = 2; 456 } 457 458 if (ensure_media_user_dirs(0) == -1) { 459 ALOGE("Failed to setup media for user 0"); 460 goto fail; 461 } 462 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 463 goto fail; 464 } 465 466 // Persist layout version if changed 467 if (version != oldVersion) { 468 if (fs_write_atomic_int(version_path, version) == -1) { 469 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno)); 470 goto fail; 471 } 472 } 473 474 // Success! 475 res = 0; 476 477 fail: 478 free(user_data_dir); 479 free(legacy_data_dir); 480 free(primary_data_dir); 481 return res; 482 } 483 484 static void drop_privileges() { 485 if (prctl(PR_SET_KEEPCAPS, 1) < 0) { 486 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); 487 exit(1); 488 } 489 490 if (setgid(AID_INSTALL) < 0) { 491 ALOGE("setgid() can't drop privileges; exiting.\n"); 492 exit(1); 493 } 494 495 if (setuid(AID_INSTALL) < 0) { 496 ALOGE("setuid() can't drop privileges; exiting.\n"); 497 exit(1); 498 } 499 500 struct __user_cap_header_struct capheader; 501 struct __user_cap_data_struct capdata[2]; 502 memset(&capheader, 0, sizeof(capheader)); 503 memset(&capdata, 0, sizeof(capdata)); 504 capheader.version = _LINUX_CAPABILITY_VERSION_3; 505 capheader.pid = 0; 506 507 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE); 508 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN); 509 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID); 510 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID); 511 512 capdata[0].effective = capdata[0].permitted; 513 capdata[1].effective = capdata[1].permitted; 514 capdata[0].inheritable = 0; 515 capdata[1].inheritable = 0; 516 517 if (capset(&capheader, &capdata[0]) < 0) { 518 ALOGE("capset failed: %s\n", strerror(errno)); 519 exit(1); 520 } 521 } 522 523 int main(const int argc, const char *argv[]) { 524 char buf[BUFFER_MAX]; 525 struct sockaddr addr; 526 socklen_t alen; 527 int lsocket, s, count; 528 529 ALOGI("installd firing up\n"); 530 531 if (initialize_globals() < 0) { 532 ALOGE("Could not initialize globals; exiting.\n"); 533 exit(1); 534 } 535 536 if (initialize_directories() < 0) { 537 ALOGE("Could not create directories; exiting.\n"); 538 exit(1); 539 } 540 541 drop_privileges(); 542 543 lsocket = android_get_control_socket(SOCKET_PATH); 544 if (lsocket < 0) { 545 ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); 546 exit(1); 547 } 548 if (listen(lsocket, 5)) { 549 ALOGE("Listen on socket failed: %s\n", strerror(errno)); 550 exit(1); 551 } 552 fcntl(lsocket, F_SETFD, FD_CLOEXEC); 553 554 for (;;) { 555 alen = sizeof(addr); 556 s = accept(lsocket, &addr, &alen); 557 if (s < 0) { 558 ALOGE("Accept failed: %s\n", strerror(errno)); 559 continue; 560 } 561 fcntl(s, F_SETFD, FD_CLOEXEC); 562 563 ALOGI("new connection\n"); 564 for (;;) { 565 unsigned short count; 566 if (readx(s, &count, sizeof(count))) { 567 ALOGE("failed to read size\n"); 568 break; 569 } 570 if ((count < 1) || (count >= BUFFER_MAX)) { 571 ALOGE("invalid size %d\n", count); 572 break; 573 } 574 if (readx(s, buf, count)) { 575 ALOGE("failed to read command\n"); 576 break; 577 } 578 buf[count] = 0; 579 if (execute(s, buf)) break; 580 } 581 ALOGI("closing connection\n"); 582 close(s); 583 } 584 585 return 0; 586 } 587