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