Skip to content

Commit 8f654d8

Browse files
silvernekoAndroid (Google) Code Review
authored andcommitted
Merge changes Iaf2ec527,I6d6abd44,I6304e0de,Ia4fbce58,I3b60dfa4, ... into sc-dev
* changes: first_stage_mount: mount point must be canonical path fs_mgr_fstab: Parse overlayfs options from fs flags Remove deprecated fs_mgr_overlayfs_required_devices() adb-remount-test: Make awk scripts mawk-v1.3.3-compatible Make fs_mgr_overlayfs_mount_fstab_entry() available for user builds adb-remount-test: Strengthen skip_administrative_mounts fs_mgr_overlayfs_mount_fstab_entry(): Rename source device name fs_mgr_overlayfs: Polish fs_mgr_overlayfs_mount_fstab_entry() first_stage_mount: Remove "overlay" hack from InitRequiredDevices() fs_mgr_vendor_overlay: Mount vendor overlay with noatime
2 parents a35d50c + 84fe96b commit 8f654d8

File tree

8 files changed

+155
-60
lines changed

8 files changed

+155
-60
lines changed

fs_mgr/fs_mgr.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,3 +2265,81 @@ std::string fs_mgr_get_super_partition_name(int slot) {
22652265
}
22662266
return LP_METADATA_DEFAULT_PARTITION_NAME;
22672267
}
2268+
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+
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+
}

fs_mgr/fs_mgr_fstab.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,16 @@ void ParseMountFlags(const std::string& flags, FstabEntry* entry) {
127127
}
128128
fs_options.append(flag);
129129

130-
if (entry->fs_type == "f2fs" && StartsWith(flag, "reserve_root=")) {
131-
std::string arg;
132-
if (auto equal_sign = flag.find('='); equal_sign != std::string::npos) {
133-
arg = flag.substr(equal_sign + 1);
134-
}
135-
if (!ParseInt(arg, &entry->reserved_size)) {
136-
LWARNING << "Warning: reserve_root= flag malformed: " << arg;
137-
} else {
138-
entry->reserved_size <<= 12;
130+
if (auto equal_sign = flag.find('='); equal_sign != std::string::npos) {
131+
const auto arg = flag.substr(equal_sign + 1);
132+
if (entry->fs_type == "f2fs" && StartsWith(flag, "reserve_root=")) {
133+
if (!ParseInt(arg, &entry->reserved_size)) {
134+
LWARNING << "Warning: reserve_root= flag malformed: " << arg;
135+
} else {
136+
entry->reserved_size <<= 12;
137+
}
138+
} else if (StartsWith(flag, "lowerdir=")) {
139+
entry->lowerdir = std::move(arg);
139140
}
140141
}
141142
}
@@ -298,8 +299,6 @@ void ParseFsMgrFlags(const std::string& flags, FstabEntry* entry) {
298299
if (!ParseByteCount(arg, &entry->zram_backingdev_size)) {
299300
LWARNING << "Warning: zram_backingdev_size= flag malformed: " << arg;
300301
}
301-
} else if (StartsWith(flag, "lowerdir=")) {
302-
entry->lowerdir = arg;
303302
} else {
304303
LWARNING << "Warning: unknown flag: " << flag;
305304
}

fs_mgr/fs_mgr_overlayfs.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,6 @@ bool fs_mgr_overlayfs_mount_all(Fstab*) {
9292
return false;
9393
}
9494

95-
bool fs_mgr_overlayfs_mount_fstab_entry(const std::string&, const std::string&) {
96-
return false;
97-
}
98-
99-
std::vector<std::string> fs_mgr_overlayfs_required_devices(Fstab*) {
100-
return {};
101-
}
102-
10395
bool fs_mgr_overlayfs_setup(const char*, const char*, bool* change, bool) {
10496
if (change) *change = false;
10597
return false;
@@ -1299,18 +1291,6 @@ static void TryMountScratch() {
12991291
}
13001292
}
13011293

1302-
bool fs_mgr_overlayfs_mount_fstab_entry(const std::string& lowers,
1303-
const std::string& mount_point) {
1304-
if (fs_mgr_overlayfs_invalid()) return false;
1305-
1306-
std::string aux = "lowerdir=" + lowers + ",override_creds=off";
1307-
auto rc = mount("overlay", mount_point.c_str(), "overlay", MS_RDONLY | MS_NOATIME, aux.c_str());
1308-
1309-
if (rc == 0) return true;
1310-
1311-
return false;
1312-
}
1313-
13141294
bool fs_mgr_overlayfs_mount_all(Fstab* fstab) {
13151295
auto ret = false;
13161296
if (fs_mgr_overlayfs_invalid()) return ret;

fs_mgr/fs_mgr_vendor_overlay.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool fs_mgr_vendor_overlay_mount(const std::pair<std::string, std::string>& moun
9292
}
9393
auto report = "__mount(source=overlay,target="s + vendor_mount_point + ",type=overlay," +
9494
options + ")=";
95-
auto ret = mount("overlay", vendor_mount_point.c_str(), "overlay", MS_RDONLY | MS_RELATIME,
95+
auto ret = mount("overlay", vendor_mount_point.c_str(), "overlay", MS_RDONLY | MS_NOATIME,
9696
options.c_str());
9797
if (ret) {
9898
PERROR << report << ret;

fs_mgr/include/fs_mgr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,12 @@ int fs_mgr_remount_userdata_into_checkpointing(android::fs_mgr::Fstab* fstab);
131131
// Finds the dm_bow device on which this block device is stacked, or returns
132132
// empty string
133133
std::string fs_mgr_find_bow_device(const std::string& block_device);
134+
135+
// Creates mount point if not already existed, and checks that mount point is a
136+
// canonical path that doesn't contain any symbolic link or /../.
137+
bool fs_mgr_create_canonical_mount_point(const std::string& mount_point);
138+
139+
// Like fs_mgr_do_mount_one() but for overlayfs fstab entries.
140+
// Unlike fs_mgr_overlayfs, mount overlayfs without upperdir and workdir, so the
141+
// filesystem cannot be remount read-write.
142+
bool fs_mgr_mount_overlayfs_fstab_entry(const android::fs_mgr::FstabEntry& entry);

fs_mgr/include/fs_mgr_overlayfs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
android::fs_mgr::Fstab fs_mgr_overlayfs_candidate_list(const android::fs_mgr::Fstab& fstab);
2828

2929
bool fs_mgr_overlayfs_mount_all(android::fs_mgr::Fstab* fstab);
30-
bool fs_mgr_overlayfs_mount_fstab_entry (const std::string& lowers, const std::string& mount_point);
31-
std::vector<std::string> fs_mgr_overlayfs_required_devices(android::fs_mgr::Fstab* fstab);
3230
bool fs_mgr_overlayfs_setup(const char* backing = nullptr, const char* mount_point = nullptr,
3331
bool* change = nullptr, bool force = true);
3432
bool fs_mgr_overlayfs_teardown(const char* mount_point = nullptr, bool* change = nullptr);

fs_mgr/tests/adb-remount-test.sh

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -735,23 +735,46 @@ check_ne() {
735735
fi
736736
}
737737

738+
[ "USAGE: join_with <delimiter> <strings>
739+
740+
Joins strings with delimiter" ]
741+
join_with() {
742+
if [ "${#}" -lt 2 ]; then
743+
echo
744+
return
745+
fi
746+
local delimiter="${1}"
747+
local result="${2}"
748+
shift 2
749+
for element in "${@}"; do
750+
result+="${delimiter}${element}"
751+
done
752+
echo "${result}"
753+
}
754+
738755
[ "USAGE: skip_administrative_mounts [data] < /proc/mounts
739756
740757
Filters out all administrative (eg: sysfs) mounts uninteresting to the test" ]
741758
skip_administrative_mounts() {
759+
local exclude_filesystems=(
760+
"overlay" "tmpfs" "none" "sysfs" "proc" "selinuxfs" "debugfs" "bpf"
761+
"binfmt_misc" "cg2_bpf" "pstore" "tracefs" "adb" "mtp" "ptp" "devpts"
762+
"ramdumpfs" "binder" "securityfs" "functionfs" "rootfs"
763+
)
764+
local exclude_devices=(
765+
"\/sys\/kernel\/debug" "\/data\/media" "\/dev\/block\/loop[0-9]*"
766+
"${exclude_filesystems[@]}"
767+
)
768+
local exclude_mount_points=(
769+
"\/cache" "\/mnt\/scratch" "\/mnt\/vendor\/persist" "\/persist"
770+
"\/metadata"
771+
)
742772
if [ "data" = "${1}" ]; then
743-
grep -v " /data "
744-
else
745-
cat -
746-
fi |
747-
grep -v \
748-
-e "^\(overlay\|tmpfs\|none\|sysfs\|proc\|selinuxfs\|debugfs\|bpf\) " \
749-
-e "^\(binfmt_misc\|cg2_bpf\|pstore\|tracefs\|adb\|mtp\|ptp\|devpts\) " \
750-
-e "^\(ramdumpfs\|binder\|/sys/kernel/debug\|securityfs\) " \
751-
-e " functionfs " \
752-
-e "^\(/data/media\|/dev/block/loop[0-9]*\) " \
753-
-e "^rootfs / rootfs rw," \
754-
-e " /\(cache\|mnt/scratch\|mnt/vendor/persist\|persist\|metadata\) "
773+
exclude_mount_points+=("\/data")
774+
fi
775+
awk '$1 !~ /^('"$(join_with "|" "${exclude_devices[@]}")"')$/ &&
776+
$2 !~ /^('"$(join_with "|" "${exclude_mount_points[@]}")"')$/ &&
777+
$3 !~ /^('"$(join_with "|" "${exclude_filesystems[@]}")"')$/'
755778
}
756779

757780
[ "USAGE: skip_unrelated_mounts < /proc/mounts
@@ -907,9 +930,11 @@ ACTIVE_SLOT=`get_active_slot`
907930

908931
# Acquire list of system partitions
909932

933+
# KISS (assume system partition mount point is "/<partition name>")
910934
PARTITIONS=`adb_su cat /vendor/etc/fstab* </dev/null |
935+
grep -v "^[#${SPACE}${TAB}]" |
911936
skip_administrative_mounts |
912-
sed -n "s@^\([^ ${TAB}/][^ ${TAB}/]*\)[ ${TAB}].*[, ${TAB}]ro[, ${TAB}].*@\1@p" |
937+
awk '$1 ~ /^[^\/]+$/ && "/"$1 == $2 && $4 ~ /(^|,)ro(,|$)/ { print $1 }' |
913938
sort -u |
914939
tr '\n' ' '`
915940
PARTITIONS="${PARTITIONS:-system vendor}"

init/first_stage_mount.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,6 @@ bool FirstStageMount::InitRequiredDevices(std::set<std::string> devices) {
331331
if (devices.empty()) {
332332
return true;
333333
}
334-
// excluding overlays
335-
for (auto iter = devices.begin(); iter != devices.end(); ) {
336-
if (*iter=="overlay") iter = devices.erase(iter);
337-
else iter++;
338-
}
339-
340334
return block_dev_init_.InitDevices(std::move(devices));
341335
}
342336

@@ -426,6 +420,10 @@ bool FirstStageMount::MountPartition(const Fstab::iterator& begin, bool erase_sa
426420
*end = begin + 1;
427421
}
428422

423+
if (!fs_mgr_create_canonical_mount_point(begin->mount_point)) {
424+
return false;
425+
}
426+
429427
if (begin->fs_mgr_flags.logical) {
430428
if (!fs_mgr_update_logical_partition(&(*begin))) {
431429
return false;
@@ -548,6 +546,7 @@ bool FirstStageMount::MountPartitions() {
548546
continue;
549547
}
550548

549+
// Handle overlayfs entries later.
551550
if (current->fs_type == "overlay") {
552551
++current;
553552
continue;
@@ -577,6 +576,12 @@ bool FirstStageMount::MountPartitions() {
577576
current = end;
578577
}
579578

579+
for (const auto& entry : fstab_) {
580+
if (entry.fs_type == "overlay") {
581+
fs_mgr_mount_overlayfs_fstab_entry(entry);
582+
}
583+
}
584+
580585
// If we don't see /system or / in the fstab, then we need to create an root entry for
581586
// overlayfs.
582587
if (!GetEntryForMountPoint(&fstab_, "/system") && !GetEntryForMountPoint(&fstab_, "/")) {
@@ -602,13 +607,6 @@ bool FirstStageMount::MountPartitions() {
602607
};
603608
MapScratchPartitionIfNeeded(&fstab_, init_devices);
604609

605-
for (auto current = fstab_.begin(); current != fstab_.end(); ) {
606-
if (current->fs_type == "overlay") {
607-
fs_mgr_overlayfs_mount_fstab_entry(current->lowerdir, current->mount_point);
608-
}
609-
++current;
610-
}
611-
612610
fs_mgr_overlayfs_mount_all(&fstab_);
613611

614612
return true;
@@ -695,6 +693,10 @@ bool FirstStageMountVBootV1::GetDmVerityDevices(std::set<std::string>* devices)
695693
// Includes the partition names of fstab records.
696694
// Notes that fstab_rec->blk_device has A/B suffix updated by fs_mgr when A/B is used.
697695
for (const auto& fstab_entry : fstab_) {
696+
// Skip pseudo filesystems.
697+
if (fstab_entry.fs_type == "overlay") {
698+
continue;
699+
}
698700
if (!fstab_entry.fs_mgr_flags.logical) {
699701
devices->emplace(basename(fstab_entry.blk_device.c_str()));
700702
}
@@ -757,6 +759,10 @@ bool FirstStageMountVBootV2::GetDmVerityDevices(std::set<std::string>* devices)
757759
if (fstab_entry.fs_mgr_flags.avb) {
758760
need_dm_verity_ = true;
759761
}
762+
// Skip pseudo filesystems.
763+
if (fstab_entry.fs_type == "overlay") {
764+
continue;
765+
}
760766
if (fstab_entry.fs_mgr_flags.logical) {
761767
// Don't try to find logical partitions via uevent regeneration.
762768
logical_partitions.emplace(basename(fstab_entry.blk_device.c_str()));

0 commit comments

Comments
 (0)