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
Fix: sanity check /dev/urandom #4777
Fix: sanity check /dev/urandom #4777
Conversation
src/crystal/system/unix/getrandom.cr
Outdated
@@ -12,8 +12,9 @@ module Crystal::System::Random | |||
|
|||
if sys_getrandom(Bytes.new(16)) >= 0 | |||
@@getrandom_available = true | |||
else | |||
elsif File.stat("/dev/urandom").chardev? |
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.
Please use #stat
on the opened file descriptor, to avoid race conditions.
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 catch. Will change.
a609119
to
97d78fc
Compare
Sanity checks /dev/urandom to be character device, not a tempered file, a socket or whatever. Also makes sure that FD_CLOEXEC is set. closes crystal-lang#4752
97d78fc
to
28c8746
Compare
Done. |
I think this is fine but it would be nice to do something about the duplicated code, especially as it is security related and reflecting any changes in all places is an absolute must. |
I'm merging as is. If someone has an epiphany to avoid the duplication in an simple way, please open a pull request :-) |
Sanity checks
/dev/urandom
to be character device, not a tempered file, a socket or whatever. Also makes sure thatFD_CLOEXEC
is set.closes #4752