blob: 8f941705126bb9f4ab4bc0601dfda2f9580133d3 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
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 Kralevichd7471292013-02-28 16:59:13 -080017#include <sys/capability.h>
Elliott Hughes119b7652014-07-18 17:29:15 -070018#include <sys/prctl.h>
Stephen Smalleybd558d62013-04-16 12:16:50 -040019#include <selinux/android.h>
20#include <selinux/avc.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070021
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
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070029static int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070030{
31 return 0;
32}
33
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070034static int do_install(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070035{
Robert Craig4d3fd4e2013-03-25 06:33:03 -040036 return install(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]); /* pkgname, uid, gid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -070037}
38
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070039static int do_dexopt(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070040{
Andreas Gampe598c25e2015-03-03 09:15:06 -080041 /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate,
42 debuggable */
43 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 0,
44 atoi(arg[6]));
Mike Lockwood94afecf2012-10-24 10:45:23 -070045}
46
Bernhard Rosenkränzer7fb390d2014-11-23 22:28:26 +010047static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused)
Narayan Kamath091ea772014-11-10 15:03:46 +000048{
49 return mark_boot_complete(arg[0] /* instruction set */);
50}
51
Bernhard Rosenkränzer7fb390d2014-11-23 22:28:26 +010052static int do_move_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070053{
Narayan Kamath1b400322014-04-11 13:17:00 +010054 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070055}
56
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070057static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070058{
Narayan Kamath1b400322014-04-11 13:17:00 +010059 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070060}
61
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070062static int do_remove(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070063{
64 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
65}
66
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070067static int do_rename(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070068{
69 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
70}
71
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070072static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070073{
74 return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
75}
76
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070077static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -070078{
79 return free_cache((int64_t)atoll(arg[0])); /* free_size */
80}
81
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070082static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070083{
84 return delete_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
85}
86
Dan Albert9e8b5282014-09-12 09:00:50 -070087static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused)
Jeff Sharkeyc796b682014-07-15 21:49:51 -070088{
89 return delete_code_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
90}
91
Mike Lockwood94afecf2012-10-24 10:45:23 -070092static int do_get_size(char **arg, char reply[REPLY_MAX])
93{
94 int64_t codesize = 0;
95 int64_t datasize = 0;
96 int64_t cachesize = 0;
97 int64_t asecsize = 0;
98 int res = 0;
99
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700100 /* pkgdir, userid, apkpath */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700101 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5],
Narayan Kamath1b400322014-04-11 13:17:00 +0100102 arg[6], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700103
104 /*
105 * Each int64_t can take up 22 characters printed out. Make sure it
106 * doesn't go over REPLY_MAX in the future.
107 */
108 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
109 codesize, datasize, cachesize, asecsize);
110 return res;
111}
112
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700113static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700114{
115 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
116}
117
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700118static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700119{
Robert Craig880d1a92013-07-29 09:18:09 -0400120 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]);
121 /* pkgname, uid, userid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122}
123
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700124static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused)
Robin Lee095c7632014-04-25 15:05:19 +0100125{
Robin Lee7c8bec02014-06-10 18:46:26 +0100126 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100127}
128
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700129static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700130{
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700131 return delete_user(atoi(arg[0])); /* userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700132}
133
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700134static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700135{
136 return movefiles();
137}
138
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700139static int do_linklib(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700140{
141 return linklib(arg[0], arg[1], atoi(arg[2]));
142}
143
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700144static int do_idmap(char **arg, char reply[REPLY_MAX] __unused)
Mårten Kongstad63568b12014-01-31 14:42:59 +0100145{
146 return idmap(arg[0], arg[1], atoi(arg[2]));
147}
148
Robert Craigda30dc72014-03-27 10:21:12 -0400149static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
Robert Craige9887e42014-02-20 10:25:56 -0500150{
Robert Craigda30dc72014-03-27 10:21:12 -0400151 return restorecon_data(arg[0], arg[1], atoi(arg[2]));
152 /* pkgName, seinfo, uid*/
Robert Craige9887e42014-02-20 10:25:56 -0500153}
154
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700155static int do_patchoat(char **arg, char reply[REPLY_MAX] __unused) {
Andreas Gampe598c25e2015-03-03 09:15:06 -0800156 /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate,
157 debuggable */
158 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 0, 1, 0);
Alex Light43c5d302014-07-21 12:23:48 -0700159}
160
Mike Lockwood94afecf2012-10-24 10:45:23 -0700161struct cmdinfo {
162 const char *name;
163 unsigned numargs;
164 int (*func)(char **arg, char reply[REPLY_MAX]);
165};
166
167struct cmdinfo cmds[] = {
168 { "ping", 0, do_ping },
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400169 { "install", 4, do_install },
Andreas Gampe598c25e2015-03-03 09:15:06 -0800170 { "dexopt", 7, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000171 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100172 { "movedex", 3, do_move_dex },
173 { "rmdex", 2, do_rm_dex },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700174 { "remove", 2, do_remove },
175 { "rename", 2, do_rename },
176 { "fixuid", 3, do_fixuid },
177 { "freecache", 1, do_free_cache },
178 { "rmcache", 2, do_rm_cache },
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700179 { "rmcodecache", 2, do_rm_code_cache },
Narayan Kamath1b400322014-04-11 13:17:00 +0100180 { "getsize", 7, do_get_size },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181 { "rmuserdata", 2, do_rm_user_data },
182 { "movefiles", 0, do_movefiles },
183 { "linklib", 3, do_linklib },
Robert Craig880d1a92013-07-29 09:18:09 -0400184 { "mkuserdata", 4, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100185 { "mkuserconfig", 1, do_mk_user_config },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700186 { "rmuser", 1, do_rm_user },
Mårten Kongstad63568b12014-01-31 14:42:59 +0100187 { "idmap", 3, do_idmap },
Robert Craigda30dc72014-03-27 10:21:12 -0400188 { "restorecondata", 3, do_restorecon_data },
Alex Light43c5d302014-07-21 12:23:48 -0700189 { "patchoat", 5, do_patchoat },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700190};
191
192static int readx(int s, void *_buf, int count)
193{
194 char *buf = _buf;
195 int n = 0, r;
196 if (count < 0) return -1;
197 while (n < count) {
198 r = read(s, buf + n, count - n);
199 if (r < 0) {
200 if (errno == EINTR) continue;
201 ALOGE("read error: %s\n", strerror(errno));
202 return -1;
203 }
204 if (r == 0) {
205 ALOGE("eof\n");
206 return -1; /* EOF */
207 }
208 n += r;
209 }
210 return 0;
211}
212
213static int writex(int s, const void *_buf, int count)
214{
215 const char *buf = _buf;
216 int n = 0, r;
217 if (count < 0) return -1;
218 while (n < count) {
219 r = write(s, buf + n, count - n);
220 if (r < 0) {
221 if (errno == EINTR) continue;
222 ALOGE("write error: %s\n", strerror(errno));
223 return -1;
224 }
225 n += r;
226 }
227 return 0;
228}
229
230
231/* Tokenize the command buffer, locate a matching command,
232 * ensure that the required number of arguments are provided,
233 * call the function(), return the result.
234 */
235static int execute(int s, char cmd[BUFFER_MAX])
236{
237 char reply[REPLY_MAX];
238 char *arg[TOKEN_MAX+1];
239 unsigned i;
240 unsigned n = 0;
241 unsigned short count;
242 int ret = -1;
243
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700244 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245
246 /* default reply is "" */
247 reply[0] = 0;
248
249 /* n is number of args (not counting arg[0]) */
250 arg[0] = cmd;
251 while (*cmd) {
252 if (isspace(*cmd)) {
253 *cmd++ = 0;
254 n++;
255 arg[n] = cmd;
256 if (n == TOKEN_MAX) {
257 ALOGE("too many arguments\n");
258 goto done;
259 }
260 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600261 if (*cmd) {
262 cmd++;
263 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700264 }
265
266 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
267 if (!strcmp(cmds[i].name,arg[0])) {
268 if (n != cmds[i].numargs) {
269 ALOGE("%s requires %d arguments (%d given)\n",
270 cmds[i].name, cmds[i].numargs, n);
271 } else {
272 ret = cmds[i].func(arg + 1, reply);
273 }
274 goto done;
275 }
276 }
277 ALOGE("unsupported command '%s'\n", arg[0]);
278
279done:
280 if (reply[0]) {
281 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
282 } else {
283 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
284 }
285 if (n > BUFFER_MAX) n = BUFFER_MAX;
286 count = n;
287
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700288 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700289 if (writex(s, &count, sizeof(count))) return -1;
290 if (writex(s, cmd, count)) return -1;
291 return 0;
292}
293
294/**
295 * Initialize all the global variables that are used elsewhere. Returns 0 upon
296 * success and -1 on error.
297 */
298void free_globals() {
299 size_t i;
300
301 for (i = 0; i < android_system_dirs.count; i++) {
302 if (android_system_dirs.dirs[i].path != NULL) {
303 free(android_system_dirs.dirs[i].path);
304 }
305 }
306
307 free(android_system_dirs.dirs);
308}
309
310int initialize_globals() {
311 // Get the android data directory.
312 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
313 return -1;
314 }
315
316 // Get the android app directory.
317 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
318 return -1;
319 }
320
321 // Get the android protected app directory.
322 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
323 return -1;
324 }
325
326 // Get the android app native library directory.
327 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
328 return -1;
329 }
330
331 // Get the sd-card ASEC mount point.
332 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
333 return -1;
334 }
335
336 // Get the android media directory.
337 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
338 return -1;
339 }
340
341 // Take note of the system and vendor directories.
Jeff Sharkey770180a2014-09-08 17:14:26 -0700342 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700343
344 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
345 if (android_system_dirs.dirs == NULL) {
346 ALOGE("Couldn't allocate array for dirs; aborting\n");
347 return -1;
348 }
349
Jeff Sharkey770180a2014-09-08 17:14:26 -0700350 dir_rec_t android_root_dir;
351 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
352 ALOGE("Missing ANDROID_ROOT; aborting\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700353 return -1;
354 }
355
Jeff Sharkey770180a2014-09-08 17:14:26 -0700356 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
357 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700358
Jeff Sharkey770180a2014-09-08 17:14:26 -0700359 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700360 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
361
Jeff Sharkey770180a2014-09-08 17:14:26 -0700362 android_system_dirs.dirs[2].path = "/vendor/app/";
363 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
364
365 android_system_dirs.dirs[3].path = "/oem/app/";
366 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
367
Mike Lockwood94afecf2012-10-24 10:45:23 -0700368 return 0;
369}
370
371int initialize_directories() {
372 int res = -1;
373
374 // Read current filesystem layout version to handle upgrade paths
375 char version_path[PATH_MAX];
376 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
377
378 int oldVersion;
379 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
380 oldVersion = 0;
381 }
382 int version = oldVersion;
383
384 // /data/user
385 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
386 // /data/data
387 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
388 // /data/user/0
389 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
390 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
391 goto fail;
392 }
393
394 // Make the /data/user directory if necessary
395 if (access(user_data_dir, R_OK) < 0) {
396 if (mkdir(user_data_dir, 0711) < 0) {
397 goto fail;
398 }
399 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
400 goto fail;
401 }
402 if (chmod(user_data_dir, 0711) < 0) {
403 goto fail;
404 }
405 }
406 // Make the /data/user/0 symlink to /data/data if necessary
407 if (access(primary_data_dir, R_OK) < 0) {
408 if (symlink(legacy_data_dir, primary_data_dir)) {
409 goto fail;
410 }
411 }
412
413 if (version == 0) {
414 // Introducing multi-user, so migrate /data/media contents into /data/media/0
415 ALOGD("Upgrading /data/media for multi-user");
416
417 // Ensure /data/media
418 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
419 goto fail;
420 }
421
422 // /data/media.tmp
423 char media_tmp_dir[PATH_MAX];
424 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
425
426 // Only copy when upgrade not already in progress
427 if (access(media_tmp_dir, F_OK) == -1) {
428 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
429 ALOGE("Failed to move legacy media path: %s", strerror(errno));
430 goto fail;
431 }
432 }
433
434 // Create /data/media again
435 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
436 goto fail;
437 }
438
Stephen Smalley26288202014-02-07 09:16:46 -0500439 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500440 goto fail;
441 }
442
Mike Lockwood94afecf2012-10-24 10:45:23 -0700443 // /data/media/0
444 char owner_media_dir[PATH_MAX];
445 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
446
447 // Move any owner data into place
448 if (access(media_tmp_dir, F_OK) == 0) {
449 if (rename(media_tmp_dir, owner_media_dir) == -1) {
450 ALOGE("Failed to move owner media path: %s", strerror(errno));
451 goto fail;
452 }
453 }
454
455 // Ensure media directories for any existing users
456 DIR *dir;
457 struct dirent *dirent;
458 char user_media_dir[PATH_MAX];
459
460 dir = opendir(user_data_dir);
461 if (dir != NULL) {
462 while ((dirent = readdir(dir))) {
463 if (dirent->d_type == DT_DIR) {
464 const char *name = dirent->d_name;
465
466 // skip "." and ".."
467 if (name[0] == '.') {
468 if (name[1] == 0) continue;
469 if ((name[1] == '.') && (name[2] == 0)) continue;
470 }
471
472 // /data/media/<user_id>
473 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
474 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
475 goto fail;
476 }
477 }
478 }
479 closedir(dir);
480 }
481
482 version = 1;
483 }
484
485 // /data/media/obb
486 char media_obb_dir[PATH_MAX];
487 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
488
489 if (version == 1) {
490 // Introducing /data/media/obb for sharing OBB across users; migrate
491 // any existing OBB files from owner.
492 ALOGD("Upgrading to shared /data/media/obb");
493
494 // /data/media/0/Android/obb
495 char owner_obb_path[PATH_MAX];
496 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
497
498 // Only move if target doesn't already exist
499 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
500 if (rename(owner_obb_path, media_obb_dir) == -1) {
501 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
502 goto fail;
503 }
504 }
505
506 version = 2;
507 }
508
509 if (ensure_media_user_dirs(0) == -1) {
510 ALOGE("Failed to setup media for user 0");
511 goto fail;
512 }
513 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
514 goto fail;
515 }
516
Robin Lee07053fc2014-04-29 19:42:01 +0100517 if (ensure_config_user_dirs(0) == -1) {
518 ALOGE("Failed to setup misc for user 0");
519 goto fail;
520 }
521
Robin Lee095c7632014-04-25 15:05:19 +0100522 if (version == 2) {
523 ALOGD("Upgrading to /data/misc/user directories");
524
Robin Lee60fd3fe2014-10-07 16:55:02 +0100525 char misc_dir[PATH_MAX];
526 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
527
528 char keychain_added_dir[PATH_MAX];
529 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
530
531 char keychain_removed_dir[PATH_MAX];
532 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
533
Robin Lee095c7632014-04-25 15:05:19 +0100534 DIR *dir;
535 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100536 dir = opendir(user_data_dir);
537 if (dir != NULL) {
538 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100539 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100540
Robin Lee60fd3fe2014-10-07 16:55:02 +0100541 // skip "." and ".."
542 if (name[0] == '.') {
543 if (name[1] == 0) continue;
544 if ((name[1] == '.') && (name[2] == 0)) continue;
545 }
546
547 uint32_t user_id = atoi(name);
548
549 // /data/misc/user/<user_id>
550 if (ensure_config_user_dirs(user_id) == -1) {
551 goto fail;
552 }
553
554 char misc_added_dir[PATH_MAX];
555 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
556
557 char misc_removed_dir[PATH_MAX];
558 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
559
560 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
561 gid_t gid = uid;
562 if (access(keychain_added_dir, F_OK) == 0) {
563 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
564 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100565 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100566 }
567 if (access(keychain_removed_dir, F_OK) == 0) {
568 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
569 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100570 }
571 }
572 }
573 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100574
Robin Lee60fd3fe2014-10-07 16:55:02 +0100575 if (access(keychain_added_dir, F_OK) == 0) {
576 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100577 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100578 if (access(keychain_removed_dir, F_OK) == 0) {
579 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100580 }
581 }
582
583 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100584 }
585
Mike Lockwood94afecf2012-10-24 10:45:23 -0700586 // Persist layout version if changed
587 if (version != oldVersion) {
588 if (fs_write_atomic_int(version_path, version) == -1) {
589 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
590 goto fail;
591 }
592 }
593
594 // Success!
595 res = 0;
596
597fail:
598 free(user_data_dir);
599 free(legacy_data_dir);
600 free(primary_data_dir);
601 return res;
602}
603
604static void drop_privileges() {
605 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
606 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
607 exit(1);
608 }
609
610 if (setgid(AID_INSTALL) < 0) {
611 ALOGE("setgid() can't drop privileges; exiting.\n");
612 exit(1);
613 }
614
615 if (setuid(AID_INSTALL) < 0) {
616 ALOGE("setuid() can't drop privileges; exiting.\n");
617 exit(1);
618 }
619
620 struct __user_cap_header_struct capheader;
621 struct __user_cap_data_struct capdata[2];
622 memset(&capheader, 0, sizeof(capheader));
623 memset(&capdata, 0, sizeof(capdata));
624 capheader.version = _LINUX_CAPABILITY_VERSION_3;
625 capheader.pid = 0;
626
627 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
628 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
629 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
630 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
Mårten Kongstad63568b12014-01-31 14:42:59 +0100631 capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700632
633 capdata[0].effective = capdata[0].permitted;
634 capdata[1].effective = capdata[1].permitted;
635 capdata[0].inheritable = 0;
636 capdata[1].inheritable = 0;
637
638 if (capset(&capheader, &capdata[0]) < 0) {
639 ALOGE("capset failed: %s\n", strerror(errno));
640 exit(1);
641 }
642}
643
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400644static int log_callback(int type, const char *fmt, ...) {
645 va_list ap;
646 int priority;
647
648 switch (type) {
649 case SELINUX_WARNING:
650 priority = ANDROID_LOG_WARN;
651 break;
652 case SELINUX_INFO:
653 priority = ANDROID_LOG_INFO;
654 break;
655 default:
656 priority = ANDROID_LOG_ERROR;
657 break;
658 }
659 va_start(ap, fmt);
660 LOG_PRI_VA(priority, "SELinux", fmt, ap);
661 va_end(ap);
662 return 0;
663}
664
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700665int main(const int argc __unused, const char *argv[] __unused) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700666 char buf[BUFFER_MAX];
667 struct sockaddr addr;
668 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700669 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400670 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700671
672 ALOGI("installd firing up\n");
673
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400674 union selinux_callback cb;
675 cb.func_log = log_callback;
676 selinux_set_callback(SELINUX_CB_LOG, cb);
677
Mike Lockwood94afecf2012-10-24 10:45:23 -0700678 if (initialize_globals() < 0) {
679 ALOGE("Could not initialize globals; exiting.\n");
680 exit(1);
681 }
682
683 if (initialize_directories() < 0) {
684 ALOGE("Could not create directories; exiting.\n");
685 exit(1);
686 }
687
Stephen Smalleybd558d62013-04-16 12:16:50 -0400688 if (selinux_enabled && selinux_status_open(true) < 0) {
689 ALOGE("Could not open selinux status; exiting.\n");
690 exit(1);
691 }
692
Mike Lockwood94afecf2012-10-24 10:45:23 -0700693 drop_privileges();
694
695 lsocket = android_get_control_socket(SOCKET_PATH);
696 if (lsocket < 0) {
697 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
698 exit(1);
699 }
700 if (listen(lsocket, 5)) {
701 ALOGE("Listen on socket failed: %s\n", strerror(errno));
702 exit(1);
703 }
704 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
705
706 for (;;) {
707 alen = sizeof(addr);
708 s = accept(lsocket, &addr, &alen);
709 if (s < 0) {
710 ALOGE("Accept failed: %s\n", strerror(errno));
711 continue;
712 }
713 fcntl(s, F_SETFD, FD_CLOEXEC);
714
715 ALOGI("new connection\n");
716 for (;;) {
717 unsigned short count;
718 if (readx(s, &count, sizeof(count))) {
719 ALOGE("failed to read size\n");
720 break;
721 }
722 if ((count < 1) || (count >= BUFFER_MAX)) {
723 ALOGE("invalid size %d\n", count);
724 break;
725 }
726 if (readx(s, buf, count)) {
727 ALOGE("failed to read command\n");
728 break;
729 }
730 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400731 if (selinux_enabled && selinux_status_updated() > 0) {
732 selinux_android_seapp_context_reload();
733 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700734 if (execute(s, buf)) break;
735 }
736 ALOGI("closing connection\n");
737 close(s);
738 }
739
740 return 0;
741}