San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER |
| 18 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 19 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 20 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 22 | #include <mntent.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/mount.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 29 | #include <sys/sysmacros.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 30 | #include <sys/types.h> |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 31 | #include <sys/wait.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 32 | #include <unistd.h> |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 33 | #include <array> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 34 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 35 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 37 | #include <ApexProperties.sysprop.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 38 | #include <android-base/logging.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 39 | #include <android-base/parseint.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 41 | #include <android-base/stringprintf.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 42 | #include <android-base/strings.h> |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 43 | #include <async_safe/log.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 44 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 45 | #include <cutils/fs.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 46 | #include <utils/Trace.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 47 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 48 | #include <selinux/android.h> |
| 49 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 50 | #include <sysutils/NetlinkEvent.h> |
| 51 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 52 | #include <private/android_filesystem_config.h> |
| 53 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 54 | #include <fscrypt/fscrypt.h> |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 55 | |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 56 | #include "AppFuseUtil.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 57 | #include "Devmapper.h" |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 58 | #include "FsCrypt.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 59 | #include "Loop.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 60 | #include "NetlinkManager.h" |
| 61 | #include "Process.h" |
| 62 | #include "Utils.h" |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 63 | #include "VoldNativeService.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 64 | #include "VoldUtil.h" |
| 65 | #include "VolumeManager.h" |
| 66 | #include "cryptfs.h" |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 67 | #include "fs/Ext4.h" |
| 68 | #include "fs/Vfat.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 69 | #include "model/EmulatedVolume.h" |
| 70 | #include "model/ObbVolume.h" |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 71 | #include "model/PrivateVolume.h" |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 72 | #include "model/StubVolume.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 73 | |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 74 | using android::OK; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 75 | using android::base::GetBoolProperty; |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 76 | using android::base::StartsWith; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 77 | using android::base::StringAppendF; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 78 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 79 | using android::base::unique_fd; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 80 | using android::vold::BindMount; |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 81 | using android::vold::CreateDir; |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 82 | using android::vold::DeleteDirContents; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 83 | using android::vold::DeleteDirContentsAndDir; |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 84 | using android::vold::PrepareDirsFromRoot; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 85 | using android::vold::PrivateVolume; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 86 | using android::vold::Symlink; |
| 87 | using android::vold::Unlink; |
| 88 | using android::vold::UnmountTree; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 89 | using android::vold::VoldNativeService; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 90 | using android::vold::VolumeBase; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 91 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 92 | static const char* kPathUserMount = "/mnt/user"; |
| 93 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 94 | |
| 95 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 96 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 97 | static const std::string kEmptyString(""); |
| 98 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 99 | /* 512MiB is large enough for testing purposes */ |
| 100 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 101 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 102 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 103 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 104 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 105 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 106 | VolumeManager* VolumeManager::sInstance = NULL; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 107 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 108 | VolumeManager* VolumeManager::Instance() { |
| 109 | if (!sInstance) sInstance = new VolumeManager(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 110 | return sInstance; |
| 111 | } |
| 112 | |
| 113 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 114 | mDebug = false; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 115 | mNextObbId = 0; |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 116 | mNextStubVolumeId = 0; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 117 | // For security reasons, assume that a secure keyguard is |
| 118 | // showing until we hear otherwise |
| 119 | mSecureKeyguardShowing = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 122 | VolumeManager::~VolumeManager() {} |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 123 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 124 | int VolumeManager::updateVirtualDisk() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 125 | ATRACE_NAME("VolumeManager::updateVirtualDisk"); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 126 | if (GetBoolProperty(kPropVirtualDisk, false)) { |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 127 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 128 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 129 | } |
| 130 | |
| 131 | if (mVirtualDisk == nullptr) { |
| 132 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 133 | LOG(ERROR) << "Failed to create virtual disk"; |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | struct stat buf; |
| 138 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 139 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 140 | return -1; |
| 141 | } |
| 142 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 143 | auto disk = new android::vold::Disk( |
| 144 | "virtual", buf.st_rdev, "virtual", |
| 145 | android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 146 | mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 147 | handleDiskAdded(mVirtualDisk); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 148 | } |
| 149 | } else { |
| 150 | if (mVirtualDisk != nullptr) { |
| 151 | dev_t device = mVirtualDisk->getDevice(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 152 | handleDiskRemoved(device); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 153 | |
| 154 | Loop::destroyByDevice(mVirtualDiskPath.c_str()); |
| 155 | mVirtualDisk = nullptr; |
| 156 | } |
| 157 | |
| 158 | if (access(kPathVirtualDisk, F_OK) == 0) { |
| 159 | unlink(kPathVirtualDisk); |
| 160 | } |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 165 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 166 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 167 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 168 | } |
| 169 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 170 | int VolumeManager::start() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 171 | ATRACE_NAME("VolumeManager::start"); |
| 172 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 173 | // Always start from a clean slate by unmounting everything in |
| 174 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 175 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 176 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 177 | Devmapper::destroyAll(); |
| 178 | Loop::destroyAll(); |
| 179 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 180 | // Assume that we always have an emulated volume on internal |
| 181 | // storage; the framework will decide if it should be mounted. |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 182 | CHECK(mInternalEmulatedVolumes.empty()); |
| 183 | |
| 184 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 185 | new android::vold::EmulatedVolume("/data/media", 0)); |
| 186 | vol->setMountUserId(0); |
| 187 | vol->create(); |
| 188 | mInternalEmulatedVolumes.push_back(vol); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 189 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 190 | // Consider creating a virtual disk |
| 191 | updateVirtualDisk(); |
| 192 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int VolumeManager::stop() { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 197 | CHECK(!mInternalEmulatedVolumes.empty()); |
| 198 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 199 | vol->destroy(); |
| 200 | } |
| 201 | mInternalEmulatedVolumes.clear(); |
| 202 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 206 | void VolumeManager::handleBlockEvent(NetlinkEvent* evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 207 | std::lock_guard<std::mutex> lock(mLock); |
| 208 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 209 | if (mDebug) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 210 | LOG(DEBUG) << "----------------"; |
| 211 | LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 212 | evt->dump(); |
| 213 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 214 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 215 | std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : ""); |
| 216 | std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : ""); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 217 | |
| 218 | if (devType != "disk") return; |
| 219 | |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 220 | int major = std::stoi(evt->findParam("MAJOR")); |
| 221 | int minor = std::stoi(evt->findParam("MINOR")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 222 | dev_t device = makedev(major, minor); |
| 223 | |
| 224 | switch (evt->getAction()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 225 | case NetlinkEvent::Action::kAdd: { |
| 226 | for (const auto& source : mDiskSources) { |
| 227 | if (source->matches(eventPath)) { |
| 228 | // For now, assume that MMC and virtio-blk (the latter is |
| 229 | // emulator-specific; see Disk.cpp for details) devices are SD, |
| 230 | // and that everything else is USB |
| 231 | int flags = source->getFlags(); |
| 232 | if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() && |
| 233 | major >= (int)kMajorBlockExperimentalMin && |
| 234 | major <= (int)kMajorBlockExperimentalMax)) { |
| 235 | flags |= android::vold::Disk::Flags::kSd; |
| 236 | } else { |
| 237 | flags |= android::vold::Disk::Flags::kUsb; |
| 238 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 239 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 240 | auto disk = |
| 241 | new android::vold::Disk(eventPath, device, source->getNickname(), flags); |
| 242 | handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk)); |
| 243 | break; |
| 244 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 245 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 246 | break; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 247 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 248 | case NetlinkEvent::Action::kChange: { |
| 249 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
| 250 | handleDiskChanged(device); |
| 251 | break; |
| 252 | } |
| 253 | case NetlinkEvent::Action::kRemove: { |
| 254 | handleDiskRemoved(device); |
| 255 | break; |
| 256 | } |
| 257 | default: { |
| 258 | LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction(); |
| 259 | break; |
| 260 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 264 | void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) { |
| 265 | // For security reasons, if secure keyguard is showing, wait |
| 266 | // until the user unlocks the device to actually touch it |
| 267 | if (mSecureKeyguardShowing) { |
| 268 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 269 | << " but delaying scan due to secure keyguard"; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 270 | mPendingDisks.push_back(disk); |
| 271 | } else { |
| 272 | disk->create(); |
| 273 | mDisks.push_back(disk); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void VolumeManager::handleDiskChanged(dev_t device) { |
| 278 | for (const auto& disk : mDisks) { |
| 279 | if (disk->getDevice() == device) { |
| 280 | disk->readMetadata(); |
| 281 | disk->readPartitions(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // For security reasons, we ignore all pending disks, since |
| 286 | // we'll scan them once the device is unlocked |
| 287 | } |
| 288 | |
| 289 | void VolumeManager::handleDiskRemoved(dev_t device) { |
| 290 | auto i = mDisks.begin(); |
| 291 | while (i != mDisks.end()) { |
| 292 | if ((*i)->getDevice() == device) { |
| 293 | (*i)->destroy(); |
| 294 | i = mDisks.erase(i); |
| 295 | } else { |
| 296 | ++i; |
| 297 | } |
| 298 | } |
| 299 | auto j = mPendingDisks.begin(); |
| 300 | while (j != mPendingDisks.end()) { |
| 301 | if ((*j)->getDevice() == device) { |
| 302 | j = mPendingDisks.erase(j); |
| 303 | } else { |
| 304 | ++j; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 309 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 310 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 311 | mDiskSources.push_back(diskSource); |
| 312 | } |
| 313 | |
| 314 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 315 | for (auto disk : mDisks) { |
| 316 | if (disk->getId() == id) { |
| 317 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 320 | return nullptr; |
| 321 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 322 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 323 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 324 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 325 | if (vol->getId() == id) { |
| 326 | return vol; |
| 327 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 328 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 329 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 330 | auto vol = disk->findVolume(id); |
| 331 | if (vol != nullptr) { |
| 332 | return vol; |
| 333 | } |
| 334 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 335 | for (const auto& vol : mStubVolumes) { |
| 336 | if (vol->getId() == id) { |
| 337 | return vol; |
| 338 | } |
| 339 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 340 | for (const auto& vol : mObbVolumes) { |
| 341 | if (vol->getId() == id) { |
| 342 | return vol; |
| 343 | } |
| 344 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 345 | return nullptr; |
| 346 | } |
| 347 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 348 | void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, |
| 349 | std::list<std::string>& list) const { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 350 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 351 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 352 | disk->listVolumes(type, list); |
| 353 | } |
| 354 | } |
| 355 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 356 | int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) { |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 357 | std::string normalizedGuid; |
| 358 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 359 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 360 | return -1; |
| 361 | } |
| 362 | |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 363 | bool success = true; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 364 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 365 | if (unlink(keyPath.c_str()) != 0) { |
| 366 | LOG(ERROR) << "Failed to unlink " << keyPath; |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 367 | success = false; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 368 | } |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 369 | if (fscrypt_is_native()) { |
| 370 | if (!fscrypt_destroy_volume_keys(fsUuid)) { |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 371 | success = false; |
| 372 | } |
| 373 | } |
| 374 | return success ? 0 : -1; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 377 | int VolumeManager::linkPrimary(userid_t userId) { |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 378 | if (!GetBoolProperty(android::vold::kPropFuse, false)) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 379 | std::string source(mPrimary->getPath()); |
| 380 | if (mPrimary->isEmulated()) { |
| 381 | source = StringPrintf("%s/%d", source.c_str(), userId); |
| 382 | fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 383 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 384 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 385 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 386 | LOG(DEBUG) << "Linking " << source << " to " << target; |
| 387 | Symlink(source, target); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 388 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 392 | void VolumeManager::destroyEmulatedVolumesForUser(userid_t userId) { |
| 393 | // Destroy and remove all unstacked EmulatedVolumes for the user |
| 394 | auto i = mInternalEmulatedVolumes.begin(); |
| 395 | while (i != mInternalEmulatedVolumes.end()) { |
| 396 | auto vol = *i; |
| 397 | if (vol->getMountUserId() == userId) { |
| 398 | vol->destroy(); |
| 399 | i = mInternalEmulatedVolumes.erase(i); |
| 400 | } else { |
| 401 | i++; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Destroy and remove all stacked EmulatedVolumes for the user on each mounted private volume |
| 406 | std::list<std::string> private_vols; |
| 407 | listVolumes(VolumeBase::Type::kPrivate, private_vols); |
| 408 | for (const std::string& id : private_vols) { |
| 409 | PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get()); |
| 410 | std::list<std::shared_ptr<VolumeBase>> vols_to_remove; |
| 411 | if (pvol->getState() == VolumeBase::State::kMounted) { |
| 412 | for (const auto& vol : pvol->getVolumes()) { |
| 413 | if (vol->getMountUserId() == userId) { |
| 414 | vols_to_remove.push_back(vol); |
| 415 | } |
| 416 | } |
| 417 | for (const auto& vol : vols_to_remove) { |
| 418 | vol->destroy(); |
| 419 | pvol->removeVolume(vol); |
| 420 | } |
| 421 | } // else EmulatedVolumes will be destroyed on VolumeBase#unmount |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void VolumeManager::createEmulatedVolumesForUser(userid_t userId) { |
| 426 | // Create unstacked EmulatedVolumes for the user |
| 427 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 428 | new android::vold::EmulatedVolume("/data/media", userId)); |
| 429 | vol->setMountUserId(userId); |
| 430 | mInternalEmulatedVolumes.push_back(vol); |
| 431 | vol->create(); |
| 432 | |
| 433 | // Create stacked EmulatedVolumes for the user on each PrivateVolume |
| 434 | std::list<std::string> private_vols; |
| 435 | listVolumes(VolumeBase::Type::kPrivate, private_vols); |
| 436 | for (const std::string& id : private_vols) { |
| 437 | PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get()); |
| 438 | if (pvol->getState() == VolumeBase::State::kMounted) { |
| 439 | auto evol = |
| 440 | std::shared_ptr<android::vold::VolumeBase>(new android::vold::EmulatedVolume( |
| 441 | pvol->getPath() + "/media", pvol->getRawDevice(), pvol->getFsUuid(), |
| 442 | userId)); |
| 443 | evol->setMountUserId(userId); |
| 444 | pvol->addVolume(evol); |
| 445 | evol->create(); |
| 446 | } // else EmulatedVolumes will be created per user when on PrivateVolume#doMount |
| 447 | } |
| 448 | } |
| 449 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 450 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 451 | LOG(INFO) << "onUserAdded: " << userId; |
| 452 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 453 | mAddedUsers[userId] = userSerialNumber; |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | int VolumeManager::onUserRemoved(userid_t userId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 458 | LOG(INFO) << "onUserRemoved: " << userId; |
| 459 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 460 | onUserStopped(userId); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 461 | mAddedUsers.erase(userId); |
| 462 | return 0; |
| 463 | } |
| 464 | |
Sudheer Shanka | ebce4cc | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 465 | int VolumeManager::onUserStarted(userid_t userId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 466 | LOG(INFO) << "onUserStarted: " << userId; |
| 467 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 468 | if (mStartedUsers.find(userId) == mStartedUsers.end()) { |
| 469 | createEmulatedVolumesForUser(userId); |
| 470 | } |
| 471 | |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 472 | if (!GetBoolProperty(android::vold::kPropFuse, false)) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 473 | // Note that sometimes the system will spin up processes from Zygote |
| 474 | // before actually starting the user, so we're okay if Zygote |
| 475 | // already created this directory. |
| 476 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
| 477 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 478 | |
| 479 | if (mPrimary) { |
| 480 | linkPrimary(userId); |
| 481 | } |
| 482 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 483 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 484 | mStartedUsers.insert(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 488 | int VolumeManager::onUserStopped(userid_t userId) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 489 | LOG(VERBOSE) << "onUserStopped: " << userId; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 490 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 491 | if (mStartedUsers.find(userId) != mStartedUsers.end()) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 492 | destroyEmulatedVolumesForUser(userId); |
| 493 | } |
| 494 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 495 | mStartedUsers.erase(userId); |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 499 | int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) { |
| 500 | mSecureKeyguardShowing = isShowing; |
| 501 | if (!mSecureKeyguardShowing) { |
| 502 | // Now that secure keyguard has been dismissed, process |
| 503 | // any pending disks |
| 504 | for (const auto& disk : mPendingDisks) { |
| 505 | disk->create(); |
| 506 | mDisks.push_back(disk); |
| 507 | } |
| 508 | mPendingDisks.clear(); |
| 509 | } |
| 510 | return 0; |
| 511 | } |
| 512 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 513 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 514 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 515 | for (userid_t userId : mStartedUsers) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 516 | linkPrimary(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 517 | } |
| 518 | return 0; |
| 519 | } |
| 520 | |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 521 | // This code is executed after a fork so it's very important that the set of |
| 522 | // methods we call here is strictly limited. |
| 523 | // |
| 524 | // TODO: Get rid of this guesswork altogether and instead exec a process |
| 525 | // immediately after fork to do our bindding for us. |
| 526 | static bool childProcess(const char* storageSource, const char* userSource, int nsFd, |
| 527 | struct dirent* de) { |
| 528 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
| 529 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to setns for %s :%s", de->d_name, |
| 530 | strerror(errno)); |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | // NOTE: Inlined from vold::UnmountTree here to avoid using PLOG methods and |
| 535 | // to also protect against future changes that may cause issues across a |
| 536 | // fork. |
| 537 | if (TEMP_FAILURE_RETRY(umount2("/storage/", MNT_DETACH)) < 0 && errno != EINVAL && |
| 538 | errno != ENOENT) { |
| 539 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to unmount /storage/ :%s", |
| 540 | strerror(errno)); |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | if (TEMP_FAILURE_RETRY(mount(storageSource, "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 545 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s", |
| 546 | storageSource, de->d_name, strerror(errno)); |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) { |
| 551 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", |
| 552 | "Failed to set MS_SLAVE to /storage for %s :%s", de->d_name, |
| 553 | strerror(errno)); |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | if (TEMP_FAILURE_RETRY(mount(userSource, "/storage/self", NULL, MS_BIND, NULL)) == -1) { |
| 558 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s", |
| 559 | userSource, de->d_name, strerror(errno)); |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | return true; |
| 564 | } |
| 565 | |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 566 | int VolumeManager::remountUid(uid_t uid, int32_t mountMode) { |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 567 | if (GetBoolProperty(android::vold::kPropFuse, false)) { |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 568 | // TODO(135341433): Implement fuse specific logic. |
| 569 | return 0; |
| 570 | } |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 571 | std::string mode; |
| 572 | switch (mountMode) { |
| 573 | case VoldNativeService::REMOUNT_MODE_NONE: |
| 574 | mode = "none"; |
| 575 | break; |
| 576 | case VoldNativeService::REMOUNT_MODE_DEFAULT: |
| 577 | mode = "default"; |
| 578 | break; |
| 579 | case VoldNativeService::REMOUNT_MODE_READ: |
| 580 | mode = "read"; |
| 581 | break; |
| 582 | case VoldNativeService::REMOUNT_MODE_WRITE: |
Sudheer Shanka | a05ea74 | 2019-04-12 13:55:28 -0700 | [diff] [blame] | 583 | case VoldNativeService::REMOUNT_MODE_LEGACY: |
| 584 | case VoldNativeService::REMOUNT_MODE_INSTALLER: |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 585 | mode = "write"; |
| 586 | break; |
Sudheer Shanka | 36bdf7a | 2019-04-18 15:18:30 -0700 | [diff] [blame] | 587 | case VoldNativeService::REMOUNT_MODE_FULL: |
| 588 | mode = "full"; |
| 589 | break; |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 590 | case VoldNativeService::REMOUNT_MODE_PASS_THROUGH: |
| 591 | mode = "pass_through"; |
| 592 | break; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 593 | default: |
| 594 | PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode); |
| 595 | return -1; |
| 596 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 597 | LOG(DEBUG) << "Remounting " << uid << " as mode " << mode; |
| 598 | |
| 599 | DIR* dir; |
| 600 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 601 | std::string rootName; |
| 602 | std::string pidName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 603 | int pidFd; |
| 604 | int nsFd; |
| 605 | struct stat sb; |
| 606 | pid_t child; |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 607 | std::string userSource; |
| 608 | std::string storageSource; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 609 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 610 | static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false); |
| 611 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 612 | if (!(dir = opendir("/proc"))) { |
| 613 | PLOG(ERROR) << "Failed to opendir"; |
| 614 | return -1; |
| 615 | } |
| 616 | |
| 617 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 618 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
| 619 | PLOG(ERROR) << "Failed to read root namespace"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 620 | closedir(dir); |
| 621 | return -1; |
| 622 | } |
| 623 | |
| 624 | // Poke through all running PIDs look for apps running as UID |
| 625 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 626 | pid_t pid; |
| 627 | if (de->d_type != DT_DIR) continue; |
| 628 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 629 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 630 | pidFd = -1; |
| 631 | nsFd = -1; |
| 632 | |
| 633 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 634 | if (pidFd < 0) { |
| 635 | goto next; |
| 636 | } |
| 637 | if (fstat(pidFd, &sb) != 0) { |
| 638 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 639 | goto next; |
| 640 | } |
| 641 | if (sb.st_uid != uid) { |
| 642 | goto next; |
| 643 | } |
| 644 | |
| 645 | // Matches so far, but refuse to touch if in root namespace |
| 646 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 647 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 648 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 649 | goto next; |
| 650 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 651 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 652 | LOG(WARNING) << "Skipping due to root namespace"; |
| 653 | goto next; |
| 654 | } |
| 655 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 656 | if (apexUpdatable) { |
| 657 | std::string exeName; |
| 658 | // When ro.apex.bionic_updatable is set to true, |
| 659 | // some early native processes have mount namespaces that are different |
| 660 | // from that of the init. Therefore, above check can't filter them out. |
| 661 | // Since the propagation type of / is 'shared', unmounting /storage |
| 662 | // for the early native processes affects other processes including |
| 663 | // init. Filter out such processes by skipping if a process is a |
| 664 | // non-Java process whose UID is < AID_APP_START. (The UID condition |
| 665 | // is required to not filter out child processes spawned by apps.) |
| 666 | if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) { |
| 667 | PLOG(WARNING) << "Failed to read exe name for " << de->d_name; |
| 668 | goto next; |
| 669 | } |
| 670 | if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) { |
| 671 | LOG(WARNING) << "Skipping due to native system process"; |
| 672 | goto next; |
| 673 | } |
| 674 | } |
| 675 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 676 | // We purposefully leave the namespace open across the fork |
Nick Kralevich | e7e89ac | 2019-03-29 16:03:51 -0700 | [diff] [blame] | 677 | // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC |
| 678 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 679 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 680 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 681 | goto next; |
| 682 | } |
| 683 | |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 684 | if (mode == "default") { |
| 685 | storageSource = "/mnt/runtime/default"; |
| 686 | } else if (mode == "read") { |
| 687 | storageSource = "/mnt/runtime/read"; |
| 688 | } else if (mode == "write") { |
| 689 | storageSource = "/mnt/runtime/write"; |
| 690 | } else if (mode == "full") { |
| 691 | storageSource = "/mnt/runtime/full"; |
| 692 | } else { |
| 693 | // Sane default of no storage visible. No need to fork a child |
| 694 | // to remount uid. |
| 695 | goto next; |
| 696 | } |
| 697 | |
| 698 | // Mount user-specific symlink helper into place |
| 699 | userSource = StringPrintf("/mnt/user/%d", multiuser_get_user_id(uid)); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 700 | if (!(child = fork())) { |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 701 | if (childProcess(storageSource.c_str(), userSource.c_str(), nsFd, de)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 702 | _exit(0); |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 703 | } else { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 704 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 705 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | if (child == -1) { |
| 709 | PLOG(ERROR) << "Failed to fork"; |
| 710 | goto next; |
| 711 | } else { |
| 712 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 713 | } |
| 714 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 715 | next: |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 716 | close(nsFd); |
| 717 | close(pidFd); |
| 718 | } |
| 719 | closedir(dir); |
| 720 | return 0; |
| 721 | } |
| 722 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 723 | int VolumeManager::reset() { |
| 724 | // Tear down all existing disks/volumes and start from a blank slate so |
| 725 | // newly connected framework hears all events. |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 726 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 727 | vol->destroy(); |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 728 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 729 | mInternalEmulatedVolumes.clear(); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 730 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 731 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 732 | disk->destroy(); |
| 733 | disk->create(); |
| 734 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 735 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 736 | mAddedUsers.clear(); |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 737 | mStartedUsers.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 738 | return 0; |
| 739 | } |
| 740 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 741 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 742 | int VolumeManager::shutdown() { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 743 | if (mInternalEmulatedVolumes.empty()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 744 | return 0; // already shutdown |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 745 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 746 | android::vold::sSleepOnUnmount = false; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 747 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 748 | vol->destroy(); |
| 749 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 750 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 751 | disk->destroy(); |
| 752 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 753 | |
| 754 | mInternalEmulatedVolumes.clear(); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 755 | mStubVolumes.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 756 | mDisks.clear(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 757 | mPendingDisks.clear(); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 758 | android::vold::sSleepOnUnmount = true; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 759 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 762 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 763 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 764 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 765 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 766 | // First, try gracefully unmounting all known devices |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 767 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 768 | vol->unmount(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 769 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 770 | for (const auto& stub : mStubVolumes) { |
| 771 | stub->unmount(); |
| 772 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 773 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 774 | disk->unmountAll(); |
| 775 | } |
| 776 | |
| 777 | // Worst case we might have some stale mounts lurking around, so |
| 778 | // force unmount those just to be safe. |
LongPing.WEI | 4f04606 | 2018-11-23 19:27:35 +0800 | [diff] [blame] | 779 | FILE* fp = setmntent("/proc/mounts", "re"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 780 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 781 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 782 | return -errno; |
| 783 | } |
| 784 | |
| 785 | // Some volumes can be stacked on each other, so force unmount in |
| 786 | // reverse order to give us the best chance of success. |
| 787 | std::list<std::string> toUnmount; |
| 788 | mntent* mentry; |
| 789 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 790 | auto test = std::string(mentry->mnt_dir); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 791 | if ((StartsWith(test, "/mnt/") && |
| 792 | #ifdef __ANDROID_DEBUGGABLE__ |
| 793 | !StartsWith(test, "/mnt/scratch") && |
| 794 | #endif |
| 795 | !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) || |
| 796 | StartsWith(test, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 797 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | endmntent(fp); |
| 801 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 802 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 803 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 804 | android::vold::ForceUnmount(path); |
| 805 | } |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 810 | int VolumeManager::setupAppDir(const std::string& path, const std::string& appDirRoot, |
| 811 | int32_t appUid) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 812 | // Only offer to create directories for paths managed by vold |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 813 | if (!StartsWith(path, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 814 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 815 | return -EINVAL; |
| 816 | } |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 817 | |
| 818 | // First create the root which holds app dirs, if needed. |
| 819 | int ret = PrepareDirsFromRoot(appDirRoot, "/storage/", 0771, AID_MEDIA_RW, AID_MEDIA_RW); |
| 820 | if (ret != 0) { |
| 821 | return ret; |
| 822 | } |
| 823 | // Then, create app-specific dirs with the correct UID/GID |
| 824 | return PrepareDirsFromRoot(path, appDirRoot, 0770, appUid, AID_MEDIA_RW); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 825 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 826 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 827 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 828 | int32_t ownerGid, std::string* outVolId) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 829 | int id = mNextObbId++; |
| 830 | |
| 831 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 832 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 833 | vol->create(); |
| 834 | |
| 835 | mObbVolumes.push_back(vol); |
| 836 | *outVolId = vol->getId(); |
| 837 | return android::OK; |
| 838 | } |
| 839 | |
| 840 | int VolumeManager::destroyObb(const std::string& volId) { |
| 841 | auto i = mObbVolumes.begin(); |
| 842 | while (i != mObbVolumes.end()) { |
| 843 | if ((*i)->getId() == volId) { |
| 844 | (*i)->destroy(); |
| 845 | i = mObbVolumes.erase(i); |
| 846 | } else { |
| 847 | ++i; |
| 848 | } |
| 849 | } |
| 850 | return android::OK; |
| 851 | } |
| 852 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 853 | int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 854 | const std::string& fsType, const std::string& fsUuid, |
| 855 | const std::string& fsLabel, std::string* outVolId) { |
| 856 | int id = mNextStubVolumeId++; |
| 857 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 858 | new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel)); |
| 859 | vol->create(); |
| 860 | |
| 861 | mStubVolumes.push_back(vol); |
| 862 | *outVolId = vol->getId(); |
| 863 | return android::OK; |
| 864 | } |
| 865 | |
| 866 | int VolumeManager::destroyStubVolume(const std::string& volId) { |
| 867 | auto i = mStubVolumes.begin(); |
| 868 | while (i != mStubVolumes.end()) { |
| 869 | if ((*i)->getId() == volId) { |
| 870 | (*i)->destroy(); |
| 871 | i = mStubVolumes.erase(i); |
| 872 | } else { |
| 873 | ++i; |
| 874 | } |
| 875 | } |
| 876 | return android::OK; |
| 877 | } |
| 878 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 879 | int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 880 | return android::vold::MountAppFuse(uid, mountId, device_fd); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 881 | } |
| 882 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 883 | int VolumeManager::unmountAppFuse(uid_t uid, int mountId) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 884 | return android::vold::UnmountAppFuse(uid, mountId); |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 888 | return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 889 | } |