blob: 03aa5fa14d757ad7f9842a572761aa38b05a9498 [file] [log] [blame]
Jeff Sharkey9c484982015-03-31 10:35:33 -07001/*
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 Sharkey9c484982015-03-31 10:35:33 -070017#include "PrivateVolume.h"
Jeff Sharkey3161fb32015-04-12 16:03:33 -070018#include "EmulatedVolume.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070019#include "Utils.h"
20#include "VolumeManager.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070021#include "cryptfs.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070022#include "fs/Ext4.h"
23#include "fs/F2fs.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070024
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/logging.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070026#include <android-base/stringprintf.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070027#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 Sharkey9c484982015-03-31 10:35:33 -070033#include <sys/param.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070034#include <sys/stat.h>
35#include <sys/sysmacros.h>
36#include <sys/types.h>
37#include <sys/wait.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070038
39using android::base::StringPrintf;
40
41namespace android {
42namespace vold {
43
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070044static const unsigned int kMajorBlockMmc = 179;
45
Paul Crowley14c8c072018-09-18 13:30:21 -070046PrivateVolume::PrivateVolume(dev_t device, const std::string& keyRaw)
47 : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070048 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
49 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070050}
51
Paul Crowley14c8c072018-09-18 13:30:21 -070052PrivateVolume::~PrivateVolume() {}
Jeff Sharkey9c484982015-03-31 10:35:33 -070053
54status_t PrivateVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060055 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060056
Jeff Sharkey814e9d32017-09-13 11:49:44 -060057 auto listener = getListener();
58 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060059
Jeff Sharkey9c484982015-03-31 10:35:33 -070060 return res;
61}
62
63status_t PrivateVolume::doCreate() {
64 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
65 return -EIO;
66 }
Greg Kaiser57f9af62018-02-16 13:13:58 -080067 if (mKeyRaw.size() != cryptfs_get_keysize()) {
Paul Crowley14c8c072018-09-18 13:30:21 -070068 PLOG(ERROR) << getId() << " Raw keysize " << mKeyRaw.size()
69 << " does not match crypt keysize " << cryptfs_get_keysize();
70 return -EIO;
Greg Kaiser57f9af62018-02-16 13:13:58 -080071 }
Jeff Sharkey9c484982015-03-31 10:35:33 -070072
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 Crowley14c8c072018-09-18 13:30:21 -070078 unsigned char* key = (unsigned char*)mKeyRaw.data();
Paul Crowley81796e92020-02-07 11:27:49 -080079 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(), key, &mDmDevPath);
Jeff Sharkey9c484982015-03-31 10:35:33 -070080 if (res != 0) {
81 PLOG(ERROR) << getId() << " failed to setup cryptfs";
82 return -EIO;
83 }
84
85 return OK;
86}
87
88status_t PrivateVolume::doDestroy() {
89 if (cryptfs_revert_ext_volume(getId().c_str())) {
90 LOG(ERROR) << getId() << " failed to revert cryptfs";
91 }
92 return DestroyDeviceNode(mRawDevPath);
93}
94
95status_t PrivateVolume::doMount() {
96 if (readMetadata()) {
97 LOG(ERROR) << getId() << " failed to read metadata";
98 return -EIO;
99 }
100
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700101 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
102 setPath(mPath);
103
104 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
105 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
106 return -EIO;
107 }
108
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700109 if (mFsType == "ext4") {
110 int res = ext4::Check(mDmDevPath, mPath);
111 if (res == 0 || res == 1) {
112 LOG(DEBUG) << getId() << " passed filesystem check";
113 } else {
114 PLOG(ERROR) << getId() << " failed filesystem check";
115 return -EIO;
116 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700117
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700118 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
119 PLOG(ERROR) << getId() << " failed to mount";
120 return -EIO;
121 }
122
123 } else if (mFsType == "f2fs") {
124 int res = f2fs::Check(mDmDevPath);
125 if (res == 0) {
126 LOG(DEBUG) << getId() << " passed filesystem check";
127 } else {
128 PLOG(ERROR) << getId() << " failed filesystem check";
129 return -EIO;
130 }
131
132 if (f2fs::Mount(mDmDevPath, mPath)) {
133 PLOG(ERROR) << getId() << " failed to mount";
134 return -EIO;
135 }
136
137 } else {
138 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700139 return -EIO;
140 }
141
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600142 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700143
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700144 // Verify that common directories are ready to roll
145 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700146 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
147 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
148 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
149 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
150 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
151 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700152 PLOG(ERROR) << getId() << " failed to prepare";
153 return -EIO;
154 }
155
Zima438b242019-09-25 14:37:38 +0100156 auto vol_manager = VolumeManager::Instance();
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700157 std::string mediaPath(mPath + "/media");
Zima438b242019-09-25 14:37:38 +0100158
159 // Create a new emulated volume stacked above us for all added users, they will automatically
160 // be destroyed during unmount
161 for (userid_t user : vol_manager->getStartedUsers()) {
162 auto vol = std::shared_ptr<VolumeBase>(
163 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user));
164 vol->setMountUserId(user);
165 addVolume(vol);
166 vol->create();
167 }
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700168
Jeff Sharkey9c484982015-03-31 10:35:33 -0700169 return OK;
170}
171
172status_t PrivateVolume::doUnmount() {
173 ForceUnmount(mPath);
174
175 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
176 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
177 }
178
179 return OK;
180}
181
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700182status_t PrivateVolume::doFormat(const std::string& fsType) {
183 std::string resolvedFsType = fsType;
184 if (fsType == "auto") {
185 // For now, assume that all MMC devices are flash-based SD cards, and
186 // give everyone else ext4 because sysfs rotational isn't reliable.
187 if ((major(mRawDevice) == kMajorBlockMmc) && f2fs::IsSupported()) {
188 resolvedFsType = "f2fs";
189 } else {
190 resolvedFsType = "ext4";
191 }
192 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
193 }
194
195 if (resolvedFsType == "ext4") {
196 // TODO: change reported mountpoint once we have better selinux support
197 if (ext4::Format(mDmDevPath, 0, "/data")) {
198 PLOG(ERROR) << getId() << " failed to format";
199 return -EIO;
200 }
201 } else if (resolvedFsType == "f2fs") {
202 if (f2fs::Format(mDmDevPath)) {
203 PLOG(ERROR) << getId() << " failed to format";
204 return -EIO;
205 }
206 } else {
207 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
208 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700209 }
210
211 return OK;
212}
213
214} // namespace vold
215} // namespace android