From be15155a66db43c02c5ea1de6fd942d601129123 Mon Sep 17 00:00:00 2001 From: Alcaro Date: Mon, 16 Apr 2018 23:21:24 +0200 Subject: [PATCH] Add hardening support and some other fixes, per #7 --- README.md | 2 +- make.sh | 41 +++++++++++++++++++++++++++++++++-------- profile/profile.sh | 10 ++++++++-- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 89ae45f..73fd81e 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Not planned (if your plans are different, send a PR): - Qt GUI support; my distro prefers GTK+, and all distros I've seen can run both GTK+ and Qt, not worth the effort (and QString's UTF-16 irritates me) To compile (Linux): -- Install g++, build-essential, libgtk-3-dev and pkg-config (names may vary on non-Debian-based distros) +- Install g++ and build-essential (names may vary on non-Debian-based distros). If you want a GUI, install libgtk-3-dev and pkg-config as well. - To build an optimized binary, use `./make.sh`; for development, use `make CFLAGS=-g` To compile (other Unix): diff --git a/make.sh b/make.sh index c335d68..24123f1 100755 --- a/make.sh +++ b/make.sh @@ -2,6 +2,36 @@ #This script creates a heavily optimized binary. For debugging, you're better off using 'make'. +FLAGS='-Wall -Werror -O3 -s -flto -fuse-linker-plugin -fweb -fomit-frame-pointer -fmerge-all-constants -fvisibility=hidden' +FLAGS=$FLAGS' -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables' +FLAGS=$FLAGS' -ffunction-sections -fdata-sections -Wl,--gc-sections -fprofile-dir=obj/' +FLAGS=$FLAGS' -Wl,-z,relro,-z,now,--as-needed,--hash-style=gnu,--relax' + +for i in "$@"; do +case "$i" in + --harden=no) + ;; + --harden=yes) + FLAGS=$FLAGS' -fstack-protector-all -Wstack-protector --param ssp-buffer-size=4 -pie -fPIE -D_FORTIFY_SOURCE=2' + true | gcc -E - -mmitigate-rop > /dev/null 2>&1 && + FLAGS=$FLAGS' -mmitigate-rop' + ;; + --profile=no) + echo n > profile/choice + ;; + --profile=yes) + if [ ! -e profile/firefox-45.0esr.tar ]; then + profile/download.sh + fi + ;; + *) # unknown option + echo "Unknown argument $1; valid arguments are: --harden=no --harden=yes --profile=no --profile=yes" + exit 1 + ;; +esac +done + + if [ ! -e profile/choice ]; then while true; do read -p "Do you wish to use profile-guided optimization? This will download a 100MB training corpus from the internet. (y/n)" yn @@ -16,11 +46,6 @@ fi #clean up rm flips flips.exe floating.zip obj/* -FLAGS='-Wall -Werror -O3 -s -flto -fwhole-program -fweb -fomit-frame-pointer -fmerge-all-constants -fvisibility=hidden' -FLAGS=$FLAGS' -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables' -FLAGS=$FLAGS' -ffunction-sections -fdata-sections -Wl,--gc-sections -fprofile-dir=obj/' -FLAGS=$FLAGS' -Wl,-z,relro,--as-needed,--hash-style=gnu,--relax' - ##create windows binary #echo 'Windows/Resource (Wine warmup)' #mingwver 5.3 @@ -47,15 +72,15 @@ FLAGS=$FLAGS' -Wl,-z,relro,--as-needed,--hash-style=gnu,--relax' #create linux binary if [ -e profile/firefox-45.0esr.tar ]; then echo 'GTK+ (1/3)' -rm flips; make TARGET=gtk OPTFLAGS="$FLAGS -fprofile-generate -lgcov" || exit $? +rm flips; TARGET=gtk make OPTFLAGS="$FLAGS -fprofile-generate -lgcov" || exit $? [ -e flips ] || exit 1 echo 'GTK+ (2/3)' profile/profile.sh ./flips echo 'GTK+ (3/3)' -rm flips; make TARGET=gtk OPTFLAGS="$FLAGS -fprofile-use" +rm flips; TARGET=gtk make OPTFLAGS="$FLAGS -fprofile-use" #mv flips '~/bin/flips' else -rm flips; make TARGET=gtk OPTFLAGS="$FLAGS" || exit $? +rm flips; TARGET=gtk make OPTFLAGS="$FLAGS" || exit $? fi #echo Finishing diff --git a/profile/profile.sh b/profile/profile.sh index 78e62df..5222a6b 100755 --- a/profile/profile.sh +++ b/profile/profile.sh @@ -3,5 +3,11 @@ #profiling plus threading tends to act weirdly, disable it export OMP_NUM_THREADS=1 -$1 --create --bps-delta profile/firefox-45.0esr.tar profile/firefox-52.0esr.tar /dev/null -$1 --create --bps-delta-moremem profile/firefox-45.0esr.tar profile/firefox-52.0esr.tar /dev/null +FX45=profile/firefox-45.0esr.tar +FX52=profile/firefox-52.0esr.tar + +#silly grep because GOutputStream can't write to /dev/null, so write fails +#the error is harmless, but confuses users, so let's hide it +#also ||true to mask Flips itself failing; doesn't really do anything, but no reason not to +$1 --create --bps-delta $FX45 $FX52 /dev/null | sed 's/.*read-only.*/The patch was created successfully!/' || true +$1 --create --bps-delta-moremem $FX45 $FX52 /dev/null | sed 's/.*read-only.*/The patch was created successfully!/' || true