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