blob: 25d46a6e7dbc10f6f96b44d12e88508366809fd8 [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>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070042#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070043#include <cutils/log.h>
44
Robert Craigb9e3ba52014-02-04 10:53:00 -050045#include <selinux/android.h>
46
San Mehatfd7f5872009-10-12 11:32:47 -070047#include <sysutils/NetlinkEvent.h>
48
Kenny Root344ca102012-04-03 17:23:01 -070049#include <private/android_filesystem_config.h>
50
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070051#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070052#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070053#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070054#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080055#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080056#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070057#include "fs/Ext4.h"
58#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070059#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080060#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080061#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080062#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090063#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070064#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080065
Mike Lockwood97f2fc12011-06-07 10:51:38 -070066#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
67
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070068#define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
69 + ((number) & (~((1U << (po2)) - 1))))
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070070
Jeff Sharkey36801cc2015-03-13 16:09:20 -070071using android::base::StringPrintf;
72
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070073/*
74 * Path to external storage where *only* root can access ASEC image files
75 */
76const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
77
78/*
79 * Path to internal storage where *only* root can access ASEC image files
80 */
81const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
82
83/*
84 * Path to where secure containers are mounted
85 */
86const char *VolumeManager::ASECDIR = "/mnt/asec";
87
88/*
89 * Path to where OBBs are mounted
90 */
91const char *VolumeManager::LOOPDIR = "/mnt/obb";
92
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060093static const char* kPathUserMount = "/mnt/user";
94static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
95
96static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
97
98/* 512MiB is large enough for testing purposes */
99static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700100
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +0800102static const unsigned int kMajorBlockExperimentalMin = 240;
103static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700104
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700105/* writes superblock at end of file or device given by name */
106static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700107 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700108 if (sbfd < 0) {
109 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
110 return -1;
111 }
112
113 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
114 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
115 close(sbfd);
116 return -1;
117 }
118
119 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
120 SLOGE("Failed to write superblock (%s)", strerror(errno));
121 close(sbfd);
122 return -1;
123 }
124 close(sbfd);
125 return 0;
126}
127
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200128static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700129 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
130 // preventing costly operations or unexpected ENOSPC error.
131 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200132 unsigned long clusterSectors = 4096 / 512;
133 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700134 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700135 return ROUND_UP_POWER_OF_2(numSectors, 3);
136}
137
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200138static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700139 /*
140 * Add some headroom
141 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200142 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700143 numSectors += fatSize + 2;
144 /*
145 * FAT is aligned to 32 kb with 512b sectors.
146 */
147 return ROUND_UP_POWER_OF_2(numSectors, 6);
148}
149
150static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
151 if (Loop::lookupActive(idHash, buffer, len)) {
152 if (Loop::create(idHash, asecFileName, buffer, len)) {
153 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
154 return -1;
155 }
156 if (debug) {
157 SLOGD("New loop device created at %s", buffer);
158 }
159 } else {
160 if (debug) {
161 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
162 }
163 }
164 return 0;
165}
166
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200167static 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 -0700168 if (strcmp(key, "none")) {
169 if (Devmapper::lookupActive(idHash, buffer, len)) {
170 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
171 buffer, len)) {
172 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
173 return -1;
174 }
175 if (debug) {
176 SLOGD("New devmapper instance created at %s", buffer);
177 }
178 } else {
179 if (debug) {
180 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
181 }
182 }
183 *createdDMDevice = true;
184 } else {
185 strcpy(buffer, loopDevice);
186 *createdDMDevice = false;
187 }
188 return 0;
189}
190
191static void waitForDevMapper(const char *dmDevice) {
192 /*
193 * Wait for the device mapper node to be created. Sometimes it takes a
194 * while. Wait for up to 1 second. We could also inspect incoming uevents,
195 * but that would take more effort.
196 */
197 int tries = 25;
198 while (tries--) {
199 if (!access(dmDevice, F_OK) || errno != ENOENT) {
200 break;
201 }
202 usleep(40 * 1000);
203 }
204}
205
San Mehatf1b736b2009-10-10 17:22:08 -0700206VolumeManager *VolumeManager::sInstance = NULL;
207
208VolumeManager *VolumeManager::Instance() {
209 if (!sInstance)
210 sInstance = new VolumeManager();
211 return sInstance;
212}
213
214VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800215 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800216 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700217 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400218 mUmsSharingCount = 0;
219 mSavedDirtyRatio = -1;
220 // set dirty ratio to 0 when UMS is active
221 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700222}
223
224VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800225 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700226}
227
Kenny Root7b18a7b2010-03-15 13:13:41 -0700228char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700229 static const char* digits = "0123456789abcdef";
230
Kenny Root7b18a7b2010-03-15 13:13:41 -0700231 unsigned char sig[MD5_DIGEST_LENGTH];
232
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700233 if (buffer == NULL) {
234 SLOGE("Destination buffer is NULL");
235 errno = ESPIPE;
236 return NULL;
237 } else if (id == NULL) {
238 SLOGE("Source buffer is NULL");
239 errno = ESPIPE;
240 return NULL;
241 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800242 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700243 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800244 errno = ESPIPE;
245 return NULL;
246 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700247
248 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800249
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700250 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700251 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700252 *p++ = digits[sig[i] >> 4];
253 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800254 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700255 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800256
257 return buffer;
258}
259
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600260int VolumeManager::updateVirtualDisk() {
261 if (property_get_bool(kPropVirtualDisk, false)) {
262 if (access(kPathVirtualDisk, F_OK) != 0) {
263 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
264 }
265
266 if (mVirtualDisk == nullptr) {
267 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
268 LOG(ERROR) << "Failed to create virtual disk";
269 return -1;
270 }
271
272 struct stat buf;
273 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
274 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
275 return -1;
276 }
277
278 auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
279 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
280 disk->create();
281 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
282 mDisks.push_back(mVirtualDisk);
283 }
284 } else {
285 if (mVirtualDisk != nullptr) {
286 dev_t device = mVirtualDisk->getDevice();
287
288 auto i = mDisks.begin();
289 while (i != mDisks.end()) {
290 if ((*i)->getDevice() == device) {
291 (*i)->destroy();
292 i = mDisks.erase(i);
293 } else {
294 ++i;
295 }
296 }
297
298 Loop::destroyByDevice(mVirtualDiskPath.c_str());
299 mVirtualDisk = nullptr;
300 }
301
302 if (access(kPathVirtualDisk, F_OK) == 0) {
303 unlink(kPathVirtualDisk);
304 }
305 }
306 return 0;
307}
308
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700309int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800310 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700311 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800312}
313
San Mehatf1b736b2009-10-10 17:22:08 -0700314int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700315 // Always start from a clean slate by unmounting everything in
316 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700317 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700318
319 // Assume that we always have an emulated volume on internal
320 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700321 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700322 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700323 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700324 mInternalEmulated->create();
325
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600326 // Consider creating a virtual disk
327 updateVirtualDisk();
328
San Mehatf1b736b2009-10-10 17:22:08 -0700329 return 0;
330}
331
332int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700333 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700334 mInternalEmulated->destroy();
335 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700336 return 0;
337}
338
San Mehatfd7f5872009-10-12 11:32:47 -0700339void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700340 std::lock_guard<std::mutex> lock(mLock);
341
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700342 if (mDebug) {
343 LOG(VERBOSE) << "----------------";
344 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
345 evt->dump();
346 }
San Mehatf1b736b2009-10-10 17:22:08 -0700347
Mateusz Nowak64403792015-08-03 16:39:19 +0200348 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
349 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700350
351 if (devType != "disk") return;
352
353 int major = atoi(evt->findParam("MAJOR"));
354 int minor = atoi(evt->findParam("MINOR"));
355 dev_t device = makedev(major, minor);
356
357 switch (evt->getAction()) {
358 case NetlinkEvent::Action::kAdd: {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700359 for (const auto& source : mDiskSources) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700360 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800361 // For now, assume that MMC and virtio-blk (the latter is
362 // emulator-specific; see Disk.cpp for details) devices are SD,
363 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700364 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800365 if (major == kMajorBlockMmc
366 || (android::vold::IsRunningInEmulator()
367 && major >= (int) kMajorBlockExperimentalMin
368 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700369 flags |= android::vold::Disk::Flags::kSd;
370 } else {
371 flags |= android::vold::Disk::Flags::kUsb;
372 }
373
374 auto disk = new android::vold::Disk(eventPath, device,
375 source->getNickname(), flags);
376 disk->create();
377 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
378 break;
379 }
380 }
381 break;
382 }
383 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700384 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700385 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700386 if (disk->getDevice() == device) {
387 disk->readMetadata();
388 disk->readPartitions();
389 }
390 }
391 break;
392 }
393 case NetlinkEvent::Action::kRemove: {
394 auto i = mDisks.begin();
395 while (i != mDisks.end()) {
396 if ((*i)->getDevice() == device) {
397 (*i)->destroy();
398 i = mDisks.erase(i);
399 } else {
400 ++i;
401 }
402 }
403 break;
404 }
405 default: {
406 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
407 break;
408 }
409 }
410}
411
412void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800413 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700414 mDiskSources.push_back(diskSource);
415}
416
417std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
418 for (auto disk : mDisks) {
419 if (disk->getId() == id) {
420 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700421 }
422 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700423 return nullptr;
424}
San Mehatf1b736b2009-10-10 17:22:08 -0700425
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700426std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800427 // Vold could receive "mount" after "shutdown" command in the extreme case.
428 // If this happens, mInternalEmulated will equal nullptr and
429 // we need to deal with it in order to avoid null pointer crash.
430 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700431 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700432 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700433 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700434 auto vol = disk->findVolume(id);
435 if (vol != nullptr) {
436 return vol;
437 }
438 }
439 return nullptr;
440}
441
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700442void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
443 std::list<std::string>& list) {
444 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700445 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700446 disk->listVolumes(type, list);
447 }
448}
449
450nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700451 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700452 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700453 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700454 } else {
455 auto vol = findVolume(id);
456 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
457 path = vol->getPath();
458 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700459 }
460
461 if (path.empty()) {
462 LOG(WARNING) << "Failed to find volume for " << id;
463 return -1;
464 }
465
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700466 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700467}
468
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700469int VolumeManager::forgetPartition(const std::string& partGuid) {
470 std::string normalizedGuid;
471 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
472 LOG(WARNING) << "Invalid GUID " << partGuid;
473 return -1;
474 }
475
476 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
477 if (unlink(keyPath.c_str()) != 0) {
478 LOG(ERROR) << "Failed to unlink " << keyPath;
479 return -1;
480 }
481
482 return 0;
483}
484
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700485int VolumeManager::linkPrimary(userid_t userId) {
486 std::string source(mPrimary->getPath());
487 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
488 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700489 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700490 }
491
492 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
493 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
494 if (errno != ENOENT) {
495 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
496 }
497 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700498 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700499 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
500 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
501 strerror(errno));
502 return -errno;
503 }
504 return 0;
505}
506
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700507int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
508 mAddedUsers[userId] = userSerialNumber;
509 return 0;
510}
511
512int VolumeManager::onUserRemoved(userid_t userId) {
513 mAddedUsers.erase(userId);
514 return 0;
515}
516
517int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700518 // Note that sometimes the system will spin up processes from Zygote
519 // before actually starting the user, so we're okay if Zygote
520 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600521 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700522 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
523
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700524 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700525 if (mPrimary) {
526 linkPrimary(userId);
527 }
528 return 0;
529}
530
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700531int VolumeManager::onUserStopped(userid_t userId) {
532 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700533 return 0;
534}
535
536int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
537 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700538 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700539 linkPrimary(userId);
540 }
541 return 0;
542}
543
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700544static int unmount_tree(const char* path) {
545 size_t path_len = strlen(path);
546
547 FILE* fp = setmntent("/proc/mounts", "r");
548 if (fp == NULL) {
549 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
550 return -errno;
551 }
552
553 // Some volumes can be stacked on each other, so force unmount in
554 // reverse order to give us the best chance of success.
555 std::list<std::string> toUnmount;
556 mntent* mentry;
557 while ((mentry = getmntent(fp)) != NULL) {
558 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
559 toUnmount.push_front(std::string(mentry->mnt_dir));
560 }
561 }
562 endmntent(fp);
563
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700564 for (const auto& path : toUnmount) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700565 if (umount2(path.c_str(), MNT_DETACH)) {
566 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
567 }
568 }
569 return 0;
570}
571
Jeff Sharkey66270a22015-06-24 11:49:24 -0700572int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
573 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
574
575 DIR* dir;
576 struct dirent* de;
577 char rootName[PATH_MAX];
578 char pidName[PATH_MAX];
579 int pidFd;
580 int nsFd;
581 struct stat sb;
582 pid_t child;
583
584 if (!(dir = opendir("/proc"))) {
585 PLOG(ERROR) << "Failed to opendir";
586 return -1;
587 }
588
589 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900590 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700591 PLOG(ERROR) << "Failed to readlink";
592 closedir(dir);
593 return -1;
594 }
595
596 // Poke through all running PIDs look for apps running as UID
597 while ((de = readdir(dir))) {
Jeff Vander Stoepa997db72017-10-23 17:12:31 -0700598 pid_t pid;
599 if (de->d_type != DT_DIR) continue;
600 if (!android::base::ParseInt(de->d_name, &pid)) continue;
601
Jeff Sharkey66270a22015-06-24 11:49:24 -0700602 pidFd = -1;
603 nsFd = -1;
604
605 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
606 if (pidFd < 0) {
607 goto next;
608 }
609 if (fstat(pidFd, &sb) != 0) {
610 PLOG(WARNING) << "Failed to stat " << de->d_name;
611 goto next;
612 }
613 if (sb.st_uid != uid) {
614 goto next;
615 }
616
617 // Matches so far, but refuse to touch if in root namespace
618 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900619 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700620 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
621 goto next;
622 }
623 if (!strcmp(rootName, pidName)) {
624 LOG(WARNING) << "Skipping due to root namespace";
625 goto next;
626 }
627
628 // We purposefully leave the namespace open across the fork
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600629 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700630 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700631 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700632 goto next;
633 }
634
635 if (!(child = fork())) {
636 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700637 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700638 _exit(1);
639 }
640
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700641 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700642
643 std::string storageSource;
644 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700645 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700646 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700647 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700648 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700649 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700650 } else {
651 // Sane default of no storage visible
652 _exit(0);
653 }
654 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
Hidehiko Abe674bed12016-03-09 16:42:10 +0900655 NULL, MS_BIND | MS_REC, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700656 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
657 << de->d_name;
658 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700659 }
Hidehiko Abe674bed12016-03-09 16:42:10 +0900660 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
661 MS_REC | MS_SLAVE, NULL)) == -1) {
662 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
663 << de->d_name;
664 _exit(1);
665 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700666
667 // Mount user-specific symlink helper into place
668 userid_t user_id = multiuser_get_user_id(uid);
669 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
670 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
671 NULL, MS_BIND, NULL)) == -1) {
672 PLOG(ERROR) << "Failed to mount " << userSource << " for "
673 << de->d_name;
674 _exit(1);
675 }
676
Jeff Sharkey66270a22015-06-24 11:49:24 -0700677 _exit(0);
678 }
679
680 if (child == -1) {
681 PLOG(ERROR) << "Failed to fork";
682 goto next;
683 } else {
684 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
685 }
686
687next:
688 close(nsFd);
689 close(pidFd);
690 }
691 closedir(dir);
692 return 0;
693}
694
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700695int VolumeManager::reset() {
696 // Tear down all existing disks/volumes and start from a blank slate so
697 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800698 if (mInternalEmulated != nullptr) {
699 mInternalEmulated->destroy();
700 mInternalEmulated->create();
701 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700702 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700703 disk->destroy();
704 disk->create();
705 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600706 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700707 mAddedUsers.clear();
708 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700709 return 0;
710}
711
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700712// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700713int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700714 if (mInternalEmulated == nullptr) {
715 return 0; // already shutdown
716 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700717 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700718 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700719 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700720 disk->destroy();
721 }
722 mDisks.clear();
723 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700724}
725
Jeff Sharkey9c484982015-03-31 10:35:33 -0700726int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700727 std::lock_guard<std::mutex> lock(mLock);
728
Jeff Sharkey9c484982015-03-31 10:35:33 -0700729 // First, try gracefully unmounting all known devices
730 if (mInternalEmulated != nullptr) {
731 mInternalEmulated->unmount();
732 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700733 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700734 disk->unmountAll();
735 }
736
737 // Worst case we might have some stale mounts lurking around, so
738 // force unmount those just to be safe.
739 FILE* fp = setmntent("/proc/mounts", "r");
740 if (fp == NULL) {
741 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
742 return -errno;
743 }
744
745 // Some volumes can be stacked on each other, so force unmount in
746 // reverse order to give us the best chance of success.
747 std::list<std::string> toUnmount;
748 mntent* mentry;
749 while ((mentry = getmntent(fp)) != NULL) {
750 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
751 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
752 toUnmount.push_front(std::string(mentry->mnt_dir));
753 }
754 }
755 endmntent(fp);
756
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700757 for (const auto& path : toUnmount) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700758 SLOGW("Tearing down stale mount %s", path.c_str());
759 android::vold::ForceUnmount(path);
760 }
761
762 return 0;
763}
764
Kenny Root508c0e12010-07-12 09:59:49 -0700765int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
766 char idHash[33];
767 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
768 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
769 return -1;
770 }
771
772 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700773 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400774 if ((written < 0) || (written >= mountPathLen)) {
775 errno = EINVAL;
776 return -1;
777 }
Kenny Root508c0e12010-07-12 09:59:49 -0700778
779 if (access(mountPath, F_OK)) {
780 errno = ENOENT;
781 return -1;
782 }
783
784 return 0;
785}
786
San Mehata19b2502010-01-06 10:33:53 -0800787int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700788 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700789
Nick Kralevich0de7c612014-01-27 14:58:06 -0800790 if (!isLegalAsecId(id)) {
791 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
792 errno = EINVAL;
793 return -1;
794 }
795
Kenny Root344ca102012-04-03 17:23:01 -0700796 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
797 SLOGE("Couldn't find ASEC %s", id);
798 return -1;
799 }
San Mehat88ac2c02010-03-23 11:15:58 -0700800
801 memset(buffer, 0, maxlen);
802 if (access(asecFileName, F_OK)) {
803 errno = ENOENT;
804 return -1;
805 }
San Mehata19b2502010-01-06 10:33:53 -0800806
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700807 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400808 if ((written < 0) || (written >= maxlen)) {
809 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
810 errno = EINVAL;
811 return -1;
812 }
813
San Mehata19b2502010-01-06 10:33:53 -0800814 return 0;
815}
816
Dianne Hackborn736910c2011-06-27 13:37:07 -0700817int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
818 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700819
Nick Kralevich0de7c612014-01-27 14:58:06 -0800820 if (!isLegalAsecId(id)) {
821 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
822 errno = EINVAL;
823 return -1;
824 }
825
Kenny Root344ca102012-04-03 17:23:01 -0700826 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
827 SLOGE("Couldn't find ASEC %s", id);
828 return -1;
829 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700830
831 memset(buffer, 0, maxlen);
832 if (access(asecFileName, F_OK)) {
833 errno = ENOENT;
834 return -1;
835 }
836
rpcraigd1c226f2012-10-09 06:58:16 -0400837 int written = snprintf(buffer, maxlen, "%s", asecFileName);
838 if ((written < 0) || (written >= maxlen)) {
839 errno = EINVAL;
840 return -1;
841 }
842
Dianne Hackborn736910c2011-06-27 13:37:07 -0700843 return 0;
844}
845
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200846int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700847 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800848 struct asec_superblock sb;
849 memset(&sb, 0, sizeof(sb));
850
Nick Kralevich0de7c612014-01-27 14:58:06 -0800851 if (!isLegalAsecId(id)) {
852 SLOGE("createAsec: Invalid asec id \"%s\"", id);
853 errno = EINVAL;
854 return -1;
855 }
856
Kenny Root344ca102012-04-03 17:23:01 -0700857 const bool wantFilesystem = strcmp(fstype, "none");
858 bool usingExt4 = false;
859 if (wantFilesystem) {
860 usingExt4 = !strcmp(fstype, "ext4");
861 if (usingExt4) {
862 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
863 } else if (strcmp(fstype, "fat")) {
864 SLOGE("Invalid filesystem type %s", fstype);
865 errno = EINVAL;
866 return -1;
867 }
868 }
869
San Mehatfcf24fe2010-03-03 12:37:32 -0800870 sb.magic = ASEC_SB_MAGIC;
871 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800872
San Mehatd31e3802010-02-18 08:37:45 -0800873 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200874 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800875 errno = EINVAL;
876 return -1;
877 }
878
San Mehata19b2502010-01-06 10:33:53 -0800879 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700880
881 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
882 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
883 asecFileName, strerror(errno));
884 errno = EADDRINUSE;
885 return -1;
886 }
887
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700888 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700889
rpcraigd1c226f2012-10-09 06:58:16 -0400890 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
891 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
892 errno = EINVAL;
893 return -1;
894 }
San Mehata19b2502010-01-06 10:33:53 -0800895
896 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700897 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700898 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800899 errno = EADDRINUSE;
900 return -1;
901 }
902
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200903 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700904 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700905 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700906 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700907 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800908
909 // Add +1 for our superblock which is at the end
910 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700911 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800912 return -1;
913 }
914
San Mehatd9a4e352010-03-12 13:32:47 -0800915 char idHash[33];
916 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700917 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800918 unlink(asecFileName);
919 return -1;
920 }
921
San Mehata19b2502010-01-06 10:33:53 -0800922 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800923 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700924 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800925 unlink(asecFileName);
926 return -1;
927 }
928
San Mehatb78a32c2010-01-10 13:02:12 -0800929 char dmDevice[255];
930 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800931
San Mehatb78a32c2010-01-10 13:02:12 -0800932 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800933 // XXX: This is all we support for now
934 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800935 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800936 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700937 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800938 Loop::destroyByDevice(loopDevice);
939 unlink(asecFileName);
940 return -1;
941 }
942 cleanupDm = true;
943 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800944 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800945 strcpy(dmDevice, loopDevice);
946 }
947
San Mehatfcf24fe2010-03-03 12:37:32 -0800948 /*
949 * Drop down the superblock at the end of the file
950 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700951 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800952 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800953 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800954 }
955 Loop::destroyByDevice(loopDevice);
956 unlink(asecFileName);
957 return -1;
958 }
959
Kenny Root344ca102012-04-03 17:23:01 -0700960 if (wantFilesystem) {
961 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400962 char mountPoint[255];
963
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700964 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400965 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
966 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
967 if (cleanupDm) {
968 Devmapper::destroy(idHash);
969 }
970 Loop::destroyByDevice(loopDevice);
971 unlink(asecFileName);
972 return -1;
973 }
rpcraiga54e13a2012-09-21 14:17:08 -0400974
Kenny Root344ca102012-04-03 17:23:01 -0700975 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700976 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700977 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700978 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800979 }
San Mehata19b2502010-01-06 10:33:53 -0800980
Kenny Root344ca102012-04-03 17:23:01 -0700981 if (formatStatus < 0) {
982 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800983 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800984 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800985 }
San Mehateb13a902010-01-07 12:12:50 -0800986 Loop::destroyByDevice(loopDevice);
987 unlink(asecFileName);
988 return -1;
989 }
Kenny Root344ca102012-04-03 17:23:01 -0700990
Kenny Root344ca102012-04-03 17:23:01 -0700991 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800992 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700993 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800994 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800995 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800996 }
997 Loop::destroyByDevice(loopDevice);
998 unlink(asecFileName);
999 return -1;
1000 }
San Mehatb78a32c2010-01-10 13:02:12 -08001001 }
San Mehata1091cb2010-02-28 20:17:20 -08001002
Kenny Root344ca102012-04-03 17:23:01 -07001003 int mountStatus;
1004 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001005 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
1006 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -07001007 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001008 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
1009 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -07001010 }
1011
1012 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -07001013 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -08001014 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001015 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001016 }
1017 Loop::destroyByDevice(loopDevice);
1018 unlink(asecFileName);
1019 return -1;
1020 }
Kenny Root344ca102012-04-03 17:23:01 -07001021
1022 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001023 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001024 if (dirfd >= 0) {
1025 if (fchown(dirfd, ownerUid, AID_SYSTEM)
1026 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
1027 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
1028 }
1029 close(dirfd);
1030 }
1031 }
San Mehata1091cb2010-02-28 20:17:20 -08001032 } else {
San Mehat97ac40e2010-03-24 10:24:19 -07001033 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -08001034 }
San Mehat88705162010-01-15 09:26:28 -08001035
Kenny Rootcbacf782010-09-24 15:11:48 -07001036 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -08001037 return 0;
1038}
1039
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001040int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001041 char asecFileName[255];
1042 char mountPoint[255];
1043 bool cleanupDm = false;
1044
1045 if (!isLegalAsecId(id)) {
1046 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
1047 errno = EINVAL;
1048 return -1;
1049 }
1050
1051 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1052 SLOGE("Couldn't find ASEC %s", id);
1053 return -1;
1054 }
1055
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001056 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001057 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1058 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
1059 return -1;
1060 }
1061
1062 if (isMountpointMounted(mountPoint)) {
1063 SLOGE("ASEC %s mounted. Unmount before resizing", id);
1064 errno = EBUSY;
1065 return -1;
1066 }
1067
1068 struct asec_superblock sb;
1069 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001070 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001071
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001072 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001073 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
1074 return -1;
1075 }
1076
1077 struct stat info;
1078 if (fstat(fd, &info) < 0) {
1079 SLOGE("Failed to get file size (%s)", strerror(errno));
1080 close(fd);
1081 return -1;
1082 }
1083
1084 oldNumSec = info.st_size / 512;
1085
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001086 /*
1087 * Try to read superblock.
1088 */
1089 memset(&sb, 0, sizeof(struct asec_superblock));
1090 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1091 SLOGE("lseek failed (%s)", strerror(errno));
1092 close(fd);
1093 return -1;
1094 }
1095 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1096 SLOGE("superblock read failed (%s)", strerror(errno));
1097 close(fd);
1098 return -1;
1099 }
1100 close(fd);
1101
1102 if (mDebug) {
1103 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1104 }
1105 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1106 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1107 errno = EMEDIUMTYPE;
1108 return -1;
1109 }
1110
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001111 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001112 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1113 SLOGE("Only ext4 partitions are supported for resize");
1114 errno = EINVAL;
1115 return -1;
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001116 } else {
1117 numImgSectors = adjustSectorNumExt4(numSectors);
1118 }
1119
1120 /*
1121 * add one block for the superblock
1122 */
1123 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1124 if (oldNumSec == numImgSectors + 1) {
1125 SLOGW("Size unchanged; ignoring resize request");
1126 return 0;
1127 } else if (oldNumSec > numImgSectors + 1) {
1128 SLOGE("Only growing is currently supported.");
1129 close(fd);
1130 return -1;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001131 }
1132
1133 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1134 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1135 return -1;
1136 }
1137
1138 /*
1139 * Drop down a copy of the superblock at the end of the file
1140 */
1141 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1142 goto fail;
1143
1144 char idHash[33];
1145 if (!asecHash(id, idHash, sizeof(idHash))) {
1146 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1147 goto fail;
1148 }
1149
1150 char loopDevice[255];
1151 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1152 goto fail;
1153
1154 char dmDevice[255];
1155
1156 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1157 Loop::destroyByDevice(loopDevice);
1158 goto fail;
1159 }
1160
1161 /*
1162 * Wait for the device mapper node to be created.
1163 */
1164 waitForDevMapper(dmDevice);
1165
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001166 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001167 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1168 if (cleanupDm) {
1169 Devmapper::destroy(idHash);
1170 }
1171 Loop::destroyByDevice(loopDevice);
1172 goto fail;
1173 }
1174
1175 return 0;
1176fail:
1177 Loop::resizeImageFile(asecFileName, oldNumSec);
1178 return -1;
1179}
1180
San Mehata19b2502010-01-06 10:33:53 -08001181int VolumeManager::finalizeAsec(const char *id) {
1182 char asecFileName[255];
1183 char loopDevice[255];
1184 char mountPoint[255];
1185
Nick Kralevich0de7c612014-01-27 14:58:06 -08001186 if (!isLegalAsecId(id)) {
1187 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1188 errno = EINVAL;
1189 return -1;
1190 }
1191
Kenny Root344ca102012-04-03 17:23:01 -07001192 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1193 SLOGE("Couldn't find ASEC %s", id);
1194 return -1;
1195 }
San Mehata19b2502010-01-06 10:33:53 -08001196
San Mehatd9a4e352010-03-12 13:32:47 -08001197 char idHash[33];
1198 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001199 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001200 return -1;
1201 }
1202
1203 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001204 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001205 return -1;
1206 }
1207
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001208 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001209 struct asec_superblock sb;
1210
1211 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1212 return -1;
1213 }
1214
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001215 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001216 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1217 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1218 return -1;
1219 }
Kenny Root344ca102012-04-03 17:23:01 -07001220
1221 int result = 0;
1222 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001223 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1224 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001225 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001226 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1227 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001228 }
1229
1230 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001231 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001232 return -1;
1233 }
1234
San Mehatd9a4e352010-03-12 13:32:47 -08001235 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001236 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001237 }
San Mehata19b2502010-01-06 10:33:53 -08001238 return 0;
1239}
1240
Kenny Root344ca102012-04-03 17:23:01 -07001241int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1242 char asecFileName[255];
1243 char loopDevice[255];
1244 char mountPoint[255];
1245
1246 if (gid < AID_APP) {
1247 SLOGE("Group ID is not in application range");
1248 return -1;
1249 }
1250
Nick Kralevich0de7c612014-01-27 14:58:06 -08001251 if (!isLegalAsecId(id)) {
1252 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1253 errno = EINVAL;
1254 return -1;
1255 }
1256
Kenny Root344ca102012-04-03 17:23:01 -07001257 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1258 SLOGE("Couldn't find ASEC %s", id);
1259 return -1;
1260 }
1261
1262 char idHash[33];
1263 if (!asecHash(id, idHash, sizeof(idHash))) {
1264 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1265 return -1;
1266 }
1267
1268 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1269 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1270 return -1;
1271 }
1272
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001273 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001274 struct asec_superblock sb;
1275
1276 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1277 return -1;
1278 }
1279
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001280 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001281 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1282 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1283 return -1;
1284 }
Kenny Root344ca102012-04-03 17:23:01 -07001285
1286 int result = 0;
1287 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1288 return 0;
1289 }
1290
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001291 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001292 false /* read-only */,
1293 true /* remount */,
1294 false /* executable */);
1295 if (ret) {
1296 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1297 return -1;
1298 }
1299
1300 char *paths[] = { mountPoint, NULL };
1301
1302 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1303 if (fts) {
1304 // Traverse the entire hierarchy and chown to system UID.
1305 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1306 // We don't care about the lost+found directory.
1307 if (!strcmp(ftsent->fts_name, "lost+found")) {
1308 continue;
1309 }
1310
1311 /*
1312 * There can only be one file marked as private right now.
1313 * This should be more robust, but it satisfies the requirements
1314 * we have for right now.
1315 */
1316 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1317
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001318 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001319 if (fd < 0) {
1320 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1321 result = -1;
1322 continue;
1323 }
1324
1325 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1326
1327 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001328 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001329 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001330 result |= fchmod(fd, privateFile ? 0640 : 0644);
1331 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001332
Stephen Smalley5093e612014-02-12 09:43:08 -05001333 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001334 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1335 result |= -1;
1336 }
1337
Kenny Root344ca102012-04-03 17:23:01 -07001338 close(fd);
1339 }
1340 fts_close(fts);
1341
1342 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001343 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001344 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1345 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1346 result |= -1;
1347 }
1348 close(dirfd);
1349 } else {
1350 result |= -1;
1351 }
1352
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001353 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001354 true /* read-only */,
1355 true /* remount */,
1356 true /* execute */);
1357
1358 if (result) {
1359 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1360 return -1;
1361 }
1362
1363 if (mDebug) {
1364 SLOGD("ASEC %s permissions fixed", id);
1365 }
1366 return 0;
1367}
1368
San Mehat048b0802010-01-23 08:17:06 -08001369int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001370 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001371 char *asecFilename2;
1372 char mountPoint[255];
1373
Kenny Root344ca102012-04-03 17:23:01 -07001374 const char *dir;
1375
Nick Kralevich0de7c612014-01-27 14:58:06 -08001376 if (!isLegalAsecId(id1)) {
1377 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1378 errno = EINVAL;
1379 return -1;
1380 }
1381
1382 if (!isLegalAsecId(id2)) {
1383 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1384 errno = EINVAL;
1385 return -1;
1386 }
1387
Kenny Root344ca102012-04-03 17:23:01 -07001388 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1389 SLOGE("Couldn't find ASEC %s", id1);
1390 return -1;
1391 }
1392
1393 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001394
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001395 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001396 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1397 SLOGE("Rename failed: couldn't construct mountpoint");
1398 goto out_err;
1399 }
1400
San Mehat048b0802010-01-23 08:17:06 -08001401 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001402 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001403 errno = EBUSY;
1404 goto out_err;
1405 }
1406
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001407 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001408 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1409 SLOGE("Rename failed: couldn't construct mountpoint2");
1410 goto out_err;
1411 }
1412
San Mehat96956ed2010-02-24 08:42:51 -08001413 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001414 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001415 errno = EBUSY;
1416 goto out_err;
1417 }
1418
San Mehat048b0802010-01-23 08:17:06 -08001419 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001420 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001421 errno = EADDRINUSE;
1422 goto out_err;
1423 }
1424
1425 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001426 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001427 goto out_err;
1428 }
1429
San Mehat048b0802010-01-23 08:17:06 -08001430 free(asecFilename2);
1431 return 0;
1432
1433out_err:
San Mehat048b0802010-01-23 08:17:06 -08001434 free(asecFilename2);
1435 return -1;
1436}
1437
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001438#define UNMOUNT_RETRIES 5
1439#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001440int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001441 char asecFileName[255];
1442 char mountPoint[255];
1443
Nick Kralevich0de7c612014-01-27 14:58:06 -08001444 if (!isLegalAsecId(id)) {
1445 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1446 errno = EINVAL;
1447 return -1;
1448 }
1449
Kenny Root344ca102012-04-03 17:23:01 -07001450 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1451 SLOGE("Couldn't find ASEC %s", id);
1452 return -1;
1453 }
1454
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001455 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001456 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1457 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1458 return -1;
1459 }
San Mehata19b2502010-01-06 10:33:53 -08001460
San Mehatd9a4e352010-03-12 13:32:47 -08001461 char idHash[33];
1462 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001463 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001464 return -1;
1465 }
1466
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001467 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1468}
1469
Kenny Root508c0e12010-07-12 09:59:49 -07001470int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001471 char mountPoint[255];
1472
1473 char idHash[33];
1474 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1475 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1476 return -1;
1477 }
1478
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001479 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001480 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1481 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1482 return -1;
1483 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001484
1485 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1486}
1487
1488int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1489 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001490 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001491 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001492 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001493 return -1;
1494 }
San Mehat23969932010-01-09 07:08:06 -08001495
San Mehatb78a32c2010-01-10 13:02:12 -08001496 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001497 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001498 rc = umount(mountPoint);
1499 if (!rc) {
1500 break;
San Mehata19b2502010-01-06 10:33:53 -08001501 }
San Mehatb78a32c2010-01-10 13:02:12 -08001502 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001503 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001504 rc = 0;
1505 break;
San Mehata19b2502010-01-06 10:33:53 -08001506 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001507 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001508 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001509
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001510 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001511
1512 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001513 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001514 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001515 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001516 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001517 }
San Mehat8c940ef2010-02-13 14:19:53 -08001518
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001519 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001520 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001521 }
1522
1523 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001524 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001525 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001526 return -1;
1527 }
1528
San Mehat12f4b892010-02-24 11:43:22 -08001529 int retries = 10;
1530
1531 while(retries--) {
1532 if (!rmdir(mountPoint)) {
1533 break;
1534 }
1535
San Mehat97ac40e2010-03-24 10:24:19 -07001536 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001537 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001538 }
1539
1540 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001541 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001542 }
San Mehat88705162010-01-15 09:26:28 -08001543
Paul Lawrence60dec162014-09-02 10:52:15 -07001544 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1545 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1546 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1547 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1548 continue;
1549 } else {
1550 break;
1551 }
San Mehata19b2502010-01-06 10:33:53 -08001552 }
1553
1554 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001555 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001556 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001557 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001558 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001559 }
San Mehat88705162010-01-15 09:26:28 -08001560
1561 AsecIdCollection::iterator it;
1562 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001563 ContainerData* cd = *it;
1564 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001565 free(*it);
1566 mActiveContainers->erase(it);
1567 break;
1568 }
1569 }
1570 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001571 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001572 }
San Mehatb78a32c2010-01-10 13:02:12 -08001573 return 0;
1574}
1575
San Mehat4ba89482010-02-18 09:00:18 -08001576int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001577 char asecFileName[255];
1578 char mountPoint[255];
1579
Nick Kralevich0de7c612014-01-27 14:58:06 -08001580 if (!isLegalAsecId(id)) {
1581 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1582 errno = EINVAL;
1583 return -1;
1584 }
1585
Kenny Root344ca102012-04-03 17:23:01 -07001586 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1587 SLOGE("Couldn't find ASEC %s", id);
1588 return -1;
1589 }
1590
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001591 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001592 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1593 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1594 return -1;
1595 }
San Mehatb78a32c2010-01-10 13:02:12 -08001596
San Mehat0586d542010-01-12 15:38:59 -08001597 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001598 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001599 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001600 }
San Mehat4ba89482010-02-18 09:00:18 -08001601 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001602 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001603 return -1;
1604 }
1605 }
San Mehata19b2502010-01-06 10:33:53 -08001606
San Mehat0586d542010-01-12 15:38:59 -08001607 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001608 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001609 return -1;
1610 }
San Mehata19b2502010-01-06 10:33:53 -08001611
San Mehatd9a4e352010-03-12 13:32:47 -08001612 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001613 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001614 }
San Mehata19b2502010-01-06 10:33:53 -08001615 return 0;
1616}
1617
Nick Kralevich0de7c612014-01-27 14:58:06 -08001618/*
1619 * Legal ASEC ids consist of alphanumeric characters, '-',
1620 * '_', or '.'. ".." is not allowed. The first or last character
1621 * of the ASEC id cannot be '.' (dot).
1622 */
1623bool VolumeManager::isLegalAsecId(const char *id) const {
1624 size_t i;
1625 size_t len = strlen(id);
1626
1627 if (len == 0) {
1628 return false;
1629 }
1630 if ((id[0] == '.') || (id[len - 1] == '.')) {
1631 return false;
1632 }
1633
1634 for (i = 0; i < len; i++) {
1635 if (id[i] == '.') {
1636 // i=0 is guaranteed never to have a dot. See above.
1637 if (id[i-1] == '.') return false;
1638 continue;
1639 }
1640 if (id[i] == '_' || id[i] == '-') continue;
1641 if (id[i] >= 'a' && id[i] <= 'z') continue;
1642 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1643 if (id[i] >= '0' && id[i] <= '9') continue;
1644 return false;
1645 }
1646
1647 return true;
1648}
1649
Kenny Root344ca102012-04-03 17:23:01 -07001650bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001651 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001652 if (dirfd < 0) {
1653 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001654 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001655 }
1656
Nick Kralevich25e581a2015-02-06 08:55:08 -08001657 struct stat sb;
1658 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1659 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001660
1661 close(dirfd);
1662
1663 return ret;
1664}
1665
1666int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1667 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001668 char *asecName;
1669
Nick Kralevich0de7c612014-01-27 14:58:06 -08001670 if (!isLegalAsecId(id)) {
1671 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1672 errno = EINVAL;
1673 return -1;
1674 }
1675
Kenny Root344ca102012-04-03 17:23:01 -07001676 if (asprintf(&asecName, "%s.asec", id) < 0) {
1677 SLOGE("Couldn't allocate string to write ASEC name");
1678 return -1;
1679 }
1680
1681 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001682 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1683 dir = VolumeManager::SEC_ASECDIR_INT;
1684 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1685 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001686 } else {
1687 free(asecName);
1688 return -1;
1689 }
1690
1691 if (directory != NULL) {
1692 *directory = dir;
1693 }
1694
1695 if (asecPath != NULL) {
1696 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001697 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1698 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001699 free(asecName);
1700 return -1;
1701 }
1702 }
1703
1704 free(asecName);
1705 return 0;
1706}
1707
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001708int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001709 char asecFileName[255];
1710 char mountPoint[255];
1711
Nick Kralevich0de7c612014-01-27 14:58:06 -08001712 if (!isLegalAsecId(id)) {
1713 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1714 errno = EINVAL;
1715 return -1;
1716 }
1717
Kenny Root344ca102012-04-03 17:23:01 -07001718 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1719 SLOGE("Couldn't find ASEC %s", id);
1720 return -1;
1721 }
1722
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001723 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001724 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001725 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001726 return -1;
1727 }
San Mehata19b2502010-01-06 10:33:53 -08001728
1729 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001730 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001731 errno = EBUSY;
1732 return -1;
1733 }
1734
San Mehatd9a4e352010-03-12 13:32:47 -08001735 char idHash[33];
1736 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001737 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001738 return -1;
1739 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001740
San Mehata19b2502010-01-06 10:33:53 -08001741 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001742 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1743 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001744
1745 char dmDevice[255];
1746 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001747
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001748 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001749 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001750
Kenny Root344ca102012-04-03 17:23:01 -07001751 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1752 return -1;
1753 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001754
San Mehatd9a4e352010-03-12 13:32:47 -08001755 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001756 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001757 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001758 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001759 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001760 Loop::destroyByDevice(loopDevice);
1761 errno = EMEDIUMTYPE;
1762 return -1;
1763 }
1764 nr_sec--; // We don't want the devmapping to extend onto our superblock
1765
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001766 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1767 Loop::destroyByDevice(loopDevice);
1768 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001769 }
1770
Kenny Root344ca102012-04-03 17:23:01 -07001771 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001772 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001773 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001774 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001775 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001776 }
1777 Loop::destroyByDevice(loopDevice);
1778 return -1;
1779 }
San Mehata19b2502010-01-06 10:33:53 -08001780 }
1781
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001782 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001783 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001784 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001785 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001786
Kenny Root344ca102012-04-03 17:23:01 -07001787 int result;
1788 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001789 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1790 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001791 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001792 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1793 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001794 }
1795
1796 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001797 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001798 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001799 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001800 }
1801 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001802 return -1;
1803 }
1804
Kenny Rootcbacf782010-09-24 15:11:48 -07001805 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001806 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001807 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001808 }
San Mehata19b2502010-01-06 10:33:53 -08001809 return 0;
1810}
1811
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001812/**
1813 * Mounts an image file <code>img</code>.
1814 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001815int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001816 char mountPoint[255];
1817
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001818 char idHash[33];
1819 if (!asecHash(img, idHash, sizeof(idHash))) {
1820 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1821 return -1;
1822 }
1823
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001824 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001825 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001826 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001827 return -1;
1828 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001829
1830 if (isMountpointMounted(mountPoint)) {
1831 SLOGE("Image %s already mounted", img);
1832 errno = EBUSY;
1833 return -1;
1834 }
1835
1836 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001837 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1838 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001839
1840 char dmDevice[255];
1841 bool cleanupDm = false;
1842 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001843 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001844
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001845 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001846 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1847 Loop::destroyByDevice(loopDevice);
1848 return -1;
1849 }
1850
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001851 get_blkdev_size(fd, &nr_sec);
1852 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001853 SLOGE("Failed to get loop size (%s)", strerror(errno));
1854 Loop::destroyByDevice(loopDevice);
1855 close(fd);
1856 return -1;
1857 }
1858
1859 close(fd);
1860
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001861 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001862 Loop::destroyByDevice(loopDevice);
1863 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001864 }
1865
1866 if (mkdir(mountPoint, 0755)) {
1867 if (errno != EEXIST) {
1868 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1869 if (cleanupDm) {
1870 Devmapper::destroy(idHash);
1871 }
1872 Loop::destroyByDevice(loopDevice);
1873 return -1;
1874 }
1875 }
1876
yoshiyuki hama476a6272015-01-28 16:37:23 +09001877 /*
1878 * Wait for the device mapper node to be created.
1879 */
1880 waitForDevMapper(dmDevice);
1881
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001882 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1883 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001884 SLOGE("Image mount failed (%s)", strerror(errno));
1885 if (cleanupDm) {
1886 Devmapper::destroy(idHash);
1887 }
1888 Loop::destroyByDevice(loopDevice);
1889 return -1;
1890 }
1891
Kenny Rootcbacf782010-09-24 15:11:48 -07001892 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001893 if (mDebug) {
1894 SLOGD("Image %s mounted", img);
1895 }
1896 return 0;
1897}
1898
Kenny Root508c0e12010-07-12 09:59:49 -07001899int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001900 FILE *fp = setmntent("/proc/mounts", "r");
1901 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001902 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1903 return -1;
1904 }
1905
1906 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001907 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001908 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001909 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001910 loopDir[loopDirLen++] = '/';
1911 loopDir[loopDirLen] = '\0';
1912
Yabin Cuid1104f72015-01-02 13:28:28 -08001913 mntent* mentry;
1914 while ((mentry = getmntent(fp)) != NULL) {
1915 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001916 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001917 if (fd >= 0) {
1918 struct loop_info64 li;
1919 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1920 cli->sendMsg(ResponseCode::AsecListResult,
1921 (const char*) li.lo_file_name, false);
1922 }
1923 close(fd);
1924 }
1925 }
1926 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001927 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001928 return 0;
1929}
1930
Jeff Sharkey9c484982015-03-31 10:35:33 -07001931extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001932 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001933 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001934}
1935
San Mehata19b2502010-01-06 10:33:53 -08001936bool VolumeManager::isMountpointMounted(const char *mp)
1937{
Yabin Cuid1104f72015-01-02 13:28:28 -08001938 FILE *fp = setmntent("/proc/mounts", "r");
1939 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001940 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001941 return false;
1942 }
1943
Yabin Cuid1104f72015-01-02 13:28:28 -08001944 bool found_mp = false;
1945 mntent* mentry;
1946 while ((mentry = getmntent(fp)) != NULL) {
1947 if (strcmp(mentry->mnt_dir, mp) == 0) {
1948 found_mp = true;
1949 break;
San Mehata19b2502010-01-06 10:33:53 -08001950 }
San Mehata19b2502010-01-06 10:33:53 -08001951 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001952 endmntent(fp);
1953 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001954}
1955
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001956int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001957 // Only offer to create directories for paths managed by vold
1958 if (strncmp(path, "/storage/", 9) == 0) {
1959 // fs_mkdirs() does symlink checking and relative path enforcement
1960 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001961 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001962 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001963 return -EINVAL;
1964 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001965}