2018-04-11 20:00:13 +02:00
|
|
|
declare -a hardeningLDFlags=()
|
|
|
|
|
|
|
|
declare -A hardeningEnableMap=()
|
2017-08-28 20:56:08 +02: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
|
2017-08-28 20:56:08 +02:00
|
|
|
|
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]"
|
2017-08-28 20:56:08 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then
|
2018-04-11 20:00:13 +02:00
|
|
|
declare -a allHardeningFlags=(pie relro bindnow)
|
|
|
|
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-28 20:56:08 +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-28 20:56:08 +02:00
|
|
|
echo 'HARDENING: Is active (not completely disabled with "all" flag)' >&2;
|
|
|
|
fi
|
|
|
|
fi
|
2018-04-11 20:00:13 +02:00
|
|
|
|
|
|
|
for flag in "${!hardeningEnableMap[@]}"; do
|
|
|
|
case $flag in
|
|
|
|
pie)
|
2021-09-21 02:24:04 +02:00
|
|
|
if [[ ! (" $* " =~ " -shared " \
|
|
|
|
|| " $* " =~ " -static " \
|
|
|
|
|| " $* " =~ " -r " \
|
|
|
|
|| " $* " =~ " -Ur " \
|
|
|
|
|| " $* " =~ " -i ") ]]; then
|
2018-04-11 20:00:13 +02:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling LDFlags -pie >&2; fi
|
|
|
|
hardeningLDFlags+=('-pie')
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
relro)
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling relro >&2; fi
|
|
|
|
hardeningLDFlags+=('-z' 'relro')
|
|
|
|
;;
|
|
|
|
bindnow)
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling bindnow >&2; fi
|
|
|
|
hardeningLDFlags+=('-z' 'now')
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# Ignore unsupported. Checked in Nix that at least *some*
|
|
|
|
# tool supports each flag.
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|