blob: ce2d93558ea15712cf8103fb34cd704270054642 [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
Greg Kaiseref9abab2018-12-18 08:42:08 -0800322void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
323 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700324 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700325 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700326 disk->listVolumes(type, list);
327 }
328}
329
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600330int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700331 std::string normalizedGuid;
332 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
333 LOG(WARNING) << "Invalid GUID " << partGuid;
334 return -1;
335 }
336
Paul Crowleyc6433a22017-10-24 14:54:43 -0700337 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700338 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
339 if (unlink(keyPath.c_str()) != 0) {
340 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700341 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700342 }
Eric Biggersa701c452018-10-23 13:06:55 -0700343 if (fscrypt_is_native()) {
344 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700345 success = false;
346 }
347 }
348 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700349}
350
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700351int VolumeManager::linkPrimary(userid_t userId) {
352 std::string source(mPrimary->getPath());
353 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
354 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700355 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700356 }
357
358 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
359 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
360 if (errno != ENOENT) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600361 PLOG(WARNING) << "Failed to unlink " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700362 }
363 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700364 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700365 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600366 PLOG(WARNING) << "Failed to link";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700367 return -errno;
368 }
369 return 0;
370}
371
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700372int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
373 mAddedUsers[userId] = userSerialNumber;
374 return 0;
375}
376
377int VolumeManager::onUserRemoved(userid_t userId) {
378 mAddedUsers.erase(userId);
379 return 0;
380}
381
382int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700383 // Note that sometimes the system will spin up processes from Zygote
384 // before actually starting the user, so we're okay if Zygote
385 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600386 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700387 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
388
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700389 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700390 if (mPrimary) {
391 linkPrimary(userId);
392 }
393 return 0;
394}
395
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700396int VolumeManager::onUserStopped(userid_t userId) {
397 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700398 return 0;
399}
400
Jeff Sharkey401b2602017-12-14 22:15:20 -0700401int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
402 mSecureKeyguardShowing = isShowing;
403 if (!mSecureKeyguardShowing) {
404 // Now that secure keyguard has been dismissed, process
405 // any pending disks
406 for (const auto& disk : mPendingDisks) {
407 disk->create();
408 mDisks.push_back(disk);
409 }
410 mPendingDisks.clear();
411 }
412 return 0;
413}
414
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700415int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
416 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700417 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700418 linkPrimary(userId);
419 }
420 return 0;
421}
422
Jeff Sharkey66270a22015-06-24 11:49:24 -0700423int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
424 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
425
426 DIR* dir;
427 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600428 std::string rootName;
429 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700430 int pidFd;
431 int nsFd;
432 struct stat sb;
433 pid_t child;
434
435 if (!(dir = opendir("/proc"))) {
436 PLOG(ERROR) << "Failed to opendir";
437 return -1;
438 }
439
440 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -0600441 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
442 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700443 closedir(dir);
444 return -1;
445 }
446
447 // Poke through all running PIDs look for apps running as UID
448 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -0700449 pid_t pid;
450 if (de->d_type != DT_DIR) continue;
451 if (!android::base::ParseInt(de->d_name, &pid)) continue;
452
Jeff Sharkey66270a22015-06-24 11:49:24 -0700453 pidFd = -1;
454 nsFd = -1;
455
456 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
457 if (pidFd < 0) {
458 goto next;
459 }
460 if (fstat(pidFd, &sb) != 0) {
461 PLOG(WARNING) << "Failed to stat " << de->d_name;
462 goto next;
463 }
464 if (sb.st_uid != uid) {
465 goto next;
466 }
467
468 // Matches so far, but refuse to touch if in root namespace
469 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600470 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700471 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
472 goto next;
473 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600474 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700475 LOG(WARNING) << "Skipping due to root namespace";
476 goto next;
477 }
478
479 // We purposefully leave the namespace open across the fork
Paul Crowley8915d622018-09-18 15:14:18 -0700480 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700481 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700482 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700483 goto next;
484 }
485
486 if (!(child = fork())) {
487 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700488 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700489 _exit(1);
490 }
491
Sudheer Shanka99d304a2018-09-27 14:53:51 -0700492 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700493
494 std::string storageSource;
495 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700496 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700497 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700498 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700499 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700500 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700501 } else {
502 // Sane default of no storage visible
503 _exit(0);
504 }
Paul Crowley8915d622018-09-18 15:14:18 -0700505 if (TEMP_FAILURE_RETRY(
506 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
507 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700508 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700509 }
Paul Crowley8915d622018-09-18 15:14:18 -0700510 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
511 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +0900512 _exit(1);
513 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700514
515 // Mount user-specific symlink helper into place
516 userid_t user_id = multiuser_get_user_id(uid);
517 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowley8915d622018-09-18 15:14:18 -0700518 if (TEMP_FAILURE_RETRY(
519 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
520 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700521 _exit(1);
522 }
523
Jeff Sharkey66270a22015-06-24 11:49:24 -0700524 _exit(0);
525 }
526
527 if (child == -1) {
528 PLOG(ERROR) << "Failed to fork";
529 goto next;
530 } else {
531 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
532 }
533
Paul Crowley8915d622018-09-18 15:14:18 -0700534 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700535 close(nsFd);
536 close(pidFd);
537 }
538 closedir(dir);
539 return 0;
540}
541
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700542int VolumeManager::reset() {
543 // Tear down all existing disks/volumes and start from a blank slate so
544 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800545 if (mInternalEmulated != nullptr) {
546 mInternalEmulated->destroy();
547 mInternalEmulated->create();
548 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700549 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700550 disk->destroy();
551 disk->create();
552 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600553 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700554 mAddedUsers.clear();
555 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700556 return 0;
557}
558
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700559// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700560int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700561 if (mInternalEmulated == nullptr) {
Paul Crowley8915d622018-09-18 15:14:18 -0700562 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700563 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700564 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700565 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700566 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700567 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700568 disk->destroy();
569 }
Risan8c9f3322018-10-29 08:52:56 +0900570 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700571 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700572 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700573 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700574 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700575}
576
Jeff Sharkey9c484982015-03-31 10:35:33 -0700577int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700578 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600579 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700580
Jeff Sharkey9c484982015-03-31 10:35:33 -0700581 // First, try gracefully unmounting all known devices
582 if (mInternalEmulated != nullptr) {
583 mInternalEmulated->unmount();
584 }
Risan8c9f3322018-10-29 08:52:56 +0900585 for (const auto& stub : mStubVolumes) {
586 stub->unmount();
587 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700588 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700589 disk->unmountAll();
590 }
591
592 // Worst case we might have some stale mounts lurking around, so
593 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +0800594 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700595 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600596 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700597 return -errno;
598 }
599
600 // Some volumes can be stacked on each other, so force unmount in
601 // reverse order to give us the best chance of success.
602 std::list<std::string> toUnmount;
603 mntent* mentry;
604 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600605 auto test = std::string(mentry->mnt_dir);
Mark Salyzync4405e92018-09-20 10:09:27 -0700606 if ((StartsWith(test, "/mnt/") &&
607#ifdef __ANDROID_DEBUGGABLE__
608 !StartsWith(test, "/mnt/scratch") &&
609#endif
610 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
611 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600612 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700613 }
614 }
615 endmntent(fp);
616
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700617 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600618 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700619 android::vold::ForceUnmount(path);
620 }
621
622 return 0;
623}
624
Jeff Sharkey3472e522017-10-06 18:02:53 -0600625int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700626 // Only offer to create directories for paths managed by vold
Mark Salyzync4405e92018-09-20 10:09:27 -0700627 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700628 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -0600629 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700630 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600631 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700632 return -EINVAL;
633 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700634}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600635
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600636int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowley8915d622018-09-18 15:14:18 -0700637 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600638 int id = mNextObbId++;
639
640 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowley8915d622018-09-18 15:14:18 -0700641 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600642 vol->create();
643
644 mObbVolumes.push_back(vol);
645 *outVolId = vol->getId();
646 return android::OK;
647}
648
649int VolumeManager::destroyObb(const std::string& volId) {
650 auto i = mObbVolumes.begin();
651 while (i != mObbVolumes.end()) {
652 if ((*i)->getId() == volId) {
653 (*i)->destroy();
654 i = mObbVolumes.erase(i);
655 } else {
656 ++i;
657 }
658 }
659 return android::OK;
660}
661
Risan8c9f3322018-10-29 08:52:56 +0900662int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
663 const std::string& fsType, const std::string& fsUuid,
664 const std::string& fsLabel, std::string* outVolId) {
665 int id = mNextStubVolumeId++;
666 auto vol = std::shared_ptr<android::vold::VolumeBase>(
667 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
668 vol->create();
669
670 mStubVolumes.push_back(vol);
671 *outVolId = vol->getId();
672 return android::OK;
673}
674
675int VolumeManager::destroyStubVolume(const std::string& volId) {
676 auto i = mStubVolumes.begin();
677 while (i != mStubVolumes.end()) {
678 if ((*i)->getId() == volId) {
679 (*i)->destroy();
680 i = mStubVolumes.erase(i);
681 } else {
682 ++i;
683 }
684 }
685 return android::OK;
686}
687
Risan5f53cd32018-10-26 20:56:45 -0600688int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -0600689 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600690}
691
Risan5f53cd32018-10-26 20:56:45 -0600692int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -0600693 return android::vold::UnmountAppFuse(uid, mountId);
Risan5f53cd32018-10-26 20:56:45 -0600694}
695
696int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -0600697 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600698}