2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "AppFuseUtil.h"
19 #include <sys/mount.h>
20 #include <utils/Errors.h>
22 #include <android-base/logging.h>
23 #include <android-base/stringprintf.h>
27 using android::base::StringPrintf;
34 static size_t kAppFuseMaxMountPointName = 32;
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.";
41 for (size_t i = 0; i < name.size(); i++) {
42 if (!isalnum(name[i])) {
43 LOG(ERROR) << "AppFuse mount name contains invalid character.";
47 *path = StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str());
51 static android::status_t Mount(int device_fd, const std::string& path) {
52 const auto opts = StringPrintf(
55 "default_permissions,"
57 "user_id=0,group_id=0,"
58 "context=\"u:object_r:app_fuse_file:s0\","
59 "fscontext=u:object_r:app_fusefs:s0",
63 TEMP_FAILURE_RETRY(mount("/dev/fuse", path.c_str(), "fuse",
64 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()));
66 PLOG(ERROR) << "Failed to mount " << path;
73 static android::status_t RunCommand(const std::string& command, uid_t uid, const std::string& path,
76 LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path << " and uid "
80 if (command == "mount") {
81 return Mount(device_fd, path);
82 } else if (command == "unmount") {
83 // If it's just after all FD opened on mount point are closed, umount2 can fail with
84 // EBUSY. To avoid the case, specify MNT_DETACH.
85 if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && errno != EINVAL &&
87 PLOG(ERROR) << "Failed to unmount directory.";
90 if (rmdir(path.c_str()) != 0) {
91 PLOG(ERROR) << "Failed to remove the mount directory.";
96 LOG(ERROR) << "Unknown appfuse command " << command;
105 int MountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd) {
106 std::string name = std::to_string(mountId);
108 // Check mount point name.
110 if (GetMountPath(uid, name, &path) != android::OK) {
111 LOG(ERROR) << "Invalid mount point name";
115 // Forcibly remove the existing mount before we attempt to prepare the
116 // directory. If we have a dangling mount, then PrepareDir may fail if the
117 // indirection to FUSE doesn't work.
118 android::vold::ForceUnmount(path);
120 // Create directories.
121 const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0);
122 if (result != android::OK) {
123 PLOG(ERROR) << "Failed to prepare directory " << path;
128 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
129 device_fd->reset(open("/dev/fuse", O_RDWR));
130 if (device_fd->get() == -1) {
131 PLOG(ERROR) << "Failed to open /dev/fuse";
136 return RunCommand("mount", uid, path, device_fd->get());
139 int UnmountAppFuse(uid_t uid, int mountId) {
140 std::string name = std::to_string(mountId);
142 // Check mount point name.
144 if (GetMountPath(uid, name, &path) != android::OK) {
145 LOG(ERROR) << "Invalid mount point name";
149 return RunCommand("unmount", uid, path, -1 /* device_fd */);
152 int OpenAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
153 std::string name = std::to_string(mountId);
155 // Check mount point name.
156 std::string mountPoint;
157 if (GetMountPath(uid, name, &mountPoint) != android::OK) {
158 LOG(ERROR) << "Invalid mount point name";
162 std::string path = StringPrintf("%s/%d", mountPoint.c_str(), fileId);
163 return TEMP_FAILURE_RETRY(open(path.c_str(), flags));
167 } // namespace android