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