blob: e1de130c2e98de47521d3702d1c44e0b86e82cdb [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
Daniel Rosenberg95268192016-08-26 16:54:46 -070031#include <android-base/file.h>
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040032#include <android-base/logging.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040033#include <android-base/macros.h>
Daniel Rosenberg95268192016-08-26 16:54:46 -070034#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040036
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040037#include <cutils/fs.h>
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040038#include <cutils/multiuser.h>
Daniel Rosenberg95268192016-08-26 16:54:46 -070039#include <cutils/properties.h>
40
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -040041#include <libminijail.h>
42#include <scoped_minijail.h>
43
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040044#include <private/android_filesystem_config.h>
45
Daniel Rosenbergc336f862018-01-17 18:11:20 -080046#define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs"
47#define PROP_SDCARDFS_USER "persist.sys.sdcardfs"
48
49static 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
61static 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 Sharkey68e10932018-01-08 11:22:38 -070084// 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 Rosenberg95268192016-08-26 16:54:46 -070087
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040088/* Supplementary groups to execute with. */
89static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
90
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -040091static void drop_privs(uid_t uid, gid_t gid) {
92 ScopedMinijail j(minijail_new());
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040093 minijail_set_supplementary_gids(j.get(), arraysize(kGroups), kGroups);
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -040094 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 Lemarchand98139142017-09-15 18:47:50 +0000100static 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 Rosenbergc336f862018-01-17 18:11:20 -0800102 mode_t mask, bool derive_gid, bool default_normal, bool use_esdfs) {
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700103 // 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 Rosenberg95268192016-08-26 16:54:46 -0700110
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700111 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 Rosenbergc336f862018-01-17 18:11:20 -0800113 if (mount(source_path.c_str(), dest_path.c_str(), use_esdfs ? "esdfs" : "sdcardfs",
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700114 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()) == -1) {
115 PLOG(WARNING) << "Failed to mount sdcardfs with options " << opts;
Rom Lemarchand98139142017-09-15 18:47:50 +0000116 } else {
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700117 return true;
Rom Lemarchand98139142017-09-15 18:47:50 +0000118 }
Daniel Rosenberg95268192016-08-26 16:54:46 -0700119 }
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700120
121 return false;
Daniel Rosenberg95268192016-08-26 16:54:46 -0700122}
123
Daniel Rosenbergfc592322016-10-27 17:43:37 -0700124static 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 Rosenbergc336f862018-01-17 18:11:20 -0800145static 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 Rosenberg95268192016-08-26 16:54:46 -0700157static void run_sdcardfs(const std::string& source_path, const std::string& label, uid_t uid,
Rom Lemarchand98139142017-09-15 18:47:50 +0000158 gid_t gid, userid_t userid, bool multi_user, bool full_write,
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800159 bool derive_gid, bool default_normal, bool use_esdfs) {
Daniel Rosenberg95268192016-08-26 16:54:46 -0700160 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;
Sudheer Shanka81c687d2019-01-16 23:25:28 -0800163 std::string dest_path_full = "/mnt/runtime/full/" + label;
Daniel Rosenberg95268192016-08-26 16:54:46 -0700164
165 umask(0);
166 if (multi_user) {
167 // Multi-user storage is fully isolated per user, so "other"
168 // permissions are completely masked off.
169 if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800170 AID_SDCARD_RW, 0006, derive_gid, default_normal, use_esdfs) ||
171 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid,
172 multi_user, userid, AID_EVERYBODY, 0027, derive_gid,
173 default_normal, use_esdfs) ||
174 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid,
175 multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0027,
Sudheer Shanka81c687d2019-01-16 23:25:28 -0800176 derive_gid, default_normal, use_esdfs) ||
177 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_full, uid, gid,
178 multi_user, userid, AID_EVERYBODY, 0007, derive_gid,
179 default_normal, use_esdfs)) {
Daniel Rosenberg95268192016-08-26 16:54:46 -0700180 LOG(FATAL) << "failed to sdcardfs_setup";
181 }
182 } else {
183 // Physical storage is readable by all users on device, but
184 // the Android directories are masked off to a single user
185 // deep inside attr_from_stat().
186 if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800187 AID_SDCARD_RW, 0006, derive_gid, default_normal, use_esdfs) ||
188 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid,
189 multi_user, userid, AID_EVERYBODY, full_write ? 0027 : 0022,
190 derive_gid, default_normal, use_esdfs) ||
191 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid,
192 multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0022,
Sudheer Shanka81c687d2019-01-16 23:25:28 -0800193 derive_gid, default_normal, use_esdfs) ||
194 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_full, uid, gid,
195 multi_user, userid, AID_EVERYBODY, 0007, derive_gid,
196 default_normal, use_esdfs)) {
Daniel Rosenberg95268192016-08-26 16:54:46 -0700197 LOG(FATAL) << "failed to sdcardfs_setup";
198 }
199 }
200
201 // Will abort if priv-dropping fails.
202 drop_privs(uid, gid);
203
204 if (multi_user) {
205 std::string obb_path = source_path + "/obb";
206 fs_prepare_dir(obb_path.c_str(), 0775, uid, gid);
207 }
208
209 exit(0);
210}
211
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400212static int usage() {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400213 LOG(ERROR) << "usage: sdcard [OPTIONS] <source_path> <label>"
214 << " -u: specify UID to run as"
215 << " -g: specify GID to run as"
216 << " -U: specify user ID that owns device"
217 << " -m: source_path is multi-user"
Rom Lemarchand98139142017-09-15 18:47:50 +0000218 << " -w: runtime write mount has full write access"
219 << " -P preserve owners on the lower file system";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400220 return 1;
221}
222
223int main(int argc, char **argv) {
224 const char *source_path = NULL;
225 const char *label = NULL;
226 uid_t uid = 0;
227 gid_t gid = 0;
228 userid_t userid = 0;
229 bool multi_user = false;
230 bool full_write = false;
Rom Lemarchand98139142017-09-15 18:47:50 +0000231 bool derive_gid = false;
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700232 bool default_normal = false;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400233 int i;
234 struct rlimit rlim;
235 int fs_version;
236
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700237 setenv("ANDROID_LOG_TAGS", "*:v", 1);
238 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
239
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400240 int opt;
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700241 while ((opt = getopt(argc, argv, "u:g:U:mwGi")) != -1) {
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400242 switch (opt) {
243 case 'u':
244 uid = strtoul(optarg, NULL, 10);
245 break;
246 case 'g':
247 gid = strtoul(optarg, NULL, 10);
248 break;
249 case 'U':
250 userid = strtoul(optarg, NULL, 10);
251 break;
252 case 'm':
253 multi_user = true;
254 break;
255 case 'w':
256 full_write = true;
257 break;
Rom Lemarchand98139142017-09-15 18:47:50 +0000258 case 'G':
259 derive_gid = true;
260 break;
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700261 case 'i':
262 default_normal = true;
263 break;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400264 case '?':
265 default:
266 return usage();
267 }
268 }
269
270 for (i = optind; i < argc; i++) {
271 char* arg = argv[i];
272 if (!source_path) {
273 source_path = arg;
274 } else if (!label) {
275 label = arg;
276 } else {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400277 LOG(ERROR) << "too many arguments";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400278 return usage();
279 }
280 }
281
282 if (!source_path) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400283 LOG(ERROR) << "no source path specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400284 return usage();
285 }
286 if (!label) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400287 LOG(ERROR) << "no label specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400288 return usage();
289 }
290 if (!uid || !gid) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400291 LOG(ERROR) << "uid and gid must be nonzero";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400292 return usage();
293 }
294
295 rlim.rlim_cur = 8192;
296 rlim.rlim_max = 8192;
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700297 if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400298 PLOG(ERROR) << "setting RLIMIT_NOFILE failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400299 }
300
301 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400302 LOG(ERROR) << "installd fs upgrade not yet complete; waiting...";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400303 sleep(1);
304 }
305
Jeff Sharkey68e10932018-01-08 11:22:38 -0700306 run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write, derive_gid,
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800307 default_normal, !should_use_sdcardfs());
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400308 return 1;
309}