drivers/virtio: Use arc4random_buf to generate MAC address.

Use arc4random_buf instead of rand() to improve randomness quality and avoid using weak PRNG for MAC generation.

Signed-off-by: liqinhui <liqinhui@xiaomi.com>
This commit is contained in:
liqinhui
2025-01-20 16:32:04 +08:00
committed by archer
parent bbda415412
commit 2740e40ee7
+2 -13
View File
@@ -28,6 +28,7 @@
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <nuttx/compiler.h>
#include <nuttx/kmalloc.h>
@@ -624,20 +625,8 @@ static void virtio_net_set_macaddr(FAR struct virtio_net_priv_s *priv)
* conflicts with something else on the network.
*/
srand(time(NULL) +
#ifdef CONFIG_NETDEV_IFINDEX
dev->d_ifindex
#else
(uintptr_t)dev % 256
#endif
);
mac[0] = 0x42;
mac[1] = rand() % 256;
mac[2] = rand() % 256;
mac[3] = rand() % 256;
mac[4] = rand() % 256;
mac[5] = rand() % 256;
arc4random_buf(mac + 1, 5);
}
}