Skip to contentSkip to conversation

net: Remove call of srand from eth_random_enetaddr()

PATCH New Christian Riesch 2 participants 8 messages Jan 8, 2013 – Jan 9, 2013 1 message not in the archive Networking

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 net: Remove call of srand from eth_random_enetaddr()

Currently eth_random_enetaddr() seeds the random number generator with get_timer(0). Some boards might want to use other sources for the seed, therefore move the call of srand() to the board specific code.

Cc Michael Walle <[email protected]>
Cc Joe Hershberger <[email protected]>
---
 board/buffalo/lsxl/lsxl.c |    1 +
 include/net.h             |    3 +++
 net/eth.c                 |    2 --
 3 files changed, 4 insertions(+), 2 deletions(-)
board/buffalo/lsxl/lsxl.c +1 -0
index 57776fb..b7eb0dc 100644
@@ -248,6 +248,7 @@ static void rescue_mode(void)
 	printf("Entering rescue mode..\n");
 #ifdef CONFIG_RANDOM_MACADDR
 	if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
+		srand(get_timer(0));
 		eth_random_enetaddr(enetaddr);
 		if (eth_setenv_enetaddr("ethaddr", enetaddr)) {
 			printf("Failed to set ethernet address\n");
include/net.h +3 -0
index 970d4d1..5fc3693 100644
@@ -141,6 +141,9 @@ extern int eth_getenv_enetaddr_by_index(const char *base_name, int index,
  *
  * In these cases, we generate a random locally administered ethernet address.
  *
+ * Remember to seed the random number generator with srand() before calling
+ * this functon.
+ *
  * Args:
  *  enetaddr - returns 6 byte hardware address
  */
net/eth.c +0 -2
index 321d5b1..dc4cc20 100644
@@ -84,8 +84,6 @@ void eth_random_enetaddr(uchar *enetaddr)
 {
 	uint32_t rval;
 
-	srand(get_timer(0));
-
 	rval = rand();
 	enetaddr[0] = rval & 0xff;
 	enetaddr[1] = (rval >> 8) & 0xff;
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 1/2 net: Remove call of srand from eth_random_enetaddr()

Dear Christian Riesch,

Quoted 7 lines · In message <[email protected]> you wrote:

Currently eth_random_enetaddr() seeds the random number generator with get_timer(0). Some boards might want to use other sources for the seed, therefore move the call of srand() to the board specific code.

Cc Michael Walle <[email protected]>
Cc Joe Hershberger <[email protected]>

I don't like this change. What exactly is wrong with using the timer
here? It is probably much more random that the (so-called)
"un-initialized" memory you suggest to use instead.

If there is really need to use another inital valu, only this should be fixed - but the srand() call itself should remain as is.

You cannot do this.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
You can observe a lot just by watchin'.                  - Yogi Berra
#3 lore raw

PATCH 1/2 net: Remove call of srand from eth_random_enetaddr()

Hello Wolfgang,
Thank you for your comments.

Quoted 8 lines · Wolfgang Denk

Dear Christian Riesch,

In message <[email protected]<javascript:;>>

Quoted 4 lines · you

Currently eth_random_enetaddr() seeds the random number generator with get_timer(0). Some boards might want to use other sources for the seed, therefore move the call of srand() to the board specific code.

Quoted 8 lines
Quoted 4 lines
Cc Michael Walle <[email protected]>
Cc Joe Hershberger <[email protected] <javascript:;>>

I don't like this change. What exactly is wrong with using the timer
here? It is probably much more random that the (so-called)
"un-initialized" memory you suggest to use instead.

On the AM1808 SoC the counter is reset to zero on power up. So using it to generate random numbers in code that is called interactively by the user is fine and will yield a random MAC address, but in my case I will get the same MAC address on each board at each power up.

Quoted 5 lines

If there is really need to use another inital valu, only this should be fixed - but the srand() call itself should remain as is.

For other boards it may be ok to use a counter, and for some there may be no SRAM or it may be already overwritten, e.g by the SPL... Therefore I am not changing this for all boards, but make it board specific.

Regards, Christian

Quoted 16 lines

You cannot do this.

Best regards,

Wolfgang Denk

--
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de<javascript:;>
You can observe a lot just by watchin'. - Yogi Berra
_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de <javascript:;>
http://lists.denx.de/mailman/listinfo/u-boot

#4 lore raw

PATCH 1/2 net: Remove call of srand from eth_random_enetaddr()

Dear Christian Riesch,

Quoted 9 lines · In message <CABkLObrL2BhKmjh9NCcv1UQ-cbfJisUidK8JoE8DSAOEkqiZ1g@mail.gmail.com>…
Quoted 4 lines

I don't like this change. What exactly is wrong with using the timer
here? It is probably much more random that the (so-called)
"un-initialized" memory you suggest to use instead.

On the AM1808 SoC the counter is reset to zero on power up. So using it to generate random numbers in code that is called interactively by the user is fine and will yield a random MAC address, but in my case I will get the same MAC address on each board at each power up.

I think the whole concept of using random MAC adresses is broken. You should consider thinking about fixing the root cause of your problem.

Quoted 6 lines
Quoted 2 lines

If there is really need to use another inital valu, only this should be fixed - but the srand() call itself should remain as is.

For other boards it may be ok to use a counter, and for some there may be no SRAM or it may be already overwritten, e.g by the SPL... Therefore I am not changing this for all boards, but make it board specific.

I don't want to see any board specific code here. If using the timer is not good enough, then pass a weak funtion as argument to srand() which defaults to using the timer as we do now, and which can be redefined in board specific code.

Other than this, the common code should not be changed.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The main thing is the play itself. I swear that greed for  money  has
nothing  to  do with it, although heaven knows I am sorely in need of
money.                                           - Feodor Dostoyevsky
#5 lore raw

PATCH 2/2 calimain: Generate random MAC address for factory tests

In reply to a message not in the archive
---
 board/omicron/calimain/calimain.c |   31 ++++++++++++++++++++++++++++++-
 include/configs/calimain.h        |    2 ++
 2 files changed, 32 insertions(+), 1 deletion(-)
board/omicron/calimain/calimain.c +30 -1
index 1060a1f..80e3893 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 OMICRON electronics GmbH
+ * Copyright (C) 2011-2013 OMICRON electronics GmbH
  *
  * Based on da850evm.c. Original Copyrights follow:
  *
@@ -136,6 +136,35 @@ int board_init(void)
 	return 0;
 }
 
+/* seed random number generator with uninitialized SRAM content */
+static void srand_sram(void)
+{
+	int *p;
+	int seed = 0;
+
+	for (p = (int *) 0x80000000; p < (int *) 0x8001ffff; p++)
+		seed ^= *p;
+
+	srand(seed);
+}
+
+int board_late_init(void)
+{
+	uchar enetaddr[6];
+
+	if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
+		srand_sram();
+		eth_random_enetaddr(enetaddr);
+		if (eth_setenv_enetaddr("ethaddr", enetaddr)) {
+			printf("Failed to set random ethernet address\n");
+		} else {
+			printf("Setting random ethernet address %pM.\n",
+			       enetaddr);
+		}
+	}
+	return 0;
+}
+
 #ifdef CONFIG_DRIVER_TI_EMAC
 /*
  * Initializes on-board ethernet controllers.
include/configs/calimain.h +2 -0
index 5c2b35d..8cea0d9 100644
@@ -30,6 +30,7 @@
 #define CONFIG_DRIVER_TI_EMAC
 #define MACH_TYPE_CALIMAIN	3528
 #define CONFIG_MACH_TYPE	MACH_TYPE_CALIMAIN
+#define CONFIG_BOARD_LATE_INIT
 
 /*
  * SoC Configuration
@@ -202,6 +203,7 @@
 #define CONFIG_BOOTP_DNS2
 #define CONFIG_BOOTP_SEND_HOSTNAME
 #define CONFIG_NET_RETRY_COUNT	10
+#define CONFIG_RANDOM_MACADDR
 #endif
 
 /*
Signature · 1 line
1.7.9.5
#6 lore raw

PATCH 2/2 calimain: Generate random MAC address for factory tests

Dear Christian Riesch,

Quoted 22 lines · In message <[email protected]> you wrote:
---
 board/omicron/calimain/calimain.c |   31 ++++++++++++++++++++++++++++++-
 include/configs/calimain.h        |    2 ++
 2 files changed, 32 insertions(+), 1 deletion(-)
board/omicron/calimain/calimain.c +2 -1
index 1060a1f..80e3893 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 OMICRON electronics GmbH
+ * Copyright (C) 2011-2013 OMICRON electronics GmbH
  *
  * Based on da850evm.c. Original Copyrights follow:
  *
@@ -136,6 +136,35 @@ int board_init(void)
 	return 0;
 }
 
+/* seed random number generator with uninitialized SRAM content */

board/omicron/calimain/calimain.c int board_init(void) · line 149

Quoted 10 lines
board/omicron/calimain/calimain.c +10 -0
+static void srand_sram(void)
+{
+	int *p;
+	int seed = 0;
+
+	for (p = (int *) 0x80000000; p < (int *) 0x8001ffff; p++)
+		seed ^= *p;
+
+	srand(seed);
+}

Note that your "uninitialized" SRAM content is probably not so much random at all - I guess, it is much less random than the originally used timer value.

What exactly is your justification for such a change? Please elucidate...

Quoted 6 lines
board/omicron/calimain/calimain.c +6 -0
+int board_late_init(void)
+{
+	uchar enetaddr[6];
+
+	if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
+		srand_sram();

board/omicron/calimain/calimain.c int board_init(void) · line 166

Quoted 10 lines
board/omicron/calimain/calimain.c +10 -0
+		eth_random_enetaddr(enetaddr);
+		if (eth_setenv_enetaddr("ethaddr", enetaddr)) {
+			printf("Failed to set random ethernet address\n");
+		} else {
+			printf("Setting random ethernet address %pM.\n",
+			       enetaddr);
+		}
+	}
+	return 0;
+}

NAK! You are but duplicating the code already present in net/eth.c

This makes no sense.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
It is dangerous to be right on a subject  on  which  the  established
authorities are wrong.                                    -- Voltaire
#7 lore raw

PATCH 2/2 calimain: Generate random MAC address for factory tests

Hello Wolfgang,
Thank you again for your comments.

Quoted 24 lines · Wolfgang Denk

Dear Christian Riesch,

Quoted 21 lines · In message <[email protected]> you wrote:
---
  board/omicron/calimain/calimain.c |   31 ++++++++++++++++++++++++++++++-
  include/configs/calimain.h        |    2 ++
  2 files changed, 32 insertions(+), 1 deletion(-)
board/omicron/calimain/calimain.c +1 -1
index 1060a1f..80e3893 100644
@@ -1,5 +1,5 @@
  /*
- * Copyright (C) 2011 OMICRON electronics GmbH
+ * Copyright (C) 2011-2013 OMICRON electronics GmbH
   *
   * Based on da850evm.c. Original Copyrights follow:
   *
@@ -136,6 +136,35 @@ int board_init(void)
  	return 0;
  }
 
Quoted 14 lines

board/omicron/calimain/calimain.c int board_init(void) · line 149

Quoted 11 lines
board/omicron/calimain/calimain.c +11 -0
+/* seed random number generator with uninitialized SRAM content */
+static void srand_sram(void)
+{
+	int *p;
+	int seed = 0;
+
+	for (p = (int *) 0x80000000; p < (int *) 0x8001ffff; p++)
+		seed ^= *p;
+
+	srand(seed);
+}

Note that your "uninitialized" SRAM content is probably not so much random at all -

There are several papers around describing the use of initial SRAM value after power up for the generation of random numbers. This is why I gave it a try, and it works pretty well for me. I get a different seed for each power-up cycle. I guess that the randomness is limited and that part of the generated seed is more a fingerprint for the chip, therefore it may not be good enough for security related stuff, but for my purpose it's ok.

Quoted 2 lines

I guess, it is much less random than the originally used timer value.

In my case the timer value is not random at all since it is reset to zero at power-up. Since seeding the random number generator is always done at the same time after power-up in the current code, the seed will always be the same for my devices. Therefore the generated MAC address will always be the same for all devices.

Quoted 2 lines

What exactly is your justification for such a change? Please elucidate...

Actually I do not change anything ;-)

For the lsxl board that is currently the only user of eth_random_enet(), nothing changes. get_timer(0) remains the source of the randomness for this board. My patches only allow other boards to use a sources of randomness that is available to them instead of forcing everyone to use get_timer(0).

Quoted 11 lines
Quoted 11 lines
board/omicron/calimain/calimain.c +10 -0
+int board_late_init(void)
+{
+	uchar enetaddr[6];
+
+	if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
+		srand_sram();
+		eth_random_enetaddr(enetaddr);
+		if (eth_setenv_enetaddr("ethaddr", enetaddr)) {
+			printf("Failed to set random ethernet address\n");
+		} else {
Quoted 8 lines

board/omicron/calimain/calimain.c int board_init(void) · line 166

Quoted 6 lines
board/omicron/calimain/calimain.c +6 -0
+			printf("Setting random ethernet address %pM.\n",
+			       enetaddr);
+		}
+	}
+	return 0;
+}

NAK! You are but duplicating the code already present in net/eth.c

Apparently I am missing something here. I do not see a call of eth_random_enetaddr() in net/eth.c. To which part of net/eth.c are you referring?

Regards, Christian

Quoted 6 lines

This makes no sense.

Best regards,

Wolfgang Denk

#8 lore raw

PATCH 2/2 calimain: Generate random MAC address for factory tests

Dear Christian Riesch,

Quoted 5 lines · In message <[email protected]> you wrote:
Quoted 3 lines

What exactly is your justification for such a change? Please elucidate...

Actually I do not change anything ;-)

Why do you need a patch then? :-P

Quoted 5 lines
Quoted 1 line

NAK! You are but duplicating the code already present in net/eth.c

Apparently I am missing something here. I do not see a call of eth_random_enetaddr() in net/eth.c. To which part of net/eth.c are you referring?

Hm... I can't find it now, either. Dunno what I have seen then. eventually I was looking at your patch in two windows :-(

Sorry - but still: very similar code exists in rescue_mode() in "board/buffalo/lsxl/lsxl.c"; this should be factored out.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Any sufficiently advanced  technology  is  indistinguishable  from  a
rigged demo.                              - Andy Finkel, computer guy