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