From f2ba29f6b69876a176d760163202bdbcb6fd3463 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 5 Dec 2022 13:56:41 +1100 Subject: [PATCH 1/4] Remove deref on an immutable reference Clippy emits: error: deref on an immutable reference As suggested, do not deref. --- src/ecdsa/serialized_signature.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ecdsa/serialized_signature.rs b/src/ecdsa/serialized_signature.rs index cbc3d00df..79ef92d35 100644 --- a/src/ecdsa/serialized_signature.rs +++ b/src/ecdsa/serialized_signature.rs @@ -45,7 +45,7 @@ impl PartialEq for SerializedSignature { impl AsRef<[u8]> for SerializedSignature { #[inline] fn as_ref(&self) -> &[u8] { - &*self + self } } From 2b9a5b695397cd6e0fe4a04bd2559844d73b16cf Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Fri, 2 Dec 2022 12:58:32 +0000 Subject: [PATCH 2/4] context: introduce unsafe `PreallocatedContext` trait Fixes unsoundness in `preallocated_gen_new` which previously did not properly constrain the lifetime of the buffer used to back the context object. We introduce an unsafe marker trait, and impl it for our existing preallocated-context markers. Annoyingly the trait has to be public even though it should never be used directly, and is only used alongside the sealed `Context` trait, so it is de-facto sealed itself. Fixes #543 --- src/context.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/context.rs b/src/context.rs index fca1dc4ef..5b3ccec10 100644 --- a/src/context.rs +++ b/src/context.rs @@ -297,8 +297,16 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> { } } -impl<'buf, C: Context + 'buf> Secp256k1 { - /// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all) +/// Trait marking that a particular context object internally points to +/// memory that must outlive `'a` +pub unsafe trait PreallocatedContext<'a> {} + +unsafe impl<'buf> PreallocatedContext<'buf> for AllPreallocated<'buf> {} +unsafe impl<'buf> PreallocatedContext<'buf> for SignOnlyPreallocated<'buf> {} +unsafe impl<'buf> PreallocatedContext<'buf> for VerifyOnlyPreallocated<'buf> {} + +impl<'buf, C: Context + PreallocatedContext<'buf>> Secp256k1 { + /// Lets you create a context with a preallocated buffer in a generic manner (sign/verify/all). pub fn preallocated_gen_new(buf: &'buf mut [AlignedType]) -> Result, Error> { #[cfg(target_arch = "wasm32")] ffi::types::sanity_checks_for_wasm(); From 989bf05b979da4d0242fb9ea84563a00b0162b53 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 5 Dec 2022 13:59:01 +1100 Subject: [PATCH 3/4] Add saftey docs for PreallocatedContext trait Add `# Safety` section to the rustdocs of the `PrealocatedContext` trait. This change was back ported manually (instead of directly cherry-picking the patch) so as to add a blank newline after the heading as is customary. Original: `commit 1e6eb6cb4dd435d062e9d4d16a2218c9033d5c78` --- src/context.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/context.rs b/src/context.rs index 5b3ccec10..098e9233e 100644 --- a/src/context.rs +++ b/src/context.rs @@ -299,6 +299,12 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> { /// Trait marking that a particular context object internally points to /// memory that must outlive `'a` +/// +/// # Safety +/// +/// This trait is used internally to gate which context markers can safely +/// be used with the `preallocated_gen_new` function. Do not implement it +/// on your own structures. pub unsafe trait PreallocatedContext<'a> {} unsafe impl<'buf> PreallocatedContext<'buf> for AllPreallocated<'buf> {} From 70e8cbea85daafdd9971180dfa459e1f6e7fb811 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 6 Dec 2022 08:20:12 +1100 Subject: [PATCH 4/4] Bump version to 0.23.5 Done after backport to fix unsoundness issue. --- CHANGELOG.md | 5 +++++ Cargo.toml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aacd9f0f8..c77ae3136 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ + +# 0.23.5 - 2022-12-05 + +* Backport [fix soundness issue with `preallocated_gen_new`](https://github.com/rust-bitcoin/rust-secp256k1/pull/548) + # 0.23.4 - 2022-07-14 * [Disable automatic rerandomization of contexts under WASM](https://github.com/rust-bitcoin/rust-secp256k1/pull/474) diff --git a/Cargo.toml b/Cargo.toml index de7ff34a3..85aa64060 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "secp256k1" -version = "0.23.4" +version = "0.23.5" authors = [ "Dawid Ciężarkiewicz ", "Andrew Poelstra " ] license = "CC0-1.0"