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