blob: 03acafd407b2080c156ecbc608033ccb810767df [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>
Mike Lockwood94afecf2012-10-24 10:45:23 -070018#include <linux/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{
41 /* apk_path, uid, is_public */
42 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]));
43}
44
45static int do_move_dex(char **arg, char reply[REPLY_MAX])
46{
47 return move_dex(arg[0], arg[1]); /* src, dst */
48}
49
50static int do_rm_dex(char **arg, char reply[REPLY_MAX])
51{
52 return rm_dex(arg[0]); /* pkgname */
53}
54
55static int do_remove(char **arg, char reply[REPLY_MAX])
56{
57 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
58}
59
60static int do_rename(char **arg, char reply[REPLY_MAX])
61{
62 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
63}
64
65static int do_fixuid(char **arg, char reply[REPLY_MAX])
66{
67 return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
68}
69
70static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
71{
72 return free_cache((int64_t)atoll(arg[0])); /* free_size */
73}
74
75static int do_rm_cache(char **arg, char reply[REPLY_MAX])
76{
77 return delete_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
78}
79
80static int do_get_size(char **arg, char reply[REPLY_MAX])
81{
82 int64_t codesize = 0;
83 int64_t datasize = 0;
84 int64_t cachesize = 0;
85 int64_t asecsize = 0;
86 int res = 0;
87
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -070088 /* pkgdir, userid, apkpath */
Dianne Hackborn8b417802013-05-01 18:55:10 -070089 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5],
Mike Lockwood94afecf2012-10-24 10:45:23 -070090 &codesize, &datasize, &cachesize, &asecsize);
91
92 /*
93 * Each int64_t can take up 22 characters printed out. Make sure it
94 * doesn't go over REPLY_MAX in the future.
95 */
96 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
97 codesize, datasize, cachesize, asecsize);
98 return res;
99}
100
101static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
102{
103 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
104}
105
106static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
107{
Robert Craig880d1a92013-07-29 09:18:09 -0400108 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]);
109 /* pkgname, uid, userid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700110}
111
112static int do_rm_user(char **arg, char reply[REPLY_MAX])
113{
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700114 return delete_user(atoi(arg[0])); /* userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700115}
116
Mike Lockwood94afecf2012-10-24 10:45:23 -0700117static int do_movefiles(char **arg, char reply[REPLY_MAX])
118{
119 return movefiles();
120}
121
122static int do_linklib(char **arg, char reply[REPLY_MAX])
123{
124 return linklib(arg[0], arg[1], atoi(arg[2]));
125}
126
127struct cmdinfo {
128 const char *name;
129 unsigned numargs;
130 int (*func)(char **arg, char reply[REPLY_MAX]);
131};
132
133struct cmdinfo cmds[] = {
134 { "ping", 0, do_ping },
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400135 { "install", 4, do_install },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136 { "dexopt", 3, do_dexopt },
137 { "movedex", 2, do_move_dex },
138 { "rmdex", 1, do_rm_dex },
139 { "remove", 2, do_remove },
140 { "rename", 2, do_rename },
141 { "fixuid", 3, do_fixuid },
142 { "freecache", 1, do_free_cache },
143 { "rmcache", 2, do_rm_cache },
Dianne Hackborn8b417802013-05-01 18:55:10 -0700144 { "getsize", 6, do_get_size },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700145 { "rmuserdata", 2, do_rm_user_data },
146 { "movefiles", 0, do_movefiles },
147 { "linklib", 3, do_linklib },
Robert Craig880d1a92013-07-29 09:18:09 -0400148 { "mkuserdata", 4, do_mk_user_data },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700149 { "rmuser", 1, do_rm_user },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150};
151
152static int readx(int s, void *_buf, int count)
153{
154 char *buf = _buf;
155 int n = 0, r;
156 if (count < 0) return -1;
157 while (n < count) {
158 r = read(s, buf + n, count - n);
159 if (r < 0) {
160 if (errno == EINTR) continue;
161 ALOGE("read error: %s\n", strerror(errno));
162 return -1;
163 }
164 if (r == 0) {
165 ALOGE("eof\n");
166 return -1; /* EOF */
167 }
168 n += r;
169 }
170 return 0;
171}
172
173static int writex(int s, const void *_buf, int count)
174{
175 const char *buf = _buf;
176 int n = 0, r;
177 if (count < 0) return -1;
178 while (n < count) {
179 r = write(s, buf + n, count - n);
180 if (r < 0) {
181 if (errno == EINTR) continue;
182 ALOGE("write error: %s\n", strerror(errno));
183 return -1;
184 }
185 n += r;
186 }
187 return 0;
188}
189
190
191/* Tokenize the command buffer, locate a matching command,
192 * ensure that the required number of arguments are provided,
193 * call the function(), return the result.
194 */
195static int execute(int s, char cmd[BUFFER_MAX])
196{
197 char reply[REPLY_MAX];
198 char *arg[TOKEN_MAX+1];
199 unsigned i;
200 unsigned n = 0;
201 unsigned short count;
202 int ret = -1;
203
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700204 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700205
206 /* default reply is "" */
207 reply[0] = 0;
208
209 /* n is number of args (not counting arg[0]) */
210 arg[0] = cmd;
211 while (*cmd) {
212 if (isspace(*cmd)) {
213 *cmd++ = 0;
214 n++;
215 arg[n] = cmd;
216 if (n == TOKEN_MAX) {
217 ALOGE("too many arguments\n");
218 goto done;
219 }
220 }
221 cmd++;
222 }
223
224 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
225 if (!strcmp(cmds[i].name,arg[0])) {
226 if (n != cmds[i].numargs) {
227 ALOGE("%s requires %d arguments (%d given)\n",
228 cmds[i].name, cmds[i].numargs, n);
229 } else {
230 ret = cmds[i].func(arg + 1, reply);
231 }
232 goto done;
233 }
234 }
235 ALOGE("unsupported command '%s'\n", arg[0]);
236
237done:
238 if (reply[0]) {
239 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
240 } else {
241 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
242 }
243 if (n > BUFFER_MAX) n = BUFFER_MAX;
244 count = n;
245
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700246 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700247 if (writex(s, &count, sizeof(count))) return -1;
248 if (writex(s, cmd, count)) return -1;
249 return 0;
250}
251
252/**
253 * Initialize all the global variables that are used elsewhere. Returns 0 upon
254 * success and -1 on error.
255 */
256void free_globals() {
257 size_t i;
258
259 for (i = 0; i < android_system_dirs.count; i++) {
260 if (android_system_dirs.dirs[i].path != NULL) {
261 free(android_system_dirs.dirs[i].path);
262 }
263 }
264
265 free(android_system_dirs.dirs);
266}
267
268int initialize_globals() {
269 // Get the android data directory.
270 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
271 return -1;
272 }
273
274 // Get the android app directory.
275 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
276 return -1;
277 }
278
279 // Get the android protected app directory.
280 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
281 return -1;
282 }
283
284 // Get the android app native library directory.
285 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
286 return -1;
287 }
288
289 // Get the sd-card ASEC mount point.
290 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
291 return -1;
292 }
293
294 // Get the android media directory.
295 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
296 return -1;
297 }
298
299 // Take note of the system and vendor directories.
300 android_system_dirs.count = 2;
301
302 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
303 if (android_system_dirs.dirs == NULL) {
304 ALOGE("Couldn't allocate array for dirs; aborting\n");
305 return -1;
306 }
307
308 // system
309 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
310 free_globals();
311 return -1;
312 }
313
314 // append "app/" to dirs[0]
315 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
316 android_system_dirs.dirs[0].path = system_app_path;
317 android_system_dirs.dirs[0].len = strlen(system_app_path);
318
319 // vendor
320 // TODO replace this with an environment variable (doesn't exist yet)
321 android_system_dirs.dirs[1].path = "/vendor/app/";
322 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
323
324 return 0;
325}
326
327int initialize_directories() {
328 int res = -1;
329
330 // Read current filesystem layout version to handle upgrade paths
331 char version_path[PATH_MAX];
332 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
333
334 int oldVersion;
335 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
336 oldVersion = 0;
337 }
338 int version = oldVersion;
339
340 // /data/user
341 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
342 // /data/data
343 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
344 // /data/user/0
345 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
346 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
347 goto fail;
348 }
349
350 // Make the /data/user directory if necessary
351 if (access(user_data_dir, R_OK) < 0) {
352 if (mkdir(user_data_dir, 0711) < 0) {
353 goto fail;
354 }
355 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
356 goto fail;
357 }
358 if (chmod(user_data_dir, 0711) < 0) {
359 goto fail;
360 }
361 }
362 // Make the /data/user/0 symlink to /data/data if necessary
363 if (access(primary_data_dir, R_OK) < 0) {
364 if (symlink(legacy_data_dir, primary_data_dir)) {
365 goto fail;
366 }
367 }
368
369 if (version == 0) {
370 // Introducing multi-user, so migrate /data/media contents into /data/media/0
371 ALOGD("Upgrading /data/media for multi-user");
372
373 // Ensure /data/media
374 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
375 goto fail;
376 }
377
378 // /data/media.tmp
379 char media_tmp_dir[PATH_MAX];
380 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
381
382 // Only copy when upgrade not already in progress
383 if (access(media_tmp_dir, F_OK) == -1) {
384 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
385 ALOGE("Failed to move legacy media path: %s", strerror(errno));
386 goto fail;
387 }
388 }
389
390 // Create /data/media again
391 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
392 goto fail;
393 }
394
Stephen Smalley26288202014-02-07 09:16:46 -0500395 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500396 goto fail;
397 }
398
Mike Lockwood94afecf2012-10-24 10:45:23 -0700399 // /data/media/0
400 char owner_media_dir[PATH_MAX];
401 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
402
403 // Move any owner data into place
404 if (access(media_tmp_dir, F_OK) == 0) {
405 if (rename(media_tmp_dir, owner_media_dir) == -1) {
406 ALOGE("Failed to move owner media path: %s", strerror(errno));
407 goto fail;
408 }
409 }
410
411 // Ensure media directories for any existing users
412 DIR *dir;
413 struct dirent *dirent;
414 char user_media_dir[PATH_MAX];
415
416 dir = opendir(user_data_dir);
417 if (dir != NULL) {
418 while ((dirent = readdir(dir))) {
419 if (dirent->d_type == DT_DIR) {
420 const char *name = dirent->d_name;
421
422 // skip "." and ".."
423 if (name[0] == '.') {
424 if (name[1] == 0) continue;
425 if ((name[1] == '.') && (name[2] == 0)) continue;
426 }
427
428 // /data/media/<user_id>
429 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
430 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
431 goto fail;
432 }
433 }
434 }
435 closedir(dir);
436 }
437
438 version = 1;
439 }
440
441 // /data/media/obb
442 char media_obb_dir[PATH_MAX];
443 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
444
445 if (version == 1) {
446 // Introducing /data/media/obb for sharing OBB across users; migrate
447 // any existing OBB files from owner.
448 ALOGD("Upgrading to shared /data/media/obb");
449
450 // /data/media/0/Android/obb
451 char owner_obb_path[PATH_MAX];
452 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
453
454 // Only move if target doesn't already exist
455 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
456 if (rename(owner_obb_path, media_obb_dir) == -1) {
457 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
458 goto fail;
459 }
460 }
461
462 version = 2;
463 }
464
465 if (ensure_media_user_dirs(0) == -1) {
466 ALOGE("Failed to setup media for user 0");
467 goto fail;
468 }
469 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
470 goto fail;
471 }
472
473 // Persist layout version if changed
474 if (version != oldVersion) {
475 if (fs_write_atomic_int(version_path, version) == -1) {
476 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
477 goto fail;
478 }
479 }
480
481 // Success!
482 res = 0;
483
484fail:
485 free(user_data_dir);
486 free(legacy_data_dir);
487 free(primary_data_dir);
488 return res;
489}
490
491static void drop_privileges() {
492 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
493 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
494 exit(1);
495 }
496
497 if (setgid(AID_INSTALL) < 0) {
498 ALOGE("setgid() can't drop privileges; exiting.\n");
499 exit(1);
500 }
501
502 if (setuid(AID_INSTALL) < 0) {
503 ALOGE("setuid() can't drop privileges; exiting.\n");
504 exit(1);
505 }
506
507 struct __user_cap_header_struct capheader;
508 struct __user_cap_data_struct capdata[2];
509 memset(&capheader, 0, sizeof(capheader));
510 memset(&capdata, 0, sizeof(capdata));
511 capheader.version = _LINUX_CAPABILITY_VERSION_3;
512 capheader.pid = 0;
513
514 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
515 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
516 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
517 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
518
519 capdata[0].effective = capdata[0].permitted;
520 capdata[1].effective = capdata[1].permitted;
521 capdata[0].inheritable = 0;
522 capdata[1].inheritable = 0;
523
524 if (capset(&capheader, &capdata[0]) < 0) {
525 ALOGE("capset failed: %s\n", strerror(errno));
526 exit(1);
527 }
528}
529
530int main(const int argc, const char *argv[]) {
531 char buf[BUFFER_MAX];
532 struct sockaddr addr;
533 socklen_t alen;
534 int lsocket, s, count;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400535 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700536
537 ALOGI("installd firing up\n");
538
539 if (initialize_globals() < 0) {
540 ALOGE("Could not initialize globals; exiting.\n");
541 exit(1);
542 }
543
544 if (initialize_directories() < 0) {
545 ALOGE("Could not create directories; exiting.\n");
546 exit(1);
547 }
548
Stephen Smalleybd558d62013-04-16 12:16:50 -0400549 if (selinux_enabled && selinux_status_open(true) < 0) {
550 ALOGE("Could not open selinux status; exiting.\n");
551 exit(1);
552 }
553
Mike Lockwood94afecf2012-10-24 10:45:23 -0700554 drop_privileges();
555
556 lsocket = android_get_control_socket(SOCKET_PATH);
557 if (lsocket < 0) {
558 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
559 exit(1);
560 }
561 if (listen(lsocket, 5)) {
562 ALOGE("Listen on socket failed: %s\n", strerror(errno));
563 exit(1);
564 }
565 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
566
567 for (;;) {
568 alen = sizeof(addr);
569 s = accept(lsocket, &addr, &alen);
570 if (s < 0) {
571 ALOGE("Accept failed: %s\n", strerror(errno));
572 continue;
573 }
574 fcntl(s, F_SETFD, FD_CLOEXEC);
575
576 ALOGI("new connection\n");
577 for (;;) {
578 unsigned short count;
579 if (readx(s, &count, sizeof(count))) {
580 ALOGE("failed to read size\n");
581 break;
582 }
583 if ((count < 1) || (count >= BUFFER_MAX)) {
584 ALOGE("invalid size %d\n", count);
585 break;
586 }
587 if (readx(s, buf, count)) {
588 ALOGE("failed to read command\n");
589 break;
590 }
591 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400592 if (selinux_enabled && selinux_status_updated() > 0) {
593 selinux_android_seapp_context_reload();
594 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700595 if (execute(s, buf)) break;
596 }
597 ALOGI("closing connection\n");
598 close(s);
599 }
600
601 return 0;
602}