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