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