first user of this applet will be LEDE (OpenWrt) to save an urandom seed
using getrandom() (so we are sure /dev/urandom pool is initialized)

function                                             old     new   delta
getrandom_main                                         -     162    +162
.rodata                                           156181  156208     +27
applet_names                                        2536    2546     +10
applet_main                                         2936    2944      +8
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 3/0 up/down: 207/0)             Total: 207 bytes

v2: reduce size by 40 bytes thanks to Tito <farmat...@tiscali.it>
v3: release under GPLv2 or later, thanks Bernhard Reutner-Fischer 
<rep.dot....@gmail.com>

Signed-off-by: Etienne CHAMPETIER <champetier.etie...@gmail.com>
---
 include/applets.src.h  |  1 +
 util-linux/Config.src  |  8 ++++++++
 util-linux/Kbuild.src  |  1 +
 util-linux/getrandom.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+)
 create mode 100644 util-linux/getrandom.c

diff --git a/include/applets.src.h b/include/applets.src.h
index 6e1b02f..b617d14 100644
--- a/include/applets.src.h
+++ b/include/applets.src.h
@@ -153,6 +153,7 @@ IF_FTPPUT(APPLET_ODDNAME(ftpput, ftpgetput, BB_DIR_USR_BIN, 
BB_SUID_DROP, ftpput
 IF_FUSER(APPLET(fuser, BB_DIR_USR_BIN, BB_SUID_DROP))
 IF_GETENFORCE(APPLET(getenforce, BB_DIR_USR_SBIN, BB_SUID_DROP))
 IF_GETOPT(APPLET(getopt, BB_DIR_BIN, BB_SUID_DROP))
+IF_GETRANDOM(APPLET(getrandom, BB_DIR_USR_BIN, BB_SUID_DROP))
 IF_GETSEBOOL(APPLET(getsebool, BB_DIR_USR_SBIN, BB_SUID_DROP))
 IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))
 IF_HDPARM(APPLET(hdparm, BB_DIR_SBIN, BB_SUID_DROP))
diff --git a/util-linux/Config.src b/util-linux/Config.src
index 922cabd..9f47db7 100644
--- a/util-linux/Config.src
+++ b/util-linux/Config.src
@@ -304,6 +304,14 @@ config FEATURE_GETOPT_LONG
        help
          Enable support for long options (option -l).
 
+config GETRANDOM
+       bool "getrandom"
+       default y
+       select PLATFORM_LINUX
+       help
+         The getrandom utility get NBYTES random bytes using getrandom()
+         syscall (available since Linux 3.17)
+
 config HEXDUMP
        bool "hexdump"
        default y
diff --git a/util-linux/Kbuild.src b/util-linux/Kbuild.src
index 0b87c52..b164fb7 100644
--- a/util-linux/Kbuild.src
+++ b/util-linux/Kbuild.src
@@ -19,6 +19,7 @@ lib-$(CONFIG_FLOCK)             += flock.o
 lib-$(CONFIG_FREERAMDISK)       += freeramdisk.o
 lib-$(CONFIG_FSCK_MINIX)        += fsck_minix.o
 lib-$(CONFIG_GETOPT)            += getopt.o
+lib-$(CONFIG_GETRANDOM)         += getrandom.o
 lib-$(CONFIG_HEXDUMP)           += hexdump.o
 lib-$(CONFIG_HWCLOCK)           += hwclock.o
 lib-$(CONFIG_IPCRM)             += ipcrm.o
diff --git a/util-linux/getrandom.c b/util-linux/getrandom.c
new file mode 100644
index 0000000..d61b53a
--- /dev/null
+++ b/util-linux/getrandom.c
@@ -0,0 +1,43 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * getrandom.c - read random bytes using getrandom() syscall
+ *
+ * Copyright (C) 2016 Etienne Champetier <champetier.etie...@gmail.com>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+
+//usage:#define getrandom_trivial_usage
+//usage:       "N"
+//usage:#define getrandom_full_usage "\n\n"
+//usage:       "Read N random bytes using getrandom()"
+
+#include <sys/syscall.h>
+#include "libbb.h"
+
+int getrandom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int getrandom_main(int argc, char **argv)
+{
+       int nbytes, len;
+       char buf[256]; /* getrandom(2) calls up to 256 bytes always succeed */
+
+       if (argc != 2)
+               bb_show_usage();
+
+       if (isatty(STDOUT_FILENO))
+               bb_error_msg_and_die("isatty");
+
+       nbytes = xatoi_range(argv[1], 1, INT_MAX);
+
+       for (len = sizeof(buf); nbytes > 0; nbytes -= sizeof(buf)) {
+               if (nbytes < sizeof(buf)) {
+                       len = nbytes;
+               }
+               if (syscall(SYS_getrandom, buf, len, 0) != len) {
+                       bb_perror_msg_and_die("getrandom");
+               }
+               xwrite(STDOUT_FILENO, buf, len);
+       }
+
+       return EXIT_SUCCESS;
+}
-- 
2.5.5

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to