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> |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 33 | #include <array> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 34 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 35 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 38 | #include <android-base/parseint.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 39 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 40 | #include <android-base/stringprintf.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 41 | #include <android-base/strings.h> |
| 42 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 43 | #include <cutils/fs.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 44 | #include <utils/Trace.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 46 | #include <selinux/android.h> |
| 47 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 48 | #include <sysutils/NetlinkEvent.h> |
| 49 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 50 | #include <private/android_filesystem_config.h> |
| 51 | |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 52 | #include <ext4_utils/ext4_crypt.h> |
| 53 | |
| 54 | #include "Devmapper.h" |
| 55 | #include "Ext4Crypt.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 56 | #include "Loop.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 57 | #include "NetlinkManager.h" |
| 58 | #include "Process.h" |
| 59 | #include "Utils.h" |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 60 | #include "VoldNativeService.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 61 | #include "VoldUtil.h" |
| 62 | #include "VolumeManager.h" |
| 63 | #include "cryptfs.h" |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 64 | #include "fs/Ext4.h" |
| 65 | #include "fs/Vfat.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 66 | #include "model/EmulatedVolume.h" |
| 67 | #include "model/ObbVolume.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 68 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 69 | using android::base::GetBoolProperty; |
| 70 | using android::base::StringAppendF; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 71 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 72 | using android::base::unique_fd; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 73 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 74 | static const char* kPathUserMount = "/mnt/user"; |
| 75 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 76 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 77 | static const char* kIsolatedStorage = "persist.sys.isolated_storage"; |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 78 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 79 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 80 | static const std::string kEmptyString(""); |
| 81 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 82 | /* 512MiB is large enough for testing purposes */ |
| 83 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 85 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 86 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 87 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 88 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 89 | VolumeManager* VolumeManager::sInstance = NULL; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 90 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 91 | VolumeManager* VolumeManager::Instance() { |
| 92 | if (!sInstance) sInstance = new VolumeManager(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 93 | return sInstance; |
| 94 | } |
| 95 | |
| 96 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 97 | mDebug = false; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 98 | mNextObbId = 0; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 99 | // For security reasons, assume that a secure keyguard is |
| 100 | // showing until we hear otherwise |
| 101 | mSecureKeyguardShowing = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 104 | VolumeManager::~VolumeManager() {} |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 105 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 106 | int VolumeManager::updateVirtualDisk() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 107 | ATRACE_NAME("VolumeManager::updateVirtualDisk"); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 108 | if (android::base::GetBoolProperty(kPropVirtualDisk, false)) { |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 109 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 110 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 111 | } |
| 112 | |
| 113 | if (mVirtualDisk == nullptr) { |
| 114 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 115 | LOG(ERROR) << "Failed to create virtual disk"; |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | struct stat buf; |
| 120 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 121 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 122 | return -1; |
| 123 | } |
| 124 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 125 | auto disk = new android::vold::Disk( |
| 126 | "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>( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -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 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -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) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 186 | LOG(DEBUG) << "----------------"; |
| 187 | LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 188 | evt->dump(); |
| 189 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 190 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [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()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 201 | case NetlinkEvent::Action::kAdd: { |
| 202 | for (const auto& source : mDiskSources) { |
| 203 | if (source->matches(eventPath)) { |
| 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 |
| 207 | int flags = source->getFlags(); |
| 208 | if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() && |
| 209 | major >= (int)kMajorBlockExperimentalMin && |
| 210 | major <= (int)kMajorBlockExperimentalMax)) { |
| 211 | flags |= android::vold::Disk::Flags::kSd; |
| 212 | } else { |
| 213 | flags |= android::vold::Disk::Flags::kUsb; |
| 214 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 215 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 216 | auto disk = |
| 217 | new android::vold::Disk(eventPath, device, source->getNickname(), flags); |
| 218 | handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk)); |
| 219 | break; |
| 220 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 221 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 222 | break; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 223 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 224 | case NetlinkEvent::Action::kChange: { |
| 225 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
| 226 | handleDiskChanged(device); |
| 227 | break; |
| 228 | } |
| 229 | case NetlinkEvent::Action::kRemove: { |
| 230 | handleDiskRemoved(device); |
| 231 | break; |
| 232 | } |
| 233 | default: { |
| 234 | LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction(); |
| 235 | break; |
| 236 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 240 | void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) { |
| 241 | // For security reasons, if secure keyguard is showing, wait |
| 242 | // until the user unlocks the device to actually touch it |
| 243 | if (mSecureKeyguardShowing) { |
| 244 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 245 | << " but delaying scan due to secure keyguard"; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 246 | mPendingDisks.push_back(disk); |
| 247 | } else { |
| 248 | disk->create(); |
| 249 | mDisks.push_back(disk); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void VolumeManager::handleDiskChanged(dev_t device) { |
| 254 | for (const auto& disk : mDisks) { |
| 255 | if (disk->getDevice() == device) { |
| 256 | disk->readMetadata(); |
| 257 | disk->readPartitions(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // For security reasons, we ignore all pending disks, since |
| 262 | // we'll scan them once the device is unlocked |
| 263 | } |
| 264 | |
| 265 | void VolumeManager::handleDiskRemoved(dev_t device) { |
| 266 | auto i = mDisks.begin(); |
| 267 | while (i != mDisks.end()) { |
| 268 | if ((*i)->getDevice() == device) { |
| 269 | (*i)->destroy(); |
| 270 | i = mDisks.erase(i); |
| 271 | } else { |
| 272 | ++i; |
| 273 | } |
| 274 | } |
| 275 | auto j = mPendingDisks.begin(); |
| 276 | while (j != mPendingDisks.end()) { |
| 277 | if ((*j)->getDevice() == device) { |
| 278 | j = mPendingDisks.erase(j); |
| 279 | } else { |
| 280 | ++j; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 285 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 286 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 287 | mDiskSources.push_back(diskSource); |
| 288 | } |
| 289 | |
| 290 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 291 | for (auto disk : mDisks) { |
| 292 | if (disk->getId() == id) { |
| 293 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 296 | return nullptr; |
| 297 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 298 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 299 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 300 | // Vold could receive "mount" after "shutdown" command in the extreme case. |
| 301 | // If this happens, mInternalEmulated will equal nullptr and |
| 302 | // we need to deal with it in order to avoid null pointer crash. |
| 303 | if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 304 | return mInternalEmulated; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 305 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 306 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 307 | auto vol = disk->findVolume(id); |
| 308 | if (vol != nullptr) { |
| 309 | return vol; |
| 310 | } |
| 311 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 312 | for (const auto& vol : mObbVolumes) { |
| 313 | if (vol->getId() == id) { |
| 314 | return vol; |
| 315 | } |
| 316 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 317 | return nullptr; |
| 318 | } |
| 319 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 320 | 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] | 321 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 322 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 323 | disk->listVolumes(type, list); |
| 324 | } |
| 325 | } |
| 326 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 327 | int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) { |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 328 | std::string normalizedGuid; |
| 329 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 330 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 331 | return -1; |
| 332 | } |
| 333 | |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 334 | bool success = true; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 335 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 336 | if (unlink(keyPath.c_str()) != 0) { |
| 337 | LOG(ERROR) << "Failed to unlink " << keyPath; |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 338 | success = false; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 339 | } |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 340 | if (e4crypt_is_native()) { |
| 341 | if (!e4crypt_destroy_volume_keys(fsUuid)) { |
| 342 | success = false; |
| 343 | } |
| 344 | } |
| 345 | return success ? 0 : -1; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 348 | int VolumeManager::linkPrimary(userid_t userId) { |
| 349 | std::string source(mPrimary->getPath()); |
| 350 | if (mPrimary->isEmulated()) { |
| 351 | source = StringPrintf("%s/%d", source.c_str(), userId); |
| 352 | fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 353 | } |
| 354 | |
| 355 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 356 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 357 | if (errno != ENOENT) { |
| 358 | PLOG(WARNING) << "Failed to unlink " << target; |
| 359 | } |
| 360 | } |
| 361 | LOG(DEBUG) << "Linking " << source << " to " << target; |
| 362 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
| 363 | PLOG(WARNING) << "Failed to link"; |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 364 | return -errno; |
| 365 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | int VolumeManager::mountPkgSpecificDir(const std::string& mntSourceRoot, |
| 370 | const std::string& mntTargetRoot, |
| 371 | const std::string& packageName, const char* dirName) { |
| 372 | std::string mntSourceDir = |
| 373 | StringPrintf("%s/Android/%s/%s", mntSourceRoot.c_str(), dirName, packageName.c_str()); |
| 374 | std::string mntTargetDir = |
| 375 | StringPrintf("%s/Android/%s/%s", mntTargetRoot.c_str(), dirName, packageName.c_str()); |
| 376 | if (umount2(mntTargetDir.c_str(), MNT_DETACH) == -1 && errno != EINVAL && errno != ENOENT) { |
| 377 | PLOG(ERROR) << "Failed to unmount " << mntTargetDir; |
| 378 | return -1; |
| 379 | } |
| 380 | if (TEMP_FAILURE_RETRY(mount(mntSourceDir.c_str(), mntTargetDir.c_str(), nullptr, |
| 381 | MS_BIND | MS_REC, nullptr)) == -1) { |
| 382 | PLOG(ERROR) << "Failed to mount " << mntSourceDir << " to " << mntTargetDir; |
| 383 | return -1; |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 384 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 385 | if (TEMP_FAILURE_RETRY( |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 386 | mount(nullptr, mntTargetDir.c_str(), nullptr, MS_REC | MS_SLAVE, nullptr)) == -1) { |
| 387 | PLOG(ERROR) << "Failed to set MS_SLAVE at " << mntTargetDir; |
| 388 | return -1; |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 393 | int VolumeManager::mountPkgSpecificDirsForRunningProcs( |
| 394 | userid_t userId, const std::vector<std::string>& packageNames, |
| 395 | const std::vector<std::string>& visibleVolLabels) { |
| 396 | // TODO: New processes could be started while traversing over the existing |
| 397 | // processes which would end up not having the necessary bind mounts. This |
| 398 | // issue needs to be fixed, may be by doing multiple passes here? |
| 399 | std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir("/proc"), closedir); |
| 400 | if (!dirp) { |
| 401 | PLOG(ERROR) << "Failed to opendir /proc"; |
| 402 | return -1; |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 403 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 404 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 405 | std::string rootName; |
| 406 | // Figure out root namespace to compare against below |
| 407 | if (!android::vold::Readlinkat(dirfd(dirp.get()), "1/ns/mnt", &rootName)) { |
| 408 | PLOG(ERROR) << "Failed to read root namespace"; |
| 409 | return -1; |
Sudheer Shanka | f768c27 | 2018-08-04 10:10:27 -0700 | [diff] [blame] | 410 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 411 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 412 | struct stat fullWriteSb; |
| 413 | if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/write", &fullWriteSb)) == -1) { |
| 414 | PLOG(ERROR) << "Failed to stat /mnt/runtime/write"; |
| 415 | return -1; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 418 | std::unordered_set<appid_t> validAppIds; |
| 419 | for (auto& package : packageNames) { |
| 420 | validAppIds.insert(mAppIds[package]); |
Sudheer Shanka | a695f25 | 2018-08-03 18:07:52 -0700 | [diff] [blame] | 421 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 422 | std::vector<std::string>& userPackages = mUserPackages[userId]; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 423 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 424 | struct dirent* de; |
| 425 | // Poke through all running PIDs look for apps running in userId |
| 426 | while ((de = readdir(dirp.get()))) { |
| 427 | pid_t pid; |
| 428 | if (de->d_type != DT_DIR) continue; |
| 429 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 430 | |
| 431 | const unique_fd pidFd( |
| 432 | openat(dirfd(dirp.get()), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 433 | if (pidFd.get() < 0) { |
| 434 | PLOG(WARNING) << "Failed to open /proc/" << pid; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 435 | continue; |
| 436 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 437 | struct stat sb; |
| 438 | if (fstat(pidFd.get(), &sb) != 0) { |
| 439 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 440 | continue; |
| 441 | } |
| 442 | if (multiuser_get_user_id(sb.st_uid) != userId) { |
| 443 | continue; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 444 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 445 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 446 | // Matches so far, but refuse to touch if in root namespace |
| 447 | LOG(VERBOSE) << "Found matching PID " << de->d_name; |
| 448 | std::string pidName; |
| 449 | if (!android::vold::Readlinkat(pidFd.get(), "ns/mnt", &pidName)) { |
| 450 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 451 | continue; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 452 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 453 | if (rootName == pidName) { |
| 454 | LOG(WARNING) << "Skipping due to root namespace"; |
| 455 | continue; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 458 | // Only update the mount points of processes running with one of validAppIds. |
| 459 | // This should skip any isolated uids. |
| 460 | appid_t appId = multiuser_get_app_id(sb.st_uid); |
| 461 | if (validAppIds.find(appId) == validAppIds.end()) { |
| 462 | continue; |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 465 | std::vector<std::string> packagesForUid; |
| 466 | for (auto& package : userPackages) { |
| 467 | if (mAppIds[package] == appId) { |
| 468 | packagesForUid.push_back(package); |
| 469 | } |
| 470 | } |
| 471 | if (packagesForUid.empty()) { |
| 472 | continue; |
| 473 | } |
| 474 | const std::string& sandboxId = mSandboxIds[appId]; |
| 475 | |
| 476 | // We purposefully leave the namespace open across the fork |
| 477 | unique_fd nsFd(openat(pidFd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC |
| 478 | if (nsFd.get() < 0) { |
| 479 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
| 480 | continue; |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 483 | pid_t child; |
| 484 | if (!(child = fork())) { |
| 485 | if (setns(nsFd.get(), CLONE_NEWNS) != 0) { |
| 486 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
| 487 | _exit(1); |
| 488 | } |
| 489 | |
| 490 | struct stat storageSb; |
| 491 | if (TEMP_FAILURE_RETRY(stat("/storage", &storageSb)) == -1) { |
| 492 | PLOG(ERROR) << "Failed to stat /storage"; |
| 493 | _exit(1); |
| 494 | } |
| 495 | |
| 496 | // Some packages have access to full external storage, identify processes belonging |
| 497 | // to those packages by comparing inode no.s of /mnt/runtime/write and /storage |
| 498 | if (storageSb.st_dev == fullWriteSb.st_dev && storageSb.st_ino == fullWriteSb.st_ino) { |
| 499 | _exit(0); |
| 500 | } else { |
| 501 | // Some packages don't have access to external storage and processes belonging to |
| 502 | // those packages don't have anything mounted at /storage. So, identify those |
| 503 | // processes by comparing inode no.s of /mnt/user/%d/package/%s |
| 504 | // and /storage |
| 505 | std::string pkgStorageSource; |
| 506 | for (auto& package : packagesForUid) { |
| 507 | std::string sandbox = |
| 508 | StringPrintf("/mnt/user/%d/package/%s", userId, package.c_str()); |
| 509 | struct stat s; |
| 510 | if (TEMP_FAILURE_RETRY(stat(sandbox.c_str(), &s)) == -1) { |
| 511 | PLOG(ERROR) << "Failed to stat " << sandbox; |
| 512 | _exit(1); |
| 513 | } |
| 514 | if (storageSb.st_dev == s.st_dev && storageSb.st_ino == s.st_ino) { |
| 515 | pkgStorageSource = sandbox; |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | if (pkgStorageSource.empty()) { |
| 520 | _exit(0); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | for (auto& volumeLabel : visibleVolLabels) { |
| 525 | std::string mntSource = StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str()); |
| 526 | std::string mntTarget = StringPrintf("/storage/%s", volumeLabel.c_str()); |
| 527 | if (volumeLabel == "emulated") { |
| 528 | StringAppendF(&mntSource, "/%d", userId); |
| 529 | StringAppendF(&mntTarget, "/%d", userId); |
| 530 | } |
| 531 | for (auto& package : packagesForUid) { |
| 532 | mountPkgSpecificDir(mntSource, mntTarget, package, "data"); |
| 533 | mountPkgSpecificDir(mntSource, mntTarget, package, "media"); |
| 534 | mountPkgSpecificDir(mntSource, mntTarget, package, "obb"); |
| 535 | } |
| 536 | } |
| 537 | _exit(0); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 538 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 539 | |
| 540 | if (child == -1) { |
| 541 | PLOG(ERROR) << "Failed to fork"; |
| 542 | } else { |
| 543 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 549 | int VolumeManager::prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames, |
| 550 | const std::vector<std::string>& visibleVolLabels) { |
| 551 | if (visibleVolLabels.empty()) { |
| 552 | return 0; |
| 553 | } |
| 554 | for (auto& volumeLabel : visibleVolLabels) { |
| 555 | std::string volumeRoot(StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str())); |
| 556 | bool isVolPrimaryEmulated = (volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated()); |
| 557 | if (isVolPrimaryEmulated) { |
| 558 | StringAppendF(&volumeRoot, "/%d", userId); |
| 559 | if (fs_prepare_dir(volumeRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) { |
| 560 | PLOG(ERROR) << "fs_prepare_dir failed on " << volumeRoot; |
| 561 | return -errno; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | std::string sandboxRoot = |
| 566 | prepareSubDirs(volumeRoot, "Android/sandbox/", 0700, AID_ROOT, AID_ROOT); |
| 567 | if (sandboxRoot.empty()) { |
| 568 | return -errno; |
| 569 | } |
| 570 | std::string sharedSandboxRoot; |
| 571 | StringAppendF(&sharedSandboxRoot, "%s/shared", sandboxRoot.c_str()); |
| 572 | // Create shared sandbox base dir for apps with sharedUserIds |
| 573 | if (fs_prepare_dir(sharedSandboxRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 574 | PLOG(ERROR) << "fs_prepare_dir failed on " << sharedSandboxRoot; |
| 575 | return -errno; |
| 576 | } |
| 577 | |
| 578 | if (!createPkgSpecificDirRoots(volumeRoot)) { |
| 579 | return -errno; |
| 580 | } |
| 581 | |
| 582 | std::string mntTargetRoot = StringPrintf("/mnt/user/%d", userId); |
| 583 | if (fs_prepare_dir(mntTargetRoot.c_str(), 0751, AID_ROOT, AID_ROOT) != 0) { |
| 584 | PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot; |
| 585 | return -errno; |
| 586 | } |
| 587 | mntTargetRoot.append("/package"); |
| 588 | if (fs_prepare_dir(mntTargetRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 589 | PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot; |
| 590 | return -errno; |
| 591 | } |
| 592 | |
| 593 | for (auto& packageName : packageNames) { |
| 594 | const auto& it = mAppIds.find(packageName); |
| 595 | if (it == mAppIds.end()) { |
| 596 | PLOG(ERROR) << "appId is not available for " << packageName; |
| 597 | continue; |
| 598 | } |
| 599 | appid_t appId = it->second; |
| 600 | std::string sandboxId = mSandboxIds[appId]; |
| 601 | uid_t uid = multiuser_get_uid(userId, appId); |
| 602 | |
| 603 | // [1] Create /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId> |
| 604 | // [2] Create /mnt/user/0/package/<packageName>/emulated/0 |
| 605 | // Mount [1] at [2] |
| 606 | std::string pkgSandboxSourceDir = prepareSandboxSource(uid, sandboxId, sandboxRoot); |
| 607 | if (pkgSandboxSourceDir.empty()) { |
| 608 | return -errno; |
| 609 | } |
| 610 | std::string pkgSandboxTargetDir = prepareSandboxTarget( |
| 611 | packageName, uid, volumeLabel, mntTargetRoot, isVolPrimaryEmulated); |
| 612 | if (pkgSandboxTargetDir.empty()) { |
| 613 | return -errno; |
| 614 | } |
| 615 | if (umount2(pkgSandboxTargetDir.c_str(), MNT_DETACH) == -1 && errno != EINVAL && |
| 616 | errno != ENOENT) { |
| 617 | PLOG(ERROR) << "Failed to unmount " << pkgSandboxTargetDir; |
| 618 | return -errno; |
| 619 | } |
| 620 | if (TEMP_FAILURE_RETRY(mount(pkgSandboxSourceDir.c_str(), pkgSandboxTargetDir.c_str(), |
| 621 | nullptr, MS_BIND | MS_REC, nullptr)) == -1) { |
| 622 | PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at " |
| 623 | << pkgSandboxTargetDir; |
| 624 | return -errno; |
| 625 | } |
| 626 | if (TEMP_FAILURE_RETRY(mount(nullptr, pkgSandboxTargetDir.c_str(), nullptr, |
| 627 | MS_SLAVE | MS_REC, nullptr)) == -1) { |
| 628 | PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at " |
| 629 | << pkgSandboxTargetDir; |
| 630 | return -errno; |
| 631 | } |
| 632 | |
| 633 | // Create Android/{data,media,obb}/<packageName> segments at |
| 634 | // [1] /mnt/runtime/write/emulated/0/ and |
| 635 | // [2] /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId>/emulated/0/ |
| 636 | if (!createPkgSpecificDirs(packageName, uid, volumeRoot, pkgSandboxSourceDir)) { |
| 637 | return -errno; |
| 638 | } |
| 639 | |
| 640 | if (volumeLabel == mPrimary->getLabel()) { |
| 641 | // Create [1] /mnt/user/0/package/<packageName>/self/ |
| 642 | // Already created [2] /mnt/user/0/package/<packageName>/emulated/0 |
| 643 | // Mount [2] at [1] |
| 644 | std::string pkgPrimaryTargetDir = |
| 645 | StringPrintf("%s/%s/self", mntTargetRoot.c_str(), packageName.c_str()); |
| 646 | if (fs_prepare_dir(pkgPrimaryTargetDir.c_str(), 0755, uid, uid) != 0) { |
| 647 | PLOG(ERROR) << "Failed to fs_prepare_dir on " << pkgPrimaryTargetDir; |
| 648 | return -errno; |
| 649 | } |
| 650 | StringAppendF(&pkgPrimaryTargetDir, "/primary"); |
| 651 | std::string primarySource(mPrimary->getPath()); |
| 652 | if (isVolPrimaryEmulated) { |
| 653 | StringAppendF(&primarySource, "/%d", userId); |
| 654 | } |
| 655 | if (TEMP_FAILURE_RETRY(unlink(pkgPrimaryTargetDir.c_str()))) { |
| 656 | if (errno != ENOENT) { |
| 657 | PLOG(ERROR) << "Failed to unlink " << pkgPrimaryTargetDir; |
| 658 | } |
| 659 | } |
| 660 | if (TEMP_FAILURE_RETRY(symlink(primarySource.c_str(), pkgPrimaryTargetDir.c_str()))) { |
| 661 | PLOG(ERROR) << "Failed to link " << primarySource << " at " |
| 662 | << pkgPrimaryTargetDir; |
| 663 | return -errno; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels); |
| 669 | return 0; |
| 670 | } |
| 671 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 672 | std::string VolumeManager::prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs, |
| 673 | mode_t mode, uid_t uid, gid_t gid) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 674 | std::string path(pathPrefix); |
| 675 | std::vector<std::string> subDirList = android::base::Split(subDirs, "/"); |
| 676 | for (size_t i = 0; i < subDirList.size(); ++i) { |
| 677 | std::string subDir = subDirList[i]; |
| 678 | if (subDir.empty()) { |
| 679 | continue; |
| 680 | } |
| 681 | StringAppendF(&path, "/%s", subDir.c_str()); |
| 682 | if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) { |
| 683 | PLOG(ERROR) << "fs_prepare_dir failed on " << path; |
| 684 | return kEmptyString; |
| 685 | } |
| 686 | } |
| 687 | return path; |
| 688 | } |
| 689 | |
| 690 | std::string VolumeManager::prepareSandboxSource(uid_t uid, const std::string& sandboxId, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 691 | const std::string& sandboxRootDir) { |
Sudheer Shanka | f768c27 | 2018-08-04 10:10:27 -0700 | [diff] [blame] | 692 | std::string sandboxSourceDir(sandboxRootDir); |
| 693 | if (android::base::StartsWith(sandboxId, "shared:")) { |
| 694 | StringAppendF(&sandboxSourceDir, "/shared/%s", sandboxId.substr(7).c_str()); |
| 695 | } else { |
| 696 | StringAppendF(&sandboxSourceDir, "/%s", sandboxId.c_str()); |
| 697 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 698 | if (fs_prepare_dir(sandboxSourceDir.c_str(), 0755, uid, uid) != 0) { |
| 699 | PLOG(ERROR) << "fs_prepare_dir failed on " << sandboxSourceDir; |
| 700 | return kEmptyString; |
| 701 | } |
| 702 | return sandboxSourceDir; |
| 703 | } |
| 704 | |
| 705 | std::string VolumeManager::prepareSandboxTarget(const std::string& packageName, uid_t uid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 706 | const std::string& volumeLabel, |
| 707 | const std::string& mntTargetRootDir, |
| 708 | bool isUserDependent) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 709 | std::string segment; |
| 710 | if (isUserDependent) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 711 | segment = StringPrintf("%s/%s/%d/", packageName.c_str(), volumeLabel.c_str(), |
| 712 | multiuser_get_user_id(uid)); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 713 | } else { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 714 | segment = StringPrintf("%s/%s/", packageName.c_str(), volumeLabel.c_str()); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 715 | } |
| 716 | return prepareSubDirs(mntTargetRootDir, segment.c_str(), 0755, uid, uid); |
| 717 | } |
| 718 | |
| 719 | std::string VolumeManager::preparePkgDataSource(const std::string& packageName, uid_t uid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 720 | const std::string& dataRootDir) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 721 | std::string dataSourceDir = StringPrintf("%s/%s", dataRootDir.c_str(), packageName.c_str()); |
| 722 | if (fs_prepare_dir(dataSourceDir.c_str(), 0755, uid, uid) != 0) { |
| 723 | PLOG(ERROR) << "fs_prepare_dir failed on " << dataSourceDir; |
| 724 | return kEmptyString; |
| 725 | } |
| 726 | return dataSourceDir; |
| 727 | } |
| 728 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 729 | bool VolumeManager::createPkgSpecificDirRoots(const std::string& volumeRoot) { |
| 730 | std::string volumeAndroidRoot = StringPrintf("%s/Android", volumeRoot.c_str()); |
| 731 | if (fs_prepare_dir(volumeAndroidRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 732 | PLOG(ERROR) << "fs_prepare_dir failed on " << volumeAndroidRoot; |
| 733 | return false; |
| 734 | } |
| 735 | std::array<std::string, 3> dirs = {"data", "media", "obb"}; |
| 736 | for (auto& dir : dirs) { |
| 737 | std::string path = StringPrintf("%s/%s", volumeAndroidRoot.c_str(), dir.c_str()); |
| 738 | if (fs_prepare_dir(path.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 739 | PLOG(ERROR) << "fs_prepare_dir failed on " << path; |
| 740 | return false; |
| 741 | } |
| 742 | } |
| 743 | return true; |
| 744 | } |
| 745 | |
| 746 | bool VolumeManager::createPkgSpecificDirs(const std::string& packageName, uid_t uid, |
| 747 | const std::string& volumeRoot, |
| 748 | const std::string& sandboxDirRoot) { |
| 749 | std::array<std::string, 3> dirs = {"data", "media", "obb"}; |
| 750 | for (auto& dir : dirs) { |
| 751 | std::string sourceDir = StringPrintf("%s/Android/%s", volumeRoot.c_str(), dir.c_str()); |
| 752 | if (prepareSubDirs(sourceDir, packageName, 0755, uid, uid).empty()) { |
| 753 | return false; |
| 754 | } |
| 755 | std::string sandboxSegment = |
| 756 | StringPrintf("Android/%s/%s/", dir.c_str(), packageName.c_str()); |
| 757 | if (prepareSubDirs(sandboxDirRoot, sandboxSegment, 0755, uid, uid).empty()) { |
| 758 | return false; |
| 759 | } |
| 760 | } |
| 761 | return true; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 764 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
| 765 | mAddedUsers[userId] = userSerialNumber; |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | int VolumeManager::onUserRemoved(userid_t userId) { |
| 770 | mAddedUsers.erase(userId); |
| 771 | return 0; |
| 772 | } |
| 773 | |
Sudheer Shanka | ebaad1c | 2018-07-31 16:39:59 -0700 | [diff] [blame] | 774 | int VolumeManager::onUserStarted(userid_t userId, const std::vector<std::string>& packageNames) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 775 | LOG(VERBOSE) << "onUserStarted: " << userId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 776 | // Note that sometimes the system will spin up processes from Zygote |
| 777 | // before actually starting the user, so we're okay if Zygote |
| 778 | // already created this directory. |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 779 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 780 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 781 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 782 | mStartedUsers.insert(userId); |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 783 | mUserPackages[userId] = packageNames; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 784 | if (mPrimary) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 785 | linkPrimary(userId); |
| 786 | } |
| 787 | if (GetBoolProperty(kIsolatedStorage, false)) { |
| 788 | std::vector<std::string> visibleVolLabels; |
| 789 | for (auto& volId : mVisibleVolumeIds) { |
| 790 | auto vol = findVolume(volId); |
| 791 | userid_t mountUserId = vol->getMountUserId(); |
| 792 | if (mountUserId == userId || vol->isEmulated()) { |
| 793 | visibleVolLabels.push_back(vol->getLabel()); |
| 794 | } |
| 795 | } |
| 796 | if (prepareSandboxes(userId, packageNames, visibleVolLabels) != 0) { |
| 797 | return -errno; |
| 798 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 799 | } |
| 800 | return 0; |
| 801 | } |
| 802 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 803 | int VolumeManager::onUserStopped(userid_t userId) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 804 | LOG(VERBOSE) << "onUserStopped: " << userId; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 805 | mStartedUsers.erase(userId); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 806 | |
| 807 | std::string mntTargetDir = StringPrintf("/mnt/user/%d", userId); |
| 808 | if (android::vold::UnmountTree(mntTargetDir) != 0) { |
| 809 | PLOG(ERROR) << "unmountTree on " << mntTargetDir << " failed"; |
| 810 | return -errno; |
| 811 | } |
| 812 | if (android::vold::DeleteDirContentsAndDir(mntTargetDir) < 0) { |
| 813 | PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << mntTargetDir; |
| 814 | return -errno; |
| 815 | } |
| 816 | LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << mntTargetDir; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 817 | return 0; |
| 818 | } |
| 819 | |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 820 | int VolumeManager::addAppIds(const std::vector<std::string>& packageNames, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 821 | const std::vector<int32_t>& appIds) { |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 822 | for (size_t i = 0; i < packageNames.size(); ++i) { |
| 823 | mAppIds[packageNames[i]] = appIds[i]; |
| 824 | } |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | int VolumeManager::addSandboxIds(const std::vector<int32_t>& appIds, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 829 | const std::vector<std::string>& sandboxIds) { |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 830 | for (size_t i = 0; i < appIds.size(); ++i) { |
| 831 | mSandboxIds[appIds[i]] = sandboxIds[i]; |
| 832 | } |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 836 | int VolumeManager::mountExternalStorageForApp(const std::string& packageName, appid_t appId, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 837 | const std::string& sandboxId, userid_t userId) { |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 838 | if (!GetBoolProperty(kIsolatedStorage, false)) { |
| 839 | return 0; |
| 840 | } else if (mStartedUsers.find(userId) == mStartedUsers.end()) { |
| 841 | // User not started, no need to do anything now. Required bind mounts for the package will |
| 842 | // be created when the user starts. |
| 843 | return 0; |
| 844 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 845 | LOG(VERBOSE) << "mountExternalStorageForApp: " << packageName << ", appId=" << appId |
| 846 | << ", sandboxId=" << sandboxId << ", userId=" << userId; |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 847 | mUserPackages[userId].push_back(packageName); |
| 848 | mAppIds[packageName] = appId; |
| 849 | mSandboxIds[appId] = sandboxId; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 850 | |
| 851 | std::vector<std::string> visibleVolLabels; |
| 852 | for (auto& volId : mVisibleVolumeIds) { |
| 853 | auto vol = findVolume(volId); |
| 854 | userid_t mountUserId = vol->getMountUserId(); |
| 855 | if (mountUserId == userId || vol->isEmulated()) { |
| 856 | visibleVolLabels.push_back(vol->getLabel()); |
| 857 | } |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 858 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 859 | return prepareSandboxes(userId, {packageName}, visibleVolLabels); |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 862 | int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) { |
| 863 | mSecureKeyguardShowing = isShowing; |
| 864 | if (!mSecureKeyguardShowing) { |
| 865 | // Now that secure keyguard has been dismissed, process |
| 866 | // any pending disks |
| 867 | for (const auto& disk : mPendingDisks) { |
| 868 | disk->create(); |
| 869 | mDisks.push_back(disk); |
| 870 | } |
| 871 | mPendingDisks.clear(); |
| 872 | } |
| 873 | return 0; |
| 874 | } |
| 875 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 876 | int VolumeManager::onVolumeMounted(android::vold::VolumeBase* vol) { |
| 877 | if (!GetBoolProperty(kIsolatedStorage, false)) { |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_VISIBLE) == 0) { |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | mVisibleVolumeIds.insert(vol->getId()); |
| 886 | userid_t mountUserId = vol->getMountUserId(); |
| 887 | if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) { |
| 888 | // We don't want to create another shared_ptr here because then we will end up with |
| 889 | // two shared_ptrs owning the underlying pointer without sharing it. |
| 890 | mPrimary = findVolume(vol->getId()); |
| 891 | for (userid_t userId : mStartedUsers) { |
| 892 | if (linkPrimary(userId) != 0) { |
| 893 | return -errno; |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | if (vol->isEmulated()) { |
| 898 | for (userid_t userId : mStartedUsers) { |
| 899 | if (prepareSandboxes(userId, mUserPackages[userId], {vol->getLabel()}) != 0) { |
| 900 | return -errno; |
| 901 | } |
| 902 | } |
| 903 | } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) { |
| 904 | if (prepareSandboxes(mountUserId, mUserPackages[mountUserId], {vol->getLabel()}) != 0) { |
| 905 | return -errno; |
| 906 | } |
| 907 | } |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | int VolumeManager::onVolumeUnmounted(android::vold::VolumeBase* vol) { |
| 912 | if (!GetBoolProperty(kIsolatedStorage, false)) { |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | if (mVisibleVolumeIds.erase(vol->getId()) == 0) { |
| 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) { |
| 921 | mPrimary = nullptr; |
| 922 | } |
| 923 | |
| 924 | LOG(VERBOSE) << "visibleVolumeUnmounted: " << vol; |
| 925 | userid_t mountUserId = vol->getMountUserId(); |
| 926 | if (vol->isEmulated()) { |
| 927 | for (userid_t userId : mStartedUsers) { |
| 928 | if (destroySandboxesForVol(vol, userId) != 0) { |
| 929 | return -errno; |
| 930 | } |
| 931 | } |
| 932 | } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) { |
| 933 | if (destroySandboxesForVol(vol, mountUserId) != 0) { |
| 934 | return -errno; |
| 935 | } |
| 936 | } |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | int VolumeManager::destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId) { |
| 941 | LOG(VERBOSE) << "destroysandboxesForVol: " << vol << " for user=" << userId; |
| 942 | const std::vector<std::string>& packageNames = mUserPackages[userId]; |
| 943 | for (auto& packageName : packageNames) { |
| 944 | std::string volSandboxRoot = StringPrintf("/mnt/user/%d/package/%s/%s", userId, |
| 945 | packageName.c_str(), vol->getLabel().c_str()); |
| 946 | if (android::vold::UnmountTree(volSandboxRoot) != 0) { |
| 947 | PLOG(ERROR) << "unmountTree on " << volSandboxRoot << " failed"; |
| 948 | continue; |
| 949 | } |
| 950 | if (android::vold::DeleteDirContentsAndDir(volSandboxRoot) < 0) { |
| 951 | PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << volSandboxRoot; |
| 952 | continue; |
| 953 | } |
| 954 | LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << volSandboxRoot; |
| 955 | } |
| 956 | return 0; |
| 957 | } |
| 958 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 959 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 960 | if (GetBoolProperty(kIsolatedStorage, false)) { |
| 961 | return 0; |
| 962 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 963 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 964 | for (userid_t userId : mStartedUsers) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 965 | linkPrimary(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 966 | } |
| 967 | return 0; |
| 968 | } |
| 969 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 970 | static int unmount_tree(const std::string& prefix) { |
Sudheer Shanka | 89ddf99 | 2018-09-25 14:22:07 -0700 | [diff] [blame] | 971 | return android::vold::UnmountTree(prefix); |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 974 | int VolumeManager::remountUid(uid_t uid, const std::string& mode) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 975 | // If the isolated storage is enabled, return -1 since in the isolated storage world, there |
| 976 | // are no longer any runtime storage permissions, so this shouldn't be called anymore. |
| 977 | if (GetBoolProperty(kIsolatedStorage, false)) { |
| 978 | return -1; |
| 979 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 980 | LOG(DEBUG) << "Remounting " << uid << " as mode " << mode; |
| 981 | |
| 982 | DIR* dir; |
| 983 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 984 | std::string rootName; |
| 985 | std::string pidName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 986 | int pidFd; |
| 987 | int nsFd; |
| 988 | struct stat sb; |
| 989 | pid_t child; |
| 990 | |
| 991 | if (!(dir = opendir("/proc"))) { |
| 992 | PLOG(ERROR) << "Failed to opendir"; |
| 993 | return -1; |
| 994 | } |
| 995 | |
| 996 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 997 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
| 998 | PLOG(ERROR) << "Failed to read root namespace"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 999 | closedir(dir); |
| 1000 | return -1; |
| 1001 | } |
| 1002 | |
| 1003 | // Poke through all running PIDs look for apps running as UID |
| 1004 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 1005 | pid_t pid; |
| 1006 | if (de->d_type != DT_DIR) continue; |
| 1007 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 1008 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1009 | pidFd = -1; |
| 1010 | nsFd = -1; |
| 1011 | |
| 1012 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 1013 | if (pidFd < 0) { |
| 1014 | goto next; |
| 1015 | } |
| 1016 | if (fstat(pidFd, &sb) != 0) { |
| 1017 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 1018 | goto next; |
| 1019 | } |
| 1020 | if (sb.st_uid != uid) { |
| 1021 | goto next; |
| 1022 | } |
| 1023 | |
| 1024 | // Matches so far, but refuse to touch if in root namespace |
| 1025 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1026 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1027 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 1028 | goto next; |
| 1029 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1030 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1031 | LOG(WARNING) << "Skipping due to root namespace"; |
| 1032 | goto next; |
| 1033 | } |
| 1034 | |
| 1035 | // We purposefully leave the namespace open across the fork |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1036 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1037 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1038 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1039 | goto next; |
| 1040 | } |
| 1041 | |
| 1042 | if (!(child = fork())) { |
| 1043 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1044 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1045 | _exit(1); |
| 1046 | } |
| 1047 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1048 | unmount_tree("/storage/"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1049 | |
| 1050 | std::string storageSource; |
| 1051 | if (mode == "default") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 1052 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1053 | } else if (mode == "read") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 1054 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1055 | } else if (mode == "write") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 1056 | storageSource = "/mnt/runtime/write"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1057 | } else { |
| 1058 | // Sane default of no storage visible |
| 1059 | _exit(0); |
| 1060 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1061 | if (TEMP_FAILURE_RETRY( |
| 1062 | mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 1063 | PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1064 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1065 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1066 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) { |
| 1067 | 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] | 1068 | _exit(1); |
| 1069 | } |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1070 | |
| 1071 | // Mount user-specific symlink helper into place |
| 1072 | userid_t user_id = multiuser_get_user_id(uid); |
| 1073 | std::string userSource(StringPrintf("/mnt/user/%d", user_id)); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1074 | if (TEMP_FAILURE_RETRY( |
| 1075 | mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) { |
| 1076 | PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1077 | _exit(1); |
| 1078 | } |
| 1079 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1080 | _exit(0); |
| 1081 | } |
| 1082 | |
| 1083 | if (child == -1) { |
| 1084 | PLOG(ERROR) << "Failed to fork"; |
| 1085 | goto next; |
| 1086 | } else { |
| 1087 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 1088 | } |
| 1089 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1090 | next: |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1091 | close(nsFd); |
| 1092 | close(pidFd); |
| 1093 | } |
| 1094 | closedir(dir); |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1098 | int VolumeManager::reset() { |
| 1099 | // Tear down all existing disks/volumes and start from a blank slate so |
| 1100 | // newly connected framework hears all events. |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 1101 | if (mInternalEmulated != nullptr) { |
| 1102 | mInternalEmulated->destroy(); |
| 1103 | mInternalEmulated->create(); |
| 1104 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1105 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1106 | disk->destroy(); |
| 1107 | disk->create(); |
| 1108 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 1109 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 1110 | mAddedUsers.clear(); |
| 1111 | mStartedUsers.clear(); |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 1112 | |
| 1113 | mUserPackages.clear(); |
| 1114 | mAppIds.clear(); |
| 1115 | mSandboxIds.clear(); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 1116 | mVisibleVolumeIds.clear(); |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 1117 | |
| 1118 | // For unmounting dirs under /mnt/user/<user-id>/package/<package-name> |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 1119 | android::vold::UnmountTree("/mnt/user/"); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1120 | return 0; |
| 1121 | } |
| 1122 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 1123 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1124 | int VolumeManager::shutdown() { |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 1125 | if (mInternalEmulated == nullptr) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1126 | return 0; // already shutdown |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 1127 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 1128 | android::vold::sSleepOnUnmount = false; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1129 | mInternalEmulated->destroy(); |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 1130 | mInternalEmulated = nullptr; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1131 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1132 | disk->destroy(); |
| 1133 | } |
| 1134 | mDisks.clear(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 1135 | mPendingDisks.clear(); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 1136 | android::vold::sSleepOnUnmount = true; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1137 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1140 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 1141 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 1142 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 1143 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1144 | // First, try gracefully unmounting all known devices |
| 1145 | if (mInternalEmulated != nullptr) { |
| 1146 | mInternalEmulated->unmount(); |
| 1147 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1148 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1149 | disk->unmountAll(); |
| 1150 | } |
| 1151 | |
| 1152 | // Worst case we might have some stale mounts lurking around, so |
| 1153 | // force unmount those just to be safe. |
| 1154 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 1155 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1156 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1157 | return -errno; |
| 1158 | } |
| 1159 | |
| 1160 | // Some volumes can be stacked on each other, so force unmount in |
| 1161 | // reverse order to give us the best chance of success. |
| 1162 | std::list<std::string> toUnmount; |
| 1163 | mntent* mentry; |
| 1164 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1165 | auto test = std::string(mentry->mnt_dir); |
Tri Vo | bca5cd7 | 2018-04-16 14:27:10 -0700 | [diff] [blame] | 1166 | if ((android::base::StartsWith(test, "/mnt/") && |
Bowgo Tsai | c0cd37b | 2018-06-29 10:31:07 +0800 | [diff] [blame] | 1167 | !android::base::StartsWith(test, "/mnt/vendor") && |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame^] | 1168 | !android::base::StartsWith(test, "/mnt/product")) || |
Tri Vo | bca5cd7 | 2018-04-16 14:27:10 -0700 | [diff] [blame] | 1169 | android::base::StartsWith(test, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1170 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | endmntent(fp); |
| 1174 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1175 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1176 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1177 | android::vold::ForceUnmount(path); |
| 1178 | } |
| 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1183 | int VolumeManager::mkdirs(const std::string& path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1184 | // Only offer to create directories for paths managed by vold |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1185 | if (android::base::StartsWith(path, "/storage/")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1186 | // fs_mkdirs() does symlink checking and relative path enforcement |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1187 | return fs_mkdirs(path.c_str(), 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1188 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1189 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1190 | return -EINVAL; |
| 1191 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1192 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1193 | |
| 1194 | static size_t kAppFuseMaxMountPointName = 32; |
| 1195 | |
| 1196 | static android::status_t getMountPath(uid_t uid, const std::string& name, std::string* path) { |
| 1197 | if (name.size() > kAppFuseMaxMountPointName) { |
| 1198 | LOG(ERROR) << "AppFuse mount name is too long."; |
| 1199 | return -EINVAL; |
| 1200 | } |
| 1201 | for (size_t i = 0; i < name.size(); i++) { |
| 1202 | if (!isalnum(name[i])) { |
| 1203 | LOG(ERROR) << "AppFuse mount name contains invalid character."; |
| 1204 | return -EINVAL; |
| 1205 | } |
| 1206 | } |
| 1207 | *path = android::base::StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str()); |
| 1208 | return android::OK; |
| 1209 | } |
| 1210 | |
| 1211 | static android::status_t mountInNamespace(uid_t uid, int device_fd, const std::string& path) { |
| 1212 | // Remove existing mount. |
| 1213 | android::vold::ForceUnmount(path); |
| 1214 | |
| 1215 | const auto opts = android::base::StringPrintf( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1216 | "fd=%i," |
| 1217 | "rootmode=40000," |
| 1218 | "default_permissions," |
| 1219 | "allow_other," |
| 1220 | "user_id=%d,group_id=%d," |
| 1221 | "context=\"u:object_r:app_fuse_file:s0\"," |
| 1222 | "fscontext=u:object_r:app_fusefs:s0", |
| 1223 | device_fd, uid, uid); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1224 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1225 | const int result = |
| 1226 | TEMP_FAILURE_RETRY(mount("/dev/fuse", path.c_str(), "fuse", |
| 1227 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str())); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1228 | if (result != 0) { |
| 1229 | PLOG(ERROR) << "Failed to mount " << path; |
| 1230 | return -errno; |
| 1231 | } |
| 1232 | |
| 1233 | return android::OK; |
| 1234 | } |
| 1235 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1236 | static android::status_t runCommandInNamespace(const std::string& command, uid_t uid, pid_t pid, |
| 1237 | const std::string& path, int device_fd) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1238 | if (DEBUG_APPFUSE) { |
| 1239 | LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path |
| 1240 | << " in namespace " << uid; |
| 1241 | } |
| 1242 | |
| 1243 | unique_fd dir(open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 1244 | if (dir.get() == -1) { |
| 1245 | PLOG(ERROR) << "Failed to open /proc"; |
| 1246 | return -errno; |
| 1247 | } |
| 1248 | |
| 1249 | // Obtains process file descriptor. |
| 1250 | const std::string pid_str = android::base::StringPrintf("%d", pid); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1251 | 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] | 1252 | if (pid_fd.get() == -1) { |
| 1253 | PLOG(ERROR) << "Failed to open /proc/" << pid; |
| 1254 | return -errno; |
| 1255 | } |
| 1256 | |
| 1257 | // Check UID of process. |
| 1258 | { |
| 1259 | struct stat sb; |
| 1260 | const int result = fstat(pid_fd.get(), &sb); |
| 1261 | if (result == -1) { |
| 1262 | PLOG(ERROR) << "Failed to stat /proc/" << pid; |
| 1263 | return -errno; |
| 1264 | } |
| 1265 | if (sb.st_uid != AID_SYSTEM) { |
| 1266 | LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1267 | << ", actual=" << sb.st_uid; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1268 | return -EPERM; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | // Matches so far, but refuse to touch if in root namespace |
| 1273 | { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1274 | std::string rootName; |
| 1275 | std::string pidName; |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1276 | if (!android::vold::Readlinkat(dir.get(), "1/ns/mnt", &rootName) || |
| 1277 | !android::vold::Readlinkat(pid_fd.get(), "ns/mnt", &pidName)) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1278 | PLOG(ERROR) << "Failed to read namespaces"; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1279 | return -EPERM; |
| 1280 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1281 | if (rootName == pidName) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1282 | LOG(ERROR) << "Don't mount appfuse in root namespace"; |
| 1283 | return -EPERM; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | // We purposefully leave the namespace open across the fork |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1288 | 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] | 1289 | if (ns_fd.get() < 0) { |
| 1290 | PLOG(ERROR) << "Failed to open namespace for /proc/" << pid << "/ns/mnt"; |
| 1291 | return -errno; |
| 1292 | } |
| 1293 | |
| 1294 | int child = fork(); |
| 1295 | if (child == 0) { |
| 1296 | if (setns(ns_fd.get(), CLONE_NEWNS) != 0) { |
| 1297 | PLOG(ERROR) << "Failed to setns"; |
| 1298 | _exit(-errno); |
| 1299 | } |
| 1300 | |
| 1301 | if (command == "mount") { |
| 1302 | _exit(mountInNamespace(uid, device_fd, path)); |
| 1303 | } else if (command == "unmount") { |
| 1304 | // If it's just after all FD opened on mount point are closed, umount2 can fail with |
| 1305 | // EBUSY. To avoid the case, specify MNT_DETACH. |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1306 | if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && errno != EINVAL && |
| 1307 | errno != ENOENT) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1308 | PLOG(ERROR) << "Failed to unmount directory."; |
| 1309 | _exit(-errno); |
| 1310 | } |
| 1311 | if (rmdir(path.c_str()) != 0) { |
| 1312 | PLOG(ERROR) << "Failed to remove the mount directory."; |
| 1313 | _exit(-errno); |
| 1314 | } |
| 1315 | _exit(android::OK); |
| 1316 | } else { |
| 1317 | LOG(ERROR) << "Unknown appfuse command " << command; |
| 1318 | _exit(-EPERM); |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | if (child == -1) { |
| 1323 | PLOG(ERROR) << "Failed to folk child process"; |
| 1324 | return -errno; |
| 1325 | } |
| 1326 | |
| 1327 | android::status_t status; |
| 1328 | TEMP_FAILURE_RETRY(waitpid(child, &status, 0)); |
| 1329 | |
| 1330 | return status; |
| 1331 | } |
| 1332 | |
| 1333 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1334 | int32_t ownerGid, std::string* outVolId) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1335 | int id = mNextObbId++; |
| 1336 | |
| 1337 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1338 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1339 | vol->create(); |
| 1340 | |
| 1341 | mObbVolumes.push_back(vol); |
| 1342 | *outVolId = vol->getId(); |
| 1343 | return android::OK; |
| 1344 | } |
| 1345 | |
| 1346 | int VolumeManager::destroyObb(const std::string& volId) { |
| 1347 | auto i = mObbVolumes.begin(); |
| 1348 | while (i != mObbVolumes.end()) { |
| 1349 | if ((*i)->getId() == volId) { |
| 1350 | (*i)->destroy(); |
| 1351 | i = mObbVolumes.erase(i); |
| 1352 | } else { |
| 1353 | ++i; |
| 1354 | } |
| 1355 | } |
| 1356 | return android::OK; |
| 1357 | } |
| 1358 | |
| 1359 | int VolumeManager::mountAppFuse(uid_t uid, pid_t pid, int mountId, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1360 | android::base::unique_fd* device_fd) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1361 | std::string name = std::to_string(mountId); |
| 1362 | |
| 1363 | // Check mount point name. |
| 1364 | std::string path; |
| 1365 | if (getMountPath(uid, name, &path) != android::OK) { |
| 1366 | LOG(ERROR) << "Invalid mount point name"; |
| 1367 | return -1; |
| 1368 | } |
| 1369 | |
| 1370 | // Create directories. |
| 1371 | const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0); |
| 1372 | if (result != android::OK) { |
| 1373 | PLOG(ERROR) << "Failed to prepare directory " << path; |
| 1374 | return -1; |
| 1375 | } |
| 1376 | |
| 1377 | // Open device FD. |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1378 | device_fd->reset(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1379 | if (device_fd->get() == -1) { |
| 1380 | PLOG(ERROR) << "Failed to open /dev/fuse"; |
| 1381 | return -1; |
| 1382 | } |
| 1383 | |
| 1384 | // Mount. |
| 1385 | return runCommandInNamespace("mount", uid, pid, path, device_fd->get()); |
| 1386 | } |
| 1387 | |
| 1388 | int VolumeManager::unmountAppFuse(uid_t uid, pid_t pid, int mountId) { |
| 1389 | std::string name = std::to_string(mountId); |
| 1390 | |
| 1391 | // Check mount point name. |
| 1392 | std::string path; |
| 1393 | if (getMountPath(uid, name, &path) != android::OK) { |
| 1394 | LOG(ERROR) << "Invalid mount point name"; |
| 1395 | return -1; |
| 1396 | } |
| 1397 | |
| 1398 | return runCommandInNamespace("unmount", uid, pid, path, -1 /* device_fd */); |
| 1399 | } |