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

Use the getentropy(2) syscall on OpenBSD #30430

Merged
merged 8 commits into from
Dec 20, 2015
Merged

Use the getentropy(2) syscall on OpenBSD #30430

merged 8 commits into from
Dec 20, 2015

Conversation

mmcco
Copy link
Contributor

@mmcco mmcco commented Dec 17, 2015

Rust already supports Linux's getrandom(2), which is very similar and
was based on getentropy(2). This is a pretty clean, simple addition that
uses the same approach as the iOS randomness API support.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

for s in v.chunks_mut(256) {
unsafe { ret = syscall(7, s.as_mut_ptr(), s.len()); }
if ret == -1 {
panic!("unexpected getrandom error: {}", errno());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the panic message isn't very accurate: use getentropy name instead of getrandom

@mmcco
Copy link
Contributor Author

mmcco commented Dec 17, 2015

@semarie Thanks for the comments, I've addressed both with additional commits.

}

extern "C" {
fn syscall(number: c_long, ...) -> c_long;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be added to libc instead of being inlined here? It's likely to be useful for others as well! (same for the NR_GETENTROPY constant below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that'd be useful, as long as it's portable across Unix platforms.

@alexcrichton
Copy link
Member

The man page for getentropy states

getentropy() is not intended for regular code; please use the arc4random(3) family of functions instead.

so should arc4random be used instead?

@mmcco
Copy link
Contributor Author

mmcco commented Dec 17, 2015

should arc4random be used instead?

I definitely don't think so.

arc4random is a userland CSPRNG complete with a stirred pool etc. So, Rust would have a ChaCha20-based userland CSPRNG that backs up to a... ChaCha20-based userland CSPRNG that backs up to an entropy syscall. That sounds like a ton of extra overhead and complexity.

Basically, arc4random is suggested over getentropy because it's a standard library CSPRNG rather than a straight syscall. But Rust implements its own standard library CSPRNG using the primitives we implement in src/libstd/rand/os.rs, so that advice doesn't apply to us

@alexcrichton
Copy link
Member

Ah ok, makes sense to me! r=me once the bits have been added to libc (just let me know if you need help)

@mmcco
Copy link
Contributor Author

mmcco commented Dec 17, 2015

r=me once the bits have been added to libc (just let me know if you need help)

You mean for the syscall() function addition, right? This PR is good to go as-is.

@alexcrichton
Copy link
Member

Yes the syscall function should live in libc as well as the NR_ constant, and then this should call them via libc::syscall(libc::NR_...)

@mmcco
Copy link
Contributor Author

mmcco commented Dec 17, 2015

In the meantime, can we add this? Considering that the existing getrandom(2) code for Linux does the same.

@alexcrichton
Copy link
Member

When moving to the external liblibc I made a sweep of the standard library to excise all remaining extern blocks on Unix, so in theory there are very few (only where appropriate), thus having a convention that all extern definitions in libc are in liblibc instead of the standard library itself. I'd like to preserve that convention for now, and it should be pretty easy to add functionality to libc

@mmcco
Copy link
Contributor Author

mmcco commented Dec 19, 2015

Now that the necessary bits are in libc, the libc submodule will have to be bumped before this can be merged.

Rust already supports Linux's getrandom(2), which is very similar and
was based on getentropy(2). This is a pretty clean, simple addition that
uses the same approach as the iOS randomness API support.
I copied it from the getrandom code but forgot to change the name.
Reported by Sebastien Marie.
@bors
Copy link
Contributor

bors commented Dec 19, 2015

☔ The latest upstream changes (presumably #30381) made this pull request unmergeable. Please resolve the merge conflicts.

@mmcco
Copy link
Contributor Author

mmcco commented Dec 19, 2015

@semarie @alexcrichton

The libc update breaks other code. Specifically, src/libstd/sys/unix/stack_overflow.rs:73 attempts to access the si_addr field of siginfo_t, which OpenBSD defines only through a macro.

I really think we should have merged the first commit and handled the rest elsewhere. This PR now involves updating a library's submodule and associated debugging, which aren't relevant to getentropy(2) support (particularly when the existing getrandom(2) support used a libc-less approach).

@semarie
Copy link
Contributor

semarie commented Dec 19, 2015

@MMCo what was done is the good order for the workflow in rust. But it seems you don't have tested your PR on master before proposing it. OpenBSD and others BSD have poor testing infrastructure, so it is really important to test it locally before asking for merging.

@alexcrichton I have a diff I am testing for the libc issue. As soon testing is ok, I will make a PR on libc.

@mmcco
Copy link
Contributor Author

mmcco commented Dec 19, 2015

@semarie The first commit was well-tested. The rest have been in the process of making things work again after the various tweaks. I did get overconfident with a couple single-line changes.

@semarie
Copy link
Contributor

semarie commented Dec 19, 2015

@MMCo there are no problem. Eventually you would only spot the issue more early. I hope I have not been too rude.

@alexcrichton
Copy link
Member

@bors: r+ 9fde3e9

Thanks @mmcco! We can fixup the build in a follow-up commit to update libc or libstd

bors added a commit that referenced this pull request Dec 19, 2015
Rust already supports Linux's getrandom(2), which is very similar and
was based on getentropy(2). This is a pretty clean, simple addition that
uses the same approach as the iOS randomness API support.
@bors
Copy link
Contributor

bors commented Dec 19, 2015

⌛ Testing commit 9fde3e9 with merge abf60b2...

@bors bors merged commit 9fde3e9 into rust-lang:master Dec 20, 2015
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.

5 participants