Re: [PATCH v29 23/32] x86/cet/shstk: Add user-mode shadow stack support

Borislav Petkov
2021-08-26T16:24:53Z

On Fri, Aug 20, 2021 at 11:11:52AM -0700, Yu-cheng Yu wrote:
> diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h
> new file mode 100644
> index 000000000000..6432baf4de1f
> --- /dev/null
> +++ b/arch/x86/include/asm/cet.h
> @@ -0,0 +1,30 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef _ASM_X86_CET_H
> +#define _ASM_X86_CET_H
> +
> +#ifndef __ASSEMBLY__
> +#include <linux/types.h>
> +
> +struct task_struct;
> +
> +/*
> + * Per-thread CET status
> + */

That comment is superfluous and wrong now. The struct name says exactly
what that thing is.

> +static unsigned long alloc_shstk(unsigned long size)
> +{
> + int flags = MAP_ANONYMOUS | MAP_PRIVATE;
> + struct mm_struct *mm = current->mm;
> + unsigned long addr, populate;

s/populate/unused/

> +
> + mmap_write_lock(mm);
> + addr = do_mmap(NULL, 0, size, PROT_READ, flags, VM_SHADOW_STACK, 0,
> + &populate, NULL);
> + mmap_write_unlock(mm);
> +
> + return addr;
> +}
> +
> +int shstk_setup(void)
> +{
> + struct thread_shstk *shstk = &current->thread.shstk;
> + unsigned long addr, size;
> + int err;
> +
> + if (!cpu_feature_enabled(X86_FEATURE_SHSTK))
> + return 0;
> +
> + size = round_up(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G), PAGE_SIZE);
> + addr = alloc_shstk(size);
> + if (IS_ERR_VALUE(addr))
> + return PTR_ERR((void *)addr);
> +
> + start_update_msrs();

You're setting CET_U with the MSR writes below. Why do you need to do
XRSTOR here? To zero out PL[012]_SSP?

If so, you can WRMSR those too - no need for a full XRSTOR...

> + err = wrmsrl_safe(MSR_IA32_PL3_SSP, addr + size);
> + if (!err)
> + wrmsrl_safe(MSR_IA32_U_CET, CET_SHSTK_EN);
> + end_update_msrs();
> +
> + if (!err) {
> + shstk->base = addr;
> + shstk->size = size;
> + }
> +
> + return err;
> +}

--
Regards/Gruss,
Boris.