[Toybox] [BUG] split and csplit destroy their input when its path coincides with an output path
João Matos
joao.silva.matos at tecnico.ulisboa.pt
Wed Oct 7 15:38:10 UTC 2026
Hello,
We found two silent data-loss bugs in toybox's "split" and "csplit".
Both are present in the current master branch, verified against commit
2a27e238cf59ca43c6985b509490a587ad882c12 (Codeberg; 6a0c0ef on GitHub
differs only in the README), and in release 0.8.14.
Summary
-------
Both commands open each output file with truncation while the input is
still being read, and never check that an output is the input. When the
input is named like one of the generated outputs, the normal case when
re-splitting a piece from a previous run, the input is truncated and
the data is lost.
- split leaves the input holding one read buffer and exits 0.
- csplit goes further: the truncation makes its rules fail, and the
error path removes the files it created, the input among them. The
input file is deleted.
Details
-------
split opens each new output (xaa, xab, ...) with
xcreate(TT.outfile, O_RDWR|O_CREAT|O_TRUNC, ...) inside the read loop
(toys/posix/split.c line 72). If the input is xaa, the first output
opened is xaa: it is truncated while still open for reading, so after
one buffer the input is empty and no other piece is written.
csplit (toys/pending/csplit.c) opens the input at line 139 and each
output with xfopen(..., "w+") (line 150). If the input is named like
the first output (xx00 by default), that open truncates it, the line
rules can no longer be satisfied, and abrt() removes the output files
(line 53) unless -k is given, which deletes the input.
Reproduction
------------
split (in defconfig):
python3 -c "import sys; sys.stdout.buffer.write(b'A'*20000)" > xaa
toybox split -b 8000 xaa
echo "exit=$?" # 0
wc -c xaa # 4096, not 20000
ls xab xac # do not exist
csplit is in pending/, so it needs CONFIG_CSPLIT=y:
seq 3000 > in
toybox csplit in 1000 2000 # works: xx00 xx01 xx02
rm xx0?
seq 3000 > xx00
toybox csplit xx00 1000 2000 # "csplit: Rules not processed", exit 1
ls xx00 # No such file or directory
Tested on Debian GNU/Linux (glibc 2.41, x86_64), toybox built from the
commit above with "make defconfig" (plus CONFIG_CSPLIT=y).
Impact
------
Silent data loss for split (exit 0), and deletion of the input file for
csplit. Both are reached by a natural workflow: splitting a piece from
a previous run again.
For split, GNU coreutils fixed the same defect in release 8.18 ("split
now checks that the input is not the output"); GNU split now refuses
with "'xaa' would overwrite input". For csplit there is no such
precedent: GNU csplit (coreutils 9.7) behaves the same way as toybox
here, so this is not a toybox-only problem.
Suggested fix
-------------
Before opening each output, compare st_dev/st_ino (from stat/fstat) of
the output path, if it exists, against the input's, and refuse if they
match. A string comparison of the names is not sufficient (xaa vs
./xaa). For csplit, the error-path cleanup should also never remove a
file that is the input.
Found during research on file-system handling in symbolic execution
tools.
Best regards,
João Matos
More information about the Toybox
mailing list