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