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