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 | // TODO: New processes could be started while traversing over the existing |
| 405 | // processes which would end up not having the necessary bind mounts. This |
| 406 | // issue needs to be fixed, may be by doing multiple passes here? |
| 407 | std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir("/proc"), closedir); |
| 408 | if (!dirp) { |
| 409 | PLOG(ERROR) << "Failed to opendir /proc"; |
| 410 | return -1; |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 411 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 412 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 413 | std::string rootName; |
| 414 | // Figure out root namespace to compare against below |
| 415 | if (!android::vold::Readlinkat(dirfd(dirp.get()), "1/ns/mnt", &rootName)) { |
| 416 | PLOG(ERROR) << "Failed to read root namespace"; |
| 417 | return -1; |
Sudheer Shanka | f768c27 | 2018-08-04 10:10:27 -0700 | [diff] [blame] | 418 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 419 | |
Sudheer Shanka | 5504901 | 2019-01-09 12:15:15 -0800 | [diff] [blame] | 420 | struct stat mntFullSb; |
| 421 | struct stat mntWriteSb; |
| 422 | if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/full", &mntFullSb)) == -1) { |
| 423 | PLOG(ERROR) << "Failed to stat /mnt/runtime/full"; |
| 424 | return -1; |
| 425 | } |
| 426 | if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/write", &mntWriteSb)) == -1) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 427 | PLOG(ERROR) << "Failed to stat /mnt/runtime/write"; |
| 428 | return -1; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 431 | std::string obbMountDir = StringPrintf("/mnt/user/%d/obb_mount", userId); |
| 432 | if (fs_prepare_dir(obbMountDir.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 433 | PLOG(ERROR) << "Failed to fs_prepare_dir " << obbMountDir; |
| 434 | return -1; |
| 435 | } |
| 436 | const unique_fd obbMountDirFd( |
| 437 | TEMP_FAILURE_RETRY(open(obbMountDir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC))); |
| 438 | if (obbMountDirFd.get() < 0) { |
| 439 | PLOG(ERROR) << "Failed to open " << obbMountDir; |
| 440 | return -1; |
| 441 | } |
| 442 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 443 | std::unordered_set<appid_t> validAppIds; |
| 444 | for (auto& package : packageNames) { |
| 445 | validAppIds.insert(mAppIds[package]); |
Sudheer Shanka | a695f25 | 2018-08-03 18:07:52 -0700 | [diff] [blame] | 446 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 447 | std::vector<std::string>& userPackages = mUserPackages[userId]; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 448 | |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 449 | std::vector<pid_t> childPids; |
| 450 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 451 | struct dirent* de; |
| 452 | // Poke through all running PIDs look for apps running in userId |
| 453 | while ((de = readdir(dirp.get()))) { |
| 454 | pid_t pid; |
| 455 | if (de->d_type != DT_DIR) continue; |
| 456 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 457 | |
| 458 | const unique_fd pidFd( |
| 459 | openat(dirfd(dirp.get()), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 460 | if (pidFd.get() < 0) { |
| 461 | PLOG(WARNING) << "Failed to open /proc/" << pid; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 462 | continue; |
| 463 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 464 | struct stat sb; |
| 465 | if (fstat(pidFd.get(), &sb) != 0) { |
| 466 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 467 | continue; |
| 468 | } |
| 469 | if (multiuser_get_user_id(sb.st_uid) != userId) { |
| 470 | continue; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 471 | } |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 472 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 473 | // Matches so far, but refuse to touch if in root namespace |
| 474 | LOG(VERBOSE) << "Found matching PID " << de->d_name; |
| 475 | std::string pidName; |
| 476 | if (!android::vold::Readlinkat(pidFd.get(), "ns/mnt", &pidName)) { |
| 477 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 478 | continue; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 479 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 480 | if (rootName == pidName) { |
| 481 | LOG(WARNING) << "Skipping due to root namespace"; |
| 482 | continue; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 485 | // Only update the mount points of processes running with one of validAppIds. |
| 486 | // This should skip any isolated uids. |
| 487 | appid_t appId = multiuser_get_app_id(sb.st_uid); |
| 488 | if (validAppIds.find(appId) == validAppIds.end()) { |
| 489 | continue; |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 492 | std::vector<std::string> packagesForUid; |
| 493 | for (auto& package : userPackages) { |
| 494 | if (mAppIds[package] == appId) { |
| 495 | packagesForUid.push_back(package); |
| 496 | } |
| 497 | } |
| 498 | if (packagesForUid.empty()) { |
| 499 | continue; |
| 500 | } |
| 501 | const std::string& sandboxId = mSandboxIds[appId]; |
| 502 | |
| 503 | // We purposefully leave the namespace open across the fork |
| 504 | unique_fd nsFd(openat(pidFd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC |
| 505 | if (nsFd.get() < 0) { |
| 506 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
| 507 | continue; |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 510 | pid_t child; |
| 511 | if (!(child = fork())) { |
| 512 | if (setns(nsFd.get(), CLONE_NEWNS) != 0) { |
| 513 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
| 514 | _exit(1); |
| 515 | } |
| 516 | |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 517 | int mountMode; |
| 518 | if (remountMode == -1) { |
Sudheer Shanka | 5504901 | 2019-01-09 12:15:15 -0800 | [diff] [blame] | 519 | mountMode = |
| 520 | getMountModeForRunningProc(packagesForUid, userId, mntWriteSb, mntFullSb); |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 521 | if (mountMode == -1) { |
| 522 | _exit(1); |
| 523 | } |
| 524 | } else { |
| 525 | mountMode = remountMode; |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 526 | if (handleMountModeInstaller(mountMode, obbMountDirFd.get(), obbMountDir, |
| 527 | sandboxId) < 0) { |
| 528 | _exit(1); |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | if (mountMode == VoldNativeService::REMOUNT_MODE_FULL || |
Sudheer Shanka | 5504901 | 2019-01-09 12:15:15 -0800 | [diff] [blame] | 532 | mountMode == VoldNativeService::REMOUNT_MODE_LEGACY || |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 533 | mountMode == VoldNativeService::REMOUNT_MODE_NONE) { |
| 534 | // These mount modes are not going to change dynamically, so don't bother |
| 535 | // unmounting/remounting dirs. |
| 536 | _exit(0); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 539 | for (auto& volumeLabel : visibleVolLabels) { |
| 540 | std::string mntSource = StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str()); |
| 541 | std::string mntTarget = StringPrintf("/storage/%s", volumeLabel.c_str()); |
| 542 | if (volumeLabel == "emulated") { |
| 543 | StringAppendF(&mntSource, "/%d", userId); |
| 544 | StringAppendF(&mntTarget, "/%d", userId); |
| 545 | } |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 546 | |
| 547 | std::string sandboxSource = |
| 548 | StringPrintf("%s/Android/sandbox/%s", mntSource.c_str(), sandboxId.c_str()); |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 549 | if (CreateDir(sandboxSource, 0755) < 0) { |
| 550 | continue; |
| 551 | } |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 552 | if (BindMount(sandboxSource, mntTarget) < 0) { |
| 553 | continue; |
| 554 | } |
| 555 | |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 556 | std::string obbSourceDir = StringPrintf("%s/Android/obb", mntSource.c_str()); |
| 557 | std::string obbTargetDir = StringPrintf("%s/Android/obb", mntTarget.c_str()); |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 558 | if (UnmountTree(obbTargetDir) < 0) { |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 559 | continue; |
| 560 | } |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 561 | if (!createPkgSpecificDirRoots(mntSource) || !createPkgSpecificDirRoots(mntTarget)) { |
| 562 | continue; |
| 563 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 564 | for (auto& package : packagesForUid) { |
| 565 | mountPkgSpecificDir(mntSource, mntTarget, package, "data"); |
| 566 | mountPkgSpecificDir(mntSource, mntTarget, package, "media"); |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 567 | if (mountMode != VoldNativeService::REMOUNT_MODE_INSTALLER) { |
| 568 | mountPkgSpecificDir(mntSource, mntTarget, package, "obb"); |
| 569 | } |
| 570 | } |
| 571 | if (mountMode == VoldNativeService::REMOUNT_MODE_INSTALLER) { |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 572 | if (BindMount(obbSourceDir, obbTargetDir) < 0) { |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 573 | continue; |
| 574 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | _exit(0); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 578 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 579 | |
| 580 | if (child == -1) { |
| 581 | PLOG(ERROR) << "Failed to fork"; |
| 582 | } else { |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 583 | childPids.push_back(child); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 586 | for (auto& child : childPids) { |
| 587 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 588 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 592 | int VolumeManager::getMountModeForRunningProc(const std::vector<std::string>& packagesForUid, |
Sudheer Shanka | 5504901 | 2019-01-09 12:15:15 -0800 | [diff] [blame] | 593 | userid_t userId, struct stat& mntWriteStat, |
| 594 | struct stat& mntFullStat) { |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 595 | struct stat storageSb; |
| 596 | if (TEMP_FAILURE_RETRY(stat("/storage", &storageSb)) == -1) { |
| 597 | PLOG(ERROR) << "Failed to stat /storage"; |
| 598 | return -1; |
| 599 | } |
| 600 | |
| 601 | // Some packages have access to full external storage, identify processes belonging |
Sudheer Shanka | 5504901 | 2019-01-09 12:15:15 -0800 | [diff] [blame] | 602 | // to those packages by comparing inode no.s of /mnt/runtime/full and /storage |
| 603 | if (storageSb.st_dev == mntFullStat.st_dev && storageSb.st_ino == mntFullStat.st_ino) { |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 604 | return VoldNativeService::REMOUNT_MODE_FULL; |
Sudheer Shanka | 5504901 | 2019-01-09 12:15:15 -0800 | [diff] [blame] | 605 | } else if (storageSb.st_dev == mntWriteStat.st_dev && storageSb.st_ino == mntWriteStat.st_ino) { |
| 606 | return VoldNativeService::REMOUNT_MODE_LEGACY; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | std::string obbMountFile = |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 610 | StringPrintf("/mnt/user/%d/obb_mount/%s", userId, packagesForUid[0].c_str()); |
| 611 | if (TEMP_FAILURE_RETRY(access(obbMountFile.c_str(), F_OK)) != -1) { |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 612 | return VoldNativeService::REMOUNT_MODE_INSTALLER; |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 613 | } else if (errno != ENOENT) { |
| 614 | PLOG(ERROR) << "Failed to access " << obbMountFile; |
| 615 | return -1; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | // Some packages don't have access to external storage and processes belonging to |
| 619 | // those packages don't have anything mounted at /storage. So, identify those |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 620 | // processes by comparing inode no.s of /mnt/user/%d/package |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 621 | // and /storage |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 622 | std::string sandbox = StringPrintf("/mnt/user/%d/package", userId); |
| 623 | struct stat sandboxStat; |
| 624 | if (TEMP_FAILURE_RETRY(stat(sandbox.c_str(), &sandboxStat)) == -1) { |
| 625 | PLOG(ERROR) << "Failed to stat " << sandbox; |
| 626 | return -1; |
| 627 | } |
| 628 | if (storageSb.st_dev == sandboxStat.st_dev && storageSb.st_ino == sandboxStat.st_ino) { |
| 629 | return VoldNativeService::REMOUNT_MODE_WRITE; |
| 630 | } |
| 631 | |
| 632 | return VoldNativeService::REMOUNT_MODE_NONE; |
| 633 | } |
| 634 | |
| 635 | int VolumeManager::handleMountModeInstaller(int mountMode, int obbMountDirFd, |
| 636 | const std::string& obbMountDir, |
| 637 | const std::string& sandboxId) { |
| 638 | if (mountMode == VoldNativeService::REMOUNT_MODE_INSTALLER) { |
| 639 | if (TEMP_FAILURE_RETRY(faccessat(obbMountDirFd, sandboxId.c_str(), F_OK, 0)) != -1) { |
| 640 | return 0; |
| 641 | } else if (errno != ENOENT) { |
| 642 | PLOG(ERROR) << "Failed to access " << obbMountDir << "/" << sandboxId; |
| 643 | return -errno; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 644 | } |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 645 | const unique_fd fd( |
| 646 | TEMP_FAILURE_RETRY(openat(obbMountDirFd, sandboxId.c_str(), O_RDWR | O_CREAT, 0600))); |
| 647 | if (fd.get() < 0) { |
| 648 | PLOG(ERROR) << "Failed to create " << obbMountDir << "/" << sandboxId; |
| 649 | return -errno; |
| 650 | } |
| 651 | } else { |
| 652 | if (TEMP_FAILURE_RETRY(faccessat(obbMountDirFd, sandboxId.c_str(), F_OK, 0)) != -1) { |
| 653 | if (TEMP_FAILURE_RETRY(unlinkat(obbMountDirFd, sandboxId.c_str(), 0)) == -1) { |
| 654 | PLOG(ERROR) << "Failed to unlink " << obbMountDir << "/" << sandboxId; |
| 655 | return -errno; |
| 656 | } |
| 657 | } else if (errno != ENOENT) { |
| 658 | PLOG(ERROR) << "Failed to access " << obbMountDir << "/" << sandboxId; |
| 659 | return -errno; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 660 | } |
| 661 | } |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 662 | return 0; |
Sudheer Shanka | 03992e3 | 2018-12-12 12:43:38 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 665 | int VolumeManager::prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames, |
| 666 | const std::vector<std::string>& visibleVolLabels) { |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 667 | prepareSandboxTargets(userId, visibleVolLabels); |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 668 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 669 | if (visibleVolLabels.empty()) { |
| 670 | return 0; |
| 671 | } |
| 672 | for (auto& volumeLabel : visibleVolLabels) { |
| 673 | std::string volumeRoot(StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str())); |
| 674 | bool isVolPrimaryEmulated = (volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated()); |
| 675 | if (isVolPrimaryEmulated) { |
| 676 | StringAppendF(&volumeRoot, "/%d", userId); |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 677 | if (CreateDir(volumeRoot, 0755) < 0) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 678 | return -errno; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | std::string sandboxRoot = |
| 683 | prepareSubDirs(volumeRoot, "Android/sandbox/", 0700, AID_ROOT, AID_ROOT); |
| 684 | if (sandboxRoot.empty()) { |
| 685 | return -errno; |
| 686 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 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 | uid_t uid = multiuser_get_uid(userId, AID_EVERYBODY); |
| 710 | for (auto& volumeLabel : visibleVolLabels) { |
| 711 | std::string sandboxTarget = |
| 712 | StringPrintf("%s/%s", mntTargetRoot.c_str(), volumeLabel.c_str()); |
| 713 | if (fs_prepare_dir(sandboxTarget.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) { |
| 714 | PLOG(ERROR) << "Failed to fs_prepare_dir " << sandboxTarget; |
| 715 | return -errno; |
| 716 | } |
| 717 | |
| 718 | if (mPrimary && volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated()) { |
| 719 | StringAppendF(&sandboxTarget, "/%d", userId); |
| 720 | if (fs_prepare_dir(sandboxTarget.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) { |
| 721 | PLOG(ERROR) << "Failed to fs_prepare_dir " << sandboxTarget; |
| 722 | return -errno; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | StringAppendF(&mntTargetRoot, "/self"); |
| 728 | if (fs_prepare_dir(mntTargetRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) { |
| 729 | PLOG(ERROR) << "Failed to fs_prepare_dir " << mntTargetRoot; |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 730 | return -errno; |
| 731 | } |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 732 | |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 733 | if (mPrimary) { |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 734 | std::string pkgPrimarySource(mPrimary->getPath()); |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 735 | if (mPrimary->isEmulated()) { |
| 736 | StringAppendF(&pkgPrimarySource, "/%d", userId); |
| 737 | } |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 738 | StringAppendF(&mntTargetRoot, "/primary"); |
| 739 | if (Symlink(pkgPrimarySource, mntTargetRoot) < 0) { |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 740 | return -errno; |
| 741 | } |
| 742 | } |
| 743 | return 0; |
| 744 | } |
| 745 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 746 | std::string VolumeManager::prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs, |
| 747 | mode_t mode, uid_t uid, gid_t gid) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 748 | std::string path(pathPrefix); |
| 749 | std::vector<std::string> subDirList = android::base::Split(subDirs, "/"); |
| 750 | for (size_t i = 0; i < subDirList.size(); ++i) { |
| 751 | std::string subDir = subDirList[i]; |
| 752 | if (subDir.empty()) { |
| 753 | continue; |
| 754 | } |
| 755 | StringAppendF(&path, "/%s", subDir.c_str()); |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 756 | if (CreateDir(path, mode) < 0) { |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 757 | return kEmptyString; |
| 758 | } |
| 759 | } |
| 760 | return path; |
| 761 | } |
| 762 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 763 | bool VolumeManager::createPkgSpecificDirRoots(const std::string& volumeRoot) { |
| 764 | std::string volumeAndroidRoot = StringPrintf("%s/Android", volumeRoot.c_str()); |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 765 | if (CreateDir(volumeAndroidRoot, 0700) < 0) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 766 | return false; |
| 767 | } |
| 768 | std::array<std::string, 3> dirs = {"data", "media", "obb"}; |
| 769 | for (auto& dir : dirs) { |
| 770 | std::string path = StringPrintf("%s/%s", volumeAndroidRoot.c_str(), dir.c_str()); |
Sudheer Shanka | f9b38a5 | 2019-02-14 19:09:51 +0000 | [diff] [blame] | 771 | if (CreateDir(path, 0700) < 0) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 772 | return false; |
| 773 | } |
| 774 | } |
| 775 | return true; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 778 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
| 779 | mAddedUsers[userId] = userSerialNumber; |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | int VolumeManager::onUserRemoved(userid_t userId) { |
| 784 | mAddedUsers.erase(userId); |
| 785 | return 0; |
| 786 | } |
| 787 | |
Sudheer Shanka | 69bc40f | 2018-10-25 11:06:55 -0700 | [diff] [blame] | 788 | int VolumeManager::onUserStarted(userid_t userId, const std::vector<std::string>& packageNames, |
| 789 | const std::vector<int>& appIds, |
| 790 | const std::vector<std::string>& sandboxIds) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 791 | LOG(VERBOSE) << "onUserStarted: " << userId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 792 | // Note that sometimes the system will spin up processes from Zygote |
| 793 | // before actually starting the user, so we're okay if Zygote |
| 794 | // already created this directory. |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 795 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 796 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 797 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 798 | mStartedUsers.insert(userId); |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 799 | mUserPackages[userId] = packageNames; |
Sudheer Shanka | 69bc40f | 2018-10-25 11:06:55 -0700 | [diff] [blame] | 800 | for (size_t i = 0; i < packageNames.size(); ++i) { |
| 801 | mAppIds[packageNames[i]] = appIds[i]; |
| 802 | mSandboxIds[appIds[i]] = sandboxIds[i]; |
| 803 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 804 | if (mPrimary) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 805 | linkPrimary(userId); |
| 806 | } |
Sudheer Shanka | 34b437b | 2019-01-17 17:20:47 -0800 | [diff] [blame] | 807 | if (hasIsolatedStorage()) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 808 | std::vector<std::string> visibleVolLabels; |
| 809 | for (auto& volId : mVisibleVolumeIds) { |
| 810 | auto vol = findVolume(volId); |
| 811 | userid_t mountUserId = vol->getMountUserId(); |
| 812 | if (mountUserId == userId || vol->isEmulated()) { |
| 813 | visibleVolLabels.push_back(vol->getLabel()); |
| 814 | } |
| 815 | } |
| 816 | if (prepareSandboxes(userId, packageNames, visibleVolLabels) != 0) { |
| 817 | return -errno; |
| 818 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 819 | } |
| 820 | return 0; |
| 821 | } |
| 822 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 823 | int VolumeManager::onUserStopped(userid_t userId) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 824 | LOG(VERBOSE) << "onUserStopped: " << userId; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 825 | mStartedUsers.erase(userId); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 826 | |
Sudheer Shanka | 34b437b | 2019-01-17 17:20:47 -0800 | [diff] [blame] | 827 | if (hasIsolatedStorage()) { |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 828 | auto& userPackages = mUserPackages[userId]; |
| 829 | std::string userMntTargetRoot = StringPrintf("/mnt/user/%d", userId); |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 830 | std::string pkgPrimaryDir = |
| 831 | StringPrintf("%s/package/self/primary", userMntTargetRoot.c_str()); |
| 832 | if (Unlink(pkgPrimaryDir) < 0) { |
| 833 | return -errno; |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 834 | } |
Sudheer Shanka | 9acc6d4 | 2018-10-06 19:03:02 -0700 | [diff] [blame] | 835 | mUserPackages.erase(userId); |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 836 | if (DeleteDirContentsAndDir(userMntTargetRoot) < 0) { |
| 837 | PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << userMntTargetRoot; |
Sudheer Shanka | 9acc6d4 | 2018-10-06 19:03:02 -0700 | [diff] [blame] | 838 | return -errno; |
| 839 | } |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 840 | LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << userMntTargetRoot; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 841 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 845 | int VolumeManager::addAppIds(const std::vector<std::string>& packageNames, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 846 | const std::vector<int32_t>& appIds) { |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 847 | for (size_t i = 0; i < packageNames.size(); ++i) { |
| 848 | mAppIds[packageNames[i]] = appIds[i]; |
| 849 | } |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | int VolumeManager::addSandboxIds(const std::vector<int32_t>& appIds, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 854 | const std::vector<std::string>& sandboxIds) { |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 855 | for (size_t i = 0; i < appIds.size(); ++i) { |
| 856 | mSandboxIds[appIds[i]] = sandboxIds[i]; |
| 857 | } |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 858 | return 0; |
| 859 | } |
| 860 | |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 861 | int VolumeManager::prepareSandboxForApp(const std::string& packageName, appid_t appId, |
| 862 | const std::string& sandboxId, userid_t userId) { |
Sudheer Shanka | 34b437b | 2019-01-17 17:20:47 -0800 | [diff] [blame] | 863 | if (!hasIsolatedStorage()) { |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | } else if (mStartedUsers.find(userId) == mStartedUsers.end()) { |
| 866 | // User not started, no need to do anything now. Required bind mounts for the package will |
| 867 | // be created when the user starts. |
| 868 | return 0; |
| 869 | } |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 870 | LOG(VERBOSE) << "prepareSandboxForApp: " << packageName << ", appId=" << appId |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 871 | << ", sandboxId=" << sandboxId << ", userId=" << userId; |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 872 | mUserPackages[userId].push_back(packageName); |
| 873 | mAppIds[packageName] = appId; |
| 874 | mSandboxIds[appId] = sandboxId; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 875 | |
| 876 | std::vector<std::string> visibleVolLabels; |
| 877 | for (auto& volId : mVisibleVolumeIds) { |
| 878 | auto vol = findVolume(volId); |
| 879 | userid_t mountUserId = vol->getMountUserId(); |
| 880 | if (mountUserId == userId || vol->isEmulated()) { |
| 881 | visibleVolLabels.push_back(vol->getLabel()); |
| 882 | } |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 883 | } |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 884 | return prepareSandboxes(userId, {packageName}, visibleVolLabels); |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Sudheer Shanka | 69bc40f | 2018-10-25 11:06:55 -0700 | [diff] [blame] | 887 | int VolumeManager::destroySandboxForApp(const std::string& packageName, |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 888 | const std::string& sandboxId, userid_t userId) { |
Sudheer Shanka | 34b437b | 2019-01-17 17:20:47 -0800 | [diff] [blame] | 889 | if (!hasIsolatedStorage()) { |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 890 | return 0; |
| 891 | } |
Sudheer Shanka | 69bc40f | 2018-10-25 11:06:55 -0700 | [diff] [blame] | 892 | LOG(VERBOSE) << "destroySandboxForApp: " << packageName << ", sandboxId=" << sandboxId |
| 893 | << ", userId=" << userId; |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 894 | auto& userPackages = mUserPackages[userId]; |
Sudheer Shanka | ba0c62f | 2018-12-16 18:22:34 -0800 | [diff] [blame] | 895 | userPackages.erase(std::remove(userPackages.begin(), userPackages.end(), packageName), |
| 896 | userPackages.end()); |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 897 | // If the package is not uninstalled in any other users, remove appId and sandboxId |
| 898 | // corresponding to it from the internal state. |
| 899 | bool installedInAnyUser = false; |
| 900 | for (auto& it : mUserPackages) { |
| 901 | auto& packages = it.second; |
| 902 | if (std::find(packages.begin(), packages.end(), packageName) != packages.end()) { |
| 903 | installedInAnyUser = true; |
| 904 | break; |
| 905 | } |
| 906 | } |
| 907 | if (!installedInAnyUser) { |
Sudheer Shanka | 69bc40f | 2018-10-25 11:06:55 -0700 | [diff] [blame] | 908 | const auto& entry = mAppIds.find(packageName); |
| 909 | if (entry != mAppIds.end()) { |
| 910 | mSandboxIds.erase(entry->second); |
| 911 | mAppIds.erase(entry); |
| 912 | } |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | std::vector<std::string> visibleVolLabels; |
| 916 | for (auto& volId : mVisibleVolumeIds) { |
| 917 | auto vol = findVolume(volId); |
| 918 | userid_t mountUserId = vol->getMountUserId(); |
| 919 | if (mountUserId == userId || vol->isEmulated()) { |
| 920 | if (destroySandboxForAppOnVol(packageName, sandboxId, userId, vol->getLabel()) < 0) { |
| 921 | return -errno; |
| 922 | } |
| 923 | } |
| 924 | } |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 925 | |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | int VolumeManager::destroySandboxForAppOnVol(const std::string& packageName, |
| 930 | const std::string& sandboxId, userid_t userId, |
| 931 | const std::string& volLabel) { |
| 932 | LOG(VERBOSE) << "destroySandboxOnVol: " << packageName << ", userId=" << userId |
| 933 | << ", volLabel=" << volLabel; |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 934 | |
| 935 | std::string sandboxDir = StringPrintf("/mnt/runtime/write/%s", volLabel.c_str()); |
| 936 | if (volLabel == mPrimary->getLabel() && mPrimary->isEmulated()) { |
| 937 | StringAppendF(&sandboxDir, "/%d", userId); |
| 938 | } |
Sudheer Shanka | 51a38da | 2018-10-18 08:51:31 -0700 | [diff] [blame] | 939 | StringAppendF(&sandboxDir, "/Android/sandbox/%s", sandboxId.c_str()); |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 940 | |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 941 | if (DeleteDirContentsAndDir(sandboxDir) < 0) { |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 942 | PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << sandboxDir; |
| 943 | return -errno; |
| 944 | } |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 945 | |
Sudheer Shanka | fa6a174 | 2018-10-04 16:26:22 -0700 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 949 | int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) { |
| 950 | mSecureKeyguardShowing = isShowing; |
| 951 | if (!mSecureKeyguardShowing) { |
| 952 | // Now that secure keyguard has been dismissed, process |
| 953 | // any pending disks |
| 954 | for (const auto& disk : mPendingDisks) { |
| 955 | disk->create(); |
| 956 | mDisks.push_back(disk); |
| 957 | } |
| 958 | mPendingDisks.clear(); |
| 959 | } |
| 960 | return 0; |
| 961 | } |
| 962 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 963 | int VolumeManager::onVolumeMounted(android::vold::VolumeBase* vol) { |
Sudheer Shanka | 34b437b | 2019-01-17 17:20:47 -0800 | [diff] [blame] | 964 | if (!hasIsolatedStorage()) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_VISIBLE) == 0) { |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | mVisibleVolumeIds.insert(vol->getId()); |
| 973 | userid_t mountUserId = vol->getMountUserId(); |
| 974 | if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) { |
| 975 | // We don't want to create another shared_ptr here because then we will end up with |
| 976 | // two shared_ptrs owning the underlying pointer without sharing it. |
| 977 | mPrimary = findVolume(vol->getId()); |
| 978 | for (userid_t userId : mStartedUsers) { |
| 979 | if (linkPrimary(userId) != 0) { |
| 980 | return -errno; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | if (vol->isEmulated()) { |
| 985 | for (userid_t userId : mStartedUsers) { |
| 986 | if (prepareSandboxes(userId, mUserPackages[userId], {vol->getLabel()}) != 0) { |
| 987 | return -errno; |
| 988 | } |
| 989 | } |
| 990 | } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) { |
| 991 | if (prepareSandboxes(mountUserId, mUserPackages[mountUserId], {vol->getLabel()}) != 0) { |
| 992 | return -errno; |
| 993 | } |
| 994 | } |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | int VolumeManager::onVolumeUnmounted(android::vold::VolumeBase* vol) { |
Sudheer Shanka | 34b437b | 2019-01-17 17:20:47 -0800 | [diff] [blame] | 999 | if (!hasIsolatedStorage()) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | if (mVisibleVolumeIds.erase(vol->getId()) == 0) { |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) { |
| 1008 | mPrimary = nullptr; |
| 1009 | } |
| 1010 | |
| 1011 | LOG(VERBOSE) << "visibleVolumeUnmounted: " << vol; |
| 1012 | userid_t mountUserId = vol->getMountUserId(); |
| 1013 | if (vol->isEmulated()) { |
| 1014 | for (userid_t userId : mStartedUsers) { |
| 1015 | if (destroySandboxesForVol(vol, userId) != 0) { |
| 1016 | return -errno; |
| 1017 | } |
| 1018 | } |
| 1019 | } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) { |
| 1020 | if (destroySandboxesForVol(vol, mountUserId) != 0) { |
| 1021 | return -errno; |
| 1022 | } |
| 1023 | } |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | int VolumeManager::destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId) { |
| 1028 | LOG(VERBOSE) << "destroysandboxesForVol: " << vol << " for user=" << userId; |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 1029 | std::string volSandboxRoot = |
| 1030 | StringPrintf("/mnt/user/%d/package/%s", userId, vol->getLabel().c_str()); |
| 1031 | if (android::vold::DeleteDirContentsAndDir(volSandboxRoot) < 0) { |
| 1032 | PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << volSandboxRoot; |
| 1033 | return -errno; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1034 | } |
Sudheer Shanka | 727c5fa | 2019-02-21 15:00:11 -0800 | [diff] [blame] | 1035 | LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << volSandboxRoot; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
| 1038 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1039 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
Sudheer Shanka | 34b437b | 2019-01-17 17:20:47 -0800 | [diff] [blame] | 1040 | if (hasIsolatedStorage()) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1041 | return 0; |
| 1042 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1043 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 1044 | for (userid_t userId : mStartedUsers) { |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1045 | linkPrimary(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1046 | } |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 1050 | int VolumeManager::remountUid(uid_t uid, int32_t mountMode) { |
Sudheer Shanka | 34b437b | 2019-01-17 17:20:47 -0800 | [diff] [blame] | 1051 | if (!hasIsolatedStorage()) { |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 1052 | return remountUidLegacy(uid, mountMode); |
| 1053 | } |
| 1054 | |
| 1055 | appid_t appId = multiuser_get_app_id(uid); |
| 1056 | userid_t userId = multiuser_get_user_id(uid); |
| 1057 | std::vector<std::string> visibleVolLabels; |
| 1058 | for (auto& volId : mVisibleVolumeIds) { |
| 1059 | auto vol = findVolume(volId); |
| 1060 | userid_t mountUserId = vol->getMountUserId(); |
| 1061 | if (mountUserId == userId || vol->isEmulated()) { |
| 1062 | visibleVolLabels.push_back(vol->getLabel()); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | // Finding one package with appId is enough |
| 1067 | std::vector<std::string> packageNames; |
| 1068 | for (auto it = mAppIds.begin(); it != mAppIds.end(); ++it) { |
| 1069 | if (it->second == appId) { |
| 1070 | packageNames.push_back(it->first); |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | if (packageNames.empty()) { |
| 1075 | PLOG(ERROR) << "Failed to find packageName for " << uid; |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 1076 | return -1; |
| 1077 | } |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 1078 | return mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels, mountMode); |
| 1079 | } |
| 1080 | |
| 1081 | int VolumeManager::remountUidLegacy(uid_t uid, int32_t mountMode) { |
| 1082 | std::string mode; |
| 1083 | switch (mountMode) { |
| 1084 | case VoldNativeService::REMOUNT_MODE_NONE: |
| 1085 | mode = "none"; |
| 1086 | break; |
| 1087 | case VoldNativeService::REMOUNT_MODE_DEFAULT: |
| 1088 | mode = "default"; |
| 1089 | break; |
| 1090 | case VoldNativeService::REMOUNT_MODE_READ: |
| 1091 | mode = "read"; |
| 1092 | break; |
| 1093 | case VoldNativeService::REMOUNT_MODE_WRITE: |
| 1094 | mode = "write"; |
| 1095 | break; |
| 1096 | default: |
| 1097 | PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode); |
| 1098 | return -1; |
| 1099 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1100 | LOG(DEBUG) << "Remounting " << uid << " as mode " << mode; |
| 1101 | |
| 1102 | DIR* dir; |
| 1103 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1104 | std::string rootName; |
| 1105 | std::string pidName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1106 | int pidFd; |
| 1107 | int nsFd; |
| 1108 | struct stat sb; |
| 1109 | pid_t child; |
| 1110 | |
| 1111 | if (!(dir = opendir("/proc"))) { |
| 1112 | PLOG(ERROR) << "Failed to opendir"; |
| 1113 | return -1; |
| 1114 | } |
| 1115 | |
| 1116 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1117 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
| 1118 | PLOG(ERROR) << "Failed to read root namespace"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1119 | closedir(dir); |
| 1120 | return -1; |
| 1121 | } |
| 1122 | |
| 1123 | // Poke through all running PIDs look for apps running as UID |
| 1124 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame] | 1125 | pid_t pid; |
| 1126 | if (de->d_type != DT_DIR) continue; |
| 1127 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 1128 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1129 | pidFd = -1; |
| 1130 | nsFd = -1; |
| 1131 | |
| 1132 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 1133 | if (pidFd < 0) { |
| 1134 | goto next; |
| 1135 | } |
| 1136 | if (fstat(pidFd, &sb) != 0) { |
| 1137 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 1138 | goto next; |
| 1139 | } |
| 1140 | if (sb.st_uid != uid) { |
| 1141 | goto next; |
| 1142 | } |
| 1143 | |
| 1144 | // Matches so far, but refuse to touch if in root namespace |
| 1145 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1146 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1147 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 1148 | goto next; |
| 1149 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1150 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1151 | LOG(WARNING) << "Skipping due to root namespace"; |
| 1152 | goto next; |
| 1153 | } |
| 1154 | |
| 1155 | // We purposefully leave the namespace open across the fork |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1156 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1157 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1158 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1159 | goto next; |
| 1160 | } |
| 1161 | |
| 1162 | if (!(child = fork())) { |
| 1163 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1164 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1165 | _exit(1); |
| 1166 | } |
| 1167 | |
Sudheer Shanka | 99d304a | 2018-09-27 14:53:51 -0700 | [diff] [blame] | 1168 | android::vold::UnmountTree("/storage/"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1169 | |
| 1170 | std::string storageSource; |
| 1171 | if (mode == "default") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 1172 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1173 | } else if (mode == "read") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 1174 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1175 | } else if (mode == "write") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 1176 | storageSource = "/mnt/runtime/write"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1177 | } else { |
| 1178 | // Sane default of no storage visible |
| 1179 | _exit(0); |
| 1180 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1181 | if (TEMP_FAILURE_RETRY( |
| 1182 | mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 1183 | PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1184 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1185 | } |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1186 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) { |
| 1187 | 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] | 1188 | _exit(1); |
| 1189 | } |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1190 | |
| 1191 | // Mount user-specific symlink helper into place |
| 1192 | userid_t user_id = multiuser_get_user_id(uid); |
| 1193 | std::string userSource(StringPrintf("/mnt/user/%d", user_id)); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1194 | if (TEMP_FAILURE_RETRY( |
| 1195 | mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) { |
| 1196 | PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 1197 | _exit(1); |
| 1198 | } |
| 1199 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1200 | _exit(0); |
| 1201 | } |
| 1202 | |
| 1203 | if (child == -1) { |
| 1204 | PLOG(ERROR) << "Failed to fork"; |
| 1205 | goto next; |
| 1206 | } else { |
| 1207 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 1208 | } |
| 1209 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1210 | next: |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 1211 | close(nsFd); |
| 1212 | close(pidFd); |
| 1213 | } |
| 1214 | closedir(dir); |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1218 | int VolumeManager::reset() { |
| 1219 | // Tear down all existing disks/volumes and start from a blank slate so |
| 1220 | // newly connected framework hears all events. |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 1221 | if (mInternalEmulated != nullptr) { |
| 1222 | mInternalEmulated->destroy(); |
| 1223 | mInternalEmulated->create(); |
| 1224 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1225 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1226 | disk->destroy(); |
| 1227 | disk->create(); |
| 1228 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 1229 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 1230 | mAddedUsers.clear(); |
Sudheer Shanka | 62bbb2b | 2018-08-01 01:09:10 -0700 | [diff] [blame] | 1231 | |
| 1232 | mUserPackages.clear(); |
| 1233 | mAppIds.clear(); |
| 1234 | mSandboxIds.clear(); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 1235 | mVisibleVolumeIds.clear(); |
Sudheer Shanka | 3a7ee5c | 2018-08-07 17:10:11 -0700 | [diff] [blame] | 1236 | |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 1237 | for (userid_t userId : mStartedUsers) { |
Sudheer Shanka | 30df1c6 | 2019-02-22 17:03:02 -0800 | [diff] [blame^] | 1238 | DeleteDirContents(StringPrintf("/mnt/user/%d/package", userId)); |
Sudheer Shanka | 023b539 | 2019-02-06 12:39:19 -0800 | [diff] [blame] | 1239 | } |
| 1240 | mStartedUsers.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1241 | return 0; |
| 1242 | } |
| 1243 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 1244 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1245 | int VolumeManager::shutdown() { |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 1246 | if (mInternalEmulated == nullptr) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1247 | return 0; // already shutdown |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 1248 | } |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 1249 | android::vold::sSleepOnUnmount = false; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1250 | mInternalEmulated->destroy(); |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 1251 | mInternalEmulated = nullptr; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1252 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1253 | disk->destroy(); |
| 1254 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1255 | mStubVolumes.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1256 | mDisks.clear(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 1257 | mPendingDisks.clear(); |
Paul Crowley | 56292ef | 2017-10-20 08:07:53 -0700 | [diff] [blame] | 1258 | android::vold::sSleepOnUnmount = true; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1259 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1262 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 1263 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 1264 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 1265 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1266 | // First, try gracefully unmounting all known devices |
| 1267 | if (mInternalEmulated != nullptr) { |
| 1268 | mInternalEmulated->unmount(); |
| 1269 | } |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1270 | for (const auto& stub : mStubVolumes) { |
| 1271 | stub->unmount(); |
| 1272 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1273 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1274 | disk->unmountAll(); |
| 1275 | } |
| 1276 | |
| 1277 | // Worst case we might have some stale mounts lurking around, so |
| 1278 | // force unmount those just to be safe. |
LongPing.WEI | 4f04606 | 2018-11-23 19:27:35 +0800 | [diff] [blame] | 1279 | FILE* fp = setmntent("/proc/mounts", "re"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1280 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1281 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1282 | return -errno; |
| 1283 | } |
| 1284 | |
| 1285 | // Some volumes can be stacked on each other, so force unmount in |
| 1286 | // reverse order to give us the best chance of success. |
| 1287 | std::list<std::string> toUnmount; |
| 1288 | mntent* mentry; |
| 1289 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1290 | auto test = std::string(mentry->mnt_dir); |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 1291 | if ((StartsWith(test, "/mnt/") && |
| 1292 | #ifdef __ANDROID_DEBUGGABLE__ |
| 1293 | !StartsWith(test, "/mnt/scratch") && |
| 1294 | #endif |
| 1295 | !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) || |
| 1296 | StartsWith(test, "/storage/")) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1297 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1298 | } |
| 1299 | } |
| 1300 | endmntent(fp); |
| 1301 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 1302 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1303 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1304 | android::vold::ForceUnmount(path); |
| 1305 | } |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1310 | int VolumeManager::mkdirs(const std::string& path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1311 | // Only offer to create directories for paths managed by vold |
Mark Salyzyn | 86e81e7 | 2018-09-20 10:09:27 -0700 | [diff] [blame] | 1312 | if (StartsWith(path, "/storage/")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1313 | // fs_mkdirs() does symlink checking and relative path enforcement |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1314 | return fs_mkdirs(path.c_str(), 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1315 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1316 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1317 | return -EINVAL; |
| 1318 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1319 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1320 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1321 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1322 | int32_t ownerGid, std::string* outVolId) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1323 | int id = mNextObbId++; |
| 1324 | |
| 1325 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 1326 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1327 | vol->create(); |
| 1328 | |
| 1329 | mObbVolumes.push_back(vol); |
| 1330 | *outVolId = vol->getId(); |
| 1331 | return android::OK; |
| 1332 | } |
| 1333 | |
| 1334 | int VolumeManager::destroyObb(const std::string& volId) { |
| 1335 | auto i = mObbVolumes.begin(); |
| 1336 | while (i != mObbVolumes.end()) { |
| 1337 | if ((*i)->getId() == volId) { |
| 1338 | (*i)->destroy(); |
| 1339 | i = mObbVolumes.erase(i); |
| 1340 | } else { |
| 1341 | ++i; |
| 1342 | } |
| 1343 | } |
| 1344 | return android::OK; |
| 1345 | } |
| 1346 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 1347 | int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 1348 | const std::string& fsType, const std::string& fsUuid, |
| 1349 | const std::string& fsLabel, std::string* outVolId) { |
| 1350 | int id = mNextStubVolumeId++; |
| 1351 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 1352 | new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel)); |
| 1353 | vol->create(); |
| 1354 | |
| 1355 | mStubVolumes.push_back(vol); |
| 1356 | *outVolId = vol->getId(); |
| 1357 | return android::OK; |
| 1358 | } |
| 1359 | |
| 1360 | int VolumeManager::destroyStubVolume(const std::string& volId) { |
| 1361 | auto i = mStubVolumes.begin(); |
| 1362 | while (i != mStubVolumes.end()) { |
| 1363 | if ((*i)->getId() == volId) { |
| 1364 | (*i)->destroy(); |
| 1365 | i = mStubVolumes.erase(i); |
| 1366 | } else { |
| 1367 | ++i; |
| 1368 | } |
| 1369 | } |
| 1370 | return android::OK; |
| 1371 | } |
| 1372 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1373 | int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1374 | return android::vold::MountAppFuse(uid, mountId, device_fd); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1375 | } |
| 1376 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1377 | int VolumeManager::unmountAppFuse(uid_t uid, int mountId) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1378 | return android::vold::UnmountAppFuse(uid, mountId); |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) { |
Risan | ac02a48 | 2018-10-31 21:59:47 -0600 | [diff] [blame] | 1382 | return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1383 | } |