[PATCH] getrandom: new applet

Etienne Champetier champetier.etienne at gmail.com
Tue Jun 28 22:08:00 UTC 2016


Hi Tito,

2016-06-28 21:37 GMT+02:00 Tito <farmatito at tiscali.it>:
>
>
> On 06/28/2016 06:56 PM, Etienne CHAMPETIER wrote:
>>
>> 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                                         -     178    +178
>> .rodata                                           156181  156232     +51
>> applet_names                                        2536    2546     +10
>> applet_main                                         2936    2944      +8
>>
>> ------------------------------------------------------------------------------
>> (add/remove: 2/0 grow/shrink: 3/0 up/down: 247/0)             Total: 247
>> bytes
>>
>> Signed-off-by: Etienne CHAMPETIER <champetier.etienne at gmail.com>
>> ---
>>  include/applets.src.h  |  1 +
>>  util-linux/Config.src  |  8 ++++++++
>>  util-linux/Kbuild.src  |  1 +
>>  util-linux/getrandom.c | 47
>> +++++++++++++++++++++++++++++++++++++++++++++++
>>  4 files changed, 57 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..165b492
>> --- /dev/null
>> +++ b/util-linux/getrandom.c
>> @@ -0,0 +1,47 @@
>> +/* vi: set sw=4 ts=4: */
>> +/*
>> + * getrandom.c - read random bytes using getrandom() syscall
>> + *
>> + * Copyright (C) 2016 Etienne Champetier <champetier.etienne at gmail.com>
>> + *
>> + * Licensed under GPLv2, 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))
>
> Hi, just a few hints to reduce size.
>

Thanks a lot, now at 207 bytes (was 247 bytes)

>
> Maybe "Not a tty" is shorter or bb_perror_msg_and_die("isatty");
>
>> +               bb_error_msg_and_die("Not outputting to a tty");
>
>
>  xatoi_range(const char *numstr, type lower, type upper)
> or
>  xatoi(const char *numstr)
>
>> +       nbytes = atoi(argv[1]);
>> +       if (nbytes <= 0)
>> +               bb_error_msg_and_die("N <= 0");
>> +
>> +       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");
>> +               }
>
>
> void xwrite(int fd, const void *buf, size_t count)
>
>> +               if (write(STDOUT_FILENO, buf, len) != len) {
>> +                       bb_perror_msg_and_die("write");
>> +               }
>> +       }
>> +
>> +       return EXIT_SUCCESS;
>> +}
>>
>
> Ciao,
> Tito
>


More information about the busybox mailing list