blob: 83984987dfc14e8f785fdac9726c3e3f51b56265 [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
Keun-young Park375ac252017-08-02 17:45:48 -070092bool VolumeManager::shutting_down = false;
93
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060094static const char* kPathUserMount = "/mnt/user";
95static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
96
97static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
98
99/* 512MiB is large enough for testing purposes */
100static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700102static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +0800103static const unsigned int kMajorBlockExperimentalMin = 240;
104static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700105
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700106/* writes superblock at end of file or device given by name */
107static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700108 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700109 if (sbfd < 0) {
110 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
111 return -1;
112 }
113
114 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
115 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
116 close(sbfd);
117 return -1;
118 }
119
120 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
121 SLOGE("Failed to write superblock (%s)", strerror(errno));
122 close(sbfd);
123 return -1;
124 }
125 close(sbfd);
126 return 0;
127}
128
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200129static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700130 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
131 // preventing costly operations or unexpected ENOSPC error.
132 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200133 unsigned long clusterSectors = 4096 / 512;
134 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700135 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700136 return ROUND_UP_POWER_OF_2(numSectors, 3);
137}
138
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200139static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700140 /*
141 * Add some headroom
142 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200143 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700144 numSectors += fatSize + 2;
145 /*
146 * FAT is aligned to 32 kb with 512b sectors.
147 */
148 return ROUND_UP_POWER_OF_2(numSectors, 6);
149}
150
151static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
152 if (Loop::lookupActive(idHash, buffer, len)) {
153 if (Loop::create(idHash, asecFileName, buffer, len)) {
154 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
155 return -1;
156 }
157 if (debug) {
158 SLOGD("New loop device created at %s", buffer);
159 }
160 } else {
161 if (debug) {
162 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
163 }
164 }
165 return 0;
166}
167
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200168static 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 -0700169 if (strcmp(key, "none")) {
170 if (Devmapper::lookupActive(idHash, buffer, len)) {
171 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
172 buffer, len)) {
173 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
174 return -1;
175 }
176 if (debug) {
177 SLOGD("New devmapper instance created at %s", buffer);
178 }
179 } else {
180 if (debug) {
181 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
182 }
183 }
184 *createdDMDevice = true;
185 } else {
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600186 strlcpy(buffer, loopDevice, len);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700187 *createdDMDevice = false;
188 }
189 return 0;
190}
191
192static void waitForDevMapper(const char *dmDevice) {
193 /*
194 * Wait for the device mapper node to be created. Sometimes it takes a
195 * while. Wait for up to 1 second. We could also inspect incoming uevents,
196 * but that would take more effort.
197 */
198 int tries = 25;
199 while (tries--) {
200 if (!access(dmDevice, F_OK) || errno != ENOENT) {
201 break;
202 }
203 usleep(40 * 1000);
204 }
205}
206
San Mehatf1b736b2009-10-10 17:22:08 -0700207VolumeManager *VolumeManager::sInstance = NULL;
208
209VolumeManager *VolumeManager::Instance() {
210 if (!sInstance)
211 sInstance = new VolumeManager();
212 return sInstance;
213}
214
215VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800216 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800217 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700218 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400219 mUmsSharingCount = 0;
220 mSavedDirtyRatio = -1;
221 // set dirty ratio to 0 when UMS is active
222 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700223}
224
225VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800226 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700227}
228
Kenny Root7b18a7b2010-03-15 13:13:41 -0700229char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700230 static const char* digits = "0123456789abcdef";
231
Kenny Root7b18a7b2010-03-15 13:13:41 -0700232 unsigned char sig[MD5_DIGEST_LENGTH];
233
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700234 if (buffer == NULL) {
235 SLOGE("Destination buffer is NULL");
236 errno = ESPIPE;
237 return NULL;
238 } else if (id == NULL) {
239 SLOGE("Source buffer is NULL");
240 errno = ESPIPE;
241 return NULL;
242 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800243 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700244 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800245 errno = ESPIPE;
246 return NULL;
247 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700248
249 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800250
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700251 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700252 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700253 *p++ = digits[sig[i] >> 4];
254 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800255 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700256 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800257
258 return buffer;
259}
260
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600261int VolumeManager::updateVirtualDisk() {
262 if (property_get_bool(kPropVirtualDisk, false)) {
263 if (access(kPathVirtualDisk, F_OK) != 0) {
264 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
265 }
266
267 if (mVirtualDisk == nullptr) {
268 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
269 LOG(ERROR) << "Failed to create virtual disk";
270 return -1;
271 }
272
273 struct stat buf;
274 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
275 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
276 return -1;
277 }
278
279 auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
280 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
281 disk->create();
282 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
283 mDisks.push_back(mVirtualDisk);
284 }
285 } else {
286 if (mVirtualDisk != nullptr) {
287 dev_t device = mVirtualDisk->getDevice();
288
289 auto i = mDisks.begin();
290 while (i != mDisks.end()) {
291 if ((*i)->getDevice() == device) {
292 (*i)->destroy();
293 i = mDisks.erase(i);
294 } else {
295 ++i;
296 }
297 }
298
299 Loop::destroyByDevice(mVirtualDiskPath.c_str());
300 mVirtualDisk = nullptr;
301 }
302
303 if (access(kPathVirtualDisk, F_OK) == 0) {
304 unlink(kPathVirtualDisk);
305 }
306 }
307 return 0;
308}
309
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700310int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800311 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700312 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800313}
314
San Mehatf1b736b2009-10-10 17:22:08 -0700315int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700316 // Always start from a clean slate by unmounting everything in
317 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700318 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700319
320 // Assume that we always have an emulated volume on internal
321 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700322 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700323 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700324 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700325 mInternalEmulated->create();
326
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600327 // Consider creating a virtual disk
328 updateVirtualDisk();
329
San Mehatf1b736b2009-10-10 17:22:08 -0700330 return 0;
331}
332
333int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700334 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700335 mInternalEmulated->destroy();
336 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700337 return 0;
338}
339
San Mehatfd7f5872009-10-12 11:32:47 -0700340void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700341 std::lock_guard<std::mutex> lock(mLock);
342
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700343 if (mDebug) {
344 LOG(VERBOSE) << "----------------";
345 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
346 evt->dump();
347 }
San Mehatf1b736b2009-10-10 17:22:08 -0700348
Mateusz Nowak64403792015-08-03 16:39:19 +0200349 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
350 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700351
352 if (devType != "disk") return;
353
354 int major = atoi(evt->findParam("MAJOR"));
355 int minor = atoi(evt->findParam("MINOR"));
356 dev_t device = makedev(major, minor);
357
358 switch (evt->getAction()) {
359 case NetlinkEvent::Action::kAdd: {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700360 for (const auto& source : mDiskSources) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700361 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800362 // For now, assume that MMC and virtio-blk (the latter is
363 // emulator-specific; see Disk.cpp for details) devices are SD,
364 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700365 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800366 if (major == kMajorBlockMmc
367 || (android::vold::IsRunningInEmulator()
368 && major >= (int) kMajorBlockExperimentalMin
369 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700370 flags |= android::vold::Disk::Flags::kSd;
371 } else {
372 flags |= android::vold::Disk::Flags::kUsb;
373 }
374
375 auto disk = new android::vold::Disk(eventPath, device,
376 source->getNickname(), flags);
377 disk->create();
378 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
379 break;
380 }
381 }
382 break;
383 }
384 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700385 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700386 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700387 if (disk->getDevice() == device) {
388 disk->readMetadata();
389 disk->readPartitions();
390 }
391 }
392 break;
393 }
394 case NetlinkEvent::Action::kRemove: {
395 auto i = mDisks.begin();
396 while (i != mDisks.end()) {
397 if ((*i)->getDevice() == device) {
398 (*i)->destroy();
399 i = mDisks.erase(i);
400 } else {
401 ++i;
402 }
403 }
404 break;
405 }
406 default: {
407 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
408 break;
409 }
410 }
411}
412
413void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800414 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700415 mDiskSources.push_back(diskSource);
416}
417
418std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
419 for (auto disk : mDisks) {
420 if (disk->getId() == id) {
421 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700422 }
423 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700424 return nullptr;
425}
San Mehatf1b736b2009-10-10 17:22:08 -0700426
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700427std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
428 if (mInternalEmulated->getId() == id) {
429 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700430 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700431 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700432 auto vol = disk->findVolume(id);
433 if (vol != nullptr) {
434 return vol;
435 }
436 }
437 return nullptr;
438}
439
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700440void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
441 std::list<std::string>& list) {
442 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700443 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700444 disk->listVolumes(type, list);
445 }
446}
447
448nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700449 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700450 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700451 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700452 } else {
453 auto vol = findVolume(id);
454 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
455 path = vol->getPath();
456 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700457 }
458
459 if (path.empty()) {
460 LOG(WARNING) << "Failed to find volume for " << id;
461 return -1;
462 }
463
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700464 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700465}
466
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700467int VolumeManager::forgetPartition(const std::string& partGuid) {
468 std::string normalizedGuid;
469 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
470 LOG(WARNING) << "Invalid GUID " << partGuid;
471 return -1;
472 }
473
474 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
475 if (unlink(keyPath.c_str()) != 0) {
476 LOG(ERROR) << "Failed to unlink " << keyPath;
477 return -1;
478 }
479
480 return 0;
481}
482
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700483int VolumeManager::linkPrimary(userid_t userId) {
484 std::string source(mPrimary->getPath());
485 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
486 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700487 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700488 }
489
490 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
491 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
492 if (errno != ENOENT) {
493 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
494 }
495 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700496 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700497 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
498 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
499 strerror(errno));
500 return -errno;
501 }
502 return 0;
503}
504
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700505int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
506 mAddedUsers[userId] = userSerialNumber;
507 return 0;
508}
509
510int VolumeManager::onUserRemoved(userid_t userId) {
511 mAddedUsers.erase(userId);
512 return 0;
513}
514
515int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700516 // Note that sometimes the system will spin up processes from Zygote
517 // before actually starting the user, so we're okay if Zygote
518 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600519 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700520 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
521
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700522 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700523 if (mPrimary) {
524 linkPrimary(userId);
525 }
526 return 0;
527}
528
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700529int VolumeManager::onUserStopped(userid_t userId) {
530 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700531 return 0;
532}
533
534int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
535 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700536 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700537 linkPrimary(userId);
538 }
539 return 0;
540}
541
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700542static int unmount_tree(const char* path) {
543 size_t path_len = strlen(path);
544
545 FILE* fp = setmntent("/proc/mounts", "r");
546 if (fp == NULL) {
547 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
548 return -errno;
549 }
550
551 // Some volumes can be stacked on each other, so force unmount in
552 // reverse order to give us the best chance of success.
553 std::list<std::string> toUnmount;
554 mntent* mentry;
555 while ((mentry = getmntent(fp)) != NULL) {
556 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
557 toUnmount.push_front(std::string(mentry->mnt_dir));
558 }
559 }
560 endmntent(fp);
561
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700562 for (const auto& path : toUnmount) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700563 if (umount2(path.c_str(), MNT_DETACH)) {
564 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
565 }
566 }
567 return 0;
568}
569
Jeff Sharkey66270a22015-06-24 11:49:24 -0700570int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
571 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
572
573 DIR* dir;
574 struct dirent* de;
575 char rootName[PATH_MAX];
576 char pidName[PATH_MAX];
577 int pidFd;
578 int nsFd;
579 struct stat sb;
580 pid_t child;
581
582 if (!(dir = opendir("/proc"))) {
583 PLOG(ERROR) << "Failed to opendir";
584 return -1;
585 }
586
587 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900588 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700589 PLOG(ERROR) << "Failed to readlink";
590 closedir(dir);
591 return -1;
592 }
593
594 // Poke through all running PIDs look for apps running as UID
595 while ((de = readdir(dir))) {
596 pidFd = -1;
597 nsFd = -1;
598
599 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
600 if (pidFd < 0) {
601 goto next;
602 }
603 if (fstat(pidFd, &sb) != 0) {
604 PLOG(WARNING) << "Failed to stat " << de->d_name;
605 goto next;
606 }
607 if (sb.st_uid != uid) {
608 goto next;
609 }
610
611 // Matches so far, but refuse to touch if in root namespace
612 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900613 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700614 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
615 goto next;
616 }
617 if (!strcmp(rootName, pidName)) {
618 LOG(WARNING) << "Skipping due to root namespace";
619 goto next;
620 }
621
622 // We purposefully leave the namespace open across the fork
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600623 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700624 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700625 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700626 goto next;
627 }
628
629 if (!(child = fork())) {
630 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700631 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700632 _exit(1);
633 }
634
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700635 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700636
637 std::string storageSource;
638 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700639 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700640 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700641 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700642 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700643 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700644 } else {
645 // Sane default of no storage visible
646 _exit(0);
647 }
648 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
Hidehiko Abe674bed12016-03-09 16:42:10 +0900649 NULL, MS_BIND | MS_REC, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700650 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
651 << de->d_name;
652 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700653 }
Hidehiko Abe674bed12016-03-09 16:42:10 +0900654 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
655 MS_REC | MS_SLAVE, NULL)) == -1) {
656 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
657 << de->d_name;
658 _exit(1);
659 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700660
661 // Mount user-specific symlink helper into place
662 userid_t user_id = multiuser_get_user_id(uid);
663 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
664 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
665 NULL, MS_BIND, NULL)) == -1) {
666 PLOG(ERROR) << "Failed to mount " << userSource << " for "
667 << de->d_name;
668 _exit(1);
669 }
670
Jeff Sharkey66270a22015-06-24 11:49:24 -0700671 _exit(0);
672 }
673
674 if (child == -1) {
675 PLOG(ERROR) << "Failed to fork";
676 goto next;
677 } else {
678 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
679 }
680
681next:
682 close(nsFd);
683 close(pidFd);
684 }
685 closedir(dir);
686 return 0;
687}
688
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700689int VolumeManager::reset() {
690 // Tear down all existing disks/volumes and start from a blank slate so
691 // newly connected framework hears all events.
692 mInternalEmulated->destroy();
693 mInternalEmulated->create();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700694 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700695 disk->destroy();
696 disk->create();
697 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600698 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700699 mAddedUsers.clear();
700 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700701 return 0;
702}
703
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700704// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700705int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700706 if (mInternalEmulated == nullptr) {
707 return 0; // already shutdown
708 }
Keun-young Park375ac252017-08-02 17:45:48 -0700709 shutting_down = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700710 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700711 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700712 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700713 disk->destroy();
714 }
715 mDisks.clear();
Keun-young Park375ac252017-08-02 17:45:48 -0700716 shutting_down = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700717 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700718}
719
Jeff Sharkey9c484982015-03-31 10:35:33 -0700720int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700721 std::lock_guard<std::mutex> lock(mLock);
722
Jeff Sharkey9c484982015-03-31 10:35:33 -0700723 // First, try gracefully unmounting all known devices
724 if (mInternalEmulated != nullptr) {
725 mInternalEmulated->unmount();
726 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700727 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700728 disk->unmountAll();
729 }
730
731 // Worst case we might have some stale mounts lurking around, so
732 // force unmount those just to be safe.
733 FILE* fp = setmntent("/proc/mounts", "r");
734 if (fp == NULL) {
735 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
736 return -errno;
737 }
738
739 // Some volumes can be stacked on each other, so force unmount in
740 // reverse order to give us the best chance of success.
741 std::list<std::string> toUnmount;
742 mntent* mentry;
743 while ((mentry = getmntent(fp)) != NULL) {
744 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
745 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
746 toUnmount.push_front(std::string(mentry->mnt_dir));
747 }
748 }
749 endmntent(fp);
750
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700751 for (const auto& path : toUnmount) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700752 SLOGW("Tearing down stale mount %s", path.c_str());
753 android::vold::ForceUnmount(path);
754 }
755
756 return 0;
757}
758
Kenny Root508c0e12010-07-12 09:59:49 -0700759int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
760 char idHash[33];
761 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
762 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
763 return -1;
764 }
765
766 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700767 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400768 if ((written < 0) || (written >= mountPathLen)) {
769 errno = EINVAL;
770 return -1;
771 }
Kenny Root508c0e12010-07-12 09:59:49 -0700772
773 if (access(mountPath, F_OK)) {
774 errno = ENOENT;
775 return -1;
776 }
777
778 return 0;
779}
780
San Mehata19b2502010-01-06 10:33:53 -0800781int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700782 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700783
Nick Kralevich0de7c612014-01-27 14:58:06 -0800784 if (!isLegalAsecId(id)) {
785 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
786 errno = EINVAL;
787 return -1;
788 }
789
Kenny Root344ca102012-04-03 17:23:01 -0700790 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
791 SLOGE("Couldn't find ASEC %s", id);
792 return -1;
793 }
San Mehat88ac2c02010-03-23 11:15:58 -0700794
795 memset(buffer, 0, maxlen);
796 if (access(asecFileName, F_OK)) {
797 errno = ENOENT;
798 return -1;
799 }
San Mehata19b2502010-01-06 10:33:53 -0800800
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700801 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400802 if ((written < 0) || (written >= maxlen)) {
803 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
804 errno = EINVAL;
805 return -1;
806 }
807
San Mehata19b2502010-01-06 10:33:53 -0800808 return 0;
809}
810
Dianne Hackborn736910c2011-06-27 13:37:07 -0700811int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
812 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700813
Nick Kralevich0de7c612014-01-27 14:58:06 -0800814 if (!isLegalAsecId(id)) {
815 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
816 errno = EINVAL;
817 return -1;
818 }
819
Kenny Root344ca102012-04-03 17:23:01 -0700820 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
821 SLOGE("Couldn't find ASEC %s", id);
822 return -1;
823 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700824
825 memset(buffer, 0, maxlen);
826 if (access(asecFileName, F_OK)) {
827 errno = ENOENT;
828 return -1;
829 }
830
rpcraigd1c226f2012-10-09 06:58:16 -0400831 int written = snprintf(buffer, maxlen, "%s", asecFileName);
832 if ((written < 0) || (written >= maxlen)) {
833 errno = EINVAL;
834 return -1;
835 }
836
Dianne Hackborn736910c2011-06-27 13:37:07 -0700837 return 0;
838}
839
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200840int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700841 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800842 struct asec_superblock sb;
843 memset(&sb, 0, sizeof(sb));
844
Nick Kralevich0de7c612014-01-27 14:58:06 -0800845 if (!isLegalAsecId(id)) {
846 SLOGE("createAsec: Invalid asec id \"%s\"", id);
847 errno = EINVAL;
848 return -1;
849 }
850
Kenny Root344ca102012-04-03 17:23:01 -0700851 const bool wantFilesystem = strcmp(fstype, "none");
852 bool usingExt4 = false;
853 if (wantFilesystem) {
854 usingExt4 = !strcmp(fstype, "ext4");
855 if (usingExt4) {
856 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
857 } else if (strcmp(fstype, "fat")) {
858 SLOGE("Invalid filesystem type %s", fstype);
859 errno = EINVAL;
860 return -1;
861 }
862 }
863
San Mehatfcf24fe2010-03-03 12:37:32 -0800864 sb.magic = ASEC_SB_MAGIC;
865 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800866
San Mehatd31e3802010-02-18 08:37:45 -0800867 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200868 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800869 errno = EINVAL;
870 return -1;
871 }
872
San Mehata19b2502010-01-06 10:33:53 -0800873 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700874
875 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
876 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
877 asecFileName, strerror(errno));
878 errno = EADDRINUSE;
879 return -1;
880 }
881
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700882 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700883
rpcraigd1c226f2012-10-09 06:58:16 -0400884 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
885 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
886 errno = EINVAL;
887 return -1;
888 }
San Mehata19b2502010-01-06 10:33:53 -0800889
890 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700891 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700892 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800893 errno = EADDRINUSE;
894 return -1;
895 }
896
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200897 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700898 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700899 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700900 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700901 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800902
903 // Add +1 for our superblock which is at the end
904 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700905 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800906 return -1;
907 }
908
San Mehatd9a4e352010-03-12 13:32:47 -0800909 char idHash[33];
910 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700911 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800912 unlink(asecFileName);
913 return -1;
914 }
915
San Mehata19b2502010-01-06 10:33:53 -0800916 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800917 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700918 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800919 unlink(asecFileName);
920 return -1;
921 }
922
San Mehatb78a32c2010-01-10 13:02:12 -0800923 char dmDevice[255];
924 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800925
San Mehatb78a32c2010-01-10 13:02:12 -0800926 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800927 // XXX: This is all we support for now
928 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800929 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800930 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700931 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800932 Loop::destroyByDevice(loopDevice);
933 unlink(asecFileName);
934 return -1;
935 }
936 cleanupDm = true;
937 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800938 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600939 strlcpy(dmDevice, loopDevice, sizeof(dmDevice));
San Mehatb78a32c2010-01-10 13:02:12 -0800940 }
941
San Mehatfcf24fe2010-03-03 12:37:32 -0800942 /*
943 * Drop down the superblock at the end of the file
944 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700945 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800946 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800947 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800948 }
949 Loop::destroyByDevice(loopDevice);
950 unlink(asecFileName);
951 return -1;
952 }
953
Kenny Root344ca102012-04-03 17:23:01 -0700954 if (wantFilesystem) {
955 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400956 char mountPoint[255];
957
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700958 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400959 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
960 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
961 if (cleanupDm) {
962 Devmapper::destroy(idHash);
963 }
964 Loop::destroyByDevice(loopDevice);
965 unlink(asecFileName);
966 return -1;
967 }
rpcraiga54e13a2012-09-21 14:17:08 -0400968
Kenny Root344ca102012-04-03 17:23:01 -0700969 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700970 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700971 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700972 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800973 }
San Mehata19b2502010-01-06 10:33:53 -0800974
Kenny Root344ca102012-04-03 17:23:01 -0700975 if (formatStatus < 0) {
976 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800977 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800978 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800979 }
San Mehateb13a902010-01-07 12:12:50 -0800980 Loop::destroyByDevice(loopDevice);
981 unlink(asecFileName);
982 return -1;
983 }
Kenny Root344ca102012-04-03 17:23:01 -0700984
Kenny Root344ca102012-04-03 17:23:01 -0700985 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800986 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700987 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800988 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800989 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800990 }
991 Loop::destroyByDevice(loopDevice);
992 unlink(asecFileName);
993 return -1;
994 }
San Mehatb78a32c2010-01-10 13:02:12 -0800995 }
San Mehata1091cb2010-02-28 20:17:20 -0800996
Kenny Root344ca102012-04-03 17:23:01 -0700997 int mountStatus;
998 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700999 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
1000 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -07001001 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001002 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
1003 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -07001004 }
1005
1006 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -07001007 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -08001008 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001009 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001010 }
1011 Loop::destroyByDevice(loopDevice);
1012 unlink(asecFileName);
1013 return -1;
1014 }
Kenny Root344ca102012-04-03 17:23:01 -07001015
1016 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001017 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001018 if (dirfd >= 0) {
1019 if (fchown(dirfd, ownerUid, AID_SYSTEM)
1020 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
1021 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
1022 }
1023 close(dirfd);
1024 }
1025 }
San Mehata1091cb2010-02-28 20:17:20 -08001026 } else {
San Mehat97ac40e2010-03-24 10:24:19 -07001027 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -08001028 }
San Mehat88705162010-01-15 09:26:28 -08001029
Kenny Rootcbacf782010-09-24 15:11:48 -07001030 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -08001031 return 0;
1032}
1033
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001034int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001035 char asecFileName[255];
1036 char mountPoint[255];
1037 bool cleanupDm = false;
1038
1039 if (!isLegalAsecId(id)) {
1040 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
1041 errno = EINVAL;
1042 return -1;
1043 }
1044
1045 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1046 SLOGE("Couldn't find ASEC %s", id);
1047 return -1;
1048 }
1049
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001050 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001051 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1052 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
1053 return -1;
1054 }
1055
1056 if (isMountpointMounted(mountPoint)) {
1057 SLOGE("ASEC %s mounted. Unmount before resizing", id);
1058 errno = EBUSY;
1059 return -1;
1060 }
1061
1062 struct asec_superblock sb;
1063 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001064 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001065
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001066 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001067 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
1068 return -1;
1069 }
1070
1071 struct stat info;
1072 if (fstat(fd, &info) < 0) {
1073 SLOGE("Failed to get file size (%s)", strerror(errno));
1074 close(fd);
1075 return -1;
1076 }
1077
1078 oldNumSec = info.st_size / 512;
1079
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001080 /*
1081 * Try to read superblock.
1082 */
1083 memset(&sb, 0, sizeof(struct asec_superblock));
1084 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1085 SLOGE("lseek failed (%s)", strerror(errno));
1086 close(fd);
1087 return -1;
1088 }
1089 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1090 SLOGE("superblock read failed (%s)", strerror(errno));
1091 close(fd);
1092 return -1;
1093 }
1094 close(fd);
1095
1096 if (mDebug) {
1097 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1098 }
1099 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1100 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1101 errno = EMEDIUMTYPE;
1102 return -1;
1103 }
1104
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001105 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001106 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1107 SLOGE("Only ext4 partitions are supported for resize");
1108 errno = EINVAL;
1109 return -1;
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001110 } else {
1111 numImgSectors = adjustSectorNumExt4(numSectors);
1112 }
1113
1114 /*
1115 * add one block for the superblock
1116 */
1117 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1118 if (oldNumSec == numImgSectors + 1) {
1119 SLOGW("Size unchanged; ignoring resize request");
1120 return 0;
1121 } else if (oldNumSec > numImgSectors + 1) {
1122 SLOGE("Only growing is currently supported.");
1123 close(fd);
1124 return -1;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001125 }
1126
1127 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1128 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1129 return -1;
1130 }
1131
1132 /*
1133 * Drop down a copy of the superblock at the end of the file
1134 */
1135 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1136 goto fail;
1137
1138 char idHash[33];
1139 if (!asecHash(id, idHash, sizeof(idHash))) {
1140 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1141 goto fail;
1142 }
1143
1144 char loopDevice[255];
1145 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1146 goto fail;
1147
1148 char dmDevice[255];
1149
1150 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1151 Loop::destroyByDevice(loopDevice);
1152 goto fail;
1153 }
1154
1155 /*
1156 * Wait for the device mapper node to be created.
1157 */
1158 waitForDevMapper(dmDevice);
1159
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001160 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001161 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1162 if (cleanupDm) {
1163 Devmapper::destroy(idHash);
1164 }
1165 Loop::destroyByDevice(loopDevice);
1166 goto fail;
1167 }
1168
1169 return 0;
1170fail:
1171 Loop::resizeImageFile(asecFileName, oldNumSec);
1172 return -1;
1173}
1174
San Mehata19b2502010-01-06 10:33:53 -08001175int VolumeManager::finalizeAsec(const char *id) {
1176 char asecFileName[255];
1177 char loopDevice[255];
1178 char mountPoint[255];
1179
Nick Kralevich0de7c612014-01-27 14:58:06 -08001180 if (!isLegalAsecId(id)) {
1181 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1182 errno = EINVAL;
1183 return -1;
1184 }
1185
Kenny Root344ca102012-04-03 17:23:01 -07001186 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1187 SLOGE("Couldn't find ASEC %s", id);
1188 return -1;
1189 }
San Mehata19b2502010-01-06 10:33:53 -08001190
San Mehatd9a4e352010-03-12 13:32:47 -08001191 char idHash[33];
1192 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001193 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001194 return -1;
1195 }
1196
1197 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001198 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001199 return -1;
1200 }
1201
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001202 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001203 struct asec_superblock sb;
1204
1205 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1206 return -1;
1207 }
1208
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001209 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001210 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1211 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1212 return -1;
1213 }
Kenny Root344ca102012-04-03 17:23:01 -07001214
1215 int result = 0;
1216 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001217 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1218 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001219 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001220 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1221 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001222 }
1223
1224 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001225 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001226 return -1;
1227 }
1228
San Mehatd9a4e352010-03-12 13:32:47 -08001229 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001230 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001231 }
San Mehata19b2502010-01-06 10:33:53 -08001232 return 0;
1233}
1234
Kenny Root344ca102012-04-03 17:23:01 -07001235int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1236 char asecFileName[255];
1237 char loopDevice[255];
1238 char mountPoint[255];
1239
1240 if (gid < AID_APP) {
1241 SLOGE("Group ID is not in application range");
1242 return -1;
1243 }
1244
Nick Kralevich0de7c612014-01-27 14:58:06 -08001245 if (!isLegalAsecId(id)) {
1246 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1247 errno = EINVAL;
1248 return -1;
1249 }
1250
Kenny Root344ca102012-04-03 17:23:01 -07001251 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1252 SLOGE("Couldn't find ASEC %s", id);
1253 return -1;
1254 }
1255
1256 char idHash[33];
1257 if (!asecHash(id, idHash, sizeof(idHash))) {
1258 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1259 return -1;
1260 }
1261
1262 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1263 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1264 return -1;
1265 }
1266
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001267 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001268 struct asec_superblock sb;
1269
1270 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1271 return -1;
1272 }
1273
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001274 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001275 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1276 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1277 return -1;
1278 }
Kenny Root344ca102012-04-03 17:23:01 -07001279
1280 int result = 0;
1281 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1282 return 0;
1283 }
1284
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001285 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001286 false /* read-only */,
1287 true /* remount */,
1288 false /* executable */);
1289 if (ret) {
1290 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1291 return -1;
1292 }
1293
1294 char *paths[] = { mountPoint, NULL };
1295
1296 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1297 if (fts) {
1298 // Traverse the entire hierarchy and chown to system UID.
1299 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1300 // We don't care about the lost+found directory.
1301 if (!strcmp(ftsent->fts_name, "lost+found")) {
1302 continue;
1303 }
1304
1305 /*
1306 * There can only be one file marked as private right now.
1307 * This should be more robust, but it satisfies the requirements
1308 * we have for right now.
1309 */
1310 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1311
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001312 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001313 if (fd < 0) {
1314 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1315 result = -1;
1316 continue;
1317 }
1318
1319 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1320
1321 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001322 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001323 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001324 result |= fchmod(fd, privateFile ? 0640 : 0644);
1325 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001326
Stephen Smalley5093e612014-02-12 09:43:08 -05001327 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001328 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1329 result |= -1;
1330 }
1331
Kenny Root344ca102012-04-03 17:23:01 -07001332 close(fd);
1333 }
1334 fts_close(fts);
1335
1336 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001337 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001338 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1339 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1340 result |= -1;
1341 }
1342 close(dirfd);
1343 } else {
1344 result |= -1;
1345 }
1346
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001347 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001348 true /* read-only */,
1349 true /* remount */,
1350 true /* execute */);
1351
1352 if (result) {
1353 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1354 return -1;
1355 }
1356
1357 if (mDebug) {
1358 SLOGD("ASEC %s permissions fixed", id);
1359 }
1360 return 0;
1361}
1362
San Mehat048b0802010-01-23 08:17:06 -08001363int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001364 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001365 char *asecFilename2;
1366 char mountPoint[255];
1367
Kenny Root344ca102012-04-03 17:23:01 -07001368 const char *dir;
1369
Nick Kralevich0de7c612014-01-27 14:58:06 -08001370 if (!isLegalAsecId(id1)) {
1371 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1372 errno = EINVAL;
1373 return -1;
1374 }
1375
1376 if (!isLegalAsecId(id2)) {
1377 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1378 errno = EINVAL;
1379 return -1;
1380 }
1381
Kenny Root344ca102012-04-03 17:23:01 -07001382 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1383 SLOGE("Couldn't find ASEC %s", id1);
1384 return -1;
1385 }
1386
1387 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001388
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001389 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001390 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1391 SLOGE("Rename failed: couldn't construct mountpoint");
1392 goto out_err;
1393 }
1394
San Mehat048b0802010-01-23 08:17:06 -08001395 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001396 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001397 errno = EBUSY;
1398 goto out_err;
1399 }
1400
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001401 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001402 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1403 SLOGE("Rename failed: couldn't construct mountpoint2");
1404 goto out_err;
1405 }
1406
San Mehat96956ed2010-02-24 08:42:51 -08001407 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001408 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001409 errno = EBUSY;
1410 goto out_err;
1411 }
1412
San Mehat048b0802010-01-23 08:17:06 -08001413 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001414 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001415 errno = EADDRINUSE;
1416 goto out_err;
1417 }
1418
1419 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001420 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001421 goto out_err;
1422 }
1423
San Mehat048b0802010-01-23 08:17:06 -08001424 free(asecFilename2);
1425 return 0;
1426
1427out_err:
San Mehat048b0802010-01-23 08:17:06 -08001428 free(asecFilename2);
1429 return -1;
1430}
1431
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001432#define UNMOUNT_RETRIES 5
1433#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001434int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001435 char asecFileName[255];
1436 char mountPoint[255];
1437
Nick Kralevich0de7c612014-01-27 14:58:06 -08001438 if (!isLegalAsecId(id)) {
1439 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1440 errno = EINVAL;
1441 return -1;
1442 }
1443
Kenny Root344ca102012-04-03 17:23:01 -07001444 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1445 SLOGE("Couldn't find ASEC %s", id);
1446 return -1;
1447 }
1448
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001449 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001450 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1451 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1452 return -1;
1453 }
San Mehata19b2502010-01-06 10:33:53 -08001454
San Mehatd9a4e352010-03-12 13:32:47 -08001455 char idHash[33];
1456 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001457 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001458 return -1;
1459 }
1460
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001461 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1462}
1463
Kenny Root508c0e12010-07-12 09:59:49 -07001464int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001465 char mountPoint[255];
1466
1467 char idHash[33];
1468 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1469 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1470 return -1;
1471 }
1472
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001473 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001474 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1475 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1476 return -1;
1477 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001478
1479 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1480}
1481
1482int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1483 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001484 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001485 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001486 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001487 return -1;
1488 }
San Mehat23969932010-01-09 07:08:06 -08001489
San Mehatb78a32c2010-01-10 13:02:12 -08001490 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001491 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001492 rc = umount(mountPoint);
1493 if (!rc) {
1494 break;
San Mehata19b2502010-01-06 10:33:53 -08001495 }
San Mehatb78a32c2010-01-10 13:02:12 -08001496 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001497 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001498 rc = 0;
1499 break;
San Mehata19b2502010-01-06 10:33:53 -08001500 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001501 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001502 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001503
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001504 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001505
1506 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001507 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001508 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001509 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001510 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001511 }
San Mehat8c940ef2010-02-13 14:19:53 -08001512
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001513 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001514 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001515 }
1516
1517 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001518 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001519 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001520 return -1;
1521 }
1522
San Mehat12f4b892010-02-24 11:43:22 -08001523 int retries = 10;
1524
1525 while(retries--) {
1526 if (!rmdir(mountPoint)) {
1527 break;
1528 }
1529
San Mehat97ac40e2010-03-24 10:24:19 -07001530 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001531 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001532 }
1533
1534 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001535 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001536 }
San Mehat88705162010-01-15 09:26:28 -08001537
Paul Lawrence60dec162014-09-02 10:52:15 -07001538 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1539 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1540 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1541 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1542 continue;
1543 } else {
1544 break;
1545 }
San Mehata19b2502010-01-06 10:33:53 -08001546 }
1547
1548 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001549 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001550 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001551 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001552 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001553 }
San Mehat88705162010-01-15 09:26:28 -08001554
1555 AsecIdCollection::iterator it;
1556 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001557 ContainerData* cd = *it;
1558 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001559 free(*it);
1560 mActiveContainers->erase(it);
1561 break;
1562 }
1563 }
1564 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001565 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001566 }
San Mehatb78a32c2010-01-10 13:02:12 -08001567 return 0;
1568}
1569
San Mehat4ba89482010-02-18 09:00:18 -08001570int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001571 char asecFileName[255];
1572 char mountPoint[255];
1573
Nick Kralevich0de7c612014-01-27 14:58:06 -08001574 if (!isLegalAsecId(id)) {
1575 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1576 errno = EINVAL;
1577 return -1;
1578 }
1579
Kenny Root344ca102012-04-03 17:23:01 -07001580 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1581 SLOGE("Couldn't find ASEC %s", id);
1582 return -1;
1583 }
1584
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001585 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001586 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1587 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1588 return -1;
1589 }
San Mehatb78a32c2010-01-10 13:02:12 -08001590
San Mehat0586d542010-01-12 15:38:59 -08001591 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001592 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001593 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001594 }
San Mehat4ba89482010-02-18 09:00:18 -08001595 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001596 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001597 return -1;
1598 }
1599 }
San Mehata19b2502010-01-06 10:33:53 -08001600
San Mehat0586d542010-01-12 15:38:59 -08001601 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001602 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001603 return -1;
1604 }
San Mehata19b2502010-01-06 10:33:53 -08001605
San Mehatd9a4e352010-03-12 13:32:47 -08001606 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001607 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001608 }
San Mehata19b2502010-01-06 10:33:53 -08001609 return 0;
1610}
1611
Nick Kralevich0de7c612014-01-27 14:58:06 -08001612/*
1613 * Legal ASEC ids consist of alphanumeric characters, '-',
1614 * '_', or '.'. ".." is not allowed. The first or last character
1615 * of the ASEC id cannot be '.' (dot).
1616 */
1617bool VolumeManager::isLegalAsecId(const char *id) const {
1618 size_t i;
1619 size_t len = strlen(id);
1620
1621 if (len == 0) {
1622 return false;
1623 }
1624 if ((id[0] == '.') || (id[len - 1] == '.')) {
1625 return false;
1626 }
1627
1628 for (i = 0; i < len; i++) {
1629 if (id[i] == '.') {
1630 // i=0 is guaranteed never to have a dot. See above.
1631 if (id[i-1] == '.') return false;
1632 continue;
1633 }
1634 if (id[i] == '_' || id[i] == '-') continue;
1635 if (id[i] >= 'a' && id[i] <= 'z') continue;
1636 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1637 if (id[i] >= '0' && id[i] <= '9') continue;
1638 return false;
1639 }
1640
1641 return true;
1642}
1643
Kenny Root344ca102012-04-03 17:23:01 -07001644bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001645 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001646 if (dirfd < 0) {
1647 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001648 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001649 }
1650
Nick Kralevich25e581a2015-02-06 08:55:08 -08001651 struct stat sb;
1652 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1653 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001654
1655 close(dirfd);
1656
1657 return ret;
1658}
1659
1660int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1661 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001662 char *asecName;
1663
Nick Kralevich0de7c612014-01-27 14:58:06 -08001664 if (!isLegalAsecId(id)) {
1665 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1666 errno = EINVAL;
1667 return -1;
1668 }
1669
Kenny Root344ca102012-04-03 17:23:01 -07001670 if (asprintf(&asecName, "%s.asec", id) < 0) {
1671 SLOGE("Couldn't allocate string to write ASEC name");
1672 return -1;
1673 }
1674
1675 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001676 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1677 dir = VolumeManager::SEC_ASECDIR_INT;
1678 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1679 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001680 } else {
1681 free(asecName);
1682 return -1;
1683 }
1684
1685 if (directory != NULL) {
1686 *directory = dir;
1687 }
1688
1689 if (asecPath != NULL) {
1690 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001691 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1692 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001693 free(asecName);
1694 return -1;
1695 }
1696 }
1697
1698 free(asecName);
1699 return 0;
1700}
1701
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001702int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001703 char asecFileName[255];
1704 char mountPoint[255];
1705
Nick Kralevich0de7c612014-01-27 14:58:06 -08001706 if (!isLegalAsecId(id)) {
1707 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1708 errno = EINVAL;
1709 return -1;
1710 }
1711
Kenny Root344ca102012-04-03 17:23:01 -07001712 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1713 SLOGE("Couldn't find ASEC %s", id);
1714 return -1;
1715 }
1716
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001717 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001718 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001719 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001720 return -1;
1721 }
San Mehata19b2502010-01-06 10:33:53 -08001722
1723 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001724 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001725 errno = EBUSY;
1726 return -1;
1727 }
1728
San Mehatd9a4e352010-03-12 13:32:47 -08001729 char idHash[33];
1730 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001731 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001732 return -1;
1733 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001734
San Mehata19b2502010-01-06 10:33:53 -08001735 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001736 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1737 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001738
1739 char dmDevice[255];
1740 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001741
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001742 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001743 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001744
Kenny Root344ca102012-04-03 17:23:01 -07001745 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1746 return -1;
1747 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001748
San Mehatd9a4e352010-03-12 13:32:47 -08001749 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001750 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001751 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001752 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001753 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001754 Loop::destroyByDevice(loopDevice);
1755 errno = EMEDIUMTYPE;
1756 return -1;
1757 }
1758 nr_sec--; // We don't want the devmapping to extend onto our superblock
1759
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001760 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1761 Loop::destroyByDevice(loopDevice);
1762 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001763 }
1764
Kenny Root344ca102012-04-03 17:23:01 -07001765 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001766 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001767 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001768 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001769 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001770 }
1771 Loop::destroyByDevice(loopDevice);
1772 return -1;
1773 }
San Mehata19b2502010-01-06 10:33:53 -08001774 }
1775
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001776 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001777 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001778 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001779 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001780
Kenny Root344ca102012-04-03 17:23:01 -07001781 int result;
1782 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001783 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1784 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001785 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001786 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1787 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001788 }
1789
1790 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001791 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001792 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001793 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001794 }
1795 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001796 return -1;
1797 }
1798
Kenny Rootcbacf782010-09-24 15:11:48 -07001799 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001800 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001801 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001802 }
San Mehata19b2502010-01-06 10:33:53 -08001803 return 0;
1804}
1805
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001806/**
1807 * Mounts an image file <code>img</code>.
1808 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001809int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001810 char mountPoint[255];
1811
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001812 char idHash[33];
1813 if (!asecHash(img, idHash, sizeof(idHash))) {
1814 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1815 return -1;
1816 }
1817
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001818 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001819 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001820 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001821 return -1;
1822 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001823
1824 if (isMountpointMounted(mountPoint)) {
1825 SLOGE("Image %s already mounted", img);
1826 errno = EBUSY;
1827 return -1;
1828 }
1829
1830 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001831 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1832 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001833
1834 char dmDevice[255];
1835 bool cleanupDm = false;
1836 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001837 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001838
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001839 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001840 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1841 Loop::destroyByDevice(loopDevice);
1842 return -1;
1843 }
1844
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001845 get_blkdev_size(fd, &nr_sec);
1846 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001847 SLOGE("Failed to get loop size (%s)", strerror(errno));
1848 Loop::destroyByDevice(loopDevice);
1849 close(fd);
1850 return -1;
1851 }
1852
1853 close(fd);
1854
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001855 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001856 Loop::destroyByDevice(loopDevice);
1857 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001858 }
1859
1860 if (mkdir(mountPoint, 0755)) {
1861 if (errno != EEXIST) {
1862 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1863 if (cleanupDm) {
1864 Devmapper::destroy(idHash);
1865 }
1866 Loop::destroyByDevice(loopDevice);
1867 return -1;
1868 }
1869 }
1870
yoshiyuki hama476a6272015-01-28 16:37:23 +09001871 /*
1872 * Wait for the device mapper node to be created.
1873 */
1874 waitForDevMapper(dmDevice);
1875
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001876 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1877 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001878 SLOGE("Image mount failed (%s)", strerror(errno));
1879 if (cleanupDm) {
1880 Devmapper::destroy(idHash);
1881 }
1882 Loop::destroyByDevice(loopDevice);
1883 return -1;
1884 }
1885
Kenny Rootcbacf782010-09-24 15:11:48 -07001886 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001887 if (mDebug) {
1888 SLOGD("Image %s mounted", img);
1889 }
1890 return 0;
1891}
1892
Kenny Root508c0e12010-07-12 09:59:49 -07001893int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001894 FILE *fp = setmntent("/proc/mounts", "r");
1895 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001896 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1897 return -1;
1898 }
1899
1900 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001901 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001902 char loopDir[loopDirLen + 2];
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001903 strlcpy(loopDir, VolumeManager::LOOPDIR, sizeof(loopDir));
Kenny Root508c0e12010-07-12 09:59:49 -07001904 loopDir[loopDirLen++] = '/';
1905 loopDir[loopDirLen] = '\0';
1906
Yabin Cuid1104f72015-01-02 13:28:28 -08001907 mntent* mentry;
1908 while ((mentry = getmntent(fp)) != NULL) {
1909 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001910 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001911 if (fd >= 0) {
1912 struct loop_info64 li;
1913 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1914 cli->sendMsg(ResponseCode::AsecListResult,
1915 (const char*) li.lo_file_name, false);
1916 }
1917 close(fd);
1918 }
1919 }
1920 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001921 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001922 return 0;
1923}
1924
Jeff Sharkey9c484982015-03-31 10:35:33 -07001925extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001926 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001927 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001928}
1929
San Mehata19b2502010-01-06 10:33:53 -08001930bool VolumeManager::isMountpointMounted(const char *mp)
1931{
Yabin Cuid1104f72015-01-02 13:28:28 -08001932 FILE *fp = setmntent("/proc/mounts", "r");
1933 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001934 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001935 return false;
1936 }
1937
Yabin Cuid1104f72015-01-02 13:28:28 -08001938 bool found_mp = false;
1939 mntent* mentry;
1940 while ((mentry = getmntent(fp)) != NULL) {
1941 if (strcmp(mentry->mnt_dir, mp) == 0) {
1942 found_mp = true;
1943 break;
San Mehata19b2502010-01-06 10:33:53 -08001944 }
San Mehata19b2502010-01-06 10:33:53 -08001945 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001946 endmntent(fp);
1947 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001948}
1949
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001950int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001951 // Only offer to create directories for paths managed by vold
1952 if (strncmp(path, "/storage/", 9) == 0) {
1953 // fs_mkdirs() does symlink checking and relative path enforcement
1954 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001955 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001956 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001957 return -EINVAL;
1958 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001959}