Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame^] | 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 | |
| 17 | #include "AppFuseUtil.h" |
| 18 | |
| 19 | #include <sys/mount.h> |
| 20 | #include <utils/Errors.h> |
| 21 | |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/stringprintf.h> |
| 24 | |
| 25 | #include "Utils.h" |
| 26 | |
| 27 | using android::base::StringPrintf; |
| 28 | |
| 29 | namespace android { |
| 30 | namespace vold { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | static size_t kAppFuseMaxMountPointName = 32; |
| 35 | |
| 36 | static android::status_t GetMountPath(uid_t uid, const std::string& name, std::string* path) { |
| 37 | if (name.size() > kAppFuseMaxMountPointName) { |
| 38 | LOG(ERROR) << "AppFuse mount name is too long."; |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | for (size_t i = 0; i < name.size(); i++) { |
| 42 | if (!isalnum(name[i])) { |
| 43 | LOG(ERROR) << "AppFuse mount name contains invalid character."; |
| 44 | return -EINVAL; |
| 45 | } |
| 46 | } |
| 47 | *path = StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str()); |
| 48 | return android::OK; |
| 49 | } |
| 50 | |
| 51 | static android::status_t Mount(int device_fd, const std::string& path) { |
| 52 | // Remove existing mount. |
| 53 | android::vold::ForceUnmount(path); |
| 54 | |
| 55 | const auto opts = StringPrintf( |
| 56 | "fd=%i," |
| 57 | "rootmode=40000," |
| 58 | "default_permissions," |
| 59 | "allow_other," |
| 60 | "user_id=0,group_id=0," |
| 61 | "context=\"u:object_r:app_fuse_file:s0\"," |
| 62 | "fscontext=u:object_r:app_fusefs:s0", |
| 63 | device_fd); |
| 64 | |
| 65 | const int result = |
| 66 | TEMP_FAILURE_RETRY(mount("/dev/fuse", path.c_str(), "fuse", |
| 67 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str())); |
| 68 | if (result != 0) { |
| 69 | PLOG(ERROR) << "Failed to mount " << path; |
| 70 | return -errno; |
| 71 | } |
| 72 | |
| 73 | return android::OK; |
| 74 | } |
| 75 | |
| 76 | static android::status_t RunCommand(const std::string& command, uid_t uid, const std::string& path, |
| 77 | int device_fd) { |
| 78 | if (DEBUG_APPFUSE) { |
| 79 | LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path << " and uid " |
| 80 | << uid; |
| 81 | } |
| 82 | |
| 83 | if (command == "mount") { |
| 84 | return Mount(device_fd, path); |
| 85 | } else if (command == "unmount") { |
| 86 | // If it's just after all FD opened on mount point are closed, umount2 can fail with |
| 87 | // EBUSY. To avoid the case, specify MNT_DETACH. |
| 88 | if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && errno != EINVAL && |
| 89 | errno != ENOENT) { |
| 90 | PLOG(ERROR) << "Failed to unmount directory."; |
| 91 | return -errno; |
| 92 | } |
| 93 | if (rmdir(path.c_str()) != 0) { |
| 94 | PLOG(ERROR) << "Failed to remove the mount directory."; |
| 95 | return -errno; |
| 96 | } |
| 97 | return android::OK; |
| 98 | } else { |
| 99 | LOG(ERROR) << "Unknown appfuse command " << command; |
| 100 | return -EPERM; |
| 101 | } |
| 102 | |
| 103 | return android::OK; |
| 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 | |
| 108 | int MountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd) { |
| 109 | std::string name = std::to_string(mountId); |
| 110 | |
| 111 | // Check mount point name. |
| 112 | std::string path; |
| 113 | if (GetMountPath(uid, name, &path) != android::OK) { |
| 114 | LOG(ERROR) << "Invalid mount point name"; |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | // Create directories. |
| 119 | const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0); |
| 120 | if (result != android::OK) { |
| 121 | PLOG(ERROR) << "Failed to prepare directory " << path; |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | // Open device FD. |
| 126 | device_fd->reset(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC |
| 127 | if (device_fd->get() == -1) { |
| 128 | PLOG(ERROR) << "Failed to open /dev/fuse"; |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | // Mount. |
| 133 | return RunCommand("mount", uid, path, device_fd->get()); |
| 134 | } |
| 135 | |
| 136 | int UnmountAppFuse(uid_t uid, int mountId) { |
| 137 | std::string name = std::to_string(mountId); |
| 138 | |
| 139 | // Check mount point name. |
| 140 | std::string path; |
| 141 | if (GetMountPath(uid, name, &path) != android::OK) { |
| 142 | LOG(ERROR) << "Invalid mount point name"; |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | return RunCommand("unmount", uid, path, -1 /* device_fd */); |
| 147 | } |
| 148 | |
| 149 | int OpenAppFuseFile(uid_t uid, int mountId, int fileId, int flags) { |
| 150 | std::string name = std::to_string(mountId); |
| 151 | |
| 152 | // Check mount point name. |
| 153 | std::string mountPoint; |
| 154 | if (GetMountPath(uid, name, &mountPoint) != android::OK) { |
| 155 | LOG(ERROR) << "Invalid mount point name"; |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | std::string path = StringPrintf("%s/%d", mountPoint.c_str(), fileId); |
| 160 | return TEMP_FAILURE_RETRY(open(path.c_str(), flags)); |
| 161 | } |
| 162 | |
| 163 | } // namespace vold |
| 164 | } // namespace android |