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