Merge pull request #309460 from r-ryantm/auto-update/home-manager
[NixPkgs.git] / pkgs / build-support / cc-wrapper / add-hardening.sh
blobef166e2f50c5e62cf7aa49e95600faa61bc5a38a
1 declare -a hardeningCFlagsAfter=()
2 declare -a hardeningCFlagsBefore=()
4 declare -A hardeningEnableMap=()
6 # Intentionally word-split in case 'NIX_HARDENING_ENABLE' is defined in Nix. The
7 # array expansion also prevents undefined variables from causing trouble with
8 # `set -u`.
9 for flag in ${NIX_HARDENING_ENABLE_@suffixSalt@-}; do
10 hardeningEnableMap["$flag"]=1
11 done
13 # fortify3 implies fortify enablement - make explicit before
14 # we filter unsupported flags because unsupporting fortify3
15 # doesn't mean we should unsupport fortify too
16 if [[ -n "${hardeningEnableMap[fortify3]-}" ]]; then
17 hardeningEnableMap["fortify"]=1
20 # Remove unsupported flags.
21 for flag in @hardening_unsupported_flags@; do
22 unset -v "hardeningEnableMap[$flag]"
23 # fortify being unsupported implies fortify3 is unsupported
24 if [[ "$flag" = 'fortify' ]] ; then
25 unset -v "hardeningEnableMap['fortify3']"
27 done
29 # now make fortify and fortify3 mutually exclusive
30 if [[ -n "${hardeningEnableMap[fortify3]-}" ]]; then
31 unset -v "hardeningEnableMap['fortify']"
34 if (( "${NIX_DEBUG:-0}" >= 1 )); then
35 declare -a allHardeningFlags=(fortify fortify3 stackprotector pie pic strictoverflow format trivialautovarinit zerocallusedregs)
36 declare -A hardeningDisableMap=()
38 # Determine which flags were effectively disabled so we can report below.
39 for flag in "${allHardeningFlags[@]}"; do
40 if [[ -z "${hardeningEnableMap[$flag]-}" ]]; then
41 hardeningDisableMap["$flag"]=1
43 done
45 printf 'HARDENING: disabled flags:' >&2
46 (( "${#hardeningDisableMap[@]}" )) && printf ' %q' "${!hardeningDisableMap[@]}" >&2
47 echo >&2
49 if (( "${#hardeningEnableMap[@]}" )); then
50 echo 'HARDENING: Is active (not completely disabled with "all" flag)' >&2;
54 for flag in "${!hardeningEnableMap[@]}"; do
55 case $flag in
56 fortify | fortify3)
57 # Use -U_FORTIFY_SOURCE to avoid warnings on toolchains that explicitly
58 # set -D_FORTIFY_SOURCE=0 (like 'clang -fsanitize=address').
59 hardeningCFlagsBefore+=('-O2' '-U_FORTIFY_SOURCE')
60 # Unset any _FORTIFY_SOURCE values the command-line may have set before
61 # enforcing our own value, avoiding (potentially fatal) redefinition
62 # warnings
63 hardeningCFlagsAfter+=('-U_FORTIFY_SOURCE')
64 case $flag in
65 fortify)
66 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling fortify >&2; fi
67 hardeningCFlagsAfter+=('-D_FORTIFY_SOURCE=2')
69 fortify3)
70 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling fortify3 >&2; fi
71 hardeningCFlagsAfter+=('-D_FORTIFY_SOURCE=3')
74 # Ignore unsupported.
76 esac
78 stackprotector)
79 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling stackprotector >&2; fi
80 hardeningCFlagsBefore+=('-fstack-protector-strong' '--param' 'ssp-buffer-size=4')
82 pie)
83 # NB: we do not use `+=` here, because PIE flags must occur before any PIC flags
84 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling CFlags -fPIE >&2; fi
85 hardeningCFlagsBefore=('-fPIE' "${hardeningCFlagsBefore[@]}")
86 if [[ ! (" ${params[*]} " =~ " -shared " || " ${params[*]} " =~ " -static ") ]]; then
87 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling LDFlags -pie >&2; fi
88 hardeningCFlagsBefore=('-pie' "${hardeningCFlagsBefore[@]}")
91 pic)
92 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling pic >&2; fi
93 hardeningCFlagsBefore+=('-fPIC')
95 strictoverflow)
96 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling strictoverflow >&2; fi
97 if (( @isClang@ )); then
98 # In Clang, -fno-strict-overflow only serves to set -fwrapv and is
99 # reported as an unused CLI argument if -fwrapv or -fno-wrapv is set
100 # explicitly, so we side step that by doing the conversion here.
102 # See: https://github.com/llvm/llvm-project/blob/llvmorg-16.0.6/clang/lib/Driver/ToolChains/Clang.cpp#L6315
104 hardeningCFlagsBefore+=('-fwrapv')
105 else
106 hardeningCFlagsBefore+=('-fno-strict-overflow')
109 trivialautovarinit)
110 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling trivialautovarinit >&2; fi
111 hardeningCFlagsBefore+=('-ftrivial-auto-var-init=pattern')
113 format)
114 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling format >&2; fi
115 hardeningCFlagsBefore+=('-Wformat' '-Wformat-security' '-Werror=format-security')
117 zerocallusedregs)
118 if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling zerocallusedregs >&2; fi
119 hardeningCFlagsBefore+=('-fzero-call-used-regs=used-gpr')
122 # Ignore unsupported. Checked in Nix that at least *some*
123 # tool supports each flag.
125 esac
126 done