Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "Utils.h" |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 18 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 19 | #include "Process.h" |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 20 | #include "sehandle.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 21 | |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 23 | #include <android-base/file.h> |
| 24 | #include <android-base/logging.h> |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 27 | #include <android-base/strings.h> |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 28 | #include <android-base/unique_fd.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 29 | #include <cutils/fs.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 30 | #include <logwrap/logwrap.h> |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 31 | #include <private/android_filesystem_config.h> |
Martijn Coenen | aee4051 | 2020-02-18 16:29:25 +0100 | [diff] [blame] | 32 | #include <private/android_projectid_config.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 33 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 34 | #include <dirent.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 35 | #include <fcntl.h> |
| 36 | #include <linux/fs.h> |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 37 | #include <linux/posix_acl.h> |
| 38 | #include <linux/posix_acl_xattr.h> |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 39 | #include <mntent.h> |
| 40 | #include <stdio.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <sys/mount.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 43 | #include <sys/stat.h> |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 44 | #include <sys/statvfs.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 45 | #include <sys/sysmacros.h> |
| 46 | #include <sys/types.h> |
| 47 | #include <sys/wait.h> |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 48 | #include <sys/xattr.h> |
Paul Crowley | 747b421 | 2019-04-05 04:09:57 -0700 | [diff] [blame] | 49 | #include <unistd.h> |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 50 | |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 51 | #include <filesystem> |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 52 | #include <list> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 53 | #include <mutex> |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 54 | #include <regex> |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 55 | #include <thread> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 56 | |
| 57 | #ifndef UMOUNT_NOFOLLOW |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 58 | #define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */ |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 59 | #endif |
| 60 | |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 61 | using namespace std::chrono_literals; |
Martijn Coenen | ba9868b | 2020-01-31 15:49:24 +0100 | [diff] [blame] | 62 | using android::base::EndsWith; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 63 | using android::base::ReadFileToString; |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 64 | using android::base::StartsWith; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 65 | using android::base::StringPrintf; |
Nikita Ioffe | dcee5c1 | 2020-06-12 12:59:45 +0100 | [diff] [blame] | 66 | using android::base::unique_fd; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 67 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 68 | namespace android { |
| 69 | namespace vold { |
| 70 | |
ThiƩbaud Weksteen | ae8550f | 2021-09-10 10:51:08 +0200 | [diff] [blame] | 71 | char* sBlkidContext = nullptr; |
| 72 | char* sBlkidUntrustedContext = nullptr; |
| 73 | char* sFsckContext = nullptr; |
| 74 | char* sFsckUntrustedContext = nullptr; |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 75 | |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 76 | bool sSleepOnUnmount = true; |
| 77 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 78 | static const char* kBlkidPath = "/system/bin/blkid"; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 79 | static const char* kKeyPath = "/data/misc/vold"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 80 | |
Alistair Delva | ff1fc9b | 2020-05-14 16:35:03 -0700 | [diff] [blame] | 81 | static const char* kProcDevices = "/proc/devices"; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 82 | static const char* kProcFilesystems = "/proc/filesystems"; |
| 83 | |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 84 | static const char* kAndroidDir = "/Android/"; |
| 85 | static const char* kAppDataDir = "/Android/data/"; |
| 86 | static const char* kAppMediaDir = "/Android/media/"; |
| 87 | static const char* kAppObbDir = "/Android/obb/"; |
| 88 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 89 | static const char* kMediaProviderCtx = "u:r:mediaprovider:"; |
| 90 | static const char* kMediaProviderAppCtx = "u:r:mediaprovider_app:"; |
| 91 | |
Jeff Sharkey | ae4f85d | 2017-10-18 17:02:21 -0600 | [diff] [blame] | 92 | // Lock used to protect process-level SELinux changes from racing with each |
| 93 | // other between multiple threads. |
| 94 | static std::mutex kSecurityLock; |
| 95 | |
Nikita Ioffe | dcee5c1 | 2020-06-12 12:59:45 +0100 | [diff] [blame] | 96 | std::string GetFuseMountPathForUser(userid_t user_id, const std::string& relative_upper_path) { |
| 97 | return StringPrintf("/mnt/user/%d/%s", user_id, relative_upper_path.c_str()); |
| 98 | } |
| 99 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 100 | status_t CreateDeviceNode(const std::string& path, dev_t dev) { |
Jeff Sharkey | ae4f85d | 2017-10-18 17:02:21 -0600 | [diff] [blame] | 101 | std::lock_guard<std::mutex> lock(kSecurityLock); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 102 | const char* cpath = path.c_str(); |
| 103 | status_t res = 0; |
| 104 | |
| 105 | char* secontext = nullptr; |
| 106 | if (sehandle) { |
| 107 | if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) { |
| 108 | setfscreatecon(secontext); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | mode_t mode = 0660 | S_IFBLK; |
| 113 | if (mknod(cpath, mode, dev) < 0) { |
| 114 | if (errno != EEXIST) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 115 | PLOG(ERROR) << "Failed to create device node for " << major(dev) << ":" << minor(dev) |
| 116 | << " at " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 117 | res = -errno; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (secontext) { |
| 122 | setfscreatecon(nullptr); |
| 123 | freecon(secontext); |
| 124 | } |
| 125 | |
| 126 | return res; |
| 127 | } |
| 128 | |
| 129 | status_t DestroyDeviceNode(const std::string& path) { |
| 130 | const char* cpath = path.c_str(); |
| 131 | if (TEMP_FAILURE_RETRY(unlink(cpath))) { |
| 132 | return -errno; |
| 133 | } else { |
| 134 | return OK; |
| 135 | } |
| 136 | } |
| 137 | |
Martijn Coenen | 442bb83 | 2020-02-18 13:44:59 +0100 | [diff] [blame] | 138 | // Sets a default ACL on the directory. |
Martijn Coenen | 5adf92a | 2021-02-01 07:57:02 +0000 | [diff] [blame] | 139 | status_t SetDefaultAcl(const std::string& path, mode_t mode, uid_t uid, gid_t gid, |
| 140 | std::vector<gid_t> additionalGids) { |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 141 | if (IsSdcardfsUsed()) { |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 142 | // sdcardfs magically takes care of this |
| 143 | return OK; |
| 144 | } |
| 145 | |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 146 | size_t num_entries = 3 + (additionalGids.size() > 0 ? additionalGids.size() + 1 : 0); |
| 147 | size_t size = sizeof(posix_acl_xattr_header) + num_entries * sizeof(posix_acl_xattr_entry); |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 148 | auto buf = std::make_unique<uint8_t[]>(size); |
| 149 | |
| 150 | posix_acl_xattr_header* acl_header = reinterpret_cast<posix_acl_xattr_header*>(buf.get()); |
| 151 | acl_header->a_version = POSIX_ACL_XATTR_VERSION; |
| 152 | |
| 153 | posix_acl_xattr_entry* entry = |
| 154 | reinterpret_cast<posix_acl_xattr_entry*>(buf.get() + sizeof(posix_acl_xattr_header)); |
| 155 | |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 156 | int tag_index = 0; |
| 157 | |
| 158 | entry[tag_index].e_tag = ACL_USER_OBJ; |
Martijn Coenen | 442bb83 | 2020-02-18 13:44:59 +0100 | [diff] [blame] | 159 | // The existing mode_t mask has the ACL in the lower 9 bits: |
| 160 | // the lowest 3 for "other", the next 3 the group, the next 3 for the owner |
| 161 | // Use the mode_t masks to get these bits out, and shift them to get the |
| 162 | // correct value per entity. |
| 163 | // |
| 164 | // Eg if mode_t = 0700, rwx for the owner, then & S_IRWXU >> 6 results in 7 |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 165 | entry[tag_index].e_perm = (mode & S_IRWXU) >> 6; |
| 166 | entry[tag_index].e_id = uid; |
| 167 | tag_index++; |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 168 | |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 169 | entry[tag_index].e_tag = ACL_GROUP_OBJ; |
| 170 | entry[tag_index].e_perm = (mode & S_IRWXG) >> 3; |
| 171 | entry[tag_index].e_id = gid; |
| 172 | tag_index++; |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 173 | |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 174 | if (additionalGids.size() > 0) { |
| 175 | for (gid_t additional_gid : additionalGids) { |
| 176 | entry[tag_index].e_tag = ACL_GROUP; |
| 177 | entry[tag_index].e_perm = (mode & S_IRWXG) >> 3; |
| 178 | entry[tag_index].e_id = additional_gid; |
| 179 | tag_index++; |
| 180 | } |
| 181 | |
| 182 | entry[tag_index].e_tag = ACL_MASK; |
| 183 | entry[tag_index].e_perm = (mode & S_IRWXG) >> 3; |
| 184 | entry[tag_index].e_id = 0; |
| 185 | tag_index++; |
| 186 | } |
| 187 | |
| 188 | entry[tag_index].e_tag = ACL_OTHER; |
| 189 | entry[tag_index].e_perm = mode & S_IRWXO; |
| 190 | entry[tag_index].e_id = 0; |
Martijn Coenen | 879fa80 | 2020-02-11 12:37:25 +0100 | [diff] [blame] | 191 | |
| 192 | int ret = setxattr(path.c_str(), XATTR_NAME_POSIX_ACL_DEFAULT, acl_header, size, 0); |
| 193 | |
| 194 | if (ret != 0) { |
| 195 | PLOG(ERROR) << "Failed to set default ACL on " << path; |
| 196 | } |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
Martijn Coenen | 5fe1b16 | 2020-02-06 18:57:47 +0100 | [diff] [blame] | 201 | int SetQuotaInherit(const std::string& path) { |
Eric Biggers | f9d9ac2 | 2020-10-23 16:32:58 -0700 | [diff] [blame] | 202 | unsigned int flags; |
Martijn Coenen | 5fe1b16 | 2020-02-06 18:57:47 +0100 | [diff] [blame] | 203 | |
| 204 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC))); |
| 205 | if (fd == -1) { |
| 206 | PLOG(ERROR) << "Failed to open " << path << " to set project id inheritance."; |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | int ret = ioctl(fd, FS_IOC_GETFLAGS, &flags); |
| 211 | if (ret == -1) { |
| 212 | PLOG(ERROR) << "Failed to get flags for " << path << " to set project id inheritance."; |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | flags |= FS_PROJINHERIT_FL; |
| 217 | |
| 218 | ret = ioctl(fd, FS_IOC_SETFLAGS, &flags); |
| 219 | if (ret == -1) { |
| 220 | PLOG(ERROR) << "Failed to set flags for " << path << " to set project id inheritance."; |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int SetQuotaProjectId(const std::string& path, long projectId) { |
Martijn Coenen | fb42bc4 | 2020-01-16 01:25:27 +0100 | [diff] [blame] | 228 | struct fsxattr fsx; |
| 229 | |
| 230 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC))); |
| 231 | if (fd == -1) { |
| 232 | PLOG(ERROR) << "Failed to open " << path << " to set project id."; |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | int ret = ioctl(fd, FS_IOC_FSGETXATTR, &fsx); |
| 237 | if (ret == -1) { |
| 238 | PLOG(ERROR) << "Failed to get extended attributes for " << path << " to get project id."; |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | fsx.fsx_projid = projectId; |
Eric Biggers | 39aa958 | 2021-05-03 12:29:40 -0700 | [diff] [blame] | 243 | ret = ioctl(fd, FS_IOC_FSSETXATTR, &fsx); |
| 244 | if (ret == -1) { |
| 245 | PLOG(ERROR) << "Failed to set project id on " << path; |
| 246 | return ret; |
| 247 | } |
| 248 | return 0; |
Martijn Coenen | fb42bc4 | 2020-01-16 01:25:27 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 251 | int PrepareDirWithProjectId(const std::string& path, mode_t mode, uid_t uid, gid_t gid, |
| 252 | long projectId) { |
| 253 | int ret = fs_prepare_dir(path.c_str(), mode, uid, gid); |
| 254 | |
| 255 | if (ret != 0) { |
| 256 | return ret; |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 257 | } |
Martijn Coenen | ba9868b | 2020-01-31 15:49:24 +0100 | [diff] [blame] | 258 | |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 259 | if (!IsSdcardfsUsed()) { |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 260 | ret = SetQuotaProjectId(path, projectId); |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 266 | static int FixupAppDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid, long projectId) { |
| 267 | namespace fs = std::filesystem; |
| 268 | |
| 269 | // Setup the directory itself correctly |
| 270 | int ret = PrepareDirWithProjectId(path, mode, uid, gid, projectId); |
| 271 | if (ret != OK) { |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | // Fixup all of its file entries |
| 276 | for (const auto& itEntry : fs::directory_iterator(path)) { |
| 277 | ret = lchown(itEntry.path().c_str(), uid, gid); |
| 278 | if (ret != 0) { |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | ret = chmod(itEntry.path().c_str(), mode); |
| 283 | if (ret != 0) { |
| 284 | return ret; |
| 285 | } |
| 286 | |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 287 | if (!IsSdcardfsUsed()) { |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 288 | ret = SetQuotaProjectId(itEntry.path(), projectId); |
| 289 | if (ret != 0) { |
| 290 | return ret; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return OK; |
| 296 | } |
| 297 | |
| 298 | int PrepareAppDirFromRoot(const std::string& path, const std::string& root, int appUid, |
| 299 | bool fixupExisting) { |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 300 | long projectId; |
| 301 | size_t pos; |
| 302 | int ret = 0; |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 303 | bool sdcardfsSupport = IsSdcardfsUsed(); |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 304 | |
| 305 | // Make sure the Android/ directories exist and are setup correctly |
| 306 | ret = PrepareAndroidDirs(root); |
| 307 | if (ret != 0) { |
| 308 | LOG(ERROR) << "Failed to prepare Android/ directories."; |
| 309 | return ret; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 312 | // Now create the application-specific subdir(s) |
| 313 | // path is something like /data/media/0/Android/data/com.foo/files |
| 314 | // First, chop off the volume root, eg /data/media/0 |
| 315 | std::string pathFromRoot = path.substr(root.length()); |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 316 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 317 | uid_t uid = appUid; |
| 318 | gid_t gid = AID_MEDIA_RW; |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 319 | std::vector<gid_t> additionalGids; |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 320 | std::string appDir; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 321 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 322 | // Check that the next part matches one of the allowed Android/ dirs |
| 323 | if (StartsWith(pathFromRoot, kAppDataDir)) { |
| 324 | appDir = kAppDataDir; |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 325 | if (!sdcardfsSupport) { |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 326 | gid = AID_EXT_DATA_RW; |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 327 | // Also add the app's own UID as a group; since apps belong to a group |
| 328 | // that matches their UID, this ensures that they will always have access to |
| 329 | // the files created in these dirs, even if they are created by other processes |
| 330 | additionalGids.push_back(uid); |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 331 | } |
| 332 | } else if (StartsWith(pathFromRoot, kAppMediaDir)) { |
| 333 | appDir = kAppMediaDir; |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 334 | if (!sdcardfsSupport) { |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 335 | gid = AID_MEDIA_RW; |
| 336 | } |
Ricky Wai | e50ddb7 | 2020-02-17 18:57:01 +0000 | [diff] [blame] | 337 | } else if (StartsWith(pathFromRoot, kAppObbDir)) { |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 338 | appDir = kAppObbDir; |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 339 | if (!sdcardfsSupport) { |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 340 | gid = AID_EXT_OBB_RW; |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 341 | // See comments for kAppDataDir above |
| 342 | additionalGids.push_back(uid); |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 343 | } |
| 344 | } else { |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 345 | LOG(ERROR) << "Invalid application directory: " << path; |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 349 | // mode = 770, plus sticky bit on directory to inherit GID when apps |
| 350 | // create subdirs |
| 351 | mode_t mode = S_IRWXU | S_IRWXG | S_ISGID; |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 352 | // the project ID for application-specific directories is directly |
| 353 | // derived from their uid |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 354 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 355 | // Chop off the generic application-specific part, eg /Android/data/ |
| 356 | // this leaves us with something like com.foo/files/ |
| 357 | std::string leftToCreate = pathFromRoot.substr(appDir.length()); |
| 358 | if (!EndsWith(leftToCreate, "/")) { |
| 359 | leftToCreate += "/"; |
| 360 | } |
| 361 | std::string pathToCreate = root + appDir; |
| 362 | int depth = 0; |
Martijn Coenen | aee4051 | 2020-02-18 16:29:25 +0100 | [diff] [blame] | 363 | // Derive initial project ID |
| 364 | if (appDir == kAppDataDir || appDir == kAppMediaDir) { |
| 365 | projectId = uid - AID_APP_START + PROJECT_ID_EXT_DATA_START; |
| 366 | } else if (appDir == kAppObbDir) { |
| 367 | projectId = uid - AID_APP_START + PROJECT_ID_EXT_OBB_START; |
| 368 | } |
| 369 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 370 | while ((pos = leftToCreate.find('/')) != std::string::npos) { |
| 371 | std::string component = leftToCreate.substr(0, pos + 1); |
| 372 | leftToCreate = leftToCreate.erase(0, pos + 1); |
| 373 | pathToCreate = pathToCreate + component; |
| 374 | |
| 375 | if (appDir == kAppDataDir && depth == 1 && component == "cache/") { |
| 376 | // All dirs use the "app" project ID, except for the cache dirs in |
| 377 | // Android/data, eg Android/data/com.foo/cache |
| 378 | // Note that this "sticks" - eg subdirs of this dir need the same |
| 379 | // project ID. |
Martijn Coenen | aee4051 | 2020-02-18 16:29:25 +0100 | [diff] [blame] | 380 | projectId = uid - AID_APP_START + PROJECT_ID_EXT_CACHE_START; |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 381 | } |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 382 | |
| 383 | if (fixupExisting && access(pathToCreate.c_str(), F_OK) == 0) { |
| 384 | // Fixup all files in this existing directory with the correct UID/GID |
| 385 | // and project ID. |
| 386 | ret = FixupAppDir(pathToCreate, mode, uid, gid, projectId); |
| 387 | } else { |
| 388 | ret = PrepareDirWithProjectId(pathToCreate, mode, uid, gid, projectId); |
| 389 | } |
| 390 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 391 | if (ret != 0) { |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | if (depth == 0) { |
| 396 | // Set the default ACL on the top-level application-specific directories, |
| 397 | // to ensure that even if applications run with a umask of 0077, |
| 398 | // new directories within these directories will allow the GID |
| 399 | // specified here to write; this is necessary for apps like |
| 400 | // installers and MTP, that require access here. |
| 401 | // |
| 402 | // See man (5) acl for more details. |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 403 | ret = SetDefaultAcl(pathToCreate, mode, uid, gid, additionalGids); |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 404 | if (ret != 0) { |
| 405 | return ret; |
| 406 | } |
Martijn Coenen | 9171fcc | 2020-03-11 11:51:45 +0100 | [diff] [blame] | 407 | |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 408 | if (!sdcardfsSupport) { |
Martijn Coenen | 9171fcc | 2020-03-11 11:51:45 +0100 | [diff] [blame] | 409 | // Set project ID inheritance, so that future subdirectories inherit the |
| 410 | // same project ID |
| 411 | ret = SetQuotaInherit(pathToCreate); |
| 412 | if (ret != 0) { |
| 413 | return ret; |
| 414 | } |
| 415 | } |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | depth++; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 421 | return OK; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Daniel Rosenberg | cc87480 | 2020-08-12 18:31:43 -0700 | [diff] [blame] | 424 | int SetAttrs(const std::string& path, unsigned int attrs) { |
Eric Biggers | f9d9ac2 | 2020-10-23 16:32:58 -0700 | [diff] [blame] | 425 | unsigned int flags; |
Daniel Rosenberg | cc87480 | 2020-08-12 18:31:43 -0700 | [diff] [blame] | 426 | android::base::unique_fd fd( |
| 427 | TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC))); |
| 428 | |
| 429 | if (fd == -1) { |
| 430 | PLOG(ERROR) << "Failed to open " << path; |
| 431 | return -1; |
| 432 | } |
| 433 | |
Eric Biggers | f9d9ac2 | 2020-10-23 16:32:58 -0700 | [diff] [blame] | 434 | if (ioctl(fd, FS_IOC_GETFLAGS, &flags)) { |
Daniel Rosenberg | cc87480 | 2020-08-12 18:31:43 -0700 | [diff] [blame] | 435 | PLOG(ERROR) << "Failed to get flags for " << path; |
| 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | if ((flags & attrs) == attrs) return 0; |
| 440 | flags |= attrs; |
Eric Biggers | f9d9ac2 | 2020-10-23 16:32:58 -0700 | [diff] [blame] | 441 | if (ioctl(fd, FS_IOC_SETFLAGS, &flags)) { |
Daniel Rosenberg | cc87480 | 2020-08-12 18:31:43 -0700 | [diff] [blame] | 442 | PLOG(ERROR) << "Failed to set flags for " << path << "(0x" << std::hex << attrs << ")"; |
| 443 | return -1; |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid, |
| 449 | unsigned int attrs) { |
Jeff Sharkey | ae4f85d | 2017-10-18 17:02:21 -0600 | [diff] [blame] | 450 | std::lock_guard<std::mutex> lock(kSecurityLock); |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 451 | const char* cpath = path.c_str(); |
| 452 | |
| 453 | char* secontext = nullptr; |
| 454 | if (sehandle) { |
| 455 | if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) { |
| 456 | setfscreatecon(secontext); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | int res = fs_prepare_dir(cpath, mode, uid, gid); |
| 461 | |
| 462 | if (secontext) { |
| 463 | setfscreatecon(nullptr); |
| 464 | freecon(secontext); |
| 465 | } |
| 466 | |
Daniel Rosenberg | cc87480 | 2020-08-12 18:31:43 -0700 | [diff] [blame] | 467 | if (res) return -errno; |
| 468 | if (attrs) res = SetAttrs(path, attrs); |
| 469 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 470 | if (res == 0) { |
| 471 | return OK; |
| 472 | } else { |
| 473 | return -errno; |
| 474 | } |
| 475 | } |
| 476 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 477 | status_t ForceUnmount(const std::string& path) { |
| 478 | const char* cpath = path.c_str(); |
| 479 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 480 | return OK; |
| 481 | } |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 482 | // Apps might still be handling eject request, so wait before |
| 483 | // we start sending signals |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 484 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 485 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 486 | KillProcessesWithOpenFiles(path, SIGINT); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 487 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 488 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 489 | return OK; |
| 490 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 491 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 492 | KillProcessesWithOpenFiles(path, SIGTERM); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 493 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 494 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 495 | return OK; |
| 496 | } |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 497 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 498 | KillProcessesWithOpenFiles(path, SIGKILL); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 499 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 500 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 501 | return OK; |
| 502 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 503 | PLOG(INFO) << "ForceUnmount failed"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 504 | return -errno; |
| 505 | } |
| 506 | |
Ricky Wai | 2335637 | 2021-04-30 09:53:07 +0100 | [diff] [blame] | 507 | status_t KillProcessesWithTmpfsMountPrefix(const std::string& path) { |
| 508 | if (KillProcessesWithTmpfsMounts(path, SIGINT) == 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 509 | return OK; |
| 510 | } |
| 511 | if (sSleepOnUnmount) sleep(5); |
| 512 | |
Ricky Wai | 2335637 | 2021-04-30 09:53:07 +0100 | [diff] [blame] | 513 | if (KillProcessesWithTmpfsMounts(path, SIGTERM) == 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 514 | return OK; |
| 515 | } |
| 516 | if (sSleepOnUnmount) sleep(5); |
| 517 | |
Ricky Wai | 2335637 | 2021-04-30 09:53:07 +0100 | [diff] [blame] | 518 | if (KillProcessesWithTmpfsMounts(path, SIGKILL) == 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 519 | return OK; |
| 520 | } |
| 521 | if (sSleepOnUnmount) sleep(5); |
| 522 | |
| 523 | // Send SIGKILL a second time to determine if we've |
| 524 | // actually killed everyone mount |
Ricky Wai | 2335637 | 2021-04-30 09:53:07 +0100 | [diff] [blame] | 525 | if (KillProcessesWithTmpfsMounts(path, SIGKILL) == 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 526 | return OK; |
| 527 | } |
| 528 | PLOG(ERROR) << "Failed to kill processes using " << path; |
| 529 | return -EBUSY; |
| 530 | } |
| 531 | |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 532 | status_t KillProcessesUsingPath(const std::string& path) { |
Zim | 7527300 | 2021-03-04 12:21:24 +0000 | [diff] [blame] | 533 | if (KillProcessesWithOpenFiles(path, SIGINT, false /* killFuseDaemon */) == 0) { |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 534 | return OK; |
| 535 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 536 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 537 | |
Zim | 7527300 | 2021-03-04 12:21:24 +0000 | [diff] [blame] | 538 | if (KillProcessesWithOpenFiles(path, SIGTERM, false /* killFuseDaemon */) == 0) { |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 539 | return OK; |
| 540 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 541 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 542 | |
Zim | 7527300 | 2021-03-04 12:21:24 +0000 | [diff] [blame] | 543 | if (KillProcessesWithOpenFiles(path, SIGKILL, false /* killFuseDaemon */) == 0) { |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 544 | return OK; |
| 545 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 546 | if (sSleepOnUnmount) sleep(5); |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 547 | |
| 548 | // Send SIGKILL a second time to determine if we've |
| 549 | // actually killed everyone with open files |
Zim | 7527300 | 2021-03-04 12:21:24 +0000 | [diff] [blame] | 550 | // This time, we also kill the FUSE daemon if found |
| 551 | if (KillProcessesWithOpenFiles(path, SIGKILL, true /* killFuseDaemon */) == 0) { |
Jeff Sharkey | 89f74fb | 2015-10-21 12:16:12 -0700 | [diff] [blame] | 552 | return OK; |
| 553 | } |
| 554 | PLOG(ERROR) << "Failed to kill processes using " << path; |
| 555 | return -EBUSY; |
| 556 | } |
| 557 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 558 | status_t BindMount(const std::string& source, const std::string& target) { |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 559 | if (UnmountTree(target) < 0) { |
| 560 | return -errno; |
| 561 | } |
| 562 | if (TEMP_FAILURE_RETRY(mount(source.c_str(), target.c_str(), nullptr, MS_BIND, nullptr)) < 0) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 563 | PLOG(ERROR) << "Failed to bind mount " << source << " to " << target; |
| 564 | return -errno; |
| 565 | } |
| 566 | return OK; |
| 567 | } |
| 568 | |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 569 | status_t Symlink(const std::string& target, const std::string& linkpath) { |
| 570 | if (Unlink(linkpath) < 0) { |
| 571 | return -errno; |
| 572 | } |
| 573 | if (TEMP_FAILURE_RETRY(symlink(target.c_str(), linkpath.c_str())) < 0) { |
| 574 | PLOG(ERROR) << "Failed to create symlink " << linkpath << " to " << target; |
| 575 | return -errno; |
| 576 | } |
| 577 | return OK; |
| 578 | } |
| 579 | |
| 580 | status_t Unlink(const std::string& linkpath) { |
| 581 | if (TEMP_FAILURE_RETRY(unlink(linkpath.c_str())) < 0 && errno != EINVAL && errno != ENOENT) { |
| 582 | PLOG(ERROR) << "Failed to unlink " << linkpath; |
| 583 | return -errno; |
| 584 | } |
| 585 | return OK; |
| 586 | } |
| 587 | |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 588 | status_t CreateDir(const std::string& dir, mode_t mode) { |
| 589 | struct stat sb; |
| 590 | if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &sb)) == 0) { |
| 591 | if (S_ISDIR(sb.st_mode)) { |
| 592 | return OK; |
| 593 | } else if (TEMP_FAILURE_RETRY(unlink(dir.c_str())) == -1) { |
| 594 | PLOG(ERROR) << "Failed to unlink " << dir; |
| 595 | return -errno; |
| 596 | } |
| 597 | } else if (errno != ENOENT) { |
| 598 | PLOG(ERROR) << "Failed to stat " << dir; |
| 599 | return -errno; |
| 600 | } |
Sudheer Shanka | 6d285ce | 2019-02-19 14:12:20 -0800 | [diff] [blame] | 601 | if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), mode)) == -1 && errno != EEXIST) { |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 602 | PLOG(ERROR) << "Failed to mkdir " << dir; |
| 603 | return -errno; |
| 604 | } |
| 605 | return OK; |
| 606 | } |
| 607 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 608 | bool FindValue(const std::string& raw, const std::string& key, std::string* value) { |
| 609 | auto qual = key + "=\""; |
Paul Crowley | 95abfa0 | 2019-02-05 15:33:34 -0800 | [diff] [blame] | 610 | size_t start = 0; |
| 611 | while (true) { |
| 612 | start = raw.find(qual, start); |
| 613 | if (start == std::string::npos) return false; |
| 614 | if (start == 0 || raw[start - 1] == ' ') { |
| 615 | break; |
| 616 | } |
| 617 | start += 1; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 618 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 619 | start += qual.length(); |
| 620 | |
| 621 | auto end = raw.find("\"", start); |
| 622 | if (end == std::string::npos) return false; |
| 623 | |
| 624 | *value = raw.substr(start, end - start); |
| 625 | return true; |
| 626 | } |
| 627 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 628 | static status_t readMetadata(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 629 | std::string* fsLabel, bool untrusted) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 630 | fsType->clear(); |
| 631 | fsUuid->clear(); |
| 632 | fsLabel->clear(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 633 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 634 | std::vector<std::string> cmd; |
| 635 | cmd.push_back(kBlkidPath); |
| 636 | cmd.push_back("-c"); |
| 637 | cmd.push_back("/dev/null"); |
Jeff Sharkey | eddf9bd | 2015-08-12 16:04:35 -0700 | [diff] [blame] | 638 | cmd.push_back("-s"); |
| 639 | cmd.push_back("TYPE"); |
| 640 | cmd.push_back("-s"); |
| 641 | cmd.push_back("UUID"); |
| 642 | cmd.push_back("-s"); |
| 643 | cmd.push_back("LABEL"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 644 | cmd.push_back(path); |
| 645 | |
| 646 | std::vector<std::string> output; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 647 | status_t res = ForkExecvp(cmd, &output, untrusted ? sBlkidUntrustedContext : sBlkidContext); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 648 | if (res != OK) { |
| 649 | LOG(WARNING) << "blkid failed to identify " << path; |
| 650 | return res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 653 | for (const auto& line : output) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 654 | // Extract values from blkid output, if defined |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 655 | FindValue(line, "TYPE", fsType); |
| 656 | FindValue(line, "UUID", fsUuid); |
| 657 | FindValue(line, "LABEL", fsLabel); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 660 | return OK; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 663 | status_t ReadMetadata(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 664 | std::string* fsLabel) { |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 665 | return readMetadata(path, fsType, fsUuid, fsLabel, false); |
| 666 | } |
| 667 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 668 | status_t ReadMetadataUntrusted(const std::string& path, std::string* fsType, std::string* fsUuid, |
| 669 | std::string* fsLabel) { |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 670 | return readMetadata(path, fsType, fsUuid, fsLabel, true); |
| 671 | } |
| 672 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 673 | static std::vector<const char*> ConvertToArgv(const std::vector<std::string>& args) { |
| 674 | std::vector<const char*> argv; |
| 675 | argv.reserve(args.size() + 1); |
| 676 | for (const auto& arg : args) { |
| 677 | if (argv.empty()) { |
| 678 | LOG(DEBUG) << arg; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 679 | } else { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 680 | LOG(DEBUG) << " " << arg; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 681 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 682 | argv.emplace_back(arg.data()); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 683 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 684 | argv.emplace_back(nullptr); |
| 685 | return argv; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 688 | static status_t ReadLinesFromFdAndLog(std::vector<std::string>* output, |
| 689 | android::base::unique_fd ufd) { |
| 690 | std::unique_ptr<FILE, int (*)(FILE*)> fp(android::base::Fdopen(std::move(ufd), "r"), fclose); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 691 | if (!fp) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 692 | PLOG(ERROR) << "fdopen in ReadLinesFromFdAndLog"; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 693 | return -errno; |
| 694 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 695 | if (output) output->clear(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 696 | char line[1024]; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 697 | while (fgets(line, sizeof(line), fp.get()) != nullptr) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 698 | LOG(DEBUG) << line; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 699 | if (output) output->emplace_back(line); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 700 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 701 | return OK; |
| 702 | } |
| 703 | |
| 704 | status_t ForkExecvp(const std::vector<std::string>& args, std::vector<std::string>* output, |
ThiƩbaud Weksteen | ae8550f | 2021-09-10 10:51:08 +0200 | [diff] [blame] | 705 | char* context) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 706 | auto argv = ConvertToArgv(args); |
| 707 | |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 708 | android::base::unique_fd pipe_read, pipe_write; |
| 709 | if (!android::base::Pipe(&pipe_read, &pipe_write)) { |
| 710 | PLOG(ERROR) << "Pipe in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 711 | return -errno; |
| 712 | } |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 713 | |
| 714 | pid_t pid = fork(); |
| 715 | if (pid == 0) { |
| 716 | if (context) { |
| 717 | if (setexeccon(context)) { |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 718 | LOG(ERROR) << "Failed to setexeccon in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 719 | abort(); |
| 720 | } |
| 721 | } |
| 722 | pipe_read.reset(); |
Paul Crowley | be857bf | 2018-12-07 12:23:25 -0800 | [diff] [blame] | 723 | if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) { |
| 724 | PLOG(ERROR) << "dup2 in ForkExecvp"; |
| 725 | _exit(EXIT_FAILURE); |
| 726 | } |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 727 | pipe_write.reset(); |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 728 | execvp(argv[0], const_cast<char**>(argv.data())); |
Paul Crowley | e6d7663 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 729 | PLOG(ERROR) << "exec in ForkExecvp"; |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 730 | _exit(EXIT_FAILURE); |
| 731 | } |
| 732 | if (pid == -1) { |
| 733 | PLOG(ERROR) << "fork in ForkExecvp"; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 734 | return -errno; |
| 735 | } |
| 736 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 737 | pipe_write.reset(); |
| 738 | auto st = ReadLinesFromFdAndLog(output, std::move(pipe_read)); |
| 739 | if (st != 0) return st; |
| 740 | |
| 741 | int status; |
| 742 | if (waitpid(pid, &status, 0) == -1) { |
| 743 | PLOG(ERROR) << "waitpid in ForkExecvp"; |
| 744 | return -errno; |
| 745 | } |
| 746 | if (!WIFEXITED(status)) { |
| 747 | LOG(ERROR) << "Process did not exit normally, status: " << status; |
| 748 | return -ECHILD; |
| 749 | } |
| 750 | if (WEXITSTATUS(status)) { |
| 751 | LOG(ERROR) << "Process exited with code: " << WEXITSTATUS(status); |
| 752 | return WEXITSTATUS(status); |
| 753 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 754 | return OK; |
| 755 | } |
| 756 | |
Daniel Rosenberg | d9261b1 | 2021-09-14 17:32:06 -0700 | [diff] [blame] | 757 | status_t ForkExecvpTimeout(const std::vector<std::string>& args, std::chrono::seconds timeout, |
| 758 | char* context) { |
| 759 | int status; |
| 760 | |
| 761 | pid_t wait_timeout_pid = fork(); |
| 762 | if (wait_timeout_pid == 0) { |
| 763 | pid_t pid = ForkExecvpAsync(args, context); |
| 764 | if (pid == -1) { |
| 765 | _exit(EXIT_FAILURE); |
| 766 | } |
| 767 | pid_t timer_pid = fork(); |
| 768 | if (timer_pid == 0) { |
| 769 | sleep(timeout.count()); |
| 770 | _exit(ETIMEDOUT); |
| 771 | } |
| 772 | if (timer_pid == -1) { |
| 773 | PLOG(ERROR) << "fork in ForkExecvpAsync_timeout"; |
| 774 | kill(pid, SIGTERM); |
| 775 | _exit(EXIT_FAILURE); |
| 776 | } |
| 777 | pid_t finished = wait(&status); |
| 778 | if (finished == pid) { |
| 779 | kill(timer_pid, SIGTERM); |
| 780 | } else { |
| 781 | kill(pid, SIGTERM); |
| 782 | } |
| 783 | if (!WIFEXITED(status)) { |
| 784 | _exit(ECHILD); |
| 785 | } |
| 786 | _exit(WEXITSTATUS(status)); |
| 787 | } |
| 788 | if (waitpid(wait_timeout_pid, &status, 0) == -1) { |
| 789 | PLOG(ERROR) << "waitpid in ForkExecvpAsync_timeout"; |
| 790 | return -errno; |
| 791 | } |
| 792 | if (!WIFEXITED(status)) { |
| 793 | LOG(ERROR) << "Process did not exit normally, status: " << status; |
| 794 | return -ECHILD; |
| 795 | } |
| 796 | if (WEXITSTATUS(status)) { |
| 797 | LOG(ERROR) << "Process exited with code: " << WEXITSTATUS(status); |
| 798 | return WEXITSTATUS(status); |
| 799 | } |
| 800 | return OK; |
| 801 | } |
| 802 | |
| 803 | pid_t ForkExecvpAsync(const std::vector<std::string>& args, char* context) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 804 | auto argv = ConvertToArgv(args); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 805 | |
| 806 | pid_t pid = fork(); |
| 807 | if (pid == 0) { |
| 808 | close(STDIN_FILENO); |
| 809 | close(STDOUT_FILENO); |
| 810 | close(STDERR_FILENO); |
Daniel Rosenberg | d9261b1 | 2021-09-14 17:32:06 -0700 | [diff] [blame] | 811 | if (context) { |
| 812 | if (setexeccon(context)) { |
| 813 | LOG(ERROR) << "Failed to setexeccon in ForkExecvpAsync"; |
| 814 | abort(); |
| 815 | } |
| 816 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 817 | |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 818 | execvp(argv[0], const_cast<char**>(argv.data())); |
| 819 | PLOG(ERROR) << "exec in ForkExecvpAsync"; |
| 820 | _exit(EXIT_FAILURE); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 821 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 822 | if (pid == -1) { |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 823 | PLOG(ERROR) << "fork in ForkExecvpAsync"; |
| 824 | return -1; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 825 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 826 | return pid; |
| 827 | } |
| 828 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 829 | status_t ReadRandomBytes(size_t bytes, std::string& out) { |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 830 | out.resize(bytes); |
| 831 | return ReadRandomBytes(bytes, &out[0]); |
| 832 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 833 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 834 | status_t ReadRandomBytes(size_t bytes, char* buf) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 835 | int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 836 | if (fd == -1) { |
| 837 | return -errno; |
| 838 | } |
| 839 | |
Eric Biggers | 0ef7bfd | 2019-01-16 13:05:34 -0800 | [diff] [blame] | 840 | ssize_t n; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 841 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], bytes))) > 0) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 842 | bytes -= n; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 843 | buf += n; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 844 | } |
Elliott Hughes | a623108 | 2015-05-15 18:34:24 -0700 | [diff] [blame] | 845 | close(fd); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 846 | |
| 847 | if (bytes == 0) { |
| 848 | return OK; |
| 849 | } else { |
| 850 | return -EIO; |
| 851 | } |
| 852 | } |
| 853 | |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 854 | status_t GenerateRandomUuid(std::string& out) { |
| 855 | status_t res = ReadRandomBytes(16, out); |
| 856 | if (res == OK) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 857 | out[6] &= 0x0f; /* clear version */ |
| 858 | out[6] |= 0x40; /* set to version 4 */ |
| 859 | out[8] &= 0x3f; /* clear variant */ |
| 860 | out[8] |= 0x80; /* set to IETF variant */ |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 861 | } |
| 862 | return res; |
| 863 | } |
| 864 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 865 | status_t HexToStr(const std::string& hex, std::string& str) { |
| 866 | str.clear(); |
| 867 | bool even = true; |
| 868 | char cur = 0; |
| 869 | for (size_t i = 0; i < hex.size(); i++) { |
| 870 | int val = 0; |
| 871 | switch (hex[i]) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 872 | // clang-format off |
| 873 | case ' ': case '-': case ':': continue; |
| 874 | case 'f': case 'F': val = 15; break; |
| 875 | case 'e': case 'E': val = 14; break; |
| 876 | case 'd': case 'D': val = 13; break; |
| 877 | case 'c': case 'C': val = 12; break; |
| 878 | case 'b': case 'B': val = 11; break; |
| 879 | case 'a': case 'A': val = 10; break; |
| 880 | case '9': val = 9; break; |
| 881 | case '8': val = 8; break; |
| 882 | case '7': val = 7; break; |
| 883 | case '6': val = 6; break; |
| 884 | case '5': val = 5; break; |
| 885 | case '4': val = 4; break; |
| 886 | case '3': val = 3; break; |
| 887 | case '2': val = 2; break; |
| 888 | case '1': val = 1; break; |
| 889 | case '0': val = 0; break; |
| 890 | default: return -EINVAL; |
| 891 | // clang-format on |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | if (even) { |
| 895 | cur = val << 4; |
| 896 | } else { |
| 897 | cur += val; |
| 898 | str.push_back(cur); |
| 899 | cur = 0; |
| 900 | } |
| 901 | even = !even; |
| 902 | } |
| 903 | return even ? OK : -EINVAL; |
| 904 | } |
| 905 | |
| 906 | static const char* kLookup = "0123456789abcdef"; |
| 907 | |
| 908 | status_t StrToHex(const std::string& str, std::string& hex) { |
| 909 | hex.clear(); |
| 910 | for (size_t i = 0; i < str.size(); i++) { |
Jeff Sharkey | ef36975 | 2015-04-29 15:57:48 -0700 | [diff] [blame] | 911 | hex.push_back(kLookup[(str[i] & 0xF0) >> 4]); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 912 | hex.push_back(kLookup[str[i] & 0x0F]); |
| 913 | } |
| 914 | return OK; |
| 915 | } |
| 916 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 917 | status_t StrToHex(const KeyBuffer& str, KeyBuffer& hex) { |
| 918 | hex.clear(); |
| 919 | for (size_t i = 0; i < str.size(); i++) { |
| 920 | hex.push_back(kLookup[(str.data()[i] & 0xF0) >> 4]); |
| 921 | hex.push_back(kLookup[str.data()[i] & 0x0F]); |
| 922 | } |
| 923 | return OK; |
| 924 | } |
| 925 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 926 | status_t NormalizeHex(const std::string& in, std::string& out) { |
| 927 | std::string tmp; |
| 928 | if (HexToStr(in, tmp)) { |
| 929 | return -EINVAL; |
| 930 | } |
| 931 | return StrToHex(tmp, out); |
| 932 | } |
| 933 | |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 934 | status_t GetBlockDevSize(int fd, uint64_t* size) { |
| 935 | if (ioctl(fd, BLKGETSIZE64, size)) { |
| 936 | return -errno; |
| 937 | } |
| 938 | |
| 939 | return OK; |
| 940 | } |
| 941 | |
| 942 | status_t GetBlockDevSize(const std::string& path, uint64_t* size) { |
| 943 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 944 | status_t res = OK; |
| 945 | |
| 946 | if (fd < 0) { |
| 947 | return -errno; |
| 948 | } |
| 949 | |
| 950 | res = GetBlockDevSize(fd, size); |
| 951 | |
| 952 | close(fd); |
| 953 | |
| 954 | return res; |
| 955 | } |
| 956 | |
| 957 | status_t GetBlockDev512Sectors(const std::string& path, uint64_t* nr_sec) { |
| 958 | uint64_t size; |
| 959 | status_t res = GetBlockDevSize(path, &size); |
| 960 | |
| 961 | if (res != OK) { |
| 962 | return res; |
| 963 | } |
| 964 | |
| 965 | *nr_sec = size / 512; |
| 966 | |
| 967 | return OK; |
| 968 | } |
| 969 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 970 | uint64_t GetFreeBytes(const std::string& path) { |
| 971 | struct statvfs sb; |
| 972 | if (statvfs(path.c_str(), &sb) == 0) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 973 | return (uint64_t)sb.f_bavail * sb.f_frsize; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 974 | } else { |
| 975 | return -1; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | // TODO: borrowed from frameworks/native/libs/diskusage/ which should |
| 980 | // eventually be migrated into system/ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 981 | static int64_t stat_size(struct stat* s) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 982 | int64_t blksize = s->st_blksize; |
| 983 | // count actual blocks used instead of nominal file size |
| 984 | int64_t size = s->st_blocks * 512; |
| 985 | |
| 986 | if (blksize) { |
| 987 | /* round up to filesystem block size */ |
| 988 | size = (size + blksize - 1) & (~(blksize - 1)); |
| 989 | } |
| 990 | |
| 991 | return size; |
| 992 | } |
| 993 | |
| 994 | // TODO: borrowed from frameworks/native/libs/diskusage/ which should |
| 995 | // eventually be migrated into system/ |
| 996 | int64_t calculate_dir_size(int dfd) { |
| 997 | int64_t size = 0; |
| 998 | struct stat s; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 999 | DIR* d; |
| 1000 | struct dirent* de; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 1001 | |
| 1002 | d = fdopendir(dfd); |
| 1003 | if (d == NULL) { |
| 1004 | close(dfd); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | while ((de = readdir(d))) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 1009 | const char* name = de->d_name; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 1010 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 1011 | size += stat_size(&s); |
| 1012 | } |
| 1013 | if (de->d_type == DT_DIR) { |
| 1014 | int subfd; |
| 1015 | |
| 1016 | /* always skip "." and ".." */ |
Eric Biggers | 7bcf427 | 2020-11-02 15:31:56 -0800 | [diff] [blame] | 1017 | if (IsDotOrDotDot(*de)) continue; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 1018 | |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 1019 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 1020 | if (subfd >= 0) { |
| 1021 | size += calculate_dir_size(subfd); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | closedir(d); |
| 1026 | return size; |
| 1027 | } |
| 1028 | |
| 1029 | uint64_t GetTreeBytes(const std::string& path) { |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 1030 | int dirfd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 1031 | if (dirfd < 0) { |
| 1032 | PLOG(WARNING) << "Failed to open " << path; |
| 1033 | return -1; |
| 1034 | } else { |
Josh Gao | 72fb1a6 | 2018-05-29 19:05:16 -0700 | [diff] [blame] | 1035 | return calculate_dir_size(dirfd); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 1039 | // TODO: Use a better way to determine if it's media provider app. |
| 1040 | bool IsFuseDaemon(const pid_t pid) { |
| 1041 | auto path = StringPrintf("/proc/%d/mounts", pid); |
| 1042 | char* tmp; |
| 1043 | if (lgetfilecon(path.c_str(), &tmp) < 0) { |
| 1044 | return false; |
| 1045 | } |
| 1046 | bool result = android::base::StartsWith(tmp, kMediaProviderAppCtx) |
| 1047 | || android::base::StartsWith(tmp, kMediaProviderCtx); |
| 1048 | freecon(tmp); |
| 1049 | return result; |
| 1050 | } |
| 1051 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1052 | bool IsFilesystemSupported(const std::string& fsType) { |
| 1053 | std::string supported; |
| 1054 | if (!ReadFileToString(kProcFilesystems, &supported)) { |
| 1055 | PLOG(ERROR) << "Failed to read supported filesystems"; |
| 1056 | return false; |
| 1057 | } |
| 1058 | return supported.find(fsType + "\n") != std::string::npos; |
| 1059 | } |
| 1060 | |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 1061 | bool IsSdcardfsUsed() { |
| 1062 | return IsFilesystemSupported("sdcardfs") && |
| 1063 | base::GetBoolProperty(kExternalStorageSdcardfs, true); |
| 1064 | } |
| 1065 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1066 | status_t WipeBlockDevice(const std::string& path) { |
| 1067 | status_t res = -1; |
| 1068 | const char* c_path = path.c_str(); |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 1069 | uint64_t range[2] = {0, 0}; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1070 | |
| 1071 | int fd = TEMP_FAILURE_RETRY(open(c_path, O_RDWR | O_CLOEXEC)); |
| 1072 | if (fd == -1) { |
| 1073 | PLOG(ERROR) << "Failed to open " << path; |
| 1074 | goto done; |
| 1075 | } |
| 1076 | |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 1077 | if (GetBlockDevSize(fd, &range[1]) != OK) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1078 | PLOG(ERROR) << "Failed to determine size of " << path; |
| 1079 | goto done; |
| 1080 | } |
| 1081 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1082 | LOG(INFO) << "About to discard " << range[1] << " on " << path; |
| 1083 | if (ioctl(fd, BLKDISCARD, &range) == 0) { |
| 1084 | LOG(INFO) << "Discard success on " << path; |
| 1085 | res = 0; |
| 1086 | } else { |
| 1087 | PLOG(ERROR) << "Discard failure on " << path; |
| 1088 | } |
| 1089 | |
| 1090 | done: |
| 1091 | close(fd); |
| 1092 | return res; |
| 1093 | } |
| 1094 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1095 | static bool isValidFilename(const std::string& name) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 1096 | if (name.empty() || (name == ".") || (name == "..") || (name.find('/') != std::string::npos)) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1097 | return false; |
| 1098 | } else { |
| 1099 | return true; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 1103 | std::string BuildKeyPath(const std::string& partGuid) { |
| 1104 | return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str()); |
| 1105 | } |
| 1106 | |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 1107 | std::string BuildDataSystemLegacyPath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1108 | return StringPrintf("%s/system/users/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 1109 | } |
| 1110 | |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1111 | std::string BuildDataSystemCePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1112 | return StringPrintf("%s/system_ce/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | std::string BuildDataSystemDePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1116 | return StringPrintf("%s/system_de/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 1119 | std::string BuildDataMiscLegacyPath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1120 | return StringPrintf("%s/misc/user/%u", BuildDataPath("").c_str(), userId); |
Jeff Sharkey | be70c9a | 2016-04-14 20:45:16 -0600 | [diff] [blame] | 1121 | } |
| 1122 | |
Calin Juravle | 79f55a4 | 2016-02-17 20:14:46 +0000 | [diff] [blame] | 1123 | // Keep in sync with installd (frameworks/native/cmds/installd/utils.h) |
| 1124 | std::string BuildDataProfilesDePath(userid_t userId) { |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1125 | return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath("").c_str(), userId); |
Calin Juravle | 79f55a4 | 2016-02-17 20:14:46 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Andreas Huber | 71cd43f | 2018-01-22 11:25:29 -0800 | [diff] [blame] | 1128 | std::string BuildDataVendorCePath(userid_t userId) { |
| 1129 | return StringPrintf("%s/vendor_ce/%u", BuildDataPath("").c_str(), userId); |
| 1130 | } |
| 1131 | |
| 1132 | std::string BuildDataVendorDePath(userid_t userId) { |
| 1133 | return StringPrintf("%s/vendor_de/%u", BuildDataPath("").c_str(), userId); |
| 1134 | } |
| 1135 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1136 | std::string BuildDataPath(const std::string& volumeUuid) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1137 | // TODO: unify with installd path generation logic |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1138 | if (volumeUuid.empty()) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1139 | return "/data"; |
| 1140 | } else { |
| 1141 | CHECK(isValidFilename(volumeUuid)); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1142 | return StringPrintf("/mnt/expand/%s", volumeUuid.c_str()); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1146 | std::string BuildDataMediaCePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | fc505c3 | 2015-12-07 17:27:01 -0700 | [diff] [blame] | 1147 | // TODO: unify with installd path generation logic |
| 1148 | std::string data(BuildDataPath(volumeUuid)); |
| 1149 | return StringPrintf("%s/media/%u", data.c_str(), userId); |
| 1150 | } |
| 1151 | |
Mohammad Samiul Islam | b459591 | 2022-03-07 20:27:06 +0000 | [diff] [blame] | 1152 | std::string BuildDataMiscCePath(const std::string& volumeUuid, userid_t userId) { |
| 1153 | return StringPrintf("%s/misc_ce/%u", BuildDataPath(volumeUuid).c_str(), userId); |
| 1154 | } |
| 1155 | |
| 1156 | std::string BuildDataMiscDePath(const std::string& volumeUuid, userid_t userId) { |
| 1157 | return StringPrintf("%s/misc_de/%u", BuildDataPath(volumeUuid).c_str(), userId); |
| 1158 | } |
| 1159 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1160 | std::string BuildDataUserCePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1161 | // TODO: unify with installd path generation logic |
| 1162 | std::string data(BuildDataPath(volumeUuid)); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1163 | if (volumeUuid.empty() && userId == 0) { |
cjbao | eb50114 | 2017-04-12 00:09:00 +0800 | [diff] [blame] | 1164 | std::string legacy = StringPrintf("%s/data", data.c_str()); |
| 1165 | struct stat sb; |
| 1166 | if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 1167 | /* /data/data is dir, return /data/data for legacy system */ |
| 1168 | return legacy; |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1169 | } |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1170 | } |
cjbao | eb50114 | 2017-04-12 00:09:00 +0800 | [diff] [blame] | 1171 | return StringPrintf("%s/user/%u", data.c_str(), userId); |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 1174 | std::string BuildDataUserDePath(const std::string& volumeUuid, userid_t userId) { |
Jeff Sharkey | d2c96e7 | 2015-11-08 17:56:23 -0800 | [diff] [blame] | 1175 | // TODO: unify with installd path generation logic |
| 1176 | std::string data(BuildDataPath(volumeUuid)); |
| 1177 | return StringPrintf("%s/user_de/%u", data.c_str(), userId); |
| 1178 | } |
| 1179 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1180 | dev_t GetDevice(const std::string& path) { |
| 1181 | struct stat sb; |
| 1182 | if (stat(path.c_str(), &sb)) { |
| 1183 | PLOG(WARNING) << "Failed to stat " << path; |
| 1184 | return 0; |
| 1185 | } else { |
| 1186 | return sb.st_dev; |
| 1187 | } |
| 1188 | } |
| 1189 | |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 1190 | // Returns true if |path1| names the same existing file or directory as |path2|. |
| 1191 | bool IsSameFile(const std::string& path1, const std::string& path2) { |
| 1192 | struct stat stbuf1, stbuf2; |
| 1193 | if (stat(path1.c_str(), &stbuf1) != 0 || stat(path2.c_str(), &stbuf2) != 0) return false; |
| 1194 | return stbuf1.st_ino == stbuf2.st_ino && stbuf1.st_dev == stbuf2.st_dev; |
| 1195 | } |
| 1196 | |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 1197 | status_t RestoreconRecursive(const std::string& path) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 1198 | LOG(DEBUG) << "Starting restorecon of " << path; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 1199 | |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 1200 | static constexpr const char* kRestoreconString = "selinux.restorecon_recursive"; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 1201 | |
Tom Cherry | d6127ef | 2017-06-15 17:13:56 -0700 | [diff] [blame] | 1202 | android::base::SetProperty(kRestoreconString, ""); |
| 1203 | android::base::SetProperty(kRestoreconString, path); |
| 1204 | |
| 1205 | android::base::WaitForProperty(kRestoreconString, path); |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 1206 | |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 1207 | LOG(DEBUG) << "Finished restorecon of " << path; |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 1208 | return OK; |
| 1209 | } |
| 1210 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1211 | bool Readlinkat(int dirfd, const std::string& path, std::string* result) { |
| 1212 | // Shamelessly borrowed from android::base::Readlink() |
| 1213 | result->clear(); |
| 1214 | |
| 1215 | // Most Linux file systems (ext2 and ext4, say) limit symbolic links to |
| 1216 | // 4095 bytes. Since we'll copy out into the string anyway, it doesn't |
| 1217 | // waste memory to just start there. We add 1 so that we can recognize |
| 1218 | // whether it actually fit (rather than being truncated to 4095). |
| 1219 | std::vector<char> buf(4095 + 1); |
| 1220 | while (true) { |
| 1221 | ssize_t size = readlinkat(dirfd, path.c_str(), &buf[0], buf.size()); |
| 1222 | // Unrecoverable error? |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 1223 | if (size == -1) return false; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1224 | // It fit! (If size == buf.size(), it may have been truncated.) |
| 1225 | if (static_cast<size_t>(size) < buf.size()) { |
| 1226 | result->assign(&buf[0], size); |
| 1227 | return true; |
| 1228 | } |
| 1229 | // Double our buffer and try again. |
| 1230 | buf.resize(buf.size() * 2); |
Daichi Hirono | 10d3488 | 2016-01-29 14:33:51 +0900 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
Alistair Delva | ff1fc9b | 2020-05-14 16:35:03 -0700 | [diff] [blame] | 1234 | static unsigned int GetMajorBlockVirtioBlk() { |
| 1235 | std::string devices; |
| 1236 | if (!ReadFileToString(kProcDevices, &devices)) { |
| 1237 | PLOG(ERROR) << "Unable to open /proc/devices"; |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | bool blockSection = false; |
| 1242 | for (auto line : android::base::Split(devices, "\n")) { |
| 1243 | if (line == "Block devices:") { |
| 1244 | blockSection = true; |
| 1245 | } else if (line == "Character devices:") { |
| 1246 | blockSection = false; |
| 1247 | } else if (blockSection) { |
| 1248 | auto tokens = android::base::Split(line, " "); |
| 1249 | if (tokens.size() == 2 && tokens[1] == "virtblk") { |
| 1250 | return std::stoul(tokens[0]); |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | bool IsVirtioBlkDevice(unsigned int major) { |
| 1259 | // Most virtualized platforms expose block devices with the virtio-blk |
| 1260 | // block device driver. Unfortunately, this driver does not use a fixed |
| 1261 | // major number, but relies on the kernel to assign one from a specific |
| 1262 | // range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE" |
| 1263 | // per Documentation/devices.txt. This is true even for the latest Linux |
| 1264 | // kernel (4.4; see init() in drivers/block/virtio_blk.c). |
| 1265 | static unsigned int kMajorBlockVirtioBlk = GetMajorBlockVirtioBlk(); |
| 1266 | return kMajorBlockVirtioBlk && major == kMajorBlockVirtioBlk; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 1267 | } |
| 1268 | |
Sudheer Shanka | 295fb24 | 2019-01-16 23:04:07 -0800 | [diff] [blame] | 1269 | status_t UnmountTree(const std::string& mountPoint) { |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 1270 | if (TEMP_FAILURE_RETRY(umount2(mountPoint.c_str(), MNT_DETACH)) < 0 && errno != EINVAL && |
| 1271 | errno != ENOENT) { |
Sudheer Shanka | 295fb24 | 2019-01-16 23:04:07 -0800 | [diff] [blame] | 1272 | PLOG(ERROR) << "Failed to unmount " << mountPoint; |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 1273 | return -errno; |
| 1274 | } |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 1275 | return OK; |
| 1276 | } |
| 1277 | |
Eric Biggers | 7bcf427 | 2020-11-02 15:31:56 -0800 | [diff] [blame] | 1278 | bool IsDotOrDotDot(const struct dirent& ent) { |
| 1279 | return strcmp(ent.d_name, ".") == 0 || strcmp(ent.d_name, "..") == 0; |
| 1280 | } |
| 1281 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1282 | static status_t delete_dir_contents(DIR* dir) { |
| 1283 | // Shamelessly borrowed from android::installd |
| 1284 | int dfd = dirfd(dir); |
| 1285 | if (dfd < 0) { |
| 1286 | return -errno; |
| 1287 | } |
| 1288 | |
Sudheer Shanka | 6bf1480 | 2019-01-17 13:38:10 -0800 | [diff] [blame] | 1289 | status_t result = OK; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1290 | struct dirent* de; |
| 1291 | while ((de = readdir(dir))) { |
| 1292 | const char* name = de->d_name; |
| 1293 | if (de->d_type == DT_DIR) { |
| 1294 | /* always skip "." and ".." */ |
Eric Biggers | 7bcf427 | 2020-11-02 15:31:56 -0800 | [diff] [blame] | 1295 | if (IsDotOrDotDot(*de)) continue; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1296 | |
| 1297 | android::base::unique_fd subfd( |
| 1298 | openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)); |
| 1299 | if (subfd.get() == -1) { |
| 1300 | PLOG(ERROR) << "Couldn't openat " << name; |
| 1301 | result = -errno; |
| 1302 | continue; |
| 1303 | } |
Josh Gao | e3c32e0 | 2018-11-05 13:47:28 -0800 | [diff] [blame] | 1304 | std::unique_ptr<DIR, decltype(&closedir)> subdirp( |
| 1305 | android::base::Fdopendir(std::move(subfd)), closedir); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1306 | if (!subdirp) { |
| 1307 | PLOG(ERROR) << "Couldn't fdopendir " << name; |
| 1308 | result = -errno; |
| 1309 | continue; |
| 1310 | } |
| 1311 | result = delete_dir_contents(subdirp.get()); |
| 1312 | if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { |
| 1313 | PLOG(ERROR) << "Couldn't unlinkat " << name; |
| 1314 | result = -errno; |
| 1315 | } |
| 1316 | } else { |
| 1317 | if (unlinkat(dfd, name, 0) < 0) { |
| 1318 | PLOG(ERROR) << "Couldn't unlinkat " << name; |
| 1319 | result = -errno; |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | return result; |
| 1324 | } |
| 1325 | |
| 1326 | status_t DeleteDirContentsAndDir(const std::string& pathname) { |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 1327 | status_t res = DeleteDirContents(pathname); |
| 1328 | if (res < 0) { |
| 1329 | return res; |
| 1330 | } |
Sudheer Shanka | 8255a2b | 2019-02-25 12:21:23 -0800 | [diff] [blame] | 1331 | if (TEMP_FAILURE_RETRY(rmdir(pathname.c_str())) < 0 && errno != ENOENT) { |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 1332 | PLOG(ERROR) << "rmdir failed on " << pathname; |
| 1333 | return -errno; |
| 1334 | } |
| 1335 | LOG(VERBOSE) << "Success: rmdir on " << pathname; |
| 1336 | return OK; |
| 1337 | } |
| 1338 | |
| 1339 | status_t DeleteDirContents(const std::string& pathname) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1340 | // Shamelessly borrowed from android::installd |
| 1341 | std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir(pathname.c_str()), closedir); |
| 1342 | if (!dirp) { |
| 1343 | if (errno == ENOENT) { |
| 1344 | return OK; |
| 1345 | } |
| 1346 | PLOG(ERROR) << "Failed to opendir " << pathname; |
| 1347 | return -errno; |
| 1348 | } |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 1349 | return delete_dir_contents(dirp.get()); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Paul Crowley | 298fa32 | 2018-10-30 15:59:24 -0700 | [diff] [blame] | 1352 | // TODO(118708649): fix duplication with init/util.h |
| 1353 | status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout) { |
| 1354 | android::base::Timer t; |
| 1355 | while (t.duration() < timeout) { |
| 1356 | struct stat sb; |
| 1357 | if (stat(filename, &sb) != -1) { |
| 1358 | LOG(INFO) << "wait for '" << filename << "' took " << t; |
| 1359 | return 0; |
| 1360 | } |
| 1361 | std::this_thread::sleep_for(10ms); |
| 1362 | } |
| 1363 | LOG(WARNING) << "wait for '" << filename << "' timed out and took " << t; |
| 1364 | return -1; |
| 1365 | } |
| 1366 | |
Eric Biggers | bd138dd | 2021-02-16 15:59:17 -0800 | [diff] [blame] | 1367 | bool pathExists(const std::string& path) { |
| 1368 | return access(path.c_str(), F_OK) == 0; |
| 1369 | } |
| 1370 | |
Paul Crowley | 621d9b9 | 2018-12-07 15:36:09 -0800 | [diff] [blame] | 1371 | bool FsyncDirectory(const std::string& dirname) { |
| 1372 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dirname.c_str(), O_RDONLY | O_CLOEXEC))); |
| 1373 | if (fd == -1) { |
| 1374 | PLOG(ERROR) << "Failed to open " << dirname; |
| 1375 | return false; |
| 1376 | } |
| 1377 | if (fsync(fd) == -1) { |
| 1378 | if (errno == EROFS || errno == EINVAL) { |
| 1379 | PLOG(WARNING) << "Skip fsync " << dirname |
| 1380 | << " on a file system does not support synchronization"; |
| 1381 | } else { |
| 1382 | PLOG(ERROR) << "Failed to fsync " << dirname; |
| 1383 | return false; |
| 1384 | } |
| 1385 | } |
| 1386 | return true; |
| 1387 | } |
| 1388 | |
Eric Biggers | 3345a2a | 2021-02-16 15:59:17 -0800 | [diff] [blame] | 1389 | bool FsyncParentDirectory(const std::string& path) { |
| 1390 | return FsyncDirectory(android::base::Dirname(path)); |
| 1391 | } |
| 1392 | |
Eric Biggers | fec0c0e | 2021-02-16 15:59:17 -0800 | [diff] [blame] | 1393 | // Creates all parent directories of |path| that don't already exist. Assigns |
| 1394 | // the specified |mode| to any new directories, and also fsync()s their parent |
| 1395 | // directories so that the new directories get written to disk right away. |
| 1396 | bool MkdirsSync(const std::string& path, mode_t mode) { |
| 1397 | if (path[0] != '/') { |
| 1398 | LOG(ERROR) << "MkdirsSync() needs an absolute path, but got " << path; |
| 1399 | return false; |
| 1400 | } |
| 1401 | std::vector<std::string> components = android::base::Split(android::base::Dirname(path), "/"); |
| 1402 | |
| 1403 | std::string current_dir = "/"; |
| 1404 | for (const std::string& component : components) { |
| 1405 | if (component.empty()) continue; |
| 1406 | |
| 1407 | std::string parent_dir = current_dir; |
| 1408 | if (current_dir != "/") current_dir += "/"; |
| 1409 | current_dir += component; |
| 1410 | |
| 1411 | if (!pathExists(current_dir)) { |
| 1412 | if (mkdir(current_dir.c_str(), mode) != 0) { |
| 1413 | PLOG(ERROR) << "Failed to create " << current_dir; |
| 1414 | return false; |
| 1415 | } |
| 1416 | if (!FsyncDirectory(parent_dir)) return false; |
| 1417 | LOG(DEBUG) << "Created directory " << current_dir; |
| 1418 | } |
| 1419 | } |
| 1420 | return true; |
| 1421 | } |
| 1422 | |
Tommy Chiu | 0bd2d11 | 2019-03-26 17:18:09 +0800 | [diff] [blame] | 1423 | bool writeStringToFile(const std::string& payload, const std::string& filename) { |
| 1424 | android::base::unique_fd fd(TEMP_FAILURE_RETRY( |
| 1425 | open(filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0666))); |
| 1426 | if (fd == -1) { |
| 1427 | PLOG(ERROR) << "Failed to open " << filename; |
| 1428 | return false; |
| 1429 | } |
| 1430 | if (!android::base::WriteStringToFd(payload, fd)) { |
| 1431 | PLOG(ERROR) << "Failed to write to " << filename; |
| 1432 | unlink(filename.c_str()); |
| 1433 | return false; |
| 1434 | } |
| 1435 | // fsync as close won't guarantee flush data |
| 1436 | // see close(2), fsync(2) and b/68901441 |
| 1437 | if (fsync(fd) == -1) { |
| 1438 | if (errno == EROFS || errno == EINVAL) { |
| 1439 | PLOG(WARNING) << "Skip fsync " << filename |
| 1440 | << " on a file system does not support synchronization"; |
| 1441 | } else { |
| 1442 | PLOG(ERROR) << "Failed to fsync " << filename; |
| 1443 | unlink(filename.c_str()); |
| 1444 | return false; |
| 1445 | } |
| 1446 | } |
| 1447 | return true; |
| 1448 | } |
| 1449 | |
Martijn Coenen | 23c0445 | 2020-04-29 07:49:41 +0200 | [diff] [blame] | 1450 | status_t AbortFuseConnections() { |
| 1451 | namespace fs = std::filesystem; |
| 1452 | |
| 1453 | for (const auto& itEntry : fs::directory_iterator("/sys/fs/fuse/connections")) { |
Tao Wu | 3d98962 | 2020-08-06 19:31:21 -0700 | [diff] [blame] | 1454 | std::string fsPath = itEntry.path().string() + "/filesystem"; |
| 1455 | std::string fs; |
| 1456 | |
| 1457 | // Virtiofs is on top of fuse and there isn't any user space daemon. |
| 1458 | // Android user space doesn't manage it. |
| 1459 | if (android::base::ReadFileToString(fsPath, &fs, false) && |
| 1460 | android::base::Trim(fs) == "virtiofs") { |
| 1461 | LOG(INFO) << "Ignore virtiofs connection entry " << itEntry.path().string(); |
| 1462 | continue; |
| 1463 | } |
| 1464 | |
Martijn Coenen | 23c0445 | 2020-04-29 07:49:41 +0200 | [diff] [blame] | 1465 | std::string abortPath = itEntry.path().string() + "/abort"; |
| 1466 | LOG(DEBUG) << "Aborting fuse connection entry " << abortPath; |
| 1467 | bool ret = writeStringToFile("1", abortPath); |
| 1468 | if (!ret) { |
| 1469 | LOG(WARNING) << "Failed to write to " << abortPath; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | return OK; |
| 1474 | } |
| 1475 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 1476 | status_t EnsureDirExists(const std::string& path, mode_t mode, uid_t uid, gid_t gid) { |
| 1477 | if (access(path.c_str(), F_OK) != 0) { |
| 1478 | PLOG(WARNING) << "Dir does not exist: " << path; |
| 1479 | if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) { |
| 1480 | return -errno; |
| 1481 | } |
| 1482 | } |
| 1483 | return OK; |
| 1484 | } |
| 1485 | |
Martijn Coenen | a485006 | 2020-06-29 11:53:34 +0200 | [diff] [blame] | 1486 | // Gets the sysfs path for parameters of the backing device info (bdi) |
| 1487 | static std::string getBdiPathForMount(const std::string& mount) { |
| 1488 | // First figure out MAJOR:MINOR of mount. Simplest way is to stat the path. |
| 1489 | struct stat info; |
| 1490 | if (stat(mount.c_str(), &info) != 0) { |
| 1491 | PLOG(ERROR) << "Failed to stat " << mount; |
| 1492 | return ""; |
| 1493 | } |
| 1494 | unsigned int maj = major(info.st_dev); |
| 1495 | unsigned int min = minor(info.st_dev); |
| 1496 | |
| 1497 | return StringPrintf("/sys/class/bdi/%u:%u", maj, min); |
| 1498 | } |
| 1499 | |
| 1500 | // Configures max_ratio for the FUSE filesystem. |
| 1501 | void ConfigureMaxDirtyRatioForFuse(const std::string& fuse_mount, unsigned int max_ratio) { |
| 1502 | LOG(INFO) << "Configuring max_ratio of " << fuse_mount << " fuse filesystem to " << max_ratio; |
| 1503 | if (max_ratio > 100) { |
| 1504 | LOG(ERROR) << "Invalid max_ratio: " << max_ratio; |
| 1505 | return; |
| 1506 | } |
| 1507 | std::string fuseBdiPath = getBdiPathForMount(fuse_mount); |
| 1508 | if (fuseBdiPath == "") { |
| 1509 | return; |
| 1510 | } |
| 1511 | std::string max_ratio_file = StringPrintf("%s/max_ratio", fuseBdiPath.c_str()); |
| 1512 | unique_fd fd(TEMP_FAILURE_RETRY(open(max_ratio_file.c_str(), O_WRONLY | O_CLOEXEC))); |
| 1513 | if (fd.get() == -1) { |
| 1514 | PLOG(ERROR) << "Failed to open " << max_ratio_file; |
| 1515 | return; |
| 1516 | } |
| 1517 | LOG(INFO) << "Writing " << max_ratio << " to " << max_ratio_file; |
| 1518 | if (!WriteStringToFd(std::to_string(max_ratio), fd)) { |
| 1519 | PLOG(ERROR) << "Failed to write to " << max_ratio_file; |
| 1520 | } |
| 1521 | } |
| 1522 | |
Nikita Ioffe | dcee5c1 | 2020-06-12 12:59:45 +0100 | [diff] [blame] | 1523 | // Configures read ahead property of the fuse filesystem with the mount point |fuse_mount| by |
| 1524 | // writing |read_ahead_kb| to the /sys/class/bdi/MAJOR:MINOR/read_ahead_kb. |
| 1525 | void ConfigureReadAheadForFuse(const std::string& fuse_mount, size_t read_ahead_kb) { |
| 1526 | LOG(INFO) << "Configuring read_ahead of " << fuse_mount << " fuse filesystem to " |
| 1527 | << read_ahead_kb << "kb"; |
Martijn Coenen | a485006 | 2020-06-29 11:53:34 +0200 | [diff] [blame] | 1528 | std::string fuseBdiPath = getBdiPathForMount(fuse_mount); |
| 1529 | if (fuseBdiPath == "") { |
Nikita Ioffe | dcee5c1 | 2020-06-12 12:59:45 +0100 | [diff] [blame] | 1530 | return; |
| 1531 | } |
Martijn Coenen | a485006 | 2020-06-29 11:53:34 +0200 | [diff] [blame] | 1532 | // We found the bdi path for our filesystem, time to configure read ahead! |
| 1533 | std::string read_ahead_file = StringPrintf("%s/read_ahead_kb", fuseBdiPath.c_str()); |
Nikita Ioffe | dcee5c1 | 2020-06-12 12:59:45 +0100 | [diff] [blame] | 1534 | unique_fd fd(TEMP_FAILURE_RETRY(open(read_ahead_file.c_str(), O_WRONLY | O_CLOEXEC))); |
| 1535 | if (fd.get() == -1) { |
| 1536 | PLOG(ERROR) << "Failed to open " << read_ahead_file; |
| 1537 | return; |
| 1538 | } |
| 1539 | LOG(INFO) << "Writing " << read_ahead_kb << " to " << read_ahead_file; |
| 1540 | if (!WriteStringToFd(std::to_string(read_ahead_kb), fd)) { |
| 1541 | PLOG(ERROR) << "Failed to write to " << read_ahead_file; |
| 1542 | } |
| 1543 | } |
| 1544 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1545 | status_t MountUserFuse(userid_t user_id, const std::string& absolute_lower_path, |
| 1546 | const std::string& relative_upper_path, android::base::unique_fd* fuse_fd) { |
| 1547 | std::string pre_fuse_path(StringPrintf("/mnt/user/%d", user_id)); |
| 1548 | std::string fuse_path( |
| 1549 | StringPrintf("%s/%s", pre_fuse_path.c_str(), relative_upper_path.c_str())); |
| 1550 | |
| 1551 | std::string pre_pass_through_path(StringPrintf("/mnt/pass_through/%d", user_id)); |
| 1552 | std::string pass_through_path( |
| 1553 | StringPrintf("%s/%s", pre_pass_through_path.c_str(), relative_upper_path.c_str())); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1554 | |
Zim | 1242be8 | 2020-01-22 18:22:29 +0000 | [diff] [blame] | 1555 | // Ensure that /mnt/user is 0700. With FUSE, apps don't need access to /mnt/user paths directly. |
| 1556 | // Without FUSE however, apps need /mnt/user access so /mnt/user in init.rc is 0755 until here |
Zim | 4dd4709 | 2020-01-29 02:44:46 +0000 | [diff] [blame] | 1557 | auto result = PrepareDir("/mnt/user", 0750, AID_ROOT, AID_MEDIA_RW); |
Zim | 1242be8 | 2020-01-22 18:22:29 +0000 | [diff] [blame] | 1558 | if (result != android::OK) { |
| 1559 | PLOG(ERROR) << "Failed to prepare directory /mnt/user"; |
| 1560 | return -1; |
| 1561 | } |
| 1562 | |
Zim | 06b0caf | 2020-01-05 02:11:47 +0000 | [diff] [blame] | 1563 | // Shell is neither AID_ROOT nor AID_EVERYBODY. Since it equally needs 'execute' access to |
| 1564 | // /mnt/user/0 to 'adb shell ls /sdcard' for instance, we set the uid bit of /mnt/user/0 to |
| 1565 | // AID_SHELL. This gives shell access along with apps running as group everybody (user 0 apps) |
| 1566 | // These bits should be consistent with what is set in zygote in |
| 1567 | // com_android_internal_os_Zygote#MountEmulatedStorage on volume bind mount during app fork |
Zim | 1242be8 | 2020-01-22 18:22:29 +0000 | [diff] [blame] | 1568 | result = PrepareDir(pre_fuse_path, 0710, user_id ? AID_ROOT : AID_SHELL, |
Zim | 06b0caf | 2020-01-05 02:11:47 +0000 | [diff] [blame] | 1569 | multiuser_get_uid(user_id, AID_EVERYBODY)); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1570 | if (result != android::OK) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1571 | PLOG(ERROR) << "Failed to prepare directory " << pre_fuse_path; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1572 | return -1; |
| 1573 | } |
| 1574 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1575 | result = PrepareDir(fuse_path, 0700, AID_ROOT, AID_ROOT); |
| 1576 | if (result != android::OK) { |
| 1577 | PLOG(ERROR) << "Failed to prepare directory " << fuse_path; |
| 1578 | return -1; |
| 1579 | } |
| 1580 | |
Zim | 26eec70 | 2020-01-31 16:00:58 +0000 | [diff] [blame] | 1581 | result = PrepareDir(pre_pass_through_path, 0710, AID_ROOT, AID_MEDIA_RW); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1582 | if (result != android::OK) { |
| 1583 | PLOG(ERROR) << "Failed to prepare directory " << pre_pass_through_path; |
| 1584 | return -1; |
| 1585 | } |
| 1586 | |
Zim | 26eec70 | 2020-01-31 16:00:58 +0000 | [diff] [blame] | 1587 | result = PrepareDir(pass_through_path, 0710, AID_ROOT, AID_MEDIA_RW); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1588 | if (result != android::OK) { |
| 1589 | PLOG(ERROR) << "Failed to prepare directory " << pass_through_path; |
| 1590 | return -1; |
| 1591 | } |
| 1592 | |
| 1593 | if (relative_upper_path == "emulated") { |
Zim | e5393d4 | 2019-11-15 11:44:12 +0000 | [diff] [blame] | 1594 | std::string linkpath(StringPrintf("/mnt/user/%d/self", user_id)); |
| 1595 | result = PrepareDir(linkpath, 0755, AID_ROOT, AID_ROOT); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1596 | if (result != android::OK) { |
Zim | e5393d4 | 2019-11-15 11:44:12 +0000 | [diff] [blame] | 1597 | PLOG(ERROR) << "Failed to prepare directory " << linkpath; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1598 | return -1; |
| 1599 | } |
Zim | e5393d4 | 2019-11-15 11:44:12 +0000 | [diff] [blame] | 1600 | linkpath += "/primary"; |
Zim | 53d16d3 | 2020-01-17 01:21:24 +0000 | [diff] [blame] | 1601 | Symlink("/storage/emulated/" + std::to_string(user_id), linkpath); |
Zim | aea1247 | 2020-01-08 11:09:47 +0000 | [diff] [blame] | 1602 | |
| 1603 | std::string pass_through_linkpath(StringPrintf("/mnt/pass_through/%d/self", user_id)); |
Zim | 26eec70 | 2020-01-31 16:00:58 +0000 | [diff] [blame] | 1604 | result = PrepareDir(pass_through_linkpath, 0710, AID_ROOT, AID_MEDIA_RW); |
Zim | aea1247 | 2020-01-08 11:09:47 +0000 | [diff] [blame] | 1605 | if (result != android::OK) { |
| 1606 | PLOG(ERROR) << "Failed to prepare directory " << pass_through_linkpath; |
| 1607 | return -1; |
| 1608 | } |
| 1609 | pass_through_linkpath += "/primary"; |
Zim | 53d16d3 | 2020-01-17 01:21:24 +0000 | [diff] [blame] | 1610 | Symlink("/storage/emulated/" + std::to_string(user_id), pass_through_linkpath); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame] | 1613 | // Open fuse fd. |
| 1614 | fuse_fd->reset(open("/dev/fuse", O_RDWR | O_CLOEXEC)); |
| 1615 | if (fuse_fd->get() == -1) { |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1616 | PLOG(ERROR) << "Failed to open /dev/fuse"; |
| 1617 | return -1; |
| 1618 | } |
| 1619 | |
| 1620 | // Note: leaving out default_permissions since we don't want kernel to do lower filesystem |
| 1621 | // permission checks before routing to FUSE daemon. |
| 1622 | const auto opts = StringPrintf( |
| 1623 | "fd=%i," |
| 1624 | "rootmode=40000," |
| 1625 | "allow_other," |
| 1626 | "user_id=0,group_id=0,", |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame] | 1627 | fuse_fd->get()); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1628 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1629 | result = TEMP_FAILURE_RETRY(mount("/dev/fuse", fuse_path.c_str(), "fuse", |
| 1630 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_LAZYTIME, |
| 1631 | opts.c_str())); |
| 1632 | if (result != 0) { |
| 1633 | PLOG(ERROR) << "Failed to mount " << fuse_path; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1634 | return -errno; |
| 1635 | } |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 1636 | |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 1637 | if (IsSdcardfsUsed()) { |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 1638 | std::string sdcardfs_path( |
| 1639 | StringPrintf("/mnt/runtime/full/%s", relative_upper_path.c_str())); |
| 1640 | |
| 1641 | LOG(INFO) << "Bind mounting " << sdcardfs_path << " to " << pass_through_path; |
| 1642 | return BindMount(sdcardfs_path, pass_through_path); |
| 1643 | } else { |
| 1644 | LOG(INFO) << "Bind mounting " << absolute_lower_path << " to " << pass_through_path; |
| 1645 | return BindMount(absolute_lower_path, pass_through_path); |
| 1646 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1647 | } |
| 1648 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 1649 | status_t UnmountUserFuse(userid_t user_id, const std::string& absolute_lower_path, |
| 1650 | const std::string& relative_upper_path) { |
| 1651 | std::string fuse_path(StringPrintf("/mnt/user/%d/%s", user_id, relative_upper_path.c_str())); |
| 1652 | std::string pass_through_path( |
| 1653 | StringPrintf("/mnt/pass_through/%d/%s", user_id, relative_upper_path.c_str())); |
| 1654 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 1655 | LOG(INFO) << "Unmounting fuse path " << fuse_path; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1656 | android::status_t result = ForceUnmount(fuse_path); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1657 | if (result != android::OK) { |
| 1658 | // TODO(b/135341433): MNT_DETACH is needed for fuse because umount2 can fail with EBUSY. |
| 1659 | // Figure out why we get EBUSY and remove this special casing if possible. |
| 1660 | PLOG(ERROR) << "Failed to unmount. Trying MNT_DETACH " << fuse_path << " ..."; |
| 1661 | if (umount2(fuse_path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) && errno != EINVAL && |
| 1662 | errno != ENOENT) { |
| 1663 | PLOG(ERROR) << "Failed to unmount with MNT_DETACH " << fuse_path; |
| 1664 | return -errno; |
| 1665 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 1666 | result = android::OK; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1667 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 1668 | rmdir(fuse_path.c_str()); |
| 1669 | |
Martijn Coenen | 64b3bba | 2020-11-05 10:34:28 +0100 | [diff] [blame] | 1670 | LOG(INFO) << "Unmounting pass_through_path " << pass_through_path; |
| 1671 | auto status = ForceUnmount(pass_through_path); |
| 1672 | if (status != android::OK) { |
| 1673 | LOG(ERROR) << "Failed to unmount " << pass_through_path; |
| 1674 | } |
| 1675 | rmdir(pass_through_path.c_str()); |
| 1676 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 1677 | return result; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 1678 | } |
| 1679 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1680 | status_t PrepareAndroidDirs(const std::string& volumeRoot) { |
| 1681 | std::string androidDir = volumeRoot + kAndroidDir; |
| 1682 | std::string androidDataDir = volumeRoot + kAppDataDir; |
| 1683 | std::string androidObbDir = volumeRoot + kAppObbDir; |
Zim | a13d81b | 2020-02-07 16:39:31 +0000 | [diff] [blame] | 1684 | std::string androidMediaDir = volumeRoot + kAppMediaDir; |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1685 | |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 1686 | bool useSdcardFs = IsSdcardfsUsed(); |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1687 | |
Martijn Coenen | 10570c0 | 2020-02-18 10:41:37 +0100 | [diff] [blame] | 1688 | // mode 0771 + sticky bit for inheriting GIDs |
| 1689 | mode_t mode = S_IRWXU | S_IRWXG | S_IXOTH | S_ISGID; |
| 1690 | if (fs_prepare_dir(androidDir.c_str(), mode, AID_MEDIA_RW, AID_MEDIA_RW) != 0) { |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1691 | PLOG(ERROR) << "Failed to create " << androidDir; |
| 1692 | return -errno; |
| 1693 | } |
| 1694 | |
| 1695 | gid_t dataGid = useSdcardFs ? AID_MEDIA_RW : AID_EXT_DATA_RW; |
Martijn Coenen | 10570c0 | 2020-02-18 10:41:37 +0100 | [diff] [blame] | 1696 | if (fs_prepare_dir(androidDataDir.c_str(), mode, AID_MEDIA_RW, dataGid) != 0) { |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1697 | PLOG(ERROR) << "Failed to create " << androidDataDir; |
| 1698 | return -errno; |
| 1699 | } |
| 1700 | |
| 1701 | gid_t obbGid = useSdcardFs ? AID_MEDIA_RW : AID_EXT_OBB_RW; |
Martijn Coenen | 10570c0 | 2020-02-18 10:41:37 +0100 | [diff] [blame] | 1702 | if (fs_prepare_dir(androidObbDir.c_str(), mode, AID_MEDIA_RW, obbGid) != 0) { |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1703 | PLOG(ERROR) << "Failed to create " << androidObbDir; |
| 1704 | return -errno; |
| 1705 | } |
Martijn Coenen | 442bb83 | 2020-02-18 13:44:59 +0100 | [diff] [blame] | 1706 | // Some other apps, like installers, have write access to the OBB directory |
| 1707 | // to pre-download them. To make sure newly created folders in this directory |
| 1708 | // have the right permissions, set a default ACL. |
Martijn Coenen | 1129b81 | 2020-06-16 14:58:52 +0200 | [diff] [blame] | 1709 | SetDefaultAcl(androidObbDir, mode, AID_MEDIA_RW, obbGid, {}); |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1710 | |
Martijn Coenen | 10570c0 | 2020-02-18 10:41:37 +0100 | [diff] [blame] | 1711 | if (fs_prepare_dir(androidMediaDir.c_str(), mode, AID_MEDIA_RW, AID_MEDIA_RW) != 0) { |
Zim | a13d81b | 2020-02-07 16:39:31 +0000 | [diff] [blame] | 1712 | PLOG(ERROR) << "Failed to create " << androidMediaDir; |
| 1713 | return -errno; |
| 1714 | } |
| 1715 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1716 | return OK; |
| 1717 | } |
Yurii Zubrytskyi | 4038982 | 2021-10-18 22:33:15 -0700 | [diff] [blame] | 1718 | |
| 1719 | namespace ab = android::base; |
| 1720 | |
| 1721 | static ab::unique_fd openDirFd(int parentFd, const char* name) { |
| 1722 | return ab::unique_fd{::openat(parentFd, name, O_CLOEXEC | O_DIRECTORY | O_PATH | O_NOFOLLOW)}; |
| 1723 | } |
| 1724 | |
| 1725 | static ab::unique_fd openAbsolutePathFd(std::string_view path) { |
| 1726 | if (path.empty() || path[0] != '/') { |
| 1727 | errno = EINVAL; |
| 1728 | return {}; |
| 1729 | } |
| 1730 | if (path == "/") { |
| 1731 | return openDirFd(-1, "/"); |
| 1732 | } |
| 1733 | |
| 1734 | // first component is special - it includes the leading slash |
| 1735 | auto next = path.find('/', 1); |
| 1736 | auto component = std::string(path.substr(0, next)); |
| 1737 | if (component == "..") { |
| 1738 | errno = EINVAL; |
| 1739 | return {}; |
| 1740 | } |
| 1741 | auto fd = openDirFd(-1, component.c_str()); |
| 1742 | if (!fd.ok()) { |
| 1743 | return fd; |
| 1744 | } |
| 1745 | path.remove_prefix(std::min(next + 1, path.size())); |
| 1746 | while (next != path.npos && !path.empty()) { |
| 1747 | next = path.find('/'); |
| 1748 | component.assign(path.substr(0, next)); |
| 1749 | fd = openDirFd(fd, component.c_str()); |
| 1750 | if (!fd.ok()) { |
| 1751 | return fd; |
| 1752 | } |
| 1753 | path.remove_prefix(std::min(next + 1, path.size())); |
| 1754 | } |
| 1755 | return fd; |
| 1756 | } |
| 1757 | |
| 1758 | std::pair<android::base::unique_fd, std::string> OpenDirInProcfs(std::string_view path) { |
| 1759 | auto fd = openAbsolutePathFd(path); |
| 1760 | if (!fd.ok()) { |
| 1761 | return {}; |
| 1762 | } |
| 1763 | |
| 1764 | auto linkPath = std::string("/proc/self/fd/") += std::to_string(fd.get()); |
| 1765 | return {std::move(fd), std::move(linkPath)}; |
| 1766 | } |
| 1767 | |
Alessio Balsini | 583ae3e | 2022-03-23 18:27:59 +0000 | [diff] [blame^] | 1768 | bool IsFuseBpfEnabled() { |
| 1769 | std::string bpf_override = android::base::GetProperty("persist.sys.fuse.bpf.override", ""); |
| 1770 | if (bpf_override == "true") { |
| 1771 | return true; |
| 1772 | } else if (bpf_override == "false") { |
| 1773 | return false; |
| 1774 | } |
| 1775 | return base::GetBoolProperty("ro.fuse.bpf.enabled", false); |
| 1776 | } |
| 1777 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1778 | } // namespace vold |
| 1779 | } // namespace android |