blob: 358d2eb53dd7556e205e721364ac76dac5460617 [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 Shankaf9b38a52019-02-14 19:09:51 +000071using android::OK;
Sudheer Shanka53947a32018-08-01 10:24:13 -070072using android::base::GetBoolProperty;
Mark Salyzyn86e81e72018-09-20 10:09:27 -070073using android::base::StartsWith;
Sudheer Shanka53947a32018-08-01 10:24:13 -070074using android::base::StringAppendF;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060076using android::base::unique_fd;
Sudheer Shanka023b5392019-02-06 12:39:19 -080077using android::vold::BindMount;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +000078using android::vold::CreateDir;
Sudheer Shanka30df1c62019-02-22 17:03:02 -080079using android::vold::DeleteDirContents;
Sudheer Shanka023b5392019-02-06 12:39:19 -080080using android::vold::DeleteDirContentsAndDir;
81using android::vold::Symlink;
82using android::vold::Unlink;
83using android::vold::UnmountTree;
Sudheer Shanka03992e32018-12-12 12:43:38 -080084using android::vold::VoldNativeService;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070085
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060086static const char* kPathUserMount = "/mnt/user";
87static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
88
Sudheer Shanka53947a32018-08-01 10:24:13 -070089static const char* kIsolatedStorage = "persist.sys.isolated_storage";
Sudheer Shanka34b437b2019-01-17 17:20:47 -080090static const char* kIsolatedStorageSnapshot = "sys.isolated_storage_snapshot";
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060091static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
92
Sudheer Shanka53947a32018-08-01 10:24:13 -070093static const std::string kEmptyString("");
94
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060095/* 512MiB is large enough for testing purposes */
96static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097
Jeff Sharkey36801cc2015-03-13 16:09:20 -070098static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080099static const unsigned int kMajorBlockExperimentalMin = 240;
100static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700102VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700103
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700104VolumeManager* VolumeManager::Instance() {
105 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -0700106 return sInstance;
107}
108
109VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800110 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600111 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +0900112 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700113 // For security reasons, assume that a secure keyguard is
114 // showing until we hear otherwise
115 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700116}
117
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700118VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800119
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800120static bool hasIsolatedStorage() {
Jeff Sharkeya6082502019-02-11 12:18:48 -0700121 return GetBoolProperty(kIsolatedStorageSnapshot, GetBoolProperty(kIsolatedStorage, true));
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800122}
123
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600124int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600125 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700126 if (GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600127 if (access(kPathVirtualDisk, F_OK) != 0) {
128 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
129 }
130
131 if (mVirtualDisk == nullptr) {
132 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
133 LOG(ERROR) << "Failed to create virtual disk";
134 return -1;
135 }
136
137 struct stat buf;
138 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
139 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
140 return -1;
141 }
142
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700143 auto disk = new android::vold::Disk(
144 "virtual", buf.st_rdev, "virtual",
145 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600146 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700147 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600148 }
149 } else {
150 if (mVirtualDisk != nullptr) {
151 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700152 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600153
154 Loop::destroyByDevice(mVirtualDiskPath.c_str());
155 mVirtualDisk = nullptr;
156 }
157
158 if (access(kPathVirtualDisk, F_OK) == 0) {
159 unlink(kPathVirtualDisk);
160 }
161 }
162 return 0;
163}
164
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700165int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800166 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700167 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800168}
169
San Mehatf1b736b2009-10-10 17:22:08 -0700170int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600171 ATRACE_NAME("VolumeManager::start");
172
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700173 // Always start from a clean slate by unmounting everything in
174 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700175 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700176
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600177 Devmapper::destroyAll();
178 Loop::destroyAll();
179
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700180 // Assume that we always have an emulated volume on internal
181 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700182 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700184 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700185 mInternalEmulated->create();
186
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600187 // Consider creating a virtual disk
188 updateVirtualDisk();
189
San Mehatf1b736b2009-10-10 17:22:08 -0700190 return 0;
191}
192
193int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700194 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700195 mInternalEmulated->destroy();
196 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700197 return 0;
198}
199
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700200void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700201 std::lock_guard<std::mutex> lock(mLock);
202
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700203 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700204 LOG(DEBUG) << "----------------";
205 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700206 evt->dump();
207 }
San Mehatf1b736b2009-10-10 17:22:08 -0700208
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700209 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
210 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700211
212 if (devType != "disk") return;
213
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600214 int major = std::stoi(evt->findParam("MAJOR"));
215 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700216 dev_t device = makedev(major, minor);
217
218 switch (evt->getAction()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700219 case NetlinkEvent::Action::kAdd: {
220 for (const auto& source : mDiskSources) {
221 if (source->matches(eventPath)) {
222 // For now, assume that MMC and virtio-blk (the latter is
223 // emulator-specific; see Disk.cpp for details) devices are SD,
224 // and that everything else is USB
225 int flags = source->getFlags();
226 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
227 major >= (int)kMajorBlockExperimentalMin &&
228 major <= (int)kMajorBlockExperimentalMax)) {
229 flags |= android::vold::Disk::Flags::kSd;
230 } else {
231 flags |= android::vold::Disk::Flags::kUsb;
232 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700233
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700234 auto disk =
235 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
236 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
237 break;
238 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700239 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700240 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700242 case NetlinkEvent::Action::kChange: {
243 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
244 handleDiskChanged(device);
245 break;
246 }
247 case NetlinkEvent::Action::kRemove: {
248 handleDiskRemoved(device);
249 break;
250 }
251 default: {
252 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
253 break;
254 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700255 }
256}
257
Jeff Sharkey401b2602017-12-14 22:15:20 -0700258void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
259 // For security reasons, if secure keyguard is showing, wait
260 // until the user unlocks the device to actually touch it
261 if (mSecureKeyguardShowing) {
262 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700263 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700264 mPendingDisks.push_back(disk);
265 } else {
266 disk->create();
267 mDisks.push_back(disk);
268 }
269}
270
271void VolumeManager::handleDiskChanged(dev_t device) {
272 for (const auto& disk : mDisks) {
273 if (disk->getDevice() == device) {
274 disk->readMetadata();
275 disk->readPartitions();
276 }
277 }
278
279 // For security reasons, we ignore all pending disks, since
280 // we'll scan them once the device is unlocked
281}
282
283void VolumeManager::handleDiskRemoved(dev_t device) {
284 auto i = mDisks.begin();
285 while (i != mDisks.end()) {
286 if ((*i)->getDevice() == device) {
287 (*i)->destroy();
288 i = mDisks.erase(i);
289 } else {
290 ++i;
291 }
292 }
293 auto j = mPendingDisks.begin();
294 while (j != mPendingDisks.end()) {
295 if ((*j)->getDevice() == device) {
296 j = mPendingDisks.erase(j);
297 } else {
298 ++j;
299 }
300 }
301}
302
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700303void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800304 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700305 mDiskSources.push_back(diskSource);
306}
307
308std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
309 for (auto disk : mDisks) {
310 if (disk->getId() == id) {
311 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700312 }
313 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700314 return nullptr;
315}
San Mehatf1b736b2009-10-10 17:22:08 -0700316
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700317std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800318 // Vold could receive "mount" after "shutdown" command in the extreme case.
319 // If this happens, mInternalEmulated will equal nullptr and
320 // we need to deal with it in order to avoid null pointer crash.
321 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700322 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700323 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700324 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700325 auto vol = disk->findVolume(id);
326 if (vol != nullptr) {
327 return vol;
328 }
329 }
Risan8c9f3322018-10-29 08:52:56 +0900330 for (const auto& vol : mStubVolumes) {
331 if (vol->getId() == id) {
332 return vol;
333 }
334 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600335 for (const auto& vol : mObbVolumes) {
336 if (vol->getId() == id) {
337 return vol;
338 }
339 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700340 return nullptr;
341}
342
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800343void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
344 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700345 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700346 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700347 disk->listVolumes(type, list);
348 }
349}
350
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600351int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700352 std::string normalizedGuid;
353 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
354 LOG(WARNING) << "Invalid GUID " << partGuid;
355 return -1;
356 }
357
Paul Crowleyc6433a22017-10-24 14:54:43 -0700358 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700359 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
360 if (unlink(keyPath.c_str()) != 0) {
361 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700362 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700363 }
Eric Biggersa701c452018-10-23 13:06:55 -0700364 if (fscrypt_is_native()) {
365 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700366 success = false;
367 }
368 }
369 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700370}
371
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700372int VolumeManager::linkPrimary(userid_t userId) {
373 std::string source(mPrimary->getPath());
374 if (mPrimary->isEmulated()) {
375 source = StringPrintf("%s/%d", source.c_str(), userId);
376 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
377 }
378
379 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700380 LOG(DEBUG) << "Linking " << source << " to " << target;
Sudheer Shanka023b5392019-02-06 12:39:19 -0800381 Symlink(source, target);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700382 return 0;
383}
384
385int VolumeManager::mountPkgSpecificDir(const std::string& mntSourceRoot,
386 const std::string& mntTargetRoot,
387 const std::string& packageName, const char* dirName) {
388 std::string mntSourceDir =
389 StringPrintf("%s/Android/%s/%s", mntSourceRoot.c_str(), dirName, packageName.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000390 if (CreateDir(mntSourceDir, 0755) < 0) {
391 return -errno;
392 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700393 std::string mntTargetDir =
394 StringPrintf("%s/Android/%s/%s", mntTargetRoot.c_str(), dirName, packageName.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000395 if (CreateDir(mntTargetDir, 0755) < 0) {
396 return -errno;
397 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800398 return BindMount(mntSourceDir, mntTargetDir);
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700399}
400
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700401int VolumeManager::mountPkgSpecificDirsForRunningProcs(
402 userid_t userId, const std::vector<std::string>& packageNames,
Sudheer Shanka817b9112018-12-13 17:40:28 -0800403 const std::vector<std::string>& visibleVolLabels, int remountMode) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700404 // TODO: New processes could be started while traversing over the existing
405 // processes which would end up not having the necessary bind mounts. This
406 // issue needs to be fixed, may be by doing multiple passes here?
407 std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir("/proc"), closedir);
408 if (!dirp) {
409 PLOG(ERROR) << "Failed to opendir /proc";
410 return -1;
Sudheer Shankac7562092018-08-24 10:20:56 -0700411 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700412
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700413 std::string rootName;
414 // Figure out root namespace to compare against below
415 if (!android::vold::Readlinkat(dirfd(dirp.get()), "1/ns/mnt", &rootName)) {
416 PLOG(ERROR) << "Failed to read root namespace";
417 return -1;
Sudheer Shankaf768c272018-08-04 10:10:27 -0700418 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700419
Sudheer Shanka55049012019-01-09 12:15:15 -0800420 struct stat mntFullSb;
421 struct stat mntWriteSb;
422 if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/full", &mntFullSb)) == -1) {
423 PLOG(ERROR) << "Failed to stat /mnt/runtime/full";
424 return -1;
425 }
426 if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/write", &mntWriteSb)) == -1) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700427 PLOG(ERROR) << "Failed to stat /mnt/runtime/write";
428 return -1;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700429 }
430
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800431 std::string obbMountDir = StringPrintf("/mnt/user/%d/obb_mount", userId);
432 if (fs_prepare_dir(obbMountDir.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
433 PLOG(ERROR) << "Failed to fs_prepare_dir " << obbMountDir;
434 return -1;
435 }
436 const unique_fd obbMountDirFd(
437 TEMP_FAILURE_RETRY(open(obbMountDir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)));
438 if (obbMountDirFd.get() < 0) {
439 PLOG(ERROR) << "Failed to open " << obbMountDir;
440 return -1;
441 }
442
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700443 std::unordered_set<appid_t> validAppIds;
444 for (auto& package : packageNames) {
445 validAppIds.insert(mAppIds[package]);
Sudheer Shankaa695f252018-08-03 18:07:52 -0700446 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700447 std::vector<std::string>& userPackages = mUserPackages[userId];
Sudheer Shanka53947a32018-08-01 10:24:13 -0700448
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800449 std::vector<pid_t> childPids;
450
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700451 struct dirent* de;
452 // Poke through all running PIDs look for apps running in userId
453 while ((de = readdir(dirp.get()))) {
454 pid_t pid;
455 if (de->d_type != DT_DIR) continue;
456 if (!android::base::ParseInt(de->d_name, &pid)) continue;
457
458 const unique_fd pidFd(
459 openat(dirfd(dirp.get()), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC));
460 if (pidFd.get() < 0) {
461 PLOG(WARNING) << "Failed to open /proc/" << pid;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700462 continue;
463 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700464 struct stat sb;
465 if (fstat(pidFd.get(), &sb) != 0) {
466 PLOG(WARNING) << "Failed to stat " << de->d_name;
467 continue;
468 }
469 if (multiuser_get_user_id(sb.st_uid) != userId) {
470 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700471 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700472
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700473 // Matches so far, but refuse to touch if in root namespace
474 LOG(VERBOSE) << "Found matching PID " << de->d_name;
475 std::string pidName;
476 if (!android::vold::Readlinkat(pidFd.get(), "ns/mnt", &pidName)) {
477 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
478 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700479 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700480 if (rootName == pidName) {
481 LOG(WARNING) << "Skipping due to root namespace";
482 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700483 }
484
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700485 // Only update the mount points of processes running with one of validAppIds.
486 // This should skip any isolated uids.
487 appid_t appId = multiuser_get_app_id(sb.st_uid);
488 if (validAppIds.find(appId) == validAppIds.end()) {
489 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700490 }
491
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700492 std::vector<std::string> packagesForUid;
493 for (auto& package : userPackages) {
494 if (mAppIds[package] == appId) {
495 packagesForUid.push_back(package);
496 }
497 }
498 if (packagesForUid.empty()) {
499 continue;
500 }
501 const std::string& sandboxId = mSandboxIds[appId];
502
503 // We purposefully leave the namespace open across the fork
504 unique_fd nsFd(openat(pidFd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
505 if (nsFd.get() < 0) {
506 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
507 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700508 }
509
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700510 pid_t child;
511 if (!(child = fork())) {
512 if (setns(nsFd.get(), CLONE_NEWNS) != 0) {
513 PLOG(ERROR) << "Failed to setns for " << de->d_name;
514 _exit(1);
515 }
516
Sudheer Shanka817b9112018-12-13 17:40:28 -0800517 int mountMode;
518 if (remountMode == -1) {
Sudheer Shanka55049012019-01-09 12:15:15 -0800519 mountMode =
520 getMountModeForRunningProc(packagesForUid, userId, mntWriteSb, mntFullSb);
Sudheer Shanka817b9112018-12-13 17:40:28 -0800521 if (mountMode == -1) {
522 _exit(1);
523 }
524 } else {
525 mountMode = remountMode;
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800526 if (handleMountModeInstaller(mountMode, obbMountDirFd.get(), obbMountDir,
527 sandboxId) < 0) {
528 _exit(1);
Sudheer Shanka817b9112018-12-13 17:40:28 -0800529 }
530 }
531 if (mountMode == VoldNativeService::REMOUNT_MODE_FULL ||
Sudheer Shanka55049012019-01-09 12:15:15 -0800532 mountMode == VoldNativeService::REMOUNT_MODE_LEGACY ||
Sudheer Shanka817b9112018-12-13 17:40:28 -0800533 mountMode == VoldNativeService::REMOUNT_MODE_NONE) {
534 // These mount modes are not going to change dynamically, so don't bother
535 // unmounting/remounting dirs.
536 _exit(0);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700537 }
538
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700539 for (auto& volumeLabel : visibleVolLabels) {
540 std::string mntSource = StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str());
541 std::string mntTarget = StringPrintf("/storage/%s", volumeLabel.c_str());
542 if (volumeLabel == "emulated") {
543 StringAppendF(&mntSource, "/%d", userId);
544 StringAppendF(&mntTarget, "/%d", userId);
545 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800546
547 std::string sandboxSource =
548 StringPrintf("%s/Android/sandbox/%s", mntSource.c_str(), sandboxId.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000549 if (CreateDir(sandboxSource, 0755) < 0) {
550 continue;
551 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800552 if (BindMount(sandboxSource, mntTarget) < 0) {
553 continue;
554 }
555
Sudheer Shanka817b9112018-12-13 17:40:28 -0800556 std::string obbSourceDir = StringPrintf("%s/Android/obb", mntSource.c_str());
557 std::string obbTargetDir = StringPrintf("%s/Android/obb", mntTarget.c_str());
Sudheer Shanka023b5392019-02-06 12:39:19 -0800558 if (UnmountTree(obbTargetDir) < 0) {
Sudheer Shanka817b9112018-12-13 17:40:28 -0800559 continue;
560 }
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000561 if (!createPkgSpecificDirRoots(mntSource) || !createPkgSpecificDirRoots(mntTarget)) {
562 continue;
563 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700564 for (auto& package : packagesForUid) {
565 mountPkgSpecificDir(mntSource, mntTarget, package, "data");
566 mountPkgSpecificDir(mntSource, mntTarget, package, "media");
Sudheer Shanka03992e32018-12-12 12:43:38 -0800567 if (mountMode != VoldNativeService::REMOUNT_MODE_INSTALLER) {
568 mountPkgSpecificDir(mntSource, mntTarget, package, "obb");
569 }
570 }
571 if (mountMode == VoldNativeService::REMOUNT_MODE_INSTALLER) {
Sudheer Shanka023b5392019-02-06 12:39:19 -0800572 if (BindMount(obbSourceDir, obbTargetDir) < 0) {
Sudheer Shanka03992e32018-12-12 12:43:38 -0800573 continue;
574 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700575 }
576 }
577 _exit(0);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700578 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700579
580 if (child == -1) {
581 PLOG(ERROR) << "Failed to fork";
582 } else {
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800583 childPids.push_back(child);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700584 }
585 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800586 for (auto& child : childPids) {
587 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
588 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700589 return 0;
590}
591
Sudheer Shanka03992e32018-12-12 12:43:38 -0800592int VolumeManager::getMountModeForRunningProc(const std::vector<std::string>& packagesForUid,
Sudheer Shanka55049012019-01-09 12:15:15 -0800593 userid_t userId, struct stat& mntWriteStat,
594 struct stat& mntFullStat) {
Sudheer Shanka03992e32018-12-12 12:43:38 -0800595 struct stat storageSb;
596 if (TEMP_FAILURE_RETRY(stat("/storage", &storageSb)) == -1) {
597 PLOG(ERROR) << "Failed to stat /storage";
598 return -1;
599 }
600
601 // Some packages have access to full external storage, identify processes belonging
Sudheer Shanka55049012019-01-09 12:15:15 -0800602 // to those packages by comparing inode no.s of /mnt/runtime/full and /storage
603 if (storageSb.st_dev == mntFullStat.st_dev && storageSb.st_ino == mntFullStat.st_ino) {
Sudheer Shanka03992e32018-12-12 12:43:38 -0800604 return VoldNativeService::REMOUNT_MODE_FULL;
Sudheer Shanka55049012019-01-09 12:15:15 -0800605 } else if (storageSb.st_dev == mntWriteStat.st_dev && storageSb.st_ino == mntWriteStat.st_ino) {
606 return VoldNativeService::REMOUNT_MODE_LEGACY;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800607 }
608
609 std::string obbMountFile =
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800610 StringPrintf("/mnt/user/%d/obb_mount/%s", userId, packagesForUid[0].c_str());
611 if (TEMP_FAILURE_RETRY(access(obbMountFile.c_str(), F_OK)) != -1) {
Sudheer Shanka03992e32018-12-12 12:43:38 -0800612 return VoldNativeService::REMOUNT_MODE_INSTALLER;
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800613 } else if (errno != ENOENT) {
614 PLOG(ERROR) << "Failed to access " << obbMountFile;
615 return -1;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800616 }
617
618 // Some packages don't have access to external storage and processes belonging to
619 // those packages don't have anything mounted at /storage. So, identify those
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800620 // processes by comparing inode no.s of /mnt/user/%d/package
Sudheer Shanka03992e32018-12-12 12:43:38 -0800621 // and /storage
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800622 std::string sandbox = StringPrintf("/mnt/user/%d/package", userId);
623 struct stat sandboxStat;
624 if (TEMP_FAILURE_RETRY(stat(sandbox.c_str(), &sandboxStat)) == -1) {
625 PLOG(ERROR) << "Failed to stat " << sandbox;
626 return -1;
627 }
628 if (storageSb.st_dev == sandboxStat.st_dev && storageSb.st_ino == sandboxStat.st_ino) {
629 return VoldNativeService::REMOUNT_MODE_WRITE;
630 }
631
632 return VoldNativeService::REMOUNT_MODE_NONE;
633}
634
635int VolumeManager::handleMountModeInstaller(int mountMode, int obbMountDirFd,
636 const std::string& obbMountDir,
637 const std::string& sandboxId) {
638 if (mountMode == VoldNativeService::REMOUNT_MODE_INSTALLER) {
639 if (TEMP_FAILURE_RETRY(faccessat(obbMountDirFd, sandboxId.c_str(), F_OK, 0)) != -1) {
640 return 0;
641 } else if (errno != ENOENT) {
642 PLOG(ERROR) << "Failed to access " << obbMountDir << "/" << sandboxId;
643 return -errno;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800644 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800645 const unique_fd fd(
646 TEMP_FAILURE_RETRY(openat(obbMountDirFd, sandboxId.c_str(), O_RDWR | O_CREAT, 0600)));
647 if (fd.get() < 0) {
648 PLOG(ERROR) << "Failed to create " << obbMountDir << "/" << sandboxId;
649 return -errno;
650 }
651 } else {
652 if (TEMP_FAILURE_RETRY(faccessat(obbMountDirFd, sandboxId.c_str(), F_OK, 0)) != -1) {
653 if (TEMP_FAILURE_RETRY(unlinkat(obbMountDirFd, sandboxId.c_str(), 0)) == -1) {
654 PLOG(ERROR) << "Failed to unlink " << obbMountDir << "/" << sandboxId;
655 return -errno;
656 }
657 } else if (errno != ENOENT) {
658 PLOG(ERROR) << "Failed to access " << obbMountDir << "/" << sandboxId;
659 return -errno;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800660 }
661 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800662 return 0;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800663}
664
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700665int VolumeManager::prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames,
666 const std::vector<std::string>& visibleVolLabels) {
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800667 prepareSandboxTargets(userId, visibleVolLabels);
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000668
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700669 if (visibleVolLabels.empty()) {
670 return 0;
671 }
672 for (auto& volumeLabel : visibleVolLabels) {
673 std::string volumeRoot(StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str()));
674 bool isVolPrimaryEmulated = (volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated());
675 if (isVolPrimaryEmulated) {
676 StringAppendF(&volumeRoot, "/%d", userId);
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000677 if (CreateDir(volumeRoot, 0755) < 0) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700678 return -errno;
679 }
680 }
681
682 std::string sandboxRoot =
683 prepareSubDirs(volumeRoot, "Android/sandbox/", 0700, AID_ROOT, AID_ROOT);
684 if (sandboxRoot.empty()) {
685 return -errno;
686 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700687 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800688 if (mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels, -1) < 0) {
689 PLOG(ERROR) << "Failed to setup sandboxes for already running processes";
690 return -errno;
691 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700692 return 0;
693}
694
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000695int VolumeManager::prepareSandboxTargets(userid_t userId,
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000696 const std::vector<std::string>& visibleVolLabels) {
697 std::string mntTargetRoot = StringPrintf("/mnt/user/%d", userId);
698 if (fs_prepare_dir(mntTargetRoot.c_str(), 0751, AID_ROOT, AID_ROOT) != 0) {
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800699 PLOG(ERROR) << "Failed to fs_prepare_dir " << mntTargetRoot;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000700 return -errno;
701 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800702
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000703 StringAppendF(&mntTargetRoot, "/package");
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800704 if (fs_prepare_dir(mntTargetRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
705 PLOG(ERROR) << "Failed to fs_prepare_dir " << mntTargetRoot;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000706 return -errno;
707 }
708
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800709 uid_t uid = multiuser_get_uid(userId, AID_EVERYBODY);
710 for (auto& volumeLabel : visibleVolLabels) {
711 std::string sandboxTarget =
712 StringPrintf("%s/%s", mntTargetRoot.c_str(), volumeLabel.c_str());
713 if (fs_prepare_dir(sandboxTarget.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
714 PLOG(ERROR) << "Failed to fs_prepare_dir " << sandboxTarget;
715 return -errno;
716 }
717
718 if (mPrimary && volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated()) {
719 StringAppendF(&sandboxTarget, "/%d", userId);
720 if (fs_prepare_dir(sandboxTarget.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
721 PLOG(ERROR) << "Failed to fs_prepare_dir " << sandboxTarget;
722 return -errno;
723 }
724 }
725 }
726
727 StringAppendF(&mntTargetRoot, "/self");
728 if (fs_prepare_dir(mntTargetRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
729 PLOG(ERROR) << "Failed to fs_prepare_dir " << mntTargetRoot;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000730 return -errno;
731 }
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000732
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000733 if (mPrimary) {
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800734 std::string pkgPrimarySource(mPrimary->getPath());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000735 if (mPrimary->isEmulated()) {
736 StringAppendF(&pkgPrimarySource, "/%d", userId);
737 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800738 StringAppendF(&mntTargetRoot, "/primary");
739 if (Symlink(pkgPrimarySource, mntTargetRoot) < 0) {
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000740 return -errno;
741 }
742 }
743 return 0;
744}
745
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700746std::string VolumeManager::prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs,
747 mode_t mode, uid_t uid, gid_t gid) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700748 std::string path(pathPrefix);
749 std::vector<std::string> subDirList = android::base::Split(subDirs, "/");
750 for (size_t i = 0; i < subDirList.size(); ++i) {
751 std::string subDir = subDirList[i];
752 if (subDir.empty()) {
753 continue;
754 }
755 StringAppendF(&path, "/%s", subDir.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000756 if (CreateDir(path, mode) < 0) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700757 return kEmptyString;
758 }
759 }
760 return path;
761}
762
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700763bool VolumeManager::createPkgSpecificDirRoots(const std::string& volumeRoot) {
764 std::string volumeAndroidRoot = StringPrintf("%s/Android", volumeRoot.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000765 if (CreateDir(volumeAndroidRoot, 0700) < 0) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700766 return false;
767 }
768 std::array<std::string, 3> dirs = {"data", "media", "obb"};
769 for (auto& dir : dirs) {
770 std::string path = StringPrintf("%s/%s", volumeAndroidRoot.c_str(), dir.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000771 if (CreateDir(path, 0700) < 0) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700772 return false;
773 }
774 }
775 return true;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700776}
777
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700778int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
779 mAddedUsers[userId] = userSerialNumber;
780 return 0;
781}
782
783int VolumeManager::onUserRemoved(userid_t userId) {
784 mAddedUsers.erase(userId);
785 return 0;
786}
787
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700788int VolumeManager::onUserStarted(userid_t userId, const std::vector<std::string>& packageNames,
789 const std::vector<int>& appIds,
790 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700791 LOG(VERBOSE) << "onUserStarted: " << userId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700792 // Note that sometimes the system will spin up processes from Zygote
793 // before actually starting the user, so we're okay if Zygote
794 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600795 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700796 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
797
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700798 mStartedUsers.insert(userId);
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700799 mUserPackages[userId] = packageNames;
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700800 for (size_t i = 0; i < packageNames.size(); ++i) {
801 mAppIds[packageNames[i]] = appIds[i];
802 mSandboxIds[appIds[i]] = sandboxIds[i];
803 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700804 if (mPrimary) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700805 linkPrimary(userId);
806 }
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800807 if (hasIsolatedStorage()) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700808 std::vector<std::string> visibleVolLabels;
809 for (auto& volId : mVisibleVolumeIds) {
810 auto vol = findVolume(volId);
811 userid_t mountUserId = vol->getMountUserId();
812 if (mountUserId == userId || vol->isEmulated()) {
813 visibleVolLabels.push_back(vol->getLabel());
814 }
815 }
816 if (prepareSandboxes(userId, packageNames, visibleVolLabels) != 0) {
817 return -errno;
818 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700819 }
820 return 0;
821}
822
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700823int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700824 LOG(VERBOSE) << "onUserStopped: " << userId;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700825 mStartedUsers.erase(userId);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700826
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800827 if (hasIsolatedStorage()) {
Sudheer Shanka023b5392019-02-06 12:39:19 -0800828 auto& userPackages = mUserPackages[userId];
829 std::string userMntTargetRoot = StringPrintf("/mnt/user/%d", userId);
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800830 std::string pkgPrimaryDir =
831 StringPrintf("%s/package/self/primary", userMntTargetRoot.c_str());
832 if (Unlink(pkgPrimaryDir) < 0) {
833 return -errno;
Sudheer Shanka023b5392019-02-06 12:39:19 -0800834 }
Sudheer Shanka9acc6d42018-10-06 19:03:02 -0700835 mUserPackages.erase(userId);
Sudheer Shanka023b5392019-02-06 12:39:19 -0800836 if (DeleteDirContentsAndDir(userMntTargetRoot) < 0) {
837 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << userMntTargetRoot;
Sudheer Shanka9acc6d42018-10-06 19:03:02 -0700838 return -errno;
839 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800840 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << userMntTargetRoot;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700841 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700842 return 0;
843}
844
Sudheer Shankad484aa92018-07-31 10:07:34 -0700845int VolumeManager::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700846 const std::vector<int32_t>& appIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700847 for (size_t i = 0; i < packageNames.size(); ++i) {
848 mAppIds[packageNames[i]] = appIds[i];
849 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700850 return 0;
851}
852
853int VolumeManager::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700854 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700855 for (size_t i = 0; i < appIds.size(); ++i) {
856 mSandboxIds[appIds[i]] = sandboxIds[i];
857 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700858 return 0;
859}
860
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700861int VolumeManager::prepareSandboxForApp(const std::string& packageName, appid_t appId,
862 const std::string& sandboxId, userid_t userId) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800863 if (!hasIsolatedStorage()) {
Sudheer Shankac7562092018-08-24 10:20:56 -0700864 return 0;
865 } else if (mStartedUsers.find(userId) == mStartedUsers.end()) {
866 // User not started, no need to do anything now. Required bind mounts for the package will
867 // be created when the user starts.
868 return 0;
869 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700870 LOG(VERBOSE) << "prepareSandboxForApp: " << packageName << ", appId=" << appId
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700871 << ", sandboxId=" << sandboxId << ", userId=" << userId;
Sudheer Shankac7562092018-08-24 10:20:56 -0700872 mUserPackages[userId].push_back(packageName);
873 mAppIds[packageName] = appId;
874 mSandboxIds[appId] = sandboxId;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700875
876 std::vector<std::string> visibleVolLabels;
877 for (auto& volId : mVisibleVolumeIds) {
878 auto vol = findVolume(volId);
879 userid_t mountUserId = vol->getMountUserId();
880 if (mountUserId == userId || vol->isEmulated()) {
881 visibleVolLabels.push_back(vol->getLabel());
882 }
Sudheer Shankac7562092018-08-24 10:20:56 -0700883 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700884 return prepareSandboxes(userId, {packageName}, visibleVolLabels);
Sudheer Shankac7562092018-08-24 10:20:56 -0700885}
886
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700887int VolumeManager::destroySandboxForApp(const std::string& packageName,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700888 const std::string& sandboxId, userid_t userId) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800889 if (!hasIsolatedStorage()) {
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700890 return 0;
891 }
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700892 LOG(VERBOSE) << "destroySandboxForApp: " << packageName << ", sandboxId=" << sandboxId
893 << ", userId=" << userId;
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700894 auto& userPackages = mUserPackages[userId];
Sudheer Shankaba0c62f2018-12-16 18:22:34 -0800895 userPackages.erase(std::remove(userPackages.begin(), userPackages.end(), packageName),
896 userPackages.end());
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700897 // If the package is not uninstalled in any other users, remove appId and sandboxId
898 // corresponding to it from the internal state.
899 bool installedInAnyUser = false;
900 for (auto& it : mUserPackages) {
901 auto& packages = it.second;
902 if (std::find(packages.begin(), packages.end(), packageName) != packages.end()) {
903 installedInAnyUser = true;
904 break;
905 }
906 }
907 if (!installedInAnyUser) {
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700908 const auto& entry = mAppIds.find(packageName);
909 if (entry != mAppIds.end()) {
910 mSandboxIds.erase(entry->second);
911 mAppIds.erase(entry);
912 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700913 }
914
915 std::vector<std::string> visibleVolLabels;
916 for (auto& volId : mVisibleVolumeIds) {
917 auto vol = findVolume(volId);
918 userid_t mountUserId = vol->getMountUserId();
919 if (mountUserId == userId || vol->isEmulated()) {
920 if (destroySandboxForAppOnVol(packageName, sandboxId, userId, vol->getLabel()) < 0) {
921 return -errno;
922 }
923 }
924 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800925
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700926 return 0;
927}
928
929int VolumeManager::destroySandboxForAppOnVol(const std::string& packageName,
930 const std::string& sandboxId, userid_t userId,
931 const std::string& volLabel) {
932 LOG(VERBOSE) << "destroySandboxOnVol: " << packageName << ", userId=" << userId
933 << ", volLabel=" << volLabel;
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700934
935 std::string sandboxDir = StringPrintf("/mnt/runtime/write/%s", volLabel.c_str());
936 if (volLabel == mPrimary->getLabel() && mPrimary->isEmulated()) {
937 StringAppendF(&sandboxDir, "/%d", userId);
938 }
Sudheer Shanka51a38da2018-10-18 08:51:31 -0700939 StringAppendF(&sandboxDir, "/Android/sandbox/%s", sandboxId.c_str());
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700940
Sudheer Shanka023b5392019-02-06 12:39:19 -0800941 if (DeleteDirContentsAndDir(sandboxDir) < 0) {
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700942 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << sandboxDir;
943 return -errno;
944 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800945
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700946 return 0;
947}
948
Jeff Sharkey401b2602017-12-14 22:15:20 -0700949int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
950 mSecureKeyguardShowing = isShowing;
951 if (!mSecureKeyguardShowing) {
952 // Now that secure keyguard has been dismissed, process
953 // any pending disks
954 for (const auto& disk : mPendingDisks) {
955 disk->create();
956 mDisks.push_back(disk);
957 }
958 mPendingDisks.clear();
959 }
960 return 0;
961}
962
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700963int VolumeManager::onVolumeMounted(android::vold::VolumeBase* vol) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800964 if (!hasIsolatedStorage()) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700965 return 0;
966 }
967
968 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_VISIBLE) == 0) {
969 return 0;
970 }
971
972 mVisibleVolumeIds.insert(vol->getId());
973 userid_t mountUserId = vol->getMountUserId();
974 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
975 // We don't want to create another shared_ptr here because then we will end up with
976 // two shared_ptrs owning the underlying pointer without sharing it.
977 mPrimary = findVolume(vol->getId());
978 for (userid_t userId : mStartedUsers) {
979 if (linkPrimary(userId) != 0) {
980 return -errno;
981 }
982 }
983 }
984 if (vol->isEmulated()) {
985 for (userid_t userId : mStartedUsers) {
986 if (prepareSandboxes(userId, mUserPackages[userId], {vol->getLabel()}) != 0) {
987 return -errno;
988 }
989 }
990 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
991 if (prepareSandboxes(mountUserId, mUserPackages[mountUserId], {vol->getLabel()}) != 0) {
992 return -errno;
993 }
994 }
995 return 0;
996}
997
998int VolumeManager::onVolumeUnmounted(android::vold::VolumeBase* vol) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800999 if (!hasIsolatedStorage()) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001000 return 0;
1001 }
1002
1003 if (mVisibleVolumeIds.erase(vol->getId()) == 0) {
1004 return 0;
1005 }
1006
1007 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
1008 mPrimary = nullptr;
1009 }
1010
1011 LOG(VERBOSE) << "visibleVolumeUnmounted: " << vol;
1012 userid_t mountUserId = vol->getMountUserId();
1013 if (vol->isEmulated()) {
1014 for (userid_t userId : mStartedUsers) {
1015 if (destroySandboxesForVol(vol, userId) != 0) {
1016 return -errno;
1017 }
1018 }
1019 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
1020 if (destroySandboxesForVol(vol, mountUserId) != 0) {
1021 return -errno;
1022 }
1023 }
1024 return 0;
1025}
1026
1027int VolumeManager::destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId) {
1028 LOG(VERBOSE) << "destroysandboxesForVol: " << vol << " for user=" << userId;
Sudheer Shanka727c5fa2019-02-21 15:00:11 -08001029 std::string volSandboxRoot =
1030 StringPrintf("/mnt/user/%d/package/%s", userId, vol->getLabel().c_str());
1031 if (android::vold::DeleteDirContentsAndDir(volSandboxRoot) < 0) {
1032 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << volSandboxRoot;
1033 return -errno;
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001034 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -08001035 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << volSandboxRoot;
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001036 return 0;
1037}
1038
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001039int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -08001040 if (hasIsolatedStorage()) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001041 return 0;
1042 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001043 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001044 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001045 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001046 }
1047 return 0;
1048}
1049
Sudheer Shanka817b9112018-12-13 17:40:28 -08001050int VolumeManager::remountUid(uid_t uid, int32_t mountMode) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -08001051 if (!hasIsolatedStorage()) {
Sudheer Shanka817b9112018-12-13 17:40:28 -08001052 return remountUidLegacy(uid, mountMode);
1053 }
1054
1055 appid_t appId = multiuser_get_app_id(uid);
1056 userid_t userId = multiuser_get_user_id(uid);
1057 std::vector<std::string> visibleVolLabels;
1058 for (auto& volId : mVisibleVolumeIds) {
1059 auto vol = findVolume(volId);
1060 userid_t mountUserId = vol->getMountUserId();
1061 if (mountUserId == userId || vol->isEmulated()) {
1062 visibleVolLabels.push_back(vol->getLabel());
1063 }
1064 }
1065
1066 // Finding one package with appId is enough
1067 std::vector<std::string> packageNames;
1068 for (auto it = mAppIds.begin(); it != mAppIds.end(); ++it) {
1069 if (it->second == appId) {
1070 packageNames.push_back(it->first);
1071 break;
1072 }
1073 }
1074 if (packageNames.empty()) {
1075 PLOG(ERROR) << "Failed to find packageName for " << uid;
Sudheer Shanka53947a32018-08-01 10:24:13 -07001076 return -1;
1077 }
Sudheer Shanka817b9112018-12-13 17:40:28 -08001078 return mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels, mountMode);
1079}
1080
1081int VolumeManager::remountUidLegacy(uid_t uid, int32_t mountMode) {
1082 std::string mode;
1083 switch (mountMode) {
1084 case VoldNativeService::REMOUNT_MODE_NONE:
1085 mode = "none";
1086 break;
1087 case VoldNativeService::REMOUNT_MODE_DEFAULT:
1088 mode = "default";
1089 break;
1090 case VoldNativeService::REMOUNT_MODE_READ:
1091 mode = "read";
1092 break;
1093 case VoldNativeService::REMOUNT_MODE_WRITE:
1094 mode = "write";
1095 break;
1096 default:
1097 PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode);
1098 return -1;
1099 }
Jeff Sharkey66270a22015-06-24 11:49:24 -07001100 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
1101
1102 DIR* dir;
1103 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001104 std::string rootName;
1105 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001106 int pidFd;
1107 int nsFd;
1108 struct stat sb;
1109 pid_t child;
1110
1111 if (!(dir = opendir("/proc"))) {
1112 PLOG(ERROR) << "Failed to opendir";
1113 return -1;
1114 }
1115
1116 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -06001117 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
1118 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001119 closedir(dir);
1120 return -1;
1121 }
1122
1123 // Poke through all running PIDs look for apps running as UID
1124 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -07001125 pid_t pid;
1126 if (de->d_type != DT_DIR) continue;
1127 if (!android::base::ParseInt(de->d_name, &pid)) continue;
1128
Jeff Sharkey66270a22015-06-24 11:49:24 -07001129 pidFd = -1;
1130 nsFd = -1;
1131
1132 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1133 if (pidFd < 0) {
1134 goto next;
1135 }
1136 if (fstat(pidFd, &sb) != 0) {
1137 PLOG(WARNING) << "Failed to stat " << de->d_name;
1138 goto next;
1139 }
1140 if (sb.st_uid != uid) {
1141 goto next;
1142 }
1143
1144 // Matches so far, but refuse to touch if in root namespace
1145 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001146 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001147 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
1148 goto next;
1149 }
Jeff Sharkey3472e522017-10-06 18:02:53 -06001150 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001151 LOG(WARNING) << "Skipping due to root namespace";
1152 goto next;
1153 }
1154
1155 // We purposefully leave the namespace open across the fork
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001156 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -07001157 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001158 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001159 goto next;
1160 }
1161
1162 if (!(child = fork())) {
1163 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001164 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001165 _exit(1);
1166 }
1167
Sudheer Shanka99d304a2018-09-27 14:53:51 -07001168 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -07001169
1170 std::string storageSource;
1171 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001172 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001173 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001174 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001175 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001176 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001177 } else {
1178 // Sane default of no storage visible
1179 _exit(0);
1180 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001181 if (TEMP_FAILURE_RETRY(
1182 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
1183 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001184 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -07001185 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001186 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
1187 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +09001188 _exit(1);
1189 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001190
1191 // Mount user-specific symlink helper into place
1192 userid_t user_id = multiuser_get_user_id(uid);
1193 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001194 if (TEMP_FAILURE_RETRY(
1195 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
1196 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001197 _exit(1);
1198 }
1199
Jeff Sharkey66270a22015-06-24 11:49:24 -07001200 _exit(0);
1201 }
1202
1203 if (child == -1) {
1204 PLOG(ERROR) << "Failed to fork";
1205 goto next;
1206 } else {
1207 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
1208 }
1209
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001210 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -07001211 close(nsFd);
1212 close(pidFd);
1213 }
1214 closedir(dir);
1215 return 0;
1216}
1217
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001218int VolumeManager::reset() {
1219 // Tear down all existing disks/volumes and start from a blank slate so
1220 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +08001221 if (mInternalEmulated != nullptr) {
1222 mInternalEmulated->destroy();
1223 mInternalEmulated->create();
1224 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001225 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001226 disk->destroy();
1227 disk->create();
1228 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -06001229 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001230 mAddedUsers.clear();
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -07001231
1232 mUserPackages.clear();
1233 mAppIds.clear();
1234 mSandboxIds.clear();
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001235 mVisibleVolumeIds.clear();
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -07001236
Sudheer Shanka023b5392019-02-06 12:39:19 -08001237 for (userid_t userId : mStartedUsers) {
Sudheer Shanka30df1c62019-02-22 17:03:02 -08001238 DeleteDirContents(StringPrintf("/mnt/user/%d/package", userId));
Sudheer Shanka023b5392019-02-06 12:39:19 -08001239 }
1240 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001241 return 0;
1242}
1243
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001244// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001245int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001246 if (mInternalEmulated == nullptr) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001247 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001248 }
Paul Crowley56292ef2017-10-20 08:07:53 -07001249 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001250 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001251 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001252 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001253 disk->destroy();
1254 }
Risan8c9f3322018-10-29 08:52:56 +09001255 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001256 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -07001257 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -07001258 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001259 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -07001260}
1261
Jeff Sharkey9c484982015-03-31 10:35:33 -07001262int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001263 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -06001264 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001265
Jeff Sharkey9c484982015-03-31 10:35:33 -07001266 // First, try gracefully unmounting all known devices
1267 if (mInternalEmulated != nullptr) {
1268 mInternalEmulated->unmount();
1269 }
Risan8c9f3322018-10-29 08:52:56 +09001270 for (const auto& stub : mStubVolumes) {
1271 stub->unmount();
1272 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001273 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001274 disk->unmountAll();
1275 }
1276
1277 // Worst case we might have some stale mounts lurking around, so
1278 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +08001279 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -07001280 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001281 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -07001282 return -errno;
1283 }
1284
1285 // Some volumes can be stacked on each other, so force unmount in
1286 // reverse order to give us the best chance of success.
1287 std::list<std::string> toUnmount;
1288 mntent* mentry;
1289 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001290 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001291 if ((StartsWith(test, "/mnt/") &&
1292#ifdef __ANDROID_DEBUGGABLE__
1293 !StartsWith(test, "/mnt/scratch") &&
1294#endif
1295 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
1296 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001297 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -07001298 }
1299 }
1300 endmntent(fp);
1301
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001302 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001303 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001304 android::vold::ForceUnmount(path);
1305 }
1306
1307 return 0;
1308}
1309
Jeff Sharkey3472e522017-10-06 18:02:53 -06001310int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001311 // Only offer to create directories for paths managed by vold
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001312 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001313 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -06001314 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001315 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001316 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001317 return -EINVAL;
1318 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001319}
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001320
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001321int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001322 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001323 int id = mNextObbId++;
1324
1325 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001326 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001327 vol->create();
1328
1329 mObbVolumes.push_back(vol);
1330 *outVolId = vol->getId();
1331 return android::OK;
1332}
1333
1334int VolumeManager::destroyObb(const std::string& volId) {
1335 auto i = mObbVolumes.begin();
1336 while (i != mObbVolumes.end()) {
1337 if ((*i)->getId() == volId) {
1338 (*i)->destroy();
1339 i = mObbVolumes.erase(i);
1340 } else {
1341 ++i;
1342 }
1343 }
1344 return android::OK;
1345}
1346
Risan8c9f3322018-10-29 08:52:56 +09001347int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
1348 const std::string& fsType, const std::string& fsUuid,
1349 const std::string& fsLabel, std::string* outVolId) {
1350 int id = mNextStubVolumeId++;
1351 auto vol = std::shared_ptr<android::vold::VolumeBase>(
1352 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
1353 vol->create();
1354
1355 mStubVolumes.push_back(vol);
1356 *outVolId = vol->getId();
1357 return android::OK;
1358}
1359
1360int VolumeManager::destroyStubVolume(const std::string& volId) {
1361 auto i = mStubVolumes.begin();
1362 while (i != mStubVolumes.end()) {
1363 if ((*i)->getId() == volId) {
1364 (*i)->destroy();
1365 i = mStubVolumes.erase(i);
1366 } else {
1367 ++i;
1368 }
1369 }
1370 return android::OK;
1371}
1372
Risan8f6198d2018-10-26 20:56:45 -06001373int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -06001374 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001375}
1376
Risan8f6198d2018-10-26 20:56:45 -06001377int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -06001378 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -06001379}
1380
1381int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -06001382 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001383}