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

Josh Wu josh.wu at atmel.com
Wed Sep 16 11:53:35 CEST 2015


Hi, Wolfgang, Andreas

Thanks for the testing and point out the wrongness in my patch.

On 9/16/2015 5:15 PM, Wolfgang Denk wrote:
> Dear Josh,
>
> In message <55F91588.3040305 at 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?

yes, this one works well. if you don't mind I want to sent v2 patch, 
which is like:

-    srand(time(0) | getpid());
+    srand(time(0) + (getpid() << 8));


>
> 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
>
>
> Thanks.
>
> Best regards,
>
> Wolfgang Denk
>

Best Regards,
Josh Wu


More information about the U-Boot mailing list