blob: e08319b3d6acac06cf116f1e36d071292afa2297 [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
Eric Biggersa701c452018-10-23 13:06:55 -070051#include <fscrypt/fscrypt.h>
Paul Crowleyc6433a22017-10-24 14:54:43 -070052
Risanac02a482018-10-31 21:59:47 -060053#include "AppFuseUtil.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070054#include "Devmapper.h"
Eric Biggersa701c452018-10-23 13:06:55 -070055#include "FsCrypt.h"
San Mehata19b2502010-01-06 10:33:53 -080056#include "Loop.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070057#include "NetlinkManager.h"
58#include "Process.h"
59#include "Utils.h"
60#include "VoldUtil.h"
61#include "VolumeManager.h"
62#include "cryptfs.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070063#include "fs/Ext4.h"
64#include "fs/Vfat.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070065#include "model/EmulatedVolume.h"
66#include "model/ObbVolume.h"
Risan8c9f3322018-10-29 08:52:56 +090067#include "model/StubVolume.h"
San Mehat23969932010-01-09 07:08:06 -080068
Mark Salyzync4405e92018-09-20 10:09:27 -070069using android::base::StartsWith;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070070using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060071using android::base::unique_fd;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070072
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060073static const char* kPathUserMount = "/mnt/user";
74static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
75
76static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
77
78/* 512MiB is large enough for testing purposes */
79static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080082static const unsigned int kMajorBlockExperimentalMin = 240;
83static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070084
Paul Crowley8915d622018-09-18 15:14:18 -070085VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -070086
Paul Crowley8915d622018-09-18 15:14:18 -070087VolumeManager* VolumeManager::Instance() {
88 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -070089 return sInstance;
90}
91
92VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -080093 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060094 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +090095 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -070096 // For security reasons, assume that a secure keyguard is
97 // showing until we hear otherwise
98 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -070099}
100
Paul Crowley8915d622018-09-18 15:14:18 -0700101VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800102
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600103int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600104 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Jeff Sharkey3472e522017-10-06 18:02:53 -0600105 if (android::base::GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600106 if (access(kPathVirtualDisk, F_OK) != 0) {
107 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
108 }
109
110 if (mVirtualDisk == nullptr) {
111 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
112 LOG(ERROR) << "Failed to create virtual disk";
113 return -1;
114 }
115
116 struct stat buf;
117 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
118 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
119 return -1;
120 }
121
Paul Crowley8915d622018-09-18 15:14:18 -0700122 auto disk = new android::vold::Disk(
123 "virtual", buf.st_rdev, "virtual",
124 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600125 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700126 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600127 }
128 } else {
129 if (mVirtualDisk != nullptr) {
130 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700131 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600132
133 Loop::destroyByDevice(mVirtualDiskPath.c_str());
134 mVirtualDisk = nullptr;
135 }
136
137 if (access(kPathVirtualDisk, F_OK) == 0) {
138 unlink(kPathVirtualDisk);
139 }
140 }
141 return 0;
142}
143
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700144int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800145 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700146 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800147}
148
San Mehatf1b736b2009-10-10 17:22:08 -0700149int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600150 ATRACE_NAME("VolumeManager::start");
151
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700152 // Always start from a clean slate by unmounting everything in
153 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700154 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600156 Devmapper::destroyAll();
157 Loop::destroyAll();
158
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159 // Assume that we always have an emulated volume on internal
160 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700161 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700162 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowley8915d622018-09-18 15:14:18 -0700163 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700164 mInternalEmulated->create();
165
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600166 // Consider creating a virtual disk
167 updateVirtualDisk();
168
San Mehatf1b736b2009-10-10 17:22:08 -0700169 return 0;
170}
171
172int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700173 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700174 mInternalEmulated->destroy();
175 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700176 return 0;
177}
178
Paul Crowley8915d622018-09-18 15:14:18 -0700179void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700180 std::lock_guard<std::mutex> lock(mLock);
181
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700182 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700183 LOG(DEBUG) << "----------------";
184 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700185 evt->dump();
186 }
San Mehatf1b736b2009-10-10 17:22:08 -0700187
Paul Crowley8915d622018-09-18 15:14:18 -0700188 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
189 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190
191 if (devType != "disk") return;
192
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600193 int major = std::stoi(evt->findParam("MAJOR"));
194 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700195 dev_t device = makedev(major, minor);
196
197 switch (evt->getAction()) {
Paul Crowley8915d622018-09-18 15:14:18 -0700198 case NetlinkEvent::Action::kAdd: {
199 for (const auto& source : mDiskSources) {
200 if (source->matches(eventPath)) {
201 // For now, assume that MMC and virtio-blk (the latter is
202 // emulator-specific; see Disk.cpp for details) devices are SD,
203 // and that everything else is USB
204 int flags = source->getFlags();
205 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
206 major >= (int)kMajorBlockExperimentalMin &&
207 major <= (int)kMajorBlockExperimentalMax)) {
208 flags |= android::vold::Disk::Flags::kSd;
209 } else {
210 flags |= android::vold::Disk::Flags::kUsb;
211 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700212
Paul Crowley8915d622018-09-18 15:14:18 -0700213 auto disk =
214 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
215 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
216 break;
217 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218 }
Paul Crowley8915d622018-09-18 15:14:18 -0700219 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700220 }
Paul Crowley8915d622018-09-18 15:14:18 -0700221 case NetlinkEvent::Action::kChange: {
222 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
223 handleDiskChanged(device);
224 break;
225 }
226 case NetlinkEvent::Action::kRemove: {
227 handleDiskRemoved(device);
228 break;
229 }
230 default: {
231 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
232 break;
233 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 }
235}
236
Jeff Sharkey401b2602017-12-14 22:15:20 -0700237void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
238 // For security reasons, if secure keyguard is showing, wait
239 // until the user unlocks the device to actually touch it
240 if (mSecureKeyguardShowing) {
241 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowley8915d622018-09-18 15:14:18 -0700242 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700243 mPendingDisks.push_back(disk);
244 } else {
245 disk->create();
246 mDisks.push_back(disk);
247 }
248}
249
250void VolumeManager::handleDiskChanged(dev_t device) {
251 for (const auto& disk : mDisks) {
252 if (disk->getDevice() == device) {
253 disk->readMetadata();
254 disk->readPartitions();
255 }
256 }
257
258 // For security reasons, we ignore all pending disks, since
259 // we'll scan them once the device is unlocked
260}
261
262void VolumeManager::handleDiskRemoved(dev_t device) {
263 auto i = mDisks.begin();
264 while (i != mDisks.end()) {
265 if ((*i)->getDevice() == device) {
266 (*i)->destroy();
267 i = mDisks.erase(i);
268 } else {
269 ++i;
270 }
271 }
272 auto j = mPendingDisks.begin();
273 while (j != mPendingDisks.end()) {
274 if ((*j)->getDevice() == device) {
275 j = mPendingDisks.erase(j);
276 } else {
277 ++j;
278 }
279 }
280}
281
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700282void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800283 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700284 mDiskSources.push_back(diskSource);
285}
286
287std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
288 for (auto disk : mDisks) {
289 if (disk->getId() == id) {
290 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700291 }
292 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700293 return nullptr;
294}
San Mehatf1b736b2009-10-10 17:22:08 -0700295
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700296std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800297 // Vold could receive "mount" after "shutdown" command in the extreme case.
298 // If this happens, mInternalEmulated will equal nullptr and
299 // we need to deal with it in order to avoid null pointer crash.
300 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700301 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700302 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700303 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700304 auto vol = disk->findVolume(id);
305 if (vol != nullptr) {
306 return vol;
307 }
308 }
Risan8c9f3322018-10-29 08:52:56 +0900309 for (const auto& vol : mStubVolumes) {
310 if (vol->getId() == id) {
311 return vol;
312 }
313 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600314 for (const auto& vol : mObbVolumes) {
315 if (vol->getId() == id) {
316 return vol;
317 }
318 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700319 return nullptr;
320}
321
Paul Crowley8915d622018-09-18 15:14:18 -0700322void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700323 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700324 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700325 disk->listVolumes(type, list);
326 }
327}
328
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600329int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700330 std::string normalizedGuid;
331 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
332 LOG(WARNING) << "Invalid GUID " << partGuid;
333 return -1;
334 }
335
Paul Crowleyc6433a22017-10-24 14:54:43 -0700336 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700337 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
338 if (unlink(keyPath.c_str()) != 0) {
339 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700340 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700341 }
Eric Biggersa701c452018-10-23 13:06:55 -0700342 if (fscrypt_is_native()) {
343 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700344 success = false;
345 }
346 }
347 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700348}
349
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700350int VolumeManager::linkPrimary(userid_t userId) {
351 std::string source(mPrimary->getPath());
352 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
353 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700354 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700355 }
356
357 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
358 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
359 if (errno != ENOENT) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600360 PLOG(WARNING) << "Failed to unlink " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700361 }
362 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700363 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700364 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600365 PLOG(WARNING) << "Failed to link";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700366 return -errno;
367 }
368 return 0;
369}
370
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700371int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
372 mAddedUsers[userId] = userSerialNumber;
373 return 0;
374}
375
376int VolumeManager::onUserRemoved(userid_t userId) {
377 mAddedUsers.erase(userId);
378 return 0;
379}
380
381int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700382 // Note that sometimes the system will spin up processes from Zygote
383 // before actually starting the user, so we're okay if Zygote
384 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600385 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700386 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
387
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700388 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700389 if (mPrimary) {
390 linkPrimary(userId);
391 }
392 return 0;
393}
394
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700395int VolumeManager::onUserStopped(userid_t userId) {
396 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700397 return 0;
398}
399
Jeff Sharkey401b2602017-12-14 22:15:20 -0700400int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
401 mSecureKeyguardShowing = isShowing;
402 if (!mSecureKeyguardShowing) {
403 // Now that secure keyguard has been dismissed, process
404 // any pending disks
405 for (const auto& disk : mPendingDisks) {
406 disk->create();
407 mDisks.push_back(disk);
408 }
409 mPendingDisks.clear();
410 }
411 return 0;
412}
413
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700414int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
415 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700416 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700417 linkPrimary(userId);
418 }
419 return 0;
420}
421
Jeff Sharkey66270a22015-06-24 11:49:24 -0700422int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
423 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
424
425 DIR* dir;
426 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600427 std::string rootName;
428 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700429 int pidFd;
430 int nsFd;
431 struct stat sb;
432 pid_t child;
433
434 if (!(dir = opendir("/proc"))) {
435 PLOG(ERROR) << "Failed to opendir";
436 return -1;
437 }
438
439 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -0600440 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
441 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700442 closedir(dir);
443 return -1;
444 }
445
446 // Poke through all running PIDs look for apps running as UID
447 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -0700448 pid_t pid;
449 if (de->d_type != DT_DIR) continue;
450 if (!android::base::ParseInt(de->d_name, &pid)) continue;
451
Jeff Sharkey66270a22015-06-24 11:49:24 -0700452 pidFd = -1;
453 nsFd = -1;
454
455 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
456 if (pidFd < 0) {
457 goto next;
458 }
459 if (fstat(pidFd, &sb) != 0) {
460 PLOG(WARNING) << "Failed to stat " << de->d_name;
461 goto next;
462 }
463 if (sb.st_uid != uid) {
464 goto next;
465 }
466
467 // Matches so far, but refuse to touch if in root namespace
468 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600469 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700470 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
471 goto next;
472 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600473 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700474 LOG(WARNING) << "Skipping due to root namespace";
475 goto next;
476 }
477
478 // We purposefully leave the namespace open across the fork
Paul Crowley8915d622018-09-18 15:14:18 -0700479 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700480 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700481 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700482 goto next;
483 }
484
485 if (!(child = fork())) {
486 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700487 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700488 _exit(1);
489 }
490
Sudheer Shanka99d304a2018-09-27 14:53:51 -0700491 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700492
493 std::string storageSource;
494 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700495 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700496 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700497 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700498 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700499 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700500 } else {
501 // Sane default of no storage visible
502 _exit(0);
503 }
Paul Crowley8915d622018-09-18 15:14:18 -0700504 if (TEMP_FAILURE_RETRY(
505 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
506 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700507 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700508 }
Paul Crowley8915d622018-09-18 15:14:18 -0700509 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
510 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +0900511 _exit(1);
512 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700513
514 // Mount user-specific symlink helper into place
515 userid_t user_id = multiuser_get_user_id(uid);
516 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowley8915d622018-09-18 15:14:18 -0700517 if (TEMP_FAILURE_RETRY(
518 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
519 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700520 _exit(1);
521 }
522
Jeff Sharkey66270a22015-06-24 11:49:24 -0700523 _exit(0);
524 }
525
526 if (child == -1) {
527 PLOG(ERROR) << "Failed to fork";
528 goto next;
529 } else {
530 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
531 }
532
Paul Crowley8915d622018-09-18 15:14:18 -0700533 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700534 close(nsFd);
535 close(pidFd);
536 }
537 closedir(dir);
538 return 0;
539}
540
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700541int VolumeManager::reset() {
542 // Tear down all existing disks/volumes and start from a blank slate so
543 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800544 if (mInternalEmulated != nullptr) {
545 mInternalEmulated->destroy();
546 mInternalEmulated->create();
547 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700548 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700549 disk->destroy();
550 disk->create();
551 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600552 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700553 mAddedUsers.clear();
554 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700555 return 0;
556}
557
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700558// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700559int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700560 if (mInternalEmulated == nullptr) {
Paul Crowley8915d622018-09-18 15:14:18 -0700561 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700562 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700563 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700564 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700565 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700566 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700567 disk->destroy();
568 }
Risan8c9f3322018-10-29 08:52:56 +0900569 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700570 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700571 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700572 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700573 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700574}
575
Jeff Sharkey9c484982015-03-31 10:35:33 -0700576int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700577 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600578 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700579
Jeff Sharkey9c484982015-03-31 10:35:33 -0700580 // First, try gracefully unmounting all known devices
581 if (mInternalEmulated != nullptr) {
582 mInternalEmulated->unmount();
583 }
Risan8c9f3322018-10-29 08:52:56 +0900584 for (const auto& stub : mStubVolumes) {
585 stub->unmount();
586 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700587 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700588 disk->unmountAll();
589 }
590
591 // Worst case we might have some stale mounts lurking around, so
592 // force unmount those just to be safe.
593 FILE* fp = setmntent("/proc/mounts", "r");
594 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600595 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700596 return -errno;
597 }
598
599 // Some volumes can be stacked on each other, so force unmount in
600 // reverse order to give us the best chance of success.
601 std::list<std::string> toUnmount;
602 mntent* mentry;
603 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600604 auto test = std::string(mentry->mnt_dir);
Mark Salyzync4405e92018-09-20 10:09:27 -0700605 if ((StartsWith(test, "/mnt/") &&
606#ifdef __ANDROID_DEBUGGABLE__
607 !StartsWith(test, "/mnt/scratch") &&
608#endif
609 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
610 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600611 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700612 }
613 }
614 endmntent(fp);
615
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700616 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600617 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700618 android::vold::ForceUnmount(path);
619 }
620
621 return 0;
622}
623
Jeff Sharkey3472e522017-10-06 18:02:53 -0600624int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700625 // Only offer to create directories for paths managed by vold
Mark Salyzync4405e92018-09-20 10:09:27 -0700626 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700627 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -0600628 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700629 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600630 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700631 return -EINVAL;
632 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700633}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600634
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600635int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowley8915d622018-09-18 15:14:18 -0700636 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600637 int id = mNextObbId++;
638
639 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowley8915d622018-09-18 15:14:18 -0700640 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600641 vol->create();
642
643 mObbVolumes.push_back(vol);
644 *outVolId = vol->getId();
645 return android::OK;
646}
647
648int VolumeManager::destroyObb(const std::string& volId) {
649 auto i = mObbVolumes.begin();
650 while (i != mObbVolumes.end()) {
651 if ((*i)->getId() == volId) {
652 (*i)->destroy();
653 i = mObbVolumes.erase(i);
654 } else {
655 ++i;
656 }
657 }
658 return android::OK;
659}
660
Risan8c9f3322018-10-29 08:52:56 +0900661int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
662 const std::string& fsType, const std::string& fsUuid,
663 const std::string& fsLabel, std::string* outVolId) {
664 int id = mNextStubVolumeId++;
665 auto vol = std::shared_ptr<android::vold::VolumeBase>(
666 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
667 vol->create();
668
669 mStubVolumes.push_back(vol);
670 *outVolId = vol->getId();
671 return android::OK;
672}
673
674int VolumeManager::destroyStubVolume(const std::string& volId) {
675 auto i = mStubVolumes.begin();
676 while (i != mStubVolumes.end()) {
677 if ((*i)->getId() == volId) {
678 (*i)->destroy();
679 i = mStubVolumes.erase(i);
680 } else {
681 ++i;
682 }
683 }
684 return android::OK;
685}
686
Risan5f53cd32018-10-26 20:56:45 -0600687int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -0600688 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600689}
690
Risan5f53cd32018-10-26 20:56:45 -0600691int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -0600692 return android::vold::UnmountAppFuse(uid, mountId);
Risan5f53cd32018-10-26 20:56:45 -0600693}
694
695int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -0600696 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600697}