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