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