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 |
| 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 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. |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 13 | lcl_maybe_fortify ?= "${@oe.utils.conditional('DEBUG_BUILD','1','','-D_FORTIFY_SOURCE=2',d)}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | # Error on use of format strings that represent possible security problems |
| 16 | SECURITY_STRINGFORMAT ?= "-Wformat -Wformat-security -Werror=format-security" |
| 17 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 18 | # Inject pie flags into compiler flags if not configured with gcc itself |
| 19 | # especially useful with external toolchains |
| 20 | SECURITY_PIE_CFLAGS ?= "${@'' if '${GCCPIE}' else '-pie -fPIE'}" |
| 21 | |
| 22 | SECURITY_NOPIE_CFLAGS ?= "-no-pie -fno-PIE" |
| 23 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 24 | SECURITY_STACK_PROTECTOR ?= "-fstack-protector-strong" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 26 | SECURITY_CFLAGS ?= "${SECURITY_STACK_PROTECTOR} ${SECURITY_PIE_CFLAGS} ${lcl_maybe_fortify} ${SECURITY_STRINGFORMAT}" |
| 27 | SECURITY_NO_PIE_CFLAGS ?= "${SECURITY_STACK_PROTECTOR} ${lcl_maybe_fortify} ${SECURITY_STRINGFORMAT}" |
| 28 | |
| 29 | SECURITY_LDFLAGS ?= "${SECURITY_STACK_PROTECTOR} -Wl,-z,relro,-z,now" |
| 30 | SECURITY_X_LDFLAGS ?= "${SECURITY_STACK_PROTECTOR} -Wl,-z,relro" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | |
| 32 | # powerpc does not get on with pie for reasons not looked into as yet |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 33 | GCCPIE_powerpc = "" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 34 | GLIBCPIE_powerpc = "" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 35 | SECURITY_CFLAGS_remove_powerpc = "${SECURITY_PIE_CFLAGS}" |
| 36 | SECURITY_CFLAGS_pn-libgcc_powerpc = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | SECURITY_CFLAGS_pn-glibc = "" |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame^] | 39 | SECURITY_CFLAGS_pn-glibc-testsuite = "" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 40 | SECURITY_CFLAGS_pn-gcc-runtime = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | SECURITY_CFLAGS_pn-grub = "" |
| 42 | SECURITY_CFLAGS_pn-grub-efi = "" |
| 43 | SECURITY_CFLAGS_pn-grub-efi-native = "" |
| 44 | SECURITY_CFLAGS_pn-grub-efi-x86-native = "" |
| 45 | SECURITY_CFLAGS_pn-grub-efi-i586-native = "" |
| 46 | SECURITY_CFLAGS_pn-grub-efi-x86-64-native = "" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 47 | SECURITY_CFLAGS_pn-mkelfimage_x86 = "" |
| 48 | |
| 49 | SECURITY_CFLAGS_pn-valgrind = "${SECURITY_NOPIE_CFLAGS}" |
| 50 | SECURITY_LDFLAGS_pn-valgrind = "" |
| 51 | SECURITY_CFLAGS_pn-sysklogd = "${SECURITY_NOPIE_CFLAGS}" |
| 52 | SECURITY_LDFLAGS_pn-sysklogd = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 54 | # Recipes which fail to compile when elevating -Wformat-security to an error |
| 55 | SECURITY_STRINGFORMAT_pn-busybox = "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 56 | SECURITY_STRINGFORMAT_pn-gcc = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 58 | TARGET_CC_ARCH_append_class-target = " ${SECURITY_CFLAGS}" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 59 | TARGET_LDFLAGS_append_class-target = " ${SECURITY_LDFLAGS}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 61 | SECURITY_STACK_PROTECTOR_pn-gcc-runtime = "" |
| 62 | SECURITY_STACK_PROTECTOR_pn-glibc = "" |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame^] | 63 | SECURITY_STACK_PROTECTOR_pn-glibc-testsuite = "" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 64 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | SECURITY_LDFLAGS_pn-xserver-xorg = "${SECURITY_X_LDFLAGS}" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 67 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 68 | TARGET_CC_ARCH_append_pn-binutils = " ${SELECTED_OPTIMIZATION}" |
| 69 | TARGET_CC_ARCH_append_pn-gcc = " ${SELECTED_OPTIMIZATION}" |
| 70 | TARGET_CC_ARCH_append_pn-gdb = " ${SELECTED_OPTIMIZATION}" |
| 71 | TARGET_CC_ARCH_append_pn-perf = " ${SELECTED_OPTIMIZATION}" |