blob: 682aaffb805525b47a836e7e1758a20488a5ad9d [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{
Narayan Kamath1b400322014-04-11 13:17:00 +010041 /* apk_path, uid, is_public, pkgname, instruction_set */
Alex Light7365a102014-07-21 12:23:48 -070042 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 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
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
Alex Light7365a102014-07-21 12:23:48 -0700149static int do_patchoat(char **arg, char reply[REPLY_MAX]) {
150 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 1);
151}
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 },
Narayan Kamath1b400322014-04-11 13:17:00 +0100162 { "dexopt", 5, do_dexopt },
163 { "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 },
Narayan Kamath1b400322014-04-11 13:17:00 +0100170 { "getsize", 7, do_get_size },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700171 { "rmuserdata", 2, do_rm_user_data },
172 { "movefiles", 0, do_movefiles },
173 { "linklib", 3, do_linklib },
Robert Craig880d1a92013-07-29 09:18:09 -0400174 { "mkuserdata", 4, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100175 { "mkuserconfig", 1, do_mk_user_config },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700176 { "rmuser", 1, do_rm_user },
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100177 { "idmap", 3, do_idmap },
Robert Craigda30dc72014-03-27 10:21:12 -0400178 { "restorecondata", 3, do_restorecon_data },
Narayan Kamath1e57e4a2014-06-17 12:54:16 +0100179 { "prunedexcache", 1, do_prune_dex_cache },
Alex Light7365a102014-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.
331 android_system_dirs.count = 2;
332
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
339 // system
340 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
341 free_globals();
342 return -1;
343 }
344
345 // append "app/" to dirs[0]
346 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
347 android_system_dirs.dirs[0].path = system_app_path;
348 android_system_dirs.dirs[0].len = strlen(system_app_path);
349
350 // vendor
351 // TODO replace this with an environment variable (doesn't exist yet)
352 android_system_dirs.dirs[1].path = "/vendor/app/";
353 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
354
355 return 0;
356}
357
358int initialize_directories() {
359 int res = -1;
360
361 // Read current filesystem layout version to handle upgrade paths
362 char version_path[PATH_MAX];
363 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
364
365 int oldVersion;
366 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
367 oldVersion = 0;
368 }
369 int version = oldVersion;
370
371 // /data/user
372 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
373 // /data/data
374 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
375 // /data/user/0
376 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
377 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
378 goto fail;
379 }
380
381 // Make the /data/user directory if necessary
382 if (access(user_data_dir, R_OK) < 0) {
383 if (mkdir(user_data_dir, 0711) < 0) {
384 goto fail;
385 }
386 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
387 goto fail;
388 }
389 if (chmod(user_data_dir, 0711) < 0) {
390 goto fail;
391 }
392 }
393 // Make the /data/user/0 symlink to /data/data if necessary
394 if (access(primary_data_dir, R_OK) < 0) {
395 if (symlink(legacy_data_dir, primary_data_dir)) {
396 goto fail;
397 }
398 }
399
400 if (version == 0) {
401 // Introducing multi-user, so migrate /data/media contents into /data/media/0
402 ALOGD("Upgrading /data/media for multi-user");
403
404 // Ensure /data/media
405 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
406 goto fail;
407 }
408
409 // /data/media.tmp
410 char media_tmp_dir[PATH_MAX];
411 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
412
413 // Only copy when upgrade not already in progress
414 if (access(media_tmp_dir, F_OK) == -1) {
415 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
416 ALOGE("Failed to move legacy media path: %s", strerror(errno));
417 goto fail;
418 }
419 }
420
421 // Create /data/media again
422 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
423 goto fail;
424 }
425
Stephen Smalley26288202014-02-07 09:16:46 -0500426 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500427 goto fail;
428 }
429
Mike Lockwood94afecf2012-10-24 10:45:23 -0700430 // /data/media/0
431 char owner_media_dir[PATH_MAX];
432 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
433
434 // Move any owner data into place
435 if (access(media_tmp_dir, F_OK) == 0) {
436 if (rename(media_tmp_dir, owner_media_dir) == -1) {
437 ALOGE("Failed to move owner media path: %s", strerror(errno));
438 goto fail;
439 }
440 }
441
442 // Ensure media directories for any existing users
443 DIR *dir;
444 struct dirent *dirent;
445 char user_media_dir[PATH_MAX];
446
447 dir = opendir(user_data_dir);
448 if (dir != NULL) {
449 while ((dirent = readdir(dir))) {
450 if (dirent->d_type == DT_DIR) {
451 const char *name = dirent->d_name;
452
453 // skip "." and ".."
454 if (name[0] == '.') {
455 if (name[1] == 0) continue;
456 if ((name[1] == '.') && (name[2] == 0)) continue;
457 }
458
459 // /data/media/<user_id>
460 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
461 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
462 goto fail;
463 }
464 }
465 }
466 closedir(dir);
467 }
468
469 version = 1;
470 }
471
472 // /data/media/obb
473 char media_obb_dir[PATH_MAX];
474 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
475
476 if (version == 1) {
477 // Introducing /data/media/obb for sharing OBB across users; migrate
478 // any existing OBB files from owner.
479 ALOGD("Upgrading to shared /data/media/obb");
480
481 // /data/media/0/Android/obb
482 char owner_obb_path[PATH_MAX];
483 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
484
485 // Only move if target doesn't already exist
486 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
487 if (rename(owner_obb_path, media_obb_dir) == -1) {
488 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
489 goto fail;
490 }
491 }
492
493 version = 2;
494 }
495
496 if (ensure_media_user_dirs(0) == -1) {
497 ALOGE("Failed to setup media for user 0");
498 goto fail;
499 }
500 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
501 goto fail;
502 }
503
Robin Lee07053fc2014-04-29 19:42:01 +0100504 if (ensure_config_user_dirs(0) == -1) {
505 ALOGE("Failed to setup misc for user 0");
506 goto fail;
507 }
508
Robin Lee095c7632014-04-25 15:05:19 +0100509 if (version == 2) {
510 ALOGD("Upgrading to /data/misc/user directories");
511
512 DIR *dir;
513 struct dirent *dirent;
514 char user_data_dir[PATH_MAX];
515
516 dir = opendir(user_data_dir);
517 if (dir != NULL) {
518 while ((dirent = readdir(dir))) {
519 if (dirent->d_type == DT_DIR) {
520 const char *name = dirent->d_name;
521
522 // skip "." and ".."
523 if (name[0] == '.') {
524 if (name[1] == 0) continue;
525 if ((name[1] == '.') && (name[2] == 0)) continue;
526 }
527
528 // /data/misc/user/<user_id>
529 if (ensure_config_user_dirs(atoi(name)) == -1) {
530 goto fail;
531 }
532 }
533 }
534 closedir(dir);
535 }
536
Robin Lee07053fc2014-04-29 19:42:01 +0100537 // Just rename keychain files into user/0; they should already have the right permissions
538 char misc_dir[PATH_MAX];
539 char keychain_added_dir[PATH_MAX];
540 char keychain_removed_dir[PATH_MAX];
541 char config_added_dir[PATH_MAX];
542 char config_removed_dir[PATH_MAX];
Robin Lee095c7632014-04-25 15:05:19 +0100543
Robin Lee07053fc2014-04-29 19:42:01 +0100544 snprintf(misc_dir, PATH_MAX, "%s/misc", android_data_dir.path);
545 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
546 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
547 snprintf(config_added_dir, PATH_MAX, "%s/user/0/cacerts-added", misc_dir);
548 snprintf(config_removed_dir, PATH_MAX, "%s/user/0/cacerts-removed", misc_dir);
549
550 if (access(keychain_added_dir, F_OK) == 0) {
551 if (rename(keychain_added_dir, config_added_dir) != 0) {
552 goto fail;
553 }
554 }
555 if (access(keychain_removed_dir, F_OK) == 0) {
556 if (rename(keychain_removed_dir, config_removed_dir) != 0) {
557 goto fail;
558 }
559 }
560
561 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100562 }
563
Mike Lockwood94afecf2012-10-24 10:45:23 -0700564 // Persist layout version if changed
565 if (version != oldVersion) {
566 if (fs_write_atomic_int(version_path, version) == -1) {
567 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
568 goto fail;
569 }
570 }
571
572 // Success!
573 res = 0;
574
575fail:
576 free(user_data_dir);
577 free(legacy_data_dir);
578 free(primary_data_dir);
579 return res;
580}
581
582static void drop_privileges() {
583 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
584 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
585 exit(1);
586 }
587
588 if (setgid(AID_INSTALL) < 0) {
589 ALOGE("setgid() can't drop privileges; exiting.\n");
590 exit(1);
591 }
592
593 if (setuid(AID_INSTALL) < 0) {
594 ALOGE("setuid() can't drop privileges; exiting.\n");
595 exit(1);
596 }
597
598 struct __user_cap_header_struct capheader;
599 struct __user_cap_data_struct capdata[2];
600 memset(&capheader, 0, sizeof(capheader));
601 memset(&capdata, 0, sizeof(capdata));
602 capheader.version = _LINUX_CAPABILITY_VERSION_3;
603 capheader.pid = 0;
604
605 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
606 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
607 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
608 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100609 capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700610
611 capdata[0].effective = capdata[0].permitted;
612 capdata[1].effective = capdata[1].permitted;
613 capdata[0].inheritable = 0;
614 capdata[1].inheritable = 0;
615
616 if (capset(&capheader, &capdata[0]) < 0) {
617 ALOGE("capset failed: %s\n", strerror(errno));
618 exit(1);
619 }
620}
621
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400622static int log_callback(int type, const char *fmt, ...) {
623 va_list ap;
624 int priority;
625
626 switch (type) {
627 case SELINUX_WARNING:
628 priority = ANDROID_LOG_WARN;
629 break;
630 case SELINUX_INFO:
631 priority = ANDROID_LOG_INFO;
632 break;
633 default:
634 priority = ANDROID_LOG_ERROR;
635 break;
636 }
637 va_start(ap, fmt);
638 LOG_PRI_VA(priority, "SELinux", fmt, ap);
639 va_end(ap);
640 return 0;
641}
642
Mike Lockwood94afecf2012-10-24 10:45:23 -0700643int main(const int argc, const char *argv[]) {
644 char buf[BUFFER_MAX];
645 struct sockaddr addr;
646 socklen_t alen;
647 int lsocket, s, count;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400648 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700649
650 ALOGI("installd firing up\n");
651
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400652 union selinux_callback cb;
653 cb.func_log = log_callback;
654 selinux_set_callback(SELINUX_CB_LOG, cb);
655
Mike Lockwood94afecf2012-10-24 10:45:23 -0700656 if (initialize_globals() < 0) {
657 ALOGE("Could not initialize globals; exiting.\n");
658 exit(1);
659 }
660
661 if (initialize_directories() < 0) {
662 ALOGE("Could not create directories; exiting.\n");
663 exit(1);
664 }
665
Stephen Smalleybd558d62013-04-16 12:16:50 -0400666 if (selinux_enabled && selinux_status_open(true) < 0) {
667 ALOGE("Could not open selinux status; exiting.\n");
668 exit(1);
669 }
670
Mike Lockwood94afecf2012-10-24 10:45:23 -0700671 drop_privileges();
672
673 lsocket = android_get_control_socket(SOCKET_PATH);
674 if (lsocket < 0) {
675 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
676 exit(1);
677 }
678 if (listen(lsocket, 5)) {
679 ALOGE("Listen on socket failed: %s\n", strerror(errno));
680 exit(1);
681 }
682 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
683
684 for (;;) {
685 alen = sizeof(addr);
686 s = accept(lsocket, &addr, &alen);
687 if (s < 0) {
688 ALOGE("Accept failed: %s\n", strerror(errno));
689 continue;
690 }
691 fcntl(s, F_SETFD, FD_CLOEXEC);
692
693 ALOGI("new connection\n");
694 for (;;) {
695 unsigned short count;
696 if (readx(s, &count, sizeof(count))) {
697 ALOGE("failed to read size\n");
698 break;
699 }
700 if ((count < 1) || (count >= BUFFER_MAX)) {
701 ALOGE("invalid size %d\n", count);
702 break;
703 }
704 if (readx(s, buf, count)) {
705 ALOGE("failed to read command\n");
706 break;
707 }
708 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400709 if (selinux_enabled && selinux_status_updated() > 0) {
710 selinux_android_seapp_context_reload();
711 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700712 if (execute(s, buf)) break;
713 }
714 ALOGI("closing connection\n");
715 close(s);
716 }
717
718 return 0;
719}