1# Setup extra CFLAGS and LDFLAGS which have 'security' benefits. These 2# don't work universally, there are recipes which can't use one, the other 3# or both so an override is maintained here. The idea would be over 4# time to reduce this list to nothing. 5# From a Yocto Project perspective, this file is included and tested 6# in the DISTRO="poky" configuration. 7 8GCCPIE ?= "--enable-default-pie" 9# If static PIE is known to work well, GLIBCPIE="--enable-static-pie" can be set 10 11# _FORTIFY_SOURCE requires -O1 or higher, so disable in debug builds as they use 12# -O0 which then results in a compiler warning. 13OPTLEVEL = "${@bb.utils.filter('SELECTED_OPTIMIZATION', '-O0 -O1 -O2 -O3 -Ofast -Og -Os -Oz -O', d)}" 14 15lcl_maybe_fortify ?= "${@oe.utils.conditional('OPTLEVEL','-O0','','${OPTLEVEL} -D_FORTIFY_SOURCE=2',d)}" 16 17# Error on use of format strings that represent possible security problems 18SECURITY_STRINGFORMAT ?= "-Wformat -Wformat-security -Werror=format-security" 19 20# Inject pie flags into compiler flags if not configured with gcc itself 21# especially useful with external toolchains 22SECURITY_PIE_CFLAGS ?= "${@'' if '${GCCPIE}' else '-pie -fPIE'}" 23 24SECURITY_NOPIE_CFLAGS ?= "-no-pie -fno-PIE" 25 26SECURITY_STACK_PROTECTOR ?= "-fstack-protector-strong" 27 28SECURITY_CFLAGS ?= "${SECURITY_STACK_PROTECTOR} ${SECURITY_PIE_CFLAGS} ${lcl_maybe_fortify} ${SECURITY_STRINGFORMAT}" 29SECURITY_NO_PIE_CFLAGS ?= "${SECURITY_STACK_PROTECTOR} ${lcl_maybe_fortify} ${SECURITY_STRINGFORMAT}" 30 31SECURITY_LDFLAGS ?= "-Wl,-z,relro,-z,now" 32SECURITY_X_LDFLAGS ?= "-Wl,-z,relro" 33 34SECURITY_CFLAGS:pn-glibc = "" 35SECURITY_CFLAGS:pn-glibc-testsuite = "" 36SECURITY_CFLAGS:pn-gcc-runtime = "" 37SECURITY_CFLAGS:pn-grub = "" 38SECURITY_CFLAGS:pn-grub-efi = "" 39SECURITY_CFLAGS:pn-mkelfimage:x86 = "" 40 41SECURITY_CFLAGS:pn-valgrind = "${SECURITY_NOPIE_CFLAGS}" 42SECURITY_LDFLAGS:pn-valgrind = "" 43SECURITY_CFLAGS:pn-sysklogd = "${SECURITY_NOPIE_CFLAGS}" 44SECURITY_LDFLAGS:pn-sysklogd = "" 45 46# Recipes which fail to compile when elevating -Wformat-security to an error 47SECURITY_STRINGFORMAT:pn-busybox = "" 48SECURITY_STRINGFORMAT:pn-gcc = "" 49 50TARGET_CC_ARCH:append:class-target = " ${SECURITY_CFLAGS}" 51TARGET_LDFLAGS:append:class-target = " ${SECURITY_LDFLAGS}" 52TARGET_CC_ARCH:append:class-cross-canadian = " ${SECURITY_CFLAGS}" 53TARGET_LDFLAGS:append:class-cross-canadian = " ${SECURITY_LDFLAGS}" 54 55SECURITY_STACK_PROTECTOR:pn-gcc-runtime = "" 56SECURITY_STACK_PROTECTOR:pn-glibc = "" 57SECURITY_STACK_PROTECTOR:pn-glibc-testsuite = "" 58SECURITY_STACK_PROTECTOR:pn-ltp = "" 59# All xorg module drivers need to be linked this way as well and are 60# handled in recipes-graphics/xorg-driver/xorg-driver-common.inc 61SECURITY_LDFLAGS:pn-xserver-xorg = "${SECURITY_X_LDFLAGS}" 62 63TARGET_CC_ARCH:append:pn-binutils = " ${SELECTED_OPTIMIZATION}" 64TARGET_CC_ARCH:append:pn-gcc = " ${SELECTED_OPTIMIZATION}" 65TARGET_CC_ARCH:append:pn-gdb = " ${SELECTED_OPTIMIZATION}" 66