blob: 98f12e8f7092aad009e9ae5d03b23dc7fb541990 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuid1104f72015-01-02 13:28:28 -080017#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070018#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080019#include <fcntl.h>
Kenny Root344ca102012-04-03 17:23:01 -070020#include <fts.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080021#include <mntent.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/mount.h>
San Mehata19b2502010-01-06 10:33:53 -080027#include <sys/stat.h>
28#include <sys/types.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070029#include <sys/sysmacros.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070030#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080031#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080032
San Mehata2677e42009-12-13 10:40:18 -080033#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070034
35#define LOG_TAG "Vold"
36
Kenny Root7b18a7b2010-03-15 13:13:41 -070037#include <openssl/md5.h>
38
Elliott Hughes7e128fb2015-12-04 15:50:53 -080039#include <android-base/logging.h>
Jeff Vander Stoepa997db72017-10-23 17:12:31 -070040#include <android-base/parseint.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080041#include <android-base/stringprintf.h>
Tri Voe98adee2018-04-16 14:27:10 -070042#include <android-base/strings.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070043#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070044#include <cutils/log.h>
45
Robert Craigb9e3ba52014-02-04 10:53:00 -050046#include <selinux/android.h>
47
San Mehatfd7f5872009-10-12 11:32:47 -070048#include <sysutils/NetlinkEvent.h>
49
Kenny Root344ca102012-04-03 17:23:01 -070050#include <private/android_filesystem_config.h>
51
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070052#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070053#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070054#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070055#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080056#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080057#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070058#include "fs/Ext4.h"
59#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070060#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080061#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080062#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080063#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090064#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070065#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080066
Mike Lockwood97f2fc12011-06-07 10:51:38 -070067#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
68
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070069#define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
70 + ((number) & (~((1U << (po2)) - 1))))
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070071
Jeff Sharkey36801cc2015-03-13 16:09:20 -070072using android::base::StringPrintf;
73
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070074/*
75 * Path to external storage where *only* root can access ASEC image files
76 */
77const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
78
79/*
80 * Path to internal storage where *only* root can access ASEC image files
81 */
82const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
83
84/*
85 * Path to where secure containers are mounted
86 */
87const char *VolumeManager::ASECDIR = "/mnt/asec";
88
89/*
90 * Path to where OBBs are mounted
91 */
92const char *VolumeManager::LOOPDIR = "/mnt/obb";
93
Keun-young Park375ac252017-08-02 17:45:48 -070094bool VolumeManager::shutting_down = false;
95
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060096static const char* kPathUserMount = "/mnt/user";
97static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
98
99static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
100
101/* 512MiB is large enough for testing purposes */
102static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700103
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700104static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +0800105static const unsigned int kMajorBlockExperimentalMin = 240;
106static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700107
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700108/* writes superblock at end of file or device given by name */
109static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700110 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700111 if (sbfd < 0) {
112 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
113 return -1;
114 }
115
116 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
117 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
118 close(sbfd);
119 return -1;
120 }
121
122 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
123 SLOGE("Failed to write superblock (%s)", strerror(errno));
124 close(sbfd);
125 return -1;
126 }
127 close(sbfd);
128 return 0;
129}
130
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200131static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700132 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
133 // preventing costly operations or unexpected ENOSPC error.
134 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200135 unsigned long clusterSectors = 4096 / 512;
136 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700137 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700138 return ROUND_UP_POWER_OF_2(numSectors, 3);
139}
140
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200141static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700142 /*
143 * Add some headroom
144 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200145 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700146 numSectors += fatSize + 2;
147 /*
148 * FAT is aligned to 32 kb with 512b sectors.
149 */
150 return ROUND_UP_POWER_OF_2(numSectors, 6);
151}
152
153static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
154 if (Loop::lookupActive(idHash, buffer, len)) {
155 if (Loop::create(idHash, asecFileName, buffer, len)) {
156 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
157 return -1;
158 }
159 if (debug) {
160 SLOGD("New loop device created at %s", buffer);
161 }
162 } else {
163 if (debug) {
164 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
165 }
166 }
167 return 0;
168}
169
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200170static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , unsigned long numImgSectors, bool* createdDMDevice, bool debug) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700171 if (strcmp(key, "none")) {
172 if (Devmapper::lookupActive(idHash, buffer, len)) {
173 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
174 buffer, len)) {
175 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
176 return -1;
177 }
178 if (debug) {
179 SLOGD("New devmapper instance created at %s", buffer);
180 }
181 } else {
182 if (debug) {
183 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
184 }
185 }
186 *createdDMDevice = true;
187 } else {
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600188 strlcpy(buffer, loopDevice, len);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700189 *createdDMDevice = false;
190 }
191 return 0;
192}
193
194static void waitForDevMapper(const char *dmDevice) {
195 /*
196 * Wait for the device mapper node to be created. Sometimes it takes a
197 * while. Wait for up to 1 second. We could also inspect incoming uevents,
198 * but that would take more effort.
199 */
200 int tries = 25;
201 while (tries--) {
202 if (!access(dmDevice, F_OK) || errno != ENOENT) {
203 break;
204 }
205 usleep(40 * 1000);
206 }
207}
208
San Mehatf1b736b2009-10-10 17:22:08 -0700209VolumeManager *VolumeManager::sInstance = NULL;
210
211VolumeManager *VolumeManager::Instance() {
212 if (!sInstance)
213 sInstance = new VolumeManager();
214 return sInstance;
215}
216
217VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800218 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800219 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700220 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400221 mUmsSharingCount = 0;
222 mSavedDirtyRatio = -1;
223 // set dirty ratio to 0 when UMS is active
224 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700225}
226
227VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800228 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700229}
230
Kenny Root7b18a7b2010-03-15 13:13:41 -0700231char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700232 static const char* digits = "0123456789abcdef";
233
Kenny Root7b18a7b2010-03-15 13:13:41 -0700234 unsigned char sig[MD5_DIGEST_LENGTH];
235
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700236 if (buffer == NULL) {
237 SLOGE("Destination buffer is NULL");
238 errno = ESPIPE;
239 return NULL;
240 } else if (id == NULL) {
241 SLOGE("Source buffer is NULL");
242 errno = ESPIPE;
243 return NULL;
244 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800245 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700246 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800247 errno = ESPIPE;
248 return NULL;
249 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700250
251 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800252
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700253 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700254 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700255 *p++ = digits[sig[i] >> 4];
256 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800257 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700258 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800259
260 return buffer;
261}
262
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600263int VolumeManager::updateVirtualDisk() {
264 if (property_get_bool(kPropVirtualDisk, false)) {
265 if (access(kPathVirtualDisk, F_OK) != 0) {
266 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
267 }
268
269 if (mVirtualDisk == nullptr) {
270 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
271 LOG(ERROR) << "Failed to create virtual disk";
272 return -1;
273 }
274
275 struct stat buf;
276 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
277 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
278 return -1;
279 }
280
281 auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
282 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
283 disk->create();
284 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
285 mDisks.push_back(mVirtualDisk);
286 }
287 } else {
288 if (mVirtualDisk != nullptr) {
289 dev_t device = mVirtualDisk->getDevice();
290
291 auto i = mDisks.begin();
292 while (i != mDisks.end()) {
293 if ((*i)->getDevice() == device) {
294 (*i)->destroy();
295 i = mDisks.erase(i);
296 } else {
297 ++i;
298 }
299 }
300
301 Loop::destroyByDevice(mVirtualDiskPath.c_str());
302 mVirtualDisk = nullptr;
303 }
304
305 if (access(kPathVirtualDisk, F_OK) == 0) {
306 unlink(kPathVirtualDisk);
307 }
308 }
309 return 0;
310}
311
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700312int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800313 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700314 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800315}
316
San Mehatf1b736b2009-10-10 17:22:08 -0700317int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700318 // Always start from a clean slate by unmounting everything in
319 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700320 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700321
322 // Assume that we always have an emulated volume on internal
323 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700324 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700325 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700326 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700327 mInternalEmulated->create();
328
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600329 // Consider creating a virtual disk
330 updateVirtualDisk();
331
San Mehatf1b736b2009-10-10 17:22:08 -0700332 return 0;
333}
334
335int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700336 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700337 mInternalEmulated->destroy();
338 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700339 return 0;
340}
341
San Mehatfd7f5872009-10-12 11:32:47 -0700342void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700343 std::lock_guard<std::mutex> lock(mLock);
344
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700345 if (mDebug) {
346 LOG(VERBOSE) << "----------------";
347 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
348 evt->dump();
349 }
San Mehatf1b736b2009-10-10 17:22:08 -0700350
Mateusz Nowak64403792015-08-03 16:39:19 +0200351 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
352 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700353
354 if (devType != "disk") return;
355
356 int major = atoi(evt->findParam("MAJOR"));
357 int minor = atoi(evt->findParam("MINOR"));
358 dev_t device = makedev(major, minor);
359
360 switch (evt->getAction()) {
361 case NetlinkEvent::Action::kAdd: {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700362 for (const auto& source : mDiskSources) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700363 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800364 // For now, assume that MMC and virtio-blk (the latter is
365 // emulator-specific; see Disk.cpp for details) devices are SD,
366 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700367 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800368 if (major == kMajorBlockMmc
369 || (android::vold::IsRunningInEmulator()
370 && major >= (int) kMajorBlockExperimentalMin
371 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700372 flags |= android::vold::Disk::Flags::kSd;
373 } else {
374 flags |= android::vold::Disk::Flags::kUsb;
375 }
376
377 auto disk = new android::vold::Disk(eventPath, device,
378 source->getNickname(), flags);
379 disk->create();
380 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
381 break;
382 }
383 }
384 break;
385 }
386 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700387 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700388 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700389 if (disk->getDevice() == device) {
390 disk->readMetadata();
391 disk->readPartitions();
392 }
393 }
394 break;
395 }
396 case NetlinkEvent::Action::kRemove: {
397 auto i = mDisks.begin();
398 while (i != mDisks.end()) {
399 if ((*i)->getDevice() == device) {
400 (*i)->destroy();
401 i = mDisks.erase(i);
402 } else {
403 ++i;
404 }
405 }
406 break;
407 }
408 default: {
409 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
410 break;
411 }
412 }
413}
414
415void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800416 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700417 mDiskSources.push_back(diskSource);
418}
419
420std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
421 for (auto disk : mDisks) {
422 if (disk->getId() == id) {
423 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700424 }
425 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700426 return nullptr;
427}
San Mehatf1b736b2009-10-10 17:22:08 -0700428
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700429std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Gao Xiangd263da82017-08-14 11:32:13 +0800430 // Vold could receive "mount" after "shutdown" command in the extreme case.
431 // If this happens, mInternalEmulated will equal nullptr and
432 // we need to deal with it in order to avoid null pointer crash.
433 if (mInternalEmulated != nullptr && mInternalEmulated->getId() == id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700434 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700435 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700436 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700437 auto vol = disk->findVolume(id);
438 if (vol != nullptr) {
439 return vol;
440 }
441 }
442 return nullptr;
443}
444
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700445void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
446 std::list<std::string>& list) {
447 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700448 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700449 disk->listVolumes(type, list);
450 }
451}
452
453nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700454 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700455 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700456 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700457 } else {
458 auto vol = findVolume(id);
459 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
460 path = vol->getPath();
461 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700462 }
463
464 if (path.empty()) {
465 LOG(WARNING) << "Failed to find volume for " << id;
466 return -1;
467 }
468
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700469 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700470}
471
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700472int VolumeManager::forgetPartition(const std::string& partGuid) {
473 std::string normalizedGuid;
474 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
475 LOG(WARNING) << "Invalid GUID " << partGuid;
476 return -1;
477 }
478
479 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
480 if (unlink(keyPath.c_str()) != 0) {
481 LOG(ERROR) << "Failed to unlink " << keyPath;
482 return -1;
483 }
484
485 return 0;
486}
487
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700488int VolumeManager::linkPrimary(userid_t userId) {
489 std::string source(mPrimary->getPath());
490 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
491 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700492 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700493 }
494
495 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
496 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
497 if (errno != ENOENT) {
498 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
499 }
500 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700501 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700502 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
503 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
504 strerror(errno));
505 return -errno;
506 }
507 return 0;
508}
509
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700510int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
511 mAddedUsers[userId] = userSerialNumber;
512 return 0;
513}
514
515int VolumeManager::onUserRemoved(userid_t userId) {
516 mAddedUsers.erase(userId);
517 return 0;
518}
519
520int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700521 // Note that sometimes the system will spin up processes from Zygote
522 // before actually starting the user, so we're okay if Zygote
523 // already created this directory.
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600524 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700525 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
526
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700527 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700528 if (mPrimary) {
529 linkPrimary(userId);
530 }
531 return 0;
532}
533
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700534int VolumeManager::onUserStopped(userid_t userId) {
535 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700536 return 0;
537}
538
539int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
540 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700541 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700542 linkPrimary(userId);
543 }
544 return 0;
545}
546
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700547static int unmount_tree(const char* path) {
548 size_t path_len = strlen(path);
549
550 FILE* fp = setmntent("/proc/mounts", "r");
551 if (fp == NULL) {
552 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
553 return -errno;
554 }
555
556 // Some volumes can be stacked on each other, so force unmount in
557 // reverse order to give us the best chance of success.
558 std::list<std::string> toUnmount;
559 mntent* mentry;
560 while ((mentry = getmntent(fp)) != NULL) {
561 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
562 toUnmount.push_front(std::string(mentry->mnt_dir));
563 }
564 }
565 endmntent(fp);
566
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700567 for (const auto& path : toUnmount) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700568 if (umount2(path.c_str(), MNT_DETACH)) {
569 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
570 }
571 }
572 return 0;
573}
574
Jeff Sharkey66270a22015-06-24 11:49:24 -0700575int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
576 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
577
578 DIR* dir;
579 struct dirent* de;
580 char rootName[PATH_MAX];
581 char pidName[PATH_MAX];
582 int pidFd;
583 int nsFd;
584 struct stat sb;
585 pid_t child;
586
587 if (!(dir = opendir("/proc"))) {
588 PLOG(ERROR) << "Failed to opendir";
589 return -1;
590 }
591
592 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900593 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700594 PLOG(ERROR) << "Failed to readlink";
595 closedir(dir);
596 return -1;
597 }
598
599 // Poke through all running PIDs look for apps running as UID
600 while ((de = readdir(dir))) {
Jeff Vander Stoepa997db72017-10-23 17:12:31 -0700601 pid_t pid;
602 if (de->d_type != DT_DIR) continue;
603 if (!android::base::ParseInt(de->d_name, &pid)) continue;
604
Jeff Sharkey66270a22015-06-24 11:49:24 -0700605 pidFd = -1;
606 nsFd = -1;
607
608 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
609 if (pidFd < 0) {
610 goto next;
611 }
612 if (fstat(pidFd, &sb) != 0) {
613 PLOG(WARNING) << "Failed to stat " << de->d_name;
614 goto next;
615 }
616 if (sb.st_uid != uid) {
617 goto next;
618 }
619
620 // Matches so far, but refuse to touch if in root namespace
621 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900622 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700623 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
624 goto next;
625 }
626 if (!strcmp(rootName, pidName)) {
627 LOG(WARNING) << "Skipping due to root namespace";
628 goto next;
629 }
630
631 // We purposefully leave the namespace open across the fork
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600632 nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
Jeff Sharkey66270a22015-06-24 11:49:24 -0700633 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700634 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700635 goto next;
636 }
637
638 if (!(child = fork())) {
639 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700640 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700641 _exit(1);
642 }
643
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700644 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700645
646 std::string storageSource;
647 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700648 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700649 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700650 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700651 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700652 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700653 } else {
654 // Sane default of no storage visible
655 _exit(0);
656 }
657 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
Hidehiko Abe674bed12016-03-09 16:42:10 +0900658 NULL, MS_BIND | MS_REC, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700659 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
660 << de->d_name;
661 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700662 }
Hidehiko Abe674bed12016-03-09 16:42:10 +0900663 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
664 MS_REC | MS_SLAVE, NULL)) == -1) {
665 PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
666 << de->d_name;
667 _exit(1);
668 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700669
670 // Mount user-specific symlink helper into place
671 userid_t user_id = multiuser_get_user_id(uid);
672 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
673 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
674 NULL, MS_BIND, NULL)) == -1) {
675 PLOG(ERROR) << "Failed to mount " << userSource << " for "
676 << de->d_name;
677 _exit(1);
678 }
679
Jeff Sharkey66270a22015-06-24 11:49:24 -0700680 _exit(0);
681 }
682
683 if (child == -1) {
684 PLOG(ERROR) << "Failed to fork";
685 goto next;
686 } else {
687 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
688 }
689
690next:
691 close(nsFd);
692 close(pidFd);
693 }
694 closedir(dir);
695 return 0;
696}
697
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700698int VolumeManager::reset() {
699 // Tear down all existing disks/volumes and start from a blank slate so
700 // newly connected framework hears all events.
Gao Xiangd263da82017-08-14 11:32:13 +0800701 if (mInternalEmulated != nullptr) {
702 mInternalEmulated->destroy();
703 mInternalEmulated->create();
704 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700705 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700706 disk->destroy();
707 disk->create();
708 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600709 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700710 mAddedUsers.clear();
711 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700712 return 0;
713}
714
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700715// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700716int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700717 if (mInternalEmulated == nullptr) {
718 return 0; // already shutdown
719 }
Keun-young Park375ac252017-08-02 17:45:48 -0700720 shutting_down = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700721 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700722 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700723 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700724 disk->destroy();
725 }
726 mDisks.clear();
Keun-young Park375ac252017-08-02 17:45:48 -0700727 shutting_down = false;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700728 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700729}
730
Jeff Sharkey9c484982015-03-31 10:35:33 -0700731int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700732 std::lock_guard<std::mutex> lock(mLock);
733
Jeff Sharkey9c484982015-03-31 10:35:33 -0700734 // First, try gracefully unmounting all known devices
735 if (mInternalEmulated != nullptr) {
736 mInternalEmulated->unmount();
737 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700738 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700739 disk->unmountAll();
740 }
741
742 // Worst case we might have some stale mounts lurking around, so
743 // force unmount those just to be safe.
744 FILE* fp = setmntent("/proc/mounts", "r");
745 if (fp == NULL) {
746 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
747 return -errno;
748 }
749
750 // Some volumes can be stacked on each other, so force unmount in
751 // reverse order to give us the best chance of success.
752 std::list<std::string> toUnmount;
753 mntent* mentry;
754 while ((mentry = getmntent(fp)) != NULL) {
Tri Voe98adee2018-04-16 14:27:10 -0700755 auto test = std::string(mentry->mnt_dir);
756 if ((android::base::StartsWith(test, "/mnt/") &&
757 !android::base::StartsWith(test, "/mnt/vendor")) ||
758 android::base::StartsWith(test, "/storage/")) {
759 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700760 }
761 }
762 endmntent(fp);
763
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700764 for (const auto& path : toUnmount) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700765 SLOGW("Tearing down stale mount %s", path.c_str());
766 android::vold::ForceUnmount(path);
767 }
768
769 return 0;
770}
771
Kenny Root508c0e12010-07-12 09:59:49 -0700772int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
773 char idHash[33];
774 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
775 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
776 return -1;
777 }
778
779 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700780 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400781 if ((written < 0) || (written >= mountPathLen)) {
782 errno = EINVAL;
783 return -1;
784 }
Kenny Root508c0e12010-07-12 09:59:49 -0700785
786 if (access(mountPath, F_OK)) {
787 errno = ENOENT;
788 return -1;
789 }
790
791 return 0;
792}
793
San Mehata19b2502010-01-06 10:33:53 -0800794int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700795 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700796
Nick Kralevich0de7c612014-01-27 14:58:06 -0800797 if (!isLegalAsecId(id)) {
798 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
799 errno = EINVAL;
800 return -1;
801 }
802
Kenny Root344ca102012-04-03 17:23:01 -0700803 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
804 SLOGE("Couldn't find ASEC %s", id);
805 return -1;
806 }
San Mehat88ac2c02010-03-23 11:15:58 -0700807
808 memset(buffer, 0, maxlen);
809 if (access(asecFileName, F_OK)) {
810 errno = ENOENT;
811 return -1;
812 }
San Mehata19b2502010-01-06 10:33:53 -0800813
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700814 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400815 if ((written < 0) || (written >= maxlen)) {
816 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
817 errno = EINVAL;
818 return -1;
819 }
820
San Mehata19b2502010-01-06 10:33:53 -0800821 return 0;
822}
823
Dianne Hackborn736910c2011-06-27 13:37:07 -0700824int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
825 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700826
Nick Kralevich0de7c612014-01-27 14:58:06 -0800827 if (!isLegalAsecId(id)) {
828 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
829 errno = EINVAL;
830 return -1;
831 }
832
Kenny Root344ca102012-04-03 17:23:01 -0700833 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
834 SLOGE("Couldn't find ASEC %s", id);
835 return -1;
836 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700837
838 memset(buffer, 0, maxlen);
839 if (access(asecFileName, F_OK)) {
840 errno = ENOENT;
841 return -1;
842 }
843
rpcraigd1c226f2012-10-09 06:58:16 -0400844 int written = snprintf(buffer, maxlen, "%s", asecFileName);
845 if ((written < 0) || (written >= maxlen)) {
846 errno = EINVAL;
847 return -1;
848 }
849
Dianne Hackborn736910c2011-06-27 13:37:07 -0700850 return 0;
851}
852
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200853int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700854 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800855 struct asec_superblock sb;
856 memset(&sb, 0, sizeof(sb));
857
Nick Kralevich0de7c612014-01-27 14:58:06 -0800858 if (!isLegalAsecId(id)) {
859 SLOGE("createAsec: Invalid asec id \"%s\"", id);
860 errno = EINVAL;
861 return -1;
862 }
863
Kenny Root344ca102012-04-03 17:23:01 -0700864 const bool wantFilesystem = strcmp(fstype, "none");
865 bool usingExt4 = false;
866 if (wantFilesystem) {
867 usingExt4 = !strcmp(fstype, "ext4");
868 if (usingExt4) {
869 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
870 } else if (strcmp(fstype, "fat")) {
871 SLOGE("Invalid filesystem type %s", fstype);
872 errno = EINVAL;
873 return -1;
874 }
875 }
876
San Mehatfcf24fe2010-03-03 12:37:32 -0800877 sb.magic = ASEC_SB_MAGIC;
878 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800879
San Mehatd31e3802010-02-18 08:37:45 -0800880 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200881 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800882 errno = EINVAL;
883 return -1;
884 }
885
San Mehata19b2502010-01-06 10:33:53 -0800886 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700887
888 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
889 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
890 asecFileName, strerror(errno));
891 errno = EADDRINUSE;
892 return -1;
893 }
894
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700895 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700896
rpcraigd1c226f2012-10-09 06:58:16 -0400897 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
898 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
899 errno = EINVAL;
900 return -1;
901 }
San Mehata19b2502010-01-06 10:33:53 -0800902
903 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700904 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700905 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800906 errno = EADDRINUSE;
907 return -1;
908 }
909
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200910 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700911 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700912 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700913 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700914 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800915
916 // Add +1 for our superblock which is at the end
917 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700918 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800919 return -1;
920 }
921
San Mehatd9a4e352010-03-12 13:32:47 -0800922 char idHash[33];
923 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700924 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800925 unlink(asecFileName);
926 return -1;
927 }
928
San Mehata19b2502010-01-06 10:33:53 -0800929 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800930 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700931 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800932 unlink(asecFileName);
933 return -1;
934 }
935
San Mehatb78a32c2010-01-10 13:02:12 -0800936 char dmDevice[255];
937 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800938
San Mehatb78a32c2010-01-10 13:02:12 -0800939 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800940 // XXX: This is all we support for now
941 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800942 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800943 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700944 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800945 Loop::destroyByDevice(loopDevice);
946 unlink(asecFileName);
947 return -1;
948 }
949 cleanupDm = true;
950 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800951 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600952 strlcpy(dmDevice, loopDevice, sizeof(dmDevice));
San Mehatb78a32c2010-01-10 13:02:12 -0800953 }
954
San Mehatfcf24fe2010-03-03 12:37:32 -0800955 /*
956 * Drop down the superblock at the end of the file
957 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700958 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800959 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800960 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800961 }
962 Loop::destroyByDevice(loopDevice);
963 unlink(asecFileName);
964 return -1;
965 }
966
Kenny Root344ca102012-04-03 17:23:01 -0700967 if (wantFilesystem) {
968 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400969 char mountPoint[255];
970
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700971 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400972 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
973 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
974 if (cleanupDm) {
975 Devmapper::destroy(idHash);
976 }
977 Loop::destroyByDevice(loopDevice);
978 unlink(asecFileName);
979 return -1;
980 }
rpcraiga54e13a2012-09-21 14:17:08 -0400981
Kenny Root344ca102012-04-03 17:23:01 -0700982 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700983 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700984 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700985 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800986 }
San Mehata19b2502010-01-06 10:33:53 -0800987
Kenny Root344ca102012-04-03 17:23:01 -0700988 if (formatStatus < 0) {
989 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800990 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800991 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800992 }
San Mehateb13a902010-01-07 12:12:50 -0800993 Loop::destroyByDevice(loopDevice);
994 unlink(asecFileName);
995 return -1;
996 }
Kenny Root344ca102012-04-03 17:23:01 -0700997
Kenny Root344ca102012-04-03 17:23:01 -0700998 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800999 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001000 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -08001001 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001002 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001003 }
1004 Loop::destroyByDevice(loopDevice);
1005 unlink(asecFileName);
1006 return -1;
1007 }
San Mehatb78a32c2010-01-10 13:02:12 -08001008 }
San Mehata1091cb2010-02-28 20:17:20 -08001009
Kenny Root344ca102012-04-03 17:23:01 -07001010 int mountStatus;
1011 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001012 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
1013 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -07001014 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001015 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
1016 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -07001017 }
1018
1019 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -07001020 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -08001021 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001022 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -08001023 }
1024 Loop::destroyByDevice(loopDevice);
1025 unlink(asecFileName);
1026 return -1;
1027 }
Kenny Root344ca102012-04-03 17:23:01 -07001028
1029 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001030 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001031 if (dirfd >= 0) {
1032 if (fchown(dirfd, ownerUid, AID_SYSTEM)
1033 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
1034 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
1035 }
1036 close(dirfd);
1037 }
1038 }
San Mehata1091cb2010-02-28 20:17:20 -08001039 } else {
San Mehat97ac40e2010-03-24 10:24:19 -07001040 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -08001041 }
San Mehat88705162010-01-15 09:26:28 -08001042
Kenny Rootcbacf782010-09-24 15:11:48 -07001043 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -08001044 return 0;
1045}
1046
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001047int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001048 char asecFileName[255];
1049 char mountPoint[255];
1050 bool cleanupDm = false;
1051
1052 if (!isLegalAsecId(id)) {
1053 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
1054 errno = EINVAL;
1055 return -1;
1056 }
1057
1058 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1059 SLOGE("Couldn't find ASEC %s", id);
1060 return -1;
1061 }
1062
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001063 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001064 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1065 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
1066 return -1;
1067 }
1068
1069 if (isMountpointMounted(mountPoint)) {
1070 SLOGE("ASEC %s mounted. Unmount before resizing", id);
1071 errno = EBUSY;
1072 return -1;
1073 }
1074
1075 struct asec_superblock sb;
1076 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001077 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001078
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001079 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001080 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
1081 return -1;
1082 }
1083
1084 struct stat info;
1085 if (fstat(fd, &info) < 0) {
1086 SLOGE("Failed to get file size (%s)", strerror(errno));
1087 close(fd);
1088 return -1;
1089 }
1090
1091 oldNumSec = info.st_size / 512;
1092
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001093 /*
1094 * Try to read superblock.
1095 */
1096 memset(&sb, 0, sizeof(struct asec_superblock));
1097 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1098 SLOGE("lseek failed (%s)", strerror(errno));
1099 close(fd);
1100 return -1;
1101 }
1102 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1103 SLOGE("superblock read failed (%s)", strerror(errno));
1104 close(fd);
1105 return -1;
1106 }
1107 close(fd);
1108
1109 if (mDebug) {
1110 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1111 }
1112 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1113 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1114 errno = EMEDIUMTYPE;
1115 return -1;
1116 }
1117
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001118 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001119 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1120 SLOGE("Only ext4 partitions are supported for resize");
1121 errno = EINVAL;
1122 return -1;
Daniel Rosenberge4c291a2016-04-20 14:07:32 -07001123 } else {
1124 numImgSectors = adjustSectorNumExt4(numSectors);
1125 }
1126
1127 /*
1128 * add one block for the superblock
1129 */
1130 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
1131 if (oldNumSec == numImgSectors + 1) {
1132 SLOGW("Size unchanged; ignoring resize request");
1133 return 0;
1134 } else if (oldNumSec > numImgSectors + 1) {
1135 SLOGE("Only growing is currently supported.");
1136 close(fd);
1137 return -1;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001138 }
1139
1140 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1141 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1142 return -1;
1143 }
1144
1145 /*
1146 * Drop down a copy of the superblock at the end of the file
1147 */
1148 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1149 goto fail;
1150
1151 char idHash[33];
1152 if (!asecHash(id, idHash, sizeof(idHash))) {
1153 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1154 goto fail;
1155 }
1156
1157 char loopDevice[255];
1158 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1159 goto fail;
1160
1161 char dmDevice[255];
1162
1163 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1164 Loop::destroyByDevice(loopDevice);
1165 goto fail;
1166 }
1167
1168 /*
1169 * Wait for the device mapper node to be created.
1170 */
1171 waitForDevMapper(dmDevice);
1172
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001173 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001174 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1175 if (cleanupDm) {
1176 Devmapper::destroy(idHash);
1177 }
1178 Loop::destroyByDevice(loopDevice);
1179 goto fail;
1180 }
1181
1182 return 0;
1183fail:
1184 Loop::resizeImageFile(asecFileName, oldNumSec);
1185 return -1;
1186}
1187
San Mehata19b2502010-01-06 10:33:53 -08001188int VolumeManager::finalizeAsec(const char *id) {
1189 char asecFileName[255];
1190 char loopDevice[255];
1191 char mountPoint[255];
1192
Nick Kralevich0de7c612014-01-27 14:58:06 -08001193 if (!isLegalAsecId(id)) {
1194 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1195 errno = EINVAL;
1196 return -1;
1197 }
1198
Kenny Root344ca102012-04-03 17:23:01 -07001199 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1200 SLOGE("Couldn't find ASEC %s", id);
1201 return -1;
1202 }
San Mehata19b2502010-01-06 10:33:53 -08001203
San Mehatd9a4e352010-03-12 13:32:47 -08001204 char idHash[33];
1205 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001206 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001207 return -1;
1208 }
1209
1210 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001211 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001212 return -1;
1213 }
1214
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001215 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001216 struct asec_superblock sb;
1217
1218 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1219 return -1;
1220 }
1221
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001222 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001223 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1224 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1225 return -1;
1226 }
Kenny Root344ca102012-04-03 17:23:01 -07001227
1228 int result = 0;
1229 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001230 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1231 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001232 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001233 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1234 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001235 }
1236
1237 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001238 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001239 return -1;
1240 }
1241
San Mehatd9a4e352010-03-12 13:32:47 -08001242 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001243 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001244 }
San Mehata19b2502010-01-06 10:33:53 -08001245 return 0;
1246}
1247
Kenny Root344ca102012-04-03 17:23:01 -07001248int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1249 char asecFileName[255];
1250 char loopDevice[255];
1251 char mountPoint[255];
1252
1253 if (gid < AID_APP) {
1254 SLOGE("Group ID is not in application range");
1255 return -1;
1256 }
1257
Nick Kralevich0de7c612014-01-27 14:58:06 -08001258 if (!isLegalAsecId(id)) {
1259 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1260 errno = EINVAL;
1261 return -1;
1262 }
1263
Kenny Root344ca102012-04-03 17:23:01 -07001264 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1265 SLOGE("Couldn't find ASEC %s", id);
1266 return -1;
1267 }
1268
1269 char idHash[33];
1270 if (!asecHash(id, idHash, sizeof(idHash))) {
1271 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1272 return -1;
1273 }
1274
1275 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1276 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1277 return -1;
1278 }
1279
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001280 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001281 struct asec_superblock sb;
1282
1283 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1284 return -1;
1285 }
1286
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001287 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001288 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1289 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1290 return -1;
1291 }
Kenny Root344ca102012-04-03 17:23:01 -07001292
1293 int result = 0;
1294 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1295 return 0;
1296 }
1297
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001298 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001299 false /* read-only */,
1300 true /* remount */,
1301 false /* executable */);
1302 if (ret) {
1303 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1304 return -1;
1305 }
1306
1307 char *paths[] = { mountPoint, NULL };
1308
1309 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1310 if (fts) {
1311 // Traverse the entire hierarchy and chown to system UID.
1312 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1313 // We don't care about the lost+found directory.
1314 if (!strcmp(ftsent->fts_name, "lost+found")) {
1315 continue;
1316 }
1317
1318 /*
1319 * There can only be one file marked as private right now.
1320 * This should be more robust, but it satisfies the requirements
1321 * we have for right now.
1322 */
1323 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1324
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001325 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001326 if (fd < 0) {
1327 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1328 result = -1;
1329 continue;
1330 }
1331
1332 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1333
1334 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001335 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001336 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001337 result |= fchmod(fd, privateFile ? 0640 : 0644);
1338 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001339
Stephen Smalley5093e612014-02-12 09:43:08 -05001340 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001341 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1342 result |= -1;
1343 }
1344
Kenny Root344ca102012-04-03 17:23:01 -07001345 close(fd);
1346 }
1347 fts_close(fts);
1348
1349 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001350 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001351 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1352 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1353 result |= -1;
1354 }
1355 close(dirfd);
1356 } else {
1357 result |= -1;
1358 }
1359
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001360 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001361 true /* read-only */,
1362 true /* remount */,
1363 true /* execute */);
1364
1365 if (result) {
1366 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1367 return -1;
1368 }
1369
1370 if (mDebug) {
1371 SLOGD("ASEC %s permissions fixed", id);
1372 }
1373 return 0;
1374}
1375
San Mehat048b0802010-01-23 08:17:06 -08001376int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001377 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001378 char *asecFilename2;
1379 char mountPoint[255];
1380
Kenny Root344ca102012-04-03 17:23:01 -07001381 const char *dir;
1382
Nick Kralevich0de7c612014-01-27 14:58:06 -08001383 if (!isLegalAsecId(id1)) {
1384 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1385 errno = EINVAL;
1386 return -1;
1387 }
1388
1389 if (!isLegalAsecId(id2)) {
1390 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1391 errno = EINVAL;
1392 return -1;
1393 }
1394
Kenny Root344ca102012-04-03 17:23:01 -07001395 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1396 SLOGE("Couldn't find ASEC %s", id1);
1397 return -1;
1398 }
1399
1400 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001401
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001402 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001403 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1404 SLOGE("Rename failed: couldn't construct mountpoint");
1405 goto out_err;
1406 }
1407
San Mehat048b0802010-01-23 08:17:06 -08001408 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001409 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001410 errno = EBUSY;
1411 goto out_err;
1412 }
1413
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001414 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001415 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1416 SLOGE("Rename failed: couldn't construct mountpoint2");
1417 goto out_err;
1418 }
1419
San Mehat96956ed2010-02-24 08:42:51 -08001420 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001421 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001422 errno = EBUSY;
1423 goto out_err;
1424 }
1425
San Mehat048b0802010-01-23 08:17:06 -08001426 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001427 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001428 errno = EADDRINUSE;
1429 goto out_err;
1430 }
1431
1432 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001433 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001434 goto out_err;
1435 }
1436
San Mehat048b0802010-01-23 08:17:06 -08001437 free(asecFilename2);
1438 return 0;
1439
1440out_err:
San Mehat048b0802010-01-23 08:17:06 -08001441 free(asecFilename2);
1442 return -1;
1443}
1444
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001445#define UNMOUNT_RETRIES 5
1446#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001447int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001448 char asecFileName[255];
1449 char mountPoint[255];
1450
Nick Kralevich0de7c612014-01-27 14:58:06 -08001451 if (!isLegalAsecId(id)) {
1452 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1453 errno = EINVAL;
1454 return -1;
1455 }
1456
Kenny Root344ca102012-04-03 17:23:01 -07001457 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1458 SLOGE("Couldn't find ASEC %s", id);
1459 return -1;
1460 }
1461
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001462 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001463 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1464 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1465 return -1;
1466 }
San Mehata19b2502010-01-06 10:33:53 -08001467
San Mehatd9a4e352010-03-12 13:32:47 -08001468 char idHash[33];
1469 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001470 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001471 return -1;
1472 }
1473
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001474 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1475}
1476
Kenny Root508c0e12010-07-12 09:59:49 -07001477int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001478 char mountPoint[255];
1479
1480 char idHash[33];
1481 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1482 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1483 return -1;
1484 }
1485
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001486 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001487 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1488 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1489 return -1;
1490 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001491
1492 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1493}
1494
1495int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1496 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001497 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001498 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001499 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001500 return -1;
1501 }
San Mehat23969932010-01-09 07:08:06 -08001502
San Mehatb78a32c2010-01-10 13:02:12 -08001503 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001504 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001505 rc = umount(mountPoint);
1506 if (!rc) {
1507 break;
San Mehata19b2502010-01-06 10:33:53 -08001508 }
San Mehatb78a32c2010-01-10 13:02:12 -08001509 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001510 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001511 rc = 0;
1512 break;
San Mehata19b2502010-01-06 10:33:53 -08001513 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001514 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001515 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001516
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001517 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001518
1519 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001520 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001521 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001522 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001523 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001524 }
San Mehat8c940ef2010-02-13 14:19:53 -08001525
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001526 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001527 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001528 }
1529
1530 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001531 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001532 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001533 return -1;
1534 }
1535
San Mehat12f4b892010-02-24 11:43:22 -08001536 int retries = 10;
1537
1538 while(retries--) {
1539 if (!rmdir(mountPoint)) {
1540 break;
1541 }
1542
San Mehat97ac40e2010-03-24 10:24:19 -07001543 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001544 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001545 }
1546
1547 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001548 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001549 }
San Mehat88705162010-01-15 09:26:28 -08001550
Paul Lawrence60dec162014-09-02 10:52:15 -07001551 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1552 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1553 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1554 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1555 continue;
1556 } else {
1557 break;
1558 }
San Mehata19b2502010-01-06 10:33:53 -08001559 }
1560
1561 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001562 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001563 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001564 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001565 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001566 }
San Mehat88705162010-01-15 09:26:28 -08001567
1568 AsecIdCollection::iterator it;
1569 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001570 ContainerData* cd = *it;
1571 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001572 free(*it);
1573 mActiveContainers->erase(it);
1574 break;
1575 }
1576 }
1577 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001578 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001579 }
San Mehatb78a32c2010-01-10 13:02:12 -08001580 return 0;
1581}
1582
San Mehat4ba89482010-02-18 09:00:18 -08001583int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001584 char asecFileName[255];
1585 char mountPoint[255];
1586
Nick Kralevich0de7c612014-01-27 14:58:06 -08001587 if (!isLegalAsecId(id)) {
1588 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1589 errno = EINVAL;
1590 return -1;
1591 }
1592
Kenny Root344ca102012-04-03 17:23:01 -07001593 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1594 SLOGE("Couldn't find ASEC %s", id);
1595 return -1;
1596 }
1597
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001598 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001599 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1600 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1601 return -1;
1602 }
San Mehatb78a32c2010-01-10 13:02:12 -08001603
San Mehat0586d542010-01-12 15:38:59 -08001604 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001605 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001606 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001607 }
San Mehat4ba89482010-02-18 09:00:18 -08001608 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001609 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001610 return -1;
1611 }
1612 }
San Mehata19b2502010-01-06 10:33:53 -08001613
San Mehat0586d542010-01-12 15:38:59 -08001614 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001615 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001616 return -1;
1617 }
San Mehata19b2502010-01-06 10:33:53 -08001618
San Mehatd9a4e352010-03-12 13:32:47 -08001619 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001620 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001621 }
San Mehata19b2502010-01-06 10:33:53 -08001622 return 0;
1623}
1624
Nick Kralevich0de7c612014-01-27 14:58:06 -08001625/*
1626 * Legal ASEC ids consist of alphanumeric characters, '-',
1627 * '_', or '.'. ".." is not allowed. The first or last character
1628 * of the ASEC id cannot be '.' (dot).
1629 */
1630bool VolumeManager::isLegalAsecId(const char *id) const {
1631 size_t i;
1632 size_t len = strlen(id);
1633
1634 if (len == 0) {
1635 return false;
1636 }
1637 if ((id[0] == '.') || (id[len - 1] == '.')) {
1638 return false;
1639 }
1640
1641 for (i = 0; i < len; i++) {
1642 if (id[i] == '.') {
1643 // i=0 is guaranteed never to have a dot. See above.
1644 if (id[i-1] == '.') return false;
1645 continue;
1646 }
1647 if (id[i] == '_' || id[i] == '-') continue;
1648 if (id[i] >= 'a' && id[i] <= 'z') continue;
1649 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1650 if (id[i] >= '0' && id[i] <= '9') continue;
1651 return false;
1652 }
1653
1654 return true;
1655}
1656
Kenny Root344ca102012-04-03 17:23:01 -07001657bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001658 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001659 if (dirfd < 0) {
1660 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001661 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001662 }
1663
Nick Kralevich25e581a2015-02-06 08:55:08 -08001664 struct stat sb;
1665 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1666 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001667
1668 close(dirfd);
1669
1670 return ret;
1671}
1672
1673int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1674 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001675 char *asecName;
1676
Nick Kralevich0de7c612014-01-27 14:58:06 -08001677 if (!isLegalAsecId(id)) {
1678 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1679 errno = EINVAL;
1680 return -1;
1681 }
1682
Kenny Root344ca102012-04-03 17:23:01 -07001683 if (asprintf(&asecName, "%s.asec", id) < 0) {
1684 SLOGE("Couldn't allocate string to write ASEC name");
1685 return -1;
1686 }
1687
1688 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001689 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1690 dir = VolumeManager::SEC_ASECDIR_INT;
1691 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1692 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001693 } else {
1694 free(asecName);
1695 return -1;
1696 }
1697
1698 if (directory != NULL) {
1699 *directory = dir;
1700 }
1701
1702 if (asecPath != NULL) {
1703 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001704 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1705 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001706 free(asecName);
1707 return -1;
1708 }
1709 }
1710
1711 free(asecName);
1712 return 0;
1713}
1714
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001715int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001716 char asecFileName[255];
1717 char mountPoint[255];
1718
Nick Kralevich0de7c612014-01-27 14:58:06 -08001719 if (!isLegalAsecId(id)) {
1720 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1721 errno = EINVAL;
1722 return -1;
1723 }
1724
Kenny Root344ca102012-04-03 17:23:01 -07001725 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1726 SLOGE("Couldn't find ASEC %s", id);
1727 return -1;
1728 }
1729
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001730 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001731 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001732 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001733 return -1;
1734 }
San Mehata19b2502010-01-06 10:33:53 -08001735
1736 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001737 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001738 errno = EBUSY;
1739 return -1;
1740 }
1741
San Mehatd9a4e352010-03-12 13:32:47 -08001742 char idHash[33];
1743 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001744 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001745 return -1;
1746 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001747
San Mehata19b2502010-01-06 10:33:53 -08001748 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001749 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1750 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001751
1752 char dmDevice[255];
1753 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001754
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001755 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001756 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001757
Kenny Root344ca102012-04-03 17:23:01 -07001758 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1759 return -1;
1760 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001761
San Mehatd9a4e352010-03-12 13:32:47 -08001762 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001763 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001764 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001765 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001766 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001767 Loop::destroyByDevice(loopDevice);
1768 errno = EMEDIUMTYPE;
1769 return -1;
1770 }
1771 nr_sec--; // We don't want the devmapping to extend onto our superblock
1772
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001773 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1774 Loop::destroyByDevice(loopDevice);
1775 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001776 }
1777
Kenny Root344ca102012-04-03 17:23:01 -07001778 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001779 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001780 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001781 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001782 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001783 }
1784 Loop::destroyByDevice(loopDevice);
1785 return -1;
1786 }
San Mehata19b2502010-01-06 10:33:53 -08001787 }
1788
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001789 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001790 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001791 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001792 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001793
Kenny Root344ca102012-04-03 17:23:01 -07001794 int result;
1795 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001796 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1797 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001798 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001799 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1800 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001801 }
1802
1803 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001804 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001805 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001806 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001807 }
1808 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001809 return -1;
1810 }
1811
Kenny Rootcbacf782010-09-24 15:11:48 -07001812 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001813 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001814 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001815 }
San Mehata19b2502010-01-06 10:33:53 -08001816 return 0;
1817}
1818
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001819/**
1820 * Mounts an image file <code>img</code>.
1821 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001822int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001823 char mountPoint[255];
1824
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001825 char idHash[33];
1826 if (!asecHash(img, idHash, sizeof(idHash))) {
1827 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1828 return -1;
1829 }
1830
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001831 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001832 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001833 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001834 return -1;
1835 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001836
1837 if (isMountpointMounted(mountPoint)) {
1838 SLOGE("Image %s already mounted", img);
1839 errno = EBUSY;
1840 return -1;
1841 }
1842
1843 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001844 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1845 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001846
1847 char dmDevice[255];
1848 bool cleanupDm = false;
1849 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001850 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001851
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001852 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001853 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1854 Loop::destroyByDevice(loopDevice);
1855 return -1;
1856 }
1857
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001858 get_blkdev_size(fd, &nr_sec);
1859 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001860 SLOGE("Failed to get loop size (%s)", strerror(errno));
1861 Loop::destroyByDevice(loopDevice);
1862 close(fd);
1863 return -1;
1864 }
1865
1866 close(fd);
1867
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001868 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001869 Loop::destroyByDevice(loopDevice);
1870 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001871 }
1872
1873 if (mkdir(mountPoint, 0755)) {
1874 if (errno != EEXIST) {
1875 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1876 if (cleanupDm) {
1877 Devmapper::destroy(idHash);
1878 }
1879 Loop::destroyByDevice(loopDevice);
1880 return -1;
1881 }
1882 }
1883
yoshiyuki hama476a6272015-01-28 16:37:23 +09001884 /*
1885 * Wait for the device mapper node to be created.
1886 */
1887 waitForDevMapper(dmDevice);
1888
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001889 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1890 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001891 SLOGE("Image mount failed (%s)", strerror(errno));
1892 if (cleanupDm) {
1893 Devmapper::destroy(idHash);
1894 }
1895 Loop::destroyByDevice(loopDevice);
1896 return -1;
1897 }
1898
Kenny Rootcbacf782010-09-24 15:11:48 -07001899 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001900 if (mDebug) {
1901 SLOGD("Image %s mounted", img);
1902 }
1903 return 0;
1904}
1905
Kenny Root508c0e12010-07-12 09:59:49 -07001906int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001907 FILE *fp = setmntent("/proc/mounts", "r");
1908 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001909 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1910 return -1;
1911 }
1912
1913 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001914 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001915 char loopDir[loopDirLen + 2];
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001916 strlcpy(loopDir, VolumeManager::LOOPDIR, sizeof(loopDir));
Kenny Root508c0e12010-07-12 09:59:49 -07001917 loopDir[loopDirLen++] = '/';
1918 loopDir[loopDirLen] = '\0';
1919
Yabin Cuid1104f72015-01-02 13:28:28 -08001920 mntent* mentry;
1921 while ((mentry = getmntent(fp)) != NULL) {
1922 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001923 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001924 if (fd >= 0) {
1925 struct loop_info64 li;
1926 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1927 cli->sendMsg(ResponseCode::AsecListResult,
1928 (const char*) li.lo_file_name, false);
1929 }
1930 close(fd);
1931 }
1932 }
1933 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001934 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001935 return 0;
1936}
1937
Jeff Sharkey9c484982015-03-31 10:35:33 -07001938extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001939 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001940 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001941}
1942
San Mehata19b2502010-01-06 10:33:53 -08001943bool VolumeManager::isMountpointMounted(const char *mp)
1944{
Yabin Cuid1104f72015-01-02 13:28:28 -08001945 FILE *fp = setmntent("/proc/mounts", "r");
1946 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001947 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001948 return false;
1949 }
1950
Yabin Cuid1104f72015-01-02 13:28:28 -08001951 bool found_mp = false;
1952 mntent* mentry;
1953 while ((mentry = getmntent(fp)) != NULL) {
1954 if (strcmp(mentry->mnt_dir, mp) == 0) {
1955 found_mp = true;
1956 break;
San Mehata19b2502010-01-06 10:33:53 -08001957 }
San Mehata19b2502010-01-06 10:33:53 -08001958 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001959 endmntent(fp);
1960 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001961}
1962
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001963int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001964 // Only offer to create directories for paths managed by vold
1965 if (strncmp(path, "/storage/", 9) == 0) {
1966 // fs_mkdirs() does symlink checking and relative path enforcement
1967 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001968 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001969 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001970 return -EINVAL;
1971 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001972}