Skip to content

Commit a6f4fb2

Browse files
oech3sylvestre
authored andcommitted
rewrite check-open-once.sh in Rust
1 parent 48819c9 commit a6f4fb2

5 files changed

Lines changed: 121 additions & 145 deletions

File tree

‎.github/workflows/CICD.yml‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -998,23 +998,6 @@ jobs:
998998
- name: Run libc interposition verification
999999
run: ./util/check-libc-interposition.sh
10001000

1001-
test_open_once:
1002-
name: Redundant Open Check
1003-
runs-on: ubuntu-latest
1004-
needs: [ min_version, deps ]
1005-
1006-
steps:
1007-
- uses: actions/checkout@v7.0.1
1008-
with:
1009-
persist-credentials: false
1010-
- uses: Swatinem/rust-cache@v2
1011-
- name: Install strace
1012-
run: sudo apt-get update && sudo apt-get install -y strace
1013-
- name: Build utilities for redundant open checks
1014-
run: cargo build --profile=release-small -p uu_cp -p uu_mv
1015-
- name: Run redundant open verification
1016-
run: ./util/check-open-once.sh
1017-
10181001
test_toctou:
10191002
name: TOCTOU Security Check
10201003
runs-on: ubuntu-latest

‎tests/by-util/test_cp.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9681,6 +9681,46 @@ fn test_cp_d_overwrites_existing_symlink_dest() {
96819681
assert!(at.read_symlink("slink").ends_with("slink-target"));
96829682
}
96839683

9684+
#[rstest]
9685+
#[case("--sparse=always")]
9686+
#[case("--sparse=never")]
9687+
#[cfg(target_os = "linux")]
9688+
#[cfg(not(wasi_runner))]
9689+
fn test_cp_open_once(#[case] mode: &str) {
9690+
let (at, mut _ucmd) = at_and_ucmd!();
9691+
let file = "source_file";
9692+
at.touch(file);
9693+
if std::process::Command::new("strace")
9694+
.args([
9695+
"-e",
9696+
"trace=open,openat",
9697+
"-o",
9698+
"strace.out",
9699+
uutests::util::get_tests_binary(),
9700+
"cp",
9701+
"--reflink=never",
9702+
mode,
9703+
file,
9704+
"target_file",
9705+
])
9706+
.current_dir(at.as_string())
9707+
.output()
9708+
.is_err()
9709+
{
9710+
return; // missing strace
9711+
}
9712+
let open_count = at
9713+
.read("strace.out")
9714+
.lines()
9715+
.filter(|l| {
9716+
let trimmed = l.trim();
9717+
(trimmed.starts_with("open(") || trimmed.starts_with("openat("))
9718+
&& (trimmed.contains(file) || trimmed.contains("target_file"))
9719+
})
9720+
.count();
9721+
assert_eq!(open_count, 2);
9722+
}
9723+
96849724
// Regression for GNU tests/cp/acl: `cp -p` must preserve POSIX ACLs on
96859725
// Linux. ACLs are part of GNU's `mode` preservation, not its `xattr`
96869726
// preservation, so the default-no-xattr change in #9704 must not strip

‎tests/by-util/test_mv.rs‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,79 @@ fn test_mv_missing_dest() {
3333
ucmd.arg(dir).fails();
3434
}
3535

36+
#[test]
37+
#[cfg(target_os = "linux")]
38+
#[cfg(not(wasi_runner))]
39+
fn test_mv_no_open_same_file_system() {
40+
let (at, _ucmd) = at_and_ucmd!();
41+
let file = "source_file";
42+
at.touch(file);
43+
if std::process::Command::new("strace")
44+
.args([
45+
"-e",
46+
"trace=open,openat",
47+
"-o",
48+
"strace.out",
49+
uutests::util::get_tests_binary(),
50+
"mv",
51+
file,
52+
"target_file",
53+
])
54+
.current_dir(at.as_string())
55+
.output()
56+
.is_err()
57+
{
58+
return; // missing strace
59+
}
60+
61+
let open_count = at
62+
.read("strace.out")
63+
.lines()
64+
.filter(|l| {
65+
let trimmed = l.trim();
66+
(trimmed.starts_with("open(") || trimmed.starts_with("openat("))
67+
&& (trimmed.contains(file) || trimmed.contains("target_file"))
68+
})
69+
.count();
70+
assert_eq!(open_count, 0);
71+
}
72+
73+
#[test]
74+
#[cfg(target_os = "linux")]
75+
#[cfg(not(wasi_runner))]
76+
fn test_mv_open_once_cross() {
77+
if !uutests::util::unshare_mountable() {
78+
println!("TEST SKIPPED: no unprivileged mount namespace available");
79+
return;
80+
}
81+
let (at, mut _ucmd) = at_and_ucmd!();
82+
let file = "source_file";
83+
at.touch(file);
84+
let coreutils = uutests::util::get_tests_binary();
85+
// make /etc a different FS
86+
if std::process::Command::new("unshare")
87+
.args([
88+
"-rm", "sh", "-c"])
89+
.arg(format!("mount -t tmpfs tmpfs /etc && strace -e trace=open,openat -o strace.out {coreutils} mv {file} /etc/dest"))
90+
.current_dir(at.as_string())
91+
.output()
92+
.is_err()
93+
{
94+
return;
95+
}
96+
97+
let open_count = at
98+
.read("strace.out")
99+
.lines()
100+
.filter(|l| {
101+
let trimmed = l.trim();
102+
(trimmed.starts_with("open(") || trimmed.starts_with("openat("))
103+
&& (trimmed.contains(file) || trimmed.contains("target_file"))
104+
})
105+
.count();
106+
assert_eq!(open_count, 1);
107+
}
108+
36109
#[test]
37110
fn test_mv_rename_dir() {
38111
let (at, mut ucmd) = at_and_ucmd!();

‎tests/uutests/src/lib/util.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,6 +3431,14 @@ pub fn run_ucmd_as_root_with_stdin_stdout(
34313431
}
34323432
}
34333433

3434+
#[cfg(all(target_os = "linux", not(wasi_runner)))]
3435+
pub fn unshare_mountable() -> bool {
3436+
Command::new("unshare")
3437+
.args(["-rm", "mount", "-t", "tmpfs", "tmpfs", "/etc"])
3438+
.status()
3439+
.is_ok_and(|s| s.success())
3440+
}
3441+
34343442
/// Sanity checks for test utils
34353443
#[cfg(test)]
34363444
mod tests {

‎util/check-open-once.sh‎

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)