-
Notifications
You must be signed in to change notification settings - Fork 12
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
V0.5.1 #1
V0.5.1 #1
Conversation
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.
Hi @nmahendru, good PR! Just left a few comments about it.
src/mpz.rs
Outdated
fn __gmpz_set(rop: mpz_ptr, op: mpz_srcptr); | ||
fn __gmpz_set_ui(rop: mpz_ptr, op: c_ulong); |
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.
It's never used in the PR. Maybe remove it?
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.
good point! I missed that.
fn drop(&mut self) { | ||
unsafe { | ||
let size_limbs = __gmpz_size(&mut self.mpz); | ||
let dst = self.mpz._mp_d as *mut c_int; |
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.
Casting and writing to a raw pointer seems pretty dangerous. Can mpz_limbs_write function replace pointer casting? It provides access to the internal limbs.
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.
Good question!
I will add a comment here as to why this.
the mpz_limbs_write is not doing compiler fencing(optimizations by compilers might get rid of the code or delay it) which is what we want to do a best effort securely wipe off the underlying limb array.
Also:
- In gmp source the mp_limb_t is defined as unsigned long so c_int will never be more than that on any platform.
- Memory for the array is guaranteed to be allocated by gmp in limb sizes. So we can be sure that we are not writing what we should not.
Also I think we should be having something in the gmp library itself to do this kind of a thing. I will try to do a PR for them and if that is accepted, We can remove it here.
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.
Actually I looked the source of mpz_limbs_write just now and looks like it makes a new alloc and does not return the internal pointer. That is probably not what we want.
#include "gmp-impl.h"
mp_ptr
mpz_limbs_write (mpz_ptr x, mp_size_t n)
{
ASSERT (n > 0);
return MPZ_NEWALLOC (x, n);
}
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.
looks like it makes a new alloc
As I understand, it will make new alloc only if n > size(x), according to macro definition:
#define MPZ_REALLOC(x, n) \
(ABSIZ(x) >= (n) ? PTR(x) : (_mpz_realloc ((x),(n)), PTR (x)))
#define MPZ_NEWALLOC MPZ_REALLOC
mpz_limbs_write is not doing compiler fencing
Maybe I missing something, but I thought that mpz_limbs_write
just returns a pointer to underlying limb array, which should be the same thing as casting pointer (you can still use std::ptr::write_volatile
and atomic::{fence, compiler_fence}
), but this function is provided by API, so any further changes in internal representation of GMP number will not affect this code.
However, I do not believe that internal representation of GMP number might be changed, I think it's okay to leave pointer casting here.
@survived I think the serde changes which are in the crates.io package were for some reason not in master on this repo. so they are also getting added when I rebased. hope that is okie ? or I can prepare another PR. |
You're right, it would be better to add serialization support here! Technically rebased changes will work, but I have some thoughts about it, I believe that it can be improved in this crate. My suggestions:
It's more convenient to have different features in separated PRs, but if you're more comfortable with keeping them in one place, I'm good with that. |
Hi @survived . I have incorporated all but suggestion 3 from your comments. |
Good! I'm merging. Zeroizing is must have here, it's a good contribution into security of upstream libraries 🙂 |
This merge has broken |
@ggutoski, just pushed a fix |
securely zeroize the underlying object.
This is just best effort.
No one can guarantee zeroizing as the underlying devices/OS may still cover this up.