فهرست منبع

check_dhcp.c - use /dev/urandom

While we are not using this address for assignment, it should follow proper practices when generating random numbers. This patch allows for claiming random bytes from /dev/urandom with fallback to srand() and random() in the event the fd could not be opened or bytes could not be read.

coverity 103713
Spenser Reinhardt 11 سال پیش
والد
کامیت
8d63dfce05
1فایلهای تغییر یافته به همراه13 افزوده شده و 4 حذف شده
  1. 13 4
      plugins-root/check_dhcp.c

+ 13 - 4
plugins-root/check_dhcp.c

@@ -466,11 +466,20 @@ int send_dhcp_discover(int sock){
 	discover_packet.hlen=ETHERNET_HARDWARE_ADDRESS_LENGTH;
 
 	/*
-	 * transaction ID is supposed to be random.  We won't use the address so
-	 * we don't care about high entropy here.  time(2) is good enough.
+	 * transaction ID is supposed to be random.
+	 * This allows for proper randomness if the system supports it, and fallback to
+	 * srand & random if not.
 	 */
-	srand(time(NULL));
-	packet_xid=random();
+	int randfd = open("/dev/urandom", O_RDONLY);
+	if (randfd > 2 && read(randfd, (char *)&packet_xid, sizeof(uint32_t)) >= 0) {
+		/* no-op as we have successfully filled packet_xid */
+	}
+	else {
+		 /* fallback bad rand */
+		srand(time(NULL));
+		packet_xid=random();
+	}
+	if (randfd > 2) close(randfd);
 	discover_packet.xid=htonl(packet_xid);
 
 	/**** WHAT THE HECK IS UP WITH THIS?!?  IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/