Skip to content

Commit 5b0b595

Browse files
committed
rewrite check-open-once.sh in Rust
1 parent 9cc868b commit 5b0b595

4 files changed

Lines changed: 119 additions & 145 deletions

File tree

‎.github/workflows/CICD.yml‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,23 +1011,6 @@ jobs:
10111011
- name: Run libc interposition verification
10121012
run: ./util/check-libc-interposition.sh
10131013

1014-
test_open_once:
1015-
name: Redundant Open Check
1016-
runs-on: ubuntu-latest
1017-
needs: [ min_version, deps ]
1018-
1019-
steps:
1020-
- uses: actions/checkout@v7.0.1
1021-
with:
1022-
persist-credentials: false
1023-
- uses: Swatinem/rust-cache@v2
1024-
- name: Install strace
1025-
run: sudo apt-get update && sudo apt-get install -y strace
1026-
- name: Build utilities for redundant open checks
1027-
run: cargo build --profile=release-small -p uu_cp -p uu_mv
1028-
- name: Run redundant open verification
1029-
run: ./util/check-open-once.sh
1030-
10311014
test_toctou:
10321015
name: TOCTOU Security Check
10331016
runs-on: ubuntu-latest

‎tests/by-util/test_cp.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9827,6 +9827,49 @@ fn test_cp_d_overwrites_existing_symlink_dest() {
98279827
assert!(at.read_symlink("slink").ends_with("slink-target"));
98289828
}
98299829

9830+
#[rstest]
9831+
#[case("always")]
9832+
#[case("auto")]
9833+
#[case("never")]
9834+
#[cfg(target_os = "linux")]
9835+
#[cfg(not(wasi_runner))]
9836+
fn test_cp_open_once(#[case] reflink_mode: &str) {
9837+
let (at, mut _ucmd) = at_and_ucmd!();
9838+
let file = "source_file";
9839+
at.touch(file);
9840+
if std::process::Command::new("strace")
9841+
.args([
9842+
"-e",
9843+
"trace=open,openat",
9844+
"-o",
9845+
"strace.out",
9846+
uutests::util::get_tests_binary(),
9847+
"cp",
9848+
&format!("--reflink={}", reflink_mode),
9849+
file,
9850+
"target_file",
9851+
])
9852+
.current_dir(at.as_string())
9853+
.output()
9854+
.is_err()
9855+
{
9856+
return; // missing strace
9857+
};
9858+
// don't theck that `cp` succeed for the system missing reflink support
9859+
let open_count = at
9860+
.read("strace.out")
9861+
.lines()
9862+
.filter(|l| {
9863+
let trimmed = l.trim();
9864+
trimmed.starts_with("open(")
9865+
|| trimmed.starts_with("openat(")
9866+
&& (trimmed.contains(file) || trimmed.contains("target_file"))
9867+
})
9868+
.count();
9869+
// don't use assert_eq! for the system missing reflink support
9870+
assert!(open_count <= 2);
9871+
}
9872+
98309873
// Regression for GNU tests/cp/acl: `cp -p` must preserve POSIX ACLs on
98319874
// Linux. ACLs are part of GNU's `mode` preservation, not its `xattr`
98329875
// preservation, so the default-no-xattr change in #9704 must not strip

‎tests/by-util/test_mv.rs‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,82 @@ 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, mut _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(")
67+
|| trimmed.starts_with("openat(")
68+
&& (trimmed.contains(file) || trimmed.contains("target_file"))
69+
})
70+
.count();
71+
assert_eq!(open_count, 0);
72+
}
73+
74+
#[test]
75+
#[cfg(target_os = "linux")]
76+
#[cfg(not(wasi_runner))]
77+
fn test_mv_open_once() {
78+
let (at, mut _ucmd) = at_and_ucmd!();
79+
let file = "source_file";
80+
at.touch(file);
81+
if std::process::Command::new("strace")
82+
.args([
83+
"-e",
84+
"trace=open,openat",
85+
"-o",
86+
"strace.out",
87+
uutests::util::get_tests_binary(),
88+
"mv",
89+
file,
90+
"/dev/shm/target_file",
91+
])
92+
.current_dir(at.as_string())
93+
.output()
94+
.is_err()
95+
{
96+
return; // missing strace
97+
};
98+
99+
let open_count = at
100+
.read("strace.out")
101+
.lines()
102+
.filter(|l| {
103+
let trimmed = l.trim();
104+
trimmed.starts_with("open(")
105+
|| trimmed.starts_with("openat(")
106+
&& (trimmed.contains(file) || trimmed.contains("target_file"))
107+
})
108+
.count();
109+
assert_eq!(open_count, 2);
110+
}
111+
36112
#[test]
37113
fn test_mv_rename_dir() {
38114
let (at, mut ucmd) = at_and_ucmd!();

‎util/check-open-once.sh‎

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

0 commit comments

Comments
 (0)