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> |
| 29 | #include <sys/types.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 30 | #include <sys/sysmacros.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> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 33 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 34 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 36 | #include <android-base/logging.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame^] | 37 | #include <android-base/parseint.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 38 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame^] | 40 | #include <android-base/strings.h> |
| 41 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 42 | #include <cutils/fs.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 43 | #include <utils/Trace.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 44 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 45 | #include <selinux/android.h> |
| 46 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 47 | #include <sysutils/NetlinkEvent.h> |
| 48 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 49 | #include <private/android_filesystem_config.h> |
| 50 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 51 | #include "model/EmulatedVolume.h" |
| 52 | #include "model/ObbVolume.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | #include "VolumeManager.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 54 | #include "NetlinkManager.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 55 | #include "Loop.h" |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 56 | #include "fs/Ext4.h" |
| 57 | #include "fs/Vfat.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 58 | #include "Utils.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 59 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 60 | #include "Process.h" |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 61 | #include "VoldUtil.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 62 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 63 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 64 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 65 | using android::base::unique_fd; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 66 | |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 67 | bool VolumeManager::shutting_down = false; |
| 68 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 69 | static const char* kPathUserMount = "/mnt/user"; |
| 70 | static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk"; |
| 71 | |
| 72 | static const char* kPropVirtualDisk = "persist.sys.virtual_disk"; |
| 73 | |
| 74 | /* 512MiB is large enough for testing purposes */ |
| 75 | static const unsigned int kSizeVirtualDisk = 536870912; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 76 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 77 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 78 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 79 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 80 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 81 | VolumeManager *VolumeManager::sInstance = NULL; |
| 82 | |
| 83 | VolumeManager *VolumeManager::Instance() { |
| 84 | if (!sInstance) |
| 85 | sInstance = new VolumeManager(); |
| 86 | return sInstance; |
| 87 | } |
| 88 | |
| 89 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 90 | mDebug = false; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 91 | mNextObbId = 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | VolumeManager::~VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 97 | int VolumeManager::updateVirtualDisk() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 98 | ATRACE_NAME("VolumeManager::updateVirtualDisk"); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 99 | if (android::base::GetBoolProperty(kPropVirtualDisk, false)) { |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 100 | if (access(kPathVirtualDisk, F_OK) != 0) { |
| 101 | Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512); |
| 102 | } |
| 103 | |
| 104 | if (mVirtualDisk == nullptr) { |
| 105 | if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) { |
| 106 | LOG(ERROR) << "Failed to create virtual disk"; |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | struct stat buf; |
| 111 | if (stat(mVirtualDiskPath.c_str(), &buf) < 0) { |
| 112 | PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath; |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual", |
| 117 | android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd); |
| 118 | disk->create(); |
| 119 | mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk); |
| 120 | mDisks.push_back(mVirtualDisk); |
| 121 | } |
| 122 | } else { |
| 123 | if (mVirtualDisk != nullptr) { |
| 124 | dev_t device = mVirtualDisk->getDevice(); |
| 125 | |
| 126 | auto i = mDisks.begin(); |
| 127 | while (i != mDisks.end()) { |
| 128 | if ((*i)->getDevice() == device) { |
| 129 | (*i)->destroy(); |
| 130 | i = mDisks.erase(i); |
| 131 | } else { |
| 132 | ++i; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | Loop::destroyByDevice(mVirtualDiskPath.c_str()); |
| 137 | mVirtualDisk = nullptr; |
| 138 | } |
| 139 | |
| 140 | if (access(kPathVirtualDisk, F_OK) == 0) { |
| 141 | unlink(kPathVirtualDisk); |
| 142 | } |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 147 | int VolumeManager::setDebug(bool enable) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 148 | mDebug = enable; |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 149 | return 0; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 150 | } |
| 151 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 152 | int VolumeManager::start() { |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 153 | ATRACE_NAME("VolumeManager::start"); |
| 154 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 155 | // Always start from a clean slate by unmounting everything in |
| 156 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 157 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 158 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 159 | Devmapper::destroyAll(); |
| 160 | Loop::destroyAll(); |
| 161 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 162 | // Assume that we always have an emulated volume on internal |
| 163 | // storage; the framework will decide if it should be mounted. |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 164 | CHECK(mInternalEmulated == nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 165 | mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>( |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 166 | new android::vold::EmulatedVolume("/data/media")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 167 | mInternalEmulated->create(); |
| 168 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 169 | // Consider creating a virtual disk |
| 170 | updateVirtualDisk(); |
| 171 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int VolumeManager::stop() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 176 | CHECK(mInternalEmulated != nullptr); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 177 | mInternalEmulated->destroy(); |
| 178 | mInternalEmulated = nullptr; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 182 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 183 | std::lock_guard<std::mutex> lock(mLock); |
| 184 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 185 | if (mDebug) { |
| 186 | LOG(VERBOSE) << "----------------"; |
| 187 | LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction(); |
| 188 | evt->dump(); |
| 189 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 190 | |
Mateusz Nowak | 6440379 | 2015-08-03 16:39:19 +0200 | [diff] [blame] | 191 | std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):""); |
| 192 | std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):""); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 193 | |
| 194 | if (devType != "disk") return; |
| 195 | |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 196 | int major = std::stoi(evt->findParam("MAJOR")); |
| 197 | int minor = std::stoi(evt->findParam("MINOR")); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 198 | dev_t device = makedev(major, minor); |
| 199 | |
| 200 | switch (evt->getAction()) { |
| 201 | case NetlinkEvent::Action::kAdd: { |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 202 | for (const auto& source : mDiskSources) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 203 | if (source->matches(eventPath)) { |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 204 | // For now, assume that MMC and virtio-blk (the latter is |
| 205 | // emulator-specific; see Disk.cpp for details) devices are SD, |
| 206 | // and that everything else is USB |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 207 | int flags = source->getFlags(); |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 208 | if (major == kMajorBlockMmc |
| 209 | || (android::vold::IsRunningInEmulator() |
| 210 | && major >= (int) kMajorBlockExperimentalMin |
| 211 | && major <= (int) kMajorBlockExperimentalMax)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 212 | flags |= android::vold::Disk::Flags::kSd; |
| 213 | } else { |
| 214 | flags |= android::vold::Disk::Flags::kUsb; |
| 215 | } |
| 216 | |
| 217 | auto disk = new android::vold::Disk(eventPath, device, |
| 218 | source->getNickname(), flags); |
| 219 | disk->create(); |
| 220 | mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk)); |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | break; |
| 225 | } |
| 226 | case NetlinkEvent::Action::kChange: { |
Jeff Sharkey | 7d9d011 | 2015-04-14 23:14:23 -0700 | [diff] [blame] | 227 | LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed"; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 228 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 229 | if (disk->getDevice() == device) { |
| 230 | disk->readMetadata(); |
| 231 | disk->readPartitions(); |
| 232 | } |
| 233 | } |
| 234 | break; |
| 235 | } |
| 236 | case NetlinkEvent::Action::kRemove: { |
| 237 | auto i = mDisks.begin(); |
| 238 | while (i != mDisks.end()) { |
| 239 | if ((*i)->getDevice() == device) { |
| 240 | (*i)->destroy(); |
| 241 | i = mDisks.erase(i); |
| 242 | } else { |
| 243 | ++i; |
| 244 | } |
| 245 | } |
| 246 | break; |
| 247 | } |
| 248 | default: { |
| 249 | LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction(); |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
Wei Wang | 6b455c2 | 2017-01-20 11:52:33 -0800 | [diff] [blame] | 256 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 257 | mDiskSources.push_back(diskSource); |
| 258 | } |
| 259 | |
| 260 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 261 | for (auto disk : mDisks) { |
| 262 | if (disk->getId() == id) { |
| 263 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 266 | return nullptr; |
| 267 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 268 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 269 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 270 | // Vold could receive "mount" after "shutdown" command in the extreme case. |
| 271 | // If this happens, mInternalEmulated will equal nullptr and |
| 272 | // we need to deal with it in order to avoid null pointer crash. |
| 273 | if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 274 | return mInternalEmulated; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 275 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 276 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 277 | auto vol = disk->findVolume(id); |
| 278 | if (vol != nullptr) { |
| 279 | return vol; |
| 280 | } |
| 281 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 282 | for (const auto& vol : mObbVolumes) { |
| 283 | if (vol->getId() == id) { |
| 284 | return vol; |
| 285 | } |
| 286 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 287 | return nullptr; |
| 288 | } |
| 289 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 290 | void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, |
| 291 | std::list<std::string>& list) { |
| 292 | list.clear(); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 293 | for (const auto& disk : mDisks) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 294 | disk->listVolumes(type, list); |
| 295 | } |
| 296 | } |
| 297 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 298 | int VolumeManager::forgetPartition(const std::string& partGuid) { |
| 299 | std::string normalizedGuid; |
| 300 | if (android::vold::NormalizeHex(partGuid, normalizedGuid)) { |
| 301 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | std::string keyPath = android::vold::BuildKeyPath(normalizedGuid); |
| 306 | if (unlink(keyPath.c_str()) != 0) { |
| 307 | LOG(ERROR) << "Failed to unlink " << keyPath; |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 314 | int VolumeManager::linkPrimary(userid_t userId) { |
| 315 | std::string source(mPrimary->getPath()); |
| 316 | if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) { |
| 317 | source = StringPrintf("%s/%d", source.c_str(), userId); |
Jeff Sharkey | 32679a8 | 2015-07-21 14:22:01 -0700 | [diff] [blame] | 318 | fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 322 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 323 | if (errno != ENOENT) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 324 | PLOG(WARNING) << "Failed to unlink " << target; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
Jeff Sharkey | 1bfb375 | 2015-04-29 15:22:23 -0700 | [diff] [blame] | 327 | LOG(DEBUG) << "Linking " << source << " to " << target; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 328 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 329 | PLOG(WARNING) << "Failed to link"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 330 | return -errno; |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 335 | int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) { |
| 336 | mAddedUsers[userId] = userSerialNumber; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | int VolumeManager::onUserRemoved(userid_t userId) { |
| 341 | mAddedUsers.erase(userId); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | int VolumeManager::onUserStarted(userid_t userId) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 346 | // Note that sometimes the system will spin up processes from Zygote |
| 347 | // before actually starting the user, so we're okay if Zygote |
| 348 | // already created this directory. |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 349 | std::string path(StringPrintf("%s/%d", kPathUserMount, userId)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 350 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 351 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 352 | mStartedUsers.insert(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 353 | if (mPrimary) { |
| 354 | linkPrimary(userId); |
| 355 | } |
| 356 | return 0; |
| 357 | } |
| 358 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 359 | int VolumeManager::onUserStopped(userid_t userId) { |
| 360 | mStartedUsers.erase(userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 365 | mPrimary = vol; |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 366 | for (userid_t userId : mStartedUsers) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 367 | linkPrimary(userId); |
| 368 | } |
| 369 | return 0; |
| 370 | } |
| 371 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 372 | static int unmount_tree(const std::string& prefix) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 373 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 374 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 375 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 376 | return -errno; |
| 377 | } |
| 378 | |
| 379 | // Some volumes can be stacked on each other, so force unmount in |
| 380 | // reverse order to give us the best chance of success. |
| 381 | std::list<std::string> toUnmount; |
| 382 | mntent* mentry; |
| 383 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 384 | auto test = std::string(mentry->mnt_dir) + "/"; |
| 385 | if (android::base::StartsWith(test, prefix.c_str())) { |
| 386 | toUnmount.push_front(test); |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | endmntent(fp); |
| 390 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 391 | for (const auto& path : toUnmount) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 392 | if (umount2(path.c_str(), MNT_DETACH)) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 393 | PLOG(ERROR) << "Failed to unmount " << path; |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | return 0; |
| 397 | } |
| 398 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 399 | int VolumeManager::remountUid(uid_t uid, const std::string& mode) { |
| 400 | LOG(DEBUG) << "Remounting " << uid << " as mode " << mode; |
| 401 | |
| 402 | DIR* dir; |
| 403 | struct dirent* de; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 404 | std::string rootName; |
| 405 | std::string pidName; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 406 | int pidFd; |
| 407 | int nsFd; |
| 408 | struct stat sb; |
| 409 | pid_t child; |
| 410 | |
| 411 | if (!(dir = opendir("/proc"))) { |
| 412 | PLOG(ERROR) << "Failed to opendir"; |
| 413 | return -1; |
| 414 | } |
| 415 | |
| 416 | // Figure out root namespace to compare against below |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 417 | if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) { |
| 418 | PLOG(ERROR) << "Failed to read root namespace"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 419 | closedir(dir); |
| 420 | return -1; |
| 421 | } |
| 422 | |
| 423 | // Poke through all running PIDs look for apps running as UID |
| 424 | while ((de = readdir(dir))) { |
Jeff Vander Stoep | 5889083 | 2017-10-23 17:12:31 -0700 | [diff] [blame^] | 425 | pid_t pid; |
| 426 | if (de->d_type != DT_DIR) continue; |
| 427 | if (!android::base::ParseInt(de->d_name, &pid)) continue; |
| 428 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 429 | pidFd = -1; |
| 430 | nsFd = -1; |
| 431 | |
| 432 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 433 | if (pidFd < 0) { |
| 434 | goto next; |
| 435 | } |
| 436 | if (fstat(pidFd, &sb) != 0) { |
| 437 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 438 | goto next; |
| 439 | } |
| 440 | if (sb.st_uid != uid) { |
| 441 | goto next; |
| 442 | } |
| 443 | |
| 444 | // Matches so far, but refuse to touch if in root namespace |
| 445 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 446 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 447 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 448 | goto next; |
| 449 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 450 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 451 | LOG(WARNING) << "Skipping due to root namespace"; |
| 452 | goto next; |
| 453 | } |
| 454 | |
| 455 | // We purposefully leave the namespace open across the fork |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 456 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 457 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 458 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 459 | goto next; |
| 460 | } |
| 461 | |
| 462 | if (!(child = fork())) { |
| 463 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 464 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 465 | _exit(1); |
| 466 | } |
| 467 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 468 | unmount_tree("/storage/"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 469 | |
| 470 | std::string storageSource; |
| 471 | if (mode == "default") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 472 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 473 | } else if (mode == "read") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 474 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 475 | } else if (mode == "write") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 476 | storageSource = "/mnt/runtime/write"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 477 | } else { |
| 478 | // Sane default of no storage visible |
| 479 | _exit(0); |
| 480 | } |
| 481 | if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage", |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 482 | NULL, MS_BIND | MS_REC, NULL)) == -1) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 483 | PLOG(ERROR) << "Failed to mount " << storageSource << " for " |
| 484 | << de->d_name; |
| 485 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 486 | } |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 487 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, |
| 488 | MS_REC | MS_SLAVE, NULL)) == -1) { |
| 489 | PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " |
| 490 | << de->d_name; |
| 491 | _exit(1); |
| 492 | } |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 493 | |
| 494 | // Mount user-specific symlink helper into place |
| 495 | userid_t user_id = multiuser_get_user_id(uid); |
| 496 | std::string userSource(StringPrintf("/mnt/user/%d", user_id)); |
| 497 | if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self", |
| 498 | NULL, MS_BIND, NULL)) == -1) { |
| 499 | PLOG(ERROR) << "Failed to mount " << userSource << " for " |
| 500 | << de->d_name; |
| 501 | _exit(1); |
| 502 | } |
| 503 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 504 | _exit(0); |
| 505 | } |
| 506 | |
| 507 | if (child == -1) { |
| 508 | PLOG(ERROR) << "Failed to fork"; |
| 509 | goto next; |
| 510 | } else { |
| 511 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 512 | } |
| 513 | |
| 514 | next: |
| 515 | close(nsFd); |
| 516 | close(pidFd); |
| 517 | } |
| 518 | closedir(dir); |
| 519 | return 0; |
| 520 | } |
| 521 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 522 | int VolumeManager::reset() { |
| 523 | // Tear down all existing disks/volumes and start from a blank slate so |
| 524 | // newly connected framework hears all events. |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 525 | if (mInternalEmulated != nullptr) { |
| 526 | mInternalEmulated->destroy(); |
| 527 | mInternalEmulated->create(); |
| 528 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 529 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 530 | disk->destroy(); |
| 531 | disk->create(); |
| 532 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 533 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 534 | mAddedUsers.clear(); |
| 535 | mStartedUsers.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 539 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 540 | int VolumeManager::shutdown() { |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 541 | if (mInternalEmulated == nullptr) { |
| 542 | return 0; // already shutdown |
| 543 | } |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 544 | shutting_down = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 545 | mInternalEmulated->destroy(); |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 546 | mInternalEmulated = nullptr; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 547 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 548 | disk->destroy(); |
| 549 | } |
| 550 | mDisks.clear(); |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 551 | shutting_down = false; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 552 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 555 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 556 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 557 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 558 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 559 | // First, try gracefully unmounting all known devices |
| 560 | if (mInternalEmulated != nullptr) { |
| 561 | mInternalEmulated->unmount(); |
| 562 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 563 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 564 | disk->unmountAll(); |
| 565 | } |
| 566 | |
| 567 | // Worst case we might have some stale mounts lurking around, so |
| 568 | // force unmount those just to be safe. |
| 569 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 570 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 571 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 572 | return -errno; |
| 573 | } |
| 574 | |
| 575 | // Some volumes can be stacked on each other, so force unmount in |
| 576 | // reverse order to give us the best chance of success. |
| 577 | std::list<std::string> toUnmount; |
| 578 | mntent* mentry; |
| 579 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 580 | auto test = std::string(mentry->mnt_dir); |
| 581 | if (android::base::StartsWith(test, "/mnt/") |
| 582 | || android::base::StartsWith(test, "/storage/")) { |
| 583 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | endmntent(fp); |
| 587 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 588 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 589 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 590 | android::vold::ForceUnmount(path); |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 596 | extern "C" int vold_unmountAll(void) { |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 597 | VolumeManager *vm = VolumeManager::Instance(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 598 | return vm->unmountAll(); |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 601 | int VolumeManager::mkdirs(const std::string& path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 602 | // Only offer to create directories for paths managed by vold |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 603 | if (android::base::StartsWith(path, "/storage/")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 604 | // fs_mkdirs() does symlink checking and relative path enforcement |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 605 | return fs_mkdirs(path.c_str(), 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 606 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 607 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 608 | return -EINVAL; |
| 609 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 610 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 611 | |
| 612 | static size_t kAppFuseMaxMountPointName = 32; |
| 613 | |
| 614 | static android::status_t getMountPath(uid_t uid, const std::string& name, std::string* path) { |
| 615 | if (name.size() > kAppFuseMaxMountPointName) { |
| 616 | LOG(ERROR) << "AppFuse mount name is too long."; |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | for (size_t i = 0; i < name.size(); i++) { |
| 620 | if (!isalnum(name[i])) { |
| 621 | LOG(ERROR) << "AppFuse mount name contains invalid character."; |
| 622 | return -EINVAL; |
| 623 | } |
| 624 | } |
| 625 | *path = android::base::StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str()); |
| 626 | return android::OK; |
| 627 | } |
| 628 | |
| 629 | static android::status_t mountInNamespace(uid_t uid, int device_fd, const std::string& path) { |
| 630 | // Remove existing mount. |
| 631 | android::vold::ForceUnmount(path); |
| 632 | |
| 633 | const auto opts = android::base::StringPrintf( |
| 634 | "fd=%i," |
| 635 | "rootmode=40000," |
| 636 | "default_permissions," |
| 637 | "allow_other," |
| 638 | "user_id=%d,group_id=%d," |
| 639 | "context=\"u:object_r:app_fuse_file:s0\"," |
| 640 | "fscontext=u:object_r:app_fusefs:s0", |
| 641 | device_fd, |
| 642 | uid, |
| 643 | uid); |
| 644 | |
| 645 | const int result = TEMP_FAILURE_RETRY(mount( |
| 646 | "/dev/fuse", path.c_str(), "fuse", |
| 647 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str())); |
| 648 | if (result != 0) { |
| 649 | PLOG(ERROR) << "Failed to mount " << path; |
| 650 | return -errno; |
| 651 | } |
| 652 | |
| 653 | return android::OK; |
| 654 | } |
| 655 | |
| 656 | static android::status_t runCommandInNamespace(const std::string& command, |
| 657 | uid_t uid, |
| 658 | pid_t pid, |
| 659 | const std::string& path, |
| 660 | int device_fd) { |
| 661 | if (DEBUG_APPFUSE) { |
| 662 | LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path |
| 663 | << " in namespace " << uid; |
| 664 | } |
| 665 | |
| 666 | unique_fd dir(open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 667 | if (dir.get() == -1) { |
| 668 | PLOG(ERROR) << "Failed to open /proc"; |
| 669 | return -errno; |
| 670 | } |
| 671 | |
| 672 | // Obtains process file descriptor. |
| 673 | const std::string pid_str = android::base::StringPrintf("%d", pid); |
| 674 | const unique_fd pid_fd( |
| 675 | openat(dir.get(), pid_str.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 676 | if (pid_fd.get() == -1) { |
| 677 | PLOG(ERROR) << "Failed to open /proc/" << pid; |
| 678 | return -errno; |
| 679 | } |
| 680 | |
| 681 | // Check UID of process. |
| 682 | { |
| 683 | struct stat sb; |
| 684 | const int result = fstat(pid_fd.get(), &sb); |
| 685 | if (result == -1) { |
| 686 | PLOG(ERROR) << "Failed to stat /proc/" << pid; |
| 687 | return -errno; |
| 688 | } |
| 689 | if (sb.st_uid != AID_SYSTEM) { |
| 690 | LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM |
| 691 | << ", actual=" << sb.st_uid; |
| 692 | return -EPERM; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | // Matches so far, but refuse to touch if in root namespace |
| 697 | { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 698 | std::string rootName; |
| 699 | std::string pidName; |
| 700 | if (!android::vold::Readlinkat(dir.get(), "1/ns/mnt", &rootName) |
| 701 | || !android::vold::Readlinkat(pid_fd.get(), "ns/mnt", &pidName)) { |
| 702 | PLOG(ERROR) << "Failed to read namespaces"; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 703 | return -EPERM; |
| 704 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 705 | if (rootName == pidName) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 706 | LOG(ERROR) << "Don't mount appfuse in root namespace"; |
| 707 | return -EPERM; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | // We purposefully leave the namespace open across the fork |
| 712 | unique_fd ns_fd(openat(pid_fd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC |
| 713 | if (ns_fd.get() < 0) { |
| 714 | PLOG(ERROR) << "Failed to open namespace for /proc/" << pid << "/ns/mnt"; |
| 715 | return -errno; |
| 716 | } |
| 717 | |
| 718 | int child = fork(); |
| 719 | if (child == 0) { |
| 720 | if (setns(ns_fd.get(), CLONE_NEWNS) != 0) { |
| 721 | PLOG(ERROR) << "Failed to setns"; |
| 722 | _exit(-errno); |
| 723 | } |
| 724 | |
| 725 | if (command == "mount") { |
| 726 | _exit(mountInNamespace(uid, device_fd, path)); |
| 727 | } else if (command == "unmount") { |
| 728 | // If it's just after all FD opened on mount point are closed, umount2 can fail with |
| 729 | // EBUSY. To avoid the case, specify MNT_DETACH. |
| 730 | if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && |
| 731 | errno != EINVAL && errno != ENOENT) { |
| 732 | PLOG(ERROR) << "Failed to unmount directory."; |
| 733 | _exit(-errno); |
| 734 | } |
| 735 | if (rmdir(path.c_str()) != 0) { |
| 736 | PLOG(ERROR) << "Failed to remove the mount directory."; |
| 737 | _exit(-errno); |
| 738 | } |
| 739 | _exit(android::OK); |
| 740 | } else { |
| 741 | LOG(ERROR) << "Unknown appfuse command " << command; |
| 742 | _exit(-EPERM); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (child == -1) { |
| 747 | PLOG(ERROR) << "Failed to folk child process"; |
| 748 | return -errno; |
| 749 | } |
| 750 | |
| 751 | android::status_t status; |
| 752 | TEMP_FAILURE_RETRY(waitpid(child, &status, 0)); |
| 753 | |
| 754 | return status; |
| 755 | } |
| 756 | |
| 757 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
| 758 | int32_t ownerGid, std::string* outVolId) { |
| 759 | int id = mNextObbId++; |
| 760 | |
| 761 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 762 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
| 763 | vol->create(); |
| 764 | |
| 765 | mObbVolumes.push_back(vol); |
| 766 | *outVolId = vol->getId(); |
| 767 | return android::OK; |
| 768 | } |
| 769 | |
| 770 | int VolumeManager::destroyObb(const std::string& volId) { |
| 771 | auto i = mObbVolumes.begin(); |
| 772 | while (i != mObbVolumes.end()) { |
| 773 | if ((*i)->getId() == volId) { |
| 774 | (*i)->destroy(); |
| 775 | i = mObbVolumes.erase(i); |
| 776 | } else { |
| 777 | ++i; |
| 778 | } |
| 779 | } |
| 780 | return android::OK; |
| 781 | } |
| 782 | |
| 783 | int VolumeManager::mountAppFuse(uid_t uid, pid_t pid, int mountId, |
| 784 | android::base::unique_fd* device_fd) { |
| 785 | std::string name = std::to_string(mountId); |
| 786 | |
| 787 | // Check mount point name. |
| 788 | std::string path; |
| 789 | if (getMountPath(uid, name, &path) != android::OK) { |
| 790 | LOG(ERROR) << "Invalid mount point name"; |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | // Create directories. |
| 795 | const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0); |
| 796 | if (result != android::OK) { |
| 797 | PLOG(ERROR) << "Failed to prepare directory " << path; |
| 798 | return -1; |
| 799 | } |
| 800 | |
| 801 | // Open device FD. |
| 802 | device_fd->reset(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC |
| 803 | if (device_fd->get() == -1) { |
| 804 | PLOG(ERROR) << "Failed to open /dev/fuse"; |
| 805 | return -1; |
| 806 | } |
| 807 | |
| 808 | // Mount. |
| 809 | return runCommandInNamespace("mount", uid, pid, path, device_fd->get()); |
| 810 | } |
| 811 | |
| 812 | int VolumeManager::unmountAppFuse(uid_t uid, pid_t pid, int mountId) { |
| 813 | std::string name = std::to_string(mountId); |
| 814 | |
| 815 | // Check mount point name. |
| 816 | std::string path; |
| 817 | if (getMountPath(uid, name, &path) != android::OK) { |
| 818 | LOG(ERROR) << "Invalid mount point name"; |
| 819 | return -1; |
| 820 | } |
| 821 | |
| 822 | return runCommandInNamespace("unmount", uid, pid, path, -1 /* device_fd */); |
| 823 | } |