blob: c491ecd9653598c475e2c68a183dbb2ffe87dbc8 [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) {
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
76static 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
108int 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.
Nick Kraleviche7e89ac2019-03-29 16:03:51 -0700126 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
127 device_fd->reset(open("/dev/fuse", O_RDWR));
Risanac02a482018-10-31 21:59:47 -0600128 if (device_fd->get() == -1) {
129 PLOG(ERROR) << "Failed to open /dev/fuse";
130 return -1;
131 }
132
133 // Mount.
134 return RunCommand("mount", uid, path, device_fd->get());
135}
136
137int UnmountAppFuse(uid_t uid, int mountId) {
138 std::string name = std::to_string(mountId);
139
140 // Check mount point name.
141 std::string path;
142 if (GetMountPath(uid, name, &path) != android::OK) {
143 LOG(ERROR) << "Invalid mount point name";
144 return -1;
145 }
146
147 return RunCommand("unmount", uid, path, -1 /* device_fd */);
148}
149
150int OpenAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
151 std::string name = std::to_string(mountId);
152
153 // Check mount point name.
154 std::string mountPoint;
155 if (GetMountPath(uid, name, &mountPoint) != android::OK) {
156 LOG(ERROR) << "Invalid mount point name";
157 return -1;
158 }
159
160 std::string path = StringPrintf("%s/%d", mountPoint.c_str(), fileId);
161 return TEMP_FAILURE_RETRY(open(path.c_str(), flags));
162}
163
164} // namespace vold
165} // namespace android