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