blob: a667e57206fec056c013a0d5868b238679deae7e [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
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800330void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
331 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700332 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700333 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700334 disk->listVolumes(type, list);
335 }
336}
337
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600338int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700339 std::string normalizedGuid;
340 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
341 LOG(WARNING) << "Invalid GUID " << partGuid;
342 return -1;
343 }
344
Paul Crowleyc6433a22017-10-24 14:54:43 -0700345 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700346 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
347 if (unlink(keyPath.c_str()) != 0) {
348 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700349 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700350 }
Eric Biggersa701c452018-10-23 13:06:55 -0700351 if (fscrypt_is_native()) {
352 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700353 success = false;
354 }
355 }
356 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700357}
358
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700359int VolumeManager::linkPrimary(userid_t userId) {
360 std::string source(mPrimary->getPath());
361 if (mPrimary->isEmulated()) {
362 source = StringPrintf("%s/%d", source.c_str(), userId);
363 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
364 }
365
366 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
367 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
368 if (errno != ENOENT) {
369 PLOG(WARNING) << "Failed to unlink " << target;
370 }
371 }
372 LOG(DEBUG) << "Linking " << source << " to " << target;
373 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
374 PLOG(WARNING) << "Failed to link";
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700375 return -errno;
376 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700377 return 0;
378}
379
380int VolumeManager::mountPkgSpecificDir(const std::string& mntSourceRoot,
381 const std::string& mntTargetRoot,
382 const std::string& packageName, const char* dirName) {
383 std::string mntSourceDir =
384 StringPrintf("%s/Android/%s/%s", mntSourceRoot.c_str(), dirName, packageName.c_str());
385 std::string mntTargetDir =
386 StringPrintf("%s/Android/%s/%s", mntTargetRoot.c_str(), dirName, packageName.c_str());
387 if (umount2(mntTargetDir.c_str(), MNT_DETACH) == -1 && errno != EINVAL && errno != ENOENT) {
388 PLOG(ERROR) << "Failed to unmount " << mntTargetDir;
389 return -1;
390 }
391 if (TEMP_FAILURE_RETRY(mount(mntSourceDir.c_str(), mntTargetDir.c_str(), nullptr,
392 MS_BIND | MS_REC, nullptr)) == -1) {
393 PLOG(ERROR) << "Failed to mount " << mntSourceDir << " to " << mntTargetDir;
394 return -1;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700395 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700396 if (TEMP_FAILURE_RETRY(
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700397 mount(nullptr, mntTargetDir.c_str(), nullptr, MS_REC | MS_SLAVE, nullptr)) == -1) {
398 PLOG(ERROR) << "Failed to set MS_SLAVE at " << mntTargetDir;
399 return -1;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700400 }
401 return 0;
402}
403
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700404int VolumeManager::mountPkgSpecificDirsForRunningProcs(
405 userid_t userId, const std::vector<std::string>& packageNames,
406 const std::vector<std::string>& visibleVolLabels) {
407 // TODO: New processes could be started while traversing over the existing
408 // processes which would end up not having the necessary bind mounts. This
409 // issue needs to be fixed, may be by doing multiple passes here?
410 std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir("/proc"), closedir);
411 if (!dirp) {
412 PLOG(ERROR) << "Failed to opendir /proc";
413 return -1;
Sudheer Shankac7562092018-08-24 10:20:56 -0700414 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700415
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700416 std::string rootName;
417 // Figure out root namespace to compare against below
418 if (!android::vold::Readlinkat(dirfd(dirp.get()), "1/ns/mnt", &rootName)) {
419 PLOG(ERROR) << "Failed to read root namespace";
420 return -1;
Sudheer Shankaf768c272018-08-04 10:10:27 -0700421 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700422
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700423 struct stat fullWriteSb;
424 if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/write", &fullWriteSb)) == -1) {
425 PLOG(ERROR) << "Failed to stat /mnt/runtime/write";
426 return -1;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700427 }
428
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700429 std::unordered_set<appid_t> validAppIds;
430 for (auto& package : packageNames) {
431 validAppIds.insert(mAppIds[package]);
Sudheer Shankaa695f252018-08-03 18:07:52 -0700432 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700433 std::vector<std::string>& userPackages = mUserPackages[userId];
Sudheer Shanka53947a32018-08-01 10:24:13 -0700434
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700435 struct dirent* de;
436 // Poke through all running PIDs look for apps running in userId
437 while ((de = readdir(dirp.get()))) {
438 pid_t pid;
439 if (de->d_type != DT_DIR) continue;
440 if (!android::base::ParseInt(de->d_name, &pid)) continue;
441
442 const unique_fd pidFd(
443 openat(dirfd(dirp.get()), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC));
444 if (pidFd.get() < 0) {
445 PLOG(WARNING) << "Failed to open /proc/" << pid;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700446 continue;
447 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700448 struct stat sb;
449 if (fstat(pidFd.get(), &sb) != 0) {
450 PLOG(WARNING) << "Failed to stat " << de->d_name;
451 continue;
452 }
453 if (multiuser_get_user_id(sb.st_uid) != userId) {
454 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700455 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700456
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700457 // Matches so far, but refuse to touch if in root namespace
458 LOG(VERBOSE) << "Found matching PID " << de->d_name;
459 std::string pidName;
460 if (!android::vold::Readlinkat(pidFd.get(), "ns/mnt", &pidName)) {
461 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
462 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700463 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700464 if (rootName == pidName) {
465 LOG(WARNING) << "Skipping due to root namespace";
466 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700467 }
468
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700469 // Only update the mount points of processes running with one of validAppIds.
470 // This should skip any isolated uids.
471 appid_t appId = multiuser_get_app_id(sb.st_uid);
472 if (validAppIds.find(appId) == validAppIds.end()) {
473 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700474 }
475
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700476 std::vector<std::string> packagesForUid;
477 for (auto& package : userPackages) {
478 if (mAppIds[package] == appId) {
479 packagesForUid.push_back(package);
480 }
481 }
482 if (packagesForUid.empty()) {
483 continue;
484 }
485 const std::string& sandboxId = mSandboxIds[appId];
486
487 // We purposefully leave the namespace open across the fork
488 unique_fd nsFd(openat(pidFd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
489 if (nsFd.get() < 0) {
490 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
491 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700492 }
493
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700494 pid_t child;
495 if (!(child = fork())) {
496 if (setns(nsFd.get(), CLONE_NEWNS) != 0) {
497 PLOG(ERROR) << "Failed to setns for " << de->d_name;
498 _exit(1);
499 }
500
Sudheer Shanka03992e32018-12-12 12:43:38 -0800501 int mountMode = getMountModeForRunningProc(packagesForUid, userId, fullWriteSb);
502 if (mountMode == -1) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700503 _exit(1);
504 }
505
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700506 for (auto& volumeLabel : visibleVolLabels) {
507 std::string mntSource = StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str());
508 std::string mntTarget = StringPrintf("/storage/%s", volumeLabel.c_str());
509 if (volumeLabel == "emulated") {
510 StringAppendF(&mntSource, "/%d", userId);
511 StringAppendF(&mntTarget, "/%d", userId);
512 }
513 for (auto& package : packagesForUid) {
514 mountPkgSpecificDir(mntSource, mntTarget, package, "data");
515 mountPkgSpecificDir(mntSource, mntTarget, package, "media");
Sudheer Shanka03992e32018-12-12 12:43:38 -0800516 if (mountMode != VoldNativeService::REMOUNT_MODE_INSTALLER) {
517 mountPkgSpecificDir(mntSource, mntTarget, package, "obb");
518 }
519 }
520 if (mountMode == VoldNativeService::REMOUNT_MODE_INSTALLER) {
521 StringAppendF(&mntSource, "/Android/obb");
522 StringAppendF(&mntTarget, "/Android/obb");
523 if (TEMP_FAILURE_RETRY(mount(mntSource.c_str(), mntTarget.c_str(), nullptr,
524 MS_BIND | MS_REC, nullptr)) == -1) {
525 PLOG(ERROR) << "Failed to mount " << mntSource << " to " << mntTarget;
526 continue;
527 }
528 if (TEMP_FAILURE_RETRY(mount(nullptr, mntTarget.c_str(), nullptr,
529 MS_REC | MS_SLAVE, nullptr)) == -1) {
530 PLOG(ERROR) << "Failed to set MS_SLAVE at " << mntTarget.c_str();
531 continue;
532 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700533 }
534 }
535 _exit(0);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700536 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700537
538 if (child == -1) {
539 PLOG(ERROR) << "Failed to fork";
540 } else {
541 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
Sudheer Shanka53947a32018-08-01 10:24:13 -0700542 }
543 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700544 return 0;
545}
546
Sudheer Shanka03992e32018-12-12 12:43:38 -0800547int VolumeManager::getMountModeForRunningProc(const std::vector<std::string>& packagesForUid,
548 userid_t userId, struct stat& mntWriteStat) {
549 struct stat storageSb;
550 if (TEMP_FAILURE_RETRY(stat("/storage", &storageSb)) == -1) {
551 PLOG(ERROR) << "Failed to stat /storage";
552 return -1;
553 }
554
555 // Some packages have access to full external storage, identify processes belonging
556 // to those packages by comparing inode no.s of /mnt/runtime/write and /storage
557 if (storageSb.st_dev == mntWriteStat.st_dev && storageSb.st_ino == mntWriteStat.st_ino) {
558 return VoldNativeService::REMOUNT_MODE_FULL;
559 }
560
561 std::string obbMountFile =
562 StringPrintf("/mnt/user/%d/package/%s/obb_mount", userId, packagesForUid[0].c_str());
563 if (access(obbMountFile.c_str(), F_OK) == 0) {
564 return VoldNativeService::REMOUNT_MODE_INSTALLER;
565 }
566
567 // Some packages don't have access to external storage and processes belonging to
568 // those packages don't have anything mounted at /storage. So, identify those
569 // processes by comparing inode no.s of /mnt/user/%d/package/%s
570 // and /storage
571 for (auto& package : packagesForUid) {
572 std::string sandbox = StringPrintf("/mnt/user/%d/package/%s", userId, package.c_str());
573 struct stat sandboxStat;
574 if (TEMP_FAILURE_RETRY(stat(sandbox.c_str(), &sandboxStat)) == -1) {
575 PLOG(ERROR) << "Failed to stat " << sandbox;
576 return -1;
577 }
578 if (storageSb.st_dev == sandboxStat.st_dev && storageSb.st_ino == sandboxStat.st_ino) {
579 return VoldNativeService::REMOUNT_MODE_WRITE;
580 }
581 }
582 return VoldNativeService::REMOUNT_MODE_NONE;
583}
584
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700585int VolumeManager::prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames,
586 const std::vector<std::string>& visibleVolLabels) {
587 if (visibleVolLabels.empty()) {
588 return 0;
589 }
590 for (auto& volumeLabel : visibleVolLabels) {
591 std::string volumeRoot(StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str()));
592 bool isVolPrimaryEmulated = (volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated());
593 if (isVolPrimaryEmulated) {
594 StringAppendF(&volumeRoot, "/%d", userId);
595 if (fs_prepare_dir(volumeRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
596 PLOG(ERROR) << "fs_prepare_dir failed on " << volumeRoot;
597 return -errno;
598 }
599 }
600
601 std::string sandboxRoot =
602 prepareSubDirs(volumeRoot, "Android/sandbox/", 0700, AID_ROOT, AID_ROOT);
603 if (sandboxRoot.empty()) {
604 return -errno;
605 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700606
607 if (!createPkgSpecificDirRoots(volumeRoot)) {
608 return -errno;
609 }
610
611 std::string mntTargetRoot = StringPrintf("/mnt/user/%d", userId);
612 if (fs_prepare_dir(mntTargetRoot.c_str(), 0751, AID_ROOT, AID_ROOT) != 0) {
613 PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot;
614 return -errno;
615 }
616 mntTargetRoot.append("/package");
617 if (fs_prepare_dir(mntTargetRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
618 PLOG(ERROR) << "fs_prepare_dir failed on " << mntTargetRoot;
619 return -errno;
620 }
621
622 for (auto& packageName : packageNames) {
623 const auto& it = mAppIds.find(packageName);
624 if (it == mAppIds.end()) {
625 PLOG(ERROR) << "appId is not available for " << packageName;
626 continue;
627 }
628 appid_t appId = it->second;
629 std::string sandboxId = mSandboxIds[appId];
630 uid_t uid = multiuser_get_uid(userId, appId);
631
632 // [1] Create /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId>
633 // [2] Create /mnt/user/0/package/<packageName>/emulated/0
634 // Mount [1] at [2]
635 std::string pkgSandboxSourceDir = prepareSandboxSource(uid, sandboxId, sandboxRoot);
636 if (pkgSandboxSourceDir.empty()) {
637 return -errno;
638 }
639 std::string pkgSandboxTargetDir = prepareSandboxTarget(
640 packageName, uid, volumeLabel, mntTargetRoot, isVolPrimaryEmulated);
641 if (pkgSandboxTargetDir.empty()) {
642 return -errno;
643 }
644 if (umount2(pkgSandboxTargetDir.c_str(), MNT_DETACH) == -1 && errno != EINVAL &&
645 errno != ENOENT) {
646 PLOG(ERROR) << "Failed to unmount " << pkgSandboxTargetDir;
647 return -errno;
648 }
649 if (TEMP_FAILURE_RETRY(mount(pkgSandboxSourceDir.c_str(), pkgSandboxTargetDir.c_str(),
650 nullptr, MS_BIND | MS_REC, nullptr)) == -1) {
651 PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at "
652 << pkgSandboxTargetDir;
653 return -errno;
654 }
655 if (TEMP_FAILURE_RETRY(mount(nullptr, pkgSandboxTargetDir.c_str(), nullptr,
656 MS_SLAVE | MS_REC, nullptr)) == -1) {
657 PLOG(ERROR) << "Failed to mount " << pkgSandboxSourceDir << " at "
658 << pkgSandboxTargetDir;
659 return -errno;
660 }
661
662 // Create Android/{data,media,obb}/<packageName> segments at
663 // [1] /mnt/runtime/write/emulated/0/ and
664 // [2] /mnt/runtime/write/emulated/0/Android/sandbox/<sandboxId>/emulated/0/
665 if (!createPkgSpecificDirs(packageName, uid, volumeRoot, pkgSandboxSourceDir)) {
666 return -errno;
667 }
668
669 if (volumeLabel == mPrimary->getLabel()) {
670 // Create [1] /mnt/user/0/package/<packageName>/self/
671 // Already created [2] /mnt/user/0/package/<packageName>/emulated/0
672 // Mount [2] at [1]
673 std::string pkgPrimaryTargetDir =
674 StringPrintf("%s/%s/self", mntTargetRoot.c_str(), packageName.c_str());
675 if (fs_prepare_dir(pkgPrimaryTargetDir.c_str(), 0755, uid, uid) != 0) {
676 PLOG(ERROR) << "Failed to fs_prepare_dir on " << pkgPrimaryTargetDir;
677 return -errno;
678 }
679 StringAppendF(&pkgPrimaryTargetDir, "/primary");
680 std::string primarySource(mPrimary->getPath());
681 if (isVolPrimaryEmulated) {
682 StringAppendF(&primarySource, "/%d", userId);
683 }
684 if (TEMP_FAILURE_RETRY(unlink(pkgPrimaryTargetDir.c_str()))) {
685 if (errno != ENOENT) {
686 PLOG(ERROR) << "Failed to unlink " << pkgPrimaryTargetDir;
687 }
688 }
689 if (TEMP_FAILURE_RETRY(symlink(primarySource.c_str(), pkgPrimaryTargetDir.c_str()))) {
690 PLOG(ERROR) << "Failed to link " << primarySource << " at "
691 << pkgPrimaryTargetDir;
692 return -errno;
693 }
694 }
695 }
696 }
697 mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels);
698 return 0;
699}
700
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700701std::string VolumeManager::prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs,
702 mode_t mode, uid_t uid, gid_t gid) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700703 std::string path(pathPrefix);
704 std::vector<std::string> subDirList = android::base::Split(subDirs, "/");
705 for (size_t i = 0; i < subDirList.size(); ++i) {
706 std::string subDir = subDirList[i];
707 if (subDir.empty()) {
708 continue;
709 }
710 StringAppendF(&path, "/%s", subDir.c_str());
711 if (fs_prepare_dir(path.c_str(), mode, uid, gid) != 0) {
712 PLOG(ERROR) << "fs_prepare_dir failed on " << path;
713 return kEmptyString;
714 }
715 }
716 return path;
717}
718
719std::string VolumeManager::prepareSandboxSource(uid_t uid, const std::string& sandboxId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700720 const std::string& sandboxRootDir) {
Sudheer Shankaf768c272018-08-04 10:10:27 -0700721 std::string sandboxSourceDir(sandboxRootDir);
Sudheer Shanka51a38da2018-10-18 08:51:31 -0700722 StringAppendF(&sandboxSourceDir, "/%s", sandboxId.c_str());
Sudheer Shanka53947a32018-08-01 10:24:13 -0700723 if (fs_prepare_dir(sandboxSourceDir.c_str(), 0755, uid, uid) != 0) {
724 PLOG(ERROR) << "fs_prepare_dir failed on " << sandboxSourceDir;
725 return kEmptyString;
726 }
727 return sandboxSourceDir;
728}
729
730std::string VolumeManager::prepareSandboxTarget(const std::string& packageName, uid_t uid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700731 const std::string& volumeLabel,
732 const std::string& mntTargetRootDir,
733 bool isUserDependent) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700734 std::string segment;
735 if (isUserDependent) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700736 segment = StringPrintf("%s/%s/%d/", packageName.c_str(), volumeLabel.c_str(),
737 multiuser_get_user_id(uid));
Sudheer Shanka53947a32018-08-01 10:24:13 -0700738 } else {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700739 segment = StringPrintf("%s/%s/", packageName.c_str(), volumeLabel.c_str());
Sudheer Shanka53947a32018-08-01 10:24:13 -0700740 }
741 return prepareSubDirs(mntTargetRootDir, segment.c_str(), 0755, uid, uid);
742}
743
744std::string VolumeManager::preparePkgDataSource(const std::string& packageName, uid_t uid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700745 const std::string& dataRootDir) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700746 std::string dataSourceDir = StringPrintf("%s/%s", dataRootDir.c_str(), packageName.c_str());
747 if (fs_prepare_dir(dataSourceDir.c_str(), 0755, uid, uid) != 0) {
748 PLOG(ERROR) << "fs_prepare_dir failed on " << dataSourceDir;
749 return kEmptyString;
750 }
751 return dataSourceDir;
752}
753
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700754bool VolumeManager::createPkgSpecificDirRoots(const std::string& volumeRoot) {
755 std::string volumeAndroidRoot = StringPrintf("%s/Android", volumeRoot.c_str());
756 if (fs_prepare_dir(volumeAndroidRoot.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
757 PLOG(ERROR) << "fs_prepare_dir failed on " << volumeAndroidRoot;
758 return false;
759 }
760 std::array<std::string, 3> dirs = {"data", "media", "obb"};
761 for (auto& dir : dirs) {
762 std::string path = StringPrintf("%s/%s", volumeAndroidRoot.c_str(), dir.c_str());
763 if (fs_prepare_dir(path.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
764 PLOG(ERROR) << "fs_prepare_dir failed on " << path;
765 return false;
766 }
767 }
768 return true;
769}
770
771bool VolumeManager::createPkgSpecificDirs(const std::string& packageName, uid_t uid,
772 const std::string& volumeRoot,
773 const std::string& sandboxDirRoot) {
774 std::array<std::string, 3> dirs = {"data", "media", "obb"};
775 for (auto& dir : dirs) {
776 std::string sourceDir = StringPrintf("%s/Android/%s", volumeRoot.c_str(), dir.c_str());
777 if (prepareSubDirs(sourceDir, packageName, 0755, uid, uid).empty()) {
778 return false;
779 }
780 std::string sandboxSegment =
781 StringPrintf("Android/%s/%s/", dir.c_str(), packageName.c_str());
782 if (prepareSubDirs(sandboxDirRoot, sandboxSegment, 0755, uid, uid).empty()) {
783 return false;
784 }
785 }
786 return true;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700787}
788
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700789int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
790 mAddedUsers[userId] = userSerialNumber;
791 return 0;
792}
793
794int VolumeManager::onUserRemoved(userid_t userId) {
795 mAddedUsers.erase(userId);
796 return 0;
797}
798
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700799int VolumeManager::onUserStarted(userid_t userId, const std::vector<std::string>& packageNames,
800 const std::vector<int>& appIds,
801 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700802 LOG(VERBOSE) << "onUserStarted: " << userId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700803 // Note that sometimes the system will spin up processes from Zygote
804 // before actually starting the user, so we're okay if Zygote
805 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600806 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700807 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
808
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700809 mStartedUsers.insert(userId);
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700810 mUserPackages[userId] = packageNames;
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700811 for (size_t i = 0; i < packageNames.size(); ++i) {
812 mAppIds[packageNames[i]] = appIds[i];
813 mSandboxIds[appIds[i]] = sandboxIds[i];
814 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700815 if (mPrimary) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700816 linkPrimary(userId);
817 }
818 if (GetBoolProperty(kIsolatedStorage, false)) {
819 std::vector<std::string> visibleVolLabels;
820 for (auto& volId : mVisibleVolumeIds) {
821 auto vol = findVolume(volId);
822 userid_t mountUserId = vol->getMountUserId();
823 if (mountUserId == userId || vol->isEmulated()) {
824 visibleVolLabels.push_back(vol->getLabel());
825 }
826 }
827 if (prepareSandboxes(userId, packageNames, visibleVolLabels) != 0) {
828 return -errno;
829 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700830 }
831 return 0;
832}
833
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700834int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700835 LOG(VERBOSE) << "onUserStopped: " << userId;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700836 mStartedUsers.erase(userId);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700837
Sudheer Shanka9acc6d42018-10-06 19:03:02 -0700838 if (GetBoolProperty(kIsolatedStorage, false)) {
839 mUserPackages.erase(userId);
840 std::string mntTargetDir = StringPrintf("/mnt/user/%d", userId);
841 if (android::vold::UnmountTree(mntTargetDir) != 0) {
842 PLOG(ERROR) << "unmountTree on " << mntTargetDir << " failed";
843 return -errno;
844 }
845 if (android::vold::DeleteDirContentsAndDir(mntTargetDir) < 0) {
846 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << mntTargetDir;
847 return -errno;
848 }
849 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << mntTargetDir;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700850 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700851 return 0;
852}
853
Sudheer Shankad484aa92018-07-31 10:07:34 -0700854int VolumeManager::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700855 const std::vector<int32_t>& appIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700856 for (size_t i = 0; i < packageNames.size(); ++i) {
857 mAppIds[packageNames[i]] = appIds[i];
858 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700859 return 0;
860}
861
862int VolumeManager::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700863 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700864 for (size_t i = 0; i < appIds.size(); ++i) {
865 mSandboxIds[appIds[i]] = sandboxIds[i];
866 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700867 return 0;
868}
869
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700870int VolumeManager::prepareSandboxForApp(const std::string& packageName, appid_t appId,
871 const std::string& sandboxId, userid_t userId) {
Sudheer Shankac7562092018-08-24 10:20:56 -0700872 if (!GetBoolProperty(kIsolatedStorage, false)) {
873 return 0;
874 } else if (mStartedUsers.find(userId) == mStartedUsers.end()) {
875 // User not started, no need to do anything now. Required bind mounts for the package will
876 // be created when the user starts.
877 return 0;
878 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700879 LOG(VERBOSE) << "prepareSandboxForApp: " << packageName << ", appId=" << appId
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700880 << ", sandboxId=" << sandboxId << ", userId=" << userId;
Sudheer Shankac7562092018-08-24 10:20:56 -0700881 mUserPackages[userId].push_back(packageName);
882 mAppIds[packageName] = appId;
883 mSandboxIds[appId] = sandboxId;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700884
885 std::vector<std::string> visibleVolLabels;
886 for (auto& volId : mVisibleVolumeIds) {
887 auto vol = findVolume(volId);
888 userid_t mountUserId = vol->getMountUserId();
889 if (mountUserId == userId || vol->isEmulated()) {
890 visibleVolLabels.push_back(vol->getLabel());
891 }
Sudheer Shankac7562092018-08-24 10:20:56 -0700892 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700893 return prepareSandboxes(userId, {packageName}, visibleVolLabels);
Sudheer Shankac7562092018-08-24 10:20:56 -0700894}
895
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700896int VolumeManager::destroySandboxForApp(const std::string& packageName,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700897 const std::string& sandboxId, userid_t userId) {
898 if (!GetBoolProperty(kIsolatedStorage, false)) {
899 return 0;
900 }
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700901 LOG(VERBOSE) << "destroySandboxForApp: " << packageName << ", sandboxId=" << sandboxId
902 << ", userId=" << userId;
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700903 auto& userPackages = mUserPackages[userId];
Sudheer Shankaba0c62f2018-12-16 18:22:34 -0800904 userPackages.erase(std::remove(userPackages.begin(), userPackages.end(), packageName),
905 userPackages.end());
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700906 // If the package is not uninstalled in any other users, remove appId and sandboxId
907 // corresponding to it from the internal state.
908 bool installedInAnyUser = false;
909 for (auto& it : mUserPackages) {
910 auto& packages = it.second;
911 if (std::find(packages.begin(), packages.end(), packageName) != packages.end()) {
912 installedInAnyUser = true;
913 break;
914 }
915 }
916 if (!installedInAnyUser) {
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700917 const auto& entry = mAppIds.find(packageName);
918 if (entry != mAppIds.end()) {
919 mSandboxIds.erase(entry->second);
920 mAppIds.erase(entry);
921 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700922 }
923
924 std::vector<std::string> visibleVolLabels;
925 for (auto& volId : mVisibleVolumeIds) {
926 auto vol = findVolume(volId);
927 userid_t mountUserId = vol->getMountUserId();
928 if (mountUserId == userId || vol->isEmulated()) {
929 if (destroySandboxForAppOnVol(packageName, sandboxId, userId, vol->getLabel()) < 0) {
930 return -errno;
931 }
932 }
933 }
934 return 0;
935}
936
937int VolumeManager::destroySandboxForAppOnVol(const std::string& packageName,
938 const std::string& sandboxId, userid_t userId,
939 const std::string& volLabel) {
940 LOG(VERBOSE) << "destroySandboxOnVol: " << packageName << ", userId=" << userId
941 << ", volLabel=" << volLabel;
942 std::string pkgSandboxTarget =
943 StringPrintf("/mnt/user/%d/package/%s", userId, packageName.c_str());
944 if (android::vold::UnmountTree(pkgSandboxTarget)) {
945 PLOG(ERROR) << "UnmountTree failed on " << pkgSandboxTarget;
946 }
947
948 std::string sandboxDir = StringPrintf("/mnt/runtime/write/%s", volLabel.c_str());
949 if (volLabel == mPrimary->getLabel() && mPrimary->isEmulated()) {
950 StringAppendF(&sandboxDir, "/%d", userId);
951 }
Sudheer Shanka51a38da2018-10-18 08:51:31 -0700952 StringAppendF(&sandboxDir, "/Android/sandbox/%s", sandboxId.c_str());
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700953
954 if (android::vold::DeleteDirContentsAndDir(sandboxDir) < 0) {
955 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << sandboxDir;
956 return -errno;
957 }
958 return 0;
959}
960
Jeff Sharkey401b2602017-12-14 22:15:20 -0700961int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
962 mSecureKeyguardShowing = isShowing;
963 if (!mSecureKeyguardShowing) {
964 // Now that secure keyguard has been dismissed, process
965 // any pending disks
966 for (const auto& disk : mPendingDisks) {
967 disk->create();
968 mDisks.push_back(disk);
969 }
970 mPendingDisks.clear();
971 }
972 return 0;
973}
974
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700975int VolumeManager::onVolumeMounted(android::vold::VolumeBase* vol) {
976 if (!GetBoolProperty(kIsolatedStorage, false)) {
977 return 0;
978 }
979
980 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_VISIBLE) == 0) {
981 return 0;
982 }
983
984 mVisibleVolumeIds.insert(vol->getId());
985 userid_t mountUserId = vol->getMountUserId();
986 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
987 // We don't want to create another shared_ptr here because then we will end up with
988 // two shared_ptrs owning the underlying pointer without sharing it.
989 mPrimary = findVolume(vol->getId());
990 for (userid_t userId : mStartedUsers) {
991 if (linkPrimary(userId) != 0) {
992 return -errno;
993 }
994 }
995 }
996 if (vol->isEmulated()) {
997 for (userid_t userId : mStartedUsers) {
998 if (prepareSandboxes(userId, mUserPackages[userId], {vol->getLabel()}) != 0) {
999 return -errno;
1000 }
1001 }
1002 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
1003 if (prepareSandboxes(mountUserId, mUserPackages[mountUserId], {vol->getLabel()}) != 0) {
1004 return -errno;
1005 }
1006 }
1007 return 0;
1008}
1009
1010int VolumeManager::onVolumeUnmounted(android::vold::VolumeBase* vol) {
1011 if (!GetBoolProperty(kIsolatedStorage, false)) {
1012 return 0;
1013 }
1014
1015 if (mVisibleVolumeIds.erase(vol->getId()) == 0) {
1016 return 0;
1017 }
1018
1019 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
1020 mPrimary = nullptr;
1021 }
1022
1023 LOG(VERBOSE) << "visibleVolumeUnmounted: " << vol;
1024 userid_t mountUserId = vol->getMountUserId();
1025 if (vol->isEmulated()) {
1026 for (userid_t userId : mStartedUsers) {
1027 if (destroySandboxesForVol(vol, userId) != 0) {
1028 return -errno;
1029 }
1030 }
1031 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
1032 if (destroySandboxesForVol(vol, mountUserId) != 0) {
1033 return -errno;
1034 }
1035 }
1036 return 0;
1037}
1038
1039int VolumeManager::destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId) {
1040 LOG(VERBOSE) << "destroysandboxesForVol: " << vol << " for user=" << userId;
1041 const std::vector<std::string>& packageNames = mUserPackages[userId];
1042 for (auto& packageName : packageNames) {
1043 std::string volSandboxRoot = StringPrintf("/mnt/user/%d/package/%s/%s", userId,
1044 packageName.c_str(), vol->getLabel().c_str());
1045 if (android::vold::UnmountTree(volSandboxRoot) != 0) {
1046 PLOG(ERROR) << "unmountTree on " << volSandboxRoot << " failed";
1047 continue;
1048 }
1049 if (android::vold::DeleteDirContentsAndDir(volSandboxRoot) < 0) {
1050 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << volSandboxRoot;
1051 continue;
1052 }
1053 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << volSandboxRoot;
1054 }
1055 return 0;
1056}
1057
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001058int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001059 if (GetBoolProperty(kIsolatedStorage, false)) {
1060 return 0;
1061 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001062 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001063 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001064 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001065 }
1066 return 0;
1067}
1068
Jeff Sharkey66270a22015-06-24 11:49:24 -07001069int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
Sudheer Shanka53947a32018-08-01 10:24:13 -07001070 // If the isolated storage is enabled, return -1 since in the isolated storage world, there
1071 // are no longer any runtime storage permissions, so this shouldn't be called anymore.
1072 if (GetBoolProperty(kIsolatedStorage, false)) {
1073 return -1;
1074 }
Jeff Sharkey66270a22015-06-24 11:49:24 -07001075 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
1076
1077 DIR* dir;
1078 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001079 std::string rootName;
1080 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001081 int pidFd;
1082 int nsFd;
1083 struct stat sb;
1084 pid_t child;
1085
1086 if (!(dir = opendir("/proc"))) {
1087 PLOG(ERROR) << "Failed to opendir";
1088 return -1;
1089 }
1090
1091 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -06001092 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
1093 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001094 closedir(dir);
1095 return -1;
1096 }
1097
1098 // Poke through all running PIDs look for apps running as UID
1099 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -07001100 pid_t pid;
1101 if (de->d_type != DT_DIR) continue;
1102 if (!android::base::ParseInt(de->d_name, &pid)) continue;
1103
Jeff Sharkey66270a22015-06-24 11:49:24 -07001104 pidFd = -1;
1105 nsFd = -1;
1106
1107 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1108 if (pidFd < 0) {
1109 goto next;
1110 }
1111 if (fstat(pidFd, &sb) != 0) {
1112 PLOG(WARNING) << "Failed to stat " << de->d_name;
1113 goto next;
1114 }
1115 if (sb.st_uid != uid) {
1116 goto next;
1117 }
1118
1119 // Matches so far, but refuse to touch if in root namespace
1120 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001121 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001122 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
1123 goto next;
1124 }
Jeff Sharkey3472e522017-10-06 18:02:53 -06001125 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001126 LOG(WARNING) << "Skipping due to root namespace";
1127 goto next;
1128 }
1129
1130 // We purposefully leave the namespace open across the fork
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001131 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -07001132 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001133 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001134 goto next;
1135 }
1136
1137 if (!(child = fork())) {
1138 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001139 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001140 _exit(1);
1141 }
1142
Sudheer Shanka99d304a2018-09-27 14:53:51 -07001143 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -07001144
1145 std::string storageSource;
1146 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001147 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001148 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001149 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001150 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001151 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001152 } else {
1153 // Sane default of no storage visible
1154 _exit(0);
1155 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001156 if (TEMP_FAILURE_RETRY(
1157 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
1158 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001159 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -07001160 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001161 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
1162 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +09001163 _exit(1);
1164 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001165
1166 // Mount user-specific symlink helper into place
1167 userid_t user_id = multiuser_get_user_id(uid);
1168 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001169 if (TEMP_FAILURE_RETRY(
1170 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
1171 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001172 _exit(1);
1173 }
1174
Jeff Sharkey66270a22015-06-24 11:49:24 -07001175 _exit(0);
1176 }
1177
1178 if (child == -1) {
1179 PLOG(ERROR) << "Failed to fork";
1180 goto next;
1181 } else {
1182 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
1183 }
1184
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001185 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -07001186 close(nsFd);
1187 close(pidFd);
1188 }
1189 closedir(dir);
1190 return 0;
1191}
1192
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001193int VolumeManager::reset() {
1194 // Tear down all existing disks/volumes and start from a blank slate so
1195 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +08001196 if (mInternalEmulated != nullptr) {
1197 mInternalEmulated->destroy();
1198 mInternalEmulated->create();
1199 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001200 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001201 disk->destroy();
1202 disk->create();
1203 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -06001204 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001205 mAddedUsers.clear();
1206 mStartedUsers.clear();
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -07001207
1208 mUserPackages.clear();
1209 mAppIds.clear();
1210 mSandboxIds.clear();
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001211 mVisibleVolumeIds.clear();
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -07001212
1213 // For unmounting dirs under /mnt/user/<user-id>/package/<package-name>
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001214 android::vold::UnmountTree("/mnt/user/");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001215 return 0;
1216}
1217
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001218// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001219int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001220 if (mInternalEmulated == nullptr) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001221 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001222 }
Paul Crowley56292ef2017-10-20 08:07:53 -07001223 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001224 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001225 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001226 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001227 disk->destroy();
1228 }
Risan8c9f3322018-10-29 08:52:56 +09001229 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001230 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -07001231 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -07001232 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001233 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -07001234}
1235
Jeff Sharkey9c484982015-03-31 10:35:33 -07001236int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001237 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -06001238 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001239
Jeff Sharkey9c484982015-03-31 10:35:33 -07001240 // First, try gracefully unmounting all known devices
1241 if (mInternalEmulated != nullptr) {
1242 mInternalEmulated->unmount();
1243 }
Risan8c9f3322018-10-29 08:52:56 +09001244 for (const auto& stub : mStubVolumes) {
1245 stub->unmount();
1246 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001247 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001248 disk->unmountAll();
1249 }
1250
1251 // Worst case we might have some stale mounts lurking around, so
1252 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +08001253 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -07001254 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001255 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -07001256 return -errno;
1257 }
1258
1259 // Some volumes can be stacked on each other, so force unmount in
1260 // reverse order to give us the best chance of success.
1261 std::list<std::string> toUnmount;
1262 mntent* mentry;
1263 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001264 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001265 if ((StartsWith(test, "/mnt/") &&
1266#ifdef __ANDROID_DEBUGGABLE__
1267 !StartsWith(test, "/mnt/scratch") &&
1268#endif
1269 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
1270 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001271 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -07001272 }
1273 }
1274 endmntent(fp);
1275
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001276 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001277 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001278 android::vold::ForceUnmount(path);
1279 }
1280
1281 return 0;
1282}
1283
Jeff Sharkey3472e522017-10-06 18:02:53 -06001284int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001285 // Only offer to create directories for paths managed by vold
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001286 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001287 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -06001288 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001289 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001290 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001291 return -EINVAL;
1292 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001293}
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001294
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001295int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001296 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001297 int id = mNextObbId++;
1298
1299 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001300 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001301 vol->create();
1302
1303 mObbVolumes.push_back(vol);
1304 *outVolId = vol->getId();
1305 return android::OK;
1306}
1307
1308int VolumeManager::destroyObb(const std::string& volId) {
1309 auto i = mObbVolumes.begin();
1310 while (i != mObbVolumes.end()) {
1311 if ((*i)->getId() == volId) {
1312 (*i)->destroy();
1313 i = mObbVolumes.erase(i);
1314 } else {
1315 ++i;
1316 }
1317 }
1318 return android::OK;
1319}
1320
Risan8c9f3322018-10-29 08:52:56 +09001321int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
1322 const std::string& fsType, const std::string& fsUuid,
1323 const std::string& fsLabel, std::string* outVolId) {
1324 int id = mNextStubVolumeId++;
1325 auto vol = std::shared_ptr<android::vold::VolumeBase>(
1326 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
1327 vol->create();
1328
1329 mStubVolumes.push_back(vol);
1330 *outVolId = vol->getId();
1331 return android::OK;
1332}
1333
1334int VolumeManager::destroyStubVolume(const std::string& volId) {
1335 auto i = mStubVolumes.begin();
1336 while (i != mStubVolumes.end()) {
1337 if ((*i)->getId() == volId) {
1338 (*i)->destroy();
1339 i = mStubVolumes.erase(i);
1340 } else {
1341 ++i;
1342 }
1343 }
1344 return android::OK;
1345}
1346
Risan8f6198d2018-10-26 20:56:45 -06001347int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -06001348 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001349}
1350
Risan8f6198d2018-10-26 20:56:45 -06001351int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -06001352 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -06001353}
1354
1355int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -06001356 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001357}