Blame SOURCES/gcc8-harden-1.patch

c616bf
From 88bf1c3910e4cf97dcb85c6d32291c23e572a516 Mon Sep 17 00:00:00 2001
c616bf
From: "H.J. Lu" <hjl.tools@gmail.com>
c616bf
Date: Wed, 27 Oct 2021 07:48:54 -0700
c616bf
Subject: [PATCH 1/4] x86: Add -mharden-sls=[none|all|return|indirect-branch]
c616bf
c616bf
Add -mharden-sls= to mitigate against straight line speculation (SLS)
c616bf
for function return and indirect branch by adding an INT3 instruction
c616bf
after function return and indirect branch.
c616bf
c616bf
gcc/
c616bf
c616bf
	PR target/102952
c616bf
	* config/i386/i386-opts.h (harden_sls): New enum.
c616bf
	* config/i386/i386.c (output_indirect_thunk): Mitigate against
c616bf
	SLS for function return.
c616bf
	(ix86_output_function_return): Likewise.
c616bf
	(ix86_output_jmp_thunk_or_indirect): Mitigate against indirect
c616bf
	branch.
c616bf
	(ix86_output_indirect_jmp): Likewise.
c616bf
	(ix86_output_call_insn): Likewise.
c616bf
	* config/i386/i386.opt: Add -mharden-sls=.
c616bf
	* doc/invoke.texi: Document -mharden-sls=.
c616bf
c616bf
gcc/testsuite/
c616bf
c616bf
	PR target/102952
c616bf
	* gcc.target/i386/harden-sls-1.c: New test.
c616bf
	* gcc.target/i386/harden-sls-2.c: Likewise.
c616bf
	* gcc.target/i386/harden-sls-3.c: Likewise.
c616bf
	* gcc.target/i386/harden-sls-4.c: Likewise.
c616bf
	* gcc.target/i386/harden-sls-5.c: Likewise.
c616bf
c616bf
(cherry picked from commit 53a643f8568067d7700a9f2facc8ba39974973d3)
c616bf
---
c616bf
 gcc/config/i386/i386-opts.h                  |  7 +++++++
c616bf
 gcc/config/i386/i386.c                       | 22 +++++++++++++++-----
c616bf
 gcc/config/i386/i386.opt                     | 20 ++++++++++++++++++
c616bf
 gcc/doc/invoke.texi                          | 10 ++++++++-
c616bf
 gcc/testsuite/gcc.target/i386/harden-sls-1.c | 14 +++++++++++++
c616bf
 gcc/testsuite/gcc.target/i386/harden-sls-2.c | 14 +++++++++++++
c616bf
 gcc/testsuite/gcc.target/i386/harden-sls-3.c | 14 +++++++++++++
c616bf
 gcc/testsuite/gcc.target/i386/harden-sls-4.c | 16 ++++++++++++++
c616bf
 gcc/testsuite/gcc.target/i386/harden-sls-5.c | 17 +++++++++++++++
c616bf
 9 files changed, 128 insertions(+), 6 deletions(-)
c616bf
 create mode 100644 gcc/testsuite/gcc.target/i386/harden-sls-1.c
c616bf
 create mode 100644 gcc/testsuite/gcc.target/i386/harden-sls-2.c
c616bf
 create mode 100644 gcc/testsuite/gcc.target/i386/harden-sls-3.c
c616bf
 create mode 100644 gcc/testsuite/gcc.target/i386/harden-sls-4.c
c616bf
 create mode 100644 gcc/testsuite/gcc.target/i386/harden-sls-5.c
c616bf
c616bf
diff --git a/gcc/config/i386/i386-opts.h b/gcc/config/i386/i386-opts.h
c616bf
index 46366cbfa72..34718b6d52c 100644
c616bf
--- a/gcc/config/i386/i386-opts.h
c616bf
+++ b/gcc/config/i386/i386-opts.h
c616bf
@@ -119,4 +119,11 @@ enum indirect_branch {
c616bf
   indirect_branch_thunk_extern
c616bf
 };
c616bf
 
c616bf
+enum harden_sls {
c616bf
+  harden_sls_none = 0,
c616bf
+  harden_sls_return = 1 << 0,
c616bf
+  harden_sls_indirect_branch = 1 << 1,
c616bf
+  harden_sls_all = harden_sls_return | harden_sls_indirect_branch
c616bf
+};
c616bf
+
c616bf
 #endif
c616bf
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
c616bf
index 31502774ef3..eb9303f8742 100644
c616bf
--- a/gcc/config/i386/i386.c
c616bf
+++ b/gcc/config/i386/i386.c
c616bf
@@ -10977,6 +10977,9 @@ output_indirect_thunk (enum indirect_thunk_prefix need_prefix,
c616bf
     fputs ("\tbnd ret\n", asm_out_file);
c616bf
   else
c616bf
     fputs ("\tret\n", asm_out_file);
c616bf
+
c616bf
+  if ((ix86_harden_sls & harden_sls_return))
c616bf
+    fputs ("\tint3\n", asm_out_file);
c616bf
 }
c616bf
 
c616bf
 /* Output a funtion with a call and return thunk for indirect branch.
c616bf
@@ -28728,6 +28731,8 @@ ix86_output_jmp_thunk_or_indirect (const char *thunk_name,
c616bf
 	fprintf (asm_out_file, "\tjmp\t");
c616bf
       assemble_name (asm_out_file, thunk_name);
c616bf
       putc ('\n', asm_out_file);
c616bf
+      if ((ix86_harden_sls & harden_sls_indirect_branch))
c616bf
+	fputs ("\tint3\n", asm_out_file);
c616bf
     }
c616bf
   else
c616bf
     output_indirect_thunk (need_prefix, regno);
c616bf
@@ -28973,10 +28978,10 @@ ix86_output_indirect_jmp (rtx call_op)
c616bf
 	gcc_unreachable ();
c616bf
 
c616bf
       ix86_output_indirect_branch (call_op, "%0", true);
c616bf
-      return "";
c616bf
     }
c616bf
   else
c616bf
-    return "%!jmp\t%A0";
c616bf
+    output_asm_insn ("%!jmp\t%A0", &call_op);
c616bf
+  return (ix86_harden_sls & harden_sls_indirect_branch) ? "int3" : "";
c616bf
 }
c616bf
 
c616bf
 /* Output function return.  CALL_OP is the jump target.  Add a REP
c616bf
@@ -29018,9 +29023,11 @@ ix86_output_function_return (bool long_p)
c616bf
     }
c616bf
 
c616bf
   if (!long_p || ix86_bnd_prefixed_insn_p (current_output_insn))
c616bf
-    return "%!ret";
c616bf
+    output_asm_insn ("%!ret", NULL);
c616bf
+  else
c616bf
+    output_asm_insn ("rep%; ret", NULL);
c616bf
 
c616bf
-  return "rep%; ret";
c616bf
+  return (ix86_harden_sls & harden_sls_return) ? "int3" : "";
c616bf
 }
c616bf
 
c616bf
 /* Output indirect function return.  RET_OP is the function return
c616bf
@@ -29158,7 +29165,12 @@ ix86_output_call_insn (rtx_insn *insn, rtx call_op)
c616bf
       if (output_indirect_p && !direct_p)
c616bf
 	ix86_output_indirect_branch (call_op, xasm, true);
c616bf
       else
c616bf
-	output_asm_insn (xasm, &call_op);
c616bf
+	{
c616bf
+	  output_asm_insn (xasm, &call_op);
c616bf
+	  if (!direct_p
c616bf
+	      && (ix86_harden_sls & harden_sls_indirect_branch))
c616bf
+	    return "int3";
c616bf
+	}
c616bf
       return "";
c616bf
     }
c616bf
 
c616bf
diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt
c616bf
index d9bd909a885..3ae48609e25 100644
c616bf
--- a/gcc/config/i386/i386.opt
c616bf
+++ b/gcc/config/i386/i386.opt
c616bf
@@ -1055,3 +1055,23 @@ Support MOVDIRI built-in functions and code generation.
c616bf
 mmovdir64b
c616bf
 Target Report Mask(ISA_MOVDIR64B) Var(ix86_isa_flags2) Save
c616bf
 Support MOVDIR64B built-in functions and code generation.
c616bf
+
c616bf
+mharden-sls=
c616bf
+Target RejectNegative Joined Enum(harden_sls) Var(ix86_harden_sls) Init(harden_sls_none)
c616bf
+Generate code to mitigate against straight line speculation.
c616bf
+
c616bf
+Enum
c616bf
+Name(harden_sls) Type(enum harden_sls)
c616bf
+Known choices for mitigation against straight line speculation with -mharden-sls=:
c616bf
+
c616bf
+EnumValue
c616bf
+Enum(harden_sls) String(none) Value(harden_sls_none)
c616bf
+
c616bf
+EnumValue
c616bf
+Enum(harden_sls) String(return) Value(harden_sls_return)
c616bf
+
c616bf
+EnumValue
c616bf
+Enum(harden_sls) String(indirect-branch) Value(harden_sls_indirect_branch)
c616bf
+
c616bf
+EnumValue
c616bf
+Enum(harden_sls) String(all) Value(harden_sls_all)
c616bf
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
c616bf
index 78ca7738df2..1e20efd6969 100644
c616bf
--- a/gcc/doc/invoke.texi
c616bf
+++ b/gcc/doc/invoke.texi
c616bf
@@ -1284,7 +1284,7 @@ See RS/6000 and PowerPC Options.
c616bf
 -mstack-protector-guard-symbol=@var{symbol} -mmitigate-rop @gol
c616bf
 -mgeneral-regs-only -mcall-ms2sysv-xlogues @gol
c616bf
 -mindirect-branch=@var{choice} -mfunction-return=@var{choice} @gol
c616bf
--mindirect-branch-register}
c616bf
+-mindirect-branch-register -mharden-sls=@var{choice}}
c616bf
 
c616bf
 @emph{x86 Windows Options}
c616bf
 @gccoptlist{-mconsole  -mcygwin  -mno-cygwin  -mdll @gol
c616bf
@@ -28036,6 +28036,14 @@ not be reachable in the large code model.
c616bf
 @opindex -mindirect-branch-register
c616bf
 Force indirect call and jump via register.
c616bf
 
c616bf
+@item -mharden-sls=@var{choice}
c616bf
+@opindex mharden-sls
c616bf
+Generate code to mitigate against straight line speculation (SLS) with
c616bf
+@var{choice}.  The default is @samp{none} which disables all SLS
c616bf
+hardening.  @samp{return} enables SLS hardening for function return.
c616bf
+@samp{indirect-branch} enables SLS hardening for indirect branch.
c616bf
+@samp{all} enables all SLS hardening.
c616bf
+
c616bf
 @end table
c616bf
 
c616bf
 These @samp{-m} switches are supported in addition to the above
c616bf
diff --git a/gcc/testsuite/gcc.target/i386/harden-sls-1.c b/gcc/testsuite/gcc.target/i386/harden-sls-1.c
c616bf
new file mode 100644
c616bf
index 00000000000..6f70dc94a23
c616bf
--- /dev/null
c616bf
+++ b/gcc/testsuite/gcc.target/i386/harden-sls-1.c
c616bf
@@ -0,0 +1,14 @@
c616bf
+/* { dg-do compile } */
c616bf
+/* { dg-options "-O2 -mindirect-branch=thunk-extern -mharden-sls=all" } */
c616bf
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
c616bf
+
c616bf
+extern void foo (void);
c616bf
+
c616bf
+void
c616bf
+bar (void)
c616bf
+{
c616bf
+  foo ();
c616bf
+}
c616bf
+
c616bf
+/* { dg-final { scan-assembler "jmp\[ \t\]+_?foo" } } */
c616bf
+/* { dg-final { scan-assembler-not {int3} } } */
c616bf
diff --git a/gcc/testsuite/gcc.target/i386/harden-sls-2.c b/gcc/testsuite/gcc.target/i386/harden-sls-2.c
c616bf
new file mode 100644
c616bf
index 00000000000..a7c59078d03
c616bf
--- /dev/null
c616bf
+++ b/gcc/testsuite/gcc.target/i386/harden-sls-2.c
c616bf
@@ -0,0 +1,14 @@
c616bf
+/* { dg-do compile } */
c616bf
+/* { dg-options "-O2 -mindirect-branch=thunk-extern -mharden-sls=all" } */
c616bf
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
c616bf
+
c616bf
+extern void (*fptr) (void);
c616bf
+
c616bf
+void
c616bf
+foo (void)
c616bf
+{
c616bf
+  fptr ();
c616bf
+}
c616bf
+
c616bf
+/* { dg-final { scan-assembler "jmp\[ \t\]+_?__x86_indirect_thunk_(r|e)ax" } } */
c616bf
+/* { dg-final { scan-assembler-times "int3" 1 } } */
c616bf
diff --git a/gcc/testsuite/gcc.target/i386/harden-sls-3.c b/gcc/testsuite/gcc.target/i386/harden-sls-3.c
c616bf
new file mode 100644
c616bf
index 00000000000..1a6056b6d7b
c616bf
--- /dev/null
c616bf
+++ b/gcc/testsuite/gcc.target/i386/harden-sls-3.c
c616bf
@@ -0,0 +1,14 @@
c616bf
+/* { dg-do compile } */
c616bf
+/* { dg-options "-O2 -mindirect-branch=thunk -mharden-sls=all" } */
c616bf
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
c616bf
+
c616bf
+extern void (*fptr) (void);
c616bf
+
c616bf
+void
c616bf
+foo (void)
c616bf
+{
c616bf
+  fptr ();
c616bf
+}
c616bf
+
c616bf
+/* { dg-final { scan-assembler "jmp\[ \t\]+_?__x86_indirect_thunk_(r|e)ax" } } */
c616bf
+/* { dg-final { scan-assembler-times "int3" 2 } } */
c616bf
diff --git a/gcc/testsuite/gcc.target/i386/harden-sls-4.c b/gcc/testsuite/gcc.target/i386/harden-sls-4.c
c616bf
new file mode 100644
c616bf
index 00000000000..f70dd1379d3
c616bf
--- /dev/null
c616bf
+++ b/gcc/testsuite/gcc.target/i386/harden-sls-4.c
c616bf
@@ -0,0 +1,16 @@
c616bf
+/* { dg-do compile } */
c616bf
+/* { dg-options "-O2 -mindirect-branch=keep -mharden-sls=all" } */
c616bf
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
c616bf
+
c616bf
+extern void (*fptr) (void);
c616bf
+
c616bf
+void
c616bf
+foo (void)
c616bf
+{
c616bf
+  fptr ();
c616bf
+}
c616bf
+
c616bf
+/* { dg-final { scan-assembler "jmp\[ \t\]+\\*_?fptr" { target { ! x32 } } } } */
c616bf
+/* { dg-final { scan-assembler "movl\[ \t\]+fptr\\(%rip\\), %eax" { target x32 } } } */
c616bf
+/* { dg-final { scan-assembler "jmp\[ \t\]+\\*%rax" { target x32 } } } */
c616bf
+/* { dg-final { scan-assembler-times "int3" 1 } } */
c616bf
diff --git a/gcc/testsuite/gcc.target/i386/harden-sls-5.c b/gcc/testsuite/gcc.target/i386/harden-sls-5.c
c616bf
new file mode 100644
c616bf
index 00000000000..613c44c6f82
c616bf
--- /dev/null
c616bf
+++ b/gcc/testsuite/gcc.target/i386/harden-sls-5.c
c616bf
@@ -0,0 +1,17 @@
c616bf
+/* { dg-do compile } */
c616bf
+/* { dg-options "-O2 -mno-indirect-branch-register -mfunction-return=keep -mindirect-branch=thunk-extern -mharden-sls=return" } */
c616bf
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
c616bf
+
c616bf
+typedef void (*dispatch_t)(long offset);
c616bf
+
c616bf
+dispatch_t dispatch;
c616bf
+
c616bf
+int
c616bf
+male_indirect_jump (long offset)
c616bf
+{
c616bf
+  dispatch(offset);
c616bf
+  return 0;
c616bf
+}
c616bf
+
c616bf
+/* { dg-final { scan-assembler-times "ret" 1 } } */
c616bf
+/* { dg-final { scan-assembler-times "int3" 1 } } */
c616bf
-- 
c616bf
2.36.1
c616bf