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 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 67 | using android::base::GetBoolProperty; |
| 68 | using android::base::StringAppendF; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 69 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 70 | using android::base::unique_fd; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 71 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 72 | static const char* kPathUserMount = "/mnt/user"; |
| 73 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 74 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 75 | static const char* kIsolatedStorage = "persist.sys.isolated_storage"; |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 76 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 77 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 78 | static const std::string kEmptyString(""); |
| 79 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 80 | /* 512MiB is large enough for testing purposes */ |
| 81 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 82 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 83 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 84 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 85 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 86 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 87 | VolumeManager *VolumeManager::sInstance = NULL; |
| 88 | |
| 89 | VolumeManager *VolumeManager::Instance() { |
| 90 | if (!sInstance) |
| 91 | sInstance = new VolumeManager(); |
| 92 | return sInstance; |
| 93 | } |
| 94 | |
| 95 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 96 | mDebug = false; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 97 | mNextObbId = 0; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 98 | // For security reasons, assume that a secure keyguard is |
| 99 | // showing until we hear otherwise |
| 100 | mSecureKeyguardShowing = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | VolumeManager::~VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 106 | int VolumeManager::updateVirtualDisk() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 107 | ATRACE_NAME("VolumeManager::updateVirtualDisk"); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 108 | if (android::base::GetBoolProperty(kPropVirtualDisk, false)) { |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 109 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 110 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 111 | } |
| 112 | |
| 113 | if (mVirtualDisk == nullptr) { |
| 114 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 115 | LOG(ERROR) << "Failed to create virtual disk"; |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | struct stat buf; |
| 120 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 121 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual", |
| 126 | android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 127 | mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 128 | handleDiskAdded(mVirtualDisk); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 129 | } |
| 130 | } else { |
| 131 | if (mVirtualDisk != nullptr) { |
| 132 | dev_t device = mVirtualDisk->getDevice(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 133 | handleDiskRemoved(device); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 134 | |
| 135 | Loop::destroyByDevice(mVirtualDiskPath.c_str()); |
| 136 | mVirtualDisk = nullptr; |
| 137 | } |
| 138 | |
| 139 | if (access(kPathVirtualDisk, F_OK) == 0) { |
| 140 | unlink(kPathVirtualDisk); |
| 141 | } |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 146 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 147 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 148 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 149 | } |
| 150 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 151 | int VolumeManager::start() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 152 | ATRACE_NAME("VolumeManager::start"); |
| 153 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 154 | // Always start from a clean slate by unmounting everything in |
| 155 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 156 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 157 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 158 | Devmapper::destroyAll(); |
| 159 | Loop::destroyAll(); |
| 160 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 161 | // Assume that we always have an emulated volume on internal |
| 162 | // storage; the framework will decide if it should be mounted. |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 163 | CHECK(mInternalEmulated == nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 164 | mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>( |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 165 | new android::vold::EmulatedVolume("/data/media")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 166 | mInternalEmulated->create(); |
| 167 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 168 | // Consider creating a virtual disk |
| 169 | updateVirtualDisk(); |
| 170 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int VolumeManager::stop() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 175 | CHECK(mInternalEmulated != nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 176 | mInternalEmulated->destroy(); |
| 177 | mInternalEmulated = nullptr; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 181 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 182 | std::lock_guard<std::mutex> lock(mLock); |
| 183 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 184 | if (mDebug) { |
| 185 | LOG(VERBOSE) << "----------------"; |
| 186 | LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction(); |
| 187 | evt->dump(); |
| 188 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 189 | |
Mateusz Nowak | 6440379 | 2015-08-03 16:39:19 +0200 | [diff] [blame] | 190 | std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):""); |
| 191 | std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):""); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 192 | |
| 193 | if (devType != "disk") return; |
| 194 | |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 195 | int major = std::stoi(evt->findParam("MAJOR")); |
| 196 | int minor = std::stoi(evt->findParam("MINOR")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 197 | dev_t device = makedev(major, minor); |
| 198 | |
| 199 | switch (evt->getAction()) { |
| 200 | case NetlinkEvent::Action::kAdd: { |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 201 | for (const auto& source : mDiskSources) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 202 | if (source->matches(eventPath)) { |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 203 | // For now, assume that MMC and virtio-blk (the latter is |
| 204 | // emulator-specific; see Disk.cpp for details) devices are SD, |
| 205 | // and that everything else is USB |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 206 | int flags = source->getFlags(); |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 207 | if (major == kMajorBlockMmc |
| 208 | || (android::vold::IsRunningInEmulator() |
| 209 | && major >= (int) kMajorBlockExperimentalMin |
| 210 | && major <= (int) kMajorBlockExperimentalMax)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 211 | flags |= android::vold::Disk::Flags::kSd; |
| 212 | } else { |
| 213 | flags |= android::vold::Disk::Flags::kUsb; |
| 214 | } |
| 215 | |
| 216 | auto disk = new android::vold::Disk(eventPath, device, |
| 217 | source->getNickname(), flags); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 218 | handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | } |
| 222 | break; |
| 223 | } |
| 224 | case NetlinkEvent::Action::kChange: { |
Jeff Sharkey | 7d9d011 | 2015-04-14 23:14:23 -0700 | [diff] [blame] | 225 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 226 | handleDiskChanged(device); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | case NetlinkEvent::Action::kRemove: { |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 230 | handleDiskRemoved(device); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | default: { |
| 234 | LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction(); |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 240 | void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) { |
| 241 | // For security reasons, if secure keyguard is showing, wait |
| 242 | // until the user unlocks the device to actually touch it |
| 243 | if (mSecureKeyguardShowing) { |
| 244 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
| 245 | << " but delaying scan due to secure keyguard"; |
| 246 | mPendingDisks.push_back(disk); |
| 247 | } else { |
| 248 | disk->create(); |
| 249 | mDisks.push_back(disk); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void VolumeManager::handleDiskChanged(dev_t device) { |
| 254 | for (const auto& disk : mDisks) { |
| 255 | if (disk->getDevice() == device) { |
| 256 | disk->readMetadata(); |
| 257 | disk->readPartitions(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // For security reasons, we ignore all pending disks, since |
| 262 | // we'll scan them once the device is unlocked |
| 263 | } |
| 264 | |
| 265 | void VolumeManager::handleDiskRemoved(dev_t device) { |
| 266 | auto i = mDisks.begin(); |
| 267 | while (i != mDisks.end()) { |
| 268 | if ((*i)->getDevice() == device) { |
| 269 | (*i)->destroy(); |
| 270 | i = mDisks.erase(i); |
| 271 | } else { |
| 272 | ++i; |
| 273 | } |
| 274 | } |
| 275 | auto j = mPendingDisks.begin(); |
| 276 | while (j != mPendingDisks.end()) { |
| 277 | if ((*j)->getDevice() == device) { |
| 278 | j = mPendingDisks.erase(j); |
| 279 | } else { |
| 280 | ++j; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 285 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 286 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 287 | mDiskSources.push_back(diskSource); |
| 288 | } |
| 289 | |
| 290 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 291 | for (auto disk : mDisks) { |
| 292 | if (disk->getId() == id) { |
| 293 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 296 | return nullptr; |
| 297 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 298 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 299 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 300 | // Vold could receive "mount" after "shutdown" command in the extreme case. |
| 301 | // If this happens, mInternalEmulated will equal nullptr and |
| 302 | // we need to deal with it in order to avoid null pointer crash. |
| 303 | if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 304 | return mInternalEmulated; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 305 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 306 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 307 | auto vol = disk->findVolume(id); |
| 308 | if (vol != nullptr) { |
| 309 | return vol; |
| 310 | } |
| 311 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 312 | for (const auto& vol : mObbVolumes) { |
| 313 | if (vol->getId() == id) { |
| 314 | return vol; |
| 315 | } |
| 316 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 317 | return nullptr; |
| 318 | } |
| 319 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 320 | void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, |
| 321 | std::list<std::string>& list) { |
| 322 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 323 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 324 | disk->listVolumes(type, list); |
| 325 | } |
| 326 | } |
| 327 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 328 | int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) { |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 329 | std::string normalizedGuid; |
| 330 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 331 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 332 | return -1; |
| 333 | } |
| 334 | |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 335 | bool success = true; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 336 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 337 | if (unlink(keyPath.c_str()) != 0) { |
| 338 | LOG(ERROR) << "Failed to unlink " << keyPath; |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 339 | success = false; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 340 | } |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 341 | if (e4crypt_is_native()) { |
| 342 | if (!e4crypt_destroy_volume_keys(fsUuid)) { |
| 343 | success = false; |
| 344 | } |
| 345 | } |
| 346 | return success ? 0 : -1; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 349 | int VolumeManager::linkPrimary(userid_t userId, const std::vector<std::string>& packageNames) { |
| 350 | if (GetBoolProperty(kIsolatedStorage, false)) { |
| 351 | std::string source(StringPrintf("/mnt/runtime/write/%s", mPrimary->getLabel().c_str())); |
| 352 | bool isPrimaryEmulated = |
| 353 | (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated); |
| 354 | if (isPrimaryEmulated) { |
| 355 | StringAppendF(&source, "/%d", userId); |
| 356 | if (fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) { |
| 357 | PLOG(ERROR) << "fs_prepare_dir failed on " << source; |
| 358 | return -errno; |
| 359 | } |
| 360 | } |
Sudheer Shanka | a695f25 | 2018-08-03 18:07:52 -0700 | [diff] [blame] | 361 | if (mountSandboxesForPrimaryVol(source, userId, packageNames, isPrimaryEmulated) != 0) { |
| 362 | return -errno; |
| 363 | } |
| 364 | // Keep /sdcard working for shell process |
| 365 | std::string primarySource(mPrimary->getPath()); |
| 366 | if (isPrimaryEmulated) { |
| 367 | StringAppendF(&primarySource, "/%d", userId); |
| 368 | } |
| 369 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 370 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 371 | if (errno != ENOENT) { |
| 372 | PLOG(WARNING) << "Failed to unlink " << target; |
| 373 | } |
| 374 | } |
| 375 | if (TEMP_FAILURE_RETRY(symlink(primarySource.c_str(), target.c_str()))) { |
| 376 | PLOG(WARNING) << "Failed to link " << primarySource << " at " << target; |
| 377 | return -errno; |
| 378 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 379 | } else { |
| 380 | std::string source(mPrimary->getPath()); |
| 381 | if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) { |
| 382 | source = StringPrintf("%s/%d", source.c_str(), userId); |
| 383 | fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 384 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 385 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 386 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 387 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 388 | if (errno != ENOENT) { |
| 389 | PLOG(WARNING) << "Failed to unlink " << target; |
| 390 | } |
| 391 | } |
| 392 | LOG(DEBUG) << "Linking " << source << " to " << target; |
| 393 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
| 394 | PLOG(WARNING) << "Failed to link"; |
| 395 | return -errno; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | int VolumeManager::mountSandboxesForPrimaryVol(const std::string& primaryRoot, userid_t userId, |
| 402 | const std::vector<std::string>& packageNames, bool isPrimaryEmulated) { |
| 403 | |
| 404 | std::string sandboxRoot = prepareSubDirs(primaryRoot, "Android/sandbox/", |
| 405 | 0700, AID_ROOT, AID_ROOT); |
| 406 | if (sandboxRoot.empty()) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 407 | return -errno; |
| 408 | } |
Sudheer Shanka | f768c27 | 2018-08-04 10:10:27 -0700 | [diff] [blame] | 409 | std::string sharedSandboxRoot; |
| 410 | StringAppendF(&sharedSandboxRoot, "%s/shared", sandboxRoot.c_str()); |
| 411 | // Create shared sandbox base dir for apps with sharedUserIds |
| 412 | if (fs_prepare_dir(sharedSandboxRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 413 | PLOG(ERROR) << "fs_prepare_dir failed on " << sharedSandboxRoot; |
| 414 | return -errno; |
| 415 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 416 | |
| 417 | std::string dataRoot = prepareSubDirs(primaryRoot, "Android/data/", |
| 418 | 0700, AID_ROOT, AID_ROOT); |
| 419 | if (dataRoot.empty()) { |
| 420 | return -errno; |
| 421 | } |
| 422 | |
Sudheer Shanka | a695f25 | 2018-08-03 18:07:52 -0700 | [diff] [blame] | 423 | std::string mntTargetRoot = StringPrintf("/mnt/user/%d", userId); |
| 424 | if (fs_prepare_dir(mntTargetRoot.c_str(), 0751, AID_ROOT, AID_ROOT) != 0) { |
| 425 | PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot; |
| 426 | return -errno; |
| 427 | } |
| 428 | mntTargetRoot.append("/package"); |
| 429 | if (fs_prepare_dir(mntTargetRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 430 | PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 431 | return -errno; |
| 432 | } |
| 433 | |
| 434 | for (auto& packageName : packageNames) { |
| 435 | const auto& it = mAppIds.find(packageName); |
| 436 | if (it == mAppIds.end()) { |
| 437 | PLOG(ERROR) << "appId is not available for " << packageName; |
| 438 | continue; |
| 439 | } |
| 440 | appid_t appId = it->second; |
| 441 | std::string sandboxId = mSandboxIds[appId]; |
| 442 | uid_t uid = multiuser_get_uid(userId, appId); |
| 443 | |
| 444 | // Create [1] /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId> |
| 445 | // Create [2] /mnt/user/0/package/<packageName>/emulated/0 |
| 446 | // Mount [1] at [2] |
| 447 | std::string pkgSandboxSourceDir = prepareSandboxSource(uid, sandboxId, sandboxRoot); |
| 448 | if (pkgSandboxSourceDir.empty()) { |
| 449 | return -errno; |
| 450 | } |
| 451 | std::string pkgSandboxTargetDir = prepareSandboxTarget(packageName, uid, |
| 452 | mPrimary->getLabel(), mntTargetRoot, isPrimaryEmulated); |
| 453 | if (pkgSandboxTargetDir.empty()) { |
| 454 | return -errno; |
| 455 | } |
| 456 | if (TEMP_FAILURE_RETRY(mount(pkgSandboxSourceDir.c_str(), pkgSandboxTargetDir.c_str(), |
| 457 | nullptr, MS_BIND | MS_REC | MS_SLAVE, nullptr)) == -1) { |
| 458 | PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at " |
| 459 | << pkgSandboxTargetDir; |
| 460 | return -errno; |
| 461 | } |
| 462 | |
| 463 | // Create [1] /mnt/runtime/write/emulated/0/Android/data/<packageName> |
| 464 | // Create [2] /mnt/user/0/package/<packageName>/emulated/0/Android/data/<packageName> |
| 465 | // Mount [1] at [2] |
| 466 | std::string pkgDataSourceDir = preparePkgDataSource(packageName, uid, dataRoot); |
| 467 | if (pkgDataSourceDir.empty()) { |
| 468 | return -errno; |
| 469 | } |
| 470 | std::string pkgDataTargetDir = preparePkgDataTarget(packageName, uid, |
| 471 | pkgSandboxTargetDir); |
| 472 | if (pkgDataTargetDir.empty()) { |
| 473 | return -errno; |
| 474 | } |
| 475 | if (TEMP_FAILURE_RETRY(mount(pkgDataSourceDir.c_str(), pkgDataTargetDir.c_str(), |
| 476 | nullptr, MS_BIND | MS_REC | MS_SLAVE, nullptr)) == -1) { |
| 477 | PLOG(ERROR) << "Failed to mount " << pkgDataSourceDir << " at " |
| 478 | << pkgDataTargetDir; |
| 479 | return -errno; |
| 480 | } |
| 481 | |
| 482 | // Create [1] /mnt/user/0/package/<packageName>/self/primary |
| 483 | // Already created [2] /mnt/user/0/package/<packageName>/emulated/0 |
| 484 | // Mount [2] at [1] |
| 485 | std::string pkgPrimaryTargetDir = prepareSubDirs( |
| 486 | StringPrintf("%s/%s", mntTargetRoot.c_str(), packageName.c_str()), |
| 487 | "self/primary/", 0755, uid, uid); |
| 488 | if (pkgPrimaryTargetDir.empty()) { |
| 489 | return -errno; |
| 490 | } |
| 491 | if (TEMP_FAILURE_RETRY(mount(pkgSandboxTargetDir.c_str(), pkgPrimaryTargetDir.c_str(), |
| 492 | nullptr, MS_BIND | MS_REC | MS_SLAVE, nullptr)) == -1) { |
| 493 | PLOG(ERROR) << "Failed to mount " << pkgSandboxTargetDir << " at " |
| 494 | << pkgPrimaryTargetDir; |
| 495 | return -errno; |
| 496 | } |
| 497 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 501 | std::string VolumeManager::prepareSubDirs(const std::string& pathPrefix, |
| 502 | const std::string& subDirs, mode_t mode, uid_t uid, gid_t gid) { |
| 503 | std::string path(pathPrefix); |
| 504 | std::vector<std::string> subDirList = android::base::Split(subDirs, "/"); |
| 505 | for (size_t i = 0; i < subDirList.size(); ++i) { |
| 506 | std::string subDir = subDirList[i]; |
| 507 | if (subDir.empty()) { |
| 508 | continue; |
| 509 | } |
| 510 | StringAppendF(&path, "/%s", subDir.c_str()); |
| 511 | if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) { |
| 512 | PLOG(ERROR) << "fs_prepare_dir failed on " << path; |
| 513 | return kEmptyString; |
| 514 | } |
| 515 | } |
| 516 | return path; |
| 517 | } |
| 518 | |
| 519 | std::string VolumeManager::prepareSandboxSource(uid_t uid, const std::string& sandboxId, |
| 520 | const std::string& sandboxRootDir) { |
Sudheer Shanka | f768c27 | 2018-08-04 10:10:27 -0700 | [diff] [blame] | 521 | std::string sandboxSourceDir(sandboxRootDir); |
| 522 | if (android::base::StartsWith(sandboxId, "shared:")) { |
| 523 | StringAppendF(&sandboxSourceDir, "/shared/%s", sandboxId.substr(7).c_str()); |
| 524 | } else { |
| 525 | StringAppendF(&sandboxSourceDir, "/%s", sandboxId.c_str()); |
| 526 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 527 | if (fs_prepare_dir(sandboxSourceDir.c_str(), 0755, uid, uid) != 0) { |
| 528 | PLOG(ERROR) << "fs_prepare_dir failed on " << sandboxSourceDir; |
| 529 | return kEmptyString; |
| 530 | } |
| 531 | return sandboxSourceDir; |
| 532 | } |
| 533 | |
| 534 | std::string VolumeManager::prepareSandboxTarget(const std::string& packageName, uid_t uid, |
| 535 | const std::string& volumeLabel, const std::string& mntTargetRootDir, |
| 536 | bool isUserDependent) { |
| 537 | std::string segment; |
| 538 | if (isUserDependent) { |
| 539 | segment = StringPrintf("%s/%s/%d/", |
| 540 | packageName.c_str(), volumeLabel.c_str(), multiuser_get_user_id(uid)); |
| 541 | } else { |
| 542 | segment = StringPrintf("%s/%s/", |
| 543 | packageName.c_str(), volumeLabel.c_str()); |
| 544 | } |
| 545 | return prepareSubDirs(mntTargetRootDir, segment.c_str(), 0755, uid, uid); |
| 546 | } |
| 547 | |
| 548 | std::string VolumeManager::preparePkgDataSource(const std::string& packageName, uid_t uid, |
| 549 | const std::string& dataRootDir) { |
| 550 | std::string dataSourceDir = StringPrintf("%s/%s", dataRootDir.c_str(), packageName.c_str()); |
| 551 | if (fs_prepare_dir(dataSourceDir.c_str(), 0755, uid, uid) != 0) { |
| 552 | PLOG(ERROR) << "fs_prepare_dir failed on " << dataSourceDir; |
| 553 | return kEmptyString; |
| 554 | } |
| 555 | return dataSourceDir; |
| 556 | } |
| 557 | |
| 558 | std::string VolumeManager::preparePkgDataTarget(const std::string& packageName, uid_t uid, |
| 559 | const std::string& pkgSandboxDir) { |
| 560 | std::string segment = StringPrintf("Android/data/%s/", packageName.c_str()); |
| 561 | return prepareSubDirs(pkgSandboxDir, segment.c_str(), 0755, uid, uid); |
| 562 | } |
| 563 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 564 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
| 565 | mAddedUsers[userId] = userSerialNumber; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | int VolumeManager::onUserRemoved(userid_t userId) { |
| 570 | mAddedUsers.erase(userId); |
| 571 | return 0; |
| 572 | } |
| 573 | |
Sudheer Shanka | ebaad1c | 2018-07-31 16:39:59 -0700 | [diff] [blame] | 574 | int VolumeManager::onUserStarted(userid_t userId, const std::vector<std::string>& packageNames) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 575 | // Note that sometimes the system will spin up processes from Zygote |
| 576 | // before actually starting the user, so we're okay if Zygote |
| 577 | // already created this directory. |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 578 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 579 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 580 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 581 | mStartedUsers.insert(userId); |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 582 | mUserPackages[userId] = packageNames; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 583 | if (mPrimary) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 584 | linkPrimary(userId, packageNames); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 589 | int VolumeManager::onUserStopped(userid_t userId) { |
| 590 | mStartedUsers.erase(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 594 | int VolumeManager::addAppIds(const std::vector<std::string>& packageNames, |
| 595 | const std::vector<int32_t>& appIds) { |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 596 | for (size_t i = 0; i < packageNames.size(); ++i) { |
| 597 | mAppIds[packageNames[i]] = appIds[i]; |
| 598 | } |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | int VolumeManager::addSandboxIds(const std::vector<int32_t>& appIds, |
| 603 | const std::vector<std::string>& sandboxIds) { |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 604 | for (size_t i = 0; i < appIds.size(); ++i) { |
| 605 | mSandboxIds[appIds[i]] = sandboxIds[i]; |
| 606 | } |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 610 | int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) { |
| 611 | mSecureKeyguardShowing = isShowing; |
| 612 | if (!mSecureKeyguardShowing) { |
| 613 | // Now that secure keyguard has been dismissed, process |
| 614 | // any pending disks |
| 615 | for (const auto& disk : mPendingDisks) { |
| 616 | disk->create(); |
| 617 | mDisks.push_back(disk); |
| 618 | } |
| 619 | mPendingDisks.clear(); |
| 620 | } |
| 621 | return 0; |
| 622 | } |
| 623 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 624 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 625 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 626 | for (userid_t userId : mStartedUsers) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 627 | linkPrimary(userId, mUserPackages[userId]); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 632 | static int unmount_tree(const std::string& prefix) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 633 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 634 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 635 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 636 | return -errno; |
| 637 | } |
| 638 | |
| 639 | // Some volumes can be stacked on each other, so force unmount in |
| 640 | // reverse order to give us the best chance of success. |
| 641 | std::list<std::string> toUnmount; |
| 642 | mntent* mentry; |
| 643 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 644 | auto test = std::string(mentry->mnt_dir) + "/"; |
Elliott Hughes | 32a5b9a | 2017-12-20 12:38:47 -0800 | [diff] [blame] | 645 | if (android::base::StartsWith(test, prefix)) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 646 | toUnmount.push_front(test); |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | endmntent(fp); |
| 650 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 651 | for (const auto& path : toUnmount) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 652 | if (umount2(path.c_str(), MNT_DETACH)) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 653 | PLOG(ERROR) << "Failed to unmount " << path; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | return 0; |
| 657 | } |
| 658 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 659 | int VolumeManager::remountUid(uid_t uid, const std::string& mode) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 660 | // If the isolated storage is enabled, return -1 since in the isolated storage world, there |
| 661 | // are no longer any runtime storage permissions, so this shouldn't be called anymore. |
| 662 | if (GetBoolProperty(kIsolatedStorage, false)) { |
| 663 | return -1; |
| 664 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 665 | LOG(DEBUG) << "Remounting " << uid << " as mode " << mode; |
| 666 | |
| 667 | DIR* dir; |
| 668 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 669 | std::string rootName; |
| 670 | std::string pidName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 671 | int pidFd; |
| 672 | int nsFd; |
| 673 | struct stat sb; |
| 674 | pid_t child; |
| 675 | |
| 676 | if (!(dir = opendir("/proc"))) { |
| 677 | PLOG(ERROR) << "Failed to opendir"; |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 682 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
| 683 | PLOG(ERROR) << "Failed to read root namespace"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 684 | closedir(dir); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | // Poke through all running PIDs look for apps running as UID |
| 689 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 690 | pid_t pid; |
| 691 | if (de->d_type != DT_DIR) continue; |
| 692 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 693 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 694 | pidFd = -1; |
| 695 | nsFd = -1; |
| 696 | |
| 697 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 698 | if (pidFd < 0) { |
| 699 | goto next; |
| 700 | } |
| 701 | if (fstat(pidFd, &sb) != 0) { |
| 702 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 703 | goto next; |
| 704 | } |
| 705 | if (sb.st_uid != uid) { |
| 706 | goto next; |
| 707 | } |
| 708 | |
| 709 | // Matches so far, but refuse to touch if in root namespace |
| 710 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 711 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 712 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 713 | goto next; |
| 714 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 715 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 716 | LOG(WARNING) << "Skipping due to root namespace"; |
| 717 | goto next; |
| 718 | } |
| 719 | |
| 720 | // We purposefully leave the namespace open across the fork |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 721 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 722 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 723 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 724 | goto next; |
| 725 | } |
| 726 | |
| 727 | if (!(child = fork())) { |
| 728 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 729 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 730 | _exit(1); |
| 731 | } |
| 732 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 733 | unmount_tree("/storage/"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 734 | |
| 735 | std::string storageSource; |
| 736 | if (mode == "default") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 737 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 738 | } else if (mode == "read") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 739 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 740 | } else if (mode == "write") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 741 | storageSource = "/mnt/runtime/write"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 742 | } else { |
| 743 | // Sane default of no storage visible |
| 744 | _exit(0); |
| 745 | } |
| 746 | if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage", |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 747 | NULL, MS_BIND | MS_REC, NULL)) == -1) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 748 | PLOG(ERROR) << "Failed to mount " << storageSource << " for " |
| 749 | << de->d_name; |
| 750 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 751 | } |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 752 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, |
| 753 | MS_REC | MS_SLAVE, NULL)) == -1) { |
| 754 | PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " |
| 755 | << de->d_name; |
| 756 | _exit(1); |
| 757 | } |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 758 | |
| 759 | // Mount user-specific symlink helper into place |
| 760 | userid_t user_id = multiuser_get_user_id(uid); |
| 761 | std::string userSource(StringPrintf("/mnt/user/%d", user_id)); |
| 762 | if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self", |
| 763 | NULL, MS_BIND, NULL)) == -1) { |
| 764 | PLOG(ERROR) << "Failed to mount " << userSource << " for " |
| 765 | << de->d_name; |
| 766 | _exit(1); |
| 767 | } |
| 768 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 769 | _exit(0); |
| 770 | } |
| 771 | |
| 772 | if (child == -1) { |
| 773 | PLOG(ERROR) << "Failed to fork"; |
| 774 | goto next; |
| 775 | } else { |
| 776 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 777 | } |
| 778 | |
| 779 | next: |
| 780 | close(nsFd); |
| 781 | close(pidFd); |
| 782 | } |
| 783 | closedir(dir); |
| 784 | return 0; |
| 785 | } |
| 786 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 787 | int VolumeManager::reset() { |
| 788 | // Tear down all existing disks/volumes and start from a blank slate so |
| 789 | // newly connected framework hears all events. |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 790 | if (mInternalEmulated != nullptr) { |
| 791 | mInternalEmulated->destroy(); |
| 792 | mInternalEmulated->create(); |
| 793 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 794 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 795 | disk->destroy(); |
| 796 | disk->create(); |
| 797 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 798 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 799 | mAddedUsers.clear(); |
| 800 | mStartedUsers.clear(); |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 801 | |
| 802 | mUserPackages.clear(); |
| 803 | mAppIds.clear(); |
| 804 | mSandboxIds.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 808 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 809 | int VolumeManager::shutdown() { |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 810 | if (mInternalEmulated == nullptr) { |
| 811 | return 0; // already shutdown |
| 812 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 813 | android::vold::sSleepOnUnmount = false; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 814 | mInternalEmulated->destroy(); |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 815 | mInternalEmulated = nullptr; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 816 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 817 | disk->destroy(); |
| 818 | } |
| 819 | mDisks.clear(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 820 | mPendingDisks.clear(); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 821 | android::vold::sSleepOnUnmount = true; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 822 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 825 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 826 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 827 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 828 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 829 | // First, try gracefully unmounting all known devices |
| 830 | if (mInternalEmulated != nullptr) { |
| 831 | mInternalEmulated->unmount(); |
| 832 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 833 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 834 | disk->unmountAll(); |
| 835 | } |
| 836 | |
| 837 | // Worst case we might have some stale mounts lurking around, so |
| 838 | // force unmount those just to be safe. |
| 839 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 840 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 841 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 842 | return -errno; |
| 843 | } |
| 844 | |
| 845 | // Some volumes can be stacked on each other, so force unmount in |
| 846 | // reverse order to give us the best chance of success. |
| 847 | std::list<std::string> toUnmount; |
| 848 | mntent* mentry; |
| 849 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 850 | auto test = std::string(mentry->mnt_dir); |
Tri Vo | bca5cd7 | 2018-04-16 14:27:10 -0700 | [diff] [blame] | 851 | if ((android::base::StartsWith(test, "/mnt/") && |
Bowgo Tsai | c0cd37b | 2018-06-29 10:31:07 +0800 | [diff] [blame] | 852 | !android::base::StartsWith(test, "/mnt/vendor") && |
| 853 | !android::base::StartsWith(test, "/mnt/product")) || |
Tri Vo | bca5cd7 | 2018-04-16 14:27:10 -0700 | [diff] [blame] | 854 | android::base::StartsWith(test, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 855 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | endmntent(fp); |
| 859 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 860 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 861 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 862 | android::vold::ForceUnmount(path); |
| 863 | } |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 868 | int VolumeManager::mkdirs(const std::string& path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 869 | // Only offer to create directories for paths managed by vold |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 870 | if (android::base::StartsWith(path, "/storage/")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 871 | // fs_mkdirs() does symlink checking and relative path enforcement |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 872 | return fs_mkdirs(path.c_str(), 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 873 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 874 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 875 | return -EINVAL; |
| 876 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 877 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 878 | |
| 879 | static size_t kAppFuseMaxMountPointName = 32; |
| 880 | |
| 881 | static android::status_t getMountPath(uid_t uid, const std::string& name, std::string* path) { |
| 882 | if (name.size() > kAppFuseMaxMountPointName) { |
| 883 | LOG(ERROR) << "AppFuse mount name is too long."; |
| 884 | return -EINVAL; |
| 885 | } |
| 886 | for (size_t i = 0; i < name.size(); i++) { |
| 887 | if (!isalnum(name[i])) { |
| 888 | LOG(ERROR) << "AppFuse mount name contains invalid character."; |
| 889 | return -EINVAL; |
| 890 | } |
| 891 | } |
| 892 | *path = android::base::StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str()); |
| 893 | return android::OK; |
| 894 | } |
| 895 | |
| 896 | static android::status_t mountInNamespace(uid_t uid, int device_fd, const std::string& path) { |
| 897 | // Remove existing mount. |
| 898 | android::vold::ForceUnmount(path); |
| 899 | |
| 900 | const auto opts = android::base::StringPrintf( |
| 901 | "fd=%i," |
| 902 | "rootmode=40000," |
| 903 | "default_permissions," |
| 904 | "allow_other," |
| 905 | "user_id=%d,group_id=%d," |
| 906 | "context=\"u:object_r:app_fuse_file:s0\"," |
| 907 | "fscontext=u:object_r:app_fusefs:s0", |
| 908 | device_fd, |
| 909 | uid, |
| 910 | uid); |
| 911 | |
| 912 | const int result = TEMP_FAILURE_RETRY(mount( |
| 913 | "/dev/fuse", path.c_str(), "fuse", |
| 914 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str())); |
| 915 | if (result != 0) { |
| 916 | PLOG(ERROR) << "Failed to mount " << path; |
| 917 | return -errno; |
| 918 | } |
| 919 | |
| 920 | return android::OK; |
| 921 | } |
| 922 | |
| 923 | static android::status_t runCommandInNamespace(const std::string& command, |
| 924 | uid_t uid, |
| 925 | pid_t pid, |
| 926 | const std::string& path, |
| 927 | int device_fd) { |
| 928 | if (DEBUG_APPFUSE) { |
| 929 | LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path |
| 930 | << " in namespace " << uid; |
| 931 | } |
| 932 | |
| 933 | unique_fd dir(open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 934 | if (dir.get() == -1) { |
| 935 | PLOG(ERROR) << "Failed to open /proc"; |
| 936 | return -errno; |
| 937 | } |
| 938 | |
| 939 | // Obtains process file descriptor. |
| 940 | const std::string pid_str = android::base::StringPrintf("%d", pid); |
| 941 | const unique_fd pid_fd( |
| 942 | openat(dir.get(), pid_str.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 943 | if (pid_fd.get() == -1) { |
| 944 | PLOG(ERROR) << "Failed to open /proc/" << pid; |
| 945 | return -errno; |
| 946 | } |
| 947 | |
| 948 | // Check UID of process. |
| 949 | { |
| 950 | struct stat sb; |
| 951 | const int result = fstat(pid_fd.get(), &sb); |
| 952 | if (result == -1) { |
| 953 | PLOG(ERROR) << "Failed to stat /proc/" << pid; |
| 954 | return -errno; |
| 955 | } |
| 956 | if (sb.st_uid != AID_SYSTEM) { |
| 957 | LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM |
| 958 | << ", actual=" << sb.st_uid; |
| 959 | return -EPERM; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | // Matches so far, but refuse to touch if in root namespace |
| 964 | { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 965 | std::string rootName; |
| 966 | std::string pidName; |
| 967 | if (!android::vold::Readlinkat(dir.get(), "1/ns/mnt", &rootName) |
| 968 | || !android::vold::Readlinkat(pid_fd.get(), "ns/mnt", &pidName)) { |
| 969 | PLOG(ERROR) << "Failed to read namespaces"; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 970 | return -EPERM; |
| 971 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 972 | if (rootName == pidName) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 973 | LOG(ERROR) << "Don't mount appfuse in root namespace"; |
| 974 | return -EPERM; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | // We purposefully leave the namespace open across the fork |
| 979 | unique_fd ns_fd(openat(pid_fd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC |
| 980 | if (ns_fd.get() < 0) { |
| 981 | PLOG(ERROR) << "Failed to open namespace for /proc/" << pid << "/ns/mnt"; |
| 982 | return -errno; |
| 983 | } |
| 984 | |
| 985 | int child = fork(); |
| 986 | if (child == 0) { |
| 987 | if (setns(ns_fd.get(), CLONE_NEWNS) != 0) { |
| 988 | PLOG(ERROR) << "Failed to setns"; |
| 989 | _exit(-errno); |
| 990 | } |
| 991 | |
| 992 | if (command == "mount") { |
| 993 | _exit(mountInNamespace(uid, device_fd, path)); |
| 994 | } else if (command == "unmount") { |
| 995 | // If it's just after all FD opened on mount point are closed, umount2 can fail with |
| 996 | // EBUSY. To avoid the case, specify MNT_DETACH. |
| 997 | if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && |
| 998 | errno != EINVAL && errno != ENOENT) { |
| 999 | PLOG(ERROR) << "Failed to unmount directory."; |
| 1000 | _exit(-errno); |
| 1001 | } |
| 1002 | if (rmdir(path.c_str()) != 0) { |
| 1003 | PLOG(ERROR) << "Failed to remove the mount directory."; |
| 1004 | _exit(-errno); |
| 1005 | } |
| 1006 | _exit(android::OK); |
| 1007 | } else { |
| 1008 | LOG(ERROR) << "Unknown appfuse command " << command; |
| 1009 | _exit(-EPERM); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | if (child == -1) { |
| 1014 | PLOG(ERROR) << "Failed to folk child process"; |
| 1015 | return -errno; |
| 1016 | } |
| 1017 | |
| 1018 | android::status_t status; |
| 1019 | TEMP_FAILURE_RETRY(waitpid(child, &status, 0)); |
| 1020 | |
| 1021 | return status; |
| 1022 | } |
| 1023 | |
| 1024 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
| 1025 | int32_t ownerGid, std::string* outVolId) { |
| 1026 | int id = mNextObbId++; |
| 1027 | |
| 1028 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 1029 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
| 1030 | vol->create(); |
| 1031 | |
| 1032 | mObbVolumes.push_back(vol); |
| 1033 | *outVolId = vol->getId(); |
| 1034 | return android::OK; |
| 1035 | } |
| 1036 | |
| 1037 | int VolumeManager::destroyObb(const std::string& volId) { |
| 1038 | auto i = mObbVolumes.begin(); |
| 1039 | while (i != mObbVolumes.end()) { |
| 1040 | if ((*i)->getId() == volId) { |
| 1041 | (*i)->destroy(); |
| 1042 | i = mObbVolumes.erase(i); |
| 1043 | } else { |
| 1044 | ++i; |
| 1045 | } |
| 1046 | } |
| 1047 | return android::OK; |
| 1048 | } |
| 1049 | |
| 1050 | int VolumeManager::mountAppFuse(uid_t uid, pid_t pid, int mountId, |
| 1051 | android::base::unique_fd* device_fd) { |
| 1052 | std::string name = std::to_string(mountId); |
| 1053 | |
| 1054 | // Check mount point name. |
| 1055 | std::string path; |
| 1056 | if (getMountPath(uid, name, &path) != android::OK) { |
| 1057 | LOG(ERROR) << "Invalid mount point name"; |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | // Create directories. |
| 1062 | const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0); |
| 1063 | if (result != android::OK) { |
| 1064 | PLOG(ERROR) << "Failed to prepare directory " << path; |
| 1065 | return -1; |
| 1066 | } |
| 1067 | |
| 1068 | // Open device FD. |
| 1069 | device_fd->reset(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC |
| 1070 | if (device_fd->get() == -1) { |
| 1071 | PLOG(ERROR) << "Failed to open /dev/fuse"; |
| 1072 | return -1; |
| 1073 | } |
| 1074 | |
| 1075 | // Mount. |
| 1076 | return runCommandInNamespace("mount", uid, pid, path, device_fd->get()); |
| 1077 | } |
| 1078 | |
| 1079 | int VolumeManager::unmountAppFuse(uid_t uid, pid_t pid, int mountId) { |
| 1080 | std::string name = std::to_string(mountId); |
| 1081 | |
| 1082 | // Check mount point name. |
| 1083 | std::string path; |
| 1084 | if (getMountPath(uid, name, &path) != android::OK) { |
| 1085 | LOG(ERROR) << "Invalid mount point name"; |
| 1086 | return -1; |
| 1087 | } |
| 1088 | |
| 1089 | return runCommandInNamespace("unmount", uid, pid, path, -1 /* device_fd */); |
| 1090 | } |