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