2016-02-26 17:38:15 +00:00
|
|
|
hardeningFlags=(fortify stackprotector pic strictoverflow format relro bindnow)
|
|
|
|
hardeningFlags+=("${hardeningEnable[@]}")
|
|
|
|
hardeningCFlags=()
|
|
|
|
hardeningLDFlags=()
|
2016-03-14 00:26:52 +00:00
|
|
|
hardeningDisable=${hardeningDisable:-""}
|
2016-03-07 20:39:26 +00:00
|
|
|
|
|
|
|
if [[ "$($LD -z 2>&1)" =~ "unknown option" ]]; then
|
2016-03-14 00:26:52 +00:00
|
|
|
hardeningDisable+=" bindnow relro"
|
2016-03-07 20:39:26 +00:00
|
|
|
fi
|
2016-02-26 17:38:15 +00:00
|
|
|
|
2016-03-11 13:02:07 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: Value of '$hardeningDisable': $hardeningDisable >&2; fi
|
|
|
|
|
2016-02-26 17:38:15 +00:00
|
|
|
if [[ ! $hardeningDisable == "all" ]]; then
|
2016-03-11 13:02:07 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo 'HARDENING: Is active (not completely disabled with "all" flag)' >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
for flag in "${hardeningFlags[@]}"
|
|
|
|
do
|
2016-03-14 00:26:52 +00:00
|
|
|
if [[ ! "${hardeningDisable}" =~ "$flag" ]]; then
|
2016-02-26 17:38:15 +00:00
|
|
|
case $flag in
|
|
|
|
fortify)
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling fortify >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags+=('-O2' '-D_FORTIFY_SOURCE=2')
|
|
|
|
;;
|
|
|
|
stackprotector)
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling stackprotector >&2; fi
|
2016-03-07 00:29:11 +00:00
|
|
|
hardeningCFlags+=('-fstack-protector-strong' '--param ssp-buffer-size=4')
|
2016-02-26 17:38:15 +00:00
|
|
|
;;
|
|
|
|
pie)
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; 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
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling LDFlags -pie >&2; fi
|
2016-03-06 14:27:41 +00:00
|
|
|
hardeningLDFlags+=('-pie')
|
|
|
|
fi
|
2016-02-26 17:38:15 +00:00
|
|
|
;;
|
|
|
|
pic)
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling pic >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags+=('-fPIC')
|
|
|
|
;;
|
|
|
|
strictoverflow)
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling strictoverflow >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags+=('-fno-strict-overflow')
|
|
|
|
;;
|
|
|
|
format)
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling format >&2; fi
|
2016-02-26 17:38:15 +00:00
|
|
|
hardeningCFlags+=('-Wformat' '-Wformat-security' '-Werror=format-security')
|
|
|
|
;;
|
|
|
|
relro)
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling relro >&2; fi
|
2016-08-23 16:25:26 +01:00
|
|
|
hardeningLDFlags+=('-z' 'relro')
|
2016-02-26 17:38:15 +00:00
|
|
|
;;
|
|
|
|
bindnow)
|
2016-03-10 14:47:55 +00:00
|
|
|
if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling bindnow >&2; fi
|
2016-08-23 16:25:26 +01:00
|
|
|
hardeningLDFlags+=('-z' 'now')
|
2016-02-26 17:38:15 +00:00
|
|
|
;;
|
|
|
|
*)
|
2016-03-10 14:47:55 +00:00
|
|
|
echo "Hardening flag unknown: $flag" >&2
|
2016-02-26 17:38:15 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|