blob: a3beaec1878bd2fe6a3eb70804afc64112154b04 [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(
Hyeeun Junfb014fc2023-10-25 13:59:42 +090053 "fd=%i,"
54 "rootmode=40000,"
55 "default_permissions,"
56 "allow_other,"
57 "max_read=65536,"
58 "user_id=0,group_id=0,"
59 "context=\"u:object_r:app_fuse_file:s0\","
60 "fscontext=u:object_r:app_fusefs:s0",
61 device_fd);
Risanac02a482018-10-31 21:59:47 -060062
63 const int result =
64 TEMP_FAILURE_RETRY(mount("/dev/fuse", path.c_str(), "fuse",
65 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()));
66 if (result != 0) {
67 PLOG(ERROR) << "Failed to mount " << path;
68 return -errno;
69 }
70
71 return android::OK;
72}
73
74static android::status_t RunCommand(const std::string& command, uid_t uid, const std::string& path,
75 int device_fd) {
76 if (DEBUG_APPFUSE) {
77 LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path << " and uid "
78 << uid;
79 }
80
81 if (command == "mount") {
82 return Mount(device_fd, path);
83 } else if (command == "unmount") {
84 // If it's just after all FD opened on mount point are closed, umount2 can fail with
85 // EBUSY. To avoid the case, specify MNT_DETACH.
86 if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && errno != EINVAL &&
87 errno != ENOENT) {
88 PLOG(ERROR) << "Failed to unmount directory.";
89 return -errno;
90 }
91 if (rmdir(path.c_str()) != 0) {
92 PLOG(ERROR) << "Failed to remove the mount directory.";
93 return -errno;
94 }
95 return android::OK;
96 } else {
97 LOG(ERROR) << "Unknown appfuse command " << command;
98 return -EPERM;
99 }
100
101 return android::OK;
102}
103
104} // namespace
105
106int MountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd) {
107 std::string name = std::to_string(mountId);
108
109 // Check mount point name.
110 std::string path;
111 if (GetMountPath(uid, name, &path) != android::OK) {
112 LOG(ERROR) << "Invalid mount point name";
113 return -1;
114 }
115
Narayan Kamath15ad33a2019-04-09 18:45:32 +0100116 // Forcibly remove the existing mount before we attempt to prepare the
117 // directory. If we have a dangling mount, then PrepareDir may fail if the
118 // indirection to FUSE doesn't work.
119 android::vold::ForceUnmount(path);
120
Risanac02a482018-10-31 21:59:47 -0600121 // Create directories.
122 const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0);
123 if (result != android::OK) {
124 PLOG(ERROR) << "Failed to prepare directory " << path;
125 return -1;
126 }
127
128 // Open device FD.
Nick Kraleviche7e89ac2019-03-29 16:03:51 -0700129 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
130 device_fd->reset(open("/dev/fuse", O_RDWR));
Risanac02a482018-10-31 21:59:47 -0600131 if (device_fd->get() == -1) {
132 PLOG(ERROR) << "Failed to open /dev/fuse";
133 return -1;
134 }
135
136 // Mount.
137 return RunCommand("mount", uid, path, device_fd->get());
138}
139
140int UnmountAppFuse(uid_t uid, int mountId) {
141 std::string name = std::to_string(mountId);
142
143 // Check mount point name.
144 std::string path;
145 if (GetMountPath(uid, name, &path) != android::OK) {
146 LOG(ERROR) << "Invalid mount point name";
147 return -1;
148 }
149
150 return RunCommand("unmount", uid, path, -1 /* device_fd */);
151}
152
153int OpenAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
154 std::string name = std::to_string(mountId);
155
156 // Check mount point name.
157 std::string mountPoint;
158 if (GetMountPath(uid, name, &mountPoint) != android::OK) {
159 LOG(ERROR) << "Invalid mount point name";
160 return -1;
161 }
162
163 std::string path = StringPrintf("%s/%d", mountPoint.c_str(), fileId);
164 return TEMP_FAILURE_RETRY(open(path.c_str(), flags));
165}
166
167} // namespace vold
168} // namespace android