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