Skip to content

Commit f02f4bc

Browse files
mikusgabrielsylvestre
authored andcommitted
split: keep O_CREATE flag on reopen when output is existing
1 parent 1b1e862 commit f02f4bc

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎src/uu/split/src/platform/output_file.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ fn create_or_truncate_output_file(input: &OsStr, filename: &OsStr) -> Result<Fil
4141
Err(e) if e.kind() == ErrorKind::AlreadyExists => {
4242
let file = OpenOptions::new()
4343
.write(true)
44+
.create(true)
45+
.truncate(false)
4446
.open(Path::new(filename))
4547
.map_err(|e| open_file_error(filename, e))?;
4648
check_not_input(input, filename, &file)?;

‎tests/by-util/test_split.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,33 @@ fn test_guard_input() {
16091609
assert_eq!(at.read("xaa"), "1\n2\n3\n");
16101610
}
16111611

1612+
/// Reopening an existing output must retain `O_CREAT` so Linux's
1613+
/// `fs.protected_regular` and `fs.protected_fifos` checks apply. A dangling
1614+
/// symlink makes that flag observable without needing a multi-user test.
1615+
#[test]
1616+
#[cfg(unix)]
1617+
fn test_existing_output_is_reopened_with_create() {
1618+
let (at, mut ucmd) = at_and_ucmd!();
1619+
1620+
at.write("input", "AB");
1621+
1622+
// An existing caller-owned output must still be truncated.
1623+
at.write("xaa", "stale contents that are longer than the chunk\n");
1624+
1625+
at.symlink_file("xab_target", "xab");
1626+
assert!(!at.file_exists("xab_target"));
1627+
1628+
ucmd.args(&["-b", "1", "input"]).succeeds().no_output();
1629+
1630+
assert_eq!(at.read("xaa"), "A");
1631+
assert!(at.plus("xab").is_symlink());
1632+
assert_eq!(
1633+
fs::read_link(at.plus("xab")).unwrap(),
1634+
at.plus("xab_target")
1635+
);
1636+
assert_eq!(at.read("xab_target"), "B");
1637+
}
1638+
16121639
#[test]
16131640
fn test_multiple_of_input_chunk() {
16141641
let (at, mut ucmd) = at_and_ucmd!();

‎util/check-safe-traversal.sh‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,30 @@ if have_util split; then
318318
fi
319319
echo "✓ split refuses to overwrite its input via a swapped output symlink"
320320
rm -f split_victim split_swap_*
321+
322+
# Existing outputs take the EEXIST fallback after O_CREAT|O_EXCL. The
323+
# reopen must retain O_CREAT so Linux's protected-file checks apply
324+
# (issue #15821).
325+
printf '0123456789abcdef' > split_input
326+
: > split_exist_aa
327+
: > split_exist_ab
328+
strace -f -e trace=openat -o strace_split_existing_output.log \
329+
$split_cmd -b 4 split_input split_exist_ 2>/dev/null || true
330+
331+
# Select the EEXIST fallback, excluding O_EXCL create attempts and unrelated
332+
# O_APPEND reopens.
333+
split_reopens=$(grep -E 'openat\(AT_FDCWD, "split_exist_[a-z]+",' strace_split_existing_output.log \
334+
| grep -v 'O_EXCL' | grep -v 'O_APPEND' || true)
335+
if [ -z "$split_reopens" ]; then
336+
cat strace_split_existing_output.log
337+
fail_immediately "split did not reopen the existing outputs - strace check broken (issue #15821)"
338+
fi
339+
if echo "$split_reopens" | grep -vq 'O_CREAT'; then
340+
cat strace_split_existing_output.log
341+
fail_immediately "split must reopen existing outputs with O_CREAT so fs.protected_regular applies (issue #15821)"
342+
fi
343+
echo "✓ split reopens existing outputs with O_CREAT"
344+
rm -f split_input split_exist_*
321345
fi
322346

323347
# mv cross-device (EXDEV) must use fd-based *xattr ops (issue #10014).

0 commit comments

Comments
 (0)