blob: 8f73e63b00b36e86a6805b2d66546f8645835586 [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 Sharkey36801cc2015-03-13 16:09:20 -070048#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070049#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070050#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080051#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080052#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070053#include "Ext4.h"
San Mehata19b2502010-01-06 10:33:53 -080054#include "Fat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070055#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080056#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080057#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080058#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090059#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070060#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080061
Mike Lockwood97f2fc12011-06-07 10:51:38 -070062#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
63
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070064#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
65 + (number & (~((1U << po2) - 1))))
66
Jeff Sharkey36801cc2015-03-13 16:09:20 -070067using android::base::StringPrintf;
68
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070069/*
70 * Path to external storage where *only* root can access ASEC image files
71 */
72const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
73
74/*
75 * Path to internal storage where *only* root can access ASEC image files
76 */
77const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
78
79/*
80 * Path to where secure containers are mounted
81 */
82const char *VolumeManager::ASECDIR = "/mnt/asec";
83
84/*
85 * Path to where OBBs are mounted
86 */
87const char *VolumeManager::LOOPDIR = "/mnt/obb";
88
Jeff Sharkey36801cc2015-03-13 16:09:20 -070089static const char* kUserMountPath = "/mnt/user";
90
Jeff Sharkey36801cc2015-03-13 16:09:20 -070091static const unsigned int kMajorBlockMmc = 179;
92
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070093/* writes superblock at end of file or device given by name */
94static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070095 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070096 if (sbfd < 0) {
97 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
98 return -1;
99 }
100
101 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
102 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
103 close(sbfd);
104 return -1;
105 }
106
107 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
108 SLOGE("Failed to write superblock (%s)", strerror(errno));
109 close(sbfd);
110 return -1;
111 }
112 close(sbfd);
113 return 0;
114}
115
116static int adjustSectorNumExt4(unsigned numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700117 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
118 // preventing costly operations or unexpected ENOSPC error.
119 // Ext4::format() uses default block size without clustering.
120 unsigned clusterSectors = 4096 / 512;
121 unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
122 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700123 return ROUND_UP_POWER_OF_2(numSectors, 3);
124}
125
126static int adjustSectorNumFAT(unsigned numSectors) {
127 /*
128 * Add some headroom
129 */
130 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
131 numSectors += fatSize + 2;
132 /*
133 * FAT is aligned to 32 kb with 512b sectors.
134 */
135 return ROUND_UP_POWER_OF_2(numSectors, 6);
136}
137
138static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
139 if (Loop::lookupActive(idHash, buffer, len)) {
140 if (Loop::create(idHash, asecFileName, buffer, len)) {
141 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
142 return -1;
143 }
144 if (debug) {
145 SLOGD("New loop device created at %s", buffer);
146 }
147 } else {
148 if (debug) {
149 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
150 }
151 }
152 return 0;
153}
154
155static 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) {
156 if (strcmp(key, "none")) {
157 if (Devmapper::lookupActive(idHash, buffer, len)) {
158 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
159 buffer, len)) {
160 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
161 return -1;
162 }
163 if (debug) {
164 SLOGD("New devmapper instance created at %s", buffer);
165 }
166 } else {
167 if (debug) {
168 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
169 }
170 }
171 *createdDMDevice = true;
172 } else {
173 strcpy(buffer, loopDevice);
174 *createdDMDevice = false;
175 }
176 return 0;
177}
178
179static void waitForDevMapper(const char *dmDevice) {
180 /*
181 * Wait for the device mapper node to be created. Sometimes it takes a
182 * while. Wait for up to 1 second. We could also inspect incoming uevents,
183 * but that would take more effort.
184 */
185 int tries = 25;
186 while (tries--) {
187 if (!access(dmDevice, F_OK) || errno != ENOENT) {
188 break;
189 }
190 usleep(40 * 1000);
191 }
192}
193
San Mehatf1b736b2009-10-10 17:22:08 -0700194VolumeManager *VolumeManager::sInstance = NULL;
195
196VolumeManager *VolumeManager::Instance() {
197 if (!sInstance)
198 sInstance = new VolumeManager();
199 return sInstance;
200}
201
202VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800203 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800204 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700205 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400206 mUmsSharingCount = 0;
207 mSavedDirtyRatio = -1;
208 // set dirty ratio to 0 when UMS is active
209 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700210}
211
212VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800213 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700214}
215
Kenny Root7b18a7b2010-03-15 13:13:41 -0700216char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700217 static const char* digits = "0123456789abcdef";
218
Kenny Root7b18a7b2010-03-15 13:13:41 -0700219 unsigned char sig[MD5_DIGEST_LENGTH];
220
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700221 if (buffer == NULL) {
222 SLOGE("Destination buffer is NULL");
223 errno = ESPIPE;
224 return NULL;
225 } else if (id == NULL) {
226 SLOGE("Source buffer is NULL");
227 errno = ESPIPE;
228 return NULL;
229 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800230 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700231 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800232 errno = ESPIPE;
233 return NULL;
234 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700235
236 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800237
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700238 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700239 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700240 *p++ = digits[sig[i] >> 4];
241 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800242 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700243 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800244
245 return buffer;
246}
247
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700248int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800249 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700250 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800251}
252
San Mehatf1b736b2009-10-10 17:22:08 -0700253int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700254 // Always start from a clean slate by unmounting everything in
255 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700256 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700257
258 // Assume that we always have an emulated volume on internal
259 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700260 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700261 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700262 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700263 mInternalEmulated->create();
264
San Mehatf1b736b2009-10-10 17:22:08 -0700265 return 0;
266}
267
268int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700269 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700270 mInternalEmulated->destroy();
271 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700272 return 0;
273}
274
San Mehatfd7f5872009-10-12 11:32:47 -0700275void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700276 std::lock_guard<std::mutex> lock(mLock);
277
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700278 if (mDebug) {
279 LOG(VERBOSE) << "----------------";
280 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
281 evt->dump();
282 }
San Mehatf1b736b2009-10-10 17:22:08 -0700283
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700284 std::string eventPath(evt->findParam("DEVPATH"));
285 std::string devType(evt->findParam("DEVTYPE"));
286
287 if (devType != "disk") return;
288
289 int major = atoi(evt->findParam("MAJOR"));
290 int minor = atoi(evt->findParam("MINOR"));
291 dev_t device = makedev(major, minor);
292
293 switch (evt->getAction()) {
294 case NetlinkEvent::Action::kAdd: {
295 for (auto source : mDiskSources) {
296 if (source->matches(eventPath)) {
297 // For now, assume that MMC devices are SD, and that
298 // everything else is USB
299 int flags = source->getFlags();
300 if (major == kMajorBlockMmc) {
301 flags |= android::vold::Disk::Flags::kSd;
302 } else {
303 flags |= android::vold::Disk::Flags::kUsb;
304 }
305
306 auto disk = new android::vold::Disk(eventPath, device,
307 source->getNickname(), flags);
308 disk->create();
309 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
310 break;
311 }
312 }
313 break;
314 }
315 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700316 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700317 for (auto disk : mDisks) {
318 if (disk->getDevice() == device) {
319 disk->readMetadata();
320 disk->readPartitions();
321 }
322 }
323 break;
324 }
325 case NetlinkEvent::Action::kRemove: {
326 auto i = mDisks.begin();
327 while (i != mDisks.end()) {
328 if ((*i)->getDevice() == device) {
329 (*i)->destroy();
330 i = mDisks.erase(i);
331 } else {
332 ++i;
333 }
334 }
335 break;
336 }
337 default: {
338 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
339 break;
340 }
341 }
342}
343
344void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
345 mDiskSources.push_back(diskSource);
346}
347
348std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
349 for (auto disk : mDisks) {
350 if (disk->getId() == id) {
351 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700352 }
353 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700354 return nullptr;
355}
San Mehatf1b736b2009-10-10 17:22:08 -0700356
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700357std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
358 if (mInternalEmulated->getId() == id) {
359 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700360 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700361 for (auto disk : mDisks) {
362 auto vol = disk->findVolume(id);
363 if (vol != nullptr) {
364 return vol;
365 }
366 }
367 return nullptr;
368}
369
370int VolumeManager::linkPrimary(userid_t userId) {
371 std::string source(mPrimary->getPath());
372 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
373 source = StringPrintf("%s/%d", source.c_str(), userId);
374 }
375
376 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
377 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
378 if (errno != ENOENT) {
379 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
380 }
381 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700382 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700383 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
384 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
385 strerror(errno));
386 return -errno;
387 }
388 return 0;
389}
390
391int VolumeManager::startUser(userid_t userId) {
392 // Note that sometimes the system will spin up processes from Zygote
393 // before actually starting the user, so we're okay if Zygote
394 // already created this directory.
395 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
396 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
397
398 mUsers.push_back(userId);
399 if (mPrimary) {
400 linkPrimary(userId);
401 }
402 return 0;
403}
404
405int VolumeManager::cleanupUser(userid_t userId) {
406 mUsers.remove(userId);
407 return 0;
408}
409
410int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
411 mPrimary = vol;
412 for (userid_t userId : mUsers) {
413 linkPrimary(userId);
414 }
415 return 0;
416}
417
418int VolumeManager::reset() {
419 // Tear down all existing disks/volumes and start from a blank slate so
420 // newly connected framework hears all events.
421 mInternalEmulated->destroy();
422 mInternalEmulated->create();
423 for (auto disk : mDisks) {
424 disk->destroy();
425 disk->create();
426 }
427 mUsers.clear();
428 return 0;
429}
430
431int VolumeManager::shutdown() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700432 mInternalEmulated->destroy();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700433 for (auto disk : mDisks) {
434 disk->destroy();
435 }
436 mDisks.clear();
437 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700438}
439
Jeff Sharkey9c484982015-03-31 10:35:33 -0700440int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700441 std::lock_guard<std::mutex> lock(mLock);
442
Jeff Sharkey9c484982015-03-31 10:35:33 -0700443 // First, try gracefully unmounting all known devices
444 if (mInternalEmulated != nullptr) {
445 mInternalEmulated->unmount();
446 }
447 for (auto disk : mDisks) {
448 disk->unmountAll();
449 }
450
451 // Worst case we might have some stale mounts lurking around, so
452 // force unmount those just to be safe.
453 FILE* fp = setmntent("/proc/mounts", "r");
454 if (fp == NULL) {
455 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
456 return -errno;
457 }
458
459 // Some volumes can be stacked on each other, so force unmount in
460 // reverse order to give us the best chance of success.
461 std::list<std::string> toUnmount;
462 mntent* mentry;
463 while ((mentry = getmntent(fp)) != NULL) {
464 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
465 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
466 toUnmount.push_front(std::string(mentry->mnt_dir));
467 }
468 }
469 endmntent(fp);
470
471 for (auto path : toUnmount) {
472 SLOGW("Tearing down stale mount %s", path.c_str());
473 android::vold::ForceUnmount(path);
474 }
475
476 return 0;
477}
478
Kenny Root508c0e12010-07-12 09:59:49 -0700479int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
480 char idHash[33];
481 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
482 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
483 return -1;
484 }
485
486 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700487 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400488 if ((written < 0) || (written >= mountPathLen)) {
489 errno = EINVAL;
490 return -1;
491 }
Kenny Root508c0e12010-07-12 09:59:49 -0700492
493 if (access(mountPath, F_OK)) {
494 errno = ENOENT;
495 return -1;
496 }
497
498 return 0;
499}
500
San Mehata19b2502010-01-06 10:33:53 -0800501int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700502 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700503
Nick Kralevich0de7c612014-01-27 14:58:06 -0800504 if (!isLegalAsecId(id)) {
505 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
506 errno = EINVAL;
507 return -1;
508 }
509
Kenny Root344ca102012-04-03 17:23:01 -0700510 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
511 SLOGE("Couldn't find ASEC %s", id);
512 return -1;
513 }
San Mehat88ac2c02010-03-23 11:15:58 -0700514
515 memset(buffer, 0, maxlen);
516 if (access(asecFileName, F_OK)) {
517 errno = ENOENT;
518 return -1;
519 }
San Mehata19b2502010-01-06 10:33:53 -0800520
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700521 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400522 if ((written < 0) || (written >= maxlen)) {
523 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
524 errno = EINVAL;
525 return -1;
526 }
527
San Mehata19b2502010-01-06 10:33:53 -0800528 return 0;
529}
530
Dianne Hackborn736910c2011-06-27 13:37:07 -0700531int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
532 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700533
Nick Kralevich0de7c612014-01-27 14:58:06 -0800534 if (!isLegalAsecId(id)) {
535 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
536 errno = EINVAL;
537 return -1;
538 }
539
Kenny Root344ca102012-04-03 17:23:01 -0700540 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
541 SLOGE("Couldn't find ASEC %s", id);
542 return -1;
543 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700544
545 memset(buffer, 0, maxlen);
546 if (access(asecFileName, F_OK)) {
547 errno = ENOENT;
548 return -1;
549 }
550
rpcraigd1c226f2012-10-09 06:58:16 -0400551 int written = snprintf(buffer, maxlen, "%s", asecFileName);
552 if ((written < 0) || (written >= maxlen)) {
553 errno = EINVAL;
554 return -1;
555 }
556
Dianne Hackborn736910c2011-06-27 13:37:07 -0700557 return 0;
558}
559
Kenny Root344ca102012-04-03 17:23:01 -0700560int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
561 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800562 struct asec_superblock sb;
563 memset(&sb, 0, sizeof(sb));
564
Nick Kralevich0de7c612014-01-27 14:58:06 -0800565 if (!isLegalAsecId(id)) {
566 SLOGE("createAsec: Invalid asec id \"%s\"", id);
567 errno = EINVAL;
568 return -1;
569 }
570
Kenny Root344ca102012-04-03 17:23:01 -0700571 const bool wantFilesystem = strcmp(fstype, "none");
572 bool usingExt4 = false;
573 if (wantFilesystem) {
574 usingExt4 = !strcmp(fstype, "ext4");
575 if (usingExt4) {
576 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
577 } else if (strcmp(fstype, "fat")) {
578 SLOGE("Invalid filesystem type %s", fstype);
579 errno = EINVAL;
580 return -1;
581 }
582 }
583
San Mehatfcf24fe2010-03-03 12:37:32 -0800584 sb.magic = ASEC_SB_MAGIC;
585 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800586
San Mehatd31e3802010-02-18 08:37:45 -0800587 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700588 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800589 errno = EINVAL;
590 return -1;
591 }
592
San Mehata19b2502010-01-06 10:33:53 -0800593 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700594
595 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
596 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
597 asecFileName, strerror(errno));
598 errno = EADDRINUSE;
599 return -1;
600 }
601
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700602 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700603
rpcraigd1c226f2012-10-09 06:58:16 -0400604 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
605 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
606 errno = EINVAL;
607 return -1;
608 }
San Mehata19b2502010-01-06 10:33:53 -0800609
610 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700611 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700612 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800613 errno = EADDRINUSE;
614 return -1;
615 }
616
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700617 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700618 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700619 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700620 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700621 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800622
623 // Add +1 for our superblock which is at the end
624 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700625 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800626 return -1;
627 }
628
San Mehatd9a4e352010-03-12 13:32:47 -0800629 char idHash[33];
630 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700631 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800632 unlink(asecFileName);
633 return -1;
634 }
635
San Mehata19b2502010-01-06 10:33:53 -0800636 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800637 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700638 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800639 unlink(asecFileName);
640 return -1;
641 }
642
San Mehatb78a32c2010-01-10 13:02:12 -0800643 char dmDevice[255];
644 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800645
San Mehatb78a32c2010-01-10 13:02:12 -0800646 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800647 // XXX: This is all we support for now
648 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800649 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800650 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700651 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800652 Loop::destroyByDevice(loopDevice);
653 unlink(asecFileName);
654 return -1;
655 }
656 cleanupDm = true;
657 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800658 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800659 strcpy(dmDevice, loopDevice);
660 }
661
San Mehatfcf24fe2010-03-03 12:37:32 -0800662 /*
663 * Drop down the superblock at the end of the file
664 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700665 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800666 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800667 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800668 }
669 Loop::destroyByDevice(loopDevice);
670 unlink(asecFileName);
671 return -1;
672 }
673
Kenny Root344ca102012-04-03 17:23:01 -0700674 if (wantFilesystem) {
675 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400676 char mountPoint[255];
677
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700678 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400679 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
680 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
681 if (cleanupDm) {
682 Devmapper::destroy(idHash);
683 }
684 Loop::destroyByDevice(loopDevice);
685 unlink(asecFileName);
686 return -1;
687 }
rpcraiga54e13a2012-09-21 14:17:08 -0400688
Kenny Root344ca102012-04-03 17:23:01 -0700689 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700690 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700691 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700692 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800693 }
San Mehata19b2502010-01-06 10:33:53 -0800694
Kenny Root344ca102012-04-03 17:23:01 -0700695 if (formatStatus < 0) {
696 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800697 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800698 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800699 }
San Mehateb13a902010-01-07 12:12:50 -0800700 Loop::destroyByDevice(loopDevice);
701 unlink(asecFileName);
702 return -1;
703 }
Kenny Root344ca102012-04-03 17:23:01 -0700704
Kenny Root344ca102012-04-03 17:23:01 -0700705 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800706 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700707 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800708 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800709 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800710 }
711 Loop::destroyByDevice(loopDevice);
712 unlink(asecFileName);
713 return -1;
714 }
San Mehatb78a32c2010-01-10 13:02:12 -0800715 }
San Mehata1091cb2010-02-28 20:17:20 -0800716
Kenny Root344ca102012-04-03 17:23:01 -0700717 int mountStatus;
718 if (usingExt4) {
719 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
720 } else {
721 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
722 false);
723 }
724
725 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700726 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800727 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800728 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800729 }
730 Loop::destroyByDevice(loopDevice);
731 unlink(asecFileName);
732 return -1;
733 }
Kenny Root344ca102012-04-03 17:23:01 -0700734
735 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700736 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -0700737 if (dirfd >= 0) {
738 if (fchown(dirfd, ownerUid, AID_SYSTEM)
739 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
740 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
741 }
742 close(dirfd);
743 }
744 }
San Mehata1091cb2010-02-28 20:17:20 -0800745 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700746 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800747 }
San Mehat88705162010-01-15 09:26:28 -0800748
Kenny Rootcbacf782010-09-24 15:11:48 -0700749 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800750 return 0;
751}
752
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700753int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
754 char asecFileName[255];
755 char mountPoint[255];
756 bool cleanupDm = false;
757
758 if (!isLegalAsecId(id)) {
759 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
760 errno = EINVAL;
761 return -1;
762 }
763
764 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
765 SLOGE("Couldn't find ASEC %s", id);
766 return -1;
767 }
768
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700769 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700770 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
771 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
772 return -1;
773 }
774
775 if (isMountpointMounted(mountPoint)) {
776 SLOGE("ASEC %s mounted. Unmount before resizing", id);
777 errno = EBUSY;
778 return -1;
779 }
780
781 struct asec_superblock sb;
782 int fd;
783 unsigned int oldNumSec = 0;
784
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700785 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700786 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
787 return -1;
788 }
789
790 struct stat info;
791 if (fstat(fd, &info) < 0) {
792 SLOGE("Failed to get file size (%s)", strerror(errno));
793 close(fd);
794 return -1;
795 }
796
797 oldNumSec = info.st_size / 512;
798
799 unsigned numImgSectors;
800 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
801 numImgSectors = adjustSectorNumExt4(numSectors);
802 else
803 numImgSectors = adjustSectorNumFAT(numSectors);
804 /*
805 * add one block for the superblock
806 */
807 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700808 if (oldNumSec == numImgSectors + 1) {
809 SLOGW("Size unchanged; ignoring resize request");
810 return 0;
811 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700812 SLOGE("Only growing is currently supported.");
813 close(fd);
814 return -1;
815 }
816
817 /*
818 * Try to read superblock.
819 */
820 memset(&sb, 0, sizeof(struct asec_superblock));
821 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
822 SLOGE("lseek failed (%s)", strerror(errno));
823 close(fd);
824 return -1;
825 }
826 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
827 SLOGE("superblock read failed (%s)", strerror(errno));
828 close(fd);
829 return -1;
830 }
831 close(fd);
832
833 if (mDebug) {
834 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
835 }
836 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
837 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
838 errno = EMEDIUMTYPE;
839 return -1;
840 }
841
842 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
843 SLOGE("Only ext4 partitions are supported for resize");
844 errno = EINVAL;
845 return -1;
846 }
847
848 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
849 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
850 return -1;
851 }
852
853 /*
854 * Drop down a copy of the superblock at the end of the file
855 */
856 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
857 goto fail;
858
859 char idHash[33];
860 if (!asecHash(id, idHash, sizeof(idHash))) {
861 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
862 goto fail;
863 }
864
865 char loopDevice[255];
866 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
867 goto fail;
868
869 char dmDevice[255];
870
871 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
872 Loop::destroyByDevice(loopDevice);
873 goto fail;
874 }
875
876 /*
877 * Wait for the device mapper node to be created.
878 */
879 waitForDevMapper(dmDevice);
880
881 if (Ext4::resize(dmDevice, numImgSectors)) {
882 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
883 if (cleanupDm) {
884 Devmapper::destroy(idHash);
885 }
886 Loop::destroyByDevice(loopDevice);
887 goto fail;
888 }
889
890 return 0;
891fail:
892 Loop::resizeImageFile(asecFileName, oldNumSec);
893 return -1;
894}
895
San Mehata19b2502010-01-06 10:33:53 -0800896int VolumeManager::finalizeAsec(const char *id) {
897 char asecFileName[255];
898 char loopDevice[255];
899 char mountPoint[255];
900
Nick Kralevich0de7c612014-01-27 14:58:06 -0800901 if (!isLegalAsecId(id)) {
902 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
903 errno = EINVAL;
904 return -1;
905 }
906
Kenny Root344ca102012-04-03 17:23:01 -0700907 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
908 SLOGE("Couldn't find ASEC %s", id);
909 return -1;
910 }
San Mehata19b2502010-01-06 10:33:53 -0800911
San Mehatd9a4e352010-03-12 13:32:47 -0800912 char idHash[33];
913 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700914 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800915 return -1;
916 }
917
918 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700919 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800920 return -1;
921 }
922
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900923 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -0700924 struct asec_superblock sb;
925
926 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
927 return -1;
928 }
929
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700930 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400931 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
932 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
933 return -1;
934 }
Kenny Root344ca102012-04-03 17:23:01 -0700935
936 int result = 0;
937 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
938 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
939 } else {
940 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
941 }
942
943 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700944 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800945 return -1;
946 }
947
San Mehatd9a4e352010-03-12 13:32:47 -0800948 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700949 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800950 }
San Mehata19b2502010-01-06 10:33:53 -0800951 return 0;
952}
953
Kenny Root344ca102012-04-03 17:23:01 -0700954int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
955 char asecFileName[255];
956 char loopDevice[255];
957 char mountPoint[255];
958
959 if (gid < AID_APP) {
960 SLOGE("Group ID is not in application range");
961 return -1;
962 }
963
Nick Kralevich0de7c612014-01-27 14:58:06 -0800964 if (!isLegalAsecId(id)) {
965 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
966 errno = EINVAL;
967 return -1;
968 }
969
Kenny Root344ca102012-04-03 17:23:01 -0700970 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
971 SLOGE("Couldn't find ASEC %s", id);
972 return -1;
973 }
974
975 char idHash[33];
976 if (!asecHash(id, idHash, sizeof(idHash))) {
977 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
978 return -1;
979 }
980
981 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
982 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
983 return -1;
984 }
985
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900986 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -0700987 struct asec_superblock sb;
988
989 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
990 return -1;
991 }
992
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700993 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400994 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
995 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
996 return -1;
997 }
Kenny Root344ca102012-04-03 17:23:01 -0700998
999 int result = 0;
1000 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1001 return 0;
1002 }
1003
1004 int ret = Ext4::doMount(loopDevice, mountPoint,
1005 false /* read-only */,
1006 true /* remount */,
1007 false /* executable */);
1008 if (ret) {
1009 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1010 return -1;
1011 }
1012
1013 char *paths[] = { mountPoint, NULL };
1014
1015 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1016 if (fts) {
1017 // Traverse the entire hierarchy and chown to system UID.
1018 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1019 // We don't care about the lost+found directory.
1020 if (!strcmp(ftsent->fts_name, "lost+found")) {
1021 continue;
1022 }
1023
1024 /*
1025 * There can only be one file marked as private right now.
1026 * This should be more robust, but it satisfies the requirements
1027 * we have for right now.
1028 */
1029 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1030
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001031 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001032 if (fd < 0) {
1033 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1034 result = -1;
1035 continue;
1036 }
1037
1038 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1039
1040 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001041 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001042 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001043 result |= fchmod(fd, privateFile ? 0640 : 0644);
1044 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001045
Stephen Smalley5093e612014-02-12 09:43:08 -05001046 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001047 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1048 result |= -1;
1049 }
1050
Kenny Root344ca102012-04-03 17:23:01 -07001051 close(fd);
1052 }
1053 fts_close(fts);
1054
1055 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001056 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001057 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1058 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1059 result |= -1;
1060 }
1061 close(dirfd);
1062 } else {
1063 result |= -1;
1064 }
1065
1066 result |= Ext4::doMount(loopDevice, mountPoint,
1067 true /* read-only */,
1068 true /* remount */,
1069 true /* execute */);
1070
1071 if (result) {
1072 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1073 return -1;
1074 }
1075
1076 if (mDebug) {
1077 SLOGD("ASEC %s permissions fixed", id);
1078 }
1079 return 0;
1080}
1081
San Mehat048b0802010-01-23 08:17:06 -08001082int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001083 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001084 char *asecFilename2;
1085 char mountPoint[255];
1086
Kenny Root344ca102012-04-03 17:23:01 -07001087 const char *dir;
1088
Nick Kralevich0de7c612014-01-27 14:58:06 -08001089 if (!isLegalAsecId(id1)) {
1090 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1091 errno = EINVAL;
1092 return -1;
1093 }
1094
1095 if (!isLegalAsecId(id2)) {
1096 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1097 errno = EINVAL;
1098 return -1;
1099 }
1100
Kenny Root344ca102012-04-03 17:23:01 -07001101 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1102 SLOGE("Couldn't find ASEC %s", id1);
1103 return -1;
1104 }
1105
1106 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001107
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001108 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001109 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1110 SLOGE("Rename failed: couldn't construct mountpoint");
1111 goto out_err;
1112 }
1113
San Mehat048b0802010-01-23 08:17:06 -08001114 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001115 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001116 errno = EBUSY;
1117 goto out_err;
1118 }
1119
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001120 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001121 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1122 SLOGE("Rename failed: couldn't construct mountpoint2");
1123 goto out_err;
1124 }
1125
San Mehat96956ed2010-02-24 08:42:51 -08001126 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001127 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001128 errno = EBUSY;
1129 goto out_err;
1130 }
1131
San Mehat048b0802010-01-23 08:17:06 -08001132 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001133 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001134 errno = EADDRINUSE;
1135 goto out_err;
1136 }
1137
1138 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001139 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001140 goto out_err;
1141 }
1142
San Mehat048b0802010-01-23 08:17:06 -08001143 free(asecFilename2);
1144 return 0;
1145
1146out_err:
San Mehat048b0802010-01-23 08:17:06 -08001147 free(asecFilename2);
1148 return -1;
1149}
1150
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001151#define UNMOUNT_RETRIES 5
1152#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001153int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001154 char asecFileName[255];
1155 char mountPoint[255];
1156
Nick Kralevich0de7c612014-01-27 14:58:06 -08001157 if (!isLegalAsecId(id)) {
1158 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1159 errno = EINVAL;
1160 return -1;
1161 }
1162
Kenny Root344ca102012-04-03 17:23:01 -07001163 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1164 SLOGE("Couldn't find ASEC %s", id);
1165 return -1;
1166 }
1167
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001168 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001169 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1170 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1171 return -1;
1172 }
San Mehata19b2502010-01-06 10:33:53 -08001173
San Mehatd9a4e352010-03-12 13:32:47 -08001174 char idHash[33];
1175 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001176 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001177 return -1;
1178 }
1179
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001180 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1181}
1182
Kenny Root508c0e12010-07-12 09:59:49 -07001183int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001184 char mountPoint[255];
1185
1186 char idHash[33];
1187 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1188 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1189 return -1;
1190 }
1191
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001192 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001193 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1194 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1195 return -1;
1196 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001197
1198 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1199}
1200
1201int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1202 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001203 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001204 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001205 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001206 return -1;
1207 }
San Mehat23969932010-01-09 07:08:06 -08001208
San Mehatb78a32c2010-01-10 13:02:12 -08001209 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001210 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001211 rc = umount(mountPoint);
1212 if (!rc) {
1213 break;
San Mehata19b2502010-01-06 10:33:53 -08001214 }
San Mehatb78a32c2010-01-10 13:02:12 -08001215 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001216 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001217 rc = 0;
1218 break;
San Mehata19b2502010-01-06 10:33:53 -08001219 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001220 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001221 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001222
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001223 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001224
1225 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001226 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001227 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001228 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001229 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001230 }
San Mehat8c940ef2010-02-13 14:19:53 -08001231
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001232 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001233 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001234 }
1235
1236 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001237 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001238 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001239 return -1;
1240 }
1241
San Mehat12f4b892010-02-24 11:43:22 -08001242 int retries = 10;
1243
1244 while(retries--) {
1245 if (!rmdir(mountPoint)) {
1246 break;
1247 }
1248
San Mehat97ac40e2010-03-24 10:24:19 -07001249 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001250 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001251 }
1252
1253 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001254 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001255 }
San Mehat88705162010-01-15 09:26:28 -08001256
Paul Lawrence60dec162014-09-02 10:52:15 -07001257 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1258 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1259 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1260 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1261 continue;
1262 } else {
1263 break;
1264 }
San Mehata19b2502010-01-06 10:33:53 -08001265 }
1266
1267 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001268 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001269 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001270 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001271 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001272 }
San Mehat88705162010-01-15 09:26:28 -08001273
1274 AsecIdCollection::iterator it;
1275 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001276 ContainerData* cd = *it;
1277 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001278 free(*it);
1279 mActiveContainers->erase(it);
1280 break;
1281 }
1282 }
1283 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001284 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001285 }
San Mehatb78a32c2010-01-10 13:02:12 -08001286 return 0;
1287}
1288
San Mehat4ba89482010-02-18 09:00:18 -08001289int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001290 char asecFileName[255];
1291 char mountPoint[255];
1292
Nick Kralevich0de7c612014-01-27 14:58:06 -08001293 if (!isLegalAsecId(id)) {
1294 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1295 errno = EINVAL;
1296 return -1;
1297 }
1298
Kenny Root344ca102012-04-03 17:23:01 -07001299 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1300 SLOGE("Couldn't find ASEC %s", id);
1301 return -1;
1302 }
1303
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001304 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001305 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1306 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1307 return -1;
1308 }
San Mehatb78a32c2010-01-10 13:02:12 -08001309
San Mehat0586d542010-01-12 15:38:59 -08001310 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001311 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001312 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001313 }
San Mehat4ba89482010-02-18 09:00:18 -08001314 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001315 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001316 return -1;
1317 }
1318 }
San Mehata19b2502010-01-06 10:33:53 -08001319
San Mehat0586d542010-01-12 15:38:59 -08001320 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001321 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001322 return -1;
1323 }
San Mehata19b2502010-01-06 10:33:53 -08001324
San Mehatd9a4e352010-03-12 13:32:47 -08001325 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001326 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001327 }
San Mehata19b2502010-01-06 10:33:53 -08001328 return 0;
1329}
1330
Nick Kralevich0de7c612014-01-27 14:58:06 -08001331/*
1332 * Legal ASEC ids consist of alphanumeric characters, '-',
1333 * '_', or '.'. ".." is not allowed. The first or last character
1334 * of the ASEC id cannot be '.' (dot).
1335 */
1336bool VolumeManager::isLegalAsecId(const char *id) const {
1337 size_t i;
1338 size_t len = strlen(id);
1339
1340 if (len == 0) {
1341 return false;
1342 }
1343 if ((id[0] == '.') || (id[len - 1] == '.')) {
1344 return false;
1345 }
1346
1347 for (i = 0; i < len; i++) {
1348 if (id[i] == '.') {
1349 // i=0 is guaranteed never to have a dot. See above.
1350 if (id[i-1] == '.') return false;
1351 continue;
1352 }
1353 if (id[i] == '_' || id[i] == '-') continue;
1354 if (id[i] >= 'a' && id[i] <= 'z') continue;
1355 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1356 if (id[i] >= '0' && id[i] <= '9') continue;
1357 return false;
1358 }
1359
1360 return true;
1361}
1362
Kenny Root344ca102012-04-03 17:23:01 -07001363bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001364 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001365 if (dirfd < 0) {
1366 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001367 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001368 }
1369
Nick Kralevich25e581a2015-02-06 08:55:08 -08001370 struct stat sb;
1371 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1372 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001373
1374 close(dirfd);
1375
1376 return ret;
1377}
1378
1379int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1380 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001381 char *asecName;
1382
Nick Kralevich0de7c612014-01-27 14:58:06 -08001383 if (!isLegalAsecId(id)) {
1384 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1385 errno = EINVAL;
1386 return -1;
1387 }
1388
Kenny Root344ca102012-04-03 17:23:01 -07001389 if (asprintf(&asecName, "%s.asec", id) < 0) {
1390 SLOGE("Couldn't allocate string to write ASEC name");
1391 return -1;
1392 }
1393
1394 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001395 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1396 dir = VolumeManager::SEC_ASECDIR_INT;
1397 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1398 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001399 } else {
1400 free(asecName);
1401 return -1;
1402 }
1403
1404 if (directory != NULL) {
1405 *directory = dir;
1406 }
1407
1408 if (asecPath != NULL) {
1409 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001410 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1411 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001412 free(asecName);
1413 return -1;
1414 }
1415 }
1416
1417 free(asecName);
1418 return 0;
1419}
1420
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001421int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001422 char asecFileName[255];
1423 char mountPoint[255];
1424
Nick Kralevich0de7c612014-01-27 14:58:06 -08001425 if (!isLegalAsecId(id)) {
1426 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1427 errno = EINVAL;
1428 return -1;
1429 }
1430
Kenny Root344ca102012-04-03 17:23:01 -07001431 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1432 SLOGE("Couldn't find ASEC %s", id);
1433 return -1;
1434 }
1435
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001436 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001437 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001438 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001439 return -1;
1440 }
San Mehata19b2502010-01-06 10:33:53 -08001441
1442 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001443 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001444 errno = EBUSY;
1445 return -1;
1446 }
1447
San Mehatd9a4e352010-03-12 13:32:47 -08001448 char idHash[33];
1449 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001450 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001451 return -1;
1452 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001453
San Mehata19b2502010-01-06 10:33:53 -08001454 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001455 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1456 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001457
1458 char dmDevice[255];
1459 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001460
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001461 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001462 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001463
Kenny Root344ca102012-04-03 17:23:01 -07001464 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1465 return -1;
1466 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001467
San Mehatd9a4e352010-03-12 13:32:47 -08001468 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001469 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001470 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001471 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001472 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001473 Loop::destroyByDevice(loopDevice);
1474 errno = EMEDIUMTYPE;
1475 return -1;
1476 }
1477 nr_sec--; // We don't want the devmapping to extend onto our superblock
1478
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001479 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1480 Loop::destroyByDevice(loopDevice);
1481 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001482 }
1483
Kenny Root344ca102012-04-03 17:23:01 -07001484 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001485 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001486 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001487 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001488 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001489 }
1490 Loop::destroyByDevice(loopDevice);
1491 return -1;
1492 }
San Mehata19b2502010-01-06 10:33:53 -08001493 }
1494
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001495 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001496 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001497 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001498 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001499
Kenny Root344ca102012-04-03 17:23:01 -07001500 int result;
1501 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001502 result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001503 } else {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001504 result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001505 }
1506
1507 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001508 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001509 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001510 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001511 }
1512 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001513 return -1;
1514 }
1515
Kenny Rootcbacf782010-09-24 15:11:48 -07001516 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001517 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001518 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001519 }
San Mehata19b2502010-01-06 10:33:53 -08001520 return 0;
1521}
1522
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001523/**
1524 * Mounts an image file <code>img</code>.
1525 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001526int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001527 char mountPoint[255];
1528
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001529 char idHash[33];
1530 if (!asecHash(img, idHash, sizeof(idHash))) {
1531 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1532 return -1;
1533 }
1534
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001535 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001536 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001537 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001538 return -1;
1539 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001540
1541 if (isMountpointMounted(mountPoint)) {
1542 SLOGE("Image %s already mounted", img);
1543 errno = EBUSY;
1544 return -1;
1545 }
1546
1547 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001548 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1549 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001550
1551 char dmDevice[255];
1552 bool cleanupDm = false;
1553 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001554 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001555
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001556 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001557 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1558 Loop::destroyByDevice(loopDevice);
1559 return -1;
1560 }
1561
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001562 get_blkdev_size(fd, &nr_sec);
1563 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001564 SLOGE("Failed to get loop size (%s)", strerror(errno));
1565 Loop::destroyByDevice(loopDevice);
1566 close(fd);
1567 return -1;
1568 }
1569
1570 close(fd);
1571
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001572 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001573 Loop::destroyByDevice(loopDevice);
1574 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001575 }
1576
1577 if (mkdir(mountPoint, 0755)) {
1578 if (errno != EEXIST) {
1579 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1580 if (cleanupDm) {
1581 Devmapper::destroy(idHash);
1582 }
1583 Loop::destroyByDevice(loopDevice);
1584 return -1;
1585 }
1586 }
1587
yoshiyuki hama476a6272015-01-28 16:37:23 +09001588 /*
1589 * Wait for the device mapper node to be created.
1590 */
1591 waitForDevMapper(dmDevice);
1592
Jeff Sharkey69479042012-09-25 16:14:57 -07001593 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001594 0227, false)) {
1595 SLOGE("Image mount failed (%s)", strerror(errno));
1596 if (cleanupDm) {
1597 Devmapper::destroy(idHash);
1598 }
1599 Loop::destroyByDevice(loopDevice);
1600 return -1;
1601 }
1602
Kenny Rootcbacf782010-09-24 15:11:48 -07001603 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001604 if (mDebug) {
1605 SLOGD("Image %s mounted", img);
1606 }
1607 return 0;
1608}
1609
Kenny Root508c0e12010-07-12 09:59:49 -07001610int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001611 FILE *fp = setmntent("/proc/mounts", "r");
1612 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001613 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1614 return -1;
1615 }
1616
1617 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001618 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001619 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001620 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001621 loopDir[loopDirLen++] = '/';
1622 loopDir[loopDirLen] = '\0';
1623
Yabin Cuid1104f72015-01-02 13:28:28 -08001624 mntent* mentry;
1625 while ((mentry = getmntent(fp)) != NULL) {
1626 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001627 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001628 if (fd >= 0) {
1629 struct loop_info64 li;
1630 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1631 cli->sendMsg(ResponseCode::AsecListResult,
1632 (const char*) li.lo_file_name, false);
1633 }
1634 close(fd);
1635 }
1636 }
1637 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001638 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001639 return 0;
1640}
1641
Jeff Sharkey9c484982015-03-31 10:35:33 -07001642extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001643 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001644 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001645}
1646
San Mehata19b2502010-01-06 10:33:53 -08001647bool VolumeManager::isMountpointMounted(const char *mp)
1648{
Yabin Cuid1104f72015-01-02 13:28:28 -08001649 FILE *fp = setmntent("/proc/mounts", "r");
1650 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001651 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001652 return false;
1653 }
1654
Yabin Cuid1104f72015-01-02 13:28:28 -08001655 bool found_mp = false;
1656 mntent* mentry;
1657 while ((mentry = getmntent(fp)) != NULL) {
1658 if (strcmp(mentry->mnt_dir, mp) == 0) {
1659 found_mp = true;
1660 break;
San Mehata19b2502010-01-06 10:33:53 -08001661 }
San Mehata19b2502010-01-06 10:33:53 -08001662 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001663 endmntent(fp);
1664 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001665}
1666
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001667int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001668 // Only offer to create directories for paths managed by vold
1669 if (strncmp(path, "/storage/", 9) == 0) {
1670 // fs_mkdirs() does symlink checking and relative path enforcement
1671 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001672 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001673 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001674 return -EINVAL;
1675 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001676}