2016-02-26 18:38:15 +01:00
|
|
|
hardeningFlags=(fortify stackprotector pic strictoverflow format relro bindnow)
|
2017-08-03 21:34:23 +02:00
|
|
|
# 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
|
2016-02-26 18:38:15 +01:00
|
|
|
hardeningCFlags=()
|
2017-09-07 12:22:33 +02:00
|
|
|
hardeningLDFlags=()
|
2016-03-07 21:39:26 +01:00
|
|
|
|
2017-08-02 18:48:51 +02:00
|
|
|
declare -A hardeningDisableMap
|
2016-02-26 18:38:15 +01:00
|
|
|
|
2017-08-03 21:34:23 +02:00
|
|
|
# Intentionally word-split in case 'hardeningDisable' is defined in Nix.
|
|
|
|
for flag in ${hardeningDisable[@]:-IGNORED_KEY} @hardening_unsupported_flags@
|
2017-08-02 18:48:51 +02:00
|
|
|
do
|
|
|
|
hardeningDisableMap[$flag]=1
|
|
|
|
done
|
2016-03-11 14:02:07 +01:00
|
|
|
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then
|
2017-08-02 18:48:51 +02:00
|
|
|
printf 'HARDENING: disabled flags:' >&2
|
|
|
|
(( "${#hardeningDisableMap[@]}" )) && printf ' %q' "${!hardeningDisableMap[@]}" >&2
|
|
|
|
echo >&2
|
|
|
|
fi
|
|
|
|
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -z "${hardeningDisableMap[all]:-}" ]]; then
|
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; 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
|
|
|
for flag in "${hardeningFlags[@]}"
|
|
|
|
do
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -z "${hardeningDisableMap[$flag]:-}" ]]; then
|
2016-02-26 18:38:15 +01:00
|
|
|
case $flag in
|
|
|
|
fortify)
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling fortify >&2; fi
|
2016-02-26 18:38:15 +01:00
|
|
|
hardeningCFlags+=('-O2' '-D_FORTIFY_SOURCE=2')
|
|
|
|
;;
|
|
|
|
stackprotector)
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling stackprotector >&2; fi
|
2017-08-02 18:48:51 +02:00
|
|
|
hardeningCFlags+=('-fstack-protector-strong' '--param' 'ssp-buffer-size=4')
|
2016-02-26 18:38:15 +01:00
|
|
|
;;
|
|
|
|
pie)
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling CFlags -fPIE >&2; fi
|
2016-03-06 00:14:55 +01:00
|
|
|
hardeningCFlags+=('-fPIE')
|
2016-03-06 18:03:57 +01:00
|
|
|
if [[ ! ("$*" =~ " -shared " || "$*" =~ " -static ") ]]; then
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling LDFlags -pie >&2; fi
|
2017-08-25 07:14:43 +02:00
|
|
|
hardeningCFlags+=('-pie')
|
2017-09-07 12:22:33 +02:00
|
|
|
hardeningLDFlags+=('-pie')
|
2016-03-06 15:27:41 +01:00
|
|
|
fi
|
2016-02-26 18:38:15 +01:00
|
|
|
;;
|
|
|
|
pic)
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling pic >&2; fi
|
2016-02-26 18:38:15 +01:00
|
|
|
hardeningCFlags+=('-fPIC')
|
|
|
|
;;
|
|
|
|
strictoverflow)
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling strictoverflow >&2; fi
|
2016-02-26 18:38:15 +01:00
|
|
|
hardeningCFlags+=('-fno-strict-overflow')
|
|
|
|
;;
|
|
|
|
format)
|
2017-08-03 21:34:23 +02:00
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling format >&2; fi
|
2016-02-26 18:38:15 +01:00
|
|
|
hardeningCFlags+=('-Wformat' '-Wformat-security' '-Werror=format-security')
|
|
|
|
;;
|
2017-09-07 12:22:33 +02:00
|
|
|
relro)
|
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling relro >&2; fi
|
|
|
|
hardeningLDFlags+=('-z' 'relro')
|
|
|
|
;;
|
|
|
|
bindnow)
|
|
|
|
if [[ -n "${NIX_DEBUG:-}" ]]; then echo HARDENING: enabling bindnow >&2; fi
|
|
|
|
hardeningLDFlags+=('-z' 'now')
|
|
|
|
;;
|
2016-02-26 18:38:15 +01:00
|
|
|
*)
|
2017-08-28 20:56:08 +02:00
|
|
|
# Ignore unsupported. Checked in Nix that at least *some*
|
|
|
|
# tool supports each flag.
|
2016-02-26 18:38:15 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|