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