2017-08-08 04:20:21 +01:00
|
|
|
allHardeningFlags=(fortify stackprotector pie pic strictoverflow format relro bindnow)
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags=()
|
2016-03-07 20:39:26 +00:00
|
|
|
|
2017-08-08 20:15:44 +01:00
|
|
|
declare -A hardeningDisableMap=()
|
|
|
|
declare -A hardeningEnableMap=()
|
2016-02-26 17:38:15 +00:00
|
|
|
|
2017-08-08 04:20:21 +01:00
|
|
|
# Create table of unsupported flags for this toolchain.
|
|
|
|
for flag in @hardening_unsupported_flags@; do
|
2017-08-02 17:48:51 +01:00
|
|
|
hardeningDisableMap[$flag]=1
|
|
|
|
done
|
2016-03-11 13:02:07 +00:00
|
|
|
|
2017-08-08 04:20:21 +01: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`.
|
2017-08-08 20:15:44 +01:00
|
|
|
for flag in ${NIX_HARDENING_ENABLE-}; do
|
|
|
|
if [[ -z "${hardeningDisableMap[$flag]-}" ]]; then
|
2017-08-08 04:20:21 +01:00
|
|
|
hardeningEnableMap[$flag]=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then
|
2017-08-08 04:20:21 +01:00
|
|
|
# Determine which flags were effectively disabled so we can report below.
|
|
|
|
for flag in ${allHardeningFlags[@]}; do
|
2017-08-08 20:15:44 +01:00
|
|
|
if [[ -z "${hardeningEnableMap[$flag]-}" ]]; then
|
2017-08-08 04:20:21 +01:00
|
|
|
hardeningDisableMap[$flag]=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-08-02 17:48:51 +01:00
|
|
|
printf 'HARDENING: disabled flags:' >&2
|
|
|
|
(( "${#hardeningDisableMap[@]}" )) && printf ' %q' "${!hardeningDisableMap[@]}" >&2
|
|
|
|
echo >&2
|
|
|
|
fi
|
|
|
|
|
2017-08-08 04:20:21 +01:00
|
|
|
if (( "${#hardeningEnableMap[@]}" )); then
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then
|
2017-08-02 17:48:51 +01:00
|
|
|
echo 'HARDENING: Is active (not completely disabled with "all" flag)' >&2;
|
|
|
|
fi
|
2017-08-08 04:20:21 +01:00
|
|
|
for flag in "${!hardeningEnableMap[@]}"; do
|
2016-02-26 17:38:15 +00:00
|
|
|
case $flag in
|
|
|
|
fortify)
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling fortify >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags+=('-O2' '-D_FORTIFY_SOURCE=2')
|
|
|
|
;;
|
|
|
|
stackprotector)
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling stackprotector >&2; fi
|
2017-08-02 17:48:51 +01:00
|
|
|
hardeningCFlags+=('-fstack-protector-strong' '--param' 'ssp-buffer-size=4')
|
2016-02-26 17:38:15 +00:00
|
|
|
;;
|
|
|
|
pie)
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling CFlags -fPIE >&2; fi
|
2016-03-05 23:14:55 +00:00
|
|
|
hardeningCFlags+=('-fPIE')
|
2016-03-06 17:03:57 +00:00
|
|
|
if [[ ! ("$*" =~ " -shared " || "$*" =~ " -static ") ]]; then
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling LDFlags -pie >&2; fi
|
2017-08-25 06:14:43 +01:00
|
|
|
hardeningCFlags+=('-pie')
|
2016-03-06 14:27:41 +00:00
|
|
|
fi
|
2016-02-26 17:38:15 +00:00
|
|
|
;;
|
|
|
|
pic)
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling pic >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags+=('-fPIC')
|
|
|
|
;;
|
|
|
|
strictoverflow)
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling strictoverflow >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags+=('-fno-strict-overflow')
|
|
|
|
;;
|
|
|
|
format)
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then echo HARDENING: enabling format >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags+=('-Wformat' '-Wformat-security' '-Werror=format-security')
|
|
|
|
;;
|
|
|
|
*)
|
2017-08-28 19:56:08 +01:00
|
|
|
# Ignore unsupported. Checked in Nix that at least *some*
|
|
|
|
# tool supports each flag.
|
2016-02-26 17:38:15 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
fi
|