blob: 0f035d00b9469b7b09e6ecd07927e411d9c7518a [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Jeff Sharkey19803802015-04-07 12:44:51 -07004** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Jeff Sharkey19803802015-04-07 12:44:51 -07008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Jeff Sharkey19803802015-04-07 12:44:51 -070010** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Nick Kralevichd7471292013-02-28 16:59:13 -080017#include <sys/capability.h>
Elliott Hughes119b7652014-07-18 17:29:15 -070018#include <sys/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 */
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -080026#define TOKEN_MAX 16 /* max number of arguments in buffer */
Mike Lockwood94afecf2012-10-24 10:45:23 -070027#define REPLY_MAX 256 /* largest reply allowed */
28
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070029static char* parse_null(char* arg) {
30 if (strcmp(arg, "!") == 0) {
31 return nullptr;
32 } else {
33 return arg;
34 }
35}
36
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070037static int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070038{
39 return 0;
40}
41
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070042static int do_install(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070043{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070044 return install(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]); /* uuid, pkgname, uid, gid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -070045}
46
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070047static int do_dexopt(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070048{
Richard Uhlerc92fb622015-03-26 15:47:38 -070049 /* apk_path, uid, is_public, pkgname, instruction_set,
50 * dexopt_needed, vm_safe_mode, debuggable, oat_dir */
51 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]),
52 atoi(arg[6]), atoi(arg[7]), arg[8]);
Mike Lockwood94afecf2012-10-24 10:45:23 -070053}
54
Bernhard Rosenkränzer7fb390d2014-11-23 22:28:26 +010055static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused)
Narayan Kamath091ea772014-11-10 15:03:46 +000056{
57 return mark_boot_complete(arg[0] /* instruction set */);
58}
59
Bernhard Rosenkränzer7fb390d2014-11-23 22:28:26 +010060static int do_move_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070061{
Narayan Kamath1b400322014-04-11 13:17:00 +010062 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070063}
64
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070065static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070066{
Narayan Kamath1b400322014-04-11 13:17:00 +010067 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070068}
69
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070070static int do_remove(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070071{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070072 return uninstall(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -070073}
74
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070075static int do_rename(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070076{
77 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
78}
79
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070080static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070081{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070082 return fix_uid(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3])); /* uuid, pkgname, uid, gid */
Mike Lockwood94afecf2012-10-24 10:45:23 -070083}
84
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070085static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -070086{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070087 return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -070088}
89
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070090static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070091{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070092 return delete_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -070093}
94
Dan Albert9e8b5282014-09-12 09:00:50 -070095static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused)
Jeff Sharkeyc796b682014-07-15 21:49:51 -070096{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070097 return delete_code_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Jeff Sharkeyc796b682014-07-15 21:49:51 -070098}
99
Mike Lockwood94afecf2012-10-24 10:45:23 -0700100static int do_get_size(char **arg, char reply[REPLY_MAX])
101{
102 int64_t codesize = 0;
103 int64_t datasize = 0;
104 int64_t cachesize = 0;
105 int64_t asecsize = 0;
106 int res = 0;
107
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700108 /* uuid, pkgdir, userid, apkpath */
109 res = get_size(parse_null(arg[0]), arg[1], atoi(arg[2]), arg[3], arg[4], arg[5], arg[6],
110 arg[7], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700111
112 /*
113 * Each int64_t can take up 22 characters printed out. Make sure it
114 * doesn't go over REPLY_MAX in the future.
115 */
116 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
117 codesize, datasize, cachesize, asecsize);
118 return res;
119}
120
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700121static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700123 return delete_user_data(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700124}
125
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700126static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700127{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700128 return make_user_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]);
129 /* uuid, pkgname, uid, userid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700130}
131
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700132static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused)
Robin Lee095c7632014-04-25 15:05:19 +0100133{
Robin Lee7c8bec02014-06-10 18:46:26 +0100134 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100135}
136
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700137static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700139 return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700140}
141
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700142static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700143{
144 return movefiles();
145}
146
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700147static int do_linklib(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700149 return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150}
151
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700152static int do_idmap(char **arg, char reply[REPLY_MAX] __unused)
Mårten Kongstad63568b12014-01-31 14:42:59 +0100153{
154 return idmap(arg[0], arg[1], atoi(arg[2]));
155}
156
Robert Craigda30dc72014-03-27 10:21:12 -0400157static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
Robert Craige9887e42014-02-20 10:25:56 -0500158{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700159 return restorecon_data(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
160 /* uuid, pkgName, seinfo, uid*/
Robert Craige9887e42014-02-20 10:25:56 -0500161}
162
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800163static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused)
164{
165 /* oat_dir, instruction_set */
166 return create_oat_dir(arg[0], arg[1]);
167}
168
169static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused)
170{
171 /* oat_dir */
172 return rm_package_dir(arg[0]);
Alex Light43c5d302014-07-21 12:23:48 -0700173}
174
Mike Lockwood94afecf2012-10-24 10:45:23 -0700175struct cmdinfo {
176 const char *name;
177 unsigned numargs;
178 int (*func)(char **arg, char reply[REPLY_MAX]);
179};
180
181struct cmdinfo cmds[] = {
182 { "ping", 0, do_ping },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700183 { "install", 5, do_install },
Richard Uhlerc92fb622015-03-26 15:47:38 -0700184 { "dexopt", 9, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000185 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100186 { "movedex", 3, do_move_dex },
187 { "rmdex", 2, do_rm_dex },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700188 { "remove", 3, do_remove },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700189 { "rename", 2, do_rename },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700190 { "fixuid", 4, do_fixuid },
191 { "freecache", 2, do_free_cache },
192 { "rmcache", 3, do_rm_cache },
193 { "rmcodecache", 3, do_rm_code_cache },
194 { "getsize", 8, do_get_size },
195 { "rmuserdata", 3, do_rm_user_data },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700196 { "movefiles", 0, do_movefiles },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700197 { "linklib", 4, do_linklib },
198 { "mkuserdata", 5, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100199 { "mkuserconfig", 1, do_mk_user_config },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700200 { "rmuser", 2, do_rm_user },
Mårten Kongstad63568b12014-01-31 14:42:59 +0100201 { "idmap", 3, do_idmap },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700202 { "restorecondata", 4, do_restorecon_data },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800203 { "createoatdir", 2, do_create_oat_dir },
204 { "rmpackagedir", 1, do_rm_package_dir},
Mike Lockwood94afecf2012-10-24 10:45:23 -0700205};
206
207static int readx(int s, void *_buf, int count)
208{
Jeff Sharkey19803802015-04-07 12:44:51 -0700209 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 int n = 0, r;
211 if (count < 0) return -1;
212 while (n < count) {
213 r = read(s, buf + n, count - n);
214 if (r < 0) {
215 if (errno == EINTR) continue;
216 ALOGE("read error: %s\n", strerror(errno));
217 return -1;
218 }
219 if (r == 0) {
220 ALOGE("eof\n");
221 return -1; /* EOF */
222 }
223 n += r;
224 }
225 return 0;
226}
227
228static int writex(int s, const void *_buf, int count)
229{
Jeff Sharkey19803802015-04-07 12:44:51 -0700230 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700231 int n = 0, r;
232 if (count < 0) return -1;
233 while (n < count) {
234 r = write(s, buf + n, count - n);
235 if (r < 0) {
236 if (errno == EINTR) continue;
237 ALOGE("write error: %s\n", strerror(errno));
238 return -1;
239 }
240 n += r;
241 }
242 return 0;
243}
244
245
246/* Tokenize the command buffer, locate a matching command,
247 * ensure that the required number of arguments are provided,
248 * call the function(), return the result.
249 */
250static int execute(int s, char cmd[BUFFER_MAX])
251{
252 char reply[REPLY_MAX];
253 char *arg[TOKEN_MAX+1];
254 unsigned i;
255 unsigned n = 0;
256 unsigned short count;
257 int ret = -1;
258
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700259 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700260
261 /* default reply is "" */
262 reply[0] = 0;
263
264 /* n is number of args (not counting arg[0]) */
265 arg[0] = cmd;
266 while (*cmd) {
267 if (isspace(*cmd)) {
268 *cmd++ = 0;
269 n++;
270 arg[n] = cmd;
271 if (n == TOKEN_MAX) {
272 ALOGE("too many arguments\n");
273 goto done;
274 }
275 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600276 if (*cmd) {
277 cmd++;
278 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700279 }
280
281 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
282 if (!strcmp(cmds[i].name,arg[0])) {
283 if (n != cmds[i].numargs) {
284 ALOGE("%s requires %d arguments (%d given)\n",
285 cmds[i].name, cmds[i].numargs, n);
286 } else {
287 ret = cmds[i].func(arg + 1, reply);
288 }
289 goto done;
290 }
291 }
292 ALOGE("unsupported command '%s'\n", arg[0]);
293
294done:
295 if (reply[0]) {
296 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
297 } else {
298 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
299 }
300 if (n > BUFFER_MAX) n = BUFFER_MAX;
301 count = n;
302
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700303 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700304 if (writex(s, &count, sizeof(count))) return -1;
305 if (writex(s, cmd, count)) return -1;
306 return 0;
307}
308
309/**
310 * Initialize all the global variables that are used elsewhere. Returns 0 upon
311 * success and -1 on error.
312 */
313void free_globals() {
314 size_t i;
315
316 for (i = 0; i < android_system_dirs.count; i++) {
317 if (android_system_dirs.dirs[i].path != NULL) {
318 free(android_system_dirs.dirs[i].path);
319 }
320 }
321
322 free(android_system_dirs.dirs);
323}
324
325int initialize_globals() {
326 // Get the android data directory.
327 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
328 return -1;
329 }
330
331 // Get the android app directory.
332 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
333 return -1;
334 }
335
336 // Get the android protected app directory.
337 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
338 return -1;
339 }
340
341 // Get the android app native library directory.
342 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
343 return -1;
344 }
345
346 // Get the sd-card ASEC mount point.
347 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
348 return -1;
349 }
350
351 // Get the android media directory.
352 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
353 return -1;
354 }
355
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700356 // Get the android external app directory.
Jeff Sharkey19803802015-04-07 12:44:51 -0700357 if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700358 return -1;
359 }
360
Mike Lockwood94afecf2012-10-24 10:45:23 -0700361 // Take note of the system and vendor directories.
Jeff Sharkey770180a2014-09-08 17:14:26 -0700362 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700363
Jeff Sharkey19803802015-04-07 12:44:51 -0700364 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700365 if (android_system_dirs.dirs == NULL) {
366 ALOGE("Couldn't allocate array for dirs; aborting\n");
367 return -1;
368 }
369
Jeff Sharkey770180a2014-09-08 17:14:26 -0700370 dir_rec_t android_root_dir;
371 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
372 ALOGE("Missing ANDROID_ROOT; aborting\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700373 return -1;
374 }
375
Jeff Sharkey770180a2014-09-08 17:14:26 -0700376 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
377 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700378
Jeff Sharkey770180a2014-09-08 17:14:26 -0700379 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700380 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
381
Jeff Sharkey19803802015-04-07 12:44:51 -0700382 android_system_dirs.dirs[2].path = strdup("/vendor/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700383 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
384
Jeff Sharkey19803802015-04-07 12:44:51 -0700385 android_system_dirs.dirs[3].path = strdup("/oem/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700386 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
387
Mike Lockwood94afecf2012-10-24 10:45:23 -0700388 return 0;
389}
390
391int initialize_directories() {
392 int res = -1;
393
394 // Read current filesystem layout version to handle upgrade paths
395 char version_path[PATH_MAX];
396 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
397
398 int oldVersion;
399 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
400 oldVersion = 0;
401 }
402 int version = oldVersion;
403
404 // /data/user
405 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
406 // /data/data
407 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
408 // /data/user/0
409 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
410 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
411 goto fail;
412 }
413
414 // Make the /data/user directory if necessary
415 if (access(user_data_dir, R_OK) < 0) {
416 if (mkdir(user_data_dir, 0711) < 0) {
417 goto fail;
418 }
419 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
420 goto fail;
421 }
422 if (chmod(user_data_dir, 0711) < 0) {
423 goto fail;
424 }
425 }
426 // Make the /data/user/0 symlink to /data/data if necessary
427 if (access(primary_data_dir, R_OK) < 0) {
428 if (symlink(legacy_data_dir, primary_data_dir)) {
429 goto fail;
430 }
431 }
432
433 if (version == 0) {
434 // Introducing multi-user, so migrate /data/media contents into /data/media/0
435 ALOGD("Upgrading /data/media for multi-user");
436
437 // Ensure /data/media
438 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
439 goto fail;
440 }
441
442 // /data/media.tmp
443 char media_tmp_dir[PATH_MAX];
444 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
445
446 // Only copy when upgrade not already in progress
447 if (access(media_tmp_dir, F_OK) == -1) {
448 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
449 ALOGE("Failed to move legacy media path: %s", strerror(errno));
450 goto fail;
451 }
452 }
453
454 // Create /data/media again
455 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
456 goto fail;
457 }
458
Stephen Smalley26288202014-02-07 09:16:46 -0500459 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500460 goto fail;
461 }
462
Mike Lockwood94afecf2012-10-24 10:45:23 -0700463 // /data/media/0
464 char owner_media_dir[PATH_MAX];
465 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
466
467 // Move any owner data into place
468 if (access(media_tmp_dir, F_OK) == 0) {
469 if (rename(media_tmp_dir, owner_media_dir) == -1) {
470 ALOGE("Failed to move owner media path: %s", strerror(errno));
471 goto fail;
472 }
473 }
474
475 // Ensure media directories for any existing users
476 DIR *dir;
477 struct dirent *dirent;
478 char user_media_dir[PATH_MAX];
479
480 dir = opendir(user_data_dir);
481 if (dir != NULL) {
482 while ((dirent = readdir(dir))) {
483 if (dirent->d_type == DT_DIR) {
484 const char *name = dirent->d_name;
485
486 // skip "." and ".."
487 if (name[0] == '.') {
488 if (name[1] == 0) continue;
489 if ((name[1] == '.') && (name[2] == 0)) continue;
490 }
491
492 // /data/media/<user_id>
493 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
494 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
495 goto fail;
496 }
497 }
498 }
499 closedir(dir);
500 }
501
502 version = 1;
503 }
504
505 // /data/media/obb
506 char media_obb_dir[PATH_MAX];
507 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
508
509 if (version == 1) {
510 // Introducing /data/media/obb for sharing OBB across users; migrate
511 // any existing OBB files from owner.
512 ALOGD("Upgrading to shared /data/media/obb");
513
514 // /data/media/0/Android/obb
515 char owner_obb_path[PATH_MAX];
516 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
517
518 // Only move if target doesn't already exist
519 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
520 if (rename(owner_obb_path, media_obb_dir) == -1) {
521 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
522 goto fail;
523 }
524 }
525
526 version = 2;
527 }
528
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700529 if (ensure_media_user_dirs(nullptr, 0) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700530 ALOGE("Failed to setup media for user 0");
531 goto fail;
532 }
533 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
534 goto fail;
535 }
536
Robin Lee07053fc2014-04-29 19:42:01 +0100537 if (ensure_config_user_dirs(0) == -1) {
538 ALOGE("Failed to setup misc for user 0");
539 goto fail;
540 }
541
Robin Lee095c7632014-04-25 15:05:19 +0100542 if (version == 2) {
543 ALOGD("Upgrading to /data/misc/user directories");
544
Robin Lee60fd3fe2014-10-07 16:55:02 +0100545 char misc_dir[PATH_MAX];
546 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
547
548 char keychain_added_dir[PATH_MAX];
549 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
550
551 char keychain_removed_dir[PATH_MAX];
552 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
553
Robin Lee095c7632014-04-25 15:05:19 +0100554 DIR *dir;
555 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100556 dir = opendir(user_data_dir);
557 if (dir != NULL) {
558 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100559 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100560
Robin Lee60fd3fe2014-10-07 16:55:02 +0100561 // skip "." and ".."
562 if (name[0] == '.') {
563 if (name[1] == 0) continue;
564 if ((name[1] == '.') && (name[2] == 0)) continue;
565 }
566
567 uint32_t user_id = atoi(name);
568
569 // /data/misc/user/<user_id>
570 if (ensure_config_user_dirs(user_id) == -1) {
571 goto fail;
572 }
573
574 char misc_added_dir[PATH_MAX];
575 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
576
577 char misc_removed_dir[PATH_MAX];
578 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
579
580 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
581 gid_t gid = uid;
582 if (access(keychain_added_dir, F_OK) == 0) {
583 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
584 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100585 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100586 }
587 if (access(keychain_removed_dir, F_OK) == 0) {
588 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
589 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100590 }
591 }
592 }
593 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100594
Robin Lee60fd3fe2014-10-07 16:55:02 +0100595 if (access(keychain_added_dir, F_OK) == 0) {
596 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100597 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100598 if (access(keychain_removed_dir, F_OK) == 0) {
599 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100600 }
601 }
602
603 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100604 }
605
Mike Lockwood94afecf2012-10-24 10:45:23 -0700606 // Persist layout version if changed
607 if (version != oldVersion) {
608 if (fs_write_atomic_int(version_path, version) == -1) {
609 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
610 goto fail;
611 }
612 }
613
614 // Success!
615 res = 0;
616
617fail:
618 free(user_data_dir);
619 free(legacy_data_dir);
620 free(primary_data_dir);
621 return res;
622}
623
624static void drop_privileges() {
625 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
626 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
627 exit(1);
628 }
629
630 if (setgid(AID_INSTALL) < 0) {
631 ALOGE("setgid() can't drop privileges; exiting.\n");
632 exit(1);
633 }
634
635 if (setuid(AID_INSTALL) < 0) {
636 ALOGE("setuid() can't drop privileges; exiting.\n");
637 exit(1);
638 }
639
640 struct __user_cap_header_struct capheader;
641 struct __user_cap_data_struct capdata[2];
642 memset(&capheader, 0, sizeof(capheader));
643 memset(&capdata, 0, sizeof(capdata));
644 capheader.version = _LINUX_CAPABILITY_VERSION_3;
645 capheader.pid = 0;
646
647 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
648 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
649 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
650 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
Mårten Kongstad63568b12014-01-31 14:42:59 +0100651 capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700652
653 capdata[0].effective = capdata[0].permitted;
654 capdata[1].effective = capdata[1].permitted;
655 capdata[0].inheritable = 0;
656 capdata[1].inheritable = 0;
657
658 if (capset(&capheader, &capdata[0]) < 0) {
659 ALOGE("capset failed: %s\n", strerror(errno));
660 exit(1);
661 }
662}
663
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400664static int log_callback(int type, const char *fmt, ...) {
665 va_list ap;
666 int priority;
667
668 switch (type) {
669 case SELINUX_WARNING:
670 priority = ANDROID_LOG_WARN;
671 break;
672 case SELINUX_INFO:
673 priority = ANDROID_LOG_INFO;
674 break;
675 default:
676 priority = ANDROID_LOG_ERROR;
677 break;
678 }
679 va_start(ap, fmt);
680 LOG_PRI_VA(priority, "SELinux", fmt, ap);
681 va_end(ap);
682 return 0;
683}
684
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700685int main(const int argc __unused, const char *argv[] __unused) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700686 char buf[BUFFER_MAX];
687 struct sockaddr addr;
688 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700689 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400690 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700691
692 ALOGI("installd firing up\n");
693
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400694 union selinux_callback cb;
695 cb.func_log = log_callback;
696 selinux_set_callback(SELINUX_CB_LOG, cb);
697
Mike Lockwood94afecf2012-10-24 10:45:23 -0700698 if (initialize_globals() < 0) {
699 ALOGE("Could not initialize globals; exiting.\n");
700 exit(1);
701 }
702
703 if (initialize_directories() < 0) {
704 ALOGE("Could not create directories; exiting.\n");
705 exit(1);
706 }
707
Stephen Smalleybd558d62013-04-16 12:16:50 -0400708 if (selinux_enabled && selinux_status_open(true) < 0) {
709 ALOGE("Could not open selinux status; exiting.\n");
710 exit(1);
711 }
712
Mike Lockwood94afecf2012-10-24 10:45:23 -0700713 drop_privileges();
714
715 lsocket = android_get_control_socket(SOCKET_PATH);
716 if (lsocket < 0) {
717 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
718 exit(1);
719 }
720 if (listen(lsocket, 5)) {
721 ALOGE("Listen on socket failed: %s\n", strerror(errno));
722 exit(1);
723 }
724 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
725
726 for (;;) {
727 alen = sizeof(addr);
728 s = accept(lsocket, &addr, &alen);
729 if (s < 0) {
730 ALOGE("Accept failed: %s\n", strerror(errno));
731 continue;
732 }
733 fcntl(s, F_SETFD, FD_CLOEXEC);
734
735 ALOGI("new connection\n");
736 for (;;) {
737 unsigned short count;
738 if (readx(s, &count, sizeof(count))) {
739 ALOGE("failed to read size\n");
740 break;
741 }
742 if ((count < 1) || (count >= BUFFER_MAX)) {
743 ALOGE("invalid size %d\n", count);
744 break;
745 }
746 if (readx(s, buf, count)) {
747 ALOGE("failed to read command\n");
748 break;
749 }
750 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400751 if (selinux_enabled && selinux_status_updated() > 0) {
752 selinux_android_seapp_context_reload();
753 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700754 if (execute(s, buf)) break;
755 }
756 ALOGI("closing connection\n");
757 close(s);
758 }
759
760 return 0;
761}