* [PATCH 00/21] sandbox: add libfuzzer-based fuzzing
@ 2025-06-05 11:35 Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 01/21] pbl: add provision for architectures without piggy loader Ahmad Fatoum
` (20 more replies)
0 siblings, 21 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox
We have a number of security-sensitive parsers in barebox that process
untrusted input, even in secure boot systems, e.g. the FIT parser, but
also the partition parser.
This series adds fuzzing tests for a number of these parsers based on
libfuzzer.
Ahmad Fatoum (21):
pbl: add provision for architectures without piggy loader
firmware: make Layerscape FMan firmware proper-only
mci: sdhci: support compiling common SDHCI code for sandbox PBL
kbuild: define and use more generic symlink command
kbuild: collect compatibility symlink creation in symlink-y
kbuild: allow customizing barebox proper binary
sandbox: make available all CONFIG_ symbols to OS glue code
sandbox: switch to using PBL
kbuild: populate non-host CXX variables
string: add fortify source support
sandbox: populate UNAME_M variable
Add fuzzing infrastructure
filetype: add fuzz target
block: mark underlying cdev with DEVFS_IS_BLOCK_DEV
block: add lightweight ramdisk support
fuzz: add support for passing fuzz data as r/o ramdisk
partitions: add partition table parser fuzz target
fdt: add fuzz test
fit: add fuzz test
Documentation: add LLVM libfuzzer documentation
sandbox: add support for coverage info generation
.gitignore | 6 +
Documentation/devel/devel.rst | 1 +
Documentation/devel/fuzzing.rst | 136 +++
Makefile | 72 +-
arch/Kconfig | 6 +
arch/sandbox/Kconfig | 10 +
arch/sandbox/Kconfig.debug | 7 +
arch/sandbox/Makefile | 91 +-
arch/sandbox/board/.gitignore | 3 -
arch/sandbox/board/Makefile | 2 -
arch/sandbox/include/asm/barebox-sandbox.h | 10 +
arch/sandbox/lib/.gitignore | 3 +
arch/sandbox/lib/Makefile | 2 +-
.../{board/barebox.lds.S => lib/pbl.lds.S} | 0
arch/sandbox/os/Makefile | 24 +-
arch/sandbox/os/common.c | 138 ++-
arch/sandbox/{lib => os}/unwind.c | 3 +-
arch/x86/um/Makefile | 4 +-
commands/Makefile | 1 +
commands/fuzz.c | 118 +++
commands/stacksmash.c | 6 +-
common/Kconfig | 7 +-
common/block.c | 9 +-
common/boards/configs/libfuzzer.config | 14 +
common/filetype.c | 12 +
common/image-fit.c | 76 +-
common/partitions.c | 56 ++
common/startup.c | 1 +
drivers/block/Kconfig | 6 +
drivers/block/Makefile | 1 +
drivers/block/ramdisk.c | 178 ++++
drivers/of/fdt.c | 39 +
firmware/Makefile | 3 +-
images/.gitignore | 2 +
images/Makefile | 26 +-
images/Makefile.sandbox | 33 +
include/asm-generic/barebox.lds.h | 13 +-
include/block.h | 13 +-
include/dma.h | 22 +-
include/driver.h | 1 +
include/filetype.h | 4 +-
include/fuzz.h | 87 ++
include/linux/compiler_types.h | 41 +
include/linux/fortify-string.h | 804 ++++++++++++++++++
include/linux/string.h | 17 +
include/mci.h | 9 +
include/ramdisk.h | 24 +
lib/Kconfig.hardening | 15 +
lib/Makefile | 3 +-
lib/fuzz.c | 79 ++
lib/string.c | 16 +-
lib/string_helpers.c | 30 +
lib/vsprintf.c | 15 +
pbl/Kconfig | 10 +-
pbl/string.c | 1 +
scripts/Kconfig.include | 1 +
scripts/Makefile.lib | 6 +-
scripts/clang-runtime-dir.sh | 19 +
scripts/subarch.include | 12 +-
test/Kconfig | 39 +
60 files changed, 2252 insertions(+), 135 deletions(-)
create mode 100644 Documentation/devel/fuzzing.rst
create mode 100644 arch/sandbox/include/asm/barebox-sandbox.h
create mode 100644 arch/sandbox/lib/.gitignore
rename arch/sandbox/{board/barebox.lds.S => lib/pbl.lds.S} (100%)
rename arch/sandbox/{lib => os}/unwind.c (88%)
create mode 100644 commands/fuzz.c
create mode 100644 common/boards/configs/libfuzzer.config
create mode 100644 drivers/block/ramdisk.c
create mode 100644 images/Makefile.sandbox
create mode 100644 include/fuzz.h
create mode 100644 include/linux/fortify-string.h
create mode 100644 include/ramdisk.h
create mode 100644 lib/fuzz.c
create mode 100644 lib/string_helpers.c
create mode 100755 scripts/clang-runtime-dir.sh
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 01/21] pbl: add provision for architectures without piggy loader
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 02/21] firmware: make Layerscape FMan firmware proper-only Ahmad Fatoum
` (19 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
In preparation for enabling PBL support on sandbox, allow sandbox to
link directly PBL with barebox proper without requiring PBL to use mmap
to change attributes.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
images/Makefile | 11 +++++++----
pbl/Kconfig | 8 ++++++++
2 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/images/Makefile b/images/Makefile
index 6daaee8b7c7e..91123ada4744 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -56,21 +56,24 @@ extra-y += $(pbl-lds)
$(pbl-lds): $(obj)/../arch/$(SRCARCH)/lib/pbl.lds.S FORCE
$(call if_changed_dep,cpp_lds_S)
+ifeq ($(CONFIG_PBL_IMAGE_NO_PIGGY),)
+BAREBOX_PIGGY_OBJS = $(obj)/piggy.o $(obj)/sha_sum.o
+endif
+
quiet_cmd_elf__ ?= LD $@
cmd_elf__ ?= $(LD) $(LDFLAGS_$(patsubst .%,%,$(suffix $(@F)))) \
-e $(2) -Map $@.map $(LDFLAGS_$(@F)) -o $@ \
--gc-sections --defsym=__pbl_board_entry=$(2) \
-T $(pbl-lds) \
- --whole-archive $(BAREBOX_PBL_OBJS) $(obj)/piggy.o \
- $(obj)/sha_sum.o
+ --whole-archive $(BAREBOX_PBL_OBJS) $(BAREBOX_PIGGY_OBJS) \
PBL_CPPFLAGS += -fdata-sections -ffunction-sections
-$(obj)/%.pbl: $(pbl-lds) $(BAREBOX_PBL_OBJS) $(obj)/piggy.o $(obj)/sha_sum.o FORCE
+$(obj)/%.pbl: $(pbl-lds) $(BAREBOX_PBL_OBJS) $(BAREBOX_PIGGY_OBJS) FORCE
$(call if_changed,elf__,$(*F))
$(call if_changed,prelink__)
-$(obj)/%.elf: $(pbl-lds) $(BAREBOX_PBL_OBJS) $(obj)/piggy.o $(obj)/sha_sum.o FORCE
+$(obj)/%.elf: $(pbl-lds) $(BAREBOX_PBL_OBJS) $(BAREBOX_PIGGY_OBJS) FORCE
$(call if_changed,elf__,$(*F))
$(call if_changed,prelink__)
diff --git a/pbl/Kconfig b/pbl/Kconfig
index 6e3581829d58..e9fa35e29b19 100644
--- a/pbl/Kconfig
+++ b/pbl/Kconfig
@@ -13,6 +13,14 @@ config PBL_IMAGE
bool "Pre-Bootloader image"
depends on HAVE_PBL_IMAGE
+config PBL_IMAGE_NO_PIGGY
+ bool
+ depends on PBL_IMAGE
+ help
+ This is a special case for architectures that don't
+ want to use the piggy mechanism to load barebox proper.
+ It's so far only intended for sandbox.
+
config PBL_MULTI_IMAGES
bool
select PBL_IMAGE
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 02/21] firmware: make Layerscape FMan firmware proper-only
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 01/21] pbl: add provision for architectures without piggy loader Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 03/21] mci: sdhci: support compiling common SDHCI code for sandbox PBL Ahmad Fatoum
` (18 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
There is no point in linking the FMan firmware, which is exclusively
used in barebox proper against the PBL.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
firmware/Makefile | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/firmware/Makefile b/firmware/Makefile
index 0bc502f3d4f3..cc32bb7c3b89 100644
--- a/firmware/Makefile
+++ b/firmware/Makefile
@@ -34,7 +34,7 @@ endif
firmware-$(CONFIG_FIRMWARE_NEXT_IMAGE) += next-image.bin
-firmware-$(CONFIG_DRIVER_NET_FSL_FMAN) += fsl_fman_ucode_ls1046_r1.0_106_4_18.bin
+obj-firmware-$(CONFIG_DRIVER_NET_FSL_FMAN) += fsl_fman_ucode_ls1046_r1.0_106_4_18.bin
fw-external-$(CONFIG_FIRMWARE_LS1028A_ATF) += ls1028a-bl31.bin
fw-external-$(CONFIG_FIRMWARE_LS1046A_ATF) += ls1046a-bl31.bin
@@ -45,6 +45,7 @@ pbl-firmware-$(CONFIG_FIRMWARE_TQMA6UL_OPTEE) += mba6ul_optee.bin
fwobjdir := $(objtree)/firmware
pbl-y := $(addsuffix .gen.o, $(pbl-firmware-y))
+obj-y := $(addsuffix .gen.o, $(obj-firmware-y))
obj-pbl-y := $(addsuffix .gen.o, $(firmware-y))
pbl-fwext-y := $(addsuffix .extgen.o, $(fw-external-y))
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 03/21] mci: sdhci: support compiling common SDHCI code for sandbox PBL
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 01/21] pbl: add provision for architectures without piggy loader Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 02/21] firmware: make Layerscape FMan firmware proper-only Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 04/21] kbuild: define and use more generic symlink command Ahmad Fatoum
` (17 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We do not link sandbox with --gc-sections, so having references to
non-existent functions would lead to errors, even if they are never
called. Fix this by providing stubs.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/dma.h | 22 +++++++++++++++++-----
include/mci.h | 9 +++++++++
2 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/include/dma.h b/include/dma.h
index cb44963cc02e..b8016c2e890e 100644
--- a/include/dma.h
+++ b/include/dma.h
@@ -105,6 +105,12 @@ void dma_sync_single_for_cpu(struct device *dev, dma_addr_t address,
void dma_sync_single_for_device(struct device *dev, dma_addr_t address,
size_t size, enum dma_data_direction dir);
+
+dma_addr_t dma_map_single(struct device *dev, void *ptr,
+ size_t size, enum dma_data_direction dir);
+
+void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
+ size_t size, enum dma_data_direction dir);
#else
/*
* assumes buffers are in coherent/uncached memory, e.g. because
@@ -121,13 +127,19 @@ static inline void dma_sync_single_for_device(struct device *dev, dma_addr_t add
{
barrier_data(address);
}
+
+static inline dma_addr_t dma_map_single(struct device *dev, void *ptr,
+ size_t size, enum dma_data_direction dir)
+{
+ return virt_to_phys(ptr);
+}
+
+static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
+ size_t size, enum dma_data_direction dir)
+{
+}
#endif
-dma_addr_t dma_map_single(struct device *dev, void *ptr,
- size_t size, enum dma_data_direction dir);
-
-void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
- size_t size, enum dma_data_direction dir);
#ifndef dma_alloc_coherent
void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle);
diff --git a/include/mci.h b/include/mci.h
index 207fb9779a14..82099f118e65 100644
--- a/include/mci.h
+++ b/include/mci.h
@@ -767,6 +767,7 @@ static inline int mmc_card_hs(struct mci *mci)
mci->host->ios.timing == MMC_TIMING_MMC_HS;
}
+#if IN_PROPER
/*
* Execute tuning sequence to seek the proper bus operating
* conditions for HS200 and HS400, which sends CMD21 to the device.
@@ -775,6 +776,14 @@ int mmc_hs200_tuning(struct mci *mci);
int mci_execute_tuning(struct mci *mci);
int mmc_send_tuning(struct mci *mci, u32 opcode);
int mci_send_abort_tuning(struct mci *mci, u32 opcode);
+
+#else
+static inline int mmc_hs200_tuning(struct mci *mci) { return -ENOSYS; }
+static inline int mci_execute_tuning(struct mci *mci) { return -ENOSYS; }
+static inline int mmc_send_tuning(struct mci *mci, u32 opcode) { return -ENOSYS; }
+static inline int mci_send_abort_tuning(struct mci *mci, u32 opcode) { return -ENOSYS; }
+#endif
+
int mmc_select_timing(struct mci *mci);
int mci_set_blockcount(struct mci *mci, unsigned int cmdarg);
int mci_blk_part_switch(struct mci_part *part);
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 04/21] kbuild: define and use more generic symlink command
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (2 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 03/21] mci: sdhci: support compiling common SDHCI code for sandbox PBL Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 05/21] kbuild: collect compatibility symlink creation in symlink-y Ahmad Fatoum
` (16 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We currently use symlinks only between files in the same directory.
Let's make it possible to symlink across directories as well.
No functional change.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Makefile | 2 +-
images/Makefile | 2 +-
scripts/Makefile.lib | 4 ++--
3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
index 031d5bdd3e20..2abd03cb0a74 100644
--- a/Makefile
+++ b/Makefile
@@ -816,7 +816,7 @@ export KBUILD_BINARY ?= barebox.bin
# the default value.
barebox-flash-image: $(KBUILD_IMAGE) FORCE
- $(call if_changed,ln)
+ $(call if_changed,symlink)
barebox-flash-images: $(KBUILD_IMAGE)
@echo $^ > $@
diff --git a/images/Makefile b/images/Makefile
index 91123ada4744..c8ab8fc015ae 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -240,7 +240,7 @@ __images_install: images
PHONY += __images_install
$(flash-link): $(link-dest) FORCE
- $(call if_changed,ln)
+ $(call if_changed,symlink)
$(flash-list): $(image-y-path)
@for i in $^; do if [ -s $$i ]; then echo $$i; fi; done > $@
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index b10119797686..e960d62ce59e 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -529,8 +529,8 @@ cmd_comp_copy ?= cat $(filter-out FORCE,$^) > $@; \
quiet_cmd_disasm = DISASM $@
cmd_disasm = $(OBJDUMP) -d $< > $@
-quiet_cmd_ln = LN $@
-cmd_ln = ln -sf $< $@
+quiet_cmd_symlink = LN $@
+ cmd_symlink = mkdir -p $(dir $@) ; ln -fsn --relative $< $@
quiet_cmd_check_size = CHKSIZE $2
cmd_check_size = set -e; \
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 05/21] kbuild: collect compatibility symlink creation in symlink-y
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (3 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 04/21] kbuild: define and use more generic symlink command Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 06/21] kbuild: allow customizing barebox proper binary Ahmad Fatoum
` (15 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
This allows for easier addition of symlinks, which is something we are
going to do soon for the fuzzing support.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Makefile | 21 ++++++++++++---------
images/Makefile | 10 +++++++++-
scripts/Makefile.lib | 2 ++
3 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/Makefile b/Makefile
index 2abd03cb0a74..ecaf195b726d 100644
--- a/Makefile
+++ b/Makefile
@@ -815,9 +815,6 @@ export KBUILD_BINARY ?= barebox.bin
# Also any assignments in arch/$(SRCARCH)/Makefile take precedence over
# the default value.
-barebox-flash-image: $(KBUILD_IMAGE) FORCE
- $(call if_changed,symlink)
-
barebox-flash-images: $(KBUILD_IMAGE)
@echo $^ > $@
@@ -826,16 +823,22 @@ images: barebox.bin FORCE
images/%: barebox.bin FORCE
$(Q)$(MAKE) $(build)=images $@
-ifdef CONFIG_EFI_STUB
-all: barebox.bin images barebox.efi
-barebox.efi: FORCE
- $(Q)ln -fsn images/barebox-dt-2nd.img $@
-else ifdef CONFIG_PBL_IMAGE
+ifdef CONFIG_PBL_IMAGE
+SYMLINK_TARGET_barebox.efi = images/barebox-dt-2nd.img
+symlink-$(CONFIG_EFI_STUB) += barebox.efi
all: barebox.bin images
else
-all: barebox-flash-image barebox-flash-images
+SYMLINK_TARGET_barebox-flash-image = $(KBUILD_IMAGE)
+symlink-y += barebox-flash-image
+all: barebox-flash-images
endif
+all: $(symlink-y)
+
+.SECONDEXPANSION:
+$(symlink-y): $$(SYMLINK_TARGET_$$(@F)) FORCE
+ $(call if_changed,symlink_quiet)
+
common-$(CONFIG_PBL_IMAGE) += pbl/
common-$(CONFIG_DEFAULT_ENVIRONMENT) += defaultenv/
diff --git a/images/Makefile b/images/Makefile
index c8ab8fc015ae..ca37c054ae9c 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -223,9 +223,12 @@ flash-list := $(obj)/../barebox-flash-images
# multiple images
flash-link := $(obj)/../barebox-flash-image
link-dest := $(if $(filter 1,$(words $(image-y))),$(image-y-path),multi-image-build)
+
+symlink-y-path := $(addprefix $(obj)/,$(symlink-y))
+
multi-image-build:
-images: $(image-y-path) $(flash-link) $(flash-list) FORCE
+images: $(image-y-path) $(flash-link) $(flash-list) $(symlink-y-path) FORCE
@echo "images built:"
@for i in $(image-y); do \
if [ -s $(obj)/$$i ]; then echo $$i; \
@@ -233,6 +236,7 @@ images: $(image-y-path) $(flash-link) $(flash-list) FORCE
$(if $(CONFIG_MISSING_FIRMWARE_ERROR), >&2 sed 's/^/\t/' <$(obj)/$${i}.missing-firmware; missing=1;) \
fi; done; if [ -n "$$missing" ]; then \
echo >&2 "Firmware missing in CONFIG_MISSING_FIRMWARE_ERROR=y build"; exit 1; fi
+ @for i in $(symlink-y); do echo $$i; done
__images_install: images
@for i in $(image-y-path); do if [ -s $$i ]; then install -t "$(INSTALL_PATH)" $$i; fi; done
@@ -245,6 +249,10 @@ $(flash-link): $(link-dest) FORCE
$(flash-list): $(image-y-path)
@for i in $^; do if [ -s $$i ]; then echo $$i; fi; done > $@
+.SECONDEXPANSION:
+$(symlink-y-path): $(obj)/$$(SYMLINK_TARGET_$$(@F)) FORCE
+ $(call if_changed,symlink_quiet)
+
clean-files := *.pbl *.pblb *.elf *.map start_*.imximg *.img barebox.z start_*.kwbimg \
start_*.kwbuartimg *.socfpgaimg *.mlo *.t20img *.t20img.cfg *.t30img \
*.t30img.cfg *.t124img *.t124img.cfg *.mlospi *.mlo *.mxsbs *.mxssd *.rkimg \
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index e960d62ce59e..e6f0e254960a 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -532,6 +532,8 @@ cmd_disasm = $(OBJDUMP) -d $< > $@
quiet_cmd_symlink = LN $@
cmd_symlink = mkdir -p $(dir $@) ; ln -fsn --relative $< $@
+cmd_symlink_quiet = mkdir -p $(dir $@) ; ln -fsn --relative $< $@
+
quiet_cmd_check_size = CHKSIZE $2
cmd_check_size = set -e; \
size=`printf "%d" $2`; \
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 06/21] kbuild: allow customizing barebox proper binary
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (4 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 05/21] kbuild: collect compatibility symlink creation in symlink-y Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 07/21] sandbox: make available all CONFIG_ symbols to OS glue code Ahmad Fatoum
` (14 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We usually objcopy barebox proper into a linear binary, but for sandbox,
we are going to rely on the OS to do loading and relocation, so let's
make it possible, which BAREBOX_PROPER binary should be the target.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Makefile | 10 ++++++----
images/Makefile | 2 +-
2 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
index ecaf195b726d..8fbe9e9622c2 100644
--- a/Makefile
+++ b/Makefile
@@ -815,18 +815,20 @@ export KBUILD_BINARY ?= barebox.bin
# Also any assignments in arch/$(SRCARCH)/Makefile take precedence over
# the default value.
+export BAREBOX_PROPER ?= barebox.bin
+
barebox-flash-images: $(KBUILD_IMAGE)
@echo $^ > $@
-images: barebox.bin FORCE
+images: $(BAREBOX_PROPER) FORCE
$(Q)$(MAKE) $(build)=images $@
-images/%: barebox.bin FORCE
+images/%: $(BAREBOX_PROPER) FORCE
$(Q)$(MAKE) $(build)=images $@
ifdef CONFIG_PBL_IMAGE
SYMLINK_TARGET_barebox.efi = images/barebox-dt-2nd.img
symlink-$(CONFIG_EFI_STUB) += barebox.efi
-all: barebox.bin images
+all: $(BAREBOX_PROPER) images
else
SYMLINK_TARGET_barebox-flash-image = $(KBUILD_IMAGE)
symlink-y += barebox-flash-image
@@ -1030,7 +1032,7 @@ ifeq ($(INSTALL_PATH),)
endif
ifdef CONFIG_PBL_IMAGE
$(Q)$(MAKE) $(build)=images __images_install
- @install -t "$(INSTALL_PATH)" barebox.bin
+ @install -t "$(INSTALL_PATH)" $(BAREBOX_PROPER)
else
@install -t "$(INSTALL_PATH)" $(KBUILD_IMAGE)
endif
diff --git a/images/Makefile b/images/Makefile
index ca37c054ae9c..40400a95a935 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -143,7 +143,7 @@ $(obj)/barebox.sum: $(obj)/barebox.z FORCE
# barebox.z - compressed barebox binary
# ----------------------------------------------------------------
-$(obj)/barebox.z: $(obj)/../barebox.bin FORCE
+$(obj)/barebox.z: $(obj)/../$(BAREBOX_PROPER) FORCE
$(call if_changed,$(suffix_y))
# %.img - create a copy from another file
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 07/21] sandbox: make available all CONFIG_ symbols to OS glue code
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (5 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 06/21] kbuild: allow customizing barebox proper binary Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 08/21] sandbox: switch to using PBL Ahmad Fatoum
` (13 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
Having to specify which config options to pass through is surprising and
led to issues in the past. Let's thus just pass along everything.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
arch/sandbox/os/Makefile | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/arch/sandbox/os/Makefile b/arch/sandbox/os/Makefile
index a71446775671..9fb2a2ce864d 100644
--- a/arch/sandbox/os/Makefile
+++ b/arch/sandbox/os/Makefile
@@ -6,11 +6,7 @@ machdirs := $(patsubst %,arch/sandbox/mach-%/,$(machine-y))
KBUILD_CPPFLAGS = $(patsubst %,-I$(srctree)/%include,$(machdirs))
-cppflags-$(CONFIG_CONSOLE_NONE) += -DCONFIG_CONSOLE_NONE=$(CONFIG_CONSOLE_NONE)
-cppflags-$(CONFIG_ASAN) += -DCONFIG_ASAN=$(CONFIG_ASAN)
-
-KBUILD_CPPFLAGS += -DCONFIG_MALLOC_SIZE=$(CONFIG_MALLOC_SIZE) -D_FILE_OFFSET_BITS=64 \
- -DCONFIG_STACK_SIZE=$(CONFIG_STACK_SIZE) $(cppflags-y)
+KBUILD_CPPFLAGS += -D_FILE_OFFSET_BITS=64 -include $(objtree)/include/generated/autoconf.h
KBUILD_CFLAGS := -Wall
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 08/21] sandbox: switch to using PBL
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (6 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 07/21] sandbox: make available all CONFIG_ symbols to OS glue code Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 09/21] kbuild: populate non-host CXX variables Ahmad Fatoum
` (12 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
The approach we inherited from Linux um doesn't scale well, because it
requires us to mask via macro every symbol that we define that clashes
with an external library and there are a lot of them in barebox due to
the minimal UNIX-like libc we have.
This clashing symbols are all in barebox proper, so by enabling PBL, we
can just leave PBL access to external symbols and link barebox proper,
so it has no references to external symbols.
That way we only need to list functions that are called between PBL and
proper and if we don't we get an error message.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Makefile | 16 ++++-
arch/sandbox/Kconfig | 4 ++
arch/sandbox/Makefile | 70 +++++++++----------
arch/sandbox/board/.gitignore | 3 -
arch/sandbox/board/Makefile | 2 -
arch/sandbox/include/asm/barebox-sandbox.h | 10 +++
arch/sandbox/lib/.gitignore | 3 +
arch/sandbox/lib/Makefile | 2 +-
.../{board/barebox.lds.S => lib/pbl.lds.S} | 0
arch/sandbox/os/Makefile | 18 ++---
arch/sandbox/os/common.c | 3 +-
arch/sandbox/{lib => os}/unwind.c | 3 +-
arch/x86/um/Makefile | 4 +-
common/Kconfig | 2 +-
images/.gitignore | 1 +
images/Makefile | 1 +
images/Makefile.sandbox | 14 ++++
pbl/Kconfig | 2 +-
18 files changed, 99 insertions(+), 59 deletions(-)
create mode 100644 arch/sandbox/include/asm/barebox-sandbox.h
create mode 100644 arch/sandbox/lib/.gitignore
rename arch/sandbox/{board/barebox.lds.S => lib/pbl.lds.S} (100%)
rename arch/sandbox/{lib => os}/unwind.c (88%)
create mode 100644 images/Makefile.sandbox
diff --git a/Makefile b/Makefile
index 8fbe9e9622c2..6f8e1a312060 100644
--- a/Makefile
+++ b/Makefile
@@ -929,6 +929,7 @@ quiet_cmd_sysmap = SYSMAP System.map
# If CONFIG_KALLSYMS is set .version is already updated
# Generate System.map and verify that the content is consistent
# Use + in front of the barebox_version rule to silent warning with make -j2
+ifndef rule_barebox__
define rule_barebox__
$(if $(CONFIG_KALLSYMS),,+$(call cmd,barebox_version))
$(call cmd,barebox__)
@@ -936,6 +937,7 @@ define rule_barebox__
$(call cmd,prelink__)
$(call cmd,sysmap)
endef
+endif
ifdef CONFIG_KALLSYMS
# Generate section listing all symbols and add it into barebox $(kallsyms.o)
@@ -1050,6 +1052,18 @@ barebox.fit: images/barebox-$(CONFIG_ARCH_LINUX_NAME).fit
barebox.srec: barebox
$(OBJCOPY) -O srec $< $@
+quiet_cmd_barebox_proper__ = CC $@
+ cmd_barebox_proper__ = $(CC) -r -o $@ -Wl,--whole-archive $(BAREBOX_OBJS)
+
+.tmp_barebox.o: $(BAREBOX_OBJS) $(kallsyms.o) FORCE
+ $(if $(CONFIG_KALLSYMS),,+$(call cmd,barebox_version))
+ $(call cmd,barebox_proper__)
+ $(Q)echo 'savedcmd_$@ := $(cmd_barebox_proper__)' > $(@D)/.$(@F).cmd
+ $(Q)rm -f .old_version
+
+barebox.o: .tmp_barebox.o FORCE
+ $(call if_changed,objcopy)
+
# The actual objects are generated when descending,
# make sure no implicit rule kicks in
$(sort $(BAREBOX_OBJS)) $(BAREBOX_LDS) $(BAREBOX_PBL_OBJS): $(barebox-dirs) ;
@@ -1269,7 +1283,7 @@ CLEAN_DIRS += $(MODVERDIR)
CLEAN_FILES += barebox System.map include/generated/barebox_default_env.h \
.tmp_version .tmp_barebox* barebox.bin barebox.map \
.tmp_kallsyms* barebox.ldr compile_commands.json \
- barebox-flash-image \
+ .tmp_barebox.o barebox.o barebox-flash-image \
barebox.srec barebox.s5p barebox.ubl \
barebox.uimage \
barebox.efi barebox.canon-a1100.bin
diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index b48eb29c7db4..4eebca6748b3 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -21,6 +21,10 @@ config SANDBOX
select ARCH_WANT_FRAME_POINTERS
select BAREBOX_CMDLINE
select HAVE_ARCH_BOOTM_OFTREE
+ select HAVE_PBL_IMAGE
+ select HAVE_PBL_MULTI_IMAGES
+ select PBL_IMAGE_NO_PIGGY
+ select PBL_CLOCKSOURCE if COMPILE_TEST
default y
config ARCH_LINUX_NAME
diff --git a/arch/sandbox/Makefile b/arch/sandbox/Makefile
index dbdf8a6e8117..2db1cb648a2b 100644
--- a/arch/sandbox/Makefile
+++ b/arch/sandbox/Makefile
@@ -17,7 +17,6 @@ machine-y := sandbox
board-y := arch/sandbox/board
BOARD := $(board-y)/
-lds-y := $(BOARD)/barebox.lds
HEADER_ARCH := $(SUBARCH)
HOST_DIR := arch/$(HEADER_ARCH)
@@ -29,28 +28,12 @@ common-y += $(HOST_DIR)/um/
KBUILD_CPPFLAGS += -I$(srctree)/$(HOST_DIR)/include
TEXT_BASE = $(CONFIG_TEXT_BASE)
-KBUILD_CFLAGS += -Dmalloc=barebox_malloc -Dcalloc=barebox_calloc \
- -Dmalloc_stats=barebox_malloc_stats -Dmemalign=barebox_memalign \
- -Dmalloc_usable_size=barebox_malloc_usable_size \
- -Dfree=barebox_free -Drealloc=barebox_realloc \
- -Dbrk=barebox_brk -Dsbrk=barebox_sbrk \
- -Dread=barebox_read -Dwrite=barebox_write \
- -Dopen=barebox_open -Dclose=barebox_close \
- -Dlseek=barebox_lseek -Dperror=barebox_perror \
- -Derrno=barebox_errno -Dgetc=barebox_getc \
- -Dputc=barebox_putc -Dfgetc=barebox_fgetc \
- -Dfputc=barebox_fputc -Dfgets=barebox_fgets \
- -Dfputs=barebox_fputs -Dsetenv=barebox_setenv \
- -Dgetenv=barebox_getenv -Dprintf=barebox_printf \
- -Dglob=barebox_glob -Dglobfree=barebox_globfree \
- -Dioctl=barebox_ioctl -Dfstat=barebox_fstat \
- -Dftruncate=barebox_ftruncate -Dasprintf=barebox_asprintf \
- -Dopendir=barebox_opendir -Dreaddir=barebox_readdir \
- -Dclosedir=barebox_closedir -Dreadlink=barebox_readlink \
- -Doptarg=barebox_optarg -Doptind=barebox_optind \
- -Dsetjmp=barebox_setjmp -Dlongjmp=barebox_longjmp \
- -Dmkdir=barebox_mkdir -Ddirname=barebox_dirname \
- -Dremove=barebox_remove -Dputchar=barebox_putchar
+
+SANDBOX_PBL2PROPER_GLUE_SYMS := \
+ putchar errno setjmp longjmp \
+ malloc_stats memalign malloc free realloc calloc brk sbrk
+
+KBUILD_CFLAGS += $(foreach s,$(SANDBOX_PBL2PROPER_GLUE_SYMS),-D$(s)=barebox_$(s))
machdirs := $(patsubst %,arch/sandbox/mach-%/,$(machine-y))
@@ -61,25 +44,27 @@ archprepare: maketools
PHONY += maketools
+export SANDBOX_LIBS BAREBOX_LDFLAGS
+
ifeq ($(CONFIG_SDL),y)
-SDL_LIBS := $(shell $(CROSS_PKG_CONFIG) sdl2 --libs)
+SANDBOX_LIBS += $(shell $(CROSS_PKG_CONFIG) sdl2 --libs)
endif
ifeq ($(CONFIG_GPIO_LIBFTDI1),y)
-FTDI1_LIBS := $(shell $(CROSS_PKG_CONFIG) libftdi1 --libs)
+SANDBOX_LIBS += $(shell $(CROSS_PKG_CONFIG) libftdi1 --libs)
endif
ifeq ($(CONFIG_ASAN),y)
KBUILD_CPPFLAGS += -fsanitize=address
-SANITIZER_LIBS += -fsanitize=address
+SANDBOX_LIBS += -fsanitize=address
ifeq ($(CONFIG_CC_IS_CLANG),y)
KBUILD_CPPFLAGS += -fno-sanitize-address-globals-dead-stripping
-BAREBOX_LDFLAGS += -fno-sanitize-address-globals-dead-stripping
+SANDBOX_LIBS += -fno-sanitize-address-globals-dead-stripping
endif
endif
ifeq ($(CONFIG_UBSAN),y)
-SANITIZER_LIBS += -fsanitize=undefined
+SANDBOX_LIBS += -fsanitize=undefined
endif
ifeq ($(CONFIG_SANDBOX_LINUX_I386),y)
@@ -89,17 +74,28 @@ KBUILD_AFLAGS += -m32
BAREBOX_LDFLAGS += -m32
endif
-BAREBOX_LDFLAGS += \
- -Wl,-T,$(BAREBOX_LDS) \
- -Wl,--whole-archive $(BAREBOX_OBJS) -Wl,--no-whole-archive \
- -lrt -pthread $(SDL_LIBS) $(FTDI1_LIBS) \
- $(SANITIZER_LIBS)
-
-cmd_barebox__ = $(CC) -o $@ $(BAREBOX_LDFLAGS)
-
common-y += $(BOARD) arch/sandbox/os/ arch/sandbox/lib/
-KBUILD_IMAGE := barebox
+SANDBOX_PROPER2PBL_GLUE_SYMS := \
+ strsep_unescaped start_barebox linux_get_stickypage_path \
+ stickypage mem_malloc_init \
+ barebox_register_filedev barebox_register_dtb barebox_register_console \
+ barebox_errno barebox_loglevel
+
+OBJCOPYFLAGS_barebox.o := $(addprefix --keep-global-symbol=, $(SANDBOX_PROPER2PBL_GLUE_SYMS))
+
+quiet_cmd_barebox__ = $(quiet_cmd_symlink)
+ cmd_barebox__ = ln -fsn --relative $(objtree)/images/barebox $(objtree)/barebox
+
+define rule_barebox__
+ $(call cmd,barebox__)
+ $(Q)echo 'savedcmd_$@ := $(cmd_barebox__)' > $(@D)/.$(@F).cmd
+endef
+
+all: barebox
+
+KBUILD_IMAGE := barebox.o
+BAREBOX_PROPER := $(KBUILD_IMAGE)
common-$(CONFIG_OFTREE) += arch/sandbox/dts/
diff --git a/arch/sandbox/board/.gitignore b/arch/sandbox/board/.gitignore
index 03987a700942..e69de29bb2d1 100644
--- a/arch/sandbox/board/.gitignore
+++ b/arch/sandbox/board/.gitignore
@@ -1,3 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-
-barebox.lds
diff --git a/arch/sandbox/board/Makefile b/arch/sandbox/board/Makefile
index 2b6346f6098d..b351504f11dd 100644
--- a/arch/sandbox/board/Makefile
+++ b/arch/sandbox/board/Makefile
@@ -14,6 +14,4 @@ obj-$(CONFIG_CMD_SANDBOX_CPUINFO) += cpuinfo.o
obj-$(CONFIG_LED) += led.o
bbenv-y += defaultenv-sandbox
-extra-y += barebox.lds
-
obj-y += stickypage.o
diff --git a/arch/sandbox/include/asm/barebox-sandbox.h b/arch/sandbox/include/asm/barebox-sandbox.h
new file mode 100644
index 000000000000..c029f9ea0086
--- /dev/null
+++ b/arch/sandbox/include/asm/barebox-sandbox.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __ASM_BAREBOX_SANDBOX_H__
+#define __ASM_BAREBOX_SANDBOX_H__
+
+#define ENTRY_FUNCTION(name, argc, argv) \
+ int name(int argc, char *argv[]); \
+ int name(int argc, char *argv[])
+
+#endif
diff --git a/arch/sandbox/lib/.gitignore b/arch/sandbox/lib/.gitignore
new file mode 100644
index 000000000000..03987a700942
--- /dev/null
+++ b/arch/sandbox/lib/.gitignore
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+barebox.lds
diff --git a/arch/sandbox/lib/Makefile b/arch/sandbox/lib/Makefile
index 9535305d5bfa..1c2b374603ce 100644
--- a/arch/sandbox/lib/Makefile
+++ b/arch/sandbox/lib/Makefile
@@ -1,3 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
-obj-$(CONFIG_ARCH_HAS_STACK_DUMP) += unwind.o
+obj- := __dummy__.o
diff --git a/arch/sandbox/board/barebox.lds.S b/arch/sandbox/lib/pbl.lds.S
similarity index 100%
rename from arch/sandbox/board/barebox.lds.S
rename to arch/sandbox/lib/pbl.lds.S
diff --git a/arch/sandbox/os/Makefile b/arch/sandbox/os/Makefile
index 9fb2a2ce864d..b305a142379e 100644
--- a/arch/sandbox/os/Makefile
+++ b/arch/sandbox/os/Makefile
@@ -4,7 +4,8 @@ machine-y := sandbox
machdirs := $(patsubst %,arch/sandbox/mach-%/,$(machine-y))
-KBUILD_CPPFLAGS = $(patsubst %,-I$(srctree)/%include,$(machdirs))
+KBUILD_CPPFLAGS = $(patsubst %,-I$(srctree)/%include,$(machdirs)) \
+ -I$(srctree)/arch/sandbox/include
KBUILD_CPPFLAGS += -D_FILE_OFFSET_BITS=64 -include $(objtree)/include/generated/autoconf.h
@@ -16,12 +17,13 @@ ifeq ($(CONFIG_SANDBOX_LINUX_I386),y)
KBUILD_CFLAGS += -m32
endif
-obj-y = common.o setjmp.o
-obj-$(CONFIG_DRIVER_NET_TAP) += tap.o
-obj-$(CONFIG_MALLOC_LIBC) += libc_malloc.o
+pbl-y = common.o setjmp.o
+pbl-$(CONFIG_DRIVER_NET_TAP) += tap.o
+pbl-$(CONFIG_MALLOC_LIBC) += libc_malloc.o
+pbl-$(CONFIG_ARCH_HAS_STACK_DUMP) += unwind.o
-CFLAGS_sdl.o = $(shell $(CROSS_PKG_CONFIG) sdl2 --cflags)
-obj-$(CONFIG_SDL) += sdl.o
+CFLAGS_sdl.pbl.o = $(shell $(CROSS_PKG_CONFIG) sdl2 --cflags)
+pbl-$(CONFIG_SDL) += sdl.o
-CFLAGS_ftdi.o = $(shell $(CROSS_PKG_CONFIG) libftdi1 --cflags)
-obj-$(CONFIG_GPIO_LIBFTDI1) += ftdi.o
+CFLAGS_ftdi.pbl.o = $(shell $(CROSS_PKG_CONFIG) libftdi1 --cflags)
+pbl-$(CONFIG_GPIO_LIBFTDI1) += ftdi.o
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index ef39f5336d60..3dbe61791ccd 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -46,6 +46,7 @@
*/
#include <mach/linux.h>
#include <mach/hostfile.h>
+#include <asm/barebox-sandbox.h>
#ifdef CONFIG_CONSOLE_NONE
int __attribute__((unused)) barebox_loglevel;
@@ -534,7 +535,7 @@ static struct option long_options[] = {
static const char optstring[] = "hm:i:c:e:d:O:I:B:x:y:";
-int main(int argc, char *argv[])
+ENTRY_FUNCTION(sandbox_main, argc, argv)
{
void *ram;
int opt, ret, fd, fd2;
diff --git a/arch/sandbox/lib/unwind.c b/arch/sandbox/os/unwind.c
similarity index 88%
rename from arch/sandbox/lib/unwind.c
rename to arch/sandbox/os/unwind.c
index f46365ac2b3b..d1608d075889 100644
--- a/arch/sandbox/lib/unwind.c
+++ b/arch/sandbox/os/unwind.c
@@ -1,10 +1,9 @@
// SPDX-License-Identifier: GPL-2.0-only
// Copyright 2019 Ahmad Fatoum
-#include <common.h>
-
void __sanitizer_print_stack_trace(void);
+void dump_stack(void);
void dump_stack(void)
{
__sanitizer_print_stack_trace();
diff --git a/arch/x86/um/Makefile b/arch/x86/um/Makefile
index d3764221a4c6..fc0442b78916 100644
--- a/arch/x86/um/Makefile
+++ b/arch/x86/um/Makefile
@@ -8,5 +8,5 @@ else
BITS := 64
endif
-AFLAGS_../lib/setjmp_$(BITS).o := -Dsetjmp=barebox_setjmp -Dlongjmp=barebox_longjmp
-obj-$(CONFIG_SANDBOX_SJLJ_ASM) += ../lib/setjmp_$(BITS).o
+AFLAGS_../lib/setjmp_$(BITS).pbl.o := -Dsetjmp=barebox_setjmp -Dlongjmp=barebox_longjmp
+pbl-$(CONFIG_SANDBOX_SJLJ_ASM) += ../lib/setjmp_$(BITS).o
diff --git a/common/Kconfig b/common/Kconfig
index fba6db3ab869..6d41b7048a32 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -965,7 +965,7 @@ config CONSOLE_DISABLE_INPUT
registered as consoles.
config PBL_CONSOLE
- depends on PBL_IMAGE
+ depends on PBL_IMAGE && !SANDBOX
depends on !CONSOLE_NONE
bool "Enable console support in PBL"
help
diff --git a/images/.gitignore b/images/.gitignore
index 8d5bd4a410cf..20133074bf87 100644
--- a/images/.gitignore
+++ b/images/.gitignore
@@ -23,6 +23,7 @@
*.mxssd
*.csfbin
pbl.lds
+barebox
barebox.x
barebox.z
barebox.sha.bin
diff --git a/images/Makefile b/images/Makefile
index 40400a95a935..e20d11e1501a 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -176,6 +176,7 @@ include $(srctree)/images/Makefile.mvebu
include $(srctree)/images/Makefile.mxs
include $(srctree)/images/Makefile.omap3
include $(srctree)/images/Makefile.rockchip
+include $(srctree)/images/Makefile.sandbox
include $(srctree)/images/Makefile.socfpga
include $(srctree)/images/Makefile.stm32mp
include $(srctree)/images/Makefile.tegra
diff --git a/images/Makefile.sandbox b/images/Makefile.sandbox
new file mode 100644
index 000000000000..0699a52663ef
--- /dev/null
+++ b/images/Makefile.sandbox
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+SYMLINK_TARGET_barebox = sandbox_main.elf
+symlink-$(CONFIG_SANDBOX) += barebox
+
+ifeq ($(CONFIG_SANDBOX),y)
+
+quiet_cmd_elf__ = LD $@
+ cmd_elf__ = $(CC) -o $@ $(BAREBOX_LDFLAGS) \
+ -Wl,-T,$(pbl-lds) -Wl,--defsym=main=$(2) -Wl,--whole-archive \
+ $(obj)/../$(BAREBOX_PROPER) $(BAREBOX_PBL_OBJS) -Wl,--no-whole-archive \
+ -lrt -pthread $(SANDBOX_LIBS) $(LDFLAGS_$(@F))
+
+endif
diff --git a/pbl/Kconfig b/pbl/Kconfig
index e9fa35e29b19..cab9325d16e8 100644
--- a/pbl/Kconfig
+++ b/pbl/Kconfig
@@ -45,7 +45,7 @@ config USE_COMPRESSED_DTB
select XZ_DECOMPRESS if IMAGE_COMPRESSION_XZKERN
config PBL_RELOCATABLE
- depends on ARM || MIPS || RISCV
+ depends on ARM || MIPS || RISCV || SANDBOX
bool "relocatable pbl image"
help
Generate a pbl binary which can relocate itself during startup to run
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 09/21] kbuild: populate non-host CXX variables
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (7 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 08/21] sandbox: switch to using PBL Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 10/21] string: add fortify source support Ahmad Fatoum
` (11 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
To be able to link statically against libfuzzer, we will need to use the
C++ compiler as linker. Prepare for that by populating CXX variables.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Makefile | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index 6f8e1a312060..df0770e832e1 100644
--- a/Makefile
+++ b/Makefile
@@ -421,6 +421,7 @@ ENVCC := $(CC)
CPP = $(CC) -E
ifneq ($(LLVM),)
CC = $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
+CXX = $(LLVM_PREFIX)clang++$(LLVM_SUFFIX)
LD = $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)
AR = $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
NM = $(LLVM_PREFIX)llvm-nm$(LLVM_SUFFIX)
@@ -430,6 +431,7 @@ READELF = $(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)
STRIP = $(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)
else
CC = $(CROSS_COMPILE)gcc
+CXX = $(CROSS_COMPILE)g++
LD = $(CROSS_COMPILE)ld
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
@@ -514,7 +516,7 @@ LDFLAGS_barebox += $(LDFLAGS_common)
LDFLAGS_pbl += $(LDFLAGS_common)
LDFLAGS_elf += $(LDFLAGS_common) --nmagic -s
-export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC
+export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC CXX
export CPP AR NM STRIP OBJCOPY OBJDUMP MAKE AWK GENKSYMS PERL PYTHON3 UTS_MACHINE
export LEX YACC
export HOSTCXX CHECK CHECKFLAGS MKIMAGE
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 10/21] string: add fortify source support
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (8 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 09/21] kbuild: populate non-host CXX variables Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 11/21] sandbox: populate UNAME_M variable Ahmad Fatoum
` (10 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
FORTIFY_SOURCE provides lightweight support for detecting buffer overflows
in various functions that perform operations on memory and strings.
This should be in future extended to all architectures, so let's start
by enabling it for sandbox for use during fuzz tests.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
arch/Kconfig | 6 +
arch/sandbox/Kconfig | 1 +
commands/stacksmash.c | 6 +-
include/linux/compiler_types.h | 41 ++
include/linux/fortify-string.h | 804 +++++++++++++++++++++++++++++++++
include/linux/string.h | 17 +
lib/Kconfig.hardening | 15 +
lib/Makefile | 2 +-
lib/string.c | 16 +-
lib/string_helpers.c | 30 ++
pbl/string.c | 1 +
11 files changed, 925 insertions(+), 14 deletions(-)
create mode 100644 include/linux/fortify-string.h
create mode 100644 lib/string_helpers.c
diff --git a/arch/Kconfig b/arch/Kconfig
index 890923ad059c..446c4b8fd960 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -116,3 +116,9 @@ config HAVE_ARCH_KASAN
config ARCH_HAS_UBSAN_SANITIZE_ALL
bool
+
+config ARCH_HAS_FORTIFY_SOURCE
+ bool
+ help
+ An architecture should select this when it can successfully
+ build and run with CONFIG_FORTIFY_SOURCE.
diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index 4eebca6748b3..7d8f88474781 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -7,6 +7,7 @@ config SANDBOX
select OFTREE
select GPIOLIB
select ARCH_HAS_UBSAN_SANITIZE_ALL
+ select ARCH_HAS_FORTIFY_SOURCE
select HAS_DMA
select BLOCK
select BLOCK_WRITE
diff --git a/commands/stacksmash.c b/commands/stacksmash.c
index e200fa5a1cbb..15c925d05f37 100644
--- a/commands/stacksmash.c
+++ b/commands/stacksmash.c
@@ -6,6 +6,8 @@
#include <linux/compiler.h>
#include <string.h>
+#define NO_FORTIFY_SOURCE "We want to test stack protector, not FORTIFY_SOURCE"
+
static noinline void stack_overflow_frame(void)
{
volatile int length = 512;
@@ -17,10 +19,10 @@ static noinline void stack_overflow_frame(void)
*/
OPTIMIZER_HIDE_VAR(length);
- memset(a, 0xa5, length);
+ unsafe_memset(a, 0xa5, length, NO_FORTIFY_SOURCE);
printf("We have smashed our stack as this should not exceed 128: sizeof(a) = %zu\n",
- strlen(a));
+ unsafe_strlen(a, NO_FORTIFY_SOURCE));
}
static noinline void stack_overflow_region(u64 i)
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 87ab117aca13..fcdf83cec195 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -124,6 +124,24 @@ struct ftrace_likely_data {
#define __has_attribute(...) 0
#endif
+/*
+ * Note: the "type" argument should match any __builtin_object_size(p, type) usage.
+ *
+ * Optional: not supported by gcc.
+ *
+ * clang: https://clang.llvm.org/docs/AttributeReference.html#pass-object-size-pass-dynamic-object-size
+ */
+#if __has_attribute(__pass_dynamic_object_size__)
+# define __pass_dynamic_object_size(type) __attribute__((__pass_dynamic_object_size__(type)))
+#else
+# define __pass_dynamic_object_size(type)
+#endif
+#if __has_attribute(__pass_object_size__)
+# define __pass_object_size(type) __attribute__((__pass_object_size__(type)))
+#else
+# define __pass_object_size(type)
+#endif
+
/*
* Add the pseudo keyword 'fallthrough' so case statement blocks
* must end with any of these keywords:
@@ -161,6 +179,29 @@ struct ftrace_likely_data {
#define __returns_nonnull
#endif
+/*
+ * Optional: not supported by gcc.
+ *
+ * clang: https://clang.llvm.org/docs/AttributeReference.html#overloadable
+ */
+#if __has_attribute(__overloadable__)
+# define __overloadable __attribute__((__overloadable__))
+#else
+# define __overloadable
+#endif
+
+/*
+ * Optional: not supported by gcc
+ * Optional: only supported since clang >= 14.0
+ *
+ * clang: https://clang.llvm.org/docs/AttributeReference.html#diagnose_as_builtin
+ */
+#if __has_attribute(__diagnose_as_builtin__)
+# define __diagnose_as(builtin...) __attribute__((__diagnose_as_builtin__(builtin)))
+#else
+# define __diagnose_as(builtin...)
+#endif
+
#endif /* __KERNEL__ */
#endif /* __ASSEMBLY__ */
diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
new file mode 100644
index 000000000000..ff2618c9c339
--- /dev/null
+++ b/include/linux/fortify-string.h
@@ -0,0 +1,804 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_FORTIFY_STRING_H_
+#define _LINUX_FORTIFY_STRING_H_
+
+#include <linux/bug.h>
+#include <linux/const.h>
+#include <linux/limits.h>
+#include <linux/stringify.h>
+
+#define __FORTIFY_INLINE extern __always_inline __gnu_inline __overloadable
+#define __RENAME(x) __asm__(#x)
+
+void fortify_panic(const char *name) __noreturn __cold;
+void __read_overflow(void) __compiletime_error("detected read beyond size of object (1st parameter)");
+void __read_overflow2(void) __compiletime_error("detected read beyond size of object (2nd parameter)");
+void __read_overflow2_field(size_t avail, size_t wanted) __compiletime_warning("detected read beyond size of field (2nd parameter); maybe use struct_group()?");
+void __write_overflow(void) __compiletime_error("detected write beyond size of object (1st parameter)");
+void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning("detected write beyond size of field (1st parameter); maybe use struct_group()?");
+
+#define __compiletime_strlen(p) \
+({ \
+ char *__p = (char *)(p); \
+ size_t __ret = SIZE_MAX; \
+ const size_t __p_size = __member_size(p); \
+ if (__p_size != SIZE_MAX && \
+ __builtin_constant_p(*__p)) { \
+ size_t __p_len = __p_size - 1; \
+ if (__builtin_constant_p(__p[__p_len]) && \
+ __p[__p_len] == '\0') \
+ __ret = __builtin_strlen(__p); \
+ } \
+ __ret; \
+})
+
+#if defined(__SANITIZE_ADDRESS__)
+extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
+extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
+extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
+extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
+extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
+extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
+extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
+extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
+extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
+extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
+#else
+
+#define __underlying_memcpy __builtin_memcpy
+#define __underlying_memmove __builtin_memmove
+#define __underlying_memset __builtin_memset
+
+#define __underlying_memchr __builtin_memchr
+#define __underlying_memcmp __builtin_memcmp
+#define __underlying_strcat __builtin_strcat
+#define __underlying_strcpy __builtin_strcpy
+#define __underlying_strlen __builtin_strlen
+#define __underlying_strncat __builtin_strncat
+#define __underlying_strncpy __builtin_strncpy
+#endif
+
+/**
+ * unsafe_memcpy - memcpy implementation with no FORTIFY bounds checking
+ *
+ * @dst: Destination memory address to write to
+ * @src: Source memory address to read from
+ * @bytes: How many bytes to write to @dst from @src
+ * @justification: Free-form text or comment describing why the use is needed
+ *
+ * This should be used for corner cases where the compiler cannot do the
+ * right thing, or during transitions between APIs, etc. It should be used
+ * very rarely, and includes a place for justification detailing where bounds
+ * checking has happened, and why existing solutions cannot be employed.
+ */
+#define unsafe_memcpy(dst, src, bytes, justification) \
+ __underlying_memcpy(dst, src, bytes)
+
+#define unsafe_memset(dst, val, size, justification) \
+ __underlying_memset(dst, val, size)
+
+#define unsafe_strlen(str, justification) \
+ __underlying_strlen(str)
+
+/*
+ * Clang's use of __builtin_*object_size() within inlines needs hinting via
+ * __pass_*object_size(). The preference is to only ever use type 1 (member
+ * size, rather than struct size), but there remain some stragglers using
+ * type 0 that will be converted in the future.
+ */
+#if __has_builtin(__builtin_dynamic_object_size)
+#define POS __pass_dynamic_object_size(1)
+#define POS0 __pass_dynamic_object_size(0)
+#define __struct_size(p) __builtin_dynamic_object_size(p, 0)
+#define __member_size(p) __builtin_dynamic_object_size(p, 1)
+#else
+#define POS __pass_object_size(1)
+#define POS0 __pass_object_size(0)
+#define __struct_size(p) __builtin_object_size(p, 0)
+#define __member_size(p) __builtin_object_size(p, 1)
+#endif
+
+#define __compiletime_lessthan(bounds, length) ( \
+ __builtin_constant_p((bounds) < (length)) && \
+ (bounds) < (length) \
+)
+
+/**
+ * strncpy - Copy a string to memory with non-guaranteed NUL padding
+ *
+ * @p: pointer to destination of copy
+ * @q: pointer to NUL-terminated source string to copy
+ * @size: bytes to write at @p
+ *
+ * If strlen(@q) >= @size, the copy of @q will stop after @size bytes,
+ * and @p will NOT be NUL-terminated
+ *
+ * If strlen(@q) < @size, following the copy of @q, trailing NUL bytes
+ * will be written to @p until @size total bytes have been written.
+ *
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
+ * over-reads of @q, it cannot defend against writing unterminated
+ * results to @p. Using strncpy() remains ambiguous and fragile.
+ * Instead, please choose an alternative, so that the expectation
+ * of @p's contents is unambiguous:
+ *
+ * +--------------------+--------------------+------------+
+ * | **p** needs to be: | padded to **size** | not padded |
+ * +====================+====================+============+
+ * | NUL-terminated | strscpy_pad() | strscpy() |
+ * +--------------------+--------------------+------------+
+ * | not NUL-terminated | strtomem_pad() | strtomem() |
+ * +--------------------+--------------------+------------+
+ *
+ * Note strscpy*()'s differing return values for detecting truncation,
+ * and strtomem*()'s expectation that the destination is marked with
+ * __nonstring when it is a character array.
+ *
+ */
+__FORTIFY_INLINE __diagnose_as(__builtin_strncpy, 1, 2, 3)
+char *strncpy(char * const POS p, const char *q, __kernel_size_t size)
+{
+ const size_t p_size = __member_size(p);
+
+ if (__compiletime_lessthan(p_size, size))
+ __write_overflow();
+ if (p_size < size)
+ fortify_panic(__func__);
+ return __underlying_strncpy(p, q, size);
+}
+
+extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
+/**
+ * strnlen - Return bounded count of characters in a NUL-terminated string
+ *
+ * @p: pointer to NUL-terminated string to count.
+ * @maxlen: maximum number of characters to count.
+ *
+ * Returns number of characters in @p (NOT including the final NUL), or
+ * @maxlen, if no NUL has been found up to there.
+ *
+ */
+__FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size_t maxlen)
+{
+ const size_t p_size = __member_size(p);
+ const size_t p_len = __compiletime_strlen(p);
+ size_t ret;
+
+ /* We can take compile-time actions when maxlen is const. */
+ if (__builtin_constant_p(maxlen) && p_len != SIZE_MAX) {
+ /* If p is const, we can use its compile-time-known len. */
+ if (maxlen >= p_size)
+ return p_len;
+ }
+
+ /* Do not check characters beyond the end of p. */
+ ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
+ if (p_size <= ret && maxlen != ret)
+ fortify_panic(__func__);
+ return ret;
+}
+
+/*
+ * Defined after fortified strnlen to reuse it. However, it must still be
+ * possible for strlen() to be used on compile-time strings for use in
+ * static initializers (i.e. as a constant expression).
+ */
+/**
+ * strlen - Return count of characters in a NUL-terminated string
+ *
+ * @p: pointer to NUL-terminated string to count.
+ *
+ * Do not use this function unless the string length is known at
+ * compile-time. When @p is unterminated, this function may crash
+ * or return unexpected counts that could lead to memory content
+ * exposures. Prefer strnlen().
+ *
+ * Returns number of characters in @p (NOT including the final NUL).
+ *
+ */
+#define strlen(p) \
+ __builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \
+ __builtin_strlen(p), __fortify_strlen(p))
+__FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1)
+__kernel_size_t __fortify_strlen(const char * const POS p)
+{
+ const size_t p_size = __member_size(p);
+ __kernel_size_t ret;
+
+ /* Give up if we don't know how large p is. */
+ if (p_size == SIZE_MAX)
+ return __underlying_strlen(p);
+ ret = strnlen(p, p_size);
+ if (p_size <= ret)
+ fortify_panic(__func__);
+ return ret;
+}
+
+/* Defined after fortified strlen() to reuse it. */
+extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
+/**
+ * strlcpy - Copy a string into another string buffer
+ *
+ * @p: pointer to destination of copy
+ * @q: pointer to NUL-terminated source string to copy
+ * @size: maximum number of bytes to write at @p
+ *
+ * If strlen(@q) >= @size, the copy of @q will be truncated at
+ * @size - 1 bytes. @p will always be NUL-terminated.
+ *
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
+ * over-reads when calculating strlen(@q), it is still possible.
+ * Prefer strscpy(), though note its different return values for
+ * detecting truncation.
+ *
+ * Returns total number of bytes written to @p, including terminating NUL.
+ *
+ */
+__FORTIFY_INLINE size_t strlcpy(char * const POS p, const char * const POS q, size_t size)
+{
+ const size_t p_size = __member_size(p);
+ const size_t q_size = __member_size(q);
+ size_t q_len; /* Full count of source string length. */
+ size_t len; /* Count of characters going into destination. */
+
+ if (p_size == SIZE_MAX && q_size == SIZE_MAX)
+ return __real_strlcpy(p, q, size);
+ q_len = strlen(q);
+ len = (q_len >= size) ? size - 1 : q_len;
+ if (__builtin_constant_p(size) && __builtin_constant_p(q_len) && size) {
+ /* Write size is always larger than destination. */
+ if (len >= p_size)
+ __write_overflow();
+ }
+ if (size) {
+ if (len >= p_size)
+ fortify_panic(__func__);
+ __underlying_memcpy(p, q, len);
+ p[len] = '\0';
+ }
+ return q_len;
+}
+
+/* Defined after fortified strnlen() to reuse it. */
+extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy);
+/**
+ * strscpy - Copy a C-string into a sized buffer
+ *
+ * @p: Where to copy the string to
+ * @q: Where to copy the string from
+ * @size: Size of destination buffer
+ *
+ * Copy the source string @q, or as much of it as fits, into the destination
+ * @p buffer. The behavior is undefined if the string buffers overlap. The
+ * destination @p buffer is always NUL terminated, unless it's zero-sized.
+ *
+ * Preferred to strlcpy() since the API doesn't require reading memory
+ * from the source @q string beyond the specified @size bytes, and since
+ * the return value is easier to error-check than strlcpy()'s.
+ * In addition, the implementation is robust to the string changing out
+ * from underneath it, unlike the current strlcpy() implementation.
+ *
+ * Preferred to strncpy() since it always returns a valid string, and
+ * doesn't unnecessarily force the tail of the destination buffer to be
+ * zero padded. If padding is desired please use strscpy_pad().
+ *
+ * Returns the number of characters copied in @p (not including the
+ * trailing %NUL) or -E2BIG if @size is 0 or the copy of @q was truncated.
+ */
+__FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, size_t size)
+{
+ /* Use string size rather than possible enclosing struct size. */
+ const size_t p_size = __member_size(p);
+ const size_t q_size = __member_size(q);
+ size_t len;
+
+ /* If we cannot get size of p and q default to call strscpy. */
+ if (p_size == SIZE_MAX && q_size == SIZE_MAX)
+ return __real_strscpy(p, q, size);
+
+ /*
+ * If size can be known at compile time and is greater than
+ * p_size, generate a compile time write overflow error.
+ */
+ if (__compiletime_lessthan(p_size, size))
+ __write_overflow();
+
+ /* Short-circuit for compile-time known-safe lengths. */
+ if (__compiletime_lessthan(p_size, SIZE_MAX)) {
+ len = __compiletime_strlen(q);
+
+ if (len < SIZE_MAX && __compiletime_lessthan(len, size)) {
+ __underlying_memcpy(p, q, len + 1);
+ return len;
+ }
+ }
+
+ /*
+ * This call protects from read overflow, because len will default to q
+ * length if it smaller than size.
+ */
+ len = strnlen(q, size);
+ /*
+ * If len equals size, we will copy only size bytes which leads to
+ * -E2BIG being returned.
+ * Otherwise we will copy len + 1 because of the final '\O'.
+ */
+ len = len == size ? size : len + 1;
+
+ /*
+ * Generate a runtime write overflow error if len is greater than
+ * p_size.
+ */
+ if (len > p_size)
+ fortify_panic(__func__);
+
+ /*
+ * We can now safely call vanilla strscpy because we are protected from:
+ * 1. Read overflow thanks to call to strnlen().
+ * 2. Write overflow thanks to above ifs.
+ */
+ return __real_strscpy(p, q, len);
+}
+
+/* Defined after fortified strlen() to reuse it. */
+extern size_t __real_strlcat(char *p, const char *q, size_t avail) __RENAME(strlcat);
+/**
+ * strlcat - Append a string to an existing string
+ *
+ * @p: pointer to %NUL-terminated string to append to
+ * @q: pointer to %NUL-terminated string to append from
+ * @avail: Maximum bytes available in @p
+ *
+ * Appends %NUL-terminated string @q after the %NUL-terminated
+ * string at @p, but will not write beyond @avail bytes total,
+ * potentially truncating the copy from @q. @p will stay
+ * %NUL-terminated only if a %NUL already existed within
+ * the @avail bytes of @p. If so, the resulting number of
+ * bytes copied from @q will be at most "@avail - strlen(@p) - 1".
+ *
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
+ * read and write overflows, this is only possible when the sizes
+ * of @p and @q are known to the compiler. Prefer building the
+ * string with formatting, via scnprintf(), seq_buf, or similar.
+ *
+ * Returns total bytes that _would_ have been contained by @p
+ * regardless of truncation, similar to snprintf(). If return
+ * value is >= @avail, the string has been truncated.
+ *
+ */
+__FORTIFY_INLINE
+size_t strlcat(char * const POS p, const char * const POS q, size_t avail)
+{
+ const size_t p_size = __member_size(p);
+ const size_t q_size = __member_size(q);
+ size_t p_len, copy_len;
+ size_t actual, wanted;
+
+ /* Give up immediately if both buffer sizes are unknown. */
+ if (p_size == SIZE_MAX && q_size == SIZE_MAX)
+ return __real_strlcat(p, q, avail);
+
+ p_len = strnlen(p, avail);
+ copy_len = strlen(q);
+ wanted = actual = p_len + copy_len;
+
+ /* Cannot append any more: report truncation. */
+ if (avail <= p_len)
+ return wanted;
+
+ /* Give up if string is already overflowed. */
+ if (p_size <= p_len)
+ fortify_panic(__func__);
+
+ if (actual >= avail) {
+ copy_len = avail - p_len - 1;
+ actual = p_len + copy_len;
+ }
+
+ /* Give up if copy will overflow. */
+ if (p_size <= actual)
+ fortify_panic(__func__);
+ __underlying_memcpy(p + p_len, q, copy_len);
+ p[actual] = '\0';
+
+ return wanted;
+}
+
+/* Defined after fortified strlcat() to reuse it. */
+/**
+ * strcat - Append a string to an existing string
+ *
+ * @p: pointer to NUL-terminated string to append to
+ * @q: pointer to NUL-terminated source string to append from
+ *
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
+ * read and write overflows, this is only possible when the
+ * destination buffer size is known to the compiler. Prefer
+ * building the string with formatting, via scnprintf() or similar.
+ * At the very least, use strncat().
+ *
+ * Returns @p.
+ *
+ */
+__FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2)
+char *strcat(char * const POS p, const char *q)
+{
+ const size_t p_size = __member_size(p);
+
+ if (strlcat(p, q, p_size) >= p_size)
+ fortify_panic(__func__);
+ return p;
+}
+
+/**
+ * strncat - Append a string to an existing string
+ *
+ * @p: pointer to NUL-terminated string to append to
+ * @q: pointer to source string to append from
+ * @count: Maximum bytes to read from @q
+ *
+ * Appends at most @count bytes from @q (stopping at the first
+ * NUL byte) after the NUL-terminated string at @p. @p will be
+ * NUL-terminated.
+ *
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
+ * read and write overflows, this is only possible when the sizes
+ * of @p and @q are known to the compiler. Prefer building the
+ * string with formatting, via scnprintf() or similar.
+ *
+ * Returns @p.
+ *
+ */
+/* Defined after fortified strlen() and strnlen() to reuse them. */
+__FORTIFY_INLINE __diagnose_as(__builtin_strncat, 1, 2, 3)
+char *strncat(char * const POS p, const char * const POS q, __kernel_size_t count)
+{
+ const size_t p_size = __member_size(p);
+ const size_t q_size = __member_size(q);
+ size_t p_len, copy_len;
+
+ if (p_size == SIZE_MAX && q_size == SIZE_MAX)
+ return __underlying_strncat(p, q, count);
+ p_len = strlen(p);
+ copy_len = strnlen(q, count);
+ if (p_size < p_len + copy_len + 1)
+ fortify_panic(__func__);
+ __underlying_memcpy(p + p_len, q, copy_len);
+ p[p_len + copy_len] = '\0';
+ return p;
+}
+
+__FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size,
+ const size_t p_size,
+ const size_t p_size_field)
+{
+ if (__builtin_constant_p(size)) {
+ /*
+ * Length argument is a constant expression, so we
+ * can perform compile-time bounds checking where
+ * buffer sizes are also known at compile time.
+ */
+
+ /* Error when size is larger than enclosing struct. */
+ if (__compiletime_lessthan(p_size_field, p_size) &&
+ __compiletime_lessthan(p_size, size))
+ __write_overflow();
+
+ /* Warn when write size is larger than dest field. */
+ if (__compiletime_lessthan(p_size_field, size))
+ __write_overflow_field(p_size_field, size);
+ }
+ /*
+ * At this point, length argument may not be a constant expression,
+ * so run-time bounds checking can be done where buffer sizes are
+ * known. (This is not an "else" because the above checks may only
+ * be compile-time warnings, and we want to still warn for run-time
+ * overflows.)
+ */
+
+ /*
+ * Always stop accesses beyond the struct that contains the
+ * field, when the buffer's remaining size is known.
+ * (The SIZE_MAX test is to optimize away checks where the buffer
+ * lengths are unknown.)
+ */
+ if (p_size != SIZE_MAX && p_size < size)
+ fortify_panic("memset");
+}
+
+#define __fortify_memset_chk(p, c, size, p_size, p_size_field) ({ \
+ size_t __fortify_size = (size_t)(size); \
+ fortify_memset_chk(__fortify_size, p_size, p_size_field), \
+ __underlying_memset(p, c, __fortify_size); \
+})
+
+/*
+ * To make sure the compiler can enforce protection against buffer overflows,
+ * memcpy(), memmove(), and memset() must not be used beyond individual
+ * struct members. If you need to copy across multiple members, please use
+ * struct_group() to create a named mirror of an anonymous struct union.
+ * (e.g. see struct sk_buff.) Read overflow checking is currently only
+ * done when a write overflow is also present, or when building with W=1.
+ *
+ * Mitigation coverage matrix
+ * Bounds checking at:
+ * +-------+-------+-------+-------+
+ * | Compile time | Run time |
+ * memcpy() argument sizes: | write | read | write | read |
+ * dest source length +-------+-------+-------+-------+
+ * memcpy(known, known, constant) | y | y | n/a | n/a |
+ * memcpy(known, unknown, constant) | y | n | n/a | V |
+ * memcpy(known, known, dynamic) | n | n | B | B |
+ * memcpy(known, unknown, dynamic) | n | n | B | V |
+ * memcpy(unknown, known, constant) | n | y | V | n/a |
+ * memcpy(unknown, unknown, constant) | n | n | V | V |
+ * memcpy(unknown, known, dynamic) | n | n | V | B |
+ * memcpy(unknown, unknown, dynamic) | n | n | V | V |
+ * +-------+-------+-------+-------+
+ *
+ * y = perform deterministic compile-time bounds checking
+ * n = cannot perform deterministic compile-time bounds checking
+ * n/a = no run-time bounds checking needed since compile-time deterministic
+ * B = can perform run-time bounds checking (currently unimplemented)
+ * V = vulnerable to run-time overflow (will need refactoring to solve)
+ *
+ */
+__FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
+ const size_t p_size,
+ const size_t q_size,
+ const size_t p_size_field,
+ const size_t q_size_field,
+ const char *func)
+{
+ if (__builtin_constant_p(size)) {
+ /*
+ * Length argument is a constant expression, so we
+ * can perform compile-time bounds checking where
+ * buffer sizes are also known at compile time.
+ */
+
+ /* Error when size is larger than enclosing struct. */
+ if (__compiletime_lessthan(p_size_field, p_size) &&
+ __compiletime_lessthan(p_size, size))
+ __write_overflow();
+ if (__compiletime_lessthan(q_size_field, q_size) &&
+ __compiletime_lessthan(q_size, size))
+ __read_overflow2();
+
+ /* Warn when write size argument larger than dest field. */
+ if (__compiletime_lessthan(p_size_field, size))
+ __write_overflow_field(p_size_field, size);
+ /*
+ * Warn for source field over-read when building with W=1
+ * or when an over-write happened, so both can be fixed at
+ * the same time.
+ */
+ if ((IS_ENABLED(KBUILD_EXTRA_WARN1) ||
+ __compiletime_lessthan(p_size_field, size)) &&
+ __compiletime_lessthan(q_size_field, size))
+ __read_overflow2_field(q_size_field, size);
+ }
+ /*
+ * At this point, length argument may not be a constant expression,
+ * so run-time bounds checking can be done where buffer sizes are
+ * known. (This is not an "else" because the above checks may only
+ * be compile-time warnings, and we want to still warn for run-time
+ * overflows.)
+ */
+
+ /*
+ * Always stop accesses beyond the struct that contains the
+ * field, when the buffer's remaining size is known.
+ * (The SIZE_MAX test is to optimize away checks where the buffer
+ * lengths are unknown.)
+ */
+ if ((p_size != SIZE_MAX && p_size < size) ||
+ (q_size != SIZE_MAX && q_size < size))
+ fortify_panic(func);
+
+ /*
+ * Warn when writing beyond destination field size.
+ *
+ * We must ignore p_size_field == 0 for existing 0-element
+ * fake flexible arrays, until they are all converted to
+ * proper flexible arrays.
+ *
+ * The implementation of __builtin_*object_size() behaves
+ * like sizeof() when not directly referencing a flexible
+ * array member, which means there will be many bounds checks
+ * that will appear at run-time, without a way for them to be
+ * detected at compile-time (as can be done when the destination
+ * is specifically the flexible array member).
+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832
+ */
+ if (p_size_field != 0 && p_size_field != SIZE_MAX &&
+ p_size != p_size_field && p_size_field < size)
+ return true;
+
+ return false;
+}
+
+#define __fortify_memcpy_chk(p, q, size, p_size, q_size, \
+ p_size_field, q_size_field, op) ({ \
+ const size_t __fortify_size = (size_t)(size); \
+ const size_t __p_size = (p_size); \
+ const size_t __q_size = (q_size); \
+ const size_t __p_size_field = (p_size_field); \
+ const size_t __q_size_field = (q_size_field); \
+ WARN_ONCE(fortify_memcpy_chk(__fortify_size, __p_size, \
+ __q_size, __p_size_field, \
+ __q_size_field, #op), \
+ #op ": detected field-spanning write (size %zu) of single %s (size %zu)\n", \
+ __fortify_size, \
+ "field \"" #p "\" at " __FILE__ ":" __stringify(__LINE__), \
+ __p_size_field); \
+ __underlying_##op(p, q, __fortify_size); \
+})
+
+/*
+ * Notes about compile-time buffer size detection:
+ *
+ * With these types...
+ *
+ * struct middle {
+ * u16 a;
+ * u8 middle_buf[16];
+ * int b;
+ * };
+ * struct end {
+ * u16 a;
+ * u8 end_buf[16];
+ * };
+ * struct flex {
+ * int a;
+ * u8 flex_buf[];
+ * };
+ *
+ * void func(TYPE *ptr) { ... }
+ *
+ * Cases where destination size cannot be currently detected:
+ * - the size of ptr's object (seemingly by design, gcc & clang fail):
+ * __builtin_object_size(ptr, 1) == SIZE_MAX
+ * - the size of flexible arrays in ptr's obj (by design, dynamic size):
+ * __builtin_object_size(ptr->flex_buf, 1) == SIZE_MAX
+ * - the size of ANY array at the end of ptr's obj (gcc and clang bug):
+ * __builtin_object_size(ptr->end_buf, 1) == SIZE_MAX
+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836
+ *
+ * Cases where destination size is currently detected:
+ * - the size of non-array members within ptr's object:
+ * __builtin_object_size(ptr->a, 1) == 2
+ * - the size of non-flexible-array in the middle of ptr's obj:
+ * __builtin_object_size(ptr->middle_buf, 1) == 16
+ *
+ */
+
+/*
+ * __struct_size() vs __member_size() must be captured here to avoid
+ * evaluating argument side-effects further into the macro layers.
+ */
+#define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \
+ __struct_size(p), __struct_size(q), \
+ __member_size(p), __member_size(q), \
+ memcpy)
+#define memmove(p, q, s) __fortify_memcpy_chk(p, q, s, \
+ __struct_size(p), __struct_size(q), \
+ __member_size(p), __member_size(q), \
+ memmove)
+
+extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
+__FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size)
+{
+ const size_t p_size = __struct_size(p);
+
+ if (__compiletime_lessthan(p_size, size))
+ __read_overflow();
+ if (p_size < size)
+ fortify_panic(__func__);
+ return __real_memscan(p, c, size);
+}
+
+__FORTIFY_INLINE __diagnose_as(__builtin_memcmp, 1, 2, 3)
+int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t size)
+{
+ const size_t p_size = __struct_size(p);
+ const size_t q_size = __struct_size(q);
+
+ if (__builtin_constant_p(size)) {
+ if (__compiletime_lessthan(p_size, size))
+ __read_overflow();
+ if (__compiletime_lessthan(q_size, size))
+ __read_overflow2();
+ }
+ if (p_size < size || q_size < size)
+ fortify_panic(__func__);
+ return __underlying_memcmp(p, q, size);
+}
+
+__FORTIFY_INLINE __diagnose_as(__builtin_memchr, 1, 2, 3)
+void *memchr(const void * const POS0 p, int c, __kernel_size_t size)
+{
+ const size_t p_size = __struct_size(p);
+
+ if (__compiletime_lessthan(p_size, size))
+ __read_overflow();
+ if (p_size < size)
+ fortify_panic(__func__);
+ return __underlying_memchr(p, c, size);
+}
+
+void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
+__FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
+{
+ const size_t p_size = __struct_size(p);
+
+ if (__compiletime_lessthan(p_size, size))
+ __read_overflow();
+ if (p_size < size)
+ fortify_panic(__func__);
+ return __real_memchr_inv(p, c, size);
+}
+
+extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup)
+ __realloc_size(2);
+__FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp)
+{
+ const size_t p_size = __struct_size(p);
+
+ if (__compiletime_lessthan(p_size, size))
+ __read_overflow();
+ if (p_size < size)
+ fortify_panic(__func__);
+ return __real_kmemdup(p, size, gfp);
+}
+
+/**
+ * strcpy - Copy a string into another string buffer
+ *
+ * @p: pointer to destination of copy
+ * @q: pointer to NUL-terminated source string to copy
+ *
+ * Do not use this function. While FORTIFY_SOURCE tries to avoid
+ * overflows, this is only possible when the sizes of @q and @p are
+ * known to the compiler. Prefer strscpy(), though note its different
+ * return values for detecting truncation.
+ *
+ * Returns @p.
+ *
+ */
+/* Defined after fortified strlen to reuse it. */
+__FORTIFY_INLINE __diagnose_as(__builtin_strcpy, 1, 2)
+char *strcpy(char * const POS p, const char * const POS q)
+{
+ const size_t p_size = __member_size(p);
+ const size_t q_size = __member_size(q);
+ size_t size;
+
+ /* If neither buffer size is known, immediately give up. */
+ if (__builtin_constant_p(p_size) &&
+ __builtin_constant_p(q_size) &&
+ p_size == SIZE_MAX && q_size == SIZE_MAX)
+ return __underlying_strcpy(p, q);
+ size = strlen(q) + 1;
+ /* Compile-time check for const size overflow. */
+ if (__compiletime_lessthan(p_size, size))
+ __write_overflow();
+ /* Run-time check for dynamic size overflow. */
+ if (p_size < size)
+ fortify_panic(__func__);
+ __underlying_memcpy(p, q, size);
+ return p;
+}
+
+/* Don't use these outside the FORITFY_SOURCE implementation */
+#undef __underlying_memchr
+#undef __underlying_memcmp
+#undef __underlying_strcat
+#undef __underlying_strcpy
+#undef __underlying_strncat
+#undef __underlying_strncpy
+
+#undef POS
+#undef POS0
+
+#endif /* _LINUX_FORTIFY_STRING_H_ */
diff --git a/include/linux/string.h b/include/linux/string.h
index 953484ebc9f6..d796c774c4fe 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -161,6 +161,23 @@ static inline const char *kbasename(const char *path)
return tail ? tail + 1 : path;
}
+#if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
+#include <linux/fortify-string.h>
+#endif
+#ifndef unsafe_memcpy
+#define unsafe_memcpy(dst, src, bytes, justification) \
+ memcpy(dst, src, bytes)
+#endif
+
+#ifndef unsafe_memset
+#define unsafe_memset(dst, val, size, justification) \
+ memset(dst, val, size)
+#endif
+
+#ifndef unsafe_strlen
+#define unsafe_strlen(str, justification) \
+ strlen(str)
+#endif
#ifdef __cplusplus
}
diff --git a/lib/Kconfig.hardening b/lib/Kconfig.hardening
index ac1acefafb2c..658de4953aa1 100644
--- a/lib/Kconfig.hardening
+++ b/lib/Kconfig.hardening
@@ -137,6 +137,19 @@ config ZERO_CALL_USED_REGS
endmenu
+menu "Buffer overflow protection"
+
+config FORTIFY_SOURCE
+ bool "Harden common str/mem functions against buffer overflows"
+ depends on ARCH_HAS_FORTIFY_SOURCE
+ # https://bugs.llvm.org/show_bug.cgi?id=41459
+ depends on !CC_IS_CLANG || CLANG_VERSION >= 120001
+ # https://github.com/llvm/llvm-project/issues/53645
+ depends on !CC_IS_CLANG || !X86_32
+ help
+ Detect overflows of buffers in common string and memory functions
+ where the compiler can determine and validate the buffer sizes.
+
config STACK_GUARD_PAGE
bool "Place guard page to catch stack overflows"
depends on ARM && MMU
@@ -251,3 +264,5 @@ config MEMORY_ATTRIBUTES
will in future be enforced by the MMU.
endmenu
+
+endmenu
diff --git a/lib/Makefile b/lib/Makefile
index 0d1d22c0845b..370d4f31d45a 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -6,7 +6,7 @@ obj-$(CONFIG_BOOTSTRAP) += bootstrap/
obj-pbl-y += ctype.o
obj-y += rbtree.o
obj-y += display_options.o
-obj-y += string.o
+obj-y += string.o string_helpers.o
obj-$(CONFIG_VERSION_CMP) += strverscmp.o
obj-y += strtox.o
obj-y += kstrtox.o
diff --git a/lib/string.c b/lib/string.c
index ebce8f0073fa..96eac6d1daf6 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -6,19 +6,13 @@
*/
/*
- * stupid library routines.. The optimized versions should generally be found
- * as inline code in <asm-xx/string.h>
- *
- * These are buggy as well..
- *
- * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
- * - Added strsep() which will replace strtok() soon (because strsep() is
- * reentrant and should be faster). Use only strsep() in new code, please.
- * * Mon Sep 14 2020, Ahmad Fatoum <a.fatoum@pengutronix.de>
- * - Kissed strtok() goodbye
- *
+ * This file should be used only for "library" routines that may have
+ * alternative implementations on specific architectures (generally
+ * found in <asm/string.h>), or get overloaded by FORTIFY_SOURCE.
+ * (Specifically, this file is built with __NO_FORTIFY.)
*/
+#define __NO_FORTIFY
#include <linux/types.h>
#include <string.h>
#include <linux/ctype.h>
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
new file mode 100644
index 000000000000..6ddef5f78e38
--- /dev/null
+++ b/lib/string_helpers.c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Helpers for formatting and printing strings
+ *
+ * Copyright 31 August 2008 James Bottomley
+ * Copyright (C) 2013, Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/bug.h>
+#include <linux/printk.h>
+#include <linux/linkage.h>
+
+void __read_overflow2_field(size_t avail, size_t wanted);
+void __write_overflow_field(size_t avail, size_t wanted);
+void fortify_panic(const char *name);
+
+#ifdef CONFIG_FORTIFY_SOURCE
+/* These are placeholders for fortify compile-time warnings. */
+void __read_overflow2_field(size_t avail, size_t wanted) { }
+EXPORT_SYMBOL(__read_overflow2_field);
+void __write_overflow_field(size_t avail, size_t wanted) { }
+EXPORT_SYMBOL(__write_overflow_field);
+
+void fortify_panic(const char *name)
+{
+ panic("detected buffer overflow in %s\n", name);
+}
+EXPORT_SYMBOL(fortify_panic);
+#endif /* CONFIG_FORTIFY_SOURCE */
diff --git a/pbl/string.c b/pbl/string.c
index 7c10f064dc7e..528f62ef2055 100644
--- a/pbl/string.c
+++ b/pbl/string.c
@@ -6,6 +6,7 @@
* Small subset of simple string routines
*/
+#define __NO_FORTIFY
#include <linux/types.h>
#include <linux/string.h>
#include <linux/compiler.h>
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 11/21] sandbox: populate UNAME_M variable
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (9 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 10/21] string: add fortify source support Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 12/21] Add fuzzing infrastructure Ahmad Fatoum
` (9 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We will need the UNAME_M information to pick the correct static library
when linking against libfuzzer.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
scripts/subarch.include | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/scripts/subarch.include b/scripts/subarch.include
index caf3c641b52a..9137ddcd92fa 100644
--- a/scripts/subarch.include
+++ b/scripts/subarch.include
@@ -4,8 +4,10 @@
# then ARCH is assigned, getting whatever value it gets normally, and
# SUBARCH is subsequently ignored.
-SUBARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ \
- -e s/arm.*/arm/ -e s/sa110/arm/ \
- -e s/aarch64.*/arm/ \
- -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
- -e s/riscv.*/riscv/)
+UNAME_M := $(shell uname -m 2>/dev/null)
+SUBARCH := $(shell echo $(UNAME_M) | \
+ sed -e s/i.86/x86/ -e s/x86_64/x86/ \
+ -e s/arm.*/arm/ -e s/sa110/arm/ \
+ -e s/aarch64.*/arm/ \
+ -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
+ -e s/riscv.*/riscv/)
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 12/21] Add fuzzing infrastructure
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (10 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 11/21] sandbox: populate UNAME_M variable Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 13/21] filetype: add fuzz target Ahmad Fatoum
` (8 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
To aid in detection of memory safety issues in security-critical code
add support for libfuzzer along with a first test exercising the
hexstring format specifier of printf.
More useful tests will follow. In addition to libfuzzer, there's also a
fuzz command that passes a fixed input to a buffer. This can be useful
when inspecting a crashing input from the barebox command line.
Note that libfuzzer is in maintenance-mode unfortunately, but it
requiring only LLVM support makes it very convenient to use, so it's a
good first step.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
arch/sandbox/Kconfig | 5 ++
arch/sandbox/Makefile | 11 ++-
arch/sandbox/os/common.c | 137 +++++++++++++++++++++++++++++-
commands/Makefile | 1 +
commands/fuzz.c | 118 +++++++++++++++++++++++++
common/Kconfig | 5 ++
common/startup.c | 1 +
images/.gitignore | 1 +
images/Makefile.sandbox | 16 +++-
include/asm-generic/barebox.lds.h | 13 ++-
include/fuzz.h | 70 +++++++++++++++
lib/Makefile | 1 +
lib/fuzz.c | 79 +++++++++++++++++
lib/vsprintf.c | 15 ++++
scripts/Kconfig.include | 1 +
scripts/clang-runtime-dir.sh | 19 +++++
test/Kconfig | 37 ++++++++
17 files changed, 523 insertions(+), 7 deletions(-)
create mode 100644 commands/fuzz.c
create mode 100644 include/fuzz.h
create mode 100644 lib/fuzz.c
create mode 100755 scripts/clang-runtime-dir.sh
diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index 7d8f88474781..5c7948b215cf 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -96,4 +96,9 @@ config HAVE_LIBFTDI
config SDL
bool
+config SANDBOX_LINK_CXX
+ def_bool FUZZ_EXTERNAL
+ help
+ Link with CXX instead of CC to support C++ static libraries.
+
endmenu
diff --git a/arch/sandbox/Makefile b/arch/sandbox/Makefile
index 2db1cb648a2b..6566cd563ed8 100644
--- a/arch/sandbox/Makefile
+++ b/arch/sandbox/Makefile
@@ -67,6 +67,15 @@ ifeq ($(CONFIG_UBSAN),y)
SANDBOX_LIBS += -fsanitize=undefined
endif
+ifeq ($(CONFIG_FUZZ_EXTERNAL),y)
+LIBARCH-y = $(UNAME_M)
+LIBARCH-$(CONFIG_SANDBOX_LINUX_I386) = i386
+
+KBUILD_CPPFLAGS += -fsanitize=fuzzer-no-link
+SANDBOX_LIBS += -Wl,-Bstatic -L"$(CONFIG_CLANG_RUNTIME_DIR)" \
+ -lclang_rt.fuzzer_no_main-$(LIBARCH-y) -Wl,-Bdynamic
+endif
+
ifeq ($(CONFIG_SANDBOX_LINUX_I386),y)
KBUILD_CFLAGS += -m32
KBUILD_LDFLAGS += -m elf_i386
@@ -80,7 +89,7 @@ SANDBOX_PROPER2PBL_GLUE_SYMS := \
strsep_unescaped start_barebox linux_get_stickypage_path \
stickypage mem_malloc_init \
barebox_register_filedev barebox_register_dtb barebox_register_console \
- barebox_errno barebox_loglevel
+ barebox_errno barebox_loglevel call_for_each_fuzz_test setup_external_fuzz
OBJCOPYFLAGS_barebox.o := $(addprefix --keep-global-symbol=, $(SANDBOX_PROPER2PBL_GLUE_SYMS))
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 3dbe61791ccd..86aaeb24ee3d 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
+#include <stdbool.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
@@ -54,6 +55,22 @@ int __attribute__((unused)) barebox_loglevel;
extern int barebox_loglevel;
#endif
+#ifdef CONFIG_FUZZ_EXTERNAL
+int call_for_each_fuzz_test(int (*fn)(const char **test));
+int setup_external_fuzz(const char *name,
+ int *argc, char ***argv);
+#else
+static inline int call_for_each_fuzz_test(int (*fn)(const char **test))
+{
+ return 0;
+}
+static inline int setup_external_fuzz(const char *name,
+ int *argc, char ***argv)
+{
+ return -1;
+}
+#endif
+
#define DELETED_OFFSET (sizeof(" (deleted)") - 1)
void __sanitizer_set_death_callback(void (*callback)(void));
@@ -514,6 +531,8 @@ const char *barebox_cmdline_get(void)
static void print_usage(const char*);
#define OPT_LOGLEVEL (CHAR_MAX + 1)
+#define OPT_LIST_FUZZERS (CHAR_MAX + 2)
+#define OPT_FUZZ (CHAR_MAX + 3)
static struct option long_options[] = {
{"help", 0, 0, 'h'},
@@ -529,14 +548,72 @@ static struct option long_options[] = {
{"yres", 1, 0, 'y'},
#ifndef CONFIG_CONSOLE_NONE
{"loglevel", 1, 0, OPT_LOGLEVEL},
+#endif
+#ifdef CONFIG_FUZZ_EXTERNAL
+ {"fuzz", 1, 0, OPT_FUZZ},
+ {"list-fuzzers", 0, 0, OPT_LIST_FUZZERS},
#endif
{0, 0, 0, 0},
};
static const char optstring[] = "hm:i:c:e:d:O:I:B:x:y:";
-ENTRY_FUNCTION(sandbox_main, argc, argv)
+static __attribute__((unused)) int print_fuzz_test_name(const char **test_name)
{
+ printf("%s\n", *test_name);
+ return 0;
+}
+
+static inline size_t str_has_prefix(const char *str, const char *prefix)
+{
+ size_t len = strlen(prefix);
+ return strncmp(str, prefix, len) == 0 ? len : 0;
+}
+
+static char *realpath_alloc(char *path)
+{
+ char *real;
+
+ real = malloc(PATH_MAX);
+ if (!real) {
+ perror("malloc");
+ exit(3);
+ }
+
+ if (!realpath(path, real)) {
+ perror("realpath");
+ exit(EXIT_FAILURE);
+ }
+
+ return real;
+}
+
+static void setup_external_fuzz_with_args(const char *fuzz, int *pargc, char **pargv[])
+{
+ char **argv = *pargv;
+ char *rlpath;
+ int ret;
+
+ rlpath = realpath_alloc(argv[0]);
+
+ asprintf(&argv[optind - 1], "%s/fuzz-%s", dirname(rlpath), fuzz);
+
+ free(rlpath);
+
+ *pargc -= optind - 1;
+ *pargv += optind - 1;
+
+ ret = setup_external_fuzz(fuzz, pargc, pargv);
+ if (ret) {
+ printf("unknown fuzz target '%s'\n", fuzz);
+ exit(ret);
+ }
+}
+
+static int normal_main(int argc, char *argv[])
+{
+ bool skip_opts = false;
+ const char *fuzz = NULL;
void *ram;
int opt, ret, fd, fd2;
int malloc_size = CONFIG_MALLOC_SIZE;
@@ -548,7 +625,7 @@ ENTRY_FUNCTION(sandbox_main, argc, argv)
__sanitizer_set_death_callback(prepare_exit);
#endif
- while (1) {
+ while (!skip_opts) {
option_index = 0;
opt = getopt_long(argc, argv, optstring,
long_options, &option_index);
@@ -589,10 +666,18 @@ ENTRY_FUNCTION(sandbox_main, argc, argv)
case 'y':
sdl_yres = strtoul(optarg, NULL, 0);
break;
+ case OPT_LIST_FUZZERS:
+ call_for_each_fuzz_test(print_fuzz_test_name);
+ exit(0);
+ break;
+ case OPT_FUZZ:
+ skip_opts = true;
+ break;
default:
break;
}
}
+ skip_opts = false;
saved_argv = argv;
@@ -610,7 +695,7 @@ ENTRY_FUNCTION(sandbox_main, argc, argv)
*/
optind = 1;
- while (1) {
+ while (!skip_opts) {
option_index = 0;
opt = getopt_long(argc, argv, optstring,
long_options, &option_index);
@@ -672,6 +757,10 @@ ENTRY_FUNCTION(sandbox_main, argc, argv)
barebox_register_console(fd2, fd);
break;
+ case OPT_FUZZ:
+ fuzz = optarg;
+ skip_opts = true;
+ break;
default:
break;
}
@@ -679,7 +768,10 @@ ENTRY_FUNCTION(sandbox_main, argc, argv)
barebox_register_console(fileno(stdin), fileno(stdout));
- rawmode();
+ if (fuzz)
+ setup_external_fuzz_with_args(fuzz, &argc, &argv);
+ else
+ rawmode();
if (loglevel >= 0)
barebox_loglevel = loglevel;
@@ -690,6 +782,39 @@ ENTRY_FUNCTION(sandbox_main, argc, argv)
return 0;
}
+ENTRY_FUNCTION(sandbox_main, argc, argv)
+{
+ char **args;
+ char *argv0;
+ size_t fuzz_off;
+ char *rlpath;
+
+ tcgetattr(0, &term_orig);
+
+ argv0 = basename(argv[0]);
+ fuzz_off = str_has_prefix(argv0, "fuzz-");
+ if (fuzz_off) {
+ args = malloc((argc + 3) * sizeof(*args));
+ if (!args)
+ exit(3);
+
+ rlpath = realpath_alloc(argv[0]);
+ asprintf(&args[0], "%s/barebox", dirname(rlpath));
+
+ free(rlpath);
+
+ args[1] = "--fuzz";
+ args[2] = argv0 + fuzz_off;
+
+ memcpy(&args[3], &argv[1], argc * sizeof(*args));
+
+ argc += 2;
+ argv = args;
+ }
+
+ return normal_main(argc, argv);
+}
+
#ifdef __PPC__
/* HACK: we need this symbol on PPC, better ask a PPC export about this :) */
char _SDA_BASE_[4096];
@@ -736,6 +861,10 @@ static void print_usage(const char *prgname)
" 6 informational (info)\n"
" 7 debug-level messages (debug)\n"
" 8 verbose debug messages (vdebug)\n"
+#endif
+#ifdef CONFIG_FUZZ_EXTERNAL
+" --fuzz=<test> Run libfuzzer against the <test> target.\n"
+" --list-fuzzers List all available fuzzing test targets.\n"
#endif
, prgname
);
diff --git a/commands/Makefile b/commands/Makefile
index 53337dfb5c2d..603e1a25244c 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -157,6 +157,7 @@ obj-$(CONFIG_CMD_IP_ROUTE_GET) += ip-route-get.o
obj-$(CONFIG_CMD_BTHREAD) += bthread.o
obj-$(CONFIG_CMD_UBSAN) += ubsan.o
obj-$(CONFIG_CMD_SELFTEST) += selftest.o
+obj-$(CONFIG_CMD_FUZZ) += fuzz.o
obj-$(CONFIG_CMD_TUTORIAL) += tutorial.o
obj-$(CONFIG_CMD_STACKSMASH) += stacksmash.o
obj-$(CONFIG_CMD_PARTED) += parted.o
diff --git a/commands/fuzz.c b/commands/fuzz.c
new file mode 100644
index 000000000000..f48032e7e1d9
--- /dev/null
+++ b/commands/fuzz.c
@@ -0,0 +1,118 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#define pr_fmt(fmt) "fuzz: " fmt
+
+#include <common.h>
+#include <command.h>
+#include <getopt.h>
+#include <fuzz.h>
+#include <libfile.h>
+#include <fs.h>
+
+static const struct fuzz_test *get_fuzz_test(const char *match, bool print)
+{
+ const struct fuzz_test *test;
+ unsigned matches = 0;
+
+ for_each_fuzz_test(test) {
+ if (print) {
+ printf("%s\n", test->name);
+ matches++;
+ }
+
+ if (match && !strcmp(test->name, match))
+ return test;
+
+ }
+
+ if (!matches) {
+ if (match)
+ printf("No fuzz tests matching '%s' found.\n", match);
+ else
+ printf("No fuzz tests registered.\n");
+ }
+
+ return NULL;
+}
+
+static int run_fuzz_test(const char *match, bool list, const u8 *buf, size_t len)
+{
+ const struct fuzz_test *test;
+
+ test = get_fuzz_test(match, list);
+ if (list)
+ return 0;
+ else if (!test)
+ return COMMAND_ERROR;
+
+ return fuzz_test_once(test, buf, len);
+}
+
+static int do_fuzz(int argc, char *argv[])
+{
+ const char *file = NULL;
+ u64 max_size = FILESIZE_MAX;
+ size_t size = 0;
+ const u8 *buf = NULL;
+ void *contents = NULL;
+ bool list = false;
+ int err = 0, opt;
+
+ while((opt = getopt(argc, argv, "ls:f:")) > 0) {
+ switch(opt) {
+ case 'l':
+ list = true;
+ break;
+ case 's':
+ err = kstrtou64(optarg, 0, &max_size);
+ if (err || max_size > SIZE_MAX) {
+ printf("invalid max size\n");
+ return COMMAND_ERROR;
+ }
+ break;
+ case 'f':
+ file = optarg;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ argv += optind;
+ argc -= optind;
+
+ if ((list && argc) || (!list && argc != 1) || (!file && !list))
+ return COMMAND_ERROR_USAGE;
+
+ if (file) {
+ err = read_file_2(file, &size, &contents, max_size);
+ if (err && err != -EFBIG) {
+ printf("error reading file: %m\n");
+ return COMMAND_ERROR;
+ }
+
+ buf = contents;
+ }
+
+ err = run_fuzz_test(argv[0], list, buf, size);
+
+ free(contents);
+ return err;
+}
+
+BAREBOX_CMD_HELP_START(fuzz)
+BAREBOX_CMD_HELP_TEXT("Run barebox fuzz test on fixed input")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-l", "list registered fuzz tests")
+BAREBOX_CMD_HELP_OPT ("-f FILE", "use FILE as fuzz input")
+BAREBOX_CMD_HELP_OPT ("-s SIZE", "read only SIZE bytes from file")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(fuzz)
+ .cmd = do_fuzz,
+ BAREBOX_CMD_DESC("Run fuzz test")
+ BAREBOX_CMD_OPTS("[-ls] -f FILE [test]")
+ BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+ BAREBOX_CMD_HELP(cmd_fuzz_help)
+BAREBOX_CMD_END
diff --git a/common/Kconfig b/common/Kconfig
index 6d41b7048a32..83a0717e76d6 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -18,6 +18,11 @@ config CLANG_VERSION
default $(cc-version) if CC_IS_CLANG
default 0
+config CLANG_RUNTIME_DIR
+ string
+ depends on CC_IS_CLANG
+ default "$(clang-runtime-dir)"
+
config GREGORIAN_CALENDER
bool
diff --git a/common/startup.c b/common/startup.c
index c9e99d47fbd7..c720664689f6 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -44,6 +44,7 @@
#include <bselftest.h>
#include <pbl/handoff-data.h>
#include <libfile.h>
+#include <fuzz.h>
extern initcall_t __barebox_initcalls_start[], __barebox_early_initcalls_end[],
__barebox_initcalls_end[];
diff --git a/images/.gitignore b/images/.gitignore
index 20133074bf87..fc464ff2e3cb 100644
--- a/images/.gitignore
+++ b/images/.gitignore
@@ -42,3 +42,4 @@ barebox.sum
*.fit
*.missing-firmware
*.k3img
+fuzz-*
diff --git a/images/Makefile.sandbox b/images/Makefile.sandbox
index 0699a52663ef..ed5d740bc1fb 100644
--- a/images/Makefile.sandbox
+++ b/images/Makefile.sandbox
@@ -3,12 +3,26 @@
SYMLINK_TARGET_barebox = sandbox_main.elf
symlink-$(CONFIG_SANDBOX) += barebox
+fuzzer-$(CONFIG_PRINTF_HEXSTR) += printf
+
ifeq ($(CONFIG_SANDBOX),y)
+ifdef CONFIG_SANDBOX_LINK_CXX
+linker = $(CXX)
+else
+linker = $(CC)
+endif
+
quiet_cmd_elf__ = LD $@
- cmd_elf__ = $(CC) -o $@ $(BAREBOX_LDFLAGS) \
+ cmd_elf__ = $(linker) -o $@ $(BAREBOX_LDFLAGS) \
-Wl,-T,$(pbl-lds) -Wl,--defsym=main=$(2) -Wl,--whole-archive \
$(obj)/../$(BAREBOX_PROPER) $(BAREBOX_PBL_OBJS) -Wl,--no-whole-archive \
-lrt -pthread $(SANDBOX_LIBS) $(LDFLAGS_$(@F))
+ifeq ($(CONFIG_FUZZ_EXTERNAL),y)
+$(foreach fuzzer, $(fuzzer-y), \
+ $(eval SYMLINK_TARGET_fuzz-$(fuzzer) = barebox) \
+ $(eval symlink-y += fuzz-$(fuzzer)))
+endif
+
endif
diff --git a/include/asm-generic/barebox.lds.h b/include/asm-generic/barebox.lds.h
index 54817b68fc22..2c0f4da9e5d4 100644
--- a/include/asm-generic/barebox.lds.h
+++ b/include/asm-generic/barebox.lds.h
@@ -118,6 +118,16 @@
KEEP(*(SORT_BY_NAME(.barebox_deep_probe*))) \
__barebox_deep_probe_end = .;
+#ifdef CONFIG_FUZZ
+#define BAREBOX_FUZZ_TESTS \
+ STRUCT_ALIGN(); \
+ __barebox_fuzz_tests_start = .; \
+ KEEP(*(SORT_BY_NAME(.barebox_fuzz_test*))) \
+ __barebox_fuzz_tests_end = .;
+#else
+#define BAREBOX_FUZZ_TESTS
+#endif
+
#ifdef CONFIG_CONSTRUCTORS
#define KERNEL_CTORS() . = ALIGN(8); \
@@ -142,7 +152,8 @@
BAREBOX_DTB \
BAREBOX_PUBLIC_KEYS \
BAREBOX_PCI_FIXUP \
- BAREBOX_DEEP_PROBE
+ BAREBOX_DEEP_PROBE \
+ BAREBOX_FUZZ_TESTS
#if defined(CONFIG_ARCH_BAREBOX_MAX_BARE_INIT_SIZE) && \
CONFIG_ARCH_BAREBOX_MAX_BARE_INIT_SIZE < CONFIG_BAREBOX_MAX_BARE_INIT_SIZE
diff --git a/include/fuzz.h b/include/fuzz.h
new file mode 100644
index 000000000000..35233f90154b
--- /dev/null
+++ b/include/fuzz.h
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022 Google, Inc.
+ * Written by Andrew Scull <ascull@google.com>
+ */
+
+#ifndef __TEST_FUZZ_H
+#define __TEST_FUZZ_H
+
+#include <linux/types.h>
+#include <linux/list.h>
+
+/**
+ * struct fuzz_test - Information about a fuzz test
+ *
+ * @name: Name of fuzz test
+ * @func: Function to call to perform fuzz test on an input
+ */
+struct fuzz_test {
+ const char *name; /* must be first member */
+ int (*func)(const uint8_t * data, size_t size);
+};
+
+extern const struct fuzz_test __barebox_fuzz_tests_start;
+extern const struct fuzz_test __barebox_fuzz_tests_end;
+
+#define for_each_fuzz_test(test) \
+ for (test = &__barebox_fuzz_tests_start; \
+ test != &__barebox_fuzz_tests_end; test++)
+
+#if IS_ENABLED(CONFIG_FUZZ) && IN_PROPER
+/**
+ * fuzz_test() - register a fuzz test
+ *
+ * The fuzz test function must return 0 as other values are reserved for future
+ * use.
+ *
+ * @_name: the name of the fuzz test function
+ */
+#define fuzz_test(_name, _func) \
+ static const struct fuzz_test _func##_entry \
+ __ll_elem(.barebox_fuzz_tests_##_func) = { \
+ .name = _name, \
+ .func = _func, \
+ }
+#else
+#define fuzz_test(_name, _func) \
+ static __always_unused void * _unused##_func = _func
+#endif
+
+static inline int fuzz_test_once(const struct fuzz_test *test, const u8 *data, size_t len)
+{
+ return test->func(data, len);
+}
+
+int call_for_each_fuzz_test(int (*fn)(const struct fuzz_test *test));
+
+int setup_external_fuzz(const char *fuzz_name,
+ int *argc, char ***argv);
+
+#ifdef CONFIG_FUZZ
+bool fuzz_external_active(void);
+#else
+static inline bool fuzz_external_active(void)
+{
+ return false;
+}
+#endif
+
+#endif /* __TEST_FUZZ_H */
diff --git a/lib/Makefile b/lib/Makefile
index 370d4f31d45a..e5cd7ae9f762 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -66,6 +66,7 @@ obj-$(CONFIG_XYMODEM) += xymodem.o
obj-y += unlink-recursive.o
obj-$(CONFIG_STMP_DEVICE) += stmp-device.o
obj-y += wchar.o
+obj-$(CONFIG_FUZZ) += fuzz.o
obj-y += libfile.o
obj-y += bitmap.o
obj-y += gcd.o
diff --git a/lib/fuzz.c b/lib/fuzz.c
new file mode 100644
index 000000000000..084455e365cd
--- /dev/null
+++ b/lib/fuzz.c
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <fuzz.h>
+#include <string.h>
+#include <common.h>
+
+int call_for_each_fuzz_test(int (*fn)(const struct fuzz_test *test))
+{
+ const struct fuzz_test *test;
+ int ret;
+
+ for_each_fuzz_test(test) {
+ ret = fn(test);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+#ifdef CONFIG_FUZZ_EXTERNAL
+const u8 *fuzzer_get_data(size_t *len);
+#else
+static inline const u8 *fuzzer_get_data(size_t *len)
+{
+ return NULL;
+}
+#endif
+
+extern int LLVMFuzzerRunDriver(int *argc, char ***argv,
+ int (*cb)(const uint8_t *, size_t));
+
+static const struct fuzz_test *fuzz;
+static int *saved_argc;
+static char ***saved_argv;
+
+static int fuzzer_run(const uint8_t *buf, size_t len)
+{
+ return fuzz_test_once(fuzz, buf, len);
+}
+
+static int fuzz_main(void)
+{
+ int ret = -1;
+
+ if (IS_ENABLED(CONFIG_FUZZ_EXTERNAL)) {
+ ret = LLVMFuzzerRunDriver(saved_argc, saved_argv, fuzzer_run);
+ pr_emerg("libfuzzer unexpectedly ended: %d\n", ret);
+ } else {
+ pr_emerg("libfuzzer not supported in this build\n");
+ }
+
+ return ret;
+}
+
+int setup_external_fuzz(const char *fuzz_name,
+ int *argc, char ***argv)
+{
+ const struct fuzz_test *test;
+
+ saved_argc = argc;
+ saved_argv = argv;
+
+ for_each_fuzz_test(test) {
+ if (streq_ptr(test->name, fuzz_name)) {
+ fuzz = test;
+ barebox_main = fuzz_main;
+ barebox_loglevel = MSG_CRIT;
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+bool fuzz_external_active(void)
+{
+ return fuzz != NULL;
+}
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index ba87f6c210d2..11be17d8cceb 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -21,6 +21,7 @@
#include <wchar.h>
#include <of.h>
#include <efi.h>
+#include <fuzz.h>
#include <common.h>
#include <pbl.h>
@@ -989,3 +990,17 @@ int asprintf(char **strp, const char *fmt, ...)
return len;
}
EXPORT_SYMBOL(asprintf);
+
+static int __maybe_unused fuzz_printf(const uint8_t *data, size_t size)
+{
+ static bool initialized = false;
+
+ if (!initialized) {
+ printf("initializing\n");
+ initialized = true;
+ }
+
+ printf("%*ph\n", (int)size, data);
+ return 0;
+}
+fuzz_test("printf", fuzz_printf);
diff --git a/scripts/Kconfig.include b/scripts/Kconfig.include
index 55ec8737d0db..93c33d9485b1 100644
--- a/scripts/Kconfig.include
+++ b/scripts/Kconfig.include
@@ -28,6 +28,7 @@ cc-info := $(shell,$(srctree)/scripts/cc-version.sh $(CC))
$(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not supported.)
cc-name := $(shell,set -- $(cc-info) && echo $1)
cc-version := $(shell,set -- $(cc-info) && echo $2)
+clang-runtime-dir = $(shell,$(srctree)/scripts/clang-runtime-dir.sh $(CC))
# $(cc-option,<flag>)
# Return y if the compiler supports <flag>, n otherwise
diff --git a/scripts/clang-runtime-dir.sh b/scripts/clang-runtime-dir.sh
new file mode 100755
index 000000000000..65b3028ad04d
--- /dev/null
+++ b/scripts/clang-runtime-dir.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+CC=${1:-${CC}}
+
+if [ "${CC}" = "" ]; then
+ echo "Error: No compiler specified." >&2
+ printf "Usage:\n\t$0 <clang-command>\n" >&2
+ exit 1
+fi
+
+rundir=$("${CC}" --print-runtime-dir 2>/dev/null)
+if [ ! -e "$rundir" ]; then
+ # Workaround https://github.com/llvm/llvm-project/issues/112458
+ guess=$(dirname "$rundir")/linux
+ if [ -e "$guess" ]; then
+ rundir="$guess"
+ fi
+fi
+echo "$rundir"
diff --git a/test/Kconfig b/test/Kconfig
index 958b483ea946..ca2d75c6b025 100644
--- a/test/Kconfig
+++ b/test/Kconfig
@@ -7,4 +7,41 @@ if TEST
source "test/self/Kconfig"
+config FUZZ
+ bool "fuzz tests"
+ help
+ Say y here to add include fuzz tests into barebox.
+ These need to be exercised either via command or
+ by libfuzzer
+
+config FUZZ_EXTERNAL
+ bool "link against libfuzzer"
+ depends on SANDBOX && CC_IS_CLANG
+ help
+ LibFuzzer is an in-process, coverage-guided, evolutionary fuzzing
+ engine. It can be linked against barebox on the sandbox
+ architecture to feed fuzzed inputs into the fuzz tests.
+
+if FUZZ
+
+config CMD_FUZZ
+ bool "fuzz command"
+ depends on COMMAND_SUPPORT
+ default y
+ help
+ Command to run enabled barebox fuzz tests on fixed input.
+ If run without arguments, all tests are run.
+
+ This is mostly useful for debugging, use FUZZ_EXTERNAL
+ to have fuzzed inputs be bed automatically .
+
+ Usage: fuzz [-ls] -f FILE [test]
+
+ Options:
+ -l list registered fuzz tests
+ -f FILE use FILE as fuzz input
+ -s SIZE read only SIZE bytes from file
+
+endif
+
endif
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 13/21] filetype: add fuzz target
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (11 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 12/21] Add fuzzing infrastructure Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 14/21] block: mark underlying cdev with DEVFS_IS_BLOCK_DEV Ahmad Fatoum
` (7 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
Checking for filetype is an operation that we do on every boot, so it's
important it's done in a memory safe manner. Add a test to exercise
this.
This test has unearthed issues which have already been fixed.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/filetype.c | 12 ++++++++++++
images/Makefile.sandbox | 1 +
include/filetype.h | 4 ++--
3 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/common/filetype.c b/common/filetype.c
index a7bbd8f48534..019f34811103 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -18,6 +18,7 @@
#include <image-sparse.h>
#include <elf.h>
#include <linux/zstd.h>
+#include <fuzz.h>
#include <mach/imx/imx-header.h>
@@ -495,6 +496,17 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
return filetype_unknown;
}
+static int fuzz_filetype(const u8 *data, size_t size)
+{
+ if (!PTR_IS_ALIGNED(data, sizeof(u64)))
+ return -EINVAL;
+
+ file_detect_type(data, size);
+
+ return 0;
+}
+fuzz_test("filetype", fuzz_filetype);
+
int file_name_detect_type_offset(const char *filename, loff_t pos, enum filetype *type,
enum filetype (*detect)(const void *buf, size_t bufsize))
{
diff --git a/images/Makefile.sandbox b/images/Makefile.sandbox
index ed5d740bc1fb..ce09d0c1374c 100644
--- a/images/Makefile.sandbox
+++ b/images/Makefile.sandbox
@@ -3,6 +3,7 @@
SYMLINK_TARGET_barebox = sandbox_main.elf
symlink-$(CONFIG_SANDBOX) += barebox
+fuzzer-$(CONFIG_FILETYPE) += filetype
fuzzer-$(CONFIG_PRINTF_HEXSTR) += printf
ifeq ($(CONFIG_SANDBOX),y)
diff --git a/include/filetype.h b/include/filetype.h
index e699815975d0..e66237878615 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -106,7 +106,7 @@ static inline bool file_is_compressed_file(enum filetype ft)
#define ARM_HEAD_MAGICWORD_OFFSET 0x20
#define ARM_HEAD_SIZE_OFFSET 0x2C
-#ifdef CONFIG_ARM
+#if defined(CONFIG_ARM) || defined(CONFIG_FUZZ)
static inline int is_barebox_arm_head(const char *head)
{
return !strcmp(head + ARM_HEAD_MAGICWORD_OFFSET, "barebox");
@@ -122,7 +122,7 @@ static inline int is_barebox_arm_head(const char *head)
#define MIPS_HEAD_MAGICWORD_OFFSET 0x10
#define MIPS_HEAD_SIZE_OFFSET 0x1C
-#ifdef CONFIG_MIPS
+#if defined(CONFIG_MIPS) || defined(CONFIG_FUZZ)
static inline int is_barebox_mips_head(const char *head)
{
return !strncmp(head + MIPS_HEAD_MAGICWORD_OFFSET, "barebox", 7);
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 14/21] block: mark underlying cdev with DEVFS_IS_BLOCK_DEV
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (12 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 13/21] filetype: add fuzz target Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 15/21] block: add lightweight ramdisk support Ahmad Fatoum
` (6 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
When given a cdev, we detect whether it's a block device by testing that
its ops are those of a block device.
This has the downside that any block device must use the block caching layer.
ramdisks don't benefit from the caching though, so let's add a new flag
bit to mark block devices and use it in preparation for adding ramdisk
support.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/block.c | 9 +--------
include/block.h | 13 +++++++------
include/driver.h | 1 +
3 files changed, 9 insertions(+), 14 deletions(-)
diff --git a/common/block.c b/common/block.c
index d401e979abff..ca2ed37dbd3e 100644
--- a/common/block.c
+++ b/common/block.c
@@ -447,14 +447,6 @@ static struct cdev_operations block_ops = {
.discard_range = block_op_discard_range,
};
-struct block_device *cdev_get_block_device(const struct cdev *cdev)
-{
- if (!cdev || cdev->ops != &block_ops)
- return NULL;
-
- return cdev->priv;
-}
-
int blockdevice_register(struct block_device *blk)
{
loff_t size = (loff_t)blk->num_blocks * BLOCKSIZE(blk);
@@ -465,6 +457,7 @@ int blockdevice_register(struct block_device *blk)
blk->cdev.dev = blk->dev;
blk->cdev.ops = &block_ops;
blk->cdev.priv = blk;
+ blk->cdev.flags |= DEVFS_IS_BLOCK_DEV;
blk->rdbufsize = BUFSIZE >> blk->blockbits;
INIT_LIST_HEAD(&blk->buffered_blocks);
diff --git a/include/block.h b/include/block.h
index 5ce3eb7d7838..73b305cdb03f 100644
--- a/include/block.h
+++ b/include/block.h
@@ -82,14 +82,9 @@ static inline int block_flush(struct block_device *blk)
}
#ifdef CONFIG_BLOCK
-struct block_device *cdev_get_block_device(const struct cdev *cdev);
unsigned file_list_add_blockdevs(struct file_list *files);
char *cdev_get_linux_rootarg(const struct cdev *partcdev);
#else
-static inline struct block_device *cdev_get_block_device(const struct cdev *cdev)
-{
- return NULL;
-}
static inline unsigned file_list_add_blockdevs(struct file_list *files)
{
return 0;
@@ -102,7 +97,8 @@ static inline char *cdev_get_linux_rootarg(const struct cdev *partcdev)
static inline bool cdev_is_block_device(const struct cdev *cdev)
{
- return cdev_get_block_device(cdev) != NULL;
+ return IS_ENABLED(CONFIG_BLOCK) && cdev &&
+ (cdev->flags & DEVFS_IS_BLOCK_DEV);
}
static inline bool cdev_is_block_partition(const struct cdev *cdev)
@@ -116,4 +112,9 @@ static inline bool cdev_is_block_disk(const struct cdev *cdev)
return cdev_is_block_device(cdev) && !cdev_is_partition(cdev);
}
+static inline struct block_device *cdev_get_block_device(const struct cdev *cdev)
+{
+ return cdev_is_block_device(cdev) ? cdev->priv : NULL;
+}
+
#endif /* __BLOCK_H */
diff --git a/include/driver.h b/include/driver.h
index e9a919f9bbb5..41e5dad724c3 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -581,6 +581,7 @@ extern struct list_head cdev_list;
#define DEVFS_PARTITION_FIXED (1U << 0)
#define DEVFS_PARTITION_READONLY (1U << 1)
#define DEVFS_IS_CHARACTER_DEV (1U << 3)
+#define DEVFS_IS_BLOCK_DEV (1U << 4)
#define DEVFS_PARTITION_FROM_OF (1U << 5)
#define DEVFS_PARTITION_FROM_TABLE (1U << 6)
#define DEVFS_IS_MBR_PARTITIONED (1U << 7)
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 15/21] block: add lightweight ramdisk support
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (13 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 14/21] block: mark underlying cdev with DEVFS_IS_BLOCK_DEV Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 16/21] fuzz: add support for passing fuzz data as r/o ramdisk Ahmad Fatoum
` (5 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
The partitions code in barebox expects a block device to operate on and
not raw memory buffers. Add an abstraction that wraps a raw memory
buffer as block device in a very lightweight manner as not to impede
fuzzing speed.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/block/Kconfig | 6 ++
drivers/block/Makefile | 1 +
drivers/block/ramdisk.c | 178 ++++++++++++++++++++++++++++++++++++++++
include/ramdisk.h | 24 ++++++
4 files changed, 209 insertions(+)
create mode 100644 drivers/block/ramdisk.c
create mode 100644 include/ramdisk.h
diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig
index bf8dfdbd5c12..5b1b7789171c 100644
--- a/drivers/block/Kconfig
+++ b/drivers/block/Kconfig
@@ -26,3 +26,9 @@ config EFI_BLK_SEPARATE_USBDISK
for your disk and checking against it with
devlookup -v $bootguid /dev/disk$bootsource_instance guid
+
+config RAMDISK_BLK
+ bool
+ help
+ This symbol is selected by testing code that requires lightweight
+ creation of anonymous block devices backed fully by memory buffers.
diff --git a/drivers/block/Makefile b/drivers/block/Makefile
index a4e14a559cf7..6066b35c313e 100644
--- a/drivers/block/Makefile
+++ b/drivers/block/Makefile
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_EFI_BLK) += efi-block-io.o
obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o
+obj-$(CONFIG_RAMDISK_BLK) += ramdisk.o
diff --git a/drivers/block/ramdisk.c b/drivers/block/ramdisk.c
new file mode 100644
index 000000000000..6f3c9348058a
--- /dev/null
+++ b/drivers/block/ramdisk.c
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// lightweight anonymous block device wrapper around memory buffers
+
+#include <ramdisk.h>
+#include <block.h>
+#include <driver.h>
+#include <fs.h>
+#include <string.h>
+#include <xfuncs.h>
+#include <linux/align.h>
+#include <linux/log2.h>
+
+struct ramdisk {
+ struct block_device blk;
+ struct device dev;
+ union {
+ void *base_rw;
+ const void *base_ro;
+ };
+ size_t size;
+ bool ro;
+};
+
+static int ramdisk_check_read(const struct ramdisk *ramdisk)
+{
+ return ramdisk->base_ro ? 1 : 0;
+}
+
+static ssize_t ramdisk_read(struct cdev *cdev, void *buf, size_t count,
+ loff_t offset, unsigned long flags)
+{
+ struct ramdisk *ramdisk = cdev->priv;
+ size_t cpy_count, pad_count;
+ int ret;
+
+ ret = ramdisk_check_read(ramdisk);
+ if (ret < 1)
+ return ret;
+
+ if (size_add(offset, count) > ramdisk->size)
+ return -ENXIO;
+
+ cpy_count = min_t(size_t, count, ramdisk->size - offset);
+ buf = mempcpy(buf, ramdisk->base_ro + offset, cpy_count);
+
+ pad_count = min_t(size_t, count, cdev->size - offset) - cpy_count;
+
+ memset(buf, 0x00, pad_count);
+
+ pr_vdebug("read %zu bytes\n", cpy_count + pad_count);
+
+ return cpy_count + pad_count;
+}
+
+static int ramdisk_check_write(const struct ramdisk *ramdisk)
+{
+ if (!ramdisk->base_rw)
+ return -EPERM;
+ if (ramdisk->ro)
+ return -EACCES;
+ return 1;
+}
+
+static ssize_t ramdisk_write(struct cdev *cdev, const void *buf, size_t count,
+ loff_t offset, unsigned long flags)
+{
+ struct ramdisk *ramdisk = cdev->priv;
+ int ret;
+
+ ret = ramdisk_check_write(ramdisk);
+ if (ret < 1)
+ return ret;
+ if (size_add(offset, count) > ramdisk->size)
+ return -ENXIO;
+
+ memcpy(ramdisk->base_rw + offset, buf,
+ min_t(size_t, count, ramdisk->size - offset));
+
+ return count;
+}
+
+static int ramdisk_memmap(struct cdev *cdev, void **map, int flags)
+{
+ struct ramdisk *ramdisk = cdev->priv;
+ int ret;
+
+ ret = flags & PROT_WRITE ? ramdisk_check_write(ramdisk)
+ : ramdisk_check_read(ramdisk);
+ if (ret < 1)
+ return ret;
+
+ *map = ramdisk->base_rw;
+ return 0;
+}
+
+static struct cdev_operations ramdisk_ops = {
+ .read = ramdisk_read,
+ .write = ramdisk_write,
+ .memmap = ramdisk_memmap,
+};
+
+struct ramdisk *ramdisk_init(int sector_size)
+{
+ struct ramdisk *ramdisk;
+ struct block_device *blk;
+ int ret;
+
+ ramdisk = xzalloc(sizeof(*ramdisk));
+
+ dev_set_name(&ramdisk->dev, "ramdisk");
+ ramdisk->dev.id = DEVICE_ID_DYNAMIC;
+
+ ret = register_device(&ramdisk->dev);
+ if (ret)
+ return NULL;
+
+ blk = &ramdisk->blk;
+ blk->dev = &ramdisk->dev;
+ blk->type = BLK_TYPE_VIRTUAL;
+
+ blk->cdev.size = 0;
+ blk->cdev.name = xstrdup(dev_name(&ramdisk->dev));
+ blk->cdev.dev = blk->dev;
+ blk->cdev.ops = &ramdisk_ops;
+ blk->cdev.priv = ramdisk;
+ blk->cdev.flags |= DEVFS_IS_BLOCK_DEV;
+ blk->blockbits = ilog2(sector_size);
+
+ ret = devfs_create(&blk->cdev);
+ if (ret)
+ return NULL;
+
+ INIT_LIST_HEAD(&blk->buffered_blocks);
+ INIT_LIST_HEAD(&blk->idle_blocks);
+
+ return ramdisk;
+}
+
+struct block_device *ramdisk_get_block_device(struct ramdisk *ramdisk)
+{
+ return &ramdisk->blk;
+}
+
+void ramdisk_free(struct ramdisk *ramdisk)
+{
+ devfs_remove(&ramdisk->blk.cdev);
+ unregister_device(&ramdisk->dev);
+ free(ramdisk);
+}
+
+static void ramdisk_setup_size(struct ramdisk *ramdisk, size_t size)
+{
+ ramdisk->size = size;
+ ramdisk->blk.cdev.size = ALIGN(size, 1 << ramdisk->blk.blockbits);
+}
+
+void ramdisk_setup_ro(struct ramdisk *ramdisk, const void *data, size_t size)
+{
+ ramdisk->ro = true;
+ ramdisk->base_ro = data;
+ ramdisk_setup_size(ramdisk, size);
+}
+
+const void *ramdisk_mmap(struct ramdisk *ramdisk, loff_t offset,
+ size_t size)
+{
+ if (size_add(offset, size) > ramdisk->size)
+ return NULL;
+
+ return ramdisk->base_ro + offset;
+}
+
+void ramdisk_setup_rw(struct ramdisk *ramdisk, void *data, size_t size)
+{
+ ramdisk->ro = false;
+ ramdisk->base_rw = data;
+ ramdisk_setup_size(ramdisk, size);
+}
diff --git a/include/ramdisk.h b/include/ramdisk.h
new file mode 100644
index 000000000000..b256a9c12ca7
--- /dev/null
+++ b/include/ramdisk.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __RAMDISK_H_
+#define __RAMDISK_H_
+
+#include <linux/types.h>
+
+struct ramdisk;
+struct block_device;
+
+struct ramdisk *ramdisk_init(int sector_size);
+void ramdisk_free(struct ramdisk *ramdisk);
+
+struct block_device *ramdisk_get_block_device(struct ramdisk *ramdisk);
+
+void ramdisk_setup_ro(struct ramdisk *ramdisk, const void *data, size_t size);
+void ramdisk_setup_rw(struct ramdisk *ramdisk, void *data, size_t size);
+
+const void *ramdisk_mmap(struct ramdisk *ramdisk, loff_t offset, size_t size);
+static inline void ramdisk_munmap(struct ramdisk *ramdisk,
+ void *ptr, loff_t offset,
+ size_t size) {}
+
+#endif
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 16/21] fuzz: add support for passing fuzz data as r/o ramdisk
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (14 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 15/21] block: add lightweight ramdisk support Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 17/21] partitions: add partition table parser fuzz target Ahmad Fatoum
` (4 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
This will be useful for fuzzing file systems and partition parsing.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/fuzz.h | 19 ++++++++++++++++++-
test/Kconfig | 2 ++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/include/fuzz.h b/include/fuzz.h
index 35233f90154b..bd95ae6203b0 100644
--- a/include/fuzz.h
+++ b/include/fuzz.h
@@ -8,7 +8,8 @@
#define __TEST_FUZZ_H
#include <linux/types.h>
-#include <linux/list.h>
+#include <linux/compiler_types.h>
+#include <ramdisk.h>
/**
* struct fuzz_test - Information about a fuzz test
@@ -48,6 +49,22 @@ extern const struct fuzz_test __barebox_fuzz_tests_end;
static __always_unused void * _unused##_func = _func
#endif
+#define fuzz_test_ramdisk(_name, _func) \
+ static int _func##_ramdisk(const u8 *data, size_t size) \
+ { \
+ static struct ramdisk *ramdisk; \
+ int ret; \
+ if (!ramdisk) \
+ ramdisk = ramdisk_init(512); \
+ if (!ramdisk) \
+ return -ENODEV; \
+ ramdisk_setup_ro(ramdisk, data, size); \
+ ret = _func(ramdisk_get_block_device(ramdisk)); \
+ ramdisk_setup_ro(ramdisk, NULL, 0); \
+ return ret; \
+ } \
+ fuzz_test(_name, _func##_ramdisk)
+
static inline int fuzz_test_once(const struct fuzz_test *test, const u8 *data, size_t len)
{
return test->func(data, len);
diff --git a/test/Kconfig b/test/Kconfig
index ca2d75c6b025..07b22f09e99b 100644
--- a/test/Kconfig
+++ b/test/Kconfig
@@ -9,6 +9,8 @@ source "test/self/Kconfig"
config FUZZ
bool "fuzz tests"
+ select RAMDISK_BLK
+ select DISK
help
Say y here to add include fuzz tests into barebox.
These need to be exercised either via command or
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 17/21] partitions: add partition table parser fuzz target
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (15 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 16/21] fuzz: add support for passing fuzz data as r/o ramdisk Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 18/21] fdt: add fuzz test Ahmad Fatoum
` (3 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Abdelrahman Youssef, Ahmad Fatoum
Parsing on-disk partition tables is something barebox often does on
every boot, so add a fuzz test to smoke out memory safety issues.
Co-developed-by: Abdelrahman Youssef <abdelrahmanyossef12@gmail.com>
Signed-off-by: Abdelrahman Youssef <abdelrahmanyossef12@gmail.com>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/partitions.c | 56 +++++++++++++++++++++++++++++++++++++++++
images/Makefile.sandbox | 1 +
2 files changed, 57 insertions(+)
diff --git a/common/partitions.c b/common/partitions.c
index 25d5f15721fc..3f618119850d 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -17,6 +17,7 @@
#include <linux/err.h>
#include <partitions.h>
#include <range.h>
+#include <fuzz.h>
static LIST_HEAD(partition_parser_list);
@@ -72,6 +73,21 @@ static int register_one_partition(struct block_device *blk, struct partition *pa
return ret;
}
+static int remove_one_partition(struct block_device *blk, int no)
+{
+ char *partition_name;
+ int ret;
+
+ partition_name = basprintf("%s.%d", blk->cdev.name, no);
+ if (!partition_name)
+ return -ENOMEM;
+
+ ret = devfs_del_partition(partition_name);
+ free(partition_name);
+
+ return ret;
+}
+
static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
{
enum filetype type;
@@ -329,6 +345,46 @@ int partition_parser_register(struct partition_parser *p)
return 0;
}
+/**
+ * Try to collect partition information on the given block device
+ * @param blk Block device to examine
+ * @return 0 most of the time, negative value else
+ *
+ * It is not a failure if no partition information is found
+ */
+static int fuzz_partition_table_parser(struct block_device *ramdisk)
+{
+ struct partition_desc *pdesc;
+ struct partition *part;
+ int rc = 0;
+ struct partition_parser *parser;
+ u8 buf[2 * SECTOR_SIZE] __aligned(8);
+
+ rc = block_read(ramdisk, buf, 0, 2);
+ if (rc != 0)
+ return 0;
+
+ parser = partition_parser_get_by_filetype(buf);
+ if (!parser)
+ return 0;
+
+ pdesc = parser->parse(buf, ramdisk);
+ if (!pdesc)
+ return 0;
+
+ pdesc->parser = parser;
+
+ list_for_each_entry(part, &pdesc->partitions, list) {
+ register_one_partition(ramdisk, part);
+ remove_one_partition(ramdisk, part->num);
+ }
+
+ partition_table_free(pdesc);
+
+ return 0;
+}
+fuzz_test_ramdisk("partitions", fuzz_partition_table_parser);
+
/**
* cdev_unallocated_space - return unallocated space
* cdev: The cdev
diff --git a/images/Makefile.sandbox b/images/Makefile.sandbox
index ce09d0c1374c..b6893d314668 100644
--- a/images/Makefile.sandbox
+++ b/images/Makefile.sandbox
@@ -4,6 +4,7 @@ SYMLINK_TARGET_barebox = sandbox_main.elf
symlink-$(CONFIG_SANDBOX) += barebox
fuzzer-$(CONFIG_FILETYPE) += filetype
+fuzzer-$(CONFIG_PARTITION) += partitions
fuzzer-$(CONFIG_PRINTF_HEXSTR) += printf
ifeq ($(CONFIG_SANDBOX),y)
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 18/21] fdt: add fuzz test
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (16 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 17/21] partitions: add partition table parser fuzz target Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 19/21] fit: " Ahmad Fatoum
` (2 subsequent siblings)
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We have four parsers that operate on device trees in barebox:
- OF unflattener: Used in barebox proper on the same DTs as libfdt,
but additionally also processes FIT images, which are untrusted
- fdt_machine_is_compatible: very minimal device tree parser for
extracting compatibles out of untrusted device trees without
unflattening
- The FIT image hashing code, but this only runs after unflattening
- libfdt: optionally used in PBL. Only operates on trusted input,
either barebox' own device tree or an externally passed device tree
from a previous boot stage
Add fuzz tests for operating on the two parsers that take untrusted
input. Multiple issues have already been found by them in the past and
fixed.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/of/fdt.c | 39 +++++++++++++++++++++++++++++++++++++++
images/Makefile.sandbox | 2 ++
2 files changed, 41 insertions(+)
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 9638b3d238be..84a36c77bbf0 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -12,6 +12,7 @@
#include <malloc.h>
#include <init.h>
#include <memory.h>
+#include <fuzz.h>
#include <linux/sizes.h>
#include <linux/ctype.h>
#include <linux/log2.h>
@@ -355,6 +356,18 @@ struct device_node *of_unflatten_dtb_const(const void *infdt, int size)
return __of_unflatten_dtb(infdt, size, true);
}
+static int fuzz_dtb(const u8 *data, size_t size)
+{
+ struct device_node *np;
+
+ np = of_unflatten_dtb_const(data, size);
+ if (!IS_ERR(np))
+ of_delete_node(np);
+
+ return 0;
+}
+fuzz_test("dtb", fuzz_dtb);
+
struct fdt {
void *dt;
uint32_t dt_nextofs;
@@ -812,3 +825,29 @@ int fdt_machine_is_compatible(const struct fdt_header *fdt, size_t fdt_size, con
return 0;
}
+
+/*
+ * In order to randomize all inputs to fdt_machine_is_compatible,
+ * we use the last 32 bytes of the random data as a compatible.
+ * As there maybe embedded nul bytes, the size thus varies
+ * between 0 and 31 bytes.
+ * of
+ */
+#define COMPAT_THRESHOLD 768
+#define COMPAT_LEN 32
+
+static int fuzz_fdt_compatible(const u8 *data, size_t size)
+{
+ char compat[32] = "barebox,sandbox";
+
+ if (size > COMPAT_THRESHOLD) {
+ size -= COMPAT_LEN;
+ memcpy(compat, &data[COMPAT_THRESHOLD - COMPAT_LEN], COMPAT_LEN);
+ compat[COMPAT_LEN - 1] = '\0';
+ }
+
+ fdt_machine_is_compatible((const void *)data, size, compat);
+
+ return 0;
+}
+fuzz_test("fdt-compatible", fuzz_fdt_compatible);
diff --git a/images/Makefile.sandbox b/images/Makefile.sandbox
index b6893d314668..87963e2f432f 100644
--- a/images/Makefile.sandbox
+++ b/images/Makefile.sandbox
@@ -4,6 +4,8 @@ SYMLINK_TARGET_barebox = sandbox_main.elf
symlink-$(CONFIG_SANDBOX) += barebox
fuzzer-$(CONFIG_FILETYPE) += filetype
+fuzzer-$(CONFIG_OFTREE) += dtb
+fuzzer-$(CONFIG_OFTREE) += fdt-compatible
fuzzer-$(CONFIG_PARTITION) += partitions
fuzzer-$(CONFIG_PRINTF_HEXSTR) += printf
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 19/21] fit: add fuzz test
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (17 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 18/21] fdt: add fuzz test Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 20/21] Documentation: add LLVM libfuzzer documentation Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 21/21] sandbox: add support for coverage info generation Ahmad Fatoum
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Abdelrahman Youssef, Steffen Trumtrar, Ahmad Fatoum
We require FIT images on non-EFI systems to implement verified boot
chains. Unfortunately, FIT is a relatively complex format for that use
case, so a fuzz test exercising the parser is pretty much in order.
Co-developed-by: Abdelrahman Youssef <abdelrahmanyossef12@gmail.com>
Signed-off-by: Abdelrahman Youssef <abdelrahmanyossef12@gmail.com>
Co-developed-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/image-fit.c | 76 +++++++++++++++++++++++++++++++++++++++--
images/Makefile.sandbox | 1 +
2 files changed, 75 insertions(+), 2 deletions(-)
diff --git a/common/image-fit.c b/common/image-fit.c
index 0cc0425284c5..5006394eb7bb 100644
--- a/common/image-fit.c
+++ b/common/image-fit.c
@@ -23,6 +23,7 @@
#include <crypto/public_key.h>
#include <uncompress.h>
#include <image-fit.h>
+#include <fuzz.h>
#define FDT_MAX_DEPTH 32
#define FDT_MAX_PATH_LEN 200
@@ -825,6 +826,26 @@ static int fit_find_compatible_unit(struct fit_handle *handle,
return -ENOENT;
}
+static int fit_find_last_unit(struct fit_handle *handle,
+ const char **out_unit)
+{
+ struct device_node *conf_node = handle->configurations;
+ struct device_node *child;
+ const char *unit = NULL;
+
+ if (!conf_node)
+ return 0;
+
+ for_each_child_of_node(conf_node, child)
+ unit = child->name;
+
+ if (!unit)
+ return -ENOENT;
+
+ *out_unit = unit;
+ return 0;
+}
+
/**
* fit_open_configuration - open a FIT configuration
* @handle: The FIT image handle
@@ -970,12 +991,16 @@ struct fit_handle *fit_open(const char *filename, bool verbose,
return handle;
}
-void fit_close(struct fit_handle *handle)
+static void __fit_close(struct fit_handle *handle)
{
if (handle->root)
of_delete_node(handle->root);
-
free(handle->fit_alloc);
+}
+
+void fit_close(struct fit_handle *handle)
+{
+ __fit_close(handle);
free(handle);
}
@@ -997,3 +1022,50 @@ static int bootm_fit_register(void)
return register_image_handler(&fit_handler);
}
late_initcall(bootm_fit_register);
+
+static int fuzz_fit(const u8 *data, size_t size)
+{
+ const char *unit, *imgname = "kernel";
+ struct fit_handle handle = {};
+ const void *outdata;
+ unsigned long outsize, addr;
+ int ret;
+ void *config;
+
+ handle.verbose = false;
+ handle.verify = BOOTM_VERIFY_AVAILABLE;
+
+ handle.size = size;
+ handle.fit = data;
+ handle.fit_alloc = NULL;
+
+ ret = fit_do_open(&handle);
+ if (ret)
+ goto out;
+
+ config = fit_open_configuration(&handle, NULL);
+ if (IS_ERR(config)) {
+ ret = fit_find_last_unit(&handle, &unit);
+ if (ret)
+ goto out;
+ config = fit_open_configuration(&handle, unit);
+ }
+ if (IS_ERR(config)) {
+ ret = PTR_ERR(config);
+ goto out;
+ }
+
+ ret = fit_open_image(&handle, config, imgname, &outdata, &outsize);
+ if (ret)
+ goto out;
+
+ fit_get_image_address(&handle, config, imgname, "load", &addr);
+ fit_get_image_address(&handle, config, imgname, "entry", &addr);
+
+ ret = fit_open_image(&handle, NULL, imgname, &outdata, &outsize);
+out:
+ __fit_close(&handle);
+
+ return 0;
+}
+fuzz_test("fit", fuzz_fit);
diff --git a/images/Makefile.sandbox b/images/Makefile.sandbox
index 87963e2f432f..b235a1195a7f 100644
--- a/images/Makefile.sandbox
+++ b/images/Makefile.sandbox
@@ -4,6 +4,7 @@ SYMLINK_TARGET_barebox = sandbox_main.elf
symlink-$(CONFIG_SANDBOX) += barebox
fuzzer-$(CONFIG_FILETYPE) += filetype
+fuzzer-$(CONFIG_FITIMAGE) += fit
fuzzer-$(CONFIG_OFTREE) += dtb
fuzzer-$(CONFIG_OFTREE) += fdt-compatible
fuzzer-$(CONFIG_PARTITION) += partitions
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 20/21] Documentation: add LLVM libfuzzer documentation
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (18 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 19/21] fit: " Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 21/21] sandbox: add support for coverage info generation Ahmad Fatoum
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
Now that first fuzzing support is in place, add a defconfig and document
how to use it to fuzz.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Documentation/devel/devel.rst | 1 +
Documentation/devel/fuzzing.rst | 106 +++++++++++++++++++++++++
arch/sandbox/Makefile | 5 +-
common/boards/configs/libfuzzer.config | 14 ++++
4 files changed, 125 insertions(+), 1 deletion(-)
create mode 100644 Documentation/devel/fuzzing.rst
create mode 100644 common/boards/configs/libfuzzer.config
diff --git a/Documentation/devel/devel.rst b/Documentation/devel/devel.rst
index 3e9d44218334..d985bff40d42 100644
--- a/Documentation/devel/devel.rst
+++ b/Documentation/devel/devel.rst
@@ -12,6 +12,7 @@ Contents:
filesystems
background-execution
project-ideas
+ fuzzing
* :ref:`search`
* :ref:`genindex`
diff --git a/Documentation/devel/fuzzing.rst b/Documentation/devel/fuzzing.rst
new file mode 100644
index 000000000000..3151246aef1a
--- /dev/null
+++ b/Documentation/devel/fuzzing.rst
@@ -0,0 +1,106 @@
+Fuzzing barebox
+===============
+
+As described in the :ref:`security` chapter, some parts of barebox need to
+deal with untrusted inputs. To aid in finding and fixing issues that might
+be exploited, barebox can be built with LLVM's libfuzzer to exercise
+these security-critical parsers.
+
+Building
+^^^^^^^^
+
+The barebox sandbox architecture has support for libfuzzer when compiled with
+LLVM. The ``libfuzzer_defconfig`` enables it as well as different hardening
+options to crash barebox on detection of memory safety issues::
+
+ $ export LLVM=1 # or e.g. LLVM=-19, if clang is called clang-19
+ $ make libfuzzer_defconfig
+ $ make -j$(nproc)
+ # [snip]
+ images built:
+ barebox
+ fuzz-filetype
+ fuzz-fit
+ fuzz-fs
+ fuzz-dtb
+ fuzz-fdt-compatible
+ fuzz-partitions
+
+All fuzzers generated are symlinks to the same barebox executable. barebox
+will detect that it was invoked via symlink and switch to fuzzing mode.
+
+Fuzzing
+^^^^^^^
+
+Fuzzers can be run directly or by invoked the main barebox binary with the
+``--fuzz`` option. The latter is mostly useful for debugging.
+
+Examples of running the fuzzers::
+
+ # Just run the fuzzer with no corpus
+ images/fuzz-filetype
+
+ # Multi-threaded fuzzing is recommended as is using a corpus
+ images/fuzz-dtb -rss_limit_mb=10000 -max_len=51200 -jobs=64 \
+ ../barebox-fuzz-corpora/dtb
+
+ # Some fuzzers still leak, so disable leak detection till resolved
+ images/fuzz-fit -max_total_time=600 -rss_limit_mb=20000 -max_len=128000 -detect_leaks=0
+
+ # Debug a crash
+ gdb --args images/fuzz-fit crash-$HASH
+
+When a crash is detected, libfuzzer will create a ``crash-$HASH`` file
+that can be passed instead of the corpus directory to run the fuzz test
+once.
+
+Corpora
+^^^^^^^
+
+We maintain a corpus for every fuzz test on
+`Github <https://github.com/barebox/barebox-fuzz-corpora>`_.
+
+This helps bootstrap the fuzzer, so it can exercise new paths more quickly.
+
+Adding a fuzzer
+^^^^^^^^^^^^^^^
+
+The barebox integration of libfuzzer is a bit unusual; barebox supplies
+its own ``main()`` and calls into libfuzzer instead of the over way round.
+
+This allows us to write fuzz tests naturally inline without having
+to setup things beforehand as barebox will have already executed all
+of its initcalls for example.
+
+To add a new fuzz test, just add a function next to the parser that
+parses a memory buffer::
+
+ #include <fuzz.h>
+
+ static int fuzz_dtb(const u8 *data, size_t size)
+ {
+ struct device_node *np;
+
+ np = of_unflatten_dtb_const(data, size);
+ if (!IS_ERR(np))
+ of_delete_node(np);
+
+ return 0;
+ }
+ fuzz_test("dtb", fuzz_dtb);
+
+
+.. note:: Fuzz tests should not leak memory, otherwise
+ the fuzzing process may abort eventually due to memory exhaustion.
+
+This function than needs to be registered by name in
+``images/Makefile.sandbox``::
+
+ fuzzer-$(CONFIG_OFTREE) += dtb
+
+Searching the source tree for ``fuzz_test`` will show more examples,
+e.g. how to wrap the received buffer in a ramdisk to interface
+with code that requires block devices.
+
+When adding a new fuzzing test, please also `submit a pullrequest
+with a corpus <https://github.com/barebox/barebox-fuzz-corpora/compare>_.
diff --git a/arch/sandbox/Makefile b/arch/sandbox/Makefile
index 6566cd563ed8..f33d7fa961da 100644
--- a/arch/sandbox/Makefile
+++ b/arch/sandbox/Makefile
@@ -2,13 +2,16 @@
KBUILD_DEFCONFIG := sandbox_defconfig
-generated_configs += headless_defconfig noshell_defconfig lockdown_defconfig
+generated_configs += headless_defconfig noshell_defconfig lockdown_defconfig \
+ libfuzzer_defconfig
headless_defconfig:
$(call merge_into_defconfig,sandbox_defconfig,headless)
noshell_defconfig:
$(call merge_into_defconfig,sandbox_defconfig,noshell)
lockdown_defconfig:
$(call merge_into_defconfig,sandbox_defconfig,headless noshell)
+libfuzzer_defconfig:
+ $(call merge_into_defconfig,sandbox_defconfig,libfuzzer)
KBUILD_CPPFLAGS += -D__SANDBOX__ -fno-strict-aliasing -fvisibility=hidden
diff --git a/common/boards/configs/libfuzzer.config b/common/boards/configs/libfuzzer.config
new file mode 100644
index 000000000000..df03beac0ec8
--- /dev/null
+++ b/common/boards/configs/libfuzzer.config
@@ -0,0 +1,14 @@
+CONFIG_BOOTM_FITIMAGE=y
+CONFIG_PRINTF_FULL=y
+CONFIG_BUG_ON_DATA_CORRUPTION=y
+CONFIG_UBSAN=y
+CONFIG_UBSAN_NO_ALIGNMENT=y
+CONFIG_ASAN=y
+CONFIG_FORTIFY_SOURCE=y
+CONFIG_HWRNG=y
+CONFIG_HWRNG_DEV_RANDOM=y
+CONFIG_STACKPROTECTOR_STRONG=y
+CONFIG_TEST=y
+CONFIG_FUZZ=y
+CONFIG_FUZZ_EXTERNAL=y
+CONFIG_CMD_FUZZ=y
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 21/21] sandbox: add support for coverage info generation
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
` (19 preceding siblings ...)
2025-06-05 11:35 ` [PATCH 20/21] Documentation: add LLVM libfuzzer documentation Ahmad Fatoum
@ 2025-06-05 11:35 ` Ahmad Fatoum
20 siblings, 0 replies; 22+ messages in thread
From: Ahmad Fatoum @ 2025-06-05 11:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
To be able to check how well along the fuzzer can descend into the
parsers, add first coverage support and a target to generate HTML
coverage information.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
.gitignore | 6 ++++++
Documentation/devel/fuzzing.rst | 30 ++++++++++++++++++++++++++++++
Makefile | 23 ++++++++++++++++++++++-
arch/sandbox/Kconfig.debug | 7 +++++++
arch/sandbox/Makefile | 7 +++++++
5 files changed, 72 insertions(+), 1 deletion(-)
diff --git a/.gitignore b/.gitignore
index 0bee67af4881..c37188a9f315 100644
--- a/.gitignore
+++ b/.gitignore
@@ -98,3 +98,9 @@ GTAGS
/allrandom.config
/allyes.config
/compile_commands.json
+
+# coverage data
+default.profdata
+default.profraw
+coverage.info
+coverage_html/
diff --git a/Documentation/devel/fuzzing.rst b/Documentation/devel/fuzzing.rst
index 3151246aef1a..4b6d565a470a 100644
--- a/Documentation/devel/fuzzing.rst
+++ b/Documentation/devel/fuzzing.rst
@@ -62,6 +62,36 @@ We maintain a corpus for every fuzz test on
This helps bootstrap the fuzzer, so it can exercise new paths more quickly.
+Determining Source Code Coverage
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+.. note::
+ Coverage instrumentation is currently only supported with LLVM
+ and sandbox.
+
+To collect coverage information, barebox must be built with ``CONFIG_GCOV=y``.
+The linking process will take much longer than usual, but once done, running
+barebox will produce coverage information.
+
+.. code-block:: bash
+
+ images/fuzz-filetype -max_total_time=60 -max_len=2048
+
+After the process exists regularly (i.e., not aborted with ctrl+C!),
+it will produce a ``default.profraw`` file, which needs to be further
+processed:
+
+.. code-block:: bash
+
+ make coverage-html
+
+This will produce a ``${KBUILD_OUTPUT}/coverage_html/`` directory, which can be
+inspected by a web browser:
+
+.. code-block:: bash
+
+ firefox coverage_html/index.html
+
Adding a fuzzer
^^^^^^^^^^^^^^^
diff --git a/Makefile b/Makefile
index df0770e832e1..80a403f61dda 100644
--- a/Makefile
+++ b/Makefile
@@ -429,6 +429,8 @@ OBJCOPY = $(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)
OBJDUMP = $(LLVM_PREFIX)llvm-objdump$(LLVM_SUFFIX)
READELF = $(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)
STRIP = $(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)
+PROFDATA = $(LLVM_PREFIX)llvm-profdata$(LLVM_SUFFIX)
+COV = $(LLVM_PREFIX)llvm-cov$(LLVM_SUFFIX)
else
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
@@ -450,6 +452,7 @@ PERL = perl
PYTHON3 = python3
CHECK = sparse
MKIMAGE = mkimage
+GENHTML = genhtml
BASH = bash
KGZIP = gzip
KBZIP2 = bzip2
@@ -518,7 +521,7 @@ LDFLAGS_elf += $(LDFLAGS_common) --nmagic -s
export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC CXX
export CPP AR NM STRIP OBJCOPY OBJDUMP MAKE AWK GENKSYMS PERL PYTHON3 UTS_MACHINE
-export LEX YACC
+export LEX YACC PROFDATA COV GENHTML
export HOSTCXX CHECK CHECKFLAGS MKIMAGE
export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ
export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
@@ -1418,6 +1421,24 @@ endif
@echo 'Execute "make" or "make all" to build all targets marked with [*] '
@echo 'For further info see the documentation'
+# Code Coverage
+# ---------------------------------------------------------------------------
+
+barebox.coverage_html: barebox.coverage-info
+ genhtml -o $@ $<
+
+barebox.coverage-info: default.profdata
+ $(COV) export --format=lcov -instr-profile $< $(objtree)/barebox >$@
+
+default.profdata: $(srctree)/default.profraw
+ $(PROFDATA) merge -sparse $< -o $@
+
+# We intentionally don't depend on barebox being built as that can take >10
+# minutes when coverage is enabled
+PHONY += coverage-html
+coverage-html: barebox.coverage_html
+ @echo "HTML coverage generated to $(objtree)/$<"
+
# Generate tags for editors
# ---------------------------------------------------------------------------
quiet_cmd_tags = GEN $@
diff --git a/arch/sandbox/Kconfig.debug b/arch/sandbox/Kconfig.debug
index 4a754e389964..82ee355815c3 100644
--- a/arch/sandbox/Kconfig.debug
+++ b/arch/sandbox/Kconfig.debug
@@ -8,3 +8,10 @@ config ASAN
This is the hosted implementation for sandbox as opposed to
KASAN, which is the bare-metal implementation.
+
+config GCOV
+ bool "Enable gcov support"
+ depends on CC_IS_CLANG
+ help
+ This option allows developers to retrieve coverage data from a sandbox
+ session. Note that this will greatly increases link times.
diff --git a/arch/sandbox/Makefile b/arch/sandbox/Makefile
index f33d7fa961da..f9d79e9a7d15 100644
--- a/arch/sandbox/Makefile
+++ b/arch/sandbox/Makefile
@@ -79,6 +79,13 @@ SANDBOX_LIBS += -Wl,-Bstatic -L"$(CONFIG_CLANG_RUNTIME_DIR)" \
-lclang_rt.fuzzer_no_main-$(LIBARCH-y) -Wl,-Bdynamic
endif
+ifeq ($(CONFIG_GCOV),y)
+GCOV_OPT-$(CONFIG_CC_IS_CLANG) = -fprofile-instr-generate -fcoverage-mapping
+GCOV_OPT-$(CONFIG_CC_IS_GCC) = -fprofile-arcs -ftest-coverage
+KBUILD_CFLAGS += $(GCOV_OPT-y)
+BAREBOX_LDFLAGS += $(GCOV_OPT-y)
+endif
+
ifeq ($(CONFIG_SANDBOX_LINUX_I386),y)
KBUILD_CFLAGS += -m32
KBUILD_LDFLAGS += -m elf_i386
--
2.39.5
^ permalink raw reply [flat|nested] 22+ messages in thread
end of thread, other threads:[~2025-06-05 11:39 UTC | newest]
Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-05 11:35 [PATCH 00/21] sandbox: add libfuzzer-based fuzzing Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 01/21] pbl: add provision for architectures without piggy loader Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 02/21] firmware: make Layerscape FMan firmware proper-only Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 03/21] mci: sdhci: support compiling common SDHCI code for sandbox PBL Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 04/21] kbuild: define and use more generic symlink command Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 05/21] kbuild: collect compatibility symlink creation in symlink-y Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 06/21] kbuild: allow customizing barebox proper binary Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 07/21] sandbox: make available all CONFIG_ symbols to OS glue code Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 08/21] sandbox: switch to using PBL Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 09/21] kbuild: populate non-host CXX variables Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 10/21] string: add fortify source support Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 11/21] sandbox: populate UNAME_M variable Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 12/21] Add fuzzing infrastructure Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 13/21] filetype: add fuzz target Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 14/21] block: mark underlying cdev with DEVFS_IS_BLOCK_DEV Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 15/21] block: add lightweight ramdisk support Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 16/21] fuzz: add support for passing fuzz data as r/o ramdisk Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 17/21] partitions: add partition table parser fuzz target Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 18/21] fdt: add fuzz test Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 19/21] fit: " Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 20/21] Documentation: add LLVM libfuzzer documentation Ahmad Fatoum
2025-06-05 11:35 ` [PATCH 21/21] sandbox: add support for coverage info generation Ahmad Fatoum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox