blob: 711e70b55c0760583420fdc718ae2d411988b511 [file] [log] [blame]
Risanac02a482018-10-31 21:59:47 -06001/*
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
27using android::base::StringPrintf;
28
29namespace android {
30namespace vold {
31
32namespace {
33
34static size_t kAppFuseMaxMountPointName = 32;
35
36static 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
51static android::status_t Mount(int device_fd, const std::string& path) {
Risanac02a482018-10-31 21:59:47 -060052 const auto opts = StringPrintf(
53 "fd=%i,"
54 "rootmode=40000,"
55 "default_permissions,"
56 "allow_other,"
57 "user_id=0,group_id=0,"
58 "context=\"u:object_r:app_fuse_file:s0\","
59 "fscontext=u:object_r:app_fusefs:s0",
60 device_fd);
61
62 const int result =
63 TEMP_FAILURE_RETRY(mount("/dev/fuse", path.c_str(), "fuse",
64 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()));
65 if (result != 0) {
66 PLOG(ERROR) << "Failed to mount " << path;
67 return -errno;
68 }
69
70 return android::OK;
71}
72
73static android::status_t RunCommand(const std::string& command, uid_t uid, const std::string& path,
74 int device_fd) {
75 if (DEBUG_APPFUSE) {
76 LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path << " and uid "
77 << uid;
78 }
79
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 &&
86 errno != ENOENT) {
87 PLOG(ERROR) << "Failed to unmount directory.";
88 return -errno;
89 }
90 if (rmdir(path.c_str()) != 0) {
91 PLOG(ERROR) << "Failed to remove the mount directory.";
92 return -errno;
93 }
94 return android::OK;
95 } else {
96 LOG(ERROR) << "Unknown appfuse command " << command;
97 return -EPERM;
98 }
99
100 return android::OK;
101}
102
103} // namespace
104
105int MountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd) {
106 std::string name = std::to_string(mountId);
107
108 // Check mount point name.
109 std::string path;
110 if (GetMountPath(uid, name, &path) != android::OK) {
111 LOG(ERROR) << "Invalid mount point name";
112 return -1;
113 }
114
Narayan Kamath15ad33a2019-04-09 18:45:32 +0100115 // 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);
119
Risanac02a482018-10-31 21:59:47 -0600120 // 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;
124 return -1;
125 }
126
127 // Open device FD.
Nick Kraleviche7e89ac2019-03-29 16:03:51 -0700128 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
129 device_fd->reset(open("/dev/fuse", O_RDWR));
Risanac02a482018-10-31 21:59:47 -0600130 if (device_fd->get() == -1) {
131 PLOG(ERROR) << "Failed to open /dev/fuse";
132 return -1;
133 }
134
135 // Mount.
136 return RunCommand("mount", uid, path, device_fd->get());
137}
138
139int UnmountAppFuse(uid_t uid, int mountId) {
140 std::string name = std::to_string(mountId);
141
142 // Check mount point name.
143 std::string path;
144 if (GetMountPath(uid, name, &path) != android::OK) {
145 LOG(ERROR) << "Invalid mount point name";
146 return -1;
147 }
148
149 return RunCommand("unmount", uid, path, -1 /* device_fd */);
150}
151
152int OpenAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
153 std::string name = std::to_string(mountId);
154
155 // Check mount point name.
156 std::string mountPoint;
157 if (GetMountPath(uid, name, &mountPoint) != android::OK) {
158 LOG(ERROR) << "Invalid mount point name";
159 return -1;
160 }
161
162 std::string path = StringPrintf("%s/%d", mountPoint.c_str(), fileId);
163 return TEMP_FAILURE_RETRY(open(path.c_str(), flags));
164}
165
166} // namespace vold
167} // namespace android