Skip to content

Commit 727a943

Browse files
committed
x86-64: support IBT (control-flow integrity for indirect jumps)
1 parent ce0f6ca commit 727a943

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

driver/Clflags.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ let option_mthumb = ref (Configuration.model = "armv7m")
4040
let option_Osize = ref false
4141
let option_finline = ref true
4242
let option_finline_functions_called_once = ref true
43+
let option_fcf_protection = ref false
4344
let option_dprepro = ref false
4445
let option_dparse = ref false
4546
let option_dcmedium = ref false

driver/Driver.ml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ Code generation options: (use -fno-<opt> to turn off -f<opt>)
209209
-falign-branch-targets <n> Set alignment (in bytes) of branch targets
210210
-falign-cond-branches <n> Set alignment (in bytes) of conditional branches
211211
-fcommon Put uninitialized globals in the common section [on].
212+
-fcf-protection=branch Add control-flow integrity checks
213+
-fcf-protection=none Don't add control-flow integrity checks
212214
|} ^
213215
target_help ^
214216
toolchain_help ^
@@ -278,6 +280,13 @@ let cmdline_actions =
278280
then option_fpie := true
279281
else warning no_loc Unnamed
280282
"option -fpie not supported on this platform, ignored" in
283+
let set_cf_protection () =
284+
match Configuration.arch, Configuration.model with
285+
| "x86", "64" ->
286+
option_fcf_protection := true
287+
| _ ->
288+
error no_loc "Option -fcf_protection=branch not supported on this target"
289+
in
281290
[
282291
(* Getting help *)
283292
Exact "-help", Unit print_usage_and_exit;
@@ -321,8 +330,10 @@ let cmdline_actions =
321330
Exact "-fpie", Unit set_pie_mode;
322331
Exact "-fPIE", Unit set_pie_mode;
323332
Exact "-fno-pie", Unset option_fpie;
324-
Exact "-fno-PIE", Unset option_fpie ] @
325-
f_opt "common" option_fcommon @
333+
Exact "-fno-PIE", Unset option_fpie;
334+
Exact "-fcf-protection=branch", Unit set_cf_protection;
335+
Exact "-fcf-protection=none", Unset option_fcf_protection ] @
336+
f_opt "common" option_fcommon @
326337
(* Target processor options *)
327338
(if Configuration.arch = "arm" then
328339
if Configuration.model = "armv6" 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
@@ -183,7 +183,23 @@ module ELF_System : SYSTEM =
183183

184184
let print_var_info = elf_print_var_info
185185

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

188204
let print_comm_decl oc name sz al =
189205
fprintf oc " .comm %a, %s, %d\n" symbol name (Z.to_string sz) al
@@ -754,7 +770,9 @@ module Target(System: SYSTEM):TARGET =
754770
fprintf oc " leaq %a(%%rip), %a\n" label l ireg tmp1;
755771
fprintf oc " movslq (%a, %a, 4), %a\n" ireg tmp1 ireg r ireg tmp2;
756772
fprintf oc " addq %a, %a\n" ireg tmp2 ireg tmp1;
757-
fprintf oc " jmp *%a\n" ireg tmp1
773+
fprintf oc " %sjmp *%a\n"
774+
(if !Clflags.option_fcf_protection then "notrack " else "")
775+
ireg tmp1
758776
end else begin
759777
fprintf oc " jmp *%a(, %a, 4)\n" label l ireg r
760778
end
@@ -911,6 +929,8 @@ module Target(System: SYSTEM):TARGET =
911929

912930
let print_instructions oc fn =
913931
current_function_sig := fn.fn_sig;
932+
if !Clflags.option_fcf_protection then
933+
fprintf oc " endbr64\n";
914934
List.iter (print_instruction oc) fn.fn_code
915935

916936
let print_optional_fun_info _ = ()

0 commit comments

Comments
 (0)