blob: db47e4dc882d92b03d3a5d9a499a260f348d3022 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Yabin Cuid1104f72015-01-02 13:28:28 -080019#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080021#include <fcntl.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080022#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 Mehata19b2502010-01-06 10:33:53 -080028#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070029#include <sys/sysmacros.h>
Paul Crowley8915d622018-09-18 15:14:18 -070030#include <sys/types.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070031#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080032#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080033
San Mehata2677e42009-12-13 10:40:18 -080034#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070035
Elliott Hughes7e128fb2015-12-04 15:50:53 -080036#include <android-base/logging.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070037#include <android-base/parseint.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060038#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080039#include <android-base/stringprintf.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070040#include <android-base/strings.h>
41
Jeff Sharkey71ebe152013-09-17 17:24:38 -070042#include <cutils/fs.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060043#include <utils/Trace.h>
San Mehatf1b736b2009-10-10 17:22:08 -070044
Robert Craigb9e3ba52014-02-04 10:53:00 -050045#include <selinux/android.h>
46
San Mehatfd7f5872009-10-12 11:32:47 -070047#include <sysutils/NetlinkEvent.h>
48
Kenny Root344ca102012-04-03 17:23:01 -070049#include <private/android_filesystem_config.h>
50
Paul Crowleyc6433a22017-10-24 14:54:43 -070051#include <ext4_utils/ext4_crypt.h>
52
53#include "Devmapper.h"
54#include "Ext4Crypt.h"
San Mehata19b2502010-01-06 10:33:53 -080055#include "Loop.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070056#include "NetlinkManager.h"
57#include "Process.h"
58#include "Utils.h"
59#include "VoldUtil.h"
60#include "VolumeManager.h"
61#include "cryptfs.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070062#include "fs/Ext4.h"
63#include "fs/Vfat.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070064#include "model/EmulatedVolume.h"
65#include "model/ObbVolume.h"
San Mehat23969932010-01-09 07:08:06 -080066
Jeff Sharkey36801cc2015-03-13 16:09:20 -070067using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060068using android::base::unique_fd;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070069
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060070static const char* kPathUserMount = "/mnt/user";
71static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
72
73static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
74
75/* 512MiB is large enough for testing purposes */
76static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070077
Jeff Sharkey36801cc2015-03-13 16:09:20 -070078static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080079static const unsigned int kMajorBlockExperimentalMin = 240;
80static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081
Paul Crowley8915d622018-09-18 15:14:18 -070082VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -070083
Paul Crowley8915d622018-09-18 15:14:18 -070084VolumeManager* VolumeManager::Instance() {
85 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -070086 return sInstance;
87}
88
89VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -080090 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060091 mNextObbId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -070092 // For security reasons, assume that a secure keyguard is
93 // showing until we hear otherwise
94 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -070095}
96
Paul Crowley8915d622018-09-18 15:14:18 -070097VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -080098
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060099int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600100 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Jeff Sharkey3472e522017-10-06 18:02:53 -0600101 if (android::base::GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600102 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
Paul Crowley8915d622018-09-18 15:14:18 -0700118 auto disk = new android::vold::Disk(
119 "virtual", buf.st_rdev, "virtual",
120 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600121 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700122 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600123 }
124 } else {
125 if (mVirtualDisk != nullptr) {
126 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700127 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600128
129 Loop::destroyByDevice(mVirtualDiskPath.c_str());
130 mVirtualDisk = nullptr;
131 }
132
133 if (access(kPathVirtualDisk, F_OK) == 0) {
134 unlink(kPathVirtualDisk);
135 }
136 }
137 return 0;
138}
139
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700140int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800141 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700142 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800143}
144
San Mehatf1b736b2009-10-10 17:22:08 -0700145int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600146 ATRACE_NAME("VolumeManager::start");
147
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700148 // Always start from a clean slate by unmounting everything in
149 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700150 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700151
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600152 Devmapper::destroyAll();
153 Loop::destroyAll();
154
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155 // Assume that we always have an emulated volume on internal
156 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700157 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700158 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowley8915d622018-09-18 15:14:18 -0700159 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700160 mInternalEmulated->create();
161
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600162 // Consider creating a virtual disk
163 updateVirtualDisk();
164
San Mehatf1b736b2009-10-10 17:22:08 -0700165 return 0;
166}
167
168int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700169 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170 mInternalEmulated->destroy();
171 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700172 return 0;
173}
174
Paul Crowley8915d622018-09-18 15:14:18 -0700175void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700176 std::lock_guard<std::mutex> lock(mLock);
177
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700178 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700179 LOG(DEBUG) << "----------------";
180 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700181 evt->dump();
182 }
San Mehatf1b736b2009-10-10 17:22:08 -0700183
Paul Crowley8915d622018-09-18 15:14:18 -0700184 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
185 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700186
187 if (devType != "disk") return;
188
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600189 int major = std::stoi(evt->findParam("MAJOR"));
190 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700191 dev_t device = makedev(major, minor);
192
193 switch (evt->getAction()) {
Paul Crowley8915d622018-09-18 15:14:18 -0700194 case NetlinkEvent::Action::kAdd: {
195 for (const auto& source : mDiskSources) {
196 if (source->matches(eventPath)) {
197 // For now, assume that MMC and virtio-blk (the latter is
198 // emulator-specific; see Disk.cpp for details) devices are SD,
199 // and that everything else is USB
200 int flags = source->getFlags();
201 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
202 major >= (int)kMajorBlockExperimentalMin &&
203 major <= (int)kMajorBlockExperimentalMax)) {
204 flags |= android::vold::Disk::Flags::kSd;
205 } else {
206 flags |= android::vold::Disk::Flags::kUsb;
207 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700208
Paul Crowley8915d622018-09-18 15:14:18 -0700209 auto disk =
210 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
211 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
212 break;
213 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700214 }
Paul Crowley8915d622018-09-18 15:14:18 -0700215 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700216 }
Paul Crowley8915d622018-09-18 15:14:18 -0700217 case NetlinkEvent::Action::kChange: {
218 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
219 handleDiskChanged(device);
220 break;
221 }
222 case NetlinkEvent::Action::kRemove: {
223 handleDiskRemoved(device);
224 break;
225 }
226 default: {
227 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
228 break;
229 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230 }
231}
232
Jeff Sharkey401b2602017-12-14 22:15:20 -0700233void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
234 // For security reasons, if secure keyguard is showing, wait
235 // until the user unlocks the device to actually touch it
236 if (mSecureKeyguardShowing) {
237 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowley8915d622018-09-18 15:14:18 -0700238 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700239 mPendingDisks.push_back(disk);
240 } else {
241 disk->create();
242 mDisks.push_back(disk);
243 }
244}
245
246void VolumeManager::handleDiskChanged(dev_t device) {
247 for (const auto& disk : mDisks) {
248 if (disk->getDevice() == device) {
249 disk->readMetadata();
250 disk->readPartitions();
251 }
252 }
253
254 // For security reasons, we ignore all pending disks, since
255 // we'll scan them once the device is unlocked
256}
257
258void VolumeManager::handleDiskRemoved(dev_t device) {
259 auto i = mDisks.begin();
260 while (i != mDisks.end()) {
261 if ((*i)->getDevice() == device) {
262 (*i)->destroy();
263 i = mDisks.erase(i);
264 } else {
265 ++i;
266 }
267 }
268 auto j = mPendingDisks.begin();
269 while (j != mPendingDisks.end()) {
270 if ((*j)->getDevice() == device) {
271 j = mPendingDisks.erase(j);
272 } else {
273 ++j;
274 }
275 }
276}
277
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700278void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800279 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700280 mDiskSources.push_back(diskSource);
281}
282
283std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
284 for (auto disk : mDisks) {
285 if (disk->getId() == id) {
286 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700287 }
288 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700289 return nullptr;
290}
San Mehatf1b736b2009-10-10 17:22:08 -0700291
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700292std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800293 // Vold could receive "mount" after "shutdown" command in the extreme case.
294 // If this happens, mInternalEmulated will equal nullptr and
295 // we need to deal with it in order to avoid null pointer crash.
296 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700297 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700298 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700299 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700300 auto vol = disk->findVolume(id);
301 if (vol != nullptr) {
302 return vol;
303 }
304 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600305 for (const auto& vol : mObbVolumes) {
306 if (vol->getId() == id) {
307 return vol;
308 }
309 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700310 return nullptr;
311}
312
Paul Crowley8915d622018-09-18 15:14:18 -0700313void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700314 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700315 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700316 disk->listVolumes(type, list);
317 }
318}
319
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600320int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700321 std::string normalizedGuid;
322 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
323 LOG(WARNING) << "Invalid GUID " << partGuid;
324 return -1;
325 }
326
Paul Crowleyc6433a22017-10-24 14:54:43 -0700327 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700328 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
329 if (unlink(keyPath.c_str()) != 0) {
330 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700331 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700332 }
Paul Crowleyc6433a22017-10-24 14:54:43 -0700333 if (e4crypt_is_native()) {
334 if (!e4crypt_destroy_volume_keys(fsUuid)) {
335 success = false;
336 }
337 }
338 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700339}
340
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700341int VolumeManager::linkPrimary(userid_t userId) {
342 std::string source(mPrimary->getPath());
343 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
344 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700345 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700346 }
347
348 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
349 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
350 if (errno != ENOENT) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600351 PLOG(WARNING) << "Failed to unlink " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700352 }
353 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700354 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700355 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600356 PLOG(WARNING) << "Failed to link";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700357 return -errno;
358 }
359 return 0;
360}
361
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700362int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
363 mAddedUsers[userId] = userSerialNumber;
364 return 0;
365}
366
367int VolumeManager::onUserRemoved(userid_t userId) {
368 mAddedUsers.erase(userId);
369 return 0;
370}
371
372int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700373 // Note that sometimes the system will spin up processes from Zygote
374 // before actually starting the user, so we're okay if Zygote
375 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600376 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700377 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
378
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700379 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700380 if (mPrimary) {
381 linkPrimary(userId);
382 }
383 return 0;
384}
385
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700386int VolumeManager::onUserStopped(userid_t userId) {
387 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700388 return 0;
389}
390
Jeff Sharkey401b2602017-12-14 22:15:20 -0700391int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
392 mSecureKeyguardShowing = isShowing;
393 if (!mSecureKeyguardShowing) {
394 // Now that secure keyguard has been dismissed, process
395 // any pending disks
396 for (const auto& disk : mPendingDisks) {
397 disk->create();
398 mDisks.push_back(disk);
399 }
400 mPendingDisks.clear();
401 }
402 return 0;
403}
404
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700405int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
406 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700407 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700408 linkPrimary(userId);
409 }
410 return 0;
411}
412
Jeff Sharkey3472e522017-10-06 18:02:53 -0600413static int unmount_tree(const std::string& prefix) {
Sudheer Shanka89ddf992018-09-25 14:22:07 -0700414 return android::vold::UnmountTree(prefix);
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700415}
416
Jeff Sharkey66270a22015-06-24 11:49:24 -0700417int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
418 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
419
420 DIR* dir;
421 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600422 std::string rootName;
423 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700424 int pidFd;
425 int nsFd;
426 struct stat sb;
427 pid_t child;
428
429 if (!(dir = opendir("/proc"))) {
430 PLOG(ERROR) << "Failed to opendir";
431 return -1;
432 }
433
434 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -0600435 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
436 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700437 closedir(dir);
438 return -1;
439 }
440
441 // Poke through all running PIDs look for apps running as UID
442 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -0700443 pid_t pid;
444 if (de->d_type != DT_DIR) continue;
445 if (!android::base::ParseInt(de->d_name, &pid)) continue;
446
Jeff Sharkey66270a22015-06-24 11:49:24 -0700447 pidFd = -1;
448 nsFd = -1;
449
450 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
451 if (pidFd < 0) {
452 goto next;
453 }
454 if (fstat(pidFd, &sb) != 0) {
455 PLOG(WARNING) << "Failed to stat " << de->d_name;
456 goto next;
457 }
458 if (sb.st_uid != uid) {
459 goto next;
460 }
461
462 // Matches so far, but refuse to touch if in root namespace
463 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600464 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700465 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
466 goto next;
467 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600468 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700469 LOG(WARNING) << "Skipping due to root namespace";
470 goto next;
471 }
472
473 // We purposefully leave the namespace open across the fork
Paul Crowley8915d622018-09-18 15:14:18 -0700474 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700475 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700476 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700477 goto next;
478 }
479
480 if (!(child = fork())) {
481 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700482 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700483 _exit(1);
484 }
485
Jeff Sharkey3472e522017-10-06 18:02:53 -0600486 unmount_tree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700487
488 std::string storageSource;
489 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700490 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700491 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700492 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700493 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700494 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700495 } else {
496 // Sane default of no storage visible
497 _exit(0);
498 }
Paul Crowley8915d622018-09-18 15:14:18 -0700499 if (TEMP_FAILURE_RETRY(
500 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
501 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700502 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700503 }
Paul Crowley8915d622018-09-18 15:14:18 -0700504 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
505 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +0900506 _exit(1);
507 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700508
509 // Mount user-specific symlink helper into place
510 userid_t user_id = multiuser_get_user_id(uid);
511 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowley8915d622018-09-18 15:14:18 -0700512 if (TEMP_FAILURE_RETRY(
513 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
514 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700515 _exit(1);
516 }
517
Jeff Sharkey66270a22015-06-24 11:49:24 -0700518 _exit(0);
519 }
520
521 if (child == -1) {
522 PLOG(ERROR) << "Failed to fork";
523 goto next;
524 } else {
525 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
526 }
527
Paul Crowley8915d622018-09-18 15:14:18 -0700528 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700529 close(nsFd);
530 close(pidFd);
531 }
532 closedir(dir);
533 return 0;
534}
535
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700536int VolumeManager::reset() {
537 // Tear down all existing disks/volumes and start from a blank slate so
538 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800539 if (mInternalEmulated != nullptr) {
540 mInternalEmulated->destroy();
541 mInternalEmulated->create();
542 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700543 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700544 disk->destroy();
545 disk->create();
546 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600547 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700548 mAddedUsers.clear();
549 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700550 return 0;
551}
552
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700553// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700554int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700555 if (mInternalEmulated == nullptr) {
Paul Crowley8915d622018-09-18 15:14:18 -0700556 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700557 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700558 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700559 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700560 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700561 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700562 disk->destroy();
563 }
564 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700565 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700566 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700567 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700568}
569
Jeff Sharkey9c484982015-03-31 10:35:33 -0700570int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700571 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600572 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700573
Jeff Sharkey9c484982015-03-31 10:35:33 -0700574 // First, try gracefully unmounting all known devices
575 if (mInternalEmulated != nullptr) {
576 mInternalEmulated->unmount();
577 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700578 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700579 disk->unmountAll();
580 }
581
582 // Worst case we might have some stale mounts lurking around, so
583 // force unmount those just to be safe.
584 FILE* fp = setmntent("/proc/mounts", "r");
585 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600586 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700587 return -errno;
588 }
589
590 // Some volumes can be stacked on each other, so force unmount in
591 // reverse order to give us the best chance of success.
592 std::list<std::string> toUnmount;
593 mntent* mentry;
594 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600595 auto test = std::string(mentry->mnt_dir);
Tri Vobca5cd72018-04-16 14:27:10 -0700596 if ((android::base::StartsWith(test, "/mnt/") &&
Bowgo Tsaic0cd37b2018-06-29 10:31:07 +0800597 !android::base::StartsWith(test, "/mnt/vendor") &&
598 !android::base::StartsWith(test, "/mnt/product")) ||
Tri Vobca5cd72018-04-16 14:27:10 -0700599 android::base::StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600600 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700601 }
602 }
603 endmntent(fp);
604
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700605 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600606 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700607 android::vold::ForceUnmount(path);
608 }
609
610 return 0;
611}
612
Jeff Sharkey3472e522017-10-06 18:02:53 -0600613int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700614 // Only offer to create directories for paths managed by vold
Jeff Sharkey3472e522017-10-06 18:02:53 -0600615 if (android::base::StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700616 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -0600617 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700618 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600619 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700620 return -EINVAL;
621 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700622}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600623
624static size_t kAppFuseMaxMountPointName = 32;
625
626static android::status_t getMountPath(uid_t uid, const std::string& name, std::string* path) {
627 if (name.size() > kAppFuseMaxMountPointName) {
628 LOG(ERROR) << "AppFuse mount name is too long.";
629 return -EINVAL;
630 }
631 for (size_t i = 0; i < name.size(); i++) {
632 if (!isalnum(name[i])) {
633 LOG(ERROR) << "AppFuse mount name contains invalid character.";
634 return -EINVAL;
635 }
636 }
637 *path = android::base::StringPrintf("/mnt/appfuse/%d_%s", uid, name.c_str());
638 return android::OK;
639}
640
641static android::status_t mountInNamespace(uid_t uid, int device_fd, const std::string& path) {
642 // Remove existing mount.
643 android::vold::ForceUnmount(path);
644
645 const auto opts = android::base::StringPrintf(
Paul Crowley8915d622018-09-18 15:14:18 -0700646 "fd=%i,"
647 "rootmode=40000,"
648 "default_permissions,"
649 "allow_other,"
650 "user_id=%d,group_id=%d,"
651 "context=\"u:object_r:app_fuse_file:s0\","
652 "fscontext=u:object_r:app_fusefs:s0",
653 device_fd, uid, uid);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600654
Paul Crowley8915d622018-09-18 15:14:18 -0700655 const int result =
656 TEMP_FAILURE_RETRY(mount("/dev/fuse", path.c_str(), "fuse",
657 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600658 if (result != 0) {
659 PLOG(ERROR) << "Failed to mount " << path;
660 return -errno;
661 }
662
663 return android::OK;
664}
665
Paul Crowley8915d622018-09-18 15:14:18 -0700666static android::status_t runCommandInNamespace(const std::string& command, uid_t uid, pid_t pid,
667 const std::string& path, int device_fd) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600668 if (DEBUG_APPFUSE) {
669 LOG(DEBUG) << "Run app fuse command " << command << " for the path " << path
670 << " in namespace " << uid;
671 }
672
673 unique_fd dir(open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
674 if (dir.get() == -1) {
675 PLOG(ERROR) << "Failed to open /proc";
676 return -errno;
677 }
678
679 // Obtains process file descriptor.
680 const std::string pid_str = android::base::StringPrintf("%d", pid);
Paul Crowley8915d622018-09-18 15:14:18 -0700681 const unique_fd pid_fd(openat(dir.get(), pid_str.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600682 if (pid_fd.get() == -1) {
683 PLOG(ERROR) << "Failed to open /proc/" << pid;
684 return -errno;
685 }
686
687 // Check UID of process.
688 {
689 struct stat sb;
690 const int result = fstat(pid_fd.get(), &sb);
691 if (result == -1) {
692 PLOG(ERROR) << "Failed to stat /proc/" << pid;
693 return -errno;
694 }
695 if (sb.st_uid != AID_SYSTEM) {
696 LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM
Paul Crowley8915d622018-09-18 15:14:18 -0700697 << ", actual=" << sb.st_uid;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600698 return -EPERM;
699 }
700 }
701
702 // Matches so far, but refuse to touch if in root namespace
703 {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600704 std::string rootName;
705 std::string pidName;
Paul Crowley8915d622018-09-18 15:14:18 -0700706 if (!android::vold::Readlinkat(dir.get(), "1/ns/mnt", &rootName) ||
707 !android::vold::Readlinkat(pid_fd.get(), "ns/mnt", &pidName)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600708 PLOG(ERROR) << "Failed to read namespaces";
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600709 return -EPERM;
710 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600711 if (rootName == pidName) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600712 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
Paul Crowley8915d622018-09-18 15:14:18 -0700718 unique_fd ns_fd(openat(pid_fd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600719 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.
Paul Crowley8915d622018-09-18 15:14:18 -0700736 if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 && errno != EINVAL &&
737 errno != ENOENT) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600738 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
763int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowley8915d622018-09-18 15:14:18 -0700764 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600765 int id = mNextObbId++;
766
767 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowley8915d622018-09-18 15:14:18 -0700768 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600769 vol->create();
770
771 mObbVolumes.push_back(vol);
772 *outVolId = vol->getId();
773 return android::OK;
774}
775
776int 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
789int VolumeManager::mountAppFuse(uid_t uid, pid_t pid, int mountId,
Paul Crowley8915d622018-09-18 15:14:18 -0700790 android::base::unique_fd* device_fd) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600791 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.
Paul Crowley8915d622018-09-18 15:14:18 -0700808 device_fd->reset(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600809 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
818int 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}