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