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