blob: 5b7117514311b7f8465d67c8485f2df1dcbf3d77 [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 */
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -080026#define TOKEN_MAX 16 /* max number of arguments in buffer */
Mike Lockwood94afecf2012-10-24 10:45:23 -070027#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{
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -080041 /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate
42 debuggable, outputPath */
43 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 0,
44 atoi(arg[6]), arg[7]);
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,
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800157 debuggable, outputPath */
158 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 0, 1, 0, "!");
159}
160
161static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused)
162{
163 /* oat_dir, instruction_set */
164 return create_oat_dir(arg[0], arg[1]);
165}
166
167static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused)
168{
169 /* oat_dir */
170 return rm_package_dir(arg[0]);
Alex Light43c5d302014-07-21 12:23:48 -0700171}
172
Mike Lockwood94afecf2012-10-24 10:45:23 -0700173struct cmdinfo {
174 const char *name;
175 unsigned numargs;
176 int (*func)(char **arg, char reply[REPLY_MAX]);
177};
178
179struct cmdinfo cmds[] = {
180 { "ping", 0, do_ping },
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400181 { "install", 4, do_install },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800182 { "dexopt", 8, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000183 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100184 { "movedex", 3, do_move_dex },
185 { "rmdex", 2, do_rm_dex },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700186 { "remove", 2, do_remove },
187 { "rename", 2, do_rename },
188 { "fixuid", 3, do_fixuid },
189 { "freecache", 1, do_free_cache },
190 { "rmcache", 2, do_rm_cache },
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700191 { "rmcodecache", 2, do_rm_code_cache },
Narayan Kamath1b400322014-04-11 13:17:00 +0100192 { "getsize", 7, do_get_size },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700193 { "rmuserdata", 2, do_rm_user_data },
194 { "movefiles", 0, do_movefiles },
195 { "linklib", 3, do_linklib },
Robert Craig880d1a92013-07-29 09:18:09 -0400196 { "mkuserdata", 4, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100197 { "mkuserconfig", 1, do_mk_user_config },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198 { "rmuser", 1, do_rm_user },
Mårten Kongstad63568b12014-01-31 14:42:59 +0100199 { "idmap", 3, do_idmap },
Robert Craigda30dc72014-03-27 10:21:12 -0400200 { "restorecondata", 3, do_restorecon_data },
Alex Light43c5d302014-07-21 12:23:48 -0700201 { "patchoat", 5, do_patchoat },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800202 { "createoatdir", 2, do_create_oat_dir },
203 { "rmpackagedir", 1, do_rm_package_dir},
Mike Lockwood94afecf2012-10-24 10:45:23 -0700204};
205
206static int readx(int s, void *_buf, int count)
207{
208 char *buf = _buf;
209 int n = 0, r;
210 if (count < 0) return -1;
211 while (n < count) {
212 r = read(s, buf + n, count - n);
213 if (r < 0) {
214 if (errno == EINTR) continue;
215 ALOGE("read error: %s\n", strerror(errno));
216 return -1;
217 }
218 if (r == 0) {
219 ALOGE("eof\n");
220 return -1; /* EOF */
221 }
222 n += r;
223 }
224 return 0;
225}
226
227static int writex(int s, const void *_buf, int count)
228{
229 const char *buf = _buf;
230 int n = 0, r;
231 if (count < 0) return -1;
232 while (n < count) {
233 r = write(s, buf + n, count - n);
234 if (r < 0) {
235 if (errno == EINTR) continue;
236 ALOGE("write error: %s\n", strerror(errno));
237 return -1;
238 }
239 n += r;
240 }
241 return 0;
242}
243
244
245/* Tokenize the command buffer, locate a matching command,
246 * ensure that the required number of arguments are provided,
247 * call the function(), return the result.
248 */
249static int execute(int s, char cmd[BUFFER_MAX])
250{
251 char reply[REPLY_MAX];
252 char *arg[TOKEN_MAX+1];
253 unsigned i;
254 unsigned n = 0;
255 unsigned short count;
256 int ret = -1;
257
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700258 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700259
260 /* default reply is "" */
261 reply[0] = 0;
262
263 /* n is number of args (not counting arg[0]) */
264 arg[0] = cmd;
265 while (*cmd) {
266 if (isspace(*cmd)) {
267 *cmd++ = 0;
268 n++;
269 arg[n] = cmd;
270 if (n == TOKEN_MAX) {
271 ALOGE("too many arguments\n");
272 goto done;
273 }
274 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600275 if (*cmd) {
276 cmd++;
277 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700278 }
279
280 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
281 if (!strcmp(cmds[i].name,arg[0])) {
282 if (n != cmds[i].numargs) {
283 ALOGE("%s requires %d arguments (%d given)\n",
284 cmds[i].name, cmds[i].numargs, n);
285 } else {
286 ret = cmds[i].func(arg + 1, reply);
287 }
288 goto done;
289 }
290 }
291 ALOGE("unsupported command '%s'\n", arg[0]);
292
293done:
294 if (reply[0]) {
295 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
296 } else {
297 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
298 }
299 if (n > BUFFER_MAX) n = BUFFER_MAX;
300 count = n;
301
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700302 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700303 if (writex(s, &count, sizeof(count))) return -1;
304 if (writex(s, cmd, count)) return -1;
305 return 0;
306}
307
308/**
309 * Initialize all the global variables that are used elsewhere. Returns 0 upon
310 * success and -1 on error.
311 */
312void free_globals() {
313 size_t i;
314
315 for (i = 0; i < android_system_dirs.count; i++) {
316 if (android_system_dirs.dirs[i].path != NULL) {
317 free(android_system_dirs.dirs[i].path);
318 }
319 }
320
321 free(android_system_dirs.dirs);
322}
323
324int initialize_globals() {
325 // Get the android data directory.
326 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
327 return -1;
328 }
329
330 // Get the android app directory.
331 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
332 return -1;
333 }
334
335 // Get the android protected app directory.
336 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
337 return -1;
338 }
339
340 // Get the android app native library directory.
341 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
342 return -1;
343 }
344
345 // Get the sd-card ASEC mount point.
346 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
347 return -1;
348 }
349
350 // Get the android media directory.
351 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
352 return -1;
353 }
354
355 // Take note of the system and vendor directories.
Jeff Sharkey770180a2014-09-08 17:14:26 -0700356 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700357
358 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
359 if (android_system_dirs.dirs == NULL) {
360 ALOGE("Couldn't allocate array for dirs; aborting\n");
361 return -1;
362 }
363
Jeff Sharkey770180a2014-09-08 17:14:26 -0700364 dir_rec_t android_root_dir;
365 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
366 ALOGE("Missing ANDROID_ROOT; aborting\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700367 return -1;
368 }
369
Jeff Sharkey770180a2014-09-08 17:14:26 -0700370 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
371 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700372
Jeff Sharkey770180a2014-09-08 17:14:26 -0700373 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700374 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
375
Jeff Sharkey770180a2014-09-08 17:14:26 -0700376 android_system_dirs.dirs[2].path = "/vendor/app/";
377 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
378
379 android_system_dirs.dirs[3].path = "/oem/app/";
380 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
381
Mike Lockwood94afecf2012-10-24 10:45:23 -0700382 return 0;
383}
384
385int initialize_directories() {
386 int res = -1;
387
388 // Read current filesystem layout version to handle upgrade paths
389 char version_path[PATH_MAX];
390 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
391
392 int oldVersion;
393 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
394 oldVersion = 0;
395 }
396 int version = oldVersion;
397
398 // /data/user
399 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
400 // /data/data
401 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
402 // /data/user/0
403 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
404 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
405 goto fail;
406 }
407
408 // Make the /data/user directory if necessary
409 if (access(user_data_dir, R_OK) < 0) {
410 if (mkdir(user_data_dir, 0711) < 0) {
411 goto fail;
412 }
413 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
414 goto fail;
415 }
416 if (chmod(user_data_dir, 0711) < 0) {
417 goto fail;
418 }
419 }
420 // Make the /data/user/0 symlink to /data/data if necessary
421 if (access(primary_data_dir, R_OK) < 0) {
422 if (symlink(legacy_data_dir, primary_data_dir)) {
423 goto fail;
424 }
425 }
426
427 if (version == 0) {
428 // Introducing multi-user, so migrate /data/media contents into /data/media/0
429 ALOGD("Upgrading /data/media for multi-user");
430
431 // Ensure /data/media
432 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
433 goto fail;
434 }
435
436 // /data/media.tmp
437 char media_tmp_dir[PATH_MAX];
438 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
439
440 // Only copy when upgrade not already in progress
441 if (access(media_tmp_dir, F_OK) == -1) {
442 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
443 ALOGE("Failed to move legacy media path: %s", strerror(errno));
444 goto fail;
445 }
446 }
447
448 // Create /data/media again
449 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
450 goto fail;
451 }
452
Stephen Smalley26288202014-02-07 09:16:46 -0500453 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500454 goto fail;
455 }
456
Mike Lockwood94afecf2012-10-24 10:45:23 -0700457 // /data/media/0
458 char owner_media_dir[PATH_MAX];
459 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
460
461 // Move any owner data into place
462 if (access(media_tmp_dir, F_OK) == 0) {
463 if (rename(media_tmp_dir, owner_media_dir) == -1) {
464 ALOGE("Failed to move owner media path: %s", strerror(errno));
465 goto fail;
466 }
467 }
468
469 // Ensure media directories for any existing users
470 DIR *dir;
471 struct dirent *dirent;
472 char user_media_dir[PATH_MAX];
473
474 dir = opendir(user_data_dir);
475 if (dir != NULL) {
476 while ((dirent = readdir(dir))) {
477 if (dirent->d_type == DT_DIR) {
478 const char *name = dirent->d_name;
479
480 // skip "." and ".."
481 if (name[0] == '.') {
482 if (name[1] == 0) continue;
483 if ((name[1] == '.') && (name[2] == 0)) continue;
484 }
485
486 // /data/media/<user_id>
487 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
488 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
489 goto fail;
490 }
491 }
492 }
493 closedir(dir);
494 }
495
496 version = 1;
497 }
498
499 // /data/media/obb
500 char media_obb_dir[PATH_MAX];
501 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
502
503 if (version == 1) {
504 // Introducing /data/media/obb for sharing OBB across users; migrate
505 // any existing OBB files from owner.
506 ALOGD("Upgrading to shared /data/media/obb");
507
508 // /data/media/0/Android/obb
509 char owner_obb_path[PATH_MAX];
510 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
511
512 // Only move if target doesn't already exist
513 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
514 if (rename(owner_obb_path, media_obb_dir) == -1) {
515 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
516 goto fail;
517 }
518 }
519
520 version = 2;
521 }
522
523 if (ensure_media_user_dirs(0) == -1) {
524 ALOGE("Failed to setup media for user 0");
525 goto fail;
526 }
527 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
528 goto fail;
529 }
530
Robin Lee07053fc2014-04-29 19:42:01 +0100531 if (ensure_config_user_dirs(0) == -1) {
532 ALOGE("Failed to setup misc for user 0");
533 goto fail;
534 }
535
Robin Lee095c7632014-04-25 15:05:19 +0100536 if (version == 2) {
537 ALOGD("Upgrading to /data/misc/user directories");
538
Robin Lee60fd3fe2014-10-07 16:55:02 +0100539 char misc_dir[PATH_MAX];
540 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
541
542 char keychain_added_dir[PATH_MAX];
543 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
544
545 char keychain_removed_dir[PATH_MAX];
546 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
547
Robin Lee095c7632014-04-25 15:05:19 +0100548 DIR *dir;
549 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100550 dir = opendir(user_data_dir);
551 if (dir != NULL) {
552 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100553 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100554
Robin Lee60fd3fe2014-10-07 16:55:02 +0100555 // skip "." and ".."
556 if (name[0] == '.') {
557 if (name[1] == 0) continue;
558 if ((name[1] == '.') && (name[2] == 0)) continue;
559 }
560
561 uint32_t user_id = atoi(name);
562
563 // /data/misc/user/<user_id>
564 if (ensure_config_user_dirs(user_id) == -1) {
565 goto fail;
566 }
567
568 char misc_added_dir[PATH_MAX];
569 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
570
571 char misc_removed_dir[PATH_MAX];
572 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
573
574 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
575 gid_t gid = uid;
576 if (access(keychain_added_dir, F_OK) == 0) {
577 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
578 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100579 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100580 }
581 if (access(keychain_removed_dir, F_OK) == 0) {
582 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
583 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100584 }
585 }
586 }
587 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100588
Robin Lee60fd3fe2014-10-07 16:55:02 +0100589 if (access(keychain_added_dir, F_OK) == 0) {
590 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100591 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100592 if (access(keychain_removed_dir, F_OK) == 0) {
593 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100594 }
595 }
596
597 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100598 }
599
Mike Lockwood94afecf2012-10-24 10:45:23 -0700600 // Persist layout version if changed
601 if (version != oldVersion) {
602 if (fs_write_atomic_int(version_path, version) == -1) {
603 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
604 goto fail;
605 }
606 }
607
608 // Success!
609 res = 0;
610
611fail:
612 free(user_data_dir);
613 free(legacy_data_dir);
614 free(primary_data_dir);
615 return res;
616}
617
618static void drop_privileges() {
619 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
620 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
621 exit(1);
622 }
623
624 if (setgid(AID_INSTALL) < 0) {
625 ALOGE("setgid() can't drop privileges; exiting.\n");
626 exit(1);
627 }
628
629 if (setuid(AID_INSTALL) < 0) {
630 ALOGE("setuid() can't drop privileges; exiting.\n");
631 exit(1);
632 }
633
634 struct __user_cap_header_struct capheader;
635 struct __user_cap_data_struct capdata[2];
636 memset(&capheader, 0, sizeof(capheader));
637 memset(&capdata, 0, sizeof(capdata));
638 capheader.version = _LINUX_CAPABILITY_VERSION_3;
639 capheader.pid = 0;
640
641 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
642 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
643 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
644 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
Mårten Kongstad63568b12014-01-31 14:42:59 +0100645 capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700646
647 capdata[0].effective = capdata[0].permitted;
648 capdata[1].effective = capdata[1].permitted;
649 capdata[0].inheritable = 0;
650 capdata[1].inheritable = 0;
651
652 if (capset(&capheader, &capdata[0]) < 0) {
653 ALOGE("capset failed: %s\n", strerror(errno));
654 exit(1);
655 }
656}
657
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400658static int log_callback(int type, const char *fmt, ...) {
659 va_list ap;
660 int priority;
661
662 switch (type) {
663 case SELINUX_WARNING:
664 priority = ANDROID_LOG_WARN;
665 break;
666 case SELINUX_INFO:
667 priority = ANDROID_LOG_INFO;
668 break;
669 default:
670 priority = ANDROID_LOG_ERROR;
671 break;
672 }
673 va_start(ap, fmt);
674 LOG_PRI_VA(priority, "SELinux", fmt, ap);
675 va_end(ap);
676 return 0;
677}
678
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700679int main(const int argc __unused, const char *argv[] __unused) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700680 char buf[BUFFER_MAX];
681 struct sockaddr addr;
682 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700683 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400684 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700685
686 ALOGI("installd firing up\n");
687
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400688 union selinux_callback cb;
689 cb.func_log = log_callback;
690 selinux_set_callback(SELINUX_CB_LOG, cb);
691
Mike Lockwood94afecf2012-10-24 10:45:23 -0700692 if (initialize_globals() < 0) {
693 ALOGE("Could not initialize globals; exiting.\n");
694 exit(1);
695 }
696
697 if (initialize_directories() < 0) {
698 ALOGE("Could not create directories; exiting.\n");
699 exit(1);
700 }
701
Stephen Smalleybd558d62013-04-16 12:16:50 -0400702 if (selinux_enabled && selinux_status_open(true) < 0) {
703 ALOGE("Could not open selinux status; exiting.\n");
704 exit(1);
705 }
706
Mike Lockwood94afecf2012-10-24 10:45:23 -0700707 drop_privileges();
708
709 lsocket = android_get_control_socket(SOCKET_PATH);
710 if (lsocket < 0) {
711 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
712 exit(1);
713 }
714 if (listen(lsocket, 5)) {
715 ALOGE("Listen on socket failed: %s\n", strerror(errno));
716 exit(1);
717 }
718 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
719
720 for (;;) {
721 alen = sizeof(addr);
722 s = accept(lsocket, &addr, &alen);
723 if (s < 0) {
724 ALOGE("Accept failed: %s\n", strerror(errno));
725 continue;
726 }
727 fcntl(s, F_SETFD, FD_CLOEXEC);
728
729 ALOGI("new connection\n");
730 for (;;) {
731 unsigned short count;
732 if (readx(s, &count, sizeof(count))) {
733 ALOGE("failed to read size\n");
734 break;
735 }
736 if ((count < 1) || (count >= BUFFER_MAX)) {
737 ALOGE("invalid size %d\n", count);
738 break;
739 }
740 if (readx(s, buf, count)) {
741 ALOGE("failed to read command\n");
742 break;
743 }
744 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400745 if (selinux_enabled && selinux_status_updated() > 0) {
746 selinux_android_seapp_context_reload();
747 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700748 if (execute(s, buf)) break;
749 }
750 ALOGI("closing connection\n");
751 close(s);
752 }
753
754 return 0;
755}