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