Re: [PATCH 4/4] x86/cfi: Add boot time hash randomization
From: Kees Cook
Date: Fri Oct 28 2022 - 13:43:17 EST
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@xxxxxxxxxxxx>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
> ---
> 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@xxxxxxxxxxxx>
--
Kees Cook