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