[vlc-commits] [Git][videolan/vlc][master] rand: use getrandom() where available

Steve Lhomme (@robUx4) gitlab at videolan.org
Tue Nov 3 04:35:11 UTC 2026



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
c6aef4be by Rémi Denis-Courmont at 2026-11-03T04:10:18+00:00
rand: use getrandom() where available

This works on non-ancient Linux and *BSD's.

Fixes #29758.

- - - - -


3 changed files:

- configure.ac
- meson.build
- src/posix/rand.c


Changes:

=====================================
configure.ac
=====================================
@@ -871,7 +871,7 @@ dnl Check for system libs needed
 need_libc=false
 
 dnl Check for usual libc functions
-AC_CHECK_FUNCS([accept4 dup3 fcntl flock fstatat fstatvfs fork getmntent_r getenv getpwuid_r isatty memalign mkostemp mmap open_memstream newlocale pipe2 posix_fadvise qsort_r setlocale uselocale wordexp])
+AC_CHECK_FUNCS([accept4 dup3 fcntl flock fstatat fstatvfs fork getmntent_r getenv getpwuid_r getrandom isatty memalign mkostemp mmap open_memstream newlocale pipe2 posix_fadvise qsort_r setlocale uselocale wordexp])
 AC_REPLACE_FUNCS([aligned_alloc asprintf atof atoll dirfd fdopendir flockfile fsync getdelim getpid gmtime_r lfind lldiv localtime_r memrchr nrand48 poll posix_memalign readv recvmsg rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy tfind timegm timespec_get strverscmp vasprintf writev])
 AC_CHECK_FUNC(fdatasync,,
   [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])


=====================================
meson.build
=====================================
@@ -618,6 +618,7 @@ check_functions = [
     ['fork',             '#include <unistd.h>'],
     ['getmntent_r',      '#include <mntent.h>'],
     ['getpwuid_r',       '#include <pwd.h>'],
+    ['getrandom',        '#include <sys/random.h>'],
     ['isatty',           '#include <unistd.h>'],
     ['isatty',           '#include <io.h>'],
     ['memalign',         '#include <malloc.h>'],


=====================================
src/posix/rand.c
=====================================
@@ -30,6 +30,9 @@
 #include <stdlib.h>
 
 #include <sys/types.h>
+#ifdef HAVE_GETRANDOM
+#include <sys/random.h>
+#endif
 #include <fcntl.h>
 #include <unistd.h>
 #include <vlc_fs.h>
@@ -38,6 +41,26 @@
 #include <vlc_tick.h>
 #include <vlc_threads.h>
 
+static ssize_t vlc_getrandom(unsigned char *buf, size_t len)
+{
+    ssize_t val;
+
+#ifdef HAVE_GETRANDOM
+    val = getrandom(buf, len, 0);
+    if (val >= 0)
+        return val;
+
+    /* ENOSYS fallback */
+#endif
+    int fd = vlc_open("/dev/urandom", O_RDONLY);
+    if (fd < 0)
+        return -1;
+
+    val = read(fd, buf, len);
+    vlc_close(fd);
+    return val;
+}
+
 /*
  * Pseudo-random number generator using a HMAC-MD5 in counter mode.
  * Probably not very secure (expert patches welcome) but definitely
@@ -49,18 +72,12 @@ static uint8_t okey[BLOCK_SIZE], ikey[BLOCK_SIZE];
 
 static void vlc_rand_init (void)
 {
-    uint8_t key[BLOCK_SIZE];
+    uint8_t key[BLOCK_SIZE] = { };
+    ssize_t val = 0;
 
     /* Get non-predictible value as key for HMAC */
-    int fd = vlc_open ("/dev/urandom", O_RDONLY);
-    if (fd == -1)
-        return; /* Uho! */
-
-    for (size_t i = 0; i < sizeof (key);)
-    {
-         ssize_t val = read (fd, key + i, sizeof (key) - i);
-         if (val > 0)
-             i += val;
+    for (size_t i = 0; i < sizeof (key) && val > 0; i += val) {
+        val = vlc_getrandom(key + i, sizeof (key) - i);
     }
 
     /* Precompute outer and inner keys for HMAC */
@@ -69,8 +86,6 @@ static void vlc_rand_init (void)
         okey[i] = key[i] ^ 0x5c;
         ikey[i] = key[i] ^ 0x36;
     }
-
-    vlc_close (fd);
 }
 
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/c6aef4be4c54353916fde26961900095871742a0

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/c6aef4be4c54353916fde26961900095871742a0
You're receiving this email because of your account on code.videolan.org. Manage all notifications: https://code.videolan.org/-/profile/notifications | Help: https://code.videolan.org/help




More information about the vlc-commits mailing list