blob: cd1017c97cc5b282bfbe828fc01ea0752db28100 [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 }
382 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
383 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
384 strerror(errno));
385 return -errno;
386 }
387 return 0;
388}
389
390int VolumeManager::startUser(userid_t userId) {
391 // Note that sometimes the system will spin up processes from Zygote
392 // before actually starting the user, so we're okay if Zygote
393 // already created this directory.
394 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
395 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
396
397 mUsers.push_back(userId);
398 if (mPrimary) {
399 linkPrimary(userId);
400 }
401 return 0;
402}
403
404int VolumeManager::cleanupUser(userid_t userId) {
405 mUsers.remove(userId);
406 return 0;
407}
408
409int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
410 mPrimary = vol;
411 for (userid_t userId : mUsers) {
412 linkPrimary(userId);
413 }
414 return 0;
415}
416
417int VolumeManager::reset() {
418 // Tear down all existing disks/volumes and start from a blank slate so
419 // newly connected framework hears all events.
420 mInternalEmulated->destroy();
421 mInternalEmulated->create();
422 for (auto disk : mDisks) {
423 disk->destroy();
424 disk->create();
425 }
426 mUsers.clear();
427 return 0;
428}
429
430int VolumeManager::shutdown() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700431 mInternalEmulated->destroy();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700432 for (auto disk : mDisks) {
433 disk->destroy();
434 }
435 mDisks.clear();
436 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700437}
438
Jeff Sharkey9c484982015-03-31 10:35:33 -0700439int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700440 std::lock_guard<std::mutex> lock(mLock);
441
Jeff Sharkey9c484982015-03-31 10:35:33 -0700442 // First, try gracefully unmounting all known devices
443 if (mInternalEmulated != nullptr) {
444 mInternalEmulated->unmount();
445 }
446 for (auto disk : mDisks) {
447 disk->unmountAll();
448 }
449
450 // Worst case we might have some stale mounts lurking around, so
451 // force unmount those just to be safe.
452 FILE* fp = setmntent("/proc/mounts", "r");
453 if (fp == NULL) {
454 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
455 return -errno;
456 }
457
458 // Some volumes can be stacked on each other, so force unmount in
459 // reverse order to give us the best chance of success.
460 std::list<std::string> toUnmount;
461 mntent* mentry;
462 while ((mentry = getmntent(fp)) != NULL) {
463 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
464 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
465 toUnmount.push_front(std::string(mentry->mnt_dir));
466 }
467 }
468 endmntent(fp);
469
470 for (auto path : toUnmount) {
471 SLOGW("Tearing down stale mount %s", path.c_str());
472 android::vold::ForceUnmount(path);
473 }
474
475 return 0;
476}
477
Kenny Root508c0e12010-07-12 09:59:49 -0700478int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
479 char idHash[33];
480 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
481 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
482 return -1;
483 }
484
485 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700486 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400487 if ((written < 0) || (written >= mountPathLen)) {
488 errno = EINVAL;
489 return -1;
490 }
Kenny Root508c0e12010-07-12 09:59:49 -0700491
492 if (access(mountPath, F_OK)) {
493 errno = ENOENT;
494 return -1;
495 }
496
497 return 0;
498}
499
San Mehata19b2502010-01-06 10:33:53 -0800500int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700501 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700502
Nick Kralevich0de7c612014-01-27 14:58:06 -0800503 if (!isLegalAsecId(id)) {
504 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
505 errno = EINVAL;
506 return -1;
507 }
508
Kenny Root344ca102012-04-03 17:23:01 -0700509 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
510 SLOGE("Couldn't find ASEC %s", id);
511 return -1;
512 }
San Mehat88ac2c02010-03-23 11:15:58 -0700513
514 memset(buffer, 0, maxlen);
515 if (access(asecFileName, F_OK)) {
516 errno = ENOENT;
517 return -1;
518 }
San Mehata19b2502010-01-06 10:33:53 -0800519
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700520 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400521 if ((written < 0) || (written >= maxlen)) {
522 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
523 errno = EINVAL;
524 return -1;
525 }
526
San Mehata19b2502010-01-06 10:33:53 -0800527 return 0;
528}
529
Dianne Hackborn736910c2011-06-27 13:37:07 -0700530int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
531 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700532
Nick Kralevich0de7c612014-01-27 14:58:06 -0800533 if (!isLegalAsecId(id)) {
534 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
535 errno = EINVAL;
536 return -1;
537 }
538
Kenny Root344ca102012-04-03 17:23:01 -0700539 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
540 SLOGE("Couldn't find ASEC %s", id);
541 return -1;
542 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700543
544 memset(buffer, 0, maxlen);
545 if (access(asecFileName, F_OK)) {
546 errno = ENOENT;
547 return -1;
548 }
549
rpcraigd1c226f2012-10-09 06:58:16 -0400550 int written = snprintf(buffer, maxlen, "%s", asecFileName);
551 if ((written < 0) || (written >= maxlen)) {
552 errno = EINVAL;
553 return -1;
554 }
555
Dianne Hackborn736910c2011-06-27 13:37:07 -0700556 return 0;
557}
558
Kenny Root344ca102012-04-03 17:23:01 -0700559int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
560 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800561 struct asec_superblock sb;
562 memset(&sb, 0, sizeof(sb));
563
Nick Kralevich0de7c612014-01-27 14:58:06 -0800564 if (!isLegalAsecId(id)) {
565 SLOGE("createAsec: Invalid asec id \"%s\"", id);
566 errno = EINVAL;
567 return -1;
568 }
569
Kenny Root344ca102012-04-03 17:23:01 -0700570 const bool wantFilesystem = strcmp(fstype, "none");
571 bool usingExt4 = false;
572 if (wantFilesystem) {
573 usingExt4 = !strcmp(fstype, "ext4");
574 if (usingExt4) {
575 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
576 } else if (strcmp(fstype, "fat")) {
577 SLOGE("Invalid filesystem type %s", fstype);
578 errno = EINVAL;
579 return -1;
580 }
581 }
582
San Mehatfcf24fe2010-03-03 12:37:32 -0800583 sb.magic = ASEC_SB_MAGIC;
584 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800585
San Mehatd31e3802010-02-18 08:37:45 -0800586 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700587 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800588 errno = EINVAL;
589 return -1;
590 }
591
San Mehata19b2502010-01-06 10:33:53 -0800592 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700593
594 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
595 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
596 asecFileName, strerror(errno));
597 errno = EADDRINUSE;
598 return -1;
599 }
600
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700601 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700602
rpcraigd1c226f2012-10-09 06:58:16 -0400603 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
604 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
605 errno = EINVAL;
606 return -1;
607 }
San Mehata19b2502010-01-06 10:33:53 -0800608
609 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700610 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700611 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800612 errno = EADDRINUSE;
613 return -1;
614 }
615
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700616 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700617 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700618 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700619 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700620 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800621
622 // Add +1 for our superblock which is at the end
623 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700624 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800625 return -1;
626 }
627
San Mehatd9a4e352010-03-12 13:32:47 -0800628 char idHash[33];
629 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700630 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800631 unlink(asecFileName);
632 return -1;
633 }
634
San Mehata19b2502010-01-06 10:33:53 -0800635 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800636 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700637 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800638 unlink(asecFileName);
639 return -1;
640 }
641
San Mehatb78a32c2010-01-10 13:02:12 -0800642 char dmDevice[255];
643 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800644
San Mehatb78a32c2010-01-10 13:02:12 -0800645 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800646 // XXX: This is all we support for now
647 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800648 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800649 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700650 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800651 Loop::destroyByDevice(loopDevice);
652 unlink(asecFileName);
653 return -1;
654 }
655 cleanupDm = true;
656 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800657 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800658 strcpy(dmDevice, loopDevice);
659 }
660
San Mehatfcf24fe2010-03-03 12:37:32 -0800661 /*
662 * Drop down the superblock at the end of the file
663 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700664 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800665 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800666 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800667 }
668 Loop::destroyByDevice(loopDevice);
669 unlink(asecFileName);
670 return -1;
671 }
672
Kenny Root344ca102012-04-03 17:23:01 -0700673 if (wantFilesystem) {
674 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400675 char mountPoint[255];
676
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700677 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400678 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
679 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
680 if (cleanupDm) {
681 Devmapper::destroy(idHash);
682 }
683 Loop::destroyByDevice(loopDevice);
684 unlink(asecFileName);
685 return -1;
686 }
rpcraiga54e13a2012-09-21 14:17:08 -0400687
Kenny Root344ca102012-04-03 17:23:01 -0700688 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700689 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700690 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700691 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800692 }
San Mehata19b2502010-01-06 10:33:53 -0800693
Kenny Root344ca102012-04-03 17:23:01 -0700694 if (formatStatus < 0) {
695 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800696 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800697 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800698 }
San Mehateb13a902010-01-07 12:12:50 -0800699 Loop::destroyByDevice(loopDevice);
700 unlink(asecFileName);
701 return -1;
702 }
Kenny Root344ca102012-04-03 17:23:01 -0700703
Kenny Root344ca102012-04-03 17:23:01 -0700704 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800705 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700706 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800707 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800708 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800709 }
710 Loop::destroyByDevice(loopDevice);
711 unlink(asecFileName);
712 return -1;
713 }
San Mehatb78a32c2010-01-10 13:02:12 -0800714 }
San Mehata1091cb2010-02-28 20:17:20 -0800715
Kenny Root344ca102012-04-03 17:23:01 -0700716 int mountStatus;
717 if (usingExt4) {
718 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
719 } else {
720 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
721 false);
722 }
723
724 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700725 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800726 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800727 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800728 }
729 Loop::destroyByDevice(loopDevice);
730 unlink(asecFileName);
731 return -1;
732 }
Kenny Root344ca102012-04-03 17:23:01 -0700733
734 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700735 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -0700736 if (dirfd >= 0) {
737 if (fchown(dirfd, ownerUid, AID_SYSTEM)
738 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
739 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
740 }
741 close(dirfd);
742 }
743 }
San Mehata1091cb2010-02-28 20:17:20 -0800744 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700745 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800746 }
San Mehat88705162010-01-15 09:26:28 -0800747
Kenny Rootcbacf782010-09-24 15:11:48 -0700748 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800749 return 0;
750}
751
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700752int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
753 char asecFileName[255];
754 char mountPoint[255];
755 bool cleanupDm = false;
756
757 if (!isLegalAsecId(id)) {
758 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
759 errno = EINVAL;
760 return -1;
761 }
762
763 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
764 SLOGE("Couldn't find ASEC %s", id);
765 return -1;
766 }
767
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700768 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700769 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
770 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
771 return -1;
772 }
773
774 if (isMountpointMounted(mountPoint)) {
775 SLOGE("ASEC %s mounted. Unmount before resizing", id);
776 errno = EBUSY;
777 return -1;
778 }
779
780 struct asec_superblock sb;
781 int fd;
782 unsigned int oldNumSec = 0;
783
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700784 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700785 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
786 return -1;
787 }
788
789 struct stat info;
790 if (fstat(fd, &info) < 0) {
791 SLOGE("Failed to get file size (%s)", strerror(errno));
792 close(fd);
793 return -1;
794 }
795
796 oldNumSec = info.st_size / 512;
797
798 unsigned numImgSectors;
799 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
800 numImgSectors = adjustSectorNumExt4(numSectors);
801 else
802 numImgSectors = adjustSectorNumFAT(numSectors);
803 /*
804 * add one block for the superblock
805 */
806 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700807 if (oldNumSec == numImgSectors + 1) {
808 SLOGW("Size unchanged; ignoring resize request");
809 return 0;
810 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700811 SLOGE("Only growing is currently supported.");
812 close(fd);
813 return -1;
814 }
815
816 /*
817 * Try to read superblock.
818 */
819 memset(&sb, 0, sizeof(struct asec_superblock));
820 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
821 SLOGE("lseek failed (%s)", strerror(errno));
822 close(fd);
823 return -1;
824 }
825 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
826 SLOGE("superblock read failed (%s)", strerror(errno));
827 close(fd);
828 return -1;
829 }
830 close(fd);
831
832 if (mDebug) {
833 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
834 }
835 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
836 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
837 errno = EMEDIUMTYPE;
838 return -1;
839 }
840
841 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
842 SLOGE("Only ext4 partitions are supported for resize");
843 errno = EINVAL;
844 return -1;
845 }
846
847 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
848 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
849 return -1;
850 }
851
852 /*
853 * Drop down a copy of the superblock at the end of the file
854 */
855 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
856 goto fail;
857
858 char idHash[33];
859 if (!asecHash(id, idHash, sizeof(idHash))) {
860 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
861 goto fail;
862 }
863
864 char loopDevice[255];
865 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
866 goto fail;
867
868 char dmDevice[255];
869
870 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
871 Loop::destroyByDevice(loopDevice);
872 goto fail;
873 }
874
875 /*
876 * Wait for the device mapper node to be created.
877 */
878 waitForDevMapper(dmDevice);
879
880 if (Ext4::resize(dmDevice, numImgSectors)) {
881 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
882 if (cleanupDm) {
883 Devmapper::destroy(idHash);
884 }
885 Loop::destroyByDevice(loopDevice);
886 goto fail;
887 }
888
889 return 0;
890fail:
891 Loop::resizeImageFile(asecFileName, oldNumSec);
892 return -1;
893}
894
San Mehata19b2502010-01-06 10:33:53 -0800895int VolumeManager::finalizeAsec(const char *id) {
896 char asecFileName[255];
897 char loopDevice[255];
898 char mountPoint[255];
899
Nick Kralevich0de7c612014-01-27 14:58:06 -0800900 if (!isLegalAsecId(id)) {
901 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
902 errno = EINVAL;
903 return -1;
904 }
905
Kenny Root344ca102012-04-03 17:23:01 -0700906 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
907 SLOGE("Couldn't find ASEC %s", id);
908 return -1;
909 }
San Mehata19b2502010-01-06 10:33:53 -0800910
San Mehatd9a4e352010-03-12 13:32:47 -0800911 char idHash[33];
912 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700913 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800914 return -1;
915 }
916
917 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700918 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800919 return -1;
920 }
921
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900922 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -0700923 struct asec_superblock sb;
924
925 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
926 return -1;
927 }
928
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700929 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400930 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
931 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
932 return -1;
933 }
Kenny Root344ca102012-04-03 17:23:01 -0700934
935 int result = 0;
936 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
937 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
938 } else {
939 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
940 }
941
942 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700943 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800944 return -1;
945 }
946
San Mehatd9a4e352010-03-12 13:32:47 -0800947 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700948 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800949 }
San Mehata19b2502010-01-06 10:33:53 -0800950 return 0;
951}
952
Kenny Root344ca102012-04-03 17:23:01 -0700953int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
954 char asecFileName[255];
955 char loopDevice[255];
956 char mountPoint[255];
957
958 if (gid < AID_APP) {
959 SLOGE("Group ID is not in application range");
960 return -1;
961 }
962
Nick Kralevich0de7c612014-01-27 14:58:06 -0800963 if (!isLegalAsecId(id)) {
964 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
965 errno = EINVAL;
966 return -1;
967 }
968
Kenny Root344ca102012-04-03 17:23:01 -0700969 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
970 SLOGE("Couldn't find ASEC %s", id);
971 return -1;
972 }
973
974 char idHash[33];
975 if (!asecHash(id, idHash, sizeof(idHash))) {
976 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
977 return -1;
978 }
979
980 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
981 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
982 return -1;
983 }
984
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900985 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -0700986 struct asec_superblock sb;
987
988 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
989 return -1;
990 }
991
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700992 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400993 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
994 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
995 return -1;
996 }
Kenny Root344ca102012-04-03 17:23:01 -0700997
998 int result = 0;
999 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1000 return 0;
1001 }
1002
1003 int ret = Ext4::doMount(loopDevice, mountPoint,
1004 false /* read-only */,
1005 true /* remount */,
1006 false /* executable */);
1007 if (ret) {
1008 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1009 return -1;
1010 }
1011
1012 char *paths[] = { mountPoint, NULL };
1013
1014 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1015 if (fts) {
1016 // Traverse the entire hierarchy and chown to system UID.
1017 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1018 // We don't care about the lost+found directory.
1019 if (!strcmp(ftsent->fts_name, "lost+found")) {
1020 continue;
1021 }
1022
1023 /*
1024 * There can only be one file marked as private right now.
1025 * This should be more robust, but it satisfies the requirements
1026 * we have for right now.
1027 */
1028 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1029
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001030 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001031 if (fd < 0) {
1032 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1033 result = -1;
1034 continue;
1035 }
1036
1037 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1038
1039 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001040 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001041 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001042 result |= fchmod(fd, privateFile ? 0640 : 0644);
1043 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001044
Stephen Smalley5093e612014-02-12 09:43:08 -05001045 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001046 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1047 result |= -1;
1048 }
1049
Kenny Root344ca102012-04-03 17:23:01 -07001050 close(fd);
1051 }
1052 fts_close(fts);
1053
1054 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001055 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001056 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1057 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1058 result |= -1;
1059 }
1060 close(dirfd);
1061 } else {
1062 result |= -1;
1063 }
1064
1065 result |= Ext4::doMount(loopDevice, mountPoint,
1066 true /* read-only */,
1067 true /* remount */,
1068 true /* execute */);
1069
1070 if (result) {
1071 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1072 return -1;
1073 }
1074
1075 if (mDebug) {
1076 SLOGD("ASEC %s permissions fixed", id);
1077 }
1078 return 0;
1079}
1080
San Mehat048b0802010-01-23 08:17:06 -08001081int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001082 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001083 char *asecFilename2;
1084 char mountPoint[255];
1085
Kenny Root344ca102012-04-03 17:23:01 -07001086 const char *dir;
1087
Nick Kralevich0de7c612014-01-27 14:58:06 -08001088 if (!isLegalAsecId(id1)) {
1089 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1090 errno = EINVAL;
1091 return -1;
1092 }
1093
1094 if (!isLegalAsecId(id2)) {
1095 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1096 errno = EINVAL;
1097 return -1;
1098 }
1099
Kenny Root344ca102012-04-03 17:23:01 -07001100 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1101 SLOGE("Couldn't find ASEC %s", id1);
1102 return -1;
1103 }
1104
1105 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001106
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001107 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001108 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1109 SLOGE("Rename failed: couldn't construct mountpoint");
1110 goto out_err;
1111 }
1112
San Mehat048b0802010-01-23 08:17:06 -08001113 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001114 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001115 errno = EBUSY;
1116 goto out_err;
1117 }
1118
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001119 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001120 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1121 SLOGE("Rename failed: couldn't construct mountpoint2");
1122 goto out_err;
1123 }
1124
San Mehat96956ed2010-02-24 08:42:51 -08001125 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001126 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001127 errno = EBUSY;
1128 goto out_err;
1129 }
1130
San Mehat048b0802010-01-23 08:17:06 -08001131 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001132 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001133 errno = EADDRINUSE;
1134 goto out_err;
1135 }
1136
1137 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001138 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001139 goto out_err;
1140 }
1141
San Mehat048b0802010-01-23 08:17:06 -08001142 free(asecFilename2);
1143 return 0;
1144
1145out_err:
San Mehat048b0802010-01-23 08:17:06 -08001146 free(asecFilename2);
1147 return -1;
1148}
1149
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001150#define UNMOUNT_RETRIES 5
1151#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001152int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001153 char asecFileName[255];
1154 char mountPoint[255];
1155
Nick Kralevich0de7c612014-01-27 14:58:06 -08001156 if (!isLegalAsecId(id)) {
1157 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1158 errno = EINVAL;
1159 return -1;
1160 }
1161
Kenny Root344ca102012-04-03 17:23:01 -07001162 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1163 SLOGE("Couldn't find ASEC %s", id);
1164 return -1;
1165 }
1166
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001167 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001168 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1169 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1170 return -1;
1171 }
San Mehata19b2502010-01-06 10:33:53 -08001172
San Mehatd9a4e352010-03-12 13:32:47 -08001173 char idHash[33];
1174 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001175 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001176 return -1;
1177 }
1178
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001179 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1180}
1181
Kenny Root508c0e12010-07-12 09:59:49 -07001182int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001183 char mountPoint[255];
1184
1185 char idHash[33];
1186 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1187 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1188 return -1;
1189 }
1190
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001191 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001192 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1193 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1194 return -1;
1195 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001196
1197 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1198}
1199
1200int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1201 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001202 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001203 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001204 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001205 return -1;
1206 }
San Mehat23969932010-01-09 07:08:06 -08001207
San Mehatb78a32c2010-01-10 13:02:12 -08001208 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001209 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001210 rc = umount(mountPoint);
1211 if (!rc) {
1212 break;
San Mehata19b2502010-01-06 10:33:53 -08001213 }
San Mehatb78a32c2010-01-10 13:02:12 -08001214 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001215 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001216 rc = 0;
1217 break;
San Mehata19b2502010-01-06 10:33:53 -08001218 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001219 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001220 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001221
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001222 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001223
1224 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001225 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001226 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001227 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001228 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001229 }
San Mehat8c940ef2010-02-13 14:19:53 -08001230
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001231 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001232 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001233 }
1234
1235 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001236 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001237 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001238 return -1;
1239 }
1240
San Mehat12f4b892010-02-24 11:43:22 -08001241 int retries = 10;
1242
1243 while(retries--) {
1244 if (!rmdir(mountPoint)) {
1245 break;
1246 }
1247
San Mehat97ac40e2010-03-24 10:24:19 -07001248 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001249 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001250 }
1251
1252 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001253 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001254 }
San Mehat88705162010-01-15 09:26:28 -08001255
Paul Lawrence60dec162014-09-02 10:52:15 -07001256 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1257 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1258 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1259 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1260 continue;
1261 } else {
1262 break;
1263 }
San Mehata19b2502010-01-06 10:33:53 -08001264 }
1265
1266 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001267 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001268 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001269 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001270 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001271 }
San Mehat88705162010-01-15 09:26:28 -08001272
1273 AsecIdCollection::iterator it;
1274 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001275 ContainerData* cd = *it;
1276 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001277 free(*it);
1278 mActiveContainers->erase(it);
1279 break;
1280 }
1281 }
1282 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001283 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001284 }
San Mehatb78a32c2010-01-10 13:02:12 -08001285 return 0;
1286}
1287
San Mehat4ba89482010-02-18 09:00:18 -08001288int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001289 char asecFileName[255];
1290 char mountPoint[255];
1291
Nick Kralevich0de7c612014-01-27 14:58:06 -08001292 if (!isLegalAsecId(id)) {
1293 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1294 errno = EINVAL;
1295 return -1;
1296 }
1297
Kenny Root344ca102012-04-03 17:23:01 -07001298 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1299 SLOGE("Couldn't find ASEC %s", id);
1300 return -1;
1301 }
1302
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001303 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001304 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1305 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1306 return -1;
1307 }
San Mehatb78a32c2010-01-10 13:02:12 -08001308
San Mehat0586d542010-01-12 15:38:59 -08001309 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001310 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001311 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001312 }
San Mehat4ba89482010-02-18 09:00:18 -08001313 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001314 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001315 return -1;
1316 }
1317 }
San Mehata19b2502010-01-06 10:33:53 -08001318
San Mehat0586d542010-01-12 15:38:59 -08001319 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001320 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001321 return -1;
1322 }
San Mehata19b2502010-01-06 10:33:53 -08001323
San Mehatd9a4e352010-03-12 13:32:47 -08001324 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001325 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001326 }
San Mehata19b2502010-01-06 10:33:53 -08001327 return 0;
1328}
1329
Nick Kralevich0de7c612014-01-27 14:58:06 -08001330/*
1331 * Legal ASEC ids consist of alphanumeric characters, '-',
1332 * '_', or '.'. ".." is not allowed. The first or last character
1333 * of the ASEC id cannot be '.' (dot).
1334 */
1335bool VolumeManager::isLegalAsecId(const char *id) const {
1336 size_t i;
1337 size_t len = strlen(id);
1338
1339 if (len == 0) {
1340 return false;
1341 }
1342 if ((id[0] == '.') || (id[len - 1] == '.')) {
1343 return false;
1344 }
1345
1346 for (i = 0; i < len; i++) {
1347 if (id[i] == '.') {
1348 // i=0 is guaranteed never to have a dot. See above.
1349 if (id[i-1] == '.') return false;
1350 continue;
1351 }
1352 if (id[i] == '_' || id[i] == '-') continue;
1353 if (id[i] >= 'a' && id[i] <= 'z') continue;
1354 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1355 if (id[i] >= '0' && id[i] <= '9') continue;
1356 return false;
1357 }
1358
1359 return true;
1360}
1361
Kenny Root344ca102012-04-03 17:23:01 -07001362bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001363 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001364 if (dirfd < 0) {
1365 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001366 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001367 }
1368
Nick Kralevich25e581a2015-02-06 08:55:08 -08001369 struct stat sb;
1370 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1371 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001372
1373 close(dirfd);
1374
1375 return ret;
1376}
1377
1378int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1379 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001380 char *asecName;
1381
Nick Kralevich0de7c612014-01-27 14:58:06 -08001382 if (!isLegalAsecId(id)) {
1383 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1384 errno = EINVAL;
1385 return -1;
1386 }
1387
Kenny Root344ca102012-04-03 17:23:01 -07001388 if (asprintf(&asecName, "%s.asec", id) < 0) {
1389 SLOGE("Couldn't allocate string to write ASEC name");
1390 return -1;
1391 }
1392
1393 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001394 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1395 dir = VolumeManager::SEC_ASECDIR_INT;
1396 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1397 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001398 } else {
1399 free(asecName);
1400 return -1;
1401 }
1402
1403 if (directory != NULL) {
1404 *directory = dir;
1405 }
1406
1407 if (asecPath != NULL) {
1408 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001409 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1410 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001411 free(asecName);
1412 return -1;
1413 }
1414 }
1415
1416 free(asecName);
1417 return 0;
1418}
1419
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001420int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001421 char asecFileName[255];
1422 char mountPoint[255];
1423
Nick Kralevich0de7c612014-01-27 14:58:06 -08001424 if (!isLegalAsecId(id)) {
1425 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1426 errno = EINVAL;
1427 return -1;
1428 }
1429
Kenny Root344ca102012-04-03 17:23:01 -07001430 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1431 SLOGE("Couldn't find ASEC %s", id);
1432 return -1;
1433 }
1434
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001435 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001436 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001437 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001438 return -1;
1439 }
San Mehata19b2502010-01-06 10:33:53 -08001440
1441 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001442 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001443 errno = EBUSY;
1444 return -1;
1445 }
1446
San Mehatd9a4e352010-03-12 13:32:47 -08001447 char idHash[33];
1448 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001449 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001450 return -1;
1451 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001452
San Mehata19b2502010-01-06 10:33:53 -08001453 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001454 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1455 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001456
1457 char dmDevice[255];
1458 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001459
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001460 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001461 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001462
Kenny Root344ca102012-04-03 17:23:01 -07001463 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1464 return -1;
1465 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001466
San Mehatd9a4e352010-03-12 13:32:47 -08001467 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001468 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001469 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001470 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001471 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001472 Loop::destroyByDevice(loopDevice);
1473 errno = EMEDIUMTYPE;
1474 return -1;
1475 }
1476 nr_sec--; // We don't want the devmapping to extend onto our superblock
1477
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001478 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1479 Loop::destroyByDevice(loopDevice);
1480 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001481 }
1482
Kenny Root344ca102012-04-03 17:23:01 -07001483 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001484 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001485 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001486 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001487 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001488 }
1489 Loop::destroyByDevice(loopDevice);
1490 return -1;
1491 }
San Mehata19b2502010-01-06 10:33:53 -08001492 }
1493
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001494 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001495 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001496 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001497 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001498
Kenny Root344ca102012-04-03 17:23:01 -07001499 int result;
1500 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001501 result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001502 } else {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001503 result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001504 }
1505
1506 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001507 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001508 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001509 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001510 }
1511 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001512 return -1;
1513 }
1514
Kenny Rootcbacf782010-09-24 15:11:48 -07001515 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001516 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001517 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001518 }
San Mehata19b2502010-01-06 10:33:53 -08001519 return 0;
1520}
1521
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001522/**
1523 * Mounts an image file <code>img</code>.
1524 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001525int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001526 char mountPoint[255];
1527
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001528 char idHash[33];
1529 if (!asecHash(img, idHash, sizeof(idHash))) {
1530 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1531 return -1;
1532 }
1533
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001534 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001535 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001536 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001537 return -1;
1538 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001539
1540 if (isMountpointMounted(mountPoint)) {
1541 SLOGE("Image %s already mounted", img);
1542 errno = EBUSY;
1543 return -1;
1544 }
1545
1546 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001547 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1548 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001549
1550 char dmDevice[255];
1551 bool cleanupDm = false;
1552 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001553 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001554
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001555 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001556 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1557 Loop::destroyByDevice(loopDevice);
1558 return -1;
1559 }
1560
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001561 get_blkdev_size(fd, &nr_sec);
1562 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001563 SLOGE("Failed to get loop size (%s)", strerror(errno));
1564 Loop::destroyByDevice(loopDevice);
1565 close(fd);
1566 return -1;
1567 }
1568
1569 close(fd);
1570
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001571 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001572 Loop::destroyByDevice(loopDevice);
1573 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001574 }
1575
1576 if (mkdir(mountPoint, 0755)) {
1577 if (errno != EEXIST) {
1578 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1579 if (cleanupDm) {
1580 Devmapper::destroy(idHash);
1581 }
1582 Loop::destroyByDevice(loopDevice);
1583 return -1;
1584 }
1585 }
1586
Jeff Sharkey69479042012-09-25 16:14:57 -07001587 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001588 0227, false)) {
1589 SLOGE("Image mount failed (%s)", strerror(errno));
1590 if (cleanupDm) {
1591 Devmapper::destroy(idHash);
1592 }
1593 Loop::destroyByDevice(loopDevice);
1594 return -1;
1595 }
1596
Kenny Rootcbacf782010-09-24 15:11:48 -07001597 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001598 if (mDebug) {
1599 SLOGD("Image %s mounted", img);
1600 }
1601 return 0;
1602}
1603
Kenny Root508c0e12010-07-12 09:59:49 -07001604int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001605 FILE *fp = setmntent("/proc/mounts", "r");
1606 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001607 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1608 return -1;
1609 }
1610
1611 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001612 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001613 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001614 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001615 loopDir[loopDirLen++] = '/';
1616 loopDir[loopDirLen] = '\0';
1617
Yabin Cuid1104f72015-01-02 13:28:28 -08001618 mntent* mentry;
1619 while ((mentry = getmntent(fp)) != NULL) {
1620 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001621 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001622 if (fd >= 0) {
1623 struct loop_info64 li;
1624 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1625 cli->sendMsg(ResponseCode::AsecListResult,
1626 (const char*) li.lo_file_name, false);
1627 }
1628 close(fd);
1629 }
1630 }
1631 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001632 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001633 return 0;
1634}
1635
Jeff Sharkey9c484982015-03-31 10:35:33 -07001636extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001637 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001638 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001639}
1640
San Mehata19b2502010-01-06 10:33:53 -08001641bool VolumeManager::isMountpointMounted(const char *mp)
1642{
Yabin Cuid1104f72015-01-02 13:28:28 -08001643 FILE *fp = setmntent("/proc/mounts", "r");
1644 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001645 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001646 return false;
1647 }
1648
Yabin Cuid1104f72015-01-02 13:28:28 -08001649 bool found_mp = false;
1650 mntent* mentry;
1651 while ((mentry = getmntent(fp)) != NULL) {
1652 if (strcmp(mentry->mnt_dir, mp) == 0) {
1653 found_mp = true;
1654 break;
San Mehata19b2502010-01-06 10:33:53 -08001655 }
San Mehata19b2502010-01-06 10:33:53 -08001656 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001657 endmntent(fp);
1658 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001659}
1660
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001661int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001662 // Only offer to create directories for paths managed by vold
1663 if (strncmp(path, "/storage/", 9) == 0) {
1664 // fs_mkdirs() does symlink checking and relative path enforcement
1665 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001666 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001667 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001668 return -EINVAL;
1669 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001670}