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