2016-02-26 18:38:15 +01:00
|
|
|
hardeningFlags=(fortify stackprotector pic strictoverflow format relro bindnow)
|
|
|
|
hardeningFlags+=("${hardeningEnable[@]}")
|
|
|
|
hardeningCFlags=()
|
|
|
|
hardeningLDFlags=()
|
2016-03-14 01:26:52 +01:00
|
|
|
hardeningDisable=${hardeningDisable:-""}
|
2016-03-07 21:39:26 +01:00
|
|
|
|
2016-08-24 10:59:52 +02:00
|
|
|
hardeningDisable+=" @hardening_unsupported_flags@"
|
2016-02-26 18:38:15 +01:00
|
|
|
|
2016-03-11 14:02:07 +01:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: Value of '$hardeningDisable': $hardeningDisable >&2; fi
|
|
|
|
|
2016-08-24 10:59:52 +02:00
|
|
|
if [[ ! $hardeningDisable =~ "all" ]]; then
|
2016-03-11 14:02:07 +01:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then 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
|
2016-03-14 01:26:52 +01:00
|
|
|
if [[ ! "${hardeningDisable}" =~ "$flag" ]]; then
|
2016-02-26 18:38:15 +01:00
|
|
|
case $flag in
|
|
|
|
fortify)
|
2016-03-10 15:47:55 +01: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)
|
2016-03-10 15:47:55 +01:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling stackprotector >&2; fi
|
2016-03-07 01:29:11 +01:00
|
|
|
hardeningCFlags+=('-fstack-protector-strong' '--param ssp-buffer-size=4')
|
2016-02-26 18:38:15 +01:00
|
|
|
;;
|
|
|
|
pie)
|
2016-03-10 15:47:55 +01: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
|
2016-03-10 15:47:55 +01:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling LDFlags -pie >&2; fi
|
2016-03-06 15:27:41 +01:00
|
|
|
hardeningLDFlags+=('-pie')
|
|
|
|
fi
|
2016-02-26 18:38:15 +01:00
|
|
|
;;
|
|
|
|
pic)
|
2016-03-10 15:47:55 +01:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling pic >&2; fi
|
2016-02-26 18:38:15 +01:00
|
|
|
hardeningCFlags+=('-fPIC')
|
|
|
|
;;
|
|
|
|
strictoverflow)
|
2016-03-10 15:47:55 +01: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)
|
2016-03-10 15:47:55 +01: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')
|
|
|
|
;;
|
|
|
|
relro)
|
2016-03-10 15:47:55 +01:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling relro >&2; fi
|
2016-08-23 17:25:26 +02:00
|
|
|
hardeningLDFlags+=('-z' 'relro')
|
2016-02-26 18:38:15 +01:00
|
|
|
;;
|
|
|
|
bindnow)
|
2016-03-10 15:47:55 +01:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling bindnow >&2; fi
|
2016-08-23 17:25:26 +02:00
|
|
|
hardeningLDFlags+=('-z' 'now')
|
2016-02-26 18:38:15 +01:00
|
|
|
;;
|
|
|
|
*)
|
2016-03-10 15:47:55 +01:00
|
|
|
echo "Hardening flag unknown: $flag" >&2
|
2016-02-26 18:38:15 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|