blob: 0137869c39a44811a338f4e1ae623d4ecab4abe1 [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
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"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070061#include "VoldNativeService.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070062#include "VoldUtil.h"
63#include "VolumeManager.h"
64#include "cryptfs.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 Shanka53947a32018-08-01 10:24:13 -070071using android::base::GetBoolProperty;
Mark Salyzyn86e81e72018-09-20 10:09:27 -070072using android::base::StartsWith;
Sudheer Shanka53947a32018-08-01 10:24:13 -070073using android::base::StringAppendF;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060075using android::base::unique_fd;
Sudheer Shanka03992e32018-12-12 12:43:38 -080076using android::vold::VoldNativeService;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070077
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060078static const char* kPathUserMount = "/mnt/user";
79static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
80
Sudheer Shanka53947a32018-08-01 10:24:13 -070081static const char* kIsolatedStorage = "persist.sys.isolated_storage";
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060082static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
83
Sudheer Shanka53947a32018-08-01 10:24:13 -070084static const std::string kEmptyString("");
85
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060086/* 512MiB is large enough for testing purposes */
87static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070088
Jeff Sharkey36801cc2015-03-13 16:09:20 -070089static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080090static const unsigned int kMajorBlockExperimentalMin = 240;
91static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070092
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070093VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -070094
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070095VolumeManager* VolumeManager::Instance() {
96 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -070097 return sInstance;
98}
99
100VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800101 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600102 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +0900103 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700104 // For security reasons, assume that a secure keyguard is
105 // showing until we hear otherwise
106 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700107}
108
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700109VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800110
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600111int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600112 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700113 if (GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600114 if (access(kPathVirtualDisk, F_OK) != 0) {
115 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
116 }
117
118 if (mVirtualDisk == nullptr) {
119 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
120 LOG(ERROR) << "Failed to create virtual disk";
121 return -1;
122 }
123
124 struct stat buf;
125 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
126 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
127 return -1;
128 }
129
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700130 auto disk = new android::vold::Disk(
131 "virtual", buf.st_rdev, "virtual",
132 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600133 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700134 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600135 }
136 } else {
137 if (mVirtualDisk != nullptr) {
138 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700139 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600140
141 Loop::destroyByDevice(mVirtualDiskPath.c_str());
142 mVirtualDisk = nullptr;
143 }
144
145 if (access(kPathVirtualDisk, F_OK) == 0) {
146 unlink(kPathVirtualDisk);
147 }
148 }
149 return 0;
150}
151
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700152int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800153 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700154 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800155}
156
San Mehatf1b736b2009-10-10 17:22:08 -0700157int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600158 ATRACE_NAME("VolumeManager::start");
159
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700160 // Always start from a clean slate by unmounting everything in
161 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700162 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700163
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600164 Devmapper::destroyAll();
165 Loop::destroyAll();
166
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700167 // Assume that we always have an emulated volume on internal
168 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700169 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700171 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172 mInternalEmulated->create();
173
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600174 // Consider creating a virtual disk
175 updateVirtualDisk();
176
San Mehatf1b736b2009-10-10 17:22:08 -0700177 return 0;
178}
179
180int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700181 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700182 mInternalEmulated->destroy();
183 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700184 return 0;
185}
186
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700187void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700188 std::lock_guard<std::mutex> lock(mLock);
189
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700190 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700191 LOG(DEBUG) << "----------------";
192 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700193 evt->dump();
194 }
San Mehatf1b736b2009-10-10 17:22:08 -0700195
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700196 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
197 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700198
199 if (devType != "disk") return;
200
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600201 int major = std::stoi(evt->findParam("MAJOR"));
202 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700203 dev_t device = makedev(major, minor);
204
205 switch (evt->getAction()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700206 case NetlinkEvent::Action::kAdd: {
207 for (const auto& source : mDiskSources) {
208 if (source->matches(eventPath)) {
209 // For now, assume that MMC and virtio-blk (the latter is
210 // emulator-specific; see Disk.cpp for details) devices are SD,
211 // and that everything else is USB
212 int flags = source->getFlags();
213 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
214 major >= (int)kMajorBlockExperimentalMin &&
215 major <= (int)kMajorBlockExperimentalMax)) {
216 flags |= android::vold::Disk::Flags::kSd;
217 } else {
218 flags |= android::vold::Disk::Flags::kUsb;
219 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700220
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700221 auto disk =
222 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
223 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
224 break;
225 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700226 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700227 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700228 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700229 case NetlinkEvent::Action::kChange: {
230 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
231 handleDiskChanged(device);
232 break;
233 }
234 case NetlinkEvent::Action::kRemove: {
235 handleDiskRemoved(device);
236 break;
237 }
238 default: {
239 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
240 break;
241 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700242 }
243}
244
Jeff Sharkey401b2602017-12-14 22:15:20 -0700245void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
246 // For security reasons, if secure keyguard is showing, wait
247 // until the user unlocks the device to actually touch it
248 if (mSecureKeyguardShowing) {
249 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700250 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700251 mPendingDisks.push_back(disk);
252 } else {
253 disk->create();
254 mDisks.push_back(disk);
255 }
256}
257
258void VolumeManager::handleDiskChanged(dev_t device) {
259 for (const auto& disk : mDisks) {
260 if (disk->getDevice() == device) {
261 disk->readMetadata();
262 disk->readPartitions();
263 }
264 }
265
266 // For security reasons, we ignore all pending disks, since
267 // we'll scan them once the device is unlocked
268}
269
270void VolumeManager::handleDiskRemoved(dev_t device) {
271 auto i = mDisks.begin();
272 while (i != mDisks.end()) {
273 if ((*i)->getDevice() == device) {
274 (*i)->destroy();
275 i = mDisks.erase(i);
276 } else {
277 ++i;
278 }
279 }
280 auto j = mPendingDisks.begin();
281 while (j != mPendingDisks.end()) {
282 if ((*j)->getDevice() == device) {
283 j = mPendingDisks.erase(j);
284 } else {
285 ++j;
286 }
287 }
288}
289
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700290void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800291 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700292 mDiskSources.push_back(diskSource);
293}
294
295std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
296 for (auto disk : mDisks) {
297 if (disk->getId() == id) {
298 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700299 }
300 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700301 return nullptr;
302}
San Mehatf1b736b2009-10-10 17:22:08 -0700303
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700304std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800305 // Vold could receive "mount" after "shutdown" command in the extreme case.
306 // If this happens, mInternalEmulated will equal nullptr and
307 // we need to deal with it in order to avoid null pointer crash.
308 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700309 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700310 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700311 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700312 auto vol = disk->findVolume(id);
313 if (vol != nullptr) {
314 return vol;
315 }
316 }
Risan8c9f3322018-10-29 08:52:56 +0900317 for (const auto& vol : mStubVolumes) {
318 if (vol->getId() == id) {
319 return vol;
320 }
321 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600322 for (const auto& vol : mObbVolumes) {
323 if (vol->getId() == id) {
324 return vol;
325 }
326 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700327 return nullptr;
328}
329
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700330void VolumeManager::listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700331 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700332 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700333 disk->listVolumes(type, list);
334 }
335}
336
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600337int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700338 std::string normalizedGuid;
339 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
340 LOG(WARNING) << "Invalid GUID " << partGuid;
341 return -1;
342 }
343
Paul Crowleyc6433a22017-10-24 14:54:43 -0700344 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700345 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
346 if (unlink(keyPath.c_str()) != 0) {
347 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700348 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700349 }
Eric Biggersa701c452018-10-23 13:06:55 -0700350 if (fscrypt_is_native()) {
351 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700352 success = false;
353 }
354 }
355 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700356}
357
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700358int VolumeManager::linkPrimary(userid_t userId) {
359 std::string source(mPrimary->getPath());
360 if (mPrimary->isEmulated()) {
361 source = StringPrintf("%s/%d", source.c_str(), userId);
362 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
363 }
364
365 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
366 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
367 if (errno != ENOENT) {
368 PLOG(WARNING) << "Failed to unlink " << target;
369 }
370 }
371 LOG(DEBUG) << "Linking " << source << " to " << target;
372 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
373 PLOG(WARNING) << "Failed to link";
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700374 return -errno;
375 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700376 return 0;
377}
378
379int VolumeManager::mountPkgSpecificDir(const std::string& mntSourceRoot,
380 const std::string& mntTargetRoot,
381 const std::string& packageName, const char* dirName) {
382 std::string mntSourceDir =
383 StringPrintf("%s/Android/%s/%s", mntSourceRoot.c_str(), dirName, packageName.c_str());
384 std::string mntTargetDir =
385 StringPrintf("%s/Android/%s/%s", mntTargetRoot.c_str(), dirName, packageName.c_str());
386 if (umount2(mntTargetDir.c_str(), MNT_DETACH) == -1 && errno != EINVAL && errno != ENOENT) {
387 PLOG(ERROR) << "Failed to unmount " << mntTargetDir;
388 return -1;
389 }
390 if (TEMP_FAILURE_RETRY(mount(mntSourceDir.c_str(), mntTargetDir.c_str(), nullptr,
391 MS_BIND | MS_REC, nullptr)) == -1) {
392 PLOG(ERROR) << "Failed to mount " << mntSourceDir << " to " << mntTargetDir;
393 return -1;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700394 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700395 if (TEMP_FAILURE_RETRY(
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700396 mount(nullptr, mntTargetDir.c_str(), nullptr, MS_REC | MS_SLAVE, nullptr)) == -1) {
397 PLOG(ERROR) << "Failed to set MS_SLAVE at " << mntTargetDir;
398 return -1;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700399 }
400 return 0;
401}
402
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700403int VolumeManager::mountPkgSpecificDirsForRunningProcs(
404 userid_t userId, const std::vector<std::string>& packageNames,
405 const std::vector<std::string>& visibleVolLabels) {
406 // TODO: New processes could be started while traversing over the existing
407 // processes which would end up not having the necessary bind mounts. This
408 // issue needs to be fixed, may be by doing multiple passes here?
409 std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir("/proc"), closedir);
410 if (!dirp) {
411 PLOG(ERROR) << "Failed to opendir /proc";
412 return -1;
Sudheer Shankac7562092018-08-24 10:20:56 -0700413 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700414
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700415 std::string rootName;
416 // Figure out root namespace to compare against below
417 if (!android::vold::Readlinkat(dirfd(dirp.get()), "1/ns/mnt", &rootName)) {
418 PLOG(ERROR) << "Failed to read root namespace";
419 return -1;
Sudheer Shankaf768c272018-08-04 10:10:27 -0700420 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700421
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700422 struct stat fullWriteSb;
423 if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/write", &fullWriteSb)) == -1) {
424 PLOG(ERROR) << "Failed to stat /mnt/runtime/write";
425 return -1;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700426 }
427
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700428 std::unordered_set<appid_t> validAppIds;
429 for (auto& package : packageNames) {
430 validAppIds.insert(mAppIds[package]);
Sudheer Shankaa695f252018-08-03 18:07:52 -0700431 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700432 std::vector<std::string>& userPackages = mUserPackages[userId];
Sudheer Shanka53947a32018-08-01 10:24:13 -0700433
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700434 struct dirent* de;
435 // Poke through all running PIDs look for apps running in userId
436 while ((de = readdir(dirp.get()))) {
437 pid_t pid;
438 if (de->d_type != DT_DIR) continue;
439 if (!android::base::ParseInt(de->d_name, &pid)) continue;
440
441 const unique_fd pidFd(
442 openat(dirfd(dirp.get()), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC));
443 if (pidFd.get() < 0) {
444 PLOG(WARNING) << "Failed to open /proc/" << pid;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700445 continue;
446 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700447 struct stat sb;
448 if (fstat(pidFd.get(), &sb) != 0) {
449 PLOG(WARNING) << "Failed to stat " << de->d_name;
450 continue;
451 }
452 if (multiuser_get_user_id(sb.st_uid) != userId) {
453 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700454 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700455
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700456 // Matches so far, but refuse to touch if in root namespace
457 LOG(VERBOSE) << "Found matching PID " << de->d_name;
458 std::string pidName;
459 if (!android::vold::Readlinkat(pidFd.get(), "ns/mnt", &pidName)) {
460 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
461 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700462 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700463 if (rootName == pidName) {
464 LOG(WARNING) << "Skipping due to root namespace";
465 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700466 }
467
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700468 // Only update the mount points of processes running with one of validAppIds.
469 // This should skip any isolated uids.
470 appid_t appId = multiuser_get_app_id(sb.st_uid);
471 if (validAppIds.find(appId) == validAppIds.end()) {
472 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700473 }
474
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700475 std::vector<std::string> packagesForUid;
476 for (auto& package : userPackages) {
477 if (mAppIds[package] == appId) {
478 packagesForUid.push_back(package);
479 }
480 }
481 if (packagesForUid.empty()) {
482 continue;
483 }
484 const std::string& sandboxId = mSandboxIds[appId];
485
486 // We purposefully leave the namespace open across the fork
487 unique_fd nsFd(openat(pidFd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
488 if (nsFd.get() < 0) {
489 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
490 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700491 }
492
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700493 pid_t child;
494 if (!(child = fork())) {
495 if (setns(nsFd.get(), CLONE_NEWNS) != 0) {
496 PLOG(ERROR) << "Failed to setns for " << de->d_name;
497 _exit(1);
498 }
499
Sudheer Shanka03992e32018-12-12 12:43:38 -0800500 int mountMode = getMountModeForRunningProc(packagesForUid, userId, fullWriteSb);
501 if (mountMode == -1) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700502 _exit(1);
503 }
504
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700505 for (auto& volumeLabel : visibleVolLabels) {
506 std::string mntSource = StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str());
507 std::string mntTarget = StringPrintf("/storage/%s", volumeLabel.c_str());
508 if (volumeLabel == "emulated") {
509 StringAppendF(&mntSource, "/%d", userId);
510 StringAppendF(&mntTarget, "/%d", userId);
511 }
512 for (auto& package : packagesForUid) {
513 mountPkgSpecificDir(mntSource, mntTarget, package, "data");
514 mountPkgSpecificDir(mntSource, mntTarget, package, "media");
Sudheer Shanka03992e32018-12-12 12:43:38 -0800515 if (mountMode != VoldNativeService::REMOUNT_MODE_INSTALLER) {
516 mountPkgSpecificDir(mntSource, mntTarget, package, "obb");
517 }
518 }
519 if (mountMode == VoldNativeService::REMOUNT_MODE_INSTALLER) {
520 StringAppendF(&mntSource, "/Android/obb");
521 StringAppendF(&mntTarget, "/Android/obb");
522 if (TEMP_FAILURE_RETRY(mount(mntSource.c_str(), mntTarget.c_str(), nullptr,
523 MS_BIND | MS_REC, nullptr)) == -1) {
524 PLOG(ERROR) << "Failed to mount " << mntSource << " to " << mntTarget;
525 continue;
526 }
527 if (TEMP_FAILURE_RETRY(mount(nullptr, mntTarget.c_str(), nullptr,
528 MS_REC | MS_SLAVE, nullptr)) == -1) {
529 PLOG(ERROR) << "Failed to set MS_SLAVE at " << mntTarget.c_str();
530 continue;
531 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700532 }
533 }
534 _exit(0);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700535 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700536
537 if (child == -1) {
538 PLOG(ERROR) << "Failed to fork";
539 } else {
540 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
Sudheer Shanka53947a32018-08-01 10:24:13 -0700541 }
542 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700543 return 0;
544}
545
Sudheer Shanka03992e32018-12-12 12:43:38 -0800546int VolumeManager::getMountModeForRunningProc(const std::vector<std::string>& packagesForUid,
547 userid_t userId, struct stat& mntWriteStat) {
548 struct stat storageSb;
549 if (TEMP_FAILURE_RETRY(stat("/storage", &storageSb)) == -1) {
550 PLOG(ERROR) << "Failed to stat /storage";
551 return -1;
552 }
553
554 // Some packages have access to full external storage, identify processes belonging
555 // to those packages by comparing inode no.s of /mnt/runtime/write and /storage
556 if (storageSb.st_dev == mntWriteStat.st_dev && storageSb.st_ino == mntWriteStat.st_ino) {
557 return VoldNativeService::REMOUNT_MODE_FULL;
558 }
559
560 std::string obbMountFile =
561 StringPrintf("/mnt/user/%d/package/%s/obb_mount", userId, packagesForUid[0].c_str());
562 if (access(obbMountFile.c_str(), F_OK) == 0) {
563 return VoldNativeService::REMOUNT_MODE_INSTALLER;
564 }
565
566 // Some packages don't have access to external storage and processes belonging to
567 // those packages don't have anything mounted at /storage. So, identify those
568 // processes by comparing inode no.s of /mnt/user/%d/package/%s
569 // and /storage
570 for (auto& package : packagesForUid) {
571 std::string sandbox = StringPrintf("/mnt/user/%d/package/%s", userId, package.c_str());
572 struct stat sandboxStat;
573 if (TEMP_FAILURE_RETRY(stat(sandbox.c_str(), &sandboxStat)) == -1) {
574 PLOG(ERROR) << "Failed to stat " << sandbox;
575 return -1;
576 }
577 if (storageSb.st_dev == sandboxStat.st_dev && storageSb.st_ino == sandboxStat.st_ino) {
578 return VoldNativeService::REMOUNT_MODE_WRITE;
579 }
580 }
581 return VoldNativeService::REMOUNT_MODE_NONE;
582}
583
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700584int VolumeManager::prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames,
585 const std::vector<std::string>& visibleVolLabels) {
586 if (visibleVolLabels.empty()) {
587 return 0;
588 }
589 for (auto& volumeLabel : visibleVolLabels) {
590 std::string volumeRoot(StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str()));
591 bool isVolPrimaryEmulated = (volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated());
592 if (isVolPrimaryEmulated) {
593 StringAppendF(&volumeRoot, "/%d", userId);
594 if (fs_prepare_dir(volumeRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
595 PLOG(ERROR) << "fs_prepare_dir failed on " << volumeRoot;
596 return -errno;
597 }
598 }
599
600 std::string sandboxRoot =
601 prepareSubDirs(volumeRoot, "Android/sandbox/", 0700, AID_ROOT, AID_ROOT);
602 if (sandboxRoot.empty()) {
603 return -errno;
604 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700605
606 if (!createPkgSpecificDirRoots(volumeRoot)) {
607 return -errno;
608 }
609
610 std::string mntTargetRoot = StringPrintf("/mnt/user/%d", userId);
611 if (fs_prepare_dir(mntTargetRoot.c_str(), 0751, AID_ROOT, AID_ROOT) != 0) {
612 PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot;
613 return -errno;
614 }
615 mntTargetRoot.append("/package");
616 if (fs_prepare_dir(mntTargetRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
617 PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot;
618 return -errno;
619 }
620
621 for (auto& packageName : packageNames) {
622 const auto& it = mAppIds.find(packageName);
623 if (it == mAppIds.end()) {
624 PLOG(ERROR) << "appId is not available for " << packageName;
625 continue;
626 }
627 appid_t appId = it->second;
628 std::string sandboxId = mSandboxIds[appId];
629 uid_t uid = multiuser_get_uid(userId, appId);
630
631 // [1] Create /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId>
632 // [2] Create /mnt/user/0/package/<packageName>/emulated/0
633 // Mount [1] at [2]
634 std::string pkgSandboxSourceDir = prepareSandboxSource(uid, sandboxId, sandboxRoot);
635 if (pkgSandboxSourceDir.empty()) {
636 return -errno;
637 }
638 std::string pkgSandboxTargetDir = prepareSandboxTarget(
639 packageName, uid, volumeLabel, mntTargetRoot, isVolPrimaryEmulated);
640 if (pkgSandboxTargetDir.empty()) {
641 return -errno;
642 }
643 if (umount2(pkgSandboxTargetDir.c_str(), MNT_DETACH) == -1 && errno != EINVAL &&
644 errno != ENOENT) {
645 PLOG(ERROR) << "Failed to unmount " << pkgSandboxTargetDir;
646 return -errno;
647 }
648 if (TEMP_FAILURE_RETRY(mount(pkgSandboxSourceDir.c_str(), pkgSandboxTargetDir.c_str(),
649 nullptr, MS_BIND | MS_REC, nullptr)) == -1) {
650 PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at "
651 << pkgSandboxTargetDir;
652 return -errno;
653 }
654 if (TEMP_FAILURE_RETRY(mount(nullptr, pkgSandboxTargetDir.c_str(), nullptr,
655 MS_SLAVE | MS_REC, nullptr)) == -1) {
656 PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at "
657 << pkgSandboxTargetDir;
658 return -errno;
659 }
660
661 // Create Android/{data,media,obb}/<packageName> segments at
662 // [1] /mnt/runtime/write/emulated/0/ and
663 // [2] /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId>/emulated/0/
664 if (!createPkgSpecificDirs(packageName, uid, volumeRoot, pkgSandboxSourceDir)) {
665 return -errno;
666 }
667
668 if (volumeLabel == mPrimary->getLabel()) {
669 // Create [1] /mnt/user/0/package/<packageName>/self/
670 // Already created [2] /mnt/user/0/package/<packageName>/emulated/0
671 // Mount [2] at [1]
672 std::string pkgPrimaryTargetDir =
673 StringPrintf("%s/%s/self", mntTargetRoot.c_str(), packageName.c_str());
674 if (fs_prepare_dir(pkgPrimaryTargetDir.c_str(), 0755, uid, uid) != 0) {
675 PLOG(ERROR) << "Failed to fs_prepare_dir on " << pkgPrimaryTargetDir;
676 return -errno;
677 }
678 StringAppendF(&pkgPrimaryTargetDir, "/primary");
679 std::string primarySource(mPrimary->getPath());
680 if (isVolPrimaryEmulated) {
681 StringAppendF(&primarySource, "/%d", userId);
682 }
683 if (TEMP_FAILURE_RETRY(unlink(pkgPrimaryTargetDir.c_str()))) {
684 if (errno != ENOENT) {
685 PLOG(ERROR) << "Failed to unlink " << pkgPrimaryTargetDir;
686 }
687 }
688 if (TEMP_FAILURE_RETRY(symlink(primarySource.c_str(), pkgPrimaryTargetDir.c_str()))) {
689 PLOG(ERROR) << "Failed to link " << primarySource << " at "
690 << pkgPrimaryTargetDir;
691 return -errno;
692 }
693 }
694 }
695 }
696 mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels);
697 return 0;
698}
699
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700700std::string VolumeManager::prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs,
701 mode_t mode, uid_t uid, gid_t gid) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700702 std::string path(pathPrefix);
703 std::vector<std::string> subDirList = android::base::Split(subDirs, "/");
704 for (size_t i = 0; i < subDirList.size(); ++i) {
705 std::string subDir = subDirList[i];
706 if (subDir.empty()) {
707 continue;
708 }
709 StringAppendF(&path, "/%s", subDir.c_str());
710 if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) {
711 PLOG(ERROR) << "fs_prepare_dir failed on " << path;
712 return kEmptyString;
713 }
714 }
715 return path;
716}
717
718std::string VolumeManager::prepareSandboxSource(uid_t uid, const std::string& sandboxId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700719 const std::string& sandboxRootDir) {
Sudheer Shankaf768c272018-08-04 10:10:27 -0700720 std::string sandboxSourceDir(sandboxRootDir);
Sudheer Shanka51a38da2018-10-18 08:51:31 -0700721 StringAppendF(&sandboxSourceDir, "/%s", sandboxId.c_str());
Sudheer Shanka53947a32018-08-01 10:24:13 -0700722 if (fs_prepare_dir(sandboxSourceDir.c_str(), 0755, uid, uid) != 0) {
723 PLOG(ERROR) << "fs_prepare_dir failed on " << sandboxSourceDir;
724 return kEmptyString;
725 }
726 return sandboxSourceDir;
727}
728
729std::string VolumeManager::prepareSandboxTarget(const std::string& packageName, uid_t uid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700730 const std::string& volumeLabel,
731 const std::string& mntTargetRootDir,
732 bool isUserDependent) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700733 std::string segment;
734 if (isUserDependent) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700735 segment = StringPrintf("%s/%s/%d/", packageName.c_str(), volumeLabel.c_str(),
736 multiuser_get_user_id(uid));
Sudheer Shanka53947a32018-08-01 10:24:13 -0700737 } else {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700738 segment = StringPrintf("%s/%s/", packageName.c_str(), volumeLabel.c_str());
Sudheer Shanka53947a32018-08-01 10:24:13 -0700739 }
740 return prepareSubDirs(mntTargetRootDir, segment.c_str(), 0755, uid, uid);
741}
742
743std::string VolumeManager::preparePkgDataSource(const std::string& packageName, uid_t uid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700744 const std::string& dataRootDir) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700745 std::string dataSourceDir = StringPrintf("%s/%s", dataRootDir.c_str(), packageName.c_str());
746 if (fs_prepare_dir(dataSourceDir.c_str(), 0755, uid, uid) != 0) {
747 PLOG(ERROR) << "fs_prepare_dir failed on " << dataSourceDir;
748 return kEmptyString;
749 }
750 return dataSourceDir;
751}
752
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700753bool VolumeManager::createPkgSpecificDirRoots(const std::string& volumeRoot) {
754 std::string volumeAndroidRoot = StringPrintf("%s/Android", volumeRoot.c_str());
755 if (fs_prepare_dir(volumeAndroidRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
756 PLOG(ERROR) << "fs_prepare_dir failed on " << volumeAndroidRoot;
757 return false;
758 }
759 std::array<std::string, 3> dirs = {"data", "media", "obb"};
760 for (auto& dir : dirs) {
761 std::string path = StringPrintf("%s/%s", volumeAndroidRoot.c_str(), dir.c_str());
762 if (fs_prepare_dir(path.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
763 PLOG(ERROR) << "fs_prepare_dir failed on " << path;
764 return false;
765 }
766 }
767 return true;
768}
769
770bool VolumeManager::createPkgSpecificDirs(const std::string& packageName, uid_t uid,
771 const std::string& volumeRoot,
772 const std::string& sandboxDirRoot) {
773 std::array<std::string, 3> dirs = {"data", "media", "obb"};
774 for (auto& dir : dirs) {
775 std::string sourceDir = StringPrintf("%s/Android/%s", volumeRoot.c_str(), dir.c_str());
776 if (prepareSubDirs(sourceDir, packageName, 0755, uid, uid).empty()) {
777 return false;
778 }
779 std::string sandboxSegment =
780 StringPrintf("Android/%s/%s/", dir.c_str(), packageName.c_str());
781 if (prepareSubDirs(sandboxDirRoot, sandboxSegment, 0755, uid, uid).empty()) {
782 return false;
783 }
784 }
785 return true;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700786}
787
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700788int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
789 mAddedUsers[userId] = userSerialNumber;
790 return 0;
791}
792
793int VolumeManager::onUserRemoved(userid_t userId) {
794 mAddedUsers.erase(userId);
795 return 0;
796}
797
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700798int VolumeManager::onUserStarted(userid_t userId, const std::vector<std::string>& packageNames,
799 const std::vector<int>& appIds,
800 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700801 LOG(VERBOSE) << "onUserStarted: " << userId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700802 // Note that sometimes the system will spin up processes from Zygote
803 // before actually starting the user, so we're okay if Zygote
804 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600805 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700806 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
807
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700808 mStartedUsers.insert(userId);
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700809 mUserPackages[userId] = packageNames;
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700810 for (size_t i = 0; i < packageNames.size(); ++i) {
811 mAppIds[packageNames[i]] = appIds[i];
812 mSandboxIds[appIds[i]] = sandboxIds[i];
813 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700814 if (mPrimary) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700815 linkPrimary(userId);
816 }
817 if (GetBoolProperty(kIsolatedStorage, false)) {
818 std::vector<std::string> visibleVolLabels;
819 for (auto& volId : mVisibleVolumeIds) {
820 auto vol = findVolume(volId);
821 userid_t mountUserId = vol->getMountUserId();
822 if (mountUserId == userId || vol->isEmulated()) {
823 visibleVolLabels.push_back(vol->getLabel());
824 }
825 }
826 if (prepareSandboxes(userId, packageNames, visibleVolLabels) != 0) {
827 return -errno;
828 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700829 }
830 return 0;
831}
832
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700833int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700834 LOG(VERBOSE) << "onUserStopped: " << userId;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700835 mStartedUsers.erase(userId);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700836
Sudheer Shanka9acc6d42018-10-06 19:03:02 -0700837 if (GetBoolProperty(kIsolatedStorage, false)) {
838 mUserPackages.erase(userId);
839 std::string mntTargetDir = StringPrintf("/mnt/user/%d", userId);
840 if (android::vold::UnmountTree(mntTargetDir) != 0) {
841 PLOG(ERROR) << "unmountTree on " << mntTargetDir << " failed";
842 return -errno;
843 }
844 if (android::vold::DeleteDirContentsAndDir(mntTargetDir) < 0) {
845 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << mntTargetDir;
846 return -errno;
847 }
848 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << mntTargetDir;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700849 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700850 return 0;
851}
852
Sudheer Shankad484aa92018-07-31 10:07:34 -0700853int VolumeManager::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700854 const std::vector<int32_t>& appIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700855 for (size_t i = 0; i < packageNames.size(); ++i) {
856 mAppIds[packageNames[i]] = appIds[i];
857 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700858 return 0;
859}
860
861int VolumeManager::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700862 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700863 for (size_t i = 0; i < appIds.size(); ++i) {
864 mSandboxIds[appIds[i]] = sandboxIds[i];
865 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700866 return 0;
867}
868
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700869int VolumeManager::prepareSandboxForApp(const std::string& packageName, appid_t appId,
870 const std::string& sandboxId, userid_t userId) {
Sudheer Shankac7562092018-08-24 10:20:56 -0700871 if (!GetBoolProperty(kIsolatedStorage, false)) {
872 return 0;
873 } else if (mStartedUsers.find(userId) == mStartedUsers.end()) {
874 // User not started, no need to do anything now. Required bind mounts for the package will
875 // be created when the user starts.
876 return 0;
877 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700878 LOG(VERBOSE) << "prepareSandboxForApp: " << packageName << ", appId=" << appId
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700879 << ", sandboxId=" << sandboxId << ", userId=" << userId;
Sudheer Shankac7562092018-08-24 10:20:56 -0700880 mUserPackages[userId].push_back(packageName);
881 mAppIds[packageName] = appId;
882 mSandboxIds[appId] = sandboxId;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700883
884 std::vector<std::string> visibleVolLabels;
885 for (auto& volId : mVisibleVolumeIds) {
886 auto vol = findVolume(volId);
887 userid_t mountUserId = vol->getMountUserId();
888 if (mountUserId == userId || vol->isEmulated()) {
889 visibleVolLabels.push_back(vol->getLabel());
890 }
Sudheer Shankac7562092018-08-24 10:20:56 -0700891 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700892 return prepareSandboxes(userId, {packageName}, visibleVolLabels);
Sudheer Shankac7562092018-08-24 10:20:56 -0700893}
894
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700895int VolumeManager::destroySandboxForApp(const std::string& packageName,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700896 const std::string& sandboxId, userid_t userId) {
897 if (!GetBoolProperty(kIsolatedStorage, false)) {
898 return 0;
899 }
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700900 LOG(VERBOSE) << "destroySandboxForApp: " << packageName << ", sandboxId=" << sandboxId
901 << ", userId=" << userId;
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700902 auto& userPackages = mUserPackages[userId];
903 std::remove(userPackages.begin(), userPackages.end(), packageName);
904 // If the package is not uninstalled in any other users, remove appId and sandboxId
905 // corresponding to it from the internal state.
906 bool installedInAnyUser = false;
907 for (auto& it : mUserPackages) {
908 auto& packages = it.second;
909 if (std::find(packages.begin(), packages.end(), packageName) != packages.end()) {
910 installedInAnyUser = true;
911 break;
912 }
913 }
914 if (!installedInAnyUser) {
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700915 const auto& entry = mAppIds.find(packageName);
916 if (entry != mAppIds.end()) {
917 mSandboxIds.erase(entry->second);
918 mAppIds.erase(entry);
919 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700920 }
921
922 std::vector<std::string> visibleVolLabels;
923 for (auto& volId : mVisibleVolumeIds) {
924 auto vol = findVolume(volId);
925 userid_t mountUserId = vol->getMountUserId();
926 if (mountUserId == userId || vol->isEmulated()) {
927 if (destroySandboxForAppOnVol(packageName, sandboxId, userId, vol->getLabel()) < 0) {
928 return -errno;
929 }
930 }
931 }
932 return 0;
933}
934
935int VolumeManager::destroySandboxForAppOnVol(const std::string& packageName,
936 const std::string& sandboxId, userid_t userId,
937 const std::string& volLabel) {
938 LOG(VERBOSE) << "destroySandboxOnVol: " << packageName << ", userId=" << userId
939 << ", volLabel=" << volLabel;
940 std::string pkgSandboxTarget =
941 StringPrintf("/mnt/user/%d/package/%s", userId, packageName.c_str());
942 if (android::vold::UnmountTree(pkgSandboxTarget)) {
943 PLOG(ERROR) << "UnmountTree failed on " << pkgSandboxTarget;
944 }
945
946 std::string sandboxDir = StringPrintf("/mnt/runtime/write/%s", volLabel.c_str());
947 if (volLabel == mPrimary->getLabel() && mPrimary->isEmulated()) {
948 StringAppendF(&sandboxDir, "/%d", userId);
949 }
Sudheer Shanka51a38da2018-10-18 08:51:31 -0700950 StringAppendF(&sandboxDir, "/Android/sandbox/%s", sandboxId.c_str());
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700951
952 if (android::vold::DeleteDirContentsAndDir(sandboxDir) < 0) {
953 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << sandboxDir;
954 return -errno;
955 }
956 return 0;
957}
958
Jeff Sharkey401b2602017-12-14 22:15:20 -0700959int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
960 mSecureKeyguardShowing = isShowing;
961 if (!mSecureKeyguardShowing) {
962 // Now that secure keyguard has been dismissed, process
963 // any pending disks
964 for (const auto& disk : mPendingDisks) {
965 disk->create();
966 mDisks.push_back(disk);
967 }
968 mPendingDisks.clear();
969 }
970 return 0;
971}
972
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700973int VolumeManager::onVolumeMounted(android::vold::VolumeBase* vol) {
974 if (!GetBoolProperty(kIsolatedStorage, false)) {
975 return 0;
976 }
977
978 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_VISIBLE) == 0) {
979 return 0;
980 }
981
982 mVisibleVolumeIds.insert(vol->getId());
983 userid_t mountUserId = vol->getMountUserId();
984 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
985 // We don't want to create another shared_ptr here because then we will end up with
986 // two shared_ptrs owning the underlying pointer without sharing it.
987 mPrimary = findVolume(vol->getId());
988 for (userid_t userId : mStartedUsers) {
989 if (linkPrimary(userId) != 0) {
990 return -errno;
991 }
992 }
993 }
994 if (vol->isEmulated()) {
995 for (userid_t userId : mStartedUsers) {
996 if (prepareSandboxes(userId, mUserPackages[userId], {vol->getLabel()}) != 0) {
997 return -errno;
998 }
999 }
1000 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
1001 if (prepareSandboxes(mountUserId, mUserPackages[mountUserId], {vol->getLabel()}) != 0) {
1002 return -errno;
1003 }
1004 }
1005 return 0;
1006}
1007
1008int VolumeManager::onVolumeUnmounted(android::vold::VolumeBase* vol) {
1009 if (!GetBoolProperty(kIsolatedStorage, false)) {
1010 return 0;
1011 }
1012
1013 if (mVisibleVolumeIds.erase(vol->getId()) == 0) {
1014 return 0;
1015 }
1016
1017 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
1018 mPrimary = nullptr;
1019 }
1020
1021 LOG(VERBOSE) << "visibleVolumeUnmounted: " << vol;
1022 userid_t mountUserId = vol->getMountUserId();
1023 if (vol->isEmulated()) {
1024 for (userid_t userId : mStartedUsers) {
1025 if (destroySandboxesForVol(vol, userId) != 0) {
1026 return -errno;
1027 }
1028 }
1029 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
1030 if (destroySandboxesForVol(vol, mountUserId) != 0) {
1031 return -errno;
1032 }
1033 }
1034 return 0;
1035}
1036
1037int VolumeManager::destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId) {
1038 LOG(VERBOSE) << "destroysandboxesForVol: " << vol << " for user=" << userId;
1039 const std::vector<std::string>& packageNames = mUserPackages[userId];
1040 for (auto& packageName : packageNames) {
1041 std::string volSandboxRoot = StringPrintf("/mnt/user/%d/package/%s/%s", userId,
1042 packageName.c_str(), vol->getLabel().c_str());
1043 if (android::vold::UnmountTree(volSandboxRoot) != 0) {
1044 PLOG(ERROR) << "unmountTree on " << volSandboxRoot << " failed";
1045 continue;
1046 }
1047 if (android::vold::DeleteDirContentsAndDir(volSandboxRoot) < 0) {
1048 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << volSandboxRoot;
1049 continue;
1050 }
1051 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << volSandboxRoot;
1052 }
1053 return 0;
1054}
1055
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001056int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001057 if (GetBoolProperty(kIsolatedStorage, false)) {
1058 return 0;
1059 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001060 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001061 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001062 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001063 }
1064 return 0;
1065}
1066
Jeff Sharkey66270a22015-06-24 11:49:24 -07001067int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
Sudheer Shanka53947a32018-08-01 10:24:13 -07001068 // If the isolated storage is enabled, return -1 since in the isolated storage world, there
1069 // are no longer any runtime storage permissions, so this shouldn't be called anymore.
1070 if (GetBoolProperty(kIsolatedStorage, false)) {
1071 return -1;
1072 }
Jeff Sharkey66270a22015-06-24 11:49:24 -07001073 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
1074
1075 DIR* dir;
1076 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001077 std::string rootName;
1078 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001079 int pidFd;
1080 int nsFd;
1081 struct stat sb;
1082 pid_t child;
1083
1084 if (!(dir = opendir("/proc"))) {
1085 PLOG(ERROR) << "Failed to opendir";
1086 return -1;
1087 }
1088
1089 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -06001090 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
1091 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001092 closedir(dir);
1093 return -1;
1094 }
1095
1096 // Poke through all running PIDs look for apps running as UID
1097 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -07001098 pid_t pid;
1099 if (de->d_type != DT_DIR) continue;
1100 if (!android::base::ParseInt(de->d_name, &pid)) continue;
1101
Jeff Sharkey66270a22015-06-24 11:49:24 -07001102 pidFd = -1;
1103 nsFd = -1;
1104
1105 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1106 if (pidFd < 0) {
1107 goto next;
1108 }
1109 if (fstat(pidFd, &sb) != 0) {
1110 PLOG(WARNING) << "Failed to stat " << de->d_name;
1111 goto next;
1112 }
1113 if (sb.st_uid != uid) {
1114 goto next;
1115 }
1116
1117 // Matches so far, but refuse to touch if in root namespace
1118 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001119 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001120 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
1121 goto next;
1122 }
Jeff Sharkey3472e522017-10-06 18:02:53 -06001123 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001124 LOG(WARNING) << "Skipping due to root namespace";
1125 goto next;
1126 }
1127
1128 // We purposefully leave the namespace open across the fork
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001129 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -07001130 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001131 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001132 goto next;
1133 }
1134
1135 if (!(child = fork())) {
1136 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001137 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001138 _exit(1);
1139 }
1140
Sudheer Shanka99d304a2018-09-27 14:53:51 -07001141 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -07001142
1143 std::string storageSource;
1144 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001145 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001146 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001147 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001148 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001149 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001150 } else {
1151 // Sane default of no storage visible
1152 _exit(0);
1153 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001154 if (TEMP_FAILURE_RETRY(
1155 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
1156 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001157 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -07001158 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001159 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
1160 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +09001161 _exit(1);
1162 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001163
1164 // Mount user-specific symlink helper into place
1165 userid_t user_id = multiuser_get_user_id(uid);
1166 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001167 if (TEMP_FAILURE_RETRY(
1168 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
1169 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001170 _exit(1);
1171 }
1172
Jeff Sharkey66270a22015-06-24 11:49:24 -07001173 _exit(0);
1174 }
1175
1176 if (child == -1) {
1177 PLOG(ERROR) << "Failed to fork";
1178 goto next;
1179 } else {
1180 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
1181 }
1182
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001183 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -07001184 close(nsFd);
1185 close(pidFd);
1186 }
1187 closedir(dir);
1188 return 0;
1189}
1190
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001191int VolumeManager::reset() {
1192 // Tear down all existing disks/volumes and start from a blank slate so
1193 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +08001194 if (mInternalEmulated != nullptr) {
1195 mInternalEmulated->destroy();
1196 mInternalEmulated->create();
1197 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001198 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001199 disk->destroy();
1200 disk->create();
1201 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -06001202 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001203 mAddedUsers.clear();
1204 mStartedUsers.clear();
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -07001205
1206 mUserPackages.clear();
1207 mAppIds.clear();
1208 mSandboxIds.clear();
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001209 mVisibleVolumeIds.clear();
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -07001210
1211 // For unmounting dirs under /mnt/user/<user-id>/package/<package-name>
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001212 android::vold::UnmountTree("/mnt/user/");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001213 return 0;
1214}
1215
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001216// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001217int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001218 if (mInternalEmulated == nullptr) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001219 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001220 }
Paul Crowley56292ef2017-10-20 08:07:53 -07001221 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001222 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001223 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001224 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001225 disk->destroy();
1226 }
Risan8c9f3322018-10-29 08:52:56 +09001227 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001228 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -07001229 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -07001230 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001231 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -07001232}
1233
Jeff Sharkey9c484982015-03-31 10:35:33 -07001234int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001235 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -06001236 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001237
Jeff Sharkey9c484982015-03-31 10:35:33 -07001238 // First, try gracefully unmounting all known devices
1239 if (mInternalEmulated != nullptr) {
1240 mInternalEmulated->unmount();
1241 }
Risan8c9f3322018-10-29 08:52:56 +09001242 for (const auto& stub : mStubVolumes) {
1243 stub->unmount();
1244 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001245 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001246 disk->unmountAll();
1247 }
1248
1249 // Worst case we might have some stale mounts lurking around, so
1250 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +08001251 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -07001252 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001253 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -07001254 return -errno;
1255 }
1256
1257 // Some volumes can be stacked on each other, so force unmount in
1258 // reverse order to give us the best chance of success.
1259 std::list<std::string> toUnmount;
1260 mntent* mentry;
1261 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001262 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001263 if ((StartsWith(test, "/mnt/") &&
1264#ifdef __ANDROID_DEBUGGABLE__
1265 !StartsWith(test, "/mnt/scratch") &&
1266#endif
1267 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
1268 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001269 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -07001270 }
1271 }
1272 endmntent(fp);
1273
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001274 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001275 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001276 android::vold::ForceUnmount(path);
1277 }
1278
1279 return 0;
1280}
1281
Jeff Sharkey3472e522017-10-06 18:02:53 -06001282int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001283 // Only offer to create directories for paths managed by vold
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001284 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001285 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -06001286 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001287 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001288 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001289 return -EINVAL;
1290 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001291}
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001292
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001293int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001294 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001295 int id = mNextObbId++;
1296
1297 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001298 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001299 vol->create();
1300
1301 mObbVolumes.push_back(vol);
1302 *outVolId = vol->getId();
1303 return android::OK;
1304}
1305
1306int VolumeManager::destroyObb(const std::string& volId) {
1307 auto i = mObbVolumes.begin();
1308 while (i != mObbVolumes.end()) {
1309 if ((*i)->getId() == volId) {
1310 (*i)->destroy();
1311 i = mObbVolumes.erase(i);
1312 } else {
1313 ++i;
1314 }
1315 }
1316 return android::OK;
1317}
1318
Risan8c9f3322018-10-29 08:52:56 +09001319int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
1320 const std::string& fsType, const std::string& fsUuid,
1321 const std::string& fsLabel, std::string* outVolId) {
1322 int id = mNextStubVolumeId++;
1323 auto vol = std::shared_ptr<android::vold::VolumeBase>(
1324 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
1325 vol->create();
1326
1327 mStubVolumes.push_back(vol);
1328 *outVolId = vol->getId();
1329 return android::OK;
1330}
1331
1332int VolumeManager::destroyStubVolume(const std::string& volId) {
1333 auto i = mStubVolumes.begin();
1334 while (i != mStubVolumes.end()) {
1335 if ((*i)->getId() == volId) {
1336 (*i)->destroy();
1337 i = mStubVolumes.erase(i);
1338 } else {
1339 ++i;
1340 }
1341 }
1342 return android::OK;
1343}
1344
Risan8f6198d2018-10-26 20:56:45 -06001345int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -06001346 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001347}
1348
Risan8f6198d2018-10-26 20:56:45 -06001349int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -06001350 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -06001351}
1352
1353int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -06001354 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001355}