[vlc-devel] OpenBSD RNG Fix

Joris van Rooij jorrizza at jrrzz.net
Tue May 12 14:18:15 CEST 2009


Hello,

The attached diff fixes the wrongful assumption that /dev/random is always 
available on OpenBSD. This caused VLC to drain system resources in an 
infinite loop, tripping over an EIO (read() wasn't checked) and trying 
again.

It's a quick fix, so feel free to conjure up a nicer looking solution.

Greetings,
Joris
-------------- next part --------------
diff --git a/src/misc/rand.c b/src/misc/rand.c
index 81bc185..64d0be7 100644
--- a/src/misc/rand.c
+++ b/src/misc/rand.c
@@ -49,12 +49,22 @@ static uint8_t okey[BLOCK_SIZE], ikey[BLOCK_SIZE];
 
 static void vlc_rand_init (void)
 {
+    uint8_t key[BLOCK_SIZE];
 #if defined (__OpenBSD__) || defined (__OpenBSD_kernel__)
-    static const char randfile[] = "/dev/random";
+    static char randfile[] = "/dev/urandom";
+    /* /dev/random is only available if a hardware RNG is present. 
+     * If it's not, read() will set errno to EIO, not ENXIO somehow.
+     */
+    int fd_test = open ("/dev/random", O_RDONLY);
+    if (fd_test != -1)
+    {
+        if (read (fd_test, key, 1) != -1)
+            strncpy (randfile, "/dev/random", 12);
+        close (fd_test);
+    }
 #else
     static const char randfile[] = "/dev/urandom";
 #endif
-    uint8_t key[BLOCK_SIZE];
 
     /* Get non-predictible value as key for HMAC */
     int fd = open (randfile, O_RDONLY);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20090512/11b5e9d7/attachment.sig>


More information about the vlc-devel mailing list