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