[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <9fc36447-5534-b93a-98d2-0999820c0def@zytor.com>
Date: Fri, 13 Jan 2023 18:10:09 -0800
From: "H. Peter Anvin" <hpa@...or.com>
To: david.keisarschm@...l.huji.ac.il, linux-kernel@...r.kernel.org,
Dave Hansen <dave.hansen@...ux.intel.com>,
Andy Lutomirski <luto@...nel.org>,
Peter Zijlstra <peterz@...radead.org>,
Thomas Gleixner <tglx@...utronix.de>,
Ingo Molnar <mingo@...hat.com>, Borislav Petkov <bp@...en8.de>,
x86@...nel.org
Cc: Jason@...c4.com, keescook@...omium.org, aksecurity@...il.com,
ilay.bahat1@...il.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
On 1/13/23 13:39, david.keisarschm@...l.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
Powered by blists - more mailing lists