blob: 933338f90855fa1bbe67114a5368acfa02d6d4fd [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
29static int do_ping(char **arg, char reply[REPLY_MAX])
30{
31 return 0;
32}
33
34static int do_install(char **arg, char reply[REPLY_MAX])
35{
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
39static int do_dexopt(char **arg, char reply[REPLY_MAX])
40{
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
45static int do_move_dex(char **arg, char reply[REPLY_MAX])
46{
Narayan Kamath1b400322014-04-11 13:17:00 +010047 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070048}
49
50static int do_rm_dex(char **arg, char reply[REPLY_MAX])
51{
Narayan Kamath1b400322014-04-11 13:17:00 +010052 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070053}
54
55static int do_remove(char **arg, char reply[REPLY_MAX])
56{
57 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
58}
59
60static int do_rename(char **arg, char reply[REPLY_MAX])
61{
62 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
63}
64
65static 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
70static 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
75static 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 Sharkeyc796b682014-07-15 21:49:51 -070080static 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 Lockwood94afecf2012-10-24 10:45:23 -070085static 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 Sharkeyabe4fe52013-07-10 16:55:46 -070093 /* pkgdir, userid, apkpath */
Dianne Hackborn8b417802013-05-01 18:55:10 -070094 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5],
Narayan Kamath1b400322014-04-11 13:17:00 +010095 arg[6], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -070096
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
106static 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
111static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
112{
Robert Craig880d1a92013-07-29 09:18:09 -0400113 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]);
114 /* pkgname, uid, userid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700115}
116
Robin Lee7c8bec02014-06-10 18:46:26 +0100117static int do_mk_user_config(char **arg, char reply[REPLY_MAX])
Robin Lee095c7632014-04-25 15:05:19 +0100118{
Robin Lee7c8bec02014-06-10 18:46:26 +0100119 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100120}
121
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122static int do_rm_user(char **arg, char reply[REPLY_MAX])
123{
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700124 return delete_user(atoi(arg[0])); /* userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700125}
126
Mike Lockwood94afecf2012-10-24 10:45:23 -0700127static int do_movefiles(char **arg, char reply[REPLY_MAX])
128{
129 return movefiles();
130}
131
132static int do_linklib(char **arg, char reply[REPLY_MAX])
133{
134 return linklib(arg[0], arg[1], atoi(arg[2]));
135}
136
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100137static int do_idmap(char **arg, char reply[REPLY_MAX])
138{
139 return idmap(arg[0], arg[1], atoi(arg[2]));
140}
141
Robert Craigda30dc72014-03-27 10:21:12 -0400142static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
Robert Craige9887e42014-02-20 10:25:56 -0500143{
Robert Craigda30dc72014-03-27 10:21:12 -0400144 return restorecon_data(arg[0], arg[1], atoi(arg[2]));
145 /* pkgName, seinfo, uid*/
Robert Craige9887e42014-02-20 10:25:56 -0500146}
147
Alex Light43c5d302014-07-21 12:23:48 -0700148static int do_patchoat(char **arg, char reply[REPLY_MAX]) {
Calin Juravleb1efac12014-08-21 19:05:20 +0100149 /* 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 Light43c5d302014-07-21 12:23:48 -0700151}
152
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153struct cmdinfo {
154 const char *name;
155 unsigned numargs;
156 int (*func)(char **arg, char reply[REPLY_MAX]);
157};
158
159struct cmdinfo cmds[] = {
160 { "ping", 0, do_ping },
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400161 { "install", 4, do_install },
Calin Juravleb1efac12014-08-21 19:05:20 +0100162 { "dexopt", 6, do_dexopt },
Narayan Kamath1b400322014-04-11 13:17:00 +0100163 { "movedex", 3, do_move_dex },
164 { "rmdex", 2, do_rm_dex },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700165 { "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 Sharkeyc796b682014-07-15 21:49:51 -0700170 { "rmcodecache", 2, do_rm_code_cache },
Narayan Kamath1b400322014-04-11 13:17:00 +0100171 { "getsize", 7, do_get_size },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700172 { "rmuserdata", 2, do_rm_user_data },
173 { "movefiles", 0, do_movefiles },
174 { "linklib", 3, do_linklib },
Robert Craig880d1a92013-07-29 09:18:09 -0400175 { "mkuserdata", 4, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100176 { "mkuserconfig", 1, do_mk_user_config },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700177 { "rmuser", 1, do_rm_user },
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100178 { "idmap", 3, do_idmap },
Robert Craigda30dc72014-03-27 10:21:12 -0400179 { "restorecondata", 3, do_restorecon_data },
Alex Light43c5d302014-07-21 12:23:48 -0700180 { "patchoat", 5, do_patchoat },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181};
182
183static 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
204static 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 */
226static 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 Carlstrom1705fc42013-03-21 18:20:22 -0700235 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700236
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
268done:
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 Carlstrom1705fc42013-03-21 18:20:22 -0700277 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700278 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 */
287void 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
299int 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 Sharkey770180a2014-09-08 17:14:26 -0700331 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700332
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 Sharkey770180a2014-09-08 17:14:26 -0700339 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 Lockwood94afecf2012-10-24 10:45:23 -0700342 return -1;
343 }
344
Jeff Sharkey770180a2014-09-08 17:14:26 -0700345 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 Lockwood94afecf2012-10-24 10:45:23 -0700347
Jeff Sharkey770180a2014-09-08 17:14:26 -0700348 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700349 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
350
Jeff Sharkey770180a2014-09-08 17:14:26 -0700351 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 Lockwood94afecf2012-10-24 10:45:23 -0700357 return 0;
358}
359
360int 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 Smalley26288202014-02-07 09:16:46 -0500428 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500429 goto fail;
430 }
431
Mike Lockwood94afecf2012-10-24 10:45:23 -0700432 // /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 Lee07053fc2014-04-29 19:42:01 +0100506 if (ensure_config_user_dirs(0) == -1) {
507 ALOGE("Failed to setup misc for user 0");
508 goto fail;
509 }
510
Robin Lee095c7632014-04-25 15:05:19 +0100511 if (version == 2) {
512 ALOGD("Upgrading to /data/misc/user directories");
513
Robin Lee60fd3fe2014-10-07 16:55:02 +0100514 char misc_dir[PATH_MAX];
515 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
516
517 char keychain_added_dir[PATH_MAX];
518 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
519
520 char keychain_removed_dir[PATH_MAX];
521 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
522
Robin Lee095c7632014-04-25 15:05:19 +0100523 DIR *dir;
524 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100525 dir = opendir(user_data_dir);
526 if (dir != NULL) {
527 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100528 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100529
Robin Lee60fd3fe2014-10-07 16:55:02 +0100530 // skip "." and ".."
531 if (name[0] == '.') {
532 if (name[1] == 0) continue;
533 if ((name[1] == '.') && (name[2] == 0)) continue;
534 }
535
536 uint32_t user_id = atoi(name);
537
538 // /data/misc/user/<user_id>
539 if (ensure_config_user_dirs(user_id) == -1) {
540 goto fail;
541 }
542
543 char misc_added_dir[PATH_MAX];
544 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
545
546 char misc_removed_dir[PATH_MAX];
547 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
548
549 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
550 gid_t gid = uid;
551 if (access(keychain_added_dir, F_OK) == 0) {
552 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
553 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100554 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100555 }
556 if (access(keychain_removed_dir, F_OK) == 0) {
557 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
558 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100559 }
560 }
561 }
562 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100563
Robin Lee60fd3fe2014-10-07 16:55:02 +0100564 if (access(keychain_added_dir, F_OK) == 0) {
565 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100566 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100567 if (access(keychain_removed_dir, F_OK) == 0) {
568 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100569 }
570 }
571
572 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100573 }
574
Mike Lockwood94afecf2012-10-24 10:45:23 -0700575 // Persist layout version if changed
576 if (version != oldVersion) {
577 if (fs_write_atomic_int(version_path, version) == -1) {
578 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
579 goto fail;
580 }
581 }
582
583 // Success!
584 res = 0;
585
586fail:
587 free(user_data_dir);
588 free(legacy_data_dir);
589 free(primary_data_dir);
590 return res;
591}
592
593static void drop_privileges() {
594 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
595 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
596 exit(1);
597 }
598
599 if (setgid(AID_INSTALL) < 0) {
600 ALOGE("setgid() can't drop privileges; exiting.\n");
601 exit(1);
602 }
603
604 if (setuid(AID_INSTALL) < 0) {
605 ALOGE("setuid() can't drop privileges; exiting.\n");
606 exit(1);
607 }
608
609 struct __user_cap_header_struct capheader;
610 struct __user_cap_data_struct capdata[2];
611 memset(&capheader, 0, sizeof(capheader));
612 memset(&capdata, 0, sizeof(capdata));
613 capheader.version = _LINUX_CAPABILITY_VERSION_3;
614 capheader.pid = 0;
615
616 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
617 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
618 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
619 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100620 capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700621
622 capdata[0].effective = capdata[0].permitted;
623 capdata[1].effective = capdata[1].permitted;
624 capdata[0].inheritable = 0;
625 capdata[1].inheritable = 0;
626
627 if (capset(&capheader, &capdata[0]) < 0) {
628 ALOGE("capset failed: %s\n", strerror(errno));
629 exit(1);
630 }
631}
632
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400633static int log_callback(int type, const char *fmt, ...) {
634 va_list ap;
635 int priority;
636
637 switch (type) {
638 case SELINUX_WARNING:
639 priority = ANDROID_LOG_WARN;
640 break;
641 case SELINUX_INFO:
642 priority = ANDROID_LOG_INFO;
643 break;
644 default:
645 priority = ANDROID_LOG_ERROR;
646 break;
647 }
648 va_start(ap, fmt);
649 LOG_PRI_VA(priority, "SELinux", fmt, ap);
650 va_end(ap);
651 return 0;
652}
653
Mike Lockwood94afecf2012-10-24 10:45:23 -0700654int main(const int argc, const char *argv[]) {
655 char buf[BUFFER_MAX];
656 struct sockaddr addr;
657 socklen_t alen;
658 int lsocket, s, count;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400659 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700660
661 ALOGI("installd firing up\n");
662
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400663 union selinux_callback cb;
664 cb.func_log = log_callback;
665 selinux_set_callback(SELINUX_CB_LOG, cb);
666
Mike Lockwood94afecf2012-10-24 10:45:23 -0700667 if (initialize_globals() < 0) {
668 ALOGE("Could not initialize globals; exiting.\n");
669 exit(1);
670 }
671
672 if (initialize_directories() < 0) {
673 ALOGE("Could not create directories; exiting.\n");
674 exit(1);
675 }
676
Stephen Smalleybd558d62013-04-16 12:16:50 -0400677 if (selinux_enabled && selinux_status_open(true) < 0) {
678 ALOGE("Could not open selinux status; exiting.\n");
679 exit(1);
680 }
681
Mike Lockwood94afecf2012-10-24 10:45:23 -0700682 drop_privileges();
683
684 lsocket = android_get_control_socket(SOCKET_PATH);
685 if (lsocket < 0) {
686 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
687 exit(1);
688 }
689 if (listen(lsocket, 5)) {
690 ALOGE("Listen on socket failed: %s\n", strerror(errno));
691 exit(1);
692 }
693 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
694
695 for (;;) {
696 alen = sizeof(addr);
697 s = accept(lsocket, &addr, &alen);
698 if (s < 0) {
699 ALOGE("Accept failed: %s\n", strerror(errno));
700 continue;
701 }
702 fcntl(s, F_SETFD, FD_CLOEXEC);
703
704 ALOGI("new connection\n");
705 for (;;) {
706 unsigned short count;
707 if (readx(s, &count, sizeof(count))) {
708 ALOGE("failed to read size\n");
709 break;
710 }
711 if ((count < 1) || (count >= BUFFER_MAX)) {
712 ALOGE("invalid size %d\n", count);
713 break;
714 }
715 if (readx(s, buf, count)) {
716 ALOGE("failed to read command\n");
717 break;
718 }
719 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400720 if (selinux_enabled && selinux_status_updated() > 0) {
721 selinux_android_seapp_context_reload();
722 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700723 if (execute(s, buf)) break;
724 }
725 ALOGI("closing connection\n");
726 close(s);
727 }
728
729 return 0;
730}