lkml.org 
[lkml]   [2022]   [Oct]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH 4/4] x86/cfi: Add boot time hash randomization
On Thu, Oct 27, 2022 at 11:28:16AM +0200, Peter Zijlstra wrote:
> In order to avoid known hashes (from knowing the boot image),
> randomize the CFI hashes with a per-boot random seed.
>
> Suggested-by: Kees Cook <keescook@chromium.org>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
> arch/x86/kernel/alternative.c | 120 +++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 108 insertions(+), 12 deletions(-)
>
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -711,6 +711,24 @@ enum cfi_mode {
> };
>
> static enum cfi_mode cfi_mode __ro_after_init = CFI_DEFAULT;
> +static bool cfi_rand __ro_after_init = true;
> +static u32 cfi_seed __ro_after_init;
> +
> +/*
> + * Re-hash the CFI hash with a boot-time seed while making sure the result is
> + * not a valid ENDBR instruction.
> + */
> +static u32 cfi_rehash(u32 hash)
> +{
> + hash ^= cfi_seed;
> + while (unlikely(is_endbr(hash) || is_endbr(-hash))) {
> + bool lsb = hash & 1;
> + hash >>= 1;
> + if (lsb)
> + hash ^= 0x80200003;
> + }
> + return hash;
> +}

I guess this risks hash collision with existing hashes, but meeeh. I'm
glad to have the randomization. :)

Reviewed-by: Kees Cook <keescook@chromium.org>

--
Kees Cook

\
 
 \ /
  Last update: 2022-10-28 19:44    [W:0.355 / U:0.008 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site