-
Notifications
You must be signed in to change notification settings - Fork 708
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
SystemRandom: Fix #326 - Prefer getrandom(2) on FreeBSD #917
Conversation
FreeBSD added the Linux-compatible getrandom(2) API in 12.0. Like Linux, prefer it to the /dev/urandom device, but fallback to the earlier method if the syscall is not available.
(Of course, it builds and tests pass on FreeBSD CURRENT. Please consider this contribution licensed under the project's preferred ISC license for new code.) |
@Thomasdezeeuw Could you please review this? I think this may be a better solution than PR #1119. |
// Clamp request size to INT_MAX due to limitation of the return type of libc::syscall() | ||
// (c_int). | ||
let chunk_len: c::size_t = dest.len().min(c_int::max_value() as usize); | ||
let r = unsafe { libc::syscall(SYS_GETRANDOM, dest.as_mut_ptr(), chunk_len, 0) }; |
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.
Does FreeBSD have a stable syscall ABI, or is it one of those platforms where only libc has a stable ABI?
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.
I could only find https://wiki.freebsd.org/Releng/ABI, which says:
We also try to maintain ABI compatibility across .0 releases, but they are not strictly enforced except for libraries that already implements versioned symbols.
Which I read as best effort, but not guaranteed.
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.
I'll see if I can add it to libc.
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.
PR for libc rust-lang/libc#1982.
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 one of those platforms where only libc has a stable ABI. The plan is to provide a stable libsystem
ABI at some point in the near future for non-C programming languages, but I'm afraid we're not there yet.
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.
PR for libc rust-lang/libc#1982.
Great. It seems like we could just call getrandom
directly from ring through FFI, without going through the libc crate, couldn't we? It seems like maybe we should go that route.
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.
Sure, you could link FreeBSD libc directly in ring without using the libc crate.
Thanks for looking at this. (Or a better avenue through the libc crate — works for me either way!) |
Thanks for the PR. I'm planning to switch the implementation to use the |
FreeBSD added the Linux-compatible getrandom(2) API in 12.0. Like Linux,
prefer it to the /dev/urandom device, but fallback to the earlier method if
the syscall is not available.