blob: a46d43b85c7394d16c9a93808d97b6565890ea79 [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>
Elliott Hughes0e08e842017-05-18 09:08:24 -070029#include <sys/sysmacros.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070030#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080031#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080032
San Mehata2677e42009-12-13 10:40:18 -080033#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070034
35#define LOG_TAG "Vold"
36
Kenny Root7b18a7b2010-03-15 13:13:41 -070037#include <openssl/md5.h>
38
Elliott Hughes7e128fb2015-12-04 15:50:53 -080039#include <android-base/logging.h>
40#include <android-base/stringprintf.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070041#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070042#include <cutils/log.h>
43
Robert Craigb9e3ba52014-02-04 10:53:00 -050044#include <selinux/android.h>
45
San Mehatfd7f5872009-10-12 11:32:47 -070046#include <sysutils/NetlinkEvent.h>
47
Kenny Root344ca102012-04-03 17:23:01 -070048#include <private/android_filesystem_config.h>
49
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070050#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070052#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070053#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080054#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080055#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070056#include "fs/Ext4.h"
57#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070058#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080059#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080060#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080061#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090062#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070063#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080064
Mike Lockwood97f2fc12011-06-07 10:51:38 -070065#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
66
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070067#define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
68 + ((number) & (~((1U << (po2)) - 1))))
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070069
Jeff Sharkey36801cc2015-03-13 16:09:20 -070070using android::base::StringPrintf;
71
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070072/*
73 * Path to external storage where *only* root can access ASEC image files
74 */
75const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
76
77/*
78 * Path to internal storage where *only* root can access ASEC image files
79 */
80const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
81
82/*
83 * Path to where secure containers are mounted
84 */
85const char *VolumeManager::ASECDIR = "/mnt/asec";
86
87/*
88 * Path to where OBBs are mounted
89 */
90const char *VolumeManager::LOOPDIR = "/mnt/obb";
91
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060092static const char* kPathUserMount = "/mnt/user";
93static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
94
95static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
96
97/* 512MiB is large enough for testing purposes */
98static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070099
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700100static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +0800101static const unsigned int kMajorBlockExperimentalMin = 240;
102static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700103
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700104/* writes superblock at end of file or device given by name */
105static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700106 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700107 if (sbfd < 0) {
108 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
109 return -1;
110 }
111
112 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
113 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
114 close(sbfd);
115 return -1;
116 }
117
118 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
119 SLOGE("Failed to write superblock (%s)", strerror(errno));
120 close(sbfd);
121 return -1;
122 }
123 close(sbfd);
124 return 0;
125}
126
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200127static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700128 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
129 // preventing costly operations or unexpected ENOSPC error.
130 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200131 unsigned long clusterSectors = 4096 / 512;
132 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700133 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700134 return ROUND_UP_POWER_OF_2(numSectors, 3);
135}
136
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200137static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700138 /*
139 * Add some headroom
140 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200141 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700142 numSectors += fatSize + 2;
143 /*
144 * FAT is aligned to 32 kb with 512b sectors.
145 */
146 return ROUND_UP_POWER_OF_2(numSectors, 6);
147}
148
149static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
150 if (Loop::lookupActive(idHash, buffer, len)) {
151 if (Loop::create(idHash, asecFileName, buffer, len)) {
152 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
153 return -1;
154 }
155 if (debug) {
156 SLOGD("New loop device created at %s", buffer);
157 }
158 } else {
159 if (debug) {
160 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
161 }
162 }
163 return 0;
164}
165
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200166static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , unsigned long numImgSectors, bool* createdDMDevice, bool debug) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700167 if (strcmp(key, "none")) {
168 if (Devmapper::lookupActive(idHash, buffer, len)) {
169 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
170 buffer, len)) {
171 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
172 return -1;
173 }
174 if (debug) {
175 SLOGD("New devmapper instance created at %s", buffer);
176 }
177 } else {
178 if (debug) {
179 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
180 }
181 }
182 *createdDMDevice = true;
183 } else {
184 strcpy(buffer, loopDevice);
185 *createdDMDevice = false;
186 }
187 return 0;
188}
189
190static void waitForDevMapper(const char *dmDevice) {
191 /*
192 * Wait for the device mapper node to be created. Sometimes it takes a
193 * while. Wait for up to 1 second. We could also inspect incoming uevents,
194 * but that would take more effort.
195 */
196 int tries = 25;
197 while (tries--) {
198 if (!access(dmDevice, F_OK) || errno != ENOENT) {
199 break;
200 }
201 usleep(40 * 1000);
202 }
203}
204
San Mehatf1b736b2009-10-10 17:22:08 -0700205VolumeManager *VolumeManager::sInstance = NULL;
206
207VolumeManager *VolumeManager::Instance() {
208 if (!sInstance)
209 sInstance = new VolumeManager();
210 return sInstance;
211}
212
213VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800214 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800215 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700216 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400217 mUmsSharingCount = 0;
218 mSavedDirtyRatio = -1;
219 // set dirty ratio to 0 when UMS is active
220 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700221}
222
223VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800224 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700225}
226
Kenny Root7b18a7b2010-03-15 13:13:41 -0700227char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700228 static const char* digits = "0123456789abcdef";
229
Kenny Root7b18a7b2010-03-15 13:13:41 -0700230 unsigned char sig[MD5_DIGEST_LENGTH];
231
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700232 if (buffer == NULL) {
233 SLOGE("Destination buffer is NULL");
234 errno = ESPIPE;
235 return NULL;
236 } else if (id == NULL) {
237 SLOGE("Source buffer is NULL");
238 errno = ESPIPE;
239 return NULL;
240 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800241 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700242 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800243 errno = ESPIPE;
244 return NULL;
245 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700246
247 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800248
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700249 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700250 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700251 *p++ = digits[sig[i] >> 4];
252 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800253 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700254 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800255
256 return buffer;
257}
258
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600259int VolumeManager::updateVirtualDisk() {
260 if (property_get_bool(kPropVirtualDisk, false)) {
261 if (access(kPathVirtualDisk, F_OK) != 0) {
262 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
263 }
264
265 if (mVirtualDisk == nullptr) {
266 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
267 LOG(ERROR) << "Failed to create virtual disk";
268 return -1;
269 }
270
271 struct stat buf;
272 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
273 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
274 return -1;
275 }
276
277 auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
278 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
279 disk->create();
280 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
281 mDisks.push_back(mVirtualDisk);
282 }
283 } else {
284 if (mVirtualDisk != nullptr) {
285 dev_t device = mVirtualDisk->getDevice();
286
287 auto i = mDisks.begin();
288 while (i != mDisks.end()) {
289 if ((*i)->getDevice() == device) {
290 (*i)->destroy();
291 i = mDisks.erase(i);
292 } else {
293 ++i;
294 }
295 }
296
297 Loop::destroyByDevice(mVirtualDiskPath.c_str());
298 mVirtualDisk = nullptr;
299 }
300
301 if (access(kPathVirtualDisk, F_OK) == 0) {
302 unlink(kPathVirtualDisk);
303 }
304 }
305 return 0;
306}
307
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700308int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800309 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700310 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800311}
312
San Mehatf1b736b2009-10-10 17:22:08 -0700313int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700314 // Always start from a clean slate by unmounting everything in
315 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700316 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700317
318 // Assume that we always have an emulated volume on internal
319 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700320 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700321 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700322 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700323 mInternalEmulated->create();
324
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600325 // Consider creating a virtual disk
326 updateVirtualDisk();
327
San Mehatf1b736b2009-10-10 17:22:08 -0700328 return 0;
329}
330
331int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700332 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700333 mInternalEmulated->destroy();
334 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700335 return 0;
336}
337
San Mehatfd7f5872009-10-12 11:32:47 -0700338void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700339 std::lock_guard<std::mutex> lock(mLock);
340
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700341 if (mDebug) {
342 LOG(VERBOSE) << "----------------";
343 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
344 evt->dump();
345 }
San Mehatf1b736b2009-10-10 17:22:08 -0700346
Mateusz Nowak64403792015-08-03 16:39:19 +0200347 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
348 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700349
350 if (devType != "disk") return;
351
352 int major = atoi(evt->findParam("MAJOR"));
353 int minor = atoi(evt->findParam("MINOR"));
354 dev_t device = makedev(major, minor);
355
356 switch (evt->getAction()) {
357 case NetlinkEvent::Action::kAdd: {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700358 for (const auto& source : mDiskSources) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700359 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800360 // For now, assume that MMC and virtio-blk (the latter is
361 // emulator-specific; see Disk.cpp for details) devices are SD,
362 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700363 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800364 if (major == kMajorBlockMmc
365 || (android::vold::IsRunningInEmulator()
366 && major >= (int) kMajorBlockExperimentalMin
367 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700368 flags |= android::vold::Disk::Flags::kSd;
369 } else {
370 flags |= android::vold::Disk::Flags::kUsb;
371 }
372
373 auto disk = new android::vold::Disk(eventPath, device,
374 source->getNickname(), flags);
375 disk->create();
376 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
377 break;
378 }
379 }
380 break;
381 }
382 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700383 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700384 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700385 if (disk->getDevice() == device) {
386 disk->readMetadata();
387 disk->readPartitions();
388 }
389 }
390 break;
391 }
392 case NetlinkEvent::Action::kRemove: {
393 auto i = mDisks.begin();
394 while (i != mDisks.end()) {
395 if ((*i)->getDevice() == device) {
396 (*i)->destroy();
397 i = mDisks.erase(i);
398 } else {
399 ++i;
400 }
401 }
402 break;
403 }
404 default: {
405 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
406 break;
407 }
408 }
409}
410
411void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800412 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700413 mDiskSources.push_back(diskSource);
414}
415
416std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
417 for (auto disk : mDisks) {
418 if (disk->getId() == id) {
419 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700420 }
421 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700422 return nullptr;
423}
San Mehatf1b736b2009-10-10 17:22:08 -0700424
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700425std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800426 // Vold could receive "mount" after "shutdown" command in the extreme case.
427 // If this happens, mInternalEmulated will equal nullptr and
428 // we need to deal with it in order to avoid null pointer crash.
429 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700430 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700431 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700432 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700433 auto vol = disk->findVolume(id);
434 if (vol != nullptr) {
435 return vol;
436 }
437 }
438 return nullptr;
439}
440
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700441void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
442 std::list<std::string>& list) {
443 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700444 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700445 disk->listVolumes(type, list);
446 }
447}
448
449nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700450 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700451 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700452 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700453 } else {
454 auto vol = findVolume(id);
455 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
456 path = vol->getPath();
457 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700458 }
459
460 if (path.empty()) {
461 LOG(WARNING) << "Failed to find volume for " << id;
462 return -1;
463 }
464
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700465 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700466}
467
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700468int VolumeManager::forgetPartition(const std::string& partGuid) {
469 std::string normalizedGuid;
470 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
471 LOG(WARNING) << "Invalid GUID " << partGuid;
472 return -1;
473 }
474
475 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
476 if (unlink(keyPath.c_str()) != 0) {
477 LOG(ERROR) << "Failed to unlink " << keyPath;
478 return -1;
479 }
480
481 return 0;
482}
483
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700484int VolumeManager::linkPrimary(userid_t userId) {
485 std::string source(mPrimary->getPath());
486 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
487 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700488 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700489 }
490
491 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
492 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
493 if (errno != ENOENT) {
494 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
495 }
496 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700497 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700498 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
499 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
500 strerror(errno));
501 return -errno;
502 }
503 return 0;
504}
505
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700506int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
507 mAddedUsers[userId] = userSerialNumber;
508 return 0;
509}
510
511int VolumeManager::onUserRemoved(userid_t userId) {
512 mAddedUsers.erase(userId);
513 return 0;
514}
515
516int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700517 // Note that sometimes the system will spin up processes from Zygote
518 // before actually starting the user, so we're okay if Zygote
519 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600520 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700521 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
522
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700523 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700524 if (mPrimary) {
525 linkPrimary(userId);
526 }
527 return 0;
528}
529
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700530int VolumeManager::onUserStopped(userid_t userId) {
531 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700532 return 0;
533}
534
535int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
536 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700537 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700538 linkPrimary(userId);
539 }
540 return 0;
541}
542
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700543static int unmount_tree(const char* path) {
544 size_t path_len = strlen(path);
545
546 FILE* fp = setmntent("/proc/mounts", "r");
547 if (fp == NULL) {
548 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
549 return -errno;
550 }
551
552 // Some volumes can be stacked on each other, so force unmount in
553 // reverse order to give us the best chance of success.
554 std::list<std::string> toUnmount;
555 mntent* mentry;
556 while ((mentry = getmntent(fp)) != NULL) {
557 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
558 toUnmount.push_front(std::string(mentry->mnt_dir));
559 }
560 }
561 endmntent(fp);
562
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700563 for (const auto& path : toUnmount) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700564 if (umount2(path.c_str(), MNT_DETACH)) {
565 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
566 }
567 }
568 return 0;
569}
570
Jeff Sharkey66270a22015-06-24 11:49:24 -0700571int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
572 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
573
574 DIR* dir;
575 struct dirent* de;
576 char rootName[PATH_MAX];
577 char pidName[PATH_MAX];
578 int pidFd;
579 int nsFd;
580 struct stat sb;
581 pid_t child;
582
583 if (!(dir = opendir("/proc"))) {
584 PLOG(ERROR) << "Failed to opendir";
585 return -1;
586 }
587
588 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900589 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700590 PLOG(ERROR) << "Failed to readlink";
591 closedir(dir);
592 return -1;
593 }
594
595 // Poke through all running PIDs look for apps running as UID
596 while ((de = readdir(dir))) {
597 pidFd = -1;
598 nsFd = -1;
599
600 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
601 if (pidFd < 0) {
602 goto next;
603 }
604 if (fstat(pidFd, &sb) != 0) {
605 PLOG(WARNING) << "Failed to stat " << de->d_name;
606 goto next;
607 }
608 if (sb.st_uid != uid) {
609 goto next;
610 }
611
612 // Matches so far, but refuse to touch if in root namespace
613 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900614 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700615 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
616 goto next;
617 }
618 if (!strcmp(rootName, pidName)) {
619 LOG(WARNING) << "Skipping due to root namespace";
620 goto next;
621 }
622
623 // We purposefully leave the namespace open across the fork
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600624 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700625 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700626 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700627 goto next;
628 }
629
630 if (!(child = fork())) {
631 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700632 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700633 _exit(1);
634 }
635
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700636 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700637
638 std::string storageSource;
639 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700640 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700641 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700642 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700643 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700644 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700645 } else {
646 // Sane default of no storage visible
647 _exit(0);
648 }
649 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
Hidehiko Abe674bed12016-03-09 16:42:10 +0900650 NULL, MS_BIND | MS_REC, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700651 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
652 << de->d_name;
653 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700654 }
Hidehiko Abe674bed12016-03-09 16:42:10 +0900655 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
656 MS_REC | MS_SLAVE, NULL)) == -1) {
657 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
658 << de->d_name;
659 _exit(1);
660 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700661
662 // Mount user-specific symlink helper into place
663 userid_t user_id = multiuser_get_user_id(uid);
664 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
665 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
666 NULL, MS_BIND, NULL)) == -1) {
667 PLOG(ERROR) << "Failed to mount " << userSource << " for "
668 << de->d_name;
669 _exit(1);
670 }
671
Jeff Sharkey66270a22015-06-24 11:49:24 -0700672 _exit(0);
673 }
674
675 if (child == -1) {
676 PLOG(ERROR) << "Failed to fork";
677 goto next;
678 } else {
679 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
680 }
681
682next:
683 close(nsFd);
684 close(pidFd);
685 }
686 closedir(dir);
687 return 0;
688}
689
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700690int VolumeManager::reset() {
691 // Tear down all existing disks/volumes and start from a blank slate so
692 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800693 if (mInternalEmulated != nullptr) {
694 mInternalEmulated->destroy();
695 mInternalEmulated->create();
696 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700697 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700698 disk->destroy();
699 disk->create();
700 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600701 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700702 mAddedUsers.clear();
703 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700704 return 0;
705}
706
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700707// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700708int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700709 if (mInternalEmulated == nullptr) {
710 return 0; // already shutdown
711 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700712 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700713 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700714 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700715 disk->destroy();
716 }
717 mDisks.clear();
718 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700719}
720
Jeff Sharkey9c484982015-03-31 10:35:33 -0700721int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700722 std::lock_guard<std::mutex> lock(mLock);
723
Jeff Sharkey9c484982015-03-31 10:35:33 -0700724 // First, try gracefully unmounting all known devices
725 if (mInternalEmulated != nullptr) {
726 mInternalEmulated->unmount();
727 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700728 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700729 disk->unmountAll();
730 }
731
732 // Worst case we might have some stale mounts lurking around, so
733 // force unmount those just to be safe.
734 FILE* fp = setmntent("/proc/mounts", "r");
735 if (fp == NULL) {
736 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
737 return -errno;
738 }
739
740 // Some volumes can be stacked on each other, so force unmount in
741 // reverse order to give us the best chance of success.
742 std::list<std::string> toUnmount;
743 mntent* mentry;
744 while ((mentry = getmntent(fp)) != NULL) {
745 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
746 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
747 toUnmount.push_front(std::string(mentry->mnt_dir));
748 }
749 }
750 endmntent(fp);
751
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700752 for (const auto& path : toUnmount) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700753 SLOGW("Tearing down stale mount %s", path.c_str());
754 android::vold::ForceUnmount(path);
755 }
756
757 return 0;
758}
759
Kenny Root508c0e12010-07-12 09:59:49 -0700760int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
761 char idHash[33];
762 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
763 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
764 return -1;
765 }
766
767 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700768 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400769 if ((written < 0) || (written >= mountPathLen)) {
770 errno = EINVAL;
771 return -1;
772 }
Kenny Root508c0e12010-07-12 09:59:49 -0700773
774 if (access(mountPath, F_OK)) {
775 errno = ENOENT;
776 return -1;
777 }
778
779 return 0;
780}
781
San Mehata19b2502010-01-06 10:33:53 -0800782int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700783 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700784
Nick Kralevich0de7c612014-01-27 14:58:06 -0800785 if (!isLegalAsecId(id)) {
786 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
787 errno = EINVAL;
788 return -1;
789 }
790
Kenny Root344ca102012-04-03 17:23:01 -0700791 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
792 SLOGE("Couldn't find ASEC %s", id);
793 return -1;
794 }
San Mehat88ac2c02010-03-23 11:15:58 -0700795
796 memset(buffer, 0, maxlen);
797 if (access(asecFileName, F_OK)) {
798 errno = ENOENT;
799 return -1;
800 }
San Mehata19b2502010-01-06 10:33:53 -0800801
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700802 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400803 if ((written < 0) || (written >= maxlen)) {
804 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
805 errno = EINVAL;
806 return -1;
807 }
808
San Mehata19b2502010-01-06 10:33:53 -0800809 return 0;
810}
811
Dianne Hackborn736910c2011-06-27 13:37:07 -0700812int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
813 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700814
Nick Kralevich0de7c612014-01-27 14:58:06 -0800815 if (!isLegalAsecId(id)) {
816 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
817 errno = EINVAL;
818 return -1;
819 }
820
Kenny Root344ca102012-04-03 17:23:01 -0700821 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
822 SLOGE("Couldn't find ASEC %s", id);
823 return -1;
824 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700825
826 memset(buffer, 0, maxlen);
827 if (access(asecFileName, F_OK)) {
828 errno = ENOENT;
829 return -1;
830 }
831
rpcraigd1c226f2012-10-09 06:58:16 -0400832 int written = snprintf(buffer, maxlen, "%s", asecFileName);
833 if ((written < 0) || (written >= maxlen)) {
834 errno = EINVAL;
835 return -1;
836 }
837
Dianne Hackborn736910c2011-06-27 13:37:07 -0700838 return 0;
839}
840
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200841int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700842 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800843 struct asec_superblock sb;
844 memset(&sb, 0, sizeof(sb));
845
Nick Kralevich0de7c612014-01-27 14:58:06 -0800846 if (!isLegalAsecId(id)) {
847 SLOGE("createAsec: Invalid asec id \"%s\"", id);
848 errno = EINVAL;
849 return -1;
850 }
851
Kenny Root344ca102012-04-03 17:23:01 -0700852 const bool wantFilesystem = strcmp(fstype, "none");
853 bool usingExt4 = false;
854 if (wantFilesystem) {
855 usingExt4 = !strcmp(fstype, "ext4");
856 if (usingExt4) {
857 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
858 } else if (strcmp(fstype, "fat")) {
859 SLOGE("Invalid filesystem type %s", fstype);
860 errno = EINVAL;
861 return -1;
862 }
863 }
864
San Mehatfcf24fe2010-03-03 12:37:32 -0800865 sb.magic = ASEC_SB_MAGIC;
866 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800867
San Mehatd31e3802010-02-18 08:37:45 -0800868 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200869 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800870 errno = EINVAL;
871 return -1;
872 }
873
San Mehata19b2502010-01-06 10:33:53 -0800874 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700875
876 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
877 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
878 asecFileName, strerror(errno));
879 errno = EADDRINUSE;
880 return -1;
881 }
882
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700883 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700884
rpcraigd1c226f2012-10-09 06:58:16 -0400885 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
886 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
887 errno = EINVAL;
888 return -1;
889 }
San Mehata19b2502010-01-06 10:33:53 -0800890
891 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700892 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700893 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800894 errno = EADDRINUSE;
895 return -1;
896 }
897
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200898 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700899 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700900 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700901 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700902 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800903
904 // Add +1 for our superblock which is at the end
905 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700906 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800907 return -1;
908 }
909
San Mehatd9a4e352010-03-12 13:32:47 -0800910 char idHash[33];
911 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700912 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800913 unlink(asecFileName);
914 return -1;
915 }
916
San Mehata19b2502010-01-06 10:33:53 -0800917 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800918 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700919 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800920 unlink(asecFileName);
921 return -1;
922 }
923
San Mehatb78a32c2010-01-10 13:02:12 -0800924 char dmDevice[255];
925 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800926
San Mehatb78a32c2010-01-10 13:02:12 -0800927 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800928 // XXX: This is all we support for now
929 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800930 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800931 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700932 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800933 Loop::destroyByDevice(loopDevice);
934 unlink(asecFileName);
935 return -1;
936 }
937 cleanupDm = true;
938 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800939 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800940 strcpy(dmDevice, loopDevice);
941 }
942
San Mehatfcf24fe2010-03-03 12:37:32 -0800943 /*
944 * Drop down the superblock at the end of the file
945 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700946 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800947 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800948 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800949 }
950 Loop::destroyByDevice(loopDevice);
951 unlink(asecFileName);
952 return -1;
953 }
954
Kenny Root344ca102012-04-03 17:23:01 -0700955 if (wantFilesystem) {
956 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400957 char mountPoint[255];
958
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700959 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400960 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
961 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
962 if (cleanupDm) {
963 Devmapper::destroy(idHash);
964 }
965 Loop::destroyByDevice(loopDevice);
966 unlink(asecFileName);
967 return -1;
968 }
rpcraiga54e13a2012-09-21 14:17:08 -0400969
Kenny Root344ca102012-04-03 17:23:01 -0700970 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700971 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700972 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700973 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800974 }
San Mehata19b2502010-01-06 10:33:53 -0800975
Kenny Root344ca102012-04-03 17:23:01 -0700976 if (formatStatus < 0) {
977 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800978 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800979 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800980 }
San Mehateb13a902010-01-07 12:12:50 -0800981 Loop::destroyByDevice(loopDevice);
982 unlink(asecFileName);
983 return -1;
984 }
Kenny Root344ca102012-04-03 17:23:01 -0700985
Kenny Root344ca102012-04-03 17:23:01 -0700986 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800987 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700988 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800989 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800990 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800991 }
992 Loop::destroyByDevice(loopDevice);
993 unlink(asecFileName);
994 return -1;
995 }
San Mehatb78a32c2010-01-10 13:02:12 -0800996 }
San Mehata1091cb2010-02-28 20:17:20 -0800997
Kenny Root344ca102012-04-03 17:23:01 -0700998 int mountStatus;
999 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001000 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
1001 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -07001002 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001003 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
1004 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -07001005 }
1006
1007 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -07001008 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -08001009 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001010 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001011 }
1012 Loop::destroyByDevice(loopDevice);
1013 unlink(asecFileName);
1014 return -1;
1015 }
Kenny Root344ca102012-04-03 17:23:01 -07001016
1017 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001018 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001019 if (dirfd >= 0) {
1020 if (fchown(dirfd, ownerUid, AID_SYSTEM)
1021 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
1022 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
1023 }
1024 close(dirfd);
1025 }
1026 }
San Mehata1091cb2010-02-28 20:17:20 -08001027 } else {
San Mehat97ac40e2010-03-24 10:24:19 -07001028 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -08001029 }
San Mehat88705162010-01-15 09:26:28 -08001030
Kenny Rootcbacf782010-09-24 15:11:48 -07001031 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -08001032 return 0;
1033}
1034
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001035int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001036 char asecFileName[255];
1037 char mountPoint[255];
1038 bool cleanupDm = false;
1039
1040 if (!isLegalAsecId(id)) {
1041 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
1042 errno = EINVAL;
1043 return -1;
1044 }
1045
1046 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1047 SLOGE("Couldn't find ASEC %s", id);
1048 return -1;
1049 }
1050
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001051 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001052 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1053 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
1054 return -1;
1055 }
1056
1057 if (isMountpointMounted(mountPoint)) {
1058 SLOGE("ASEC %s mounted. Unmount before resizing", id);
1059 errno = EBUSY;
1060 return -1;
1061 }
1062
1063 struct asec_superblock sb;
1064 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001065 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001066
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001067 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001068 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
1069 return -1;
1070 }
1071
1072 struct stat info;
1073 if (fstat(fd, &info) < 0) {
1074 SLOGE("Failed to get file size (%s)", strerror(errno));
1075 close(fd);
1076 return -1;
1077 }
1078
1079 oldNumSec = info.st_size / 512;
1080
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001081 /*
1082 * Try to read superblock.
1083 */
1084 memset(&sb, 0, sizeof(struct asec_superblock));
1085 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1086 SLOGE("lseek failed (%s)", strerror(errno));
1087 close(fd);
1088 return -1;
1089 }
1090 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1091 SLOGE("superblock read failed (%s)", strerror(errno));
1092 close(fd);
1093 return -1;
1094 }
1095 close(fd);
1096
1097 if (mDebug) {
1098 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1099 }
1100 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1101 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1102 errno = EMEDIUMTYPE;
1103 return -1;
1104 }
1105
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001106 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001107 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1108 SLOGE("Only ext4 partitions are supported for resize");
1109 errno = EINVAL;
1110 return -1;
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001111 } else {
1112 numImgSectors = adjustSectorNumExt4(numSectors);
1113 }
1114
1115 /*
1116 * add one block for the superblock
1117 */
1118 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1119 if (oldNumSec == numImgSectors + 1) {
1120 SLOGW("Size unchanged; ignoring resize request");
1121 return 0;
1122 } else if (oldNumSec > numImgSectors + 1) {
1123 SLOGE("Only growing is currently supported.");
1124 close(fd);
1125 return -1;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001126 }
1127
1128 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1129 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1130 return -1;
1131 }
1132
1133 /*
1134 * Drop down a copy of the superblock at the end of the file
1135 */
1136 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1137 goto fail;
1138
1139 char idHash[33];
1140 if (!asecHash(id, idHash, sizeof(idHash))) {
1141 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1142 goto fail;
1143 }
1144
1145 char loopDevice[255];
1146 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1147 goto fail;
1148
1149 char dmDevice[255];
1150
1151 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1152 Loop::destroyByDevice(loopDevice);
1153 goto fail;
1154 }
1155
1156 /*
1157 * Wait for the device mapper node to be created.
1158 */
1159 waitForDevMapper(dmDevice);
1160
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001161 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001162 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1163 if (cleanupDm) {
1164 Devmapper::destroy(idHash);
1165 }
1166 Loop::destroyByDevice(loopDevice);
1167 goto fail;
1168 }
1169
1170 return 0;
1171fail:
1172 Loop::resizeImageFile(asecFileName, oldNumSec);
1173 return -1;
1174}
1175
San Mehata19b2502010-01-06 10:33:53 -08001176int VolumeManager::finalizeAsec(const char *id) {
1177 char asecFileName[255];
1178 char loopDevice[255];
1179 char mountPoint[255];
1180
Nick Kralevich0de7c612014-01-27 14:58:06 -08001181 if (!isLegalAsecId(id)) {
1182 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1183 errno = EINVAL;
1184 return -1;
1185 }
1186
Kenny Root344ca102012-04-03 17:23:01 -07001187 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1188 SLOGE("Couldn't find ASEC %s", id);
1189 return -1;
1190 }
San Mehata19b2502010-01-06 10:33:53 -08001191
San Mehatd9a4e352010-03-12 13:32:47 -08001192 char idHash[33];
1193 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001194 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001195 return -1;
1196 }
1197
1198 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001199 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001200 return -1;
1201 }
1202
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001203 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001204 struct asec_superblock sb;
1205
1206 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1207 return -1;
1208 }
1209
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001210 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001211 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1212 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1213 return -1;
1214 }
Kenny Root344ca102012-04-03 17:23:01 -07001215
1216 int result = 0;
1217 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001218 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1219 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001220 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001221 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1222 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001223 }
1224
1225 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001226 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001227 return -1;
1228 }
1229
San Mehatd9a4e352010-03-12 13:32:47 -08001230 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001231 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001232 }
San Mehata19b2502010-01-06 10:33:53 -08001233 return 0;
1234}
1235
Kenny Root344ca102012-04-03 17:23:01 -07001236int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1237 char asecFileName[255];
1238 char loopDevice[255];
1239 char mountPoint[255];
1240
1241 if (gid < AID_APP) {
1242 SLOGE("Group ID is not in application range");
1243 return -1;
1244 }
1245
Nick Kralevich0de7c612014-01-27 14:58:06 -08001246 if (!isLegalAsecId(id)) {
1247 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1248 errno = EINVAL;
1249 return -1;
1250 }
1251
Kenny Root344ca102012-04-03 17:23:01 -07001252 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1253 SLOGE("Couldn't find ASEC %s", id);
1254 return -1;
1255 }
1256
1257 char idHash[33];
1258 if (!asecHash(id, idHash, sizeof(idHash))) {
1259 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1260 return -1;
1261 }
1262
1263 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1264 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1265 return -1;
1266 }
1267
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001268 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001269 struct asec_superblock sb;
1270
1271 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1272 return -1;
1273 }
1274
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001275 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001276 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1277 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1278 return -1;
1279 }
Kenny Root344ca102012-04-03 17:23:01 -07001280
1281 int result = 0;
1282 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1283 return 0;
1284 }
1285
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001286 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001287 false /* read-only */,
1288 true /* remount */,
1289 false /* executable */);
1290 if (ret) {
1291 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1292 return -1;
1293 }
1294
1295 char *paths[] = { mountPoint, NULL };
1296
1297 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1298 if (fts) {
1299 // Traverse the entire hierarchy and chown to system UID.
1300 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1301 // We don't care about the lost+found directory.
1302 if (!strcmp(ftsent->fts_name, "lost+found")) {
1303 continue;
1304 }
1305
1306 /*
1307 * There can only be one file marked as private right now.
1308 * This should be more robust, but it satisfies the requirements
1309 * we have for right now.
1310 */
1311 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1312
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001313 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001314 if (fd < 0) {
1315 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1316 result = -1;
1317 continue;
1318 }
1319
1320 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1321
1322 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001323 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001324 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001325 result |= fchmod(fd, privateFile ? 0640 : 0644);
1326 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001327
Stephen Smalley5093e612014-02-12 09:43:08 -05001328 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001329 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1330 result |= -1;
1331 }
1332
Kenny Root344ca102012-04-03 17:23:01 -07001333 close(fd);
1334 }
1335 fts_close(fts);
1336
1337 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001338 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001339 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1340 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1341 result |= -1;
1342 }
1343 close(dirfd);
1344 } else {
1345 result |= -1;
1346 }
1347
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001348 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001349 true /* read-only */,
1350 true /* remount */,
1351 true /* execute */);
1352
1353 if (result) {
1354 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1355 return -1;
1356 }
1357
1358 if (mDebug) {
1359 SLOGD("ASEC %s permissions fixed", id);
1360 }
1361 return 0;
1362}
1363
San Mehat048b0802010-01-23 08:17:06 -08001364int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001365 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001366 char *asecFilename2;
1367 char mountPoint[255];
1368
Kenny Root344ca102012-04-03 17:23:01 -07001369 const char *dir;
1370
Nick Kralevich0de7c612014-01-27 14:58:06 -08001371 if (!isLegalAsecId(id1)) {
1372 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1373 errno = EINVAL;
1374 return -1;
1375 }
1376
1377 if (!isLegalAsecId(id2)) {
1378 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1379 errno = EINVAL;
1380 return -1;
1381 }
1382
Kenny Root344ca102012-04-03 17:23:01 -07001383 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1384 SLOGE("Couldn't find ASEC %s", id1);
1385 return -1;
1386 }
1387
1388 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001389
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001390 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001391 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1392 SLOGE("Rename failed: couldn't construct mountpoint");
1393 goto out_err;
1394 }
1395
San Mehat048b0802010-01-23 08:17:06 -08001396 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001397 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001398 errno = EBUSY;
1399 goto out_err;
1400 }
1401
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001402 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001403 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1404 SLOGE("Rename failed: couldn't construct mountpoint2");
1405 goto out_err;
1406 }
1407
San Mehat96956ed2010-02-24 08:42:51 -08001408 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001409 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001410 errno = EBUSY;
1411 goto out_err;
1412 }
1413
San Mehat048b0802010-01-23 08:17:06 -08001414 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001415 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001416 errno = EADDRINUSE;
1417 goto out_err;
1418 }
1419
1420 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001421 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001422 goto out_err;
1423 }
1424
San Mehat048b0802010-01-23 08:17:06 -08001425 free(asecFilename2);
1426 return 0;
1427
1428out_err:
San Mehat048b0802010-01-23 08:17:06 -08001429 free(asecFilename2);
1430 return -1;
1431}
1432
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001433#define UNMOUNT_RETRIES 5
1434#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001435int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001436 char asecFileName[255];
1437 char mountPoint[255];
1438
Nick Kralevich0de7c612014-01-27 14:58:06 -08001439 if (!isLegalAsecId(id)) {
1440 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1441 errno = EINVAL;
1442 return -1;
1443 }
1444
Kenny Root344ca102012-04-03 17:23:01 -07001445 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1446 SLOGE("Couldn't find ASEC %s", id);
1447 return -1;
1448 }
1449
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001450 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001451 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1452 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1453 return -1;
1454 }
San Mehata19b2502010-01-06 10:33:53 -08001455
San Mehatd9a4e352010-03-12 13:32:47 -08001456 char idHash[33];
1457 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001458 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001459 return -1;
1460 }
1461
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001462 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1463}
1464
Kenny Root508c0e12010-07-12 09:59:49 -07001465int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001466 char mountPoint[255];
1467
1468 char idHash[33];
1469 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1470 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1471 return -1;
1472 }
1473
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001474 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001475 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1476 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1477 return -1;
1478 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001479
1480 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1481}
1482
1483int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1484 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001485 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001486 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001487 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001488 return -1;
1489 }
San Mehat23969932010-01-09 07:08:06 -08001490
San Mehatb78a32c2010-01-10 13:02:12 -08001491 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001492 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001493 rc = umount(mountPoint);
1494 if (!rc) {
1495 break;
San Mehata19b2502010-01-06 10:33:53 -08001496 }
San Mehatb78a32c2010-01-10 13:02:12 -08001497 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001498 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001499 rc = 0;
1500 break;
San Mehata19b2502010-01-06 10:33:53 -08001501 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001502 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001503 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001504
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001505 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001506
1507 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001508 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001509 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001510 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001511 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001512 }
San Mehat8c940ef2010-02-13 14:19:53 -08001513
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001514 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001515 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001516 }
1517
1518 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001519 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001520 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001521 return -1;
1522 }
1523
San Mehat12f4b892010-02-24 11:43:22 -08001524 int retries = 10;
1525
1526 while(retries--) {
1527 if (!rmdir(mountPoint)) {
1528 break;
1529 }
1530
San Mehat97ac40e2010-03-24 10:24:19 -07001531 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001532 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001533 }
1534
1535 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001536 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001537 }
San Mehat88705162010-01-15 09:26:28 -08001538
Paul Lawrence60dec162014-09-02 10:52:15 -07001539 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1540 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1541 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1542 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1543 continue;
1544 } else {
1545 break;
1546 }
San Mehata19b2502010-01-06 10:33:53 -08001547 }
1548
1549 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001550 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001551 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001552 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001553 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001554 }
San Mehat88705162010-01-15 09:26:28 -08001555
1556 AsecIdCollection::iterator it;
1557 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001558 ContainerData* cd = *it;
1559 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001560 free(*it);
1561 mActiveContainers->erase(it);
1562 break;
1563 }
1564 }
1565 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001566 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001567 }
San Mehatb78a32c2010-01-10 13:02:12 -08001568 return 0;
1569}
1570
San Mehat4ba89482010-02-18 09:00:18 -08001571int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001572 char asecFileName[255];
1573 char mountPoint[255];
1574
Nick Kralevich0de7c612014-01-27 14:58:06 -08001575 if (!isLegalAsecId(id)) {
1576 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1577 errno = EINVAL;
1578 return -1;
1579 }
1580
Kenny Root344ca102012-04-03 17:23:01 -07001581 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1582 SLOGE("Couldn't find ASEC %s", id);
1583 return -1;
1584 }
1585
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001586 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001587 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1588 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1589 return -1;
1590 }
San Mehatb78a32c2010-01-10 13:02:12 -08001591
San Mehat0586d542010-01-12 15:38:59 -08001592 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001593 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001594 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001595 }
San Mehat4ba89482010-02-18 09:00:18 -08001596 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001597 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001598 return -1;
1599 }
1600 }
San Mehata19b2502010-01-06 10:33:53 -08001601
San Mehat0586d542010-01-12 15:38:59 -08001602 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001603 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001604 return -1;
1605 }
San Mehata19b2502010-01-06 10:33:53 -08001606
San Mehatd9a4e352010-03-12 13:32:47 -08001607 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001608 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001609 }
San Mehata19b2502010-01-06 10:33:53 -08001610 return 0;
1611}
1612
Nick Kralevich0de7c612014-01-27 14:58:06 -08001613/*
1614 * Legal ASEC ids consist of alphanumeric characters, '-',
1615 * '_', or '.'. ".." is not allowed. The first or last character
1616 * of the ASEC id cannot be '.' (dot).
1617 */
1618bool VolumeManager::isLegalAsecId(const char *id) const {
1619 size_t i;
1620 size_t len = strlen(id);
1621
1622 if (len == 0) {
1623 return false;
1624 }
1625 if ((id[0] == '.') || (id[len - 1] == '.')) {
1626 return false;
1627 }
1628
1629 for (i = 0; i < len; i++) {
1630 if (id[i] == '.') {
1631 // i=0 is guaranteed never to have a dot. See above.
1632 if (id[i-1] == '.') return false;
1633 continue;
1634 }
1635 if (id[i] == '_' || id[i] == '-') continue;
1636 if (id[i] >= 'a' && id[i] <= 'z') continue;
1637 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1638 if (id[i] >= '0' && id[i] <= '9') continue;
1639 return false;
1640 }
1641
1642 return true;
1643}
1644
Kenny Root344ca102012-04-03 17:23:01 -07001645bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001646 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001647 if (dirfd < 0) {
1648 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001649 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001650 }
1651
Nick Kralevich25e581a2015-02-06 08:55:08 -08001652 struct stat sb;
1653 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1654 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001655
1656 close(dirfd);
1657
1658 return ret;
1659}
1660
1661int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1662 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001663 char *asecName;
1664
Nick Kralevich0de7c612014-01-27 14:58:06 -08001665 if (!isLegalAsecId(id)) {
1666 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1667 errno = EINVAL;
1668 return -1;
1669 }
1670
Kenny Root344ca102012-04-03 17:23:01 -07001671 if (asprintf(&asecName, "%s.asec", id) < 0) {
1672 SLOGE("Couldn't allocate string to write ASEC name");
1673 return -1;
1674 }
1675
1676 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001677 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1678 dir = VolumeManager::SEC_ASECDIR_INT;
1679 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1680 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001681 } else {
1682 free(asecName);
1683 return -1;
1684 }
1685
1686 if (directory != NULL) {
1687 *directory = dir;
1688 }
1689
1690 if (asecPath != NULL) {
1691 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001692 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1693 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001694 free(asecName);
1695 return -1;
1696 }
1697 }
1698
1699 free(asecName);
1700 return 0;
1701}
1702
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001703int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001704 char asecFileName[255];
1705 char mountPoint[255];
1706
Nick Kralevich0de7c612014-01-27 14:58:06 -08001707 if (!isLegalAsecId(id)) {
1708 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1709 errno = EINVAL;
1710 return -1;
1711 }
1712
Kenny Root344ca102012-04-03 17:23:01 -07001713 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1714 SLOGE("Couldn't find ASEC %s", id);
1715 return -1;
1716 }
1717
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001718 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001719 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001720 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001721 return -1;
1722 }
San Mehata19b2502010-01-06 10:33:53 -08001723
1724 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001725 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001726 errno = EBUSY;
1727 return -1;
1728 }
1729
San Mehatd9a4e352010-03-12 13:32:47 -08001730 char idHash[33];
1731 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001732 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001733 return -1;
1734 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001735
San Mehata19b2502010-01-06 10:33:53 -08001736 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001737 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1738 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001739
1740 char dmDevice[255];
1741 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001742
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001743 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001744 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001745
Kenny Root344ca102012-04-03 17:23:01 -07001746 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1747 return -1;
1748 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001749
San Mehatd9a4e352010-03-12 13:32:47 -08001750 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001751 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001752 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001753 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001754 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001755 Loop::destroyByDevice(loopDevice);
1756 errno = EMEDIUMTYPE;
1757 return -1;
1758 }
1759 nr_sec--; // We don't want the devmapping to extend onto our superblock
1760
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001761 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1762 Loop::destroyByDevice(loopDevice);
1763 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001764 }
1765
Kenny Root344ca102012-04-03 17:23:01 -07001766 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001767 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001768 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001769 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001770 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001771 }
1772 Loop::destroyByDevice(loopDevice);
1773 return -1;
1774 }
San Mehata19b2502010-01-06 10:33:53 -08001775 }
1776
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001777 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001778 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001779 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001780 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001781
Kenny Root344ca102012-04-03 17:23:01 -07001782 int result;
1783 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001784 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1785 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001786 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001787 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1788 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001789 }
1790
1791 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001792 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001793 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001794 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001795 }
1796 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001797 return -1;
1798 }
1799
Kenny Rootcbacf782010-09-24 15:11:48 -07001800 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001801 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001802 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001803 }
San Mehata19b2502010-01-06 10:33:53 -08001804 return 0;
1805}
1806
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001807/**
1808 * Mounts an image file <code>img</code>.
1809 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001810int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001811 char mountPoint[255];
1812
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001813 char idHash[33];
1814 if (!asecHash(img, idHash, sizeof(idHash))) {
1815 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1816 return -1;
1817 }
1818
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001819 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001820 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001821 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001822 return -1;
1823 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001824
1825 if (isMountpointMounted(mountPoint)) {
1826 SLOGE("Image %s already mounted", img);
1827 errno = EBUSY;
1828 return -1;
1829 }
1830
1831 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001832 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1833 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001834
1835 char dmDevice[255];
1836 bool cleanupDm = false;
1837 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001838 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001839
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001840 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001841 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1842 Loop::destroyByDevice(loopDevice);
1843 return -1;
1844 }
1845
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001846 get_blkdev_size(fd, &nr_sec);
1847 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001848 SLOGE("Failed to get loop size (%s)", strerror(errno));
1849 Loop::destroyByDevice(loopDevice);
1850 close(fd);
1851 return -1;
1852 }
1853
1854 close(fd);
1855
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001856 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001857 Loop::destroyByDevice(loopDevice);
1858 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001859 }
1860
1861 if (mkdir(mountPoint, 0755)) {
1862 if (errno != EEXIST) {
1863 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1864 if (cleanupDm) {
1865 Devmapper::destroy(idHash);
1866 }
1867 Loop::destroyByDevice(loopDevice);
1868 return -1;
1869 }
1870 }
1871
yoshiyuki hama476a6272015-01-28 16:37:23 +09001872 /*
1873 * Wait for the device mapper node to be created.
1874 */
1875 waitForDevMapper(dmDevice);
1876
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001877 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1878 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001879 SLOGE("Image mount failed (%s)", strerror(errno));
1880 if (cleanupDm) {
1881 Devmapper::destroy(idHash);
1882 }
1883 Loop::destroyByDevice(loopDevice);
1884 return -1;
1885 }
1886
Kenny Rootcbacf782010-09-24 15:11:48 -07001887 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001888 if (mDebug) {
1889 SLOGD("Image %s mounted", img);
1890 }
1891 return 0;
1892}
1893
Kenny Root508c0e12010-07-12 09:59:49 -07001894int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001895 FILE *fp = setmntent("/proc/mounts", "r");
1896 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001897 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1898 return -1;
1899 }
1900
1901 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001902 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001903 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001904 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001905 loopDir[loopDirLen++] = '/';
1906 loopDir[loopDirLen] = '\0';
1907
Yabin Cuid1104f72015-01-02 13:28:28 -08001908 mntent* mentry;
1909 while ((mentry = getmntent(fp)) != NULL) {
1910 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001911 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001912 if (fd >= 0) {
1913 struct loop_info64 li;
1914 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1915 cli->sendMsg(ResponseCode::AsecListResult,
1916 (const char*) li.lo_file_name, false);
1917 }
1918 close(fd);
1919 }
1920 }
1921 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001922 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001923 return 0;
1924}
1925
Jeff Sharkey9c484982015-03-31 10:35:33 -07001926extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001927 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001928 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001929}
1930
San Mehata19b2502010-01-06 10:33:53 -08001931bool VolumeManager::isMountpointMounted(const char *mp)
1932{
Yabin Cuid1104f72015-01-02 13:28:28 -08001933 FILE *fp = setmntent("/proc/mounts", "r");
1934 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001935 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001936 return false;
1937 }
1938
Yabin Cuid1104f72015-01-02 13:28:28 -08001939 bool found_mp = false;
1940 mntent* mentry;
1941 while ((mentry = getmntent(fp)) != NULL) {
1942 if (strcmp(mentry->mnt_dir, mp) == 0) {
1943 found_mp = true;
1944 break;
San Mehata19b2502010-01-06 10:33:53 -08001945 }
San Mehata19b2502010-01-06 10:33:53 -08001946 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001947 endmntent(fp);
1948 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001949}
1950
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001951int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001952 // Only offer to create directories for paths managed by vold
1953 if (strncmp(path, "/storage/", 9) == 0) {
1954 // fs_mkdirs() does symlink checking and relative path enforcement
1955 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001956 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001957 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001958 return -EINVAL;
1959 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001960}