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