sdcard-old.cpp 12 KB

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