2023-07-30 13:25:02 +02:00
|
|
|
declare -a hardeningCFlagsAfter=()
|
|
|
|
declare -a hardeningCFlagsBefore=()
|
2018-04-11 20:00:13 +02:00
|
|
|
|
|
|
|
declare -A hardeningEnableMap=()
|
2016-03-07 21:39:26 +01:00
|
|
|
|
2018-04-11 20:00:13 +02:00
|
|
|
# Intentionally word-split in case 'NIX_HARDENING_ENABLE' is defined in Nix. The
|
|
|
|
# array expansion also prevents undefined variables from causing trouble with
|
|
|
|
# `set -u`.
|
2020-04-28 06:08:48 +02:00
|
|
|
for flag in ${NIX_HARDENING_ENABLE_@suffixSalt@-}; do
|
2018-04-11 20:00:13 +02:00
|
|
|
hardeningEnableMap["$flag"]=1
|
|
|
|
done
|
2016-02-26 18:38:15 +01:00
|
|
|
|
2023-09-03 22:32:36 +02:00
|
|
|
# fortify3 implies fortify enablement - make explicit before
|
|
|
|
# we filter unsupported flags because unsupporting fortify3
|
|
|
|
# doesn't mean we should unsupport fortify too
|
|
|
|
if [[ -n "${hardeningEnableMap[fortify3]-}" ]]; then
|
|
|
|
hardeningEnableMap["fortify"]=1
|
|
|
|
fi
|
|
|
|
|
2018-04-11 20:00:13 +02:00
|
|
|
# Remove unsupported flags.
|
|
|
|
for flag in @hardening_unsupported_flags@; do
|
2018-04-14 16:16:34 +02:00
|
|
|
unset -v "hardeningEnableMap[$flag]"
|
2023-01-21 13:38:30 +01:00
|
|
|
# fortify being unsupported implies fortify3 is unsupported
|
|
|
|
if [[ "$flag" = 'fortify' ]] ; then
|
|
|
|
unset -v "hardeningEnableMap['fortify3']"
|
|
|
|
fi
|
2017-08-02 18:48:51 +02:00
|
|
|
done
|
2016-03-11 14:02:07 +01:00
|
|
|
|
2023-09-03 22:32:36 +02:00
|
|
|
# now make fortify and fortify3 mutually exclusive
|
2023-02-21 01:27:57 +01:00
|
|
|
if [[ -n "${hardeningEnableMap[fortify3]-}" ]]; then
|
2023-01-21 13:38:30 +01:00
|
|
|
unset -v "hardeningEnableMap['fortify']"
|
|
|
|
fi
|
|
|
|
|
2017-09-20 01:10:49 +02:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then
|
2023-10-08 23:56:46 +02:00
|
|
|
declare -a allHardeningFlags=(fortify fortify3 stackprotector pie pic strictoverflow format zerocallusedregs)
|
2018-04-11 20:00:13 +02:00
|
|
|
declare -A hardeningDisableMap=()
|
|
|
|
|
|
|
|
# Determine which flags were effectively disabled so we can report below.
|
|
|
|
for flag in "${allHardeningFlags[@]}"; do
|
|
|
|
if [[ -z "${hardeningEnableMap[$flag]-}" ]]; then
|
|
|
|
hardeningDisableMap["$flag"]=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-08-02 18:48:51 +02:00
|
|
|
printf 'HARDENING: disabled flags:' >&2
|
|
|
|
(( "${#hardeningDisableMap[@]}" )) && printf ' %q' "${!hardeningDisableMap[@]}" >&2
|
|
|
|
echo >&2
|
|
|
|
|
2018-04-11 20:00:13 +02:00
|
|
|
if (( "${#hardeningEnableMap[@]}" )); then
|
2017-08-02 18:48:51 +02:00
|
|
|
echo 'HARDENING: Is active (not completely disabled with "all" flag)' >&2;
|
|
|
|
fi
|
2016-02-26 18:38:15 +01:00
|
|
|
fi
|
2018-04-11 20:00:13 +02:00
|
|
|
|
|
|
|
for flag in "${!hardeningEnableMap[@]}"; do
|
|
|
|
case $flag in
|
2023-01-21 13:38:30 +01:00
|
|
|
fortify | fortify3)
|
2022-10-01 09:27:38 +02:00
|
|
|
# Use -U_FORTIFY_SOURCE to avoid warnings on toolchains that explicitly
|
|
|
|
# set -D_FORTIFY_SOURCE=0 (like 'clang -fsanitize=address').
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsBefore+=('-O2' '-U_FORTIFY_SOURCE')
|
|
|
|
# Unset any _FORTIFY_SOURCE values the command-line may have set before
|
|
|
|
# enforcing our own value, avoiding (potentially fatal) redefinition
|
|
|
|
# warnings
|
|
|
|
hardeningCFlagsAfter+=('-U_FORTIFY_SOURCE')
|
2023-01-21 13:38:30 +01:00
|
|
|
case $flag in
|
|
|
|
fortify)
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling fortify >&2; fi
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsAfter+=('-D_FORTIFY_SOURCE=2')
|
2023-01-21 13:38:30 +01:00
|
|
|
;;
|
|
|
|
fortify3)
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling fortify3 >&2; fi
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsAfter+=('-D_FORTIFY_SOURCE=3')
|
2023-01-21 13:38:30 +01:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# Ignore unsupported.
|
|
|
|
;;
|
|
|
|
esac
|
2018-04-11 20:00:13 +02:00
|
|
|
;;
|
|
|
|
stackprotector)
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling stackprotector >&2; fi
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsBefore+=('-fstack-protector-strong' '--param' 'ssp-buffer-size=4')
|
2018-04-11 20:00:13 +02:00
|
|
|
;;
|
|
|
|
pie)
|
2021-08-24 09:21:04 +02:00
|
|
|
# NB: we do not use `+=` here, because PIE flags must occur before any PIC flags
|
2018-04-11 20:00:13 +02:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling CFlags -fPIE >&2; fi
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsBefore=('-fPIE' "${hardeningCFlagsBefore[@]}")
|
2023-06-23 11:46:49 +02:00
|
|
|
if [[ ! (" ${params[*]} " =~ " -shared " || " ${params[*]} " =~ " -static ") ]]; then
|
2018-04-11 20:00:13 +02:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling LDFlags -pie >&2; fi
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsBefore=('-pie' "${hardeningCFlagsBefore[@]}")
|
2018-04-11 20:00:13 +02:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
pic)
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling pic >&2; fi
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsBefore+=('-fPIC')
|
2018-04-11 20:00:13 +02:00
|
|
|
;;
|
|
|
|
strictoverflow)
|
2023-07-15 07:35:50 +02:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling strictoverflow >&2; fi
|
|
|
|
if (( @isClang@ )); then
|
|
|
|
# In Clang, -fno-strict-overflow only serves to set -fwrapv and is
|
|
|
|
# reported as an unused CLI argument if -fwrapv or -fno-wrapv is set
|
|
|
|
# explicitly, so we side step that by doing the conversion here.
|
|
|
|
#
|
|
|
|
# See: https://github.com/llvm/llvm-project/blob/llvmorg-16.0.6/clang/lib/Driver/ToolChains/Clang.cpp#L6315
|
|
|
|
#
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsBefore+=('-fwrapv')
|
2023-07-15 07:35:50 +02:00
|
|
|
else
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsBefore+=('-fno-strict-overflow')
|
2023-07-15 07:35:50 +02:00
|
|
|
fi
|
2018-04-11 20:00:13 +02:00
|
|
|
;;
|
|
|
|
format)
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling format >&2; fi
|
2023-07-30 13:25:02 +02:00
|
|
|
hardeningCFlagsBefore+=('-Wformat' '-Wformat-security' '-Werror=format-security')
|
2018-04-11 20:00:13 +02:00
|
|
|
;;
|
2023-10-08 23:56:46 +02:00
|
|
|
zerocallusedregs)
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling zerocallusedregs >&2; fi
|
|
|
|
hardeningCFlagsBefore+=('-fzero-call-used-regs=used-gpr')
|
|
|
|
;;
|
2018-04-11 20:00:13 +02:00
|
|
|
*)
|
|
|
|
# Ignore unsupported. Checked in Nix that at least *some*
|
|
|
|
# tool supports each flag.
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|