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