Debian Codegraph @

clang/docs/ShadowCallStack.rst

raw
1 ===============
2 ShadowCallStack
3 ===============
4
5 .. contents::
6 :local:
7
8 Introduction
9 ============
10
11 ShadowCallStack is an instrumentation pass, currently only implemented for
12 aarch64 and RISC-V, that protects programs against return address overwrites
13 (e.g. stack buffer overflows.) It works by saving a function's return address
14 to a separately allocated 'shadow call stack' in the function prolog in
15 non-leaf functions and loading the return address from the shadow call stack
16 in the function epilog. The return address is also stored on the regular stack
17 for compatibility with unwinders, but is otherwise unused.
18
19 The aarch64 implementation is considered production ready, and
20 an `implementation of the runtime`_ has been added to Android's libc
21 (bionic). An x86_64 implementation was evaluated using Chromium and was found
22 to have critical performance and security deficiencies--it was removed in
23 LLVM 9.0. Details on the x86_64 implementation can be found in the
24 `Clang 7.0.1 documentation`_.
25
26 .. _`implementation of the runtime`: https://android.googlesource.com/platform/bionic/+/808d176e7e0dd727c7f929622ec017f6e065c582/libc/bionic/pthread_create.cpp#128
27 .. _`Clang 7.0.1 documentation`: https://releases.llvm.org/7.0.1/tools/clang/docs/ShadowCallStack.html
28
29 Comparison
30 ----------
31
32 To optimize for memory consumption and cache locality, the shadow call
33 stack stores only an array of return addresses. This is in contrast to other
34 schemes, like :doc:`SafeStack`, that mirror the entire stack and trade-off
35 consuming more memory for shorter function prologs and epilogs with fewer
36 memory accesses.
37
38 `Return Flow Guard`_ is a pure software implementation of shadow call stacks
39 on x86_64. Like the previous implementation of ShadowCallStack on x86_64, it is
40 inherently racy due to the architecture's use of the stack for calls and
41 returns.
42
43 Intel `Control-flow Enforcement Technology`_ (CET) is a proposed hardware
44 extension that would add native support to use a shadow stack to store/check
45 return addresses at call/return time. Being a hardware implementation, it
46 would not suffer from race conditions and would not incur the overhead of
47 function instrumentation, but it does require operating system support.
48
49 .. _`Return Flow Guard`: https://xlab.tencent.com/en/2016/11/02/return-flow-guard/
50 .. _`Control-flow Enforcement Technology`: https://software.intel.com/sites/default/files/managed/4d/2a/control-flow-enforcement-technology-preview.pdf
51
52 Compatibility
53 -------------
54
55 A runtime is not provided in compiler-rt so one must be provided by the
56 compiled application or the operating system. Integrating the runtime into
57 the operating system should be preferred since otherwise all thread creation
58 and destruction would need to be intercepted by the application.
59
60 The instrumentation makes use of the platform register ``x18`` on AArch64,
61 ``x3`` (``gp``) on RISC-V with software shadow stack and ``ssp`` on RISC-V with
62 hardware shadow stack, which needs `Zicfiss`_ and ``-mno-forced-sw-shadow-stack``
63 (default option). Note that with ``Zicfiss``_ the RISC-V backend will default to
64 the hardware based shadow call stack. Users can force the RISC-V backend to
65 generate the software shadow call stack with ``Zicfiss``_ by passing
66 ``-mforced-sw-shadow-stack``.
67 For simplicity we will refer to this as the ``SCSReg``. On some platforms,
68 ``SCSReg`` is reserved, and on others, it is designated as a scratch register.
69 This generally means that any code that may run on the same thread as code
70 compiled with ShadowCallStack must either target one of the platforms whose ABI
71 reserves ``SCSReg`` (currently Android, Darwin, Fuchsia and Windows) or be
72 compiled with a flag to reserve that register (e.g., ``-ffixed-x18``). If
73 absolutely necessary, code compiled without reserving the register may be run on
74 the same thread as code that uses ShadowCallStack by saving the register value
75 temporarily on the stack (`example in Android`_) but this should be done with
76 care since it risks leaking the shadow call stack address.
77
78 .. _`Zicfiss`: https://github.com/riscv/riscv-cfi/blob/main/cfi_backward.adoc
79 .. _`example in Android`: https://android-review.googlesource.com/c/platform/frameworks/base/+/803717
80
81 Because it requires a dedicated register, the ShadowCallStack feature is
82 incompatible with any other feature that may use ``SCSReg``. However, there is
83 no inherent reason why ShadowCallStack needs to use a specific register; in
84 principle, a platform could choose to reserve and use another register for
85 ShadowCallStack, but this would be incompatible with the ABI standards
86 published in AAPCS64 and the RISC-V psABI.
87
88 Special unwind information is required on functions that are compiled
89 with ShadowCallStack and that may be unwound, i.e. functions compiled with
90 ``-fexceptions`` (which is the default in C++). Some unwinders (such as the
91 libgcc 4.9 unwinder) do not understand this unwind info and will segfault
92 when encountering it. LLVM libunwind processes this unwind info correctly,
93 however. This means that if exceptions are used together with ShadowCallStack,
94 the program must use a compatible unwinder.
95
96 Security
97 ========
98
99 ShadowCallStack is intended to be a stronger alternative to
100 ``-fstack-protector``. It protects from non-linear overflows and arbitrary
101 memory writes to the return address slot.
102
103 The instrumentation makes use of the ``SCSReg`` register to reference the shadow
104 call stack, meaning that references to the shadow call stack do not have
105 to be stored in memory. This makes it possible to implement a runtime that
106 avoids exposing the address of the shadow call stack to attackers that can
107 read arbitrary memory. However, attackers could still try to exploit side
108 channels exposed by the operating system `[1]`_ `[2]`_ or processor `[3]`_
109 to discover the address of the shadow call stack.
110
111 .. _`[1]`: https://eyalitkin.wordpress.com/2017/09/01/cartography-lighting-up-the-shadows/
112 .. _`[2]`: https://www.blackhat.com/docs/eu-16/materials/eu-16-Goktas-Bypassing-Clangs-SafeStack.pdf
113 .. _`[3]`: https://www.vusec.net/projects/anc/
114
115 Unless care is taken when allocating the shadow call stack, it may be
116 possible for an attacker to guess its address using the addresses of
117 other allocations. Therefore, the address should be chosen to make this
118 difficult. One way to do this is to allocate a large guard region without
119 read/write permissions, randomly select a small region within it to be
120 used as the address of the shadow call stack and mark only that region as
121 read/write. This also mitigates somewhat against processor side channels.
122 The intent is that the Android runtime `will do this`_, but the platform will
123 first need to be `changed`_ to avoid using ``setrlimit(RLIMIT_AS)`` to limit
124 memory allocations in certain processes, as this also limits the number of
125 guard regions that can be allocated.
126
127 .. _`will do this`: https://android-review.googlesource.com/c/platform/bionic/+/891622
128 .. _`changed`: https://android-review.googlesource.com/c/platform/frameworks/av/+/837745
129
130 The runtime will need the address of the shadow call stack in order to
131 deallocate it when destroying the thread. If the entire program is compiled
132 with ``SCSReg`` reserved, this is trivial: the address can be derived from the
133 value stored in ``SCSReg`` (e.g. by masking out the lower bits). If a guard
134 region is used, the address of the start of the guard region could then be
135 stored at the start of the shadow call stack itself. But if it is possible
136 for code compiled without reserving ``SCSReg`` to run on a thread managed by the
137 runtime, which is the case on Android for example, the address must be stored
138 somewhere else instead. On Android we store the address of the start of the
139 guard region in TLS and deallocate the entire guard region including the
140 shadow call stack at thread exit. This is considered acceptable given that
141 the address of the start of the guard region is already somewhat guessable.
142
143 One way in which the address of the shadow call stack could leak is in the
144 ``jmp_buf`` data structure used by ``setjmp`` and ``longjmp``. The Android
145 runtime `avoids this`_ by only storing the low bits of ``SCSReg`` in the
146 ``jmp_buf``, which requires the address of the shadow call stack to be
147 aligned to its size.
148
149 .. _`avoids this`: https://android.googlesource.com/platform/bionic/+/808d176e7e0dd727c7f929622ec017f6e065c582/libc/arch-arm64/bionic/setjmp.S#49
150
151 The architecture's call and return instructions (``bl`` and ``ret``) operate on
152 a register rather than the stack, which means that leaf functions are generally
153 protected from return address overwrites even without ShadowCallStack.
154
155 Usage
156 =====
157
158 To enable ShadowCallStack, just pass the ``-fsanitize=shadow-call-stack`` flag
159 to both compile and link command lines. On aarch64, you also need to pass
160 ``-ffixed-x18`` unless your target already reserves ``x18``. No additional flags
161 need to be passed on RISC-V because the software based shadow stack uses
162 ``x3`` (``gp``), which is always reserved, and the hardware based shadow call
163 stack uses a dedicated register, ``ssp``.
164 However, it is important to disable GP relaxation in the linker when using the
165 software based shadow call stack on RISC-V. This can be done with the
166 ``--no-relax-gp`` flag in GNU ld, and is off by default in LLD.
167
168 Low-level API
169 -------------
170
171 ``__has_feature(shadow_call_stack)``
172 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
173
174 In some cases one may need to execute different code depending on whether
175 ShadowCallStack is enabled. The macro ``__has_feature(shadow_call_stack)`` can
176 be used for this purpose.
177
178 .. code-block:: c
179
180 #if defined(__has_feature)
181 # if __has_feature(shadow_call_stack)
182 // code that builds only under ShadowCallStack
183 # endif
184 #endif
185
186 ``__attribute__((no_sanitize("shadow-call-stack")))``
187 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188
189 Use ``__attribute__((no_sanitize("shadow-call-stack")))`` on a function
190 declaration to specify that the shadow call stack instrumentation should not be
191 applied to that function, even if enabled globally.
192
193 Example
194 =======
195
196 The following example code:
197
198 .. code-block:: c++
199
200 int foo() {
201 return bar() + 1;
202 }
203
204 Generates the following aarch64 assembly when compiled with ``-O2``:
205
206 .. code-block:: none
207
208 stp x29, x30, [sp, #-16]!
209 mov x29, sp
210 bl bar
211 add w0, w0, #1
212 ldp x29, x30, [sp], #16
213 ret
214
215 Adding ``-fsanitize=shadow-call-stack`` would output the following assembly:
216
217 .. code-block:: none
218
219 str x30, [x18], #8
220 stp x29, x30, [sp, #-16]!
221 mov x29, sp
222 bl bar
223 add w0, w0, #1
224 ldp x29, x30, [sp], #16
225 ldr x30, [x18, #-8]!
226 ret