From: "H. Peter Anvin" <hpa@zytor.com>
To: david.keisarschm@mail.huji.ac.il, linux-kernel@vger.kernel.org,
Dave Hansen <dave.hansen@linux.intel.com>,
Andy Lutomirski <luto@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
x86@kernel.org
Cc: Jason@zx2c4.com, keescook@chromium.org, aksecurity@gmail.com,
ilay.bahat1@gmail.com
Subject: Re: [PATCH v4 3/3] x86 mm, x86 architecture (32-bit and 64-bit): arch/x86/mm/kaslr.c: Adds 64bits version of prandom_seed_state
Date: Fri, 13 Jan 2023 18:10:09 -0800 [thread overview]
Message-ID: <9fc36447-5534-b93a-98d2-0999820c0def@zytor.com> (raw)
In-Reply-To: <295dceed7bef2391f86e751b9bfc9a34347062e4.1673470326.git.david.keisarschm@mail.huji.ac.il>
On 1/13/23 13:39, david.keisarschm@mail.huji.ac.il wrote:
> +{
> + u32 i = ((seed >> 32) ^ (seed << 10) ^ seed) & 0xffffffffUL;
> + // To take advantage of all 64 bits of the seed
> + u32 j = ((seed>>32) ^ (seed<<10)) & 0xffffffffUL;
The and operation here is pointless. You are implicitly casting to u32.
Even if you had to mask explicitly, (u32) would be a lot more clear than
a hard-to-read constant.
> + state->s1 = __seed(i, 2U);
> + state->s2 = __seed(j, 8U);
> + /* Ensure no obvious linear relation with the previous states */
> + state->s3 = __seed(next_pseudo_random32(i+j), 16U);
> + state->s4 = __seed(next_pseudo_random32(j-((i>>16)^(i<<16))), 128U);
> +
> + /* Calling RNG ten times to satisfy recurrence condition */
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> + prandom_u32_state(state);
> +}
What recurrence relation is that? This looks like you are just trying to
re-invoke prandom_warmup(), but for heaven's sake, don't open-code it!
In fact, it seems you are just hacking an alternate version of
prandom_seed_full_state(). Why not just change prandom_seed_full_state()
if that is motivated? (You may need to factor out the iteration over all
CPUs.)
Honestly, I'm not a super expert in PRNGs, but this seems both slow (the
*only* motivation for prandom_u32() is to be fast) and pointless. If we
are relying on this for security, we should be using something that is
actually cryptographic; especially since this is only invoked on boot,
which is where entropy is maximally scarce and PRNGs maximally
vulnerable due to being close to their seed state.
Additionally, in terms of creating performant mixing functions, one
thing that comes to mind is the circular multiply:
static inline u32 circular_multiply(u32 a, u32 b)
{
u64 m = (u64)a * b;
return m + (m >> 32);
}
On x86, this is two instructions, even on 32 bits.
One can use ^ instead of + to make it a bit less algebraic, but I don't
know for sure that that is a net improvement.
-hpa
next prev parent reply other threads:[~2023-01-14 2:10 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-13 21:33 [PATCH v4 0/3] Secure prandom_u32 invocations david.keisarschm
2023-01-13 21:35 ` [PATCH v4 1/3] slab_allocator: mm/slab.c: Replace invocation of weak PRNG david.keisarschm
2023-01-13 21:37 ` [PATCH v4 2/3] slab_allocator: mm/slab_common.c: " david.keisarschm
2023-01-13 21:39 ` [PATCH v4 3/3] x86 mm, x86 architecture (32-bit and 64-bit): arch/x86/mm/kaslr.c: Adds 64bits version of prandom_seed_state david.keisarschm
2023-01-14 2:10 ` H. Peter Anvin [this message]
[not found] ` <CAD0z2U1Y1CR_erAj+akPbVF2jxicdQKQfqWPc69fwL-rvSu_fQ@mail.gmail.com>
2023-01-17 23:50 ` H. Peter Anvin
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=9fc36447-5534-b93a-98d2-0999820c0def@zytor.com \
--to=hpa@zytor.com \
--cc=Jason@zx2c4.com \
--cc=aksecurity@gmail.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=david.keisarschm@mail.huji.ac.il \
--cc=ilay.bahat1@gmail.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=x86@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
all inboxes | Powered by JetHome®