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