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 | c96f53e | 2016-07-14 14:50:14 -0400 | [diff] [blame] | 41 | #include <libminijail.h> |
| 42 | #include <scoped_minijail.h> |
| 43 | |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 44 | #include <private/android_filesystem_config.h> |
| 45 | |
Daniel Rosenberg | c336f86 | 2018-01-17 18:11:20 -0800 | [diff] [blame] | 46 | #define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs" |
| 47 | #define PROP_SDCARDFS_USER "persist.sys.sdcardfs" |
| 48 | |
| 49 | static bool supports_esdfs(void) { |
| 50 | std::string filesystems; |
| 51 | if (!android::base::ReadFileToString("/proc/filesystems", &filesystems)) { |
| 52 | PLOG(ERROR) << "Could not read /proc/filesystems"; |
| 53 | return false; |
| 54 | } |
| 55 | for (const auto& fs : android::base::Split(filesystems, "\n")) { |
| 56 | if (fs.find("esdfs") != std::string::npos) return true; |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | static bool should_use_sdcardfs(void) { |
| 62 | char property[PROPERTY_VALUE_MAX]; |
| 63 | |
| 64 | // Allow user to have a strong opinion about state |
| 65 | property_get(PROP_SDCARDFS_USER, property, ""); |
| 66 | if (!strcmp(property, "force_on")) { |
| 67 | LOG(WARNING) << "User explicitly enabled sdcardfs"; |
| 68 | return true; |
| 69 | } else if (!strcmp(property, "force_off")) { |
| 70 | LOG(WARNING) << "User explicitly disabled sdcardfs"; |
| 71 | return !supports_esdfs(); |
| 72 | } |
| 73 | |
| 74 | // Fall back to device opinion about state |
| 75 | if (property_get_bool(PROP_SDCARDFS_DEVICE, true)) { |
| 76 | LOG(WARNING) << "Device explicitly enabled sdcardfs"; |
| 77 | return true; |
| 78 | } else { |
| 79 | LOG(WARNING) << "Device explicitly disabled sdcardfs"; |
| 80 | return !supports_esdfs(); |
| 81 | } |
| 82 | } |
| 83 | |
Jeff Sharkey | 68e1093 | 2018-01-08 11:22:38 -0700 | [diff] [blame] | 84 | // NOTE: This is a vestigial program that simply exists to mount the in-kernel |
| 85 | // sdcardfs filesystem. The older FUSE-based design that used to live here has |
| 86 | // been completely removed to avoid confusion. |
Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 87 | |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 88 | /* Supplementary groups to execute with. */ |
| 89 | static const gid_t kGroups[1] = { AID_PACKAGE_INFO }; |
| 90 | |
Jorge Lucangeli Obes | c96f53e | 2016-07-14 14:50:14 -0400 | [diff] [blame] | 91 | static void drop_privs(uid_t uid, gid_t gid) { |
| 92 | ScopedMinijail j(minijail_new()); |
Jorge Lucangeli Obes | bae15b4 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 93 | minijail_set_supplementary_gids(j.get(), arraysize(kGroups), kGroups); |
Jorge Lucangeli Obes | c96f53e | 2016-07-14 14:50:14 -0400 | [diff] [blame] | 94 | minijail_change_gid(j.get(), gid); |
| 95 | minijail_change_uid(j.get(), uid); |
| 96 | /* minijail_enter() will abort if priv-dropping fails. */ |
| 97 | minijail_enter(j.get()); |
| 98 | } |
| 99 | |
Rom Lemarchand | 9813914 | 2017-09-15 18:47:50 +0000 | [diff] [blame] | 100 | static bool sdcardfs_setup(const std::string& source_path, const std::string& dest_path, |
| 101 | uid_t fsuid, gid_t fsgid, bool multi_user, userid_t userid, gid_t gid, |
Daniel Rosenberg | c336f86 | 2018-01-17 18:11:20 -0800 | [diff] [blame] | 102 | mode_t mask, bool derive_gid, bool default_normal, bool use_esdfs) { |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 103 | // Try several attempts, each time with one less option, to gracefully |
| 104 | // handle older kernels that aren't updated yet. |
| 105 | for (int i = 0; i < 4; i++) { |
| 106 | std::string new_opts; |
| 107 | if (multi_user && i < 3) new_opts += "multiuser,"; |
| 108 | if (derive_gid && i < 2) new_opts += "derive_gid,"; |
| 109 | if (default_normal && i < 1) new_opts += "default_normal,"; |
Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 110 | |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 111 | auto opts = android::base::StringPrintf("fsuid=%d,fsgid=%d,%smask=%d,userid=%d,gid=%d", |
| 112 | fsuid, fsgid, new_opts.c_str(), mask, userid, gid); |
Daniel Rosenberg | c336f86 | 2018-01-17 18:11:20 -0800 | [diff] [blame] | 113 | if (mount(source_path.c_str(), dest_path.c_str(), use_esdfs ? "esdfs" : "sdcardfs", |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 114 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()) == -1) { |
| 115 | PLOG(WARNING) << "Failed to mount sdcardfs with options " << opts; |
Rom Lemarchand | 9813914 | 2017-09-15 18:47:50 +0000 | [diff] [blame] | 116 | } else { |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 117 | return true; |
Rom Lemarchand | 9813914 | 2017-09-15 18:47:50 +0000 | [diff] [blame] | 118 | } |
Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 119 | } |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 120 | |
| 121 | return false; |
Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Daniel Rosenberg | fc59232 | 2016-10-27 17:43:37 -0700 | [diff] [blame] | 124 | static bool sdcardfs_setup_bind_remount(const std::string& source_path, const std::string& dest_path, |
| 125 | gid_t gid, mode_t mask) { |
| 126 | std::string opts = android::base::StringPrintf("mask=%d,gid=%d", mask, gid); |
| 127 | |
| 128 | if (mount(source_path.c_str(), dest_path.c_str(), nullptr, |
| 129 | MS_BIND, nullptr) != 0) { |
| 130 | PLOG(ERROR) << "failed to bind mount sdcardfs filesystem"; |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (mount(source_path.c_str(), dest_path.c_str(), "none", |
| 135 | MS_REMOUNT | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()) != 0) { |
| 136 | PLOG(ERROR) << "failed to mount sdcardfs filesystem"; |
| 137 | if (umount2(dest_path.c_str(), MNT_DETACH)) |
| 138 | PLOG(WARNING) << "Failed to unmount bind"; |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
Daniel Rosenberg | c336f86 | 2018-01-17 18:11:20 -0800 | [diff] [blame] | 145 | static bool sdcardfs_setup_secondary(const std::string& default_path, const std::string& source_path, |
| 146 | const std::string& dest_path, uid_t fsuid, gid_t fsgid, |
| 147 | bool multi_user, userid_t userid, gid_t gid, mode_t mask, |
| 148 | bool derive_gid, bool default_normal, bool use_esdfs) { |
| 149 | if (use_esdfs) { |
| 150 | return sdcardfs_setup(source_path, dest_path, fsuid, fsgid, multi_user, userid, gid, mask, |
| 151 | derive_gid, default_normal, use_esdfs); |
| 152 | } else { |
| 153 | return sdcardfs_setup_bind_remount(default_path, dest_path, gid, mask); |
| 154 | } |
| 155 | } |
| 156 | |
Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 157 | static void run_sdcardfs(const std::string& source_path, const std::string& label, uid_t uid, |
Rom Lemarchand | 9813914 | 2017-09-15 18:47:50 +0000 | [diff] [blame] | 158 | gid_t gid, userid_t userid, bool multi_user, bool full_write, |
Daniel Rosenberg | c336f86 | 2018-01-17 18:11:20 -0800 | [diff] [blame] | 159 | bool derive_gid, bool default_normal, bool use_esdfs) { |
Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 160 | std::string dest_path_default = "/mnt/runtime/default/" + label; |
| 161 | std::string dest_path_read = "/mnt/runtime/read/" + label; |
| 162 | std::string dest_path_write = "/mnt/runtime/write/" + label; |
| 163 | |
| 164 | umask(0); |
| 165 | if (multi_user) { |
| 166 | // Multi-user storage is fully isolated per user, so "other" |
| 167 | // permissions are completely masked off. |
| 168 | if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid, |
Daniel Rosenberg | c336f86 | 2018-01-17 18:11:20 -0800 | [diff] [blame] | 169 | AID_SDCARD_RW, 0006, derive_gid, default_normal, use_esdfs) || |
| 170 | !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid, |
| 171 | multi_user, userid, AID_EVERYBODY, 0027, derive_gid, |
| 172 | default_normal, use_esdfs) || |
| 173 | !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid, |
| 174 | multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0027, |
| 175 | derive_gid, default_normal, use_esdfs)) { |
Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 176 | LOG(FATAL) << "failed to sdcardfs_setup"; |
| 177 | } |
| 178 | } else { |
| 179 | // Physical storage is readable by all users on device, but |
| 180 | // the Android directories are masked off to a single user |
| 181 | // deep inside attr_from_stat(). |
| 182 | if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid, |
Daniel Rosenberg | c336f86 | 2018-01-17 18:11:20 -0800 | [diff] [blame] | 183 | AID_SDCARD_RW, 0006, derive_gid, default_normal, use_esdfs) || |
| 184 | !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid, |
| 185 | multi_user, userid, AID_EVERYBODY, full_write ? 0027 : 0022, |
| 186 | derive_gid, default_normal, use_esdfs) || |
| 187 | !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid, |
| 188 | multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0022, |
| 189 | derive_gid, default_normal, use_esdfs)) { |
Daniel Rosenberg | 9526819 | 2016-08-26 16:54:46 -0700 | [diff] [blame] | 190 | LOG(FATAL) << "failed to sdcardfs_setup"; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Will abort if priv-dropping fails. |
| 195 | drop_privs(uid, gid); |
| 196 | |
| 197 | if (multi_user) { |
| 198 | std::string obb_path = source_path + "/obb"; |
| 199 | fs_prepare_dir(obb_path.c_str(), 0775, uid, gid); |
| 200 | } |
| 201 | |
| 202 | exit(0); |
| 203 | } |
| 204 | |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 205 | static int usage() { |
Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 206 | LOG(ERROR) << "usage: sdcard [OPTIONS] <source_path> <label>" |
| 207 | << " -u: specify UID to run as" |
| 208 | << " -g: specify GID to run as" |
| 209 | << " -U: specify user ID that owns device" |
| 210 | << " -m: source_path is multi-user" |
Rom Lemarchand | 9813914 | 2017-09-15 18:47:50 +0000 | [diff] [blame] | 211 | << " -w: runtime write mount has full write access" |
| 212 | << " -P preserve owners on the lower file system"; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | int main(int argc, char **argv) { |
| 217 | const char *source_path = NULL; |
| 218 | const char *label = NULL; |
| 219 | uid_t uid = 0; |
| 220 | gid_t gid = 0; |
| 221 | userid_t userid = 0; |
| 222 | bool multi_user = false; |
| 223 | bool full_write = false; |
Rom Lemarchand | 9813914 | 2017-09-15 18:47:50 +0000 | [diff] [blame] | 224 | bool derive_gid = false; |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 225 | bool default_normal = false; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 226 | int i; |
| 227 | struct rlimit rlim; |
| 228 | int fs_version; |
| 229 | |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 230 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
| 231 | android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM)); |
| 232 | |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 233 | int opt; |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 234 | while ((opt = getopt(argc, argv, "u:g:U:mwGi")) != -1) { |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 235 | switch (opt) { |
| 236 | case 'u': |
| 237 | uid = strtoul(optarg, NULL, 10); |
| 238 | break; |
| 239 | case 'g': |
| 240 | gid = strtoul(optarg, NULL, 10); |
| 241 | break; |
| 242 | case 'U': |
| 243 | userid = strtoul(optarg, NULL, 10); |
| 244 | break; |
| 245 | case 'm': |
| 246 | multi_user = true; |
| 247 | break; |
| 248 | case 'w': |
| 249 | full_write = true; |
| 250 | break; |
Rom Lemarchand | 9813914 | 2017-09-15 18:47:50 +0000 | [diff] [blame] | 251 | case 'G': |
| 252 | derive_gid = true; |
| 253 | break; |
Jeff Sharkey | e2e3610 | 2018-01-08 11:43:46 -0700 | [diff] [blame] | 254 | case 'i': |
| 255 | default_normal = true; |
| 256 | break; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 257 | case '?': |
| 258 | default: |
| 259 | return usage(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | for (i = optind; i < argc; i++) { |
| 264 | char* arg = argv[i]; |
| 265 | if (!source_path) { |
| 266 | source_path = arg; |
| 267 | } else if (!label) { |
| 268 | label = arg; |
| 269 | } else { |
Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 270 | LOG(ERROR) << "too many arguments"; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 271 | return usage(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (!source_path) { |
Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 276 | LOG(ERROR) << "no source path specified"; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 277 | return usage(); |
| 278 | } |
| 279 | if (!label) { |
Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 280 | LOG(ERROR) << "no label specified"; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 281 | return usage(); |
| 282 | } |
| 283 | if (!uid || !gid) { |
Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 284 | LOG(ERROR) << "uid and gid must be nonzero"; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 285 | return usage(); |
| 286 | } |
| 287 | |
| 288 | rlim.rlim_cur = 8192; |
| 289 | rlim.rlim_max = 8192; |
Christopher Ferris | d6b0d37 | 2016-10-06 12:51:20 -0700 | [diff] [blame] | 290 | if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) { |
Jorge Lucangeli Obes | c9e1710 | 2016-07-12 17:05:32 -0400 | [diff] [blame] | 291 | PLOG(ERROR) << "setting RLIMIT_NOFILE failed"; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | 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] | 295 | LOG(ERROR) << "installd fs upgrade not yet complete; waiting..."; |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 296 | sleep(1); |
| 297 | } |
| 298 | |
Jeff Sharkey | 68e1093 | 2018-01-08 11:22:38 -0700 | [diff] [blame] | 299 | run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write, derive_gid, |
Daniel Rosenberg | c336f86 | 2018-01-17 18:11:20 -0800 | [diff] [blame] | 300 | default_normal, !should_use_sdcardfs()); |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 301 | return 1; |
| 302 | } |