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