Skip to content

fix(sv1_api): reject numeric ntime and nonce wider than 32 bits - #2496

Open
amartya1523 wants to merge 2 commits into
stratum-mining:mainfrom
amartya1523:fix/sv1-reject-wide-ntime-nonce
Open

amartya1523 wants to merge 2 commits into
stratum-mining:mainfrom
amartya1523:fix/sv1-reject-wide-ntime-nonce

Conversation

@amartya1523

Copy link
Copy Markdown
Contributor

Closes #2490.

Submit::try_from accepts ntime and nonce as JSON numbers in both the 5- and 6-param forms, and narrowed them with as u32. Anything above u32::MAX was silently truncated to its low 32 bits. An ntime of 4294967296 parsed as 0, so the share was validated against a time the miner never sent, rather than being rejected as malformed.

Commit 1: lossless conversion. Both numeric arms now go through one numeric_u32 helper. It uses u32::try_from and returns ParsingMethodError::UnexpectedValue for values that don't fit. A number that isn't an unsigned integer at all (negative, fractional) still gets ImpossibleToParseAsU64, as before, so callers matching on that variant see no change. The hex-string forms already reject anything longer than 8 hex digits and are untouched.

Commit 2: sv1_api 7.0.0 → 7.0.1. Only parsing changes, nothing public is added.

Tests: for both the 5- and 6-param forms, u32::MAX is accepted for both fields. 2^32 in ntime or in nonce is rejected, and that case fails on main. The existing negative and fractional tests still pass.

Overlap: #2485, #2486 and #2495 also bump sv1_api, so whichever lands later needs a one-line rebase of its bump commit, which I'll do.

Submit accepted ntime and nonce as JSON numbers and narrowed them with
as u32, so an ntime of 4294967296 parsed as 0 and the share was checked
against a time the miner never sent.

Convert them with u32::try_from and reject values that do not fit,
keeping ImpossibleToParseAsU64 for numbers that are not unsigned
integers at all.
Only mining.submit parsing changes, so sv1_api takes a patch bump.

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using one numeric_u32 helper for both ntime and nonce in both five- and six-parameter submit forms prevents either branch from silently narrowing a JSON integer. The regression covers u32::MAX as accepted and 2^32 as rejected in both fields, which is the exact wraparound boundary the old as u32 cast missed.

@Christopherdominic

Copy link
Copy Markdown
Contributor

#2498 lists closes: #2490 too, and its rewrite of Submit parsing rejects >32-bit ntime/nonce as well. @bit-aloo do you want the small fixes (#2486, #2495, #2496) to land first so #2498 rebases on them, or should these be folded into #2498 and closed? Asking so amartya doesn't keep rebasing version bumps against a PR that removes this code.

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.

Numeric ntime and nonce are truncated to 32 bits

3 participants