~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/x86/sysret_ss_attrs.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * sysret_ss_attrs.c - test that syscalls return valid hidden SS attributes
  4  * Copyright (c) 2015 Andrew Lutomirski
  5  *
  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.
  9  */
 10 
 11 #define _GNU_SOURCE
 12 
 13 #include <stdlib.h>
 14 #include <unistd.h>
 15 #include <stdio.h>
 16 #include <string.h>
 17 #include <sys/mman.h>
 18 #include <err.h>
 19 #include <stddef.h>
 20 #include <stdbool.h>
 21 #include <pthread.h>
 22 
 23 static void *threadproc(void *ctx)
 24 {
 25         /*
 26          * Do our best to cause sleeps on this CPU to exit the kernel and
 27          * re-enter with SS = 0.
 28          */
 29         while (true)
 30                 ;
 31 
 32         return NULL;
 33 }
 34 
 35 #ifdef __x86_64__
 36 extern unsigned long call32_from_64(void *stack, void (*function)(void));
 37 
 38 asm (".pushsection .text\n\t"
 39      ".code32\n\t"
 40      "test_ss:\n\t"
 41      "pushl $0\n\t"
 42      "popl %eax\n\t"
 43      "ret\n\t"
 44      ".code64");
 45 extern void test_ss(void);
 46 #endif
 47 
 48 int main()
 49 {
 50         /*
 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.
 54          */
 55         cpu_set_t cpuset;
 56         CPU_ZERO(&cpuset);
 57         CPU_SET(0, &cpuset);
 58         if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
 59                 printf("[WARN]\tsched_setaffinity failed\n");
 60 
 61         pthread_t thread;
 62         if (pthread_create(&thread, 0, threadproc, 0) != 0)
 63                 err(1, "pthread_create");
 64 
 65 #ifdef __x86_64__
 66         unsigned char *stack32 = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
 67                                       MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE,
 68                                       -1, 0);
 69         if (stack32 == MAP_FAILED)
 70                 err(1, "mmap");
 71 #endif
 72 
 73         printf("[RUN]\tSyscalls followed by SS validation\n");
 74 
 75         for (int i = 0; i < 1000; i++) {
 76                 /*
 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.
 84                  */
 85                 usleep(2);
 86 
 87 #ifdef __x86_64__
 88                 /*
 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.
 92                  */
 93                 call32_from_64(stack32 + 4088, test_ss);
 94 #endif
 95         }
 96 
 97         printf("[OK]\tWe survived\n");
 98 
 99 #ifdef __x86_64__
100         munmap(stack32, 4096);
101 #endif
102 
103         return 0;
104 }
105 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php