| 1 |
/* _Unwind_Frames_Extra with shadow stack for x86-64 and x86. |
| 2 |
Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
Under Section 7 of GPL version 3, you are granted additional |
| 17 |
permissions described in the GCC Runtime Library Exception, version |
| 18 |
3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License and |
| 21 |
a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
<http://www.gnu.org/licenses/>. */ |
| 24 |
|
| 25 |
#include <x86gprintrin.h> |
| 26 |
|
| 27 |
/* Unwind the shadow stack for EH. */ |
| 28 |
#undef _Unwind_Frames_Extra |
| 29 |
#define _Unwind_Frames_Extra(x) \ |
| 30 |
do \ |
| 31 |
{ \ |
| 32 |
_Unwind_Word ssp = _get_ssp (); \ |
| 33 |
if (ssp != 0) \ |
| 34 |
{ \ |
| 35 |
_Unwind_Word tmp = (x); \ |
| 36 |
while (tmp > 255) \ |
| 37 |
{ \ |
| 38 |
_inc_ssp (255); \ |
| 39 |
tmp -= 255; \ |
| 40 |
} \ |
| 41 |
_inc_ssp (tmp); \ |
| 42 |
} \ |
| 43 |
} \ |
| 44 |
while (0) |
| 45 |
|
| 46 |
/* Linux CET kernel places a restore token on shadow stack for signal |
| 47 |
handler to enhance security. The restore token is 8 byte and aligned |
| 48 |
to 8 bytes. It is usually transparent to user programs since kernel |
| 49 |
will pop the restore token when signal handler returns. But when an |
| 50 |
exception is thrown from a signal handler, now we need to pop the |
| 51 |
restore token from shadow stack. For x86-64, we just need to treat |
| 52 |
the signal frame as normal frame. For i386, we need to search for |
| 53 |
the restore token to check if the original shadow stack is 8 byte |
| 54 |
aligned. If the original shadow stack is 8 byte aligned, we just |
| 55 |
need to pop 2 slots, one restore token, from shadow stack. Otherwise, |
| 56 |
we need to pop 3 slots, one restore token + 4 byte padding, from |
| 57 |
shadow stack. |
| 58 |
|
| 59 |
When popping a stack frame, we compare the return address on normal |
| 60 |
stack against the return address on shadow stack. If they don't match, |
| 61 |
return _URC_FATAL_PHASE2_ERROR for the corrupted return address on |
| 62 |
normal stack. Don't check the return address for |
| 63 |
1. Non-catchable exception where exception_class == 0. Process will |
| 64 |
be terminated. |
| 65 |
2. Zero return address which marks the outermost stack frame. |
| 66 |
3. Signal stack frame since kernel puts a restore token on shadow |
| 67 |
stack. |
| 68 |
*/ |
| 69 |
#undef _Unwind_Frames_Increment |
| 70 |
#ifdef __x86_64__ |
| 71 |
#define _Unwind_Frames_Increment(exc, context, frames) \ |
| 72 |
{ \ |
| 73 |
frames++; \ |
| 74 |
if (exc->exception_class != 0 \ |
| 75 |
&& _Unwind_GetIP (context) != 0 \ |
| 76 |
&& !_Unwind_IsSignalFrame (context)) \ |
| 77 |
{ \ |
| 78 |
_Unwind_Word ssp = _get_ssp (); \ |
| 79 |
if (ssp != 0) \ |
| 80 |
{ \ |
| 81 |
ssp += 8 * frames; \ |
| 82 |
_Unwind_Word ra = *(_Unwind_Word *) ssp; \ |
| 83 |
if (ra != _Unwind_GetIP (context)) \ |
| 84 |
return _URC_FATAL_PHASE2_ERROR; \ |
| 85 |
} \ |
| 86 |
} \ |
| 87 |
} |
| 88 |
#else |
| 89 |
#define _Unwind_Frames_Increment(exc, context, frames) \ |
| 90 |
if (_Unwind_IsSignalFrame (context)) \ |
| 91 |
do \ |
| 92 |
{ \ |
| 93 |
_Unwind_Word ssp, prev_ssp, token; \ |
| 94 |
ssp = _get_ssp (); \ |
| 95 |
if (ssp != 0) \ |
| 96 |
{ \ |
| 97 |
/* Align shadow stack pointer to the next \ |
| 98 |
8 byte aligned boundary. */ \ |
| 99 |
ssp = (ssp + 4) & ~7; \ |
| 100 |
do \ |
| 101 |
{ \ |
| 102 |
/* Look for a restore token. */ \ |
| 103 |
token = (*(_Unwind_Word *) (ssp - 8)); \ |
| 104 |
prev_ssp = token & ~7; \ |
| 105 |
if (prev_ssp == ssp) \ |
| 106 |
break; \ |
| 107 |
ssp += 8; \ |
| 108 |
} \ |
| 109 |
while (1); \ |
| 110 |
frames += (token & 0x4) ? 3 : 2; \ |
| 111 |
} \ |
| 112 |
} \ |
| 113 |
while (0); \ |
| 114 |
else \ |
| 115 |
{ \ |
| 116 |
frames++; \ |
| 117 |
if (exc->exception_class != 0 \ |
| 118 |
&& _Unwind_GetIP (context) != 0) \ |
| 119 |
{ \ |
| 120 |
_Unwind_Word ssp = _get_ssp (); \ |
| 121 |
if (ssp != 0) \ |
| 122 |
{ \ |
| 123 |
ssp += 4 * frames; \ |
| 124 |
_Unwind_Word ra = *(_Unwind_Word *) ssp; \ |
| 125 |
if (ra != _Unwind_GetIP (context)) \ |
| 126 |
return _URC_FATAL_PHASE2_ERROR; \ |
| 127 |
} \ |
| 128 |
} \ |
| 129 |
} |
| 130 |
#endif |