shadow-stack-unwind.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* _Unwind_Frames_Extra with shadow stack for x86-64 and x86.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include <x86gprintrin.h>
  20. /* Unwind the shadow stack for EH. */
  21. #undef _Unwind_Frames_Extra
  22. #define _Unwind_Frames_Extra(x) \
  23. do \
  24. { \
  25. _Unwind_Word ssp = _get_ssp (); \
  26. if (ssp != 0) \
  27. { \
  28. _Unwind_Word tmp = (x); \
  29. while (tmp > 255) \
  30. { \
  31. _inc_ssp (255); \
  32. tmp -= 255; \
  33. } \
  34. _inc_ssp (tmp); \
  35. } \
  36. } \
  37. while (0)
  38. /* Linux CET kernel places a restore token on shadow stack for signal
  39. handler to enhance security. The restore token is 8 byte and aligned
  40. to 8 bytes. It is usually transparent to user programs since kernel
  41. will pop the restore token when signal handler returns. But when an
  42. exception is thrown from a signal handler, now we need to pop the
  43. restore token from shadow stack. For x86-64, we just need to treat
  44. the signal frame as normal frame. For i386, we need to search for
  45. the restore token to check if the original shadow stack is 8 byte
  46. aligned. If the original shadow stack is 8 byte aligned, we just
  47. need to pop 2 slots, one restore token, from shadow stack. Otherwise,
  48. we need to pop 3 slots, one restore token + 4 byte padding, from
  49. shadow stack. */
  50. #ifndef __x86_64__
  51. #undef _Unwind_Frames_Increment
  52. #define _Unwind_Frames_Increment(context, frames) \
  53. if (_Unwind_IsSignalFrame (context)) \
  54. do \
  55. { \
  56. _Unwind_Word ssp, prev_ssp, token; \
  57. ssp = _get_ssp (); \
  58. if (ssp != 0) \
  59. { \
  60. /* Align shadow stack pointer to the next \
  61. 8 byte aligned boundary. */ \
  62. ssp = (ssp + 4) & ~7; \
  63. do \
  64. { \
  65. /* Look for a restore token. */ \
  66. token = (*(_Unwind_Word *) (ssp - 8)); \
  67. prev_ssp = token & ~7; \
  68. if (prev_ssp == ssp) \
  69. break; \
  70. ssp += 8; \
  71. } \
  72. while (1); \
  73. frames += (token & 0x4) ? 3 : 2; \
  74. } \
  75. } \
  76. while (0); \
  77. else \
  78. frames++;
  79. #endif