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