blob: f1667f23fb3f032f00b9065d2bd674c17e5e95c0 [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
Yabin Cuid1104f72015-01-02 13:28:28 -080017#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070018#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080019#include <fcntl.h>
Kenny Root344ca102012-04-03 17:23:01 -070020#include <fts.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080021#include <mntent.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/mount.h>
San Mehata19b2502010-01-06 10:33:53 -080027#include <sys/stat.h>
28#include <sys/types.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080029#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080030
San Mehata2677e42009-12-13 10:40:18 -080031#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070032
33#define LOG_TAG "Vold"
34
Kenny Root7b18a7b2010-03-15 13:13:41 -070035#include <openssl/md5.h>
36
Jeff Sharkey36801cc2015-03-13 16:09:20 -070037#include <base/logging.h>
38#include <base/stringprintf.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070039#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070040#include <cutils/log.h>
41
Robert Craigb9e3ba52014-02-04 10:53:00 -050042#include <selinux/android.h>
43
San Mehatfd7f5872009-10-12 11:32:47 -070044#include <sysutils/NetlinkEvent.h>
45
Kenny Root344ca102012-04-03 17:23:01 -070046#include <private/android_filesystem_config.h>
47
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070048#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070049#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070050#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080052#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080053#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070054#include "fs/Ext4.h"
55#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070056#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080057#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080058#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080059#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090060#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070061#include "cryptfs.h"
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070062#include "fstrim.h"
San Mehat23969932010-01-09 07:08:06 -080063
Mike Lockwood97f2fc12011-06-07 10:51:38 -070064#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
65
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070066#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
67 + (number & (~((1U << po2) - 1))))
68
Jeff Sharkey36801cc2015-03-13 16:09:20 -070069using android::base::StringPrintf;
70
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070071/*
72 * Path to external storage where *only* root can access ASEC image files
73 */
74const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
75
76/*
77 * Path to internal storage where *only* root can access ASEC image files
78 */
79const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
80
81/*
82 * Path to where secure containers are mounted
83 */
84const char *VolumeManager::ASECDIR = "/mnt/asec";
85
86/*
87 * Path to where OBBs are mounted
88 */
89const char *VolumeManager::LOOPDIR = "/mnt/obb";
90
Jeff Sharkey36801cc2015-03-13 16:09:20 -070091static const char* kUserMountPath = "/mnt/user";
92
Jeff Sharkey36801cc2015-03-13 16:09:20 -070093static const unsigned int kMajorBlockMmc = 179;
94
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070095/* writes superblock at end of file or device given by name */
96static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070097 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070098 if (sbfd < 0) {
99 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
100 return -1;
101 }
102
103 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
104 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
105 close(sbfd);
106 return -1;
107 }
108
109 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
110 SLOGE("Failed to write superblock (%s)", strerror(errno));
111 close(sbfd);
112 return -1;
113 }
114 close(sbfd);
115 return 0;
116}
117
118static int adjustSectorNumExt4(unsigned numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700119 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
120 // preventing costly operations or unexpected ENOSPC error.
121 // Ext4::format() uses default block size without clustering.
122 unsigned clusterSectors = 4096 / 512;
123 unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
124 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700125 return ROUND_UP_POWER_OF_2(numSectors, 3);
126}
127
128static int adjustSectorNumFAT(unsigned numSectors) {
129 /*
130 * Add some headroom
131 */
132 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
133 numSectors += fatSize + 2;
134 /*
135 * FAT is aligned to 32 kb with 512b sectors.
136 */
137 return ROUND_UP_POWER_OF_2(numSectors, 6);
138}
139
140static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
141 if (Loop::lookupActive(idHash, buffer, len)) {
142 if (Loop::create(idHash, asecFileName, buffer, len)) {
143 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
144 return -1;
145 }
146 if (debug) {
147 SLOGD("New loop device created at %s", buffer);
148 }
149 } else {
150 if (debug) {
151 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
152 }
153 }
154 return 0;
155}
156
157static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) {
158 if (strcmp(key, "none")) {
159 if (Devmapper::lookupActive(idHash, buffer, len)) {
160 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
161 buffer, len)) {
162 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
163 return -1;
164 }
165 if (debug) {
166 SLOGD("New devmapper instance created at %s", buffer);
167 }
168 } else {
169 if (debug) {
170 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
171 }
172 }
173 *createdDMDevice = true;
174 } else {
175 strcpy(buffer, loopDevice);
176 *createdDMDevice = false;
177 }
178 return 0;
179}
180
181static void waitForDevMapper(const char *dmDevice) {
182 /*
183 * Wait for the device mapper node to be created. Sometimes it takes a
184 * while. Wait for up to 1 second. We could also inspect incoming uevents,
185 * but that would take more effort.
186 */
187 int tries = 25;
188 while (tries--) {
189 if (!access(dmDevice, F_OK) || errno != ENOENT) {
190 break;
191 }
192 usleep(40 * 1000);
193 }
194}
195
San Mehatf1b736b2009-10-10 17:22:08 -0700196VolumeManager *VolumeManager::sInstance = NULL;
197
198VolumeManager *VolumeManager::Instance() {
199 if (!sInstance)
200 sInstance = new VolumeManager();
201 return sInstance;
202}
203
204VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800205 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800206 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700207 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400208 mUmsSharingCount = 0;
209 mSavedDirtyRatio = -1;
210 // set dirty ratio to 0 when UMS is active
211 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700212}
213
214VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800215 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700216}
217
Kenny Root7b18a7b2010-03-15 13:13:41 -0700218char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700219 static const char* digits = "0123456789abcdef";
220
Kenny Root7b18a7b2010-03-15 13:13:41 -0700221 unsigned char sig[MD5_DIGEST_LENGTH];
222
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700223 if (buffer == NULL) {
224 SLOGE("Destination buffer is NULL");
225 errno = ESPIPE;
226 return NULL;
227 } else if (id == NULL) {
228 SLOGE("Source buffer is NULL");
229 errno = ESPIPE;
230 return NULL;
231 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800232 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700233 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800234 errno = ESPIPE;
235 return NULL;
236 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700237
238 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800239
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700240 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700241 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700242 *p++ = digits[sig[i] >> 4];
243 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800244 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700245 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800246
247 return buffer;
248}
249
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700250int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800251 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700252 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800253}
254
San Mehatf1b736b2009-10-10 17:22:08 -0700255int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700256 // Always start from a clean slate by unmounting everything in
257 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700258 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700259
260 // Assume that we always have an emulated volume on internal
261 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700262 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700263 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700264 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700265 mInternalEmulated->create();
266
San Mehatf1b736b2009-10-10 17:22:08 -0700267 return 0;
268}
269
270int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700271 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700272 mInternalEmulated->destroy();
273 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700274 return 0;
275}
276
San Mehatfd7f5872009-10-12 11:32:47 -0700277void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700278 std::lock_guard<std::mutex> lock(mLock);
279
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700280 if (mDebug) {
281 LOG(VERBOSE) << "----------------";
282 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
283 evt->dump();
284 }
San Mehatf1b736b2009-10-10 17:22:08 -0700285
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700286 std::string eventPath(evt->findParam("DEVPATH"));
287 std::string devType(evt->findParam("DEVTYPE"));
288
289 if (devType != "disk") return;
290
291 int major = atoi(evt->findParam("MAJOR"));
292 int minor = atoi(evt->findParam("MINOR"));
293 dev_t device = makedev(major, minor);
294
295 switch (evt->getAction()) {
296 case NetlinkEvent::Action::kAdd: {
297 for (auto source : mDiskSources) {
298 if (source->matches(eventPath)) {
299 // For now, assume that MMC devices are SD, and that
300 // everything else is USB
301 int flags = source->getFlags();
302 if (major == kMajorBlockMmc) {
303 flags |= android::vold::Disk::Flags::kSd;
304 } else {
305 flags |= android::vold::Disk::Flags::kUsb;
306 }
307
308 auto disk = new android::vold::Disk(eventPath, device,
309 source->getNickname(), flags);
310 disk->create();
311 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
312 break;
313 }
314 }
315 break;
316 }
317 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700318 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700319 for (auto disk : mDisks) {
320 if (disk->getDevice() == device) {
321 disk->readMetadata();
322 disk->readPartitions();
323 }
324 }
325 break;
326 }
327 case NetlinkEvent::Action::kRemove: {
328 auto i = mDisks.begin();
329 while (i != mDisks.end()) {
330 if ((*i)->getDevice() == device) {
331 (*i)->destroy();
332 i = mDisks.erase(i);
333 } else {
334 ++i;
335 }
336 }
337 break;
338 }
339 default: {
340 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
341 break;
342 }
343 }
344}
345
346void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
347 mDiskSources.push_back(diskSource);
348}
349
350std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
351 for (auto disk : mDisks) {
352 if (disk->getId() == id) {
353 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700354 }
355 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700356 return nullptr;
357}
San Mehatf1b736b2009-10-10 17:22:08 -0700358
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700359std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
360 if (mInternalEmulated->getId() == id) {
361 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700362 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700363 for (auto disk : mDisks) {
364 auto vol = disk->findVolume(id);
365 if (vol != nullptr) {
366 return vol;
367 }
368 }
369 return nullptr;
370}
371
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700372nsecs_t VolumeManager::benchmarkVolume(const std::string& id) {
373 std::string path;
374 std::string sysPath;
375 auto vol = findVolume(id);
376 if (vol != nullptr) {
377 if (vol->getState() == android::vold::VolumeBase::State::kMounted) {
378 path = vol->getPath();
379 auto disk = findDisk(vol->getDiskId());
380 if (disk != nullptr) {
381 sysPath = disk->getSysPath();
382 }
383 }
384 } else {
385 path = "/data";
386 }
387
388 if (path.empty()) {
389 LOG(WARNING) << "Failed to find volume for " << id;
390 return -1;
391 }
392
393 path += "/misc";
394 if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
395 return -1;
396 }
397 path += "/vold";
398 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
399 return -1;
400 }
401 path += "/bench";
402 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
403 return -1;
404 }
405
406 return android::vold::Benchmark(path, sysPath);
407}
408
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700409int VolumeManager::forgetPartition(const std::string& partGuid) {
410 std::string normalizedGuid;
411 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
412 LOG(WARNING) << "Invalid GUID " << partGuid;
413 return -1;
414 }
415
416 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
417 if (unlink(keyPath.c_str()) != 0) {
418 LOG(ERROR) << "Failed to unlink " << keyPath;
419 return -1;
420 }
421
422 return 0;
423}
424
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700425int VolumeManager::linkPrimary(userid_t userId) {
426 std::string source(mPrimary->getPath());
427 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
428 source = StringPrintf("%s/%d", source.c_str(), userId);
429 }
430
431 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
432 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
433 if (errno != ENOENT) {
434 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
435 }
436 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700437 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700438 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
439 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
440 strerror(errno));
441 return -errno;
442 }
443 return 0;
444}
445
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700446int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
447 mAddedUsers[userId] = userSerialNumber;
448 return 0;
449}
450
451int VolumeManager::onUserRemoved(userid_t userId) {
452 mAddedUsers.erase(userId);
453 return 0;
454}
455
456int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700457 // Note that sometimes the system will spin up processes from Zygote
458 // before actually starting the user, so we're okay if Zygote
459 // already created this directory.
460 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
461 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
462
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700463 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700464 if (mPrimary) {
465 linkPrimary(userId);
466 }
467 return 0;
468}
469
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700470int VolumeManager::onUserStopped(userid_t userId) {
471 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700472 return 0;
473}
474
475int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
476 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700477 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700478 linkPrimary(userId);
479 }
480 return 0;
481}
482
483int VolumeManager::reset() {
484 // Tear down all existing disks/volumes and start from a blank slate so
485 // newly connected framework hears all events.
486 mInternalEmulated->destroy();
487 mInternalEmulated->create();
488 for (auto disk : mDisks) {
489 disk->destroy();
490 disk->create();
491 }
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700492 mAddedUsers.clear();
493 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700494 return 0;
495}
496
497int VolumeManager::shutdown() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700498 mInternalEmulated->destroy();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700499 for (auto disk : mDisks) {
500 disk->destroy();
501 }
502 mDisks.clear();
503 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700504}
505
Jeff Sharkey9c484982015-03-31 10:35:33 -0700506int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700507 std::lock_guard<std::mutex> lock(mLock);
508
Jeff Sharkey9c484982015-03-31 10:35:33 -0700509 // First, try gracefully unmounting all known devices
510 if (mInternalEmulated != nullptr) {
511 mInternalEmulated->unmount();
512 }
513 for (auto disk : mDisks) {
514 disk->unmountAll();
515 }
516
517 // Worst case we might have some stale mounts lurking around, so
518 // force unmount those just to be safe.
519 FILE* fp = setmntent("/proc/mounts", "r");
520 if (fp == NULL) {
521 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
522 return -errno;
523 }
524
525 // Some volumes can be stacked on each other, so force unmount in
526 // reverse order to give us the best chance of success.
527 std::list<std::string> toUnmount;
528 mntent* mentry;
529 while ((mentry = getmntent(fp)) != NULL) {
530 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
531 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
532 toUnmount.push_front(std::string(mentry->mnt_dir));
533 }
534 }
535 endmntent(fp);
536
537 for (auto path : toUnmount) {
538 SLOGW("Tearing down stale mount %s", path.c_str());
539 android::vold::ForceUnmount(path);
540 }
541
542 return 0;
543}
544
Kenny Root508c0e12010-07-12 09:59:49 -0700545int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
546 char idHash[33];
547 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
548 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
549 return -1;
550 }
551
552 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700553 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400554 if ((written < 0) || (written >= mountPathLen)) {
555 errno = EINVAL;
556 return -1;
557 }
Kenny Root508c0e12010-07-12 09:59:49 -0700558
559 if (access(mountPath, F_OK)) {
560 errno = ENOENT;
561 return -1;
562 }
563
564 return 0;
565}
566
San Mehata19b2502010-01-06 10:33:53 -0800567int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700568 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700569
Nick Kralevich0de7c612014-01-27 14:58:06 -0800570 if (!isLegalAsecId(id)) {
571 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
572 errno = EINVAL;
573 return -1;
574 }
575
Kenny Root344ca102012-04-03 17:23:01 -0700576 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
577 SLOGE("Couldn't find ASEC %s", id);
578 return -1;
579 }
San Mehat88ac2c02010-03-23 11:15:58 -0700580
581 memset(buffer, 0, maxlen);
582 if (access(asecFileName, F_OK)) {
583 errno = ENOENT;
584 return -1;
585 }
San Mehata19b2502010-01-06 10:33:53 -0800586
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700587 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400588 if ((written < 0) || (written >= maxlen)) {
589 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
590 errno = EINVAL;
591 return -1;
592 }
593
San Mehata19b2502010-01-06 10:33:53 -0800594 return 0;
595}
596
Dianne Hackborn736910c2011-06-27 13:37:07 -0700597int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
598 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700599
Nick Kralevich0de7c612014-01-27 14:58:06 -0800600 if (!isLegalAsecId(id)) {
601 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
602 errno = EINVAL;
603 return -1;
604 }
605
Kenny Root344ca102012-04-03 17:23:01 -0700606 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
607 SLOGE("Couldn't find ASEC %s", id);
608 return -1;
609 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700610
611 memset(buffer, 0, maxlen);
612 if (access(asecFileName, F_OK)) {
613 errno = ENOENT;
614 return -1;
615 }
616
rpcraigd1c226f2012-10-09 06:58:16 -0400617 int written = snprintf(buffer, maxlen, "%s", asecFileName);
618 if ((written < 0) || (written >= maxlen)) {
619 errno = EINVAL;
620 return -1;
621 }
622
Dianne Hackborn736910c2011-06-27 13:37:07 -0700623 return 0;
624}
625
Kenny Root344ca102012-04-03 17:23:01 -0700626int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
627 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800628 struct asec_superblock sb;
629 memset(&sb, 0, sizeof(sb));
630
Nick Kralevich0de7c612014-01-27 14:58:06 -0800631 if (!isLegalAsecId(id)) {
632 SLOGE("createAsec: Invalid asec id \"%s\"", id);
633 errno = EINVAL;
634 return -1;
635 }
636
Kenny Root344ca102012-04-03 17:23:01 -0700637 const bool wantFilesystem = strcmp(fstype, "none");
638 bool usingExt4 = false;
639 if (wantFilesystem) {
640 usingExt4 = !strcmp(fstype, "ext4");
641 if (usingExt4) {
642 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
643 } else if (strcmp(fstype, "fat")) {
644 SLOGE("Invalid filesystem type %s", fstype);
645 errno = EINVAL;
646 return -1;
647 }
648 }
649
San Mehatfcf24fe2010-03-03 12:37:32 -0800650 sb.magic = ASEC_SB_MAGIC;
651 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800652
San Mehatd31e3802010-02-18 08:37:45 -0800653 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700654 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800655 errno = EINVAL;
656 return -1;
657 }
658
San Mehata19b2502010-01-06 10:33:53 -0800659 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700660
661 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
662 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
663 asecFileName, strerror(errno));
664 errno = EADDRINUSE;
665 return -1;
666 }
667
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700668 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700669
rpcraigd1c226f2012-10-09 06:58:16 -0400670 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
671 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
672 errno = EINVAL;
673 return -1;
674 }
San Mehata19b2502010-01-06 10:33:53 -0800675
676 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700677 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700678 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800679 errno = EADDRINUSE;
680 return -1;
681 }
682
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700683 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700684 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700685 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700686 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700687 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800688
689 // Add +1 for our superblock which is at the end
690 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700691 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800692 return -1;
693 }
694
San Mehatd9a4e352010-03-12 13:32:47 -0800695 char idHash[33];
696 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700697 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800698 unlink(asecFileName);
699 return -1;
700 }
701
San Mehata19b2502010-01-06 10:33:53 -0800702 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800703 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700704 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800705 unlink(asecFileName);
706 return -1;
707 }
708
San Mehatb78a32c2010-01-10 13:02:12 -0800709 char dmDevice[255];
710 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800711
San Mehatb78a32c2010-01-10 13:02:12 -0800712 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800713 // XXX: This is all we support for now
714 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800715 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800716 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700717 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800718 Loop::destroyByDevice(loopDevice);
719 unlink(asecFileName);
720 return -1;
721 }
722 cleanupDm = true;
723 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800724 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800725 strcpy(dmDevice, loopDevice);
726 }
727
San Mehatfcf24fe2010-03-03 12:37:32 -0800728 /*
729 * Drop down the superblock at the end of the file
730 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700731 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800732 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800733 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800734 }
735 Loop::destroyByDevice(loopDevice);
736 unlink(asecFileName);
737 return -1;
738 }
739
Kenny Root344ca102012-04-03 17:23:01 -0700740 if (wantFilesystem) {
741 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400742 char mountPoint[255];
743
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700744 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400745 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
746 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
747 if (cleanupDm) {
748 Devmapper::destroy(idHash);
749 }
750 Loop::destroyByDevice(loopDevice);
751 unlink(asecFileName);
752 return -1;
753 }
rpcraiga54e13a2012-09-21 14:17:08 -0400754
Kenny Root344ca102012-04-03 17:23:01 -0700755 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700756 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700757 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700758 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800759 }
San Mehata19b2502010-01-06 10:33:53 -0800760
Kenny Root344ca102012-04-03 17:23:01 -0700761 if (formatStatus < 0) {
762 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800763 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800764 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800765 }
San Mehateb13a902010-01-07 12:12:50 -0800766 Loop::destroyByDevice(loopDevice);
767 unlink(asecFileName);
768 return -1;
769 }
Kenny Root344ca102012-04-03 17:23:01 -0700770
Kenny Root344ca102012-04-03 17:23:01 -0700771 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800772 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700773 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800774 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800775 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800776 }
777 Loop::destroyByDevice(loopDevice);
778 unlink(asecFileName);
779 return -1;
780 }
San Mehatb78a32c2010-01-10 13:02:12 -0800781 }
San Mehata1091cb2010-02-28 20:17:20 -0800782
Kenny Root344ca102012-04-03 17:23:01 -0700783 int mountStatus;
784 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700785 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
786 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -0700787 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700788 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
789 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -0700790 }
791
792 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700793 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800794 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800795 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800796 }
797 Loop::destroyByDevice(loopDevice);
798 unlink(asecFileName);
799 return -1;
800 }
Kenny Root344ca102012-04-03 17:23:01 -0700801
802 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700803 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -0700804 if (dirfd >= 0) {
805 if (fchown(dirfd, ownerUid, AID_SYSTEM)
806 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
807 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
808 }
809 close(dirfd);
810 }
811 }
San Mehata1091cb2010-02-28 20:17:20 -0800812 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700813 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800814 }
San Mehat88705162010-01-15 09:26:28 -0800815
Kenny Rootcbacf782010-09-24 15:11:48 -0700816 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800817 return 0;
818}
819
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700820int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
821 char asecFileName[255];
822 char mountPoint[255];
823 bool cleanupDm = false;
824
825 if (!isLegalAsecId(id)) {
826 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
827 errno = EINVAL;
828 return -1;
829 }
830
831 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
832 SLOGE("Couldn't find ASEC %s", id);
833 return -1;
834 }
835
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700836 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700837 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
838 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
839 return -1;
840 }
841
842 if (isMountpointMounted(mountPoint)) {
843 SLOGE("ASEC %s mounted. Unmount before resizing", id);
844 errno = EBUSY;
845 return -1;
846 }
847
848 struct asec_superblock sb;
849 int fd;
850 unsigned int oldNumSec = 0;
851
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700852 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700853 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
854 return -1;
855 }
856
857 struct stat info;
858 if (fstat(fd, &info) < 0) {
859 SLOGE("Failed to get file size (%s)", strerror(errno));
860 close(fd);
861 return -1;
862 }
863
864 oldNumSec = info.st_size / 512;
865
866 unsigned numImgSectors;
867 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
868 numImgSectors = adjustSectorNumExt4(numSectors);
869 else
870 numImgSectors = adjustSectorNumFAT(numSectors);
871 /*
872 * add one block for the superblock
873 */
874 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700875 if (oldNumSec == numImgSectors + 1) {
876 SLOGW("Size unchanged; ignoring resize request");
877 return 0;
878 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700879 SLOGE("Only growing is currently supported.");
880 close(fd);
881 return -1;
882 }
883
884 /*
885 * Try to read superblock.
886 */
887 memset(&sb, 0, sizeof(struct asec_superblock));
888 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
889 SLOGE("lseek failed (%s)", strerror(errno));
890 close(fd);
891 return -1;
892 }
893 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
894 SLOGE("superblock read failed (%s)", strerror(errno));
895 close(fd);
896 return -1;
897 }
898 close(fd);
899
900 if (mDebug) {
901 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
902 }
903 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
904 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
905 errno = EMEDIUMTYPE;
906 return -1;
907 }
908
909 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
910 SLOGE("Only ext4 partitions are supported for resize");
911 errno = EINVAL;
912 return -1;
913 }
914
915 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
916 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
917 return -1;
918 }
919
920 /*
921 * Drop down a copy of the superblock at the end of the file
922 */
923 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
924 goto fail;
925
926 char idHash[33];
927 if (!asecHash(id, idHash, sizeof(idHash))) {
928 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
929 goto fail;
930 }
931
932 char loopDevice[255];
933 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
934 goto fail;
935
936 char dmDevice[255];
937
938 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
939 Loop::destroyByDevice(loopDevice);
940 goto fail;
941 }
942
943 /*
944 * Wait for the device mapper node to be created.
945 */
946 waitForDevMapper(dmDevice);
947
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700948 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700949 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
950 if (cleanupDm) {
951 Devmapper::destroy(idHash);
952 }
953 Loop::destroyByDevice(loopDevice);
954 goto fail;
955 }
956
957 return 0;
958fail:
959 Loop::resizeImageFile(asecFileName, oldNumSec);
960 return -1;
961}
962
San Mehata19b2502010-01-06 10:33:53 -0800963int VolumeManager::finalizeAsec(const char *id) {
964 char asecFileName[255];
965 char loopDevice[255];
966 char mountPoint[255];
967
Nick Kralevich0de7c612014-01-27 14:58:06 -0800968 if (!isLegalAsecId(id)) {
969 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
970 errno = EINVAL;
971 return -1;
972 }
973
Kenny Root344ca102012-04-03 17:23:01 -0700974 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
975 SLOGE("Couldn't find ASEC %s", id);
976 return -1;
977 }
San Mehata19b2502010-01-06 10:33:53 -0800978
San Mehatd9a4e352010-03-12 13:32:47 -0800979 char idHash[33];
980 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700981 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800982 return -1;
983 }
984
985 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700986 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800987 return -1;
988 }
989
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900990 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -0700991 struct asec_superblock sb;
992
993 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
994 return -1;
995 }
996
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700997 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400998 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
999 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1000 return -1;
1001 }
Kenny Root344ca102012-04-03 17:23:01 -07001002
1003 int result = 0;
1004 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001005 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1006 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001007 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001008 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1009 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001010 }
1011
1012 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001013 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001014 return -1;
1015 }
1016
San Mehatd9a4e352010-03-12 13:32:47 -08001017 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001018 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001019 }
San Mehata19b2502010-01-06 10:33:53 -08001020 return 0;
1021}
1022
Kenny Root344ca102012-04-03 17:23:01 -07001023int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1024 char asecFileName[255];
1025 char loopDevice[255];
1026 char mountPoint[255];
1027
1028 if (gid < AID_APP) {
1029 SLOGE("Group ID is not in application range");
1030 return -1;
1031 }
1032
Nick Kralevich0de7c612014-01-27 14:58:06 -08001033 if (!isLegalAsecId(id)) {
1034 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1035 errno = EINVAL;
1036 return -1;
1037 }
1038
Kenny Root344ca102012-04-03 17:23:01 -07001039 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1040 SLOGE("Couldn't find ASEC %s", id);
1041 return -1;
1042 }
1043
1044 char idHash[33];
1045 if (!asecHash(id, idHash, sizeof(idHash))) {
1046 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1047 return -1;
1048 }
1049
1050 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1051 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1052 return -1;
1053 }
1054
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001055 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001056 struct asec_superblock sb;
1057
1058 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1059 return -1;
1060 }
1061
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001062 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001063 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1064 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1065 return -1;
1066 }
Kenny Root344ca102012-04-03 17:23:01 -07001067
1068 int result = 0;
1069 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1070 return 0;
1071 }
1072
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001073 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001074 false /* read-only */,
1075 true /* remount */,
1076 false /* executable */);
1077 if (ret) {
1078 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1079 return -1;
1080 }
1081
1082 char *paths[] = { mountPoint, NULL };
1083
1084 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1085 if (fts) {
1086 // Traverse the entire hierarchy and chown to system UID.
1087 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1088 // We don't care about the lost+found directory.
1089 if (!strcmp(ftsent->fts_name, "lost+found")) {
1090 continue;
1091 }
1092
1093 /*
1094 * There can only be one file marked as private right now.
1095 * This should be more robust, but it satisfies the requirements
1096 * we have for right now.
1097 */
1098 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1099
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001100 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001101 if (fd < 0) {
1102 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1103 result = -1;
1104 continue;
1105 }
1106
1107 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1108
1109 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001110 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001111 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001112 result |= fchmod(fd, privateFile ? 0640 : 0644);
1113 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001114
Stephen Smalley5093e612014-02-12 09:43:08 -05001115 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001116 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1117 result |= -1;
1118 }
1119
Kenny Root344ca102012-04-03 17:23:01 -07001120 close(fd);
1121 }
1122 fts_close(fts);
1123
1124 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001125 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001126 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1127 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1128 result |= -1;
1129 }
1130 close(dirfd);
1131 } else {
1132 result |= -1;
1133 }
1134
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001135 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001136 true /* read-only */,
1137 true /* remount */,
1138 true /* execute */);
1139
1140 if (result) {
1141 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1142 return -1;
1143 }
1144
1145 if (mDebug) {
1146 SLOGD("ASEC %s permissions fixed", id);
1147 }
1148 return 0;
1149}
1150
San Mehat048b0802010-01-23 08:17:06 -08001151int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001152 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001153 char *asecFilename2;
1154 char mountPoint[255];
1155
Kenny Root344ca102012-04-03 17:23:01 -07001156 const char *dir;
1157
Nick Kralevich0de7c612014-01-27 14:58:06 -08001158 if (!isLegalAsecId(id1)) {
1159 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1160 errno = EINVAL;
1161 return -1;
1162 }
1163
1164 if (!isLegalAsecId(id2)) {
1165 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1166 errno = EINVAL;
1167 return -1;
1168 }
1169
Kenny Root344ca102012-04-03 17:23:01 -07001170 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1171 SLOGE("Couldn't find ASEC %s", id1);
1172 return -1;
1173 }
1174
1175 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001176
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001177 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001178 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1179 SLOGE("Rename failed: couldn't construct mountpoint");
1180 goto out_err;
1181 }
1182
San Mehat048b0802010-01-23 08:17:06 -08001183 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001184 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001185 errno = EBUSY;
1186 goto out_err;
1187 }
1188
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001189 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001190 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1191 SLOGE("Rename failed: couldn't construct mountpoint2");
1192 goto out_err;
1193 }
1194
San Mehat96956ed2010-02-24 08:42:51 -08001195 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001196 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001197 errno = EBUSY;
1198 goto out_err;
1199 }
1200
San Mehat048b0802010-01-23 08:17:06 -08001201 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001202 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001203 errno = EADDRINUSE;
1204 goto out_err;
1205 }
1206
1207 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001208 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001209 goto out_err;
1210 }
1211
San Mehat048b0802010-01-23 08:17:06 -08001212 free(asecFilename2);
1213 return 0;
1214
1215out_err:
San Mehat048b0802010-01-23 08:17:06 -08001216 free(asecFilename2);
1217 return -1;
1218}
1219
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001220#define UNMOUNT_RETRIES 5
1221#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001222int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001223 char asecFileName[255];
1224 char mountPoint[255];
1225
Nick Kralevich0de7c612014-01-27 14:58:06 -08001226 if (!isLegalAsecId(id)) {
1227 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1228 errno = EINVAL;
1229 return -1;
1230 }
1231
Kenny Root344ca102012-04-03 17:23:01 -07001232 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1233 SLOGE("Couldn't find ASEC %s", id);
1234 return -1;
1235 }
1236
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001237 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001238 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1239 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1240 return -1;
1241 }
San Mehata19b2502010-01-06 10:33:53 -08001242
San Mehatd9a4e352010-03-12 13:32:47 -08001243 char idHash[33];
1244 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001245 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001246 return -1;
1247 }
1248
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001249 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1250}
1251
Kenny Root508c0e12010-07-12 09:59:49 -07001252int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001253 char mountPoint[255];
1254
1255 char idHash[33];
1256 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1257 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1258 return -1;
1259 }
1260
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001261 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001262 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1263 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1264 return -1;
1265 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001266
1267 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1268}
1269
1270int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1271 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001272 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001273 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001274 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001275 return -1;
1276 }
San Mehat23969932010-01-09 07:08:06 -08001277
San Mehatb78a32c2010-01-10 13:02:12 -08001278 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001279 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001280 rc = umount(mountPoint);
1281 if (!rc) {
1282 break;
San Mehata19b2502010-01-06 10:33:53 -08001283 }
San Mehatb78a32c2010-01-10 13:02:12 -08001284 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001285 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001286 rc = 0;
1287 break;
San Mehata19b2502010-01-06 10:33:53 -08001288 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001289 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001290 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001291
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001292 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001293
1294 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001295 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001296 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001297 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001298 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001299 }
San Mehat8c940ef2010-02-13 14:19:53 -08001300
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001301 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001302 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001303 }
1304
1305 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001306 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001307 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001308 return -1;
1309 }
1310
San Mehat12f4b892010-02-24 11:43:22 -08001311 int retries = 10;
1312
1313 while(retries--) {
1314 if (!rmdir(mountPoint)) {
1315 break;
1316 }
1317
San Mehat97ac40e2010-03-24 10:24:19 -07001318 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001319 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001320 }
1321
1322 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001323 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001324 }
San Mehat88705162010-01-15 09:26:28 -08001325
Paul Lawrence60dec162014-09-02 10:52:15 -07001326 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1327 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1328 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1329 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1330 continue;
1331 } else {
1332 break;
1333 }
San Mehata19b2502010-01-06 10:33:53 -08001334 }
1335
1336 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001337 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001338 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001339 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001340 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001341 }
San Mehat88705162010-01-15 09:26:28 -08001342
1343 AsecIdCollection::iterator it;
1344 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001345 ContainerData* cd = *it;
1346 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001347 free(*it);
1348 mActiveContainers->erase(it);
1349 break;
1350 }
1351 }
1352 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001353 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001354 }
San Mehatb78a32c2010-01-10 13:02:12 -08001355 return 0;
1356}
1357
San Mehat4ba89482010-02-18 09:00:18 -08001358int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001359 char asecFileName[255];
1360 char mountPoint[255];
1361
Nick Kralevich0de7c612014-01-27 14:58:06 -08001362 if (!isLegalAsecId(id)) {
1363 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1364 errno = EINVAL;
1365 return -1;
1366 }
1367
Kenny Root344ca102012-04-03 17:23:01 -07001368 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1369 SLOGE("Couldn't find ASEC %s", id);
1370 return -1;
1371 }
1372
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001373 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001374 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1375 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1376 return -1;
1377 }
San Mehatb78a32c2010-01-10 13:02:12 -08001378
San Mehat0586d542010-01-12 15:38:59 -08001379 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001380 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001381 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001382 }
San Mehat4ba89482010-02-18 09:00:18 -08001383 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001384 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001385 return -1;
1386 }
1387 }
San Mehata19b2502010-01-06 10:33:53 -08001388
San Mehat0586d542010-01-12 15:38:59 -08001389 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001390 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001391 return -1;
1392 }
San Mehata19b2502010-01-06 10:33:53 -08001393
San Mehatd9a4e352010-03-12 13:32:47 -08001394 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001395 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001396 }
San Mehata19b2502010-01-06 10:33:53 -08001397 return 0;
1398}
1399
Nick Kralevich0de7c612014-01-27 14:58:06 -08001400/*
1401 * Legal ASEC ids consist of alphanumeric characters, '-',
1402 * '_', or '.'. ".." is not allowed. The first or last character
1403 * of the ASEC id cannot be '.' (dot).
1404 */
1405bool VolumeManager::isLegalAsecId(const char *id) const {
1406 size_t i;
1407 size_t len = strlen(id);
1408
1409 if (len == 0) {
1410 return false;
1411 }
1412 if ((id[0] == '.') || (id[len - 1] == '.')) {
1413 return false;
1414 }
1415
1416 for (i = 0; i < len; i++) {
1417 if (id[i] == '.') {
1418 // i=0 is guaranteed never to have a dot. See above.
1419 if (id[i-1] == '.') return false;
1420 continue;
1421 }
1422 if (id[i] == '_' || id[i] == '-') continue;
1423 if (id[i] >= 'a' && id[i] <= 'z') continue;
1424 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1425 if (id[i] >= '0' && id[i] <= '9') continue;
1426 return false;
1427 }
1428
1429 return true;
1430}
1431
Kenny Root344ca102012-04-03 17:23:01 -07001432bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001433 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001434 if (dirfd < 0) {
1435 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001436 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001437 }
1438
Nick Kralevich25e581a2015-02-06 08:55:08 -08001439 struct stat sb;
1440 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1441 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001442
1443 close(dirfd);
1444
1445 return ret;
1446}
1447
1448int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1449 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001450 char *asecName;
1451
Nick Kralevich0de7c612014-01-27 14:58:06 -08001452 if (!isLegalAsecId(id)) {
1453 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1454 errno = EINVAL;
1455 return -1;
1456 }
1457
Kenny Root344ca102012-04-03 17:23:01 -07001458 if (asprintf(&asecName, "%s.asec", id) < 0) {
1459 SLOGE("Couldn't allocate string to write ASEC name");
1460 return -1;
1461 }
1462
1463 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001464 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1465 dir = VolumeManager::SEC_ASECDIR_INT;
1466 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1467 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001468 } else {
1469 free(asecName);
1470 return -1;
1471 }
1472
1473 if (directory != NULL) {
1474 *directory = dir;
1475 }
1476
1477 if (asecPath != NULL) {
1478 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001479 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1480 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001481 free(asecName);
1482 return -1;
1483 }
1484 }
1485
1486 free(asecName);
1487 return 0;
1488}
1489
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001490int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001491 char asecFileName[255];
1492 char mountPoint[255];
1493
Nick Kralevich0de7c612014-01-27 14:58:06 -08001494 if (!isLegalAsecId(id)) {
1495 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1496 errno = EINVAL;
1497 return -1;
1498 }
1499
Kenny Root344ca102012-04-03 17:23:01 -07001500 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1501 SLOGE("Couldn't find ASEC %s", id);
1502 return -1;
1503 }
1504
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001505 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001506 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001507 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001508 return -1;
1509 }
San Mehata19b2502010-01-06 10:33:53 -08001510
1511 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001512 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001513 errno = EBUSY;
1514 return -1;
1515 }
1516
San Mehatd9a4e352010-03-12 13:32:47 -08001517 char idHash[33];
1518 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001519 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001520 return -1;
1521 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001522
San Mehata19b2502010-01-06 10:33:53 -08001523 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001524 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1525 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001526
1527 char dmDevice[255];
1528 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001529
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001530 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001531 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001532
Kenny Root344ca102012-04-03 17:23:01 -07001533 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1534 return -1;
1535 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001536
San Mehatd9a4e352010-03-12 13:32:47 -08001537 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001538 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001539 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001540 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001541 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001542 Loop::destroyByDevice(loopDevice);
1543 errno = EMEDIUMTYPE;
1544 return -1;
1545 }
1546 nr_sec--; // We don't want the devmapping to extend onto our superblock
1547
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001548 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1549 Loop::destroyByDevice(loopDevice);
1550 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001551 }
1552
Kenny Root344ca102012-04-03 17:23:01 -07001553 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001554 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001555 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001556 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001557 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001558 }
1559 Loop::destroyByDevice(loopDevice);
1560 return -1;
1561 }
San Mehata19b2502010-01-06 10:33:53 -08001562 }
1563
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001564 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001565 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001566 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001567 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001568
Kenny Root344ca102012-04-03 17:23:01 -07001569 int result;
1570 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001571 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1572 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001573 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001574 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1575 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001576 }
1577
1578 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001579 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001580 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001581 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001582 }
1583 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001584 return -1;
1585 }
1586
Kenny Rootcbacf782010-09-24 15:11:48 -07001587 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001588 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001589 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001590 }
San Mehata19b2502010-01-06 10:33:53 -08001591 return 0;
1592}
1593
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001594/**
1595 * Mounts an image file <code>img</code>.
1596 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001597int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001598 char mountPoint[255];
1599
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001600 char idHash[33];
1601 if (!asecHash(img, idHash, sizeof(idHash))) {
1602 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1603 return -1;
1604 }
1605
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001606 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001607 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001608 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001609 return -1;
1610 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001611
1612 if (isMountpointMounted(mountPoint)) {
1613 SLOGE("Image %s already mounted", img);
1614 errno = EBUSY;
1615 return -1;
1616 }
1617
1618 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001619 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1620 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001621
1622 char dmDevice[255];
1623 bool cleanupDm = false;
1624 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001625 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001626
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001627 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001628 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1629 Loop::destroyByDevice(loopDevice);
1630 return -1;
1631 }
1632
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001633 get_blkdev_size(fd, &nr_sec);
1634 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001635 SLOGE("Failed to get loop size (%s)", strerror(errno));
1636 Loop::destroyByDevice(loopDevice);
1637 close(fd);
1638 return -1;
1639 }
1640
1641 close(fd);
1642
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001643 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001644 Loop::destroyByDevice(loopDevice);
1645 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001646 }
1647
1648 if (mkdir(mountPoint, 0755)) {
1649 if (errno != EEXIST) {
1650 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1651 if (cleanupDm) {
1652 Devmapper::destroy(idHash);
1653 }
1654 Loop::destroyByDevice(loopDevice);
1655 return -1;
1656 }
1657 }
1658
yoshiyuki hama476a6272015-01-28 16:37:23 +09001659 /*
1660 * Wait for the device mapper node to be created.
1661 */
1662 waitForDevMapper(dmDevice);
1663
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001664 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1665 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001666 SLOGE("Image mount failed (%s)", strerror(errno));
1667 if (cleanupDm) {
1668 Devmapper::destroy(idHash);
1669 }
1670 Loop::destroyByDevice(loopDevice);
1671 return -1;
1672 }
1673
Kenny Rootcbacf782010-09-24 15:11:48 -07001674 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001675 if (mDebug) {
1676 SLOGD("Image %s mounted", img);
1677 }
1678 return 0;
1679}
1680
Kenny Root508c0e12010-07-12 09:59:49 -07001681int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001682 FILE *fp = setmntent("/proc/mounts", "r");
1683 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001684 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1685 return -1;
1686 }
1687
1688 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001689 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001690 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001691 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001692 loopDir[loopDirLen++] = '/';
1693 loopDir[loopDirLen] = '\0';
1694
Yabin Cuid1104f72015-01-02 13:28:28 -08001695 mntent* mentry;
1696 while ((mentry = getmntent(fp)) != NULL) {
1697 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001698 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001699 if (fd >= 0) {
1700 struct loop_info64 li;
1701 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1702 cli->sendMsg(ResponseCode::AsecListResult,
1703 (const char*) li.lo_file_name, false);
1704 }
1705 close(fd);
1706 }
1707 }
1708 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001709 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001710 return 0;
1711}
1712
Jeff Sharkey9c484982015-03-31 10:35:33 -07001713extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001714 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001715 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001716}
1717
San Mehata19b2502010-01-06 10:33:53 -08001718bool VolumeManager::isMountpointMounted(const char *mp)
1719{
Yabin Cuid1104f72015-01-02 13:28:28 -08001720 FILE *fp = setmntent("/proc/mounts", "r");
1721 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001722 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001723 return false;
1724 }
1725
Yabin Cuid1104f72015-01-02 13:28:28 -08001726 bool found_mp = false;
1727 mntent* mentry;
1728 while ((mentry = getmntent(fp)) != NULL) {
1729 if (strcmp(mentry->mnt_dir, mp) == 0) {
1730 found_mp = true;
1731 break;
San Mehata19b2502010-01-06 10:33:53 -08001732 }
San Mehata19b2502010-01-06 10:33:53 -08001733 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001734 endmntent(fp);
1735 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001736}
1737
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001738int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001739 // Only offer to create directories for paths managed by vold
1740 if (strncmp(path, "/storage/", 9) == 0) {
1741 // fs_mkdirs() does symlink checking and relative path enforcement
1742 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001743 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001744 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001745 return -EINVAL;
1746 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001747}