← Documents Documentation/arch/arm64/gcs.rst GitHub 원문 ↗

Linux 6.18.37 · Architecture

Guarded Control Stack Support for AArch64 Linux

AArch64 GCS의 protected return stack, shadow-stack prctl ABI, allocation, signal frame, ptrace, coredump와 /proc 표시를 설명합니다.

Source pathDocumentation/arch/arm64/gcs.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

gcs.rst:1-227

GCS는 call 시 return PC를 별도 protected stack에 기록하고 `RET`에서 `LR`과 비교해 ROP 공격을 어렵게 합니다. Linux ABI는 x86과 interface를 공유하므로 shadow stack 명칭을 사용하며 thread별 상태, signal 복원과 debugger 우회를 명시적으로 규정합니다.

GCS call·return 검증
Procedure call현재 PC를 GCS에 pushCallee 실행`RET`에서 LR readGCS top과 비교일치 시 return

일반 call stack과 별도 GCS가 return address를 교차 검증합니다.

GCS ABI 표면
경로Interface역할
Aux vector`HWCAP_GCS`기능 존재 확인
Thread 제어`PR_*_SHADOW_STACK_STATUS`활성화·조회·잠금
Signal`gcs_context`Mode·GCSPR_EL0 저장/복원
Debugger`NT_ARM_GCS`ptrace regset
Core`NT_ARM_GCS` noteThread별 상태 dump
/proc`VmFlags: ss`GCS page 식별

상태 제어와 관찰 경로를 역할별로 정리합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 ===============================================
2 Guarded Control Stack support for AArch64 Linux
3 ===============================================
4
5 This document outlines briefly the interface provided to userspace by Linux in
6 order to support use of the ARM Guarded Control Stack (GCS) feature.
7
8 This is an outline of the most important features and issues only and not
9 intended to be exhaustive.
10
11
12
13 1. General
14 -----------
15
16 * GCS is an architecture feature intended to provide greater protection
17 against return oriented programming (ROP) attacks and to simplify the
18 implementation of features that need to collect stack traces such as
19 profiling.
20
21 * When GCS is enabled a separate guarded control stack is maintained by the
22 PE which is writeable only through specific GCS operations. This
23 stores the call stack only, when a procedure call instruction is
24 performed the current PC is pushed onto the GCS and on RET the
25 address in the LR is verified against that on the top of the GCS.
26
27 * When active the current GCS pointer is stored in the system register
28 GCSPR_EL0. This is readable by userspace but can only be updated
29 via specific GCS instructions.
30
31 * The architecture provides instructions for switching between guarded
32 control stacks with checks to ensure that the new stack is a valid
33 target for switching.
34
35 * The functionality of GCS is similar to that provided by the x86 Shadow
36 Stack feature, due to sharing of userspace interfaces the ABI refers to
37 shadow stacks rather than GCS.
38
39 * Support for GCS is reported to userspace via HWCAP_GCS in the aux vector
40 AT_HWCAP entry.
41
42 * GCS is enabled per thread. While there is support for disabling GCS
43 at runtime this should be done with great care.
44
45 * GCS memory access faults are reported as normal memory access faults.
46
47 * GCS specific errors (those reported with EC 0x2d) will be reported as
48 SIGSEGV with a si_code of SEGV_CPERR (control protection error).
49
50 * GCS is supported only for AArch64.
51
52 * On systems where GCS is supported GCSPR_EL0 is always readable by EL0
53 regardless of the GCS configuration for the thread.
54
55 * The architecture supports enabling GCS without verifying that return values
56 in LR match those in the GCS, the LR will be ignored. This is not supported
57 by Linux.
58
59
60
61 2. Enabling and disabling Guarded Control Stacks
62 -------------------------------------------------
63
64 * GCS is enabled and disabled for a thread via the PR_SET_SHADOW_STACK_STATUS
65 prctl(), this takes a single flags argument specifying which GCS features
66 should be used.
67
68 * When set PR_SHADOW_STACK_ENABLE flag allocates a Guarded Control Stack
69 and enables GCS for the thread, enabling the functionality controlled by
70 GCSCRE0_EL1.{nTR, RVCHKEN, PCRSEL}.
71
72 * When set the PR_SHADOW_STACK_PUSH flag enables the functionality controlled
73 by GCSCRE0_EL1.PUSHMEn, allowing explicit GCS pushes.
74
75 * When set the PR_SHADOW_STACK_WRITE flag enables the functionality controlled
76 by GCSCRE0_EL1.STREn, allowing explicit stores to the Guarded Control Stack.
77
78 * Any unknown flags will cause PR_SET_SHADOW_STACK_STATUS to return -EINVAL.
79
80 * PR_LOCK_SHADOW_STACK_STATUS is passed a bitmask of features with the same
81 values as used for PR_SET_SHADOW_STACK_STATUS. Any future changes to the
82 status of the specified GCS mode bits will be rejected.
83
84 * PR_LOCK_SHADOW_STACK_STATUS allows any bit to be locked, this allows
85 userspace to prevent changes to any future features.
86
87 * There is no support for a process to remove a lock that has been set for
88 it.
89
90 * PR_SET_SHADOW_STACK_STATUS and PR_LOCK_SHADOW_STACK_STATUS affect only the
91 thread that called them, any other running threads will be unaffected.
92
93 * New threads inherit the GCS configuration of the thread that created them.
94
95 * GCS is disabled on exec().
96
97 * The current GCS configuration for a thread may be read with the
98 PR_GET_SHADOW_STACK_STATUS prctl(), this returns the same flags that
99 are passed to PR_SET_SHADOW_STACK_STATUS.
100
101 * If GCS is disabled for a thread after having previously been enabled then
102 the stack will remain allocated for the lifetime of the thread. At present
103 any attempt to reenable GCS for the thread will be rejected, this may be
104 revisited in future.
105
106 * It should be noted that since enabling GCS will result in GCS becoming
107 active immediately it is not normally possible to return from the function
108 that invoked the prctl() that enabled GCS. It is expected that the normal
109 usage will be that GCS is enabled very early in execution of a program.
110
111
112
113 3. Allocation of Guarded Control Stacks
114 ----------------------------------------
115
116 * When GCS is enabled for a thread a new Guarded Control Stack will be
117 allocated for it of half the standard stack size or 2 gigabytes,
118 whichever is smaller.
119
120 * When a new thread is created by a thread which has GCS enabled then a
121 new Guarded Control Stack will be allocated for the new thread with
122 half the size of the standard stack.
123
124 * When a stack is allocated by enabling GCS or during thread creation then
125 the top 8 bytes of the stack will be initialised to 0 and GCSPR_EL0 will
126 be set to point to the address of this 0 value, this can be used to
127 detect the top of the stack.
128
129 * Additional Guarded Control Stacks can be allocated using the
130 map_shadow_stack() system call.
131
132 * Stacks allocated using map_shadow_stack() can optionally have an end of
133 stack marker and cap placed at the top of the stack. If the flag
134 SHADOW_STACK_SET_TOKEN is specified a cap will be placed on the stack,
135 if SHADOW_STACK_SET_MARKER is not specified the cap will be the top 8
136 bytes of the stack and if it is specified then the cap will be the next
137 8 bytes. While specifying just SHADOW_STACK_SET_MARKER by itself is
138 valid since the marker is all bits 0 it has no observable effect.
139
140 * Stacks allocated using map_shadow_stack() must have a size which is a
141 multiple of 8 bytes larger than 8 bytes and must be 8 bytes aligned.
142
143 * An address can be specified to map_shadow_stack(), if one is provided then
144 it must be aligned to a page boundary.
145
146 * When a thread is freed the Guarded Control Stack initially allocated for
147 that thread will be freed. Note carefully that if the stack has been
148 switched this may not be the stack currently in use by the thread.
149
150
151 4. Signal handling
152 --------------------
153
154 * A new signal frame record gcs_context encodes the current GCS mode and
155 pointer for the interrupted context on signal delivery. This will always
156 be present on systems that support GCS.
157
158 * The record contains a flag field which reports the current GCS configuration
159 for the interrupted context as PR_GET_SHADOW_STACK_STATUS would.
160
161 * The signal handler is run with the same GCS configuration as the interrupted
162 context.
163
164 * When GCS is enabled for the interrupted thread a signal handling specific
165 GCS cap token will be written to the GCS, this is an architectural GCS cap
166 with the token type (bits 0..11) all clear. The GCSPR_EL0 reported in the
167 signal frame will point to this cap token.
168
169 * The signal handler will use the same GCS as the interrupted context.
170
171 * When GCS is enabled on signal entry a frame with the address of the signal
172 return handler will be pushed onto the GCS, allowing return from the signal
173 handler via RET as normal. This will not be reported in the gcs_context in
174 the signal frame.
175
176
177 5. Signal return
178 -----------------
179
180 When returning from a signal handler:
181
182 * If there is a gcs_context record in the signal frame then the GCS flags
183 and GCSPR_EL0 will be restored from that context prior to further
184 validation.
185
186 * If there is no gcs_context record in the signal frame then the GCS
187 configuration will be unchanged.
188
189 * If GCS is enabled on return from a signal handler then GCSPR_EL0 must
190 point to a valid GCS signal cap record, this will be popped from the
191 GCS prior to signal return.
192
193 * If the GCS configuration is locked when returning from a signal then any
194 attempt to change the GCS configuration will be treated as an error. This
195 is true even if GCS was not enabled prior to signal entry.
196
197 * GCS may be disabled via signal return but any attempt to enable GCS via
198 signal return will be rejected.
199
200
201 6. ptrace extensions
202 ---------------------
203
204 * A new regset NT_ARM_GCS is defined for use with PTRACE_GETREGSET and
205 PTRACE_SETREGSET.
206
207 * The GCS mode, including enable and disable, may be configured via ptrace.
208 If GCS is enabled via ptrace no new GCS will be allocated for the thread.
209
210 * Configuration via ptrace ignores locking of GCS mode bits.
211
212
213 7. ELF coredump extensions
214 ---------------------------
215
216 * NT_ARM_GCS notes will be added to each coredump for each thread of the
217 dumped process. The contents will be equivalent to the data that would
218 have been read if a PTRACE_GETREGSET of the corresponding type were
219 executed for each thread when the coredump was generated.
220
221
222
223 8. /proc extensions
224 --------------------
225
226 * Guarded Control Stack pages will include "ss" in their VmFlags in
227 /proc/<pid>/smaps.
228

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

문서 범위

1-12

이 문서는 ARM Guarded Control Stack(GCS) 기능을 사용하도록 AArch64 Linux가 userspace에 제공하는 interface를 간략히 설명합니다. 가장 중요한 기능과 주의점의 개요이며 모든 세부사항을 포괄하려는 문서는 아닙니다.

GCS 기본 동작과 오류 보고

13-60

GCS는 return-oriented programming(ROP) 공격에 대한 보호를 강화하고 profiling처럼 stack trace를 수집하는 기능의 구현을 단순화하기 위한 architecture 기능입니다.

GCS가 활성화되면 PE는 일반 data stack과 별도의 guarded control stack을 유지합니다. 이 stack은 전용 GCS operation으로만 쓸 수 있고 call stack만 저장합니다. Procedure call instruction을 실행하면 현재 PC를 GCS에 push하며, `RET`에서는 `LR`의 address를 GCS top 값과 비교해 검증합니다.

활성 GCS pointer는 `GCSPR_EL0` system register에 저장됩니다. Userspace가 읽을 수 있지만 전용 GCS instruction으로만 갱신할 수 있습니다. Architecture는 새 stack이 유효한 switch target인지 검사하면서 GCS 사이를 전환하는 instruction도 제공합니다.

기능은 x86 Shadow Stack과 유사하고 userspace interface를 공유하므로 ABI에서는 GCS보다 `shadow stack`이라는 용어를 사용합니다. 지원 여부는 auxiliary vector의 `AT_HWCAP` entry에 있는 `HWCAP_GCS`로 보고됩니다.

  • GCS는 thread별로 활성화합니다. Runtime에 끌 수 있지만 매우 신중해야 합니다.
  • GCS memory access fault는 일반 memory access fault로 보고됩니다.
  • EC `0x2d`로 보고되는 GCS 전용 오류는 `SIGSEGV`, `si_code = SEGV_CPERR`인 control-protection error로 전달됩니다.
  • GCS는 AArch64에서만 지원됩니다.
  • GCS 지원 system의 `GCSPR_EL0`는 해당 thread의 GCS 설정과 무관하게 EL0에서 항상 읽을 수 있습니다.
  • Architecture에는 `LR`과 GCS의 return value를 비교하지 않고 GCS를 활성화하는 mode가 있지만 Linux는 이를 지원하지 않습니다.

GCS 활성화·잠금·상태 조회

61-112

Thread의 GCS는 `PR_SET_SHADOW_STACK_STATUS` `prctl()`로 활성화하거나 비활성화합니다. 단일 flags 인자가 사용할 GCS 기능을 지정합니다.

Operation 또는 flag효과
`PR_SHADOW_STACK_ENABLE`GCS를 할당·활성화하고 `GCSCRE0_EL1.{nTR, RVCHKEN, PCRSEL}` 기능을 켭니다.
`PR_SHADOW_STACK_PUSH``GCSCRE0_EL1.PUSHMEn`을 켜 명시적 GCS push를 허용합니다.
`PR_SHADOW_STACK_WRITE``GCSCRE0_EL1.STREn`을 켜 GCS에 대한 명시적 store를 허용합니다.
알 수 없는 flag`PR_SET_SHADOW_STACK_STATUS`가 `-EINVAL`을 반환합니다.
`PR_LOCK_SHADOW_STACK_STATUS`같은 bitmask의 mode bit를 잠가 이후 상태 변경을 거부합니다.
`PR_GET_SHADOW_STACK_STATUS`현재 thread의 설정을 `PR_SET_SHADOW_STACK_STATUS`와 같은 flag 형식으로 반환합니다.

`PR_LOCK_SHADOW_STACK_STATUS`는 아직 정의되지 않은 미래 기능 bit도 잠글 수 있어 userspace가 향후 기능 변경까지 막을 수 있습니다. Process가 이미 설정한 lock을 제거하는 기능은 없습니다.

`PR_SET_SHADOW_STACK_STATUS`와 `PR_LOCK_SHADOW_STACK_STATUS`는 호출 thread에만 영향을 주고 다른 실행 thread는 바뀌지 않습니다. 새 thread는 생성한 thread의 GCS 설정을 상속하지만 `exec()`에서는 GCS가 비활성화됩니다.

한번 활성화했다가 끈 GCS stack은 thread lifetime 동안 계속 할당되어 있습니다. 현재는 그 thread에서 GCS를 다시 활성화하려는 시도가 거부되지만 미래에 바뀔 수 있습니다.

GCS 활성화는 즉시 효력이 생기므로 일반적으로 이를 켠 `prctl()` 호출 함수에서 정상적으로 return할 수 없습니다. 프로그램 실행의 아주 이른 단계에서 활성화하는 사용법이 예상됩니다.

Guarded Control Stack 할당

113-150

Thread에서 GCS를 활성화하면 표준 stack 크기의 절반과 2GiB 중 작은 크기로 새 GCS를 할당합니다. GCS가 활성화된 thread가 새 thread를 만들면 새 thread에는 표준 stack 절반 크기의 별도 GCS가 할당됩니다.

활성화 또는 thread 생성으로 stack을 할당할 때 top 8 byte를 `0`으로 초기화하고 `GCSPR_EL0`가 이 `0`의 address를 가리키게 합니다. 이 값으로 stack top을 감지할 수 있습니다.

추가 GCS는 `map_shadow_stack()` system call로 할당할 수 있습니다. `SHADOW_STACK_SET_TOKEN`을 지정하면 stack top에 cap을 놓습니다. `SHADOW_STACK_SET_MARKER`가 없으면 cap은 top 8 byte이고, 함께 지정하면 marker 다음 8 byte에 cap이 놓입니다. Marker만 지정하는 것도 유효하지만 marker가 all-zero라 관찰 가능한 효과는 없습니다.

`map_shadow_stack()` 크기는 8 byte보다 크면서 8 byte 배수여야 하고 8-byte aligned여야 합니다. Address를 지정한다면 page boundary에 정렬해야 합니다.

Thread가 해제되면 처음 그 thread에 할당된 GCS가 해제됩니다. Stack을 전환했다면 이것이 현재 thread가 사용 중인 stack과 다를 수 있다는 점에 주의해야 합니다.

Signal 전달 시 GCS 상태

151-176

새 signal-frame record `gcs_context`는 signal 전달로 중단된 context의 현재 GCS mode와 pointer를 인코딩합니다. GCS 지원 system에서는 항상 존재하며, flag field는 `PR_GET_SHADOW_STACK_STATUS`와 같은 형식으로 중단 context의 설정을 보고합니다.

Signal handler는 중단된 context와 같은 GCS 설정과 같은 GCS를 사용합니다. 중단 thread에서 GCS가 활성화되어 있으면 signal 전용 GCS cap token을 GCS에 씁니다. 이는 token type인 bit `0..11`이 모두 clear된 architecture GCS cap이며, signal frame의 `GCSPR_EL0`는 이 cap token을 가리킵니다.

Signal 진입 시 GCS가 활성화되어 있으면 signal return handler address가 든 frame을 GCS에 push하므로 handler가 평소처럼 `RET`으로 돌아갈 수 있습니다. 이 frame은 signal frame의 `gcs_context`에는 보고되지 않습니다.

Signal return 검증

177-200

Signal handler에서 돌아올 때 다음 규칙을 적용합니다.

  • Signal frame에 `gcs_context`가 있으면 추가 검증 전에 그 context에서 GCS flag와 `GCSPR_EL0`를 복원합니다.
  • `gcs_context`가 없으면 GCS 설정은 변경되지 않습니다.
  • Return 시 GCS가 활성화되어 있다면 `GCSPR_EL0`는 유효한 GCS signal cap record를 가리켜야 하며, signal return 전에 이 record를 pop합니다.
  • GCS 설정이 잠겨 있으면 signal 진입 전에 GCS가 꺼져 있었더라도 설정을 바꾸려는 모든 시도를 오류로 처리합니다.
  • Signal return으로 GCS를 끌 수는 있지만 켜려는 시도는 거부됩니다.

ptrace 확장

201-212

`PTRACE_GETREGSET`과 `PTRACE_SETREGSET`에서 사용할 새 regset `NT_ARM_GCS`가 정의됩니다. `ptrace`로 enable/disable을 포함한 GCS mode를 설정할 수 있지만, 이 방법으로 활성화해도 thread에 새 GCS를 할당하지 않습니다. `ptrace` 설정은 GCS mode bit의 lock을 무시합니다.

ELF coredump 확장

213-222

Dump 대상 process의 각 thread마다 `NT_ARM_GCS` note가 coredump에 추가됩니다. 내용은 coredump 생성 시 각 thread에 대응하는 type으로 `PTRACE_GETREGSET`을 수행했을 때 읽을 data와 같습니다.

/proc 확장

223-227

Guarded Control Stack page는 `/proc/<pid>/smaps`의 `VmFlags`에 `ss`를 포함합니다.