Skip to content

std::rand::OsRng: Use getrandom syscall on Linux #18664

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

Merged
merged 1 commit into from
Nov 7, 2014

Conversation

klutzy
Copy link
Contributor

@klutzy klutzy commented Nov 5, 2014

getrandom(2) system call 1 has been added on Linux 3.17.
This patch makes OsRng use getrandom if available, and use
traditional /dev/urandom fallback if not.

Fixes #17922.

@rust-highfive
Copy link
Contributor

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@klutzy
Copy link
Contributor Author

klutzy commented Nov 5, 2014

cc @thestinger @huonw

: "{rax}"(GETRANDOM), "{rdi}"(buf.as_ptr()), "{rsi}"(buf.len()), "{rdx}"(flags)
: "rcx", "r11", "memory"
: "volatile");
}
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using inline assembly, could this use the extern_weak trick that __pthread_get_minstack uses?

Copy link
Member

Choose a reason for hiding this comment

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

That may end up simplifying a large amount of what's going on here as well...

Copy link
Contributor

Choose a reason for hiding this comment

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

There's no symbol for getrandom at all yet. Using syscall to call it would work.

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok, using syscall sounds like it would work just as well!

@thestinger
Copy link
Contributor

@klutzy: I think this should use syscall rather than platform-dependent assembly. It will return -1 and set errno to ENOSYS if the system call number is invalid.

@thestinger
Copy link
Contributor

Rust supports calls to variadic C functions so it should work fine.

ret
}

#[cfg(not(any(target_os = "linux", target_arch = "x86_64", target_arch = "x86")))]
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens to ARM Linux?

I think this line should be

#[cfg(not(all(target_os = "linux", any(target_arch = "x86", target_arch = "x86_64"))))]

Ah, if you switch to the variadic syscall(2), it should take care of ENOSYS

`getrandom(2)` system call [1] has been added on Linux 3.17.
This patch makes `OsRng` use `getrandom` if available, and use
traditional `/dev/urandom` fallback if not.

[1]:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=c6e9d6f38894798696f23c8084ca7edbf16ee895
@klutzy
Copy link
Contributor Author

klutzy commented Nov 6, 2014

updated with syscall.

@nodakai Added arm-eabi (syscall number 384), although I have no way to test it right now.

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Nov 6, 2014
@bors bors merged commit fdf5195 into rust-lang:master Nov 7, 2014
@klutzy klutzy deleted the getrandom branch November 7, 2014 02:39
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.

use the getrandom syscall instead of urandom when available
6 participants