Skip to content

fix(cksum): reject absurdly large SHAKE --length instead of aborting#13083

Open
koopatroopa787 wants to merge 2 commits into
uutils:mainfrom
koopatroopa787:fix-cksum-shake-length-panic
Open

fix(cksum): reject absurdly large SHAKE --length instead of aborting#13083
koopatroopa787 wants to merge 2 commits into
uutils:mainfrom
koopatroopa787:fix-cksum-shake-length-panic

Conversation

@koopatroopa787

Copy link
Copy Markdown
Contributor

Fixes

Fixes #12869

Problem

`cksum -a shake128/shake256 --length` accepted any `usize` value in bits with no upper bound. For example:

```
$ cksum -l=10011111117721172727 --algorithm shake128 a
memory allocation of 1251388889715146591 bytes failed
Aborted (core dumped)
```

The unsanitized length flows straight into the digest buffer allocation in `Digest::result()` (`vec![0; self.output_bits().div_ceil(8)]`), which aborts the whole process on an allocation failure rather than returning a clean error.

Fix

Cap `--length` for SHAKE128/SHAKE256 at 8192 bits (1 KiB of output), comfortably above any realistic use case, and report a GNU-style error (`invalid length: '...'` + `maximum digest length for 'SHAKE128' is 8192 bits`) when exceeded — mirroring the existing bound already enforced for BLAKE2b.

Test plan

  • Added test_shake_length_too_large_does_not_panic covering both shake128 and shake256.
  • cargo test --test tests shake passes (29 tests).
  • Manually verified the original repro from the issue now exits 1 with a clean error instead of aborting.

…ixes uutils#12869)

cksum -a shake128/shake256 --length accepted any usize value in bits
with no upper bound. A value like 10011111117721172727 overflows the
digest buffer allocation in Digest::result(), causing a Rust memory
allocation failure and process abort (exit 134) instead of a clean
error.

Cap --length for SHAKE algorithms at 8192 bits (1 KiB of output),
comfortably above any real use case, and report a GNU-style error
when exceeded. Add a regression test.
@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/symlink (passes in this run but fails in the 'main' branch)

@oech3

This comment was marked as resolved.

@oech3

This comment was marked as resolved.

Per review feedback, replace the fixed upper bound on --length with a
recoverable error from Vec::try_reserve_exact. GNU utils don't impose
arbitrary "realistic use case" limits, so Digest::result() now returns
io::Result<DigestOutput> and reports an OutOfMemory error instead of
letting the allocator abort the process when the requested length is
absurdly large.

Fixes uutils#12869
@koopatroopa787

Copy link
Copy Markdown
Contributor Author

Done — reworked the fix as suggested. Digest::result() now returns io::Result<DigestOutput>, and all implementations (including the impl_digest_shake! macro) allocate the output buffer via a new try_reserve_zeroed helper that uses Vec::try_reserve_exact and maps an allocation failure to an io::Error with ErrorKind::OutOfMemory, instead of imposing an arbitrary upper bound. The one production call site (digest_reader in checksum/mod.rs) now propagates this with ?.

Dropped the fixed-cap test and replaced it with one that just verifies an absurdly large --length fails cleanly instead of aborting the process, without asserting a specific cap value.

Pushed as c3bb6a7.

@oech3

oech3 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Thankyou!

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(cksum): panics when using an algorithm shake128 and shake256 with an extremly large length

2 participants