Add support for stack-protector
authorJoel Peshkin <joel.peshkin@broadcom.com>
Sun, 11 Apr 2021 09:21:58 +0000 (11:21 +0200)
committerTom Rini <trini@konsulko.com>
Tue, 20 Apr 2021 11:31:12 +0000 (07:31 -0400)
Add support for stack protector for UBOOT, SPL, and TPL
as well as new pytest for stackprotector

Signed-off-by: Joel Peshkin <joel.peshkin@broadcom.com>
Adjust UEFI build flags.
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
14 files changed:
MAINTAINERS
Makefile
arch/arm/config.mk
arch/riscv/lib/Makefile
arch/x86/config.mk
cmd/Kconfig
cmd/Makefile
cmd/stackprot_test.c [new file with mode: 0644]
common/Kconfig
common/Makefile
common/stackprot.c [new file with mode: 0644]
configs/sandbox_defconfig
scripts/Makefile.spl
test/py/tests/test_stackprotector.py [new file with mode: 0644]

index c6dd9bf838d3f042a0933bc4aa01b1d56c45a2ae..2d267aeff249b310ea54c498082ec65487ff7b0b 100644 (file)
@@ -1062,6 +1062,13 @@ F:       include/sqfs.h
 F:     cmd/sqfs.c
 F:     test/py/tests/test_fs/test_squashfs/
 
+STACKPROTECTOR
+M:     Joel Peshkin <joel.peshkin@broadcom.com>
+S:     Maintained
+F:     common/stackprot.c
+F:     cmd/stackprot_test.c
+F:     test/py/tests/test_stackprotector.py
+
 TARGET_BCMNS3
 M:     Bharat Gooty <bharat.gooty@broadcom.com>
 M:     Rayagonda Kokatanur <rayagonda.kokatanur@broadcom.com>
index e423f6de74680654591b4577885ee8a19dc89b8b..3fc9777b0b2a642d8fb2751ce9dc4afe75fb0228 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -676,7 +676,12 @@ else
 KBUILD_CFLAGS  += -O2
 endif
 
+ifeq ($(CONFIG_STACKPROTECTOR),y)
+KBUILD_CFLAGS += $(call cc-option,-fstack-protector-strong)
+CFLAGS_EFI += $(call cc-option,-fno-stack-protector)
+else
 KBUILD_CFLAGS += $(call cc-option,-fno-stack-protector)
+endif
 KBUILD_CFLAGS += $(call cc-option,-fno-delete-null-pointer-checks)
 
 # disable stringop warnings in gcc 8+
index 4153f7e37130fa70f99a2bd16c0363f886065ead..e79f0104b92f78825385823418d09047e109db91 100644 (file)
@@ -11,7 +11,8 @@ CONFIG_STANDALONE_LOAD_ADDR = 0xc100000
 endif
 endif
 
-CFLAGS_NON_EFI := -fno-pic -ffixed-r9 -ffunction-sections -fdata-sections
+CFLAGS_NON_EFI := -fno-pic -ffixed-r9 -ffunction-sections -fdata-sections \
+                 -fstack-protector-strong
 CFLAGS_EFI := -fpic -fshort-wchar
 
 LDFLAGS_FINAL += --gc-sections
index ff0677aa966008b406d40d7b7e7a99f99ac797c1..d08cbe9b79deb8a8914fdc8405905e6474d6d504 100644 (file)
@@ -27,6 +27,7 @@ obj-$(CONFIG_SPL_BUILD)       += spl.o
 obj-y   += fdt_fixup.o
 
 # For building EFI apps
+CFLAGS_NON_EFI := -fstack-protector-strong
 CFLAGS_$(EFI_CRT0) := $(CFLAGS_EFI)
 CFLAGS_REMOVE_$(EFI_CRT0) := $(CFLAGS_NON_EFI)
 
index 3067702858a5d29e0c2f2a5cd0f1cf39c71cc2bb..7a8242562dbfe1da80a9fbd998f2841d7f4a859e 100644 (file)
@@ -42,7 +42,7 @@ OBJCOPYFLAGS_EFI := -j .text -j .sdata -j .data -j .dynamic -j .dynsym \
 # Compiler flags to be added when building UEFI applications
 CFLAGS_EFI := -fpic -fshort-wchar
 # Compiler flags to be removed when building UEFI applications
-CFLAGS_NON_EFI := -mregparm=3
+CFLAGS_NON_EFI := -mregparm=3 -fstack-protector-strong
 
 ifeq ($(CONFIG_EFI_STUB_64BIT),)
 CFLAGS_EFI += $(call cc-option, -mno-red-zone)
index 2b66285e0d4f8d6f5b8d526d06d98b2a131e03bd..9e8b69258ff7210872f5409e471a7608c0b1ea4e 100644 (file)
@@ -2325,6 +2325,15 @@ config CMD_AVB
            avb read_part_hex - read data from partition and output to stdout
            avb write_part - write data to partition
            avb verify - run full verification chain
+
+config CMD_STACKPROTECTOR_TEST
+       bool "Test command for stack protector"
+       depends on STACKPROTECTOR
+       help
+         Enable stackprot_test command
+         The stackprot_test command will force a stack overrun to test
+         the stack smashing detection mechanisms.
+
 endmenu
 
 config CMD_UBI
index e606ac4e8c0fe5bcd7acf9a2211771aaa397dd9d..4977fa15f43a592fe6f06519bc0e89e65c5c7e37 100644 (file)
@@ -144,6 +144,7 @@ obj-$(CONFIG_CMD_SPI) += spi.o
 obj-$(CONFIG_CMD_STRINGS) += strings.o
 obj-$(CONFIG_CMD_SMC) += smccc.o
 obj-$(CONFIG_CMD_SYSBOOT) += sysboot.o pxe_utils.o
+obj-$(CONFIG_CMD_STACKPROTECTOR_TEST) += stackprot_test.o
 obj-$(CONFIG_CMD_TERMINAL) += terminal.o
 obj-$(CONFIG_CMD_TIME) += time.o
 obj-$(CONFIG_CMD_TIMER) += timer.o
diff --git a/cmd/stackprot_test.c b/cmd/stackprot_test.c
new file mode 100644 (file)
index 0000000..36f5bac
--- /dev/null
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ *  Copyright 2021 Broadcom
+ */
+
+#include <common.h>
+#include <command.h>
+
+static int do_test_stackprot_fail(struct cmd_tbl *cmdtp, int flag, int argc,
+                                 char *const argv[])
+{
+       char a[128];
+
+       memset(a, 0xa5, 512);
+       return 0;
+}
+
+U_BOOT_CMD(stackprot_test, 1, 1, do_test_stackprot_fail,
+          "test stack protector fail", "");
index 0e36dfd2368e3f3507d36312976011e41d5d2df3..26496f9a2e79d8d3b8c01a6d1a473dd5043dfed3 100644 (file)
@@ -618,6 +618,23 @@ config TPL_HASH
          and the algorithms it supports are defined in common/hash.c. See
          also CMD_HASH for command-line access.
 
+config STACKPROTECTOR
+       bool "Stack Protector buffer overflow detection"
+       default n
+       help
+         Enable stack smash detection through compiler's stack-protector
+         canary logic
+
+config SPL_STACKPROTECTOR
+       bool "Stack Protector buffer overflow detection for SPL"
+       depends on STACKPROTECTOR && SPL
+       default n
+
+config TPL_STACKPROTECTOR
+       bool "Stack Protector buffer overflow detection for TPL"
+       depends on STACKPROTECTOR && TPL
+       default n
+
 endmenu
 
 menu "Update support"
index 0952ae23f8c0a3a24dd5e2885776bd516a59b42f..829ea5fb42663c34d93cc492b01c0f36e3275cce 100644 (file)
@@ -137,6 +137,7 @@ obj-$(CONFIG_CMD_LOADB) += xyzModem.o
 obj-$(CONFIG_$(SPL_TPL_)YMODEM_SUPPORT) += xyzModem.o
 
 obj-$(CONFIG_AVB_VERIFY) += avb_verify.o
+obj-$(CONFIG_$(SPL_TPL_)STACKPROTECTOR) += stackprot.o
 obj-$(CONFIG_SCP03) += scp03.o
 
 obj-$(CONFIG_QFW) += qfw.o
diff --git a/common/stackprot.c b/common/stackprot.c
new file mode 100644 (file)
index 0000000..d5b7061
--- /dev/null
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ *  Copyright 2021 Broadcom
+ */
+
+#include <common.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+unsigned long __stack_chk_guard = (unsigned long)(0xfeedf00ddeadbeef & ~0UL);
+
+void __stack_chk_fail(void)
+{
+       void *ra;
+
+       ra = __builtin_extract_return_addr(__builtin_return_address(0));
+       panic("Stack smashing detected in function:\n%p relocated from %p",
+             ra, ra - gd->reloc_off);
+}
index 12af9be360313f28110b627f31b90c10caf24e40..d3de9c31554b0b472ada7055d5262f60229d01fd 100644 (file)
@@ -25,6 +25,7 @@ CONFIG_LOG_SYSLOG=y
 CONFIG_LOG_ERROR_RETURN=y
 CONFIG_DISPLAY_BOARDINFO_LATE=y
 CONFIG_MISC_INIT_F=y
+CONFIG_STACKPROTECTOR=y
 CONFIG_ANDROID_AB=y
 CONFIG_CMD_CPU=y
 CONFIG_CMD_LICENSE=y
@@ -97,6 +98,7 @@ CONFIG_CMD_CRAMFS=y
 CONFIG_CMD_EXT4_WRITE=y
 CONFIG_CMD_SQUASHFS=y
 CONFIG_CMD_MTDPARTS=y
+CONFIG_CMD_STACKPROTECTOR_TEST=y
 CONFIG_MAC_PARTITION=y
 CONFIG_AMIGA_PARTITION=y
 CONFIG_OF_CONTROL=y
index ca988224dad8c0df5384e13e805216269b117b24..c69525fa74806eb12497cffe902561f606321ac4 100644 (file)
@@ -67,6 +67,12 @@ include $(srctree)/scripts/Makefile.lib
 KBUILD_CFLAGS += -ffunction-sections -fdata-sections
 LDFLAGS_FINAL += --gc-sections
 
+ifeq ($(CONFIG_$(SPL_TPL_)STACKPROTECTOR),y)
+KBUILD_CFLAGS += -fstack-protector-strong
+else
+KBUILD_CFLAGS += -fno-stack-protector
+endif
+
 # FIX ME
 cpp_flags := $(KBUILD_CPPFLAGS) $(PLATFORM_CPPFLAGS) $(UBOOTINCLUDE) \
                                                        $(NOSTDINC_FLAGS)
diff --git a/test/py/tests/test_stackprotector.py b/test/py/tests/test_stackprotector.py
new file mode 100644 (file)
index 0000000..b009437
--- /dev/null
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2021 Broadcom
+
+import pytest
+import signal
+
+@pytest.mark.buildconfigspec('cmd_stackprotector_test')
+def test_stackprotector(u_boot_console):
+    """Test that the stackprotector function works."""
+
+    u_boot_console.run_command('stackprot_test',wait_for_prompt=False)
+    expected_response = 'Stack smashing detected'
+    u_boot_console.wait_for(expected_response)
+    u_boot_console.restart_uboot()