2018-04-11 01:07:27 +01:00
|
|
|
hardeningFlags=(relro bindnow)
|
|
|
|
# Intentionally word-split in case 'hardeningEnable' is defined in
|
|
|
|
# Nix. Also, our bootstrap tools version of bash is old enough that
|
|
|
|
# undefined arrays trip `set -u`.
|
|
|
|
if [[ -v hardeningEnable[@] ]]; then
|
|
|
|
hardeningFlags+=(${hardeningEnable[@]})
|
|
|
|
fi
|
|
|
|
hardeningLDFlags=()
|
2017-08-28 19:56:08 +01:00
|
|
|
|
2018-04-11 01:07:27 +01:00
|
|
|
declare -A hardeningDisableMap
|
2017-08-28 19:56:08 +01:00
|
|
|
|
2018-04-11 01:07:27 +01:00
|
|
|
# Intentionally word-split in case 'hardeningDisable' is defined in Nix.
|
|
|
|
for flag in ${hardeningDisable[@]:-IGNORED_KEY} @hardening_unsupported_flags@
|
|
|
|
do
|
|
|
|
hardeningDisableMap[$flag]=1
|
2018-03-07 00:21:10 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then
|
2017-08-28 19:56:08 +01:00
|
|
|
printf 'HARDENING: disabled flags:' >&2
|
|
|
|
(( "${#hardeningDisableMap[@]}" )) && printf ' %q' "${!hardeningDisableMap[@]}" >&2
|
|
|
|
echo >&2
|
2018-04-11 01:07:27 +01:00
|
|
|
fi
|
2017-08-28 19:56:08 +01:00
|
|
|
|
2018-04-11 01:07:27 +01:00
|
|
|
if [[ -z "${hardeningDisableMap[all]:-}" ]]; then
|
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then
|
2017-08-28 19:56:08 +01:00
|
|
|
echo 'HARDENING: Is active (not completely disabled with "all" flag)' >&2;
|
|
|
|
fi
|
2018-04-11 01:07:27 +01:00
|
|
|
for flag in "${hardeningFlags[@]}"
|
|
|
|
do
|
|
|
|
if [[ -z "${hardeningDisableMap[$flag]:-}" ]]; then
|
|
|
|
case $flag in
|
|
|
|
pie)
|
|
|
|
if [[ ! ("$*" =~ " -shared " || "$*" =~ " -static ") ]]; then
|
|
|
|
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
|
|
|
|
fi
|
|
|
|
done
|
2018-04-10 20:42:05 +01:00
|
|
|
fi
|