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