blob: f860eef9f5ad4cb25385450a566df6af83dab6ff [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 Crowleyedf7a4e2018-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>
Sudheer Shanka40ab6742018-09-18 13:07:45 -070033#include <array>
San Mehata19b2502010-01-06 10:33:53 -080034
San Mehata2677e42009-12-13 10:40:18 -080035#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070036
Jiyong Park8d21c922019-01-04 13:35:25 +090037#include <ApexProperties.sysprop.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080038#include <android-base/logging.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070039#include <android-base/parseint.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060040#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080041#include <android-base/stringprintf.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070042#include <android-base/strings.h>
43
Jeff Sharkey71ebe152013-09-17 17:24:38 -070044#include <cutils/fs.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060045#include <utils/Trace.h>
San Mehatf1b736b2009-10-10 17:22:08 -070046
Robert Craigb9e3ba52014-02-04 10:53:00 -050047#include <selinux/android.h>
48
San Mehatfd7f5872009-10-12 11:32:47 -070049#include <sysutils/NetlinkEvent.h>
50
Kenny Root344ca102012-04-03 17:23:01 -070051#include <private/android_filesystem_config.h>
52
Eric Biggersa701c452018-10-23 13:06:55 -070053#include <fscrypt/fscrypt.h>
Paul Crowleyc6433a22017-10-24 14:54:43 -070054
Risanac02a482018-10-31 21:59:47 -060055#include "AppFuseUtil.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070056#include "Devmapper.h"
Eric Biggersa701c452018-10-23 13:06:55 -070057#include "FsCrypt.h"
San Mehata19b2502010-01-06 10:33:53 -080058#include "Loop.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070059#include "NetlinkManager.h"
60#include "Process.h"
61#include "Utils.h"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070062#include "VoldNativeService.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070063#include "VoldUtil.h"
64#include "VolumeManager.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070065#include "fs/Ext4.h"
66#include "fs/Vfat.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070067#include "model/EmulatedVolume.h"
68#include "model/ObbVolume.h"
Risan8c9f3322018-10-29 08:52:56 +090069#include "model/StubVolume.h"
San Mehat23969932010-01-09 07:08:06 -080070
Sudheer Shankaf9b38a52019-02-14 19:09:51 +000071using android::OK;
Sudheer Shanka53947a32018-08-01 10:24:13 -070072using android::base::GetBoolProperty;
Mark Salyzyn86e81e72018-09-20 10:09:27 -070073using android::base::StartsWith;
Sudheer Shanka53947a32018-08-01 10:24:13 -070074using android::base::StringAppendF;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060076using android::base::unique_fd;
Sudheer Shanka023b5392019-02-06 12:39:19 -080077using android::vold::BindMount;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +000078using android::vold::CreateDir;
Sudheer Shanka30df1c62019-02-22 17:03:02 -080079using android::vold::DeleteDirContents;
Sudheer Shanka023b5392019-02-06 12:39:19 -080080using android::vold::DeleteDirContentsAndDir;
81using android::vold::Symlink;
82using android::vold::Unlink;
83using android::vold::UnmountTree;
Sudheer Shanka03992e32018-12-12 12:43:38 -080084using android::vold::VoldNativeService;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070085
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060086static const char* kPathUserMount = "/mnt/user";
87static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
88
89static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
90
Sudheer Shanka53947a32018-08-01 10:24:13 -070091static const std::string kEmptyString("");
92
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060093/* 512MiB is large enough for testing purposes */
94static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070095
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080097static const unsigned int kMajorBlockExperimentalMin = 240;
98static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070099
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700100VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700101
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700102VolumeManager* VolumeManager::Instance() {
103 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -0700104 return sInstance;
105}
106
107VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800108 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600109 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +0900110 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700111 // For security reasons, assume that a secure keyguard is
112 // showing until we hear otherwise
113 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700114}
115
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700116VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800117
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600118int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600119 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700120 if (GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600121 if (access(kPathVirtualDisk, F_OK) != 0) {
122 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
123 }
124
125 if (mVirtualDisk == nullptr) {
126 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
127 LOG(ERROR) << "Failed to create virtual disk";
128 return -1;
129 }
130
131 struct stat buf;
132 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
133 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
134 return -1;
135 }
136
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700137 auto disk = new android::vold::Disk(
138 "virtual", buf.st_rdev, "virtual",
139 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600140 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700141 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600142 }
143 } else {
144 if (mVirtualDisk != nullptr) {
145 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700146 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600147
148 Loop::destroyByDevice(mVirtualDiskPath.c_str());
149 mVirtualDisk = nullptr;
150 }
151
152 if (access(kPathVirtualDisk, F_OK) == 0) {
153 unlink(kPathVirtualDisk);
154 }
155 }
156 return 0;
157}
158
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700159int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800160 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700161 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800162}
163
San Mehatf1b736b2009-10-10 17:22:08 -0700164int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600165 ATRACE_NAME("VolumeManager::start");
166
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700167 // Always start from a clean slate by unmounting everything in
168 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700169 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600171 Devmapper::destroyAll();
172 Loop::destroyAll();
173
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700174 // Assume that we always have an emulated volume on internal
175 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700176 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700177 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700178 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179 mInternalEmulated->create();
180
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600181 // Consider creating a virtual disk
182 updateVirtualDisk();
183
San Mehatf1b736b2009-10-10 17:22:08 -0700184 return 0;
185}
186
187int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700188 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700189 mInternalEmulated->destroy();
190 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700191 return 0;
192}
193
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700194void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700195 std::lock_guard<std::mutex> lock(mLock);
196
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700197 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700198 LOG(DEBUG) << "----------------";
199 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700200 evt->dump();
201 }
San Mehatf1b736b2009-10-10 17:22:08 -0700202
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700203 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
204 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700205
206 if (devType != "disk") return;
207
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600208 int major = std::stoi(evt->findParam("MAJOR"));
209 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700210 dev_t device = makedev(major, minor);
211
212 switch (evt->getAction()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700213 case NetlinkEvent::Action::kAdd: {
214 for (const auto& source : mDiskSources) {
215 if (source->matches(eventPath)) {
216 // For now, assume that MMC and virtio-blk (the latter is
217 // emulator-specific; see Disk.cpp for details) devices are SD,
218 // and that everything else is USB
219 int flags = source->getFlags();
220 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
221 major >= (int)kMajorBlockExperimentalMin &&
222 major <= (int)kMajorBlockExperimentalMax)) {
223 flags |= android::vold::Disk::Flags::kSd;
224 } else {
225 flags |= android::vold::Disk::Flags::kUsb;
226 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700228 auto disk =
229 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
230 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
231 break;
232 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700233 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700234 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700235 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700236 case NetlinkEvent::Action::kChange: {
237 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
238 handleDiskChanged(device);
239 break;
240 }
241 case NetlinkEvent::Action::kRemove: {
242 handleDiskRemoved(device);
243 break;
244 }
245 default: {
246 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
247 break;
248 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700249 }
250}
251
Jeff Sharkey401b2602017-12-14 22:15:20 -0700252void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
253 // For security reasons, if secure keyguard is showing, wait
254 // until the user unlocks the device to actually touch it
255 if (mSecureKeyguardShowing) {
256 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700257 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700258 mPendingDisks.push_back(disk);
259 } else {
260 disk->create();
261 mDisks.push_back(disk);
262 }
263}
264
265void VolumeManager::handleDiskChanged(dev_t device) {
266 for (const auto& disk : mDisks) {
267 if (disk->getDevice() == device) {
268 disk->readMetadata();
269 disk->readPartitions();
270 }
271 }
272
273 // For security reasons, we ignore all pending disks, since
274 // we'll scan them once the device is unlocked
275}
276
277void VolumeManager::handleDiskRemoved(dev_t device) {
278 auto i = mDisks.begin();
279 while (i != mDisks.end()) {
280 if ((*i)->getDevice() == device) {
281 (*i)->destroy();
282 i = mDisks.erase(i);
283 } else {
284 ++i;
285 }
286 }
287 auto j = mPendingDisks.begin();
288 while (j != mPendingDisks.end()) {
289 if ((*j)->getDevice() == device) {
290 j = mPendingDisks.erase(j);
291 } else {
292 ++j;
293 }
294 }
295}
296
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700297void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800298 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700299 mDiskSources.push_back(diskSource);
300}
301
302std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
303 for (auto disk : mDisks) {
304 if (disk->getId() == id) {
305 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700306 }
307 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700308 return nullptr;
309}
San Mehatf1b736b2009-10-10 17:22:08 -0700310
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700311std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800312 // Vold could receive "mount" after "shutdown" command in the extreme case.
313 // If this happens, mInternalEmulated will equal nullptr and
314 // we need to deal with it in order to avoid null pointer crash.
315 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700316 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700317 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700318 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700319 auto vol = disk->findVolume(id);
320 if (vol != nullptr) {
321 return vol;
322 }
323 }
Risan8c9f3322018-10-29 08:52:56 +0900324 for (const auto& vol : mStubVolumes) {
325 if (vol->getId() == id) {
326 return vol;
327 }
328 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600329 for (const auto& vol : mObbVolumes) {
330 if (vol->getId() == id) {
331 return vol;
332 }
333 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700334 return nullptr;
335}
336
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800337void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
338 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700339 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700340 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700341 disk->listVolumes(type, list);
342 }
343}
344
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600345int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700346 std::string normalizedGuid;
347 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
348 LOG(WARNING) << "Invalid GUID " << partGuid;
349 return -1;
350 }
351
Paul Crowleyc6433a22017-10-24 14:54:43 -0700352 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700353 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
354 if (unlink(keyPath.c_str()) != 0) {
355 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700356 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700357 }
Eric Biggersa701c452018-10-23 13:06:55 -0700358 if (fscrypt_is_native()) {
359 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700360 success = false;
361 }
362 }
363 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700364}
365
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700366int VolumeManager::linkPrimary(userid_t userId) {
367 std::string source(mPrimary->getPath());
368 if (mPrimary->isEmulated()) {
369 source = StringPrintf("%s/%d", source.c_str(), userId);
370 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
371 }
372
373 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700374 LOG(DEBUG) << "Linking " << source << " to " << target;
Sudheer Shanka023b5392019-02-06 12:39:19 -0800375 Symlink(source, target);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700376 return 0;
377}
378
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700379int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
380 mAddedUsers[userId] = userSerialNumber;
381 return 0;
382}
383
384int VolumeManager::onUserRemoved(userid_t userId) {
385 mAddedUsers.erase(userId);
386 return 0;
387}
388
Sudheer Shankaebce4cc2019-04-29 10:46:35 -0700389int VolumeManager::onUserStarted(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700390 LOG(VERBOSE) << "onUserStarted: " << userId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700391 // Note that sometimes the system will spin up processes from Zygote
392 // before actually starting the user, so we're okay if Zygote
393 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600394 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700395 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
396
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700397 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700398 if (mPrimary) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700399 linkPrimary(userId);
400 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700401 return 0;
402}
403
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700404int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700405 LOG(VERBOSE) << "onUserStopped: " << userId;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700406 mStartedUsers.erase(userId);
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700407 return 0;
408}
409
Jeff Sharkey401b2602017-12-14 22:15:20 -0700410int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
411 mSecureKeyguardShowing = isShowing;
412 if (!mSecureKeyguardShowing) {
413 // Now that secure keyguard has been dismissed, process
414 // any pending disks
415 for (const auto& disk : mPendingDisks) {
416 disk->create();
417 mDisks.push_back(disk);
418 }
419 mPendingDisks.clear();
420 }
421 return 0;
422}
423
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700424int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
425 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700426 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700427 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700428 }
429 return 0;
430}
431
Sudheer Shanka817b9112018-12-13 17:40:28 -0800432int VolumeManager::remountUid(uid_t uid, int32_t mountMode) {
Sudheer Shanka817b9112018-12-13 17:40:28 -0800433 std::string mode;
434 switch (mountMode) {
435 case VoldNativeService::REMOUNT_MODE_NONE:
436 mode = "none";
437 break;
438 case VoldNativeService::REMOUNT_MODE_DEFAULT:
439 mode = "default";
440 break;
441 case VoldNativeService::REMOUNT_MODE_READ:
442 mode = "read";
443 break;
444 case VoldNativeService::REMOUNT_MODE_WRITE:
Sudheer Shankaa05ea742019-04-12 13:55:28 -0700445 case VoldNativeService::REMOUNT_MODE_LEGACY:
446 case VoldNativeService::REMOUNT_MODE_INSTALLER:
Sudheer Shanka817b9112018-12-13 17:40:28 -0800447 mode = "write";
448 break;
Sudheer Shanka36bdf7a2019-04-18 15:18:30 -0700449 case VoldNativeService::REMOUNT_MODE_FULL:
450 mode = "full";
451 break;
Sudheer Shanka817b9112018-12-13 17:40:28 -0800452 default:
453 PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode);
454 return -1;
455 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700456 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
457
458 DIR* dir;
459 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600460 std::string rootName;
461 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700462 int pidFd;
463 int nsFd;
464 struct stat sb;
465 pid_t child;
466
Jiyong Park8d21c922019-01-04 13:35:25 +0900467 static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false);
468
Jeff Sharkey66270a22015-06-24 11:49:24 -0700469 if (!(dir = opendir("/proc"))) {
470 PLOG(ERROR) << "Failed to opendir";
471 return -1;
472 }
473
474 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -0600475 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
476 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700477 closedir(dir);
478 return -1;
479 }
480
481 // Poke through all running PIDs look for apps running as UID
482 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -0700483 pid_t pid;
484 if (de->d_type != DT_DIR) continue;
485 if (!android::base::ParseInt(de->d_name, &pid)) continue;
486
Jeff Sharkey66270a22015-06-24 11:49:24 -0700487 pidFd = -1;
488 nsFd = -1;
489
490 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
491 if (pidFd < 0) {
492 goto next;
493 }
494 if (fstat(pidFd, &sb) != 0) {
495 PLOG(WARNING) << "Failed to stat " << de->d_name;
496 goto next;
497 }
498 if (sb.st_uid != uid) {
499 goto next;
500 }
501
502 // Matches so far, but refuse to touch if in root namespace
503 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600504 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700505 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
506 goto next;
507 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600508 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700509 LOG(WARNING) << "Skipping due to root namespace";
510 goto next;
511 }
512
Jiyong Park8d21c922019-01-04 13:35:25 +0900513 if (apexUpdatable) {
514 std::string exeName;
515 // When ro.apex.bionic_updatable is set to true,
516 // some early native processes have mount namespaces that are different
517 // from that of the init. Therefore, above check can't filter them out.
518 // Since the propagation type of / is 'shared', unmounting /storage
519 // for the early native processes affects other processes including
520 // init. Filter out such processes by skipping if a process is a
521 // non-Java process whose UID is < AID_APP_START. (The UID condition
522 // is required to not filter out child processes spawned by apps.)
523 if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) {
524 PLOG(WARNING) << "Failed to read exe name for " << de->d_name;
525 goto next;
526 }
527 if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) {
528 LOG(WARNING) << "Skipping due to native system process";
529 goto next;
530 }
531 }
532
Jeff Sharkey66270a22015-06-24 11:49:24 -0700533 // We purposefully leave the namespace open across the fork
Nick Kraleviche7e89ac2019-03-29 16:03:51 -0700534 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
535 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700536 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700537 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700538 goto next;
539 }
540
541 if (!(child = fork())) {
542 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700543 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700544 _exit(1);
545 }
546
Sudheer Shanka99d304a2018-09-27 14:53:51 -0700547 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700548
549 std::string storageSource;
550 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700551 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700552 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700553 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700554 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700555 storageSource = "/mnt/runtime/write";
Sudheer Shanka8f2ff802019-05-30 16:40:20 -0700556 } else if (mode == "full") {
557 storageSource = "/mnt/runtime/full";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700558 } else {
559 // Sane default of no storage visible
560 _exit(0);
561 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700562 if (TEMP_FAILURE_RETRY(
563 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
564 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700565 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700566 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700567 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
568 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +0900569 _exit(1);
570 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700571
572 // Mount user-specific symlink helper into place
573 userid_t user_id = multiuser_get_user_id(uid);
574 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700575 if (TEMP_FAILURE_RETRY(
576 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
577 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700578 _exit(1);
579 }
580
Jeff Sharkey66270a22015-06-24 11:49:24 -0700581 _exit(0);
582 }
583
584 if (child == -1) {
585 PLOG(ERROR) << "Failed to fork";
586 goto next;
587 } else {
588 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
589 }
590
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700591 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700592 close(nsFd);
593 close(pidFd);
594 }
595 closedir(dir);
596 return 0;
597}
598
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700599int VolumeManager::reset() {
600 // Tear down all existing disks/volumes and start from a blank slate so
601 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800602 if (mInternalEmulated != nullptr) {
603 mInternalEmulated->destroy();
604 mInternalEmulated->create();
605 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700606 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700607 disk->destroy();
608 disk->create();
609 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600610 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700611 mAddedUsers.clear();
Sudheer Shanka023b5392019-02-06 12:39:19 -0800612 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700613 return 0;
614}
615
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700616// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700617int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700618 if (mInternalEmulated == nullptr) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700619 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700620 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700621 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700622 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700623 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700624 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700625 disk->destroy();
626 }
Risan8c9f3322018-10-29 08:52:56 +0900627 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700628 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700629 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700630 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700631 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700632}
633
Jeff Sharkey9c484982015-03-31 10:35:33 -0700634int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700635 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600636 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700637
Jeff Sharkey9c484982015-03-31 10:35:33 -0700638 // First, try gracefully unmounting all known devices
639 if (mInternalEmulated != nullptr) {
640 mInternalEmulated->unmount();
641 }
Risan8c9f3322018-10-29 08:52:56 +0900642 for (const auto& stub : mStubVolumes) {
643 stub->unmount();
644 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700645 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700646 disk->unmountAll();
647 }
648
649 // Worst case we might have some stale mounts lurking around, so
650 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +0800651 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700652 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600653 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700654 return -errno;
655 }
656
657 // Some volumes can be stacked on each other, so force unmount in
658 // reverse order to give us the best chance of success.
659 std::list<std::string> toUnmount;
660 mntent* mentry;
661 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600662 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700663 if ((StartsWith(test, "/mnt/") &&
664#ifdef __ANDROID_DEBUGGABLE__
665 !StartsWith(test, "/mnt/scratch") &&
666#endif
667 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
668 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600669 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700670 }
671 }
672 endmntent(fp);
673
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700674 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600675 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700676 android::vold::ForceUnmount(path);
677 }
678
679 return 0;
680}
681
Jeff Sharkey3472e522017-10-06 18:02:53 -0600682int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700683 // Only offer to create directories for paths managed by vold
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700684 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700685 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -0600686 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700687 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600688 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700689 return -EINVAL;
690 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700691}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600692
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600693int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700694 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600695 int id = mNextObbId++;
696
697 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700698 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600699 vol->create();
700
701 mObbVolumes.push_back(vol);
702 *outVolId = vol->getId();
703 return android::OK;
704}
705
706int VolumeManager::destroyObb(const std::string& volId) {
707 auto i = mObbVolumes.begin();
708 while (i != mObbVolumes.end()) {
709 if ((*i)->getId() == volId) {
710 (*i)->destroy();
711 i = mObbVolumes.erase(i);
712 } else {
713 ++i;
714 }
715 }
716 return android::OK;
717}
718
Risan8c9f3322018-10-29 08:52:56 +0900719int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
720 const std::string& fsType, const std::string& fsUuid,
721 const std::string& fsLabel, std::string* outVolId) {
722 int id = mNextStubVolumeId++;
723 auto vol = std::shared_ptr<android::vold::VolumeBase>(
724 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
725 vol->create();
726
727 mStubVolumes.push_back(vol);
728 *outVolId = vol->getId();
729 return android::OK;
730}
731
732int VolumeManager::destroyStubVolume(const std::string& volId) {
733 auto i = mStubVolumes.begin();
734 while (i != mStubVolumes.end()) {
735 if ((*i)->getId() == volId) {
736 (*i)->destroy();
737 i = mStubVolumes.erase(i);
738 } else {
739 ++i;
740 }
741 }
742 return android::OK;
743}
744
Risan8f6198d2018-10-26 20:56:45 -0600745int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -0600746 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600747}
748
Risan8f6198d2018-10-26 20:56:45 -0600749int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -0600750 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -0600751}
752
753int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -0600754 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600755}