blob: c3c95f6c5cf6ab5a7878518c7725a9e12b0d7ff2 [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
Keun-young Park375ac252017-08-02 17:45:48 -070093bool VolumeManager::shutting_down = false;
94
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060095static const char* kPathUserMount = "/mnt/user";
96static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
97
98static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
99
100/* 512MiB is large enough for testing purposes */
101static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700102
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700103static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +0800104static const unsigned int kMajorBlockExperimentalMin = 240;
105static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700106
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700107/* writes superblock at end of file or device given by name */
108static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700109 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700110 if (sbfd < 0) {
111 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
112 return -1;
113 }
114
115 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
116 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
117 close(sbfd);
118 return -1;
119 }
120
121 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
122 SLOGE("Failed to write superblock (%s)", strerror(errno));
123 close(sbfd);
124 return -1;
125 }
126 close(sbfd);
127 return 0;
128}
129
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200130static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700131 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
132 // preventing costly operations or unexpected ENOSPC error.
133 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200134 unsigned long clusterSectors = 4096 / 512;
135 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700136 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700137 return ROUND_UP_POWER_OF_2(numSectors, 3);
138}
139
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200140static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700141 /*
142 * Add some headroom
143 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200144 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700145 numSectors += fatSize + 2;
146 /*
147 * FAT is aligned to 32 kb with 512b sectors.
148 */
149 return ROUND_UP_POWER_OF_2(numSectors, 6);
150}
151
152static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
153 if (Loop::lookupActive(idHash, buffer, len)) {
154 if (Loop::create(idHash, asecFileName, buffer, len)) {
155 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
156 return -1;
157 }
158 if (debug) {
159 SLOGD("New loop device created at %s", buffer);
160 }
161 } else {
162 if (debug) {
163 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
164 }
165 }
166 return 0;
167}
168
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200169static 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 -0700170 if (strcmp(key, "none")) {
171 if (Devmapper::lookupActive(idHash, buffer, len)) {
172 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
173 buffer, len)) {
174 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
175 return -1;
176 }
177 if (debug) {
178 SLOGD("New devmapper instance created at %s", buffer);
179 }
180 } else {
181 if (debug) {
182 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
183 }
184 }
185 *createdDMDevice = true;
186 } else {
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600187 strlcpy(buffer, loopDevice, len);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700188 *createdDMDevice = false;
189 }
190 return 0;
191}
192
193static void waitForDevMapper(const char *dmDevice) {
194 /*
195 * Wait for the device mapper node to be created. Sometimes it takes a
196 * while. Wait for up to 1 second. We could also inspect incoming uevents,
197 * but that would take more effort.
198 */
199 int tries = 25;
200 while (tries--) {
201 if (!access(dmDevice, F_OK) || errno != ENOENT) {
202 break;
203 }
204 usleep(40 * 1000);
205 }
206}
207
San Mehatf1b736b2009-10-10 17:22:08 -0700208VolumeManager *VolumeManager::sInstance = NULL;
209
210VolumeManager *VolumeManager::Instance() {
211 if (!sInstance)
212 sInstance = new VolumeManager();
213 return sInstance;
214}
215
216VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800217 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800218 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700219 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400220 mUmsSharingCount = 0;
221 mSavedDirtyRatio = -1;
222 // set dirty ratio to 0 when UMS is active
223 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700224}
225
226VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800227 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700228}
229
Kenny Root7b18a7b2010-03-15 13:13:41 -0700230char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700231 static const char* digits = "0123456789abcdef";
232
Kenny Root7b18a7b2010-03-15 13:13:41 -0700233 unsigned char sig[MD5_DIGEST_LENGTH];
234
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700235 if (buffer == NULL) {
236 SLOGE("Destination buffer is NULL");
237 errno = ESPIPE;
238 return NULL;
239 } else if (id == NULL) {
240 SLOGE("Source buffer is NULL");
241 errno = ESPIPE;
242 return NULL;
243 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800244 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700245 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800246 errno = ESPIPE;
247 return NULL;
248 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700249
250 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800251
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700252 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700253 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700254 *p++ = digits[sig[i] >> 4];
255 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800256 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700257 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800258
259 return buffer;
260}
261
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600262int VolumeManager::updateVirtualDisk() {
263 if (property_get_bool(kPropVirtualDisk, false)) {
264 if (access(kPathVirtualDisk, F_OK) != 0) {
265 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
266 }
267
268 if (mVirtualDisk == nullptr) {
269 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
270 LOG(ERROR) << "Failed to create virtual disk";
271 return -1;
272 }
273
274 struct stat buf;
275 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
276 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
277 return -1;
278 }
279
280 auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
281 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
282 disk->create();
283 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
284 mDisks.push_back(mVirtualDisk);
285 }
286 } else {
287 if (mVirtualDisk != nullptr) {
288 dev_t device = mVirtualDisk->getDevice();
289
290 auto i = mDisks.begin();
291 while (i != mDisks.end()) {
292 if ((*i)->getDevice() == device) {
293 (*i)->destroy();
294 i = mDisks.erase(i);
295 } else {
296 ++i;
297 }
298 }
299
300 Loop::destroyByDevice(mVirtualDiskPath.c_str());
301 mVirtualDisk = nullptr;
302 }
303
304 if (access(kPathVirtualDisk, F_OK) == 0) {
305 unlink(kPathVirtualDisk);
306 }
307 }
308 return 0;
309}
310
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700311int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800312 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700313 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800314}
315
San Mehatf1b736b2009-10-10 17:22:08 -0700316int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700317 // Always start from a clean slate by unmounting everything in
318 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700319 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700320
321 // Assume that we always have an emulated volume on internal
322 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700323 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700324 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700325 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700326 mInternalEmulated->create();
327
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600328 // Consider creating a virtual disk
329 updateVirtualDisk();
330
San Mehatf1b736b2009-10-10 17:22:08 -0700331 return 0;
332}
333
334int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700335 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700336 mInternalEmulated->destroy();
337 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700338 return 0;
339}
340
San Mehatfd7f5872009-10-12 11:32:47 -0700341void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700342 std::lock_guard<std::mutex> lock(mLock);
343
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700344 if (mDebug) {
345 LOG(VERBOSE) << "----------------";
346 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
347 evt->dump();
348 }
San Mehatf1b736b2009-10-10 17:22:08 -0700349
Mateusz Nowak64403792015-08-03 16:39:19 +0200350 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
351 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700352
353 if (devType != "disk") return;
354
355 int major = atoi(evt->findParam("MAJOR"));
356 int minor = atoi(evt->findParam("MINOR"));
357 dev_t device = makedev(major, minor);
358
359 switch (evt->getAction()) {
360 case NetlinkEvent::Action::kAdd: {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700361 for (const auto& source : mDiskSources) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700362 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800363 // For now, assume that MMC and virtio-blk (the latter is
364 // emulator-specific; see Disk.cpp for details) devices are SD,
365 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700366 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800367 if (major == kMajorBlockMmc
368 || (android::vold::IsRunningInEmulator()
369 && major >= (int) kMajorBlockExperimentalMin
370 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700371 flags |= android::vold::Disk::Flags::kSd;
372 } else {
373 flags |= android::vold::Disk::Flags::kUsb;
374 }
375
376 auto disk = new android::vold::Disk(eventPath, device,
377 source->getNickname(), flags);
378 disk->create();
379 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
380 break;
381 }
382 }
383 break;
384 }
385 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700386 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700387 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700388 if (disk->getDevice() == device) {
389 disk->readMetadata();
390 disk->readPartitions();
391 }
392 }
393 break;
394 }
395 case NetlinkEvent::Action::kRemove: {
396 auto i = mDisks.begin();
397 while (i != mDisks.end()) {
398 if ((*i)->getDevice() == device) {
399 (*i)->destroy();
400 i = mDisks.erase(i);
401 } else {
402 ++i;
403 }
404 }
405 break;
406 }
407 default: {
408 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
409 break;
410 }
411 }
412}
413
414void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800415 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700416 mDiskSources.push_back(diskSource);
417}
418
419std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
420 for (auto disk : mDisks) {
421 if (disk->getId() == id) {
422 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700423 }
424 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700425 return nullptr;
426}
San Mehatf1b736b2009-10-10 17:22:08 -0700427
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700428std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800429 // Vold could receive "mount" after "shutdown" command in the extreme case.
430 // If this happens, mInternalEmulated will equal nullptr and
431 // we need to deal with it in order to avoid null pointer crash.
432 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700433 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700434 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700435 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700436 auto vol = disk->findVolume(id);
437 if (vol != nullptr) {
438 return vol;
439 }
440 }
441 return nullptr;
442}
443
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700444void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
445 std::list<std::string>& list) {
446 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700447 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700448 disk->listVolumes(type, list);
449 }
450}
451
452nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700453 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700454 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700455 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700456 } else {
457 auto vol = findVolume(id);
458 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
459 path = vol->getPath();
460 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700461 }
462
463 if (path.empty()) {
464 LOG(WARNING) << "Failed to find volume for " << id;
465 return -1;
466 }
467
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700468 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700469}
470
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700471int VolumeManager::forgetPartition(const std::string& partGuid) {
472 std::string normalizedGuid;
473 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
474 LOG(WARNING) << "Invalid GUID " << partGuid;
475 return -1;
476 }
477
478 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
479 if (unlink(keyPath.c_str()) != 0) {
480 LOG(ERROR) << "Failed to unlink " << keyPath;
481 return -1;
482 }
483
484 return 0;
485}
486
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700487int VolumeManager::linkPrimary(userid_t userId) {
488 std::string source(mPrimary->getPath());
489 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
490 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700491 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700492 }
493
494 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
495 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
496 if (errno != ENOENT) {
497 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
498 }
499 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700500 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700501 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
502 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
503 strerror(errno));
504 return -errno;
505 }
506 return 0;
507}
508
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700509int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
510 mAddedUsers[userId] = userSerialNumber;
511 return 0;
512}
513
514int VolumeManager::onUserRemoved(userid_t userId) {
515 mAddedUsers.erase(userId);
516 return 0;
517}
518
519int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700520 // Note that sometimes the system will spin up processes from Zygote
521 // before actually starting the user, so we're okay if Zygote
522 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600523 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700524 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
525
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700526 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700527 if (mPrimary) {
528 linkPrimary(userId);
529 }
530 return 0;
531}
532
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700533int VolumeManager::onUserStopped(userid_t userId) {
534 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700535 return 0;
536}
537
538int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
539 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700540 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700541 linkPrimary(userId);
542 }
543 return 0;
544}
545
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700546static int unmount_tree(const char* path) {
547 size_t path_len = strlen(path);
548
549 FILE* fp = setmntent("/proc/mounts", "r");
550 if (fp == NULL) {
551 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
552 return -errno;
553 }
554
555 // Some volumes can be stacked on each other, so force unmount in
556 // reverse order to give us the best chance of success.
557 std::list<std::string> toUnmount;
558 mntent* mentry;
559 while ((mentry = getmntent(fp)) != NULL) {
560 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
561 toUnmount.push_front(std::string(mentry->mnt_dir));
562 }
563 }
564 endmntent(fp);
565
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700566 for (const auto& path : toUnmount) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700567 if (umount2(path.c_str(), MNT_DETACH)) {
568 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
569 }
570 }
571 return 0;
572}
573
Jeff Sharkey66270a22015-06-24 11:49:24 -0700574int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
575 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
576
577 DIR* dir;
578 struct dirent* de;
579 char rootName[PATH_MAX];
580 char pidName[PATH_MAX];
581 int pidFd;
582 int nsFd;
583 struct stat sb;
584 pid_t child;
585
586 if (!(dir = opendir("/proc"))) {
587 PLOG(ERROR) << "Failed to opendir";
588 return -1;
589 }
590
591 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900592 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700593 PLOG(ERROR) << "Failed to readlink";
594 closedir(dir);
595 return -1;
596 }
597
598 // Poke through all running PIDs look for apps running as UID
599 while ((de = readdir(dir))) {
Jeff Vander Stoepa997db72017-10-23 17:12:31 -0700600 pid_t pid;
601 if (de->d_type != DT_DIR) continue;
602 if (!android::base::ParseInt(de->d_name, &pid)) continue;
603
Jeff Sharkey66270a22015-06-24 11:49:24 -0700604 pidFd = -1;
605 nsFd = -1;
606
607 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
608 if (pidFd < 0) {
609 goto next;
610 }
611 if (fstat(pidFd, &sb) != 0) {
612 PLOG(WARNING) << "Failed to stat " << de->d_name;
613 goto next;
614 }
615 if (sb.st_uid != uid) {
616 goto next;
617 }
618
619 // Matches so far, but refuse to touch if in root namespace
620 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900621 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700622 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
623 goto next;
624 }
625 if (!strcmp(rootName, pidName)) {
626 LOG(WARNING) << "Skipping due to root namespace";
627 goto next;
628 }
629
630 // We purposefully leave the namespace open across the fork
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600631 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700632 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700633 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700634 goto next;
635 }
636
637 if (!(child = fork())) {
638 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700639 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700640 _exit(1);
641 }
642
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700643 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700644
645 std::string storageSource;
646 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700647 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700648 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700649 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700650 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700651 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700652 } else {
653 // Sane default of no storage visible
654 _exit(0);
655 }
656 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
Hidehiko Abe674bed12016-03-09 16:42:10 +0900657 NULL, MS_BIND | MS_REC, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700658 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
659 << de->d_name;
660 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700661 }
Hidehiko Abe674bed12016-03-09 16:42:10 +0900662 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
663 MS_REC | MS_SLAVE, NULL)) == -1) {
664 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
665 << de->d_name;
666 _exit(1);
667 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700668
669 // Mount user-specific symlink helper into place
670 userid_t user_id = multiuser_get_user_id(uid);
671 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
672 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
673 NULL, MS_BIND, NULL)) == -1) {
674 PLOG(ERROR) << "Failed to mount " << userSource << " for "
675 << de->d_name;
676 _exit(1);
677 }
678
Jeff Sharkey66270a22015-06-24 11:49:24 -0700679 _exit(0);
680 }
681
682 if (child == -1) {
683 PLOG(ERROR) << "Failed to fork";
684 goto next;
685 } else {
686 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
687 }
688
689next:
690 close(nsFd);
691 close(pidFd);
692 }
693 closedir(dir);
694 return 0;
695}
696
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700697int VolumeManager::reset() {
698 // Tear down all existing disks/volumes and start from a blank slate so
699 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800700 if (mInternalEmulated != nullptr) {
701 mInternalEmulated->destroy();
702 mInternalEmulated->create();
703 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700704 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700705 disk->destroy();
706 disk->create();
707 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600708 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700709 mAddedUsers.clear();
710 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700711 return 0;
712}
713
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700714// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700715int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700716 if (mInternalEmulated == nullptr) {
717 return 0; // already shutdown
718 }
Keun-young Park375ac252017-08-02 17:45:48 -0700719 shutting_down = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700720 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700721 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700722 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700723 disk->destroy();
724 }
725 mDisks.clear();
Keun-young Park375ac252017-08-02 17:45:48 -0700726 shutting_down = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700727 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700728}
729
Jeff Sharkey9c484982015-03-31 10:35:33 -0700730int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700731 std::lock_guard<std::mutex> lock(mLock);
732
Jeff Sharkey9c484982015-03-31 10:35:33 -0700733 // First, try gracefully unmounting all known devices
734 if (mInternalEmulated != nullptr) {
735 mInternalEmulated->unmount();
736 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700737 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700738 disk->unmountAll();
739 }
740
741 // Worst case we might have some stale mounts lurking around, so
742 // force unmount those just to be safe.
743 FILE* fp = setmntent("/proc/mounts", "r");
744 if (fp == NULL) {
745 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
746 return -errno;
747 }
748
749 // Some volumes can be stacked on each other, so force unmount in
750 // reverse order to give us the best chance of success.
751 std::list<std::string> toUnmount;
752 mntent* mentry;
753 while ((mentry = getmntent(fp)) != NULL) {
754 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
755 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
756 toUnmount.push_front(std::string(mentry->mnt_dir));
757 }
758 }
759 endmntent(fp);
760
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700761 for (const auto& path : toUnmount) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700762 SLOGW("Tearing down stale mount %s", path.c_str());
763 android::vold::ForceUnmount(path);
764 }
765
766 return 0;
767}
768
Kenny Root508c0e12010-07-12 09:59:49 -0700769int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
770 char idHash[33];
771 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
772 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
773 return -1;
774 }
775
776 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700777 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400778 if ((written < 0) || (written >= mountPathLen)) {
779 errno = EINVAL;
780 return -1;
781 }
Kenny Root508c0e12010-07-12 09:59:49 -0700782
783 if (access(mountPath, F_OK)) {
784 errno = ENOENT;
785 return -1;
786 }
787
788 return 0;
789}
790
San Mehata19b2502010-01-06 10:33:53 -0800791int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700792 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700793
Nick Kralevich0de7c612014-01-27 14:58:06 -0800794 if (!isLegalAsecId(id)) {
795 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
796 errno = EINVAL;
797 return -1;
798 }
799
Kenny Root344ca102012-04-03 17:23:01 -0700800 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
801 SLOGE("Couldn't find ASEC %s", id);
802 return -1;
803 }
San Mehat88ac2c02010-03-23 11:15:58 -0700804
805 memset(buffer, 0, maxlen);
806 if (access(asecFileName, F_OK)) {
807 errno = ENOENT;
808 return -1;
809 }
San Mehata19b2502010-01-06 10:33:53 -0800810
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700811 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400812 if ((written < 0) || (written >= maxlen)) {
813 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
814 errno = EINVAL;
815 return -1;
816 }
817
San Mehata19b2502010-01-06 10:33:53 -0800818 return 0;
819}
820
Dianne Hackborn736910c2011-06-27 13:37:07 -0700821int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
822 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700823
Nick Kralevich0de7c612014-01-27 14:58:06 -0800824 if (!isLegalAsecId(id)) {
825 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
826 errno = EINVAL;
827 return -1;
828 }
829
Kenny Root344ca102012-04-03 17:23:01 -0700830 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
831 SLOGE("Couldn't find ASEC %s", id);
832 return -1;
833 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700834
835 memset(buffer, 0, maxlen);
836 if (access(asecFileName, F_OK)) {
837 errno = ENOENT;
838 return -1;
839 }
840
rpcraigd1c226f2012-10-09 06:58:16 -0400841 int written = snprintf(buffer, maxlen, "%s", asecFileName);
842 if ((written < 0) || (written >= maxlen)) {
843 errno = EINVAL;
844 return -1;
845 }
846
Dianne Hackborn736910c2011-06-27 13:37:07 -0700847 return 0;
848}
849
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200850int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700851 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800852 struct asec_superblock sb;
853 memset(&sb, 0, sizeof(sb));
854
Nick Kralevich0de7c612014-01-27 14:58:06 -0800855 if (!isLegalAsecId(id)) {
856 SLOGE("createAsec: Invalid asec id \"%s\"", id);
857 errno = EINVAL;
858 return -1;
859 }
860
Kenny Root344ca102012-04-03 17:23:01 -0700861 const bool wantFilesystem = strcmp(fstype, "none");
862 bool usingExt4 = false;
863 if (wantFilesystem) {
864 usingExt4 = !strcmp(fstype, "ext4");
865 if (usingExt4) {
866 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
867 } else if (strcmp(fstype, "fat")) {
868 SLOGE("Invalid filesystem type %s", fstype);
869 errno = EINVAL;
870 return -1;
871 }
872 }
873
San Mehatfcf24fe2010-03-03 12:37:32 -0800874 sb.magic = ASEC_SB_MAGIC;
875 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800876
San Mehatd31e3802010-02-18 08:37:45 -0800877 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200878 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800879 errno = EINVAL;
880 return -1;
881 }
882
San Mehata19b2502010-01-06 10:33:53 -0800883 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700884
885 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
886 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
887 asecFileName, strerror(errno));
888 errno = EADDRINUSE;
889 return -1;
890 }
891
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700892 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700893
rpcraigd1c226f2012-10-09 06:58:16 -0400894 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
895 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
896 errno = EINVAL;
897 return -1;
898 }
San Mehata19b2502010-01-06 10:33:53 -0800899
900 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700901 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700902 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800903 errno = EADDRINUSE;
904 return -1;
905 }
906
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200907 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700908 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700909 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700910 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700911 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800912
913 // Add +1 for our superblock which is at the end
914 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700915 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800916 return -1;
917 }
918
San Mehatd9a4e352010-03-12 13:32:47 -0800919 char idHash[33];
920 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700921 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800922 unlink(asecFileName);
923 return -1;
924 }
925
San Mehata19b2502010-01-06 10:33:53 -0800926 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800927 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700928 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800929 unlink(asecFileName);
930 return -1;
931 }
932
San Mehatb78a32c2010-01-10 13:02:12 -0800933 char dmDevice[255];
934 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800935
San Mehatb78a32c2010-01-10 13:02:12 -0800936 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800937 // XXX: This is all we support for now
938 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800939 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800940 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700941 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800942 Loop::destroyByDevice(loopDevice);
943 unlink(asecFileName);
944 return -1;
945 }
946 cleanupDm = true;
947 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800948 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600949 strlcpy(dmDevice, loopDevice, sizeof(dmDevice));
San Mehatb78a32c2010-01-10 13:02:12 -0800950 }
951
San Mehatfcf24fe2010-03-03 12:37:32 -0800952 /*
953 * Drop down the superblock at the end of the file
954 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700955 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800956 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800957 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800958 }
959 Loop::destroyByDevice(loopDevice);
960 unlink(asecFileName);
961 return -1;
962 }
963
Kenny Root344ca102012-04-03 17:23:01 -0700964 if (wantFilesystem) {
965 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400966 char mountPoint[255];
967
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700968 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400969 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
970 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
971 if (cleanupDm) {
972 Devmapper::destroy(idHash);
973 }
974 Loop::destroyByDevice(loopDevice);
975 unlink(asecFileName);
976 return -1;
977 }
rpcraiga54e13a2012-09-21 14:17:08 -0400978
Kenny Root344ca102012-04-03 17:23:01 -0700979 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700980 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700981 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700982 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800983 }
San Mehata19b2502010-01-06 10:33:53 -0800984
Kenny Root344ca102012-04-03 17:23:01 -0700985 if (formatStatus < 0) {
986 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800987 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800988 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800989 }
San Mehateb13a902010-01-07 12:12:50 -0800990 Loop::destroyByDevice(loopDevice);
991 unlink(asecFileName);
992 return -1;
993 }
Kenny Root344ca102012-04-03 17:23:01 -0700994
Kenny Root344ca102012-04-03 17:23:01 -0700995 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800996 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700997 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800998 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800999 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001000 }
1001 Loop::destroyByDevice(loopDevice);
1002 unlink(asecFileName);
1003 return -1;
1004 }
San Mehatb78a32c2010-01-10 13:02:12 -08001005 }
San Mehata1091cb2010-02-28 20:17:20 -08001006
Kenny Root344ca102012-04-03 17:23:01 -07001007 int mountStatus;
1008 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001009 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
1010 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -07001011 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001012 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
1013 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -07001014 }
1015
1016 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -07001017 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -08001018 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001019 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001020 }
1021 Loop::destroyByDevice(loopDevice);
1022 unlink(asecFileName);
1023 return -1;
1024 }
Kenny Root344ca102012-04-03 17:23:01 -07001025
1026 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001027 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001028 if (dirfd >= 0) {
1029 if (fchown(dirfd, ownerUid, AID_SYSTEM)
1030 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
1031 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
1032 }
1033 close(dirfd);
1034 }
1035 }
San Mehata1091cb2010-02-28 20:17:20 -08001036 } else {
San Mehat97ac40e2010-03-24 10:24:19 -07001037 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -08001038 }
San Mehat88705162010-01-15 09:26:28 -08001039
Kenny Rootcbacf782010-09-24 15:11:48 -07001040 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -08001041 return 0;
1042}
1043
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001044int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001045 char asecFileName[255];
1046 char mountPoint[255];
1047 bool cleanupDm = false;
1048
1049 if (!isLegalAsecId(id)) {
1050 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
1051 errno = EINVAL;
1052 return -1;
1053 }
1054
1055 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1056 SLOGE("Couldn't find ASEC %s", id);
1057 return -1;
1058 }
1059
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001060 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001061 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1062 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
1063 return -1;
1064 }
1065
1066 if (isMountpointMounted(mountPoint)) {
1067 SLOGE("ASEC %s mounted. Unmount before resizing", id);
1068 errno = EBUSY;
1069 return -1;
1070 }
1071
1072 struct asec_superblock sb;
1073 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001074 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001075
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001076 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001077 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
1078 return -1;
1079 }
1080
1081 struct stat info;
1082 if (fstat(fd, &info) < 0) {
1083 SLOGE("Failed to get file size (%s)", strerror(errno));
1084 close(fd);
1085 return -1;
1086 }
1087
1088 oldNumSec = info.st_size / 512;
1089
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001090 /*
1091 * Try to read superblock.
1092 */
1093 memset(&sb, 0, sizeof(struct asec_superblock));
1094 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1095 SLOGE("lseek failed (%s)", strerror(errno));
1096 close(fd);
1097 return -1;
1098 }
1099 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1100 SLOGE("superblock read failed (%s)", strerror(errno));
1101 close(fd);
1102 return -1;
1103 }
1104 close(fd);
1105
1106 if (mDebug) {
1107 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1108 }
1109 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1110 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1111 errno = EMEDIUMTYPE;
1112 return -1;
1113 }
1114
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001115 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001116 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1117 SLOGE("Only ext4 partitions are supported for resize");
1118 errno = EINVAL;
1119 return -1;
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001120 } else {
1121 numImgSectors = adjustSectorNumExt4(numSectors);
1122 }
1123
1124 /*
1125 * add one block for the superblock
1126 */
1127 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1128 if (oldNumSec == numImgSectors + 1) {
1129 SLOGW("Size unchanged; ignoring resize request");
1130 return 0;
1131 } else if (oldNumSec > numImgSectors + 1) {
1132 SLOGE("Only growing is currently supported.");
1133 close(fd);
1134 return -1;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001135 }
1136
1137 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1138 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1139 return -1;
1140 }
1141
1142 /*
1143 * Drop down a copy of the superblock at the end of the file
1144 */
1145 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1146 goto fail;
1147
1148 char idHash[33];
1149 if (!asecHash(id, idHash, sizeof(idHash))) {
1150 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1151 goto fail;
1152 }
1153
1154 char loopDevice[255];
1155 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1156 goto fail;
1157
1158 char dmDevice[255];
1159
1160 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1161 Loop::destroyByDevice(loopDevice);
1162 goto fail;
1163 }
1164
1165 /*
1166 * Wait for the device mapper node to be created.
1167 */
1168 waitForDevMapper(dmDevice);
1169
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001170 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001171 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1172 if (cleanupDm) {
1173 Devmapper::destroy(idHash);
1174 }
1175 Loop::destroyByDevice(loopDevice);
1176 goto fail;
1177 }
1178
1179 return 0;
1180fail:
1181 Loop::resizeImageFile(asecFileName, oldNumSec);
1182 return -1;
1183}
1184
San Mehata19b2502010-01-06 10:33:53 -08001185int VolumeManager::finalizeAsec(const char *id) {
1186 char asecFileName[255];
1187 char loopDevice[255];
1188 char mountPoint[255];
1189
Nick Kralevich0de7c612014-01-27 14:58:06 -08001190 if (!isLegalAsecId(id)) {
1191 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1192 errno = EINVAL;
1193 return -1;
1194 }
1195
Kenny Root344ca102012-04-03 17:23:01 -07001196 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1197 SLOGE("Couldn't find ASEC %s", id);
1198 return -1;
1199 }
San Mehata19b2502010-01-06 10:33:53 -08001200
San Mehatd9a4e352010-03-12 13:32:47 -08001201 char idHash[33];
1202 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001203 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001204 return -1;
1205 }
1206
1207 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001208 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001209 return -1;
1210 }
1211
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001212 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001213 struct asec_superblock sb;
1214
1215 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1216 return -1;
1217 }
1218
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001219 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001220 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1221 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1222 return -1;
1223 }
Kenny Root344ca102012-04-03 17:23:01 -07001224
1225 int result = 0;
1226 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001227 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1228 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001229 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001230 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1231 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001232 }
1233
1234 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001235 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001236 return -1;
1237 }
1238
San Mehatd9a4e352010-03-12 13:32:47 -08001239 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001240 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001241 }
San Mehata19b2502010-01-06 10:33:53 -08001242 return 0;
1243}
1244
Kenny Root344ca102012-04-03 17:23:01 -07001245int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1246 char asecFileName[255];
1247 char loopDevice[255];
1248 char mountPoint[255];
1249
1250 if (gid < AID_APP) {
1251 SLOGE("Group ID is not in application range");
1252 return -1;
1253 }
1254
Nick Kralevich0de7c612014-01-27 14:58:06 -08001255 if (!isLegalAsecId(id)) {
1256 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1257 errno = EINVAL;
1258 return -1;
1259 }
1260
Kenny Root344ca102012-04-03 17:23:01 -07001261 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1262 SLOGE("Couldn't find ASEC %s", id);
1263 return -1;
1264 }
1265
1266 char idHash[33];
1267 if (!asecHash(id, idHash, sizeof(idHash))) {
1268 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1269 return -1;
1270 }
1271
1272 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1273 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1274 return -1;
1275 }
1276
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001277 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001278 struct asec_superblock sb;
1279
1280 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1281 return -1;
1282 }
1283
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001284 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001285 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1286 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1287 return -1;
1288 }
Kenny Root344ca102012-04-03 17:23:01 -07001289
1290 int result = 0;
1291 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1292 return 0;
1293 }
1294
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001295 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001296 false /* read-only */,
1297 true /* remount */,
1298 false /* executable */);
1299 if (ret) {
1300 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1301 return -1;
1302 }
1303
1304 char *paths[] = { mountPoint, NULL };
1305
1306 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1307 if (fts) {
1308 // Traverse the entire hierarchy and chown to system UID.
1309 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1310 // We don't care about the lost+found directory.
1311 if (!strcmp(ftsent->fts_name, "lost+found")) {
1312 continue;
1313 }
1314
1315 /*
1316 * There can only be one file marked as private right now.
1317 * This should be more robust, but it satisfies the requirements
1318 * we have for right now.
1319 */
1320 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1321
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001322 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001323 if (fd < 0) {
1324 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1325 result = -1;
1326 continue;
1327 }
1328
1329 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1330
1331 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001332 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001333 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001334 result |= fchmod(fd, privateFile ? 0640 : 0644);
1335 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001336
Stephen Smalley5093e612014-02-12 09:43:08 -05001337 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001338 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1339 result |= -1;
1340 }
1341
Kenny Root344ca102012-04-03 17:23:01 -07001342 close(fd);
1343 }
1344 fts_close(fts);
1345
1346 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001347 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001348 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1349 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1350 result |= -1;
1351 }
1352 close(dirfd);
1353 } else {
1354 result |= -1;
1355 }
1356
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001357 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001358 true /* read-only */,
1359 true /* remount */,
1360 true /* execute */);
1361
1362 if (result) {
1363 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1364 return -1;
1365 }
1366
1367 if (mDebug) {
1368 SLOGD("ASEC %s permissions fixed", id);
1369 }
1370 return 0;
1371}
1372
San Mehat048b0802010-01-23 08:17:06 -08001373int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001374 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001375 char *asecFilename2;
1376 char mountPoint[255];
1377
Kenny Root344ca102012-04-03 17:23:01 -07001378 const char *dir;
1379
Nick Kralevich0de7c612014-01-27 14:58:06 -08001380 if (!isLegalAsecId(id1)) {
1381 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1382 errno = EINVAL;
1383 return -1;
1384 }
1385
1386 if (!isLegalAsecId(id2)) {
1387 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1388 errno = EINVAL;
1389 return -1;
1390 }
1391
Kenny Root344ca102012-04-03 17:23:01 -07001392 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1393 SLOGE("Couldn't find ASEC %s", id1);
1394 return -1;
1395 }
1396
1397 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001398
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001399 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001400 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1401 SLOGE("Rename failed: couldn't construct mountpoint");
1402 goto out_err;
1403 }
1404
San Mehat048b0802010-01-23 08:17:06 -08001405 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001406 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001407 errno = EBUSY;
1408 goto out_err;
1409 }
1410
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001411 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001412 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1413 SLOGE("Rename failed: couldn't construct mountpoint2");
1414 goto out_err;
1415 }
1416
San Mehat96956ed2010-02-24 08:42:51 -08001417 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001418 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001419 errno = EBUSY;
1420 goto out_err;
1421 }
1422
San Mehat048b0802010-01-23 08:17:06 -08001423 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001424 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001425 errno = EADDRINUSE;
1426 goto out_err;
1427 }
1428
1429 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001430 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001431 goto out_err;
1432 }
1433
San Mehat048b0802010-01-23 08:17:06 -08001434 free(asecFilename2);
1435 return 0;
1436
1437out_err:
San Mehat048b0802010-01-23 08:17:06 -08001438 free(asecFilename2);
1439 return -1;
1440}
1441
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001442#define UNMOUNT_RETRIES 5
1443#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001444int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001445 char asecFileName[255];
1446 char mountPoint[255];
1447
Nick Kralevich0de7c612014-01-27 14:58:06 -08001448 if (!isLegalAsecId(id)) {
1449 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1450 errno = EINVAL;
1451 return -1;
1452 }
1453
Kenny Root344ca102012-04-03 17:23:01 -07001454 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1455 SLOGE("Couldn't find ASEC %s", id);
1456 return -1;
1457 }
1458
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001459 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001460 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1461 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1462 return -1;
1463 }
San Mehata19b2502010-01-06 10:33:53 -08001464
San Mehatd9a4e352010-03-12 13:32:47 -08001465 char idHash[33];
1466 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001467 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001468 return -1;
1469 }
1470
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001471 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1472}
1473
Kenny Root508c0e12010-07-12 09:59:49 -07001474int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001475 char mountPoint[255];
1476
1477 char idHash[33];
1478 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1479 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1480 return -1;
1481 }
1482
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001483 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001484 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1485 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1486 return -1;
1487 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001488
1489 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1490}
1491
1492int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1493 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001494 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001495 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001496 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001497 return -1;
1498 }
San Mehat23969932010-01-09 07:08:06 -08001499
San Mehatb78a32c2010-01-10 13:02:12 -08001500 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001501 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001502 rc = umount(mountPoint);
1503 if (!rc) {
1504 break;
San Mehata19b2502010-01-06 10:33:53 -08001505 }
San Mehatb78a32c2010-01-10 13:02:12 -08001506 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001507 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001508 rc = 0;
1509 break;
San Mehata19b2502010-01-06 10:33:53 -08001510 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001511 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001512 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001513
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001514 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001515
1516 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001517 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001518 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001519 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001520 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001521 }
San Mehat8c940ef2010-02-13 14:19:53 -08001522
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001523 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001524 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001525 }
1526
1527 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001528 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001529 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001530 return -1;
1531 }
1532
San Mehat12f4b892010-02-24 11:43:22 -08001533 int retries = 10;
1534
1535 while(retries--) {
1536 if (!rmdir(mountPoint)) {
1537 break;
1538 }
1539
San Mehat97ac40e2010-03-24 10:24:19 -07001540 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001541 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001542 }
1543
1544 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001545 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001546 }
San Mehat88705162010-01-15 09:26:28 -08001547
Paul Lawrence60dec162014-09-02 10:52:15 -07001548 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1549 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1550 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1551 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1552 continue;
1553 } else {
1554 break;
1555 }
San Mehata19b2502010-01-06 10:33:53 -08001556 }
1557
1558 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001559 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001560 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001561 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001562 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001563 }
San Mehat88705162010-01-15 09:26:28 -08001564
1565 AsecIdCollection::iterator it;
1566 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001567 ContainerData* cd = *it;
1568 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001569 free(*it);
1570 mActiveContainers->erase(it);
1571 break;
1572 }
1573 }
1574 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001575 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001576 }
San Mehatb78a32c2010-01-10 13:02:12 -08001577 return 0;
1578}
1579
San Mehat4ba89482010-02-18 09:00:18 -08001580int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001581 char asecFileName[255];
1582 char mountPoint[255];
1583
Nick Kralevich0de7c612014-01-27 14:58:06 -08001584 if (!isLegalAsecId(id)) {
1585 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1586 errno = EINVAL;
1587 return -1;
1588 }
1589
Kenny Root344ca102012-04-03 17:23:01 -07001590 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1591 SLOGE("Couldn't find ASEC %s", id);
1592 return -1;
1593 }
1594
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001595 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001596 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1597 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1598 return -1;
1599 }
San Mehatb78a32c2010-01-10 13:02:12 -08001600
San Mehat0586d542010-01-12 15:38:59 -08001601 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001602 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001603 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001604 }
San Mehat4ba89482010-02-18 09:00:18 -08001605 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001606 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001607 return -1;
1608 }
1609 }
San Mehata19b2502010-01-06 10:33:53 -08001610
San Mehat0586d542010-01-12 15:38:59 -08001611 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001612 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001613 return -1;
1614 }
San Mehata19b2502010-01-06 10:33:53 -08001615
San Mehatd9a4e352010-03-12 13:32:47 -08001616 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001617 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001618 }
San Mehata19b2502010-01-06 10:33:53 -08001619 return 0;
1620}
1621
Nick Kralevich0de7c612014-01-27 14:58:06 -08001622/*
1623 * Legal ASEC ids consist of alphanumeric characters, '-',
1624 * '_', or '.'. ".." is not allowed. The first or last character
1625 * of the ASEC id cannot be '.' (dot).
1626 */
1627bool VolumeManager::isLegalAsecId(const char *id) const {
1628 size_t i;
1629 size_t len = strlen(id);
1630
1631 if (len == 0) {
1632 return false;
1633 }
1634 if ((id[0] == '.') || (id[len - 1] == '.')) {
1635 return false;
1636 }
1637
1638 for (i = 0; i < len; i++) {
1639 if (id[i] == '.') {
1640 // i=0 is guaranteed never to have a dot. See above.
1641 if (id[i-1] == '.') return false;
1642 continue;
1643 }
1644 if (id[i] == '_' || id[i] == '-') continue;
1645 if (id[i] >= 'a' && id[i] <= 'z') continue;
1646 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1647 if (id[i] >= '0' && id[i] <= '9') continue;
1648 return false;
1649 }
1650
1651 return true;
1652}
1653
Kenny Root344ca102012-04-03 17:23:01 -07001654bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001655 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001656 if (dirfd < 0) {
1657 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001658 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001659 }
1660
Nick Kralevich25e581a2015-02-06 08:55:08 -08001661 struct stat sb;
1662 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1663 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001664
1665 close(dirfd);
1666
1667 return ret;
1668}
1669
1670int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1671 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001672 char *asecName;
1673
Nick Kralevich0de7c612014-01-27 14:58:06 -08001674 if (!isLegalAsecId(id)) {
1675 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1676 errno = EINVAL;
1677 return -1;
1678 }
1679
Kenny Root344ca102012-04-03 17:23:01 -07001680 if (asprintf(&asecName, "%s.asec", id) < 0) {
1681 SLOGE("Couldn't allocate string to write ASEC name");
1682 return -1;
1683 }
1684
1685 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001686 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1687 dir = VolumeManager::SEC_ASECDIR_INT;
1688 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1689 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001690 } else {
1691 free(asecName);
1692 return -1;
1693 }
1694
1695 if (directory != NULL) {
1696 *directory = dir;
1697 }
1698
1699 if (asecPath != NULL) {
1700 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001701 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1702 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001703 free(asecName);
1704 return -1;
1705 }
1706 }
1707
1708 free(asecName);
1709 return 0;
1710}
1711
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001712int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001713 char asecFileName[255];
1714 char mountPoint[255];
1715
Nick Kralevich0de7c612014-01-27 14:58:06 -08001716 if (!isLegalAsecId(id)) {
1717 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1718 errno = EINVAL;
1719 return -1;
1720 }
1721
Kenny Root344ca102012-04-03 17:23:01 -07001722 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1723 SLOGE("Couldn't find ASEC %s", id);
1724 return -1;
1725 }
1726
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001727 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001728 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001729 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001730 return -1;
1731 }
San Mehata19b2502010-01-06 10:33:53 -08001732
1733 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001734 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001735 errno = EBUSY;
1736 return -1;
1737 }
1738
San Mehatd9a4e352010-03-12 13:32:47 -08001739 char idHash[33];
1740 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001741 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001742 return -1;
1743 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001744
San Mehata19b2502010-01-06 10:33:53 -08001745 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001746 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1747 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001748
1749 char dmDevice[255];
1750 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001751
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001752 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001753 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001754
Kenny Root344ca102012-04-03 17:23:01 -07001755 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1756 return -1;
1757 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001758
San Mehatd9a4e352010-03-12 13:32:47 -08001759 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001760 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001761 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001762 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001763 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001764 Loop::destroyByDevice(loopDevice);
1765 errno = EMEDIUMTYPE;
1766 return -1;
1767 }
1768 nr_sec--; // We don't want the devmapping to extend onto our superblock
1769
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001770 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1771 Loop::destroyByDevice(loopDevice);
1772 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001773 }
1774
Kenny Root344ca102012-04-03 17:23:01 -07001775 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001776 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001777 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001778 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001779 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001780 }
1781 Loop::destroyByDevice(loopDevice);
1782 return -1;
1783 }
San Mehata19b2502010-01-06 10:33:53 -08001784 }
1785
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001786 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001787 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001788 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001789 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001790
Kenny Root344ca102012-04-03 17:23:01 -07001791 int result;
1792 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001793 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1794 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001795 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001796 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1797 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001798 }
1799
1800 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001801 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001802 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001803 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001804 }
1805 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001806 return -1;
1807 }
1808
Kenny Rootcbacf782010-09-24 15:11:48 -07001809 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001810 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001811 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001812 }
San Mehata19b2502010-01-06 10:33:53 -08001813 return 0;
1814}
1815
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001816/**
1817 * Mounts an image file <code>img</code>.
1818 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001819int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001820 char mountPoint[255];
1821
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001822 char idHash[33];
1823 if (!asecHash(img, idHash, sizeof(idHash))) {
1824 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1825 return -1;
1826 }
1827
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001828 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001829 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001830 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001831 return -1;
1832 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001833
1834 if (isMountpointMounted(mountPoint)) {
1835 SLOGE("Image %s already mounted", img);
1836 errno = EBUSY;
1837 return -1;
1838 }
1839
1840 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001841 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1842 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001843
1844 char dmDevice[255];
1845 bool cleanupDm = false;
1846 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001847 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001848
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001849 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001850 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1851 Loop::destroyByDevice(loopDevice);
1852 return -1;
1853 }
1854
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001855 get_blkdev_size(fd, &nr_sec);
1856 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001857 SLOGE("Failed to get loop size (%s)", strerror(errno));
1858 Loop::destroyByDevice(loopDevice);
1859 close(fd);
1860 return -1;
1861 }
1862
1863 close(fd);
1864
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001865 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001866 Loop::destroyByDevice(loopDevice);
1867 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001868 }
1869
1870 if (mkdir(mountPoint, 0755)) {
1871 if (errno != EEXIST) {
1872 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1873 if (cleanupDm) {
1874 Devmapper::destroy(idHash);
1875 }
1876 Loop::destroyByDevice(loopDevice);
1877 return -1;
1878 }
1879 }
1880
yoshiyuki hama476a6272015-01-28 16:37:23 +09001881 /*
1882 * Wait for the device mapper node to be created.
1883 */
1884 waitForDevMapper(dmDevice);
1885
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001886 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1887 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001888 SLOGE("Image mount failed (%s)", strerror(errno));
1889 if (cleanupDm) {
1890 Devmapper::destroy(idHash);
1891 }
1892 Loop::destroyByDevice(loopDevice);
1893 return -1;
1894 }
1895
Kenny Rootcbacf782010-09-24 15:11:48 -07001896 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001897 if (mDebug) {
1898 SLOGD("Image %s mounted", img);
1899 }
1900 return 0;
1901}
1902
Kenny Root508c0e12010-07-12 09:59:49 -07001903int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001904 FILE *fp = setmntent("/proc/mounts", "r");
1905 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001906 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1907 return -1;
1908 }
1909
1910 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001911 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001912 char loopDir[loopDirLen + 2];
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001913 strlcpy(loopDir, VolumeManager::LOOPDIR, sizeof(loopDir));
Kenny Root508c0e12010-07-12 09:59:49 -07001914 loopDir[loopDirLen++] = '/';
1915 loopDir[loopDirLen] = '\0';
1916
Yabin Cuid1104f72015-01-02 13:28:28 -08001917 mntent* mentry;
1918 while ((mentry = getmntent(fp)) != NULL) {
1919 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001920 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001921 if (fd >= 0) {
1922 struct loop_info64 li;
1923 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1924 cli->sendMsg(ResponseCode::AsecListResult,
1925 (const char*) li.lo_file_name, false);
1926 }
1927 close(fd);
1928 }
1929 }
1930 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001931 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001932 return 0;
1933}
1934
Jeff Sharkey9c484982015-03-31 10:35:33 -07001935extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001936 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001937 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001938}
1939
San Mehata19b2502010-01-06 10:33:53 -08001940bool VolumeManager::isMountpointMounted(const char *mp)
1941{
Yabin Cuid1104f72015-01-02 13:28:28 -08001942 FILE *fp = setmntent("/proc/mounts", "r");
1943 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001944 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001945 return false;
1946 }
1947
Yabin Cuid1104f72015-01-02 13:28:28 -08001948 bool found_mp = false;
1949 mntent* mentry;
1950 while ((mentry = getmntent(fp)) != NULL) {
1951 if (strcmp(mentry->mnt_dir, mp) == 0) {
1952 found_mp = true;
1953 break;
San Mehata19b2502010-01-06 10:33:53 -08001954 }
San Mehata19b2502010-01-06 10:33:53 -08001955 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001956 endmntent(fp);
1957 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001958}
1959
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001960int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001961 // Only offer to create directories for paths managed by vold
1962 if (strncmp(path, "/storage/", 9) == 0) {
1963 // fs_mkdirs() does symlink checking and relative path enforcement
1964 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001965 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001966 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001967 return -EINVAL;
1968 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001969}