1
- From 4900a67c4daabedbf83b563d77830c3f1c6eb599 Mon Sep 17 00:00:00 2001
2
- From: Gilles Duboscq <
[email protected] >
3
- Date: Wed, 23 Apr 2025 18:45:10 +0200
4
- Subject: [PATCH 2/3] Add support __SANDBOX_SWCFI__ in unix64.S and win64.S
1
+ From 0e770017b54a270b80a4c696703e79560f263541 Mon Sep 17 00:00:00 2001
2
+ From: Matthias Neugschwandtner <
[email protected] >
3
+ Date: Wed, 10 Sep 2025 14:54:12 +0200
4
+ Subject: [PATCH 2/3] Add support __SANDBOX_SWCFI__ in unix64.S and
5
+ win64.S
5
6
6
7
This mode requires a software check for jump targets.
8
+
9
+ Co-authored-by: Gilles Duboscq <
[email protected] >
10
+
7
11
---
8
12
src/x86/internal64.h | 4 +++
9
- src/x86/unix64.S | 30 ++++++++++++++++++++
10
- src/x86/win64.S | 65 +++++++++++++++++++++++++++++++++++++++++++-
11
- 3 files changed, 98 insertions(+), 1 deletion (-)
13
+ src/x86/unix64.S | 54 +++++++++++++++++++++++++++++--
14
+ src/x86/win64.S | 77 +++++++++++++++++++++++++++++++++++++++++++-
15
+ 3 files changed, 131 insertions(+), 4 deletions (-)
12
16
13
17
diff --git a/src/x86/internal64.h b/src/x86/internal64.h
14
18
index 282b408..7142645 100644
@@ -27,10 +31,25 @@ index 282b408..7142645 100644
27
31
#define UNIX64_TRAMP_SIZE 32
28
32
#endif
29
33
diff --git a/src/x86/unix64.S b/src/x86/unix64.S
30
- index d9c5bd4..10d9c8d 100644
34
+ index d9c5bd4..3239ea3 100644
31
35
--- a/src/x86/unix64.S
32
36
+++ b/src/x86/unix64.S
33
- @@ -98,6 +98,13 @@ L(ret_from_load_sse):
37
+ @@ -39,7 +39,13 @@
38
+ actual table. The entry points into the table are all 8 bytes.
39
+ The use of ORG asserts that we're at the correct location. */
40
+ /* ??? The clang assembler doesn't handle .org with symbolic expressions. */
41
+ - #ifdef __CET__
42
+ + #ifdef __SANDBOX_SWCFI__
43
+ + /* Increase slot size to accomodate ENDBR64 (+4 bytes) and SWCFI pattern (+24 bytes) for ret. */
44
+ + # define E(BASE, X) .balign 8; .org BASE + (X) * 40
45
+ + #elif defined __SANDBOX_HWCFI__
46
+ + /* Increase slot size to accomodate ENDBR64 (+4 bytes) and HWCFI pattern (+6 bytes) for ret. */
47
+ + # define E(BASE, X) .balign 8; .org BASE + (X) * 24
48
+ + #elif defined __CET__
49
+ /* Double slot size to 16 byte to add 4 bytes of ENDBR64. */
50
+ # define E(BASE, X) .balign 8; .org BASE + X * 16
51
+ #elif defined(__clang__) || defined(__APPLE__) || (defined (__sun__) && defined(__svr4__))
52
+ @@ -98,6 +104,13 @@ L(ret_from_load_sse):
34
53
35
54
/* Deallocate the reg arg area, except for r10, then load via pop. */
36
55
leaq 0xb8(%r10), %rsp
@@ -44,7 +63,19 @@ index d9c5bd4..10d9c8d 100644
44
63
popq %r10
45
64
46
65
/* Call the user function. */
47
- @@ -126,6 +133,13 @@ L(UW2):
66
+ @@ -119,13 +132,24 @@ L(UW2):
67
+ movzbl %cl, %r10d
68
+ leaq L(store_table)(%rip), %r11
69
+ ja L(sa)
70
+ - #ifdef __CET__
71
+ + #ifdef __SANDBOX_SWCFI__
72
+ + lea (%r10, %r10, 4), %r10
73
+ + #elif defined __SANDBOX_HWCFI__
74
+ + lea (%r10, %r10, 2), %r10
75
+ + #elif defined __CET__
76
+ /* NB: Originally, each slot is 8 byte. 4 bytes of ENDBR64 +
77
+ 4 bytes NOP padding double slot size to 16 bytes. */
78
+ addl %r10d, %r10d
48
79
#endif
49
80
leaq (%r11, %r10, 8), %r10
50
81
@@ -58,7 +89,18 @@ index d9c5bd4..10d9c8d 100644
58
89
/* Prep for the structure cases: scratch area in redzone. */
59
90
leaq -20(%rsp), %rsi
60
91
jmp *%r10
61
- @@ -318,6 +332,13 @@ L(UW10):
92
+ @@ -312,12 +336,25 @@ L(UW10):
93
+ movzbl %al, %r10d
94
+ leaq L(load_table)(%rip), %r11
95
+ ja L(la)
96
+ - #ifdef __CET__
97
+ + #ifdef __SANDBOX_SWCFI__
98
+ + lea (%r10, %r10, 4), %r10
99
+ + #elif defined __SANDBOX_HWCFI__
100
+ + lea (%r10, %r10, 2), %r10
101
+ + #elif defined __CET__
102
+ /* NB: Originally, each slot is 8 byte. 4 bytes of ENDBR64 +
103
+ 4 bytes NOP padding double slot size to 16 bytes. */
62
104
addl %r10d, %r10d
63
105
#endif
64
106
leaq (%r11, %r10, 8), %r10
@@ -68,11 +110,13 @@ index d9c5bd4..10d9c8d 100644
68
110
+ jz 1f
69
111
+ int3
70
112
+ 1:
113
+ + #elif defined __SANDBOX_HWCFI__
114
+ + test %r10, (%r10)
71
115
+ #endif
72
116
leaq ffi_closure_RED_RVALUE(%rsp), %rsi
73
117
jmp *%r10
74
118
75
- @@ -538,6 +559,15 @@ C(trampoline_code_table):
119
+ @@ -538,6 +575,17 @@ C(trampoline_code_table):
76
120
movl X86_CODE_OFFSET(%rip), %r10d /* Copy code into %r10 */
77
121
#else
78
122
movq X86_CODE_OFFSET(%rip), %r10 /* Copy code into %r10 */
@@ -85,26 +129,31 @@ index d9c5bd4..10d9c8d 100644
85
129
+ int3
86
130
+ 1:
87
131
+ popq %rdi
132
+ + #elif defined __SANDBOX_HWCFI__
133
+ + test %r10, (%r10)
88
134
#endif
89
135
jmp *%r10 /* Jump to code */
90
136
.align 8
91
137
diff --git a/src/x86/win64.S b/src/x86/win64.S
92
- index 58ec6a1..d1a180a 100644
138
+ index 58ec6a1..8b142d9 100644
93
139
--- a/src/x86/win64.S
94
140
+++ b/src/x86/win64.S
95
- @@ -27,7 +27,10 @@
141
+ @@ -27,7 +27,13 @@
96
142
actual table. The entry points into the table are all 8 bytes.
97
143
The use of ORG asserts that we're at the correct location. */
98
144
/* ??? The clang assembler doesn't handle .org with symbolic expressions. */
99
145
- #if defined(__clang__) || defined(__APPLE__) || (defined (__sun__) && defined(__svr4__))
100
146
+ #ifdef __SANDBOX_SWCFI__
101
- + /* Triple slot size to 24 byte to add ENDBR64 and jump for ret. */
147
+ + /* Increase slot size to accomodate ENDBR64 (+4 bytes) and SWCFI pattern (+24 bytes) for ret. */
148
+ + # define E(BASE, X) .balign 8; .org BASE + (X) * 40
149
+ + #elif defined __SANDBOX_HWCFI__
150
+ + /* Increase slot size to accomodate ENDBR64 (+4 bytes) and HWCFI pattern (+6 bytes) for ret. */
102
151
+ # define E(BASE, X) .balign 8; .org BASE + (X) * 24
103
152
+ #elif defined(__clang__) || defined(__APPLE__) || (defined (__sun__) && defined(__svr4__))
104
153
# define E(BASE, X) .balign 8
105
154
#else
106
155
# define E(BASE, X) .balign 8; .org BASE + (X) * 8
107
- @@ -73,18 +76,58 @@ C(ffi_call_win64):
156
+ @@ -73,18 +79,67 @@ C(ffi_call_win64):
108
157
movq 24(%rsp), %r9
109
158
movsd 24(%rsp), %xmm3
110
159
@@ -118,6 +167,10 @@ index 58ec6a1..d1a180a 100644
118
167
+ 1:
119
168
+ popq %r10
120
169
+ call *%r11
170
+ + #elif defined __SANDBOX_HWCFI__
171
+ + movq 16(%rbp), %r11
172
+ + test %r11, (%r11)
173
+ + call *%r11
121
174
+ #else
122
175
call *16(%rbp)
123
176
+ #endif
@@ -127,19 +180,24 @@ index 58ec6a1..d1a180a 100644
127
180
leaq 0f(%rip), %r10
128
181
cmpl $FFI_TYPE_SMALL_STRUCT_4B, %ecx
129
182
+
130
- + #ifdef __SANDBOX_SWCFI__
131
- + /* avoid leave in this mode, use larger slots (3 *8) */
183
+ + #ifdef __SANDBOX_CFI__
184
+ + /* avoid leave in this mode, use larger slots (5 *8) */
132
185
+ ja 99f
133
- + movl %ecx, %r11d
134
- + addl %ecx, %ecx
135
- + addl %r11d, %ecx
186
+ + #ifdef __SANDBOX_SWCFI__
187
+ + lea (%rcx, %rcx, 4), %rcx
136
188
+ leaq (%r10, %rcx, 8), %r10
137
189
+ movl (%r10), %ecx
138
190
+ addl $0x5e1f00d, %ecx
139
191
+ jz 1f
140
192
+ int3
141
193
+ 1:
142
194
+ jmp *%r10
195
+ + #elif defined __SANDBOX_HWCFI__
196
+ + lea (%rcx, %rcx, 2), %rcx
197
+ + leaq (%r10, %rcx, 8), %r10
198
+ + test %r10, (%r10)
199
+ + jmp *%r10
200
+ + #endif /* SWCFI/HWCFI */
143
201
+
144
202
+ #define jmp_target \
145
203
+ _CET_ENDBR
@@ -163,7 +221,7 @@ index 58ec6a1..d1a180a 100644
163
221
#define epilogue \
164
222
leaveq; \
165
223
cfi_remember_state; \
166
- @@ -92,66 +135 ,86 @@ C(ffi_call_win64):
224
+ @@ -92,66 +147 ,86 @@ C(ffi_call_win64):
167
225
cfi_restore(%rbp); \
168
226
ret; \
169
227
cfi_restore_state
@@ -251,5 +309,5 @@ index 58ec6a1..d1a180a 100644
251
309
epilogue
252
310
253
311
- -
254
- 2.43.0
312
+ 2.34.1
255
313
0 commit comments