Skip to contentSkip to conversation

lib/randutils: use getrandom(2) when it is available

PATCH In review Sami Kerola 2 participants 3 messages Dec 4, 2016 – Dec 11, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/randutils: use getrandom(2) when it is available

System call getrandom(2) is relatively new, available since kernel 3.17 but not supported by glibc 2.24. That in mind autotools is made to check availability of this function and keep old code as fallback. It is reasonable assume it will take years before the syscall(2) and fallback are unproblematic to remove.

One might ask why bother using getrandom(2). Main reason is to avoid unnecessary system calls to achieve exactly same end result. That demonstrated with 'strace -c ./mcookie' showing 36 calls before, and 32 after this change. Secondly the getrandom(2) function got to kernel with promise it can be used to avoid file descriptor run down, and since uuidd uses random_get_bytes() it should fulfill it's promise here.

Reference: http://man7.org/linux/man-pages/man2/getrandom.2.html
Reference: https://lwn.net/Articles/606141/

---
 configure.ac    |  8 ++++++++
 lib/randutils.c | 60 ++++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 53 insertions(+), 15 deletions(-)
configure.ac +8 -0
index dddc6a9..788cd53 100644
@@ -427,6 +427,14 @@ AC_CHECK_FUNCS([reboot], [have_reboot=yes],[have_reboot=no])
 
 AM_CONDITIONAL([HAVE_OPENAT], [test "x$have_openat" = xyes])
 
+AC_CHECK_FUNCS([getrandom], [], [
+	AS_IF([UL_CHECK_SYSCALL([getrandom])
+	  test "$ul_cv_syscall_getrandom" = SYS_getrandom],
+		[AC_DEFINE([SYSTEM_GETRANDOM], [1],
+			[Define if getrandom syscall is missing from libc])
+	])
+])
+
 dnl lib/mononotic.c may require -lrt
 AC_CHECK_FUNCS([clock_gettime], [],
 	[AC_CHECK_LIB([rt], [clock_gettime], [REALTIME_LIBS="-lrt"])]
lib/randutils.c +45 -15

109 lines not on this page · raw message

Signature · 1 line
2.10.2
maintainer
#1 lore raw

configure.ac AC_CHECK_FUNCS([reboot], [have_reboot=yes],[have_reboot=no]) · line 436

Quoted 7 lines · Sami Kerola
configure.ac +7 -0
+AC_CHECK_FUNCS([getrandom], [], [
+	AS_IF([UL_CHECK_SYSCALL([getrandom])
+	  test "$ul_cv_syscall_getrandom" = SYS_getrandom],
+		[AC_DEFINE([SYSTEM_GETRANDOM], [1],
+			[Define if getrandom syscall is missing from libc])
+	])
+])

I have doubts we need SYSTEM_GETRANDOM macro:

Why not like in another utils (see sched utils for example):

#ifndef HAVE_FALLOCATE

# include <sys/syscall.h>
#else
# include <linux/random.h>
#endif

/* usable kernel-headers, but old glibc-headers */
#if defined (__linux__) && !defined(SYS_getrandom) && defined(__NR_getrandom)

# define SYS_getrandom __NR_getrandom
#endif

/* libc without function, but we have syscal */
#if !defined(HAVE_GETRANDOM) && defined(SYS_getrandom) static int getrandom(void *buf, size_t buflen, unsigned int flags)

{
    return (syscall(SYS_getrandom, buf, buflen, flags));
}

#define HAVE_GETRANDOM
#endif

lib/randutils.c int random_get_fd(void) · line 93

Quoted 12 lines

/*
* Generate a stream of random nbytes into buf.
* Use /dev/urandom if possible, and if not,

lib/randutils.c +4 -1
@@ -72,10 +87,19 @@ int random_get_fd(void)
  */
 void random_get_bytes(void *buf, size_t nbytes)
 {
-	size_t i, n = nbytes;
+	size_t i;
+	unsigned char *cp = (unsigned char *)buf;
+
+#if defined(SYSTEM_GETRANDOM) || defined(HAVE_GETRANDOM)

#ifdef HAVE_GETRANDOM

lib/randutils.c void random_get_bytes(void *buf, size_t nbytes) · line 154

Quoted 7 lines
lib/randutils.c +5 -0
 const char *random_tell_source(void)
 {
+#if defined(SYSTEM_GETRANDOM)
+	return _("syscall(SYS_getrandom) function");
+#elif defined(HAVE_GETRANDOM)
+	return _("libc getrandom() function");
+#else

seems like overkill :-)

#ifdef HAVE_GETRANDOM
    return _("getrandom function");
#else
    ...


    Karel
Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw

On 9 December 2016 at 13:15, Karel Zak <[email protected]> wrote:

Thank you Karel for good and easy to action review.

Quoted 10 lines · Sami Kerola

configure.ac AC_CHECK_FUNCS([reboot], [have_reboot=yes],[have_reboot=no]) · line 436

Quoted 7 lines
configure.ac +7 -0
+AC_CHECK_FUNCS([getrandom], [], [
+     AS_IF([UL_CHECK_SYSCALL([getrandom])
+       test "$ul_cv_syscall_getrandom" = SYS_getrandom],
+             [AC_DEFINE([SYSTEM_GETRANDOM], [1],
+                     [Define if getrandom syscall is missing from libc])
+     ])
+])

I have doubts we need SYSTEM_GETRANDOM macro:

Removed, and getrandom() check added to long AC_CHECK_FUNCS list.

Quoted 13 lines

Why not like in another utils (see sched utils for example):

#ifndef HAVE_FALLOCATE

# include <sys/syscall.h>
#else
# include <linux/random.h>
#endif

/* usable kernel-headers, but old glibc-headers */
#if defined (__linux__) && !defined(SYS_getrandom) && defined(__NR_getrandom)

# define SYS_getrandom __NR_getrandom
#endif
Quoted 8 lines

/* libc without function, but we have syscal */
#if !defined(HAVE_GETRANDOM) && defined(SYS_getrandom) static int getrandom(void *buf, size_t buflen, unsigned int flags)

{
    return (syscall(SYS_getrandom, buf, buflen, flags));
}

#define HAVE_GETRANDOM
#endif

Done very similar way as above, with exception sys/syscall.h is included only when __linux__ is defined. I am not entirely sure, but osx compilation for example is less prone to break this way.

CC: Rudi
https://github.com/kerolasa/lelux-utiliteetit/blob/cc01c2dca4f62e36505570d5cb15f868aa44bf54/lib/randutils.c#L30

Quoted 25 lines

lib/randutils.c int random_get_fd(void) · line 93

Quoted 12 lines

/*
* Generate a stream of random nbytes into buf.
* Use /dev/urandom if possible, and if not,

lib/randutils.c +4 -1
@@ -72,10 +87,19 @@ int random_get_fd(void)
  */
 void random_get_bytes(void *buf, size_t nbytes)
 {
-     size_t i, n = nbytes;
+     size_t i;
+     unsigned char *cp = (unsigned char *)buf;
+
+#if defined(SYSTEM_GETRANDOM) || defined(HAVE_GETRANDOM)

#ifdef HAVE_GETRANDOM

lib/randutils.c void random_get_bytes(void *buf, size_t nbytes) · line 154

Quoted 7 lines
lib/randutils.c +5 -0
 const char *random_tell_source(void)
 {
+#if defined(SYSTEM_GETRANDOM)
+     return _("syscall(SYS_getrandom) function");
+#elif defined(HAVE_GETRANDOM)
+     return _("libc getrandom() function");
+#else

seems like overkill :-)

Quoted 4 lines
#ifdef HAVE_GETRANDOM
    return _("getrandom function");
#else
    ...

Unecessary fine grained detail from printiout is removed.

Updated changes are available in the git repository at:

git://github.com/kerolasa/lelux-utiliteetit.git getrandom

for you to fetch changes up to cc01c2dca4f62e36505570d5cb15f868aa44bf54:

lib/randutils: use getrandom(2) when it is available (2016-12-11
11:46:54 +0000)

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/