Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1560350

Re: random: /dev/random often returns short reads

From Denys Vlasenko <vda.linux@googlemail.com>
Newsgroups linux.kernel
Subject Re: random: /dev/random often returns short reads
Date 2017-01-17 09:40 +0100
Message-ID <t0xt8-4GT-11@gated-at.bofh.it> (permalink)
References <t0kFz-4uU-5@gated-at.bofh.it> <t0tIS-2as-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Tue, Jan 17, 2017 at 5:36 AM, Theodore Ts'o <tytso@mit.edu> wrote:
> On Mon, Jan 16, 2017 at 07:50:55PM +0100, Denys Vlasenko wrote:
>>
>> /dev/random can legitimately returns short reads
>> when there is not enough entropy for the full request.
>
> Yes, but callers of /dev/random should be able to handle short reads.
> So it's a bug in the application as well.

I absolutely agree, whoever stumbled over it has a bug
in their app.

>> The code looks like it effectively credits the pool only for ~3/4
>> of the amount, i.e. 24 bytes, not 32.
>
> How much it credits the pools varies depending on how many bits of
> entropy are being transferred and how full the pool happens to be
> beforehand.

I think the problem is that even if the target pool has no entropy
at all, current algorithm thinks that transferring N random bytes
to it gives it N*3/4 bytes of randomness.

> Reversing the calculation so that we transfer exactly the
> right number of bits is tricky, and if we transfer too many bits, we
> risk "wasting" entropy bits.  Of course, it doesn't matter if we're
> transfering pretend entropy only for the purposes of getting FIPS
> certification, but getting it Right(tm) is non-trivial.
>
> If someone wants to send me a patch, I'll happily take a look at it,

Will something along these lines be accepted?

--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -653,6 +653,9 @@ static void credit_entropy_bits(struct
entropy_store *r, int nbits)
        if (nfrac < 0) {
                /* Debit */
                entropy_count += nfrac;
+       } else if (entropy_count == 0) {
+               /* Credit, and the pool is empty */
+               entropy_count += nfrac;
        } else {
                /*
                 * Credit: we have to account for the possibility of
                 * overwriting already present entropy.  Even in the


> but given that fixing userspace is something you really should do
> anyway

I agree. It's just not in my (or my company's, IIUC) userspace code.
I wouldn't even know about this thing since *my* programs do handle
short reads correctly.

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

random: /dev/random often returns short reads Denys Vlasenko <vda.linux@googlemail.com> - 2017-01-16 20:00 +0100
  Re: random: /dev/random often returns short reads Denys Vlasenko <vda.linux@googlemail.com> - 2017-01-16 20:00 +0100
  Re: random: /dev/random often returns short reads Theodore Ts'o <tytso@mit.edu> - 2017-01-17 05:40 +0100
    Re: random: /dev/random often returns short reads Denys Vlasenko <vda.linux@googlemail.com> - 2017-01-17 09:40 +0100
      Re: random: /dev/random often returns short reads Theodore Ts'o <tytso@mit.edu> - 2017-01-17 19:00 +0100
        Re: random: /dev/random often returns short reads Denys Vlasenko <dvlasenk@redhat.com> - 2017-01-17 19:00 +0100
          Re: random: /dev/random often returns short reads "H. Peter Anvin" <hpa@linux.intel.com> - 2017-01-17 23:30 +0100
            Re: random: /dev/random often returns short reads Theodore Ts'o <tytso@mit.edu> - 2017-01-18 01:30 +0100
              Re: random: /dev/random often returns short reads "H. Peter Anvin" <hpa@linux.intel.com> - 2017-01-18 03:00 +0100
            Re: random: /dev/random often returns short reads Denys Vlasenko <dvlasenk@redhat.com> - 2017-01-18 17:00 +0100
              Re: random: /dev/random often returns short reads Theodore Ts'o <tytso@mit.edu> - 2017-01-18 19:10 +0100
                Re: random: /dev/random often returns short reads Denys Vlasenko <vda.linux@googlemail.com> - 2017-01-19 22:50 +0100
                Re: random: /dev/random often returns short reads "H. Peter Anvin" <hpa@linux.intel.com> - 2017-01-20 04:20 +0100

csiph-web