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