Patchwork [2/2] Use urandom to get random seed

login
register
mail settings
Submitter Andi Kleen
Date Sept. 27, 2011, 9:30 p.m.
Message ID <1317159016-19625-2-git-send-email-andi@firstfloor.org>
Download mbox | patch
Permalink /patch/116670/
State New
Headers show

Comments

Andi Kleen - Sept. 27, 2011, 9:30 p.m.
From: Andi Kleen <ak@linux.intel.com>

When available use /dev/urandom to get the random seem. This will lower the probability
of collisions.

On other systems it will fallback to the old methods.

Passes bootstrap + testsuite on x86_64. Ok?

gcc/:

* 2011-09-26   Andi Kleen <ak@linux.intel.com>

	* toplev.c (init_local_tick): Try reading random seed from /dev/urandom
---
 gcc/toplev.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
Joseph S. Myers - Sept. 27, 2011, 11:24 p.m.
On Tue, 27 Sep 2011, Andi Kleen wrote:

>    if (!flag_random_seed)
>      {
> +      /* Try urandom first. Time of day is too likely to collide. */
> +
> +      int fd = open("/dev/urandom", O_RDONLY);
> +      read(fd, &random_seed, sizeof(random_seed));
> +      close(fd);

There appear to be no error checks and an unconditional fallthrough to the 
HAVE_GETTIMEOFDAY code.  Also, missing spaces before open parentheses.
Andi Kleen - Sept. 28, 2011, 12:15 a.m.
On 9/27/2011 4:24 PM, Joseph S. Myers wrote:
> On Tue, 27 Sep 2011, Andi Kleen wrote:
>
>>     if (!flag_random_seed)
>>       {
>> +      /* Try urandom first. Time of day is too likely to collide. */
>> +
>> +      int fd = open("/dev/urandom", O_RDONLY);
>> +      read(fd,&random_seed, sizeof(random_seed));
>> +      close(fd);
> There appear to be no error checks and an unconditional fallthrough to the
> HAVE_GETTIMEOFDAY code.  Also, missing spaces before open parentheses.

In case of any error the variable just stays 0, which is what is intended.
The gettimeofday code reads another variable and the decision between 
the two is in
init_random_seed()

I'll fix the whitespace.

-Andi

Patch

diff --git a/gcc/toplev.c b/gcc/toplev.c
index e1a8b35..6298595 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -262,6 +262,12 @@  init_local_tick (void)
 {
   if (!flag_random_seed)
     {
+      /* Try urandom first. Time of day is too likely to collide. */
+
+      int fd = open("/dev/urandom", O_RDONLY);
+      read(fd, &random_seed, sizeof(random_seed));
+      close(fd);
+
       /* Get some more or less random data.  */
 #ifdef HAVE_GETTIMEOFDAY
       {