linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-arch@vger.kernel.org, Dinh Nguyen <dinguyen@kernel.org>,
	Nick Hu <nickhu@andestech.com>, Max Filippov <jcmvbkbc@gmail.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	"David S . Miller" <davem@davemloft.net>,
	Yoshinori Sato <ysato@users.sourceforge.jp>,
	Michal Simek <monstr@monstr.eu>, Borislav Petkov <bp@alien8.de>,
	Guo Ren <guoren@kernel.org>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	Joshua Kinard <kumba@gentoo.org>,
	David Laight <David.Laight@aculab.com>,
	Dominik Brodowski <linux@dominikbrodowski.net>,
	Eric Biggers <ebiggers@google.com>,
	Ard Biesheuvel <ardb@kernel.org>, Arnd Bergmann <arnd@arndb.de>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andy Lutomirski <luto@kernel.org>,
	Kees Cook <keescook@chromium.org>,
	Lennart Poettering <mzxreary@0pointer.de>,
	Konstantin Ryabitsev <konstantin@linuxfoundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Theodore Ts'o <tytso@mit.edu>
Subject: Re: [PATCH v1] random: block in /dev/urandom
Date: Tue, 22 Mar 2022 12:19:16 -0600	[thread overview]
Message-ID: <YjoTJFRook+rGyDI@zx2c4.com> (raw)
In-Reply-To: <d5c23f68-30ba-a5eb-6bea-501736e79c88@roeck-us.net>

Hi Guenter,

On Tue, Mar 22, 2022 at 10:56:44AM -0700, Guenter Roeck wrote:
> Everything - including the various root file systems - is at
> git@github.com:groeck/linux-build-test.git. Look into rootfs/ for the
> various boot tests. I'll be happy to provide some qemu command lines
> if needed.

Thanks. It looks like the "problem" is with this shell script:

  init_rng() {
    if check_file_size; then
      printf 'Initializing random number generator: '
      dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
      status=$?
      if [ "$status" -eq 0 ]; then
        echo "OK"
      else
        echo "FAIL"
      fi
      return "$status"
    fi
  }
  
  save_random_seed() {
    printf 'Saving random seed: '
    if touch "$URANDOM_SEED" 2> /dev/null; then
      old_umask=$(umask)
      umask 077
      dd if=/dev/urandom of="$URANDOM_SEED" bs="$pool_size" count=1 2> /dev/null
      status=$?
      umask "$old_umask"
      if [ "$status" -eq 0 ]; then
        echo "OK"
      else
        echo "FAIL"
      fi
    else
      status=$?
      echo "SKIP (read-only file system detected)"
    fi
    return "$status"
  }

  case "$1" in
    start|restart|reload)
      # Carry a random seed from start-up to start-up
      # Load and then save the whole entropy pool
      init_rng && save_random_seed;;

This code is actually problematic for a number of reasons. (And Linus,
I'm not saying "userspace is wrong" to justify breaking it or something,
don't worry.)

The first `dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1`
will write the seed into the input pool, but:

  - It won't credit the entropy from that seed, so the pool won't
    actually initialize. (You need to use the ioctl to credit it.)
  - Because the pool doesn't initialize, subsequent reads from
    /dev/urandom won't actually use that seed.

The first point is why we had to revert this patch. But the second one
is actually a bit dangerous: you might write in a perfectly good seed to
/dev/urandom, but what you read out for the subsequent seed may be
complete deterministic crap. This is because the call to write_pool()
goes right into the input pool and doesn't touch any of the "fast init"
stuff, where we immediately mutate the crng key during early boot.

As far as I can tell, this has been the behavior for a really long time,
making the above innocuous pattern a pretty old thing that's broken. So
I could perhaps say, "this behavior is so old now, that your userspace
code is just plain broken," but I think I might actually have a very
quick unobtrusive fix for this. I'll mull some things over for rc2 or
later in rc1.

But, anyway, this only fixes the second point mentioned above. The first
one -- which resulted in the revert -- remains a stumper for now.

Jason

  reply	other threads:[~2022-03-22 18:19 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-17 16:28 [PATCH v1] random: block in /dev/urandom Jason A. Donenfeld
2022-02-21 18:01 ` Andy Lutomirski
2022-02-23 17:02   ` Jason A. Donenfeld
2022-02-23 17:55     ` Theodore Ts'o
2022-03-12 20:17 ` Eric Biggers
2022-03-12 20:27   ` Eric Biggers
2022-03-22 15:58 ` Guenter Roeck
2022-03-22 16:21   ` Linus Torvalds
2022-03-22 16:40     ` Jason A. Donenfeld
2022-03-22 17:09   ` Jason A. Donenfeld
2022-03-22 17:56     ` Guenter Roeck
2022-03-22 18:19       ` Jason A. Donenfeld [this message]
2022-03-22 18:29         ` Linus Torvalds
2022-03-22 18:36           ` Jason A. Donenfeld
2022-04-22 13:42       ` Jason A. Donenfeld
2022-04-22 23:46         ` Guenter Roeck
2022-04-23 13:56         ` Guenter Roeck
2022-04-23 14:28           ` Jason A. Donenfeld
2022-04-23 16:35             ` Guenter Roeck
2022-04-23 21:10           ` Jason A. Donenfeld
2022-04-24  2:04             ` Guenter Roeck
2022-04-25  0:12               ` Jason A. Donenfeld
2022-04-25  1:54                 ` Guenter Roeck
2022-04-25 11:11                   ` Jason A. Donenfeld
2022-03-22 18:24   ` Mark Brown
2022-03-22 21:54     ` Guenter Roeck
2022-03-22 22:25       ` David Laight
2022-03-23 12:10       ` Mark Brown
2022-03-23 14:23         ` Guenter Roeck
2022-03-23 15:53           ` Arnd Bergmann
2022-03-23 16:18             ` Mark Brown
2022-03-23 16:41               ` Arnd Bergmann
2022-03-23 16:47                 ` Mark Brown
2022-04-23  0:52             ` Jason A. Donenfeld
2022-04-25 12:09               ` Mark Brown

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=YjoTJFRook+rGyDI@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=David.Laight@aculab.com \
    --cc=ardb@kernel.org \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=davem@davemloft.net \
    --cc=dinguyen@kernel.org \
    --cc=ebiggers@google.com \
    --cc=geert@linux-m68k.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=guoren@kernel.org \
    --cc=jcmvbkbc@gmail.com \
    --cc=keescook@chromium.org \
    --cc=konstantin@linuxfoundation.org \
    --cc=kumba@gentoo.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@dominikbrodowski.net \
    --cc=linux@roeck-us.net \
    --cc=luto@kernel.org \
    --cc=monstr@monstr.eu \
    --cc=mzxreary@0pointer.de \
    --cc=nickhu@andestech.com \
    --cc=palmer@dabbelt.com \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=tytso@mit.edu \
    --cc=ysato@users.sourceforge.jp \
    /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;
as well as URLs for NNTP newsgroup(s).