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