]> git.ipfire.org Git - people/stevee/ipfire-3.x.git/blame - gcc/patches/a25982ada523689c8745d7fb4b1b93c8f5dab2e7.patch
gcc: Harden this package
[people/stevee/ipfire-3.x.git] / gcc / patches / a25982ada523689c8745d7fb4b1b93c8f5dab2e7.patch
CommitLineData
48c0a841
MT
1From a25982ada523689c8745d7fb4b1b93c8f5dab2e7 Mon Sep 17 00:00:00 2001
2From: "H.J. Lu" <hjl.tools@gmail.com>
3Date: Thu, 14 Jul 2022 08:23:38 -0700
4Subject: [PATCH] stack-protector: Check stack canary before throwing exception
5
6Check stack canary before throwing exception to avoid stack corruption.
7
8gcc/
9
10 PR middle-end/58245
11 * calls.cc: Include "tree-eh.h".
12 (expand_call): Check stack canary before throwing exception.
13
14gcc/testsuite/
15
16 PR middle-end/58245
17 * g++.dg/fstack-protector-strong.C: Adjusted.
18 * g++.dg/pr58245-1.C: New test.
19---
20 gcc/calls.cc | 6 +++++-
21 gcc/testsuite/g++.dg/fstack-protector-strong.C | 2 +-
22 gcc/testsuite/g++.dg/pr58245-1.C | 10 ++++++++++
23 3 files changed, 16 insertions(+), 2 deletions(-)
24 create mode 100644 gcc/testsuite/g++.dg/pr58245-1.C
25
26diff --git a/gcc/calls.cc b/gcc/calls.cc
27index bc96aff38f0..6dd6f73e978 100644
28--- a/gcc/calls.cc
29+++ b/gcc/calls.cc
30@@ -60,6 +60,7 @@ along with GCC; see the file COPYING3. If not see
31 #include "attr-fnspec.h"
32 #include "value-query.h"
33 #include "tree-pretty-print.h"
34+#include "tree-eh.h"
35
36 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
37 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
38@@ -3154,7 +3155,10 @@ expand_call (tree exp, rtx target, int ignore)
39 if (pass && (flags & ECF_MALLOC))
40 start_sequence ();
41
42- if (pass == 0
43+ /* Check the canary value for sibcall or function which doesn't
44+ return and could throw. */
45+ if ((pass == 0
46+ || ((flags & ECF_NORETURN) != 0 && tree_could_throw_p (exp)))
47 && crtl->stack_protect_guard
48 && targetm.stack_protect_runtime_enabled_p ())
49 stack_protect_epilogue ();
50diff --git a/gcc/testsuite/g++.dg/fstack-protector-strong.C b/gcc/testsuite/g++.dg/fstack-protector-strong.C
51index ae6d2fdb8df..034af2ce9ab 100644
52--- a/gcc/testsuite/g++.dg/fstack-protector-strong.C
53+++ b/gcc/testsuite/g++.dg/fstack-protector-strong.C
54@@ -85,4 +85,4 @@ int foo7 (B *p)
55 return p->return_slot ().a1;
56 }
57
58-/* { dg-final { scan-assembler-times "stack_chk_fail" 7 } } */
59+/* { dg-final { scan-assembler-times "stack_chk_fail" 8 } } */
60diff --git a/gcc/testsuite/g++.dg/pr58245-1.C b/gcc/testsuite/g++.dg/pr58245-1.C
61new file mode 100644
62index 00000000000..1439bc62e71
63--- /dev/null
64+++ b/gcc/testsuite/g++.dg/pr58245-1.C
65@@ -0,0 +1,10 @@
66+/* { dg-do compile { target i?86-*-* x86_64-*-* rs6000-*-* s390x-*-* } } */
67+/* { dg-options "-O2 -fstack-protector-all" } */
68+
69+void
70+bar (void)
71+{
72+ throw 1;
73+}
74+
75+/* { dg-final { scan-assembler-times "stack_chk_fail" 1 } } */
76--
772.31.1
78