Skip to content

fix uucore/fsext/MountInfo::new mount_dir not unescaped #5754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/uucore/src/lib/features/fsext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,22 @@ impl MountInfo {
dev_name = raw[after_fields + 1].to_string();
fs_type = raw[after_fields].to_string();
mount_root = raw[3].to_string();
mount_dir = raw[4].to_string();
mount_dir = raw[4]
.to_string()
.replace(r#"\040"#, " ")
.replace(r#"\011"#, " ")
.replace(r#"\134"#, r#"\"#);
Comment on lines +163 to +165
Copy link
Contributor

@sylvestre sylvestre Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.replace(r#"\040"#, " ")
.replace(r#"\011"#, " ")
.replace(r#"\134"#, r#"\"#);
// Replace ASCII space with a regular space character
.replace(r#"\040"#, " ")
// Replace \011 ASCII horizontal tab with a tab character
.replace(r#"\011"#, " ")
// Replace ASCII backslash with an actual backslash character
.replace(r#"\134"#, r#"\"#);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it should be moved into a function

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yykz did you see this comment? thanks :)

mount_option = raw[5].to_string();
}
LINUX_MTAB => {
dev_name = raw[0].to_string();
fs_type = raw[2].to_string();
mount_root = String::new();
mount_dir = raw[1].to_string();
mount_dir = raw[1]
.to_string()
.replace(r#"\040"#, " ")
.replace(r#"\011"#, " ")
.replace(r#"\134"#, r#"\"#);
mount_option = raw[3].to_string();
}
_ => return None,
Expand Down Expand Up @@ -1089,4 +1097,28 @@ mod tests {
assert_eq!(info.fs_type, "xfs");
assert_eq!(info.dev_name, "/dev/fs0");
}

#[test]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn test_mountinfo_dir_special_chars() {
let info = MountInfo::new(
LINUX_MOUNTINFO,
&r#"317 61 7:0 / /mnt/f\134\040\011oo rw,relatime shared:641 - ext4 /dev/loop0 rw"#
.split_ascii_whitespace()
.collect::<Vec<_>>(),
)
.unwrap();

assert_eq!(info.mount_dir, r#"/mnt/f\ oo"#);

let info = MountInfo::new(
LINUX_MTAB,
&r#"/dev/loop0 /mnt/f\134\040\011oo ext4 rw,relatime 0 0"#
.split_ascii_whitespace()
.collect::<Vec<_>>(),
)
.unwrap();

assert_eq!(info.mount_dir, r#"/mnt/f\ oo"#);
}
}
Loading