1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * sysret_ss_attrs.c - test that syscalls return valid hidden SS attributes
4*4882a593Smuzhiyun * Copyright (c) 2015 Andrew Lutomirski
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * On AMD CPUs, SYSRET can return with a valid SS descriptor with with
7*4882a593Smuzhiyun * the hidden attributes set to an unusable state. Make sure the kernel
8*4882a593Smuzhiyun * doesn't let this happen.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #define _GNU_SOURCE
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <stdlib.h>
14*4882a593Smuzhiyun #include <unistd.h>
15*4882a593Smuzhiyun #include <stdio.h>
16*4882a593Smuzhiyun #include <string.h>
17*4882a593Smuzhiyun #include <sys/mman.h>
18*4882a593Smuzhiyun #include <err.h>
19*4882a593Smuzhiyun #include <stddef.h>
20*4882a593Smuzhiyun #include <stdbool.h>
21*4882a593Smuzhiyun #include <pthread.h>
22*4882a593Smuzhiyun
threadproc(void * ctx)23*4882a593Smuzhiyun static void *threadproc(void *ctx)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun * Do our best to cause sleeps on this CPU to exit the kernel and
27*4882a593Smuzhiyun * re-enter with SS = 0.
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun while (true)
30*4882a593Smuzhiyun ;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun return NULL;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #ifdef __x86_64__
36*4882a593Smuzhiyun extern unsigned long call32_from_64(void *stack, void (*function)(void));
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun asm (".pushsection .text\n\t"
39*4882a593Smuzhiyun ".code32\n\t"
40*4882a593Smuzhiyun "test_ss:\n\t"
41*4882a593Smuzhiyun "pushl $0\n\t"
42*4882a593Smuzhiyun "popl %eax\n\t"
43*4882a593Smuzhiyun "ret\n\t"
44*4882a593Smuzhiyun ".code64");
45*4882a593Smuzhiyun extern void test_ss(void);
46*4882a593Smuzhiyun #endif
47*4882a593Smuzhiyun
main()48*4882a593Smuzhiyun int main()
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * Start a busy-looping thread on the same CPU we're on.
52*4882a593Smuzhiyun * For simplicity, just stick everything to CPU 0. This will
53*4882a593Smuzhiyun * fail in some containers, but that's probably okay.
54*4882a593Smuzhiyun */
55*4882a593Smuzhiyun cpu_set_t cpuset;
56*4882a593Smuzhiyun CPU_ZERO(&cpuset);
57*4882a593Smuzhiyun CPU_SET(0, &cpuset);
58*4882a593Smuzhiyun if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
59*4882a593Smuzhiyun printf("[WARN]\tsched_setaffinity failed\n");
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun pthread_t thread;
62*4882a593Smuzhiyun if (pthread_create(&thread, 0, threadproc, 0) != 0)
63*4882a593Smuzhiyun err(1, "pthread_create");
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #ifdef __x86_64__
66*4882a593Smuzhiyun unsigned char *stack32 = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
67*4882a593Smuzhiyun MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE,
68*4882a593Smuzhiyun -1, 0);
69*4882a593Smuzhiyun if (stack32 == MAP_FAILED)
70*4882a593Smuzhiyun err(1, "mmap");
71*4882a593Smuzhiyun #endif
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun printf("[RUN]\tSyscalls followed by SS validation\n");
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun for (int i = 0; i < 1000; i++) {
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * Go to sleep and return using sysret (if we're 64-bit
78*4882a593Smuzhiyun * or we're 32-bit on AMD on a 64-bit kernel). On AMD CPUs,
79*4882a593Smuzhiyun * SYSRET doesn't fix up the cached SS descriptor, so the
80*4882a593Smuzhiyun * kernel needs some kind of workaround to make sure that we
81*4882a593Smuzhiyun * end the system call with a valid stack segment. This
82*4882a593Smuzhiyun * can be a confusing failure because the SS *selector*
83*4882a593Smuzhiyun * is the same regardless.
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun usleep(2);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun #ifdef __x86_64__
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * On 32-bit, just doing a syscall through glibc is enough
90*4882a593Smuzhiyun * to cause a crash if our cached SS descriptor is invalid.
91*4882a593Smuzhiyun * On 64-bit, it's not, so try extra hard.
92*4882a593Smuzhiyun */
93*4882a593Smuzhiyun call32_from_64(stack32 + 4088, test_ss);
94*4882a593Smuzhiyun #endif
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun printf("[OK]\tWe survived\n");
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun #ifdef __x86_64__
100*4882a593Smuzhiyun munmap(stack32, 4096);
101*4882a593Smuzhiyun #endif
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun return 0;
104*4882a593Smuzhiyun }
105