blob: 9f151ea034a6769035e08d169a64740dda44e1d9 [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;
Alistair Delvad3c230b2020-05-14 16:35:03 -070081using android::vold::IsVirtioBlkDevice;
Sudheer Shanka023b5392019-02-06 12:39:19 -080082using android::vold::Symlink;
83using android::vold::Unlink;
84using android::vold::UnmountTree;
Sudheer Shanka03992e32018-12-12 12:43:38 -080085using android::vold::VoldNativeService;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070086
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060087static const char* kPathUserMount = "/mnt/user";
88static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
89
90static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
91
Sudheer Shanka53947a32018-08-01 10:24:13 -070092static const std::string kEmptyString("");
93
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060094/* 512MiB is large enough for testing purposes */
95static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097static const unsigned int kMajorBlockMmc = 179;
98
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070099VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700100
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700101VolumeManager* VolumeManager::Instance() {
102 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -0700103 return sInstance;
104}
105
106VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800107 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600108 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +0900109 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700110 // For security reasons, assume that a secure keyguard is
111 // showing until we hear otherwise
112 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700113}
114
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700115VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800116
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600117int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600118 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700119 if (GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600120 if (access(kPathVirtualDisk, F_OK) != 0) {
121 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
122 }
123
124 if (mVirtualDisk == nullptr) {
125 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
126 LOG(ERROR) << "Failed to create virtual disk";
127 return -1;
128 }
129
130 struct stat buf;
131 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
132 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
133 return -1;
134 }
135
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700136 auto disk = new android::vold::Disk(
137 "virtual", buf.st_rdev, "virtual",
138 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600139 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700140 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600141 }
142 } else {
143 if (mVirtualDisk != nullptr) {
144 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700145 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600146
147 Loop::destroyByDevice(mVirtualDiskPath.c_str());
148 mVirtualDisk = nullptr;
149 }
150
151 if (access(kPathVirtualDisk, F_OK) == 0) {
152 unlink(kPathVirtualDisk);
153 }
154 }
155 return 0;
156}
157
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700158int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800159 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700160 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800161}
162
San Mehatf1b736b2009-10-10 17:22:08 -0700163int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600164 ATRACE_NAME("VolumeManager::start");
165
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700166 // Always start from a clean slate by unmounting everything in
167 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700168 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700169
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600170 Devmapper::destroyAll();
171 Loop::destroyAll();
172
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700173 // Assume that we always have an emulated volume on internal
174 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700175 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700176 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700177 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700178 mInternalEmulated->create();
179
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600180 // Consider creating a virtual disk
181 updateVirtualDisk();
182
San Mehatf1b736b2009-10-10 17:22:08 -0700183 return 0;
184}
185
186int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700187 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188 mInternalEmulated->destroy();
189 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700190 return 0;
191}
192
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700193void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700194 std::lock_guard<std::mutex> lock(mLock);
195
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700196 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700197 LOG(DEBUG) << "----------------";
198 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700199 evt->dump();
200 }
San Mehatf1b736b2009-10-10 17:22:08 -0700201
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700202 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
203 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700204
205 if (devType != "disk") return;
206
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600207 int major = std::stoi(evt->findParam("MAJOR"));
208 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700209 dev_t device = makedev(major, minor);
210
211 switch (evt->getAction()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700212 case NetlinkEvent::Action::kAdd: {
213 for (const auto& source : mDiskSources) {
214 if (source->matches(eventPath)) {
215 // For now, assume that MMC and virtio-blk (the latter is
Alistair Delvad3c230b2020-05-14 16:35:03 -0700216 // specific to virtual platforms; see Utils.cpp for details)
217 // devices are SD, and that everything else is USB
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700218 int flags = source->getFlags();
Alistair Delvad3c230b2020-05-14 16:35:03 -0700219 if (major == kMajorBlockMmc || IsVirtioBlkDevice(major)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700220 flags |= android::vold::Disk::Flags::kSd;
221 } else {
222 flags |= android::vold::Disk::Flags::kUsb;
223 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700224
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700225 auto disk =
226 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
227 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
228 break;
229 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700231 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700233 case NetlinkEvent::Action::kChange: {
234 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
235 handleDiskChanged(device);
236 break;
237 }
238 case NetlinkEvent::Action::kRemove: {
239 handleDiskRemoved(device);
240 break;
241 }
242 default: {
243 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
244 break;
245 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700246 }
247}
248
Jeff Sharkey401b2602017-12-14 22:15:20 -0700249void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
250 // For security reasons, if secure keyguard is showing, wait
251 // until the user unlocks the device to actually touch it
252 if (mSecureKeyguardShowing) {
253 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700254 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700255 mPendingDisks.push_back(disk);
256 } else {
257 disk->create();
258 mDisks.push_back(disk);
259 }
260}
261
262void VolumeManager::handleDiskChanged(dev_t device) {
263 for (const auto& disk : mDisks) {
264 if (disk->getDevice() == device) {
265 disk->readMetadata();
266 disk->readPartitions();
267 }
268 }
269
270 // For security reasons, we ignore all pending disks, since
271 // we'll scan them once the device is unlocked
272}
273
274void VolumeManager::handleDiskRemoved(dev_t device) {
275 auto i = mDisks.begin();
276 while (i != mDisks.end()) {
277 if ((*i)->getDevice() == device) {
278 (*i)->destroy();
279 i = mDisks.erase(i);
280 } else {
281 ++i;
282 }
283 }
284 auto j = mPendingDisks.begin();
285 while (j != mPendingDisks.end()) {
286 if ((*j)->getDevice() == device) {
287 j = mPendingDisks.erase(j);
288 } else {
289 ++j;
290 }
291 }
292}
293
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700294void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800295 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700296 mDiskSources.push_back(diskSource);
297}
298
299std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
300 for (auto disk : mDisks) {
301 if (disk->getId() == id) {
302 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700303 }
304 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700305 return nullptr;
306}
San Mehatf1b736b2009-10-10 17:22:08 -0700307
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700308std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800309 // Vold could receive "mount" after "shutdown" command in the extreme case.
310 // If this happens, mInternalEmulated will equal nullptr and
311 // we need to deal with it in order to avoid null pointer crash.
312 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700313 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700314 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700315 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700316 auto vol = disk->findVolume(id);
317 if (vol != nullptr) {
318 return vol;
319 }
320 }
Risan8c9f3322018-10-29 08:52:56 +0900321 for (const auto& vol : mStubVolumes) {
322 if (vol->getId() == id) {
323 return vol;
324 }
325 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600326 for (const auto& vol : mObbVolumes) {
327 if (vol->getId() == id) {
328 return vol;
329 }
330 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700331 return nullptr;
332}
333
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800334void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
335 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700336 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700337 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700338 disk->listVolumes(type, list);
339 }
340}
341
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600342int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700343 std::string normalizedGuid;
344 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
345 LOG(WARNING) << "Invalid GUID " << partGuid;
346 return -1;
347 }
348
Paul Crowleyc6433a22017-10-24 14:54:43 -0700349 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700350 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
351 if (unlink(keyPath.c_str()) != 0) {
352 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700353 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700354 }
Eric Biggersa701c452018-10-23 13:06:55 -0700355 if (fscrypt_is_native()) {
356 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700357 success = false;
358 }
359 }
360 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700361}
362
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700363int VolumeManager::linkPrimary(userid_t userId) {
364 std::string source(mPrimary->getPath());
365 if (mPrimary->isEmulated()) {
366 source = StringPrintf("%s/%d", source.c_str(), userId);
367 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
368 }
369
370 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700371 LOG(DEBUG) << "Linking " << source << " to " << target;
Sudheer Shanka023b5392019-02-06 12:39:19 -0800372 Symlink(source, target);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700373 return 0;
374}
375
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700376int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
377 mAddedUsers[userId] = userSerialNumber;
378 return 0;
379}
380
381int VolumeManager::onUserRemoved(userid_t userId) {
382 mAddedUsers.erase(userId);
383 return 0;
384}
385
Sudheer Shankaebce4cc2019-04-29 10:46:35 -0700386int VolumeManager::onUserStarted(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700387 LOG(VERBOSE) << "onUserStarted: " << userId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700388 // Note that sometimes the system will spin up processes from Zygote
389 // before actually starting the user, so we're okay if Zygote
390 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600391 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700392 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
393
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700394 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700395 if (mPrimary) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700396 linkPrimary(userId);
397 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700398 return 0;
399}
400
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700401int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700402 LOG(VERBOSE) << "onUserStopped: " << userId;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700403 mStartedUsers.erase(userId);
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700404 return 0;
405}
406
Jeff Sharkey401b2602017-12-14 22:15:20 -0700407int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
408 mSecureKeyguardShowing = isShowing;
409 if (!mSecureKeyguardShowing) {
410 // Now that secure keyguard has been dismissed, process
411 // any pending disks
412 for (const auto& disk : mPendingDisks) {
413 disk->create();
414 mDisks.push_back(disk);
415 }
416 mPendingDisks.clear();
417 }
418 return 0;
419}
420
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700421int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
422 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700423 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700424 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700425 }
426 return 0;
427}
428
Sudheer Shanka817b9112018-12-13 17:40:28 -0800429int VolumeManager::remountUid(uid_t uid, int32_t mountMode) {
Sudheer Shanka817b9112018-12-13 17:40:28 -0800430 std::string mode;
431 switch (mountMode) {
432 case VoldNativeService::REMOUNT_MODE_NONE:
433 mode = "none";
434 break;
435 case VoldNativeService::REMOUNT_MODE_DEFAULT:
436 mode = "default";
437 break;
438 case VoldNativeService::REMOUNT_MODE_READ:
439 mode = "read";
440 break;
441 case VoldNativeService::REMOUNT_MODE_WRITE:
Sudheer Shankaa05ea742019-04-12 13:55:28 -0700442 case VoldNativeService::REMOUNT_MODE_LEGACY:
443 case VoldNativeService::REMOUNT_MODE_INSTALLER:
Sudheer Shanka817b9112018-12-13 17:40:28 -0800444 mode = "write";
445 break;
Sudheer Shanka36bdf7a2019-04-18 15:18:30 -0700446 case VoldNativeService::REMOUNT_MODE_FULL:
447 mode = "full";
448 break;
Sudheer Shanka817b9112018-12-13 17:40:28 -0800449 default:
450 PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode);
451 return -1;
452 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700453 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
454
455 DIR* dir;
456 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600457 std::string rootName;
458 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700459 int pidFd;
460 int nsFd;
461 struct stat sb;
462 pid_t child;
463
Jiyong Park8d21c922019-01-04 13:35:25 +0900464 static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false);
465
Jeff Sharkey66270a22015-06-24 11:49:24 -0700466 if (!(dir = opendir("/proc"))) {
467 PLOG(ERROR) << "Failed to opendir";
468 return -1;
469 }
470
471 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -0600472 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
473 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700474 closedir(dir);
475 return -1;
476 }
477
478 // Poke through all running PIDs look for apps running as UID
479 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -0700480 pid_t pid;
481 if (de->d_type != DT_DIR) continue;
482 if (!android::base::ParseInt(de->d_name, &pid)) continue;
483
Jeff Sharkey66270a22015-06-24 11:49:24 -0700484 pidFd = -1;
485 nsFd = -1;
486
487 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
488 if (pidFd < 0) {
489 goto next;
490 }
491 if (fstat(pidFd, &sb) != 0) {
492 PLOG(WARNING) << "Failed to stat " << de->d_name;
493 goto next;
494 }
495 if (sb.st_uid != uid) {
496 goto next;
497 }
498
499 // Matches so far, but refuse to touch if in root namespace
500 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600501 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700502 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
503 goto next;
504 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600505 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700506 LOG(WARNING) << "Skipping due to root namespace";
507 goto next;
508 }
509
Jiyong Park8d21c922019-01-04 13:35:25 +0900510 if (apexUpdatable) {
511 std::string exeName;
512 // When ro.apex.bionic_updatable is set to true,
513 // some early native processes have mount namespaces that are different
514 // from that of the init. Therefore, above check can't filter them out.
515 // Since the propagation type of / is 'shared', unmounting /storage
516 // for the early native processes affects other processes including
517 // init. Filter out such processes by skipping if a process is a
518 // non-Java process whose UID is < AID_APP_START. (The UID condition
519 // is required to not filter out child processes spawned by apps.)
520 if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) {
521 PLOG(WARNING) << "Failed to read exe name for " << de->d_name;
522 goto next;
523 }
524 if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) {
525 LOG(WARNING) << "Skipping due to native system process";
526 goto next;
527 }
528 }
529
Jeff Sharkey66270a22015-06-24 11:49:24 -0700530 // We purposefully leave the namespace open across the fork
Nick Kraleviche7e89ac2019-03-29 16:03:51 -0700531 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
532 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700533 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700534 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700535 goto next;
536 }
537
538 if (!(child = fork())) {
539 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700540 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700541 _exit(1);
542 }
543
Sudheer Shanka99d304a2018-09-27 14:53:51 -0700544 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700545
546 std::string storageSource;
547 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700548 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700549 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700550 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700551 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700552 storageSource = "/mnt/runtime/write";
Sudheer Shanka8f2ff802019-05-30 16:40:20 -0700553 } else if (mode == "full") {
554 storageSource = "/mnt/runtime/full";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700555 } else {
556 // Sane default of no storage visible
557 _exit(0);
558 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700559 if (TEMP_FAILURE_RETRY(
560 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
561 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700562 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700563 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700564 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
565 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +0900566 _exit(1);
567 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700568
569 // Mount user-specific symlink helper into place
570 userid_t user_id = multiuser_get_user_id(uid);
571 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700572 if (TEMP_FAILURE_RETRY(
573 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
574 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700575 _exit(1);
576 }
577
Jeff Sharkey66270a22015-06-24 11:49:24 -0700578 _exit(0);
579 }
580
581 if (child == -1) {
582 PLOG(ERROR) << "Failed to fork";
583 goto next;
584 } else {
585 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
586 }
587
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700588 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700589 close(nsFd);
590 close(pidFd);
591 }
592 closedir(dir);
593 return 0;
594}
595
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700596int VolumeManager::reset() {
597 // Tear down all existing disks/volumes and start from a blank slate so
598 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800599 if (mInternalEmulated != nullptr) {
600 mInternalEmulated->destroy();
601 mInternalEmulated->create();
602 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700603 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700604 disk->destroy();
605 disk->create();
606 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600607 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700608 mAddedUsers.clear();
Sudheer Shanka023b5392019-02-06 12:39:19 -0800609 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700610 return 0;
611}
612
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700613// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700614int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700615 if (mInternalEmulated == nullptr) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700616 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700617 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700618 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700619 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700620 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700621 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700622 disk->destroy();
623 }
Risan8c9f3322018-10-29 08:52:56 +0900624 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700625 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700626 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700627 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700628 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700629}
630
Jeff Sharkey9c484982015-03-31 10:35:33 -0700631int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700632 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600633 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700634
Jeff Sharkey9c484982015-03-31 10:35:33 -0700635 // First, try gracefully unmounting all known devices
636 if (mInternalEmulated != nullptr) {
637 mInternalEmulated->unmount();
638 }
Risan8c9f3322018-10-29 08:52:56 +0900639 for (const auto& stub : mStubVolumes) {
640 stub->unmount();
641 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700642 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700643 disk->unmountAll();
644 }
645
646 // Worst case we might have some stale mounts lurking around, so
647 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +0800648 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700649 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600650 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700651 return -errno;
652 }
653
654 // Some volumes can be stacked on each other, so force unmount in
655 // reverse order to give us the best chance of success.
656 std::list<std::string> toUnmount;
657 mntent* mentry;
658 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600659 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700660 if ((StartsWith(test, "/mnt/") &&
661#ifdef __ANDROID_DEBUGGABLE__
662 !StartsWith(test, "/mnt/scratch") &&
663#endif
664 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
665 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600666 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700667 }
668 }
669 endmntent(fp);
670
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700671 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600672 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700673 android::vold::ForceUnmount(path);
674 }
675
676 return 0;
677}
678
Jeff Sharkey3472e522017-10-06 18:02:53 -0600679int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700680 // Only offer to create directories for paths managed by vold
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700681 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700682 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -0600683 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700684 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600685 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700686 return -EINVAL;
687 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700688}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600689
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600690int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700691 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600692 int id = mNextObbId++;
693
694 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700695 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600696 vol->create();
697
698 mObbVolumes.push_back(vol);
699 *outVolId = vol->getId();
700 return android::OK;
701}
702
703int VolumeManager::destroyObb(const std::string& volId) {
704 auto i = mObbVolumes.begin();
705 while (i != mObbVolumes.end()) {
706 if ((*i)->getId() == volId) {
707 (*i)->destroy();
708 i = mObbVolumes.erase(i);
709 } else {
710 ++i;
711 }
712 }
713 return android::OK;
714}
715
Risan8c9f3322018-10-29 08:52:56 +0900716int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
717 const std::string& fsType, const std::string& fsUuid,
718 const std::string& fsLabel, std::string* outVolId) {
719 int id = mNextStubVolumeId++;
720 auto vol = std::shared_ptr<android::vold::VolumeBase>(
721 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
722 vol->create();
723
724 mStubVolumes.push_back(vol);
725 *outVolId = vol->getId();
726 return android::OK;
727}
728
729int VolumeManager::destroyStubVolume(const std::string& volId) {
730 auto i = mStubVolumes.begin();
731 while (i != mStubVolumes.end()) {
732 if ((*i)->getId() == volId) {
733 (*i)->destroy();
734 i = mStubVolumes.erase(i);
735 } else {
736 ++i;
737 }
738 }
739 return android::OK;
740}
741
Risan8f6198d2018-10-26 20:56:45 -0600742int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -0600743 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600744}
745
Risan8f6198d2018-10-26 20:56:45 -0600746int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -0600747 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -0600748}
749
750int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -0600751 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600752}