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