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