blob: 1a8c79d9a0441a9ed3879eab00f0a56e98d58230 [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>
Jeff Vander Stoepa997db72017-10-23 17:12:31 -070040#include <android-base/parseint.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080041#include <android-base/stringprintf.h>
Tri Voe98adee2018-04-16 14:27:10 -070042#include <android-base/strings.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070043#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070044#include <cutils/log.h>
45
Robert Craigb9e3ba52014-02-04 10:53:00 -050046#include <selinux/android.h>
47
San Mehatfd7f5872009-10-12 11:32:47 -070048#include <sysutils/NetlinkEvent.h>
49
Kenny Root344ca102012-04-03 17:23:01 -070050#include <private/android_filesystem_config.h>
51
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070052#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070053#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070054#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070055#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080056#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080057#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070058#include "fs/Ext4.h"
59#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070060#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080061#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080062#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080063#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090064#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070065#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080066
Mike Lockwood97f2fc12011-06-07 10:51:38 -070067#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
68
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070069#define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
70 + ((number) & (~((1U << (po2)) - 1))))
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070071
Jeff Sharkey36801cc2015-03-13 16:09:20 -070072using android::base::StringPrintf;
73
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070074/*
75 * Path to external storage where *only* root can access ASEC image files
76 */
77const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
78
79/*
80 * Path to internal storage where *only* root can access ASEC image files
81 */
82const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
83
84/*
85 * Path to where secure containers are mounted
86 */
87const char *VolumeManager::ASECDIR = "/mnt/asec";
88
89/*
90 * Path to where OBBs are mounted
91 */
92const char *VolumeManager::LOOPDIR = "/mnt/obb";
93
Keun-young Park375ac252017-08-02 17:45:48 -070094bool VolumeManager::shutting_down = false;
95
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060096static const char* kPathUserMount = "/mnt/user";
97static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
98
99static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
100
101/* 512MiB is large enough for testing purposes */
102static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700103
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700104static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +0800105static const unsigned int kMajorBlockExperimentalMin = 240;
106static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700107
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700108/* writes superblock at end of file or device given by name */
109static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700110 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700111 if (sbfd < 0) {
112 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
113 return -1;
114 }
115
116 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
117 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
118 close(sbfd);
119 return -1;
120 }
121
122 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
123 SLOGE("Failed to write superblock (%s)", strerror(errno));
124 close(sbfd);
125 return -1;
126 }
127 close(sbfd);
128 return 0;
129}
130
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200131static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700132 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
133 // preventing costly operations or unexpected ENOSPC error.
134 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200135 unsigned long clusterSectors = 4096 / 512;
136 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700137 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700138 return ROUND_UP_POWER_OF_2(numSectors, 3);
139}
140
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200141static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700142 /*
143 * Add some headroom
144 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200145 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700146 numSectors += fatSize + 2;
147 /*
148 * FAT is aligned to 32 kb with 512b sectors.
149 */
150 return ROUND_UP_POWER_OF_2(numSectors, 6);
151}
152
153static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
154 if (Loop::lookupActive(idHash, buffer, len)) {
155 if (Loop::create(idHash, asecFileName, buffer, len)) {
156 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
157 return -1;
158 }
159 if (debug) {
160 SLOGD("New loop device created at %s", buffer);
161 }
162 } else {
163 if (debug) {
164 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
165 }
166 }
167 return 0;
168}
169
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200170static 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 -0700171 if (strcmp(key, "none")) {
172 if (Devmapper::lookupActive(idHash, buffer, len)) {
173 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
174 buffer, len)) {
175 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
176 return -1;
177 }
178 if (debug) {
179 SLOGD("New devmapper instance created at %s", buffer);
180 }
181 } else {
182 if (debug) {
183 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
184 }
185 }
186 *createdDMDevice = true;
187 } else {
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600188 strlcpy(buffer, loopDevice, len);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700189 *createdDMDevice = false;
190 }
191 return 0;
192}
193
194static void waitForDevMapper(const char *dmDevice) {
195 /*
196 * Wait for the device mapper node to be created. Sometimes it takes a
197 * while. Wait for up to 1 second. We could also inspect incoming uevents,
198 * but that would take more effort.
199 */
200 int tries = 25;
201 while (tries--) {
202 if (!access(dmDevice, F_OK) || errno != ENOENT) {
203 break;
204 }
205 usleep(40 * 1000);
206 }
207}
208
San Mehatf1b736b2009-10-10 17:22:08 -0700209VolumeManager *VolumeManager::sInstance = NULL;
210
211VolumeManager *VolumeManager::Instance() {
212 if (!sInstance)
213 sInstance = new VolumeManager();
214 return sInstance;
215}
216
217VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800218 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800219 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700220 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400221 mUmsSharingCount = 0;
222 mSavedDirtyRatio = -1;
223 // set dirty ratio to 0 when UMS is active
224 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700225}
226
227VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800228 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700229}
230
Kenny Root7b18a7b2010-03-15 13:13:41 -0700231char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700232 static const char* digits = "0123456789abcdef";
233
Kenny Root7b18a7b2010-03-15 13:13:41 -0700234 unsigned char sig[MD5_DIGEST_LENGTH];
235
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700236 if (buffer == NULL) {
237 SLOGE("Destination buffer is NULL");
238 errno = ESPIPE;
239 return NULL;
240 } else if (id == NULL) {
241 SLOGE("Source buffer is NULL");
242 errno = ESPIPE;
243 return NULL;
244 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800245 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700246 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800247 errno = ESPIPE;
248 return NULL;
249 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700250
251 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800252
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700253 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700254 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700255 *p++ = digits[sig[i] >> 4];
256 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800257 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700258 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800259
260 return buffer;
261}
262
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600263int VolumeManager::updateVirtualDisk() {
264 if (property_get_bool(kPropVirtualDisk, false)) {
265 if (access(kPathVirtualDisk, F_OK) != 0) {
266 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
267 }
268
269 if (mVirtualDisk == nullptr) {
270 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
271 LOG(ERROR) << "Failed to create virtual disk";
272 return -1;
273 }
274
275 struct stat buf;
276 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
277 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
278 return -1;
279 }
280
281 auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
282 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
283 disk->create();
284 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
285 mDisks.push_back(mVirtualDisk);
286 }
287 } else {
288 if (mVirtualDisk != nullptr) {
289 dev_t device = mVirtualDisk->getDevice();
290
291 auto i = mDisks.begin();
292 while (i != mDisks.end()) {
293 if ((*i)->getDevice() == device) {
294 (*i)->destroy();
295 i = mDisks.erase(i);
296 } else {
297 ++i;
298 }
299 }
300
301 Loop::destroyByDevice(mVirtualDiskPath.c_str());
302 mVirtualDisk = nullptr;
303 }
304
305 if (access(kPathVirtualDisk, F_OK) == 0) {
306 unlink(kPathVirtualDisk);
307 }
308 }
309 return 0;
310}
311
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700312int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800313 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700314 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800315}
316
San Mehatf1b736b2009-10-10 17:22:08 -0700317int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700318 // Always start from a clean slate by unmounting everything in
319 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700320 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700321
322 // Assume that we always have an emulated volume on internal
323 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700324 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700325 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700326 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700327 mInternalEmulated->create();
328
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600329 // Consider creating a virtual disk
330 updateVirtualDisk();
331
San Mehatf1b736b2009-10-10 17:22:08 -0700332 return 0;
333}
334
335int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700336 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700337 mInternalEmulated->destroy();
338 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700339 return 0;
340}
341
San Mehatfd7f5872009-10-12 11:32:47 -0700342void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700343 std::lock_guard<std::mutex> lock(mLock);
344
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700345 if (mDebug) {
346 LOG(VERBOSE) << "----------------";
347 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
348 evt->dump();
349 }
San Mehatf1b736b2009-10-10 17:22:08 -0700350
Mateusz Nowak64403792015-08-03 16:39:19 +0200351 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
352 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700353
354 if (devType != "disk") return;
355
356 int major = atoi(evt->findParam("MAJOR"));
357 int minor = atoi(evt->findParam("MINOR"));
358 dev_t device = makedev(major, minor);
359
360 switch (evt->getAction()) {
361 case NetlinkEvent::Action::kAdd: {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700362 for (const auto& source : mDiskSources) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700363 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800364 // For now, assume that MMC and virtio-blk (the latter is
365 // emulator-specific; see Disk.cpp for details) devices are SD,
366 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700367 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800368 if (major == kMajorBlockMmc
369 || (android::vold::IsRunningInEmulator()
370 && major >= (int) kMajorBlockExperimentalMin
371 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700372 flags |= android::vold::Disk::Flags::kSd;
373 } else {
374 flags |= android::vold::Disk::Flags::kUsb;
375 }
376
377 auto disk = new android::vold::Disk(eventPath, device,
378 source->getNickname(), flags);
379 disk->create();
380 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
381 break;
382 }
383 }
384 break;
385 }
386 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700387 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700388 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700389 if (disk->getDevice() == device) {
390 disk->readMetadata();
391 disk->readPartitions();
392 }
393 }
394 break;
395 }
396 case NetlinkEvent::Action::kRemove: {
397 auto i = mDisks.begin();
398 while (i != mDisks.end()) {
399 if ((*i)->getDevice() == device) {
400 (*i)->destroy();
401 i = mDisks.erase(i);
402 } else {
403 ++i;
404 }
405 }
406 break;
407 }
408 default: {
409 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
410 break;
411 }
412 }
413}
414
415void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800416 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700417 mDiskSources.push_back(diskSource);
418}
419
420std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
421 for (auto disk : mDisks) {
422 if (disk->getId() == id) {
423 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700424 }
425 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700426 return nullptr;
427}
San Mehatf1b736b2009-10-10 17:22:08 -0700428
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700429std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800430 // Vold could receive "mount" after "shutdown" command in the extreme case.
431 // If this happens, mInternalEmulated will equal nullptr and
432 // we need to deal with it in order to avoid null pointer crash.
433 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700434 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700435 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700436 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700437 auto vol = disk->findVolume(id);
438 if (vol != nullptr) {
439 return vol;
440 }
441 }
442 return nullptr;
443}
444
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700445void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
446 std::list<std::string>& list) {
447 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700448 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700449 disk->listVolumes(type, list);
450 }
451}
452
453nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700454 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700455 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700456 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700457 } else {
458 auto vol = findVolume(id);
459 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
460 path = vol->getPath();
461 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700462 }
463
464 if (path.empty()) {
465 LOG(WARNING) << "Failed to find volume for " << id;
466 return -1;
467 }
468
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700469 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700470}
471
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700472int VolumeManager::forgetPartition(const std::string& partGuid) {
473 std::string normalizedGuid;
474 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
475 LOG(WARNING) << "Invalid GUID " << partGuid;
476 return -1;
477 }
478
479 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
480 if (unlink(keyPath.c_str()) != 0) {
481 LOG(ERROR) << "Failed to unlink " << keyPath;
482 return -1;
483 }
484
485 return 0;
486}
487
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700488int VolumeManager::linkPrimary(userid_t userId) {
489 std::string source(mPrimary->getPath());
490 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
491 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700492 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700493 }
494
495 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
496 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
497 if (errno != ENOENT) {
498 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
499 }
500 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700501 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700502 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
503 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
504 strerror(errno));
505 return -errno;
506 }
507 return 0;
508}
509
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700510int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
511 mAddedUsers[userId] = userSerialNumber;
512 return 0;
513}
514
515int VolumeManager::onUserRemoved(userid_t userId) {
516 mAddedUsers.erase(userId);
517 return 0;
518}
519
520int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700521 // Note that sometimes the system will spin up processes from Zygote
522 // before actually starting the user, so we're okay if Zygote
523 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600524 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700525 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
526
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700527 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700528 if (mPrimary) {
529 linkPrimary(userId);
530 }
531 return 0;
532}
533
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700534int VolumeManager::onUserStopped(userid_t userId) {
535 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700536 return 0;
537}
538
539int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
540 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700541 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700542 linkPrimary(userId);
543 }
544 return 0;
545}
546
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700547static int unmount_tree(const char* path) {
548 size_t path_len = strlen(path);
549
550 FILE* fp = setmntent("/proc/mounts", "r");
551 if (fp == NULL) {
552 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
553 return -errno;
554 }
555
556 // Some volumes can be stacked on each other, so force unmount in
557 // reverse order to give us the best chance of success.
558 std::list<std::string> toUnmount;
559 mntent* mentry;
560 while ((mentry = getmntent(fp)) != NULL) {
561 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
562 toUnmount.push_front(std::string(mentry->mnt_dir));
563 }
564 }
565 endmntent(fp);
566
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700567 for (const auto& path : toUnmount) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700568 if (umount2(path.c_str(), MNT_DETACH)) {
569 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
570 }
571 }
572 return 0;
573}
574
Jeff Sharkey66270a22015-06-24 11:49:24 -0700575int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
576 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
577
578 DIR* dir;
579 struct dirent* de;
580 char rootName[PATH_MAX];
581 char pidName[PATH_MAX];
582 int pidFd;
583 int nsFd;
584 struct stat sb;
585 pid_t child;
586
587 if (!(dir = opendir("/proc"))) {
588 PLOG(ERROR) << "Failed to opendir";
589 return -1;
590 }
591
592 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900593 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700594 PLOG(ERROR) << "Failed to readlink";
595 closedir(dir);
596 return -1;
597 }
598
599 // Poke through all running PIDs look for apps running as UID
600 while ((de = readdir(dir))) {
Jeff Vander Stoepa997db72017-10-23 17:12:31 -0700601 pid_t pid;
602 if (de->d_type != DT_DIR) continue;
603 if (!android::base::ParseInt(de->d_name, &pid)) continue;
604
Jeff Sharkey66270a22015-06-24 11:49:24 -0700605 pidFd = -1;
606 nsFd = -1;
607
608 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
609 if (pidFd < 0) {
610 goto next;
611 }
612 if (fstat(pidFd, &sb) != 0) {
613 PLOG(WARNING) << "Failed to stat " << de->d_name;
614 goto next;
615 }
616 if (sb.st_uid != uid) {
617 goto next;
618 }
619
620 // Matches so far, but refuse to touch if in root namespace
621 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900622 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700623 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
624 goto next;
625 }
626 if (!strcmp(rootName, pidName)) {
627 LOG(WARNING) << "Skipping due to root namespace";
628 goto next;
629 }
630
631 // We purposefully leave the namespace open across the fork
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600632 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700633 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700634 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700635 goto next;
636 }
637
638 if (!(child = fork())) {
639 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700640 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700641 _exit(1);
642 }
643
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700644 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700645
646 std::string storageSource;
647 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700648 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700649 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700650 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700651 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700652 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700653 } else {
654 // Sane default of no storage visible
655 _exit(0);
656 }
657 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
Hidehiko Abe674bed12016-03-09 16:42:10 +0900658 NULL, MS_BIND | MS_REC, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700659 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
660 << de->d_name;
661 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700662 }
Hidehiko Abe674bed12016-03-09 16:42:10 +0900663 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
664 MS_REC | MS_SLAVE, NULL)) == -1) {
665 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
666 << de->d_name;
667 _exit(1);
668 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700669
670 // Mount user-specific symlink helper into place
671 userid_t user_id = multiuser_get_user_id(uid);
672 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
673 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
674 NULL, MS_BIND, NULL)) == -1) {
675 PLOG(ERROR) << "Failed to mount " << userSource << " for "
676 << de->d_name;
677 _exit(1);
678 }
679
Jeff Sharkey66270a22015-06-24 11:49:24 -0700680 _exit(0);
681 }
682
683 if (child == -1) {
684 PLOG(ERROR) << "Failed to fork";
685 goto next;
686 } else {
687 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
688 }
689
690next:
691 close(nsFd);
692 close(pidFd);
693 }
694 closedir(dir);
695 return 0;
696}
697
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700698int VolumeManager::reset() {
699 // Tear down all existing disks/volumes and start from a blank slate so
700 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800701 if (mInternalEmulated != nullptr) {
702 mInternalEmulated->destroy();
703 mInternalEmulated->create();
704 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700705 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700706 disk->destroy();
707 disk->create();
708 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600709 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700710 mAddedUsers.clear();
711 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700712 return 0;
713}
714
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700715// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700716int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700717 if (mInternalEmulated == nullptr) {
718 return 0; // already shutdown
719 }
Keun-young Park375ac252017-08-02 17:45:48 -0700720 shutting_down = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700721 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700722 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700723 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700724 disk->destroy();
725 }
726 mDisks.clear();
Keun-young Park375ac252017-08-02 17:45:48 -0700727 shutting_down = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700728 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700729}
730
Jeff Sharkey9c484982015-03-31 10:35:33 -0700731int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700732 std::lock_guard<std::mutex> lock(mLock);
733
Jeff Sharkey9c484982015-03-31 10:35:33 -0700734 // First, try gracefully unmounting all known devices
735 if (mInternalEmulated != nullptr) {
736 mInternalEmulated->unmount();
737 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700738 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700739 disk->unmountAll();
740 }
741
742 // Worst case we might have some stale mounts lurking around, so
743 // force unmount those just to be safe.
744 FILE* fp = setmntent("/proc/mounts", "r");
745 if (fp == NULL) {
746 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
747 return -errno;
748 }
749
750 // Some volumes can be stacked on each other, so force unmount in
751 // reverse order to give us the best chance of success.
752 std::list<std::string> toUnmount;
753 mntent* mentry;
754 while ((mentry = getmntent(fp)) != NULL) {
Tri Voe98adee2018-04-16 14:27:10 -0700755 auto test = std::string(mentry->mnt_dir);
756 if ((android::base::StartsWith(test, "/mnt/") &&
Bowgo Tsaic0cd37b2018-06-29 10:31:07 +0800757 !android::base::StartsWith(test, "/mnt/vendor") &&
758 !android::base::StartsWith(test, "/mnt/product")) ||
Tri Voe98adee2018-04-16 14:27:10 -0700759 android::base::StartsWith(test, "/storage/")) {
760 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700761 }
762 }
763 endmntent(fp);
764
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700765 for (const auto& path : toUnmount) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700766 SLOGW("Tearing down stale mount %s", path.c_str());
767 android::vold::ForceUnmount(path);
768 }
769
770 return 0;
771}
772
Kenny Root508c0e12010-07-12 09:59:49 -0700773int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
774 char idHash[33];
775 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
776 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
777 return -1;
778 }
779
780 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700781 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400782 if ((written < 0) || (written >= mountPathLen)) {
783 errno = EINVAL;
784 return -1;
785 }
Kenny Root508c0e12010-07-12 09:59:49 -0700786
787 if (access(mountPath, F_OK)) {
788 errno = ENOENT;
789 return -1;
790 }
791
792 return 0;
793}
794
San Mehata19b2502010-01-06 10:33:53 -0800795int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700796 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700797
Nick Kralevich0de7c612014-01-27 14:58:06 -0800798 if (!isLegalAsecId(id)) {
799 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
800 errno = EINVAL;
801 return -1;
802 }
803
Kenny Root344ca102012-04-03 17:23:01 -0700804 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
805 SLOGE("Couldn't find ASEC %s", id);
806 return -1;
807 }
San Mehat88ac2c02010-03-23 11:15:58 -0700808
809 memset(buffer, 0, maxlen);
810 if (access(asecFileName, F_OK)) {
811 errno = ENOENT;
812 return -1;
813 }
San Mehata19b2502010-01-06 10:33:53 -0800814
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700815 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400816 if ((written < 0) || (written >= maxlen)) {
817 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
818 errno = EINVAL;
819 return -1;
820 }
821
San Mehata19b2502010-01-06 10:33:53 -0800822 return 0;
823}
824
Dianne Hackborn736910c2011-06-27 13:37:07 -0700825int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
826 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700827
Nick Kralevich0de7c612014-01-27 14:58:06 -0800828 if (!isLegalAsecId(id)) {
829 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
830 errno = EINVAL;
831 return -1;
832 }
833
Kenny Root344ca102012-04-03 17:23:01 -0700834 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
835 SLOGE("Couldn't find ASEC %s", id);
836 return -1;
837 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700838
839 memset(buffer, 0, maxlen);
840 if (access(asecFileName, F_OK)) {
841 errno = ENOENT;
842 return -1;
843 }
844
rpcraigd1c226f2012-10-09 06:58:16 -0400845 int written = snprintf(buffer, maxlen, "%s", asecFileName);
846 if ((written < 0) || (written >= maxlen)) {
847 errno = EINVAL;
848 return -1;
849 }
850
Dianne Hackborn736910c2011-06-27 13:37:07 -0700851 return 0;
852}
853
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200854int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700855 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800856 struct asec_superblock sb;
857 memset(&sb, 0, sizeof(sb));
858
Nick Kralevich0de7c612014-01-27 14:58:06 -0800859 if (!isLegalAsecId(id)) {
860 SLOGE("createAsec: Invalid asec id \"%s\"", id);
861 errno = EINVAL;
862 return -1;
863 }
864
Kenny Root344ca102012-04-03 17:23:01 -0700865 const bool wantFilesystem = strcmp(fstype, "none");
866 bool usingExt4 = false;
867 if (wantFilesystem) {
868 usingExt4 = !strcmp(fstype, "ext4");
869 if (usingExt4) {
870 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
871 } else if (strcmp(fstype, "fat")) {
872 SLOGE("Invalid filesystem type %s", fstype);
873 errno = EINVAL;
874 return -1;
875 }
876 }
877
San Mehatfcf24fe2010-03-03 12:37:32 -0800878 sb.magic = ASEC_SB_MAGIC;
879 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800880
San Mehatd31e3802010-02-18 08:37:45 -0800881 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200882 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800883 errno = EINVAL;
884 return -1;
885 }
886
San Mehata19b2502010-01-06 10:33:53 -0800887 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700888
889 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
890 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
891 asecFileName, strerror(errno));
892 errno = EADDRINUSE;
893 return -1;
894 }
895
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700896 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700897
rpcraigd1c226f2012-10-09 06:58:16 -0400898 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
899 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
900 errno = EINVAL;
901 return -1;
902 }
San Mehata19b2502010-01-06 10:33:53 -0800903
904 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700905 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700906 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800907 errno = EADDRINUSE;
908 return -1;
909 }
910
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200911 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700912 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700913 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700914 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700915 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800916
917 // Add +1 for our superblock which is at the end
918 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700919 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800920 return -1;
921 }
922
San Mehatd9a4e352010-03-12 13:32:47 -0800923 char idHash[33];
924 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700925 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800926 unlink(asecFileName);
927 return -1;
928 }
929
San Mehata19b2502010-01-06 10:33:53 -0800930 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800931 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700932 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800933 unlink(asecFileName);
934 return -1;
935 }
936
San Mehatb78a32c2010-01-10 13:02:12 -0800937 char dmDevice[255];
938 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800939
San Mehatb78a32c2010-01-10 13:02:12 -0800940 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800941 // XXX: This is all we support for now
942 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800943 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800944 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700945 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800946 Loop::destroyByDevice(loopDevice);
947 unlink(asecFileName);
948 return -1;
949 }
950 cleanupDm = true;
951 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800952 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600953 strlcpy(dmDevice, loopDevice, sizeof(dmDevice));
San Mehatb78a32c2010-01-10 13:02:12 -0800954 }
955
San Mehatfcf24fe2010-03-03 12:37:32 -0800956 /*
957 * Drop down the superblock at the end of the file
958 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700959 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800960 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800961 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800962 }
963 Loop::destroyByDevice(loopDevice);
964 unlink(asecFileName);
965 return -1;
966 }
967
Kenny Root344ca102012-04-03 17:23:01 -0700968 if (wantFilesystem) {
969 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400970 char mountPoint[255];
971
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700972 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400973 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
974 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
975 if (cleanupDm) {
976 Devmapper::destroy(idHash);
977 }
978 Loop::destroyByDevice(loopDevice);
979 unlink(asecFileName);
980 return -1;
981 }
rpcraiga54e13a2012-09-21 14:17:08 -0400982
Kenny Root344ca102012-04-03 17:23:01 -0700983 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700984 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700985 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700986 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800987 }
San Mehata19b2502010-01-06 10:33:53 -0800988
Kenny Root344ca102012-04-03 17:23:01 -0700989 if (formatStatus < 0) {
990 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800991 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800992 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800993 }
San Mehateb13a902010-01-07 12:12:50 -0800994 Loop::destroyByDevice(loopDevice);
995 unlink(asecFileName);
996 return -1;
997 }
Kenny Root344ca102012-04-03 17:23:01 -0700998
Kenny Root344ca102012-04-03 17:23:01 -0700999 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -08001000 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001001 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -08001002 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001003 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001004 }
1005 Loop::destroyByDevice(loopDevice);
1006 unlink(asecFileName);
1007 return -1;
1008 }
San Mehatb78a32c2010-01-10 13:02:12 -08001009 }
San Mehata1091cb2010-02-28 20:17:20 -08001010
Kenny Root344ca102012-04-03 17:23:01 -07001011 int mountStatus;
1012 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001013 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
1014 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -07001015 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001016 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
1017 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -07001018 }
1019
1020 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -07001021 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -08001022 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001023 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001024 }
1025 Loop::destroyByDevice(loopDevice);
1026 unlink(asecFileName);
1027 return -1;
1028 }
Kenny Root344ca102012-04-03 17:23:01 -07001029
1030 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001031 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001032 if (dirfd >= 0) {
1033 if (fchown(dirfd, ownerUid, AID_SYSTEM)
1034 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
1035 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
1036 }
1037 close(dirfd);
1038 }
1039 }
San Mehata1091cb2010-02-28 20:17:20 -08001040 } else {
San Mehat97ac40e2010-03-24 10:24:19 -07001041 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -08001042 }
San Mehat88705162010-01-15 09:26:28 -08001043
Kenny Rootcbacf782010-09-24 15:11:48 -07001044 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -08001045 return 0;
1046}
1047
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001048int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001049 char asecFileName[255];
1050 char mountPoint[255];
1051 bool cleanupDm = false;
1052
1053 if (!isLegalAsecId(id)) {
1054 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
1055 errno = EINVAL;
1056 return -1;
1057 }
1058
1059 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1060 SLOGE("Couldn't find ASEC %s", id);
1061 return -1;
1062 }
1063
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001064 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001065 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1066 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
1067 return -1;
1068 }
1069
1070 if (isMountpointMounted(mountPoint)) {
1071 SLOGE("ASEC %s mounted. Unmount before resizing", id);
1072 errno = EBUSY;
1073 return -1;
1074 }
1075
1076 struct asec_superblock sb;
1077 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001078 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001079
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001080 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001081 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
1082 return -1;
1083 }
1084
1085 struct stat info;
1086 if (fstat(fd, &info) < 0) {
1087 SLOGE("Failed to get file size (%s)", strerror(errno));
1088 close(fd);
1089 return -1;
1090 }
1091
1092 oldNumSec = info.st_size / 512;
1093
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001094 /*
1095 * Try to read superblock.
1096 */
1097 memset(&sb, 0, sizeof(struct asec_superblock));
1098 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1099 SLOGE("lseek failed (%s)", strerror(errno));
1100 close(fd);
1101 return -1;
1102 }
1103 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1104 SLOGE("superblock read failed (%s)", strerror(errno));
1105 close(fd);
1106 return -1;
1107 }
1108 close(fd);
1109
1110 if (mDebug) {
1111 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1112 }
1113 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1114 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1115 errno = EMEDIUMTYPE;
1116 return -1;
1117 }
1118
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001119 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001120 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1121 SLOGE("Only ext4 partitions are supported for resize");
1122 errno = EINVAL;
1123 return -1;
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001124 } else {
1125 numImgSectors = adjustSectorNumExt4(numSectors);
1126 }
1127
1128 /*
1129 * add one block for the superblock
1130 */
1131 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1132 if (oldNumSec == numImgSectors + 1) {
1133 SLOGW("Size unchanged; ignoring resize request");
1134 return 0;
1135 } else if (oldNumSec > numImgSectors + 1) {
1136 SLOGE("Only growing is currently supported.");
1137 close(fd);
1138 return -1;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001139 }
1140
1141 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1142 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1143 return -1;
1144 }
1145
1146 /*
1147 * Drop down a copy of the superblock at the end of the file
1148 */
1149 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1150 goto fail;
1151
1152 char idHash[33];
1153 if (!asecHash(id, idHash, sizeof(idHash))) {
1154 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1155 goto fail;
1156 }
1157
1158 char loopDevice[255];
1159 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1160 goto fail;
1161
1162 char dmDevice[255];
1163
1164 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1165 Loop::destroyByDevice(loopDevice);
1166 goto fail;
1167 }
1168
1169 /*
1170 * Wait for the device mapper node to be created.
1171 */
1172 waitForDevMapper(dmDevice);
1173
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001174 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001175 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1176 if (cleanupDm) {
1177 Devmapper::destroy(idHash);
1178 }
1179 Loop::destroyByDevice(loopDevice);
1180 goto fail;
1181 }
1182
1183 return 0;
1184fail:
1185 Loop::resizeImageFile(asecFileName, oldNumSec);
1186 return -1;
1187}
1188
San Mehata19b2502010-01-06 10:33:53 -08001189int VolumeManager::finalizeAsec(const char *id) {
1190 char asecFileName[255];
1191 char loopDevice[255];
1192 char mountPoint[255];
1193
Nick Kralevich0de7c612014-01-27 14:58:06 -08001194 if (!isLegalAsecId(id)) {
1195 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1196 errno = EINVAL;
1197 return -1;
1198 }
1199
Kenny Root344ca102012-04-03 17:23:01 -07001200 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1201 SLOGE("Couldn't find ASEC %s", id);
1202 return -1;
1203 }
San Mehata19b2502010-01-06 10:33:53 -08001204
San Mehatd9a4e352010-03-12 13:32:47 -08001205 char idHash[33];
1206 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001207 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001208 return -1;
1209 }
1210
1211 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001212 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001213 return -1;
1214 }
1215
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001216 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001217 struct asec_superblock sb;
1218
1219 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1220 return -1;
1221 }
1222
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001223 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001224 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1225 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1226 return -1;
1227 }
Kenny Root344ca102012-04-03 17:23:01 -07001228
1229 int result = 0;
1230 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001231 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1232 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001233 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001234 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1235 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001236 }
1237
1238 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001239 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001240 return -1;
1241 }
1242
San Mehatd9a4e352010-03-12 13:32:47 -08001243 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001244 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001245 }
San Mehata19b2502010-01-06 10:33:53 -08001246 return 0;
1247}
1248
Kenny Root344ca102012-04-03 17:23:01 -07001249int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1250 char asecFileName[255];
1251 char loopDevice[255];
1252 char mountPoint[255];
1253
1254 if (gid < AID_APP) {
1255 SLOGE("Group ID is not in application range");
1256 return -1;
1257 }
1258
Nick Kralevich0de7c612014-01-27 14:58:06 -08001259 if (!isLegalAsecId(id)) {
1260 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1261 errno = EINVAL;
1262 return -1;
1263 }
1264
Kenny Root344ca102012-04-03 17:23:01 -07001265 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1266 SLOGE("Couldn't find ASEC %s", id);
1267 return -1;
1268 }
1269
1270 char idHash[33];
1271 if (!asecHash(id, idHash, sizeof(idHash))) {
1272 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1273 return -1;
1274 }
1275
1276 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1277 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1278 return -1;
1279 }
1280
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001281 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001282 struct asec_superblock sb;
1283
1284 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1285 return -1;
1286 }
1287
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001288 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001289 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1290 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1291 return -1;
1292 }
Kenny Root344ca102012-04-03 17:23:01 -07001293
1294 int result = 0;
1295 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1296 return 0;
1297 }
1298
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001299 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001300 false /* read-only */,
1301 true /* remount */,
1302 false /* executable */);
1303 if (ret) {
1304 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1305 return -1;
1306 }
1307
1308 char *paths[] = { mountPoint, NULL };
1309
1310 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1311 if (fts) {
1312 // Traverse the entire hierarchy and chown to system UID.
1313 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1314 // We don't care about the lost+found directory.
1315 if (!strcmp(ftsent->fts_name, "lost+found")) {
1316 continue;
1317 }
1318
1319 /*
1320 * There can only be one file marked as private right now.
1321 * This should be more robust, but it satisfies the requirements
1322 * we have for right now.
1323 */
1324 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1325
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001326 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001327 if (fd < 0) {
1328 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1329 result = -1;
1330 continue;
1331 }
1332
1333 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1334
1335 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001336 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001337 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001338 result |= fchmod(fd, privateFile ? 0640 : 0644);
1339 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001340
Stephen Smalley5093e612014-02-12 09:43:08 -05001341 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001342 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1343 result |= -1;
1344 }
1345
Kenny Root344ca102012-04-03 17:23:01 -07001346 close(fd);
1347 }
1348 fts_close(fts);
1349
1350 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001351 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001352 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1353 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1354 result |= -1;
1355 }
1356 close(dirfd);
1357 } else {
1358 result |= -1;
1359 }
1360
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001361 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001362 true /* read-only */,
1363 true /* remount */,
1364 true /* execute */);
1365
1366 if (result) {
1367 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1368 return -1;
1369 }
1370
1371 if (mDebug) {
1372 SLOGD("ASEC %s permissions fixed", id);
1373 }
1374 return 0;
1375}
1376
San Mehat048b0802010-01-23 08:17:06 -08001377int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001378 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001379 char *asecFilename2;
1380 char mountPoint[255];
1381
Kenny Root344ca102012-04-03 17:23:01 -07001382 const char *dir;
1383
Nick Kralevich0de7c612014-01-27 14:58:06 -08001384 if (!isLegalAsecId(id1)) {
1385 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1386 errno = EINVAL;
1387 return -1;
1388 }
1389
1390 if (!isLegalAsecId(id2)) {
1391 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1392 errno = EINVAL;
1393 return -1;
1394 }
1395
Kenny Root344ca102012-04-03 17:23:01 -07001396 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1397 SLOGE("Couldn't find ASEC %s", id1);
1398 return -1;
1399 }
1400
1401 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001402
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001403 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001404 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1405 SLOGE("Rename failed: couldn't construct mountpoint");
1406 goto out_err;
1407 }
1408
San Mehat048b0802010-01-23 08:17:06 -08001409 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001410 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001411 errno = EBUSY;
1412 goto out_err;
1413 }
1414
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001415 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001416 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1417 SLOGE("Rename failed: couldn't construct mountpoint2");
1418 goto out_err;
1419 }
1420
San Mehat96956ed2010-02-24 08:42:51 -08001421 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001422 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001423 errno = EBUSY;
1424 goto out_err;
1425 }
1426
San Mehat048b0802010-01-23 08:17:06 -08001427 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001428 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001429 errno = EADDRINUSE;
1430 goto out_err;
1431 }
1432
1433 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001434 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001435 goto out_err;
1436 }
1437
San Mehat048b0802010-01-23 08:17:06 -08001438 free(asecFilename2);
1439 return 0;
1440
1441out_err:
San Mehat048b0802010-01-23 08:17:06 -08001442 free(asecFilename2);
1443 return -1;
1444}
1445
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001446#define UNMOUNT_RETRIES 5
1447#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001448int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001449 char asecFileName[255];
1450 char mountPoint[255];
1451
Nick Kralevich0de7c612014-01-27 14:58:06 -08001452 if (!isLegalAsecId(id)) {
1453 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1454 errno = EINVAL;
1455 return -1;
1456 }
1457
Kenny Root344ca102012-04-03 17:23:01 -07001458 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1459 SLOGE("Couldn't find ASEC %s", id);
1460 return -1;
1461 }
1462
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001463 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001464 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1465 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1466 return -1;
1467 }
San Mehata19b2502010-01-06 10:33:53 -08001468
San Mehatd9a4e352010-03-12 13:32:47 -08001469 char idHash[33];
1470 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001471 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001472 return -1;
1473 }
1474
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001475 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1476}
1477
Kenny Root508c0e12010-07-12 09:59:49 -07001478int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001479 char mountPoint[255];
1480
1481 char idHash[33];
1482 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1483 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1484 return -1;
1485 }
1486
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001487 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001488 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1489 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1490 return -1;
1491 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001492
1493 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1494}
1495
1496int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1497 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001498 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001499 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001500 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001501 return -1;
1502 }
San Mehat23969932010-01-09 07:08:06 -08001503
San Mehatb78a32c2010-01-10 13:02:12 -08001504 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001505 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001506 rc = umount(mountPoint);
1507 if (!rc) {
1508 break;
San Mehata19b2502010-01-06 10:33:53 -08001509 }
San Mehatb78a32c2010-01-10 13:02:12 -08001510 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001511 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001512 rc = 0;
1513 break;
San Mehata19b2502010-01-06 10:33:53 -08001514 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001515 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001516 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001517
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001518 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001519
1520 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001521 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001522 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001523 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001524 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001525 }
San Mehat8c940ef2010-02-13 14:19:53 -08001526
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001527 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001528 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001529 }
1530
1531 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001532 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001533 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001534 return -1;
1535 }
1536
San Mehat12f4b892010-02-24 11:43:22 -08001537 int retries = 10;
1538
1539 while(retries--) {
1540 if (!rmdir(mountPoint)) {
1541 break;
1542 }
1543
San Mehat97ac40e2010-03-24 10:24:19 -07001544 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001545 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001546 }
1547
1548 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001549 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001550 }
San Mehat88705162010-01-15 09:26:28 -08001551
Paul Lawrence60dec162014-09-02 10:52:15 -07001552 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1553 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1554 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1555 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1556 continue;
1557 } else {
1558 break;
1559 }
San Mehata19b2502010-01-06 10:33:53 -08001560 }
1561
1562 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001563 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001564 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001565 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001566 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001567 }
San Mehat88705162010-01-15 09:26:28 -08001568
1569 AsecIdCollection::iterator it;
1570 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001571 ContainerData* cd = *it;
1572 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001573 free(*it);
1574 mActiveContainers->erase(it);
1575 break;
1576 }
1577 }
1578 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001579 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001580 }
San Mehatb78a32c2010-01-10 13:02:12 -08001581 return 0;
1582}
1583
San Mehat4ba89482010-02-18 09:00:18 -08001584int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001585 char asecFileName[255];
1586 char mountPoint[255];
1587
Nick Kralevich0de7c612014-01-27 14:58:06 -08001588 if (!isLegalAsecId(id)) {
1589 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1590 errno = EINVAL;
1591 return -1;
1592 }
1593
Kenny Root344ca102012-04-03 17:23:01 -07001594 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1595 SLOGE("Couldn't find ASEC %s", id);
1596 return -1;
1597 }
1598
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001599 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001600 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1601 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1602 return -1;
1603 }
San Mehatb78a32c2010-01-10 13:02:12 -08001604
San Mehat0586d542010-01-12 15:38:59 -08001605 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001606 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001607 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001608 }
San Mehat4ba89482010-02-18 09:00:18 -08001609 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001610 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001611 return -1;
1612 }
1613 }
San Mehata19b2502010-01-06 10:33:53 -08001614
San Mehat0586d542010-01-12 15:38:59 -08001615 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001616 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001617 return -1;
1618 }
San Mehata19b2502010-01-06 10:33:53 -08001619
San Mehatd9a4e352010-03-12 13:32:47 -08001620 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001621 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001622 }
San Mehata19b2502010-01-06 10:33:53 -08001623 return 0;
1624}
1625
Nick Kralevich0de7c612014-01-27 14:58:06 -08001626/*
1627 * Legal ASEC ids consist of alphanumeric characters, '-',
1628 * '_', or '.'. ".." is not allowed. The first or last character
1629 * of the ASEC id cannot be '.' (dot).
1630 */
1631bool VolumeManager::isLegalAsecId(const char *id) const {
1632 size_t i;
1633 size_t len = strlen(id);
1634
1635 if (len == 0) {
1636 return false;
1637 }
1638 if ((id[0] == '.') || (id[len - 1] == '.')) {
1639 return false;
1640 }
1641
1642 for (i = 0; i < len; i++) {
1643 if (id[i] == '.') {
1644 // i=0 is guaranteed never to have a dot. See above.
1645 if (id[i-1] == '.') return false;
1646 continue;
1647 }
1648 if (id[i] == '_' || id[i] == '-') continue;
1649 if (id[i] >= 'a' && id[i] <= 'z') continue;
1650 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1651 if (id[i] >= '0' && id[i] <= '9') continue;
1652 return false;
1653 }
1654
1655 return true;
1656}
1657
Kenny Root344ca102012-04-03 17:23:01 -07001658bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001659 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001660 if (dirfd < 0) {
1661 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001662 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001663 }
1664
Nick Kralevich25e581a2015-02-06 08:55:08 -08001665 struct stat sb;
1666 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1667 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001668
1669 close(dirfd);
1670
1671 return ret;
1672}
1673
1674int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1675 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001676 char *asecName;
1677
Nick Kralevich0de7c612014-01-27 14:58:06 -08001678 if (!isLegalAsecId(id)) {
1679 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1680 errno = EINVAL;
1681 return -1;
1682 }
1683
Kenny Root344ca102012-04-03 17:23:01 -07001684 if (asprintf(&asecName, "%s.asec", id) < 0) {
1685 SLOGE("Couldn't allocate string to write ASEC name");
1686 return -1;
1687 }
1688
1689 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001690 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1691 dir = VolumeManager::SEC_ASECDIR_INT;
1692 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1693 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001694 } else {
1695 free(asecName);
1696 return -1;
1697 }
1698
1699 if (directory != NULL) {
1700 *directory = dir;
1701 }
1702
1703 if (asecPath != NULL) {
1704 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001705 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1706 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001707 free(asecName);
1708 return -1;
1709 }
1710 }
1711
1712 free(asecName);
1713 return 0;
1714}
1715
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001716int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001717 char asecFileName[255];
1718 char mountPoint[255];
1719
Nick Kralevich0de7c612014-01-27 14:58:06 -08001720 if (!isLegalAsecId(id)) {
1721 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1722 errno = EINVAL;
1723 return -1;
1724 }
1725
Kenny Root344ca102012-04-03 17:23:01 -07001726 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1727 SLOGE("Couldn't find ASEC %s", id);
1728 return -1;
1729 }
1730
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001731 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001732 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001733 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001734 return -1;
1735 }
San Mehata19b2502010-01-06 10:33:53 -08001736
1737 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001738 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001739 errno = EBUSY;
1740 return -1;
1741 }
1742
San Mehatd9a4e352010-03-12 13:32:47 -08001743 char idHash[33];
1744 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001745 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001746 return -1;
1747 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001748
San Mehata19b2502010-01-06 10:33:53 -08001749 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001750 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1751 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001752
1753 char dmDevice[255];
1754 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001755
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001756 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001757 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001758
Kenny Root344ca102012-04-03 17:23:01 -07001759 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1760 return -1;
1761 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001762
San Mehatd9a4e352010-03-12 13:32:47 -08001763 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001764 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001765 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001766 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001767 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001768 Loop::destroyByDevice(loopDevice);
1769 errno = EMEDIUMTYPE;
1770 return -1;
1771 }
1772 nr_sec--; // We don't want the devmapping to extend onto our superblock
1773
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001774 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1775 Loop::destroyByDevice(loopDevice);
1776 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001777 }
1778
Kenny Root344ca102012-04-03 17:23:01 -07001779 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001780 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001781 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001782 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001783 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001784 }
1785 Loop::destroyByDevice(loopDevice);
1786 return -1;
1787 }
San Mehata19b2502010-01-06 10:33:53 -08001788 }
1789
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001790 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001791 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001792 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001793 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001794
Kenny Root344ca102012-04-03 17:23:01 -07001795 int result;
1796 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001797 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1798 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001799 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001800 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1801 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001802 }
1803
1804 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001805 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001806 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001807 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001808 }
1809 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001810 return -1;
1811 }
1812
Kenny Rootcbacf782010-09-24 15:11:48 -07001813 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001814 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001815 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001816 }
San Mehata19b2502010-01-06 10:33:53 -08001817 return 0;
1818}
1819
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001820/**
1821 * Mounts an image file <code>img</code>.
1822 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001823int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001824 char mountPoint[255];
1825
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001826 char idHash[33];
1827 if (!asecHash(img, idHash, sizeof(idHash))) {
1828 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1829 return -1;
1830 }
1831
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001832 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001833 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001834 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001835 return -1;
1836 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001837
1838 if (isMountpointMounted(mountPoint)) {
1839 SLOGE("Image %s already mounted", img);
1840 errno = EBUSY;
1841 return -1;
1842 }
1843
1844 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001845 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1846 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001847
1848 char dmDevice[255];
1849 bool cleanupDm = false;
1850 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001851 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001852
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001853 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001854 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1855 Loop::destroyByDevice(loopDevice);
1856 return -1;
1857 }
1858
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001859 get_blkdev_size(fd, &nr_sec);
1860 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001861 SLOGE("Failed to get loop size (%s)", strerror(errno));
1862 Loop::destroyByDevice(loopDevice);
1863 close(fd);
1864 return -1;
1865 }
1866
1867 close(fd);
1868
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001869 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001870 Loop::destroyByDevice(loopDevice);
1871 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001872 }
1873
1874 if (mkdir(mountPoint, 0755)) {
1875 if (errno != EEXIST) {
1876 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1877 if (cleanupDm) {
1878 Devmapper::destroy(idHash);
1879 }
1880 Loop::destroyByDevice(loopDevice);
1881 return -1;
1882 }
1883 }
1884
yoshiyuki hama476a6272015-01-28 16:37:23 +09001885 /*
1886 * Wait for the device mapper node to be created.
1887 */
1888 waitForDevMapper(dmDevice);
1889
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001890 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1891 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001892 SLOGE("Image mount failed (%s)", strerror(errno));
1893 if (cleanupDm) {
1894 Devmapper::destroy(idHash);
1895 }
1896 Loop::destroyByDevice(loopDevice);
1897 return -1;
1898 }
1899
Kenny Rootcbacf782010-09-24 15:11:48 -07001900 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001901 if (mDebug) {
1902 SLOGD("Image %s mounted", img);
1903 }
1904 return 0;
1905}
1906
Kenny Root508c0e12010-07-12 09:59:49 -07001907int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001908 FILE *fp = setmntent("/proc/mounts", "r");
1909 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001910 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1911 return -1;
1912 }
1913
1914 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001915 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001916 char loopDir[loopDirLen + 2];
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001917 strlcpy(loopDir, VolumeManager::LOOPDIR, sizeof(loopDir));
Kenny Root508c0e12010-07-12 09:59:49 -07001918 loopDir[loopDirLen++] = '/';
1919 loopDir[loopDirLen] = '\0';
1920
Yabin Cuid1104f72015-01-02 13:28:28 -08001921 mntent* mentry;
1922 while ((mentry = getmntent(fp)) != NULL) {
1923 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001924 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001925 if (fd >= 0) {
1926 struct loop_info64 li;
1927 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1928 cli->sendMsg(ResponseCode::AsecListResult,
1929 (const char*) li.lo_file_name, false);
1930 }
1931 close(fd);
1932 }
1933 }
1934 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001935 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001936 return 0;
1937}
1938
Jeff Sharkey9c484982015-03-31 10:35:33 -07001939extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001940 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001941 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001942}
1943
San Mehata19b2502010-01-06 10:33:53 -08001944bool VolumeManager::isMountpointMounted(const char *mp)
1945{
Yabin Cuid1104f72015-01-02 13:28:28 -08001946 FILE *fp = setmntent("/proc/mounts", "r");
1947 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001948 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001949 return false;
1950 }
1951
Yabin Cuid1104f72015-01-02 13:28:28 -08001952 bool found_mp = false;
1953 mntent* mentry;
1954 while ((mentry = getmntent(fp)) != NULL) {
1955 if (strcmp(mentry->mnt_dir, mp) == 0) {
1956 found_mp = true;
1957 break;
San Mehata19b2502010-01-06 10:33:53 -08001958 }
San Mehata19b2502010-01-06 10:33:53 -08001959 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001960 endmntent(fp);
1961 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001962}
1963
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001964int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001965 // Only offer to create directories for paths managed by vold
1966 if (strncmp(path, "/storage/", 9) == 0) {
1967 // fs_mkdirs() does symlink checking and relative path enforcement
1968 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001969 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001970 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001971 return -EINVAL;
1972 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001973}