Skip to content

Commit 84fe96b

Browse files
committed
first_stage_mount: mount point must be canonical path
Ban weird paths such as /../system or //vendor in first stage mount. Add utility function fs_mgr_create_canonical_mount_point() that: * mkdir(mount_point) to ensure mount_point's existence * Test that realpath(mount_point) =?= mount_point Bug: 188898525 Test: Presubmit Test: Boot CF Change-Id: Iaf2ec52701277f26cc81f3e15a47b6083a788334 Merged-In: Iaf2ec52701277f26cc81f3e15a47b6083a788334 (cherry picked from commit 3431d52)
1 parent a79bc0d commit 84fe96b

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

fs_mgr/fs_mgr.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,26 @@ std::string fs_mgr_get_super_partition_name(int slot) {
22662266
return LP_METADATA_DEFAULT_PARTITION_NAME;
22672267
}
22682268

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+
22692289
bool fs_mgr_mount_overlayfs_fstab_entry(const FstabEntry& entry) {
22702290
auto overlayfs_valid_result = fs_mgr_overlayfs_valid();
22712291
if (overlayfs_valid_result == OverlayfsValidResult::kNotSupported) {
@@ -2298,18 +2318,7 @@ bool fs_mgr_mount_overlayfs_fstab_entry(const FstabEntry& entry) {
22982318
}
22992319
#endif // ALLOW_ADBD_DISABLE_VERITY == 0
23002320

2301-
// Create the mount point in case it doesn't exist.
2302-
mkdir(entry.mount_point.c_str(), 0755);
2303-
2304-
// Ensure that mount point exists and doesn't contain symbolic link or /../.
2305-
std::string mount_point;
2306-
if (!Realpath(entry.mount_point, &mount_point)) {
2307-
PERROR << __FUNCTION__ << "(): failed to realpath " << entry.mount_point;
2308-
return false;
2309-
}
2310-
if (entry.mount_point != mount_point) {
2311-
LERROR << __FUNCTION__ << "(): mount point must be a canonicalized path: realpath "
2312-
<< entry.mount_point << " = " << mount_point;
2321+
if (!fs_mgr_create_canonical_mount_point(entry.mount_point)) {
23132322
return false;
23142323
}
23152324

fs_mgr/include/fs_mgr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ int fs_mgr_remount_userdata_into_checkpointing(android::fs_mgr::Fstab* fstab);
132132
// empty string
133133
std::string fs_mgr_find_bow_device(const std::string& block_device);
134134

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+
135139
// Like fs_mgr_do_mount_one() but for overlayfs fstab entries.
136140
// Unlike fs_mgr_overlayfs, mount overlayfs without upperdir and workdir, so the
137141
// filesystem cannot be remount read-write.

init/first_stage_mount.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ bool FirstStageMount::MountPartition(const Fstab::iterator& begin, bool erase_sa
420420
*end = begin + 1;
421421
}
422422

423+
if (!fs_mgr_create_canonical_mount_point(begin->mount_point)) {
424+
return false;
425+
}
426+
423427
if (begin->fs_mgr_flags.logical) {
424428
if (!fs_mgr_update_logical_partition(&(*begin))) {
425429
return false;

0 commit comments

Comments
 (0)