blob: dd0c433e27278b5d50daae4f4b12996686cbf201 [file] [log] [blame]
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001// Copyright (C) 2016 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#define LOG_TAG "sdcard"
16
17#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <linux/fuse.h>
21#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/inotify.h>
25#include <sys/mount.h>
26#include <sys/resource.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <unistd.h>
30
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040031#include <android-base/logging.h>
32
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040033#include <cutils/fs.h>
34#include <cutils/hashmap.h>
35#include <cutils/log.h>
36#include <cutils/multiuser.h>
37#include <packagelistparser/packagelistparser.h>
38
39#include <private/android_filesystem_config.h>
40
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040041// README
42//
43// What is this?
44//
45// sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
46// directory permissions (all files are given fixed owner, group, and
47// permissions at creation, owner, group, and permissions are not
48// changeable, symlinks and hardlinks are not createable, etc.
49//
50// See usage() for command line options.
51//
52// It must be run as root, but will drop to requested UID/GID as soon as it
53// mounts a filesystem. It will refuse to run if requested UID/GID are zero.
54//
55// Things I believe to be true:
56//
57// - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
58// CREAT) must bump that node's refcount
59// - don't forget that FORGET can forget multiple references (req->nlookup)
60// - if an op that returns a fuse_entry fails writing the reply to the
61// kernel, you must rollback the refcount to reflect the reference the
62// kernel did not actually acquire
63//
64// This daemon can also derive custom filesystem permissions based on directory
65// structure when requested. These custom permissions support several features:
66//
67// - Apps can access their own files in /Android/data/com.example/ without
68// requiring any additional GIDs.
69// - Separate permissions for protecting directories like Pictures and Music.
70// - Multi-user separation on the same physical device.
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040071
72#include "fuse.h"
73
74/* Supplementary groups to execute with. */
75static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
76
77static int str_hash(void *key) {
78 return hashmapHash(key, strlen(static_cast<const char*>(key)));
79}
80
81/* Tests if two string keys are equal ignoring case. */
82static bool str_icase_equals(void *keyA, void *keyB) {
83 return strcasecmp(static_cast<const char*>(keyA), static_cast<const char*>(keyB)) == 0;
84}
85
86static bool remove_str_to_int(void *key, void *value, void *context) {
87 Hashmap* map = static_cast<Hashmap*>(context);
88 hashmapRemove(map, key);
89 free(key);
90 return true;
91}
92
93static bool package_parse_callback(pkg_info *info, void *userdata) {
94 struct fuse_global *global = (struct fuse_global *)userdata;
95
96 char* name = strdup(info->name);
97 hashmapPut(global->package_to_appid, name, (void*) (uintptr_t) info->uid);
98 packagelist_free(info);
99 return true;
100}
101
102static bool read_package_list(struct fuse_global* global) {
103 pthread_mutex_lock(&global->lock);
104
105 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
106
107 bool rc = packagelist_parse(package_parse_callback, global);
108 TRACE("read_package_list: found %zu packages\n",
109 hashmapSize(global->package_to_appid));
110
111 /* Regenerate ownership details using newly loaded mapping */
112 derive_permissions_recursive_locked(global->fuse_default, &global->root);
113
114 pthread_mutex_unlock(&global->lock);
115
116 return rc;
117}
118
119static void watch_package_list(struct fuse_global* global) {
120 struct inotify_event *event;
121 char event_buf[512];
122
123 int nfd = inotify_init();
124 if (nfd < 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400125 PLOG(ERROR) << "inotify_init failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400126 return;
127 }
128
129 bool active = false;
130 while (1) {
131 if (!active) {
132 int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
133 if (res == -1) {
134 if (errno == ENOENT || errno == EACCES) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400135 /* Framework may not have created the file yet, sleep and retry. */
136 LOG(ERROR) << "missing \"" << PACKAGES_LIST_FILE << "\"; retrying...";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400137 sleep(3);
138 continue;
139 } else {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400140 PLOG(ERROR) << "inotify_add_watch failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400141 return;
142 }
143 }
144
145 /* Watch above will tell us about any future changes, so
146 * read the current state. */
147 if (read_package_list(global) == false) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400148 LOG(ERROR) << "read_package_list failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400149 return;
150 }
151 active = true;
152 }
153
154 int event_pos = 0;
155 int res = read(nfd, event_buf, sizeof(event_buf));
156 if (res < (int) sizeof(*event)) {
157 if (errno == EINTR)
158 continue;
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400159 PLOG(ERROR) << "failed to read inotify event";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400160 return;
161 }
162
163 while (res >= (int) sizeof(*event)) {
164 int event_size;
165 event = (struct inotify_event *) (event_buf + event_pos);
166
167 TRACE("inotify event: %08x\n", event->mask);
168 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
169 /* Previously watched file was deleted, probably due to move
170 * that swapped in new data; re-arm the watch and read. */
171 active = false;
172 }
173
174 event_size = sizeof(*event) + event->len;
175 res -= event_size;
176 event_pos += event_size;
177 }
178 }
179}
180
181static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
182 char opts[256];
183
184 fuse->fd = open("/dev/fuse", O_RDWR);
185 if (fuse->fd == -1) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400186 PLOG(ERROR) << "failed to open fuse device";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400187 return -1;
188 }
189
190 umount2(fuse->dest_path, MNT_DETACH);
191
192 snprintf(opts, sizeof(opts),
193 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
194 fuse->fd, fuse->global->uid, fuse->global->gid);
195 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
196 MS_NOATIME, opts) != 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400197 PLOG(ERROR) << "failed to mount fuse filesystem";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400198 return -1;
199 }
200
201 fuse->gid = gid;
202 fuse->mask = mask;
203
204 return 0;
205}
206
207static void* start_handler(void* data) {
208 struct fuse_handler* handler = static_cast<fuse_handler*>(data);
209 handle_fuse_requests(handler);
210 return NULL;
211}
212
213static void run(const char* source_path, const char* label, uid_t uid,
214 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
215 struct fuse_global global;
216 struct fuse fuse_default;
217 struct fuse fuse_read;
218 struct fuse fuse_write;
219 struct fuse_handler handler_default;
220 struct fuse_handler handler_read;
221 struct fuse_handler handler_write;
222 pthread_t thread_default;
223 pthread_t thread_read;
224 pthread_t thread_write;
225
226 memset(&global, 0, sizeof(global));
227 memset(&fuse_default, 0, sizeof(fuse_default));
228 memset(&fuse_read, 0, sizeof(fuse_read));
229 memset(&fuse_write, 0, sizeof(fuse_write));
230 memset(&handler_default, 0, sizeof(handler_default));
231 memset(&handler_read, 0, sizeof(handler_read));
232 memset(&handler_write, 0, sizeof(handler_write));
233
234 pthread_mutex_init(&global.lock, NULL);
235 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
236 global.uid = uid;
237 global.gid = gid;
238 global.multi_user = multi_user;
239 global.next_generation = 0;
240 global.inode_ctr = 1;
241
242 memset(&global.root, 0, sizeof(global.root));
243 global.root.nid = FUSE_ROOT_ID; /* 1 */
244 global.root.refcount = 2;
245 global.root.namelen = strlen(source_path);
246 global.root.name = strdup(source_path);
247 global.root.userid = userid;
248 global.root.uid = AID_ROOT;
249 global.root.under_android = false;
250
251 strcpy(global.source_path, source_path);
252
253 if (multi_user) {
254 global.root.perm = PERM_PRE_ROOT;
255 snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path);
256 } else {
257 global.root.perm = PERM_ROOT;
258 snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path);
259 }
260
261 fuse_default.global = &global;
262 fuse_read.global = &global;
263 fuse_write.global = &global;
264
265 global.fuse_default = &fuse_default;
266 global.fuse_read = &fuse_read;
267 global.fuse_write = &fuse_write;
268
269 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime/default/%s", label);
270 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime/read/%s", label);
271 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime/write/%s", label);
272
273 handler_default.fuse = &fuse_default;
274 handler_read.fuse = &fuse_read;
275 handler_write.fuse = &fuse_write;
276
277 handler_default.token = 0;
278 handler_read.token = 1;
279 handler_write.token = 2;
280
281 umask(0);
282
283 if (multi_user) {
284 /* Multi-user storage is fully isolated per user, so "other"
285 * permissions are completely masked off. */
286 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
287 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
288 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400289 PLOG(FATAL) << "failed to fuse_setup";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400290 }
291 } else {
292 /* Physical storage is readable by all users on device, but
293 * the Android directories are masked off to a single user
294 * deep inside attr_from_stat(). */
295 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
296 || fuse_setup(&fuse_read, AID_EVERYBODY, full_write ? 0027 : 0022)
297 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0022)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400298 PLOG(FATAL) << "failed to fuse_setup";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400299 }
300 }
301
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400302 /* Drop privs. */
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400303 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400304 PLOG(FATAL) << "cannot setgroups";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400305 }
306 if (setgid(gid) < 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400307 PLOG(FATAL) << "cannot setgid";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400308 }
309 if (setuid(uid) < 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400310 PLOG(FATAL) << "cannot setuid";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400311 }
312
313 if (multi_user) {
314 fs_prepare_dir(global.obb_path, 0775, uid, gid);
315 }
316
317 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
318 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
319 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400320 LOG(FATAL) << "failed to pthread_create";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400321 }
322
323 watch_package_list(&global);
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400324 LOG(FATAL) << "terminated prematurely";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400325}
326
327static int usage() {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400328 LOG(ERROR) << "usage: sdcard [OPTIONS] <source_path> <label>"
329 << " -u: specify UID to run as"
330 << " -g: specify GID to run as"
331 << " -U: specify user ID that owns device"
332 << " -m: source_path is multi-user"
333 << " -w: runtime write mount has full write access";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400334 return 1;
335}
336
337int main(int argc, char **argv) {
338 const char *source_path = NULL;
339 const char *label = NULL;
340 uid_t uid = 0;
341 gid_t gid = 0;
342 userid_t userid = 0;
343 bool multi_user = false;
344 bool full_write = false;
345 int i;
346 struct rlimit rlim;
347 int fs_version;
348
349 int opt;
350 while ((opt = getopt(argc, argv, "u:g:U:mw")) != -1) {
351 switch (opt) {
352 case 'u':
353 uid = strtoul(optarg, NULL, 10);
354 break;
355 case 'g':
356 gid = strtoul(optarg, NULL, 10);
357 break;
358 case 'U':
359 userid = strtoul(optarg, NULL, 10);
360 break;
361 case 'm':
362 multi_user = true;
363 break;
364 case 'w':
365 full_write = true;
366 break;
367 case '?':
368 default:
369 return usage();
370 }
371 }
372
373 for (i = optind; i < argc; i++) {
374 char* arg = argv[i];
375 if (!source_path) {
376 source_path = arg;
377 } else if (!label) {
378 label = arg;
379 } else {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400380 LOG(ERROR) << "too many arguments";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400381 return usage();
382 }
383 }
384
385 if (!source_path) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400386 LOG(ERROR) << "no source path specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400387 return usage();
388 }
389 if (!label) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400390 LOG(ERROR) << "no label specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400391 return usage();
392 }
393 if (!uid || !gid) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400394 LOG(ERROR) << "uid and gid must be nonzero";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400395 return usage();
396 }
397
398 rlim.rlim_cur = 8192;
399 rlim.rlim_max = 8192;
400 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400401 PLOG(ERROR) << "setting RLIMIT_NOFILE failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400402 }
403
404 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400405 LOG(ERROR) << "installd fs upgrade not yet complete; waiting...";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400406 sleep(1);
407 }
408
409 run(source_path, label, uid, gid, userid, multi_user, full_write);
410 return 1;
411}