blob: 83632e4f99f27f14cf8001f58a299aca1264b28f [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 Shanka023b5392019-02-06 12:39:19 -080079using android::vold::DeleteDirContentsAndDir;
80using android::vold::Symlink;
81using android::vold::Unlink;
82using android::vold::UnmountTree;
Sudheer Shanka03992e32018-12-12 12:43:38 -080083using android::vold::VoldNativeService;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070084
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060085static const char* kPathUserMount = "/mnt/user";
86static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
87
Sudheer Shanka53947a32018-08-01 10:24:13 -070088static const char* kIsolatedStorage = "persist.sys.isolated_storage";
Sudheer Shanka34b437b2019-01-17 17:20:47 -080089static const char* kIsolatedStorageSnapshot = "sys.isolated_storage_snapshot";
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060090static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
91
Sudheer Shanka53947a32018-08-01 10:24:13 -070092static const std::string kEmptyString("");
93
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060094/* 512MiB is large enough for testing purposes */
95static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080098static const unsigned int kMajorBlockExperimentalMin = 240;
99static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700100
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700101VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700102
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700103VolumeManager* VolumeManager::Instance() {
104 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -0700105 return sInstance;
106}
107
108VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800109 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600110 mNextObbId = 0;
Risan8c9f3322018-10-29 08:52:56 +0900111 mNextStubVolumeId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700112 // For security reasons, assume that a secure keyguard is
113 // showing until we hear otherwise
114 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700115}
116
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700117VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800118
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800119static bool hasIsolatedStorage() {
Jeff Sharkeya6082502019-02-11 12:18:48 -0700120 return GetBoolProperty(kIsolatedStorageSnapshot, GetBoolProperty(kIsolatedStorage, true));
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800121}
122
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600123int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600124 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700125 if (GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600126 if (access(kPathVirtualDisk, F_OK) != 0) {
127 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
128 }
129
130 if (mVirtualDisk == nullptr) {
131 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
132 LOG(ERROR) << "Failed to create virtual disk";
133 return -1;
134 }
135
136 struct stat buf;
137 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
138 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
139 return -1;
140 }
141
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700142 auto disk = new android::vold::Disk(
143 "virtual", buf.st_rdev, "virtual",
144 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600145 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700146 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600147 }
148 } else {
149 if (mVirtualDisk != nullptr) {
150 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700151 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600152
153 Loop::destroyByDevice(mVirtualDiskPath.c_str());
154 mVirtualDisk = nullptr;
155 }
156
157 if (access(kPathVirtualDisk, F_OK) == 0) {
158 unlink(kPathVirtualDisk);
159 }
160 }
161 return 0;
162}
163
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700164int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800165 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700166 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800167}
168
San Mehatf1b736b2009-10-10 17:22:08 -0700169int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600170 ATRACE_NAME("VolumeManager::start");
171
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172 // Always start from a clean slate by unmounting everything in
173 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700174 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600176 Devmapper::destroyAll();
177 Loop::destroyAll();
178
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179 // Assume that we always have an emulated volume on internal
180 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700181 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700182 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700183 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700184 mInternalEmulated->create();
185
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600186 // Consider creating a virtual disk
187 updateVirtualDisk();
188
San Mehatf1b736b2009-10-10 17:22:08 -0700189 return 0;
190}
191
192int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700193 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700194 mInternalEmulated->destroy();
195 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700196 return 0;
197}
198
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700199void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700200 std::lock_guard<std::mutex> lock(mLock);
201
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700202 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700203 LOG(DEBUG) << "----------------";
204 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700205 evt->dump();
206 }
San Mehatf1b736b2009-10-10 17:22:08 -0700207
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700208 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
209 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700210
211 if (devType != "disk") return;
212
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600213 int major = std::stoi(evt->findParam("MAJOR"));
214 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700215 dev_t device = makedev(major, minor);
216
217 switch (evt->getAction()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700218 case NetlinkEvent::Action::kAdd: {
219 for (const auto& source : mDiskSources) {
220 if (source->matches(eventPath)) {
221 // For now, assume that MMC and virtio-blk (the latter is
222 // emulator-specific; see Disk.cpp for details) devices are SD,
223 // and that everything else is USB
224 int flags = source->getFlags();
225 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
226 major >= (int)kMajorBlockExperimentalMin &&
227 major <= (int)kMajorBlockExperimentalMax)) {
228 flags |= android::vold::Disk::Flags::kSd;
229 } else {
230 flags |= android::vold::Disk::Flags::kUsb;
231 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700233 auto disk =
234 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
235 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
236 break;
237 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700238 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700239 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700240 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700241 case NetlinkEvent::Action::kChange: {
242 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
243 handleDiskChanged(device);
244 break;
245 }
246 case NetlinkEvent::Action::kRemove: {
247 handleDiskRemoved(device);
248 break;
249 }
250 default: {
251 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
252 break;
253 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700254 }
255}
256
Jeff Sharkey401b2602017-12-14 22:15:20 -0700257void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
258 // For security reasons, if secure keyguard is showing, wait
259 // until the user unlocks the device to actually touch it
260 if (mSecureKeyguardShowing) {
261 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700262 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700263 mPendingDisks.push_back(disk);
264 } else {
265 disk->create();
266 mDisks.push_back(disk);
267 }
268}
269
270void VolumeManager::handleDiskChanged(dev_t device) {
271 for (const auto& disk : mDisks) {
272 if (disk->getDevice() == device) {
273 disk->readMetadata();
274 disk->readPartitions();
275 }
276 }
277
278 // For security reasons, we ignore all pending disks, since
279 // we'll scan them once the device is unlocked
280}
281
282void VolumeManager::handleDiskRemoved(dev_t device) {
283 auto i = mDisks.begin();
284 while (i != mDisks.end()) {
285 if ((*i)->getDevice() == device) {
286 (*i)->destroy();
287 i = mDisks.erase(i);
288 } else {
289 ++i;
290 }
291 }
292 auto j = mPendingDisks.begin();
293 while (j != mPendingDisks.end()) {
294 if ((*j)->getDevice() == device) {
295 j = mPendingDisks.erase(j);
296 } else {
297 ++j;
298 }
299 }
300}
301
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700302void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800303 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700304 mDiskSources.push_back(diskSource);
305}
306
307std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
308 for (auto disk : mDisks) {
309 if (disk->getId() == id) {
310 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700311 }
312 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700313 return nullptr;
314}
San Mehatf1b736b2009-10-10 17:22:08 -0700315
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700316std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800317 // Vold could receive "mount" after "shutdown" command in the extreme case.
318 // If this happens, mInternalEmulated will equal nullptr and
319 // we need to deal with it in order to avoid null pointer crash.
320 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700321 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700322 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700323 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700324 auto vol = disk->findVolume(id);
325 if (vol != nullptr) {
326 return vol;
327 }
328 }
Risan8c9f3322018-10-29 08:52:56 +0900329 for (const auto& vol : mStubVolumes) {
330 if (vol->getId() == id) {
331 return vol;
332 }
333 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600334 for (const auto& vol : mObbVolumes) {
335 if (vol->getId() == id) {
336 return vol;
337 }
338 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700339 return nullptr;
340}
341
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800342void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
343 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700344 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700345 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700346 disk->listVolumes(type, list);
347 }
348}
349
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600350int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700351 std::string normalizedGuid;
352 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
353 LOG(WARNING) << "Invalid GUID " << partGuid;
354 return -1;
355 }
356
Paul Crowleyc6433a22017-10-24 14:54:43 -0700357 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700358 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
359 if (unlink(keyPath.c_str()) != 0) {
360 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700361 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700362 }
Eric Biggersa701c452018-10-23 13:06:55 -0700363 if (fscrypt_is_native()) {
364 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700365 success = false;
366 }
367 }
368 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700369}
370
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700371int VolumeManager::linkPrimary(userid_t userId) {
372 std::string source(mPrimary->getPath());
373 if (mPrimary->isEmulated()) {
374 source = StringPrintf("%s/%d", source.c_str(), userId);
375 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
376 }
377
378 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700379 LOG(DEBUG) << "Linking " << source << " to " << target;
Sudheer Shanka023b5392019-02-06 12:39:19 -0800380 Symlink(source, target);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700381 return 0;
382}
383
384int VolumeManager::mountPkgSpecificDir(const std::string& mntSourceRoot,
385 const std::string& mntTargetRoot,
386 const std::string& packageName, const char* dirName) {
387 std::string mntSourceDir =
388 StringPrintf("%s/Android/%s/%s", mntSourceRoot.c_str(), dirName, packageName.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000389 if (CreateDir(mntSourceDir, 0755) < 0) {
390 return -errno;
391 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700392 std::string mntTargetDir =
393 StringPrintf("%s/Android/%s/%s", mntTargetRoot.c_str(), dirName, packageName.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000394 if (CreateDir(mntTargetDir, 0755) < 0) {
395 return -errno;
396 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800397 return BindMount(mntSourceDir, mntTargetDir);
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700398}
399
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700400int VolumeManager::mountPkgSpecificDirsForRunningProcs(
401 userid_t userId, const std::vector<std::string>& packageNames,
Sudheer Shanka817b9112018-12-13 17:40:28 -0800402 const std::vector<std::string>& visibleVolLabels, int remountMode) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700403 // TODO: New processes could be started while traversing over the existing
404 // processes which would end up not having the necessary bind mounts. This
405 // issue needs to be fixed, may be by doing multiple passes here?
406 std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir("/proc"), closedir);
407 if (!dirp) {
408 PLOG(ERROR) << "Failed to opendir /proc";
409 return -1;
Sudheer Shankac7562092018-08-24 10:20:56 -0700410 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700411
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700412 std::string rootName;
413 // Figure out root namespace to compare against below
414 if (!android::vold::Readlinkat(dirfd(dirp.get()), "1/ns/mnt", &rootName)) {
415 PLOG(ERROR) << "Failed to read root namespace";
416 return -1;
Sudheer Shankaf768c272018-08-04 10:10:27 -0700417 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700418
Sudheer Shanka55049012019-01-09 12:15:15 -0800419 struct stat mntFullSb;
420 struct stat mntWriteSb;
421 if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/full", &mntFullSb)) == -1) {
422 PLOG(ERROR) << "Failed to stat /mnt/runtime/full";
423 return -1;
424 }
425 if (TEMP_FAILURE_RETRY(stat("/mnt/runtime/write", &mntWriteSb)) == -1) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700426 PLOG(ERROR) << "Failed to stat /mnt/runtime/write";
427 return -1;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700428 }
429
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800430 std::string obbMountDir = StringPrintf("/mnt/user/%d/obb_mount", userId);
431 if (fs_prepare_dir(obbMountDir.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) {
432 PLOG(ERROR) << "Failed to fs_prepare_dir " << obbMountDir;
433 return -1;
434 }
435 const unique_fd obbMountDirFd(
436 TEMP_FAILURE_RETRY(open(obbMountDir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)));
437 if (obbMountDirFd.get() < 0) {
438 PLOG(ERROR) << "Failed to open " << obbMountDir;
439 return -1;
440 }
441
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700442 std::unordered_set<appid_t> validAppIds;
443 for (auto& package : packageNames) {
444 validAppIds.insert(mAppIds[package]);
Sudheer Shankaa695f252018-08-03 18:07:52 -0700445 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700446 std::vector<std::string>& userPackages = mUserPackages[userId];
Sudheer Shanka53947a32018-08-01 10:24:13 -0700447
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800448 std::vector<pid_t> childPids;
449
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700450 struct dirent* de;
451 // Poke through all running PIDs look for apps running in userId
452 while ((de = readdir(dirp.get()))) {
453 pid_t pid;
454 if (de->d_type != DT_DIR) continue;
455 if (!android::base::ParseInt(de->d_name, &pid)) continue;
456
457 const unique_fd pidFd(
458 openat(dirfd(dirp.get()), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC));
459 if (pidFd.get() < 0) {
460 PLOG(WARNING) << "Failed to open /proc/" << pid;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700461 continue;
462 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700463 struct stat sb;
464 if (fstat(pidFd.get(), &sb) != 0) {
465 PLOG(WARNING) << "Failed to stat " << de->d_name;
466 continue;
467 }
468 if (multiuser_get_user_id(sb.st_uid) != userId) {
469 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700470 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700471
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700472 // Matches so far, but refuse to touch if in root namespace
473 LOG(VERBOSE) << "Found matching PID " << de->d_name;
474 std::string pidName;
475 if (!android::vold::Readlinkat(pidFd.get(), "ns/mnt", &pidName)) {
476 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
477 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700478 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700479 if (rootName == pidName) {
480 LOG(WARNING) << "Skipping due to root namespace";
481 continue;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700482 }
483
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700484 // Only update the mount points of processes running with one of validAppIds.
485 // This should skip any isolated uids.
486 appid_t appId = multiuser_get_app_id(sb.st_uid);
487 if (validAppIds.find(appId) == validAppIds.end()) {
488 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700489 }
490
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700491 std::vector<std::string> packagesForUid;
492 for (auto& package : userPackages) {
493 if (mAppIds[package] == appId) {
494 packagesForUid.push_back(package);
495 }
496 }
497 if (packagesForUid.empty()) {
498 continue;
499 }
500 const std::string& sandboxId = mSandboxIds[appId];
501
502 // We purposefully leave the namespace open across the fork
503 unique_fd nsFd(openat(pidFd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
504 if (nsFd.get() < 0) {
505 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
506 continue;
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -0700507 }
508
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700509 pid_t child;
510 if (!(child = fork())) {
511 if (setns(nsFd.get(), CLONE_NEWNS) != 0) {
512 PLOG(ERROR) << "Failed to setns for " << de->d_name;
513 _exit(1);
514 }
515
Sudheer Shanka817b9112018-12-13 17:40:28 -0800516 int mountMode;
517 if (remountMode == -1) {
Sudheer Shanka55049012019-01-09 12:15:15 -0800518 mountMode =
519 getMountModeForRunningProc(packagesForUid, userId, mntWriteSb, mntFullSb);
Sudheer Shanka817b9112018-12-13 17:40:28 -0800520 if (mountMode == -1) {
521 _exit(1);
522 }
523 } else {
524 mountMode = remountMode;
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800525 if (handleMountModeInstaller(mountMode, obbMountDirFd.get(), obbMountDir,
526 sandboxId) < 0) {
527 _exit(1);
Sudheer Shanka817b9112018-12-13 17:40:28 -0800528 }
529 }
530 if (mountMode == VoldNativeService::REMOUNT_MODE_FULL ||
Sudheer Shanka55049012019-01-09 12:15:15 -0800531 mountMode == VoldNativeService::REMOUNT_MODE_LEGACY ||
Sudheer Shanka817b9112018-12-13 17:40:28 -0800532 mountMode == VoldNativeService::REMOUNT_MODE_NONE) {
533 // These mount modes are not going to change dynamically, so don't bother
534 // unmounting/remounting dirs.
535 _exit(0);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700536 }
537
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700538 for (auto& volumeLabel : visibleVolLabels) {
539 std::string mntSource = StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str());
540 std::string mntTarget = StringPrintf("/storage/%s", volumeLabel.c_str());
541 if (volumeLabel == "emulated") {
542 StringAppendF(&mntSource, "/%d", userId);
543 StringAppendF(&mntTarget, "/%d", userId);
544 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800545
546 std::string sandboxSource =
547 StringPrintf("%s/Android/sandbox/%s", mntSource.c_str(), sandboxId.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000548 if (CreateDir(sandboxSource, 0755) < 0) {
549 continue;
550 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800551 if (BindMount(sandboxSource, mntTarget) < 0) {
552 continue;
553 }
554
Sudheer Shanka817b9112018-12-13 17:40:28 -0800555 std::string obbSourceDir = StringPrintf("%s/Android/obb", mntSource.c_str());
556 std::string obbTargetDir = StringPrintf("%s/Android/obb", mntTarget.c_str());
Sudheer Shanka023b5392019-02-06 12:39:19 -0800557 if (UnmountTree(obbTargetDir) < 0) {
Sudheer Shanka817b9112018-12-13 17:40:28 -0800558 continue;
559 }
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000560 if (!createPkgSpecificDirRoots(mntSource) || !createPkgSpecificDirRoots(mntTarget)) {
561 continue;
562 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700563 for (auto& package : packagesForUid) {
564 mountPkgSpecificDir(mntSource, mntTarget, package, "data");
565 mountPkgSpecificDir(mntSource, mntTarget, package, "media");
Sudheer Shanka03992e32018-12-12 12:43:38 -0800566 if (mountMode != VoldNativeService::REMOUNT_MODE_INSTALLER) {
567 mountPkgSpecificDir(mntSource, mntTarget, package, "obb");
568 }
569 }
570 if (mountMode == VoldNativeService::REMOUNT_MODE_INSTALLER) {
Sudheer Shanka023b5392019-02-06 12:39:19 -0800571 if (BindMount(obbSourceDir, obbTargetDir) < 0) {
Sudheer Shanka03992e32018-12-12 12:43:38 -0800572 continue;
573 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700574 }
575 }
576 _exit(0);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700577 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700578
579 if (child == -1) {
580 PLOG(ERROR) << "Failed to fork";
581 } else {
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800582 childPids.push_back(child);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700583 }
584 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800585 for (auto& child : childPids) {
586 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
587 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700588 return 0;
589}
590
Sudheer Shanka03992e32018-12-12 12:43:38 -0800591int VolumeManager::getMountModeForRunningProc(const std::vector<std::string>& packagesForUid,
Sudheer Shanka55049012019-01-09 12:15:15 -0800592 userid_t userId, struct stat& mntWriteStat,
593 struct stat& mntFullStat) {
Sudheer Shanka03992e32018-12-12 12:43:38 -0800594 struct stat storageSb;
595 if (TEMP_FAILURE_RETRY(stat("/storage", &storageSb)) == -1) {
596 PLOG(ERROR) << "Failed to stat /storage";
597 return -1;
598 }
599
600 // Some packages have access to full external storage, identify processes belonging
Sudheer Shanka55049012019-01-09 12:15:15 -0800601 // to those packages by comparing inode no.s of /mnt/runtime/full and /storage
602 if (storageSb.st_dev == mntFullStat.st_dev && storageSb.st_ino == mntFullStat.st_ino) {
Sudheer Shanka03992e32018-12-12 12:43:38 -0800603 return VoldNativeService::REMOUNT_MODE_FULL;
Sudheer Shanka55049012019-01-09 12:15:15 -0800604 } else if (storageSb.st_dev == mntWriteStat.st_dev && storageSb.st_ino == mntWriteStat.st_ino) {
605 return VoldNativeService::REMOUNT_MODE_LEGACY;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800606 }
607
608 std::string obbMountFile =
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800609 StringPrintf("/mnt/user/%d/obb_mount/%s", userId, packagesForUid[0].c_str());
610 if (TEMP_FAILURE_RETRY(access(obbMountFile.c_str(), F_OK)) != -1) {
Sudheer Shanka03992e32018-12-12 12:43:38 -0800611 return VoldNativeService::REMOUNT_MODE_INSTALLER;
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800612 } else if (errno != ENOENT) {
613 PLOG(ERROR) << "Failed to access " << obbMountFile;
614 return -1;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800615 }
616
617 // Some packages don't have access to external storage and processes belonging to
618 // those packages don't have anything mounted at /storage. So, identify those
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800619 // processes by comparing inode no.s of /mnt/user/%d/package
Sudheer Shanka03992e32018-12-12 12:43:38 -0800620 // and /storage
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800621 std::string sandbox = StringPrintf("/mnt/user/%d/package", userId);
622 struct stat sandboxStat;
623 if (TEMP_FAILURE_RETRY(stat(sandbox.c_str(), &sandboxStat)) == -1) {
624 PLOG(ERROR) << "Failed to stat " << sandbox;
625 return -1;
626 }
627 if (storageSb.st_dev == sandboxStat.st_dev && storageSb.st_ino == sandboxStat.st_ino) {
628 return VoldNativeService::REMOUNT_MODE_WRITE;
629 }
630
631 return VoldNativeService::REMOUNT_MODE_NONE;
632}
633
634int VolumeManager::handleMountModeInstaller(int mountMode, int obbMountDirFd,
635 const std::string& obbMountDir,
636 const std::string& sandboxId) {
637 if (mountMode == VoldNativeService::REMOUNT_MODE_INSTALLER) {
638 if (TEMP_FAILURE_RETRY(faccessat(obbMountDirFd, sandboxId.c_str(), F_OK, 0)) != -1) {
639 return 0;
640 } else if (errno != ENOENT) {
641 PLOG(ERROR) << "Failed to access " << obbMountDir << "/" << sandboxId;
642 return -errno;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800643 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800644 const unique_fd fd(
645 TEMP_FAILURE_RETRY(openat(obbMountDirFd, sandboxId.c_str(), O_RDWR | O_CREAT, 0600)));
646 if (fd.get() < 0) {
647 PLOG(ERROR) << "Failed to create " << obbMountDir << "/" << sandboxId;
648 return -errno;
649 }
650 } else {
651 if (TEMP_FAILURE_RETRY(faccessat(obbMountDirFd, sandboxId.c_str(), F_OK, 0)) != -1) {
652 if (TEMP_FAILURE_RETRY(unlinkat(obbMountDirFd, sandboxId.c_str(), 0)) == -1) {
653 PLOG(ERROR) << "Failed to unlink " << obbMountDir << "/" << sandboxId;
654 return -errno;
655 }
656 } else if (errno != ENOENT) {
657 PLOG(ERROR) << "Failed to access " << obbMountDir << "/" << sandboxId;
658 return -errno;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800659 }
660 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800661 return 0;
Sudheer Shanka03992e32018-12-12 12:43:38 -0800662}
663
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700664int VolumeManager::prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames,
665 const std::vector<std::string>& visibleVolLabels) {
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800666 prepareSandboxTargets(userId, visibleVolLabels);
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000667
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700668 if (visibleVolLabels.empty()) {
669 return 0;
670 }
671 for (auto& volumeLabel : visibleVolLabels) {
672 std::string volumeRoot(StringPrintf("/mnt/runtime/write/%s", volumeLabel.c_str()));
673 bool isVolPrimaryEmulated = (volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated());
674 if (isVolPrimaryEmulated) {
675 StringAppendF(&volumeRoot, "/%d", userId);
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000676 if (CreateDir(volumeRoot, 0755) < 0) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700677 return -errno;
678 }
679 }
680
681 std::string sandboxRoot =
682 prepareSubDirs(volumeRoot, "Android/sandbox/", 0700, AID_ROOT, AID_ROOT);
683 if (sandboxRoot.empty()) {
684 return -errno;
685 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700686 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800687 if (mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels, -1) < 0) {
688 PLOG(ERROR) << "Failed to setup sandboxes for already running processes";
689 return -errno;
690 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700691 return 0;
692}
693
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000694int VolumeManager::prepareSandboxTargets(userid_t userId,
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000695 const std::vector<std::string>& visibleVolLabels) {
696 std::string mntTargetRoot = StringPrintf("/mnt/user/%d", userId);
697 if (fs_prepare_dir(mntTargetRoot.c_str(), 0751, AID_ROOT, AID_ROOT) != 0) {
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800698 PLOG(ERROR) << "Failed to fs_prepare_dir " << mntTargetRoot;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000699 return -errno;
700 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800701
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000702 StringAppendF(&mntTargetRoot, "/package");
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800703 if (fs_prepare_dir(mntTargetRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
704 PLOG(ERROR) << "Failed to fs_prepare_dir " << mntTargetRoot;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000705 return -errno;
706 }
707
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800708 uid_t uid = multiuser_get_uid(userId, AID_EVERYBODY);
709 for (auto& volumeLabel : visibleVolLabels) {
710 std::string sandboxTarget =
711 StringPrintf("%s/%s", mntTargetRoot.c_str(), volumeLabel.c_str());
712 if (fs_prepare_dir(sandboxTarget.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
713 PLOG(ERROR) << "Failed to fs_prepare_dir " << sandboxTarget;
714 return -errno;
715 }
716
717 if (mPrimary && volumeLabel == mPrimary->getLabel() && mPrimary->isEmulated()) {
718 StringAppendF(&sandboxTarget, "/%d", userId);
719 if (fs_prepare_dir(sandboxTarget.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
720 PLOG(ERROR) << "Failed to fs_prepare_dir " << sandboxTarget;
721 return -errno;
722 }
723 }
724 }
725
726 StringAppendF(&mntTargetRoot, "/self");
727 if (fs_prepare_dir(mntTargetRoot.c_str(), 0755, AID_ROOT, AID_ROOT) != 0) {
728 PLOG(ERROR) << "Failed to fs_prepare_dir " << mntTargetRoot;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000729 return -errno;
730 }
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000731
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000732 if (mPrimary) {
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800733 std::string pkgPrimarySource(mPrimary->getPath());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000734 if (mPrimary->isEmulated()) {
735 StringAppendF(&pkgPrimarySource, "/%d", userId);
736 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800737 StringAppendF(&mntTargetRoot, "/primary");
738 if (Symlink(pkgPrimarySource, mntTargetRoot) < 0) {
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000739 return -errno;
740 }
741 }
742 return 0;
743}
744
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700745std::string VolumeManager::prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs,
746 mode_t mode, uid_t uid, gid_t gid) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700747 std::string path(pathPrefix);
748 std::vector<std::string> subDirList = android::base::Split(subDirs, "/");
749 for (size_t i = 0; i < subDirList.size(); ++i) {
750 std::string subDir = subDirList[i];
751 if (subDir.empty()) {
752 continue;
753 }
754 StringAppendF(&path, "/%s", subDir.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000755 if (CreateDir(path, mode) < 0) {
Sudheer Shanka53947a32018-08-01 10:24:13 -0700756 return kEmptyString;
757 }
758 }
759 return path;
760}
761
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700762bool VolumeManager::createPkgSpecificDirRoots(const std::string& volumeRoot) {
763 std::string volumeAndroidRoot = StringPrintf("%s/Android", volumeRoot.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000764 if (CreateDir(volumeAndroidRoot, 0700) < 0) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700765 return false;
766 }
767 std::array<std::string, 3> dirs = {"data", "media", "obb"};
768 for (auto& dir : dirs) {
769 std::string path = StringPrintf("%s/%s", volumeAndroidRoot.c_str(), dir.c_str());
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000770 if (CreateDir(path, 0700) < 0) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700771 return false;
772 }
773 }
774 return true;
Sudheer Shanka53947a32018-08-01 10:24:13 -0700775}
776
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700777int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
778 mAddedUsers[userId] = userSerialNumber;
779 return 0;
780}
781
782int VolumeManager::onUserRemoved(userid_t userId) {
783 mAddedUsers.erase(userId);
784 return 0;
785}
786
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700787int VolumeManager::onUserStarted(userid_t userId, const std::vector<std::string>& packageNames,
788 const std::vector<int>& appIds,
789 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700790 LOG(VERBOSE) << "onUserStarted: " << userId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700791 // Note that sometimes the system will spin up processes from Zygote
792 // before actually starting the user, so we're okay if Zygote
793 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600794 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700795 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
796
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700797 mStartedUsers.insert(userId);
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700798 mUserPackages[userId] = packageNames;
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700799 for (size_t i = 0; i < packageNames.size(); ++i) {
800 mAppIds[packageNames[i]] = appIds[i];
801 mSandboxIds[appIds[i]] = sandboxIds[i];
802 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700803 if (mPrimary) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700804 linkPrimary(userId);
805 }
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800806 if (hasIsolatedStorage()) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700807 std::vector<std::string> visibleVolLabels;
808 for (auto& volId : mVisibleVolumeIds) {
809 auto vol = findVolume(volId);
810 userid_t mountUserId = vol->getMountUserId();
811 if (mountUserId == userId || vol->isEmulated()) {
812 visibleVolLabels.push_back(vol->getLabel());
813 }
814 }
815 if (prepareSandboxes(userId, packageNames, visibleVolLabels) != 0) {
816 return -errno;
817 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700818 }
819 return 0;
820}
821
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700822int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700823 LOG(VERBOSE) << "onUserStopped: " << userId;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700824 mStartedUsers.erase(userId);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700825
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800826 if (hasIsolatedStorage()) {
Sudheer Shanka023b5392019-02-06 12:39:19 -0800827 auto& userPackages = mUserPackages[userId];
828 std::string userMntTargetRoot = StringPrintf("/mnt/user/%d", userId);
Sudheer Shanka727c5fa2019-02-21 15:00:11 -0800829 std::string pkgPrimaryDir =
830 StringPrintf("%s/package/self/primary", userMntTargetRoot.c_str());
831 if (Unlink(pkgPrimaryDir) < 0) {
832 return -errno;
Sudheer Shanka023b5392019-02-06 12:39:19 -0800833 }
Sudheer Shanka9acc6d42018-10-06 19:03:02 -0700834 mUserPackages.erase(userId);
Sudheer Shanka023b5392019-02-06 12:39:19 -0800835 if (DeleteDirContentsAndDir(userMntTargetRoot) < 0) {
836 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << userMntTargetRoot;
Sudheer Shanka9acc6d42018-10-06 19:03:02 -0700837 return -errno;
838 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800839 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << userMntTargetRoot;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700840 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700841 return 0;
842}
843
Sudheer Shankad484aa92018-07-31 10:07:34 -0700844int VolumeManager::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700845 const std::vector<int32_t>& appIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700846 for (size_t i = 0; i < packageNames.size(); ++i) {
847 mAppIds[packageNames[i]] = appIds[i];
848 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700849 return 0;
850}
851
852int VolumeManager::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700853 const std::vector<std::string>& sandboxIds) {
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700854 for (size_t i = 0; i < appIds.size(); ++i) {
855 mSandboxIds[appIds[i]] = sandboxIds[i];
856 }
Sudheer Shankad484aa92018-07-31 10:07:34 -0700857 return 0;
858}
859
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700860int VolumeManager::prepareSandboxForApp(const std::string& packageName, appid_t appId,
861 const std::string& sandboxId, userid_t userId) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800862 if (!hasIsolatedStorage()) {
Sudheer Shankac7562092018-08-24 10:20:56 -0700863 return 0;
864 } else if (mStartedUsers.find(userId) == mStartedUsers.end()) {
865 // User not started, no need to do anything now. Required bind mounts for the package will
866 // be created when the user starts.
867 return 0;
868 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700869 LOG(VERBOSE) << "prepareSandboxForApp: " << packageName << ", appId=" << appId
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700870 << ", sandboxId=" << sandboxId << ", userId=" << userId;
Sudheer Shankac7562092018-08-24 10:20:56 -0700871 mUserPackages[userId].push_back(packageName);
872 mAppIds[packageName] = appId;
873 mSandboxIds[appId] = sandboxId;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700874
875 std::vector<std::string> visibleVolLabels;
876 for (auto& volId : mVisibleVolumeIds) {
877 auto vol = findVolume(volId);
878 userid_t mountUserId = vol->getMountUserId();
879 if (mountUserId == userId || vol->isEmulated()) {
880 visibleVolLabels.push_back(vol->getLabel());
881 }
Sudheer Shankac7562092018-08-24 10:20:56 -0700882 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700883 return prepareSandboxes(userId, {packageName}, visibleVolLabels);
Sudheer Shankac7562092018-08-24 10:20:56 -0700884}
885
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700886int VolumeManager::destroySandboxForApp(const std::string& packageName,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700887 const std::string& sandboxId, userid_t userId) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800888 if (!hasIsolatedStorage()) {
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700889 return 0;
890 }
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700891 LOG(VERBOSE) << "destroySandboxForApp: " << packageName << ", sandboxId=" << sandboxId
892 << ", userId=" << userId;
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700893 auto& userPackages = mUserPackages[userId];
Sudheer Shankaba0c62f2018-12-16 18:22:34 -0800894 userPackages.erase(std::remove(userPackages.begin(), userPackages.end(), packageName),
895 userPackages.end());
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700896 // If the package is not uninstalled in any other users, remove appId and sandboxId
897 // corresponding to it from the internal state.
898 bool installedInAnyUser = false;
899 for (auto& it : mUserPackages) {
900 auto& packages = it.second;
901 if (std::find(packages.begin(), packages.end(), packageName) != packages.end()) {
902 installedInAnyUser = true;
903 break;
904 }
905 }
906 if (!installedInAnyUser) {
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700907 const auto& entry = mAppIds.find(packageName);
908 if (entry != mAppIds.end()) {
909 mSandboxIds.erase(entry->second);
910 mAppIds.erase(entry);
911 }
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700912 }
913
914 std::vector<std::string> visibleVolLabels;
915 for (auto& volId : mVisibleVolumeIds) {
916 auto vol = findVolume(volId);
917 userid_t mountUserId = vol->getMountUserId();
918 if (mountUserId == userId || vol->isEmulated()) {
919 if (destroySandboxForAppOnVol(packageName, sandboxId, userId, vol->getLabel()) < 0) {
920 return -errno;
921 }
922 }
923 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800924
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700925 return 0;
926}
927
928int VolumeManager::destroySandboxForAppOnVol(const std::string& packageName,
929 const std::string& sandboxId, userid_t userId,
930 const std::string& volLabel) {
931 LOG(VERBOSE) << "destroySandboxOnVol: " << packageName << ", userId=" << userId
932 << ", volLabel=" << volLabel;
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700933
934 std::string sandboxDir = StringPrintf("/mnt/runtime/write/%s", volLabel.c_str());
935 if (volLabel == mPrimary->getLabel() && mPrimary->isEmulated()) {
936 StringAppendF(&sandboxDir, "/%d", userId);
937 }
Sudheer Shanka51a38da2018-10-18 08:51:31 -0700938 StringAppendF(&sandboxDir, "/Android/sandbox/%s", sandboxId.c_str());
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700939
Sudheer Shanka023b5392019-02-06 12:39:19 -0800940 if (DeleteDirContentsAndDir(sandboxDir) < 0) {
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700941 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << sandboxDir;
942 return -errno;
943 }
Sudheer Shanka023b5392019-02-06 12:39:19 -0800944
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700945 return 0;
946}
947
Jeff Sharkey401b2602017-12-14 22:15:20 -0700948int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
949 mSecureKeyguardShowing = isShowing;
950 if (!mSecureKeyguardShowing) {
951 // Now that secure keyguard has been dismissed, process
952 // any pending disks
953 for (const auto& disk : mPendingDisks) {
954 disk->create();
955 mDisks.push_back(disk);
956 }
957 mPendingDisks.clear();
958 }
959 return 0;
960}
961
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700962int VolumeManager::onVolumeMounted(android::vold::VolumeBase* vol) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800963 if (!hasIsolatedStorage()) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700964 return 0;
965 }
966
967 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_VISIBLE) == 0) {
968 return 0;
969 }
970
971 mVisibleVolumeIds.insert(vol->getId());
972 userid_t mountUserId = vol->getMountUserId();
973 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
974 // We don't want to create another shared_ptr here because then we will end up with
975 // two shared_ptrs owning the underlying pointer without sharing it.
976 mPrimary = findVolume(vol->getId());
977 for (userid_t userId : mStartedUsers) {
978 if (linkPrimary(userId) != 0) {
979 return -errno;
980 }
981 }
982 }
983 if (vol->isEmulated()) {
984 for (userid_t userId : mStartedUsers) {
985 if (prepareSandboxes(userId, mUserPackages[userId], {vol->getLabel()}) != 0) {
986 return -errno;
987 }
988 }
989 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
990 if (prepareSandboxes(mountUserId, mUserPackages[mountUserId], {vol->getLabel()}) != 0) {
991 return -errno;
992 }
993 }
994 return 0;
995}
996
997int VolumeManager::onVolumeUnmounted(android::vold::VolumeBase* vol) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -0800998 if (!hasIsolatedStorage()) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700999 return 0;
1000 }
1001
1002 if (mVisibleVolumeIds.erase(vol->getId()) == 0) {
1003 return 0;
1004 }
1005
1006 if ((vol->getMountFlags() & android::vold::VoldNativeService::MOUNT_FLAG_PRIMARY) != 0) {
1007 mPrimary = nullptr;
1008 }
1009
1010 LOG(VERBOSE) << "visibleVolumeUnmounted: " << vol;
1011 userid_t mountUserId = vol->getMountUserId();
1012 if (vol->isEmulated()) {
1013 for (userid_t userId : mStartedUsers) {
1014 if (destroySandboxesForVol(vol, userId) != 0) {
1015 return -errno;
1016 }
1017 }
1018 } else if (mStartedUsers.find(mountUserId) != mStartedUsers.end()) {
1019 if (destroySandboxesForVol(vol, mountUserId) != 0) {
1020 return -errno;
1021 }
1022 }
1023 return 0;
1024}
1025
1026int VolumeManager::destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId) {
1027 LOG(VERBOSE) << "destroysandboxesForVol: " << vol << " for user=" << userId;
Sudheer Shanka727c5fa2019-02-21 15:00:11 -08001028 std::string volSandboxRoot =
1029 StringPrintf("/mnt/user/%d/package/%s", userId, vol->getLabel().c_str());
1030 if (android::vold::DeleteDirContentsAndDir(volSandboxRoot) < 0) {
1031 PLOG(ERROR) << "DeleteDirContentsAndDir failed on " << volSandboxRoot;
1032 return -errno;
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001033 }
Sudheer Shanka727c5fa2019-02-21 15:00:11 -08001034 LOG(VERBOSE) << "Success: DeleteDirContentsAndDir on " << volSandboxRoot;
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001035 return 0;
1036}
1037
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001038int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -08001039 if (hasIsolatedStorage()) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001040 return 0;
1041 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001042 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001043 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001044 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001045 }
1046 return 0;
1047}
1048
Sudheer Shanka817b9112018-12-13 17:40:28 -08001049int VolumeManager::remountUid(uid_t uid, int32_t mountMode) {
Sudheer Shanka34b437b2019-01-17 17:20:47 -08001050 if (!hasIsolatedStorage()) {
Sudheer Shanka817b9112018-12-13 17:40:28 -08001051 return remountUidLegacy(uid, mountMode);
1052 }
1053
1054 appid_t appId = multiuser_get_app_id(uid);
1055 userid_t userId = multiuser_get_user_id(uid);
1056 std::vector<std::string> visibleVolLabels;
1057 for (auto& volId : mVisibleVolumeIds) {
1058 auto vol = findVolume(volId);
1059 userid_t mountUserId = vol->getMountUserId();
1060 if (mountUserId == userId || vol->isEmulated()) {
1061 visibleVolLabels.push_back(vol->getLabel());
1062 }
1063 }
1064
1065 // Finding one package with appId is enough
1066 std::vector<std::string> packageNames;
1067 for (auto it = mAppIds.begin(); it != mAppIds.end(); ++it) {
1068 if (it->second == appId) {
1069 packageNames.push_back(it->first);
1070 break;
1071 }
1072 }
1073 if (packageNames.empty()) {
1074 PLOG(ERROR) << "Failed to find packageName for " << uid;
Sudheer Shanka53947a32018-08-01 10:24:13 -07001075 return -1;
1076 }
Sudheer Shanka817b9112018-12-13 17:40:28 -08001077 return mountPkgSpecificDirsForRunningProcs(userId, packageNames, visibleVolLabels, mountMode);
1078}
1079
1080int VolumeManager::remountUidLegacy(uid_t uid, int32_t mountMode) {
1081 std::string mode;
1082 switch (mountMode) {
1083 case VoldNativeService::REMOUNT_MODE_NONE:
1084 mode = "none";
1085 break;
1086 case VoldNativeService::REMOUNT_MODE_DEFAULT:
1087 mode = "default";
1088 break;
1089 case VoldNativeService::REMOUNT_MODE_READ:
1090 mode = "read";
1091 break;
1092 case VoldNativeService::REMOUNT_MODE_WRITE:
1093 mode = "write";
1094 break;
1095 default:
1096 PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode);
1097 return -1;
1098 }
Jeff Sharkey66270a22015-06-24 11:49:24 -07001099 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
1100
1101 DIR* dir;
1102 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001103 std::string rootName;
1104 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001105 int pidFd;
1106 int nsFd;
1107 struct stat sb;
1108 pid_t child;
1109
1110 if (!(dir = opendir("/proc"))) {
1111 PLOG(ERROR) << "Failed to opendir";
1112 return -1;
1113 }
1114
1115 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -06001116 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
1117 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001118 closedir(dir);
1119 return -1;
1120 }
1121
1122 // Poke through all running PIDs look for apps running as UID
1123 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -07001124 pid_t pid;
1125 if (de->d_type != DT_DIR) continue;
1126 if (!android::base::ParseInt(de->d_name, &pid)) continue;
1127
Jeff Sharkey66270a22015-06-24 11:49:24 -07001128 pidFd = -1;
1129 nsFd = -1;
1130
1131 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1132 if (pidFd < 0) {
1133 goto next;
1134 }
1135 if (fstat(pidFd, &sb) != 0) {
1136 PLOG(WARNING) << "Failed to stat " << de->d_name;
1137 goto next;
1138 }
1139 if (sb.st_uid != uid) {
1140 goto next;
1141 }
1142
1143 // Matches so far, but refuse to touch if in root namespace
1144 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -06001145 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001146 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
1147 goto next;
1148 }
Jeff Sharkey3472e522017-10-06 18:02:53 -06001149 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -07001150 LOG(WARNING) << "Skipping due to root namespace";
1151 goto next;
1152 }
1153
1154 // We purposefully leave the namespace open across the fork
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001155 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -07001156 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001157 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001158 goto next;
1159 }
1160
1161 if (!(child = fork())) {
1162 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001163 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -07001164 _exit(1);
1165 }
1166
Sudheer Shanka99d304a2018-09-27 14:53:51 -07001167 android::vold::UnmountTree("/storage/");
Jeff Sharkey66270a22015-06-24 11:49:24 -07001168
1169 std::string storageSource;
1170 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001171 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001172 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001173 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001174 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -07001175 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -07001176 } else {
1177 // Sane default of no storage visible
1178 _exit(0);
1179 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001180 if (TEMP_FAILURE_RETRY(
1181 mount(storageSource.c_str(), "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
1182 PLOG(ERROR) << "Failed to mount " << storageSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001183 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -07001184 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001185 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
1186 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for " << de->d_name;
Hidehiko Abe674bed12016-03-09 16:42:10 +09001187 _exit(1);
1188 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001189
1190 // Mount user-specific symlink helper into place
1191 userid_t user_id = multiuser_get_user_id(uid);
1192 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001193 if (TEMP_FAILURE_RETRY(
1194 mount(userSource.c_str(), "/storage/self", NULL, MS_BIND, NULL)) == -1) {
1195 PLOG(ERROR) << "Failed to mount " << userSource << " for " << de->d_name;
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -07001196 _exit(1);
1197 }
1198
Jeff Sharkey66270a22015-06-24 11:49:24 -07001199 _exit(0);
1200 }
1201
1202 if (child == -1) {
1203 PLOG(ERROR) << "Failed to fork";
1204 goto next;
1205 } else {
1206 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
1207 }
1208
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001209 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -07001210 close(nsFd);
1211 close(pidFd);
1212 }
1213 closedir(dir);
1214 return 0;
1215}
1216
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001217int VolumeManager::reset() {
1218 // Tear down all existing disks/volumes and start from a blank slate so
1219 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +08001220 if (mInternalEmulated != nullptr) {
1221 mInternalEmulated->destroy();
1222 mInternalEmulated->create();
1223 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001224 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001225 disk->destroy();
1226 disk->create();
1227 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -06001228 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -07001229 mAddedUsers.clear();
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -07001230
1231 mUserPackages.clear();
1232 mAppIds.clear();
1233 mSandboxIds.clear();
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001234 mVisibleVolumeIds.clear();
Sudheer Shanka3a7ee5c2018-08-07 17:10:11 -07001235
Sudheer Shanka023b5392019-02-06 12:39:19 -08001236 for (userid_t userId : mStartedUsers) {
1237 DeleteDirContentsAndDir(StringPrintf("/mnt/user/%d/package", userId));
1238 }
1239 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001240 return 0;
1241}
1242
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001243// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001244int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001245 if (mInternalEmulated == nullptr) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001246 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001247 }
Paul Crowley56292ef2017-10-20 08:07:53 -07001248 android::vold::sSleepOnUnmount = false;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001249 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -07001250 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001251 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001252 disk->destroy();
1253 }
Risan8c9f3322018-10-29 08:52:56 +09001254 mStubVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001255 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -07001256 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -07001257 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001258 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -07001259}
1260
Jeff Sharkey9c484982015-03-31 10:35:33 -07001261int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001262 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -06001263 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -07001264
Jeff Sharkey9c484982015-03-31 10:35:33 -07001265 // First, try gracefully unmounting all known devices
1266 if (mInternalEmulated != nullptr) {
1267 mInternalEmulated->unmount();
1268 }
Risan8c9f3322018-10-29 08:52:56 +09001269 for (const auto& stub : mStubVolumes) {
1270 stub->unmount();
1271 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001272 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001273 disk->unmountAll();
1274 }
1275
1276 // Worst case we might have some stale mounts lurking around, so
1277 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +08001278 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -07001279 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001280 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -07001281 return -errno;
1282 }
1283
1284 // Some volumes can be stacked on each other, so force unmount in
1285 // reverse order to give us the best chance of success.
1286 std::list<std::string> toUnmount;
1287 mntent* mentry;
1288 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001289 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001290 if ((StartsWith(test, "/mnt/") &&
1291#ifdef __ANDROID_DEBUGGABLE__
1292 !StartsWith(test, "/mnt/scratch") &&
1293#endif
1294 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product")) ||
1295 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001296 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -07001297 }
1298 }
1299 endmntent(fp);
1300
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -07001301 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001302 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -07001303 android::vold::ForceUnmount(path);
1304 }
1305
1306 return 0;
1307}
1308
Jeff Sharkey3472e522017-10-06 18:02:53 -06001309int VolumeManager::mkdirs(const std::string& path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001310 // Only offer to create directories for paths managed by vold
Mark Salyzyn86e81e72018-09-20 10:09:27 -07001311 if (StartsWith(path, "/storage/")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001312 // fs_mkdirs() does symlink checking and relative path enforcement
Jeff Sharkey3472e522017-10-06 18:02:53 -06001313 return fs_mkdirs(path.c_str(), 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001314 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001315 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001316 return -EINVAL;
1317 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001318}
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001319
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001320int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001321 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001322 int id = mNextObbId++;
1323
1324 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -07001325 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001326 vol->create();
1327
1328 mObbVolumes.push_back(vol);
1329 *outVolId = vol->getId();
1330 return android::OK;
1331}
1332
1333int VolumeManager::destroyObb(const std::string& volId) {
1334 auto i = mObbVolumes.begin();
1335 while (i != mObbVolumes.end()) {
1336 if ((*i)->getId() == volId) {
1337 (*i)->destroy();
1338 i = mObbVolumes.erase(i);
1339 } else {
1340 ++i;
1341 }
1342 }
1343 return android::OK;
1344}
1345
Risan8c9f3322018-10-29 08:52:56 +09001346int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
1347 const std::string& fsType, const std::string& fsUuid,
1348 const std::string& fsLabel, std::string* outVolId) {
1349 int id = mNextStubVolumeId++;
1350 auto vol = std::shared_ptr<android::vold::VolumeBase>(
1351 new android::vold::StubVolume(id, sourcePath, mountPath, fsType, fsUuid, fsLabel));
1352 vol->create();
1353
1354 mStubVolumes.push_back(vol);
1355 *outVolId = vol->getId();
1356 return android::OK;
1357}
1358
1359int VolumeManager::destroyStubVolume(const std::string& volId) {
1360 auto i = mStubVolumes.begin();
1361 while (i != mStubVolumes.end()) {
1362 if ((*i)->getId() == volId) {
1363 (*i)->destroy();
1364 i = mStubVolumes.erase(i);
1365 } else {
1366 ++i;
1367 }
1368 }
1369 return android::OK;
1370}
1371
Risan8f6198d2018-10-26 20:56:45 -06001372int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -06001373 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001374}
1375
Risan8f6198d2018-10-26 20:56:45 -06001376int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -06001377 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -06001378}
1379
1380int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -06001381 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001382}