blob: 3d7bdc9021a58d9288f3a830c4534d9c7d467ab3 [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>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040032#include <android-base/macros.h>
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040033
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040034#include <cutils/fs.h>
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040035#include <cutils/log.h>
36#include <cutils/multiuser.h>
37#include <packagelistparser/packagelistparser.h>
38
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -040039#include <libminijail.h>
40#include <scoped_minijail.h>
41
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040042#include <private/android_filesystem_config.h>
43
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040044// README
45//
46// What is this?
47//
48// sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
49// directory permissions (all files are given fixed owner, group, and
50// permissions at creation, owner, group, and permissions are not
51// changeable, symlinks and hardlinks are not createable, etc.
52//
53// See usage() for command line options.
54//
55// It must be run as root, but will drop to requested UID/GID as soon as it
56// mounts a filesystem. It will refuse to run if requested UID/GID are zero.
57//
58// Things I believe to be true:
59//
60// - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
61// CREAT) must bump that node's refcount
62// - don't forget that FORGET can forget multiple references (req->nlookup)
63// - if an op that returns a fuse_entry fails writing the reply to the
64// kernel, you must rollback the refcount to reflect the reference the
65// kernel did not actually acquire
66//
67// This daemon can also derive custom filesystem permissions based on directory
68// structure when requested. These custom permissions support several features:
69//
70// - Apps can access their own files in /Android/data/com.example/ without
71// requiring any additional GIDs.
72// - Separate permissions for protecting directories like Pictures and Music.
73// - Multi-user separation on the same physical device.
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040074
75#include "fuse.h"
76
77/* Supplementary groups to execute with. */
78static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
79
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040080static bool package_parse_callback(pkg_info *info, void *userdata) {
81 struct fuse_global *global = (struct fuse_global *)userdata;
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040082 bool res = global->package_to_appid->emplace(info->name, info->uid).second;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040083 packagelist_free(info);
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040084 return res;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040085}
86
87static bool read_package_list(struct fuse_global* global) {
88 pthread_mutex_lock(&global->lock);
89
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040090 global->package_to_appid->clear();
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040091 bool rc = packagelist_parse(package_parse_callback, global);
92 TRACE("read_package_list: found %zu packages\n",
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040093 global->package_to_appid->size());
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040094
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040095 // Regenerate ownership details using newly loaded mapping.
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040096 derive_permissions_recursive_locked(global->fuse_default, &global->root);
97
98 pthread_mutex_unlock(&global->lock);
99
100 return rc;
101}
102
103static void watch_package_list(struct fuse_global* global) {
104 struct inotify_event *event;
105 char event_buf[512];
106
107 int nfd = inotify_init();
108 if (nfd < 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400109 PLOG(ERROR) << "inotify_init failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400110 return;
111 }
112
113 bool active = false;
114 while (1) {
115 if (!active) {
116 int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
117 if (res == -1) {
118 if (errno == ENOENT || errno == EACCES) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400119 /* Framework may not have created the file yet, sleep and retry. */
120 LOG(ERROR) << "missing \"" << PACKAGES_LIST_FILE << "\"; retrying...";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400121 sleep(3);
122 continue;
123 } else {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400124 PLOG(ERROR) << "inotify_add_watch failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400125 return;
126 }
127 }
128
129 /* Watch above will tell us about any future changes, so
130 * read the current state. */
131 if (read_package_list(global) == false) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400132 LOG(ERROR) << "read_package_list failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400133 return;
134 }
135 active = true;
136 }
137
138 int event_pos = 0;
139 int res = read(nfd, event_buf, sizeof(event_buf));
140 if (res < (int) sizeof(*event)) {
141 if (errno == EINTR)
142 continue;
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400143 PLOG(ERROR) << "failed to read inotify event";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400144 return;
145 }
146
147 while (res >= (int) sizeof(*event)) {
148 int event_size;
149 event = (struct inotify_event *) (event_buf + event_pos);
150
151 TRACE("inotify event: %08x\n", event->mask);
152 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
153 /* Previously watched file was deleted, probably due to move
154 * that swapped in new data; re-arm the watch and read. */
155 active = false;
156 }
157
158 event_size = sizeof(*event) + event->len;
159 res -= event_size;
160 event_pos += event_size;
161 }
162 }
163}
164
165static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
166 char opts[256];
167
168 fuse->fd = open("/dev/fuse", O_RDWR);
169 if (fuse->fd == -1) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400170 PLOG(ERROR) << "failed to open fuse device";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400171 return -1;
172 }
173
174 umount2(fuse->dest_path, MNT_DETACH);
175
176 snprintf(opts, sizeof(opts),
177 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
178 fuse->fd, fuse->global->uid, fuse->global->gid);
179 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
180 MS_NOATIME, opts) != 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400181 PLOG(ERROR) << "failed to mount fuse filesystem";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400182 return -1;
183 }
184
185 fuse->gid = gid;
186 fuse->mask = mask;
187
188 return 0;
189}
190
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -0400191static void drop_privs(uid_t uid, gid_t gid) {
192 ScopedMinijail j(minijail_new());
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400193 minijail_set_supplementary_gids(j.get(), arraysize(kGroups), kGroups);
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -0400194 minijail_change_gid(j.get(), gid);
195 minijail_change_uid(j.get(), uid);
196 /* minijail_enter() will abort if priv-dropping fails. */
197 minijail_enter(j.get());
198}
199
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400200static void* start_handler(void* data) {
201 struct fuse_handler* handler = static_cast<fuse_handler*>(data);
202 handle_fuse_requests(handler);
203 return NULL;
204}
205
206static void run(const char* source_path, const char* label, uid_t uid,
207 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
208 struct fuse_global global;
209 struct fuse fuse_default;
210 struct fuse fuse_read;
211 struct fuse fuse_write;
212 struct fuse_handler handler_default;
213 struct fuse_handler handler_read;
214 struct fuse_handler handler_write;
215 pthread_t thread_default;
216 pthread_t thread_read;
217 pthread_t thread_write;
218
219 memset(&global, 0, sizeof(global));
220 memset(&fuse_default, 0, sizeof(fuse_default));
221 memset(&fuse_read, 0, sizeof(fuse_read));
222 memset(&fuse_write, 0, sizeof(fuse_write));
223 memset(&handler_default, 0, sizeof(handler_default));
224 memset(&handler_read, 0, sizeof(handler_read));
225 memset(&handler_write, 0, sizeof(handler_write));
226
227 pthread_mutex_init(&global.lock, NULL);
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400228 global.package_to_appid = new AppIdMap;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400229 global.uid = uid;
230 global.gid = gid;
231 global.multi_user = multi_user;
232 global.next_generation = 0;
233 global.inode_ctr = 1;
234
235 memset(&global.root, 0, sizeof(global.root));
236 global.root.nid = FUSE_ROOT_ID; /* 1 */
237 global.root.refcount = 2;
238 global.root.namelen = strlen(source_path);
239 global.root.name = strdup(source_path);
240 global.root.userid = userid;
241 global.root.uid = AID_ROOT;
242 global.root.under_android = false;
243
244 strcpy(global.source_path, source_path);
245
246 if (multi_user) {
247 global.root.perm = PERM_PRE_ROOT;
248 snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path);
249 } else {
250 global.root.perm = PERM_ROOT;
251 snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path);
252 }
253
254 fuse_default.global = &global;
255 fuse_read.global = &global;
256 fuse_write.global = &global;
257
258 global.fuse_default = &fuse_default;
259 global.fuse_read = &fuse_read;
260 global.fuse_write = &fuse_write;
261
262 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime/default/%s", label);
263 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime/read/%s", label);
264 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime/write/%s", label);
265
266 handler_default.fuse = &fuse_default;
267 handler_read.fuse = &fuse_read;
268 handler_write.fuse = &fuse_write;
269
270 handler_default.token = 0;
271 handler_read.token = 1;
272 handler_write.token = 2;
273
274 umask(0);
275
276 if (multi_user) {
277 /* Multi-user storage is fully isolated per user, so "other"
278 * permissions are completely masked off. */
279 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
280 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
281 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400282 PLOG(FATAL) << "failed to fuse_setup";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400283 }
284 } else {
285 /* Physical storage is readable by all users on device, but
286 * the Android directories are masked off to a single user
287 * deep inside attr_from_stat(). */
288 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
289 || fuse_setup(&fuse_read, AID_EVERYBODY, full_write ? 0027 : 0022)
290 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0022)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400291 PLOG(FATAL) << "failed to fuse_setup";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400292 }
293 }
294
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -0400295 // Will abort if priv-dropping fails.
296 drop_privs(uid, gid);
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400297
298 if (multi_user) {
299 fs_prepare_dir(global.obb_path, 0775, uid, gid);
300 }
301
302 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
303 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
304 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400305 LOG(FATAL) << "failed to pthread_create";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400306 }
307
308 watch_package_list(&global);
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400309 LOG(FATAL) << "terminated prematurely";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400310}
311
312static int usage() {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400313 LOG(ERROR) << "usage: sdcard [OPTIONS] <source_path> <label>"
314 << " -u: specify UID to run as"
315 << " -g: specify GID to run as"
316 << " -U: specify user ID that owns device"
317 << " -m: source_path is multi-user"
318 << " -w: runtime write mount has full write access";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400319 return 1;
320}
321
322int main(int argc, char **argv) {
323 const char *source_path = NULL;
324 const char *label = NULL;
325 uid_t uid = 0;
326 gid_t gid = 0;
327 userid_t userid = 0;
328 bool multi_user = false;
329 bool full_write = false;
330 int i;
331 struct rlimit rlim;
332 int fs_version;
333
334 int opt;
335 while ((opt = getopt(argc, argv, "u:g:U:mw")) != -1) {
336 switch (opt) {
337 case 'u':
338 uid = strtoul(optarg, NULL, 10);
339 break;
340 case 'g':
341 gid = strtoul(optarg, NULL, 10);
342 break;
343 case 'U':
344 userid = strtoul(optarg, NULL, 10);
345 break;
346 case 'm':
347 multi_user = true;
348 break;
349 case 'w':
350 full_write = true;
351 break;
352 case '?':
353 default:
354 return usage();
355 }
356 }
357
358 for (i = optind; i < argc; i++) {
359 char* arg = argv[i];
360 if (!source_path) {
361 source_path = arg;
362 } else if (!label) {
363 label = arg;
364 } else {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400365 LOG(ERROR) << "too many arguments";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400366 return usage();
367 }
368 }
369
370 if (!source_path) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400371 LOG(ERROR) << "no source path specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400372 return usage();
373 }
374 if (!label) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400375 LOG(ERROR) << "no label specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400376 return usage();
377 }
378 if (!uid || !gid) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400379 LOG(ERROR) << "uid and gid must be nonzero";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400380 return usage();
381 }
382
383 rlim.rlim_cur = 8192;
384 rlim.rlim_max = 8192;
385 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400386 PLOG(ERROR) << "setting RLIMIT_NOFILE failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400387 }
388
389 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400390 LOG(ERROR) << "installd fs upgrade not yet complete; waiting...";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400391 sleep(1);
392 }
393
394 run(source_path, label, uid, gid, userid, multi_user, full_write);
395 return 1;
396}