dd: avoid overflow converting a block count to bytes#13473
Open
Angadi56 wants to merge 2 commits into
Open
Conversation
Contributor
|
the description is hard to read, please fix it also, please add tests that reproduces the issue |
|
GNU testsuite comparison: |
Author
|
Done. Rewrote the description with a concrete repro, and added a CLI regression test in tests/by-util/test_dd.rs next to the existing overflow tests. It fails without the fix (debug builds panic on the multiply) and passes with it. The parseargs unit test covering the parse-time rejection was already in the diff. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
skip=andseek=values given as block counts are converted to bytes inNum::to_bytesby multiplying the count byibs/obs. The result is then checked againsti64::MAX(GNU's intmax_t limit), but the multiply itself was unchecked, so the product can wrap around before the check ever sees it.Repro:
That is 2^55 blocks. With the default
ibs=512, the byte value is 2^55 * 512 == 2^64, which wraps to 0. A release build then skips nothing and continues silently; a debug build panics with "attempt to multiply with overflow".The fix switches the multiply to
saturating_mul, so an oversized count saturates tou64::MAXand the existing bound check rejects it with the usual "Value too large for defined data type" error. This keeps the u64::MAX-means-too-big convention the byte-size parser already uses, and matchescalc_bsize, which already saturates the same multiply.calc_loop_bsizehad the same uncheckedrremain * ibson thecount=path, so it gets the same change.Tests:
parseargs.rschecking thatskip=/seek=block counts whose byte size overflows are rejected at parse timetests/by-util/test_dd.rsrunningdd skip=.../seek=...and checking for the error; it fails without the fix (panic in debug builds) and passes with it