1 /*
2 * Copyright (C) 2012 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
17 #include "fs_mgr.h"
18
19 #include <ctype.h>
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <libgen.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/ioctl.h>
29 #include <sys/mount.h>
30 #include <sys/stat.h>
31 #include <sys/swap.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <time.h>
35 #include <unistd.h>
36
37 #include <chrono>
38 #include <functional>
39 #include <map>
40 #include <memory>
41 #include <string>
42 #include <thread>
43 #include <utility>
44 #include <vector>
45
46 #include <android-base/chrono_utils.h>
47 #include <android-base/file.h>
48 #include <android-base/properties.h>
49 #include <android-base/stringprintf.h>
50 #include <android-base/strings.h>
51 #include <android-base/unique_fd.h>
52 #include <cutils/android_filesystem_config.h>
53 #include <cutils/android_reboot.h>
54 #include <cutils/partition_utils.h>
55 #include <cutils/properties.h>
56 #include <ext4_utils/ext4.h>
57 #include <ext4_utils/ext4_sb.h>
58 #include <ext4_utils/ext4_utils.h>
59 #include <ext4_utils/wipe.h>
60 #include <fs_avb/fs_avb.h>
61 #include <fs_mgr/file_wait.h>
62 #include <fs_mgr_overlayfs.h>
63 #include <fscrypt/fscrypt.h>
64 #include <libdm/dm.h>
65 #include <libdm/loop_control.h>
66 #include <liblp/metadata_format.h>
67 #include <linux/fs.h>
68 #include <linux/loop.h>
69 #include <linux/magic.h>
70 #include <log/log_properties.h>
71 #include <logwrap/logwrap.h>
72
73 #include "blockdev.h"
74 #include "fs_mgr_priv.h"
75
76 #define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
77 #define KEY_IN_FOOTER "footer"
78
79 #define E2FSCK_BIN "/system/bin/e2fsck"
80 #define F2FS_FSCK_BIN "/system/bin/fsck.f2fs"
81 #define MKSWAP_BIN "/system/bin/mkswap"
82 #define TUNE2FS_BIN "/system/bin/tune2fs"
83 #define RESIZE2FS_BIN "/system/bin/resize2fs"
84
85 #define FSCK_LOG_FILE "/dev/fscklogs/log"
86
87 #define ZRAM_CONF_DEV "/sys/block/zram0/disksize"
88 #define ZRAM_CONF_MCS "/sys/block/zram0/max_comp_streams"
89 #define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev"
90
91 #define SYSFS_EXT4_VERITY "/sys/fs/ext4/features/verity"
92 #define SYSFS_EXT4_CASEFOLD "/sys/fs/ext4/features/casefold"
93
94 // FIXME: this should be in system/extras
95 #define EXT4_FEATURE_COMPAT_STABLE_INODES 0x0800
96
97 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
98
99 using android::base::Basename;
100 using android::base::GetBoolProperty;
101 using android::base::GetUintProperty;
102 using android::base::Realpath;
103 using android::base::SetProperty;
104 using android::base::StartsWith;
105 using android::base::Timer;
106 using android::base::unique_fd;
107 using android::dm::DeviceMapper;
108 using android::dm::DmDeviceState;
109 using android::dm::DmTargetLinear;
110 using android::dm::LoopControl;
111
112 // Realistically, this file should be part of the android::fs_mgr namespace;
113 using namespace android::fs_mgr;
114
115 using namespace std::literals;
116
117 // record fs stat
118 enum FsStatFlags {
119 FS_STAT_IS_EXT4 = 0x0001,
120 FS_STAT_NEW_IMAGE_VERSION = 0x0002,
121 FS_STAT_E2FSCK_F_ALWAYS = 0x0004,
122 FS_STAT_UNCLEAN_SHUTDOWN = 0x0008,
123 FS_STAT_QUOTA_ENABLED = 0x0010,
124 FS_STAT_RO_MOUNT_FAILED = 0x0040,
125 FS_STAT_RO_UNMOUNT_FAILED = 0x0080,
126 FS_STAT_FULL_MOUNT_FAILED = 0x0100,
127 FS_STAT_E2FSCK_FAILED = 0x0200,
128 FS_STAT_E2FSCK_FS_FIXED = 0x0400,
129 FS_STAT_INVALID_MAGIC = 0x0800,
130 FS_STAT_TOGGLE_QUOTAS_FAILED = 0x10000,
131 FS_STAT_SET_RESERVED_BLOCKS_FAILED = 0x20000,
132 FS_STAT_ENABLE_ENCRYPTION_FAILED = 0x40000,
133 FS_STAT_ENABLE_VERITY_FAILED = 0x80000,
134 FS_STAT_ENABLE_CASEFOLD_FAILED = 0x100000,
135 FS_STAT_ENABLE_METADATA_CSUM_FAILED = 0x200000,
136 };
137
log_fs_stat(const std::string & blk_device,int fs_stat)138 static void log_fs_stat(const std::string& blk_device, int fs_stat) {
139 if ((fs_stat & FS_STAT_IS_EXT4) == 0) return; // only log ext4
140 std::string msg =
141 android::base::StringPrintf("\nfs_stat,%s,0x%x\n", blk_device.c_str(), fs_stat);
142 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(FSCK_LOG_FILE, O_WRONLY | O_CLOEXEC |
143 O_APPEND | O_CREAT, 0664)));
144 if (fd == -1 || !android::base::WriteStringToFd(msg, fd)) {
145 LWARNING << __FUNCTION__ << "() cannot log " << msg;
146 }
147 }
148
is_extfs(const std::string & fs_type)149 static bool is_extfs(const std::string& fs_type) {
150 return fs_type == "ext4" || fs_type == "ext3" || fs_type == "ext2";
151 }
152
is_f2fs(const std::string & fs_type)153 static bool is_f2fs(const std::string& fs_type) {
154 return fs_type == "f2fs";
155 }
156
realpath(const std::string & blk_device)157 static std::string realpath(const std::string& blk_device) {
158 std::string real_path;
159 if (!Realpath(blk_device, &real_path)) {
160 real_path = blk_device;
161 }
162 return real_path;
163 }
164
should_force_check(int fs_stat)165 static bool should_force_check(int fs_stat) {
166 return fs_stat &
167 (FS_STAT_E2FSCK_F_ALWAYS | FS_STAT_UNCLEAN_SHUTDOWN | FS_STAT_QUOTA_ENABLED |
168 FS_STAT_RO_MOUNT_FAILED | FS_STAT_RO_UNMOUNT_FAILED | FS_STAT_FULL_MOUNT_FAILED |
169 FS_STAT_E2FSCK_FAILED | FS_STAT_TOGGLE_QUOTAS_FAILED |
170 FS_STAT_SET_RESERVED_BLOCKS_FAILED | FS_STAT_ENABLE_ENCRYPTION_FAILED);
171 }
172
check_fs(const std::string & blk_device,const std::string & fs_type,const std::string & target,int * fs_stat)173 static void check_fs(const std::string& blk_device, const std::string& fs_type,
174 const std::string& target, int* fs_stat) {
175 int status;
176 int ret;
177 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
178 auto tmpmnt_opts = "errors=remount-ro"s;
179 const char* e2fsck_argv[] = {E2FSCK_BIN, "-y", blk_device.c_str()};
180 const char* e2fsck_forced_argv[] = {E2FSCK_BIN, "-f", "-y", blk_device.c_str()};
181
182 if (*fs_stat & FS_STAT_INVALID_MAGIC) { // will fail, so do not try
183 return;
184 }
185
186 Timer t;
187 /* Check for the types of filesystems we know how to check */
188 if (is_extfs(fs_type)) {
189 /*
190 * First try to mount and unmount the filesystem. We do this because
191 * the kernel is more efficient than e2fsck in running the journal and
192 * processing orphaned inodes, and on at least one device with a
193 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
194 * to do what the kernel does in about a second.
195 *
196 * After mounting and unmounting the filesystem, run e2fsck, and if an
197 * error is recorded in the filesystem superblock, e2fsck will do a full
198 * check. Otherwise, it does nothing. If the kernel cannot mount the
199 * filesytsem due to an error, e2fsck is still run to do a full check
200 * fix the filesystem.
201 */
202 if (!(*fs_stat & FS_STAT_FULL_MOUNT_FAILED)) { // already tried if full mount failed
203 errno = 0;
204 if (fs_type == "ext4") {
205 // This option is only valid with ext4
206 tmpmnt_opts += ",nomblk_io_submit";
207 }
208 ret = mount(blk_device.c_str(), target.c_str(), fs_type.c_str(), tmpmnt_flags,
209 tmpmnt_opts.c_str());
210 PINFO << __FUNCTION__ << "(): mount(" << blk_device << "," << target << "," << fs_type
211 << ")=" << ret;
212 if (!ret) {
213 bool umounted = false;
214 int retry_count = 5;
215 while (retry_count-- > 0) {
216 umounted = umount(target.c_str()) == 0;
217 if (umounted) {
218 LINFO << __FUNCTION__ << "(): unmount(" << target << ") succeeded";
219 break;
220 }
221 PERROR << __FUNCTION__ << "(): umount(" << target << ") failed";
222 if (retry_count) sleep(1);
223 }
224 if (!umounted) {
225 // boot may fail but continue and leave it to later stage for now.
226 PERROR << __FUNCTION__ << "(): umount(" << target << ") timed out";
227 *fs_stat |= FS_STAT_RO_UNMOUNT_FAILED;
228 }
229 } else {
230 *fs_stat |= FS_STAT_RO_MOUNT_FAILED;
231 }
232 }
233
234 /*
235 * Some system images do not have e2fsck for licensing reasons
236 * (e.g. recent SDK system images). Detect these and skip the check.
237 */
238 if (access(E2FSCK_BIN, X_OK)) {
239 LINFO << "Not running " << E2FSCK_BIN << " on " << realpath(blk_device)
240 << " (executable not in system image)";
241 } else {
242 LINFO << "Running " << E2FSCK_BIN << " on " << realpath(blk_device);
243 if (should_force_check(*fs_stat)) {
244 ret = logwrap_fork_execvp(ARRAY_SIZE(e2fsck_forced_argv), e2fsck_forced_argv,
245 &status, false, LOG_KLOG | LOG_FILE, false,
246 FSCK_LOG_FILE);
247 } else {
248 ret = logwrap_fork_execvp(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, &status, false,
249 LOG_KLOG | LOG_FILE, false, FSCK_LOG_FILE);
250 }
251
252 if (ret < 0) {
253 /* No need to check for error in fork, we can't really handle it now */
254 LERROR << "Failed trying to run " << E2FSCK_BIN;
255 *fs_stat |= FS_STAT_E2FSCK_FAILED;
256 } else if (status != 0) {
257 LINFO << "e2fsck returned status 0x" << std::hex << status;
258 *fs_stat |= FS_STAT_E2FSCK_FS_FIXED;
259 }
260 }
261 } else if (is_f2fs(fs_type)) {
262 const char* f2fs_fsck_argv[] = {F2FS_FSCK_BIN, "-a", "-c", "10000", "--debug-cache",
263 blk_device.c_str()};
264 const char* f2fs_fsck_forced_argv[] = {
265 F2FS_FSCK_BIN, "-f", "-c", "10000", "--debug-cache", blk_device.c_str()};
266
267 if (should_force_check(*fs_stat)) {
268 LINFO << "Running " << F2FS_FSCK_BIN << " -f -c 10000 --debug-cache "
269 << realpath(blk_device);
270 ret = logwrap_fork_execvp(ARRAY_SIZE(f2fs_fsck_forced_argv), f2fs_fsck_forced_argv,
271 &status, false, LOG_KLOG | LOG_FILE, false, FSCK_LOG_FILE);
272 } else {
273 LINFO << "Running " << F2FS_FSCK_BIN << " -a -c 10000 --debug-cache "
274 << realpath(blk_device);
275 ret = logwrap_fork_execvp(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv, &status, false,
276 LOG_KLOG | LOG_FILE, false, FSCK_LOG_FILE);
277 }
278 if (ret < 0) {
279 /* No need to check for error in fork, we can't really handle it now */
280 LERROR << "Failed trying to run " << F2FS_FSCK_BIN;
281 }
282 }
283 android::base::SetProperty("ro.boottime.init.fsck." + Basename(target),
284 std::to_string(t.duration().count()));
285 return;
286 }
287
ext4_blocks_count(const struct ext4_super_block * es)288 static ext4_fsblk_t ext4_blocks_count(const struct ext4_super_block* es) {
289 return ((ext4_fsblk_t)le32_to_cpu(es->s_blocks_count_hi) << 32) |
290 le32_to_cpu(es->s_blocks_count_lo);
291 }
292
ext4_r_blocks_count(const struct ext4_super_block * es)293 static ext4_fsblk_t ext4_r_blocks_count(const struct ext4_super_block* es) {
294 return ((ext4_fsblk_t)le32_to_cpu(es->s_r_blocks_count_hi) << 32) |
295 le32_to_cpu(es->s_r_blocks_count_lo);
296 }
297
is_ext4_superblock_valid(const struct ext4_super_block * es)298 static bool is_ext4_superblock_valid(const struct ext4_super_block* es) {
299 if (es->s_magic != EXT4_SUPER_MAGIC) return false;
300 if (es->s_rev_level != EXT4_DYNAMIC_REV && es->s_rev_level != EXT4_GOOD_OLD_REV) return false;
301 if (EXT4_INODES_PER_GROUP(es) == 0) return false;
302 return true;
303 }
304
305 // Read the primary superblock from an ext4 filesystem. On failure return
306 // false. If it's not an ext4 filesystem, also set FS_STAT_INVALID_MAGIC.
read_ext4_superblock(const std::string & blk_device,struct ext4_super_block * sb,int * fs_stat)307 static bool read_ext4_superblock(const std::string& blk_device, struct ext4_super_block* sb,
308 int* fs_stat) {
309 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC)));
310
311 if (fd < 0) {
312 PERROR << "Failed to open '" << blk_device << "'";
313 return false;
314 }
315
316 if (TEMP_FAILURE_RETRY(pread(fd, sb, sizeof(*sb), 1024)) != sizeof(*sb)) {
317 PERROR << "Can't read '" << blk_device << "' superblock";
318 return false;
319 }
320
321 if (!is_ext4_superblock_valid(sb)) {
322 LINFO << "Invalid ext4 superblock on '" << blk_device << "'";
323 // not a valid fs, tune2fs, fsck, and mount will all fail.
324 *fs_stat |= FS_STAT_INVALID_MAGIC;
325 return false;
326 }
327 *fs_stat |= FS_STAT_IS_EXT4;
328 LINFO << "superblock s_max_mnt_count:" << sb->s_max_mnt_count << "," << blk_device;
329 if (sb->s_max_mnt_count == 0xffff) { // -1 (int16) in ext2, but uint16 in ext4
330 *fs_stat |= FS_STAT_NEW_IMAGE_VERSION;
331 }
332 return true;
333 }
334
335 // exported silent version of the above that just answer the question is_ext4
fs_mgr_is_ext4(const std::string & blk_device)336 bool fs_mgr_is_ext4(const std::string& blk_device) {
337 android::base::ErrnoRestorer restore;
338 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC)));
339 if (fd < 0) return false;
340 ext4_super_block sb;
341 if (TEMP_FAILURE_RETRY(pread(fd, &sb, sizeof(sb), 1024)) != sizeof(sb)) return false;
342 if (!is_ext4_superblock_valid(&sb)) return false;
343 return true;
344 }
345
346 // Some system images do not have tune2fs for licensing reasons.
347 // Detect these and skip running it.
tune2fs_available(void)348 static bool tune2fs_available(void) {
349 return access(TUNE2FS_BIN, X_OK) == 0;
350 }
351
run_command(const char * argv[],int argc)352 static bool run_command(const char* argv[], int argc) {
353 int ret;
354
355 ret = logwrap_fork_execvp(argc, argv, nullptr, false, LOG_KLOG, false, nullptr);
356 return ret == 0;
357 }
358
359 // Enable/disable quota support on the filesystem if needed.
tune_quota(const std::string & blk_device,const FstabEntry & entry,const struct ext4_super_block * sb,int * fs_stat)360 static void tune_quota(const std::string& blk_device, const FstabEntry& entry,
361 const struct ext4_super_block* sb, int* fs_stat) {
362 bool has_quota = (sb->s_feature_ro_compat & cpu_to_le32(EXT4_FEATURE_RO_COMPAT_QUOTA)) != 0;
363 bool want_quota = entry.fs_mgr_flags.quota;
364 bool want_projid = android::base::GetBoolProperty("external_storage.projid.enabled", false);
365
366 if (has_quota == want_quota) {
367 return;
368 }
369
370 if (!tune2fs_available()) {
371 LERROR << "Unable to " << (want_quota ? "enable" : "disable") << " quotas on " << blk_device
372 << " because " TUNE2FS_BIN " is missing";
373 return;
374 }
375
376 const char* argv[] = {TUNE2FS_BIN, nullptr, nullptr, blk_device.c_str()};
377
378 if (want_quota) {
379 LINFO << "Enabling quotas on " << blk_device;
380 argv[1] = "-Oquota";
381 // Once usr/grp unneeded, make just prjquota to save overhead
382 if (want_projid)
383 argv[2] = "-Qusrquota,grpquota,prjquota";
384 else
385 argv[2] = "-Qusrquota,grpquota";
386 *fs_stat |= FS_STAT_QUOTA_ENABLED;
387 } else {
388 LINFO << "Disabling quotas on " << blk_device;
389 argv[1] = "-O^quota";
390 argv[2] = "-Q^usrquota,^grpquota,^prjquota";
391 }
392
393 if (!run_command(argv, ARRAY_SIZE(argv))) {
394 LERROR << "Failed to run " TUNE2FS_BIN " to " << (want_quota ? "enable" : "disable")
395 << " quotas on " << blk_device;
396 *fs_stat |= FS_STAT_TOGGLE_QUOTAS_FAILED;
397 }
398 }
399
400 // Set the number of reserved filesystem blocks if needed.
tune_reserved_size(const std::string & blk_device,const FstabEntry & entry,const struct ext4_super_block * sb,int * fs_stat)401 static void tune_reserved_size(const std::string& blk_device, const FstabEntry& entry,
402 const struct ext4_super_block* sb, int* fs_stat) {
403 if (entry.reserved_size == 0) {
404 return;
405 }
406
407 // The size to reserve is given in the fstab, but we won't reserve more
408 // than 2% of the filesystem.
409 const uint64_t max_reserved_blocks = ext4_blocks_count(sb) * 0.02;
410 uint64_t reserved_blocks = entry.reserved_size / EXT4_BLOCK_SIZE(sb);
411
412 if (reserved_blocks > max_reserved_blocks) {
413 LWARNING << "Reserved blocks " << reserved_blocks << " is too large; "
414 << "capping to " << max_reserved_blocks;
415 reserved_blocks = max_reserved_blocks;
416 }
417
418 if ((ext4_r_blocks_count(sb) == reserved_blocks) && (sb->s_def_resgid == AID_RESERVED_DISK)) {
419 return;
420 }
421
422 if (!tune2fs_available()) {
423 LERROR << "Unable to set the number of reserved blocks on " << blk_device
424 << " because " TUNE2FS_BIN " is missing";
425 return;
426 }
427
428 LINFO << "Setting reserved block count on " << blk_device << " to " << reserved_blocks;
429
430 auto reserved_blocks_str = std::to_string(reserved_blocks);
431 auto reserved_gid_str = std::to_string(AID_RESERVED_DISK);
432 const char* argv[] = {
433 TUNE2FS_BIN, "-r", reserved_blocks_str.c_str(), "-g", reserved_gid_str.c_str(),
434 blk_device.c_str()};
435 if (!run_command(argv, ARRAY_SIZE(argv))) {
436 LERROR << "Failed to run " TUNE2FS_BIN " to set the number of reserved blocks on "
437 << blk_device;
438 *fs_stat |= FS_STAT_SET_RESERVED_BLOCKS_FAILED;
439 }
440 }
441
442 // Enable file-based encryption if needed.
tune_encrypt(const std::string & blk_device,const FstabEntry & entry,const struct ext4_super_block * sb,int * fs_stat)443 static void tune_encrypt(const std::string& blk_device, const FstabEntry& entry,
444 const struct ext4_super_block* sb, int* fs_stat) {
445 if (!entry.fs_mgr_flags.file_encryption) {
446 return; // Nothing needs done.
447 }
448 std::vector<std::string> features_needed;
449 if ((sb->s_feature_incompat & cpu_to_le32(EXT4_FEATURE_INCOMPAT_ENCRYPT)) == 0) {
450 features_needed.emplace_back("encrypt");
451 }
452 android::fscrypt::EncryptionOptions options;
453 if (!android::fscrypt::ParseOptions(entry.encryption_options, &options)) {
454 LERROR << "Unable to parse encryption options on " << blk_device << ": "
455 << entry.encryption_options;
456 return;
457 }
458 if ((options.flags &
459 (FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) != 0) {
460 // We can only use this policy on ext4 if the "stable_inodes" feature
461 // is set on the filesystem, otherwise shrinking will break encrypted files.
462 if ((sb->s_feature_compat & cpu_to_le32(EXT4_FEATURE_COMPAT_STABLE_INODES)) == 0) {
463 features_needed.emplace_back("stable_inodes");
464 }
465 }
466 if (features_needed.size() == 0) {
467 return;
468 }
469 if (!tune2fs_available()) {
470 LERROR << "Unable to enable ext4 encryption on " << blk_device
471 << " because " TUNE2FS_BIN " is missing";
472 return;
473 }
474
475 auto flags = android::base::Join(features_needed, ',');
476 auto flag_arg = "-O"s + flags;
477 const char* argv[] = {TUNE2FS_BIN, flag_arg.c_str(), blk_device.c_str()};
478
479 LINFO << "Enabling ext4 flags " << flags << " on " << blk_device;
480 if (!run_command(argv, ARRAY_SIZE(argv))) {
481 LERROR << "Failed to run " TUNE2FS_BIN " to enable "
482 << "ext4 flags " << flags << " on " << blk_device;
483 *fs_stat |= FS_STAT_ENABLE_ENCRYPTION_FAILED;
484 }
485 }
486
487 // Enable fs-verity if needed.
tune_verity(const std::string & blk_device,const FstabEntry & entry,const struct ext4_super_block * sb,int * fs_stat)488 static void tune_verity(const std::string& blk_device, const FstabEntry& entry,
489 const struct ext4_super_block* sb, int* fs_stat) {
490 bool has_verity = (sb->s_feature_ro_compat & cpu_to_le32(EXT4_FEATURE_RO_COMPAT_VERITY)) != 0;
491 bool want_verity = entry.fs_mgr_flags.fs_verity;
492
493 if (has_verity || !want_verity) {
494 return;
495 }
496
497 std::string verity_support;
498 if (!android::base::ReadFileToString(SYSFS_EXT4_VERITY, &verity_support)) {
499 LERROR << "Failed to open " << SYSFS_EXT4_VERITY;
500 return;
501 }
502
503 if (!(android::base::Trim(verity_support) == "supported")) {
504 LERROR << "Current ext4 verity not supported by kernel";
505 return;
506 }
507
508 if (!tune2fs_available()) {
509 LERROR << "Unable to enable ext4 verity on " << blk_device
510 << " because " TUNE2FS_BIN " is missing";
511 return;
512 }
513
514 LINFO << "Enabling ext4 verity on " << blk_device;
515
516 const char* argv[] = {TUNE2FS_BIN, "-O", "verity", blk_device.c_str()};
517 if (!run_command(argv, ARRAY_SIZE(argv))) {
518 LERROR << "Failed to run " TUNE2FS_BIN " to enable "
519 << "ext4 verity on " << blk_device;
520 *fs_stat |= FS_STAT_ENABLE_VERITY_FAILED;
521 }
522 }
523
524 // Enable casefold if needed.
tune_casefold(const std::string & blk_device,const FstabEntry & entry,const struct ext4_super_block * sb,int * fs_stat)525 static void tune_casefold(const std::string& blk_device, const FstabEntry& entry,
526 const struct ext4_super_block* sb, int* fs_stat) {
527 bool has_casefold = (sb->s_feature_incompat & cpu_to_le32(EXT4_FEATURE_INCOMPAT_CASEFOLD)) != 0;
528 bool wants_casefold =
529 android::base::GetBoolProperty("external_storage.casefold.enabled", false);
530
531 if (entry.mount_point != "/data" || !wants_casefold || has_casefold) return;
532
533 std::string casefold_support;
534 if (!android::base::ReadFileToString(SYSFS_EXT4_CASEFOLD, &casefold_support)) {
535 LERROR << "Failed to open " << SYSFS_EXT4_CASEFOLD;
536 return;
537 }
538
539 if (!(android::base::Trim(casefold_support) == "supported")) {
540 LERROR << "Current ext4 casefolding not supported by kernel";
541 return;
542 }
543
544 if (!tune2fs_available()) {
545 LERROR << "Unable to enable ext4 casefold on " << blk_device
546 << " because " TUNE2FS_BIN " is missing";
547 return;
548 }
549
550 LINFO << "Enabling ext4 casefold on " << blk_device;
551
552 const char* argv[] = {TUNE2FS_BIN, "-O", "casefold", "-E", "encoding=utf8", blk_device.c_str()};
553 if (!run_command(argv, ARRAY_SIZE(argv))) {
554 LERROR << "Failed to run " TUNE2FS_BIN " to enable "
555 << "ext4 casefold on " << blk_device;
556 *fs_stat |= FS_STAT_ENABLE_CASEFOLD_FAILED;
557 }
558 }
559
resize2fs_available(void)560 static bool resize2fs_available(void) {
561 return access(RESIZE2FS_BIN, X_OK) == 0;
562 }
563
564 // Enable metadata_csum
tune_metadata_csum(const std::string & blk_device,const FstabEntry & entry,const struct ext4_super_block * sb,int * fs_stat)565 static void tune_metadata_csum(const std::string& blk_device, const FstabEntry& entry,
566 const struct ext4_super_block* sb, int* fs_stat) {
567 bool has_meta_csum =
568 (sb->s_feature_ro_compat & cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) != 0;
569 bool want_meta_csum = entry.fs_mgr_flags.ext_meta_csum;
570
571 if (has_meta_csum || !want_meta_csum) return;
572
573 if (!tune2fs_available()) {
574 LERROR << "Unable to enable metadata_csum on " << blk_device
575 << " because " TUNE2FS_BIN " is missing";
576 return;
577 }
578 if (!resize2fs_available()) {
579 LERROR << "Unable to enable metadata_csum on " << blk_device
580 << " because " RESIZE2FS_BIN " is missing";
581 return;
582 }
583
584 LINFO << "Enabling ext4 metadata_csum on " << blk_device;
585
586 // Must give `-T now` to prevent last_fsck_time from growing too large,
587 // otherwise, tune2fs won't enable metadata_csum.
588 const char* tune2fs_args[] = {TUNE2FS_BIN, "-O", "metadata_csum,64bit,extent",
589 "-T", "now", blk_device.c_str()};
590 const char* resize2fs_args[] = {RESIZE2FS_BIN, "-b", blk_device.c_str()};
591
592 if (!run_command(tune2fs_args, ARRAY_SIZE(tune2fs_args))) {
593 LERROR << "Failed to run " TUNE2FS_BIN " to enable "
594 << "ext4 metadata_csum on " << blk_device;
595 *fs_stat |= FS_STAT_ENABLE_METADATA_CSUM_FAILED;
596 } else if (!run_command(resize2fs_args, ARRAY_SIZE(resize2fs_args))) {
597 LERROR << "Failed to run " RESIZE2FS_BIN " to enable "
598 << "ext4 metadata_csum on " << blk_device;
599 *fs_stat |= FS_STAT_ENABLE_METADATA_CSUM_FAILED;
600 }
601 }
602
603 // Read the primary superblock from an f2fs filesystem. On failure return
604 // false. If it's not an f2fs filesystem, also set FS_STAT_INVALID_MAGIC.
605 #define F2FS_BLKSIZE 4096
606 #define F2FS_SUPER_OFFSET 1024
read_f2fs_superblock(const std::string & blk_device,int * fs_stat)607 static bool read_f2fs_superblock(const std::string& blk_device, int* fs_stat) {
608 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC)));
609 __le32 sb1, sb2;
610
611 if (fd < 0) {
612 PERROR << "Failed to open '" << blk_device << "'";
613 return false;
614 }
615
616 if (TEMP_FAILURE_RETRY(pread(fd, &sb1, sizeof(sb1), F2FS_SUPER_OFFSET)) != sizeof(sb1)) {
617 PERROR << "Can't read '" << blk_device << "' superblock1";
618 return false;
619 }
620 if (TEMP_FAILURE_RETRY(pread(fd, &sb2, sizeof(sb2), F2FS_BLKSIZE + F2FS_SUPER_OFFSET)) !=
621 sizeof(sb2)) {
622 PERROR << "Can't read '" << blk_device << "' superblock2";
623 return false;
624 }
625
626 if (sb1 != cpu_to_le32(F2FS_SUPER_MAGIC) && sb2 != cpu_to_le32(F2FS_SUPER_MAGIC)) {
627 LINFO << "Invalid f2fs superblock on '" << blk_device << "'";
628 *fs_stat |= FS_STAT_INVALID_MAGIC;
629 return false;
630 }
631 return true;
632 }
633
634 // exported silent version of the above that just answer the question is_f2fs
fs_mgr_is_f2fs(const std::string & blk_device)635 bool fs_mgr_is_f2fs(const std::string& blk_device) {
636 android::base::ErrnoRestorer restore;
637 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC)));
638 if (fd < 0) return false;
639 __le32 sb;
640 if (TEMP_FAILURE_RETRY(pread(fd, &sb, sizeof(sb), F2FS_SUPER_OFFSET)) != sizeof(sb)) {
641 return false;
642 }
643 if (sb == cpu_to_le32(F2FS_SUPER_MAGIC)) return true;
644 if (TEMP_FAILURE_RETRY(pread(fd, &sb, sizeof(sb), F2FS_BLKSIZE + F2FS_SUPER_OFFSET)) !=
645 sizeof(sb)) {
646 return false;
647 }
648 return sb == cpu_to_le32(F2FS_SUPER_MAGIC);
649 }
650
SetReadAheadSize(const std::string & entry_block_device,off64_t size_kb)651 static void SetReadAheadSize(const std::string& entry_block_device, off64_t size_kb) {
652 std::string block_device;
653 if (!Realpath(entry_block_device, &block_device)) {
654 PERROR << "Failed to realpath " << entry_block_device;
655 return;
656 }
657
658 static constexpr std::string_view kDevBlockPrefix("/dev/block/");
659 if (!android::base::StartsWith(block_device, kDevBlockPrefix)) {
660 LWARNING << block_device << " is not a block device";
661 return;
662 }
663
664 DeviceMapper& dm = DeviceMapper::Instance();
665 while (true) {
666 std::string block_name = block_device;
667 if (android::base::StartsWith(block_device, kDevBlockPrefix)) {
668 block_name = block_device.substr(kDevBlockPrefix.length());
669 }
670 std::string sys_partition =
671 android::base::StringPrintf("/sys/class/block/%s/partition", block_name.c_str());
672 struct stat info;
673 if (lstat(sys_partition.c_str(), &info) == 0) {
674 // it has a partition like "sda12".
675 block_name += "/..";
676 }
677 std::string sys_ra = android::base::StringPrintf("/sys/class/block/%s/queue/read_ahead_kb",
678 block_name.c_str());
679 std::string size = android::base::StringPrintf("%llu", (long long)size_kb);
680 android::base::WriteStringToFile(size, sys_ra.c_str());
681 LINFO << "Set readahead_kb: " << size << " on " << sys_ra;
682
683 auto parent = dm.GetParentBlockDeviceByPath(block_device);
684 if (!parent) {
685 return;
686 }
687 block_device = *parent;
688 }
689 }
690
691 //
692 // Prepare the filesystem on the given block device to be mounted.
693 //
694 // If the "check" option was given in the fstab record, or it seems that the
695 // filesystem was uncleanly shut down, we'll run fsck on the filesystem.
696 //
697 // If needed, we'll also enable (or disable) filesystem features as specified by
698 // the fstab record.
699 //
prepare_fs_for_mount(const std::string & blk_device,const FstabEntry & entry,const std::string & alt_mount_point="")700 static int prepare_fs_for_mount(const std::string& blk_device, const FstabEntry& entry,
701 const std::string& alt_mount_point = "") {
702 auto& mount_point = alt_mount_point.empty() ? entry.mount_point : alt_mount_point;
703 // We need this because sometimes we have legacy symlinks that are
704 // lingering around and need cleaning up.
705 struct stat info;
706 if (lstat(mount_point.c_str(), &info) == 0 && (info.st_mode & S_IFMT) == S_IFLNK) {
707 unlink(mount_point.c_str());
708 }
709 mkdir(mount_point.c_str(), 0755);
710
711 // Don't need to return error, since it's a salt
712 if (entry.readahead_size_kb != -1) {
713 SetReadAheadSize(blk_device, entry.readahead_size_kb);
714 }
715
716 int fs_stat = 0;
717
718 if (is_extfs(entry.fs_type)) {
719 struct ext4_super_block sb;
720
721 if (read_ext4_superblock(blk_device, &sb, &fs_stat)) {
722 if ((sb.s_feature_incompat & EXT4_FEATURE_INCOMPAT_RECOVER) != 0 ||
723 (sb.s_state & EXT4_VALID_FS) == 0) {
724 LINFO << "Filesystem on " << blk_device << " was not cleanly shutdown; "
725 << "state flags: 0x" << std::hex << sb.s_state << ", "
726 << "incompat feature flags: 0x" << std::hex << sb.s_feature_incompat;
727 fs_stat |= FS_STAT_UNCLEAN_SHUTDOWN;
728 }
729
730 // Note: quotas should be enabled before running fsck.
731 tune_quota(blk_device, entry, &sb, &fs_stat);
732 } else {
733 return fs_stat;
734 }
735 } else if (is_f2fs(entry.fs_type)) {
736 if (!read_f2fs_superblock(blk_device, &fs_stat)) {
737 return fs_stat;
738 }
739 }
740
741 if (entry.fs_mgr_flags.check ||
742 (fs_stat & (FS_STAT_UNCLEAN_SHUTDOWN | FS_STAT_QUOTA_ENABLED))) {
743 check_fs(blk_device, entry.fs_type, mount_point, &fs_stat);
744 }
745
746 if (is_extfs(entry.fs_type) &&
747 (entry.reserved_size != 0 || entry.fs_mgr_flags.file_encryption ||
748 entry.fs_mgr_flags.fs_verity || entry.fs_mgr_flags.ext_meta_csum)) {
749 struct ext4_super_block sb;
750
751 if (read_ext4_superblock(blk_device, &sb, &fs_stat)) {
752 tune_reserved_size(blk_device, entry, &sb, &fs_stat);
753 tune_encrypt(blk_device, entry, &sb, &fs_stat);
754 tune_verity(blk_device, entry, &sb, &fs_stat);
755 tune_casefold(blk_device, entry, &sb, &fs_stat);
756 tune_metadata_csum(blk_device, entry, &sb, &fs_stat);
757 }
758 }
759
760 return fs_stat;
761 }
762
763 // Mark the given block device as read-only, using the BLKROSET ioctl.
fs_mgr_set_blk_ro(const std::string & blockdev,bool readonly)764 bool fs_mgr_set_blk_ro(const std::string& blockdev, bool readonly) {
765 unique_fd fd(TEMP_FAILURE_RETRY(open(blockdev.c_str(), O_RDONLY | O_CLOEXEC)));
766 if (fd < 0) {
767 return false;
768 }
769
770 int ON = readonly;
771 return ioctl(fd, BLKROSET, &ON) == 0;
772 }
773
774 // Orange state means the device is unlocked, see the following link for details.
775 // https://source.android.com/security/verifiedboot/verified-boot#device_state
fs_mgr_is_device_unlocked()776 bool fs_mgr_is_device_unlocked() {
777 std::string verified_boot_state;
778 if (fs_mgr_get_boot_config("verifiedbootstate", &verified_boot_state)) {
779 return verified_boot_state == "orange";
780 }
781 return false;
782 }
783
784 // __mount(): wrapper around the mount() system call which also
785 // sets the underlying block device to read-only if the mount is read-only.
786 // See "man 2 mount" for return values.
__mount(const std::string & source,const std::string & target,const FstabEntry & entry)787 static int __mount(const std::string& source, const std::string& target, const FstabEntry& entry) {
788 errno = 0;
789 unsigned long mountflags = entry.flags;
790 int ret = 0;
791 int save_errno = 0;
792 int gc_allowance = 0;
793 std::string opts;
794 bool try_f2fs_gc_allowance = is_f2fs(entry.fs_type) && entry.fs_checkpoint_opts.length() > 0;
795 Timer t;
796
797 do {
798 if (save_errno == EINVAL && try_f2fs_gc_allowance) {
799 PINFO << "Kernel does not support checkpoint=disable:[n]%, trying without.";
800 try_f2fs_gc_allowance = false;
801 }
802 if (try_f2fs_gc_allowance) {
803 opts = entry.fs_options + entry.fs_checkpoint_opts + ":" +
804 std::to_string(gc_allowance) + "%";
805 } else {
806 opts = entry.fs_options;
807 }
808 if (save_errno == EAGAIN) {
809 PINFO << "Retrying mount (source=" << source << ",target=" << target
810 << ",type=" << entry.fs_type << ", gc_allowance=" << gc_allowance << "%)=" << ret
811 << "(" << save_errno << ")";
812 }
813 ret = mount(source.c_str(), target.c_str(), entry.fs_type.c_str(), mountflags,
814 opts.c_str());
815 save_errno = errno;
816 if (try_f2fs_gc_allowance) gc_allowance += 10;
817 } while ((ret && save_errno == EAGAIN && gc_allowance <= 100) ||
818 (ret && save_errno == EINVAL && try_f2fs_gc_allowance));
819 const char* target_missing = "";
820 const char* source_missing = "";
821 if (save_errno == ENOENT) {
822 if (access(target.c_str(), F_OK)) {
823 target_missing = "(missing)";
824 } else if (access(source.c_str(), F_OK)) {
825 source_missing = "(missing)";
826 }
827 errno = save_errno;
828 }
829 PINFO << __FUNCTION__ << "(source=" << source << source_missing << ",target=" << target
830 << target_missing << ",type=" << entry.fs_type << ")=" << ret;
831 if ((ret == 0) && (mountflags & MS_RDONLY) != 0) {
832 fs_mgr_set_blk_ro(source);
833 }
834 android::base::SetProperty("ro.boottime.init.mount." + Basename(target),
835 std::to_string(t.duration().count()));
836 errno = save_errno;
837 return ret;
838 }
839
fs_match(const std::string & in1,const std::string & in2)840 static bool fs_match(const std::string& in1, const std::string& in2) {
841 if (in1.empty() || in2.empty()) {
842 return false;
843 }
844
845 auto in1_end = in1.size() - 1;
846 while (in1_end > 0 && in1[in1_end] == '/') {
847 in1_end--;
848 }
849
850 auto in2_end = in2.size() - 1;
851 while (in2_end > 0 && in2[in2_end] == '/') {
852 in2_end--;
853 }
854
855 if (in1_end != in2_end) {
856 return false;
857 }
858
859 for (size_t i = 0; i <= in1_end; ++i) {
860 if (in1[i] != in2[i]) {
861 return false;
862 }
863 }
864
865 return true;
866 }
867
868 // Tries to mount any of the consecutive fstab entries that match
869 // the mountpoint of the one given by fstab[start_idx].
870 //
871 // end_idx: On return, will be the last entry that was looked at.
872 // attempted_idx: On return, will indicate which fstab entry
873 // succeeded. In case of failure, it will be the start_idx.
874 // Sets errno to match the 1st mount failure on failure.
mount_with_alternatives(const Fstab & fstab,int start_idx,int * end_idx,int * attempted_idx)875 static bool mount_with_alternatives(const Fstab& fstab, int start_idx, int* end_idx,
876 int* attempted_idx) {
877 unsigned long i;
878 int mount_errno = 0;
879 bool mounted = false;
880
881 // Hunt down an fstab entry for the same mount point that might succeed.
882 for (i = start_idx;
883 // We required that fstab entries for the same mountpoint be consecutive.
884 i < fstab.size() && fstab[start_idx].mount_point == fstab[i].mount_point; i++) {
885 // Don't try to mount/encrypt the same mount point again.
886 // Deal with alternate entries for the same point which are required to be all following
887 // each other.
888 if (mounted) {
889 LERROR << __FUNCTION__ << "(): skipping fstab dup mountpoint=" << fstab[i].mount_point
890 << " rec[" << i << "].fs_type=" << fstab[i].fs_type << " already mounted as "
891 << fstab[*attempted_idx].fs_type;
892 continue;
893 }
894
895 int fs_stat = prepare_fs_for_mount(fstab[i].blk_device, fstab[i]);
896 if (fs_stat & FS_STAT_INVALID_MAGIC) {
897 LERROR << __FUNCTION__
898 << "(): skipping mount due to invalid magic, mountpoint=" << fstab[i].mount_point
899 << " blk_dev=" << realpath(fstab[i].blk_device) << " rec[" << i
900 << "].fs_type=" << fstab[i].fs_type;
901 mount_errno = EINVAL; // continue bootup for FDE
902 continue;
903 }
904
905 int retry_count = 2;
906 while (retry_count-- > 0) {
907 if (!__mount(fstab[i].blk_device, fstab[i].mount_point, fstab[i])) {
908 *attempted_idx = i;
909 mounted = true;
910 if (i != start_idx) {
911 LERROR << __FUNCTION__ << "(): Mounted " << fstab[i].blk_device << " on "
912 << fstab[i].mount_point << " with fs_type=" << fstab[i].fs_type
913 << " instead of " << fstab[start_idx].fs_type;
914 }
915 fs_stat &= ~FS_STAT_FULL_MOUNT_FAILED;
916 mount_errno = 0;
917 break;
918 } else {
919 if (retry_count <= 0) break; // run check_fs only once
920 fs_stat |= FS_STAT_FULL_MOUNT_FAILED;
921 // back up the first errno for crypto decisions.
922 if (mount_errno == 0) {
923 mount_errno = errno;
924 }
925 // retry after fsck
926 check_fs(fstab[i].blk_device, fstab[i].fs_type, fstab[i].mount_point, &fs_stat);
927 }
928 }
929 log_fs_stat(fstab[i].blk_device, fs_stat);
930 }
931
932 /* Adjust i for the case where it was still withing the recs[] */
933 if (i < fstab.size()) --i;
934
935 *end_idx = i;
936 if (!mounted) {
937 *attempted_idx = start_idx;
938 errno = mount_errno;
939 return false;
940 }
941 return true;
942 }
943
TranslateExtLabels(FstabEntry * entry)944 static bool TranslateExtLabels(FstabEntry* entry) {
945 if (!StartsWith(entry->blk_device, "LABEL=")) {
946 return true;
947 }
948
949 std::string label = entry->blk_device.substr(6);
950 if (label.size() > 16) {
951 LERROR << "FS label is longer than allowed by filesystem";
952 return false;
953 }
954
955 auto blockdir = std::unique_ptr<DIR, decltype(&closedir)>{opendir("/dev/block"), closedir};
956 if (!blockdir) {
957 LERROR << "couldn't open /dev/block";
958 return false;
959 }
960
961 struct dirent* ent;
962 while ((ent = readdir(blockdir.get()))) {
963 if (ent->d_type != DT_BLK)
964 continue;
965
966 unique_fd fd(TEMP_FAILURE_RETRY(
967 openat(dirfd(blockdir.get()), ent->d_name, O_RDONLY | O_CLOEXEC)));
968 if (fd < 0) {
969 LERROR << "Cannot open block device /dev/block/" << ent->d_name;
970 return false;
971 }
972
973 ext4_super_block super_block;
974 if (TEMP_FAILURE_RETRY(lseek(fd, 1024, SEEK_SET)) < 0 ||
975 TEMP_FAILURE_RETRY(read(fd, &super_block, sizeof(super_block))) !=
976 sizeof(super_block)) {
977 // Probably a loopback device or something else without a readable superblock.
978 continue;
979 }
980
981 if (super_block.s_magic != EXT4_SUPER_MAGIC) {
982 LINFO << "/dev/block/" << ent->d_name << " not ext{234}";
983 continue;
984 }
985
986 if (label == super_block.s_volume_name) {
987 std::string new_blk_device = "/dev/block/"s + ent->d_name;
988
989 LINFO << "resolved label " << entry->blk_device << " to " << new_blk_device;
990
991 entry->blk_device = new_blk_device;
992 return true;
993 }
994 }
995
996 return false;
997 }
998
needs_block_encryption(const FstabEntry & entry)999 static bool needs_block_encryption(const FstabEntry& entry) {
1000 if (android::base::GetBoolProperty("ro.vold.forceencryption", false) && entry.is_encryptable())
1001 return true;
1002 if (entry.fs_mgr_flags.force_crypt) return true;
1003 if (entry.fs_mgr_flags.crypt) {
1004 // Check for existence of convert_fde breadcrumb file.
1005 auto convert_fde_name = entry.mount_point + "/misc/vold/convert_fde";
1006 if (access(convert_fde_name.c_str(), F_OK) == 0) return true;
1007 }
1008 if (entry.fs_mgr_flags.force_fde_or_fbe) {
1009 // Check for absence of convert_fbe breadcrumb file.
1010 auto convert_fbe_name = entry.mount_point + "/convert_fbe";
1011 if (access(convert_fbe_name.c_str(), F_OK) != 0) return true;
1012 }
1013 return false;
1014 }
1015
should_use_metadata_encryption(const FstabEntry & entry)1016 static bool should_use_metadata_encryption(const FstabEntry& entry) {
1017 return !entry.metadata_key_dir.empty() &&
1018 (entry.fs_mgr_flags.file_encryption || entry.fs_mgr_flags.force_fde_or_fbe);
1019 }
1020
1021 // Check to see if a mountable volume has encryption requirements
handle_encryptable(const FstabEntry & entry)1022 static int handle_encryptable(const FstabEntry& entry) {
1023 // If this is block encryptable, need to trigger encryption.
1024 if (needs_block_encryption(entry)) {
1025 if (umount(entry.mount_point.c_str()) == 0) {
1026 return FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION;
1027 } else {
1028 PWARNING << "Could not umount " << entry.mount_point << " - allow continue unencrypted";
1029 return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
1030 }
1031 } else if (should_use_metadata_encryption(entry)) {
1032 if (umount(entry.mount_point.c_str()) == 0) {
1033 return FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION;
1034 } else {
1035 PERROR << "Could not umount " << entry.mount_point << " - fail since can't encrypt";
1036 return FS_MGR_MNTALL_FAIL;
1037 }
1038 } else if (entry.fs_mgr_flags.file_encryption || entry.fs_mgr_flags.force_fde_or_fbe) {
1039 LINFO << entry.mount_point << " is file encrypted";
1040 return FS_MGR_MNTALL_DEV_FILE_ENCRYPTED;
1041 } else if (entry.is_encryptable()) {
1042 return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
1043 } else {
1044 return FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE;
1045 }
1046 }
1047
set_type_property(int status)1048 static void set_type_property(int status) {
1049 switch (status) {
1050 case FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED:
1051 SetProperty("ro.crypto.type", "block");
1052 break;
1053 case FS_MGR_MNTALL_DEV_FILE_ENCRYPTED:
1054 case FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED:
1055 case FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION:
1056 SetProperty("ro.crypto.type", "file");
1057 break;
1058 }
1059 }
1060
call_vdc(const std::vector<std::string> & args,int * ret)1061 static bool call_vdc(const std::vector<std::string>& args, int* ret) {
1062 std::vector<char const*> argv;
1063 argv.emplace_back("/system/bin/vdc");
1064 for (auto& arg : args) {
1065 argv.emplace_back(arg.c_str());
1066 }
1067 LOG(INFO) << "Calling: " << android::base::Join(argv, ' ');
1068 int err = logwrap_fork_execvp(argv.size(), argv.data(), ret, false, LOG_ALOG, false, nullptr);
1069 if (err != 0) {
1070 LOG(ERROR) << "vdc call failed with error code: " << err;
1071 return false;
1072 }
1073 LOG(DEBUG) << "vdc finished successfully";
1074 if (ret != nullptr) {
1075 *ret = WEXITSTATUS(*ret);
1076 }
1077 return true;
1078 }
1079
fs_mgr_update_logical_partition(FstabEntry * entry)1080 bool fs_mgr_update_logical_partition(FstabEntry* entry) {
1081 // Logical partitions are specified with a named partition rather than a
1082 // block device, so if the block device is a path, then it has already
1083 // been updated.
1084 if (entry->blk_device[0] == '/') {
1085 return true;
1086 }
1087
1088 DeviceMapper& dm = DeviceMapper::Instance();
1089 std::string device_name;
1090 if (!dm.GetDmDevicePathByName(entry->blk_device, &device_name)) {
1091 return false;
1092 }
1093
1094 entry->blk_device = device_name;
1095 return true;
1096 }
1097
SupportsCheckpoint(FstabEntry * entry)1098 static bool SupportsCheckpoint(FstabEntry* entry) {
1099 return entry->fs_mgr_flags.checkpoint_blk || entry->fs_mgr_flags.checkpoint_fs;
1100 }
1101
1102 class CheckpointManager {
1103 public:
CheckpointManager(int needs_checkpoint=-1,bool metadata_encrypted=false)1104 CheckpointManager(int needs_checkpoint = -1, bool metadata_encrypted = false)
1105 : needs_checkpoint_(needs_checkpoint), metadata_encrypted_(metadata_encrypted) {}
1106
NeedsCheckpoint()1107 bool NeedsCheckpoint() {
1108 if (needs_checkpoint_ != UNKNOWN) {
1109 return needs_checkpoint_ == YES;
1110 }
1111 if (!call_vdc({"checkpoint", "needsCheckpoint"}, &needs_checkpoint_)) {
1112 LERROR << "Failed to find if checkpointing is needed. Assuming no.";
1113 needs_checkpoint_ = NO;
1114 }
1115 return needs_checkpoint_ == YES;
1116 }
1117
Update(FstabEntry * entry,const std::string & block_device=std::string ())1118 bool Update(FstabEntry* entry, const std::string& block_device = std::string()) {
1119 if (!SupportsCheckpoint(entry)) {
1120 return true;
1121 }
1122
1123 if (entry->fs_mgr_flags.checkpoint_blk && !metadata_encrypted_) {
1124 call_vdc({"checkpoint", "restoreCheckpoint", entry->blk_device}, nullptr);
1125 }
1126
1127 if (!NeedsCheckpoint()) {
1128 return true;
1129 }
1130
1131 if (!UpdateCheckpointPartition(entry, block_device)) {
1132 LERROR << "Could not set up checkpoint partition, skipping!";
1133 return false;
1134 }
1135
1136 return true;
1137 }
1138
Revert(FstabEntry * entry)1139 bool Revert(FstabEntry* entry) {
1140 if (!SupportsCheckpoint(entry)) {
1141 return true;
1142 }
1143
1144 if (device_map_.find(entry->blk_device) == device_map_.end()) {
1145 return true;
1146 }
1147
1148 std::string bow_device = entry->blk_device;
1149 entry->blk_device = device_map_[bow_device];
1150 device_map_.erase(bow_device);
1151
1152 DeviceMapper& dm = DeviceMapper::Instance();
1153 if (!dm.DeleteDevice("bow")) {
1154 PERROR << "Failed to remove bow device";
1155 }
1156
1157 return true;
1158 }
1159
1160 private:
UpdateCheckpointPartition(FstabEntry * entry,const std::string & block_device)1161 bool UpdateCheckpointPartition(FstabEntry* entry, const std::string& block_device) {
1162 if (entry->fs_mgr_flags.checkpoint_fs) {
1163 if (is_f2fs(entry->fs_type)) {
1164 entry->fs_checkpoint_opts = ",checkpoint=disable";
1165 } else {
1166 LERROR << entry->fs_type << " does not implement checkpoints.";
1167 }
1168 } else if (entry->fs_mgr_flags.checkpoint_blk) {
1169 auto actual_block_device = block_device.empty() ? entry->blk_device : block_device;
1170 if (fs_mgr_find_bow_device(actual_block_device).empty()) {
1171 unique_fd fd(
1172 TEMP_FAILURE_RETRY(open(entry->blk_device.c_str(), O_RDONLY | O_CLOEXEC)));
1173 if (fd < 0) {
1174 PERROR << "Cannot open device " << entry->blk_device;
1175 return false;
1176 }
1177
1178 uint64_t size = get_block_device_size(fd) / 512;
1179 if (!size) {
1180 PERROR << "Cannot get device size";
1181 return false;
1182 }
1183
1184 android::dm::DmTable table;
1185 auto bowTarget =
1186 std::make_unique<android::dm::DmTargetBow>(0, size, entry->blk_device);
1187
1188 // dm-bow uses the first block as a log record, and relocates the real first block
1189 // elsewhere. For metadata encrypted devices, dm-bow sits below dm-default-key, and
1190 // for post Android Q devices dm-default-key uses a block size of 4096 always.
1191 // So if dm-bow's block size, which by default is the block size of the underlying
1192 // hardware, is less than dm-default-key's, blocks will get broken up and I/O will
1193 // fail as it won't be data_unit_size aligned.
1194 // However, since it is possible there is an already shipping non
1195 // metadata-encrypted device with smaller blocks, we must not change this for
1196 // devices shipped with Q or earlier unless they explicitly selected dm-default-key
1197 // v2
1198 unsigned int options_format_version = android::base::GetUintProperty<unsigned int>(
1199 "ro.crypto.dm_default_key.options_format.version",
1200 (android::fscrypt::GetFirstApiLevel() <= __ANDROID_API_Q__ ? 1 : 2));
1201 if (options_format_version > 1) {
1202 bowTarget->SetBlockSize(4096);
1203 }
1204
1205 if (!table.AddTarget(std::move(bowTarget))) {
1206 LERROR << "Failed to add bow target";
1207 return false;
1208 }
1209
1210 DeviceMapper& dm = DeviceMapper::Instance();
1211 if (!dm.CreateDevice("bow", table)) {
1212 PERROR << "Failed to create bow device";
1213 return false;
1214 }
1215
1216 std::string name;
1217 if (!dm.GetDmDevicePathByName("bow", &name)) {
1218 PERROR << "Failed to get bow device name";
1219 return false;
1220 }
1221
1222 device_map_[name] = entry->blk_device;
1223 entry->blk_device = name;
1224 }
1225 }
1226 return true;
1227 }
1228
1229 enum { UNKNOWN = -1, NO = 0, YES = 1 };
1230 int needs_checkpoint_;
1231 bool metadata_encrypted_;
1232 std::map<std::string, std::string> device_map_;
1233 };
1234
fs_mgr_find_bow_device(const std::string & block_device)1235 std::string fs_mgr_find_bow_device(const std::string& block_device) {
1236 if (block_device.substr(0, 5) != "/dev/") {
1237 LOG(ERROR) << "Expected block device, got " << block_device;
1238 return std::string();
1239 }
1240
1241 std::string sys_dir = std::string("/sys/") + block_device.substr(5);
1242
1243 for (;;) {
1244 std::string name;
1245 if (!android::base::ReadFileToString(sys_dir + "/dm/name", &name)) {
1246 PLOG(ERROR) << block_device << " is not dm device";
1247 return std::string();
1248 }
1249
1250 if (name == "bow\n") return sys_dir;
1251
1252 std::string slaves = sys_dir + "/slaves";
1253 std::unique_ptr<DIR, decltype(&closedir)> directory(opendir(slaves.c_str()), closedir);
1254 if (!directory) {
1255 PLOG(ERROR) << "Can't open slave directory " << slaves;
1256 return std::string();
1257 }
1258
1259 int count = 0;
1260 for (dirent* entry = readdir(directory.get()); entry; entry = readdir(directory.get())) {
1261 if (entry->d_type != DT_LNK) continue;
1262
1263 if (count == 1) {
1264 LOG(ERROR) << "Too many slaves in " << slaves;
1265 return std::string();
1266 }
1267
1268 ++count;
1269 sys_dir = std::string("/sys/block/") + entry->d_name;
1270 }
1271
1272 if (count != 1) {
1273 LOG(ERROR) << "No slave in " << slaves;
1274 return std::string();
1275 }
1276 }
1277 }
1278
1279 static constexpr const char* kUserdataWrapperName = "userdata-wrapper";
1280
WrapUserdata(FstabEntry * entry,dev_t dev,const std::string & block_device)1281 static void WrapUserdata(FstabEntry* entry, dev_t dev, const std::string& block_device) {
1282 DeviceMapper& dm = DeviceMapper::Instance();
1283 if (dm.GetState(kUserdataWrapperName) != DmDeviceState::INVALID) {
1284 // This will report failure for us. If we do fail to get the path,
1285 // we leave the device unwrapped.
1286 dm.GetDmDevicePathByName(kUserdataWrapperName, &entry->blk_device);
1287 return;
1288 }
1289
1290 unique_fd fd(open(block_device.c_str(), O_RDONLY | O_CLOEXEC));
1291 if (fd < 0) {
1292 PLOG(ERROR) << "open failed: " << entry->blk_device;
1293 return;
1294 }
1295
1296 auto dev_str = android::base::StringPrintf("%u:%u", major(dev), minor(dev));
1297 uint64_t sectors = get_block_device_size(fd) / 512;
1298
1299 android::dm::DmTable table;
1300 table.Emplace<DmTargetLinear>(0, sectors, dev_str, 0);
1301
1302 std::string dm_path;
1303 if (!dm.CreateDevice(kUserdataWrapperName, table, &dm_path, 20s)) {
1304 LOG(ERROR) << "Failed to create userdata wrapper device";
1305 return;
1306 }
1307 entry->blk_device = dm_path;
1308 }
1309
1310 // When using Virtual A/B, partitions can be backed by /data and mapped with
1311 // device-mapper in first-stage init. This can happen when merging an OTA or
1312 // when using adb remount to house "scratch". In this case, /data cannot be
1313 // mounted directly off the userdata block device, and e2fsck will refuse to
1314 // scan it, because the kernel reports the block device as in-use.
1315 //
1316 // As a workaround, when mounting /data, we create a trivial dm-linear wrapper
1317 // if the underlying block device already has dependencies. Note that we make
1318 // an exception for metadata-encrypted devices, since dm-default-key is already
1319 // a wrapper.
WrapUserdataIfNeeded(FstabEntry * entry,const std::string & actual_block_device={})1320 static void WrapUserdataIfNeeded(FstabEntry* entry, const std::string& actual_block_device = {}) {
1321 const auto& block_device =
1322 actual_block_device.empty() ? entry->blk_device : actual_block_device;
1323 if (entry->mount_point != "/data" || !entry->metadata_key_dir.empty() ||
1324 android::base::StartsWith(block_device, "/dev/block/dm-")) {
1325 return;
1326 }
1327
1328 struct stat st;
1329 if (stat(block_device.c_str(), &st) < 0) {
1330 PLOG(ERROR) << "stat failed: " << block_device;
1331 return;
1332 }
1333
1334 std::string path = android::base::StringPrintf("/sys/dev/block/%u:%u/holders",
1335 major(st.st_rdev), minor(st.st_rdev));
1336 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
1337 if (!dir) {
1338 PLOG(ERROR) << "opendir failed: " << path;
1339 return;
1340 }
1341
1342 struct dirent* d;
1343 bool has_holders = false;
1344 while ((d = readdir(dir.get())) != nullptr) {
1345 if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
1346 has_holders = true;
1347 break;
1348 }
1349 }
1350
1351 if (has_holders) {
1352 WrapUserdata(entry, st.st_rdev, block_device);
1353 }
1354 }
1355
IsMountPointMounted(const std::string & mount_point)1356 static bool IsMountPointMounted(const std::string& mount_point) {
1357 // Check if this is already mounted.
1358 Fstab fstab;
1359 if (!ReadFstabFromFile("/proc/mounts", &fstab)) {
1360 return false;
1361 }
1362 return GetEntryForMountPoint(&fstab, mount_point) != nullptr;
1363 }
1364
1365 // When multiple fstab records share the same mount_point, it will try to mount each
1366 // one in turn, and ignore any duplicates after a first successful mount.
1367 // Returns -1 on error, and FS_MGR_MNTALL_* otherwise.
fs_mgr_mount_all(Fstab * fstab,int mount_mode)1368 MountAllResult fs_mgr_mount_all(Fstab* fstab, int mount_mode) {
1369 int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE;
1370 int error_count = 0;
1371 CheckpointManager checkpoint_manager;
1372 AvbUniquePtr avb_handle(nullptr);
1373 bool wiped = false;
1374
1375 bool userdata_mounted = false;
1376 if (fstab->empty()) {
1377 return {FS_MGR_MNTALL_FAIL, userdata_mounted};
1378 }
1379
1380 // Keep i int to prevent unsigned integer overflow from (i = top_idx - 1),
1381 // where top_idx is 0. It will give SIGABRT
1382 for (int i = 0; i < static_cast<int>(fstab->size()); i++) {
1383 auto& current_entry = (*fstab)[i];
1384
1385 // If a filesystem should have been mounted in the first stage, we
1386 // ignore it here. With one exception, if the filesystem is
1387 // formattable, then it can only be formatted in the second stage,
1388 // so we allow it to mount here.
1389 if (current_entry.fs_mgr_flags.first_stage_mount &&
1390 (!current_entry.fs_mgr_flags.formattable ||
1391 IsMountPointMounted(current_entry.mount_point))) {
1392 continue;
1393 }
1394
1395 // Don't mount entries that are managed by vold or not for the mount mode.
1396 if (current_entry.fs_mgr_flags.vold_managed || current_entry.fs_mgr_flags.recovery_only ||
1397 ((mount_mode == MOUNT_MODE_LATE) && !current_entry.fs_mgr_flags.late_mount) ||
1398 ((mount_mode == MOUNT_MODE_EARLY) && current_entry.fs_mgr_flags.late_mount)) {
1399 continue;
1400 }
1401
1402 // Skip swap and raw partition entries such as boot, recovery, etc.
1403 if (current_entry.fs_type == "swap" || current_entry.fs_type == "emmc" ||
1404 current_entry.fs_type == "mtd") {
1405 continue;
1406 }
1407
1408 // Skip mounting the root partition, as it will already have been mounted.
1409 if (current_entry.mount_point == "/" || current_entry.mount_point == "/system") {
1410 if ((current_entry.flags & MS_RDONLY) != 0) {
1411 fs_mgr_set_blk_ro(current_entry.blk_device);
1412 }
1413 continue;
1414 }
1415
1416 // Terrible hack to make it possible to remount /data.
1417 // TODO: refactor fs_mgr_mount_all and get rid of this.
1418 if (mount_mode == MOUNT_MODE_ONLY_USERDATA && current_entry.mount_point != "/data") {
1419 continue;
1420 }
1421
1422 // Translate LABEL= file system labels into block devices.
1423 if (is_extfs(current_entry.fs_type)) {
1424 if (!TranslateExtLabels(¤t_entry)) {
1425 LERROR << "Could not translate label to block device";
1426 continue;
1427 }
1428 }
1429
1430 if (current_entry.fs_mgr_flags.logical) {
1431 if (!fs_mgr_update_logical_partition(¤t_entry)) {
1432 LERROR << "Could not set up logical partition, skipping!";
1433 continue;
1434 }
1435 }
1436
1437 WrapUserdataIfNeeded(¤t_entry);
1438
1439 if (!checkpoint_manager.Update(¤t_entry)) {
1440 continue;
1441 }
1442
1443 if (current_entry.fs_mgr_flags.wait && !WaitForFile(current_entry.blk_device, 20s)) {
1444 LERROR << "Skipping '" << current_entry.blk_device << "' during mount_all";
1445 continue;
1446 }
1447
1448 if (current_entry.fs_mgr_flags.avb) {
1449 if (!avb_handle) {
1450 avb_handle = AvbHandle::Open();
1451 if (!avb_handle) {
1452 LERROR << "Failed to open AvbHandle";
1453 set_type_property(encryptable);
1454 return {FS_MGR_MNTALL_FAIL, userdata_mounted};
1455 }
1456 }
1457 if (avb_handle->SetUpAvbHashtree(¤t_entry, true /* wait_for_verity_dev */) ==
1458 AvbHashtreeResult::kFail) {
1459 LERROR << "Failed to set up AVB on partition: " << current_entry.mount_point
1460 << ", skipping!";
1461 // Skips mounting the device.
1462 continue;
1463 }
1464 } else if (!current_entry.avb_keys.empty()) {
1465 if (AvbHandle::SetUpStandaloneAvbHashtree(¤t_entry) == AvbHashtreeResult::kFail) {
1466 LERROR << "Failed to set up AVB on standalone partition: "
1467 << current_entry.mount_point << ", skipping!";
1468 // Skips mounting the device.
1469 continue;
1470 }
1471 } else if ((current_entry.fs_mgr_flags.verify)) {
1472 int rc = fs_mgr_setup_verity(¤t_entry, true);
1473 if (rc == FS_MGR_SETUP_VERITY_DISABLED || rc == FS_MGR_SETUP_VERITY_SKIPPED) {
1474 LINFO << "Verity disabled";
1475 } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
1476 LERROR << "Could not set up verified partition, skipping!";
1477 continue;
1478 }
1479 }
1480
1481 int last_idx_inspected;
1482 int top_idx = i;
1483 int attempted_idx = -1;
1484
1485 bool mret = mount_with_alternatives(*fstab, i, &last_idx_inspected, &attempted_idx);
1486 auto& attempted_entry = (*fstab)[attempted_idx];
1487 i = last_idx_inspected;
1488 int mount_errno = errno;
1489
1490 // Handle success and deal with encryptability.
1491 if (mret) {
1492 int status = handle_encryptable(attempted_entry);
1493
1494 if (status == FS_MGR_MNTALL_FAIL) {
1495 // Fatal error - no point continuing.
1496 return {status, userdata_mounted};
1497 }
1498
1499 if (status != FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
1500 if (encryptable != FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
1501 // Log and continue
1502 LERROR << "Only one encryptable/encrypted partition supported";
1503 }
1504 encryptable = status;
1505 if (status == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) {
1506 if (!call_vdc({"cryptfs", "encryptFstab", attempted_entry.blk_device,
1507 attempted_entry.mount_point, wiped ? "true" : "false",
1508 attempted_entry.fs_type},
1509 nullptr)) {
1510 LERROR << "Encryption failed";
1511 set_type_property(encryptable);
1512 return {FS_MGR_MNTALL_FAIL, userdata_mounted};
1513 }
1514 }
1515 }
1516
1517 if (current_entry.mount_point == "/data") {
1518 userdata_mounted = true;
1519 }
1520 // Success! Go get the next one.
1521 continue;
1522 }
1523
1524 // Mounting failed, understand why and retry.
1525 wiped = partition_wiped(current_entry.blk_device.c_str());
1526 bool crypt_footer = false;
1527 if (mount_errno != EBUSY && mount_errno != EACCES &&
1528 current_entry.fs_mgr_flags.formattable && wiped) {
1529 // current_entry and attempted_entry point at the same partition, but sometimes
1530 // at two different lines in the fstab. Use current_entry for formatting
1531 // as that is the preferred one.
1532 LERROR << __FUNCTION__ << "(): " << realpath(current_entry.blk_device)
1533 << " is wiped and " << current_entry.mount_point << " " << current_entry.fs_type
1534 << " is formattable. Format it.";
1535
1536 checkpoint_manager.Revert(¤t_entry);
1537
1538 if (current_entry.is_encryptable() && current_entry.key_loc != KEY_IN_FOOTER) {
1539 unique_fd fd(TEMP_FAILURE_RETRY(
1540 open(current_entry.key_loc.c_str(), O_WRONLY | O_CLOEXEC)));
1541 if (fd >= 0) {
1542 LINFO << __FUNCTION__ << "(): also wipe " << current_entry.key_loc;
1543 wipe_block_device(fd, get_file_size(fd));
1544 } else {
1545 PERROR << __FUNCTION__ << "(): " << current_entry.key_loc << " wouldn't open";
1546 }
1547 } else if (current_entry.is_encryptable() && current_entry.key_loc == KEY_IN_FOOTER) {
1548 crypt_footer = true;
1549 }
1550
1551 // EncryptInplace will be used when vdc gives an error or needs to format partitions
1552 // other than /data
1553 if (should_use_metadata_encryption(current_entry) &&
1554 current_entry.mount_point == "/data") {
1555
1556 // vdc->Format requires "ro.crypto.type" to set an encryption flag
1557 encryptable = FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED;
1558 set_type_property(encryptable);
1559
1560 if (!call_vdc({"cryptfs", "encryptFstab", current_entry.blk_device,
1561 current_entry.mount_point, "true" /* shouldFormat */,
1562 current_entry.fs_type},
1563 nullptr)) {
1564 LERROR << "Encryption failed";
1565 } else {
1566 userdata_mounted = true;
1567 continue;
1568 }
1569 }
1570
1571 if (fs_mgr_do_format(current_entry, crypt_footer) == 0) {
1572 // Let's replay the mount actions.
1573 i = top_idx - 1;
1574 continue;
1575 } else {
1576 LERROR << __FUNCTION__ << "(): Format failed. "
1577 << "Suggest recovery...";
1578 encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY;
1579 continue;
1580 }
1581 }
1582
1583 // mount(2) returned an error, handle the encryptable/formattable case.
1584 if (mount_errno != EBUSY && mount_errno != EACCES && attempted_entry.is_encryptable()) {
1585 if (wiped) {
1586 LERROR << __FUNCTION__ << "(): " << attempted_entry.blk_device << " is wiped and "
1587 << attempted_entry.mount_point << " " << attempted_entry.fs_type
1588 << " is encryptable. Suggest recovery...";
1589 encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY;
1590 continue;
1591 } else {
1592 // Need to mount a tmpfs at this mountpoint for now, and set
1593 // properties that vold will query later for decrypting
1594 LERROR << __FUNCTION__ << "(): possibly an encryptable blkdev "
1595 << attempted_entry.blk_device << " for mount " << attempted_entry.mount_point
1596 << " type " << attempted_entry.fs_type;
1597 if (fs_mgr_do_tmpfs_mount(attempted_entry.mount_point.c_str()) < 0) {
1598 ++error_count;
1599 continue;
1600 }
1601 }
1602 encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
1603 } else if (mount_errno != EBUSY && mount_errno != EACCES &&
1604 should_use_metadata_encryption(attempted_entry)) {
1605 if (!call_vdc({"cryptfs", "mountFstab", attempted_entry.blk_device,
1606 attempted_entry.mount_point},
1607 nullptr)) {
1608 ++error_count;
1609 } else if (current_entry.mount_point == "/data") {
1610 userdata_mounted = true;
1611 }
1612 encryptable = FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED;
1613 continue;
1614 } else {
1615 // fs_options might be null so we cannot use PERROR << directly.
1616 // Use StringPrintf to output "(null)" instead.
1617 if (attempted_entry.fs_mgr_flags.no_fail) {
1618 PERROR << android::base::StringPrintf(
1619 "Ignoring failure to mount an un-encryptable or wiped "
1620 "partition on %s at %s options: %s",
1621 attempted_entry.blk_device.c_str(), attempted_entry.mount_point.c_str(),
1622 attempted_entry.fs_options.c_str());
1623 } else {
1624 PERROR << android::base::StringPrintf(
1625 "Failed to mount an un-encryptable or wiped partition "
1626 "on %s at %s options: %s",
1627 attempted_entry.blk_device.c_str(), attempted_entry.mount_point.c_str(),
1628 attempted_entry.fs_options.c_str());
1629 ++error_count;
1630 }
1631 continue;
1632 }
1633 }
1634
1635 set_type_property(encryptable);
1636
1637 #if ALLOW_ADBD_DISABLE_VERITY == 1 // "userdebug" build
1638 fs_mgr_overlayfs_mount_all(fstab);
1639 #endif
1640
1641 if (error_count) {
1642 return {FS_MGR_MNTALL_FAIL, userdata_mounted};
1643 } else {
1644 return {encryptable, userdata_mounted};
1645 }
1646 }
1647
fs_mgr_umount_all(android::fs_mgr::Fstab * fstab)1648 int fs_mgr_umount_all(android::fs_mgr::Fstab* fstab) {
1649 AvbUniquePtr avb_handle(nullptr);
1650 int ret = FsMgrUmountStatus::SUCCESS;
1651 for (auto& current_entry : *fstab) {
1652 if (!IsMountPointMounted(current_entry.mount_point)) {
1653 continue;
1654 }
1655
1656 if (umount(current_entry.mount_point.c_str()) == -1) {
1657 PERROR << "Failed to umount " << current_entry.mount_point;
1658 ret |= FsMgrUmountStatus::ERROR_UMOUNT;
1659 continue;
1660 }
1661
1662 if (current_entry.fs_mgr_flags.logical) {
1663 if (!fs_mgr_update_logical_partition(¤t_entry)) {
1664 LERROR << "Could not get logical partition blk_device, skipping!";
1665 ret |= FsMgrUmountStatus::ERROR_DEVICE_MAPPER;
1666 continue;
1667 }
1668 }
1669
1670 if (current_entry.fs_mgr_flags.avb || !current_entry.avb_keys.empty()) {
1671 if (!AvbHandle::TearDownAvbHashtree(¤t_entry, true /* wait */)) {
1672 LERROR << "Failed to tear down AVB on mount point: " << current_entry.mount_point;
1673 ret |= FsMgrUmountStatus::ERROR_VERITY;
1674 continue;
1675 }
1676 } else if ((current_entry.fs_mgr_flags.verify)) {
1677 if (!fs_mgr_teardown_verity(¤t_entry)) {
1678 LERROR << "Failed to tear down verified partition on mount point: "
1679 << current_entry.mount_point;
1680 ret |= FsMgrUmountStatus::ERROR_VERITY;
1681 continue;
1682 }
1683 }
1684 }
1685 return ret;
1686 }
1687
GetMillisProperty(const std::string & name,std::chrono::milliseconds default_value)1688 static std::chrono::milliseconds GetMillisProperty(const std::string& name,
1689 std::chrono::milliseconds default_value) {
1690 auto value = GetUintProperty(name, static_cast<uint64_t>(default_value.count()));
1691 return std::chrono::milliseconds(std::move(value));
1692 }
1693
fs_mgr_unmount_all_data_mounts(const std::string & data_block_device)1694 static bool fs_mgr_unmount_all_data_mounts(const std::string& data_block_device) {
1695 LINFO << __FUNCTION__ << "(): about to umount everything on top of " << data_block_device;
1696 Timer t;
1697 auto timeout = GetMillisProperty("init.userspace_reboot.userdata_remount.timeoutmillis", 5s);
1698 while (true) {
1699 bool umount_done = true;
1700 Fstab proc_mounts;
1701 if (!ReadFstabFromFile("/proc/mounts", &proc_mounts)) {
1702 LERROR << __FUNCTION__ << "(): Can't read /proc/mounts";
1703 return false;
1704 }
1705 // Now proceed with other bind mounts on top of /data.
1706 for (const auto& entry : proc_mounts) {
1707 std::string block_device;
1708 if (StartsWith(entry.blk_device, "/dev/block") &&
1709 !Realpath(entry.blk_device, &block_device)) {
1710 PWARNING << __FUNCTION__ << "(): failed to realpath " << entry.blk_device;
1711 block_device = entry.blk_device;
1712 }
1713 if (data_block_device == block_device) {
1714 if (umount2(entry.mount_point.c_str(), 0) != 0) {
1715 PERROR << __FUNCTION__ << "(): Failed to umount " << entry.mount_point;
1716 umount_done = false;
1717 }
1718 }
1719 }
1720 if (umount_done) {
1721 LINFO << __FUNCTION__ << "(): Unmounting /data took " << t;
1722 return true;
1723 }
1724 if (t.duration() > timeout) {
1725 LERROR << __FUNCTION__ << "(): Timed out unmounting all mounts on "
1726 << data_block_device;
1727 Fstab remaining_mounts;
1728 if (!ReadFstabFromFile("/proc/mounts", &remaining_mounts)) {
1729 LERROR << __FUNCTION__ << "(): Can't read /proc/mounts";
1730 } else {
1731 LERROR << __FUNCTION__ << "(): Following mounts remaining";
1732 for (const auto& e : remaining_mounts) {
1733 LERROR << __FUNCTION__ << "(): mount point: " << e.mount_point
1734 << " block device: " << e.blk_device;
1735 }
1736 }
1737 return false;
1738 }
1739 std::this_thread::sleep_for(50ms);
1740 }
1741 }
1742
UnwindDmDeviceStack(const std::string & block_device,std::vector<std::string> * dm_stack)1743 static bool UnwindDmDeviceStack(const std::string& block_device,
1744 std::vector<std::string>* dm_stack) {
1745 if (!StartsWith(block_device, "/dev/block/")) {
1746 LWARNING << block_device << " is not a block device";
1747 return false;
1748 }
1749 std::string current = block_device;
1750 DeviceMapper& dm = DeviceMapper::Instance();
1751 while (true) {
1752 dm_stack->push_back(current);
1753 if (!dm.IsDmBlockDevice(current)) {
1754 break;
1755 }
1756 auto parent = dm.GetParentBlockDeviceByPath(current);
1757 if (!parent) {
1758 return false;
1759 }
1760 current = *parent;
1761 }
1762 return true;
1763 }
1764
fs_mgr_get_mounted_entry_for_userdata(Fstab * fstab,const std::string & data_block_device)1765 FstabEntry* fs_mgr_get_mounted_entry_for_userdata(Fstab* fstab,
1766 const std::string& data_block_device) {
1767 std::vector<std::string> dm_stack;
1768 if (!UnwindDmDeviceStack(data_block_device, &dm_stack)) {
1769 LERROR << "Failed to unwind dm-device stack for " << data_block_device;
1770 return nullptr;
1771 }
1772 for (auto& entry : *fstab) {
1773 if (entry.mount_point != "/data") {
1774 continue;
1775 }
1776 std::string block_device;
1777 if (entry.fs_mgr_flags.logical) {
1778 if (!fs_mgr_update_logical_partition(&entry)) {
1779 LERROR << "Failed to update logic partition " << entry.blk_device;
1780 continue;
1781 }
1782 block_device = entry.blk_device;
1783 } else if (!Realpath(entry.blk_device, &block_device)) {
1784 PWARNING << "Failed to realpath " << entry.blk_device;
1785 block_device = entry.blk_device;
1786 }
1787 if (std::find(dm_stack.begin(), dm_stack.end(), block_device) != dm_stack.end()) {
1788 return &entry;
1789 }
1790 }
1791 LERROR << "Didn't find entry that was used to mount /data onto " << data_block_device;
1792 return nullptr;
1793 }
1794
1795 // TODO(b/143970043): return different error codes based on which step failed.
fs_mgr_remount_userdata_into_checkpointing(Fstab * fstab)1796 int fs_mgr_remount_userdata_into_checkpointing(Fstab* fstab) {
1797 Fstab proc_mounts;
1798 if (!ReadFstabFromFile("/proc/mounts", &proc_mounts)) {
1799 LERROR << "Can't read /proc/mounts";
1800 return -1;
1801 }
1802 auto mounted_entry = GetEntryForMountPoint(&proc_mounts, "/data");
1803 if (mounted_entry == nullptr) {
1804 LERROR << "/data is not mounted";
1805 return -1;
1806 }
1807 std::string block_device;
1808 if (!Realpath(mounted_entry->blk_device, &block_device)) {
1809 PERROR << "Failed to realpath " << mounted_entry->blk_device;
1810 return -1;
1811 }
1812 auto fstab_entry = fs_mgr_get_mounted_entry_for_userdata(fstab, block_device);
1813 if (fstab_entry == nullptr) {
1814 LERROR << "Can't find /data in fstab";
1815 return -1;
1816 }
1817 bool force_umount = GetBoolProperty("sys.init.userdata_remount.force_umount", false);
1818 if (force_umount) {
1819 LINFO << "Will force an umount of userdata even if it's not required";
1820 }
1821 if (!force_umount && !SupportsCheckpoint(fstab_entry)) {
1822 LINFO << "Userdata doesn't support checkpointing. Nothing to do";
1823 return 0;
1824 }
1825 CheckpointManager checkpoint_manager;
1826 if (!force_umount && !checkpoint_manager.NeedsCheckpoint()) {
1827 LINFO << "Checkpointing not needed. Don't remount";
1828 return 0;
1829 }
1830 if (!force_umount && fstab_entry->fs_mgr_flags.checkpoint_fs) {
1831 // Userdata is f2fs, simply remount it.
1832 if (!checkpoint_manager.Update(fstab_entry)) {
1833 LERROR << "Failed to remount userdata in checkpointing mode";
1834 return -1;
1835 }
1836 if (mount(block_device.c_str(), fstab_entry->mount_point.c_str(), "none",
1837 MS_REMOUNT | fstab_entry->flags, fstab_entry->fs_options.c_str()) != 0) {
1838 PERROR << "Failed to remount userdata in checkpointing mode";
1839 return -1;
1840 }
1841 } else {
1842 LINFO << "Unmounting /data before remounting into checkpointing mode";
1843 if (!fs_mgr_unmount_all_data_mounts(block_device)) {
1844 LERROR << "Failed to umount /data";
1845 return -1;
1846 }
1847 DeviceMapper& dm = DeviceMapper::Instance();
1848 while (dm.IsDmBlockDevice(block_device)) {
1849 auto next_device = dm.GetParentBlockDeviceByPath(block_device);
1850 auto name = dm.GetDmDeviceNameByPath(block_device);
1851 if (!name) {
1852 LERROR << "Failed to get dm-name for " << block_device;
1853 return -1;
1854 }
1855 LINFO << "Deleting " << block_device << " named " << *name;
1856 if (!dm.DeleteDevice(*name, 3s)) {
1857 return -1;
1858 }
1859 if (!next_device) {
1860 LERROR << "Failed to find parent device for " << block_device;
1861 }
1862 block_device = *next_device;
1863 }
1864 LINFO << "Remounting /data";
1865 // TODO(b/143970043): remove this hack after fs_mgr_mount_all is refactored.
1866 auto result = fs_mgr_mount_all(fstab, MOUNT_MODE_ONLY_USERDATA);
1867 return result.code == FS_MGR_MNTALL_FAIL ? -1 : 0;
1868 }
1869 return 0;
1870 }
1871
1872 // wrapper to __mount() and expects a fully prepared fstab_rec,
1873 // unlike fs_mgr_do_mount which does more things with avb / verity etc.
fs_mgr_do_mount_one(const FstabEntry & entry,const std::string & alt_mount_point)1874 int fs_mgr_do_mount_one(const FstabEntry& entry, const std::string& alt_mount_point) {
1875 // First check the filesystem if requested.
1876 if (entry.fs_mgr_flags.wait && !WaitForFile(entry.blk_device, 20s)) {
1877 LERROR << "Skipping mounting '" << entry.blk_device << "'";
1878 }
1879
1880 auto& mount_point = alt_mount_point.empty() ? entry.mount_point : alt_mount_point;
1881
1882 // Run fsck if needed
1883 prepare_fs_for_mount(entry.blk_device, entry, mount_point);
1884
1885 int ret = __mount(entry.blk_device, mount_point, entry);
1886 if (ret) {
1887 ret = (errno == EBUSY) ? FS_MGR_DOMNT_BUSY : FS_MGR_DOMNT_FAILED;
1888 }
1889
1890 return ret;
1891 }
1892
1893 // If tmp_mount_point is non-null, mount the filesystem there. This is for the
1894 // tmp mount we do to check the user password
1895 // If multiple fstab entries are to be mounted on "n_name", it will try to mount each one
1896 // in turn, and stop on 1st success, or no more match.
fs_mgr_do_mount_helper(Fstab * fstab,const std::string & n_name,const std::string & n_blk_device,const char * tmp_mount_point,int needs_checkpoint,bool metadata_encrypted)1897 static int fs_mgr_do_mount_helper(Fstab* fstab, const std::string& n_name,
1898 const std::string& n_blk_device, const char* tmp_mount_point,
1899 int needs_checkpoint, bool metadata_encrypted) {
1900 int mount_errors = 0;
1901 int first_mount_errno = 0;
1902 std::string mount_point;
1903 CheckpointManager checkpoint_manager(needs_checkpoint, metadata_encrypted);
1904 AvbUniquePtr avb_handle(nullptr);
1905
1906 if (!fstab) {
1907 return FS_MGR_DOMNT_FAILED;
1908 }
1909
1910 for (auto& fstab_entry : *fstab) {
1911 if (!fs_match(fstab_entry.mount_point, n_name)) {
1912 continue;
1913 }
1914
1915 // We found our match.
1916 // If this swap or a raw partition, report an error.
1917 if (fstab_entry.fs_type == "swap" || fstab_entry.fs_type == "emmc" ||
1918 fstab_entry.fs_type == "mtd") {
1919 LERROR << "Cannot mount filesystem of type " << fstab_entry.fs_type << " on "
1920 << n_blk_device;
1921 return FS_MGR_DOMNT_FAILED;
1922 }
1923
1924 if (fstab_entry.fs_mgr_flags.logical) {
1925 if (!fs_mgr_update_logical_partition(&fstab_entry)) {
1926 LERROR << "Could not set up logical partition, skipping!";
1927 continue;
1928 }
1929 }
1930
1931 WrapUserdataIfNeeded(&fstab_entry, n_blk_device);
1932
1933 if (!checkpoint_manager.Update(&fstab_entry, n_blk_device)) {
1934 LERROR << "Could not set up checkpoint partition, skipping!";
1935 continue;
1936 }
1937
1938 // First check the filesystem if requested.
1939 if (fstab_entry.fs_mgr_flags.wait && !WaitForFile(n_blk_device, 20s)) {
1940 LERROR << "Skipping mounting '" << n_blk_device << "'";
1941 continue;
1942 }
1943
1944 // Now mount it where requested */
1945 if (tmp_mount_point) {
1946 mount_point = tmp_mount_point;
1947 } else {
1948 mount_point = fstab_entry.mount_point;
1949 }
1950
1951 int fs_stat = prepare_fs_for_mount(n_blk_device, fstab_entry, mount_point);
1952
1953 if (fstab_entry.fs_mgr_flags.avb) {
1954 if (!avb_handle) {
1955 avb_handle = AvbHandle::Open();
1956 if (!avb_handle) {
1957 LERROR << "Failed to open AvbHandle";
1958 return FS_MGR_DOMNT_FAILED;
1959 }
1960 }
1961 if (avb_handle->SetUpAvbHashtree(&fstab_entry, true /* wait_for_verity_dev */) ==
1962 AvbHashtreeResult::kFail) {
1963 LERROR << "Failed to set up AVB on partition: " << fstab_entry.mount_point
1964 << ", skipping!";
1965 // Skips mounting the device.
1966 continue;
1967 }
1968 } else if (!fstab_entry.avb_keys.empty()) {
1969 if (AvbHandle::SetUpStandaloneAvbHashtree(&fstab_entry) == AvbHashtreeResult::kFail) {
1970 LERROR << "Failed to set up AVB on standalone partition: "
1971 << fstab_entry.mount_point << ", skipping!";
1972 // Skips mounting the device.
1973 continue;
1974 }
1975 } else if (fstab_entry.fs_mgr_flags.verify) {
1976 int rc = fs_mgr_setup_verity(&fstab_entry, true);
1977 if (rc == FS_MGR_SETUP_VERITY_DISABLED || rc == FS_MGR_SETUP_VERITY_SKIPPED) {
1978 LINFO << "Verity disabled";
1979 } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
1980 LERROR << "Could not set up verified partition, skipping!";
1981 continue;
1982 }
1983 }
1984
1985 int retry_count = 2;
1986 while (retry_count-- > 0) {
1987 if (!__mount(n_blk_device, mount_point, fstab_entry)) {
1988 fs_stat &= ~FS_STAT_FULL_MOUNT_FAILED;
1989 return FS_MGR_DOMNT_SUCCESS;
1990 } else {
1991 if (retry_count <= 0) break; // run check_fs only once
1992 if (!first_mount_errno) first_mount_errno = errno;
1993 mount_errors++;
1994 fs_stat |= FS_STAT_FULL_MOUNT_FAILED;
1995 // try again after fsck
1996 check_fs(n_blk_device, fstab_entry.fs_type, mount_point, &fs_stat);
1997 }
1998 }
1999 log_fs_stat(fstab_entry.blk_device, fs_stat);
2000 }
2001
2002 // Reach here means the mount attempt fails.
2003 if (mount_errors) {
2004 PERROR << "Cannot mount filesystem on " << n_blk_device << " at " << mount_point;
2005 if (first_mount_errno == EBUSY) return FS_MGR_DOMNT_BUSY;
2006 } else {
2007 // We didn't find a match, say so and return an error.
2008 LERROR << "Cannot find mount point " << n_name << " in fstab";
2009 }
2010 return FS_MGR_DOMNT_FAILED;
2011 }
2012
fs_mgr_do_mount(Fstab * fstab,const char * n_name,char * n_blk_device,char * tmp_mount_point)2013 int fs_mgr_do_mount(Fstab* fstab, const char* n_name, char* n_blk_device, char* tmp_mount_point) {
2014 return fs_mgr_do_mount_helper(fstab, n_name, n_blk_device, tmp_mount_point, -1, false);
2015 }
2016
fs_mgr_do_mount(Fstab * fstab,const char * n_name,char * n_blk_device,char * tmp_mount_point,bool needs_checkpoint,bool metadata_encrypted)2017 int fs_mgr_do_mount(Fstab* fstab, const char* n_name, char* n_blk_device, char* tmp_mount_point,
2018 bool needs_checkpoint, bool metadata_encrypted) {
2019 return fs_mgr_do_mount_helper(fstab, n_name, n_blk_device, tmp_mount_point, needs_checkpoint,
2020 metadata_encrypted);
2021 }
2022
2023 /*
2024 * mount a tmpfs filesystem at the given point.
2025 * return 0 on success, non-zero on failure.
2026 */
fs_mgr_do_tmpfs_mount(const char * n_name)2027 int fs_mgr_do_tmpfs_mount(const char *n_name)
2028 {
2029 int ret;
2030
2031 ret = mount("tmpfs", n_name, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV | MS_NOEXEC,
2032 CRYPTO_TMPFS_OPTIONS);
2033 if (ret < 0) {
2034 LERROR << "Cannot mount tmpfs filesystem at " << n_name;
2035 return -1;
2036 }
2037
2038 /* Success */
2039 return 0;
2040 }
2041
InstallZramDevice(const std::string & device)2042 static bool InstallZramDevice(const std::string& device) {
2043 if (!android::base::WriteStringToFile(device, ZRAM_BACK_DEV)) {
2044 PERROR << "Cannot write " << device << " in: " << ZRAM_BACK_DEV;
2045 return false;
2046 }
2047 LINFO << "Success to set " << device << " to " << ZRAM_BACK_DEV;
2048 return true;
2049 }
2050
PrepareZramBackingDevice(off64_t size)2051 static bool PrepareZramBackingDevice(off64_t size) {
2052
2053 constexpr const char* file_path = "/data/per_boot/zram_swap";
2054 if (size == 0) return true;
2055
2056 // Prepare target path
2057 unique_fd target_fd(TEMP_FAILURE_RETRY(open(file_path, O_RDWR | O_CREAT | O_CLOEXEC, 0600)));
2058 if (target_fd.get() == -1) {
2059 PERROR << "Cannot open target path: " << file_path;
2060 return false;
2061 }
2062 if (fallocate(target_fd.get(), 0, 0, size) < 0) {
2063 PERROR << "Cannot truncate target path: " << file_path;
2064 return false;
2065 }
2066
2067 // Allocate loop device and attach it to file_path.
2068 LoopControl loop_control;
2069 std::string loop_device;
2070 if (!loop_control.Attach(target_fd.get(), 5s, &loop_device)) {
2071 return false;
2072 }
2073
2074 ConfigureQueueDepth(loop_device, "/");
2075
2076 // set block size & direct IO
2077 unique_fd loop_fd(TEMP_FAILURE_RETRY(open(loop_device.c_str(), O_RDWR | O_CLOEXEC)));
2078 if (loop_fd.get() == -1) {
2079 PERROR << "Cannot open " << loop_device;
2080 return false;
2081 }
2082 if (!LoopControl::EnableDirectIo(loop_fd.get())) {
2083 return false;
2084 }
2085
2086 return InstallZramDevice(loop_device);
2087 }
2088
fs_mgr_swapon_all(const Fstab & fstab)2089 bool fs_mgr_swapon_all(const Fstab& fstab) {
2090 bool ret = true;
2091 for (const auto& entry : fstab) {
2092 // Skip non-swap entries.
2093 if (entry.fs_type != "swap") {
2094 continue;
2095 }
2096
2097 if (entry.zram_size > 0) {
2098 if (!PrepareZramBackingDevice(entry.zram_backingdev_size)) {
2099 LERROR << "Failure of zram backing device file for '" << entry.blk_device << "'";
2100 }
2101 // A zram_size was specified, so we need to configure the
2102 // device. There is no point in having multiple zram devices
2103 // on a system (all the memory comes from the same pool) so
2104 // we can assume the device number is 0.
2105 if (entry.max_comp_streams >= 0) {
2106 auto zram_mcs_fp = std::unique_ptr<FILE, decltype(&fclose)>{
2107 fopen(ZRAM_CONF_MCS, "re"), fclose};
2108 if (zram_mcs_fp == nullptr) {
2109 LERROR << "Unable to open zram conf comp device " << ZRAM_CONF_MCS;
2110 ret = false;
2111 continue;
2112 }
2113 fprintf(zram_mcs_fp.get(), "%d\n", entry.max_comp_streams);
2114 }
2115
2116 auto zram_fp =
2117 std::unique_ptr<FILE, decltype(&fclose)>{fopen(ZRAM_CONF_DEV, "re+"), fclose};
2118 if (zram_fp == nullptr) {
2119 LERROR << "Unable to open zram conf device " << ZRAM_CONF_DEV;
2120 ret = false;
2121 continue;
2122 }
2123 fprintf(zram_fp.get(), "%" PRId64 "\n", entry.zram_size);
2124 }
2125
2126 if (entry.fs_mgr_flags.wait && !WaitForFile(entry.blk_device, 20s)) {
2127 LERROR << "Skipping mkswap for '" << entry.blk_device << "'";
2128 ret = false;
2129 continue;
2130 }
2131
2132 // Initialize the swap area.
2133 const char* mkswap_argv[2] = {
2134 MKSWAP_BIN,
2135 entry.blk_device.c_str(),
2136 };
2137 int err = logwrap_fork_execvp(ARRAY_SIZE(mkswap_argv), mkswap_argv, nullptr, false,
2138 LOG_KLOG, false, nullptr);
2139 if (err) {
2140 LERROR << "mkswap failed for " << entry.blk_device;
2141 ret = false;
2142 continue;
2143 }
2144
2145 /* If -1, then no priority was specified in fstab, so don't set
2146 * SWAP_FLAG_PREFER or encode the priority */
2147 int flags = 0;
2148 if (entry.swap_prio >= 0) {
2149 flags = (entry.swap_prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK;
2150 flags |= SWAP_FLAG_PREFER;
2151 } else {
2152 flags = 0;
2153 }
2154 err = swapon(entry.blk_device.c_str(), flags);
2155 if (err) {
2156 LERROR << "swapon failed for " << entry.blk_device;
2157 ret = false;
2158 }
2159 }
2160
2161 return ret;
2162 }
2163
fs_mgr_is_verity_enabled(const FstabEntry & entry)2164 bool fs_mgr_is_verity_enabled(const FstabEntry& entry) {
2165 if (!entry.fs_mgr_flags.verify && !entry.fs_mgr_flags.avb) {
2166 return false;
2167 }
2168
2169 DeviceMapper& dm = DeviceMapper::Instance();
2170
2171 std::string mount_point = GetVerityDeviceName(entry);
2172 if (dm.GetState(mount_point) == DmDeviceState::INVALID) {
2173 return false;
2174 }
2175
2176 const char* status;
2177 std::vector<DeviceMapper::TargetInfo> table;
2178 if (!dm.GetTableStatus(mount_point, &table) || table.empty() || table[0].data.empty()) {
2179 if (!entry.fs_mgr_flags.verify_at_boot) {
2180 return false;
2181 }
2182 status = "V";
2183 } else {
2184 status = table[0].data.c_str();
2185 }
2186
2187 if (*status == 'C' || *status == 'V') {
2188 return true;
2189 }
2190
2191 return false;
2192 }
2193
fs_mgr_get_hashtree_algorithm(const android::fs_mgr::FstabEntry & entry)2194 std::string fs_mgr_get_hashtree_algorithm(const android::fs_mgr::FstabEntry& entry) {
2195 if (!entry.fs_mgr_flags.verify && !entry.fs_mgr_flags.avb) {
2196 return "";
2197 }
2198 DeviceMapper& dm = DeviceMapper::Instance();
2199 std::string device = GetVerityDeviceName(entry);
2200
2201 std::vector<DeviceMapper::TargetInfo> table;
2202 if (dm.GetState(device) == DmDeviceState::INVALID || !dm.GetTableInfo(device, &table)) {
2203 return "";
2204 }
2205 for (const auto& target : table) {
2206 if (strcmp(target.spec.target_type, "verity") != 0) {
2207 continue;
2208 }
2209
2210 // The format is stable for dm-verity version 0 & 1. And the data is expected to have
2211 // the fixed format:
2212 // <version> <dev> <hash_dev> <data_block_size> <hash_block_size> <num_data_blocks>
2213 // <hash_start_block> <algorithm> <digest> <salt>
2214 // Details in https://www.kernel.org/doc/html/latest/admin-guide/device-mapper/verity.html
2215
2216 std::vector<std::string> tokens = android::base::Split(target.data, " \t\r\n");
2217 if (tokens[0] != "0" && tokens[0] != "1") {
2218 LOG(WARNING) << "Unrecognized device mapper version in " << target.data;
2219 return "";
2220 }
2221
2222 // Hashtree algorithm is the 8th token in the output
2223 return android::base::Trim(tokens[7]);
2224 }
2225
2226 return "";
2227 }
2228
fs_mgr_verity_is_check_at_most_once(const android::fs_mgr::FstabEntry & entry)2229 bool fs_mgr_verity_is_check_at_most_once(const android::fs_mgr::FstabEntry& entry) {
2230 if (!entry.fs_mgr_flags.verify && !entry.fs_mgr_flags.avb) {
2231 return false;
2232 }
2233
2234 DeviceMapper& dm = DeviceMapper::Instance();
2235 std::string device = GetVerityDeviceName(entry);
2236
2237 std::vector<DeviceMapper::TargetInfo> table;
2238 if (dm.GetState(device) == DmDeviceState::INVALID || !dm.GetTableInfo(device, &table)) {
2239 return false;
2240 }
2241 for (const auto& target : table) {
2242 if (strcmp(target.spec.target_type, "verity") == 0 &&
2243 target.data.find("check_at_most_once") != std::string::npos) {
2244 return true;
2245 }
2246 }
2247 return false;
2248 }
2249
fs_mgr_get_super_partition_name(int slot)2250 std::string fs_mgr_get_super_partition_name(int slot) {
2251 // Devices upgrading to dynamic partitions are allowed to specify a super
2252 // partition name. This includes cuttlefish, which is a non-A/B device.
2253 std::string super_partition;
2254 if (fs_mgr_get_boot_config_from_bootconfig_source("super_partition", &super_partition) ||
2255 fs_mgr_get_boot_config_from_kernel_cmdline("super_partition", &super_partition)) {
2256 if (fs_mgr_get_slot_suffix().empty()) {
2257 return super_partition;
2258 }
2259 std::string suffix;
2260 if (slot == 0) {
2261 suffix = "_a";
2262 } else if (slot == 1) {
2263 suffix = "_b";
2264 } else if (slot == -1) {
2265 suffix = fs_mgr_get_slot_suffix();
2266 }
2267 return super_partition + suffix;
2268 }
2269 return LP_METADATA_DEFAULT_PARTITION_NAME;
2270 }
2271
fs_mgr_create_canonical_mount_point(const std::string & mount_point)2272 bool fs_mgr_create_canonical_mount_point(const std::string& mount_point) {
2273 auto saved_errno = errno;
2274 auto ok = true;
2275 auto created_mount_point = !mkdir(mount_point.c_str(), 0755);
2276 std::string real_mount_point;
2277 if (!Realpath(mount_point, &real_mount_point)) {
2278 ok = false;
2279 PERROR << "failed to realpath(" << mount_point << ")";
2280 } else if (mount_point != real_mount_point) {
2281 ok = false;
2282 LERROR << "mount point is not canonical: realpath(" << mount_point << ") -> "
2283 << real_mount_point;
2284 }
2285 if (!ok && created_mount_point) {
2286 rmdir(mount_point.c_str());
2287 }
2288 errno = saved_errno;
2289 return ok;
2290 }
2291
fs_mgr_mount_overlayfs_fstab_entry(const FstabEntry & entry)2292 bool fs_mgr_mount_overlayfs_fstab_entry(const FstabEntry& entry) {
2293 auto overlayfs_valid_result = fs_mgr_overlayfs_valid();
2294 if (overlayfs_valid_result == OverlayfsValidResult::kNotSupported) {
2295 LERROR << __FUNCTION__ << "(): kernel does not support overlayfs";
2296 return false;
2297 }
2298
2299 #if ALLOW_ADBD_DISABLE_VERITY == 0
2300 // Allowlist the mount point if user build.
2301 static const std::vector<const std::string> kAllowedPaths = {
2302 "/odm", "/odm_dlkm", "/oem", "/product", "/system_ext", "/vendor", "/vendor_dlkm",
2303 };
2304 static const std::vector<const std::string> kAllowedPrefixes = {
2305 "/mnt/product/",
2306 "/mnt/vendor/",
2307 };
2308 if (std::none_of(kAllowedPaths.begin(), kAllowedPaths.end(),
2309 [&entry](const auto& path) -> bool {
2310 return entry.mount_point == path ||
2311 StartsWith(entry.mount_point, path + "/");
2312 }) &&
2313 std::none_of(kAllowedPrefixes.begin(), kAllowedPrefixes.end(),
2314 [&entry](const auto& prefix) -> bool {
2315 return entry.mount_point != prefix &&
2316 StartsWith(entry.mount_point, prefix);
2317 })) {
2318 LERROR << __FUNCTION__
2319 << "(): mount point is forbidden on user build: " << entry.mount_point;
2320 return false;
2321 }
2322 #endif // ALLOW_ADBD_DISABLE_VERITY == 0
2323
2324 if (!fs_mgr_create_canonical_mount_point(entry.mount_point)) {
2325 return false;
2326 }
2327
2328 auto options = "lowerdir=" + entry.lowerdir;
2329 if (overlayfs_valid_result == OverlayfsValidResult::kOverrideCredsRequired) {
2330 options += ",override_creds=off";
2331 }
2332
2333 // Use "overlay-" + entry.blk_device as the mount() source, so that adb-remout-test don't
2334 // confuse this with adb remount overlay, whose device name is "overlay".
2335 // Overlayfs is a pseudo filesystem, so the source device is a symbolic value and isn't used to
2336 // back the filesystem. However the device name would be shown in /proc/mounts.
2337 auto source = "overlay-" + entry.blk_device;
2338 auto report = "__mount(source=" + source + ",target=" + entry.mount_point + ",type=overlay," +
2339 options + ")=";
2340 auto ret = mount(source.c_str(), entry.mount_point.c_str(), "overlay", MS_RDONLY | MS_NOATIME,
2341 options.c_str());
2342 if (ret) {
2343 PERROR << report << ret;
2344 return false;
2345 }
2346 LINFO << report << ret;
2347 return true;
2348 }
2349