Skip to content

fix(dd): enforce a declared file-size limit at write time (ulimit -f / RLIMIT_FSIZE) - #1455

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/dd-ulimit-f
Oct 30, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/dd-ulimit-f

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #1453.

What

dd now enforces a file-size limit declared by its POSIX parent, closing the gap where dd.exe ignored ulimit -f 0 and wrote the full output with exit 0 while GNU dd dies by SIGXFSZ (rc 153, shell prints File size limit exceeded).

Why the parent declares it through the environment

Windows has no setrlimit()/RLIMIT_FSIZE. The limit has to travel parent→child explicitly. Today:

  • winuxsh has no ulimit support at all (no ulimit/RLIMIT references in the repo), so it declares nothing.
  • rubash persists its Windows ulimit emulation per resource in the child-inherited environment: __RUBASH_ULIMIT_F=<soft>:<hard> in decimal bytes with 18446744073709551615 = RLIM_INFINITY (rubash src/builtins/ulimit.rs, platform-contract note lines 36-58, key format lines 326-338 — the value stored is the setrlimit-semantic byte count, block factor already applied).

So dd honors two channels, in precedence order:

  1. WINUXCMD_RLIMIT_FSIZE=<bytes> — the canonical, documented WinuxCmd contract (opt-in; unlimited/empty disables; a malformed value warns once and disables). This is what a future winuxsh-side ulimit should set.
  2. __RUBASH_ULIMIT_F — rubash's existing channel, so ulimit -f 0; exec dd ... under rubash reaches dd with zero parent-side changes.

GNU semantics reproduced (coreutils dd + setrlimit(RLIMIT_FSIZE), Linux)

  • Writes to the output regular file are metered against the file's write frontier (GetFileType(hOut) == FILE_TYPE_DISK only — Linux does not apply RLIMIT_FSIZE to pipe writes, so dd | cat keeps working).
  • The write that would cross the limit is delivered only up to the limit, then dd dies with the SIGXFSZ shape: no diagnostic, no transfer summary, exit 128+25 = 153 — the parent shell reports the notice, $? is 153.
  • A write ending exactly at the limit is legal (the kernel fires only when a write would make the file exceed it).
  • The output file is still created (O_CREAT semantics), capped at the limit.
  • Documented non-goal: the EFBIG-after-caught-SIGXFSZ shape (dd: error writing 'FILE': File size limit exceeded + summary, exit 1) needs the parent's trap state, which has no channel on Windows.

Tests

8 new unit tests in tests/unit/dd/dd_unit_test.cpp (winuxcmd-tests): zero cap through both channels (silent stderr, rc 153, empty file), crossing-write truncation (cap 10, obs 4 → exactly 10 bytes land), exact-at-limit success, unlimited literal, malformed value warn-and-disable, no-limit regression guard, pipe exemption.

Manual evidence: DD evidence script over the release build — WINUXCMD_RLIMIT_FSIZE=0 dd if=/dev/zero of=big bs=1024 count=100 → rc 153, empty stderr, big size 0 (GNU: rc 153 under WSL bash 5.3 with ulimit -f 0).

Notes for reviewers

  • flush_output_buffer returns a tri-state now (ok / error / sigxfsz); both call sites handle the death shape by returning 153 immediately without printing stats, matching a signal kill (GNU dd prints nothing when SIGXFSZ kills it).
  • The frontier starts at the real output position after the seek=/truncate handling, so seek= offsets count toward the cap (ftruncate-style seek truncation is not limited, matching Linux where ftruncate bypasses RLIMIT_FSIZE).

dd ignored the file-size limit its POSIX parent had declared: under
`ulimit -f 0` the GNU dd child dies by SIGXFSZ (rc 153, shell prints
"File size limit exceeded"), while WinuxCmd dd wrote the full output
and exited 0, so the death a shell must notice never happens
(reproduced by rubash tests/issue1493_subshell_signal_notice_regressions.rs
against WinuxCmd 1.1.20).

Windows has no setrlimit()/RLIMIT_FSIZE, so the limit has to be
declared by the parent through the environment. dd now honors two
channels, in precedence order:

- WINUXCMD_RLIMIT_FSIZE=<bytes> - the canonical opt-in contract
  (decimal bytes; "unlimited"/empty disables; a malformed value warns
  once and disables). A winuxsh-side `ulimit` can adopt it later.
- __RUBASH_ULIMIT_F=<soft>:<hard> - rubash's Windows ulimit emulation
  fork-inheritance channel (bytes, 18446744073709551615 =
  RLIM_INFINITY); the soft value is taken as the RLIMIT_FSIZE soft
  limit, so `ulimit -f 0; exec dd ...` reaches dd today.

Enforcement mirrors the kernel's RLIMIT_FSIZE write semantics (GNU
coreutils dd under setrlimit, Linux): writes are metered against the
output file's frontier; the crossing write is delivered only up to the
limit and dd then dies with the SIGXFSZ shape - no diagnostic, no
transfer summary, exit 128+25 = 153. A write ending exactly at the
limit is legal. Only regular-file outputs are covered
(GetFileType == FILE_TYPE_DISK), matching Linux where pipe writes are
not subject to RLIMIT_FSIZE. The EFBIG-after-caught-SIGXFSZ shape
(diagnostic + summary, exit 1) needs the parent's trap state, which
has no channel, and is documented as not reproduced.

Unit tests: tests/unit/dd/dd_unit_test.cpp (8 new cases: zero cap
through both channels, crossing-write truncation, exact-at-limit,
unlimited literal, malformed value, no-limit regression guard, pipe
exemption).
@caomengxuan666
caomengxuan666 merged commit 4a6c5cb into main Oct 30, 2026
8 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/dd-ulimit-f branch November 2, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] dd ignores ulimit -f 0 (write should fail with SIGXFSZ/file-size-limit; currently writes freely)

1 participant