tools, Add CFLAGS_SECURITY for RELRO and PIE
Subsystems (9)
PATCH 1/1 tools, Add CFLAGS_SECURITY for RELRO and PIE
My Makefile-fu has always been lacking and I know there's a better way to do this. I'm trying to avoid heavily modifying the tools/ Makefiles because I see that every author has their own way of doing things and I didn't want to get in the way of specific use-case and design decisions.
P.
---8<---
PIE and RELRO security, while not quite new, is a relatively recent addition for user space executable security.
This patch adds PIE and RELRO support for some /usr/bin/ executables by adding the appropriate compiler flags. Information on PIE (and how it differs from relocatable code) can be found at
http://en.wikipedia.org/wiki/Position-independent_code
and information on RELRO can be found at
https://isisblogs.poly.edu/2011/06/01/relro-relocation-read-only/
I used the following test to confirm the changes,
find ./ -executable -type f | xargs file | grep ELF | awk -F ":" ' { print $1 } ' | sed 's/^..//' | while read -r f
do
f=${f%:* ELF}
pushd ${f%/*} >/dev/null
readelf -W -l ${f##*/} > ~-/program_headers 2> ~-/erroutput
readelf -W -d ${f##*/} > ~-/dynamic_section 2>> ~-/erroutput
readelf -W -h ${f##*/} > ~-/elf_header 2>> ~-/erroutput
popd >/dev/null
TYPE=$(grep -m1 -F 'Type:' elf_header | awk '{ print $2; }')
if [[ -s erroutput ]]
then
printf "PROCESSING FAILED: (%04d) %s : %s\n" $cnt "$pkg" "$f"
elif [[ $TYPE != EXEC && $TYPE != DYN ]]
then
printf "%4s: (%04d) %s : %s\n" $TYPE $cnt "$pkg" "$f"
continue
else
printf "PROCESSED: (%04d) %s : %s\n" $cnt "$pkg" "$f"
fi
f=${f#.}
RELRO=no
PIE=no
grep -F -m1 -q GNU_RELRO program_headers && RELRO=partial
[[ $RELRO = partial ]] && grep -F -m1 -q BIND_NOW dynamic_section && RELRO=full
grep -m1 -q 'Type:[[:space:]]*DYN' elf_header && PIE=dso
[[ $PIE = dso ]] && grep -m1 -F -q '(DEBUG)' dynamic_section && PIE=yes
printf "RELRO=%s\nPIE=%s\n\n" $RELRO $PIEdone; unset f
and also used 'LD_DEBUG=statistics <executable>' to check to see if there were any serious performance issues.
--- tools/cgroup/Makefile | 2 +- tools/firewire/Makefile | 11 ++++++----- tools/hv/Makefile | 2 +- tools/net/Makefile | 18 ++++++++++-------- tools/power/acpi/Makefile | 2 +- tools/power/cpupower/Makefile | 3 ++- tools/power/cpupower/bench/Makefile | 2 +- tools/power/cpupower/debug/i386/Makefile | 8 ++++---- tools/power/cpupower/debug/x86_64/Makefile | 4 ++-- tools/power/x86/turbostat/Makefile | 2 +- tools/power/x86/x86_energy_perf_policy/Makefile | 1 + tools/scripts/Makefile.include | 3 +++ tools/thermal/tmon/Makefile | 2 +- tools/usb/Makefile | 2 +- tools/virtio/Makefile | 12 +++++++++--- tools/vm/Makefile | 2 +- 16 files changed, 45 insertions(+), 31 deletions(-)
tools/cgroup/Makefile +1 -1
@@ -1,7 +1,7 @@ # Makefile for cgroup tools CC = $(CROSS_COMPILE)gcc -CFLAGS = -Wall -Wextra +CFLAGS = -Wall -Wextra $(CFLAGS_SECURITY) all: cgroup_event_listener %: %.c
tools/firewire/Makefile +6 -5
@@ -5,12 +5,13 @@ CC = gcc all : nosy-dump -nosy-dump : CFLAGS = -Wall -O2 -g -nosy-dump : CPPFLAGS = -DVERSION=\"$(nosy-dump-version)\" -I../../drivers/firewire -nosy-dump : LDFLAGS = -g -nosy-dump : LDLIBS = -lpopt +CFLAGS := -Wall -O2 -g $(CFLAGS_SECURITY) +CPPFLAGS := -DVERSION=\"$(nosy-dump-version)\" -I../../drivers/firewire +LDLIBS := -lpopt -nosy-dump : nosy-dump.o decode-fcp.o +NOSY_DUMP_OBJS = nosy-dump.o decode-fcp.o +nosy-dump : $(NOSY_DUMP_OBJS) + $(CC) $(CFLAGS) $(NOSY_DUMP_OBJS) $(LDLIBS) -o $@ clean : rm -rf *.o nosy-dump
tools/hv/Makefile +1 -1
@@ -3,7 +3,7 @@ CC = $(CROSS_COMPILE)gcc PTHREAD_LIBS = -lpthread WARNINGS = -Wall -Wextra -CFLAGS = $(WARNINGS) -g $(PTHREAD_LIBS) $(shell getconf LFS_CFLAGS) +CFLAGS = $(WARNINGS) $(CFLAGS_SECURITY) -g $(PTHREAD_LIBS) $(shell getconf LFS_CFLAGS) all: hv_kvp_daemon hv_vss_daemon hv_fcopy_daemon %: %.c
tools/net/Makefile +10 -8
@@ -10,20 +10,22 @@ YACC = bison %.lex.c: %.l $(LEX) -o $@ $< +CFLAGS := -Wall -O2 $(CFLAGS_SECURITY) all : bpf_jit_disasm bpf_dbg bpf_asm -bpf_jit_disasm : CFLAGS = -Wall -O2 -DPACKAGE='bpf_jit_disasm' +bpf_jit_disasm : CFLAGS += -DPACKAGE='bpf_jit_disasm' bpf_jit_disasm : LDLIBS = -lopcodes -lbfd -ldl -bpf_jit_disasm : bpf_jit_disasm.o +bpf_jit_disasm : bpf_jit_disasm.c + $(CC) $(CFLAGS) -o $@ $< $(LDLIBS) -bpf_dbg : CFLAGS = -Wall -O2 bpf_dbg : LDLIBS = -lreadline -bpf_dbg : bpf_dbg.o +bpf_dbg : bpf_dbg.c + $(CC) $(CFLAGS) -o $@ $< $(LDLIBS) -bpf_asm : CFLAGS = -Wall -O2 -I. -bpf_asm : LDLIBS = -bpf_asm : bpf_asm.o bpf_exp.yacc.o bpf_exp.lex.o -bpf_exp.lex.o : bpf_exp.yacc.c +BPF_ASM_OBJS = bpf_asm.o bpf_exp.yacc.o bpf_exp.lex.o +bpf_asm : CFLAGS += -I. +bpf_asm : $(BPF_ASM_OBJS) + $(CC) $(CFLAGS) $(BPF_ASM_OBJS) -o $@ clean : rm -rf *.o bpf_jit_disasm bpf_dbg bpf_asm bpf_exp.yacc.* bpf_exp.lex.*
tools/power/acpi/Makefile +1 -1
@@ -72,7 +72,7 @@ WARNINGS += $(call cc-supports,-Wdeclaration-after-statement) KERNEL_INCLUDE := ../../../include ACPICA_INCLUDE := ../../../drivers/acpi/acpica CFLAGS += -D_LINUX -I$(KERNEL_INCLUDE) -I$(ACPICA_INCLUDE) -CFLAGS += $(WARNINGS) +CFLAGS += $(WARNINGS) $(CFLAGS_SECURITY) ifeq ($(strip $(V)),false) QUIET=@
tools/power/cpupower/Makefile +2 -1
@@ -181,6 +181,7 @@ else STRIPCMD = $(STRIP) -s --remove-section=.note --remove-section=.comment endif +CFLAGS += -Wl,-z,relro,-z,now -fpie # the actual make rules
@@ -209,7 +210,7 @@ $(OUTPUT)%.o: %.c $(OUTPUT)cpupower: $(UTIL_OBJS) $(OUTPUT)libcpupower.so.$(LIB_MAJ) $(ECHO) " CC " $@ - $(QUIET) $(CC) $(CFLAGS) $(LDFLAGS) $(UTIL_OBJS) -lcpupower -lrt -lpci -L$(OUTPUT) -o $@ + $(QUIET) $(CC) $(CFLAGS) -pie $(LDFLAGS) $(UTIL_OBJS) -lcpupower -lrt -lpci -L$(OUTPUT) -o $@ $(QUIET) $(STRIPCMD) $@ $(OUTPUT)po/$(PACKAGE).pot: $(UTIL_SRC)
tools/power/cpupower/bench/Makefile +1 -1
@@ -16,7 +16,7 @@ $(OUTPUT)%.o : %.c $(OUTPUT)cpufreq-bench: $(OBJS) $(ECHO) " CC " $@ - $(QUIET) $(CC) -o $@ $(CFLAGS) $(OBJS) $(LIBS) + $(QUIET) $(CC) -o $@ $(CFLAGS) -fpie -pie $(OBJS) $(LIBS) all: $(OUTPUT)cpufreq-bench
tools/power/cpupower/debug/i386/Makefile +4 -4
@@ -12,16 +12,16 @@ INSTALL = /usr/bin/install default: all $(OUTPUT)centrino-decode: centrino-decode.c - $(CC) $(CFLAGS) -o $@ centrino-decode.c + $(CC) $(CFLAGS) -Wl,-z,relro,-z,now -fpie -pie -o $@ centrino-decode.c $(OUTPUT)dump_psb: dump_psb.c - $(CC) $(CFLAGS) -o $@ dump_psb.c + $(CC) $(CFLAGS) -Wl,-z,relro,-z,now -fpie -pie -o $@ dump_psb.c $(OUTPUT)intel_gsic: intel_gsic.c - $(CC) $(CFLAGS) -o $@ -llrmi intel_gsic.c + $(CC) $(CFLAGS) -Wl,-z,relro,-z,now -fpie -pie -o $@ -llrmi intel_gsic.c $(OUTPUT)powernow-k8-decode: powernow-k8-decode.c - $(CC) $(CFLAGS) -o $@ powernow-k8-decode.c + $(CC) $(CFLAGS) -Wl,-z,relro,-z,now -fpie -pie -o $@ powernow-k8-decode.c all: $(OUTPUT)centrino-decode $(OUTPUT)dump_psb $(OUTPUT)intel_gsic $(OUTPUT)powernow-k8-decode
tools/power/cpupower/debug/x86_64/Makefile +2 -2
@@ -12,10 +12,10 @@ INSTALL = /usr/bin/install default: all $(OUTPUT)centrino-decode: ../i386/centrino-decode.c - $(CC) $(CFLAGS) -o $@ $< + $(CC) $(CFLAGS) -Wl,-z,relro,-z,now -fpie -pie -o $@ $< $(OUTPUT)powernow-k8-decode: ../i386/powernow-k8-decode.c - $(CC) $(CFLAGS) -o $@ $< + $(CC) $(CFLAGS) -Wl,-z,relro,-z,now -fpie -pie -o $@ $< all: $(OUTPUT)centrino-decode $(OUTPUT)powernow-k8-decode
tools/power/x86/turbostat/Makefile +1 -1
@@ -8,7 +8,7 @@ ifeq ("$(origin O)", "command line") endif turbostat : turbostat.c -CFLAGS += -Wall +CFLAGS += -Wall $(CFLAGS_SECURITY) CFLAGS += -DMSRHEADER='"../../../../arch/x86/include/asm/msr-index.h"' %: %.c
tools/power/x86/x86_energy_perf_policy/Makefile +1 -0
@@ -1,4 +1,5 @@ DESTDIR ?= +CFLAGS += $(CFLAGS_SECURITY) x86_energy_perf_policy : x86_energy_perf_policy.c
tools/scripts/Makefile.include +3 -0
@@ -40,6 +40,9 @@ EXTRA_WARNINGS += -Wundef EXTRA_WARNINGS += -Wwrite-strings EXTRA_WARNINGS += -Wformat +CFLAGS_SECURITY := -Wl,-z,relro,-z,now -fpie -pie +export CFLAGS_SECURITY + ifneq ($(findstring $(MAKEFLAGS), w),w) PRINT_DIR = --no-print-directory else
tools/thermal/tmon/Makefile +1 -1
@@ -2,7 +2,7 @@ VERSION = 1.0 BINDIR=usr/bin WARNFLAGS=-Wall -Wshadow -W -Wformat -Wimplicit-function-declaration -Wimplicit-int -CFLAGS+= -O1 ${WARNFLAGS} -fstack-protector +CFLAGS+= -O1 ${WARNFLAGS} -fstack-protector $(CFLAGS_SECURITY) CC=$(CROSS_COMPILE)gcc CFLAGS+=-D VERSION=\"$(VERSION)\"
tools/usb/Makefile +1 -1
@@ -3,7 +3,7 @@ CC = $(CROSS_COMPILE)gcc PTHREAD_LIBS = -lpthread WARNINGS = -Wall -Wextra -CFLAGS = $(WARNINGS) -g -I../include +CFLAGS = $(WARNINGS) $(CFLAGS_SECURITY) -g -I../include LDFLAGS = $(PTHREAD_LIBS) all: testusb ffs-test
tools/virtio/Makefile +9 -3
@@ -1,10 +1,16 @@ all: test mod test: virtio_test vringh_test -virtio_test: virtio_ring.o virtio_test.o -vringh_test: vringh_test.o vringh.o virtio_ring.o +VIRTIO_TEST_OBJS = virtio_ring.o virtio_test.o +VRINGH_TEST_OBJS = vringh_test.o vringh.o virtio_ring.o -CFLAGS += -g -O2 -Werror -Wall -I. -I../include/ -I ../../usr/include/ -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE +CFLAGS += -g -O2 -Werror -Wall -I. -I../include/ -I ../../usr/include/ -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE $(CFLAGS_SECURITY) vpath %.c ../../drivers/virtio ../../drivers/vhost + +virtio_test: $(VIRTIO_TEST_OBJS) + $(CC) $(CFLAGS) $(VIRTIO_TEST_OBJS) -o $@ +vringh_test: $(VRINGH_TEST_OBJS) + $(CC) $(CFLAGS) $(VRINGH_TEST_OBJS) -o $@ + mod: ${MAKE} -C `pwd`/../.. M=`pwd`/vhost_test .PHONY: all test mod clean
tools/vm/Makefile +1 -1
@@ -15,7 +15,7 @@ $(LIBS): make -C $(LIB_DIR) %: %.c - $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + $(CC) $(CFLAGS) -Wl,-z,relro,-z,now -fpie -pie -o $@ $< $(LDFLAGS) clean: $(RM) page-types slabinfo page_owner_sort
Signature · 1 line
1.7.9.3