Commit Graph

10 Commits

Author SHA1 Message Date
Jason A. Donenfeld
05c366a560 seedrng: prune header includes
Remove redundant includes.
We have platform specific handling in libbb.h and platform.h so we can
handle quirks in a central place.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-24 10:03:59 +02:00
Jason A. Donenfeld
f9ea8ba5ed seedrng: code-golf even smaller
Since we're passing 0 as the timeout, we don't need safe_poll. Remove
cleanup at end of program, since OS does that, which lets us simplify
control flow. Factor repeated function calls into ternary loop.

function                                             old     new   delta
seedrng_main                                        1061    1459    +398
seed_from_file_if_exists                             468       -    -468
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/0 up/down: 398/-468)          Total: -70 bytes
   text    data     bss     dec     hex filename
1052781   16515    1816 1071112  105808 busybox_old
1052711   16515    1816 1071042  1057c2 busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-24 10:01:07 +02:00
Jason A. Donenfeld
57fea029cc seedrng: compress format strings with %s arguments
- Avoid an xstrdup call with seed_dir.
- Compress format strings with %s arguments.
- Open /dev/urandom for add entropy ioctl rather than /dev/random, so that
  /dev/random is only used for the already-sightly-flawed poll() check for
  creditability.

function                                             old     new   delta
seedrng_main                                         948     958     +10
seed_from_file_if_exists                             410     417      +7
.rodata                                           108338  108206    -132
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 17/-132)          Total: -115 bytes
   text	   data	    bss	    dec	    hex	filename
 975829	   4227	   1816	 981872	  efb70	busybox_old
 975714	   4227	   1816	 981757	  efafd	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-20 15:43:04 +02:00
Jason A. Donenfeld
3cb40f89de seedrng: avoid needless runtime strlen() call
- Avoid needless runtime strlen() call, bloating binary.
- Replace failed seed string with series of nulls.

function                                             old     new   delta
.rodata                                           108350  108338     -12
static.seedrng_prefix                                 26       -     -26
seedrng_main                                        1000     948     -52
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/2 up/down: 0/-90)             Total: -90 bytes
   text	   data	    bss	    dec	    hex	filename
 975919	   4227	   1816	 981962	  efbca	busybox_old
 975829	   4227	   1816	 981872	  efb70	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-20 15:43:00 +02:00
Jason A. Donenfeld
ce9a345632 seedrng: use predefined strings where possible
- Use predefined strings where possible.
- Open /dev/random with O_RDONLY for ioctl().

function                                             old     new   delta
seed_from_file_if_exists                             413     410      -3
.rodata                                           108407  108350     -57
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-60)             Total: -60 bytes
   text	   data	    bss	    dec	    hex	filename
 975979	   4227	   1816	 982022	  efc06	busybox_old
 975919	   4227	   1816	 981962	  efbca	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-20 15:43:00 +02:00
Jason A. Donenfeld
398bb3861a seedrng: further reduce size
- Remove all games with errno to further reduce code size.
- Combine error messages that don't benefit from being separated.
- Lock directory fd instead of separate file.

function                                             old     new   delta
static.longopts                                       38      26     -12
seed_from_file_if_exists                             426     413     -13
packed_usage                                       34519   34480     -39
.rodata                                           108484  108407     -77
seedrng_main                                        1088    1000     -88
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-229)           Total: -229 bytes
   text	   data	    bss	    dec	    hex	filename
 976208	   4227	   1816	 982251	  efceb	busybox_old
 975979	   4227	   1816	 982022	  efc06	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-20 15:43:00 +02:00
Jason A. Donenfeld
3c60711f83 seedrng: remove some global variables
- Remove global variables and pass dfd by value, opened once instead of
  twice, which shaves off some more bytes.

function                                             old     new   delta
seedrng_main                                        1086    1088      +2
seed_dir                                               8       -      -8
non_creditable_seed                                    8       -      -8
lock_file                                              8       -      -8
creditable_seed                                        8       -      -8
seed_from_file_if_exists                             456     426     -30
------------------------------------------------------------------------------
(add/remove: 0/4 grow/shrink: 1/1 up/down: 2/-62)             Total: -60 bytes
   text	   data	    bss	    dec	    hex	filename
 976236	   4227	   1848	 982311	  efd27	busybox_old
 976208	   4227	   1816	 982251	  efceb	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-20 15:43:00 +02:00
Jason A. Donenfeld
31ec481baf seedrng: hoist bb_strtoul out of min/max
- Hoist bb_strtoul out of min/max to prevent quadruple evaluation.
- Don't use separate variables for boottime/realtime.
- Make use of ENABLE_FEATURE_CLEAN_UP where appropriate.
- Order hash initialization after lock taking per Bernhard's taste.
- Add comment description of theory of operation.

function                                             old     new   delta
seed_from_file_if_exists                             533     456     -77
seedrng_main                                        1218    1086    -132
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-209)           Total: -209 bytes
   text	   data	    bss	    dec	    hex	filename
 976445	   4227	   1848	 982520	  efdf8	busybox_old
 976236	   4227	   1848	 982311	  efd27	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-20 15:42:53 +02:00
Jason A. Donenfeld
4538578996 seedrng: use libbb functions
- Make extensive use of libbb.h functions, which simplify a lot of code
  and reduce binary size considerably.
- Use the already existing PID_FILE_PATH variable.

function                                             old     new   delta
seed_from_file_if_exists                             697     533    -164
.rodata                                           108665  108484    -181
seedrng_main                                        1463    1218    -245
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-590)           Total: -590 bytes
   text	   data	    bss	    dec	    hex	filename
 977035	   4227	   1848	 983110	  f0046	busybox_old
 976445	   4227	   1848	 982520	  efdf8	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-20 15:22:55 +02:00
Jason A. Donenfeld
4b407bacd4 seedrng: import SeedRNG utility for kernel RNG seed files
The RNG can't actually be seeded from a shell script, due to the
reliance on ioctls and the fact that entropy written into the
unprivileged /dev/urandom device is not immediately mixed in, making
subsequent seed reads dangerous. For this reason, the seedrng project
provides a basic "C script" meant to be copy and pasted into projects
like Busybox and tweaked as needed: <https://git.zx2c4.com/seedrng/about/>.

The SeedRNG construction has been part of systemd's seeder since
January, and recently was added to Android, OpenRC, and Void's Runit,
with more integrations on their way depending on context. Virtually
every single Busybox-based distro I have seen seeds things in wrong,
incomplete, or otherwise dangerous way. For example, fixing this issue
in Buildroot requires first for Busybox to have this fix.

This commit imports it into Busybox and wires up the basic config. The
utility itself is tiny, and unlike the example code from the SeedRNG
project, we can re-use libbb's existing hash functions, rather than
having to ship a standalone BLAKE2s, which makes this even smaller.

function                                             old     new   delta
seedrng_main                                           -    1463   +1463
.rodata                                           107858  108665    +807
seed_from_file_if_exists                               -     697    +697
packed_usage                                       34414   34519    +105
static.longopts                                        -      38     +38
static.seedrng_prefix                                  -      26     +26
seed_dir                                               -       8      +8
non_creditable_seed                                    -       8      +8
lock_file                                              -       8      +8
creditable_seed                                        -       8      +8
applet_names                                        2747    2755      +8
applet_main                                         3192    3200      +8
------------------------------------------------------------------------------
(add/remove: 9/0 grow/shrink: 4/0 up/down: 3184/0)           Total: 3184 bytes
   text	   data	    bss	    dec	    hex	filename
 973776	   4219	   1816	 979811	  ef363	busybox_old
 977035	   4227	   1848	 983110	  f0046	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-04-20 15:20:29 +02:00