AppFuseUtil.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "AppFuseUtil.h"
  17. #include <sys/mount.h>
  18. #include <utils/Errors.h>
  19. #include <android-base/logging.h>
  20. #include <android-base/stringprintf.h>
  21. #include "Utils.h"
  22. using android::base::StringPrintf;
  23. namespace android {
  24. namespace vold {
  25. namespace {
  26. static size_t kAppFuseMaxMountPointName = 32;
  27. static android::status_t GetMountPath(uid_t uid, const std::string& name, std::string* path) {
  28. if (name.size() > kAppFuseMaxMountPointName) {
  29. LOG(ERROR) << "AppFuse mount name is too long.";
  30. return -EINVAL;
  31. }
  32. for (size_t i = 0; i < name.size(); i++) {
  33. if (!isalnum(name[i])) {
  34. LOG(ERROR) << "AppFuse mount name contains invalid character.";
  35. return -EINVAL;
  36. }
  37. }
  38. *path = StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str());
  39. return android::OK;
  40. }
  41. static android::status_t Mount(int device_fd, const std::string& path) {
  42. const auto opts = StringPrintf(
  43. "fd=%i,"
  44. "rootmode=40000,"
  45. "default_permissions,"
  46. "allow_other,"
  47. "user_id=0,group_id=0,"
  48. "context=\"u:object_r:app_fuse_file:s0\","
  49. "fscontext=u:object_r:app_fusefs:s0",
  50. device_fd);
  51. const int result =
  52. TEMP_FAILURE_RETRY(mount("/dev/fuse", path.c_str(), "fuse",
  53. MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()));
  54. if (result != 0) {
  55. PLOG(ERROR) << "Failed to mount " << path;
  56. return -errno;
  57. }
  58. return android::OK;
  59. }
  60. static android::status_t RunCommand(const std::string& command, uid_t uid, const std::string& path,
  61. int device_fd) {
  62. if (DEBUG_APPFUSE) {
  63. LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path << " and uid "
  64. << uid;
  65. }
  66. if (command == "mount") {
  67. return Mount(device_fd, path);
  68. } else if (command == "unmount") {
  69. // If it's just after all FD opened on mount point are closed, umount2 can fail with
  70. // EBUSY. To avoid the case, specify MNT_DETACH.
  71. if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && errno != EINVAL &&
  72. errno != ENOENT) {
  73. PLOG(ERROR) << "Failed to unmount directory.";
  74. return -errno;
  75. }
  76. if (rmdir(path.c_str()) != 0) {
  77. PLOG(ERROR) << "Failed to remove the mount directory.";
  78. return -errno;
  79. }
  80. return android::OK;
  81. } else {
  82. LOG(ERROR) << "Unknown appfuse command " << command;
  83. return -EPERM;
  84. }
  85. return android::OK;
  86. }
  87. } // namespace
  88. int MountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd) {
  89. std::string name = std::to_string(mountId);
  90. // Check mount point name.
  91. std::string path;
  92. if (GetMountPath(uid, name, &path) != android::OK) {
  93. LOG(ERROR) << "Invalid mount point name";
  94. return -1;
  95. }
  96. // Forcibly remove the existing mount before we attempt to prepare the
  97. // directory. If we have a dangling mount, then PrepareDir may fail if the
  98. // indirection to FUSE doesn't work.
  99. android::vold::ForceUnmount(path);
  100. // Create directories.
  101. const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0);
  102. if (result != android::OK) {
  103. PLOG(ERROR) << "Failed to prepare directory " << path;
  104. return -1;
  105. }
  106. // Open device FD.
  107. // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
  108. device_fd->reset(open("/dev/fuse", O_RDWR));
  109. if (device_fd->get() == -1) {
  110. PLOG(ERROR) << "Failed to open /dev/fuse";
  111. return -1;
  112. }
  113. // Mount.
  114. return RunCommand("mount", uid, path, device_fd->get());
  115. }
  116. int UnmountAppFuse(uid_t uid, int mountId) {
  117. std::string name = std::to_string(mountId);
  118. // Check mount point name.
  119. std::string path;
  120. if (GetMountPath(uid, name, &path) != android::OK) {
  121. LOG(ERROR) << "Invalid mount point name";
  122. return -1;
  123. }
  124. return RunCommand("unmount", uid, path, -1 /* device_fd */);
  125. }
  126. int OpenAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
  127. std::string name = std::to_string(mountId);
  128. // Check mount point name.
  129. std::string mountPoint;
  130. if (GetMountPath(uid, name, &mountPoint) != android::OK) {
  131. LOG(ERROR) << "Invalid mount point name";
  132. return -1;
  133. }
  134. std::string path = StringPrintf("%s/%d", mountPoint.c_str(), fileId);
  135. return TEMP_FAILURE_RETRY(open(path.c_str(), flags));
  136. }
  137. } // namespace vold
  138. } // namespace android