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