Skip to content

Commit

Permalink
Fix: check /dev/urandom is a character device
Browse files Browse the repository at this point in the history
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 #4752
  • Loading branch information
ysbaddaden committed Aug 1, 2017
1 parent c8cb5cd commit a609119
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/crystal/system/unix/getrandom.cr
Expand Up @@ -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?
@@urandom = urandom = File.open("/dev/urandom", "r")
urandom.close_on_exec = true
urandom.sync = true # don't buffer bytes
end
end
Expand Down
7 changes: 5 additions & 2 deletions src/crystal/system/unix/urandom.cr
Expand Up @@ -6,8 +6,11 @@ module Crystal::System::Random

private def self.init
@@initialized = true
@@urandom = urandom = File.open("/dev/urandom", "r")
urandom.sync = true # don't buffer bytes
if File.stat("/dev/urandom").chardev?
@@urandom = urandom = File.open("/dev/urandom", "r")
urandom.close_on_exec = true
urandom.sync = true # don't buffer bytes
end
end

def self.random_bytes(buf : Bytes) : Nil
Expand Down

0 comments on commit a609119

Please sign in to comment.