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