| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 1 | // 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 |  | 
| Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 31 | #include <android-base/file.h> | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 32 | #include <android-base/logging.h> | 
| Jorge Lucangeli Obes | bae15b4 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 33 | #include <android-base/macros.h> | 
| Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> | 
|  | 35 | #include <android-base/strings.h> | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 36 |  | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 37 | #include <cutils/fs.h> | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 38 | #include <cutils/multiuser.h> | 
| Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 39 | #include <cutils/properties.h> | 
|  | 40 |  | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 41 | #include <packagelistparser/packagelistparser.h> | 
|  | 42 |  | 
| Jorge Lucangeli Obes | c96f53e | 2016-07-14 14:50:14 -0400 | [diff] [blame] | 43 | #include <libminijail.h> | 
|  | 44 | #include <scoped_minijail.h> | 
|  | 45 |  | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> | 
|  | 47 |  | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 48 | // README | 
|  | 49 | // | 
|  | 50 | // What is this? | 
|  | 51 | // | 
|  | 52 | // sdcard is a program that uses FUSE to emulate FAT-on-sdcard style | 
|  | 53 | // directory permissions (all files are given fixed owner, group, and | 
|  | 54 | // permissions at creation, owner, group, and permissions are not | 
|  | 55 | // changeable, symlinks and hardlinks are not createable, etc. | 
|  | 56 | // | 
|  | 57 | // See usage() for command line options. | 
|  | 58 | // | 
|  | 59 | // It must be run as root, but will drop to requested UID/GID as soon as it | 
|  | 60 | // mounts a filesystem.  It will refuse to run if requested UID/GID are zero. | 
|  | 61 | // | 
|  | 62 | // Things I believe to be true: | 
|  | 63 | // | 
|  | 64 | // - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK, | 
|  | 65 | // CREAT) must bump that node's refcount | 
|  | 66 | // - don't forget that FORGET can forget multiple references (req->nlookup) | 
|  | 67 | // - if an op that returns a fuse_entry fails writing the reply to the | 
|  | 68 | // kernel, you must rollback the refcount to reflect the reference the | 
|  | 69 | // kernel did not actually acquire | 
|  | 70 | // | 
|  | 71 | // This daemon can also derive custom filesystem permissions based on directory | 
|  | 72 | // structure when requested. These custom permissions support several features: | 
|  | 73 | // | 
|  | 74 | // - Apps can access their own files in /Android/data/com.example/ without | 
|  | 75 | // requiring any additional GIDs. | 
|  | 76 | // - Separate permissions for protecting directories like Pictures and Music. | 
|  | 77 | // - Multi-user separation on the same physical device. | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 78 |  | 
|  | 79 | #include "fuse.h" | 
|  | 80 |  | 
| Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 81 | #define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs" | 
|  | 82 | #define PROP_SDCARDFS_USER "persist.sys.sdcardfs" | 
|  | 83 |  | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 84 | /* Supplementary groups to execute with. */ | 
|  | 85 | static const gid_t kGroups[1] = { AID_PACKAGE_INFO }; | 
|  | 86 |  | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 87 | static bool package_parse_callback(pkg_info *info, void *userdata) { | 
|  | 88 | struct fuse_global *global = (struct fuse_global *)userdata; | 
| Jorge Lucangeli Obes | d6d8faa | 2016-07-19 12:10:26 -0400 | [diff] [blame] | 89 | bool res = global->package_to_appid->emplace(info->name, info->uid).second; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 90 | packagelist_free(info); | 
| Jorge Lucangeli Obes | d6d8faa | 2016-07-19 12:10:26 -0400 | [diff] [blame] | 91 | return res; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | static bool read_package_list(struct fuse_global* global) { | 
|  | 95 | pthread_mutex_lock(&global->lock); | 
|  | 96 |  | 
| Jorge Lucangeli Obes | d6d8faa | 2016-07-19 12:10:26 -0400 | [diff] [blame] | 97 | global->package_to_appid->clear(); | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 98 | bool rc = packagelist_parse(package_parse_callback, global); | 
| Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame] | 99 | DLOG(INFO) << "read_package_list: found " << global->package_to_appid->size() << " packages"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 100 |  | 
| Jorge Lucangeli Obes | d6d8faa | 2016-07-19 12:10:26 -0400 | [diff] [blame] | 101 | // Regenerate ownership details using newly loaded mapping. | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 102 | derive_permissions_recursive_locked(global->fuse_default, &global->root); | 
|  | 103 |  | 
|  | 104 | pthread_mutex_unlock(&global->lock); | 
|  | 105 |  | 
|  | 106 | return rc; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | static void watch_package_list(struct fuse_global* global) { | 
|  | 110 | struct inotify_event *event; | 
|  | 111 | char event_buf[512]; | 
|  | 112 |  | 
|  | 113 | int nfd = inotify_init(); | 
|  | 114 | if (nfd < 0) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 115 | PLOG(ERROR) << "inotify_init failed"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 116 | return; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | bool active = false; | 
|  | 120 | while (1) { | 
|  | 121 | if (!active) { | 
|  | 122 | int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF); | 
|  | 123 | if (res == -1) { | 
|  | 124 | if (errno == ENOENT || errno == EACCES) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 125 | /* Framework may not have created the file yet, sleep and retry. */ | 
|  | 126 | LOG(ERROR) << "missing \"" << PACKAGES_LIST_FILE << "\"; retrying..."; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 127 | sleep(3); | 
|  | 128 | continue; | 
|  | 129 | } else { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 130 | PLOG(ERROR) << "inotify_add_watch failed"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 131 | return; | 
|  | 132 | } | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | /* Watch above will tell us about any future changes, so | 
|  | 136 | * read the current state. */ | 
|  | 137 | if (read_package_list(global) == false) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 138 | LOG(ERROR) << "read_package_list failed"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 139 | return; | 
|  | 140 | } | 
|  | 141 | active = true; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | int event_pos = 0; | 
|  | 145 | int res = read(nfd, event_buf, sizeof(event_buf)); | 
|  | 146 | if (res < (int) sizeof(*event)) { | 
|  | 147 | if (errno == EINTR) | 
|  | 148 | continue; | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 149 | PLOG(ERROR) << "failed to read inotify event"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 150 | return; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | while (res >= (int) sizeof(*event)) { | 
|  | 154 | int event_size; | 
|  | 155 | event = (struct inotify_event *) (event_buf + event_pos); | 
|  | 156 |  | 
| Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame] | 157 | DLOG(INFO) << "inotify event: " << std::hex << event->mask << std::dec; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 158 | if ((event->mask & IN_IGNORED) == IN_IGNORED) { | 
|  | 159 | /* Previously watched file was deleted, probably due to move | 
|  | 160 | * that swapped in new data; re-arm the watch and read. */ | 
|  | 161 | active = false; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | event_size = sizeof(*event) + event->len; | 
|  | 165 | res -= event_size; | 
|  | 166 | event_pos += event_size; | 
|  | 167 | } | 
|  | 168 | } | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) { | 
|  | 172 | char opts[256]; | 
|  | 173 |  | 
|  | 174 | fuse->fd = open("/dev/fuse", O_RDWR); | 
|  | 175 | if (fuse->fd == -1) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 176 | PLOG(ERROR) << "failed to open fuse device"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 177 | return -1; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | umount2(fuse->dest_path, MNT_DETACH); | 
|  | 181 |  | 
|  | 182 | snprintf(opts, sizeof(opts), | 
|  | 183 | "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d", | 
|  | 184 | fuse->fd, fuse->global->uid, fuse->global->gid); | 
|  | 185 | if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC | | 
|  | 186 | MS_NOATIME, opts) != 0) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 187 | PLOG(ERROR) << "failed to mount fuse filesystem"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 188 | return -1; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | fuse->gid = gid; | 
|  | 192 | fuse->mask = mask; | 
|  | 193 |  | 
|  | 194 | return 0; | 
|  | 195 | } | 
|  | 196 |  | 
| Jorge Lucangeli Obes | c96f53e | 2016-07-14 14:50:14 -0400 | [diff] [blame] | 197 | static void drop_privs(uid_t uid, gid_t gid) { | 
|  | 198 | ScopedMinijail j(minijail_new()); | 
| Jorge Lucangeli Obes | bae15b4 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 199 | minijail_set_supplementary_gids(j.get(), arraysize(kGroups), kGroups); | 
| Jorge Lucangeli Obes | c96f53e | 2016-07-14 14:50:14 -0400 | [diff] [blame] | 200 | minijail_change_gid(j.get(), gid); | 
|  | 201 | minijail_change_uid(j.get(), uid); | 
|  | 202 | /* minijail_enter() will abort if priv-dropping fails. */ | 
|  | 203 | minijail_enter(j.get()); | 
|  | 204 | } | 
|  | 205 |  | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 206 | static void* start_handler(void* data) { | 
|  | 207 | struct fuse_handler* handler = static_cast<fuse_handler*>(data); | 
|  | 208 | handle_fuse_requests(handler); | 
|  | 209 | return NULL; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | static void run(const char* source_path, const char* label, uid_t uid, | 
|  | 213 | gid_t gid, userid_t userid, bool multi_user, bool full_write) { | 
|  | 214 | struct fuse_global global; | 
|  | 215 | struct fuse fuse_default; | 
|  | 216 | struct fuse fuse_read; | 
|  | 217 | struct fuse fuse_write; | 
|  | 218 | struct fuse_handler handler_default; | 
|  | 219 | struct fuse_handler handler_read; | 
|  | 220 | struct fuse_handler handler_write; | 
|  | 221 | pthread_t thread_default; | 
|  | 222 | pthread_t thread_read; | 
|  | 223 | pthread_t thread_write; | 
|  | 224 |  | 
|  | 225 | memset(&global, 0, sizeof(global)); | 
|  | 226 | memset(&fuse_default, 0, sizeof(fuse_default)); | 
|  | 227 | memset(&fuse_read, 0, sizeof(fuse_read)); | 
|  | 228 | memset(&fuse_write, 0, sizeof(fuse_write)); | 
|  | 229 | memset(&handler_default, 0, sizeof(handler_default)); | 
|  | 230 | memset(&handler_read, 0, sizeof(handler_read)); | 
|  | 231 | memset(&handler_write, 0, sizeof(handler_write)); | 
|  | 232 |  | 
|  | 233 | pthread_mutex_init(&global.lock, NULL); | 
| Jorge Lucangeli Obes | d6d8faa | 2016-07-19 12:10:26 -0400 | [diff] [blame] | 234 | global.package_to_appid = new AppIdMap; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 235 | global.uid = uid; | 
|  | 236 | global.gid = gid; | 
|  | 237 | global.multi_user = multi_user; | 
|  | 238 | global.next_generation = 0; | 
|  | 239 | global.inode_ctr = 1; | 
|  | 240 |  | 
|  | 241 | memset(&global.root, 0, sizeof(global.root)); | 
|  | 242 | global.root.nid = FUSE_ROOT_ID; /* 1 */ | 
|  | 243 | global.root.refcount = 2; | 
|  | 244 | global.root.namelen = strlen(source_path); | 
|  | 245 | global.root.name = strdup(source_path); | 
|  | 246 | global.root.userid = userid; | 
|  | 247 | global.root.uid = AID_ROOT; | 
|  | 248 | global.root.under_android = false; | 
|  | 249 |  | 
|  | 250 | strcpy(global.source_path, source_path); | 
|  | 251 |  | 
|  | 252 | if (multi_user) { | 
|  | 253 | global.root.perm = PERM_PRE_ROOT; | 
|  | 254 | snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path); | 
|  | 255 | } else { | 
|  | 256 | global.root.perm = PERM_ROOT; | 
|  | 257 | snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path); | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | fuse_default.global = &global; | 
|  | 261 | fuse_read.global = &global; | 
|  | 262 | fuse_write.global = &global; | 
|  | 263 |  | 
|  | 264 | global.fuse_default = &fuse_default; | 
|  | 265 | global.fuse_read = &fuse_read; | 
|  | 266 | global.fuse_write = &fuse_write; | 
|  | 267 |  | 
|  | 268 | snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime/default/%s", label); | 
|  | 269 | snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime/read/%s", label); | 
|  | 270 | snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime/write/%s", label); | 
|  | 271 |  | 
|  | 272 | handler_default.fuse = &fuse_default; | 
|  | 273 | handler_read.fuse = &fuse_read; | 
|  | 274 | handler_write.fuse = &fuse_write; | 
|  | 275 |  | 
|  | 276 | handler_default.token = 0; | 
|  | 277 | handler_read.token = 1; | 
|  | 278 | handler_write.token = 2; | 
|  | 279 |  | 
|  | 280 | umask(0); | 
|  | 281 |  | 
|  | 282 | if (multi_user) { | 
|  | 283 | /* Multi-user storage is fully isolated per user, so "other" | 
|  | 284 | * permissions are completely masked off. */ | 
|  | 285 | if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006) | 
|  | 286 | || fuse_setup(&fuse_read, AID_EVERYBODY, 0027) | 
|  | 287 | || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 288 | PLOG(FATAL) << "failed to fuse_setup"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 289 | } | 
|  | 290 | } else { | 
|  | 291 | /* Physical storage is readable by all users on device, but | 
|  | 292 | * the Android directories are masked off to a single user | 
|  | 293 | * deep inside attr_from_stat(). */ | 
|  | 294 | if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006) | 
|  | 295 | || fuse_setup(&fuse_read, AID_EVERYBODY, full_write ? 0027 : 0022) | 
|  | 296 | || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0022)) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 297 | PLOG(FATAL) << "failed to fuse_setup"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 298 | } | 
|  | 299 | } | 
|  | 300 |  | 
| Jorge Lucangeli Obes | c96f53e | 2016-07-14 14:50:14 -0400 | [diff] [blame] | 301 | // Will abort if priv-dropping fails. | 
|  | 302 | drop_privs(uid, gid); | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 303 |  | 
|  | 304 | if (multi_user) { | 
|  | 305 | fs_prepare_dir(global.obb_path, 0775, uid, gid); | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | if (pthread_create(&thread_default, NULL, start_handler, &handler_default) | 
|  | 309 | || pthread_create(&thread_read, NULL, start_handler, &handler_read) | 
|  | 310 | || pthread_create(&thread_write, NULL, start_handler, &handler_write)) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 311 | LOG(FATAL) << "failed to pthread_create"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 312 | } | 
|  | 313 |  | 
|  | 314 | watch_package_list(&global); | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 315 | LOG(FATAL) << "terminated prematurely"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 316 | } | 
|  | 317 |  | 
| Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 318 | static bool sdcardfs_setup(const std::string& source_path, const std::string& dest_path, uid_t fsuid, | 
|  | 319 | gid_t fsgid, bool multi_user, userid_t userid, gid_t gid, mode_t mask) { | 
|  | 320 | std::string opts = android::base::StringPrintf("fsuid=%d,fsgid=%d,%smask=%d,userid=%d,gid=%d", | 
|  | 321 | fsuid, fsgid, multi_user?"multiuser,":"", mask, userid, gid); | 
|  | 322 |  | 
|  | 323 | if (mount(source_path.c_str(), dest_path.c_str(), "sdcardfs", | 
|  | 324 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()) != 0) { | 
|  | 325 | PLOG(ERROR) << "failed to mount sdcardfs filesystem"; | 
|  | 326 | return false; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | return true; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | static void run_sdcardfs(const std::string& source_path, const std::string& label, uid_t uid, | 
|  | 333 | gid_t gid, userid_t userid, bool multi_user, bool full_write) { | 
|  | 334 | std::string dest_path_default = "/mnt/runtime/default/" + label; | 
|  | 335 | std::string dest_path_read = "/mnt/runtime/read/" + label; | 
|  | 336 | std::string dest_path_write = "/mnt/runtime/write/" + label; | 
|  | 337 |  | 
|  | 338 | umask(0); | 
|  | 339 | if (multi_user) { | 
|  | 340 | // Multi-user storage is fully isolated per user, so "other" | 
|  | 341 | // permissions are completely masked off. | 
|  | 342 | if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid, | 
|  | 343 | AID_SDCARD_RW, 0006) | 
|  | 344 | || !sdcardfs_setup(source_path, dest_path_read, uid, gid, multi_user, userid, | 
|  | 345 | AID_EVERYBODY, 0027) | 
|  | 346 | || !sdcardfs_setup(source_path, dest_path_write, uid, gid, multi_user, userid, | 
|  | 347 | AID_EVERYBODY, full_write ? 0007 : 0027)) { | 
|  | 348 | LOG(FATAL) << "failed to sdcardfs_setup"; | 
|  | 349 | } | 
|  | 350 | } else { | 
|  | 351 | // Physical storage is readable by all users on device, but | 
|  | 352 | // the Android directories are masked off to a single user | 
|  | 353 | // deep inside attr_from_stat(). | 
|  | 354 | if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid, | 
|  | 355 | AID_SDCARD_RW, 0006) | 
|  | 356 | || !sdcardfs_setup(source_path, dest_path_read, uid, gid, multi_user, userid, | 
|  | 357 | AID_EVERYBODY, full_write ? 0027 : 0022) | 
|  | 358 | || !sdcardfs_setup(source_path, dest_path_write, uid, gid, multi_user, userid, | 
|  | 359 | AID_EVERYBODY, full_write ? 0007 : 0022)) { | 
|  | 360 | LOG(FATAL) << "failed to sdcardfs_setup"; | 
|  | 361 | } | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | // Will abort if priv-dropping fails. | 
|  | 365 | drop_privs(uid, gid); | 
|  | 366 |  | 
|  | 367 | if (multi_user) { | 
|  | 368 | std::string obb_path = source_path + "/obb"; | 
|  | 369 | fs_prepare_dir(obb_path.c_str(), 0775, uid, gid); | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | exit(0); | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | static bool supports_sdcardfs(void) { | 
|  | 376 | std::string filesystems; | 
|  | 377 | if (!android::base::ReadFileToString("/proc/filesystems", &filesystems)) { | 
|  | 378 | PLOG(ERROR) << "Could not read /proc/filesystems"; | 
|  | 379 | return false; | 
|  | 380 | } | 
|  | 381 | for (const auto& fs : android::base::Split(filesystems, "\n")) { | 
|  | 382 | if (fs.find("sdcardfs") != std::string::npos) return true; | 
|  | 383 | } | 
|  | 384 | return false; | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | static bool should_use_sdcardfs(void) { | 
|  | 388 | char property[PROPERTY_VALUE_MAX]; | 
|  | 389 |  | 
|  | 390 | // Allow user to have a strong opinion about state | 
|  | 391 | property_get(PROP_SDCARDFS_USER, property, ""); | 
|  | 392 | if (!strcmp(property, "force_on")) { | 
|  | 393 | LOG(WARNING) << "User explicitly enabled sdcardfs"; | 
|  | 394 | return supports_sdcardfs(); | 
|  | 395 | } else if (!strcmp(property, "force_off")) { | 
|  | 396 | LOG(WARNING) << "User explicitly disabled sdcardfs"; | 
|  | 397 | return false; | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | // Fall back to device opinion about state | 
|  | 401 | if (property_get_bool(PROP_SDCARDFS_DEVICE, false)) { | 
|  | 402 | LOG(WARNING) << "Device explicitly enabled sdcardfs"; | 
|  | 403 | return supports_sdcardfs(); | 
|  | 404 | } else { | 
|  | 405 | LOG(WARNING) << "Device explicitly disabled sdcardfs"; | 
|  | 406 | return false; | 
|  | 407 | } | 
|  | 408 | } | 
|  | 409 |  | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 410 | static int usage() { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 411 | LOG(ERROR) << "usage: sdcard [OPTIONS] <source_path> <label>" | 
|  | 412 | << "    -u: specify UID to run as" | 
|  | 413 | << "    -g: specify GID to run as" | 
|  | 414 | << "    -U: specify user ID that owns device" | 
|  | 415 | << "    -m: source_path is multi-user" | 
|  | 416 | << "    -w: runtime write mount has full write access"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 417 | return 1; | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | int main(int argc, char **argv) { | 
|  | 421 | const char *source_path = NULL; | 
|  | 422 | const char *label = NULL; | 
|  | 423 | uid_t uid = 0; | 
|  | 424 | gid_t gid = 0; | 
|  | 425 | userid_t userid = 0; | 
|  | 426 | bool multi_user = false; | 
|  | 427 | bool full_write = false; | 
|  | 428 | int i; | 
|  | 429 | struct rlimit rlim; | 
|  | 430 | int fs_version; | 
|  | 431 |  | 
|  | 432 | int opt; | 
|  | 433 | while ((opt = getopt(argc, argv, "u:g:U:mw")) != -1) { | 
|  | 434 | switch (opt) { | 
|  | 435 | case 'u': | 
|  | 436 | uid = strtoul(optarg, NULL, 10); | 
|  | 437 | break; | 
|  | 438 | case 'g': | 
|  | 439 | gid = strtoul(optarg, NULL, 10); | 
|  | 440 | break; | 
|  | 441 | case 'U': | 
|  | 442 | userid = strtoul(optarg, NULL, 10); | 
|  | 443 | break; | 
|  | 444 | case 'm': | 
|  | 445 | multi_user = true; | 
|  | 446 | break; | 
|  | 447 | case 'w': | 
|  | 448 | full_write = true; | 
|  | 449 | break; | 
|  | 450 | case '?': | 
|  | 451 | default: | 
|  | 452 | return usage(); | 
|  | 453 | } | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | for (i = optind; i < argc; i++) { | 
|  | 457 | char* arg = argv[i]; | 
|  | 458 | if (!source_path) { | 
|  | 459 | source_path = arg; | 
|  | 460 | } else if (!label) { | 
|  | 461 | label = arg; | 
|  | 462 | } else { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 463 | LOG(ERROR) << "too many arguments"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 464 | return usage(); | 
|  | 465 | } | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | if (!source_path) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 469 | LOG(ERROR) << "no source path specified"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 470 | return usage(); | 
|  | 471 | } | 
|  | 472 | if (!label) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 473 | LOG(ERROR) << "no label specified"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 474 | return usage(); | 
|  | 475 | } | 
|  | 476 | if (!uid || !gid) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 477 | LOG(ERROR) << "uid and gid must be nonzero"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 478 | return usage(); | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | rlim.rlim_cur = 8192; | 
|  | 482 | rlim.rlim_max = 8192; | 
|  | 483 | if (setrlimit(RLIMIT_NOFILE, &rlim)) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 484 | PLOG(ERROR) << "setting RLIMIT_NOFILE failed"; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 485 | } | 
|  | 486 |  | 
|  | 487 | while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) { | 
| Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 488 | LOG(ERROR) << "installd fs upgrade not yet complete; waiting..."; | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 489 | sleep(1); | 
|  | 490 | } | 
|  | 491 |  | 
| Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 492 | if (should_use_sdcardfs()) { | 
|  | 493 | run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write); | 
|  | 494 | } else { | 
|  | 495 | run(source_path, label, uid, gid, userid, multi_user, full_write); | 
|  | 496 | } | 
| Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 497 | return 1; | 
|  | 498 | } |