• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <vector>
31 
32 #include <android-base/file.h>
33 #include <android-base/logging.h>
34 #include <android-base/macros.h>
35 #include <android-base/stringprintf.h>
36 #include <android-base/strings.h>
37 
38 #include <cutils/fs.h>
39 #include <cutils/multiuser.h>
40 #include <cutils/properties.h>
41 
42 #include <libminijail.h>
43 #include <scoped_minijail.h>
44 
45 #include <private/android_filesystem_config.h>
46 
47 #define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs"
48 #define PROP_SDCARDFS_USER "persist.sys.sdcardfs"
49 
supports_esdfs(void)50 static 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 
should_use_sdcardfs(void)62 static 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 
85 // 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.
88 
89 /* Supplementary groups to execute with. */
90 static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
91 
drop_privs(uid_t uid,gid_t gid)92 static void drop_privs(uid_t uid, gid_t gid) {
93     ScopedMinijail j(minijail_new());
94     minijail_set_supplementary_gids(j.get(), arraysize(kGroups), kGroups);
95     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 
sdcardfs_setup(const std::string & source_path,const std::string & dest_path,uid_t fsuid,gid_t fsgid,bool multi_user,userid_t userid,gid_t gid,mode_t mask,bool derive_gid,bool default_normal,bool unshared_obb,bool use_esdfs)101 static 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,
103                            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,");
111     // Try several attempts, each time with one less option, to gracefully
112     // handle older kernels that aren't updated yet.
113     for (int i = 0; i <= new_opts_list.size(); ++i) {
114         std::string new_opts;
115         for (int j = 0; j < new_opts_list.size() - i; ++j) {
116             new_opts += new_opts_list[j];
117         }
118 
119         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);
121         if (mount(source_path.c_str(), dest_path.c_str(), use_esdfs ? "esdfs" : "sdcardfs",
122                   MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()) == -1) {
123             PLOG(WARNING) << "Failed to mount sdcardfs with options " << opts;
124         } else {
125             return true;
126         }
127     }
128 
129     return false;
130 }
131 
sdcardfs_setup_bind_remount(const std::string & source_path,const std::string & dest_path,gid_t gid,mode_t mask)132 static 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 
sdcardfs_setup_secondary(const std::string & default_path,const std::string & source_path,const std::string & dest_path,uid_t fsuid,gid_t fsgid,bool multi_user,userid_t userid,gid_t gid,mode_t mask,bool derive_gid,bool default_normal,bool unshared_obb,bool use_esdfs)153 static 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) {
158     if (use_esdfs) {
159         return sdcardfs_setup(source_path, dest_path, fsuid, fsgid, multi_user, userid, gid, mask,
160                               derive_gid, default_normal, unshared_obb, use_esdfs);
161     } else {
162         return sdcardfs_setup_bind_remount(default_path, dest_path, gid, mask);
163     }
164 }
165 
run_sdcardfs(const std::string & source_path,const std::string & label,uid_t uid,gid_t gid,userid_t userid,bool multi_user,bool full_write,bool derive_gid,bool default_normal,bool unshared_obb,bool use_esdfs)166 static void run_sdcardfs(const std::string& source_path, const std::string& label, uid_t uid,
167                          gid_t gid, userid_t userid, bool multi_user, bool full_write,
168                          bool derive_gid, bool default_normal, bool unshared_obb, bool use_esdfs) {
169     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;
172     std::string dest_path_full = "/mnt/runtime/full/" + label;
173 
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,
179                             AID_SDCARD_RW, 0006, derive_gid, default_normal, unshared_obb,
180                             use_esdfs) ||
181             !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid,
182                                       multi_user, userid, AID_EVERYBODY, 0027, derive_gid,
183                                       default_normal, unshared_obb, use_esdfs) ||
184             !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid,
185                                       multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0027,
186                                       derive_gid, default_normal, unshared_obb, use_esdfs) ||
187             !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_full, uid, gid,
188                                       multi_user, userid, AID_EVERYBODY, 0007, derive_gid,
189                                       default_normal, unshared_obb, use_esdfs)) {
190             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,
197                             AID_SDCARD_RW, 0006, derive_gid, default_normal, unshared_obb,
198                             use_esdfs) ||
199             !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid,
200                                       multi_user, userid, AID_EVERYBODY, full_write ? 0027 : 0022,
201                                       derive_gid, default_normal, unshared_obb, use_esdfs) ||
202             !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid,
203                                       multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0022,
204                                       derive_gid, default_normal, unshared_obb, use_esdfs) ||
205             !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_full, uid, gid,
206                                       multi_user, userid, AID_EVERYBODY, 0007, derive_gid,
207                                       default_normal, unshared_obb, use_esdfs)) {
208             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 
usage()223 static int usage() {
224     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"
229                << "    -w: runtime write mount has full write access"
230                << "    -P: preserve owners on the lower file system"
231                << "    -o: obb dir doesn't need to be shared between users";
232     return 1;
233 }
234 
main(int argc,char ** argv)235 int 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;
243     bool derive_gid = false;
244     bool default_normal = false;
245     bool unshared_obb = false;
246     int i;
247     struct rlimit rlim;
248     int fs_version;
249 
250     setenv("ANDROID_LOG_TAGS", "*:v", 1);
251     android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
252 
253     int opt;
254     while ((opt = getopt(argc, argv, "u:g:U:mwGio")) != -1) {
255         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;
271             case 'G':
272                 derive_gid = true;
273                 break;
274             case 'i':
275                 default_normal = true;
276                 break;
277             case 'o':
278                 unshared_obb = true;
279                 break;
280             case '?':
281             default:
282                 LOG(ERROR) << "Unknown option: '" << opt << "'";
283                 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 {
294             LOG(ERROR) << "too many arguments";
295             return usage();
296         }
297     }
298 
299     if (!source_path) {
300         LOG(ERROR) << "no source path specified";
301         return usage();
302     }
303     if (!label) {
304         LOG(ERROR) << "no label specified";
305         return usage();
306     }
307     if (!uid || !gid) {
308         LOG(ERROR) << "uid and gid must be nonzero";
309         return usage();
310     }
311 
312     rlim.rlim_cur = 8192;
313     rlim.rlim_max = 8192;
314     if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) {
315         PLOG(ERROR) << "setting RLIMIT_NOFILE failed";
316     }
317 
318     while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
319         LOG(ERROR) << "installd fs upgrade not yet complete; waiting...";
320         sleep(1);
321     }
322 
323     run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write, derive_gid,
324                  default_normal, unshared_obb, !should_use_sdcardfs());
325     return 1;
326 }
327