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