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