Skip to content

fix(sv1_api): reject extranonce2 sizes a submit cannot carry - #2495

Open
amartya1523 wants to merge 2 commits into
stratum-mining:mainfrom
amartya1523:fix/sv1-bound-extranonce2-size
Open

amartya1523 wants to merge 2 commits into
stratum-mining:mainfrom
amartya1523:fix/sv1-bound-extranonce2-size

Conversation

@amartya1523

Copy link
Copy Markdown
Contributor

Closes #2478.

SetExtranonce::try_from and the mining.subscribe response parser accepted any u64 extranonce2 size and narrowed it with as usize. That causes two problems:

  • A size above 32 leaves the client unable to build a mining.submit, because Submit::extra_nonce2 is an Extranonce (a B032). The session is configured, but no share it finds can ever be sent.
  • On 32-bit targets the cast truncates: 4294967300 becomes 4, so the client silently works with a size the server never sent.

Commit 1: bound the size before converting it. Extranonce gets pub const MAX_LEN: usize = 32, and both parsers go through one helper that rejects anything above it with ParsingMethodError::UnexpectedValue. The existing variants are kept for values that are not a u64 at all, so callers matching on those see no change. The conversion only happens after the bound check, so it is lossless on every target.

Commit 2: sv1_api 7.0.0 → 7.1.0, for the new public constant. No other crate uses it, so the ^7.0.0 requirements stay.

I reused UnexpectedValue instead of adding a ParsingMethodError variant, because that enum is not #[non_exhaustive] and a new variant would be a major bump.

Tests: 0 and 32 are accepted by both parsers. 33, 4294967300 (which a 32-bit cast would turn into 4) and u64::MAX are rejected by both. The rejection cases fail on main.

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

SetExtranonce and the mining.subscribe response parser accepted any u64
extranonce2 size and cast it with as usize. A size above 32 bytes left
the client unable to build a mining.submit, since extranonce2 travels
as an Extranonce, and on 32-bit targets the cast truncated the value.

Reject sizes above the new Extranonce::MAX_LEN in both parsers before
converting them, so the conversion is lossless.
sv1_api gains Extranonce::MAX_LEN, so it takes a minor bump. No other
crate uses it, so their requirements stay at ^7.0.0.
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.

Extranonce2 size from the server is not checked against the 32-byte Extranonce limit

1 participant