-
Notifications
You must be signed in to change notification settings - Fork 301
secp256k1-sys: remove CPtr
impl for &[T]
and associated weirdness
#816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
In rust-bitcoin#794 I accidentally changed a *mut to *const. This is incorrect and unsound.
We take an optional `data` argument to `shared_secret`, but the upstream function we call never uses it. However, this argument *does* use the CPtr impl on &[T] in order to obtain a pointer to pass across the FFI boundary. This impl is very dangerous, and its use here is sound only because the resulting pointer is never used. See rust-bitcoin#627 (comment)
In sort_pubkeys() we convert a slice of PublicKeys to a slice of ffi::PublicKeys. This is OK, because we have repr(transparent) allowing us to do this. But we do so in an unnecessarily-complicated way, which winds up going through the CPtr implementation on &[u8], and which I think may technically be unsound. Simplify it and avoid using this impl. The next commit will remove it entirely.
I am equivocating on whether I should backport the last commit to secp256k1-sys 0.9.x, 0.10.x and 0.11.x. Doing so would be a breaking change (and would break rust-secp256k1, necessitating a minor release of those) and technically speaking there is no unsoundness here so it is not urgent to do so. But on the other hand, |
For anyone struggling to follow my story here -- checkout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On debae90 successfully ran local tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK debae90
This is a bit over my head. I went to read the Rustnomicon and decided I don't want to do that ATM, one day ... |
…mpl for `&[T]` and associated weirdness debae9017592c4659bb0d64294d1cdadbf8e652f secp256k1-sys: remove CPtr impl for &[T] (Andrew Poelstra) 2cef56192f8107ab4ee2c8f5e1131708769b82fc key.rs: clean up some pointer mangling in sort_pubkeys() (Andrew Poelstra) 44bcb893053d96bf5f082c6acd45e89226128300 ellswift: remove unused `data` argument from `shared_secret` (Andrew Poelstra) 2139aff0d705142ffb40883023bf47d4978dae70 secp256k1-sys: fix type signature in secp256k1_ec_pubkey_sort (Andrew Poelstra) Pull request description: Fixes the signature of secp256k1_ec_pubkey_sort to use `*mut` rather than `*const`. Separately, our call to `secp256k1_ec_pubkey_sort`, while correctly starting from a `&mut [PublicKey]`, briefly constructed a `&[ffi::PublicKey]` slice before obtaining a `*mut` pointer from this. This was unsound for completely unnecessary reasons, and only compiled because we have an impl of the `CPtr` trait for `&[T]`, which provides a `as_mut_c_ptr()` method which basically cannot be safely used. Removing that `&[T]` impl revealed another case where it was (unnecessarily) being used: in `EllSwift::shared_secret`, where we use it to obtain a `*mut` pointer from a `&[u8]`, which we pass across the FFI boundary which simply drops it. ACKs for top commit: tcharding: ACK debae9017592c4659bb0d64294d1cdadbf8e652f Tree-SHA512: 2245883cbd0695dcf495a075d530155c6f496cf7fe78c2560b567888dc5fb5f3160970c8bbd44abd67e240bccf41555fc71fd3f74daac11d446fc2b4783028ed
…mpl for `&[T]` and associated weirdness debae9017592c4659bb0d64294d1cdadbf8e652f secp256k1-sys: remove CPtr impl for &[T] (Andrew Poelstra) 2cef56192f8107ab4ee2c8f5e1131708769b82fc key.rs: clean up some pointer mangling in sort_pubkeys() (Andrew Poelstra) 44bcb893053d96bf5f082c6acd45e89226128300 ellswift: remove unused `data` argument from `shared_secret` (Andrew Poelstra) 2139aff0d705142ffb40883023bf47d4978dae70 secp256k1-sys: fix type signature in secp256k1_ec_pubkey_sort (Andrew Poelstra) Pull request description: Fixes the signature of secp256k1_ec_pubkey_sort to use `*mut` rather than `*const`. Separately, our call to `secp256k1_ec_pubkey_sort`, while correctly starting from a `&mut [PublicKey]`, briefly constructed a `&[ffi::PublicKey]` slice before obtaining a `*mut` pointer from this. This was unsound for completely unnecessary reasons, and only compiled because we have an impl of the `CPtr` trait for `&[T]`, which provides a `as_mut_c_ptr()` method which basically cannot be safely used. Removing that `&[T]` impl revealed another case where it was (unnecessarily) being used: in `EllSwift::shared_secret`, where we use it to obtain a `*mut` pointer from a `&[u8]`, which we pass across the FFI boundary which simply drops it. ACKs for top commit: tcharding: ACK debae9017592c4659bb0d64294d1cdadbf8e652f Tree-SHA512: 2245883cbd0695dcf495a075d530155c6f496cf7fe78c2560b567888dc5fb5f3160970c8bbd44abd67e240bccf41555fc71fd3f74daac11d446fc2b4783028ed
Fixes the signature of secp256k1_ec_pubkey_sort to use
*mut
rather than*const
.Separately, our call to
secp256k1_ec_pubkey_sort
, while correctly starting from a&mut [PublicKey]
, briefly constructed a&[ffi::PublicKey]
slice before obtaining a*mut
pointer from this. This was unsound for completely unnecessary reasons, and only compiled because we have an impl of theCPtr
trait for&[T]
, which provides aas_mut_c_ptr()
method which basically cannot be safely used.Removing that
&[T]
impl revealed another case where it was (unnecessarily) being used: inEllSwift::shared_secret
, where we use it to obtain a*mut
pointer from a&[u8]
, which we pass across the FFI boundary which simply drops it.