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 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 37 | #include <ApexProperties.sysprop.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 38 | #include <android-base/logging.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 39 | #include <android-base/parseint.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 41 | #include <android-base/stringprintf.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 42 | #include <android-base/strings.h> |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 43 | #include <async_safe/log.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 44 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 45 | #include <cutils/fs.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 46 | #include <utils/Trace.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 47 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 48 | #include <selinux/android.h> |
| 49 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 50 | #include <sysutils/NetlinkEvent.h> |
| 51 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 52 | #include <private/android_filesystem_config.h> |
| 53 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 54 | #include <fscrypt/fscrypt.h> |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 55 | |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 56 | #include "AppFuseUtil.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 57 | #include "Devmapper.h" |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 58 | #include "FsCrypt.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 59 | #include "Loop.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 60 | #include "NetlinkManager.h" |
| 61 | #include "Process.h" |
| 62 | #include "Utils.h" |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 63 | #include "VoldNativeService.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 64 | #include "VoldUtil.h" |
| 65 | #include "VolumeManager.h" |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 66 | #include "fs/Ext4.h" |
| 67 | #include "fs/Vfat.h" |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 68 | #include "model/EmulatedVolume.h" |
| 69 | #include "model/ObbVolume.h" |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 70 | #include "model/PrivateVolume.h" |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 71 | #include "model/StubVolume.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 72 | |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 73 | using android::OK; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 74 | using android::base::GetBoolProperty; |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 75 | using android::base::StartsWith; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 76 | using android::base::StringAppendF; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 77 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 78 | using android::base::unique_fd; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 79 | using android::vold::BindMount; |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 80 | using android::vold::CreateDir; |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame] | 81 | using android::vold::DeleteDirContents; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 82 | using android::vold::DeleteDirContentsAndDir; |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 83 | using android::vold::EnsureDirExists; |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 84 | using android::vold::IsFilesystemSupported; |
| 85 | using android::vold::PrepareAndroidDirs; |
Martijn Coenen | 04bb17f | 2020-02-10 23:48:11 +0100 | [diff] [blame] | 86 | using android::vold::PrepareAppDirFromRoot; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 87 | using android::vold::PrivateVolume; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 88 | using android::vold::Symlink; |
| 89 | using android::vold::Unlink; |
| 90 | using android::vold::UnmountTree; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 91 | using android::vold::VoldNativeService; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 92 | using android::vold::VolumeBase; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 93 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 94 | static const char* kPathUserMount = "/mnt/user"; |
| 95 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 96 | |
| 97 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 98 | |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 99 | static const std::string kEmptyString(""); |
| 100 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 101 | /* 512MiB is large enough for testing purposes */ |
| 102 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 103 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 104 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 105 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 106 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 107 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 108 | using ScanProcCallback = bool(*)(uid_t uid, pid_t pid, int nsFd, const char* name, void* params); |
| 109 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 110 | VolumeManager* VolumeManager::sInstance = NULL; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 111 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 112 | VolumeManager* VolumeManager::Instance() { |
| 113 | if (!sInstance) sInstance = new VolumeManager(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 114 | return sInstance; |
| 115 | } |
| 116 | |
| 117 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 118 | mDebug = false; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 119 | mNextObbId = 0; |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 120 | mNextStubId = 0; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 121 | // For security reasons, assume that a secure keyguard is |
| 122 | // showing until we hear otherwise |
| 123 | mSecureKeyguardShowing = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 126 | VolumeManager::~VolumeManager() {} |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 127 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 128 | int VolumeManager::updateVirtualDisk() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 129 | ATRACE_NAME("VolumeManager::updateVirtualDisk"); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 130 | if (GetBoolProperty(kPropVirtualDisk, false)) { |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 131 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 132 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 133 | } |
| 134 | |
| 135 | if (mVirtualDisk == nullptr) { |
| 136 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 137 | LOG(ERROR) << "Failed to create virtual disk"; |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | struct stat buf; |
| 142 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 143 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 144 | return -1; |
| 145 | } |
| 146 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 147 | auto disk = new android::vold::Disk( |
| 148 | "virtual", buf.st_rdev, "virtual", |
| 149 | android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 150 | mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 151 | handleDiskAdded(mVirtualDisk); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 152 | } |
| 153 | } else { |
| 154 | if (mVirtualDisk != nullptr) { |
| 155 | dev_t device = mVirtualDisk->getDevice(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 156 | handleDiskRemoved(device); |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 157 | |
| 158 | Loop::destroyByDevice(mVirtualDiskPath.c_str()); |
| 159 | mVirtualDisk = nullptr; |
| 160 | } |
| 161 | |
| 162 | if (access(kPathVirtualDisk, F_OK) == 0) { |
| 163 | unlink(kPathVirtualDisk); |
| 164 | } |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 169 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 170 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 171 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 172 | } |
| 173 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 174 | int VolumeManager::start() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 175 | ATRACE_NAME("VolumeManager::start"); |
| 176 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 177 | // Always start from a clean slate by unmounting everything in |
| 178 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 179 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 181 | Devmapper::destroyAll(); |
| 182 | Loop::destroyAll(); |
| 183 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 184 | // Assume that we always have an emulated volume on internal |
| 185 | // storage; the framework will decide if it should be mounted. |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 186 | CHECK(mInternalEmulatedVolumes.empty()); |
| 187 | |
| 188 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 189 | new android::vold::EmulatedVolume("/data/media", 0)); |
| 190 | vol->setMountUserId(0); |
| 191 | vol->create(); |
| 192 | mInternalEmulatedVolumes.push_back(vol); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 193 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 194 | // Consider creating a virtual disk |
| 195 | updateVirtualDisk(); |
| 196 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int VolumeManager::stop() { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 201 | CHECK(!mInternalEmulatedVolumes.empty()); |
| 202 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 203 | vol->destroy(); |
| 204 | } |
| 205 | mInternalEmulatedVolumes.clear(); |
| 206 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 210 | void VolumeManager::handleBlockEvent(NetlinkEvent* evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 211 | std::lock_guard<std::mutex> lock(mLock); |
| 212 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 213 | if (mDebug) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 214 | LOG(DEBUG) << "----------------"; |
| 215 | LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 216 | evt->dump(); |
| 217 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 218 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 219 | std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : ""); |
| 220 | std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : ""); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 221 | |
| 222 | if (devType != "disk") return; |
| 223 | |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 224 | int major = std::stoi(evt->findParam("MAJOR")); |
| 225 | int minor = std::stoi(evt->findParam("MINOR")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 226 | dev_t device = makedev(major, minor); |
| 227 | |
| 228 | switch (evt->getAction()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 229 | case NetlinkEvent::Action::kAdd: { |
| 230 | for (const auto& source : mDiskSources) { |
| 231 | if (source->matches(eventPath)) { |
| 232 | // For now, assume that MMC and virtio-blk (the latter is |
| 233 | // emulator-specific; see Disk.cpp for details) devices are SD, |
| 234 | // and that everything else is USB |
| 235 | int flags = source->getFlags(); |
| 236 | if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() && |
| 237 | major >= (int)kMajorBlockExperimentalMin && |
| 238 | major <= (int)kMajorBlockExperimentalMax)) { |
| 239 | flags |= android::vold::Disk::Flags::kSd; |
| 240 | } else { |
| 241 | flags |= android::vold::Disk::Flags::kUsb; |
| 242 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 243 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 244 | auto disk = |
| 245 | new android::vold::Disk(eventPath, device, source->getNickname(), flags); |
| 246 | handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk)); |
| 247 | break; |
| 248 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 249 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 250 | break; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 251 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 252 | case NetlinkEvent::Action::kChange: { |
| 253 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
| 254 | handleDiskChanged(device); |
| 255 | break; |
| 256 | } |
| 257 | case NetlinkEvent::Action::kRemove: { |
| 258 | handleDiskRemoved(device); |
| 259 | break; |
| 260 | } |
| 261 | default: { |
| 262 | LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction(); |
| 263 | break; |
| 264 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 268 | void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) { |
| 269 | // For security reasons, if secure keyguard is showing, wait |
| 270 | // until the user unlocks the device to actually touch it |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 271 | // Additionally, wait until user 0 is actually started, since we need |
| 272 | // the user to be up before we can mount a FUSE daemon to handle the disk. |
| 273 | bool userZeroStarted = mStartedUsers.find(0) != mStartedUsers.end(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 274 | if (mSecureKeyguardShowing) { |
| 275 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 276 | << " but delaying scan due to secure keyguard"; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 277 | mPendingDisks.push_back(disk); |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 278 | } else if (!userZeroStarted) { |
| 279 | LOG(INFO) << "Found disk at " << disk->getEventPath() |
| 280 | << " but delaying scan due to user zero not having started"; |
| 281 | mPendingDisks.push_back(disk); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 282 | } else { |
| 283 | disk->create(); |
| 284 | mDisks.push_back(disk); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void VolumeManager::handleDiskChanged(dev_t device) { |
| 289 | for (const auto& disk : mDisks) { |
| 290 | if (disk->getDevice() == device) { |
| 291 | disk->readMetadata(); |
| 292 | disk->readPartitions(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // For security reasons, we ignore all pending disks, since |
| 297 | // we'll scan them once the device is unlocked |
| 298 | } |
| 299 | |
| 300 | void VolumeManager::handleDiskRemoved(dev_t device) { |
| 301 | auto i = mDisks.begin(); |
| 302 | while (i != mDisks.end()) { |
| 303 | if ((*i)->getDevice() == device) { |
| 304 | (*i)->destroy(); |
| 305 | i = mDisks.erase(i); |
| 306 | } else { |
| 307 | ++i; |
| 308 | } |
| 309 | } |
| 310 | auto j = mPendingDisks.begin(); |
| 311 | while (j != mPendingDisks.end()) { |
| 312 | if ((*j)->getDevice() == device) { |
| 313 | j = mPendingDisks.erase(j); |
| 314 | } else { |
| 315 | ++j; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 320 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 321 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 322 | mDiskSources.push_back(diskSource); |
| 323 | } |
| 324 | |
| 325 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 326 | for (auto disk : mDisks) { |
| 327 | if (disk->getId() == id) { |
| 328 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 331 | return nullptr; |
| 332 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 333 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 334 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 335 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 336 | if (vol->getId() == id) { |
| 337 | return vol; |
| 338 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 339 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 340 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 341 | auto vol = disk->findVolume(id); |
| 342 | if (vol != nullptr) { |
| 343 | return vol; |
| 344 | } |
| 345 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 346 | for (const auto& vol : mObbVolumes) { |
| 347 | if (vol->getId() == id) { |
| 348 | return vol; |
| 349 | } |
| 350 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 351 | return nullptr; |
| 352 | } |
| 353 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 354 | void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, |
| 355 | std::list<std::string>& list) const { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 356 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 357 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 358 | disk->listVolumes(type, list); |
| 359 | } |
| 360 | } |
| 361 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 362 | int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) { |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 363 | std::string normalizedGuid; |
| 364 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 365 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 366 | return -1; |
| 367 | } |
| 368 | |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 369 | bool success = true; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 370 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 371 | if (unlink(keyPath.c_str()) != 0) { |
| 372 | LOG(ERROR) << "Failed to unlink " << keyPath; |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 373 | success = false; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 374 | } |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 375 | if (fscrypt_is_native()) { |
| 376 | if (!fscrypt_destroy_volume_keys(fsUuid)) { |
Paul Crowley | c6433a2 | 2017-10-24 14:54:43 -0700 | [diff] [blame] | 377 | success = false; |
| 378 | } |
| 379 | } |
| 380 | return success ? 0 : -1; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 383 | int VolumeManager::linkPrimary(userid_t userId) { |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 384 | if (!GetBoolProperty(android::vold::kPropFuse, false)) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 385 | std::string source(mPrimary->getPath()); |
| 386 | if (mPrimary->isEmulated()) { |
| 387 | source = StringPrintf("%s/%d", source.c_str(), userId); |
| 388 | fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 389 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 390 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 391 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 392 | LOG(DEBUG) << "Linking " << source << " to " << target; |
| 393 | Symlink(source, target); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 394 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 398 | void VolumeManager::destroyEmulatedVolumesForUser(userid_t userId) { |
| 399 | // Destroy and remove all unstacked EmulatedVolumes for the user |
| 400 | auto i = mInternalEmulatedVolumes.begin(); |
| 401 | while (i != mInternalEmulatedVolumes.end()) { |
| 402 | auto vol = *i; |
| 403 | if (vol->getMountUserId() == userId) { |
| 404 | vol->destroy(); |
| 405 | i = mInternalEmulatedVolumes.erase(i); |
| 406 | } else { |
| 407 | i++; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Destroy and remove all stacked EmulatedVolumes for the user on each mounted private volume |
| 412 | std::list<std::string> private_vols; |
| 413 | listVolumes(VolumeBase::Type::kPrivate, private_vols); |
| 414 | for (const std::string& id : private_vols) { |
| 415 | PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get()); |
| 416 | std::list<std::shared_ptr<VolumeBase>> vols_to_remove; |
| 417 | if (pvol->getState() == VolumeBase::State::kMounted) { |
| 418 | for (const auto& vol : pvol->getVolumes()) { |
| 419 | if (vol->getMountUserId() == userId) { |
| 420 | vols_to_remove.push_back(vol); |
| 421 | } |
| 422 | } |
| 423 | for (const auto& vol : vols_to_remove) { |
| 424 | vol->destroy(); |
| 425 | pvol->removeVolume(vol); |
| 426 | } |
| 427 | } // else EmulatedVolumes will be destroyed on VolumeBase#unmount |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void VolumeManager::createEmulatedVolumesForUser(userid_t userId) { |
| 432 | // Create unstacked EmulatedVolumes for the user |
| 433 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 434 | new android::vold::EmulatedVolume("/data/media", userId)); |
| 435 | vol->setMountUserId(userId); |
| 436 | mInternalEmulatedVolumes.push_back(vol); |
| 437 | vol->create(); |
| 438 | |
| 439 | // Create stacked EmulatedVolumes for the user on each PrivateVolume |
| 440 | std::list<std::string> private_vols; |
| 441 | listVolumes(VolumeBase::Type::kPrivate, private_vols); |
| 442 | for (const std::string& id : private_vols) { |
| 443 | PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get()); |
| 444 | if (pvol->getState() == VolumeBase::State::kMounted) { |
| 445 | auto evol = |
| 446 | std::shared_ptr<android::vold::VolumeBase>(new android::vold::EmulatedVolume( |
| 447 | pvol->getPath() + "/media", pvol->getRawDevice(), pvol->getFsUuid(), |
| 448 | userId)); |
| 449 | evol->setMountUserId(userId); |
| 450 | pvol->addVolume(evol); |
| 451 | evol->create(); |
| 452 | } // else EmulatedVolumes will be created per user when on PrivateVolume#doMount |
| 453 | } |
| 454 | } |
| 455 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 456 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 457 | LOG(INFO) << "onUserAdded: " << userId; |
| 458 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 459 | mAddedUsers[userId] = userSerialNumber; |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | int VolumeManager::onUserRemoved(userid_t userId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 464 | LOG(INFO) << "onUserRemoved: " << userId; |
| 465 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 466 | onUserStopped(userId); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 467 | mAddedUsers.erase(userId); |
| 468 | return 0; |
| 469 | } |
| 470 | |
Sudheer Shanka | ebce4cc | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 471 | int VolumeManager::onUserStarted(userid_t userId) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 472 | LOG(INFO) << "onUserStarted: " << userId; |
| 473 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 474 | if (mStartedUsers.find(userId) == mStartedUsers.end()) { |
| 475 | createEmulatedVolumesForUser(userId); |
| 476 | } |
| 477 | |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 478 | if (!GetBoolProperty(android::vold::kPropFuse, false)) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 479 | // Note that sometimes the system will spin up processes from Zygote |
| 480 | // before actually starting the user, so we're okay if Zygote |
| 481 | // already created this directory. |
| 482 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
| 483 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 484 | |
| 485 | if (mPrimary) { |
| 486 | linkPrimary(userId); |
| 487 | } |
| 488 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 489 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 490 | mStartedUsers.insert(userId); |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 491 | |
| 492 | createPendingDisksIfNeeded(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 496 | int VolumeManager::onUserStopped(userid_t userId) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 497 | LOG(VERBOSE) << "onUserStopped: " << userId; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 498 | |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 499 | if (mStartedUsers.find(userId) != mStartedUsers.end()) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 500 | destroyEmulatedVolumesForUser(userId); |
| 501 | } |
| 502 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 503 | mStartedUsers.erase(userId); |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 507 | void VolumeManager::createPendingDisksIfNeeded() { |
| 508 | bool userZeroStarted = mStartedUsers.find(0) != mStartedUsers.end(); |
| 509 | if (!mSecureKeyguardShowing && userZeroStarted) { |
| 510 | // Now that secure keyguard has been dismissed and user 0 has |
| 511 | // started, process any pending disks |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 512 | for (const auto& disk : mPendingDisks) { |
| 513 | disk->create(); |
| 514 | mDisks.push_back(disk); |
| 515 | } |
| 516 | mPendingDisks.clear(); |
| 517 | } |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) { |
| 521 | mSecureKeyguardShowing = isShowing; |
| 522 | createPendingDisksIfNeeded(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 526 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 527 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 528 | for (userid_t userId : mStartedUsers) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 529 | linkPrimary(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 530 | } |
| 531 | return 0; |
| 532 | } |
| 533 | |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 534 | // This code is executed after a fork so it's very important that the set of |
| 535 | // methods we call here is strictly limited. |
| 536 | // |
| 537 | // TODO: Get rid of this guesswork altogether and instead exec a process |
| 538 | // immediately after fork to do our bindding for us. |
| 539 | static bool childProcess(const char* storageSource, const char* userSource, int nsFd, |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 540 | const char* name) { |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 541 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 542 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to setns for %s :%s", name, |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 543 | strerror(errno)); |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | // NOTE: Inlined from vold::UnmountTree here to avoid using PLOG methods and |
| 548 | // to also protect against future changes that may cause issues across a |
| 549 | // fork. |
| 550 | if (TEMP_FAILURE_RETRY(umount2("/storage/", MNT_DETACH)) < 0 && errno != EINVAL && |
| 551 | errno != ENOENT) { |
| 552 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to unmount /storage/ :%s", |
| 553 | strerror(errno)); |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | if (TEMP_FAILURE_RETRY(mount(storageSource, "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 558 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s", |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 559 | storageSource, name, strerror(errno)); |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 560 | return false; |
| 561 | } |
| 562 | |
| 563 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) { |
| 564 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 565 | "Failed to set MS_SLAVE to /storage for %s :%s", name, |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 566 | strerror(errno)); |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | if (TEMP_FAILURE_RETRY(mount(userSource, "/storage/self", NULL, MS_BIND, NULL)) == -1) { |
| 571 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s", |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 572 | userSource, name, strerror(errno)); |
Narayan Kamath | 02efdf5 | 2019-11-27 10:53:51 +0000 | [diff] [blame] | 573 | return false; |
| 574 | } |
| 575 | |
| 576 | return true; |
| 577 | } |
| 578 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 579 | // Fork the process and remount storage |
| 580 | bool forkAndRemountChild(uid_t uid, pid_t pid, int nsFd, const char* name, void* params) { |
| 581 | int32_t mountMode = *static_cast<int32_t*>(params); |
| 582 | std::string userSource; |
| 583 | std::string storageSource; |
| 584 | pid_t child; |
| 585 | // Need to fix these paths to account for when sdcardfs is gone |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 586 | switch (mountMode) { |
| 587 | case VoldNativeService::REMOUNT_MODE_NONE: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 588 | return true; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 589 | case VoldNativeService::REMOUNT_MODE_DEFAULT: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 590 | storageSource = "/mnt/runtime/default"; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 591 | break; |
| 592 | case VoldNativeService::REMOUNT_MODE_READ: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 593 | storageSource = "/mnt/runtime/read"; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 594 | break; |
| 595 | case VoldNativeService::REMOUNT_MODE_WRITE: |
Sudheer Shanka | a05ea74 | 2019-04-12 13:55:28 -0700 | [diff] [blame] | 596 | case VoldNativeService::REMOUNT_MODE_LEGACY: |
| 597 | case VoldNativeService::REMOUNT_MODE_INSTALLER: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 598 | storageSource = "/mnt/runtime/write"; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 599 | break; |
Sudheer Shanka | 36bdf7a | 2019-04-18 15:18:30 -0700 | [diff] [blame] | 600 | case VoldNativeService::REMOUNT_MODE_FULL: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 601 | storageSource = "/mnt/runtime/full"; |
Sudheer Shanka | 36bdf7a | 2019-04-18 15:18:30 -0700 | [diff] [blame] | 602 | break; |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 603 | case VoldNativeService::REMOUNT_MODE_PASS_THROUGH: |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 604 | return true; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 605 | default: |
| 606 | PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 607 | return false; |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 608 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 609 | LOG(DEBUG) << "Remounting " << uid << " as " << storageSource; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 610 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 611 | // Fork a child to mount user-specific symlink helper into place |
| 612 | userSource = StringPrintf("/mnt/user/%d", multiuser_get_user_id(uid)); |
| 613 | if (!(child = fork())) { |
| 614 | if (childProcess(storageSource.c_str(), userSource.c_str(), nsFd, name)) { |
| 615 | _exit(0); |
| 616 | } else { |
| 617 | _exit(1); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if (child == -1) { |
| 622 | PLOG(ERROR) << "Failed to fork"; |
| 623 | return false; |
| 624 | } else { |
| 625 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 626 | } |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | // Helper function to scan all processes in /proc and call the callback if: |
| 631 | // 1). pid belongs to an app process |
| 632 | // 2). If input uid is 0 or it matches the process uid |
| 633 | // 3). If userId is not -1 or userId matches the process userId |
| 634 | bool scanProcProcesses(uid_t uid, userid_t userId, ScanProcCallback callback, void* params) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 635 | DIR* dir; |
| 636 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 637 | std::string rootName; |
| 638 | std::string pidName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 639 | int pidFd; |
| 640 | int nsFd; |
| 641 | struct stat sb; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 642 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 643 | static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false); |
| 644 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 645 | if (!(dir = opendir("/proc"))) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 646 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to opendir"); |
| 647 | return false; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 651 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 652 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to read root namespace"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 653 | closedir(dir); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 654 | return false; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 657 | async_safe_format_log(ANDROID_LOG_INFO, "vold", "Start scanning all processes"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 658 | // Poke through all running PIDs look for apps running as UID |
| 659 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 660 | pid_t pid; |
| 661 | if (de->d_type != DT_DIR) continue; |
| 662 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 663 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 664 | pidFd = -1; |
| 665 | nsFd = -1; |
| 666 | |
| 667 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 668 | if (pidFd < 0) { |
| 669 | goto next; |
| 670 | } |
| 671 | if (fstat(pidFd, &sb) != 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 672 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to stat %s", de->d_name); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 673 | goto next; |
| 674 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 675 | if (uid != 0 && sb.st_uid != uid) { |
| 676 | goto next; |
| 677 | } |
| 678 | if (userId != static_cast<userid_t>(-1) && multiuser_get_user_id(sb.st_uid) != userId) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 679 | goto next; |
| 680 | } |
| 681 | |
| 682 | // Matches so far, but refuse to touch if in root namespace |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 683 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 684 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", |
| 685 | "Failed to read namespacefor %s", de->d_name); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 686 | goto next; |
| 687 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 688 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 689 | goto next; |
| 690 | } |
| 691 | |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 692 | if (apexUpdatable) { |
| 693 | std::string exeName; |
| 694 | // When ro.apex.bionic_updatable is set to true, |
| 695 | // some early native processes have mount namespaces that are different |
| 696 | // from that of the init. Therefore, above check can't filter them out. |
| 697 | // Since the propagation type of / is 'shared', unmounting /storage |
| 698 | // for the early native processes affects other processes including |
| 699 | // init. Filter out such processes by skipping if a process is a |
| 700 | // non-Java process whose UID is < AID_APP_START. (The UID condition |
| 701 | // is required to not filter out child processes spawned by apps.) |
| 702 | if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) { |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 703 | goto next; |
| 704 | } |
| 705 | if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) { |
Jiyong Park | 8d21c92 | 2019-01-04 13:35:25 +0900 | [diff] [blame] | 706 | goto next; |
| 707 | } |
| 708 | } |
| 709 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 710 | // We purposefully leave the namespace open across the fork |
Nick Kralevich | e7e89ac | 2019-03-29 16:03:51 -0700 | [diff] [blame] | 711 | // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC |
| 712 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 713 | if (nsFd < 0) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 714 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", |
| 715 | "Failed to open namespace for %s", de->d_name); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 716 | goto next; |
| 717 | } |
| 718 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 719 | if (!callback(sb.st_uid, pid, nsFd, de->d_name, params)) { |
| 720 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed in callback"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 723 | next: |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 724 | close(nsFd); |
| 725 | close(pidFd); |
| 726 | } |
| 727 | closedir(dir); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 728 | async_safe_format_log(ANDROID_LOG_INFO, "vold", "Finished scanning all processes"); |
| 729 | return true; |
| 730 | } |
| 731 | |
| 732 | int VolumeManager::remountUid(uid_t uid, int32_t mountMode) { |
| 733 | if (GetBoolProperty(android::vold::kPropFuse, false)) { |
| 734 | // TODO(135341433): Implement fuse specific logic. |
| 735 | return 0; |
| 736 | } |
| 737 | return scanProcProcesses(uid, static_cast<userid_t>(-1), |
| 738 | forkAndRemountChild, &mountMode) ? 0 : -1; |
| 739 | } |
| 740 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 741 | |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 742 | // Set the namespace the app process and remount its storage directories. |
| 743 | static bool remountStorageDirs(int nsFd, const char* sources[], const char* targets[], int size) { |
| 744 | // This code is executed after a fork so it's very important that the set of |
| 745 | // methods we call here is strictly limited. |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 746 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
| 747 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to setns %s", strerror(errno)); |
| 748 | return false; |
| 749 | } |
| 750 | |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 751 | for (int i = 0; i < size; i++) { |
| 752 | if (TEMP_FAILURE_RETRY(mount(sources[i], targets[i], NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 753 | async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s to %s :%s", |
| 754 | sources[i], targets[i], strerror(errno)); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 755 | return false; |
| 756 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 757 | } |
| 758 | return true; |
| 759 | } |
| 760 | |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 761 | static std::string getStorageDirSrc(userid_t userId, const std::string& dirName, |
| 762 | const std::string& packageName) { |
| 763 | if (IsFilesystemSupported("sdcardfs")) { |
| 764 | return StringPrintf("/mnt/runtime/default/emulated/%d/%s/%s", |
| 765 | userId, dirName.c_str(), packageName.c_str()); |
| 766 | } else { |
| 767 | return StringPrintf("/mnt/pass_through/%d/emulated/%d/%s/%s", |
| 768 | userId, userId, dirName.c_str(), packageName.c_str()); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 769 | } |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | static std::string getStorageDirTarget(userid_t userId, std::string dirName, |
| 773 | std::string packageName) { |
| 774 | return StringPrintf("/storage/emulated/%d/%s/%s", |
| 775 | userId, dirName.c_str(), packageName.c_str()); |
| 776 | } |
| 777 | |
| 778 | // Fork the process and remount storage |
Linus Tufvesson | 75973cb | 2020-03-23 11:59:43 +0000 | [diff] [blame] | 779 | bool VolumeManager::forkAndRemountStorage(int uid, int pid, |
| 780 | const std::vector<std::string>& packageNames) { |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 781 | userid_t userId = multiuser_get_user_id(uid); |
| 782 | std::string mnt_path = StringPrintf("/proc/%d/ns/mnt", pid); |
| 783 | android::base::unique_fd nsFd( |
| 784 | TEMP_FAILURE_RETRY(open(mnt_path.c_str(), O_RDONLY | O_CLOEXEC))); |
| 785 | if (nsFd == -1) { |
| 786 | PLOG(ERROR) << "Unable to open " << mnt_path.c_str(); |
| 787 | return false; |
| 788 | } |
| 789 | // Storing both Android/obb and Android/data paths. |
| 790 | int size = packageNames.size() * 2; |
| 791 | |
| 792 | std::unique_ptr<std::string[]> sources(new std::string[size]); |
| 793 | std::unique_ptr<std::string[]> targets(new std::string[size]); |
| 794 | std::unique_ptr<const char*[]> sources_uptr(new const char*[size]); |
| 795 | std::unique_ptr<const char*[]> targets_uptr(new const char*[size]); |
| 796 | const char** sources_cstr = sources_uptr.get(); |
| 797 | const char** targets_cstr = targets_uptr.get(); |
| 798 | |
| 799 | for (int i = 0; i < size; i += 2) { |
| 800 | std::string const& packageName = packageNames[i/2]; |
| 801 | sources[i] = getStorageDirSrc(userId, "Android/data", packageName); |
| 802 | targets[i] = getStorageDirTarget(userId, "Android/data", packageName); |
| 803 | sources[i+1] = getStorageDirSrc(userId, "Android/obb", packageName); |
| 804 | targets[i+1] = getStorageDirTarget(userId, "Android/obb", packageName); |
| 805 | |
| 806 | sources_cstr[i] = sources[i].c_str(); |
| 807 | targets_cstr[i] = targets[i].c_str(); |
| 808 | sources_cstr[i+1] = sources[i+1].c_str(); |
| 809 | targets_cstr[i+1] = targets[i+1].c_str(); |
| 810 | } |
| 811 | |
| 812 | for (int i = 0; i < size; i++) { |
| 813 | auto status = EnsureDirExists(sources_cstr[i], 0771, AID_MEDIA_RW, AID_MEDIA_RW); |
| 814 | if (status != OK) { |
| 815 | PLOG(ERROR) << "Failed to create dir: " << sources_cstr[i]; |
| 816 | return false; |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 817 | } |
Linus Tufvesson | 75973cb | 2020-03-23 11:59:43 +0000 | [diff] [blame] | 818 | // Make sure /storage/emulated/... paths are setup correctly |
| 819 | status = setupAppDir(targets_cstr[i], uid, false /* fixupExistingOnly */); |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 820 | if (status != OK) { |
| 821 | PLOG(ERROR) << "Failed to create dir: " << targets_cstr[i]; |
| 822 | return false; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | pid_t child; |
| 827 | // Fork a child to mount Android/obb android Android/data dirs, as we don't want it to affect |
| 828 | // original vold process mount namespace. |
| 829 | if (!(child = fork())) { |
| 830 | if (remountStorageDirs(nsFd, sources_cstr, targets_cstr, size)) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 831 | _exit(0); |
| 832 | } else { |
| 833 | _exit(1); |
| 834 | } |
| 835 | } |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 836 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 837 | if (child == -1) { |
| 838 | PLOG(ERROR) << "Failed to fork"; |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 839 | return false; |
| 840 | } else { |
| 841 | int status; |
| 842 | if (TEMP_FAILURE_RETRY(waitpid(child, &status, 0)) == -1) { |
| 843 | PLOG(ERROR) << "Failed to waitpid: " << child; |
| 844 | return false; |
| 845 | } |
| 846 | if (!WIFEXITED(status)) { |
| 847 | PLOG(ERROR) << "Process did not exit normally, status: " << status; |
| 848 | return false; |
| 849 | } |
| 850 | if (WEXITSTATUS(status)) { |
| 851 | PLOG(ERROR) << "Process exited with code: " << WEXITSTATUS(status); |
| 852 | return false; |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 855 | return true; |
| 856 | } |
| 857 | |
| 858 | int VolumeManager::remountAppStorageDirs(int uid, int pid, |
| 859 | const std::vector<std::string>& packageNames) { |
| 860 | if (!GetBoolProperty(android::vold::kPropFuse, false)) { |
| 861 | return 0; |
| 862 | } |
| 863 | // Only run the remount if fuse is mounted for that user. |
| 864 | userid_t userId = multiuser_get_user_id(uid); |
| 865 | bool fuseMounted = false; |
| 866 | for (auto& vol : mInternalEmulatedVolumes) { |
| 867 | if (vol->getMountUserId() == userId && vol->getState() == VolumeBase::State::kMounted) { |
| 868 | auto* emulatedVol = static_cast<android::vold::EmulatedVolume*>(vol.get()); |
| 869 | if (emulatedVol) { |
| 870 | fuseMounted = emulatedVol->isFuseMounted(); |
| 871 | } |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | if (fuseMounted) { |
| 876 | forkAndRemountStorage(uid, pid, packageNames); |
| 877 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 881 | int VolumeManager::reset() { |
| 882 | // Tear down all existing disks/volumes and start from a blank slate so |
| 883 | // newly connected framework hears all events. |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 884 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 885 | vol->destroy(); |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 886 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 887 | mInternalEmulatedVolumes.clear(); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 888 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 889 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 890 | disk->destroy(); |
| 891 | disk->create(); |
| 892 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 893 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 894 | mAddedUsers.clear(); |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 895 | mStartedUsers.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 899 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 900 | int VolumeManager::shutdown() { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 901 | if (mInternalEmulatedVolumes.empty()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 902 | return 0; // already shutdown |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 903 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 904 | android::vold::sSleepOnUnmount = false; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 905 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 906 | vol->destroy(); |
| 907 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 908 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 909 | disk->destroy(); |
| 910 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 911 | |
| 912 | mInternalEmulatedVolumes.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 913 | mDisks.clear(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 914 | mPendingDisks.clear(); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 915 | android::vold::sSleepOnUnmount = true; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 916 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 919 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 920 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 921 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 922 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 923 | // First, try gracefully unmounting all known devices |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 924 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 925 | vol->unmount(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 926 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 927 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 928 | disk->unmountAll(); |
| 929 | } |
| 930 | |
| 931 | // Worst case we might have some stale mounts lurking around, so |
| 932 | // force unmount those just to be safe. |
LongPing.WEI | 4f04606 | 2018-11-23 19:27:35 +0800 | [diff] [blame] | 933 | FILE* fp = setmntent("/proc/mounts", "re"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 934 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 935 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 936 | return -errno; |
| 937 | } |
| 938 | |
| 939 | // Some volumes can be stacked on each other, so force unmount in |
| 940 | // reverse order to give us the best chance of success. |
| 941 | std::list<std::string> toUnmount; |
| 942 | mntent* mentry; |
| 943 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 944 | auto test = std::string(mentry->mnt_dir); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 945 | if ((StartsWith(test, "/mnt/") && |
| 946 | #ifdef __ANDROID_DEBUGGABLE__ |
| 947 | !StartsWith(test, "/mnt/scratch") && |
| 948 | #endif |
Martijn Coenen | b0e977a | 2020-01-11 19:24:26 +0100 | [diff] [blame] | 949 | !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product") && |
Ricky Wai | ef63921 | 2020-04-07 13:43:20 +0100 | [diff] [blame^] | 950 | !StartsWith(test, "/mnt/installer") && !StartsWith(test, "/mnt/androidwritable")) || |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 951 | StartsWith(test, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 952 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | endmntent(fp); |
| 956 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 957 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 958 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 959 | android::vold::ForceUnmount(path); |
| 960 | } |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 965 | int VolumeManager::setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 966 | // Only offer to create directories for paths managed by vold |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 967 | if (!StartsWith(path, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 968 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 969 | return -EINVAL; |
| 970 | } |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 971 | |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 972 | // Find the volume it belongs to |
| 973 | auto filter_fn = [&](const VolumeBase& vol) { |
| 974 | if (vol.getState() != VolumeBase::State::kMounted) { |
| 975 | // The volume must be mounted |
| 976 | return false; |
| 977 | } |
| 978 | if ((vol.getMountFlags() & VolumeBase::MountFlags::kVisible) == 0) { |
| 979 | // and visible |
| 980 | return false; |
| 981 | } |
| 982 | if (vol.getInternalPath().empty()) { |
| 983 | return false; |
| 984 | } |
| 985 | if (vol.getMountUserId() != USER_UNKNOWN && |
| 986 | vol.getMountUserId() != multiuser_get_user_id(appUid)) { |
| 987 | // The app dir must be created on a volume with the same user-id |
| 988 | return false; |
| 989 | } |
| 990 | if (!path.empty() && StartsWith(path, vol.getPath())) { |
| 991 | return true; |
| 992 | } |
| 993 | |
| 994 | return false; |
| 995 | }; |
| 996 | auto volume = findVolumeWithFilter(filter_fn); |
| 997 | if (volume == nullptr) { |
| 998 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
| 999 | return -EINVAL; |
| 1000 | } |
Zim | c59d774 | 2020-01-06 21:48:16 +0000 | [diff] [blame] | 1001 | // Convert paths to lower filesystem paths to avoid making FUSE requests for these reasons: |
| 1002 | // 1. A FUSE request from vold puts vold at risk of hanging if the FUSE daemon is down |
| 1003 | // 2. The FUSE daemon prevents requests on /mnt/user/0/emulated/<userid != 0> and a request |
| 1004 | // on /storage/emulated/10 means /mnt/user/0/emulated/10 |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 1005 | const std::string lowerPath = |
| 1006 | volume->getInternalPath() + path.substr(volume->getPath().length()); |
Zim | c59d774 | 2020-01-06 21:48:16 +0000 | [diff] [blame] | 1007 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 1008 | const std::string volumeRoot = volume->getRootPath(); // eg /data/media/0 |
| 1009 | |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 1010 | if (fixupExistingOnly && (access(lowerPath.c_str(), F_OK) != 0)) { |
| 1011 | // Nothing to fixup |
| 1012 | return OK; |
| 1013 | } |
| 1014 | |
Martijn Coenen | b5a31c9 | 2020-02-13 23:30:38 +0100 | [diff] [blame] | 1015 | // Create the app paths we need from the root |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 1016 | return PrepareAppDirFromRoot(lowerPath, volumeRoot, appUid, fixupExistingOnly); |
| 1017 | } |
| 1018 | |
| 1019 | int VolumeManager::fixupAppDir(const std::string& path, int32_t appUid) { |
| 1020 | if (IsFilesystemSupported("sdcardfs")) { |
| 1021 | //sdcardfs magically does this for us |
| 1022 | return OK; |
| 1023 | } |
| 1024 | return setupAppDir(path, appUid, true /* fixupExistingOnly */); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1025 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1026 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1027 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1028 | int32_t ownerGid, std::string* outVolId) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1029 | int id = mNextObbId++; |
| 1030 | |
| 1031 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1032 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1033 | vol->create(); |
| 1034 | |
| 1035 | mObbVolumes.push_back(vol); |
| 1036 | *outVolId = vol->getId(); |
| 1037 | return android::OK; |
| 1038 | } |
| 1039 | |
| 1040 | int VolumeManager::destroyObb(const std::string& volId) { |
| 1041 | auto i = mObbVolumes.begin(); |
| 1042 | while (i != mObbVolumes.end()) { |
| 1043 | if ((*i)->getId() == volId) { |
| 1044 | (*i)->destroy(); |
| 1045 | i = mObbVolumes.erase(i); |
| 1046 | } else { |
| 1047 | ++i; |
| 1048 | } |
| 1049 | } |
| 1050 | return android::OK; |
| 1051 | } |
| 1052 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1053 | int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 1054 | const std::string& fsType, const std::string& fsUuid, |
Risan | 73a7a85 | 2020-02-07 18:03:44 +0900 | [diff] [blame] | 1055 | const std::string& fsLabel, int32_t flags, |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1056 | std::string* outVolId) { |
| 1057 | dev_t stubId = --mNextStubId; |
| 1058 | auto vol = std::shared_ptr<android::vold::StubVolume>( |
| 1059 | new android::vold::StubVolume(stubId, sourcePath, mountPath, fsType, fsUuid, fsLabel)); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1060 | |
Risan | 73a7a85 | 2020-02-07 18:03:44 +0900 | [diff] [blame] | 1061 | int32_t passedFlags = android::vold::Disk::Flags::kStub; |
| 1062 | passedFlags |= (flags & android::vold::Disk::Flags::kUsb); |
| 1063 | passedFlags |= (flags & android::vold::Disk::Flags::kSd); |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1064 | // StubDisk doesn't have device node corresponds to it. So, a fake device |
Risan | 73a7a85 | 2020-02-07 18:03:44 +0900 | [diff] [blame] | 1065 | // number is used. |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1066 | auto disk = std::shared_ptr<android::vold::Disk>( |
Risan | 73a7a85 | 2020-02-07 18:03:44 +0900 | [diff] [blame] | 1067 | new android::vold::Disk("stub", stubId, "stub", passedFlags)); |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1068 | disk->initializePartition(vol); |
| 1069 | handleDiskAdded(disk); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1070 | *outVolId = vol->getId(); |
| 1071 | return android::OK; |
| 1072 | } |
| 1073 | |
| 1074 | int VolumeManager::destroyStubVolume(const std::string& volId) { |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 1075 | auto tokens = android::base::Split(volId, ":"); |
| 1076 | CHECK(tokens.size() == 2); |
| 1077 | dev_t stubId; |
| 1078 | CHECK(android::base::ParseUint(tokens[1], &stubId)); |
| 1079 | handleDiskRemoved(stubId); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1080 | return android::OK; |
| 1081 | } |
| 1082 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1083 | int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1084 | return android::vold::MountAppFuse(uid, mountId, device_fd); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1085 | } |
| 1086 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1087 | int VolumeManager::unmountAppFuse(uid_t uid, int mountId) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1088 | return android::vold::UnmountAppFuse(uid, mountId); |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1092 | return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1093 | } |