Skip to content

Commit 66801e0

Browse files
committed
x86-64: support IBT (control-flow integrity for indirect jumps)
1 parent 76f7fea commit 66801e0

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

driver/Clflags.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ let option_mthumb = ref (Configuration.model = "armv7m")
3838
let option_Osize = ref false
3939
let option_finline = ref true
4040
let option_finline_functions_called_once = ref true
41+
let option_fcf_protection = ref false
4142
let option_dprepro = ref false
4243
let option_dparse = ref false
4344
let option_dcmedium = ref false

driver/Driver.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ Code generation options: (use -fno-<opt> to turn off -f<opt>)
207207
-falign-branch-targets <n> Set alignment (in bytes) of branch targets
208208
-falign-cond-branches <n> Set alignment (in bytes) of conditional branches
209209
-fcommon Put uninitialized globals in the common section [on].
210+
-fcf-protection=branch Add control-flow integrity checks
211+
-fcf-protection=none Don't add control-flow integrity checks
210212
|} ^
211213
target_help ^
212214
toolchain_help ^
@@ -266,6 +268,13 @@ let cmdline_actions =
266268
if n <= 0 || ((n land (n - 1)) <> 0) then
267269
error no_loc "requested alignment %d is not a power of 2" n
268270
in
271+
let set_cf_protection () =
272+
match Configuration.arch, Configuration.model with
273+
| "x86", "64" ->
274+
option_fcf_protection := true
275+
| _ ->
276+
error no_loc "Option -fcf_protection=branch not supported on this target"
277+
in
269278
[
270279
(* Getting help *)
271280
Exact "-help", Unit print_usage_and_exit;
@@ -301,7 +310,10 @@ let cmdline_actions =
301310
Exact "-ffloat-const-prop", Integer(fun n -> option_ffloatconstprop := n);
302311
Exact "-falign-functions", Integer(fun n -> check_align n; option_falignfunctions := Some n);
303312
Exact "-falign-branch-targets", Integer(fun n -> check_align n; option_falignbranchtargets := n);
304-
Exact "-falign-cond-branches", Integer(fun n -> check_align n; option_faligncondbranchs := n);] @
313+
Exact "-falign-cond-branches", Integer(fun n -> check_align n; option_faligncondbranchs := n);
314+
Exact "-fcf-protection=branch", Unit set_cf_protection;
315+
Exact "-fcf-protection=none", Unset option_fcf_protection
316+
] @
305317
f_opt "common" option_fcommon @
306318
(* Target processor options *)
307319
(if Configuration.arch = "arm" then

runtime/x86_64/sysdeps.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@
3838

3939
.section .note.GNU-stack,"",%progbits
4040

41+
// The runtime library code is compatible with IBT and SHSTK
42+
.section .note.gnu.property,"a"
43+
.align 8
44+
.long 4
45+
.long 4f - 1f
46+
.long 5
47+
.string "GNU"
48+
1: .align 8
49+
.long 0xc0000002
50+
.long 3f - 2f
51+
2: .long 0x3
52+
3: .align 8
53+
4:
54+
4155
#define GLOB(x) x
4256
#define FUNCTION(f) \
4357
.text; \

x86/TargetPrinter.ml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,23 @@ module ELF_System : SYSTEM =
178178

179179
let print_var_info = elf_print_var_info
180180

181-
let print_epilogue _ = ()
181+
let print_epilogue oc =
182+
if !Clflags.option_fcf_protection then begin
183+
output_string oc
184+
{| .section .note.gnu.property,"a"
185+
.align 8
186+
.long 4
187+
.long 4f - 1f
188+
.long 5
189+
.string "GNU"
190+
1: .align 8
191+
.long 0xc0000002
192+
.long 3f - 2f
193+
2: .long 0x3
194+
3: .align 8
195+
4:
196+
|}
197+
end
182198

183199
let print_comm_decl oc name sz al =
184200
fprintf oc " .comm %a, %s, %d\n" symbol name (Z.to_string sz) al
@@ -744,7 +760,9 @@ module Target(System: SYSTEM):TARGET =
744760
fprintf oc " leaq %a(%%rip), %a\n" label l ireg tmp1;
745761
fprintf oc " movslq (%a, %a, 4), %a\n" ireg tmp1 ireg r ireg tmp2;
746762
fprintf oc " addq %a, %a\n" ireg tmp2 ireg tmp1;
747-
fprintf oc " jmp *%a\n" ireg tmp1
763+
fprintf oc " %sjmp *%a\n"
764+
(if !Clflags.option_fcf_protection then "notrack " else "")
765+
ireg tmp1
748766
end else begin
749767
fprintf oc " jmp *%a(, %a, 4)\n" label l ireg r
750768
end
@@ -901,6 +919,8 @@ module Target(System: SYSTEM):TARGET =
901919

902920
let print_instructions oc fn =
903921
current_function_sig := fn.fn_sig;
922+
if !Clflags.option_fcf_protection then
923+
fprintf oc " endbr64\n";
904924
List.iter (print_instruction oc) fn.fn_code
905925

906926
let print_optional_fun_info _ = ()

0 commit comments

Comments
 (0)