diff --git a/Cargo.lock b/Cargo.lock index 58a34da8dd..889cdd7883 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1203,7 +1203,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sv1_api" -version = "7.0.0" +version = "7.1.0" dependencies = [ "binary_sv2", "bitcoin", diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 79d118bb53..a519d82878 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -559,7 +559,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sv1_api" -version = "7.0.0" +version = "7.1.0" dependencies = [ "binary_sv2", "bitcoin", diff --git a/sv1/Cargo.toml b/sv1/Cargo.toml index 33b1b3233c..374f26b4f5 100644 --- a/sv1/Cargo.toml +++ b/sv1/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sv1_api" -version = "7.0.0" +version = "7.1.0" authors = ["The Stratum V2 Developers"] edition = "2021" readme = "README.md" diff --git a/sv1/src/methods/server_to_client.rs b/sv1/src/methods/server_to_client.rs index 0f0f04b284..3e4deccb77 100644 --- a/sv1/src/methods/server_to_client.rs +++ b/sv1/src/methods/server_to_client.rs @@ -269,9 +269,11 @@ impl TryFrom for SetExtranonce { let (extra_nonce1, extra_nonce2_size) = match ¶ms[..] { [JString(a), JNumber(b)] => ( Extranonce::try_from(hex_decode(a)?)?, - b.as_u64() - .ok_or_else(|| ParsingMethodError::not_unsigned_from_value(b.clone()))? - as usize, + extranonce2_size( + b.as_u64() + .ok_or_else(|| ParsingMethodError::not_unsigned_from_value(b.clone()))?, + b, + )?, ), _ => return Err(ParsingMethodError::wrong_args_from_value(msg.params)), }; @@ -282,6 +284,21 @@ impl TryFrom for SetExtranonce { } } +/// Checks an extranonce2 size sent by the server, given as `size` and as the JSON `value` it was +/// read from. +/// +/// The miner sends extranonce2 in [`crate::client_to_server::Submit`] as an [`Extranonce`], so a +/// size above [`Extranonce::MAX_LEN`] would leave it unable to submit any share. Bounding the +/// size first also keeps the conversion to `usize` lossless on 32-bit targets. +fn extranonce2_size(size: u64, value: &serde_json::Number) -> Result { + if size > Extranonce::MAX_LEN as u64 { + return Err(ParsingMethodError::unexpected_value_from_value(JNumber( + value.clone(), + ))); + } + Ok(size as usize) +} + #[derive(Debug, Clone)] /// Server may arbitrarily adjust version mask pub struct SetVersionMask { @@ -503,9 +520,12 @@ impl TryFrom<&Response> for Subscribe { a, // infallible b.as_str().try_into()?, - c.as_u64().ok_or_else(|| { - ParsingMethodError::ImpossibleToParseAsU64(Box::new(c.clone())) - })? as usize, + extranonce2_size( + c.as_u64().ok_or_else(|| { + ParsingMethodError::ImpossibleToParseAsU64(Box::new(c.clone())) + })?, + c, + )?, ), _ => return Err(ParsingMethodError::UnexpectedArrayParams(params.clone())), }; @@ -817,3 +837,51 @@ impl From for serde_json::Map { params } } + +#[test] +fn extranonce2_size_must_fit_a_submit() { + let set_extranonce = |size: serde_json::Value| { + SetExtranonce::try_from(Notification { + method: "mining.set_extranonce".to_string(), + params: serde_json::json!(["08000002", size]), + }) + }; + let subscribe = |size: serde_json::Value| { + let response: Response = serde_json::from_value(serde_json::json!({ + "id": 1, + "error": null, + "result": [[["mining.notify", "1"]], "08000002", size], + })) + .unwrap(); + Subscribe::try_from(&response) + }; + + assert_eq!( + set_extranonce(serde_json::json!(0)) + .unwrap() + .extra_nonce2_size, + 0 + ); + assert_eq!( + set_extranonce(serde_json::json!(32)) + .unwrap() + .extra_nonce2_size, + 32 + ); + assert_eq!( + subscribe(serde_json::json!(32)).unwrap().extra_nonce2_size, + 32 + ); + + // 2^32 + 4 would become 4 if it were cast to a 32-bit usize + for size in [33, 4_294_967_300, u64::MAX] { + assert!(matches!( + set_extranonce(serde_json::json!(size)), + Err(ParsingMethodError::UnexpectedValue(_)) + )); + assert!(matches!( + subscribe(serde_json::json!(size)), + Err(ParsingMethodError::UnexpectedValue(_)) + )); + } +} diff --git a/sv1/src/utils.rs b/sv1/src/utils.rs index a013174c1f..a4cf5eb993 100644 --- a/sv1/src/utils.rs +++ b/sv1/src/utils.rs @@ -26,6 +26,9 @@ impl fmt::Display for Extranonce { } impl Extranonce { + /// The most bytes an extranonce can hold. + pub const MAX_LEN: usize = 32; + pub fn len(&self) -> usize { self.0.len() }