Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
mllib: Add a utility function for safely reading from /dev/urandom.
OCaml's buffered 'in_channel' has a 64k buffer, so using it to read a few bytes from /dev/urandom removes a lot of the system's entropy (for example /proc/sys/kernel/random/entropy_avail goes from ~3000 to 128). This patch was originally by Edwin Török for builder.ml. I generalized it because there are two other places where we did over-sized reads from /dev/urandom.
- Loading branch information
Showing
7 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| (* Read /dev/urandom. | ||
| * Copyright (C) 2013 Red Hat Inc. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along | ||
| * with this program; if not, write to the Free Software Foundation, Inc., | ||
| * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| *) | ||
|
|
||
| (* Read and return N bytes (only) from /dev/urandom. | ||
| * | ||
| * As pointed out by Edwin Török, previous versions of this had a big | ||
| * problem. They used the OCaml buffered I/O library which would read | ||
| * a lot more data than requested. This version uses unbuffered I/O | ||
| * from the Unix module. | ||
| *) | ||
|
|
||
| open Unix | ||
|
|
||
| let open_urandom_fd () = openfile "/dev/urandom" [O_RDONLY] 0 | ||
|
|
||
| let read_byte fd = | ||
| let s = String.make 1 ' ' in | ||
| fun () -> | ||
| if read fd s 0 1 = 0 then ( | ||
| close fd; | ||
| raise End_of_file | ||
| ); | ||
| Char.code s.[0] | ||
|
|
||
| let urandom_bytes n = | ||
| assert (n > 0); | ||
| let ret = String.make n ' ' in | ||
| let fd = open_urandom_fd () in | ||
| for i = 0 to n-1 do | ||
| ret.[i] <- Char.chr (read_byte fd ()) | ||
| done; | ||
| close fd; | ||
| ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| (* Read /dev/urandom. | ||
| * Copyright (C) 2013 Red Hat Inc. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along | ||
| * with this program; if not, write to the Free Software Foundation, Inc., | ||
| * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| *) | ||
|
|
||
| (** Read and return N bytes (only) from /dev/urandom. *) | ||
|
|
||
| val urandom_bytes : int -> string | ||
| (** Read N bytes from /dev/urandom and return it as a binary string. *) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters