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