blob: e469eadca1ffca45fe3cfd13a7879872b6d8303b [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001# Setup extra CFLAGS and LDFLAGS which have 'security' benefits. These
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002# don't work universally, there are recipes which can't use one, the other
3# or both so a blacklist 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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08006# in the DISTRO="poky" configuration.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
Brad Bishopd7bf8c12018-02-25 22:55:05 -05008GCCPIE ?= "--enable-default-pie"
Brad Bishop6ef32652018-10-09 18:59:25 +01009# If static PIE is known to work well, GLIBCPIE="--enable-static-pie" can be set
Brad Bishopd7bf8c12018-02-25 22:55:05 -050010
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011# _FORTIFY_SOURCE requires -O1 or higher, so disable in debug builds as they use
12# -O0 which then results in a compiler warning.
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060013OPTLEVEL = "${@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)}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016
Patrick Williamsc0f7c042017-02-23 20:41:17 -060017# Error on use of format strings that represent possible security problems
18SECURITY_STRINGFORMAT ?= "-Wformat -Wformat-security -Werror=format-security"
19
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020# 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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080026SECURITY_STACK_PROTECTOR ?= "-fstack-protector-strong"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080028SECURITY_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
Andrew Geissler4ed12e12020-06-05 18:00:41 -050031SECURITY_LDFLAGS ?= "-Wl,-z,relro,-z,now"
32SECURITY_X_LDFLAGS ?= "-Wl,-z,relro"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
34# powerpc does not get on with pie for reasons not looked into as yet
Patrick Williams213cb262021-08-07 19:21:33 -050035GCCPIE:powerpc = ""
36GLIBCPIE:powerpc = ""
37SECURITY_CFLAGS:remove:powerpc = "${SECURITY_PIE_CFLAGS}"
38SECURITY_CFLAGS:pn-libgcc:powerpc = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039
Patrick Williams213cb262021-08-07 19:21:33 -050040SECURITY_CFLAGS:pn-glibc = ""
41SECURITY_CFLAGS:pn-glibc-testsuite = ""
42SECURITY_CFLAGS:pn-gcc-runtime = ""
43SECURITY_CFLAGS:pn-grub = ""
44SECURITY_CFLAGS:pn-grub-efi = ""
45SECURITY_CFLAGS:pn-mkelfimage:x86 = ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050046
Patrick Williams213cb262021-08-07 19:21:33 -050047SECURITY_CFLAGS:pn-valgrind = "${SECURITY_NOPIE_CFLAGS}"
48SECURITY_LDFLAGS:pn-valgrind = ""
49SECURITY_CFLAGS:pn-sysklogd = "${SECURITY_NOPIE_CFLAGS}"
50SECURITY_LDFLAGS:pn-sysklogd = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051
Patrick Williamsc0f7c042017-02-23 20:41:17 -060052# Recipes which fail to compile when elevating -Wformat-security to an error
Patrick Williams213cb262021-08-07 19:21:33 -050053SECURITY_STRINGFORMAT:pn-busybox = ""
54SECURITY_STRINGFORMAT:pn-gcc = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055
Patrick Williams213cb262021-08-07 19:21:33 -050056TARGET_CC_ARCH:append:class-target = " ${SECURITY_CFLAGS}"
57TARGET_LDFLAGS:append:class-target = " ${SECURITY_LDFLAGS}"
58TARGET_CC_ARCH:append:class-cross-canadian = " ${SECURITY_CFLAGS}"
59TARGET_LDFLAGS:append:class-cross-canadian = " ${SECURITY_LDFLAGS}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060
Patrick Williams213cb262021-08-07 19:21:33 -050061SECURITY_STACK_PROTECTOR:pn-gcc-runtime = ""
62SECURITY_STACK_PROTECTOR:pn-glibc = ""
63SECURITY_STACK_PROTECTOR:pn-glibc-testsuite = ""
Brad Bishop316dfdd2018-06-25 12:45:53 -040064# All xorg module drivers need to be linked this way as well and are
65# handled in recipes-graphics/xorg-driver/xorg-driver-common.inc
Patrick Williams213cb262021-08-07 19:21:33 -050066SECURITY_LDFLAGS:pn-xserver-xorg = "${SECURITY_X_LDFLAGS}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050067
Patrick Williams213cb262021-08-07 19:21:33 -050068TARGET_CC_ARCH:append:pn-binutils = " ${SELECTED_OPTIMIZATION}"
69TARGET_CC_ARCH:append:pn-gcc = " ${SELECTED_OPTIMIZATION}"
70TARGET_CC_ARCH:append:pn-gdb = " ${SELECTED_OPTIMIZATION}"
71TARGET_CC_ARCH:append:pn-perf = " ${SELECTED_OPTIMIZATION}"