Skip to content

Commit 51bce00

Browse files
unsound-mintsylvestre
authored andcommitted
df: only match an operand against mount sources when it is a device
mount_info_from_path() canonicalized the source of every mount entry to see whether the operand names a device. That is wrong for any operand that is not one: sources like "tmpfs" are labels that canonicalize() resolved against the working directory, and a mount whose source is "/" captured `df /`. With a symlink named tmpfs pointing at / in the current directory, `df /` reported the tmpfs mounted on /dev/shm. Try the device match only when the operand is a block or character device; otherwise the mount directory decides. Sources that are not absolute paths are still skipped. Closes #15758
1 parent 0222fd3 commit 51bce00

2 files changed

Lines changed: 60 additions & 20 deletions

File tree

‎src/uu/df/src/filesystem.rs‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use std::ffi::OsString;
1515
#[cfg(unix)]
16+
use std::os::unix::fs::FileTypeExt;
17+
#[cfg(unix)]
1618
use std::path::Path;
1719

1820
use uucore::fsext::{FsUsage, MountInfo};
@@ -89,18 +91,32 @@ where
8991
path.as_ref().to_path_buf()
9092
};
9193

92-
// Find the potential mount point that matches entered path
93-
let maybe_mount_point = mounts
94-
.iter()
95-
// Create pair MountInfo, canonicalized device name
96-
// TODO Abstract from accessing real filesystem to
97-
// make code more testable
98-
.map(|m| (m, std::fs::canonicalize(&m.dev_name)))
99-
// Ignore non existing paths
100-
.filter_map(|m| m.1.ok().map(|m1| (m.0, m1)))
101-
// Try to find canonicalized device name corresponding to entered path
102-
.find(|m| m.1.eq(&path))
103-
.map(|m| m.0);
94+
// Match on the device only when the operand is one (`df /dev/sda1`).
95+
// For any other file the mount directory decides: a mount whose source is
96+
// `/` or a symlink to a directory must not capture `df /`.
97+
let is_device = std::fs::metadata(&path).is_ok_and(|m| {
98+
let file_type = m.file_type();
99+
file_type.is_block_device() || file_type.is_char_device()
100+
});
101+
let maybe_mount_point = if is_device {
102+
mounts
103+
.iter()
104+
// Only absolute sources are device paths. Others, like "tmpfs" or
105+
// "proc", are labels, and canonicalizing them would resolve a file
106+
// of that name in the working directory.
107+
.filter(|m| Path::new(&m.dev_name).is_absolute())
108+
// Create pair MountInfo, canonicalized device name
109+
// TODO Abstract from accessing real filesystem to
110+
// make code more testable
111+
.map(|m| (m, std::fs::canonicalize(&m.dev_name)))
112+
// Ignore non existing paths
113+
.filter_map(|m| m.1.ok().map(|m1| (m.0, m1)))
114+
// Try to find canonicalized device name corresponding to entered path
115+
.find(|m| m.1.eq(&path))
116+
.map(|m| m.0)
117+
} else {
118+
None
119+
};
104120

105121
maybe_mount_point
106122
.or_else(|| {
@@ -255,16 +271,22 @@ mod tests {
255271

256272
#[test]
257273
fn test_dev_name_match() {
258-
let tmp = tempfile::TempDir::new().expect("Failed to create temp dir");
259-
let dev_name = std::fs::canonicalize(tmp.path())
260-
.expect("Failed to canonicalize tmp path")
261-
.to_string_lossy()
262-
.to_string();
263-
274+
// /dev/null is a character device on every unix we build for.
264275
let mut mount_info = mount_info("/foo");
265-
mount_info.dev_name = dev_name.clone();
276+
mount_info.dev_name = "/dev/null".to_string();
266277
let mounts = [mount_info];
267-
let actual = mount_info_from_path(&mounts, dev_name, false).unwrap();
278+
let actual = mount_info_from_path(&mounts, "/dev/null", false).unwrap();
279+
assert!(mount_info_eq(actual, &mounts[0]));
280+
}
281+
282+
#[test]
283+
fn test_directory_operand_ignores_dev_name() {
284+
// A mount whose source is "/" (a bind mount or a btrfs subvolume)
285+
// must not capture a directory operand through its device name.
286+
let mut bound = mount_info("/mnt/bound");
287+
bound.dev_name = "/".to_string();
288+
let mounts = [mount_info("/"), bound];
289+
let actual = mount_info_from_path(&mounts, "/", false).unwrap();
268290
assert!(mount_info_eq(actual, &mounts[0]));
269291
}
270292
}

‎tests/by-util/test_df.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ fn test_df_follows_symlinks() {
155155
);
156156
}
157157

158+
// `at_and_ucmd` is not imported on FreeBSD or Windows.
159+
#[cfg(all(unix, not(target_os = "freebsd")))]
160+
#[test]
161+
fn test_df_operand_is_not_matched_by_a_file_named_like_a_mount_source() {
162+
// Mount sources such as "tmpfs" are labels. A file of that name in the
163+
// working directory must not stand in for the operand.
164+
let (at, mut ucmd) = at_and_ucmd!();
165+
at.symlink_dir("/", "tmpfs");
166+
167+
let output = ucmd
168+
.arg("--output=target")
169+
.arg("/")
170+
.succeeds()
171+
.stdout_str_lossy();
172+
173+
assert_eq!(output.lines().nth(1).expect("df printed no row"), "/");
174+
}
175+
158176
#[test]
159177
fn test_df_trailing_zeros() {
160178
use regex::Regex;

0 commit comments

Comments
 (0)