blob: 3481ec300f0075c4ad77321ffb1b5a20c75c4d05 [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/multiuser.h>
36#include <packagelistparser/packagelistparser.h>
37
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -040038#include <libminijail.h>
39#include <scoped_minijail.h>
40
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040041#include <private/android_filesystem_config.h>
42
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040043// README
44//
45// What is this?
46//
47// sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
48// directory permissions (all files are given fixed owner, group, and
49// permissions at creation, owner, group, and permissions are not
50// changeable, symlinks and hardlinks are not createable, etc.
51//
52// See usage() for command line options.
53//
54// It must be run as root, but will drop to requested UID/GID as soon as it
55// mounts a filesystem. It will refuse to run if requested UID/GID are zero.
56//
57// Things I believe to be true:
58//
59// - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
60// CREAT) must bump that node's refcount
61// - don't forget that FORGET can forget multiple references (req->nlookup)
62// - if an op that returns a fuse_entry fails writing the reply to the
63// kernel, you must rollback the refcount to reflect the reference the
64// kernel did not actually acquire
65//
66// This daemon can also derive custom filesystem permissions based on directory
67// structure when requested. These custom permissions support several features:
68//
69// - Apps can access their own files in /Android/data/com.example/ without
70// requiring any additional GIDs.
71// - Separate permissions for protecting directories like Pictures and Music.
72// - Multi-user separation on the same physical device.
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040073
74#include "fuse.h"
75
76/* Supplementary groups to execute with. */
77static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
78
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040079static bool package_parse_callback(pkg_info *info, void *userdata) {
80 struct fuse_global *global = (struct fuse_global *)userdata;
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040081 bool res = global->package_to_appid->emplace(info->name, info->uid).second;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040082 packagelist_free(info);
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040083 return res;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040084}
85
86static bool read_package_list(struct fuse_global* global) {
87 pthread_mutex_lock(&global->lock);
88
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040089 global->package_to_appid->clear();
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040090 bool rc = packagelist_parse(package_parse_callback, global);
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040091 DLOG(INFO) << "read_package_list: found " << global->package_to_appid->size() << " packages";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040092
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040093 // Regenerate ownership details using newly loaded mapping.
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040094 derive_permissions_recursive_locked(global->fuse_default, &global->root);
95
96 pthread_mutex_unlock(&global->lock);
97
98 return rc;
99}
100
101static void watch_package_list(struct fuse_global* global) {
102 struct inotify_event *event;
103 char event_buf[512];
104
105 int nfd = inotify_init();
106 if (nfd < 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400107 PLOG(ERROR) << "inotify_init failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400108 return;
109 }
110
111 bool active = false;
112 while (1) {
113 if (!active) {
114 int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
115 if (res == -1) {
116 if (errno == ENOENT || errno == EACCES) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400117 /* Framework may not have created the file yet, sleep and retry. */
118 LOG(ERROR) << "missing \"" << PACKAGES_LIST_FILE << "\"; retrying...";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400119 sleep(3);
120 continue;
121 } else {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400122 PLOG(ERROR) << "inotify_add_watch failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400123 return;
124 }
125 }
126
127 /* Watch above will tell us about any future changes, so
128 * read the current state. */
129 if (read_package_list(global) == false) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400130 LOG(ERROR) << "read_package_list failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400131 return;
132 }
133 active = true;
134 }
135
136 int event_pos = 0;
137 int res = read(nfd, event_buf, sizeof(event_buf));
138 if (res < (int) sizeof(*event)) {
139 if (errno == EINTR)
140 continue;
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400141 PLOG(ERROR) << "failed to read inotify event";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400142 return;
143 }
144
145 while (res >= (int) sizeof(*event)) {
146 int event_size;
147 event = (struct inotify_event *) (event_buf + event_pos);
148
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400149 DLOG(INFO) << "inotify event: " << std::hex << event->mask << std::dec;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400150 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
151 /* Previously watched file was deleted, probably due to move
152 * that swapped in new data; re-arm the watch and read. */
153 active = false;
154 }
155
156 event_size = sizeof(*event) + event->len;
157 res -= event_size;
158 event_pos += event_size;
159 }
160 }
161}
162
163static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
164 char opts[256];
165
166 fuse->fd = open("/dev/fuse", O_RDWR);
167 if (fuse->fd == -1) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400168 PLOG(ERROR) << "failed to open fuse device";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400169 return -1;
170 }
171
172 umount2(fuse->dest_path, MNT_DETACH);
173
174 snprintf(opts, sizeof(opts),
175 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
176 fuse->fd, fuse->global->uid, fuse->global->gid);
177 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
178 MS_NOATIME, opts) != 0) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400179 PLOG(ERROR) << "failed to mount fuse filesystem";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400180 return -1;
181 }
182
183 fuse->gid = gid;
184 fuse->mask = mask;
185
186 return 0;
187}
188
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -0400189static void drop_privs(uid_t uid, gid_t gid) {
190 ScopedMinijail j(minijail_new());
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400191 minijail_set_supplementary_gids(j.get(), arraysize(kGroups), kGroups);
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -0400192 minijail_change_gid(j.get(), gid);
193 minijail_change_uid(j.get(), uid);
194 /* minijail_enter() will abort if priv-dropping fails. */
195 minijail_enter(j.get());
196}
197
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400198static void* start_handler(void* data) {
199 struct fuse_handler* handler = static_cast<fuse_handler*>(data);
200 handle_fuse_requests(handler);
201 return NULL;
202}
203
204static void run(const char* source_path, const char* label, uid_t uid,
205 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
206 struct fuse_global global;
207 struct fuse fuse_default;
208 struct fuse fuse_read;
209 struct fuse fuse_write;
210 struct fuse_handler handler_default;
211 struct fuse_handler handler_read;
212 struct fuse_handler handler_write;
213 pthread_t thread_default;
214 pthread_t thread_read;
215 pthread_t thread_write;
216
217 memset(&global, 0, sizeof(global));
218 memset(&fuse_default, 0, sizeof(fuse_default));
219 memset(&fuse_read, 0, sizeof(fuse_read));
220 memset(&fuse_write, 0, sizeof(fuse_write));
221 memset(&handler_default, 0, sizeof(handler_default));
222 memset(&handler_read, 0, sizeof(handler_read));
223 memset(&handler_write, 0, sizeof(handler_write));
224
225 pthread_mutex_init(&global.lock, NULL);
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400226 global.package_to_appid = new AppIdMap;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400227 global.uid = uid;
228 global.gid = gid;
229 global.multi_user = multi_user;
230 global.next_generation = 0;
231 global.inode_ctr = 1;
232
233 memset(&global.root, 0, sizeof(global.root));
234 global.root.nid = FUSE_ROOT_ID; /* 1 */
235 global.root.refcount = 2;
236 global.root.namelen = strlen(source_path);
237 global.root.name = strdup(source_path);
238 global.root.userid = userid;
239 global.root.uid = AID_ROOT;
240 global.root.under_android = false;
241
242 strcpy(global.source_path, source_path);
243
244 if (multi_user) {
245 global.root.perm = PERM_PRE_ROOT;
246 snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path);
247 } else {
248 global.root.perm = PERM_ROOT;
249 snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path);
250 }
251
252 fuse_default.global = &global;
253 fuse_read.global = &global;
254 fuse_write.global = &global;
255
256 global.fuse_default = &fuse_default;
257 global.fuse_read = &fuse_read;
258 global.fuse_write = &fuse_write;
259
260 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime/default/%s", label);
261 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime/read/%s", label);
262 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime/write/%s", label);
263
264 handler_default.fuse = &fuse_default;
265 handler_read.fuse = &fuse_read;
266 handler_write.fuse = &fuse_write;
267
268 handler_default.token = 0;
269 handler_read.token = 1;
270 handler_write.token = 2;
271
272 umask(0);
273
274 if (multi_user) {
275 /* Multi-user storage is fully isolated per user, so "other"
276 * permissions are completely masked off. */
277 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
278 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
279 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400280 PLOG(FATAL) << "failed to fuse_setup";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400281 }
282 } else {
283 /* Physical storage is readable by all users on device, but
284 * the Android directories are masked off to a single user
285 * deep inside attr_from_stat(). */
286 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
287 || fuse_setup(&fuse_read, AID_EVERYBODY, full_write ? 0027 : 0022)
288 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0022)) {
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 }
292
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -0400293 // Will abort if priv-dropping fails.
294 drop_privs(uid, gid);
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400295
296 if (multi_user) {
297 fs_prepare_dir(global.obb_path, 0775, uid, gid);
298 }
299
300 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
301 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
302 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400303 LOG(FATAL) << "failed to pthread_create";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400304 }
305
306 watch_package_list(&global);
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400307 LOG(FATAL) << "terminated prematurely";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400308}
309
310static int usage() {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400311 LOG(ERROR) << "usage: sdcard [OPTIONS] <source_path> <label>"
312 << " -u: specify UID to run as"
313 << " -g: specify GID to run as"
314 << " -U: specify user ID that owns device"
315 << " -m: source_path is multi-user"
316 << " -w: runtime write mount has full write access";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400317 return 1;
318}
319
320int main(int argc, char **argv) {
321 const char *source_path = NULL;
322 const char *label = NULL;
323 uid_t uid = 0;
324 gid_t gid = 0;
325 userid_t userid = 0;
326 bool multi_user = false;
327 bool full_write = false;
328 int i;
329 struct rlimit rlim;
330 int fs_version;
331
332 int opt;
333 while ((opt = getopt(argc, argv, "u:g:U:mw")) != -1) {
334 switch (opt) {
335 case 'u':
336 uid = strtoul(optarg, NULL, 10);
337 break;
338 case 'g':
339 gid = strtoul(optarg, NULL, 10);
340 break;
341 case 'U':
342 userid = strtoul(optarg, NULL, 10);
343 break;
344 case 'm':
345 multi_user = true;
346 break;
347 case 'w':
348 full_write = true;
349 break;
350 case '?':
351 default:
352 return usage();
353 }
354 }
355
356 for (i = optind; i < argc; i++) {
357 char* arg = argv[i];
358 if (!source_path) {
359 source_path = arg;
360 } else if (!label) {
361 label = arg;
362 } else {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400363 LOG(ERROR) << "too many arguments";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400364 return usage();
365 }
366 }
367
368 if (!source_path) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400369 LOG(ERROR) << "no source path specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400370 return usage();
371 }
372 if (!label) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400373 LOG(ERROR) << "no label specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400374 return usage();
375 }
376 if (!uid || !gid) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400377 LOG(ERROR) << "uid and gid must be nonzero";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400378 return usage();
379 }
380
381 rlim.rlim_cur = 8192;
382 rlim.rlim_max = 8192;
383 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400384 PLOG(ERROR) << "setting RLIMIT_NOFILE failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400385 }
386
387 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400388 LOG(ERROR) << "installd fs upgrade not yet complete; waiting...";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400389 sleep(1);
390 }
391
392 run(source_path, label, uid, gid, userid, multi_user, full_write);
393 return 1;
394}