blob: 9c66f2d722365e3f39a7d43d13977488e5d8110e [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
Mårten Kongstad63568b12014-01-31 14:42:59 +0100127static int do_idmap(char **arg, char reply[REPLY_MAX])
128{
129 return idmap(arg[0], arg[1], atoi(arg[2]));
130}
131
Mike Lockwood94afecf2012-10-24 10:45:23 -0700132struct cmdinfo {
133 const char *name;
134 unsigned numargs;
135 int (*func)(char **arg, char reply[REPLY_MAX]);
136};
137
138struct cmdinfo cmds[] = {
139 { "ping", 0, do_ping },
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400140 { "install", 4, do_install },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700141 { "dexopt", 3, do_dexopt },
142 { "movedex", 2, do_move_dex },
143 { "rmdex", 1, do_rm_dex },
144 { "remove", 2, do_remove },
145 { "rename", 2, do_rename },
146 { "fixuid", 3, do_fixuid },
147 { "freecache", 1, do_free_cache },
148 { "rmcache", 2, do_rm_cache },
Dianne Hackborn8b417802013-05-01 18:55:10 -0700149 { "getsize", 6, do_get_size },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150 { "rmuserdata", 2, do_rm_user_data },
151 { "movefiles", 0, do_movefiles },
152 { "linklib", 3, do_linklib },
Robert Craig880d1a92013-07-29 09:18:09 -0400153 { "mkuserdata", 4, do_mk_user_data },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700154 { "rmuser", 1, do_rm_user },
Mårten Kongstad63568b12014-01-31 14:42:59 +0100155 { "idmap", 3, do_idmap },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156};
157
158static int readx(int s, void *_buf, int count)
159{
160 char *buf = _buf;
161 int n = 0, r;
162 if (count < 0) return -1;
163 while (n < count) {
164 r = read(s, buf + n, count - n);
165 if (r < 0) {
166 if (errno == EINTR) continue;
167 ALOGE("read error: %s\n", strerror(errno));
168 return -1;
169 }
170 if (r == 0) {
171 ALOGE("eof\n");
172 return -1; /* EOF */
173 }
174 n += r;
175 }
176 return 0;
177}
178
179static int writex(int s, const void *_buf, int count)
180{
181 const char *buf = _buf;
182 int n = 0, r;
183 if (count < 0) return -1;
184 while (n < count) {
185 r = write(s, buf + n, count - n);
186 if (r < 0) {
187 if (errno == EINTR) continue;
188 ALOGE("write error: %s\n", strerror(errno));
189 return -1;
190 }
191 n += r;
192 }
193 return 0;
194}
195
196
197/* Tokenize the command buffer, locate a matching command,
198 * ensure that the required number of arguments are provided,
199 * call the function(), return the result.
200 */
201static int execute(int s, char cmd[BUFFER_MAX])
202{
203 char reply[REPLY_MAX];
204 char *arg[TOKEN_MAX+1];
205 unsigned i;
206 unsigned n = 0;
207 unsigned short count;
208 int ret = -1;
209
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700210 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700211
212 /* default reply is "" */
213 reply[0] = 0;
214
215 /* n is number of args (not counting arg[0]) */
216 arg[0] = cmd;
217 while (*cmd) {
218 if (isspace(*cmd)) {
219 *cmd++ = 0;
220 n++;
221 arg[n] = cmd;
222 if (n == TOKEN_MAX) {
223 ALOGE("too many arguments\n");
224 goto done;
225 }
226 }
227 cmd++;
228 }
229
230 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
231 if (!strcmp(cmds[i].name,arg[0])) {
232 if (n != cmds[i].numargs) {
233 ALOGE("%s requires %d arguments (%d given)\n",
234 cmds[i].name, cmds[i].numargs, n);
235 } else {
236 ret = cmds[i].func(arg + 1, reply);
237 }
238 goto done;
239 }
240 }
241 ALOGE("unsupported command '%s'\n", arg[0]);
242
243done:
244 if (reply[0]) {
245 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
246 } else {
247 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
248 }
249 if (n > BUFFER_MAX) n = BUFFER_MAX;
250 count = n;
251
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700252 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700253 if (writex(s, &count, sizeof(count))) return -1;
254 if (writex(s, cmd, count)) return -1;
255 return 0;
256}
257
258/**
259 * Initialize all the global variables that are used elsewhere. Returns 0 upon
260 * success and -1 on error.
261 */
262void free_globals() {
263 size_t i;
264
265 for (i = 0; i < android_system_dirs.count; i++) {
266 if (android_system_dirs.dirs[i].path != NULL) {
267 free(android_system_dirs.dirs[i].path);
268 }
269 }
270
271 free(android_system_dirs.dirs);
272}
273
274int initialize_globals() {
275 // Get the android data directory.
276 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
277 return -1;
278 }
279
280 // Get the android app directory.
281 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
282 return -1;
283 }
284
285 // Get the android protected app directory.
286 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
287 return -1;
288 }
289
290 // Get the android app native library directory.
291 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
292 return -1;
293 }
294
295 // Get the sd-card ASEC mount point.
296 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
297 return -1;
298 }
299
300 // Get the android media directory.
301 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
302 return -1;
303 }
304
305 // Take note of the system and vendor directories.
306 android_system_dirs.count = 2;
307
308 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
309 if (android_system_dirs.dirs == NULL) {
310 ALOGE("Couldn't allocate array for dirs; aborting\n");
311 return -1;
312 }
313
314 // system
315 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
316 free_globals();
317 return -1;
318 }
319
320 // append "app/" to dirs[0]
321 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
322 android_system_dirs.dirs[0].path = system_app_path;
323 android_system_dirs.dirs[0].len = strlen(system_app_path);
324
325 // vendor
326 // TODO replace this with an environment variable (doesn't exist yet)
327 android_system_dirs.dirs[1].path = "/vendor/app/";
328 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
329
330 return 0;
331}
332
333int initialize_directories() {
334 int res = -1;
335
336 // Read current filesystem layout version to handle upgrade paths
337 char version_path[PATH_MAX];
338 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
339
340 int oldVersion;
341 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
342 oldVersion = 0;
343 }
344 int version = oldVersion;
345
346 // /data/user
347 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
348 // /data/data
349 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
350 // /data/user/0
351 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
352 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
353 goto fail;
354 }
355
356 // Make the /data/user directory if necessary
357 if (access(user_data_dir, R_OK) < 0) {
358 if (mkdir(user_data_dir, 0711) < 0) {
359 goto fail;
360 }
361 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
362 goto fail;
363 }
364 if (chmod(user_data_dir, 0711) < 0) {
365 goto fail;
366 }
367 }
368 // Make the /data/user/0 symlink to /data/data if necessary
369 if (access(primary_data_dir, R_OK) < 0) {
370 if (symlink(legacy_data_dir, primary_data_dir)) {
371 goto fail;
372 }
373 }
374
375 if (version == 0) {
376 // Introducing multi-user, so migrate /data/media contents into /data/media/0
377 ALOGD("Upgrading /data/media for multi-user");
378
379 // Ensure /data/media
380 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
381 goto fail;
382 }
383
384 // /data/media.tmp
385 char media_tmp_dir[PATH_MAX];
386 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
387
388 // Only copy when upgrade not already in progress
389 if (access(media_tmp_dir, F_OK) == -1) {
390 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
391 ALOGE("Failed to move legacy media path: %s", strerror(errno));
392 goto fail;
393 }
394 }
395
396 // Create /data/media again
397 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
398 goto fail;
399 }
400
Stephen Smalley47a35182013-12-17 16:04:20 -0500401 if (selinux_android_restorecon(android_media_dir.path)) {
402 goto fail;
403 }
404
Mike Lockwood94afecf2012-10-24 10:45:23 -0700405 // /data/media/0
406 char owner_media_dir[PATH_MAX];
407 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
408
409 // Move any owner data into place
410 if (access(media_tmp_dir, F_OK) == 0) {
411 if (rename(media_tmp_dir, owner_media_dir) == -1) {
412 ALOGE("Failed to move owner media path: %s", strerror(errno));
413 goto fail;
414 }
415 }
416
417 // Ensure media directories for any existing users
418 DIR *dir;
419 struct dirent *dirent;
420 char user_media_dir[PATH_MAX];
421
422 dir = opendir(user_data_dir);
423 if (dir != NULL) {
424 while ((dirent = readdir(dir))) {
425 if (dirent->d_type == DT_DIR) {
426 const char *name = dirent->d_name;
427
428 // skip "." and ".."
429 if (name[0] == '.') {
430 if (name[1] == 0) continue;
431 if ((name[1] == '.') && (name[2] == 0)) continue;
432 }
433
434 // /data/media/<user_id>
435 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
436 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
437 goto fail;
438 }
439 }
440 }
441 closedir(dir);
442 }
443
444 version = 1;
445 }
446
447 // /data/media/obb
448 char media_obb_dir[PATH_MAX];
449 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
450
451 if (version == 1) {
452 // Introducing /data/media/obb for sharing OBB across users; migrate
453 // any existing OBB files from owner.
454 ALOGD("Upgrading to shared /data/media/obb");
455
456 // /data/media/0/Android/obb
457 char owner_obb_path[PATH_MAX];
458 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
459
460 // Only move if target doesn't already exist
461 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
462 if (rename(owner_obb_path, media_obb_dir) == -1) {
463 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
464 goto fail;
465 }
466 }
467
468 version = 2;
469 }
470
471 if (ensure_media_user_dirs(0) == -1) {
472 ALOGE("Failed to setup media for user 0");
473 goto fail;
474 }
475 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
476 goto fail;
477 }
478
479 // Persist layout version if changed
480 if (version != oldVersion) {
481 if (fs_write_atomic_int(version_path, version) == -1) {
482 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
483 goto fail;
484 }
485 }
486
487 // Success!
488 res = 0;
489
490fail:
491 free(user_data_dir);
492 free(legacy_data_dir);
493 free(primary_data_dir);
494 return res;
495}
496
497static void drop_privileges() {
498 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
499 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
500 exit(1);
501 }
502
503 if (setgid(AID_INSTALL) < 0) {
504 ALOGE("setgid() can't drop privileges; exiting.\n");
505 exit(1);
506 }
507
508 if (setuid(AID_INSTALL) < 0) {
509 ALOGE("setuid() can't drop privileges; exiting.\n");
510 exit(1);
511 }
512
513 struct __user_cap_header_struct capheader;
514 struct __user_cap_data_struct capdata[2];
515 memset(&capheader, 0, sizeof(capheader));
516 memset(&capdata, 0, sizeof(capdata));
517 capheader.version = _LINUX_CAPABILITY_VERSION_3;
518 capheader.pid = 0;
519
520 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
521 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
522 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
523 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
Mårten Kongstad63568b12014-01-31 14:42:59 +0100524 capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700525
526 capdata[0].effective = capdata[0].permitted;
527 capdata[1].effective = capdata[1].permitted;
528 capdata[0].inheritable = 0;
529 capdata[1].inheritable = 0;
530
531 if (capset(&capheader, &capdata[0]) < 0) {
532 ALOGE("capset failed: %s\n", strerror(errno));
533 exit(1);
534 }
535}
536
537int main(const int argc, const char *argv[]) {
538 char buf[BUFFER_MAX];
539 struct sockaddr addr;
540 socklen_t alen;
541 int lsocket, s, count;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400542 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700543
544 ALOGI("installd firing up\n");
545
546 if (initialize_globals() < 0) {
547 ALOGE("Could not initialize globals; exiting.\n");
548 exit(1);
549 }
550
551 if (initialize_directories() < 0) {
552 ALOGE("Could not create directories; exiting.\n");
553 exit(1);
554 }
555
Stephen Smalleybd558d62013-04-16 12:16:50 -0400556 if (selinux_enabled && selinux_status_open(true) < 0) {
557 ALOGE("Could not open selinux status; exiting.\n");
558 exit(1);
559 }
560
Mike Lockwood94afecf2012-10-24 10:45:23 -0700561 drop_privileges();
562
563 lsocket = android_get_control_socket(SOCKET_PATH);
564 if (lsocket < 0) {
565 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
566 exit(1);
567 }
568 if (listen(lsocket, 5)) {
569 ALOGE("Listen on socket failed: %s\n", strerror(errno));
570 exit(1);
571 }
572 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
573
574 for (;;) {
575 alen = sizeof(addr);
576 s = accept(lsocket, &addr, &alen);
577 if (s < 0) {
578 ALOGE("Accept failed: %s\n", strerror(errno));
579 continue;
580 }
581 fcntl(s, F_SETFD, FD_CLOEXEC);
582
583 ALOGI("new connection\n");
584 for (;;) {
585 unsigned short count;
586 if (readx(s, &count, sizeof(count))) {
587 ALOGE("failed to read size\n");
588 break;
589 }
590 if ((count < 1) || (count >= BUFFER_MAX)) {
591 ALOGE("invalid size %d\n", count);
592 break;
593 }
594 if (readx(s, buf, count)) {
595 ALOGE("failed to read command\n");
596 break;
597 }
598 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400599 if (selinux_enabled && selinux_status_updated() > 0) {
600 selinux_android_seapp_context_reload();
601 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700602 if (execute(s, buf)) break;
603 }
604 ALOGI("closing connection\n");
605 close(s);
606 }
607
608 return 0;
609}