blob: 897c2a81c44df8589d4ecfe334c49d9b558f3f6f [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
Nick Kraleviche7e89ac2019-03-29 16:03:51 -0700503 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
504 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700505 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700506 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700507 goto next;
508 }
509
510 if (!(child = fork())) {
511 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700512 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700513 _exit(1);
514 }
515
Sudheer Shanka99d304a2018-09-27 14:53:51 -0700516 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700517
518 std::string storageSource;
519 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700520 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700521 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700522 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700523 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700524 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700525 } else {
526 // Sane default of no storage visible
527 _exit(0);
528 }
Paul Crowley8915d622018-09-18 15:14:18 -0700529 if (TEMP_FAILURE_RETRY(
530 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
531 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700532 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700533 }
Paul Crowley8915d622018-09-18 15:14:18 -0700534 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
535 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +0900536 _exit(1);
537 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700538
539 // Mount user-specific symlink helper into place
540 userid_t user_id = multiuser_get_user_id(uid);
541 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowley8915d622018-09-18 15:14:18 -0700542 if (TEMP_FAILURE_RETRY(
543 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
544 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700545 _exit(1);
546 }
547
Jeff Sharkey66270a22015-06-24 11:49:24 -0700548 _exit(0);
549 }
550
551 if (child == -1) {
552 PLOG(ERROR) << "Failed to fork";
553 goto next;
554 } else {
555 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
556 }
557
Paul Crowley8915d622018-09-18 15:14:18 -0700558 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700559 close(nsFd);
560 close(pidFd);
561 }
562 closedir(dir);
563 return 0;
564}
565
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700566int VolumeManager::reset() {
567 // Tear down all existing disks/volumes and start from a blank slate so
568 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800569 if (mInternalEmulated != nullptr) {
570 mInternalEmulated->destroy();
571 mInternalEmulated->create();
572 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700573 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700574 disk->destroy();
575 disk->create();
576 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600577 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700578 mAddedUsers.clear();
579 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700580 return 0;
581}
582
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700583// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700584int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700585 if (mInternalEmulated == nullptr) {
Paul Crowley8915d622018-09-18 15:14:18 -0700586 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700587 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700588 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700589 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700590 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700591 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700592 disk->destroy();
593 }
Risan8c9f3322018-10-29 08:52:56 +0900594 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700595 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700596 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700597 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700598 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700599}
600
Jeff Sharkey9c484982015-03-31 10:35:33 -0700601int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700602 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600603 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700604
Jeff Sharkey9c484982015-03-31 10:35:33 -0700605 // First, try gracefully unmounting all known devices
606 if (mInternalEmulated != nullptr) {
607 mInternalEmulated->unmount();
608 }
Risan8c9f3322018-10-29 08:52:56 +0900609 for (const auto& stub : mStubVolumes) {
610 stub->unmount();
611 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700612 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700613 disk->unmountAll();
614 }
615
616 // Worst case we might have some stale mounts lurking around, so
617 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +0800618 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700619 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600620 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700621 return -errno;
622 }
623
624 // Some volumes can be stacked on each other, so force unmount in
625 // reverse order to give us the best chance of success.
626 std::list<std::string> toUnmount;
627 mntent* mentry;
628 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600629 auto test = std::string(mentry->mnt_dir);
Mark Salyzync4405e92018-09-20 10:09:27 -0700630 if ((StartsWith(test, "/mnt/") &&
631#ifdef __ANDROID_DEBUGGABLE__
632 !StartsWith(test, "/mnt/scratch") &&
633#endif
634 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
635 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600636 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700637 }
638 }
639 endmntent(fp);
640
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700641 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600642 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700643 android::vold::ForceUnmount(path);
644 }
645
646 return 0;
647}
648
Jeff Sharkey3472e522017-10-06 18:02:53 -0600649int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700650 // Only offer to create directories for paths managed by vold
Mark Salyzync4405e92018-09-20 10:09:27 -0700651 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700652 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -0600653 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700654 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600655 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700656 return -EINVAL;
657 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700658}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600659
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600660int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowley8915d622018-09-18 15:14:18 -0700661 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600662 int id = mNextObbId++;
663
664 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowley8915d622018-09-18 15:14:18 -0700665 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600666 vol->create();
667
668 mObbVolumes.push_back(vol);
669 *outVolId = vol->getId();
670 return android::OK;
671}
672
673int VolumeManager::destroyObb(const std::string& volId) {
674 auto i = mObbVolumes.begin();
675 while (i != mObbVolumes.end()) {
676 if ((*i)->getId() == volId) {
677 (*i)->destroy();
678 i = mObbVolumes.erase(i);
679 } else {
680 ++i;
681 }
682 }
683 return android::OK;
684}
685
Risan8c9f3322018-10-29 08:52:56 +0900686int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
687 const std::string& fsType, const std::string& fsUuid,
688 const std::string& fsLabel, std::string* outVolId) {
689 int id = mNextStubVolumeId++;
690 auto vol = std::shared_ptr<android::vold::VolumeBase>(
691 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
692 vol->create();
693
694 mStubVolumes.push_back(vol);
695 *outVolId = vol->getId();
696 return android::OK;
697}
698
699int VolumeManager::destroyStubVolume(const std::string& volId) {
700 auto i = mStubVolumes.begin();
701 while (i != mStubVolumes.end()) {
702 if ((*i)->getId() == volId) {
703 (*i)->destroy();
704 i = mStubVolumes.erase(i);
705 } else {
706 ++i;
707 }
708 }
709 return android::OK;
710}
711
Risan5f53cd32018-10-26 20:56:45 -0600712int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -0600713 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600714}
715
Risan5f53cd32018-10-26 20:56:45 -0600716int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -0600717 return android::vold::UnmountAppFuse(uid, mountId);
Risan5f53cd32018-10-26 20:56:45 -0600718}
719
720int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -0600721 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600722}