diff mbox

[U-Boot] tools: gen_eth_addr: remove getpid() operation for the random seed

Message ID 20150916091511.5238F380905@gemini.denx.de
State RFC
Delegated to: Wolfgang Denk
Headers show

Commit Message

Wolfgang Denk Sept. 16, 2015, 9:15 a.m. UTC
Dear Josh,

In message <55F91588.3040305@atmel.com> you wrote:
> 
> In above commands, I have two duplicated eth addr:
> 92:33:16:3f:0a:56
> d2:41:66:54:64:aa

Agreed. Randomness is really poor; for a sequence of 1000 invocations
of gen_eth_addr in a shell loop I would only gt 124 different MAC
addresses:

-> for i in {1..1000} ; do ./gen_eth_addr ; done >/tmp/0
-> sort -u /tmp/0 >/tmp/1 ; wc -l /tmp/[01]
 1000 /tmp/0
  124 /tmp/1

In a second run, even only 41 :-(

But without the getpid() part, it gets even worse - the same loop
would produce only 15 different addresses!


Changing the '|' into a '+' would reliably generate 1000 different MAC
addresses.

> I understand your concern. My intention is make it harder to generate 
> the duplicated result.

I understand this, and agree that we should implement such a fix.

> Maybe we can ORing the MSB of time(0)?
> I'll investigate it little more.

The following patch appears to work fine for me.  Maybe you can test
it?



Thanks.

Best regards,

Wolfgang Denk
diff mbox

Patch

diff --git a/tools/gen_eth_addr.c b/tools/gen_eth_addr.c
index bf9d935..834163a 100644
--- a/tools/gen_eth_addr.c
+++ b/tools/gen_eth_addr.c
@@ -15,7 +15,7 @@  main(int argc, char *argv[])
 {
     unsigned long ethaddr_low, ethaddr_high;
 
-    srand(time(0) | getpid());
+    srand(time(0) + getpid());
 
     /*
      * setting the 2nd LSB in the most significant byte of