blob: b0e0b23f95d386d6a7299047b6f7139fd729bd73 [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>
Zim3623a212019-07-19 16:46:53 +010034#include <thread>
San Mehata19b2502010-01-06 10:33:53 -080035
San Mehata2677e42009-12-13 10:40:18 -080036#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070037
Jiyong Park8d21c922019-01-04 13:35:25 +090038#include <ApexProperties.sysprop.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080039#include <android-base/logging.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070040#include <android-base/parseint.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060041#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080042#include <android-base/stringprintf.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070043#include <android-base/strings.h>
44
Jeff Sharkey71ebe152013-09-17 17:24:38 -070045#include <cutils/fs.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060046#include <utils/Trace.h>
San Mehatf1b736b2009-10-10 17:22:08 -070047
Robert Craigb9e3ba52014-02-04 10:53:00 -050048#include <selinux/android.h>
49
San Mehatfd7f5872009-10-12 11:32:47 -070050#include <sysutils/NetlinkEvent.h>
51
Kenny Root344ca102012-04-03 17:23:01 -070052#include <private/android_filesystem_config.h>
53
Eric Biggersa701c452018-10-23 13:06:55 -070054#include <fscrypt/fscrypt.h>
Paul Crowleyc6433a22017-10-24 14:54:43 -070055
Risanac02a482018-10-31 21:59:47 -060056#include "AppFuseUtil.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070057#include "Devmapper.h"
Eric Biggersa701c452018-10-23 13:06:55 -070058#include "FsCrypt.h"
San Mehata19b2502010-01-06 10:33:53 -080059#include "Loop.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070060#include "NetlinkManager.h"
61#include "Process.h"
62#include "Utils.h"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070063#include "VoldNativeService.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070064#include "VoldUtil.h"
65#include "VolumeManager.h"
66#include "cryptfs.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070067#include "fs/Ext4.h"
68#include "fs/Vfat.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070069#include "model/EmulatedVolume.h"
70#include "model/ObbVolume.h"
Risan8c9f3322018-10-29 08:52:56 +090071#include "model/StubVolume.h"
San Mehat23969932010-01-09 07:08:06 -080072
Sudheer Shankaf9b38a52019-02-14 19:09:51 +000073using android::OK;
Sudheer Shanka53947a32018-08-01 10:24:13 -070074using android::base::GetBoolProperty;
Mark Salyzyn86e81e72018-09-20 10:09:27 -070075using android::base::StartsWith;
Sudheer Shanka53947a32018-08-01 10:24:13 -070076using android::base::StringAppendF;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070077using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060078using android::base::unique_fd;
Sudheer Shanka023b5392019-02-06 12:39:19 -080079using android::vold::BindMount;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +000080using android::vold::CreateDir;
Sudheer Shanka30df1c62019-02-22 17:03:02 -080081using android::vold::DeleteDirContents;
Sudheer Shanka023b5392019-02-06 12:39:19 -080082using android::vold::DeleteDirContentsAndDir;
83using android::vold::Symlink;
84using android::vold::Unlink;
85using android::vold::UnmountTree;
Sudheer Shanka03992e32018-12-12 12:43:38 -080086using android::vold::VoldNativeService;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070087
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060088static const char* kPathUserMount = "/mnt/user";
89static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
90
91static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
92
Sudheer Shanka53947a32018-08-01 10:24:13 -070093static const std::string kEmptyString("");
94
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060095/* 512MiB is large enough for testing purposes */
96static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097
Jeff Sharkey36801cc2015-03-13 16:09:20 -070098static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080099static const unsigned int kMajorBlockExperimentalMin = 240;
100static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700102VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700103
Zim3623a212019-07-19 16:46:53 +0100104static void* symlinkPrimary(void* data) {
105 std::unique_ptr<std::pair<std::string, std::string>> linkInfo(
106 static_cast<std::pair<std::string, std::string>*>(data));
107 std::string* source = &linkInfo->first;
108 std::string* target = &linkInfo->second;
109
110 fs_prepare_dir(source->c_str(), 0755, AID_ROOT, AID_ROOT);
111 fs_prepare_dir(target->c_str(), 0755, AID_ROOT, AID_ROOT);
112 *target = *target + "/primary";
113
114 // Link source to target
115 LOG(DEBUG) << "Linking " << *source << " to " << *target;
116 Symlink(*source, *target);
117 return nullptr;
118}
119
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700120VolumeManager* VolumeManager::Instance() {
121 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -0700122 return sInstance;
123}
124
125VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800126 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600127 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +0900128 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700129 // For security reasons, assume that a secure keyguard is
130 // showing until we hear otherwise
131 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700132}
133
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700134VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800135
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600136int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600137 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700138 if (GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600139 if (access(kPathVirtualDisk, F_OK) != 0) {
140 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
141 }
142
143 if (mVirtualDisk == nullptr) {
144 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
145 LOG(ERROR) << "Failed to create virtual disk";
146 return -1;
147 }
148
149 struct stat buf;
150 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
151 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
152 return -1;
153 }
154
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700155 auto disk = new android::vold::Disk(
156 "virtual", buf.st_rdev, "virtual",
157 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600158 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700159 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600160 }
161 } else {
162 if (mVirtualDisk != nullptr) {
163 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700164 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600165
166 Loop::destroyByDevice(mVirtualDiskPath.c_str());
167 mVirtualDisk = nullptr;
168 }
169
170 if (access(kPathVirtualDisk, F_OK) == 0) {
171 unlink(kPathVirtualDisk);
172 }
173 }
174 return 0;
175}
176
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700177int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800178 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700179 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800180}
181
San Mehatf1b736b2009-10-10 17:22:08 -0700182int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600183 ATRACE_NAME("VolumeManager::start");
184
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700185 // Always start from a clean slate by unmounting everything in
186 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700187 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600189 Devmapper::destroyAll();
190 Loop::destroyAll();
191
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700192 // Assume that we always have an emulated volume on internal
193 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700194 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700195 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700196 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700197 mInternalEmulated->create();
198
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600199 // Consider creating a virtual disk
200 updateVirtualDisk();
201
San Mehatf1b736b2009-10-10 17:22:08 -0700202 return 0;
203}
204
205int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700206 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700207 mInternalEmulated->destroy();
208 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700209 return 0;
210}
211
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700212void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700213 std::lock_guard<std::mutex> lock(mLock);
214
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700215 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700216 LOG(DEBUG) << "----------------";
217 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700218 evt->dump();
219 }
San Mehatf1b736b2009-10-10 17:22:08 -0700220
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700221 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
222 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223
224 if (devType != "disk") return;
225
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600226 int major = std::stoi(evt->findParam("MAJOR"));
227 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700228 dev_t device = makedev(major, minor);
229
230 switch (evt->getAction()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700231 case NetlinkEvent::Action::kAdd: {
232 for (const auto& source : mDiskSources) {
233 if (source->matches(eventPath)) {
234 // For now, assume that MMC and virtio-blk (the latter is
235 // emulator-specific; see Disk.cpp for details) devices are SD,
236 // and that everything else is USB
237 int flags = source->getFlags();
238 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
239 major >= (int)kMajorBlockExperimentalMin &&
240 major <= (int)kMajorBlockExperimentalMax)) {
241 flags |= android::vold::Disk::Flags::kSd;
242 } else {
243 flags |= android::vold::Disk::Flags::kUsb;
244 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700245
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700246 auto disk =
247 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
248 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
249 break;
250 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700251 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700252 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700253 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700254 case NetlinkEvent::Action::kChange: {
255 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
256 handleDiskChanged(device);
257 break;
258 }
259 case NetlinkEvent::Action::kRemove: {
260 handleDiskRemoved(device);
261 break;
262 }
263 default: {
264 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
265 break;
266 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700267 }
268}
269
Jeff Sharkey401b2602017-12-14 22:15:20 -0700270void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
271 // For security reasons, if secure keyguard is showing, wait
272 // until the user unlocks the device to actually touch it
273 if (mSecureKeyguardShowing) {
274 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700275 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700276 mPendingDisks.push_back(disk);
277 } else {
278 disk->create();
279 mDisks.push_back(disk);
280 }
281}
282
283void VolumeManager::handleDiskChanged(dev_t device) {
284 for (const auto& disk : mDisks) {
285 if (disk->getDevice() == device) {
286 disk->readMetadata();
287 disk->readPartitions();
288 }
289 }
290
291 // For security reasons, we ignore all pending disks, since
292 // we'll scan them once the device is unlocked
293}
294
295void VolumeManager::handleDiskRemoved(dev_t device) {
296 auto i = mDisks.begin();
297 while (i != mDisks.end()) {
298 if ((*i)->getDevice() == device) {
299 (*i)->destroy();
300 i = mDisks.erase(i);
301 } else {
302 ++i;
303 }
304 }
305 auto j = mPendingDisks.begin();
306 while (j != mPendingDisks.end()) {
307 if ((*j)->getDevice() == device) {
308 j = mPendingDisks.erase(j);
309 } else {
310 ++j;
311 }
312 }
313}
314
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700315void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800316 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700317 mDiskSources.push_back(diskSource);
318}
319
320std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
321 for (auto disk : mDisks) {
322 if (disk->getId() == id) {
323 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700324 }
325 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700326 return nullptr;
327}
San Mehatf1b736b2009-10-10 17:22:08 -0700328
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700329std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800330 // Vold could receive "mount" after "shutdown" command in the extreme case.
331 // If this happens, mInternalEmulated will equal nullptr and
332 // we need to deal with it in order to avoid null pointer crash.
333 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700334 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700335 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700336 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700337 auto vol = disk->findVolume(id);
338 if (vol != nullptr) {
339 return vol;
340 }
341 }
Risan8c9f3322018-10-29 08:52:56 +0900342 for (const auto& vol : mStubVolumes) {
343 if (vol->getId() == id) {
344 return vol;
345 }
346 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600347 for (const auto& vol : mObbVolumes) {
348 if (vol->getId() == id) {
349 return vol;
350 }
351 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700352 return nullptr;
353}
354
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800355void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
356 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700357 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700358 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700359 disk->listVolumes(type, list);
360 }
361}
362
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600363int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700364 std::string normalizedGuid;
365 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
366 LOG(WARNING) << "Invalid GUID " << partGuid;
367 return -1;
368 }
369
Paul Crowleyc6433a22017-10-24 14:54:43 -0700370 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700371 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
372 if (unlink(keyPath.c_str()) != 0) {
373 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700374 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700375 }
Eric Biggersa701c452018-10-23 13:06:55 -0700376 if (fscrypt_is_native()) {
377 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700378 success = false;
379 }
380 }
381 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700382}
383
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700384int VolumeManager::linkPrimary(userid_t userId) {
shafik5cf32b52019-09-25 13:56:01 +0100385 bool isFuse = GetBoolProperty(android::vold::kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +0100386
387 if (isFuse) {
388 // Here we have to touch /mnt/user/userid>/<volumeid> which was already mounted as part of
389 // the boot sequence, requiring waiting till a fuse handler is available. If we do this work
390 // in foreground we could hang the caller, i.e. system server, which needs to start the fuse
391 // handler. So do it in the background.
392 std::string source(
393 StringPrintf("/mnt/user/%d/%s/%d", userId, mPrimary->getId().c_str(), userId));
394 std::string target(StringPrintf("/mnt/user/%d/self", userId));
395
396 auto symlinkInfo = new std::pair<std::string, std::string>(source, target);
397 std::thread(symlinkPrimary, symlinkInfo).detach();
Zim17112362019-09-03 20:50:53 +0100398 return 0;
Zim3623a212019-07-19 16:46:53 +0100399 }
400
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700401 std::string source(mPrimary->getPath());
402 if (mPrimary->isEmulated()) {
403 source = StringPrintf("%s/%d", source.c_str(), userId);
404 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
405 }
406
407 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700408 LOG(DEBUG) << "Linking " << source << " to " << target;
Sudheer Shanka023b5392019-02-06 12:39:19 -0800409 Symlink(source, target);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700410 return 0;
411}
412
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700413int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
414 mAddedUsers[userId] = userSerialNumber;
415 return 0;
416}
417
418int VolumeManager::onUserRemoved(userid_t userId) {
419 mAddedUsers.erase(userId);
420 return 0;
421}
422
Sudheer Shankaebce4cc2019-04-29 10:46:35 -0700423int VolumeManager::onUserStarted(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700424 LOG(VERBOSE) << "onUserStarted: " << userId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700425 // Note that sometimes the system will spin up processes from Zygote
426 // before actually starting the user, so we're okay if Zygote
427 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600428 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700429 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
430
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700431 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700432 if (mPrimary) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700433 linkPrimary(userId);
434 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700435 return 0;
436}
437
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700438int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700439 LOG(VERBOSE) << "onUserStopped: " << userId;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700440 mStartedUsers.erase(userId);
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700441 return 0;
442}
443
Jeff Sharkey401b2602017-12-14 22:15:20 -0700444int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
445 mSecureKeyguardShowing = isShowing;
446 if (!mSecureKeyguardShowing) {
447 // Now that secure keyguard has been dismissed, process
448 // any pending disks
449 for (const auto& disk : mPendingDisks) {
450 disk->create();
451 mDisks.push_back(disk);
452 }
453 mPendingDisks.clear();
454 }
455 return 0;
456}
457
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700458int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
459 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700460 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700461 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700462 }
463 return 0;
464}
465
Sudheer Shanka817b9112018-12-13 17:40:28 -0800466int VolumeManager::remountUid(uid_t uid, int32_t mountMode) {
shafik5cf32b52019-09-25 13:56:01 +0100467 if (GetBoolProperty(android::vold::kPropFuseSnapshot, false)) {
Zim3623a212019-07-19 16:46:53 +0100468 // TODO(135341433): Implement fuse specific logic.
469 return 0;
470 }
Sudheer Shanka817b9112018-12-13 17:40:28 -0800471 std::string mode;
472 switch (mountMode) {
473 case VoldNativeService::REMOUNT_MODE_NONE:
474 mode = "none";
475 break;
476 case VoldNativeService::REMOUNT_MODE_DEFAULT:
477 mode = "default";
478 break;
479 case VoldNativeService::REMOUNT_MODE_READ:
480 mode = "read";
481 break;
482 case VoldNativeService::REMOUNT_MODE_WRITE:
Sudheer Shankaa05ea742019-04-12 13:55:28 -0700483 case VoldNativeService::REMOUNT_MODE_LEGACY:
484 case VoldNativeService::REMOUNT_MODE_INSTALLER:
Sudheer Shanka817b9112018-12-13 17:40:28 -0800485 mode = "write";
486 break;
Sudheer Shanka36bdf7a2019-04-18 15:18:30 -0700487 case VoldNativeService::REMOUNT_MODE_FULL:
488 mode = "full";
489 break;
Zim981222f2019-09-09 10:24:44 +0100490 case VoldNativeService::REMOUNT_MODE_PASS_THROUGH:
491 mode = "pass_through";
492 break;
Sudheer Shanka817b9112018-12-13 17:40:28 -0800493 default:
494 PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode);
495 return -1;
496 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700497 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
498
499 DIR* dir;
500 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600501 std::string rootName;
502 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700503 int pidFd;
504 int nsFd;
505 struct stat sb;
506 pid_t child;
507
Jiyong Park8d21c922019-01-04 13:35:25 +0900508 static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false);
509
Jeff Sharkey66270a22015-06-24 11:49:24 -0700510 if (!(dir = opendir("/proc"))) {
511 PLOG(ERROR) << "Failed to opendir";
512 return -1;
513 }
514
515 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -0600516 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
517 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700518 closedir(dir);
519 return -1;
520 }
521
522 // Poke through all running PIDs look for apps running as UID
523 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -0700524 pid_t pid;
525 if (de->d_type != DT_DIR) continue;
526 if (!android::base::ParseInt(de->d_name, &pid)) continue;
527
Jeff Sharkey66270a22015-06-24 11:49:24 -0700528 pidFd = -1;
529 nsFd = -1;
530
531 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
532 if (pidFd < 0) {
533 goto next;
534 }
535 if (fstat(pidFd, &sb) != 0) {
536 PLOG(WARNING) << "Failed to stat " << de->d_name;
537 goto next;
538 }
539 if (sb.st_uid != uid) {
540 goto next;
541 }
542
543 // Matches so far, but refuse to touch if in root namespace
544 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600545 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700546 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
547 goto next;
548 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600549 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700550 LOG(WARNING) << "Skipping due to root namespace";
551 goto next;
552 }
553
Jiyong Park8d21c922019-01-04 13:35:25 +0900554 if (apexUpdatable) {
555 std::string exeName;
556 // When ro.apex.bionic_updatable is set to true,
557 // some early native processes have mount namespaces that are different
558 // from that of the init. Therefore, above check can't filter them out.
559 // Since the propagation type of / is 'shared', unmounting /storage
560 // for the early native processes affects other processes including
561 // init. Filter out such processes by skipping if a process is a
562 // non-Java process whose UID is < AID_APP_START. (The UID condition
563 // is required to not filter out child processes spawned by apps.)
564 if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) {
565 PLOG(WARNING) << "Failed to read exe name for " << de->d_name;
566 goto next;
567 }
568 if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) {
569 LOG(WARNING) << "Skipping due to native system process";
570 goto next;
571 }
572 }
573
Jeff Sharkey66270a22015-06-24 11:49:24 -0700574 // We purposefully leave the namespace open across the fork
Nick Kraleviche7e89ac2019-03-29 16:03:51 -0700575 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
576 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700577 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700578 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700579 goto next;
580 }
581
582 if (!(child = fork())) {
583 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700584 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700585 _exit(1);
586 }
587
Sudheer Shanka99d304a2018-09-27 14:53:51 -0700588 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700589
590 std::string storageSource;
591 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700592 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700593 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700594 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700595 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700596 storageSource = "/mnt/runtime/write";
Sudheer Shanka8f2ff802019-05-30 16:40:20 -0700597 } else if (mode == "full") {
598 storageSource = "/mnt/runtime/full";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700599 } else {
600 // Sane default of no storage visible
601 _exit(0);
602 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700603 if (TEMP_FAILURE_RETRY(
604 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
605 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700606 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700607 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700608 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
609 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +0900610 _exit(1);
611 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700612
613 // Mount user-specific symlink helper into place
614 userid_t user_id = multiuser_get_user_id(uid);
615 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700616 if (TEMP_FAILURE_RETRY(
617 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
618 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700619 _exit(1);
620 }
621
Jeff Sharkey66270a22015-06-24 11:49:24 -0700622 _exit(0);
623 }
624
625 if (child == -1) {
626 PLOG(ERROR) << "Failed to fork";
627 goto next;
628 } else {
629 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
630 }
631
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700632 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700633 close(nsFd);
634 close(pidFd);
635 }
636 closedir(dir);
637 return 0;
638}
639
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700640int VolumeManager::reset() {
641 // Tear down all existing disks/volumes and start from a blank slate so
642 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800643 if (mInternalEmulated != nullptr) {
644 mInternalEmulated->destroy();
645 mInternalEmulated->create();
646 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700647 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700648 disk->destroy();
649 disk->create();
650 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600651 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700652 mAddedUsers.clear();
Sudheer Shanka023b5392019-02-06 12:39:19 -0800653 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700654 return 0;
655}
656
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700657// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700658int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700659 if (mInternalEmulated == nullptr) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700660 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700661 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700662 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700663 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700664 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700665 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700666 disk->destroy();
667 }
Risan8c9f3322018-10-29 08:52:56 +0900668 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700669 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700670 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700671 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700672 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700673}
674
Jeff Sharkey9c484982015-03-31 10:35:33 -0700675int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700676 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600677 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700678
Jeff Sharkey9c484982015-03-31 10:35:33 -0700679 // First, try gracefully unmounting all known devices
680 if (mInternalEmulated != nullptr) {
681 mInternalEmulated->unmount();
682 }
Risan8c9f3322018-10-29 08:52:56 +0900683 for (const auto& stub : mStubVolumes) {
684 stub->unmount();
685 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700686 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700687 disk->unmountAll();
688 }
689
690 // Worst case we might have some stale mounts lurking around, so
691 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +0800692 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700693 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600694 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700695 return -errno;
696 }
697
698 // Some volumes can be stacked on each other, so force unmount in
699 // reverse order to give us the best chance of success.
700 std::list<std::string> toUnmount;
701 mntent* mentry;
702 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600703 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700704 if ((StartsWith(test, "/mnt/") &&
705#ifdef __ANDROID_DEBUGGABLE__
706 !StartsWith(test, "/mnt/scratch") &&
707#endif
708 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
709 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600710 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700711 }
712 }
713 endmntent(fp);
714
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700715 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600716 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700717 android::vold::ForceUnmount(path);
718 }
719
720 return 0;
721}
722
Jeff Sharkey3472e522017-10-06 18:02:53 -0600723int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700724 // Only offer to create directories for paths managed by vold
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700725 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700726 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -0600727 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700728 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600729 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700730 return -EINVAL;
731 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700732}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600733
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600734int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700735 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600736 int id = mNextObbId++;
737
738 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700739 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600740 vol->create();
741
742 mObbVolumes.push_back(vol);
743 *outVolId = vol->getId();
744 return android::OK;
745}
746
747int VolumeManager::destroyObb(const std::string& volId) {
748 auto i = mObbVolumes.begin();
749 while (i != mObbVolumes.end()) {
750 if ((*i)->getId() == volId) {
751 (*i)->destroy();
752 i = mObbVolumes.erase(i);
753 } else {
754 ++i;
755 }
756 }
757 return android::OK;
758}
759
Risan8c9f3322018-10-29 08:52:56 +0900760int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
761 const std::string& fsType, const std::string& fsUuid,
762 const std::string& fsLabel, std::string* outVolId) {
763 int id = mNextStubVolumeId++;
764 auto vol = std::shared_ptr<android::vold::VolumeBase>(
765 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
766 vol->create();
767
768 mStubVolumes.push_back(vol);
769 *outVolId = vol->getId();
770 return android::OK;
771}
772
773int VolumeManager::destroyStubVolume(const std::string& volId) {
774 auto i = mStubVolumes.begin();
775 while (i != mStubVolumes.end()) {
776 if ((*i)->getId() == volId) {
777 (*i)->destroy();
778 i = mStubVolumes.erase(i);
779 } else {
780 ++i;
781 }
782 }
783 return android::OK;
784}
785
Risan8f6198d2018-10-26 20:56:45 -0600786int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -0600787 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600788}
789
Risan8f6198d2018-10-26 20:56:45 -0600790int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -0600791 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -0600792}
793
794int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -0600795 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600796}