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