sysret_ss_attrs.c 2.9 KB

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