blob: 51eec8aacc4bb9ad706d345b90560b6627728d56 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Yabin Cuid1104f72015-01-02 13:28:28 -080019#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080021#include <fcntl.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080022#include <mntent.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/ioctl.h>
27#include <sys/mount.h>
San Mehata19b2502010-01-06 10:33:53 -080028#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070029#include <sys/sysmacros.h>
Paul Crowley8915d622018-09-18 15:14:18 -070030#include <sys/types.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070031#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080032#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080033
San Mehata2677e42009-12-13 10:40:18 -080034#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070035
Jiyong Park8d21c922019-01-04 13:35:25 +090036#include <ApexProperties.sysprop.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080037#include <android-base/logging.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070038#include <android-base/parseint.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060039#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080040#include <android-base/stringprintf.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070041#include <android-base/strings.h>
42
Jeff Sharkey71ebe152013-09-17 17:24:38 -070043#include <cutils/fs.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060044#include <utils/Trace.h>
San Mehatf1b736b2009-10-10 17:22:08 -070045
Robert Craigb9e3ba52014-02-04 10:53:00 -050046#include <selinux/android.h>
47
San Mehatfd7f5872009-10-12 11:32:47 -070048#include <sysutils/NetlinkEvent.h>
49
Kenny Root344ca102012-04-03 17:23:01 -070050#include <private/android_filesystem_config.h>
51
Eric Biggersa701c452018-10-23 13:06:55 -070052#include <fscrypt/fscrypt.h>
Paul Crowleyc6433a22017-10-24 14:54:43 -070053
Risanac02a482018-10-31 21:59:47 -060054#include "AppFuseUtil.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070055#include "Devmapper.h"
Eric Biggersa701c452018-10-23 13:06:55 -070056#include "FsCrypt.h"
San Mehata19b2502010-01-06 10:33:53 -080057#include "Loop.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070058#include "NetlinkManager.h"
59#include "Process.h"
60#include "Utils.h"
61#include "VoldUtil.h"
62#include "VolumeManager.h"
63#include "cryptfs.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070064#include "fs/Ext4.h"
65#include "fs/Vfat.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070066#include "model/EmulatedVolume.h"
67#include "model/ObbVolume.h"
Risan8c9f3322018-10-29 08:52:56 +090068#include "model/StubVolume.h"
San Mehat23969932010-01-09 07:08:06 -080069
Mark Salyzync4405e92018-09-20 10:09:27 -070070using android::base::StartsWith;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070071using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060072using android::base::unique_fd;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070073
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060074static const char* kPathUserMount = "/mnt/user";
75static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
76
77static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
78
79/* 512MiB is large enough for testing purposes */
80static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081
Jeff Sharkey36801cc2015-03-13 16:09:20 -070082static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080083static const unsigned int kMajorBlockExperimentalMin = 240;
84static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070085
Paul Crowley8915d622018-09-18 15:14:18 -070086VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -070087
Paul Crowley8915d622018-09-18 15:14:18 -070088VolumeManager* VolumeManager::Instance() {
89 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -070090 return sInstance;
91}
92
93VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -080094 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060095 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +090096 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -070097 // For security reasons, assume that a secure keyguard is
98 // showing until we hear otherwise
99 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700100}
101
Paul Crowley8915d622018-09-18 15:14:18 -0700102VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800103
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600104int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600105 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Jeff Sharkey3472e522017-10-06 18:02:53 -0600106 if (android::base::GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600107 if (access(kPathVirtualDisk, F_OK) != 0) {
108 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
109 }
110
111 if (mVirtualDisk == nullptr) {
112 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
113 LOG(ERROR) << "Failed to create virtual disk";
114 return -1;
115 }
116
117 struct stat buf;
118 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
119 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
120 return -1;
121 }
122
Paul Crowley8915d622018-09-18 15:14:18 -0700123 auto disk = new android::vold::Disk(
124 "virtual", buf.st_rdev, "virtual",
125 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600126 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700127 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600128 }
129 } else {
130 if (mVirtualDisk != nullptr) {
131 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700132 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600133
134 Loop::destroyByDevice(mVirtualDiskPath.c_str());
135 mVirtualDisk = nullptr;
136 }
137
138 if (access(kPathVirtualDisk, F_OK) == 0) {
139 unlink(kPathVirtualDisk);
140 }
141 }
142 return 0;
143}
144
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700145int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800146 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700147 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800148}
149
San Mehatf1b736b2009-10-10 17:22:08 -0700150int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600151 ATRACE_NAME("VolumeManager::start");
152
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700153 // Always start from a clean slate by unmounting everything in
154 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700155 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700156
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600157 Devmapper::destroyAll();
158 Loop::destroyAll();
159
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700160 // Assume that we always have an emulated volume on internal
161 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700162 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700163 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowley8915d622018-09-18 15:14:18 -0700164 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700165 mInternalEmulated->create();
166
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600167 // Consider creating a virtual disk
168 updateVirtualDisk();
169
San Mehatf1b736b2009-10-10 17:22:08 -0700170 return 0;
171}
172
173int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700174 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175 mInternalEmulated->destroy();
176 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700177 return 0;
178}
179
Paul Crowley8915d622018-09-18 15:14:18 -0700180void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700181 std::lock_guard<std::mutex> lock(mLock);
182
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700183 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700184 LOG(DEBUG) << "----------------";
185 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700186 evt->dump();
187 }
San Mehatf1b736b2009-10-10 17:22:08 -0700188
Paul Crowley8915d622018-09-18 15:14:18 -0700189 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
190 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700191
192 if (devType != "disk") return;
193
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600194 int major = std::stoi(evt->findParam("MAJOR"));
195 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700196 dev_t device = makedev(major, minor);
197
198 switch (evt->getAction()) {
Paul Crowley8915d622018-09-18 15:14:18 -0700199 case NetlinkEvent::Action::kAdd: {
200 for (const auto& source : mDiskSources) {
201 if (source->matches(eventPath)) {
202 // For now, assume that MMC and virtio-blk (the latter is
203 // emulator-specific; see Disk.cpp for details) devices are SD,
204 // and that everything else is USB
205 int flags = source->getFlags();
206 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
207 major >= (int)kMajorBlockExperimentalMin &&
208 major <= (int)kMajorBlockExperimentalMax)) {
209 flags |= android::vold::Disk::Flags::kSd;
210 } else {
211 flags |= android::vold::Disk::Flags::kUsb;
212 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700213
Paul Crowley8915d622018-09-18 15:14:18 -0700214 auto disk =
215 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
216 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
217 break;
218 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700219 }
Paul Crowley8915d622018-09-18 15:14:18 -0700220 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221 }
Paul Crowley8915d622018-09-18 15:14:18 -0700222 case NetlinkEvent::Action::kChange: {
223 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
224 handleDiskChanged(device);
225 break;
226 }
227 case NetlinkEvent::Action::kRemove: {
228 handleDiskRemoved(device);
229 break;
230 }
231 default: {
232 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
233 break;
234 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700235 }
236}
237
Jeff Sharkey401b2602017-12-14 22:15:20 -0700238void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
239 // For security reasons, if secure keyguard is showing, wait
240 // until the user unlocks the device to actually touch it
241 if (mSecureKeyguardShowing) {
242 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowley8915d622018-09-18 15:14:18 -0700243 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700244 mPendingDisks.push_back(disk);
245 } else {
246 disk->create();
247 mDisks.push_back(disk);
248 }
249}
250
251void VolumeManager::handleDiskChanged(dev_t device) {
252 for (const auto& disk : mDisks) {
253 if (disk->getDevice() == device) {
254 disk->readMetadata();
255 disk->readPartitions();
256 }
257 }
258
259 // For security reasons, we ignore all pending disks, since
260 // we'll scan them once the device is unlocked
261}
262
263void VolumeManager::handleDiskRemoved(dev_t device) {
264 auto i = mDisks.begin();
265 while (i != mDisks.end()) {
266 if ((*i)->getDevice() == device) {
267 (*i)->destroy();
268 i = mDisks.erase(i);
269 } else {
270 ++i;
271 }
272 }
273 auto j = mPendingDisks.begin();
274 while (j != mPendingDisks.end()) {
275 if ((*j)->getDevice() == device) {
276 j = mPendingDisks.erase(j);
277 } else {
278 ++j;
279 }
280 }
281}
282
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700283void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800284 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700285 mDiskSources.push_back(diskSource);
286}
287
288std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
289 for (auto disk : mDisks) {
290 if (disk->getId() == id) {
291 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700292 }
293 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700294 return nullptr;
295}
San Mehatf1b736b2009-10-10 17:22:08 -0700296
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700297std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800298 // Vold could receive "mount" after "shutdown" command in the extreme case.
299 // If this happens, mInternalEmulated will equal nullptr and
300 // we need to deal with it in order to avoid null pointer crash.
301 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700302 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700303 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700304 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700305 auto vol = disk->findVolume(id);
306 if (vol != nullptr) {
307 return vol;
308 }
309 }
Risan8c9f3322018-10-29 08:52:56 +0900310 for (const auto& vol : mStubVolumes) {
311 if (vol->getId() == id) {
312 return vol;
313 }
314 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600315 for (const auto& vol : mObbVolumes) {
316 if (vol->getId() == id) {
317 return vol;
318 }
319 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700320 return nullptr;
321}
322
Greg Kaiseref9abab2018-12-18 08:42:08 -0800323void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
324 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700325 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700326 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700327 disk->listVolumes(type, list);
328 }
329}
330
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600331int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700332 std::string normalizedGuid;
333 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
334 LOG(WARNING) << "Invalid GUID " << partGuid;
335 return -1;
336 }
337
Paul Crowleyc6433a22017-10-24 14:54:43 -0700338 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700339 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
340 if (unlink(keyPath.c_str()) != 0) {
341 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700342 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700343 }
Eric Biggersa701c452018-10-23 13:06:55 -0700344 if (fscrypt_is_native()) {
345 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700346 success = false;
347 }
348 }
349 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700350}
351
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700352int VolumeManager::linkPrimary(userid_t userId) {
353 std::string source(mPrimary->getPath());
354 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
355 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700356 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700357 }
358
359 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
360 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
361 if (errno != ENOENT) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600362 PLOG(WARNING) << "Failed to unlink " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700363 }
364 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700365 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700366 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600367 PLOG(WARNING) << "Failed to link";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700368 return -errno;
369 }
370 return 0;
371}
372
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700373int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
374 mAddedUsers[userId] = userSerialNumber;
375 return 0;
376}
377
378int VolumeManager::onUserRemoved(userid_t userId) {
379 mAddedUsers.erase(userId);
380 return 0;
381}
382
383int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700384 // Note that sometimes the system will spin up processes from Zygote
385 // before actually starting the user, so we're okay if Zygote
386 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600387 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700388 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
389
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700390 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700391 if (mPrimary) {
392 linkPrimary(userId);
393 }
394 return 0;
395}
396
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700397int VolumeManager::onUserStopped(userid_t userId) {
398 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700399 return 0;
400}
401
Jeff Sharkey401b2602017-12-14 22:15:20 -0700402int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
403 mSecureKeyguardShowing = isShowing;
404 if (!mSecureKeyguardShowing) {
405 // Now that secure keyguard has been dismissed, process
406 // any pending disks
407 for (const auto& disk : mPendingDisks) {
408 disk->create();
409 mDisks.push_back(disk);
410 }
411 mPendingDisks.clear();
412 }
413 return 0;
414}
415
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700416int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
417 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700418 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700419 linkPrimary(userId);
420 }
421 return 0;
422}
423
Jeff Sharkey66270a22015-06-24 11:49:24 -0700424int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
425 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
426
427 DIR* dir;
428 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600429 std::string rootName;
430 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700431 int pidFd;
432 int nsFd;
433 struct stat sb;
434 pid_t child;
435
Jiyong Park8d21c922019-01-04 13:35:25 +0900436 static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false);
437
Jeff Sharkey66270a22015-06-24 11:49:24 -0700438 if (!(dir = opendir("/proc"))) {
439 PLOG(ERROR) << "Failed to opendir";
440 return -1;
441 }
442
443 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -0600444 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
445 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700446 closedir(dir);
447 return -1;
448 }
449
450 // Poke through all running PIDs look for apps running as UID
451 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -0700452 pid_t pid;
453 if (de->d_type != DT_DIR) continue;
454 if (!android::base::ParseInt(de->d_name, &pid)) continue;
455
Jeff Sharkey66270a22015-06-24 11:49:24 -0700456 pidFd = -1;
457 nsFd = -1;
458
459 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
460 if (pidFd < 0) {
461 goto next;
462 }
463 if (fstat(pidFd, &sb) != 0) {
464 PLOG(WARNING) << "Failed to stat " << de->d_name;
465 goto next;
466 }
467 if (sb.st_uid != uid) {
468 goto next;
469 }
470
471 // Matches so far, but refuse to touch if in root namespace
472 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600473 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700474 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
475 goto next;
476 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600477 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700478 LOG(WARNING) << "Skipping due to root namespace";
479 goto next;
480 }
481
Jiyong Park8d21c922019-01-04 13:35:25 +0900482 if (apexUpdatable) {
483 std::string exeName;
484 // When ro.apex.bionic_updatable is set to true,
485 // some early native processes have mount namespaces that are different
486 // from that of the init. Therefore, above check can't filter them out.
487 // Since the propagation type of / is 'shared', unmounting /storage
488 // for the early native processes affects other processes including
489 // init. Filter out such processes by skipping if a process is a
490 // non-Java process whose UID is < AID_APP_START. (The UID condition
491 // is required to not filter out child processes spawned by apps.)
492 if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) {
493 PLOG(WARNING) << "Failed to read exe name for " << de->d_name;
494 goto next;
495 }
496 if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) {
497 LOG(WARNING) << "Skipping due to native system process";
498 goto next;
499 }
500 }
501
Jeff Sharkey66270a22015-06-24 11:49:24 -0700502 // We purposefully leave the namespace open across the fork
Paul Crowley8915d622018-09-18 15:14:18 -0700503 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700504 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700505 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700506 goto next;
507 }
508
509 if (!(child = fork())) {
510 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700511 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700512 _exit(1);
513 }
514
Sudheer Shanka99d304a2018-09-27 14:53:51 -0700515 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700516
517 std::string storageSource;
518 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700519 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700520 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700521 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700522 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700523 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700524 } else {
525 // Sane default of no storage visible
526 _exit(0);
527 }
Paul Crowley8915d622018-09-18 15:14:18 -0700528 if (TEMP_FAILURE_RETRY(
529 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
530 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700531 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700532 }
Paul Crowley8915d622018-09-18 15:14:18 -0700533 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
534 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +0900535 _exit(1);
536 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700537
538 // Mount user-specific symlink helper into place
539 userid_t user_id = multiuser_get_user_id(uid);
540 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowley8915d622018-09-18 15:14:18 -0700541 if (TEMP_FAILURE_RETRY(
542 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
543 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700544 _exit(1);
545 }
546
Jeff Sharkey66270a22015-06-24 11:49:24 -0700547 _exit(0);
548 }
549
550 if (child == -1) {
551 PLOG(ERROR) << "Failed to fork";
552 goto next;
553 } else {
554 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
555 }
556
Paul Crowley8915d622018-09-18 15:14:18 -0700557 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700558 close(nsFd);
559 close(pidFd);
560 }
561 closedir(dir);
562 return 0;
563}
564
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700565int VolumeManager::reset() {
566 // Tear down all existing disks/volumes and start from a blank slate so
567 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800568 if (mInternalEmulated != nullptr) {
569 mInternalEmulated->destroy();
570 mInternalEmulated->create();
571 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700572 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700573 disk->destroy();
574 disk->create();
575 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600576 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700577 mAddedUsers.clear();
578 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700579 return 0;
580}
581
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700582// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700583int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700584 if (mInternalEmulated == nullptr) {
Paul Crowley8915d622018-09-18 15:14:18 -0700585 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700586 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700587 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700588 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700589 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700590 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700591 disk->destroy();
592 }
Risan8c9f3322018-10-29 08:52:56 +0900593 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700594 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700595 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700596 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700597 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700598}
599
Jeff Sharkey9c484982015-03-31 10:35:33 -0700600int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700601 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600602 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700603
Jeff Sharkey9c484982015-03-31 10:35:33 -0700604 // First, try gracefully unmounting all known devices
605 if (mInternalEmulated != nullptr) {
606 mInternalEmulated->unmount();
607 }
Risan8c9f3322018-10-29 08:52:56 +0900608 for (const auto& stub : mStubVolumes) {
609 stub->unmount();
610 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700611 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700612 disk->unmountAll();
613 }
614
615 // Worst case we might have some stale mounts lurking around, so
616 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +0800617 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700618 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600619 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700620 return -errno;
621 }
622
623 // Some volumes can be stacked on each other, so force unmount in
624 // reverse order to give us the best chance of success.
625 std::list<std::string> toUnmount;
626 mntent* mentry;
627 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600628 auto test = std::string(mentry->mnt_dir);
Mark Salyzync4405e92018-09-20 10:09:27 -0700629 if ((StartsWith(test, "/mnt/") &&
630#ifdef __ANDROID_DEBUGGABLE__
631 !StartsWith(test, "/mnt/scratch") &&
632#endif
633 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
634 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600635 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700636 }
637 }
638 endmntent(fp);
639
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700640 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600641 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700642 android::vold::ForceUnmount(path);
643 }
644
645 return 0;
646}
647
Jeff Sharkey3472e522017-10-06 18:02:53 -0600648int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700649 // Only offer to create directories for paths managed by vold
Mark Salyzync4405e92018-09-20 10:09:27 -0700650 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700651 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -0600652 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700653 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600654 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700655 return -EINVAL;
656 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700657}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600658
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600659int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowley8915d622018-09-18 15:14:18 -0700660 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600661 int id = mNextObbId++;
662
663 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowley8915d622018-09-18 15:14:18 -0700664 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600665 vol->create();
666
667 mObbVolumes.push_back(vol);
668 *outVolId = vol->getId();
669 return android::OK;
670}
671
672int VolumeManager::destroyObb(const std::string& volId) {
673 auto i = mObbVolumes.begin();
674 while (i != mObbVolumes.end()) {
675 if ((*i)->getId() == volId) {
676 (*i)->destroy();
677 i = mObbVolumes.erase(i);
678 } else {
679 ++i;
680 }
681 }
682 return android::OK;
683}
684
Risan8c9f3322018-10-29 08:52:56 +0900685int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
686 const std::string& fsType, const std::string& fsUuid,
687 const std::string& fsLabel, std::string* outVolId) {
688 int id = mNextStubVolumeId++;
689 auto vol = std::shared_ptr<android::vold::VolumeBase>(
690 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
691 vol->create();
692
693 mStubVolumes.push_back(vol);
694 *outVolId = vol->getId();
695 return android::OK;
696}
697
698int VolumeManager::destroyStubVolume(const std::string& volId) {
699 auto i = mStubVolumes.begin();
700 while (i != mStubVolumes.end()) {
701 if ((*i)->getId() == volId) {
702 (*i)->destroy();
703 i = mStubVolumes.erase(i);
704 } else {
705 ++i;
706 }
707 }
708 return android::OK;
709}
710
Risan5f53cd32018-10-26 20:56:45 -0600711int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -0600712 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600713}
714
Risan5f53cd32018-10-26 20:56:45 -0600715int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -0600716 return android::vold::UnmountAppFuse(uid, mountId);
Risan5f53cd32018-10-26 20:56:45 -0600717}
718
719int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -0600720 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600721}