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