Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

cemeyer
Copy link

@cemeyer cemeyer commented Nov 23, 2019

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.

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.
@cemeyer
Copy link
Author

cemeyer commented Nov 23, 2019

(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.)

@briansmith
Copy link
Owner

@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) };
Copy link
Owner

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?

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.

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.

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.

Copy link
Author

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.

Copy link
Owner

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.

Copy link
Author

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.

@cemeyer
Copy link
Author

cemeyer commented Nov 24, 2020

Thanks for looking at this. (Or a better avenue through the libc crate — works for me either way!)

@briansmith
Copy link
Owner

Thanks for the PR. I'm planning to switch the implementation to use the getrandom crate as part of the goal of encapsulating/minimizing operating-system-specific code in ring; see draft PR #1531. I'm closing this in favor of that approach.

@briansmith briansmith closed this Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants