Re: [patch 1/8] selftests/x86: Test signal frame XSTATE header corruption handling
From: Borislav Petkov
Date: Wed Jun 02 2021 - 08:38:20 EST
On Wed, Jun 02, 2021 at 11:55:44AM +0200, Thomas Gleixner wrote:
> From: Andy Lutomirski <luto@xxxxxxxxxx>
>
> This is very heavily based on some code from Thomas Gleixner. On a system
> without XSAVES, it triggers the WARN_ON():
>
> Bad FPU state detected at copy_kernel_to_fpregs+0x2f/0x40, reinitializing FPU registers.
That triggers
[ 149.497274] corrupt_xstate_[1627] bad frame in rt_sigreturn frame:00000000dad08ab1 ip:7f031449ffe1 sp:7ffd0c5c59f0 orax:ffffffffffffffff in libpthread-2.31.so[7f0314493000+10000]
on an AMD laptop here.
> +static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
> + unsigned int *ecx, unsigned int *edx)
> +{
> + asm volatile(
> + "cpuid;"
> + : "=a" (*eax),
> + "=b" (*ebx),
> + "=c" (*ecx),
> + "=d" (*edx)
> + : "0" (*eax), "2" (*ecx));
> +}
> +
> +static inline int xsave_enabled(void)
> +{
> + unsigned int eax, ebx, ecx, edx;
> +
> + eax = 0x1;
> + ecx = 0x0;
> + __cpuid(&eax, &ebx, &ecx, &edx);
> +
> + /* Is CR4.OSXSAVE enabled ? */
> + return ecx & (1U << 27);
> +}
One fine day someone should sit down and unify all those auxillary
functions used in the selftests into a lib...
> +
> +static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
> + int flags)
> +{
> + struct sigaction sa;
> +
> + memset(&sa, 0, sizeof(sa));
> + sa.sa_sigaction = handler;
> + sa.sa_flags = SA_SIGINFO | flags;
> + sigemptyset(&sa.sa_mask);
> + if (sigaction(sig, &sa, 0))
> + err(1, "sigaction");
> +}
> +
> +static void sigusr1(int sig, siginfo_t *info, void *uc_void)
> +{
> + ucontext_t *uc = uc_void;
> + uint8_t *fpstate = (uint8_t *)uc->uc_mcontext.fpregs;
> + uint64_t *xfeatures = (uint64_t *)(fpstate + 512);
> +
> + printf("\tWreckage XSTATE header\n");
> + /* Wreckage the first reserved byte in the header */
> + *(xfeatures + 2) = 0xfffffff;
> +}
> +
> +static void sigsegv(int sig, siginfo_t *info, void *uc_void)
> +{
> + printf("\tGot SIGSEGV\n");
> +}
> +
> +int main()
ERROR: Bad function definition - int main() should probably be int main(void)
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette