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