]> sourceware.org Git - glibc.git/commitdiff
Add getentropy, getrandom, <sys/random.h> [BZ #17252]
authorFlorian Weimer <fweimer@redhat.com>
Mon, 12 Dec 2016 16:28:03 +0000 (17:28 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Mon, 12 Dec 2016 16:28:04 +0000 (17:28 +0100)
41 files changed:
ChangeLog
NEWS
manual/crypt.texi
manual/math.texi
stdlib/Makefile
stdlib/Versions
stdlib/getentropy.c [new file with mode: 0644]
stdlib/getrandom.c [new file with mode: 0644]
stdlib/sys/random.h [new file with mode: 0644]
stdlib/tst-getrandom.c [new file with mode: 0644]
sysdeps/arm/nacl/libc.abilist
sysdeps/unix/sysv/linux/aarch64/libc.abilist
sysdeps/unix/sysv/linux/alpha/libc.abilist
sysdeps/unix/sysv/linux/arm/libc.abilist
sysdeps/unix/sysv/linux/getentropy.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/getrandom.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/hppa/libc.abilist
sysdeps/unix/sysv/linux/i386/libc.abilist
sysdeps/unix/sysv/linux/ia64/libc.abilist
sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
sysdeps/unix/sysv/linux/microblaze/libc.abilist
sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
sysdeps/unix/sysv/linux/nios2/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
sysdeps/unix/sysv/linux/sh/libc.abilist
sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist

index 4d31f4f81e8fdd2f8516d7b8dce651ed10596d3a..91cc64d59fb5b8d61f91290ee2d8f8a906d784c8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,51 @@
+2016-12-12  Florian Weimer  <fweimer@redhat.com>
+
+       [BZ #17252]
+       Add getentropy, getrandom.
+       * stdlib/sys/random.h: New file.
+       (headers): Add it.
+       * stdlib/Makefile (routines): Add getentropy, getrandom.
+       (tests): Add tst-getrandom.
+       * stdlib/Versions (GLIBC_2.25): Add getrandom, getentropy.
+       * stdlib/getentropy.c: New file.
+       * stdlib/getrandom.c: Likewise.
+       * stdlib/tst-getrandom.c: Likewise.
+       * sysdeps/unix/sysv/linux/getentropy.c: Likewise.
+       * sysdeps/unix/sysv/linux/getrandom.c: Likewise.
+       * manual/crypt.texi (Unpredictable Bytes): New section.
+       * manual/math.texi (Pseudo-Random Numbers): Add cross-reference.
+       * sysdeps/arm/nacl/libc.abilist: Add getrandom, getentropy.
+       * sysdeps/unix/sysv/linux/aarch64/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/alpha/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/arm/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/hppa/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/i386/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/ia64/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/microblaze/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/nios2/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist:
+       Likewise.
+       * sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/sh/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/x86_64/64/libc.abilist: Likewise.
+       * sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist: Likewise.
+
 2016-12-10  Florian Weimer  <fweimer@redhat.com>
 
        [BZ #20956]
diff --git a/NEWS b/NEWS
index 4fe8886e837608e9e02fab066c2e238452ee052f..64ed6593cb143e959a12a6c8ca92e890d1d2a7c3 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -96,6 +96,9 @@ Version 2.25
   affect the ABI of other libraries that use this type in their interfaces,
   if they are compiled or used with those options.
 
+* The getentropy and getrandom functions, and the <sys/random.h> header file
+  have been added.
+
 * The buffer size for byte-oriented stdio streams is now limited to 8192
   bytes by default.  Previously, on Linux, the default buffer size on most
   file systems was 4096 bytes (and thus remains unchanged), except on
index 9f4474023e15709ca202b153f8b47930b18d57f1..4ab512bb37769a9df07fc3d352f2ed2b44046136 100644 (file)
@@ -45,6 +45,7 @@ encrypted authentication use normal DES.
 * getpass::                     Prompting the user for a password.
 * crypt::                       A one-way function for passwords.
 * DES Encryption::              Routines for DES encryption.
+* Unpredictable Bytes::         Randomness for cryptography purposes.
 @end menu
 
 @node Legal Problems
@@ -428,3 +429,114 @@ each byte.
 The @code{ecb_crypt}, @code{cbc_crypt}, and @code{des_setparity}
 functions and their accompanying macros are all defined in the header
 @file{rpc/des_crypt.h}.
+
+@node Unpredictable Bytes
+@section Generating Unpredictable Bytes
+
+Some cryptographic applications (such as session key generation) need
+unpredictable bytes.
+
+In general, application code should use a deterministic random bit
+generator, which could call the @code{getentropy} function described
+below internally to obtain randomness to seed the generator.  The
+@code{getrandom} function is intended for low-level applications which
+need additional control over the blocking behavior.
+
+@comment sys/random.h
+@comment GNU
+@deftypefun int getentropy (void *@var{buffer}, size_t @var{length})
+@safety{@mtsafe{}@assafe{}@acsafe{}}
+
+This function writes @var{length} bytes of random data to the array
+starting at @var{buffer}, which must be at most 256 bytes long.  The
+function returns zero on success.  On failure, it returns @code{-1} and
+@code{errno} is updated accordingly.
+
+The @code{getentropy} function is declared in the header file
+@file{sys/random.h}.  It is derived from OpenBSD.
+
+The @code{getentropy} function is not a cancellation point.  A call to
+@code{getentropy} can block if the system has just booted and the kernel
+entropy pool has not yet been initialized.  In this case, the function
+will keep blocking even if a signal arrives, and return only after the
+entropy pool has been initialized.
+
+The @code{getentropy} function can fail with several errors, some of
+which are listed below.
+
+@table @code
+@item ENOSYS
+The kernel does not implement the required system call.
+
+@item EFAULT
+The combination of @var{buffer} and @var{length} arguments specifies
+an invalid memory range.
+
+@item EIO
+More than 256 bytes of randomness have been requested, or the buffer
+could not be overwritten with random data for an unspecified reason.
+
+@end table
+
+@end deftypefun
+
+@comment sys/random.h
+@comment GNU
+@deftypefun ssize_t getrandom (void *@var{buffer}, size_t @var{length}, unsigned int @var{flags})
+@safety{@mtsafe{}@assafe{}@acsafe{}}
+
+This function writes @var{length} bytes of random data to the array
+starting at @var{buffer}.  On success, this function returns the number
+of bytes which have been written to the buffer (which can be less than
+@var{length}).  On error, @code{-1} is returned, and @code{errno} is
+updated accordingly.
+
+The @code{getrandom} function is declared in the header file
+@file{sys/random.h}.  It is a GNU extension.
+
+The following flags are defined for the @var{flags} argument:
+
+@table @code
+@item GRND_RANDOM
+Use the @file{/dev/random} (blocking) pool instead of the
+@file{/dev/urandom} (non-blocking) pool to obtain randomness.  If the
+@code{GRND_RANDOM} flag is specified, the @code{getrandom} function can
+block even after the randomness source has been initialized.
+
+@item GRND_NONBLOCK
+Instead of blocking, return to the caller immediately if no data is
+available.
+@end table
+
+The @code{getrandom} function is a cancellation point.
+
+Obtaining randomness from the @file{/dev/urandom} pool (i.e., a call
+without the @code{GRND_RANDOM} flag) can block if the system has just
+booted and the pool has not yet been initialized.
+
+The @code{getrandom} function can fail with several errors, some of
+which are listed below.  In addition, the function may not fill the
+buffer completely and return a value less than @var{length}.
+
+@table @code
+@item ENOSYS
+The kernel does not implement the @code{getrandom} system call.
+
+@item EAGAIN
+No random data was available and @code{GRND_NONBLOCK} was specified in
+@var{flags}.
+
+@item EFAULT
+The combination of @var{buffer} and @var{length} arguments specifies
+an invalid memory range.
+
+@item EINTR
+The system call was interrupted.  During the system boot process, before
+the kernel randomness pool is initialized, this can happen even if
+@var{flags} is zero.
+
+@item EINVAL
+The @var{flags} argument contains an invalid combination of flags.
+@end table
+
+@end deftypefun
index 5ad8732eca187bc5d6ca845aa92de6822370c72b..9b699f1defc9bb607c17de4bccf7c02905423f38 100644 (file)
@@ -1443,7 +1443,8 @@ is convenient when you are debugging a program, but it is unhelpful if
 you want the program to behave unpredictably.  If you want a different
 pseudo-random series each time your program runs, you must specify a
 different seed each time.  For ordinary purposes, basing the seed on the
-current time works well.
+current time works well.  For random numbers in cryptography,
+@pxref{Unpredictable Bytes}.
 
 You can obtain repeatable sequences of numbers on a particular machine type
 by specifying the same initial seed value for the random number
index 3cce9d98d42ae9fc4972fb1a9bee2df23794b260..d6512639e0c96eb9c7e3b8e7ff9877a958ba42dc 100644 (file)
@@ -28,7 +28,7 @@ headers       := stdlib.h bits/stdlib.h bits/stdlib-ldbl.h bits/stdlib-float.h      \
           errno.h sys/errno.h bits/errno.h                                   \
           ucontext.h sys/ucontext.h                                          \
           alloca.h fmtmsg.h                                                  \
-          bits/stdlib-bsearch.h
+          bits/stdlib-bsearch.h sys/random.h
 
 routines       :=                                                            \
        atof atoi atol atoll                                                  \
@@ -45,7 +45,7 @@ routines      :=                                                            \
        srand48 seed48 lcong48                                                \
        drand48_r erand48_r lrand48_r nrand48_r mrand48_r jrand48_r           \
        srand48_r seed48_r lcong48_r                                          \
-       drand48-iter                                                          \
+       drand48-iter getrandom getentropy                                     \
        strfromf strfromd strfroml                                            \
        strtol strtoul strtoll strtoull                                       \
        strtol_l strtoul_l strtoll_l strtoull_l                               \
@@ -79,7 +79,8 @@ tests         := tst-strtol tst-strtod testmb testrand testsort testdiv   \
                   tst-setcontext3 tst-tls-atexit-nodelete                  \
                   tst-strtol-locale tst-strtod-nan-locale tst-strfmon_l    \
                   tst-quick_exit tst-thread-quick_exit tst-width           \
-                  tst-width-stdint tst-strfrom tst-strfrom-locale
+                  tst-width-stdint tst-strfrom tst-strfrom-locale \
+                  tst-getrandom
 tests-static   := tst-secure-getenv
 ifeq ($(have-cxx-thread_local),yes)
 CFLAGS-tst-quick_exit.o = -std=c++11
index 54416b7ea61a80796196499d72c85fecfab57cc9..415b9945e2e5fd9c66662e7692adc79920eedc57 100644 (file)
@@ -115,6 +115,7 @@ libc {
   GLIBC_2.25 {
     # s*
     strfromd; strfromf; strfroml;
+    getrandom; getentropy;
   }
   GLIBC_PRIVATE {
     # functions which have an additional interface since they are
diff --git a/stdlib/getentropy.c b/stdlib/getentropy.c
new file mode 100644 (file)
index 0000000..32a24c7
--- /dev/null
@@ -0,0 +1,31 @@
+/* Stub for getentropy.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <sys/random.h>
+#include <errno.h>
+
+/* Write LENGTH bytes of randomness starting at BUFFER.  Return 0 on
+   success and -1 on failure.  */
+ssize_t
+getentropy (void *buffer, size_t length)
+{
+  __set_errno (ENOSYS);
+  return -1;
+}
+
+stub_warning (getentropy)
diff --git a/stdlib/getrandom.c b/stdlib/getrandom.c
new file mode 100644 (file)
index 0000000..a24727b
--- /dev/null
@@ -0,0 +1,31 @@
+/* Stub for getrandom.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <sys/random.h>
+#include <errno.h>
+
+/* Write LENGTH bytes of randomness starting at BUFFER.  Return the
+   number of bytes written, or -1 on error.  */
+ssize_t
+getrandom (void *buffer, size_t length, unsigned int flags)
+{
+  __set_errno (ENOSYS);
+  return -1;
+}
+
+stub_warning (getrandom)
diff --git a/stdlib/sys/random.h b/stdlib/sys/random.h
new file mode 100644 (file)
index 0000000..610b468
--- /dev/null
@@ -0,0 +1,42 @@
+/* Interfaces for obtaining random bytes.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_RANDOM_H
+#define _SYS_RANDOM_H 1
+
+#include <features.h>
+#include <sys/types.h>
+
+/* Flags for use with getrandom.  */
+#define GRND_NONBLOCK 0x01
+#define GRND_RANDOM 0x02
+
+__BEGIN_DECLS
+
+/* Write LENGTH bytes of randomness starting at BUFFER.  Return the
+   number of bytes written, or -1 on error.  */
+ssize_t getrandom (void *__buffer, size_t __length,
+                   unsigned int __flags) __wur;
+
+/* Write LENGTH bytes of randomness starting at BUFFER.  Return 0 on
+   success or -1 on error.  */
+int getentropy (void *__buffer, size_t __length) __wur;
+
+__END_DECLS
+
+#endif /* _SYS_RANDOM_H */
diff --git a/stdlib/tst-getrandom.c b/stdlib/tst-getrandom.c
new file mode 100644 (file)
index 0000000..a76cd18
--- /dev/null
@@ -0,0 +1,245 @@
+/* Tests for the getentropy, getrandom functions.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/random.h>
+
+/* Set to true if any errors are encountered.  */
+static bool errors;
+
+/* Test getrandom with a single buffer length.  NB: The passed-in
+   buffer must have room for four extra bytes after the specified
+   length, which are used to test that getrandom leaves those bytes
+   unchanged.  */
+static void
+test_length (char *buffer, size_t length, unsigned int flags)
+{
+  memset (buffer, 0, length);
+  strcpy (buffer + length, "123");
+  ssize_t ret = getrandom (buffer, length, flags);
+  if (ret < 0)
+    {
+      /* EAGAIN is an expected error with GRND_RANDOM and
+         GRND_NONBLOCK.  */
+      if ((flags & GRND_RANDOM)
+          && (flags & GRND_NONBLOCK)
+          && errno == EAGAIN)
+        return;
+      printf ("error: getrandom (%zu, 0x%x): %m\n", length, flags);
+      errors = true;
+      return;
+    }
+ if (ret != length)
+    {
+      if (flags & GRND_RANDOM)
+        {
+          if (ret == 0 || ret > length)
+            {
+              printf ("error: getrandom (%zu, 0x%x) returned %zd\n",
+                      length, flags, ret);
+              errors = true;
+            }
+        }
+      else
+        {
+          printf ("error: getrandom (%zu, 0x%x) returned %zd\n",
+                  length, flags, ret);
+          errors = true;
+        }
+    }
+  if (length >= 7)
+    {
+      /* One spurious test failure in 2**56 is sufficiently
+         unlikely.  */
+      int non_null = 0;
+      for (int i = 0; i < length; ++i)
+        non_null += buffer[i] != 0;
+      if (non_null == 0)
+        {
+          printf ("error: getrandom (%zu, 0x%x) returned all-zero bytes\n",
+                  length, flags);
+          errors = true;
+        }
+    }
+  if (memcmp (buffer + length, "123", 4) != 0)
+    {
+      printf ("error: getrandom (%zu, 0x%x) wrote spurious bytes\n",
+              length, flags);
+      errors = true;
+    }
+}
+
+/* Call getrandom repeatedly to fill the buffer.  */
+static bool
+getrandom_full (char *buffer, size_t length, unsigned int flags)
+{
+  char *end = buffer + length;
+  while (buffer < end)
+    {
+      ssize_t ret = getrandom (buffer, end - buffer, flags);
+      if (ret < 0)
+        {
+          printf ("error: getrandom (%zu, 0x%x): %m\n", length, flags);
+          errors = true;
+          return false;
+        }
+      buffer += ret;
+    }
+
+  return true;
+}
+
+static void
+test_flags (unsigned int flags)
+{
+  /* Test various lengths, but only for !GRND_RANDOM, to conserve
+     entropy.  */
+  {
+    enum { max_length = 300 };
+    char buffer[max_length + 4];
+    if (flags & GRND_RANDOM)
+      test_length (buffer, 0, flags);
+    else
+      {
+        for (int length = 0; length <= 9; ++length)
+          test_length (buffer, length, flags);
+        test_length (buffer, 16, flags);
+        test_length (buffer, max_length, flags);
+      }
+  }
+
+  /* Test that getrandom returns different data.  */
+  if (!(flags & GRND_NONBLOCK))
+    {
+      char buffer1[8];
+      memset (buffer1, 0, sizeof (buffer1));
+
+      char buffer2[8];
+      memset (buffer2, 0, sizeof (buffer2));
+
+      if (getrandom_full (buffer1, sizeof (buffer1), flags)
+          && getrandom_full (buffer2, sizeof (buffer2), flags))
+        {
+          /* The probability that these two 8-byte buffers are equal
+             is very small (assuming that two subsequent calls to
+             getrandom result are independent, uniformly distributed
+             random variables).  */
+          if (memcmp (buffer1, buffer2, sizeof (buffer1)) == 0)
+            {
+              printf ("error: getrandom returns constant value\n");
+              errors = true;
+            }
+        }
+    }
+}
+
+static void
+test_getentropy (void)
+{
+  char buf[16];
+  memset (buf, '@', sizeof (buf));
+  if (getentropy (buf, 0) != 0)
+    {
+      printf ("error: getentropy zero length: %m\n");
+      errors = true;
+      return;
+    }
+  for (size_t i = 0; i < sizeof (buf); ++i)
+    if (buf[i] != '@')
+      {
+        printf ("error: getentropy modified zero-length buffer\n");
+        errors = true;
+        return;
+      }
+
+  if (getentropy (buf, sizeof (buf)) != 0)
+    {
+      printf ("error: getentropy buf: %m\n");
+      errors = true;
+      return;
+    }
+
+  char buf2[256];
+  _Static_assert (sizeof (buf) < sizeof (buf2), "buf and buf2 compatible");
+  memset (buf2, '@', sizeof (buf2));
+  if (getentropy (buf2, sizeof (buf)) != 0)
+    {
+      printf ("error: getentropy buf2: %m\n");
+      errors = true;
+      return;
+    }
+
+  /* The probability that these two buffers are equal is very
+     small. */
+  if (memcmp (buf, buf2, sizeof (buf) == 0))
+    {
+      printf ("error: getentropy appears to return constant bytes\n");
+      errors = true;
+      return;
+    }
+
+  for (size_t i = sizeof (buf); i < sizeof (buf2); ++i)
+    if (buf2[i] != '@')
+      {
+        printf ("error: getentropy wrote beyond the end of the buffer\n");
+        errors = true;
+        return;
+      }
+
+  char buf3[257];
+  if (getentropy (buf3, sizeof (buf3)) == 0)
+    {
+      printf ("error: getentropy successful for 257 byte buffer\n");
+      errors = true;
+      return;
+    }
+  if (errno != EIO)
+    {
+      printf ("error: getentropy wrong error for 257 byte buffer: %m\n");
+      errors = true;
+      return;
+    }
+}
+
+static int
+do_test (void)
+{
+  /* Check if getrandom is not supported by this system.  */
+  if (getrandom (NULL, 0, 0) == -1 && errno == ENOSYS)
+    return 77;
+
+  for (int use_random = 0; use_random < 2; ++use_random)
+    for (int use_nonblock = 0; use_nonblock < 2; ++use_nonblock)
+      {
+        unsigned int flags = 0;
+        if (use_random)
+          flags |= GRND_RANDOM;
+        if (use_nonblock)
+          flags |= GRND_NONBLOCK;
+        test_flags (flags);
+      }
+
+  test_getentropy ();
+
+  return errors;
+}
+
+#include <support/test-driver.c>
index 807e43d9355563439e3046627e6ae249c8975943..0a39f4d13e45895e1da5a92c3dc93ebda2ce1e30 100644 (file)
@@ -1843,6 +1843,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 gnu_dev_major F
 GLIBC_2.25 gnu_dev_makedev F
 GLIBC_2.25 gnu_dev_minor F
index 77accdf5c1c53f57feb57cf72aafa9da5327c547..8d3435f210fe9e78cb756d27391774162659927a 100644 (file)
@@ -2090,6 +2090,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 659b7fcf37b9711c569778f7c35bcec25f14e792..2381616b71ebf7b7919f10d96e3ceae43dd7b328 100644 (file)
@@ -2001,6 +2001,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 8bc979ad7fdc18884b151a01641dfabbcf2b556e..e2dbf6fee7284f3f5f8775efaef7268e37724569 100644 (file)
@@ -91,6 +91,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
diff --git a/sysdeps/unix/sysv/linux/getentropy.c b/sysdeps/unix/sysv/linux/getentropy.c
new file mode 100644 (file)
index 0000000..1778632
--- /dev/null
@@ -0,0 +1,73 @@
+/* Implementation of getentropy based on the getrandom system call.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <sys/random.h>
+#include <assert.h>
+#include <errno.h>
+#include <unistd.h>
+
+#ifdef __NR_getrandom
+/* Write LENGTH bytes of randomness starting at BUFFER.  Return 0 on
+   success and -1 on failure.  */
+int
+getentropy (void *buffer, size_t length)
+{
+  /* The interface is documented to return EIO for buffer lengths
+     longer than 256 bytes.  */
+  if (length > 256)
+    {
+      __set_errno (EIO);
+      return -1;
+    }
+
+  /* Try to fill the buffer completely.  Even with the 256 byte limit
+     above, we might still receive an EINTR error (when blocking
+     during boot).  */
+  void *end = buffer + length;
+  while (buffer < end)
+    {
+      /* NB: No cancellation point.  */
+      ssize_t bytes = INLINE_SYSCALL_CALL (getrandom, buffer, end - buffer, 0);
+      if (bytes < 0)
+        {
+          if (errno == EINTR)
+            /* Try again if interrupted by a signal.  */
+            continue;
+          else
+            return -1;
+        }
+      if (bytes == 0)
+        {
+          /* No more bytes available.  This should not happen under
+             normal circumstances.  */
+          __set_errno (EIO);
+          return -1;
+        }
+      /* Try again in case of a short read.  */
+      buffer += bytes;
+    }
+  return 0;
+}
+#else
+int
+getentropy (void *buffer, size_t length)
+{
+  __set_errno (ENOSYS);
+  return -1;
+}
+#endif
diff --git a/sysdeps/unix/sysv/linux/getrandom.c b/sysdeps/unix/sysv/linux/getrandom.c
new file mode 100644 (file)
index 0000000..7df0092
--- /dev/null
@@ -0,0 +1,43 @@
+/* Implementation of the getrandom system call.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <sys/random.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sysdep-cancel.h>
+
+#ifdef __NR_getrandom
+/* Write LENGTH bytes of randomness starting at BUFFER.  Return 0 on
+   success and -1 on failure.  */
+ssize_t
+getrandom (void *buffer, size_t length, unsigned int flags)
+{
+  return SYSCALL_CANCEL (getrandom, buffer, length, flags);
+}
+#else
+/* Always provide a definition, even if the kernel headers lack the
+   system call number. */
+ssize_t
+getrandom (void *buffer, size_t length, unsigned int flags)
+{
+  /* Ideally, we would add a cancellation point here, but we currently
+     cannot do so inside libc.  */
+  __set_errno (ENOSYS);
+  return -1;
+}
+#endif
index 299b705156f5b36131784e3efe801bb8b171de83..231f039e69b7f780110d6b34b24357925f9b5978 100644 (file)
@@ -1855,6 +1855,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index f00345f6739412a9fa20e1f65017e5aab6beba52..5d0159348b9439941994f7044748c6b51fb8e6a7 100644 (file)
@@ -2013,6 +2013,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index e5fcf88b18cb7b2cd88e036e05b7e8c6dcbfbc70..00ef4d77ab56436125855e27159b72d17ec6af9e 100644 (file)
@@ -1877,6 +1877,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 8f382f680bf07cfc408b024a4384c7295bc864ff..7de0b0e9bd56d75eccc0a4c16b7c30173f1b5798 100644 (file)
@@ -92,6 +92,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 320b7fef5fa1bfbe40d0e57a25120b0e5c99c95b..93e11245920bc65a4a964c8620ff8cab5c135944 100644 (file)
@@ -1969,6 +1969,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 21b14262c6eca8781fd3134381ff896ad5e5d7eb..da70083bdda0c69a96281c9317ae1d049d6a5c10 100644 (file)
@@ -2090,6 +2090,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 5c4b596c5468f4a5031e99f0da66f3b4e029cae7..570bbf6791729b2d11cace76d7e73d6e005f619c 100644 (file)
@@ -1944,6 +1944,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 001fa6c12962549b72bcb4bf97f09b8f46bf23b2..5022e5a186c66c601abe3015636bec35f4c6de89 100644 (file)
@@ -1942,6 +1942,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 2d870013f72b944ba68de284e6f71d254db8b965..ca9f91924f9b4eee48e71fc3c8a0982e2aaf81f1 100644 (file)
@@ -1940,6 +1940,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index aa1ee66ab7074fb58f044cef37245a5e3505bf64..2a090db5c6febd6ea73b68b69b8e9d91a8c71bec 100644 (file)
@@ -1935,6 +1935,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 2471d68e87e7b2f3cc3e722ea8f489ca23b00493..d10bb32513ba165d34279473f6ea387f687d0009 100644 (file)
@@ -2131,6 +2131,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 4ba31469027910e2dd27547564916ec0d4449498..a7e688bed905c83299d934b0a847cd6335871af0 100644 (file)
@@ -1973,6 +1973,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 0557c1613083452b0be3a925a34a105a1aaac4da..8c32c33076336e6d232295e016e8d89bab9286f4 100644 (file)
@@ -1978,6 +1978,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 821384ebd6877b0ffca26fdbfb3f262da10eb3e5..2a13e49a9ea21cb9763c252fd833d9caa51b3f2c 100644 (file)
@@ -2178,6 +2178,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index c40a3f1cba8f94b13b1f6eb4cd5994c9db814005..4f29a6db6ee3988c8b8846bde2461edc3d5240cd 100644 (file)
@@ -92,6 +92,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 5b39a60638e72453a1adfe6c572f01da72436c46..1e9d4f4d914f56bb229eb5ceea7708eddd89fc15 100644 (file)
@@ -1973,6 +1973,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index a9db32f6c2c23b324879e8c1b48c36b6d8d4685c..6b3d9320e1d080e0343e7a13d84f51d6e302657d 100644 (file)
@@ -1874,6 +1874,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 294af0a5013d7af9537b287ad28e1fa19ec93dca..93b79d3758575c299047c007649db96f85dc5b37 100644 (file)
@@ -1859,6 +1859,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 32747bd22da61f738af1d64380fecca5f83f3155..a29b0c149b9d30e36ff6d203b78515bb7be20277 100644 (file)
@@ -1965,6 +1965,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index b0ac4d447282bb86c828543a45c1c86b91eb8129..8ac06e9b01f817adf540de4c4da50ae247e1dbb3 100644 (file)
@@ -1903,6 +1903,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 4d92d81b85e7be2d99a971421ba5b52937e3601d..208e7425ff832cb1b488366a65ad80ee5dd25cad 100644 (file)
@@ -2097,6 +2097,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index a68aef7bb75fb7eba2301ab28d99acbd8eee9f29..3d7836d5468bef112301d14d988089723aa0e811 100644 (file)
@@ -2097,6 +2097,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index 4d92d81b85e7be2d99a971421ba5b52937e3601d..208e7425ff832cb1b488366a65ad80ee5dd25cad 100644 (file)
@@ -2097,6 +2097,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index b8623fc59f69c112921627b73cb137730e603253..32cd1e07b480e0601f19a83d6abf3df46b0982a1 100644 (file)
@@ -1854,6 +1854,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
index a61d874dfef356561bf5b8f7b7742ef57efd9f8d..4ff7acf30000daa9ff4db11375a1dbf82d4d6b0a 100644 (file)
@@ -2097,6 +2097,8 @@ GLIBC_2.23 fts64_set F
 GLIBC_2.24 GLIBC_2.24 A
 GLIBC_2.24 quick_exit F
 GLIBC_2.25 GLIBC_2.25 A
+GLIBC_2.25 getentropy F
+GLIBC_2.25 getrandom F
 GLIBC_2.25 strfromd F
 GLIBC_2.25 strfromf F
 GLIBC_2.25 strfroml F
This page took 0.241182 seconds and 5 git commands to generate.