San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 18 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 20 | #include <fts.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 21 | #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 Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 30 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 31 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 32 | |
| 33 | #define LOG_TAG "Vold" |
| 34 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 35 | #include <openssl/md5.h> |
| 36 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 37 | #include <base/logging.h> |
| 38 | #include <base/stringprintf.h> |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 39 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 40 | #include <cutils/log.h> |
| 41 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 42 | #include <selinux/android.h> |
| 43 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 44 | #include <sysutils/NetlinkEvent.h> |
| 45 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> |
| 47 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 48 | #include "EmulatedVolume.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 49 | #include "VolumeManager.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 50 | #include "NetlinkManager.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 51 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 52 | #include "Loop.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 53 | #include "Ext4.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 54 | #include "Fat.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 55 | #include "Utils.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 56 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 57 | #include "Process.h" |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 58 | #include "Asec.h" |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 59 | #include "VoldUtil.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 60 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 61 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 62 | #define DEBUG_NETLINK 0 |
| 63 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 64 | #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file" |
| 65 | |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 66 | #define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\ |
| 67 | + (number & (~((1U << po2) - 1)))) |
| 68 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 69 | using android::base::StringPrintf; |
| 70 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 71 | /* |
| 72 | * Path to external storage where *only* root can access ASEC image files |
| 73 | */ |
| 74 | const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec"; |
| 75 | |
| 76 | /* |
| 77 | * Path to internal storage where *only* root can access ASEC image files |
| 78 | */ |
| 79 | const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec"; |
| 80 | |
| 81 | /* |
| 82 | * Path to where secure containers are mounted |
| 83 | */ |
| 84 | const char *VolumeManager::ASECDIR = "/mnt/asec"; |
| 85 | |
| 86 | /* |
| 87 | * Path to where OBBs are mounted |
| 88 | */ |
| 89 | const char *VolumeManager::LOOPDIR = "/mnt/obb"; |
| 90 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 91 | static const char* kUserMountPath = "/mnt/user"; |
| 92 | |
| 93 | static const unsigned int kMajorBlockScsi = 8; |
| 94 | static const unsigned int kMajorBlockMmc = 179; |
| 95 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 96 | /* writes superblock at end of file or device given by name */ |
| 97 | static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) { |
| 98 | int sbfd = open(name, O_RDWR); |
| 99 | if (sbfd < 0) { |
| 100 | SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno)); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 105 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
| 106 | close(sbfd); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 111 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
| 112 | close(sbfd); |
| 113 | return -1; |
| 114 | } |
| 115 | close(sbfd); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int adjustSectorNumExt4(unsigned numSectors) { |
Daniel Rosenberg | e9196fe | 2014-06-10 17:16:03 -0700 | [diff] [blame] | 120 | // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for |
| 121 | // preventing costly operations or unexpected ENOSPC error. |
| 122 | // Ext4::format() uses default block size without clustering. |
| 123 | unsigned clusterSectors = 4096 / 512; |
| 124 | unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0); |
| 125 | numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 126 | return ROUND_UP_POWER_OF_2(numSectors, 3); |
| 127 | } |
| 128 | |
| 129 | static int adjustSectorNumFAT(unsigned numSectors) { |
| 130 | /* |
| 131 | * Add some headroom |
| 132 | */ |
| 133 | unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; |
| 134 | numSectors += fatSize + 2; |
| 135 | /* |
| 136 | * FAT is aligned to 32 kb with 512b sectors. |
| 137 | */ |
| 138 | return ROUND_UP_POWER_OF_2(numSectors, 6); |
| 139 | } |
| 140 | |
| 141 | static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) { |
| 142 | if (Loop::lookupActive(idHash, buffer, len)) { |
| 143 | if (Loop::create(idHash, asecFileName, buffer, len)) { |
| 144 | SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno)); |
| 145 | return -1; |
| 146 | } |
| 147 | if (debug) { |
| 148 | SLOGD("New loop device created at %s", buffer); |
| 149 | } |
| 150 | } else { |
| 151 | if (debug) { |
| 152 | SLOGD("Found active loopback for %s at %s", asecFileName, buffer); |
| 153 | } |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) { |
| 159 | if (strcmp(key, "none")) { |
| 160 | if (Devmapper::lookupActive(idHash, buffer, len)) { |
| 161 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, |
| 162 | buffer, len)) { |
| 163 | SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno)); |
| 164 | return -1; |
| 165 | } |
| 166 | if (debug) { |
| 167 | SLOGD("New devmapper instance created at %s", buffer); |
| 168 | } |
| 169 | } else { |
| 170 | if (debug) { |
| 171 | SLOGD("Found active devmapper for %s at %s", asecFileName, buffer); |
| 172 | } |
| 173 | } |
| 174 | *createdDMDevice = true; |
| 175 | } else { |
| 176 | strcpy(buffer, loopDevice); |
| 177 | *createdDMDevice = false; |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static void waitForDevMapper(const char *dmDevice) { |
| 183 | /* |
| 184 | * Wait for the device mapper node to be created. Sometimes it takes a |
| 185 | * while. Wait for up to 1 second. We could also inspect incoming uevents, |
| 186 | * but that would take more effort. |
| 187 | */ |
| 188 | int tries = 25; |
| 189 | while (tries--) { |
| 190 | if (!access(dmDevice, F_OK) || errno != ENOENT) { |
| 191 | break; |
| 192 | } |
| 193 | usleep(40 * 1000); |
| 194 | } |
| 195 | } |
| 196 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 197 | VolumeManager *VolumeManager::sInstance = NULL; |
| 198 | |
| 199 | VolumeManager *VolumeManager::Instance() { |
| 200 | if (!sInstance) |
| 201 | sInstance = new VolumeManager(); |
| 202 | return sInstance; |
| 203 | } |
| 204 | |
| 205 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 206 | mDebug = false; |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 207 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 208 | mBroadcaster = NULL; |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 209 | mUmsSharingCount = 0; |
| 210 | mSavedDirtyRatio = -1; |
| 211 | // set dirty ratio to 0 when UMS is active |
| 212 | mUmsDirtyRatio = 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 216 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 219 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 220 | static const char* digits = "0123456789abcdef"; |
| 221 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 222 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 223 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 224 | if (buffer == NULL) { |
| 225 | SLOGE("Destination buffer is NULL"); |
| 226 | errno = ESPIPE; |
| 227 | return NULL; |
| 228 | } else if (id == NULL) { |
| 229 | SLOGE("Source buffer is NULL"); |
| 230 | errno = ESPIPE; |
| 231 | return NULL; |
| 232 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 233 | SLOGE("Target hash buffer size < %d bytes (%zu)", |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 234 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 235 | errno = ESPIPE; |
| 236 | return NULL; |
| 237 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 238 | |
| 239 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 240 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 241 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 242 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 243 | *p++ = digits[sig[i] >> 4]; |
| 244 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 245 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 246 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 247 | |
| 248 | return buffer; |
| 249 | } |
| 250 | |
| 251 | void VolumeManager::setDebug(bool enable) { |
| 252 | mDebug = enable; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 253 | } |
| 254 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 255 | int VolumeManager::start() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 256 | // Always start from a clean slate by unmounting everything in |
| 257 | // directories that we own, in case we crashed. |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 258 | unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 259 | |
| 260 | // Assume that we always have an emulated volume on internal |
| 261 | // storage; the framework will decide if it should be mounted. |
| 262 | mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>( |
| 263 | new android::vold::EmulatedVolume("/data/media", "")); |
| 264 | mInternalEmulated->create(); |
| 265 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | int VolumeManager::stop() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 270 | mInternalEmulated->destroy(); |
| 271 | mInternalEmulated = nullptr; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 275 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 276 | #if DEBUG_NETLINK |
| 277 | LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction(); |
| 278 | evt->dump(); |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 279 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 280 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 281 | std::string eventPath(evt->findParam("DEVPATH")); |
| 282 | std::string devType(evt->findParam("DEVTYPE")); |
| 283 | |
| 284 | if (devType != "disk") return; |
| 285 | |
| 286 | int major = atoi(evt->findParam("MAJOR")); |
| 287 | int minor = atoi(evt->findParam("MINOR")); |
| 288 | dev_t device = makedev(major, minor); |
| 289 | |
| 290 | switch (evt->getAction()) { |
| 291 | case NetlinkEvent::Action::kAdd: { |
| 292 | for (auto source : mDiskSources) { |
| 293 | if (source->matches(eventPath)) { |
| 294 | // For now, assume that MMC devices are SD, and that |
| 295 | // everything else is USB |
| 296 | int flags = source->getFlags(); |
| 297 | if (major == kMajorBlockMmc) { |
| 298 | flags |= android::vold::Disk::Flags::kSd; |
| 299 | } else { |
| 300 | flags |= android::vold::Disk::Flags::kUsb; |
| 301 | } |
| 302 | |
| 303 | auto disk = new android::vold::Disk(eventPath, device, |
| 304 | source->getNickname(), flags); |
| 305 | disk->create(); |
| 306 | mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk)); |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | break; |
| 311 | } |
| 312 | case NetlinkEvent::Action::kChange: { |
| 313 | for (auto disk : mDisks) { |
| 314 | if (disk->getDevice() == device) { |
| 315 | disk->readMetadata(); |
| 316 | disk->readPartitions(); |
| 317 | } |
| 318 | } |
| 319 | break; |
| 320 | } |
| 321 | case NetlinkEvent::Action::kRemove: { |
| 322 | auto i = mDisks.begin(); |
| 323 | while (i != mDisks.end()) { |
| 324 | if ((*i)->getDevice() == device) { |
| 325 | (*i)->destroy(); |
| 326 | i = mDisks.erase(i); |
| 327 | } else { |
| 328 | ++i; |
| 329 | } |
| 330 | } |
| 331 | break; |
| 332 | } |
| 333 | default: { |
| 334 | LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction(); |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) { |
| 341 | mDiskSources.push_back(diskSource); |
| 342 | } |
| 343 | |
| 344 | std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) { |
| 345 | for (auto disk : mDisks) { |
| 346 | if (disk->getId() == id) { |
| 347 | return disk; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 350 | return nullptr; |
| 351 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 352 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 353 | std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) { |
| 354 | if (mInternalEmulated->getId() == id) { |
| 355 | return mInternalEmulated; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 356 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 357 | for (auto disk : mDisks) { |
| 358 | auto vol = disk->findVolume(id); |
| 359 | if (vol != nullptr) { |
| 360 | return vol; |
| 361 | } |
| 362 | } |
| 363 | return nullptr; |
| 364 | } |
| 365 | |
| 366 | int VolumeManager::linkPrimary(userid_t userId) { |
| 367 | std::string source(mPrimary->getPath()); |
| 368 | if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) { |
| 369 | source = StringPrintf("%s/%d", source.c_str(), userId); |
| 370 | } |
| 371 | |
| 372 | std::string target(StringPrintf("/mnt/user/%d/primary", userId)); |
| 373 | if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) { |
| 374 | if (errno != ENOENT) { |
| 375 | SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno)); |
| 376 | } |
| 377 | } |
| 378 | if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) { |
| 379 | SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(), |
| 380 | strerror(errno)); |
| 381 | return -errno; |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | int VolumeManager::startUser(userid_t userId) { |
| 387 | // Note that sometimes the system will spin up processes from Zygote |
| 388 | // before actually starting the user, so we're okay if Zygote |
| 389 | // already created this directory. |
| 390 | std::string path(StringPrintf("%s/%d", kUserMountPath, userId)); |
| 391 | fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT); |
| 392 | |
| 393 | mUsers.push_back(userId); |
| 394 | if (mPrimary) { |
| 395 | linkPrimary(userId); |
| 396 | } |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | int VolumeManager::cleanupUser(userid_t userId) { |
| 401 | mUsers.remove(userId); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) { |
| 406 | mPrimary = vol; |
| 407 | for (userid_t userId : mUsers) { |
| 408 | linkPrimary(userId); |
| 409 | } |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | int VolumeManager::reset() { |
| 414 | // Tear down all existing disks/volumes and start from a blank slate so |
| 415 | // newly connected framework hears all events. |
| 416 | mInternalEmulated->destroy(); |
| 417 | mInternalEmulated->create(); |
| 418 | for (auto disk : mDisks) { |
| 419 | disk->destroy(); |
| 420 | disk->create(); |
| 421 | } |
| 422 | mUsers.clear(); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | int VolumeManager::shutdown() { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 427 | mInternalEmulated->destroy(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 428 | for (auto disk : mDisks) { |
| 429 | disk->destroy(); |
| 430 | } |
| 431 | mDisks.clear(); |
| 432 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 435 | int VolumeManager::unmountAll() { |
| 436 | // First, try gracefully unmounting all known devices |
| 437 | if (mInternalEmulated != nullptr) { |
| 438 | mInternalEmulated->unmount(); |
| 439 | } |
| 440 | for (auto disk : mDisks) { |
| 441 | disk->unmountAll(); |
| 442 | } |
| 443 | |
| 444 | // Worst case we might have some stale mounts lurking around, so |
| 445 | // force unmount those just to be safe. |
| 446 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 447 | if (fp == NULL) { |
| 448 | SLOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 449 | return -errno; |
| 450 | } |
| 451 | |
| 452 | // Some volumes can be stacked on each other, so force unmount in |
| 453 | // reverse order to give us the best chance of success. |
| 454 | std::list<std::string> toUnmount; |
| 455 | mntent* mentry; |
| 456 | while ((mentry = getmntent(fp)) != NULL) { |
| 457 | if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0 |
| 458 | || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) { |
| 459 | toUnmount.push_front(std::string(mentry->mnt_dir)); |
| 460 | } |
| 461 | } |
| 462 | endmntent(fp); |
| 463 | |
| 464 | for (auto path : toUnmount) { |
| 465 | SLOGW("Tearing down stale mount %s", path.c_str()); |
| 466 | android::vold::ForceUnmount(path); |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 472 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 473 | char idHash[33]; |
| 474 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 475 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | memset(mountPath, 0, mountPathLen); |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 480 | int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 481 | if ((written < 0) || (written >= mountPathLen)) { |
| 482 | errno = EINVAL; |
| 483 | return -1; |
| 484 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 485 | |
| 486 | if (access(mountPath, F_OK)) { |
| 487 | errno = ENOENT; |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 494 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 495 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 496 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 497 | if (!isLegalAsecId(id)) { |
| 498 | SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id); |
| 499 | errno = EINVAL; |
| 500 | return -1; |
| 501 | } |
| 502 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 503 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 504 | SLOGE("Couldn't find ASEC %s", id); |
| 505 | return -1; |
| 506 | } |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 507 | |
| 508 | memset(buffer, 0, maxlen); |
| 509 | if (access(asecFileName, F_OK)) { |
| 510 | errno = ENOENT; |
| 511 | return -1; |
| 512 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 513 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 514 | int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 515 | if ((written < 0) || (written >= maxlen)) { |
| 516 | SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id); |
| 517 | errno = EINVAL; |
| 518 | return -1; |
| 519 | } |
| 520 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 524 | int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) { |
| 525 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 526 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 527 | if (!isLegalAsecId(id)) { |
| 528 | SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id); |
| 529 | errno = EINVAL; |
| 530 | return -1; |
| 531 | } |
| 532 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 533 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 534 | SLOGE("Couldn't find ASEC %s", id); |
| 535 | return -1; |
| 536 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 537 | |
| 538 | memset(buffer, 0, maxlen); |
| 539 | if (access(asecFileName, F_OK)) { |
| 540 | errno = ENOENT; |
| 541 | return -1; |
| 542 | } |
| 543 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 544 | int written = snprintf(buffer, maxlen, "%s", asecFileName); |
| 545 | if ((written < 0) || (written >= maxlen)) { |
| 546 | errno = EINVAL; |
| 547 | return -1; |
| 548 | } |
| 549 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 553 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype, |
| 554 | const char *key, const int ownerUid, bool isExternal) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 555 | struct asec_superblock sb; |
| 556 | memset(&sb, 0, sizeof(sb)); |
| 557 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 558 | if (!isLegalAsecId(id)) { |
| 559 | SLOGE("createAsec: Invalid asec id \"%s\"", id); |
| 560 | errno = EINVAL; |
| 561 | return -1; |
| 562 | } |
| 563 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 564 | const bool wantFilesystem = strcmp(fstype, "none"); |
| 565 | bool usingExt4 = false; |
| 566 | if (wantFilesystem) { |
| 567 | usingExt4 = !strcmp(fstype, "ext4"); |
| 568 | if (usingExt4) { |
| 569 | sb.c_opts |= ASEC_SB_C_OPTS_EXT4; |
| 570 | } else if (strcmp(fstype, "fat")) { |
| 571 | SLOGE("Invalid filesystem type %s", fstype); |
| 572 | errno = EINVAL; |
| 573 | return -1; |
| 574 | } |
| 575 | } |
| 576 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 577 | sb.magic = ASEC_SB_MAGIC; |
| 578 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 579 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 580 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 581 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 582 | errno = EINVAL; |
| 583 | return -1; |
| 584 | } |
| 585 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 586 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 587 | |
| 588 | if (!findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 589 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
| 590 | asecFileName, strerror(errno)); |
| 591 | errno = EADDRINUSE; |
| 592 | return -1; |
| 593 | } |
| 594 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 595 | const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 596 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 597 | int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id); |
| 598 | if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) { |
| 599 | errno = EINVAL; |
| 600 | return -1; |
| 601 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 602 | |
| 603 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 604 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 605 | asecFileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 606 | errno = EADDRINUSE; |
| 607 | return -1; |
| 608 | } |
| 609 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 610 | unsigned numImgSectors; |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 611 | if (usingExt4) |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 612 | numImgSectors = adjustSectorNumExt4(numSectors); |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 613 | else |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 614 | numImgSectors = adjustSectorNumFAT(numSectors); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 615 | |
| 616 | // Add +1 for our superblock which is at the end |
| 617 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 618 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 619 | return -1; |
| 620 | } |
| 621 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 622 | char idHash[33]; |
| 623 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 624 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 625 | unlink(asecFileName); |
| 626 | return -1; |
| 627 | } |
| 628 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 629 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 630 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 631 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 632 | unlink(asecFileName); |
| 633 | return -1; |
| 634 | } |
| 635 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 636 | char dmDevice[255]; |
| 637 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 638 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 639 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 640 | // XXX: This is all we support for now |
| 641 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 642 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 643 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 644 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 645 | Loop::destroyByDevice(loopDevice); |
| 646 | unlink(asecFileName); |
| 647 | return -1; |
| 648 | } |
| 649 | cleanupDm = true; |
| 650 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 651 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 652 | strcpy(dmDevice, loopDevice); |
| 653 | } |
| 654 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 655 | /* |
| 656 | * Drop down the superblock at the end of the file |
| 657 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 658 | if (writeSuperBlock(loopDevice, &sb, numImgSectors)) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 659 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 660 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 661 | } |
| 662 | Loop::destroyByDevice(loopDevice); |
| 663 | unlink(asecFileName); |
| 664 | return -1; |
| 665 | } |
| 666 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 667 | if (wantFilesystem) { |
| 668 | int formatStatus; |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 669 | char mountPoint[255]; |
| 670 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 671 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 672 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 673 | SLOGE("ASEC fs format failed: couldn't construct mountPoint"); |
| 674 | if (cleanupDm) { |
| 675 | Devmapper::destroy(idHash); |
| 676 | } |
| 677 | Loop::destroyByDevice(loopDevice); |
| 678 | unlink(asecFileName); |
| 679 | return -1; |
| 680 | } |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 681 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 682 | if (usingExt4) { |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 683 | formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 684 | } else { |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 685 | formatStatus = Fat::format(dmDevice, numImgSectors, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 686 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 687 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 688 | if (formatStatus < 0) { |
| 689 | SLOGE("ASEC fs format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 690 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 691 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 692 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 693 | Loop::destroyByDevice(loopDevice); |
| 694 | unlink(asecFileName); |
| 695 | return -1; |
| 696 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 697 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 698 | if (mkdir(mountPoint, 0000)) { |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 699 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 700 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 701 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 702 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 703 | } |
| 704 | Loop::destroyByDevice(loopDevice); |
| 705 | unlink(asecFileName); |
| 706 | return -1; |
| 707 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 708 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 709 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 710 | int mountStatus; |
| 711 | if (usingExt4) { |
| 712 | mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false); |
| 713 | } else { |
| 714 | mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000, |
| 715 | false); |
| 716 | } |
| 717 | |
| 718 | if (mountStatus) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 719 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 720 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 721 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 722 | } |
| 723 | Loop::destroyByDevice(loopDevice); |
| 724 | unlink(asecFileName); |
| 725 | return -1; |
| 726 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 727 | |
| 728 | if (usingExt4) { |
| 729 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 730 | if (dirfd >= 0) { |
| 731 | if (fchown(dirfd, ownerUid, AID_SYSTEM) |
| 732 | || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) { |
| 733 | SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint); |
| 734 | } |
| 735 | close(dirfd); |
| 736 | } |
| 737 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 738 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 739 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 740 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 741 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 742 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 746 | int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) { |
| 747 | char asecFileName[255]; |
| 748 | char mountPoint[255]; |
| 749 | bool cleanupDm = false; |
| 750 | |
| 751 | if (!isLegalAsecId(id)) { |
| 752 | SLOGE("resizeAsec: Invalid asec id \"%s\"", id); |
| 753 | errno = EINVAL; |
| 754 | return -1; |
| 755 | } |
| 756 | |
| 757 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 758 | SLOGE("Couldn't find ASEC %s", id); |
| 759 | return -1; |
| 760 | } |
| 761 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 762 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 763 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 764 | SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id); |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | if (isMountpointMounted(mountPoint)) { |
| 769 | SLOGE("ASEC %s mounted. Unmount before resizing", id); |
| 770 | errno = EBUSY; |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | struct asec_superblock sb; |
| 775 | int fd; |
| 776 | unsigned int oldNumSec = 0; |
| 777 | |
| 778 | if ((fd = open(asecFileName, O_RDONLY)) < 0) { |
| 779 | SLOGE("Failed to open ASEC file (%s)", strerror(errno)); |
| 780 | return -1; |
| 781 | } |
| 782 | |
| 783 | struct stat info; |
| 784 | if (fstat(fd, &info) < 0) { |
| 785 | SLOGE("Failed to get file size (%s)", strerror(errno)); |
| 786 | close(fd); |
| 787 | return -1; |
| 788 | } |
| 789 | |
| 790 | oldNumSec = info.st_size / 512; |
| 791 | |
| 792 | unsigned numImgSectors; |
| 793 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) |
| 794 | numImgSectors = adjustSectorNumExt4(numSectors); |
| 795 | else |
| 796 | numImgSectors = adjustSectorNumFAT(numSectors); |
| 797 | /* |
| 798 | * add one block for the superblock |
| 799 | */ |
| 800 | SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1); |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 801 | if (oldNumSec == numImgSectors + 1) { |
| 802 | SLOGW("Size unchanged; ignoring resize request"); |
| 803 | return 0; |
| 804 | } else if (oldNumSec > numImgSectors + 1) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 805 | SLOGE("Only growing is currently supported."); |
| 806 | close(fd); |
| 807 | return -1; |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * Try to read superblock. |
| 812 | */ |
| 813 | memset(&sb, 0, sizeof(struct asec_superblock)); |
| 814 | if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) { |
| 815 | SLOGE("lseek failed (%s)", strerror(errno)); |
| 816 | close(fd); |
| 817 | return -1; |
| 818 | } |
| 819 | if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 820 | SLOGE("superblock read failed (%s)", strerror(errno)); |
| 821 | close(fd); |
| 822 | return -1; |
| 823 | } |
| 824 | close(fd); |
| 825 | |
| 826 | if (mDebug) { |
| 827 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
| 828 | } |
| 829 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
| 830 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
| 831 | errno = EMEDIUMTYPE; |
| 832 | return -1; |
| 833 | } |
| 834 | |
| 835 | if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) { |
| 836 | SLOGE("Only ext4 partitions are supported for resize"); |
| 837 | errno = EINVAL; |
| 838 | return -1; |
| 839 | } |
| 840 | |
| 841 | if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) { |
| 842 | SLOGE("Resize of ASEC image file failed. Could not resize %s", id); |
| 843 | return -1; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Drop down a copy of the superblock at the end of the file |
| 848 | */ |
| 849 | if (writeSuperBlock(asecFileName, &sb, numImgSectors)) |
| 850 | goto fail; |
| 851 | |
| 852 | char idHash[33]; |
| 853 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 854 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 855 | goto fail; |
| 856 | } |
| 857 | |
| 858 | char loopDevice[255]; |
| 859 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 860 | goto fail; |
| 861 | |
| 862 | char dmDevice[255]; |
| 863 | |
| 864 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) { |
| 865 | Loop::destroyByDevice(loopDevice); |
| 866 | goto fail; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Wait for the device mapper node to be created. |
| 871 | */ |
| 872 | waitForDevMapper(dmDevice); |
| 873 | |
| 874 | if (Ext4::resize(dmDevice, numImgSectors)) { |
| 875 | SLOGE("Unable to resize %s (%s)", id, strerror(errno)); |
| 876 | if (cleanupDm) { |
| 877 | Devmapper::destroy(idHash); |
| 878 | } |
| 879 | Loop::destroyByDevice(loopDevice); |
| 880 | goto fail; |
| 881 | } |
| 882 | |
| 883 | return 0; |
| 884 | fail: |
| 885 | Loop::resizeImageFile(asecFileName, oldNumSec); |
| 886 | return -1; |
| 887 | } |
| 888 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 889 | int VolumeManager::finalizeAsec(const char *id) { |
| 890 | char asecFileName[255]; |
| 891 | char loopDevice[255]; |
| 892 | char mountPoint[255]; |
| 893 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 894 | if (!isLegalAsecId(id)) { |
| 895 | SLOGE("finalizeAsec: Invalid asec id \"%s\"", id); |
| 896 | errno = EINVAL; |
| 897 | return -1; |
| 898 | } |
| 899 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 900 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 901 | SLOGE("Couldn't find ASEC %s", id); |
| 902 | return -1; |
| 903 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 904 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 905 | char idHash[33]; |
| 906 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 907 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 908 | return -1; |
| 909 | } |
| 910 | |
| 911 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 912 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 913 | return -1; |
| 914 | } |
| 915 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 916 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 917 | struct asec_superblock sb; |
| 918 | |
| 919 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 920 | return -1; |
| 921 | } |
| 922 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 923 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 924 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 925 | SLOGE("ASEC finalize failed: couldn't construct mountPoint"); |
| 926 | return -1; |
| 927 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 928 | |
| 929 | int result = 0; |
| 930 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 931 | result = Ext4::doMount(loopDevice, mountPoint, true, true, true); |
| 932 | } else { |
| 933 | result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false); |
| 934 | } |
| 935 | |
| 936 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 937 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 938 | return -1; |
| 939 | } |
| 940 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 941 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 942 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 943 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 944 | return 0; |
| 945 | } |
| 946 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 947 | int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) { |
| 948 | char asecFileName[255]; |
| 949 | char loopDevice[255]; |
| 950 | char mountPoint[255]; |
| 951 | |
| 952 | if (gid < AID_APP) { |
| 953 | SLOGE("Group ID is not in application range"); |
| 954 | return -1; |
| 955 | } |
| 956 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 957 | if (!isLegalAsecId(id)) { |
| 958 | SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id); |
| 959 | errno = EINVAL; |
| 960 | return -1; |
| 961 | } |
| 962 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 963 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 964 | SLOGE("Couldn't find ASEC %s", id); |
| 965 | return -1; |
| 966 | } |
| 967 | |
| 968 | char idHash[33]; |
| 969 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 970 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 971 | return -1; |
| 972 | } |
| 973 | |
| 974 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 975 | SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno)); |
| 976 | return -1; |
| 977 | } |
| 978 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 979 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 980 | struct asec_superblock sb; |
| 981 | |
| 982 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 983 | return -1; |
| 984 | } |
| 985 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 986 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 987 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 988 | SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id); |
| 989 | return -1; |
| 990 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 991 | |
| 992 | int result = 0; |
| 993 | if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) { |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | int ret = Ext4::doMount(loopDevice, mountPoint, |
| 998 | false /* read-only */, |
| 999 | true /* remount */, |
| 1000 | false /* executable */); |
| 1001 | if (ret) { |
| 1002 | SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno)); |
| 1003 | return -1; |
| 1004 | } |
| 1005 | |
| 1006 | char *paths[] = { mountPoint, NULL }; |
| 1007 | |
| 1008 | FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL); |
| 1009 | if (fts) { |
| 1010 | // Traverse the entire hierarchy and chown to system UID. |
| 1011 | for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { |
| 1012 | // We don't care about the lost+found directory. |
| 1013 | if (!strcmp(ftsent->fts_name, "lost+found")) { |
| 1014 | continue; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * There can only be one file marked as private right now. |
| 1019 | * This should be more robust, but it satisfies the requirements |
| 1020 | * we have for right now. |
| 1021 | */ |
| 1022 | const bool privateFile = !strcmp(ftsent->fts_name, filename); |
| 1023 | |
| 1024 | int fd = open(ftsent->fts_accpath, O_NOFOLLOW); |
| 1025 | if (fd < 0) { |
| 1026 | SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno)); |
| 1027 | result = -1; |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM); |
| 1032 | |
| 1033 | if (ftsent->fts_info & FTS_D) { |
Kenny Root | 1a673c8 | 2012-05-10 16:45:29 -0700 | [diff] [blame] | 1034 | result |= fchmod(fd, 0755); |
Kenny Root | 348c8ab | 2012-05-10 15:39:53 -0700 | [diff] [blame] | 1035 | } else if (ftsent->fts_info & FTS_F) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1036 | result |= fchmod(fd, privateFile ? 0640 : 0644); |
| 1037 | } |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1038 | |
Stephen Smalley | 5093e61 | 2014-02-12 09:43:08 -0500 | [diff] [blame] | 1039 | if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) { |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 1040 | SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno)); |
| 1041 | result |= -1; |
| 1042 | } |
| 1043 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1044 | close(fd); |
| 1045 | } |
| 1046 | fts_close(fts); |
| 1047 | |
| 1048 | // Finally make the directory readable by everyone. |
| 1049 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 1050 | if (dirfd < 0 || fchmod(dirfd, 0755)) { |
| 1051 | SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno)); |
| 1052 | result |= -1; |
| 1053 | } |
| 1054 | close(dirfd); |
| 1055 | } else { |
| 1056 | result |= -1; |
| 1057 | } |
| 1058 | |
| 1059 | result |= Ext4::doMount(loopDevice, mountPoint, |
| 1060 | true /* read-only */, |
| 1061 | true /* remount */, |
| 1062 | true /* execute */); |
| 1063 | |
| 1064 | if (result) { |
| 1065 | SLOGE("ASEC fix permissions failed (%s)", strerror(errno)); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
| 1069 | if (mDebug) { |
| 1070 | SLOGD("ASEC %s permissions fixed", id); |
| 1071 | } |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1075 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1076 | char asecFilename1[255]; |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1077 | char *asecFilename2; |
| 1078 | char mountPoint[255]; |
| 1079 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1080 | const char *dir; |
| 1081 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1082 | if (!isLegalAsecId(id1)) { |
| 1083 | SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1); |
| 1084 | errno = EINVAL; |
| 1085 | return -1; |
| 1086 | } |
| 1087 | |
| 1088 | if (!isLegalAsecId(id2)) { |
| 1089 | SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2); |
| 1090 | errno = EINVAL; |
| 1091 | return -1; |
| 1092 | } |
| 1093 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1094 | if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) { |
| 1095 | SLOGE("Couldn't find ASEC %s", id1); |
| 1096 | return -1; |
| 1097 | } |
| 1098 | |
| 1099 | asprintf(&asecFilename2, "%s/%s.asec", dir, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1100 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1101 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1102 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1103 | SLOGE("Rename failed: couldn't construct mountpoint"); |
| 1104 | goto out_err; |
| 1105 | } |
| 1106 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1107 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1108 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1109 | errno = EBUSY; |
| 1110 | goto out_err; |
| 1111 | } |
| 1112 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1113 | written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1114 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1115 | SLOGE("Rename failed: couldn't construct mountpoint2"); |
| 1116 | goto out_err; |
| 1117 | } |
| 1118 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1119 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1120 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 1121 | errno = EBUSY; |
| 1122 | goto out_err; |
| 1123 | } |
| 1124 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1125 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1126 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1127 | errno = EADDRINUSE; |
| 1128 | goto out_err; |
| 1129 | } |
| 1130 | |
| 1131 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1132 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1133 | goto out_err; |
| 1134 | } |
| 1135 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1136 | free(asecFilename2); |
| 1137 | return 0; |
| 1138 | |
| 1139 | out_err: |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 1140 | free(asecFilename2); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1144 | #define UNMOUNT_RETRIES 5 |
| 1145 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1146 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1147 | char asecFileName[255]; |
| 1148 | char mountPoint[255]; |
| 1149 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1150 | if (!isLegalAsecId(id)) { |
| 1151 | SLOGE("unmountAsec: Invalid asec id \"%s\"", id); |
| 1152 | errno = EINVAL; |
| 1153 | return -1; |
| 1154 | } |
| 1155 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1156 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1157 | SLOGE("Couldn't find ASEC %s", id); |
| 1158 | return -1; |
| 1159 | } |
| 1160 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1161 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1162 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1163 | SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id); |
| 1164 | return -1; |
| 1165 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1166 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1167 | char idHash[33]; |
| 1168 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1169 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1170 | return -1; |
| 1171 | } |
| 1172 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1173 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 1174 | } |
| 1175 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1176 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1177 | char mountPoint[255]; |
| 1178 | |
| 1179 | char idHash[33]; |
| 1180 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 1181 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 1182 | return -1; |
| 1183 | } |
| 1184 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1185 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1186 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1187 | SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName); |
| 1188 | return -1; |
| 1189 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1190 | |
| 1191 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 1192 | } |
| 1193 | |
| 1194 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 1195 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1196 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1197 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 1198 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1199 | return -1; |
| 1200 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 1201 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1202 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1203 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1204 | rc = umount(mountPoint); |
| 1205 | if (!rc) { |
| 1206 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1207 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1208 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1209 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1210 | rc = 0; |
| 1211 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1212 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1213 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1214 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1215 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1216 | int signal = 0; // default is to just complain |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1217 | |
| 1218 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1219 | if (i > (UNMOUNT_RETRIES - 2)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1220 | signal = SIGKILL; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1221 | else if (i > (UNMOUNT_RETRIES - 3)) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1222 | signal = SIGTERM; |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1223 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1224 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1225 | Process::killProcessesWithOpenFiles(mountPoint, signal); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1226 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1230 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1231 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1232 | return -1; |
| 1233 | } |
| 1234 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1235 | int retries = 10; |
| 1236 | |
| 1237 | while(retries--) { |
| 1238 | if (!rmdir(mountPoint)) { |
| 1239 | break; |
| 1240 | } |
| 1241 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1242 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1243 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1247 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 1248 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1249 | |
Paul Lawrence | 60dec16 | 2014-09-02 10:52:15 -0700 | [diff] [blame] | 1250 | for (i=1; i <= UNMOUNT_RETRIES; i++) { |
| 1251 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
| 1252 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
| 1253 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
| 1254 | continue; |
| 1255 | } else { |
| 1256 | break; |
| 1257 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1261 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1262 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1263 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1264 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1265 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1266 | |
| 1267 | AsecIdCollection::iterator it; |
| 1268 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1269 | ContainerData* cd = *it; |
| 1270 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1271 | free(*it); |
| 1272 | mActiveContainers->erase(it); |
| 1273 | break; |
| 1274 | } |
| 1275 | } |
| 1276 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1277 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1278 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1279 | return 0; |
| 1280 | } |
| 1281 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1282 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1283 | char asecFileName[255]; |
| 1284 | char mountPoint[255]; |
| 1285 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1286 | if (!isLegalAsecId(id)) { |
| 1287 | SLOGE("destroyAsec: Invalid asec id \"%s\"", id); |
| 1288 | errno = EINVAL; |
| 1289 | return -1; |
| 1290 | } |
| 1291 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1292 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1293 | SLOGE("Couldn't find ASEC %s", id); |
| 1294 | return -1; |
| 1295 | } |
| 1296 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1297 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1298 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1299 | SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id); |
| 1300 | return -1; |
| 1301 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1302 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1303 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1304 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1305 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1306 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1307 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1308 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1309 | return -1; |
| 1310 | } |
| 1311 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1312 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1313 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1314 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1315 | return -1; |
| 1316 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1317 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1318 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1319 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1320 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1321 | return 0; |
| 1322 | } |
| 1323 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1324 | /* |
| 1325 | * Legal ASEC ids consist of alphanumeric characters, '-', |
| 1326 | * '_', or '.'. ".." is not allowed. The first or last character |
| 1327 | * of the ASEC id cannot be '.' (dot). |
| 1328 | */ |
| 1329 | bool VolumeManager::isLegalAsecId(const char *id) const { |
| 1330 | size_t i; |
| 1331 | size_t len = strlen(id); |
| 1332 | |
| 1333 | if (len == 0) { |
| 1334 | return false; |
| 1335 | } |
| 1336 | if ((id[0] == '.') || (id[len - 1] == '.')) { |
| 1337 | return false; |
| 1338 | } |
| 1339 | |
| 1340 | for (i = 0; i < len; i++) { |
| 1341 | if (id[i] == '.') { |
| 1342 | // i=0 is guaranteed never to have a dot. See above. |
| 1343 | if (id[i-1] == '.') return false; |
| 1344 | continue; |
| 1345 | } |
| 1346 | if (id[i] == '_' || id[i] == '-') continue; |
| 1347 | if (id[i] >= 'a' && id[i] <= 'z') continue; |
| 1348 | if (id[i] >= 'A' && id[i] <= 'Z') continue; |
| 1349 | if (id[i] >= '0' && id[i] <= '9') continue; |
| 1350 | return false; |
| 1351 | } |
| 1352 | |
| 1353 | return true; |
| 1354 | } |
| 1355 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1356 | bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const { |
| 1357 | int dirfd = open(dir, O_DIRECTORY); |
| 1358 | if (dirfd < 0) { |
| 1359 | SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno)); |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1360 | return false; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1363 | struct stat sb; |
| 1364 | bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0) |
| 1365 | && S_ISREG(sb.st_mode); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1366 | |
| 1367 | close(dirfd); |
| 1368 | |
| 1369 | return ret; |
| 1370 | } |
| 1371 | |
| 1372 | int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen, |
| 1373 | const char **directory) const { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1374 | char *asecName; |
| 1375 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1376 | if (!isLegalAsecId(id)) { |
| 1377 | SLOGE("findAsec: Invalid asec id \"%s\"", id); |
| 1378 | errno = EINVAL; |
| 1379 | return -1; |
| 1380 | } |
| 1381 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1382 | if (asprintf(&asecName, "%s.asec", id) < 0) { |
| 1383 | SLOGE("Couldn't allocate string to write ASEC name"); |
| 1384 | return -1; |
| 1385 | } |
| 1386 | |
| 1387 | const char *dir; |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1388 | if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) { |
| 1389 | dir = VolumeManager::SEC_ASECDIR_INT; |
| 1390 | } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) { |
| 1391 | dir = VolumeManager::SEC_ASECDIR_EXT; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1392 | } else { |
| 1393 | free(asecName); |
| 1394 | return -1; |
| 1395 | } |
| 1396 | |
| 1397 | if (directory != NULL) { |
| 1398 | *directory = dir; |
| 1399 | } |
| 1400 | |
| 1401 | if (asecPath != NULL) { |
| 1402 | int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1403 | if ((written < 0) || (size_t(written) >= asecPathLen)) { |
| 1404 | SLOGE("findAsec failed for %s: couldn't construct ASEC path", id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1405 | free(asecName); |
| 1406 | return -1; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | free(asecName); |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1414 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1415 | char asecFileName[255]; |
| 1416 | char mountPoint[255]; |
| 1417 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1418 | if (!isLegalAsecId(id)) { |
| 1419 | SLOGE("mountAsec: Invalid asec id \"%s\"", id); |
| 1420 | errno = EINVAL; |
| 1421 | return -1; |
| 1422 | } |
| 1423 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1424 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1425 | SLOGE("Couldn't find ASEC %s", id); |
| 1426 | return -1; |
| 1427 | } |
| 1428 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1429 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1430 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1431 | SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1432 | return -1; |
| 1433 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1434 | |
| 1435 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1436 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1437 | errno = EBUSY; |
| 1438 | return -1; |
| 1439 | } |
| 1440 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1441 | char idHash[33]; |
| 1442 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1443 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1444 | return -1; |
| 1445 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 1446 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1447 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1448 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 1449 | return -1; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1450 | |
| 1451 | char dmDevice[255]; |
| 1452 | bool cleanupDm = false; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1453 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1454 | unsigned long nr_sec = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1455 | struct asec_superblock sb; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1456 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1457 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1458 | return -1; |
| 1459 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1460 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1461 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1462 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1463 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1464 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1465 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1466 | Loop::destroyByDevice(loopDevice); |
| 1467 | errno = EMEDIUMTYPE; |
| 1468 | return -1; |
| 1469 | } |
| 1470 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 1471 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1472 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) { |
| 1473 | Loop::destroyByDevice(loopDevice); |
| 1474 | return -1; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1477 | if (mkdir(mountPoint, 0000)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1478 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1479 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1480 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1481 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1482 | } |
| 1483 | Loop::destroyByDevice(loopDevice); |
| 1484 | return -1; |
| 1485 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1488 | /* |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1489 | * Wait for the device mapper node to be created. |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1490 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1491 | waitForDevMapper(dmDevice); |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1492 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1493 | int result; |
| 1494 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1495 | result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1496 | } else { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1497 | result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1501 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1502 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1503 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1504 | } |
| 1505 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1506 | return -1; |
| 1507 | } |
| 1508 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1509 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1510 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1511 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1512 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1513 | return 0; |
| 1514 | } |
| 1515 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1516 | /** |
| 1517 | * Mounts an image file <code>img</code>. |
| 1518 | */ |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1519 | int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1520 | char mountPoint[255]; |
| 1521 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1522 | char idHash[33]; |
| 1523 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 1524 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 1525 | return -1; |
| 1526 | } |
| 1527 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1528 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1529 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1530 | SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1531 | return -1; |
| 1532 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1533 | |
| 1534 | if (isMountpointMounted(mountPoint)) { |
| 1535 | SLOGE("Image %s already mounted", img); |
| 1536 | errno = EBUSY; |
| 1537 | return -1; |
| 1538 | } |
| 1539 | |
| 1540 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1541 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug)) |
| 1542 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1543 | |
| 1544 | char dmDevice[255]; |
| 1545 | bool cleanupDm = false; |
| 1546 | int fd; |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1547 | unsigned long nr_sec = 0; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1548 | |
| 1549 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 1550 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 1551 | Loop::destroyByDevice(loopDevice); |
| 1552 | return -1; |
| 1553 | } |
| 1554 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1555 | get_blkdev_size(fd, &nr_sec); |
| 1556 | if (nr_sec == 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1557 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 1558 | Loop::destroyByDevice(loopDevice); |
| 1559 | close(fd); |
| 1560 | return -1; |
| 1561 | } |
| 1562 | |
| 1563 | close(fd); |
| 1564 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1565 | if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1566 | Loop::destroyByDevice(loopDevice); |
| 1567 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | if (mkdir(mountPoint, 0755)) { |
| 1571 | if (errno != EEXIST) { |
| 1572 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 1573 | if (cleanupDm) { |
| 1574 | Devmapper::destroy(idHash); |
| 1575 | } |
| 1576 | Loop::destroyByDevice(loopDevice); |
| 1577 | return -1; |
| 1578 | } |
| 1579 | } |
| 1580 | |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1581 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1582 | 0227, false)) { |
| 1583 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 1584 | if (cleanupDm) { |
| 1585 | Devmapper::destroy(idHash); |
| 1586 | } |
| 1587 | Loop::destroyByDevice(loopDevice); |
| 1588 | return -1; |
| 1589 | } |
| 1590 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1591 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1592 | if (mDebug) { |
| 1593 | SLOGD("Image %s mounted", img); |
| 1594 | } |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1598 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1599 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1600 | if (fp == NULL) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1601 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 1602 | return -1; |
| 1603 | } |
| 1604 | |
| 1605 | // Create a string to compare against that has a trailing slash |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1606 | int loopDirLen = strlen(VolumeManager::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1607 | char loopDir[loopDirLen + 2]; |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 1608 | strcpy(loopDir, VolumeManager::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1609 | loopDir[loopDirLen++] = '/'; |
| 1610 | loopDir[loopDirLen] = '\0'; |
| 1611 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1612 | mntent* mentry; |
| 1613 | while ((mentry = getmntent(fp)) != NULL) { |
| 1614 | if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) { |
| 1615 | int fd = open(mentry->mnt_fsname, O_RDONLY); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1616 | if (fd >= 0) { |
| 1617 | struct loop_info64 li; |
| 1618 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 1619 | cli->sendMsg(ResponseCode::AsecListResult, |
| 1620 | (const char*) li.lo_file_name, false); |
| 1621 | } |
| 1622 | close(fd); |
| 1623 | } |
| 1624 | } |
| 1625 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1626 | endmntent(fp); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1627 | return 0; |
| 1628 | } |
| 1629 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1630 | extern "C" int vold_unmountAll(void) { |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1631 | VolumeManager *vm = VolumeManager::Instance(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1632 | return vm->unmountAll(); |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1635 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1636 | { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1637 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1638 | if (fp == NULL) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1639 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1640 | return false; |
| 1641 | } |
| 1642 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1643 | bool found_mp = false; |
| 1644 | mntent* mentry; |
| 1645 | while ((mentry = getmntent(fp)) != NULL) { |
| 1646 | if (strcmp(mentry->mnt_dir, mp) == 0) { |
| 1647 | found_mp = true; |
| 1648 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1649 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1650 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1651 | endmntent(fp); |
| 1652 | return found_mp; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1655 | int VolumeManager::mkdirs(char* path) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1656 | // Only offer to create directories for paths managed by vold |
| 1657 | if (strncmp(path, "/storage/", 9) == 0) { |
| 1658 | // fs_mkdirs() does symlink checking and relative path enforcement |
| 1659 | return fs_mkdirs(path, 0700); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1660 | } else { |
Cylen Yao | 27cfee3 | 2014-05-02 19:23:42 +0800 | [diff] [blame] | 1661 | SLOGE("Failed to find mounted volume for %s", path); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1662 | return -EINVAL; |
| 1663 | } |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1664 | } |