Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 17 | #include "PrivateVolume.h" |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 18 | #include "EmulatedVolume.h" |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 19 | #include "Utils.h" |
| 20 | #include "VolumeManager.h" |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 21 | #include "cryptfs.h" |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 22 | #include "fs/Ext4.h" |
| 23 | #include "fs/F2fs.h" |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 27 | #include <cutils/fs.h> |
| 28 | #include <private/android_filesystem_config.h> |
| 29 | |
| 30 | #include <fcntl.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <sys/mount.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 33 | #include <sys/param.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 34 | #include <sys/stat.h> |
| 35 | #include <sys/sysmacros.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/wait.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 38 | |
| 39 | using android::base::StringPrintf; |
| 40 | |
| 41 | namespace android { |
| 42 | namespace vold { |
| 43 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 44 | static const unsigned int kMajorBlockMmc = 179; |
| 45 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 46 | PrivateVolume::PrivateVolume(dev_t device, const std::string& keyRaw) |
| 47 | : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 48 | setId(StringPrintf("private:%u,%u", major(device), minor(device))); |
| 49 | mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str()); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 52 | PrivateVolume::~PrivateVolume() {} |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 53 | |
| 54 | status_t PrivateVolume::readMetadata() { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 55 | status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 56 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 57 | auto listener = getListener(); |
| 58 | if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 59 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 60 | return res; |
| 61 | } |
| 62 | |
| 63 | status_t PrivateVolume::doCreate() { |
| 64 | if (CreateDeviceNode(mRawDevPath, mRawDevice)) { |
| 65 | return -EIO; |
| 66 | } |
Greg Kaiser | 57f9af6 | 2018-02-16 13:13:58 -0800 | [diff] [blame] | 67 | if (mKeyRaw.size() != cryptfs_get_keysize()) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 68 | PLOG(ERROR) << getId() << " Raw keysize " << mKeyRaw.size() |
| 69 | << " does not match crypt keysize " << cryptfs_get_keysize(); |
| 70 | return -EIO; |
Greg Kaiser | 57f9af6 | 2018-02-16 13:13:58 -0800 | [diff] [blame] | 71 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 72 | |
| 73 | // Recover from stale vold by tearing down any old mappings |
| 74 | cryptfs_revert_ext_volume(getId().c_str()); |
| 75 | |
| 76 | // TODO: figure out better SELinux labels for private volumes |
| 77 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 78 | unsigned char* key = (unsigned char*)mKeyRaw.data(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 79 | char crypto_blkdev[MAXPATHLEN]; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 80 | int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(), key, crypto_blkdev); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 81 | mDmDevPath = crypto_blkdev; |
| 82 | if (res != 0) { |
| 83 | PLOG(ERROR) << getId() << " failed to setup cryptfs"; |
| 84 | return -EIO; |
| 85 | } |
| 86 | |
| 87 | return OK; |
| 88 | } |
| 89 | |
| 90 | status_t PrivateVolume::doDestroy() { |
| 91 | if (cryptfs_revert_ext_volume(getId().c_str())) { |
| 92 | LOG(ERROR) << getId() << " failed to revert cryptfs"; |
| 93 | } |
| 94 | return DestroyDeviceNode(mRawDevPath); |
| 95 | } |
| 96 | |
| 97 | status_t PrivateVolume::doMount() { |
| 98 | if (readMetadata()) { |
| 99 | LOG(ERROR) << getId() << " failed to read metadata"; |
| 100 | return -EIO; |
| 101 | } |
| 102 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 103 | mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str()); |
| 104 | setPath(mPath); |
| 105 | |
| 106 | if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) { |
| 107 | PLOG(ERROR) << getId() << " failed to create mount point " << mPath; |
| 108 | return -EIO; |
| 109 | } |
| 110 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 111 | if (mFsType == "ext4") { |
| 112 | int res = ext4::Check(mDmDevPath, mPath); |
| 113 | if (res == 0 || res == 1) { |
| 114 | LOG(DEBUG) << getId() << " passed filesystem check"; |
| 115 | } else { |
| 116 | PLOG(ERROR) << getId() << " failed filesystem check"; |
| 117 | return -EIO; |
| 118 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 119 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 120 | if (ext4::Mount(mDmDevPath, mPath, false, false, true)) { |
| 121 | PLOG(ERROR) << getId() << " failed to mount"; |
| 122 | return -EIO; |
| 123 | } |
| 124 | |
| 125 | } else if (mFsType == "f2fs") { |
| 126 | int res = f2fs::Check(mDmDevPath); |
| 127 | if (res == 0) { |
| 128 | LOG(DEBUG) << getId() << " passed filesystem check"; |
| 129 | } else { |
| 130 | PLOG(ERROR) << getId() << " failed filesystem check"; |
| 131 | return -EIO; |
| 132 | } |
| 133 | |
| 134 | if (f2fs::Mount(mDmDevPath, mPath)) { |
| 135 | PLOG(ERROR) << getId() << " failed to mount"; |
| 136 | return -EIO; |
| 137 | } |
| 138 | |
| 139 | } else { |
| 140 | LOG(ERROR) << getId() << " unsupported filesystem " << mFsType; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 141 | return -EIO; |
| 142 | } |
| 143 | |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 144 | RestoreconRecursive(mPath); |
Jeff Sharkey | 3482412 | 2015-06-09 10:59:17 -0700 | [diff] [blame] | 145 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 146 | // Verify that common directories are ready to roll |
| 147 | if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) || |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 148 | PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) || |
| 149 | PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) || |
| 150 | PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) || |
| 151 | PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) || |
| 152 | PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) || |
| 153 | PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) { |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 154 | PLOG(ERROR) << getId() << " failed to prepare"; |
| 155 | return -EIO; |
| 156 | } |
| 157 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame^] | 158 | auto vol_manager = VolumeManager::Instance(); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 159 | std::string mediaPath(mPath + "/media"); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame^] | 160 | |
| 161 | // Create a new emulated volume stacked above us for all added users, they will automatically |
| 162 | // be destroyed during unmount |
| 163 | for (userid_t user : vol_manager->getStartedUsers()) { |
| 164 | auto vol = std::shared_ptr<VolumeBase>( |
| 165 | new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user)); |
| 166 | vol->setMountUserId(user); |
| 167 | addVolume(vol); |
| 168 | vol->create(); |
| 169 | } |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 170 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 171 | return OK; |
| 172 | } |
| 173 | |
| 174 | status_t PrivateVolume::doUnmount() { |
| 175 | ForceUnmount(mPath); |
| 176 | |
| 177 | if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) { |
| 178 | PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath; |
| 179 | } |
| 180 | |
| 181 | return OK; |
| 182 | } |
| 183 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 184 | status_t PrivateVolume::doFormat(const std::string& fsType) { |
| 185 | std::string resolvedFsType = fsType; |
| 186 | if (fsType == "auto") { |
| 187 | // For now, assume that all MMC devices are flash-based SD cards, and |
| 188 | // give everyone else ext4 because sysfs rotational isn't reliable. |
| 189 | if ((major(mRawDevice) == kMajorBlockMmc) && f2fs::IsSupported()) { |
| 190 | resolvedFsType = "f2fs"; |
| 191 | } else { |
| 192 | resolvedFsType = "ext4"; |
| 193 | } |
| 194 | LOG(DEBUG) << "Resolved auto to " << resolvedFsType; |
| 195 | } |
| 196 | |
| 197 | if (resolvedFsType == "ext4") { |
| 198 | // TODO: change reported mountpoint once we have better selinux support |
| 199 | if (ext4::Format(mDmDevPath, 0, "/data")) { |
| 200 | PLOG(ERROR) << getId() << " failed to format"; |
| 201 | return -EIO; |
| 202 | } |
| 203 | } else if (resolvedFsType == "f2fs") { |
| 204 | if (f2fs::Format(mDmDevPath)) { |
| 205 | PLOG(ERROR) << getId() << " failed to format"; |
| 206 | return -EIO; |
| 207 | } |
| 208 | } else { |
| 209 | LOG(ERROR) << getId() << " unsupported filesystem " << fsType; |
| 210 | return -EINVAL; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return OK; |
| 214 | } |
| 215 | |
| 216 | } // namespace vold |
| 217 | } // namespace android |