blob: 7caa1465464a3f41f0f093f4220f21fbb8006643 [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{
Calin Juravleb1efac12014-08-21 19:05:20 +010041 /* 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 Lockwood94afecf2012-10-24 10:45:23 -070043}
44
Narayan Kamath091ea772014-11-10 15:03:46 +000045static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX])
46{
47 return mark_boot_complete(arg[0] /* instruction set */);
48}
49
50static int do_move_dex(char **arg, char reply[REPLY_MAX])
Mike Lockwood94afecf2012-10-24 10:45:23 -070051{
Narayan Kamath1b400322014-04-11 13:17:00 +010052 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070053}
54
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070055static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070056{
Narayan Kamath1b400322014-04-11 13:17:00 +010057 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070058}
59
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070060static int do_remove(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070061{
62 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
63}
64
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070065static int do_rename(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070066{
67 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
68}
69
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070070static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070071{
72 return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
73}
74
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070075static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -070076{
77 return free_cache((int64_t)atoll(arg[0])); /* free_size */
78}
79
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070080static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070081{
82 return delete_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
83}
84
Dan Albert9e8b5282014-09-12 09:00:50 -070085static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused)
Jeff Sharkeyc796b682014-07-15 21:49:51 -070086{
87 return delete_code_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
88}
89
Mike Lockwood94afecf2012-10-24 10:45:23 -070090static int do_get_size(char **arg, char reply[REPLY_MAX])
91{
92 int64_t codesize = 0;
93 int64_t datasize = 0;
94 int64_t cachesize = 0;
95 int64_t asecsize = 0;
96 int res = 0;
97
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -070098 /* pkgdir, userid, apkpath */
Dianne Hackborn8b417802013-05-01 18:55:10 -070099 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5],
Narayan Kamath1b400322014-04-11 13:17:00 +0100100 arg[6], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700101
102 /*
103 * Each int64_t can take up 22 characters printed out. Make sure it
104 * doesn't go over REPLY_MAX in the future.
105 */
106 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
107 codesize, datasize, cachesize, asecsize);
108 return res;
109}
110
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700111static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112{
113 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
114}
115
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700116static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700117{
Robert Craig880d1a92013-07-29 09:18:09 -0400118 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]);
119 /* pkgname, uid, userid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700120}
121
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700122static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused)
Robin Lee095c7632014-04-25 15:05:19 +0100123{
Robin Lee7c8bec02014-06-10 18:46:26 +0100124 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100125}
126
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700127static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700128{
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700129 return delete_user(atoi(arg[0])); /* userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700130}
131
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700132static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133{
134 return movefiles();
135}
136
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700137static int do_linklib(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138{
139 return linklib(arg[0], arg[1], atoi(arg[2]));
140}
141
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700142static int do_idmap(char **arg, char reply[REPLY_MAX] __unused)
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100143{
144 return idmap(arg[0], arg[1], atoi(arg[2]));
145}
146
Robert Craigda30dc72014-03-27 10:21:12 -0400147static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
Robert Craige9887e42014-02-20 10:25:56 -0500148{
Robert Craigda30dc72014-03-27 10:21:12 -0400149 return restorecon_data(arg[0], arg[1], atoi(arg[2]));
150 /* pkgName, seinfo, uid*/
Robert Craige9887e42014-02-20 10:25:56 -0500151}
152
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700153static int do_patchoat(char **arg, char reply[REPLY_MAX] __unused) {
Calin Juravleb1efac12014-08-21 19:05:20 +0100154 /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate */
155 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 0, 1);
Alex Light43c5d302014-07-21 12:23:48 -0700156}
157
Mike Lockwood94afecf2012-10-24 10:45:23 -0700158struct cmdinfo {
159 const char *name;
160 unsigned numargs;
161 int (*func)(char **arg, char reply[REPLY_MAX]);
162};
163
164struct cmdinfo cmds[] = {
165 { "ping", 0, do_ping },
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400166 { "install", 4, do_install },
Calin Juravleb1efac12014-08-21 19:05:20 +0100167 { "dexopt", 6, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000168 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100169 { "movedex", 3, do_move_dex },
170 { "rmdex", 2, do_rm_dex },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700171 { "remove", 2, do_remove },
172 { "rename", 2, do_rename },
173 { "fixuid", 3, do_fixuid },
174 { "freecache", 1, do_free_cache },
175 { "rmcache", 2, do_rm_cache },
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700176 { "rmcodecache", 2, do_rm_code_cache },
Narayan Kamath1b400322014-04-11 13:17:00 +0100177 { "getsize", 7, do_get_size },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700178 { "rmuserdata", 2, do_rm_user_data },
179 { "movefiles", 0, do_movefiles },
180 { "linklib", 3, do_linklib },
Robert Craig880d1a92013-07-29 09:18:09 -0400181 { "mkuserdata", 4, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100182 { "mkuserconfig", 1, do_mk_user_config },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700183 { "rmuser", 1, do_rm_user },
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100184 { "idmap", 3, do_idmap },
Robert Craigda30dc72014-03-27 10:21:12 -0400185 { "restorecondata", 3, do_restorecon_data },
Alex Light43c5d302014-07-21 12:23:48 -0700186 { "patchoat", 5, do_patchoat },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187};
188
189static int readx(int s, void *_buf, int count)
190{
191 char *buf = _buf;
192 int n = 0, r;
193 if (count < 0) return -1;
194 while (n < count) {
195 r = read(s, buf + n, count - n);
196 if (r < 0) {
197 if (errno == EINTR) continue;
198 ALOGE("read error: %s\n", strerror(errno));
199 return -1;
200 }
201 if (r == 0) {
202 ALOGE("eof\n");
203 return -1; /* EOF */
204 }
205 n += r;
206 }
207 return 0;
208}
209
210static int writex(int s, const void *_buf, int count)
211{
212 const char *buf = _buf;
213 int n = 0, r;
214 if (count < 0) return -1;
215 while (n < count) {
216 r = write(s, buf + n, count - n);
217 if (r < 0) {
218 if (errno == EINTR) continue;
219 ALOGE("write error: %s\n", strerror(errno));
220 return -1;
221 }
222 n += r;
223 }
224 return 0;
225}
226
227
228/* Tokenize the command buffer, locate a matching command,
229 * ensure that the required number of arguments are provided,
230 * call the function(), return the result.
231 */
232static int execute(int s, char cmd[BUFFER_MAX])
233{
234 char reply[REPLY_MAX];
235 char *arg[TOKEN_MAX+1];
236 unsigned i;
237 unsigned n = 0;
238 unsigned short count;
239 int ret = -1;
240
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700241 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700242
243 /* default reply is "" */
244 reply[0] = 0;
245
246 /* n is number of args (not counting arg[0]) */
247 arg[0] = cmd;
248 while (*cmd) {
249 if (isspace(*cmd)) {
250 *cmd++ = 0;
251 n++;
252 arg[n] = cmd;
253 if (n == TOKEN_MAX) {
254 ALOGE("too many arguments\n");
255 goto done;
256 }
257 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600258 if (*cmd) {
259 cmd++;
260 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700261 }
262
263 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
264 if (!strcmp(cmds[i].name,arg[0])) {
265 if (n != cmds[i].numargs) {
266 ALOGE("%s requires %d arguments (%d given)\n",
267 cmds[i].name, cmds[i].numargs, n);
268 } else {
269 ret = cmds[i].func(arg + 1, reply);
270 }
271 goto done;
272 }
273 }
274 ALOGE("unsupported command '%s'\n", arg[0]);
275
276done:
277 if (reply[0]) {
278 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
279 } else {
280 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
281 }
282 if (n > BUFFER_MAX) n = BUFFER_MAX;
283 count = n;
284
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700285 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700286 if (writex(s, &count, sizeof(count))) return -1;
287 if (writex(s, cmd, count)) return -1;
288 return 0;
289}
290
291/**
292 * Initialize all the global variables that are used elsewhere. Returns 0 upon
293 * success and -1 on error.
294 */
295void free_globals() {
296 size_t i;
297
298 for (i = 0; i < android_system_dirs.count; i++) {
299 if (android_system_dirs.dirs[i].path != NULL) {
300 free(android_system_dirs.dirs[i].path);
301 }
302 }
303
304 free(android_system_dirs.dirs);
305}
306
307int initialize_globals() {
308 // Get the android data directory.
309 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
310 return -1;
311 }
312
313 // Get the android app directory.
314 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
315 return -1;
316 }
317
318 // Get the android protected app directory.
319 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
320 return -1;
321 }
322
323 // Get the android app native library directory.
324 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
325 return -1;
326 }
327
328 // Get the sd-card ASEC mount point.
329 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
330 return -1;
331 }
332
333 // Get the android media directory.
334 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
335 return -1;
336 }
337
338 // Take note of the system and vendor directories.
Jeff Sharkey770180a2014-09-08 17:14:26 -0700339 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700340
341 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
342 if (android_system_dirs.dirs == NULL) {
343 ALOGE("Couldn't allocate array for dirs; aborting\n");
344 return -1;
345 }
346
Jeff Sharkey770180a2014-09-08 17:14:26 -0700347 dir_rec_t android_root_dir;
348 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
349 ALOGE("Missing ANDROID_ROOT; aborting\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700350 return -1;
351 }
352
Jeff Sharkey770180a2014-09-08 17:14:26 -0700353 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
354 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700355
Jeff Sharkey770180a2014-09-08 17:14:26 -0700356 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700357 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
358
Jeff Sharkey770180a2014-09-08 17:14:26 -0700359 android_system_dirs.dirs[2].path = "/vendor/app/";
360 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
361
362 android_system_dirs.dirs[3].path = "/oem/app/";
363 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
364
Mike Lockwood94afecf2012-10-24 10:45:23 -0700365 return 0;
366}
367
368int initialize_directories() {
369 int res = -1;
370
371 // Read current filesystem layout version to handle upgrade paths
372 char version_path[PATH_MAX];
373 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
374
375 int oldVersion;
376 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
377 oldVersion = 0;
378 }
379 int version = oldVersion;
380
381 // /data/user
382 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
383 // /data/data
384 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
385 // /data/user/0
386 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
387 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
388 goto fail;
389 }
390
391 // Make the /data/user directory if necessary
392 if (access(user_data_dir, R_OK) < 0) {
393 if (mkdir(user_data_dir, 0711) < 0) {
394 goto fail;
395 }
396 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
397 goto fail;
398 }
399 if (chmod(user_data_dir, 0711) < 0) {
400 goto fail;
401 }
402 }
403 // Make the /data/user/0 symlink to /data/data if necessary
404 if (access(primary_data_dir, R_OK) < 0) {
405 if (symlink(legacy_data_dir, primary_data_dir)) {
406 goto fail;
407 }
408 }
409
410 if (version == 0) {
411 // Introducing multi-user, so migrate /data/media contents into /data/media/0
412 ALOGD("Upgrading /data/media for multi-user");
413
414 // Ensure /data/media
415 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
416 goto fail;
417 }
418
419 // /data/media.tmp
420 char media_tmp_dir[PATH_MAX];
421 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
422
423 // Only copy when upgrade not already in progress
424 if (access(media_tmp_dir, F_OK) == -1) {
425 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
426 ALOGE("Failed to move legacy media path: %s", strerror(errno));
427 goto fail;
428 }
429 }
430
431 // Create /data/media again
432 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
433 goto fail;
434 }
435
Stephen Smalley26288202014-02-07 09:16:46 -0500436 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500437 goto fail;
438 }
439
Mike Lockwood94afecf2012-10-24 10:45:23 -0700440 // /data/media/0
441 char owner_media_dir[PATH_MAX];
442 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
443
444 // Move any owner data into place
445 if (access(media_tmp_dir, F_OK) == 0) {
446 if (rename(media_tmp_dir, owner_media_dir) == -1) {
447 ALOGE("Failed to move owner media path: %s", strerror(errno));
448 goto fail;
449 }
450 }
451
452 // Ensure media directories for any existing users
453 DIR *dir;
454 struct dirent *dirent;
455 char user_media_dir[PATH_MAX];
456
457 dir = opendir(user_data_dir);
458 if (dir != NULL) {
459 while ((dirent = readdir(dir))) {
460 if (dirent->d_type == DT_DIR) {
461 const char *name = dirent->d_name;
462
463 // skip "." and ".."
464 if (name[0] == '.') {
465 if (name[1] == 0) continue;
466 if ((name[1] == '.') && (name[2] == 0)) continue;
467 }
468
469 // /data/media/<user_id>
470 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
471 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
472 goto fail;
473 }
474 }
475 }
476 closedir(dir);
477 }
478
479 version = 1;
480 }
481
482 // /data/media/obb
483 char media_obb_dir[PATH_MAX];
484 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
485
486 if (version == 1) {
487 // Introducing /data/media/obb for sharing OBB across users; migrate
488 // any existing OBB files from owner.
489 ALOGD("Upgrading to shared /data/media/obb");
490
491 // /data/media/0/Android/obb
492 char owner_obb_path[PATH_MAX];
493 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
494
495 // Only move if target doesn't already exist
496 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
497 if (rename(owner_obb_path, media_obb_dir) == -1) {
498 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
499 goto fail;
500 }
501 }
502
503 version = 2;
504 }
505
506 if (ensure_media_user_dirs(0) == -1) {
507 ALOGE("Failed to setup media for user 0");
508 goto fail;
509 }
510 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
511 goto fail;
512 }
513
Robin Lee07053fc2014-04-29 19:42:01 +0100514 if (ensure_config_user_dirs(0) == -1) {
515 ALOGE("Failed to setup misc for user 0");
516 goto fail;
517 }
518
Robin Lee095c7632014-04-25 15:05:19 +0100519 if (version == 2) {
520 ALOGD("Upgrading to /data/misc/user directories");
521
Robin Lee60fd3fe2014-10-07 16:55:02 +0100522 char misc_dir[PATH_MAX];
523 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
524
525 char keychain_added_dir[PATH_MAX];
526 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
527
528 char keychain_removed_dir[PATH_MAX];
529 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
530
Robin Lee095c7632014-04-25 15:05:19 +0100531 DIR *dir;
532 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100533 dir = opendir(user_data_dir);
534 if (dir != NULL) {
535 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100536 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100537
Robin Lee60fd3fe2014-10-07 16:55:02 +0100538 // skip "." and ".."
539 if (name[0] == '.') {
540 if (name[1] == 0) continue;
541 if ((name[1] == '.') && (name[2] == 0)) continue;
542 }
543
544 uint32_t user_id = atoi(name);
545
546 // /data/misc/user/<user_id>
547 if (ensure_config_user_dirs(user_id) == -1) {
548 goto fail;
549 }
550
551 char misc_added_dir[PATH_MAX];
552 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
553
554 char misc_removed_dir[PATH_MAX];
555 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
556
557 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
558 gid_t gid = uid;
559 if (access(keychain_added_dir, F_OK) == 0) {
560 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
561 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100562 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100563 }
564 if (access(keychain_removed_dir, F_OK) == 0) {
565 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
566 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100567 }
568 }
569 }
570 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100571
Robin Lee60fd3fe2014-10-07 16:55:02 +0100572 if (access(keychain_added_dir, F_OK) == 0) {
573 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100574 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100575 if (access(keychain_removed_dir, F_OK) == 0) {
576 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100577 }
578 }
579
580 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100581 }
582
Mike Lockwood94afecf2012-10-24 10:45:23 -0700583 // Persist layout version if changed
584 if (version != oldVersion) {
585 if (fs_write_atomic_int(version_path, version) == -1) {
586 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
587 goto fail;
588 }
589 }
590
591 // Success!
592 res = 0;
593
594fail:
595 free(user_data_dir);
596 free(legacy_data_dir);
597 free(primary_data_dir);
598 return res;
599}
600
601static void drop_privileges() {
602 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
603 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
604 exit(1);
605 }
606
607 if (setgid(AID_INSTALL) < 0) {
608 ALOGE("setgid() can't drop privileges; exiting.\n");
609 exit(1);
610 }
611
612 if (setuid(AID_INSTALL) < 0) {
613 ALOGE("setuid() can't drop privileges; exiting.\n");
614 exit(1);
615 }
616
617 struct __user_cap_header_struct capheader;
618 struct __user_cap_data_struct capdata[2];
619 memset(&capheader, 0, sizeof(capheader));
620 memset(&capdata, 0, sizeof(capdata));
621 capheader.version = _LINUX_CAPABILITY_VERSION_3;
622 capheader.pid = 0;
623
624 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
625 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
626 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
627 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100628 capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700629
630 capdata[0].effective = capdata[0].permitted;
631 capdata[1].effective = capdata[1].permitted;
632 capdata[0].inheritable = 0;
633 capdata[1].inheritable = 0;
634
635 if (capset(&capheader, &capdata[0]) < 0) {
636 ALOGE("capset failed: %s\n", strerror(errno));
637 exit(1);
638 }
639}
640
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400641static int log_callback(int type, const char *fmt, ...) {
642 va_list ap;
643 int priority;
644
645 switch (type) {
646 case SELINUX_WARNING:
647 priority = ANDROID_LOG_WARN;
648 break;
649 case SELINUX_INFO:
650 priority = ANDROID_LOG_INFO;
651 break;
652 default:
653 priority = ANDROID_LOG_ERROR;
654 break;
655 }
656 va_start(ap, fmt);
657 LOG_PRI_VA(priority, "SELinux", fmt, ap);
658 va_end(ap);
659 return 0;
660}
661
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700662int main(const int argc __unused, const char *argv[] __unused) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700663 char buf[BUFFER_MAX];
664 struct sockaddr addr;
665 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700666 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400667 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700668
669 ALOGI("installd firing up\n");
670
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400671 union selinux_callback cb;
672 cb.func_log = log_callback;
673 selinux_set_callback(SELINUX_CB_LOG, cb);
674
Mike Lockwood94afecf2012-10-24 10:45:23 -0700675 if (initialize_globals() < 0) {
676 ALOGE("Could not initialize globals; exiting.\n");
677 exit(1);
678 }
679
680 if (initialize_directories() < 0) {
681 ALOGE("Could not create directories; exiting.\n");
682 exit(1);
683 }
684
Stephen Smalleybd558d62013-04-16 12:16:50 -0400685 if (selinux_enabled && selinux_status_open(true) < 0) {
686 ALOGE("Could not open selinux status; exiting.\n");
687 exit(1);
688 }
689
Mike Lockwood94afecf2012-10-24 10:45:23 -0700690 drop_privileges();
691
692 lsocket = android_get_control_socket(SOCKET_PATH);
693 if (lsocket < 0) {
694 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
695 exit(1);
696 }
697 if (listen(lsocket, 5)) {
698 ALOGE("Listen on socket failed: %s\n", strerror(errno));
699 exit(1);
700 }
701 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
702
703 for (;;) {
704 alen = sizeof(addr);
705 s = accept(lsocket, &addr, &alen);
706 if (s < 0) {
707 ALOGE("Accept failed: %s\n", strerror(errno));
708 continue;
709 }
710 fcntl(s, F_SETFD, FD_CLOEXEC);
711
712 ALOGI("new connection\n");
713 for (;;) {
714 unsigned short count;
715 if (readx(s, &count, sizeof(count))) {
716 ALOGE("failed to read size\n");
717 break;
718 }
719 if ((count < 1) || (count >= BUFFER_MAX)) {
720 ALOGE("invalid size %d\n", count);
721 break;
722 }
723 if (readx(s, buf, count)) {
724 ALOGE("failed to read command\n");
725 break;
726 }
727 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400728 if (selinux_enabled && selinux_status_updated() > 0) {
729 selinux_android_seapp_context_reload();
730 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700731 if (execute(s, buf)) break;
732 }
733 ALOGI("closing connection\n");
734 close(s);
735 }
736
737 return 0;
738}