sysret_ss_attrs.c 2.5 KB

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