blob: 6f783a2530f8fd01699c8fd6f1cd90528b1cc26d [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuid1104f72015-01-02 13:28:28 -080017#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070018#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080019#include <fcntl.h>
Kenny Root344ca102012-04-03 17:23:01 -070020#include <fts.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080021#include <mntent.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/mount.h>
San Mehata19b2502010-01-06 10:33:53 -080027#include <sys/stat.h>
28#include <sys/types.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080029#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080030
San Mehata2677e42009-12-13 10:40:18 -080031#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070032
33#define LOG_TAG "Vold"
34
Kenny Root7b18a7b2010-03-15 13:13:41 -070035#include <openssl/md5.h>
36
Jeff Sharkey36801cc2015-03-13 16:09:20 -070037#include <base/logging.h>
38#include <base/stringprintf.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070039#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070040#include <cutils/log.h>
41
Robert Craigb9e3ba52014-02-04 10:53:00 -050042#include <selinux/android.h>
43
San Mehatfd7f5872009-10-12 11:32:47 -070044#include <sysutils/NetlinkEvent.h>
45
Kenny Root344ca102012-04-03 17:23:01 -070046#include <private/android_filesystem_config.h>
47
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070048#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070049#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070050#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080052#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080053#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070054#include "fs/Ext4.h"
55#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070056#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080057#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080058#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080059#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090060#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070061#include "cryptfs.h"
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
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700429int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
430 mAddedUsers[userId] = userSerialNumber;
431 return 0;
432}
433
434int VolumeManager::onUserRemoved(userid_t userId) {
435 mAddedUsers.erase(userId);
436 return 0;
437}
438
439int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700440 // Note that sometimes the system will spin up processes from Zygote
441 // before actually starting the user, so we're okay if Zygote
442 // already created this directory.
443 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
444 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
445
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700446 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700447 if (mPrimary) {
448 linkPrimary(userId);
449 }
450 return 0;
451}
452
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700453int VolumeManager::onUserStopped(userid_t userId) {
454 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700455 return 0;
456}
457
458int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
459 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700460 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700461 linkPrimary(userId);
462 }
463 return 0;
464}
465
466int VolumeManager::reset() {
467 // Tear down all existing disks/volumes and start from a blank slate so
468 // newly connected framework hears all events.
469 mInternalEmulated->destroy();
470 mInternalEmulated->create();
471 for (auto disk : mDisks) {
472 disk->destroy();
473 disk->create();
474 }
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700475 mAddedUsers.clear();
476 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700477 return 0;
478}
479
480int VolumeManager::shutdown() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700481 mInternalEmulated->destroy();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700482 for (auto disk : mDisks) {
483 disk->destroy();
484 }
485 mDisks.clear();
486 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700487}
488
Jeff Sharkey9c484982015-03-31 10:35:33 -0700489int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700490 std::lock_guard<std::mutex> lock(mLock);
491
Jeff Sharkey9c484982015-03-31 10:35:33 -0700492 // First, try gracefully unmounting all known devices
493 if (mInternalEmulated != nullptr) {
494 mInternalEmulated->unmount();
495 }
496 for (auto disk : mDisks) {
497 disk->unmountAll();
498 }
499
500 // Worst case we might have some stale mounts lurking around, so
501 // force unmount those just to be safe.
502 FILE* fp = setmntent("/proc/mounts", "r");
503 if (fp == NULL) {
504 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
505 return -errno;
506 }
507
508 // Some volumes can be stacked on each other, so force unmount in
509 // reverse order to give us the best chance of success.
510 std::list<std::string> toUnmount;
511 mntent* mentry;
512 while ((mentry = getmntent(fp)) != NULL) {
513 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
514 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
515 toUnmount.push_front(std::string(mentry->mnt_dir));
516 }
517 }
518 endmntent(fp);
519
520 for (auto path : toUnmount) {
521 SLOGW("Tearing down stale mount %s", path.c_str());
522 android::vold::ForceUnmount(path);
523 }
524
525 return 0;
526}
527
Kenny Root508c0e12010-07-12 09:59:49 -0700528int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
529 char idHash[33];
530 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
531 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
532 return -1;
533 }
534
535 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700536 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400537 if ((written < 0) || (written >= mountPathLen)) {
538 errno = EINVAL;
539 return -1;
540 }
Kenny Root508c0e12010-07-12 09:59:49 -0700541
542 if (access(mountPath, F_OK)) {
543 errno = ENOENT;
544 return -1;
545 }
546
547 return 0;
548}
549
San Mehata19b2502010-01-06 10:33:53 -0800550int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700551 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700552
Nick Kralevich0de7c612014-01-27 14:58:06 -0800553 if (!isLegalAsecId(id)) {
554 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
555 errno = EINVAL;
556 return -1;
557 }
558
Kenny Root344ca102012-04-03 17:23:01 -0700559 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
560 SLOGE("Couldn't find ASEC %s", id);
561 return -1;
562 }
San Mehat88ac2c02010-03-23 11:15:58 -0700563
564 memset(buffer, 0, maxlen);
565 if (access(asecFileName, F_OK)) {
566 errno = ENOENT;
567 return -1;
568 }
San Mehata19b2502010-01-06 10:33:53 -0800569
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700570 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400571 if ((written < 0) || (written >= maxlen)) {
572 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
573 errno = EINVAL;
574 return -1;
575 }
576
San Mehata19b2502010-01-06 10:33:53 -0800577 return 0;
578}
579
Dianne Hackborn736910c2011-06-27 13:37:07 -0700580int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
581 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700582
Nick Kralevich0de7c612014-01-27 14:58:06 -0800583 if (!isLegalAsecId(id)) {
584 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
585 errno = EINVAL;
586 return -1;
587 }
588
Kenny Root344ca102012-04-03 17:23:01 -0700589 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
590 SLOGE("Couldn't find ASEC %s", id);
591 return -1;
592 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700593
594 memset(buffer, 0, maxlen);
595 if (access(asecFileName, F_OK)) {
596 errno = ENOENT;
597 return -1;
598 }
599
rpcraigd1c226f2012-10-09 06:58:16 -0400600 int written = snprintf(buffer, maxlen, "%s", asecFileName);
601 if ((written < 0) || (written >= maxlen)) {
602 errno = EINVAL;
603 return -1;
604 }
605
Dianne Hackborn736910c2011-06-27 13:37:07 -0700606 return 0;
607}
608
Kenny Root344ca102012-04-03 17:23:01 -0700609int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
610 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800611 struct asec_superblock sb;
612 memset(&sb, 0, sizeof(sb));
613
Nick Kralevich0de7c612014-01-27 14:58:06 -0800614 if (!isLegalAsecId(id)) {
615 SLOGE("createAsec: Invalid asec id \"%s\"", id);
616 errno = EINVAL;
617 return -1;
618 }
619
Kenny Root344ca102012-04-03 17:23:01 -0700620 const bool wantFilesystem = strcmp(fstype, "none");
621 bool usingExt4 = false;
622 if (wantFilesystem) {
623 usingExt4 = !strcmp(fstype, "ext4");
624 if (usingExt4) {
625 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
626 } else if (strcmp(fstype, "fat")) {
627 SLOGE("Invalid filesystem type %s", fstype);
628 errno = EINVAL;
629 return -1;
630 }
631 }
632
San Mehatfcf24fe2010-03-03 12:37:32 -0800633 sb.magic = ASEC_SB_MAGIC;
634 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800635
San Mehatd31e3802010-02-18 08:37:45 -0800636 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700637 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800638 errno = EINVAL;
639 return -1;
640 }
641
San Mehata19b2502010-01-06 10:33:53 -0800642 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700643
644 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
645 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
646 asecFileName, strerror(errno));
647 errno = EADDRINUSE;
648 return -1;
649 }
650
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700651 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700652
rpcraigd1c226f2012-10-09 06:58:16 -0400653 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
654 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
655 errno = EINVAL;
656 return -1;
657 }
San Mehata19b2502010-01-06 10:33:53 -0800658
659 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700660 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700661 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800662 errno = EADDRINUSE;
663 return -1;
664 }
665
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700666 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700667 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700668 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700669 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700670 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800671
672 // Add +1 for our superblock which is at the end
673 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700674 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800675 return -1;
676 }
677
San Mehatd9a4e352010-03-12 13:32:47 -0800678 char idHash[33];
679 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700680 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800681 unlink(asecFileName);
682 return -1;
683 }
684
San Mehata19b2502010-01-06 10:33:53 -0800685 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800686 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700687 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800688 unlink(asecFileName);
689 return -1;
690 }
691
San Mehatb78a32c2010-01-10 13:02:12 -0800692 char dmDevice[255];
693 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800694
San Mehatb78a32c2010-01-10 13:02:12 -0800695 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800696 // XXX: This is all we support for now
697 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800698 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800699 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700700 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800701 Loop::destroyByDevice(loopDevice);
702 unlink(asecFileName);
703 return -1;
704 }
705 cleanupDm = true;
706 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800707 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800708 strcpy(dmDevice, loopDevice);
709 }
710
San Mehatfcf24fe2010-03-03 12:37:32 -0800711 /*
712 * Drop down the superblock at the end of the file
713 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700714 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800715 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800716 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800717 }
718 Loop::destroyByDevice(loopDevice);
719 unlink(asecFileName);
720 return -1;
721 }
722
Kenny Root344ca102012-04-03 17:23:01 -0700723 if (wantFilesystem) {
724 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400725 char mountPoint[255];
726
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700727 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400728 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
729 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
730 if (cleanupDm) {
731 Devmapper::destroy(idHash);
732 }
733 Loop::destroyByDevice(loopDevice);
734 unlink(asecFileName);
735 return -1;
736 }
rpcraiga54e13a2012-09-21 14:17:08 -0400737
Kenny Root344ca102012-04-03 17:23:01 -0700738 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700739 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700740 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700741 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800742 }
San Mehata19b2502010-01-06 10:33:53 -0800743
Kenny Root344ca102012-04-03 17:23:01 -0700744 if (formatStatus < 0) {
745 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800746 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800747 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800748 }
San Mehateb13a902010-01-07 12:12:50 -0800749 Loop::destroyByDevice(loopDevice);
750 unlink(asecFileName);
751 return -1;
752 }
Kenny Root344ca102012-04-03 17:23:01 -0700753
Kenny Root344ca102012-04-03 17:23:01 -0700754 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800755 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700756 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800757 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800758 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800759 }
760 Loop::destroyByDevice(loopDevice);
761 unlink(asecFileName);
762 return -1;
763 }
San Mehatb78a32c2010-01-10 13:02:12 -0800764 }
San Mehata1091cb2010-02-28 20:17:20 -0800765
Kenny Root344ca102012-04-03 17:23:01 -0700766 int mountStatus;
767 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700768 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
769 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -0700770 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700771 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
772 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -0700773 }
774
775 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700776 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800777 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800778 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800779 }
780 Loop::destroyByDevice(loopDevice);
781 unlink(asecFileName);
782 return -1;
783 }
Kenny Root344ca102012-04-03 17:23:01 -0700784
785 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700786 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -0700787 if (dirfd >= 0) {
788 if (fchown(dirfd, ownerUid, AID_SYSTEM)
789 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
790 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
791 }
792 close(dirfd);
793 }
794 }
San Mehata1091cb2010-02-28 20:17:20 -0800795 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700796 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800797 }
San Mehat88705162010-01-15 09:26:28 -0800798
Kenny Rootcbacf782010-09-24 15:11:48 -0700799 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800800 return 0;
801}
802
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700803int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
804 char asecFileName[255];
805 char mountPoint[255];
806 bool cleanupDm = false;
807
808 if (!isLegalAsecId(id)) {
809 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
810 errno = EINVAL;
811 return -1;
812 }
813
814 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
815 SLOGE("Couldn't find ASEC %s", id);
816 return -1;
817 }
818
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700819 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700820 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
821 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
822 return -1;
823 }
824
825 if (isMountpointMounted(mountPoint)) {
826 SLOGE("ASEC %s mounted. Unmount before resizing", id);
827 errno = EBUSY;
828 return -1;
829 }
830
831 struct asec_superblock sb;
832 int fd;
833 unsigned int oldNumSec = 0;
834
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700835 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700836 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
837 return -1;
838 }
839
840 struct stat info;
841 if (fstat(fd, &info) < 0) {
842 SLOGE("Failed to get file size (%s)", strerror(errno));
843 close(fd);
844 return -1;
845 }
846
847 oldNumSec = info.st_size / 512;
848
849 unsigned numImgSectors;
850 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
851 numImgSectors = adjustSectorNumExt4(numSectors);
852 else
853 numImgSectors = adjustSectorNumFAT(numSectors);
854 /*
855 * add one block for the superblock
856 */
857 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700858 if (oldNumSec == numImgSectors + 1) {
859 SLOGW("Size unchanged; ignoring resize request");
860 return 0;
861 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700862 SLOGE("Only growing is currently supported.");
863 close(fd);
864 return -1;
865 }
866
867 /*
868 * Try to read superblock.
869 */
870 memset(&sb, 0, sizeof(struct asec_superblock));
871 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
872 SLOGE("lseek failed (%s)", strerror(errno));
873 close(fd);
874 return -1;
875 }
876 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
877 SLOGE("superblock read failed (%s)", strerror(errno));
878 close(fd);
879 return -1;
880 }
881 close(fd);
882
883 if (mDebug) {
884 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
885 }
886 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
887 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
888 errno = EMEDIUMTYPE;
889 return -1;
890 }
891
892 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
893 SLOGE("Only ext4 partitions are supported for resize");
894 errno = EINVAL;
895 return -1;
896 }
897
898 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
899 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
900 return -1;
901 }
902
903 /*
904 * Drop down a copy of the superblock at the end of the file
905 */
906 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
907 goto fail;
908
909 char idHash[33];
910 if (!asecHash(id, idHash, sizeof(idHash))) {
911 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
912 goto fail;
913 }
914
915 char loopDevice[255];
916 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
917 goto fail;
918
919 char dmDevice[255];
920
921 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
922 Loop::destroyByDevice(loopDevice);
923 goto fail;
924 }
925
926 /*
927 * Wait for the device mapper node to be created.
928 */
929 waitForDevMapper(dmDevice);
930
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700931 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700932 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
933 if (cleanupDm) {
934 Devmapper::destroy(idHash);
935 }
936 Loop::destroyByDevice(loopDevice);
937 goto fail;
938 }
939
940 return 0;
941fail:
942 Loop::resizeImageFile(asecFileName, oldNumSec);
943 return -1;
944}
945
San Mehata19b2502010-01-06 10:33:53 -0800946int VolumeManager::finalizeAsec(const char *id) {
947 char asecFileName[255];
948 char loopDevice[255];
949 char mountPoint[255];
950
Nick Kralevich0de7c612014-01-27 14:58:06 -0800951 if (!isLegalAsecId(id)) {
952 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
953 errno = EINVAL;
954 return -1;
955 }
956
Kenny Root344ca102012-04-03 17:23:01 -0700957 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
958 SLOGE("Couldn't find ASEC %s", id);
959 return -1;
960 }
San Mehata19b2502010-01-06 10:33:53 -0800961
San Mehatd9a4e352010-03-12 13:32:47 -0800962 char idHash[33];
963 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700964 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800965 return -1;
966 }
967
968 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700969 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800970 return -1;
971 }
972
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900973 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -0700974 struct asec_superblock sb;
975
976 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
977 return -1;
978 }
979
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700980 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400981 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
982 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
983 return -1;
984 }
Kenny Root344ca102012-04-03 17:23:01 -0700985
986 int result = 0;
987 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700988 result = android::vold::ext4::Mount(loopDevice, mountPoint,
989 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -0700990 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700991 result = android::vold::vfat::Mount(loopDevice, mountPoint,
992 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -0700993 }
994
995 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700996 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800997 return -1;
998 }
999
San Mehatd9a4e352010-03-12 13:32:47 -08001000 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001001 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001002 }
San Mehata19b2502010-01-06 10:33:53 -08001003 return 0;
1004}
1005
Kenny Root344ca102012-04-03 17:23:01 -07001006int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1007 char asecFileName[255];
1008 char loopDevice[255];
1009 char mountPoint[255];
1010
1011 if (gid < AID_APP) {
1012 SLOGE("Group ID is not in application range");
1013 return -1;
1014 }
1015
Nick Kralevich0de7c612014-01-27 14:58:06 -08001016 if (!isLegalAsecId(id)) {
1017 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1018 errno = EINVAL;
1019 return -1;
1020 }
1021
Kenny Root344ca102012-04-03 17:23:01 -07001022 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1023 SLOGE("Couldn't find ASEC %s", id);
1024 return -1;
1025 }
1026
1027 char idHash[33];
1028 if (!asecHash(id, idHash, sizeof(idHash))) {
1029 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1030 return -1;
1031 }
1032
1033 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1034 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1035 return -1;
1036 }
1037
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001038 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001039 struct asec_superblock sb;
1040
1041 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1042 return -1;
1043 }
1044
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001045 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001046 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1047 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1048 return -1;
1049 }
Kenny Root344ca102012-04-03 17:23:01 -07001050
1051 int result = 0;
1052 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1053 return 0;
1054 }
1055
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001056 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001057 false /* read-only */,
1058 true /* remount */,
1059 false /* executable */);
1060 if (ret) {
1061 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1062 return -1;
1063 }
1064
1065 char *paths[] = { mountPoint, NULL };
1066
1067 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1068 if (fts) {
1069 // Traverse the entire hierarchy and chown to system UID.
1070 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1071 // We don't care about the lost+found directory.
1072 if (!strcmp(ftsent->fts_name, "lost+found")) {
1073 continue;
1074 }
1075
1076 /*
1077 * There can only be one file marked as private right now.
1078 * This should be more robust, but it satisfies the requirements
1079 * we have for right now.
1080 */
1081 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1082
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001083 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001084 if (fd < 0) {
1085 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1086 result = -1;
1087 continue;
1088 }
1089
1090 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1091
1092 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001093 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001094 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001095 result |= fchmod(fd, privateFile ? 0640 : 0644);
1096 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001097
Stephen Smalley5093e612014-02-12 09:43:08 -05001098 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001099 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1100 result |= -1;
1101 }
1102
Kenny Root344ca102012-04-03 17:23:01 -07001103 close(fd);
1104 }
1105 fts_close(fts);
1106
1107 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001108 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001109 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1110 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1111 result |= -1;
1112 }
1113 close(dirfd);
1114 } else {
1115 result |= -1;
1116 }
1117
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001118 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001119 true /* read-only */,
1120 true /* remount */,
1121 true /* execute */);
1122
1123 if (result) {
1124 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1125 return -1;
1126 }
1127
1128 if (mDebug) {
1129 SLOGD("ASEC %s permissions fixed", id);
1130 }
1131 return 0;
1132}
1133
San Mehat048b0802010-01-23 08:17:06 -08001134int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001135 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001136 char *asecFilename2;
1137 char mountPoint[255];
1138
Kenny Root344ca102012-04-03 17:23:01 -07001139 const char *dir;
1140
Nick Kralevich0de7c612014-01-27 14:58:06 -08001141 if (!isLegalAsecId(id1)) {
1142 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1143 errno = EINVAL;
1144 return -1;
1145 }
1146
1147 if (!isLegalAsecId(id2)) {
1148 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1149 errno = EINVAL;
1150 return -1;
1151 }
1152
Kenny Root344ca102012-04-03 17:23:01 -07001153 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1154 SLOGE("Couldn't find ASEC %s", id1);
1155 return -1;
1156 }
1157
1158 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001159
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001160 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001161 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1162 SLOGE("Rename failed: couldn't construct mountpoint");
1163 goto out_err;
1164 }
1165
San Mehat048b0802010-01-23 08:17:06 -08001166 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001167 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001168 errno = EBUSY;
1169 goto out_err;
1170 }
1171
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001172 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001173 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1174 SLOGE("Rename failed: couldn't construct mountpoint2");
1175 goto out_err;
1176 }
1177
San Mehat96956ed2010-02-24 08:42:51 -08001178 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001179 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001180 errno = EBUSY;
1181 goto out_err;
1182 }
1183
San Mehat048b0802010-01-23 08:17:06 -08001184 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001185 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001186 errno = EADDRINUSE;
1187 goto out_err;
1188 }
1189
1190 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001191 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001192 goto out_err;
1193 }
1194
San Mehat048b0802010-01-23 08:17:06 -08001195 free(asecFilename2);
1196 return 0;
1197
1198out_err:
San Mehat048b0802010-01-23 08:17:06 -08001199 free(asecFilename2);
1200 return -1;
1201}
1202
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001203#define UNMOUNT_RETRIES 5
1204#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001205int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001206 char asecFileName[255];
1207 char mountPoint[255];
1208
Nick Kralevich0de7c612014-01-27 14:58:06 -08001209 if (!isLegalAsecId(id)) {
1210 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1211 errno = EINVAL;
1212 return -1;
1213 }
1214
Kenny Root344ca102012-04-03 17:23:01 -07001215 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1216 SLOGE("Couldn't find ASEC %s", id);
1217 return -1;
1218 }
1219
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001220 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001221 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1222 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1223 return -1;
1224 }
San Mehata19b2502010-01-06 10:33:53 -08001225
San Mehatd9a4e352010-03-12 13:32:47 -08001226 char idHash[33];
1227 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001228 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001229 return -1;
1230 }
1231
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001232 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1233}
1234
Kenny Root508c0e12010-07-12 09:59:49 -07001235int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001236 char mountPoint[255];
1237
1238 char idHash[33];
1239 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1240 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1241 return -1;
1242 }
1243
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001244 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001245 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1246 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1247 return -1;
1248 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001249
1250 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1251}
1252
1253int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1254 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001255 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001256 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001257 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001258 return -1;
1259 }
San Mehat23969932010-01-09 07:08:06 -08001260
San Mehatb78a32c2010-01-10 13:02:12 -08001261 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001262 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001263 rc = umount(mountPoint);
1264 if (!rc) {
1265 break;
San Mehata19b2502010-01-06 10:33:53 -08001266 }
San Mehatb78a32c2010-01-10 13:02:12 -08001267 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001268 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001269 rc = 0;
1270 break;
San Mehata19b2502010-01-06 10:33:53 -08001271 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001272 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001273 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001274
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001275 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001276
1277 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001278 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001279 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001280 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001281 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001282 }
San Mehat8c940ef2010-02-13 14:19:53 -08001283
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001284 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001285 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001286 }
1287
1288 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001289 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001290 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001291 return -1;
1292 }
1293
San Mehat12f4b892010-02-24 11:43:22 -08001294 int retries = 10;
1295
1296 while(retries--) {
1297 if (!rmdir(mountPoint)) {
1298 break;
1299 }
1300
San Mehat97ac40e2010-03-24 10:24:19 -07001301 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001302 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001303 }
1304
1305 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001306 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001307 }
San Mehat88705162010-01-15 09:26:28 -08001308
Paul Lawrence60dec162014-09-02 10:52:15 -07001309 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1310 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1311 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1312 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1313 continue;
1314 } else {
1315 break;
1316 }
San Mehata19b2502010-01-06 10:33:53 -08001317 }
1318
1319 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001320 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001321 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001322 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001323 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001324 }
San Mehat88705162010-01-15 09:26:28 -08001325
1326 AsecIdCollection::iterator it;
1327 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001328 ContainerData* cd = *it;
1329 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001330 free(*it);
1331 mActiveContainers->erase(it);
1332 break;
1333 }
1334 }
1335 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001336 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001337 }
San Mehatb78a32c2010-01-10 13:02:12 -08001338 return 0;
1339}
1340
San Mehat4ba89482010-02-18 09:00:18 -08001341int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001342 char asecFileName[255];
1343 char mountPoint[255];
1344
Nick Kralevich0de7c612014-01-27 14:58:06 -08001345 if (!isLegalAsecId(id)) {
1346 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1347 errno = EINVAL;
1348 return -1;
1349 }
1350
Kenny Root344ca102012-04-03 17:23:01 -07001351 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1352 SLOGE("Couldn't find ASEC %s", id);
1353 return -1;
1354 }
1355
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001356 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001357 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1358 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1359 return -1;
1360 }
San Mehatb78a32c2010-01-10 13:02:12 -08001361
San Mehat0586d542010-01-12 15:38:59 -08001362 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001363 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001364 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001365 }
San Mehat4ba89482010-02-18 09:00:18 -08001366 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001367 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001368 return -1;
1369 }
1370 }
San Mehata19b2502010-01-06 10:33:53 -08001371
San Mehat0586d542010-01-12 15:38:59 -08001372 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001373 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001374 return -1;
1375 }
San Mehata19b2502010-01-06 10:33:53 -08001376
San Mehatd9a4e352010-03-12 13:32:47 -08001377 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001378 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001379 }
San Mehata19b2502010-01-06 10:33:53 -08001380 return 0;
1381}
1382
Nick Kralevich0de7c612014-01-27 14:58:06 -08001383/*
1384 * Legal ASEC ids consist of alphanumeric characters, '-',
1385 * '_', or '.'. ".." is not allowed. The first or last character
1386 * of the ASEC id cannot be '.' (dot).
1387 */
1388bool VolumeManager::isLegalAsecId(const char *id) const {
1389 size_t i;
1390 size_t len = strlen(id);
1391
1392 if (len == 0) {
1393 return false;
1394 }
1395 if ((id[0] == '.') || (id[len - 1] == '.')) {
1396 return false;
1397 }
1398
1399 for (i = 0; i < len; i++) {
1400 if (id[i] == '.') {
1401 // i=0 is guaranteed never to have a dot. See above.
1402 if (id[i-1] == '.') return false;
1403 continue;
1404 }
1405 if (id[i] == '_' || id[i] == '-') continue;
1406 if (id[i] >= 'a' && id[i] <= 'z') continue;
1407 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1408 if (id[i] >= '0' && id[i] <= '9') continue;
1409 return false;
1410 }
1411
1412 return true;
1413}
1414
Kenny Root344ca102012-04-03 17:23:01 -07001415bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001416 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001417 if (dirfd < 0) {
1418 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001419 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001420 }
1421
Nick Kralevich25e581a2015-02-06 08:55:08 -08001422 struct stat sb;
1423 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1424 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001425
1426 close(dirfd);
1427
1428 return ret;
1429}
1430
1431int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1432 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001433 char *asecName;
1434
Nick Kralevich0de7c612014-01-27 14:58:06 -08001435 if (!isLegalAsecId(id)) {
1436 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1437 errno = EINVAL;
1438 return -1;
1439 }
1440
Kenny Root344ca102012-04-03 17:23:01 -07001441 if (asprintf(&asecName, "%s.asec", id) < 0) {
1442 SLOGE("Couldn't allocate string to write ASEC name");
1443 return -1;
1444 }
1445
1446 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001447 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1448 dir = VolumeManager::SEC_ASECDIR_INT;
1449 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1450 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001451 } else {
1452 free(asecName);
1453 return -1;
1454 }
1455
1456 if (directory != NULL) {
1457 *directory = dir;
1458 }
1459
1460 if (asecPath != NULL) {
1461 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001462 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1463 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001464 free(asecName);
1465 return -1;
1466 }
1467 }
1468
1469 free(asecName);
1470 return 0;
1471}
1472
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001473int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001474 char asecFileName[255];
1475 char mountPoint[255];
1476
Nick Kralevich0de7c612014-01-27 14:58:06 -08001477 if (!isLegalAsecId(id)) {
1478 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1479 errno = EINVAL;
1480 return -1;
1481 }
1482
Kenny Root344ca102012-04-03 17:23:01 -07001483 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1484 SLOGE("Couldn't find ASEC %s", id);
1485 return -1;
1486 }
1487
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001488 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001489 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001490 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001491 return -1;
1492 }
San Mehata19b2502010-01-06 10:33:53 -08001493
1494 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001495 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001496 errno = EBUSY;
1497 return -1;
1498 }
1499
San Mehatd9a4e352010-03-12 13:32:47 -08001500 char idHash[33];
1501 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001502 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001503 return -1;
1504 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001505
San Mehata19b2502010-01-06 10:33:53 -08001506 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001507 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1508 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001509
1510 char dmDevice[255];
1511 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001512
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001513 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001514 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001515
Kenny Root344ca102012-04-03 17:23:01 -07001516 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1517 return -1;
1518 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001519
San Mehatd9a4e352010-03-12 13:32:47 -08001520 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001521 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001522 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001523 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001524 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001525 Loop::destroyByDevice(loopDevice);
1526 errno = EMEDIUMTYPE;
1527 return -1;
1528 }
1529 nr_sec--; // We don't want the devmapping to extend onto our superblock
1530
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001531 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1532 Loop::destroyByDevice(loopDevice);
1533 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001534 }
1535
Kenny Root344ca102012-04-03 17:23:01 -07001536 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001537 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001538 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001539 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001540 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001541 }
1542 Loop::destroyByDevice(loopDevice);
1543 return -1;
1544 }
San Mehata19b2502010-01-06 10:33:53 -08001545 }
1546
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001547 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001548 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001549 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001550 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001551
Kenny Root344ca102012-04-03 17:23:01 -07001552 int result;
1553 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001554 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1555 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001556 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001557 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1558 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001559 }
1560
1561 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001562 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001563 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001564 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001565 }
1566 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001567 return -1;
1568 }
1569
Kenny Rootcbacf782010-09-24 15:11:48 -07001570 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001571 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001572 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001573 }
San Mehata19b2502010-01-06 10:33:53 -08001574 return 0;
1575}
1576
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001577/**
1578 * Mounts an image file <code>img</code>.
1579 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001580int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001581 char mountPoint[255];
1582
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001583 char idHash[33];
1584 if (!asecHash(img, idHash, sizeof(idHash))) {
1585 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1586 return -1;
1587 }
1588
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001589 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001590 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001591 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001592 return -1;
1593 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001594
1595 if (isMountpointMounted(mountPoint)) {
1596 SLOGE("Image %s already mounted", img);
1597 errno = EBUSY;
1598 return -1;
1599 }
1600
1601 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001602 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1603 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001604
1605 char dmDevice[255];
1606 bool cleanupDm = false;
1607 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001608 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001609
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001610 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001611 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1612 Loop::destroyByDevice(loopDevice);
1613 return -1;
1614 }
1615
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001616 get_blkdev_size(fd, &nr_sec);
1617 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001618 SLOGE("Failed to get loop size (%s)", strerror(errno));
1619 Loop::destroyByDevice(loopDevice);
1620 close(fd);
1621 return -1;
1622 }
1623
1624 close(fd);
1625
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001626 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001627 Loop::destroyByDevice(loopDevice);
1628 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001629 }
1630
1631 if (mkdir(mountPoint, 0755)) {
1632 if (errno != EEXIST) {
1633 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1634 if (cleanupDm) {
1635 Devmapper::destroy(idHash);
1636 }
1637 Loop::destroyByDevice(loopDevice);
1638 return -1;
1639 }
1640 }
1641
yoshiyuki hama476a6272015-01-28 16:37:23 +09001642 /*
1643 * Wait for the device mapper node to be created.
1644 */
1645 waitForDevMapper(dmDevice);
1646
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001647 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1648 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001649 SLOGE("Image mount failed (%s)", strerror(errno));
1650 if (cleanupDm) {
1651 Devmapper::destroy(idHash);
1652 }
1653 Loop::destroyByDevice(loopDevice);
1654 return -1;
1655 }
1656
Kenny Rootcbacf782010-09-24 15:11:48 -07001657 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001658 if (mDebug) {
1659 SLOGD("Image %s mounted", img);
1660 }
1661 return 0;
1662}
1663
Kenny Root508c0e12010-07-12 09:59:49 -07001664int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001665 FILE *fp = setmntent("/proc/mounts", "r");
1666 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001667 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1668 return -1;
1669 }
1670
1671 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001672 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001673 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001674 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001675 loopDir[loopDirLen++] = '/';
1676 loopDir[loopDirLen] = '\0';
1677
Yabin Cuid1104f72015-01-02 13:28:28 -08001678 mntent* mentry;
1679 while ((mentry = getmntent(fp)) != NULL) {
1680 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001681 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001682 if (fd >= 0) {
1683 struct loop_info64 li;
1684 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1685 cli->sendMsg(ResponseCode::AsecListResult,
1686 (const char*) li.lo_file_name, false);
1687 }
1688 close(fd);
1689 }
1690 }
1691 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001692 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001693 return 0;
1694}
1695
Jeff Sharkey9c484982015-03-31 10:35:33 -07001696extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001697 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001698 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001699}
1700
San Mehata19b2502010-01-06 10:33:53 -08001701bool VolumeManager::isMountpointMounted(const char *mp)
1702{
Yabin Cuid1104f72015-01-02 13:28:28 -08001703 FILE *fp = setmntent("/proc/mounts", "r");
1704 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001705 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001706 return false;
1707 }
1708
Yabin Cuid1104f72015-01-02 13:28:28 -08001709 bool found_mp = false;
1710 mntent* mentry;
1711 while ((mentry = getmntent(fp)) != NULL) {
1712 if (strcmp(mentry->mnt_dir, mp) == 0) {
1713 found_mp = true;
1714 break;
San Mehata19b2502010-01-06 10:33:53 -08001715 }
San Mehata19b2502010-01-06 10:33:53 -08001716 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001717 endmntent(fp);
1718 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001719}
1720
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001721int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001722 // Only offer to create directories for paths managed by vold
1723 if (strncmp(path, "/storage/", 9) == 0) {
1724 // fs_mkdirs() does symlink checking and relative path enforcement
1725 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001726 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001727 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001728 return -EINVAL;
1729 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001730}