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