From: Daniel Borkmann <dborkman@redhat.com>
To: David Miller <davem@davemloft.net>
Cc: netdev@vger.kernel.org, hannes@stressinduktion.org,
akinobu.mita@gmail.com
Subject: Re: [PATCH net-next 1/2] random32: improvements to prandom_bytes
Date: Mon, 04 Aug 2014 11:48:33 +0200 [thread overview]
Message-ID: <53DF56F1.4020701@redhat.com> (raw)
In-Reply-To: <20140731.222804.1403723639172562683.davem@davemloft.net>
[ Cc'ing Akinobu, 6582c665d6b882 ("prandom: introduce prandom_bytes()
and prandom_bytes_state()") ]
On 08/01/2014 07:28 AM, David Miller wrote:
> From: Daniel Borkmann <dborkman@redhat.com>
> Date: Thu, 31 Jul 2014 22:11:16 +0200
>
>> - for (j = 0; j < sizeof(u32); j++) {
>> - p[i + j] = random;
>> - random >>= BITS_PER_BYTE;
>> - }
>> + while (bytes > sizeof(u32)) {
[ Should have been >= here, missed that earlier. ]
>> + put_unaligned(prandom_u32_state(state), (u32 *) ptr);
>> + ptr += sizeof(u32);
>> + bytes -= sizeof(u32);
>> }
>> - if (i < bytes) {
>> - u32 random = prandom_u32_state(state);
>>
>> - for (; i < bytes; i++) {
>> - p[i] = random;
>> - random >>= BITS_PER_BYTE;
>> - }
>> + if (bytes > 0) {
>> + u32 rem = prandom_u32_state(state);
>> + do {
>> + *ptr++ = (u8) rem;
>> + bytes--;
>> + rem >>= BITS_PER_BYTE;
>> + } while (bytes > 0);
>> }
>
> This conversion to put_unaligned() is not an equivalent depending
> upon the endianness of the cpu.
>
> And this means the random value gets distributed differently into
> full words than it will into trailing bytes.
>
> Let's just not mess around with this, ok?
Sorry for the late answer, Dave.
So for the case of callers of prandom_bytes() which internally
invoke prandom_bytes_state(), it doesn't matter at all since, well,
they expect the array to be filled randomly and don't have any
control of the internal state anyway (that's also why we have
periodic reseeding there, etc), so they really don't care.
Now for the direct callers of prandom_bytes_state(), which are
solely located in drivers/mtd/tests/{oobtest.c, pagetest.c,
subpagetest.c}:
These tests basically fill a test write-vector through prandom_bytes_state()
with an a-priori defined seed each time and write that to a MTD
device.
Later on, they set up a read-vector and read back that blocks from
the device. So in the verification phase, the write-vector is being
re-setup [ so same seed and prandom_bytes_state() called ], and then
memcmp()'ed against the read-vector to check if the data is the same.
Akinobu and I also tested this patch with the above fix included,
and it runs through the 3 relevant test cases _without_ any errors
on the nandsim device (simulator for MTD devs):
# modprobe nandsim first_id_byte=0x20 second_id_byte=0xac \
third_id_byte=0x00 fourth_id_byte=0x15
# modprobe mtd_oobtest dev=0
# modprobe mtd_pagetest dev=0
# modprobe mtd_subpagetest dev=0
[ ... 'finished with 0 errors' each in klog ]
We also don't have any users depending directly on a particular
result of the PRNG (except the PRNG self-test itself), and that's
just fine as it e.g. allowed us easily to do things like upgrading
from taus88 to taus113.
Thanks,
Daniel
next prev parent reply other threads:[~2014-08-04 9:48 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-31 20:11 [PATCH net-next 1/2] random32: improvements to prandom_bytes Daniel Borkmann
2014-08-01 5:28 ` David Miller
2014-08-04 9:48 ` Daniel Borkmann [this message]
2014-08-04 18:41 ` David Miller
2014-08-05 15:16 ` Daniel Borkmann
2014-08-06 10:12 ` Daniel Borkmann
2014-08-06 19:19 ` David Miller
-- strict thread matches above, loose matches on Subject: below --
2014-07-31 20:07 Daniel Borkmann
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=53DF56F1.4020701@redhat.com \
--to=dborkman@redhat.com \
--cc=akinobu.mita@gmail.com \
--cc=davem@davemloft.net \
--cc=hannes@stressinduktion.org \
--cc=netdev@vger.kernel.org \
/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).