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