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 Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 37 | #include <android-base/properties.h> |
| 38 | #include <android-base/strings.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 40 | #include <cutils/fs.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 41 | #include <utils/Trace.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 42 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 43 | #include <selinux/android.h> |
| 44 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 45 | #include <sysutils/NetlinkEvent.h> |
| 46 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 47 | #include <private/android_filesystem_config.h> |
| 48 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 49 | #include "model/EmulatedVolume.h" |
| 50 | #include "model/ObbVolume.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 51 | #include "VolumeManager.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 52 | #include "NetlinkManager.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 53 | #include "Loop.h" |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 54 | #include "fs/Ext4.h" |
| 55 | #include "fs/Vfat.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 56 | #include "Utils.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 57 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 58 | #include "Process.h" |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 59 | #include "VoldUtil.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 60 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 61 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 62 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 63 | using android::base::unique_fd; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 64 | |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 65 | bool VolumeManager::shutting_down = false; |
| 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 | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 296 | int VolumeManager::forgetPartition(const std::string& partGuid) { |
| 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))) { |
| 423 | pidFd = -1; |
| 424 | nsFd = -1; |
| 425 | |
| 426 | pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 427 | if (pidFd < 0) { |
| 428 | goto next; |
| 429 | } |
| 430 | if (fstat(pidFd, &sb) != 0) { |
| 431 | PLOG(WARNING) << "Failed to stat " << de->d_name; |
| 432 | goto next; |
| 433 | } |
| 434 | if (sb.st_uid != uid) { |
| 435 | goto next; |
| 436 | } |
| 437 | |
| 438 | // Matches so far, but refuse to touch if in root namespace |
| 439 | LOG(DEBUG) << "Found matching PID " << de->d_name; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 440 | if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 441 | PLOG(WARNING) << "Failed to read namespace for " << de->d_name; |
| 442 | goto next; |
| 443 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 444 | if (rootName == pidName) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 445 | LOG(WARNING) << "Skipping due to root namespace"; |
| 446 | goto next; |
| 447 | } |
| 448 | |
| 449 | // We purposefully leave the namespace open across the fork |
Jeff Sharkey | fd3dc3c | 2017-03-27 10:49:21 -0600 | [diff] [blame] | 450 | nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 451 | if (nsFd < 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 452 | PLOG(WARNING) << "Failed to open namespace for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 453 | goto next; |
| 454 | } |
| 455 | |
| 456 | if (!(child = fork())) { |
| 457 | if (setns(nsFd, CLONE_NEWNS) != 0) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 458 | PLOG(ERROR) << "Failed to setns for " << de->d_name; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 459 | _exit(1); |
| 460 | } |
| 461 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 462 | unmount_tree("/storage/"); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 463 | |
| 464 | std::string storageSource; |
| 465 | if (mode == "default") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 466 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 467 | } else if (mode == "read") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 468 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 469 | } else if (mode == "write") { |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 470 | storageSource = "/mnt/runtime/write"; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 471 | } else { |
| 472 | // Sane default of no storage visible |
| 473 | _exit(0); |
| 474 | } |
| 475 | if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage", |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 476 | NULL, MS_BIND | MS_REC, NULL)) == -1) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 477 | PLOG(ERROR) << "Failed to mount " << storageSource << " for " |
| 478 | << de->d_name; |
| 479 | _exit(1); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 480 | } |
Hidehiko Abe | 674bed1 | 2016-03-09 16:42:10 +0900 | [diff] [blame] | 481 | if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, |
| 482 | MS_REC | MS_SLAVE, NULL)) == -1) { |
| 483 | PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " |
| 484 | << de->d_name; |
| 485 | _exit(1); |
| 486 | } |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 487 | |
| 488 | // Mount user-specific symlink helper into place |
| 489 | userid_t user_id = multiuser_get_user_id(uid); |
| 490 | std::string userSource(StringPrintf("/mnt/user/%d", user_id)); |
| 491 | if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self", |
| 492 | NULL, MS_BIND, NULL)) == -1) { |
| 493 | PLOG(ERROR) << "Failed to mount " << userSource << " for " |
| 494 | << de->d_name; |
| 495 | _exit(1); |
| 496 | } |
| 497 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 498 | _exit(0); |
| 499 | } |
| 500 | |
| 501 | if (child == -1) { |
| 502 | PLOG(ERROR) << "Failed to fork"; |
| 503 | goto next; |
| 504 | } else { |
| 505 | TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0)); |
| 506 | } |
| 507 | |
| 508 | next: |
| 509 | close(nsFd); |
| 510 | close(pidFd); |
| 511 | } |
| 512 | closedir(dir); |
| 513 | return 0; |
| 514 | } |
| 515 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 516 | int VolumeManager::reset() { |
| 517 | // Tear down all existing disks/volumes and start from a blank slate so |
| 518 | // newly connected framework hears all events. |
Gao Xiang | d263da8 | 2017-08-14 11:32:13 +0800 | [diff] [blame] | 519 | if (mInternalEmulated != nullptr) { |
| 520 | mInternalEmulated->destroy(); |
| 521 | mInternalEmulated->create(); |
| 522 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 523 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 524 | disk->destroy(); |
| 525 | disk->create(); |
| 526 | } |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 527 | updateVirtualDisk(); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 528 | mAddedUsers.clear(); |
| 529 | mStartedUsers.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 533 | // Can be called twice (sequentially) during shutdown. should be safe for that. |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 534 | int VolumeManager::shutdown() { |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 535 | if (mInternalEmulated == nullptr) { |
| 536 | return 0; // already shutdown |
| 537 | } |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 538 | shutting_down = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 539 | mInternalEmulated->destroy(); |
Keun-young Park | a5bbb5e | 2017-03-13 18:02:50 -0700 | [diff] [blame] | 540 | mInternalEmulated = nullptr; |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 541 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 542 | disk->destroy(); |
| 543 | } |
| 544 | mDisks.clear(); |
Keun-young Park | 375ac25 | 2017-08-02 17:45:48 -0700 | [diff] [blame] | 545 | shutting_down = false; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 546 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 549 | int VolumeManager::unmountAll() { |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 550 | std::lock_guard<std::mutex> lock(mLock); |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 551 | ATRACE_NAME("VolumeManager::unmountAll()"); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 552 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 553 | // First, try gracefully unmounting all known devices |
| 554 | if (mInternalEmulated != nullptr) { |
| 555 | mInternalEmulated->unmount(); |
| 556 | } |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 557 | for (const auto& disk : mDisks) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 558 | disk->unmountAll(); |
| 559 | } |
| 560 | |
| 561 | // Worst case we might have some stale mounts lurking around, so |
| 562 | // force unmount those just to be safe. |
| 563 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 564 | if (fp == NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 565 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 566 | return -errno; |
| 567 | } |
| 568 | |
| 569 | // Some volumes can be stacked on each other, so force unmount in |
| 570 | // reverse order to give us the best chance of success. |
| 571 | std::list<std::string> toUnmount; |
| 572 | mntent* mentry; |
| 573 | while ((mentry = getmntent(fp)) != NULL) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 574 | auto test = std::string(mentry->mnt_dir); |
| 575 | if (android::base::StartsWith(test, "/mnt/") |
| 576 | || android::base::StartsWith(test, "/storage/")) { |
| 577 | toUnmount.push_front(test); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | endmntent(fp); |
| 581 | |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 582 | for (const auto& path : toUnmount) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 583 | LOG(DEBUG) << "Tearing down stale mount " << path; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 584 | android::vold::ForceUnmount(path); |
| 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 590 | extern "C" int vold_unmountAll(void) { |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 591 | VolumeManager *vm = VolumeManager::Instance(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 592 | return vm->unmountAll(); |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 595 | int VolumeManager::mkdirs(const std::string& path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 596 | // Only offer to create directories for paths managed by vold |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 597 | if (android::base::StartsWith(path, "/storage/")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 598 | // fs_mkdirs() does symlink checking and relative path enforcement |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 599 | return fs_mkdirs(path.c_str(), 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 600 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 601 | LOG(ERROR) << "Failed to find mounted volume for " << path; |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 602 | return -EINVAL; |
| 603 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 604 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 605 | |
| 606 | static size_t kAppFuseMaxMountPointName = 32; |
| 607 | |
| 608 | static android::status_t getMountPath(uid_t uid, const std::string& name, std::string* path) { |
| 609 | if (name.size() > kAppFuseMaxMountPointName) { |
| 610 | LOG(ERROR) << "AppFuse mount name is too long."; |
| 611 | return -EINVAL; |
| 612 | } |
| 613 | for (size_t i = 0; i < name.size(); i++) { |
| 614 | if (!isalnum(name[i])) { |
| 615 | LOG(ERROR) << "AppFuse mount name contains invalid character."; |
| 616 | return -EINVAL; |
| 617 | } |
| 618 | } |
| 619 | *path = android::base::StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str()); |
| 620 | return android::OK; |
| 621 | } |
| 622 | |
| 623 | static android::status_t mountInNamespace(uid_t uid, int device_fd, const std::string& path) { |
| 624 | // Remove existing mount. |
| 625 | android::vold::ForceUnmount(path); |
| 626 | |
| 627 | const auto opts = android::base::StringPrintf( |
| 628 | "fd=%i," |
| 629 | "rootmode=40000," |
| 630 | "default_permissions," |
| 631 | "allow_other," |
| 632 | "user_id=%d,group_id=%d," |
| 633 | "context=\"u:object_r:app_fuse_file:s0\"," |
| 634 | "fscontext=u:object_r:app_fusefs:s0", |
| 635 | device_fd, |
| 636 | uid, |
| 637 | uid); |
| 638 | |
| 639 | const int result = TEMP_FAILURE_RETRY(mount( |
| 640 | "/dev/fuse", path.c_str(), "fuse", |
| 641 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str())); |
| 642 | if (result != 0) { |
| 643 | PLOG(ERROR) << "Failed to mount " << path; |
| 644 | return -errno; |
| 645 | } |
| 646 | |
| 647 | return android::OK; |
| 648 | } |
| 649 | |
| 650 | static android::status_t runCommandInNamespace(const std::string& command, |
| 651 | uid_t uid, |
| 652 | pid_t pid, |
| 653 | const std::string& path, |
| 654 | int device_fd) { |
| 655 | if (DEBUG_APPFUSE) { |
| 656 | LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path |
| 657 | << " in namespace " << uid; |
| 658 | } |
| 659 | |
| 660 | unique_fd dir(open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 661 | if (dir.get() == -1) { |
| 662 | PLOG(ERROR) << "Failed to open /proc"; |
| 663 | return -errno; |
| 664 | } |
| 665 | |
| 666 | // Obtains process file descriptor. |
| 667 | const std::string pid_str = android::base::StringPrintf("%d", pid); |
| 668 | const unique_fd pid_fd( |
| 669 | openat(dir.get(), pid_str.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 670 | if (pid_fd.get() == -1) { |
| 671 | PLOG(ERROR) << "Failed to open /proc/" << pid; |
| 672 | return -errno; |
| 673 | } |
| 674 | |
| 675 | // Check UID of process. |
| 676 | { |
| 677 | struct stat sb; |
| 678 | const int result = fstat(pid_fd.get(), &sb); |
| 679 | if (result == -1) { |
| 680 | PLOG(ERROR) << "Failed to stat /proc/" << pid; |
| 681 | return -errno; |
| 682 | } |
| 683 | if (sb.st_uid != AID_SYSTEM) { |
| 684 | LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM |
| 685 | << ", actual=" << sb.st_uid; |
| 686 | return -EPERM; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // Matches so far, but refuse to touch if in root namespace |
| 691 | { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 692 | std::string rootName; |
| 693 | std::string pidName; |
| 694 | if (!android::vold::Readlinkat(dir.get(), "1/ns/mnt", &rootName) |
| 695 | || !android::vold::Readlinkat(pid_fd.get(), "ns/mnt", &pidName)) { |
| 696 | PLOG(ERROR) << "Failed to read namespaces"; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 697 | return -EPERM; |
| 698 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame^] | 699 | if (rootName == pidName) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 700 | LOG(ERROR) << "Don't mount appfuse in root namespace"; |
| 701 | return -EPERM; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // We purposefully leave the namespace open across the fork |
| 706 | unique_fd ns_fd(openat(pid_fd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC |
| 707 | if (ns_fd.get() < 0) { |
| 708 | PLOG(ERROR) << "Failed to open namespace for /proc/" << pid << "/ns/mnt"; |
| 709 | return -errno; |
| 710 | } |
| 711 | |
| 712 | int child = fork(); |
| 713 | if (child == 0) { |
| 714 | if (setns(ns_fd.get(), CLONE_NEWNS) != 0) { |
| 715 | PLOG(ERROR) << "Failed to setns"; |
| 716 | _exit(-errno); |
| 717 | } |
| 718 | |
| 719 | if (command == "mount") { |
| 720 | _exit(mountInNamespace(uid, device_fd, path)); |
| 721 | } else if (command == "unmount") { |
| 722 | // If it's just after all FD opened on mount point are closed, umount2 can fail with |
| 723 | // EBUSY. To avoid the case, specify MNT_DETACH. |
| 724 | if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && |
| 725 | errno != EINVAL && errno != ENOENT) { |
| 726 | PLOG(ERROR) << "Failed to unmount directory."; |
| 727 | _exit(-errno); |
| 728 | } |
| 729 | if (rmdir(path.c_str()) != 0) { |
| 730 | PLOG(ERROR) << "Failed to remove the mount directory."; |
| 731 | _exit(-errno); |
| 732 | } |
| 733 | _exit(android::OK); |
| 734 | } else { |
| 735 | LOG(ERROR) << "Unknown appfuse command " << command; |
| 736 | _exit(-EPERM); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | if (child == -1) { |
| 741 | PLOG(ERROR) << "Failed to folk child process"; |
| 742 | return -errno; |
| 743 | } |
| 744 | |
| 745 | android::status_t status; |
| 746 | TEMP_FAILURE_RETRY(waitpid(child, &status, 0)); |
| 747 | |
| 748 | return status; |
| 749 | } |
| 750 | |
| 751 | int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey, |
| 752 | int32_t ownerGid, std::string* outVolId) { |
| 753 | int id = mNextObbId++; |
| 754 | |
| 755 | auto vol = std::shared_ptr<android::vold::VolumeBase>( |
| 756 | new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid)); |
| 757 | vol->create(); |
| 758 | |
| 759 | mObbVolumes.push_back(vol); |
| 760 | *outVolId = vol->getId(); |
| 761 | return android::OK; |
| 762 | } |
| 763 | |
| 764 | int VolumeManager::destroyObb(const std::string& volId) { |
| 765 | auto i = mObbVolumes.begin(); |
| 766 | while (i != mObbVolumes.end()) { |
| 767 | if ((*i)->getId() == volId) { |
| 768 | (*i)->destroy(); |
| 769 | i = mObbVolumes.erase(i); |
| 770 | } else { |
| 771 | ++i; |
| 772 | } |
| 773 | } |
| 774 | return android::OK; |
| 775 | } |
| 776 | |
| 777 | int VolumeManager::mountAppFuse(uid_t uid, pid_t pid, int mountId, |
| 778 | android::base::unique_fd* device_fd) { |
| 779 | std::string name = std::to_string(mountId); |
| 780 | |
| 781 | // Check mount point name. |
| 782 | std::string path; |
| 783 | if (getMountPath(uid, name, &path) != android::OK) { |
| 784 | LOG(ERROR) << "Invalid mount point name"; |
| 785 | return -1; |
| 786 | } |
| 787 | |
| 788 | // Create directories. |
| 789 | const android::status_t result = android::vold::PrepareDir(path, 0700, 0, 0); |
| 790 | if (result != android::OK) { |
| 791 | PLOG(ERROR) << "Failed to prepare directory " << path; |
| 792 | return -1; |
| 793 | } |
| 794 | |
| 795 | // Open device FD. |
| 796 | device_fd->reset(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC |
| 797 | if (device_fd->get() == -1) { |
| 798 | PLOG(ERROR) << "Failed to open /dev/fuse"; |
| 799 | return -1; |
| 800 | } |
| 801 | |
| 802 | // Mount. |
| 803 | return runCommandInNamespace("mount", uid, pid, path, device_fd->get()); |
| 804 | } |
| 805 | |
| 806 | int VolumeManager::unmountAppFuse(uid_t uid, pid_t pid, int mountId) { |
| 807 | std::string name = std::to_string(mountId); |
| 808 | |
| 809 | // Check mount point name. |
| 810 | std::string path; |
| 811 | if (getMountPath(uid, name, &path) != android::OK) { |
| 812 | LOG(ERROR) << "Invalid mount point name"; |
| 813 | return -1; |
| 814 | } |
| 815 | |
| 816 | return runCommandInNamespace("unmount", uid, pid, path, -1 /* device_fd */); |
| 817 | } |