public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: "Roberto A. Foglietta" <roberto.foglietta@gmail.com>
To: isar-users <isar-users@googlegroups.com>
Subject: Fwd: How busybox is using rand(), it can be an issue
Date: Wed, 22 Jul 2026 00:27:29 +0200	[thread overview]
Message-ID: <CAJGKYO5qwMO4fv4Q_oSE-j-1iMo0r52qqo5UN7fGCUg9zWQxMw@mail.gmail.com> (raw)
In-Reply-To: <CAJGKYO4BuG77Kc04DGbRYxoOz1+TQqu6HfAPCcuAcrnhVsX2eA@mail.gmail.com>

Hi,

Working on shuf to validate a 3rd-party bugfix, I realised that
BusyBox uses rand() in "best effort" despite some TODO and warnings
being disseminated around. Others applets that uses rand() are (for
example, but not an exhaustive list) zcip.c, > awk.c, ntpd.c,
telnetd.c and tls.c

Searching srand, and finding that busybox uses it since 2001 despite
the fact that we know that rand() in some cases cannot even fill a 16
bit register.

    v5 --> v6:
    - even when rand() is limited to 15 bits but properly seeded,
      the new function random_in_range() passes PractRand RNG_test
      for a size of 8GB which is a dramatic improvement compared
      the current use of rand() in BusyBox in terms of randomness.

Paradoxically some people out there that went crazy about uchaos as
RNG, never took into consideration to audit how busybox TLS was using
rand()... LOL

busybox/testsuite$ ./rtest 0 0xffffffff $(( 1 << 31 )) | RNG_test stdin64

Passing 8GB is a good sign of pseudo-RNG and not necessarily a source
of randomness based on true entropy. In fact, rand() requires being
seeded by srand(). Every real-entropy randomness generator is able to
self-seed itself (warm-up, not technically a seeding).

static inline
uint32_t random_in_range(uint32_t min, uint32_t max)
{
uint32_t r = (rand() % RAND_MAX);

/* RAND_MAX can be as small as 32767 */
  r ^= (uint32_t)(rand() % RAND_MAX) << 17;
  r ^= (uint32_t)(rand() % RAND_MAX) <<  7;
  r *= murmul1;
  r ^= r >> 16;
r %= max-min;
r += min;

return r;
}

static inline
void srand_init(void)
{
  struct timespec ts;
  clock_gettime(CLOCK_MONOTONIC, &ts);
  srand( ts.tv_sec ^ ts.tv_nsec );
}

Anyway, the combo of random_in_range() and srand_init() are shocking
for their relative simplicity. They are a bit more convoluted than the
srand() / rand() combo but compared to passing 8GB in RNG_test, they
are pretty simple. In fact changing shuf.c required 26 bytes of extra
footprint. Moving these functions in libbb.a for general usage seems
the most reasonable choice, at this point and unless confutation in
some relevant cases (aka downstream testing).

https://github.com/robang74/busybox/
  /commit/edff9d2478733a8f0df6fc7d5c1ba0e3defc8db1

So the question is: are there specific cases in which
testsuite/randtest.c fails?

Best regards,
--
Roberto A. Foglietta
+49.176.274.75.661
+39.349.33.30.697

-- 
You received this message because you are subscribed to the Google Groups "isar-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to isar-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/isar-users/CAJGKYO5qwMO4fv4Q_oSE-j-1iMo0r52qqo5UN7fGCUg9zWQxMw%40mail.gmail.com.

           reply	other threads:[~2026-07-21 22:28 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <CAJGKYO4BuG77Kc04DGbRYxoOz1+TQqu6HfAPCcuAcrnhVsX2eA@mail.gmail.com>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJGKYO5qwMO4fv4Q_oSE-j-1iMo0r52qqo5UN7fGCUg9zWQxMw@mail.gmail.com \
    --to=roberto.foglietta@gmail.com \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox