1 // SPDX-License-Identifier: GPL-2.0-only
3 * sysret_ss_attrs.c - test that syscalls return valid hidden SS attributes
4 * Copyright (c) 2015 Andrew Lutomirski
6 * On AMD CPUs, SYSRET can return with a valid SS descriptor with with
7 * the hidden attributes set to an unusable state. Make sure the kernel
8 * doesn't let this happen.
23 static void *threadproc(void *ctx
)
26 * Do our best to cause sleeps on this CPU to exit the kernel and
27 * re-enter with SS = 0.
36 extern unsigned long call32_from_64(void *stack
, void (*function
)(void));
38 asm (".pushsection .text\n\t"
45 extern void test_ss(void);
51 * Start a busy-looping thread on the same CPU we're on.
52 * For simplicity, just stick everything to CPU 0. This will
53 * fail in some containers, but that's probably okay.
58 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
59 printf("[WARN]\tsched_setaffinity failed\n");
62 if (pthread_create(&thread
, 0, threadproc
, 0) != 0)
63 err(1, "pthread_create");
66 unsigned char *stack32
= mmap(NULL
, 4096, PROT_READ
| PROT_WRITE
,
67 MAP_32BIT
| MAP_ANONYMOUS
| MAP_PRIVATE
,
69 if (stack32
== MAP_FAILED
)
73 printf("[RUN]\tSyscalls followed by SS validation\n");
75 for (int i
= 0; i
< 1000; i
++) {
77 * Go to sleep and return using sysret (if we're 64-bit
78 * or we're 32-bit on AMD on a 64-bit kernel). On AMD CPUs,
79 * SYSRET doesn't fix up the cached SS descriptor, so the
80 * kernel needs some kind of workaround to make sure that we
81 * end the system call with a valid stack segment. This
82 * can be a confusing failure because the SS *selector*
83 * is the same regardless.
89 * On 32-bit, just doing a syscall through glibc is enough
90 * to cause a crash if our cached SS descriptor is invalid.
91 * On 64-bit, it's not, so try extra hard.
93 call32_from_64(stack32
+ 4088, test_ss
);
97 printf("[OK]\tWe survived\n");
100 munmap(stack32
, 4096);