Skip to contentSkip to conversation

net: make net_random_ethaddr() more random

PATCH New Michael Walle 2 participants 3 messages Aug 22, 2019 – Aug 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: make net_random_ethaddr() more random

The net_random_ethaddr() tries to get some entropy from different startup times of a board. The seed is initialized with get_timer() which has only a granularity of milliseconds. We can do better if we use get_ticks() which returns the raw timer ticks. Using this we have a higher chance of getting different values at startup.

---
 include/net.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net.h +1 -1
index a54d5eeac5..8215316bd3 100644
@@ -816,7 +816,7 @@ static inline int is_valid_ethaddr(const u8 *addr)
 static inline void net_random_ethaddr(uchar *addr)
 {
 	int i;
-	unsigned int seed = get_timer(0);
+	unsigned int seed = get_ticks() & (unsigned int)-1;
 
 	for (i = 0; i < 6; i++)
 		addr[i] = rand_r(&seed);
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/1 net: make net_random_ethaddr() more random

Quoted 17 lines · Michael Walle

The net_random_ethaddr() tries to get some entropy from different startup times of a board. The seed is initialized with get_timer() which has only a granularity of milliseconds. We can do better if we use get_ticks() which returns the raw timer ticks. Using this we have a higher chance of getting different values at startup.

---
 include/net.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net.h +0 -0
index a54d5eeac5..8215316bd3 100644

include/net.h static inline int is_valid_ethaddr(const u8 *addr) · line 819

Quoted 5 lines
include/net.h +1 -1
@@ -816,7 +816,7 @@ static inline int is_valid_ethaddr(const u8 *addr)
 static inline void net_random_ethaddr(uchar *addr)
 {
        int i;
-       unsigned int seed = get_timer(0);
+       unsigned int seed = get_ticks() & (unsigned int)-1;

Shouldn't this be directly:

unsigned int seed = get_ticks();

include/net.h static inline int is_valid_ethaddr(const u8 *addr) · line 822

Quoted 3 lines
include/net.h +0 -0
        for (i = 0; i < 6; i++)
                addr[i] = rand_r(&seed);

Regards,
Bin

#2 lore raw

PATCH 1/1 net: make net_random_ethaddr() more random

Am 2019-08-23 05:17, schrieb Bin Meng:

Quoted 27 lines · Michael Walle
Quoted 18 lines

The net_random_ethaddr() tries to get some entropy from different startup times of a board. The seed is initialized with get_timer() which
has only a granularity of milliseconds. We can do better if we use get_ticks() which returns the raw timer ticks. Using this we have a higher chance of getting different values at startup.

---
 include/net.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net.h +0 -0
index a54d5eeac5..8215316bd3 100644

include/net.h static inline int is_valid_ethaddr(const u8 *addr) · line 819

Quoted 5 lines
include/net.h +1 -1
@@ -816,7 +816,7 @@ static inline int is_valid_ethaddr(const u8 *addr)
 static inline void net_random_ethaddr(uchar *addr)
 {
        int i;
-       unsigned int seed = get_timer(0);
+       unsigned int seed = get_ticks() & (unsigned int)-1;

Shouldn't this be directly:

Quoted 1 line

unsigned int seed = get_ticks();

get_ticks() returns an uint64_t. I wanted to explicitly strip the top bits if sizeof(uint64_t) != sizeof(unsigned int). Frankly, I also find this kinda ugly but wanted to avoid any compiler warnings. But apparently there is no warning (yet?).

So I'm happy to remove it if this is not an issue.

-michael