blob: 622de5b7a144b6969ebbde98ca7f6aceb1ffb5cd [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>
Sudheer Shankacabc5262019-03-05 14:38:45 -080030#include <vector>
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040031
Daniel Rosenberg95268192016-08-26 16:54:46 -070032#include <android-base/file.h>
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040033#include <android-base/logging.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040034#include <android-base/macros.h>
Daniel Rosenberg95268192016-08-26 16:54:46 -070035#include <android-base/stringprintf.h>
36#include <android-base/strings.h>
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -040037
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040038#include <cutils/fs.h>
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040039#include <cutils/multiuser.h>
Daniel Rosenberg95268192016-08-26 16:54:46 -070040#include <cutils/properties.h>
41
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -040042#include <libminijail.h>
43#include <scoped_minijail.h>
44
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040045#include <private/android_filesystem_config.h>
46
Daniel Rosenbergc336f862018-01-17 18:11:20 -080047#define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs"
48#define PROP_SDCARDFS_USER "persist.sys.sdcardfs"
49
50static bool supports_esdfs(void) {
51 std::string filesystems;
52 if (!android::base::ReadFileToString("/proc/filesystems", &filesystems)) {
53 PLOG(ERROR) << "Could not read /proc/filesystems";
54 return false;
55 }
56 for (const auto& fs : android::base::Split(filesystems, "\n")) {
57 if (fs.find("esdfs") != std::string::npos) return true;
58 }
59 return false;
60}
61
62static bool should_use_sdcardfs(void) {
63 char property[PROPERTY_VALUE_MAX];
64
65 // Allow user to have a strong opinion about state
66 property_get(PROP_SDCARDFS_USER, property, "");
67 if (!strcmp(property, "force_on")) {
68 LOG(WARNING) << "User explicitly enabled sdcardfs";
69 return true;
70 } else if (!strcmp(property, "force_off")) {
71 LOG(WARNING) << "User explicitly disabled sdcardfs";
72 return !supports_esdfs();
73 }
74
75 // Fall back to device opinion about state
76 if (property_get_bool(PROP_SDCARDFS_DEVICE, true)) {
77 LOG(WARNING) << "Device explicitly enabled sdcardfs";
78 return true;
79 } else {
80 LOG(WARNING) << "Device explicitly disabled sdcardfs";
81 return !supports_esdfs();
82 }
83}
84
Jeff Sharkey68e10932018-01-08 11:22:38 -070085// NOTE: This is a vestigial program that simply exists to mount the in-kernel
86// sdcardfs filesystem. The older FUSE-based design that used to live here has
87// been completely removed to avoid confusion.
Daniel Rosenberg95268192016-08-26 16:54:46 -070088
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040089/* Supplementary groups to execute with. */
90static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
91
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -040092static void drop_privs(uid_t uid, gid_t gid) {
93 ScopedMinijail j(minijail_new());
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040094 minijail_set_supplementary_gids(j.get(), arraysize(kGroups), kGroups);
Jorge Lucangeli Obesc96f53e2016-07-14 14:50:14 -040095 minijail_change_gid(j.get(), gid);
96 minijail_change_uid(j.get(), uid);
97 /* minijail_enter() will abort if priv-dropping fails. */
98 minijail_enter(j.get());
99}
100
Rom Lemarchand98139142017-09-15 18:47:50 +0000101static bool sdcardfs_setup(const std::string& source_path, const std::string& dest_path,
102 uid_t fsuid, gid_t fsgid, bool multi_user, userid_t userid, gid_t gid,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800103 mode_t mask, bool derive_gid, bool default_normal, bool unshared_obb,
104 bool use_esdfs) {
105 // Add new options at the end of the vector.
106 std::vector<std::string> new_opts_list;
107 if (multi_user) new_opts_list.push_back("multiuser,");
108 if (derive_gid) new_opts_list.push_back("derive_gid,");
109 if (default_normal) new_opts_list.push_back("default_normal,");
110 if (unshared_obb) new_opts_list.push_back("unshared_obb,");
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700111 // Try several attempts, each time with one less option, to gracefully
112 // handle older kernels that aren't updated yet.
Sudheer Shankacabc5262019-03-05 14:38:45 -0800113 for (int i = 0; i <= new_opts_list.size(); ++i) {
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700114 std::string new_opts;
Sudheer Shankacabc5262019-03-05 14:38:45 -0800115 for (int j = 0; j < new_opts_list.size() - i; ++j) {
116 new_opts += new_opts_list[j];
117 }
Daniel Rosenberg95268192016-08-26 16:54:46 -0700118
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700119 auto opts = android::base::StringPrintf("fsuid=%d,fsgid=%d,%smask=%d,userid=%d,gid=%d",
120 fsuid, fsgid, new_opts.c_str(), mask, userid, gid);
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800121 if (mount(source_path.c_str(), dest_path.c_str(), use_esdfs ? "esdfs" : "sdcardfs",
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700122 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()) == -1) {
123 PLOG(WARNING) << "Failed to mount sdcardfs with options " << opts;
Rom Lemarchand98139142017-09-15 18:47:50 +0000124 } else {
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700125 return true;
Rom Lemarchand98139142017-09-15 18:47:50 +0000126 }
Daniel Rosenberg95268192016-08-26 16:54:46 -0700127 }
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700128
129 return false;
Daniel Rosenberg95268192016-08-26 16:54:46 -0700130}
131
Daniel Rosenbergfc592322016-10-27 17:43:37 -0700132static bool sdcardfs_setup_bind_remount(const std::string& source_path, const std::string& dest_path,
133 gid_t gid, mode_t mask) {
134 std::string opts = android::base::StringPrintf("mask=%d,gid=%d", mask, gid);
135
136 if (mount(source_path.c_str(), dest_path.c_str(), nullptr,
137 MS_BIND, nullptr) != 0) {
138 PLOG(ERROR) << "failed to bind mount sdcardfs filesystem";
139 return false;
140 }
141
142 if (mount(source_path.c_str(), dest_path.c_str(), "none",
143 MS_REMOUNT | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()) != 0) {
144 PLOG(ERROR) << "failed to mount sdcardfs filesystem";
145 if (umount2(dest_path.c_str(), MNT_DETACH))
146 PLOG(WARNING) << "Failed to unmount bind";
147 return false;
148 }
149
150 return true;
151}
152
Sudheer Shankacabc5262019-03-05 14:38:45 -0800153static bool sdcardfs_setup_secondary(const std::string& default_path,
154 const std::string& source_path, const std::string& dest_path,
155 uid_t fsuid, gid_t fsgid, bool multi_user, userid_t userid,
156 gid_t gid, mode_t mask, bool derive_gid, bool default_normal,
157 bool unshared_obb, bool use_esdfs) {
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800158 if (use_esdfs) {
159 return sdcardfs_setup(source_path, dest_path, fsuid, fsgid, multi_user, userid, gid, mask,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800160 derive_gid, default_normal, unshared_obb, use_esdfs);
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800161 } else {
162 return sdcardfs_setup_bind_remount(default_path, dest_path, gid, mask);
163 }
164}
165
Daniel Rosenberg95268192016-08-26 16:54:46 -0700166static void run_sdcardfs(const std::string& source_path, const std::string& label, uid_t uid,
Rom Lemarchand98139142017-09-15 18:47:50 +0000167 gid_t gid, userid_t userid, bool multi_user, bool full_write,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800168 bool derive_gid, bool default_normal, bool unshared_obb, bool use_esdfs) {
Daniel Rosenberg95268192016-08-26 16:54:46 -0700169 std::string dest_path_default = "/mnt/runtime/default/" + label;
170 std::string dest_path_read = "/mnt/runtime/read/" + label;
171 std::string dest_path_write = "/mnt/runtime/write/" + label;
Sudheer Shanka81c687d2019-01-16 23:25:28 -0800172 std::string dest_path_full = "/mnt/runtime/full/" + label;
Daniel Rosenberg95268192016-08-26 16:54:46 -0700173
174 umask(0);
175 if (multi_user) {
176 // Multi-user storage is fully isolated per user, so "other"
177 // permissions are completely masked off.
178 if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800179 AID_SDCARD_RW, 0006, derive_gid, default_normal, unshared_obb,
180 use_esdfs) ||
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800181 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid,
182 multi_user, userid, AID_EVERYBODY, 0027, derive_gid,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800183 default_normal, unshared_obb, use_esdfs) ||
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800184 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid,
185 multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0027,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800186 derive_gid, default_normal, unshared_obb, use_esdfs) ||
Sudheer Shanka81c687d2019-01-16 23:25:28 -0800187 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_full, uid, gid,
188 multi_user, userid, AID_EVERYBODY, 0007, derive_gid,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800189 default_normal, unshared_obb, use_esdfs)) {
Daniel Rosenberg95268192016-08-26 16:54:46 -0700190 LOG(FATAL) << "failed to sdcardfs_setup";
191 }
192 } else {
193 // Physical storage is readable by all users on device, but
194 // the Android directories are masked off to a single user
195 // deep inside attr_from_stat().
196 if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800197 AID_SDCARD_RW, 0006, derive_gid, default_normal, unshared_obb,
198 use_esdfs) ||
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800199 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid,
200 multi_user, userid, AID_EVERYBODY, full_write ? 0027 : 0022,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800201 derive_gid, default_normal, unshared_obb, use_esdfs) ||
Daniel Rosenbergc336f862018-01-17 18:11:20 -0800202 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid,
203 multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0022,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800204 derive_gid, default_normal, unshared_obb, use_esdfs) ||
Sudheer Shanka81c687d2019-01-16 23:25:28 -0800205 !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_full, uid, gid,
206 multi_user, userid, AID_EVERYBODY, 0007, derive_gid,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800207 default_normal, unshared_obb, use_esdfs)) {
Daniel Rosenberg95268192016-08-26 16:54:46 -0700208 LOG(FATAL) << "failed to sdcardfs_setup";
209 }
210 }
211
212 // Will abort if priv-dropping fails.
213 drop_privs(uid, gid);
214
215 if (multi_user) {
216 std::string obb_path = source_path + "/obb";
217 fs_prepare_dir(obb_path.c_str(), 0775, uid, gid);
218 }
219
220 exit(0);
221}
222
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400223static int usage() {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400224 LOG(ERROR) << "usage: sdcard [OPTIONS] <source_path> <label>"
225 << " -u: specify UID to run as"
226 << " -g: specify GID to run as"
227 << " -U: specify user ID that owns device"
228 << " -m: source_path is multi-user"
Rom Lemarchand98139142017-09-15 18:47:50 +0000229 << " -w: runtime write mount has full write access"
Sudheer Shankacabc5262019-03-05 14:38:45 -0800230 << " -P: preserve owners on the lower file system"
231 << " -o: obb dir doesn't need to be shared between users";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400232 return 1;
233}
234
235int main(int argc, char **argv) {
236 const char *source_path = NULL;
237 const char *label = NULL;
238 uid_t uid = 0;
239 gid_t gid = 0;
240 userid_t userid = 0;
241 bool multi_user = false;
242 bool full_write = false;
Rom Lemarchand98139142017-09-15 18:47:50 +0000243 bool derive_gid = false;
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700244 bool default_normal = false;
Sudheer Shankacabc5262019-03-05 14:38:45 -0800245 bool unshared_obb = false;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400246 int i;
247 struct rlimit rlim;
248 int fs_version;
249
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700250 setenv("ANDROID_LOG_TAGS", "*:v", 1);
251 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
252
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400253 int opt;
Sudheer Shankacabc5262019-03-05 14:38:45 -0800254 while ((opt = getopt(argc, argv, "u:g:U:mwGio")) != -1) {
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400255 switch (opt) {
256 case 'u':
257 uid = strtoul(optarg, NULL, 10);
258 break;
259 case 'g':
260 gid = strtoul(optarg, NULL, 10);
261 break;
262 case 'U':
263 userid = strtoul(optarg, NULL, 10);
264 break;
265 case 'm':
266 multi_user = true;
267 break;
268 case 'w':
269 full_write = true;
270 break;
Rom Lemarchand98139142017-09-15 18:47:50 +0000271 case 'G':
272 derive_gid = true;
273 break;
Jeff Sharkeye2e36102018-01-08 11:43:46 -0700274 case 'i':
275 default_normal = true;
276 break;
Sudheer Shankacabc5262019-03-05 14:38:45 -0800277 case 'o':
278 unshared_obb = true;
279 break;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400280 case '?':
281 default:
Sudheer Shankacabc5262019-03-05 14:38:45 -0800282 LOG(ERROR) << "Unknown option: '" << opt << "'";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400283 return usage();
284 }
285 }
286
287 for (i = optind; i < argc; i++) {
288 char* arg = argv[i];
289 if (!source_path) {
290 source_path = arg;
291 } else if (!label) {
292 label = arg;
293 } else {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400294 LOG(ERROR) << "too many arguments";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400295 return usage();
296 }
297 }
298
299 if (!source_path) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400300 LOG(ERROR) << "no source path specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400301 return usage();
302 }
303 if (!label) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400304 LOG(ERROR) << "no label specified";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400305 return usage();
306 }
307 if (!uid || !gid) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400308 LOG(ERROR) << "uid and gid must be nonzero";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400309 return usage();
310 }
311
312 rlim.rlim_cur = 8192;
313 rlim.rlim_max = 8192;
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700314 if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400315 PLOG(ERROR) << "setting RLIMIT_NOFILE failed";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400316 }
317
Paul Crowley26aabfd2019-08-16 12:39:17 -0700318 while ((fs_read_atomic_int("/data/misc/installd/layout_version", &fs_version) == -1) ||
319 (fs_version < 3)) {
Jorge Lucangeli Obesc9e17102016-07-12 17:05:32 -0400320 LOG(ERROR) << "installd fs upgrade not yet complete; waiting...";
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400321 sleep(1);
322 }
323
Jeff Sharkey68e10932018-01-08 11:22:38 -0700324 run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write, derive_gid,
Sudheer Shankacabc5262019-03-05 14:38:45 -0800325 default_normal, unshared_obb, !should_use_sdcardfs());
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400326 return 1;
327}