Debian Codegraph @

m4/hardening.m4

raw
1 AC_DEFUN([SUDO_CHECK_HARDENING], [
2 if test "$enable_hardening" != "no"; then
3 #
4 # Attempt to use _FORTIFY_SOURCE with sprintf. If the headers support
5 # it but libc does not, __sprintf_chk should be an undefined symbol.
6 #
7 O_CPPFLAGS="$CPPFLAGS"
8 AX_APPEND_FLAG([-D_FORTIFY_SOURCE=2], [CPPFLAGS])
9 AC_CACHE_CHECK([whether _FORTIFY_SOURCE may be specified],
10 [sudo_cv_use_fortify_source],
11 [AC_LINK_IFELSE([
12 AC_LANG_PROGRAM(
13 [[#include <stdio.h>]],
14 [[char buf[4]; sprintf(buf, "%s", "foo"); return buf[0];]]
15 )],
16 [sudo_cv_use_fortify_source=yes],
17 [sudo_cv_use_fortify_source=no]
18 )
19 ]
20 )
21 if test "$sudo_cv_use_fortify_source" != yes; then
22 CPPFLAGS="$O_CPPFLAGS"
23 fi
24
25 dnl
26 dnl The following tests rely on AC_LANG_WERROR.
27 dnl
28 if test -n "$GCC" -a "$enable_ssp" != "no"; then
29 AC_CACHE_CHECK([for compiler stack protector support],
30 [sudo_cv_var_stack_protector],
31 [
32 # Avoid CFLAGS since the compiler might optimize away our
33 # test. We don't want CPPFLAGS or LIBS to interfere with
34 # the test but keep LDFLAGS as it may have an rpath needed
35 # to find the ssp lib.
36 _CPPFLAGS="$CPPFLAGS"
37 _CFLAGS="$CFLAGS"
38 _LDFLAGS="$LDFLAGS"
39 _LIBS="$LIBS"
40 CPPFLAGS=
41 LIBS=
42
43 sudo_cv_var_stack_protector="-fstack-protector-strong"
44 CFLAGS="$sudo_cv_var_stack_protector"
45 LDFLAGS="$_LDFLAGS $sudo_cv_var_stack_protector"
46 AC_LINK_IFELSE([
47 AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
48 [[char buf[1024]; buf[1023] = '\0';]])
49 ], [], [
50 sudo_cv_var_stack_protector="-fstack-protector-all"
51 CFLAGS="$sudo_cv_var_stack_protector"
52 LDFLAGS="$_LDFLAGS $sudo_cv_var_stack_protector"
53 AC_LINK_IFELSE([
54 AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
55 [[char buf[1024]; buf[1023] = '\0';]])
56 ], [], [
57 sudo_cv_var_stack_protector="-fstack-protector"
58 CFLAGS="$sudo_cv_var_stack_protector"
59 LDFLAGS="$_LDFLAGS $sudo_cv_var_stack_protector"
60 AC_LINK_IFELSE([
61 AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
62 [[char buf[1024]; buf[1023] = '\0';]])
63 ], [], [
64 sudo_cv_var_stack_protector=no
65 ])
66 ])
67 ])
68 CPPFLAGS="$_CPPFLAGS"
69 CFLAGS="$_CFLAGS"
70 LDFLAGS="$_LDFLAGS"
71 LIBS="$_LIBS"
72 ]
73 )
74 if test X"$sudo_cv_var_stack_protector" != X"no"; then
75 HARDENING_CFLAGS="$sudo_cv_var_stack_protector"
76 HARDENING_LDFLAGS="-Wc,$sudo_cv_var_stack_protector"
77 fi
78 fi
79
80 # The gcc front-end may accept -fstack-clash-protection even if the
81 # machine-specific code does not support it. We use a test program
82 # with a large stack allocation to try to cause the compiler to
83 # insert the stack clash protection code, or fail if not supported.
84 if test -n "$GCC"; then
85 AC_CACHE_CHECK([whether C compiler supports -fstack-clash-protection],
86 [sudo_cv_check_cflags___fstack_clash_protection],
87 [
88 _CFLAGS="$CFLAGS"
89 CFLAGS="$CFLAGS -fstack-clash-protection"
90 AC_COMPILE_IFELSE([
91 AC_LANG_SOURCE([[int main(int argc, char *argv[]) { char buf[16384], *src = argv[0], *dst = buf; while ((*dst++ = *src++) != '\0') { continue; } return buf[argc]; }]])
92 ], [sudo_cv_check_cflags___fstack_clash_protection=yes], [sudo_cv_check_cflags___fstack_clash_protection=no])
93 CFLAGS="$_CFLAGS"
94 ]
95 )
96 if test X"$sudo_cv_check_cflags___fstack_clash_protection" = X"yes"; then
97 AX_CHECK_LINK_FLAG([-fstack-clash-protection], [
98 AX_APPEND_FLAG([-fstack-clash-protection], [HARDENING_CFLAGS])
99 AX_APPEND_FLAG([-Wc,-fstack-clash-protection], [HARDENING_LDFLAGS])
100 ])
101 fi
102
103 # Check for control-flow transfer instrumentation (Intel CET).
104 AX_CHECK_COMPILE_FLAG([-fcf-protection], [
105 AX_CHECK_LINK_FLAG([-fcf-protection], [
106 AX_APPEND_FLAG([-fcf-protection], [HARDENING_CFLAGS])
107 AX_APPEND_FLAG([-Wc,-fcf-protection], [HARDENING_LDFLAGS])
108 ])
109 ])
110 fi
111
112 # Linker-specific hardening flags.
113 if test X"$with_gnu_ld" = X"yes"; then
114 # GNU ld, and similar (gold, lld, etc).
115 AX_CHECK_LINK_FLAG([-Wl,-z,relro], [AX_APPEND_FLAG([-Wl,-z,relro], [HARDENING_LDFLAGS])])
116 AX_CHECK_LINK_FLAG([-Wl,-z,now], [AX_APPEND_FLAG([-Wl,-z,now], [HARDENING_LDFLAGS])])
117 AX_CHECK_LINK_FLAG([-Wl,-z,noexecstack], [AX_APPEND_FLAG([-Wl,-z,noexecstack], [HARDENING_LDFLAGS])])
118 fi
119 fi])