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