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