blob: c34f1697b4752ad7cfc966860bbb5dca9ac25d0a [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuid1104f72015-01-02 13:28:28 -080017#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070018#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080019#include <fcntl.h>
Kenny Root344ca102012-04-03 17:23:01 -070020#include <fts.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080021#include <mntent.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/mount.h>
San Mehata19b2502010-01-06 10:33:53 -080027#include <sys/stat.h>
28#include <sys/types.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080029#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080030
San Mehata2677e42009-12-13 10:40:18 -080031#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070032
33#define LOG_TAG "Vold"
34
Kenny Root7b18a7b2010-03-15 13:13:41 -070035#include <openssl/md5.h>
36
Jeff Sharkey36801cc2015-03-13 16:09:20 -070037#include <base/logging.h>
38#include <base/stringprintf.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070039#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070040#include <cutils/log.h>
41
Robert Craigb9e3ba52014-02-04 10:53:00 -050042#include <selinux/android.h>
43
San Mehatfd7f5872009-10-12 11:32:47 -070044#include <sysutils/NetlinkEvent.h>
45
Kenny Root344ca102012-04-03 17:23:01 -070046#include <private/android_filesystem_config.h>
47
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070048#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070049#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070050#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080052#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080053#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070054#include "Ext4.h"
San Mehata19b2502010-01-06 10:33:53 -080055#include "Fat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070056#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080057#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080058#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080059#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090060#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070061#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080062
Mike Lockwood97f2fc12011-06-07 10:51:38 -070063#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
64
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070065#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
66 + (number & (~((1U << po2) - 1))))
67
Jeff Sharkey36801cc2015-03-13 16:09:20 -070068using android::base::StringPrintf;
69
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070070/*
71 * Path to external storage where *only* root can access ASEC image files
72 */
73const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
74
75/*
76 * Path to internal storage where *only* root can access ASEC image files
77 */
78const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
79
80/*
81 * Path to where secure containers are mounted
82 */
83const char *VolumeManager::ASECDIR = "/mnt/asec";
84
85/*
86 * Path to where OBBs are mounted
87 */
88const char *VolumeManager::LOOPDIR = "/mnt/obb";
89
Jeff Sharkey36801cc2015-03-13 16:09:20 -070090static const char* kUserMountPath = "/mnt/user";
91
Jeff Sharkey36801cc2015-03-13 16:09:20 -070092static const unsigned int kMajorBlockMmc = 179;
93
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070094/* writes superblock at end of file or device given by name */
95static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070096 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070097 if (sbfd < 0) {
98 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
99 return -1;
100 }
101
102 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
103 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
104 close(sbfd);
105 return -1;
106 }
107
108 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
109 SLOGE("Failed to write superblock (%s)", strerror(errno));
110 close(sbfd);
111 return -1;
112 }
113 close(sbfd);
114 return 0;
115}
116
117static int adjustSectorNumExt4(unsigned numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700118 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
119 // preventing costly operations or unexpected ENOSPC error.
120 // Ext4::format() uses default block size without clustering.
121 unsigned clusterSectors = 4096 / 512;
122 unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
123 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700124 return ROUND_UP_POWER_OF_2(numSectors, 3);
125}
126
127static int adjustSectorNumFAT(unsigned numSectors) {
128 /*
129 * Add some headroom
130 */
131 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
132 numSectors += fatSize + 2;
133 /*
134 * FAT is aligned to 32 kb with 512b sectors.
135 */
136 return ROUND_UP_POWER_OF_2(numSectors, 6);
137}
138
139static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
140 if (Loop::lookupActive(idHash, buffer, len)) {
141 if (Loop::create(idHash, asecFileName, buffer, len)) {
142 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
143 return -1;
144 }
145 if (debug) {
146 SLOGD("New loop device created at %s", buffer);
147 }
148 } else {
149 if (debug) {
150 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
151 }
152 }
153 return 0;
154}
155
156static 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) {
157 if (strcmp(key, "none")) {
158 if (Devmapper::lookupActive(idHash, buffer, len)) {
159 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
160 buffer, len)) {
161 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
162 return -1;
163 }
164 if (debug) {
165 SLOGD("New devmapper instance created at %s", buffer);
166 }
167 } else {
168 if (debug) {
169 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
170 }
171 }
172 *createdDMDevice = true;
173 } else {
174 strcpy(buffer, loopDevice);
175 *createdDMDevice = false;
176 }
177 return 0;
178}
179
180static void waitForDevMapper(const char *dmDevice) {
181 /*
182 * Wait for the device mapper node to be created. Sometimes it takes a
183 * while. Wait for up to 1 second. We could also inspect incoming uevents,
184 * but that would take more effort.
185 */
186 int tries = 25;
187 while (tries--) {
188 if (!access(dmDevice, F_OK) || errno != ENOENT) {
189 break;
190 }
191 usleep(40 * 1000);
192 }
193}
194
San Mehatf1b736b2009-10-10 17:22:08 -0700195VolumeManager *VolumeManager::sInstance = NULL;
196
197VolumeManager *VolumeManager::Instance() {
198 if (!sInstance)
199 sInstance = new VolumeManager();
200 return sInstance;
201}
202
203VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800204 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800205 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700206 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400207 mUmsSharingCount = 0;
208 mSavedDirtyRatio = -1;
209 // set dirty ratio to 0 when UMS is active
210 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700211}
212
213VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800214 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700215}
216
Kenny Root7b18a7b2010-03-15 13:13:41 -0700217char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700218 static const char* digits = "0123456789abcdef";
219
Kenny Root7b18a7b2010-03-15 13:13:41 -0700220 unsigned char sig[MD5_DIGEST_LENGTH];
221
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700222 if (buffer == NULL) {
223 SLOGE("Destination buffer is NULL");
224 errno = ESPIPE;
225 return NULL;
226 } else if (id == NULL) {
227 SLOGE("Source buffer is NULL");
228 errno = ESPIPE;
229 return NULL;
230 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800231 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700232 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800233 errno = ESPIPE;
234 return NULL;
235 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700236
237 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800238
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700239 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700240 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700241 *p++ = digits[sig[i] >> 4];
242 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800243 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700244 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800245
246 return buffer;
247}
248
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700249int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800250 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700251 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800252}
253
San Mehatf1b736b2009-10-10 17:22:08 -0700254int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700255 // Always start from a clean slate by unmounting everything in
256 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700257 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700258
259 // Assume that we always have an emulated volume on internal
260 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700261 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700262 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700263 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700264 mInternalEmulated->create();
265
San Mehatf1b736b2009-10-10 17:22:08 -0700266 return 0;
267}
268
269int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700270 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700271 mInternalEmulated->destroy();
272 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700273 return 0;
274}
275
San Mehatfd7f5872009-10-12 11:32:47 -0700276void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700277 std::lock_guard<std::mutex> lock(mLock);
278
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700279 if (mDebug) {
280 LOG(VERBOSE) << "----------------";
281 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
282 evt->dump();
283 }
San Mehatf1b736b2009-10-10 17:22:08 -0700284
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700285 std::string eventPath(evt->findParam("DEVPATH"));
286 std::string devType(evt->findParam("DEVTYPE"));
287
288 if (devType != "disk") return;
289
290 int major = atoi(evt->findParam("MAJOR"));
291 int minor = atoi(evt->findParam("MINOR"));
292 dev_t device = makedev(major, minor);
293
294 switch (evt->getAction()) {
295 case NetlinkEvent::Action::kAdd: {
296 for (auto source : mDiskSources) {
297 if (source->matches(eventPath)) {
298 // For now, assume that MMC devices are SD, and that
299 // everything else is USB
300 int flags = source->getFlags();
301 if (major == kMajorBlockMmc) {
302 flags |= android::vold::Disk::Flags::kSd;
303 } else {
304 flags |= android::vold::Disk::Flags::kUsb;
305 }
306
307 auto disk = new android::vold::Disk(eventPath, device,
308 source->getNickname(), flags);
309 disk->create();
310 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
311 break;
312 }
313 }
314 break;
315 }
316 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700317 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700318 for (auto disk : mDisks) {
319 if (disk->getDevice() == device) {
320 disk->readMetadata();
321 disk->readPartitions();
322 }
323 }
324 break;
325 }
326 case NetlinkEvent::Action::kRemove: {
327 auto i = mDisks.begin();
328 while (i != mDisks.end()) {
329 if ((*i)->getDevice() == device) {
330 (*i)->destroy();
331 i = mDisks.erase(i);
332 } else {
333 ++i;
334 }
335 }
336 break;
337 }
338 default: {
339 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
340 break;
341 }
342 }
343}
344
345void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
346 mDiskSources.push_back(diskSource);
347}
348
349std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
350 for (auto disk : mDisks) {
351 if (disk->getId() == id) {
352 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700353 }
354 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700355 return nullptr;
356}
San Mehatf1b736b2009-10-10 17:22:08 -0700357
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700358std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
359 if (mInternalEmulated->getId() == id) {
360 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700361 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700362 for (auto disk : mDisks) {
363 auto vol = disk->findVolume(id);
364 if (vol != nullptr) {
365 return vol;
366 }
367 }
368 return nullptr;
369}
370
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700371nsecs_t VolumeManager::benchmarkVolume(const std::string& id) {
372 std::string path;
373 std::string sysPath;
374 auto vol = findVolume(id);
375 if (vol != nullptr) {
376 if (vol->getState() == android::vold::VolumeBase::State::kMounted) {
377 path = vol->getPath();
378 auto disk = findDisk(vol->getDiskId());
379 if (disk != nullptr) {
380 sysPath = disk->getSysPath();
381 }
382 }
383 } else {
384 path = "/data";
385 }
386
387 if (path.empty()) {
388 LOG(WARNING) << "Failed to find volume for " << id;
389 return -1;
390 }
391
392 path += "/misc";
393 if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
394 return -1;
395 }
396 path += "/vold";
397 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
398 return -1;
399 }
400 path += "/bench";
401 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
402 return -1;
403 }
404
405 return android::vold::Benchmark(path, sysPath);
406}
407
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700408int VolumeManager::linkPrimary(userid_t userId) {
409 std::string source(mPrimary->getPath());
410 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
411 source = StringPrintf("%s/%d", source.c_str(), userId);
412 }
413
414 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
415 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
416 if (errno != ENOENT) {
417 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
418 }
419 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700420 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700421 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
422 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
423 strerror(errno));
424 return -errno;
425 }
426 return 0;
427}
428
429int VolumeManager::startUser(userid_t userId) {
430 // Note that sometimes the system will spin up processes from Zygote
431 // before actually starting the user, so we're okay if Zygote
432 // already created this directory.
433 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
434 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
435
436 mUsers.push_back(userId);
437 if (mPrimary) {
438 linkPrimary(userId);
439 }
440 return 0;
441}
442
443int VolumeManager::cleanupUser(userid_t userId) {
444 mUsers.remove(userId);
445 return 0;
446}
447
448int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
449 mPrimary = vol;
450 for (userid_t userId : mUsers) {
451 linkPrimary(userId);
452 }
453 return 0;
454}
455
456int VolumeManager::reset() {
457 // Tear down all existing disks/volumes and start from a blank slate so
458 // newly connected framework hears all events.
459 mInternalEmulated->destroy();
460 mInternalEmulated->create();
461 for (auto disk : mDisks) {
462 disk->destroy();
463 disk->create();
464 }
465 mUsers.clear();
466 return 0;
467}
468
469int VolumeManager::shutdown() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700470 mInternalEmulated->destroy();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700471 for (auto disk : mDisks) {
472 disk->destroy();
473 }
474 mDisks.clear();
475 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700476}
477
Jeff Sharkey9c484982015-03-31 10:35:33 -0700478int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700479 std::lock_guard<std::mutex> lock(mLock);
480
Jeff Sharkey9c484982015-03-31 10:35:33 -0700481 // First, try gracefully unmounting all known devices
482 if (mInternalEmulated != nullptr) {
483 mInternalEmulated->unmount();
484 }
485 for (auto disk : mDisks) {
486 disk->unmountAll();
487 }
488
489 // Worst case we might have some stale mounts lurking around, so
490 // force unmount those just to be safe.
491 FILE* fp = setmntent("/proc/mounts", "r");
492 if (fp == NULL) {
493 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
494 return -errno;
495 }
496
497 // Some volumes can be stacked on each other, so force unmount in
498 // reverse order to give us the best chance of success.
499 std::list<std::string> toUnmount;
500 mntent* mentry;
501 while ((mentry = getmntent(fp)) != NULL) {
502 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
503 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
504 toUnmount.push_front(std::string(mentry->mnt_dir));
505 }
506 }
507 endmntent(fp);
508
509 for (auto path : toUnmount) {
510 SLOGW("Tearing down stale mount %s", path.c_str());
511 android::vold::ForceUnmount(path);
512 }
513
514 return 0;
515}
516
Kenny Root508c0e12010-07-12 09:59:49 -0700517int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
518 char idHash[33];
519 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
520 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
521 return -1;
522 }
523
524 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700525 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400526 if ((written < 0) || (written >= mountPathLen)) {
527 errno = EINVAL;
528 return -1;
529 }
Kenny Root508c0e12010-07-12 09:59:49 -0700530
531 if (access(mountPath, F_OK)) {
532 errno = ENOENT;
533 return -1;
534 }
535
536 return 0;
537}
538
San Mehata19b2502010-01-06 10:33:53 -0800539int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700540 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700541
Nick Kralevich0de7c612014-01-27 14:58:06 -0800542 if (!isLegalAsecId(id)) {
543 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
544 errno = EINVAL;
545 return -1;
546 }
547
Kenny Root344ca102012-04-03 17:23:01 -0700548 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
549 SLOGE("Couldn't find ASEC %s", id);
550 return -1;
551 }
San Mehat88ac2c02010-03-23 11:15:58 -0700552
553 memset(buffer, 0, maxlen);
554 if (access(asecFileName, F_OK)) {
555 errno = ENOENT;
556 return -1;
557 }
San Mehata19b2502010-01-06 10:33:53 -0800558
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700559 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400560 if ((written < 0) || (written >= maxlen)) {
561 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
562 errno = EINVAL;
563 return -1;
564 }
565
San Mehata19b2502010-01-06 10:33:53 -0800566 return 0;
567}
568
Dianne Hackborn736910c2011-06-27 13:37:07 -0700569int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
570 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700571
Nick Kralevich0de7c612014-01-27 14:58:06 -0800572 if (!isLegalAsecId(id)) {
573 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
574 errno = EINVAL;
575 return -1;
576 }
577
Kenny Root344ca102012-04-03 17:23:01 -0700578 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
579 SLOGE("Couldn't find ASEC %s", id);
580 return -1;
581 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700582
583 memset(buffer, 0, maxlen);
584 if (access(asecFileName, F_OK)) {
585 errno = ENOENT;
586 return -1;
587 }
588
rpcraigd1c226f2012-10-09 06:58:16 -0400589 int written = snprintf(buffer, maxlen, "%s", asecFileName);
590 if ((written < 0) || (written >= maxlen)) {
591 errno = EINVAL;
592 return -1;
593 }
594
Dianne Hackborn736910c2011-06-27 13:37:07 -0700595 return 0;
596}
597
Kenny Root344ca102012-04-03 17:23:01 -0700598int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
599 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800600 struct asec_superblock sb;
601 memset(&sb, 0, sizeof(sb));
602
Nick Kralevich0de7c612014-01-27 14:58:06 -0800603 if (!isLegalAsecId(id)) {
604 SLOGE("createAsec: Invalid asec id \"%s\"", id);
605 errno = EINVAL;
606 return -1;
607 }
608
Kenny Root344ca102012-04-03 17:23:01 -0700609 const bool wantFilesystem = strcmp(fstype, "none");
610 bool usingExt4 = false;
611 if (wantFilesystem) {
612 usingExt4 = !strcmp(fstype, "ext4");
613 if (usingExt4) {
614 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
615 } else if (strcmp(fstype, "fat")) {
616 SLOGE("Invalid filesystem type %s", fstype);
617 errno = EINVAL;
618 return -1;
619 }
620 }
621
San Mehatfcf24fe2010-03-03 12:37:32 -0800622 sb.magic = ASEC_SB_MAGIC;
623 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800624
San Mehatd31e3802010-02-18 08:37:45 -0800625 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700626 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800627 errno = EINVAL;
628 return -1;
629 }
630
San Mehata19b2502010-01-06 10:33:53 -0800631 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700632
633 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
634 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
635 asecFileName, strerror(errno));
636 errno = EADDRINUSE;
637 return -1;
638 }
639
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700640 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700641
rpcraigd1c226f2012-10-09 06:58:16 -0400642 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
643 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
644 errno = EINVAL;
645 return -1;
646 }
San Mehata19b2502010-01-06 10:33:53 -0800647
648 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700649 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700650 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800651 errno = EADDRINUSE;
652 return -1;
653 }
654
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700655 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700656 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700657 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700658 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700659 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800660
661 // Add +1 for our superblock which is at the end
662 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700663 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800664 return -1;
665 }
666
San Mehatd9a4e352010-03-12 13:32:47 -0800667 char idHash[33];
668 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700669 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800670 unlink(asecFileName);
671 return -1;
672 }
673
San Mehata19b2502010-01-06 10:33:53 -0800674 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800675 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700676 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800677 unlink(asecFileName);
678 return -1;
679 }
680
San Mehatb78a32c2010-01-10 13:02:12 -0800681 char dmDevice[255];
682 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800683
San Mehatb78a32c2010-01-10 13:02:12 -0800684 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800685 // XXX: This is all we support for now
686 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800687 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800688 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700689 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800690 Loop::destroyByDevice(loopDevice);
691 unlink(asecFileName);
692 return -1;
693 }
694 cleanupDm = true;
695 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800696 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800697 strcpy(dmDevice, loopDevice);
698 }
699
San Mehatfcf24fe2010-03-03 12:37:32 -0800700 /*
701 * Drop down the superblock at the end of the file
702 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700703 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800704 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800705 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800706 }
707 Loop::destroyByDevice(loopDevice);
708 unlink(asecFileName);
709 return -1;
710 }
711
Kenny Root344ca102012-04-03 17:23:01 -0700712 if (wantFilesystem) {
713 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400714 char mountPoint[255];
715
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700716 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400717 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
718 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
719 if (cleanupDm) {
720 Devmapper::destroy(idHash);
721 }
722 Loop::destroyByDevice(loopDevice);
723 unlink(asecFileName);
724 return -1;
725 }
rpcraiga54e13a2012-09-21 14:17:08 -0400726
Kenny Root344ca102012-04-03 17:23:01 -0700727 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700728 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700729 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700730 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800731 }
San Mehata19b2502010-01-06 10:33:53 -0800732
Kenny Root344ca102012-04-03 17:23:01 -0700733 if (formatStatus < 0) {
734 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800735 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800736 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800737 }
San Mehateb13a902010-01-07 12:12:50 -0800738 Loop::destroyByDevice(loopDevice);
739 unlink(asecFileName);
740 return -1;
741 }
Kenny Root344ca102012-04-03 17:23:01 -0700742
Kenny Root344ca102012-04-03 17:23:01 -0700743 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800744 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700745 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800746 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800747 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800748 }
749 Loop::destroyByDevice(loopDevice);
750 unlink(asecFileName);
751 return -1;
752 }
San Mehatb78a32c2010-01-10 13:02:12 -0800753 }
San Mehata1091cb2010-02-28 20:17:20 -0800754
Kenny Root344ca102012-04-03 17:23:01 -0700755 int mountStatus;
756 if (usingExt4) {
757 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
758 } else {
759 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
760 false);
761 }
762
763 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700764 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800765 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800766 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800767 }
768 Loop::destroyByDevice(loopDevice);
769 unlink(asecFileName);
770 return -1;
771 }
Kenny Root344ca102012-04-03 17:23:01 -0700772
773 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700774 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -0700775 if (dirfd >= 0) {
776 if (fchown(dirfd, ownerUid, AID_SYSTEM)
777 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
778 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
779 }
780 close(dirfd);
781 }
782 }
San Mehata1091cb2010-02-28 20:17:20 -0800783 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700784 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800785 }
San Mehat88705162010-01-15 09:26:28 -0800786
Kenny Rootcbacf782010-09-24 15:11:48 -0700787 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800788 return 0;
789}
790
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700791int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
792 char asecFileName[255];
793 char mountPoint[255];
794 bool cleanupDm = false;
795
796 if (!isLegalAsecId(id)) {
797 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
798 errno = EINVAL;
799 return -1;
800 }
801
802 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
803 SLOGE("Couldn't find ASEC %s", id);
804 return -1;
805 }
806
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700807 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700808 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
809 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
810 return -1;
811 }
812
813 if (isMountpointMounted(mountPoint)) {
814 SLOGE("ASEC %s mounted. Unmount before resizing", id);
815 errno = EBUSY;
816 return -1;
817 }
818
819 struct asec_superblock sb;
820 int fd;
821 unsigned int oldNumSec = 0;
822
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700823 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700824 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
825 return -1;
826 }
827
828 struct stat info;
829 if (fstat(fd, &info) < 0) {
830 SLOGE("Failed to get file size (%s)", strerror(errno));
831 close(fd);
832 return -1;
833 }
834
835 oldNumSec = info.st_size / 512;
836
837 unsigned numImgSectors;
838 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
839 numImgSectors = adjustSectorNumExt4(numSectors);
840 else
841 numImgSectors = adjustSectorNumFAT(numSectors);
842 /*
843 * add one block for the superblock
844 */
845 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700846 if (oldNumSec == numImgSectors + 1) {
847 SLOGW("Size unchanged; ignoring resize request");
848 return 0;
849 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700850 SLOGE("Only growing is currently supported.");
851 close(fd);
852 return -1;
853 }
854
855 /*
856 * Try to read superblock.
857 */
858 memset(&sb, 0, sizeof(struct asec_superblock));
859 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
860 SLOGE("lseek failed (%s)", strerror(errno));
861 close(fd);
862 return -1;
863 }
864 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
865 SLOGE("superblock read failed (%s)", strerror(errno));
866 close(fd);
867 return -1;
868 }
869 close(fd);
870
871 if (mDebug) {
872 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
873 }
874 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
875 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
876 errno = EMEDIUMTYPE;
877 return -1;
878 }
879
880 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
881 SLOGE("Only ext4 partitions are supported for resize");
882 errno = EINVAL;
883 return -1;
884 }
885
886 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
887 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
888 return -1;
889 }
890
891 /*
892 * Drop down a copy of the superblock at the end of the file
893 */
894 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
895 goto fail;
896
897 char idHash[33];
898 if (!asecHash(id, idHash, sizeof(idHash))) {
899 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
900 goto fail;
901 }
902
903 char loopDevice[255];
904 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
905 goto fail;
906
907 char dmDevice[255];
908
909 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
910 Loop::destroyByDevice(loopDevice);
911 goto fail;
912 }
913
914 /*
915 * Wait for the device mapper node to be created.
916 */
917 waitForDevMapper(dmDevice);
918
919 if (Ext4::resize(dmDevice, numImgSectors)) {
920 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
921 if (cleanupDm) {
922 Devmapper::destroy(idHash);
923 }
924 Loop::destroyByDevice(loopDevice);
925 goto fail;
926 }
927
928 return 0;
929fail:
930 Loop::resizeImageFile(asecFileName, oldNumSec);
931 return -1;
932}
933
San Mehata19b2502010-01-06 10:33:53 -0800934int VolumeManager::finalizeAsec(const char *id) {
935 char asecFileName[255];
936 char loopDevice[255];
937 char mountPoint[255];
938
Nick Kralevich0de7c612014-01-27 14:58:06 -0800939 if (!isLegalAsecId(id)) {
940 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
941 errno = EINVAL;
942 return -1;
943 }
944
Kenny Root344ca102012-04-03 17:23:01 -0700945 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
946 SLOGE("Couldn't find ASEC %s", id);
947 return -1;
948 }
San Mehata19b2502010-01-06 10:33:53 -0800949
San Mehatd9a4e352010-03-12 13:32:47 -0800950 char idHash[33];
951 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700952 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800953 return -1;
954 }
955
956 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700957 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800958 return -1;
959 }
960
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900961 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -0700962 struct asec_superblock sb;
963
964 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
965 return -1;
966 }
967
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700968 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400969 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
970 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
971 return -1;
972 }
Kenny Root344ca102012-04-03 17:23:01 -0700973
974 int result = 0;
975 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
976 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
977 } else {
978 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
979 }
980
981 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700982 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800983 return -1;
984 }
985
San Mehatd9a4e352010-03-12 13:32:47 -0800986 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700987 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800988 }
San Mehata19b2502010-01-06 10:33:53 -0800989 return 0;
990}
991
Kenny Root344ca102012-04-03 17:23:01 -0700992int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
993 char asecFileName[255];
994 char loopDevice[255];
995 char mountPoint[255];
996
997 if (gid < AID_APP) {
998 SLOGE("Group ID is not in application range");
999 return -1;
1000 }
1001
Nick Kralevich0de7c612014-01-27 14:58:06 -08001002 if (!isLegalAsecId(id)) {
1003 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1004 errno = EINVAL;
1005 return -1;
1006 }
1007
Kenny Root344ca102012-04-03 17:23:01 -07001008 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1009 SLOGE("Couldn't find ASEC %s", id);
1010 return -1;
1011 }
1012
1013 char idHash[33];
1014 if (!asecHash(id, idHash, sizeof(idHash))) {
1015 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1016 return -1;
1017 }
1018
1019 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1020 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1021 return -1;
1022 }
1023
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001024 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001025 struct asec_superblock sb;
1026
1027 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1028 return -1;
1029 }
1030
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001031 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001032 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1033 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1034 return -1;
1035 }
Kenny Root344ca102012-04-03 17:23:01 -07001036
1037 int result = 0;
1038 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1039 return 0;
1040 }
1041
1042 int ret = Ext4::doMount(loopDevice, mountPoint,
1043 false /* read-only */,
1044 true /* remount */,
1045 false /* executable */);
1046 if (ret) {
1047 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1048 return -1;
1049 }
1050
1051 char *paths[] = { mountPoint, NULL };
1052
1053 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1054 if (fts) {
1055 // Traverse the entire hierarchy and chown to system UID.
1056 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1057 // We don't care about the lost+found directory.
1058 if (!strcmp(ftsent->fts_name, "lost+found")) {
1059 continue;
1060 }
1061
1062 /*
1063 * There can only be one file marked as private right now.
1064 * This should be more robust, but it satisfies the requirements
1065 * we have for right now.
1066 */
1067 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1068
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001069 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001070 if (fd < 0) {
1071 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1072 result = -1;
1073 continue;
1074 }
1075
1076 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1077
1078 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001079 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001080 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001081 result |= fchmod(fd, privateFile ? 0640 : 0644);
1082 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001083
Stephen Smalley5093e612014-02-12 09:43:08 -05001084 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001085 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1086 result |= -1;
1087 }
1088
Kenny Root344ca102012-04-03 17:23:01 -07001089 close(fd);
1090 }
1091 fts_close(fts);
1092
1093 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001094 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001095 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1096 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1097 result |= -1;
1098 }
1099 close(dirfd);
1100 } else {
1101 result |= -1;
1102 }
1103
1104 result |= Ext4::doMount(loopDevice, mountPoint,
1105 true /* read-only */,
1106 true /* remount */,
1107 true /* execute */);
1108
1109 if (result) {
1110 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1111 return -1;
1112 }
1113
1114 if (mDebug) {
1115 SLOGD("ASEC %s permissions fixed", id);
1116 }
1117 return 0;
1118}
1119
San Mehat048b0802010-01-23 08:17:06 -08001120int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001121 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001122 char *asecFilename2;
1123 char mountPoint[255];
1124
Kenny Root344ca102012-04-03 17:23:01 -07001125 const char *dir;
1126
Nick Kralevich0de7c612014-01-27 14:58:06 -08001127 if (!isLegalAsecId(id1)) {
1128 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1129 errno = EINVAL;
1130 return -1;
1131 }
1132
1133 if (!isLegalAsecId(id2)) {
1134 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1135 errno = EINVAL;
1136 return -1;
1137 }
1138
Kenny Root344ca102012-04-03 17:23:01 -07001139 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1140 SLOGE("Couldn't find ASEC %s", id1);
1141 return -1;
1142 }
1143
1144 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001145
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001146 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001147 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1148 SLOGE("Rename failed: couldn't construct mountpoint");
1149 goto out_err;
1150 }
1151
San Mehat048b0802010-01-23 08:17:06 -08001152 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001153 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001154 errno = EBUSY;
1155 goto out_err;
1156 }
1157
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001158 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001159 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1160 SLOGE("Rename failed: couldn't construct mountpoint2");
1161 goto out_err;
1162 }
1163
San Mehat96956ed2010-02-24 08:42:51 -08001164 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001165 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001166 errno = EBUSY;
1167 goto out_err;
1168 }
1169
San Mehat048b0802010-01-23 08:17:06 -08001170 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001171 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001172 errno = EADDRINUSE;
1173 goto out_err;
1174 }
1175
1176 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001177 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001178 goto out_err;
1179 }
1180
San Mehat048b0802010-01-23 08:17:06 -08001181 free(asecFilename2);
1182 return 0;
1183
1184out_err:
San Mehat048b0802010-01-23 08:17:06 -08001185 free(asecFilename2);
1186 return -1;
1187}
1188
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001189#define UNMOUNT_RETRIES 5
1190#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001191int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001192 char asecFileName[255];
1193 char mountPoint[255];
1194
Nick Kralevich0de7c612014-01-27 14:58:06 -08001195 if (!isLegalAsecId(id)) {
1196 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1197 errno = EINVAL;
1198 return -1;
1199 }
1200
Kenny Root344ca102012-04-03 17:23:01 -07001201 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1202 SLOGE("Couldn't find ASEC %s", id);
1203 return -1;
1204 }
1205
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001206 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001207 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1208 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1209 return -1;
1210 }
San Mehata19b2502010-01-06 10:33:53 -08001211
San Mehatd9a4e352010-03-12 13:32:47 -08001212 char idHash[33];
1213 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001214 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001215 return -1;
1216 }
1217
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001218 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1219}
1220
Kenny Root508c0e12010-07-12 09:59:49 -07001221int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001222 char mountPoint[255];
1223
1224 char idHash[33];
1225 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1226 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1227 return -1;
1228 }
1229
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001230 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001231 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1232 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1233 return -1;
1234 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001235
1236 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1237}
1238
1239int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1240 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001241 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001242 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001243 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001244 return -1;
1245 }
San Mehat23969932010-01-09 07:08:06 -08001246
San Mehatb78a32c2010-01-10 13:02:12 -08001247 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001248 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001249 rc = umount(mountPoint);
1250 if (!rc) {
1251 break;
San Mehata19b2502010-01-06 10:33:53 -08001252 }
San Mehatb78a32c2010-01-10 13:02:12 -08001253 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001254 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001255 rc = 0;
1256 break;
San Mehata19b2502010-01-06 10:33:53 -08001257 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001258 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001259 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001260
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001261 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001262
1263 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001264 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001265 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001266 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001267 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001268 }
San Mehat8c940ef2010-02-13 14:19:53 -08001269
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001270 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001271 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001272 }
1273
1274 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001275 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001276 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001277 return -1;
1278 }
1279
San Mehat12f4b892010-02-24 11:43:22 -08001280 int retries = 10;
1281
1282 while(retries--) {
1283 if (!rmdir(mountPoint)) {
1284 break;
1285 }
1286
San Mehat97ac40e2010-03-24 10:24:19 -07001287 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001288 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001289 }
1290
1291 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001292 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001293 }
San Mehat88705162010-01-15 09:26:28 -08001294
Paul Lawrence60dec162014-09-02 10:52:15 -07001295 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1296 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1297 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1298 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1299 continue;
1300 } else {
1301 break;
1302 }
San Mehata19b2502010-01-06 10:33:53 -08001303 }
1304
1305 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001306 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001307 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001308 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001309 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001310 }
San Mehat88705162010-01-15 09:26:28 -08001311
1312 AsecIdCollection::iterator it;
1313 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001314 ContainerData* cd = *it;
1315 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001316 free(*it);
1317 mActiveContainers->erase(it);
1318 break;
1319 }
1320 }
1321 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001322 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001323 }
San Mehatb78a32c2010-01-10 13:02:12 -08001324 return 0;
1325}
1326
San Mehat4ba89482010-02-18 09:00:18 -08001327int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001328 char asecFileName[255];
1329 char mountPoint[255];
1330
Nick Kralevich0de7c612014-01-27 14:58:06 -08001331 if (!isLegalAsecId(id)) {
1332 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1333 errno = EINVAL;
1334 return -1;
1335 }
1336
Kenny Root344ca102012-04-03 17:23:01 -07001337 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1338 SLOGE("Couldn't find ASEC %s", id);
1339 return -1;
1340 }
1341
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001342 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001343 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1344 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1345 return -1;
1346 }
San Mehatb78a32c2010-01-10 13:02:12 -08001347
San Mehat0586d542010-01-12 15:38:59 -08001348 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001349 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001350 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001351 }
San Mehat4ba89482010-02-18 09:00:18 -08001352 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001353 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001354 return -1;
1355 }
1356 }
San Mehata19b2502010-01-06 10:33:53 -08001357
San Mehat0586d542010-01-12 15:38:59 -08001358 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001359 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001360 return -1;
1361 }
San Mehata19b2502010-01-06 10:33:53 -08001362
San Mehatd9a4e352010-03-12 13:32:47 -08001363 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001364 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001365 }
San Mehata19b2502010-01-06 10:33:53 -08001366 return 0;
1367}
1368
Nick Kralevich0de7c612014-01-27 14:58:06 -08001369/*
1370 * Legal ASEC ids consist of alphanumeric characters, '-',
1371 * '_', or '.'. ".." is not allowed. The first or last character
1372 * of the ASEC id cannot be '.' (dot).
1373 */
1374bool VolumeManager::isLegalAsecId(const char *id) const {
1375 size_t i;
1376 size_t len = strlen(id);
1377
1378 if (len == 0) {
1379 return false;
1380 }
1381 if ((id[0] == '.') || (id[len - 1] == '.')) {
1382 return false;
1383 }
1384
1385 for (i = 0; i < len; i++) {
1386 if (id[i] == '.') {
1387 // i=0 is guaranteed never to have a dot. See above.
1388 if (id[i-1] == '.') return false;
1389 continue;
1390 }
1391 if (id[i] == '_' || id[i] == '-') continue;
1392 if (id[i] >= 'a' && id[i] <= 'z') continue;
1393 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1394 if (id[i] >= '0' && id[i] <= '9') continue;
1395 return false;
1396 }
1397
1398 return true;
1399}
1400
Kenny Root344ca102012-04-03 17:23:01 -07001401bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001402 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001403 if (dirfd < 0) {
1404 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001405 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001406 }
1407
Nick Kralevich25e581a2015-02-06 08:55:08 -08001408 struct stat sb;
1409 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1410 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001411
1412 close(dirfd);
1413
1414 return ret;
1415}
1416
1417int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1418 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001419 char *asecName;
1420
Nick Kralevich0de7c612014-01-27 14:58:06 -08001421 if (!isLegalAsecId(id)) {
1422 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1423 errno = EINVAL;
1424 return -1;
1425 }
1426
Kenny Root344ca102012-04-03 17:23:01 -07001427 if (asprintf(&asecName, "%s.asec", id) < 0) {
1428 SLOGE("Couldn't allocate string to write ASEC name");
1429 return -1;
1430 }
1431
1432 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001433 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1434 dir = VolumeManager::SEC_ASECDIR_INT;
1435 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1436 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001437 } else {
1438 free(asecName);
1439 return -1;
1440 }
1441
1442 if (directory != NULL) {
1443 *directory = dir;
1444 }
1445
1446 if (asecPath != NULL) {
1447 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001448 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1449 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001450 free(asecName);
1451 return -1;
1452 }
1453 }
1454
1455 free(asecName);
1456 return 0;
1457}
1458
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001459int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001460 char asecFileName[255];
1461 char mountPoint[255];
1462
Nick Kralevich0de7c612014-01-27 14:58:06 -08001463 if (!isLegalAsecId(id)) {
1464 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1465 errno = EINVAL;
1466 return -1;
1467 }
1468
Kenny Root344ca102012-04-03 17:23:01 -07001469 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1470 SLOGE("Couldn't find ASEC %s", id);
1471 return -1;
1472 }
1473
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001474 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001475 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001476 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001477 return -1;
1478 }
San Mehata19b2502010-01-06 10:33:53 -08001479
1480 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001481 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001482 errno = EBUSY;
1483 return -1;
1484 }
1485
San Mehatd9a4e352010-03-12 13:32:47 -08001486 char idHash[33];
1487 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001488 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001489 return -1;
1490 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001491
San Mehata19b2502010-01-06 10:33:53 -08001492 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001493 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1494 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001495
1496 char dmDevice[255];
1497 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001498
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001499 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001500 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001501
Kenny Root344ca102012-04-03 17:23:01 -07001502 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1503 return -1;
1504 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001505
San Mehatd9a4e352010-03-12 13:32:47 -08001506 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001507 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001508 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001509 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001510 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001511 Loop::destroyByDevice(loopDevice);
1512 errno = EMEDIUMTYPE;
1513 return -1;
1514 }
1515 nr_sec--; // We don't want the devmapping to extend onto our superblock
1516
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001517 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1518 Loop::destroyByDevice(loopDevice);
1519 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001520 }
1521
Kenny Root344ca102012-04-03 17:23:01 -07001522 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001523 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001524 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001525 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001526 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001527 }
1528 Loop::destroyByDevice(loopDevice);
1529 return -1;
1530 }
San Mehata19b2502010-01-06 10:33:53 -08001531 }
1532
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001533 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001534 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001535 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001536 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001537
Kenny Root344ca102012-04-03 17:23:01 -07001538 int result;
1539 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001540 result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001541 } else {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001542 result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001543 }
1544
1545 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001546 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001547 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001548 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001549 }
1550 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001551 return -1;
1552 }
1553
Kenny Rootcbacf782010-09-24 15:11:48 -07001554 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001555 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001556 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001557 }
San Mehata19b2502010-01-06 10:33:53 -08001558 return 0;
1559}
1560
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001561/**
1562 * Mounts an image file <code>img</code>.
1563 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001564int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001565 char mountPoint[255];
1566
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001567 char idHash[33];
1568 if (!asecHash(img, idHash, sizeof(idHash))) {
1569 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1570 return -1;
1571 }
1572
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001573 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001574 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001575 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001576 return -1;
1577 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001578
1579 if (isMountpointMounted(mountPoint)) {
1580 SLOGE("Image %s already mounted", img);
1581 errno = EBUSY;
1582 return -1;
1583 }
1584
1585 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001586 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1587 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001588
1589 char dmDevice[255];
1590 bool cleanupDm = false;
1591 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001592 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001593
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001594 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001595 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1596 Loop::destroyByDevice(loopDevice);
1597 return -1;
1598 }
1599
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001600 get_blkdev_size(fd, &nr_sec);
1601 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001602 SLOGE("Failed to get loop size (%s)", strerror(errno));
1603 Loop::destroyByDevice(loopDevice);
1604 close(fd);
1605 return -1;
1606 }
1607
1608 close(fd);
1609
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001610 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001611 Loop::destroyByDevice(loopDevice);
1612 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001613 }
1614
1615 if (mkdir(mountPoint, 0755)) {
1616 if (errno != EEXIST) {
1617 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1618 if (cleanupDm) {
1619 Devmapper::destroy(idHash);
1620 }
1621 Loop::destroyByDevice(loopDevice);
1622 return -1;
1623 }
1624 }
1625
yoshiyuki hama476a6272015-01-28 16:37:23 +09001626 /*
1627 * Wait for the device mapper node to be created.
1628 */
1629 waitForDevMapper(dmDevice);
1630
Jeff Sharkey69479042012-09-25 16:14:57 -07001631 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001632 0227, false)) {
1633 SLOGE("Image mount failed (%s)", strerror(errno));
1634 if (cleanupDm) {
1635 Devmapper::destroy(idHash);
1636 }
1637 Loop::destroyByDevice(loopDevice);
1638 return -1;
1639 }
1640
Kenny Rootcbacf782010-09-24 15:11:48 -07001641 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001642 if (mDebug) {
1643 SLOGD("Image %s mounted", img);
1644 }
1645 return 0;
1646}
1647
Kenny Root508c0e12010-07-12 09:59:49 -07001648int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001649 FILE *fp = setmntent("/proc/mounts", "r");
1650 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001651 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1652 return -1;
1653 }
1654
1655 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001656 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001657 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001658 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001659 loopDir[loopDirLen++] = '/';
1660 loopDir[loopDirLen] = '\0';
1661
Yabin Cuid1104f72015-01-02 13:28:28 -08001662 mntent* mentry;
1663 while ((mentry = getmntent(fp)) != NULL) {
1664 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001665 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001666 if (fd >= 0) {
1667 struct loop_info64 li;
1668 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1669 cli->sendMsg(ResponseCode::AsecListResult,
1670 (const char*) li.lo_file_name, false);
1671 }
1672 close(fd);
1673 }
1674 }
1675 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001676 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001677 return 0;
1678}
1679
Jeff Sharkey9c484982015-03-31 10:35:33 -07001680extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001681 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001682 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001683}
1684
San Mehata19b2502010-01-06 10:33:53 -08001685bool VolumeManager::isMountpointMounted(const char *mp)
1686{
Yabin Cuid1104f72015-01-02 13:28:28 -08001687 FILE *fp = setmntent("/proc/mounts", "r");
1688 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001689 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001690 return false;
1691 }
1692
Yabin Cuid1104f72015-01-02 13:28:28 -08001693 bool found_mp = false;
1694 mntent* mentry;
1695 while ((mentry = getmntent(fp)) != NULL) {
1696 if (strcmp(mentry->mnt_dir, mp) == 0) {
1697 found_mp = true;
1698 break;
San Mehata19b2502010-01-06 10:33:53 -08001699 }
San Mehata19b2502010-01-06 10:33:53 -08001700 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001701 endmntent(fp);
1702 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001703}
1704
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001705int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001706 // Only offer to create directories for paths managed by vold
1707 if (strncmp(path, "/storage/", 9) == 0) {
1708 // fs_mkdirs() does symlink checking and relative path enforcement
1709 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001710 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001711 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001712 return -EINVAL;
1713 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001714}