1*4882a593SmuzhiyunFrom 133d73079c5771bbf3d8311281b6772846357ec1 Mon Sep 17 00:00:00 2001
2*4882a593SmuzhiyunFrom: Chris Coulson <chris.coulson@canonical.com>
3*4882a593SmuzhiyunDate: Tue, 1 Dec 2020 23:03:39 +0000
4*4882a593SmuzhiyunSubject: [PATCH] kern/efi: Add initial stack protector implementation
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunIt works only on UEFI platforms but can be quite easily extended to
7*4882a593Smuzhiyunothers architectures and platforms if needed.
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunSigned-off-by: Chris Coulson <chris.coulson@canonical.com>
10*4882a593SmuzhiyunSigned-off-by: Daniel Kiper <daniel.kiper@oracle.com>
11*4882a593SmuzhiyunReviewed-by: Marco A Benatto <mbenatto@redhat.com>
12*4882a593SmuzhiyunReviewed-by: Javier Martinez Canillas <javierm@redhat.com>
13*4882a593SmuzhiyunSigned-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
14*4882a593Smuzhiyun---
15*4882a593Smuzhiyun acinclude.m4                   | 38 ++++++++++++++++-
16*4882a593Smuzhiyun configure                      | 97 +++++++++++++++++++++++++++++++++++++++---
17*4882a593Smuzhiyun configure.ac                   | 44 ++++++++++++++++---
18*4882a593Smuzhiyun grub-core/Makefile.am          |  1 +
19*4882a593Smuzhiyun grub-core/Makefile.in          |  1 +
20*4882a593Smuzhiyun grub-core/kern/efi/init.c      | 54 +++++++++++++++++++++++
21*4882a593Smuzhiyun include/grub/efi/api.h         | 19 +++++++++
22*4882a593Smuzhiyun include/grub/stack_protector.h | 30 +++++++++++++
23*4882a593Smuzhiyun po/POTFILES.in                 |  1 +
24*4882a593Smuzhiyun 9 files changed, 272 insertions(+), 13 deletions(-)
25*4882a593Smuzhiyun create mode 100644 include/grub/stack_protector.h
26*4882a593Smuzhiyun
27*4882a593Smuzhiyundiff --git a/acinclude.m4 b/acinclude.m4
28*4882a593Smuzhiyunindex 78cdf6e..6e14bb5 100644
29*4882a593Smuzhiyun--- a/acinclude.m4
30*4882a593Smuzhiyun+++ b/acinclude.m4
31*4882a593Smuzhiyun@@ -305,9 +305,9 @@ fi
32*4882a593Smuzhiyun ])
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun-dnl Check if the C compiler supports `-fstack-protector'.
36*4882a593Smuzhiyun+dnl Check if the C compiler supports the stack protector
37*4882a593Smuzhiyun AC_DEFUN([grub_CHECK_STACK_PROTECTOR],[
38*4882a593Smuzhiyun-[# Smashing stack protector.
39*4882a593Smuzhiyun+[# Stack smashing protector.
40*4882a593Smuzhiyun ssp_possible=yes]
41*4882a593Smuzhiyun AC_MSG_CHECKING([whether `$CC' accepts `-fstack-protector'])
42*4882a593Smuzhiyun # Is this a reliable test case?
43*4882a593Smuzhiyun@@ -324,6 +324,40 @@ else
44*4882a593Smuzhiyun   ssp_possible=no]
45*4882a593Smuzhiyun   AC_MSG_RESULT([no])
46*4882a593Smuzhiyun [fi]
47*4882a593Smuzhiyun+[# Strong stack smashing protector.
48*4882a593Smuzhiyun+ssp_strong_possible=yes]
49*4882a593Smuzhiyun+AC_MSG_CHECKING([whether `$CC' accepts `-fstack-protector-strong'])
50*4882a593Smuzhiyun+# Is this a reliable test case?
51*4882a593Smuzhiyun+AC_LANG_CONFTEST([AC_LANG_SOURCE([[
52*4882a593Smuzhiyun+void foo (void) { volatile char a[8]; a[3]; }
53*4882a593Smuzhiyun+]])])
54*4882a593Smuzhiyun+[# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
55*4882a593Smuzhiyun+# `ac_compile' like this correct, after all?
56*4882a593Smuzhiyun+if eval "$ac_compile -S -fstack-protector-strong -o conftest.s" 2> /dev/null; then]
57*4882a593Smuzhiyun+  AC_MSG_RESULT([yes])
58*4882a593Smuzhiyun+  [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
59*4882a593Smuzhiyun+  rm -f conftest.s
60*4882a593Smuzhiyun+else
61*4882a593Smuzhiyun+  ssp_strong_possible=no]
62*4882a593Smuzhiyun+  AC_MSG_RESULT([no])
63*4882a593Smuzhiyun+[fi]
64*4882a593Smuzhiyun+[# Global stack smashing protector.
65*4882a593Smuzhiyun+ssp_global_possible=yes]
66*4882a593Smuzhiyun+AC_MSG_CHECKING([whether `$CC' accepts `-mstack-protector-guard=global'])
67*4882a593Smuzhiyun+# Is this a reliable test case?
68*4882a593Smuzhiyun+AC_LANG_CONFTEST([AC_LANG_SOURCE([[
69*4882a593Smuzhiyun+void foo (void) { volatile char a[8]; a[3]; }
70*4882a593Smuzhiyun+]])])
71*4882a593Smuzhiyun+[# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
72*4882a593Smuzhiyun+# `ac_compile' like this correct, after all?
73*4882a593Smuzhiyun+if eval "$ac_compile -S -fstack-protector -mstack-protector-guard=global -o conftest.s" 2> /dev/null; then]
74*4882a593Smuzhiyun+  AC_MSG_RESULT([yes])
75*4882a593Smuzhiyun+  [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
76*4882a593Smuzhiyun+  rm -f conftest.s
77*4882a593Smuzhiyun+else
78*4882a593Smuzhiyun+  ssp_global_possible=no]
79*4882a593Smuzhiyun+  AC_MSG_RESULT([no])
80*4882a593Smuzhiyun+[fi]
81*4882a593Smuzhiyun ])
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun dnl Check if the C compiler supports `-mstack-arg-probe' (Cygwin).
84*4882a593Smuzhiyundiff --git a/configure b/configure
85*4882a593Smuzhiyunindex 9290ae8..973f702 100755
86*4882a593Smuzhiyun--- a/configure
87*4882a593Smuzhiyun+++ b/configure
88*4882a593Smuzhiyun@@ -1778,6 +1778,7 @@ with_libintl_prefix
89*4882a593Smuzhiyun with_libpth_prefix
90*4882a593Smuzhiyun with_included_regex
91*4882a593Smuzhiyun enable_efiemu
92*4882a593Smuzhiyun+enable_stack_protector
93*4882a593Smuzhiyun enable_mm_debug
94*4882a593Smuzhiyun enable_cache_stats
95*4882a593Smuzhiyun enable_boot_time
96*4882a593Smuzhiyun@@ -2459,6 +2460,8 @@ Optional Features:
97*4882a593Smuzhiyun   --disable-rpath         do not hardcode runtime library paths
98*4882a593Smuzhiyun   --enable-efiemu         build and install the efiemu runtimes
99*4882a593Smuzhiyun                           (default=guessed)
100*4882a593Smuzhiyun+  --enable-stack-protector
101*4882a593Smuzhiyun+                          enable the stack protector
102*4882a593Smuzhiyun   --enable-mm-debug       include memory manager debugging
103*4882a593Smuzhiyun   --enable-cache-stats    enable disk cache statistics collection
104*4882a593Smuzhiyun   --enable-boot-time      enable boot time statistics collection
105*4882a593Smuzhiyun@@ -32348,9 +32351,9 @@ fi
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun CFLAGS="$TARGET_CFLAGS"
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun-# Smashing stack protector.
110*4882a593Smuzhiyun+# Stack smashing protector.
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun-# Smashing stack protector.
113*4882a593Smuzhiyun+# Stack smashing protector.
114*4882a593Smuzhiyun ssp_possible=yes
115*4882a593Smuzhiyun { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether \`$CC' accepts \`-fstack-protector'" >&5
116*4882a593Smuzhiyun $as_echo_n "checking whether \`$CC' accepts \`-fstack-protector'... " >&6; }
117*4882a593Smuzhiyun@@ -32373,11 +32376,88 @@ else
118*4882a593Smuzhiyun   { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
119*4882a593Smuzhiyun $as_echo "no" >&6; }
120*4882a593Smuzhiyun fi
121*4882a593Smuzhiyun+# Strong stack smashing protector.
122*4882a593Smuzhiyun+ssp_strong_possible=yes
123*4882a593Smuzhiyun+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether \`$CC' accepts \`-fstack-protector-strong'" >&5
124*4882a593Smuzhiyun+$as_echo_n "checking whether \`$CC' accepts \`-fstack-protector-strong'... " >&6; }
125*4882a593Smuzhiyun+# Is this a reliable test case?
126*4882a593Smuzhiyun+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
127*4882a593Smuzhiyun+/* end confdefs.h.  */
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun-# Need that, because some distributions ship compilers that include
130*4882a593Smuzhiyun-# `-fstack-protector' in the default specs.
131*4882a593Smuzhiyun-if test "x$ssp_possible" = xyes; then
132*4882a593Smuzhiyun-  TARGET_CFLAGS="$TARGET_CFLAGS -fno-stack-protector"
133*4882a593Smuzhiyun+void foo (void) { volatile char a[8]; a[3]; }
134*4882a593Smuzhiyun+
135*4882a593Smuzhiyun+_ACEOF
136*4882a593Smuzhiyun+# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
137*4882a593Smuzhiyun+# `ac_compile' like this correct, after all?
138*4882a593Smuzhiyun+if eval "$ac_compile -S -fstack-protector-strong -o conftest.s" 2> /dev/null; then
139*4882a593Smuzhiyun+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
140*4882a593Smuzhiyun+$as_echo "yes" >&6; }
141*4882a593Smuzhiyun+  # Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
142*4882a593Smuzhiyun+  rm -f conftest.s
143*4882a593Smuzhiyun+else
144*4882a593Smuzhiyun+  ssp_strong_possible=no
145*4882a593Smuzhiyun+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
146*4882a593Smuzhiyun+$as_echo "no" >&6; }
147*4882a593Smuzhiyun+fi
148*4882a593Smuzhiyun+# Global stack smashing protector.
149*4882a593Smuzhiyun+ssp_global_possible=yes
150*4882a593Smuzhiyun+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether \`$CC' accepts \`-mstack-protector-guard=global'" >&5
151*4882a593Smuzhiyun+$as_echo_n "checking whether \`$CC' accepts \`-mstack-protector-guard=global'... " >&6; }
152*4882a593Smuzhiyun+# Is this a reliable test case?
153*4882a593Smuzhiyun+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
154*4882a593Smuzhiyun+/* end confdefs.h.  */
155*4882a593Smuzhiyun+
156*4882a593Smuzhiyun+void foo (void) { volatile char a[8]; a[3]; }
157*4882a593Smuzhiyun+
158*4882a593Smuzhiyun+_ACEOF
159*4882a593Smuzhiyun+# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
160*4882a593Smuzhiyun+# `ac_compile' like this correct, after all?
161*4882a593Smuzhiyun+if eval "$ac_compile -S -fstack-protector -mstack-protector-guard=global -o conftest.s" 2> /dev/null; then
162*4882a593Smuzhiyun+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
163*4882a593Smuzhiyun+$as_echo "yes" >&6; }
164*4882a593Smuzhiyun+  # Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
165*4882a593Smuzhiyun+  rm -f conftest.s
166*4882a593Smuzhiyun+else
167*4882a593Smuzhiyun+  ssp_global_possible=no
168*4882a593Smuzhiyun+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
169*4882a593Smuzhiyun+$as_echo "no" >&6; }
170*4882a593Smuzhiyun+fi
171*4882a593Smuzhiyun+
172*4882a593Smuzhiyun+# Check whether --enable-stack-protector was given.
173*4882a593Smuzhiyun+if test "${enable_stack_protector+set}" = set; then :
174*4882a593Smuzhiyun+  enableval=$enable_stack_protector;
175*4882a593Smuzhiyun+else
176*4882a593Smuzhiyun+  enable_stack_protector=no
177*4882a593Smuzhiyun+fi
178*4882a593Smuzhiyun+
179*4882a593Smuzhiyun+if test "x$enable_stack_protector" = xno; then
180*4882a593Smuzhiyun+  if test "x$ssp_possible" = xyes; then
181*4882a593Smuzhiyun+    # Need that, because some distributions ship compilers that include
182*4882a593Smuzhiyun+    # `-fstack-protector' in the default specs.
183*4882a593Smuzhiyun+    TARGET_CFLAGS="$TARGET_CFLAGS -fno-stack-protector"
184*4882a593Smuzhiyun+  fi
185*4882a593Smuzhiyun+elif test "x$platform" != xefi; then
186*4882a593Smuzhiyun+  as_fn_error $? "--enable-stack-protector is only supported on EFI platforms" "$LINENO" 5
187*4882a593Smuzhiyun+elif test "x$ssp_global_possible" != xyes; then
188*4882a593Smuzhiyun+  as_fn_error $? "--enable-stack-protector is not supported (compiler doesn't support -mstack-protector-guard=global)" "$LINENO" 5
189*4882a593Smuzhiyun+else
190*4882a593Smuzhiyun+  TARGET_CFLAGS="$TARGET_CFLAGS -mstack-protector-guard=global"
191*4882a593Smuzhiyun+  if test "x$enable_stack_protector" = xyes; then
192*4882a593Smuzhiyun+    if test "x$ssp_possible" != xyes; then
193*4882a593Smuzhiyun+      as_fn_error $? "--enable-stack-protector is not supported (compiler doesn't support -fstack-protector)" "$LINENO" 5
194*4882a593Smuzhiyun+    fi
195*4882a593Smuzhiyun+    TARGET_CFLAGS="$TARGET_CFLAGS -fstack-protector"
196*4882a593Smuzhiyun+  elif test "x$enable_stack_protector" = xstrong; then
197*4882a593Smuzhiyun+    if test "x$ssp_strong_possible" != xyes; then
198*4882a593Smuzhiyun+      as_fn_error $? "--enable-stack-protector=strong is not supported (compiler doesn't support -fstack-protector-strong)" "$LINENO" 5
199*4882a593Smuzhiyun+    fi
200*4882a593Smuzhiyun+    TARGET_CFLAGS="$TARGET_CFLAGS -fstack-protector-strong"
201*4882a593Smuzhiyun+  else
202*4882a593Smuzhiyun+    # Note, -fstack-protector-all requires that the protector is disabled for
203*4882a593Smuzhiyun+    # functions that appear in the call stack when the canary is initialized.
204*4882a593Smuzhiyun+    as_fn_error $? "invalid value $enable_stack_protector for --enable-stack-protector" "$LINENO" 5
205*4882a593Smuzhiyun+  fi
206*4882a593Smuzhiyun+  TARGET_CPPFLAGS="$TARGET_CPPFLAGS -DGRUB_STACK_PROTECTOR=1"
207*4882a593Smuzhiyun fi
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun CFLAGS="$TARGET_CFLAGS"
210*4882a593Smuzhiyun@@ -37054,5 +37134,10 @@ echo "Without liblzma (no support for XZ-compressed mips images) ($liblzma_excus
211*4882a593Smuzhiyun else
212*4882a593Smuzhiyun echo "With liblzma from $LIBLZMA (support for XZ-compressed mips images)"
213*4882a593Smuzhiyun fi
214*4882a593Smuzhiyun+if test "x$enable_stack_protector" != xno; then
215*4882a593Smuzhiyun+echo "With stack smashing protector: Yes"
216*4882a593Smuzhiyun+else
217*4882a593Smuzhiyun+echo "With stack smashing protector: No"
218*4882a593Smuzhiyun+fi
219*4882a593Smuzhiyun echo "*******************************************************"
220*4882a593Smuzhiyun
221*4882a593Smuzhiyundiff --git a/configure.ac b/configure.ac
222*4882a593Smuzhiyunindex 7656f24..bb6b02a 100644
223*4882a593Smuzhiyun--- a/configure.ac
224*4882a593Smuzhiyun+++ b/configure.ac
225*4882a593Smuzhiyun@@ -1285,12 +1285,41 @@ fi]
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun CFLAGS="$TARGET_CFLAGS"
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun-# Smashing stack protector.
230*4882a593Smuzhiyun+# Stack smashing protector.
231*4882a593Smuzhiyun grub_CHECK_STACK_PROTECTOR
232*4882a593Smuzhiyun-# Need that, because some distributions ship compilers that include
233*4882a593Smuzhiyun-# `-fstack-protector' in the default specs.
234*4882a593Smuzhiyun-if test "x$ssp_possible" = xyes; then
235*4882a593Smuzhiyun-  TARGET_CFLAGS="$TARGET_CFLAGS -fno-stack-protector"
236*4882a593Smuzhiyun+AC_ARG_ENABLE([stack-protector],
237*4882a593Smuzhiyun+	      AS_HELP_STRING([--enable-stack-protector],
238*4882a593Smuzhiyun+			     [enable the stack protector]),
239*4882a593Smuzhiyun+	      [],
240*4882a593Smuzhiyun+	      [enable_stack_protector=no])
241*4882a593Smuzhiyun+if test "x$enable_stack_protector" = xno; then
242*4882a593Smuzhiyun+  if test "x$ssp_possible" = xyes; then
243*4882a593Smuzhiyun+    # Need that, because some distributions ship compilers that include
244*4882a593Smuzhiyun+    # `-fstack-protector' in the default specs.
245*4882a593Smuzhiyun+    TARGET_CFLAGS="$TARGET_CFLAGS -fno-stack-protector"
246*4882a593Smuzhiyun+  fi
247*4882a593Smuzhiyun+elif test "x$platform" != xefi; then
248*4882a593Smuzhiyun+  AC_MSG_ERROR([--enable-stack-protector is only supported on EFI platforms])
249*4882a593Smuzhiyun+elif test "x$ssp_global_possible" != xyes; then
250*4882a593Smuzhiyun+  AC_MSG_ERROR([--enable-stack-protector is not supported (compiler doesn't support -mstack-protector-guard=global)])
251*4882a593Smuzhiyun+else
252*4882a593Smuzhiyun+  TARGET_CFLAGS="$TARGET_CFLAGS -mstack-protector-guard=global"
253*4882a593Smuzhiyun+  if test "x$enable_stack_protector" = xyes; then
254*4882a593Smuzhiyun+    if test "x$ssp_possible" != xyes; then
255*4882a593Smuzhiyun+      AC_MSG_ERROR([--enable-stack-protector is not supported (compiler doesn't support -fstack-protector)])
256*4882a593Smuzhiyun+    fi
257*4882a593Smuzhiyun+    TARGET_CFLAGS="$TARGET_CFLAGS -fstack-protector"
258*4882a593Smuzhiyun+  elif test "x$enable_stack_protector" = xstrong; then
259*4882a593Smuzhiyun+    if test "x$ssp_strong_possible" != xyes; then
260*4882a593Smuzhiyun+      AC_MSG_ERROR([--enable-stack-protector=strong is not supported (compiler doesn't support -fstack-protector-strong)])
261*4882a593Smuzhiyun+    fi
262*4882a593Smuzhiyun+    TARGET_CFLAGS="$TARGET_CFLAGS -fstack-protector-strong"
263*4882a593Smuzhiyun+  else
264*4882a593Smuzhiyun+    # Note, -fstack-protector-all requires that the protector is disabled for
265*4882a593Smuzhiyun+    # functions that appear in the call stack when the canary is initialized.
266*4882a593Smuzhiyun+    AC_MSG_ERROR([invalid value $enable_stack_protector for --enable-stack-protector])
267*4882a593Smuzhiyun+  fi
268*4882a593Smuzhiyun+  TARGET_CPPFLAGS="$TARGET_CPPFLAGS -DGRUB_STACK_PROTECTOR=1"
269*4882a593Smuzhiyun fi
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun CFLAGS="$TARGET_CFLAGS"
272*4882a593Smuzhiyun@@ -2103,5 +2132,10 @@ echo "Without liblzma (no support for XZ-compressed mips images) ($liblzma_excus
273*4882a593Smuzhiyun else
274*4882a593Smuzhiyun echo "With liblzma from $LIBLZMA (support for XZ-compressed mips images)"
275*4882a593Smuzhiyun fi
276*4882a593Smuzhiyun+if test "x$enable_stack_protector" != xno; then
277*4882a593Smuzhiyun+echo "With stack smashing protector: Yes"
278*4882a593Smuzhiyun+else
279*4882a593Smuzhiyun+echo "With stack smashing protector: No"
280*4882a593Smuzhiyun+fi
281*4882a593Smuzhiyun echo "*******************************************************"
282*4882a593Smuzhiyun ]
283*4882a593Smuzhiyundiff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
284*4882a593Smuzhiyunindex 30e23ad..ee88e44 100644
285*4882a593Smuzhiyun--- a/grub-core/Makefile.am
286*4882a593Smuzhiyun+++ b/grub-core/Makefile.am
287*4882a593Smuzhiyun@@ -90,6 +90,7 @@ endif
288*4882a593Smuzhiyun KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h
289*4882a593Smuzhiyun KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h
290*4882a593Smuzhiyun KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h
291*4882a593Smuzhiyun+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/stack_protector.h
292*4882a593Smuzhiyun KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
293*4882a593Smuzhiyun KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
294*4882a593Smuzhiyun KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/verify.h
295*4882a593Smuzhiyundiff --git a/grub-core/Makefile.in b/grub-core/Makefile.in
296*4882a593Smuzhiyunindex 1f8133b..50c70b5 100644
297*4882a593Smuzhiyun--- a/grub-core/Makefile.in
298*4882a593Smuzhiyun+++ b/grub-core/Makefile.in
299*4882a593Smuzhiyun@@ -16425,6 +16425,7 @@ KERNEL_HEADER_FILES = $(top_srcdir)/include/grub/cache.h \
300*4882a593Smuzhiyun 	$(am__append_5795) $(top_srcdir)/include/grub/mm.h \
301*4882a593Smuzhiyun 	$(top_srcdir)/include/grub/parser.h \
302*4882a593Smuzhiyun 	$(top_srcdir)/include/grub/partition.h \
303*4882a593Smuzhiyun+	$(top_srcdir)/include/grub/stack_protector.h \
304*4882a593Smuzhiyun 	$(top_srcdir)/include/grub/term.h \
305*4882a593Smuzhiyun 	$(top_srcdir)/include/grub/time.h \
306*4882a593Smuzhiyun 	$(top_srcdir)/include/grub/verify.h \
307*4882a593Smuzhiyundiff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c
308*4882a593Smuzhiyunindex 1333465..7facacf 100644
309*4882a593Smuzhiyun--- a/grub-core/kern/efi/init.c
310*4882a593Smuzhiyun+++ b/grub-core/kern/efi/init.c
311*4882a593Smuzhiyun@@ -27,6 +27,58 @@
312*4882a593Smuzhiyun #include <grub/env.h>
313*4882a593Smuzhiyun #include <grub/mm.h>
314*4882a593Smuzhiyun #include <grub/kernel.h>
315*4882a593Smuzhiyun+#include <grub/stack_protector.h>
316*4882a593Smuzhiyun+
317*4882a593Smuzhiyun+#ifdef GRUB_STACK_PROTECTOR
318*4882a593Smuzhiyun+
319*4882a593Smuzhiyun+static grub_efi_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
320*4882a593Smuzhiyun+
321*4882a593Smuzhiyun+/*
322*4882a593Smuzhiyun+ * Don't put this on grub_efi_init()'s local stack to avoid it
323*4882a593Smuzhiyun+ * getting a stack check.
324*4882a593Smuzhiyun+ */
325*4882a593Smuzhiyun+static grub_efi_uint8_t stack_chk_guard_buf[32];
326*4882a593Smuzhiyun+
327*4882a593Smuzhiyun+grub_addr_t __stack_chk_guard;
328*4882a593Smuzhiyun+
329*4882a593Smuzhiyun+void __attribute__ ((noreturn))
330*4882a593Smuzhiyun+__stack_chk_fail (void)
331*4882a593Smuzhiyun+{
332*4882a593Smuzhiyun+  /*
333*4882a593Smuzhiyun+   * Assume it's not safe to call into EFI Boot Services. Sorry, that
334*4882a593Smuzhiyun+   * means no console message here.
335*4882a593Smuzhiyun+   */
336*4882a593Smuzhiyun+  do
337*4882a593Smuzhiyun+    {
338*4882a593Smuzhiyun+      /* Do not optimize out the loop. */
339*4882a593Smuzhiyun+      asm volatile ("");
340*4882a593Smuzhiyun+    }
341*4882a593Smuzhiyun+  while (1);
342*4882a593Smuzhiyun+}
343*4882a593Smuzhiyun+
344*4882a593Smuzhiyun+static void
345*4882a593Smuzhiyun+stack_protector_init (void)
346*4882a593Smuzhiyun+{
347*4882a593Smuzhiyun+  grub_efi_rng_protocol_t *rng;
348*4882a593Smuzhiyun+
349*4882a593Smuzhiyun+  /* Set up the stack canary. Make errors here non-fatal for now. */
350*4882a593Smuzhiyun+  rng = grub_efi_locate_protocol (&rng_protocol_guid, NULL);
351*4882a593Smuzhiyun+  if (rng != NULL)
352*4882a593Smuzhiyun+    {
353*4882a593Smuzhiyun+      grub_efi_status_t status;
354*4882a593Smuzhiyun+
355*4882a593Smuzhiyun+      status = efi_call_4 (rng->get_rng, rng, NULL, sizeof (stack_chk_guard_buf),
356*4882a593Smuzhiyun+			   stack_chk_guard_buf);
357*4882a593Smuzhiyun+      if (status == GRUB_EFI_SUCCESS)
358*4882a593Smuzhiyun+	grub_memcpy (&__stack_chk_guard, stack_chk_guard_buf, sizeof (__stack_chk_guard));
359*4882a593Smuzhiyun+    }
360*4882a593Smuzhiyun+}
361*4882a593Smuzhiyun+#else
362*4882a593Smuzhiyun+static void
363*4882a593Smuzhiyun+stack_protector_init (void)
364*4882a593Smuzhiyun+{
365*4882a593Smuzhiyun+}
366*4882a593Smuzhiyun+#endif
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun grub_addr_t grub_modbase;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun@@ -38,6 +90,8 @@ grub_efi_init (void)
371*4882a593Smuzhiyun      messages.  */
372*4882a593Smuzhiyun   grub_console_init ();
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun+  stack_protector_init ();
375*4882a593Smuzhiyun+
376*4882a593Smuzhiyun   /* Initialize the memory management system.  */
377*4882a593Smuzhiyun   grub_efi_mm_init ();
378*4882a593Smuzhiyun
379*4882a593Smuzhiyundiff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
380*4882a593Smuzhiyunindex 13e5715..5517f7e 100644
381*4882a593Smuzhiyun--- a/include/grub/efi/api.h
382*4882a593Smuzhiyun+++ b/include/grub/efi/api.h
383*4882a593Smuzhiyun@@ -339,6 +339,11 @@
384*4882a593Smuzhiyun     { 0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23 } \
385*4882a593Smuzhiyun   }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun+#define GRUB_EFI_RNG_PROTOCOL_GUID \
388*4882a593Smuzhiyun+  { 0x3152bca5, 0xeade, 0x433d, \
389*4882a593Smuzhiyun+    { 0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44 } \
390*4882a593Smuzhiyun+  }
391*4882a593Smuzhiyun+
392*4882a593Smuzhiyun struct grub_efi_sal_system_table
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun   grub_uint32_t signature;
395*4882a593Smuzhiyun@@ -1700,6 +1705,20 @@ struct grub_efi_shim_lock_protocol
396*4882a593Smuzhiyun };
397*4882a593Smuzhiyun typedef struct grub_efi_shim_lock_protocol grub_efi_shim_lock_protocol_t;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun+typedef grub_efi_guid_t grub_efi_rng_algorithm_t;
400*4882a593Smuzhiyun+
401*4882a593Smuzhiyun+struct grub_efi_rng_protocol
402*4882a593Smuzhiyun+{
403*4882a593Smuzhiyun+  grub_efi_status_t (*get_info) (struct grub_efi_rng_protocol *this,
404*4882a593Smuzhiyun+				 grub_efi_uintn_t *rng_algorithm_list_size,
405*4882a593Smuzhiyun+				 grub_efi_rng_algorithm_t *rng_algorithm_list);
406*4882a593Smuzhiyun+  grub_efi_status_t (*get_rng) (struct grub_efi_rng_protocol *this,
407*4882a593Smuzhiyun+				grub_efi_rng_algorithm_t *rng_algorithm,
408*4882a593Smuzhiyun+				grub_efi_uintn_t rng_value_length,
409*4882a593Smuzhiyun+				grub_efi_uint8_t *rng_value);
410*4882a593Smuzhiyun+};
411*4882a593Smuzhiyun+typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t;
412*4882a593Smuzhiyun+
413*4882a593Smuzhiyun #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
414*4882a593Smuzhiyun   || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) \
415*4882a593Smuzhiyun   || defined(__riscv)
416*4882a593Smuzhiyundiff --git a/include/grub/stack_protector.h b/include/grub/stack_protector.h
417*4882a593Smuzhiyunnew file mode 100644
418*4882a593Smuzhiyunindex 0000000..c88dc00
419*4882a593Smuzhiyun--- /dev/null
420*4882a593Smuzhiyun+++ b/include/grub/stack_protector.h
421*4882a593Smuzhiyun@@ -0,0 +1,30 @@
422*4882a593Smuzhiyun+/*
423*4882a593Smuzhiyun+ *  GRUB  --  GRand Unified Bootloader
424*4882a593Smuzhiyun+ *  Copyright (C) 2021  Free Software Foundation, Inc.
425*4882a593Smuzhiyun+ *
426*4882a593Smuzhiyun+ *  GRUB is free software: you can redistribute it and/or modify
427*4882a593Smuzhiyun+ *  it under the terms of the GNU General Public License as published by
428*4882a593Smuzhiyun+ *  the Free Software Foundation, either version 3 of the License, or
429*4882a593Smuzhiyun+ *  (at your option) any later version.
430*4882a593Smuzhiyun+ *
431*4882a593Smuzhiyun+ *  GRUB is distributed in the hope that it will be useful,
432*4882a593Smuzhiyun+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
433*4882a593Smuzhiyun+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
434*4882a593Smuzhiyun+ *  GNU General Public License for more details.
435*4882a593Smuzhiyun+ *
436*4882a593Smuzhiyun+ *  You should have received a copy of the GNU General Public License
437*4882a593Smuzhiyun+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
438*4882a593Smuzhiyun+ */
439*4882a593Smuzhiyun+
440*4882a593Smuzhiyun+#ifndef GRUB_STACK_PROTECTOR_H
441*4882a593Smuzhiyun+#define GRUB_STACK_PROTECTOR_H	1
442*4882a593Smuzhiyun+
443*4882a593Smuzhiyun+#include <grub/symbol.h>
444*4882a593Smuzhiyun+#include <grub/types.h>
445*4882a593Smuzhiyun+
446*4882a593Smuzhiyun+#ifdef GRUB_STACK_PROTECTOR
447*4882a593Smuzhiyun+extern grub_addr_t EXPORT_VAR (__stack_chk_guard);
448*4882a593Smuzhiyun+extern void __attribute__ ((noreturn)) EXPORT_FUNC (__stack_chk_fail) (void);
449*4882a593Smuzhiyun+#endif
450*4882a593Smuzhiyun+
451*4882a593Smuzhiyun+#endif /* GRUB_STACK_PROTECTOR_H */
452*4882a593Smuzhiyundiff --git a/po/POTFILES.in b/po/POTFILES.in
453*4882a593Smuzhiyunindex 7753ab4..ef42c7d 100644
454*4882a593Smuzhiyun--- a/po/POTFILES.in
455*4882a593Smuzhiyun+++ b/po/POTFILES.in
456*4882a593Smuzhiyun@@ -1319,6 +1319,7 @@
457*4882a593Smuzhiyun ./include/grub/sparc64/time.h
458*4882a593Smuzhiyun ./include/grub/sparc64/types.h
459*4882a593Smuzhiyun ./include/grub/speaker.h
460*4882a593Smuzhiyun+./include/grub/stack_protector.h
461*4882a593Smuzhiyun ./include/grub/symbol.h
462*4882a593Smuzhiyun ./include/grub/syslinux_parse.h
463*4882a593Smuzhiyun ./include/grub/term.h
464*4882a593Smuzhiyun--
465*4882a593Smuzhiyun2.14.2
466*4882a593Smuzhiyun
467