Skip to contentSkip to conversation

leancrypto: Fix FIPS library link with lld on musl

PATCH New Khem Raj 1 participant 3 messages Oct 20

lore
Share & tools
lore

Khem Raj

author
#0 lore raw

PATCH 1/3 leancrypto: Fix FIPS library link with lld on musl

The FIPS integrity linker script takes ADDR(.init) for the hashed regions, but musl provides no .init section on riscv64. GNU ld accepts the reference to the missing section and yields an empty range, while lld errors out:

ld.lld: error: internal/src/fips_integrity_check.ld:11: undefined section .init

Use ADDR(.init) only when the section has a size, and otherwise an empty range at the start of .text, matching GNU ld behaviour.

AI-Generated: Uses Claude Code

---
 ...cript-Handle-a-missing-.init-section.patch | 39 +++++++++++++++++++
 .../leancrypto/leancrypto_1.9.1.bb            |  1 +
 2 files changed, 40 insertions(+)
 create mode 100644 meta-oe/recipes-crypto/leancrypto/files/0002-Linker-script-Handle-a-missing-.init-section.patch
meta-oe/recipes-crypto/leancrypto/files/0002-Linker-script-Handle-a-missing-.init-section.patch new file +39 -0
new file mode 100644
index 0000000000..364f223a8e
@@ -0,0 +1,39 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Khem Raj <[email protected]>
+Date: Mon, 19 Oct 2026 22:58:25 -0700
+Subject: [PATCH] Linker script: Handle a missing .init section
+
+Not every C library provides a .init section; musl, for example, has
+no crti.o .init/.fini code on riscv64 and only uses .init_array. The
+linker script then refers to ADDR(.init) of a section which does not
+exist. GNU ld accepts this and yields an empty range, but LLD rejects
+it and the link fails:
+
+  ld.lld: error: internal/src/fips_integrity_check.ld:11: undefined section .init
+  ld.lld: error: internal/src/fips_integrity_check.ld:12: undefined section .init
+
+Only use ADDR(.init) when the section has a size, and otherwise provide
+an empty range at the start of .text, which matches what GNU ld does.
+The symbol values are unchanged whenever .init exists.
+
+Upstream-Status: Pending
+Signed-off-by: Khem Raj <[email protected]>
+---
+ internal/src/fips_integrity_check.ld | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/internal/src/fips_integrity_check.ld b/internal/src/fips_integrity_check.ld
+index 61397f5..e803d2f 100644
+--- a/internal/src/fips_integrity_check.ld
++++ b/internal/src/fips_integrity_check.ld
+@@ -8,8 +8,8 @@ SECTIONS
+ 	.lc_fips_integrity_hash :
+ 	{
+ 		/* Executable code */
+-		PROVIDE(lc_fips_start_init = ADDR(.init));
+-		PROVIDE(lc_fips_end_init = ADDR(.init) + SIZEOF(.init));
++		PROVIDE(lc_fips_start_init = SIZEOF(.init) ? ADDR(.init) : ADDR(.text));
++		PROVIDE(lc_fips_end_init = SIZEOF(.init) ? ADDR(.init) + SIZEOF(.init) : ADDR(.text));
+
+ 		/* Executable code */
+ 		PROVIDE(lc_fips_start_text = ADDR(.text));
meta-oe/recipes-crypto/leancrypto/leancrypto_1.9.1.bb +1 -0
index 083b70fe8c..c5c7bc104a 100644
@@ -14,6 +14,7 @@ SECTION = "libs"
 SRC_URI = "git://github.com/smuellerDD/leancrypto.git;branch=master;protocol=https;tag=v${PV} \
            file://leancrypto-tests.sh \
            file://0001-secure_execution_linux-drop-redundant-linux-prctl.h-.patch \
+           file://0002-Linker-script-Handle-a-missing-.init-section.patch \
            "
 SRCREV = "65eb4e7e84e3a5499460a66b1623ccf6c59628b1"
 
author
#1 lore raw

PATCH 2/3 leancrypto: Fix FIPS integrity digest generation when cross compiling

The script that inserts the FIPS integrity digest into libleancrypto-fips runs the build host's objcopy, which cannot read the target ELF file, and it hides the failure because "exit $?" after echo returns 0. The library is then installed with the placeholder digest, its integrity self-test fails at load time and all algorithms are put into the error state.

Make the script use $OBJCOPY, which points to the target objcopy, keep the exit status of failing commands, and tolerate a missing .init section which llvm-objcopy otherwise errors out on.

Tested by loading the riscv64 musl library under qemu-riscv64: the integrity test now passes and FIPS140Mode is reported as true.

AI-Generated: Uses Claude Code

---
 ...lder-Use-OBJCOPY-and-report-failures.patch | 96 +++++++++++++++++++
 .../leancrypto/leancrypto_1.9.1.bb            |  1 +
 2 files changed, 97 insertions(+)
 create mode 100644 meta-oe/recipes-crypto/leancrypto/files/0003-FIPS-checker-builder-Use-OBJCOPY-and-report-failures.patch
meta-oe/recipes-crypto/leancrypto/files/0003-FIPS-checker-builder-Use-OBJCOPY-and-report-failures.patch new file +96 -0
new file mode 100644
index 0000000000..4aa2432db6
@@ -0,0 +1,96 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Khem Raj <[email protected]>
+Date: Mon, 19 Oct 2026 23:09:52 -0700
+Subject: [PATCH] FIPS checker builder: Use $OBJCOPY and report failures
+
+The script that inserts the FIPS integrity digest into leancrypto-fips
+runs the build machine's objcopy. When cross compiling, that objcopy
+usually cannot read the target ELF file. The failure is not noticed
+either: "exit $?" after echo returns the status of echo, so the
+script exits 0, and the library is installed with the placeholder
+digest. At runtime the integrity test then fails and all algorithms
+are put into the error state:
+
+  ERROR: objcopy command failed: 0
+
+Use $OBJCOPY when set, as cross build environments export the objcopy
+for the target there, and keep the exit status so errors fail the
+build.
+
+llvm-objcopy fails when a section to dump does not exist, and some C
+libraries (e.g. musl on RISC-V) provide no .init section. Extract the
+init section separately and use an empty file when it is not present,
+which matches the empty range the linker script provides for it.
+
+Upstream-Status: Pending
+Signed-off-by: Khem Raj <[email protected]>
+---
+ addon/fips_integrity_checker_elf_generator.sh | 34 +++++++++++++------
+ 1 file changed, 23 insertions(+), 11 deletions(-)
+
+diff --git a/addon/fips_integrity_checker_elf_generator.sh b/addon/fips_integrity_checker_elf_generator.sh
+index 10628f5..1675f23 100755
+--- a/addon/fips_integrity_checker_elf_generator.sh
++++ b/addon/fips_integrity_checker_elf_generator.sh
+@@ -9,7 +9,7 @@ RODATASEGMENT=".lc_fips_rodata"
+ FIPSDATASEGMENT=".lc_fips_integrity_data"
+ SECOUTFILE="extracted_sections.digest"
+
+-OBJCOPY="objcopy"
++OBJCOPY="${OBJCOPY:-objcopy}"
+ CAT="cat"
+ CUT="cut"
+
+@@ -43,30 +43,42 @@ touch $RODATASEGMENT
+ # Extract sections
+ $OBJCOPY							\
+  --dump-section $TEXTSEGMENT=$TEXTSEGMENT			\
+- --dump-section $INITSEGMENT=$INITSEGMENT			\
+  --dump-section $RODATASEGMENT=$RODATASEGMENT			\
+- $SOFILE 2>/dev/null
++ $SOFILE
++ret=$?
++if [ $ret -ne 0 ]
++then
++	echo "ERROR: $OBJCOPY command failed: $ret"
++	exit $ret
++fi
++
++# Not every C library provides an init section (e.g. musl on RISC-V). The
++# linker script then defines an empty init range, which matches the empty
++# file created above. llvm-objcopy fails on a missing section, so extract it
++# separately and only when it is present.
++$OBJCOPY --dump-section $INITSEGMENT=$INITSEGMENT $SOFILE 2>/dev/null
+ if [ $? -ne 0 ]
+ then
+-	echo "ERROR: $OBJCOPY command failed: $?"
+-	exit $?
++	: > $INITSEGMENT
+ fi
+
+ # Merge sections into file and create digest
+ $CAT $TEXTSEGMENT $INITSEGMENT $RODATASEGMENT | $HASHER -b - > $SECOUTFILE 2>/dev/null
+-if [ $? -ne 0 ]
++ret=$?
++if [ $ret -ne 0 ]
+ then
+-	echo "ERROR: $CAT command failed: $?"
+-	exit $?
++	echo "ERROR: $CAT command failed: $ret"
++	exit $ret
+ fi
+
+ # Insert data into library file using an atomic operation
+ trap "rm -f $SOFILE.$$" 0 1 2 3 15
+ cp $SOFILE $SOFILE.$$
+ $OBJCOPY --update-section $FIPSDATASEGMENT="$SECOUTFILE" $SOFILE.$$
+-if [ $? -ne 0 ]
++ret=$?
++if [ $ret -ne 0 ]
+ then
+-	echo "ERROR: $OBJCOPY command failed: $?"
+-	exit $?
++	echo "ERROR: $OBJCOPY command failed: $ret"
++	exit $ret
+ fi
+ mv -f $SOFILE.$$ $SOFILE
meta-oe/recipes-crypto/leancrypto/leancrypto_1.9.1.bb +1 -0
index c5c7bc104a..17f7f0a7f7 100644
@@ -15,6 +15,7 @@ SRC_URI = "git://github.com/smuellerDD/leancrypto.git;branch=master;protocol=htt
            file://leancrypto-tests.sh \
            file://0001-secure_execution_linux-drop-redundant-linux-prctl.h-.patch \
            file://0002-Linker-script-Handle-a-missing-.init-section.patch \
+           file://0003-FIPS-checker-builder-Use-OBJCOPY-and-report-failures.patch \
            "
 SRCREV = "65eb4e7e84e3a5499460a66b1623ccf6c59628b1"
 
author
#2 lore raw

PATCH 3/3 frr: Fix build with bison rejecting paths in output directives

In reply to Khem Raj

bison now carries the fix for CVE-2026-56390, which rejects '/' in %header/%defines and %output file names, so generating the command parser fails:

lib/command_parse.y:20.10-30: error: invalid %header file name ignored lib/command_parse.y:21.10-30: error: invalid %output file name ignored

Backport the upstream fix which drops these directives and passes the output file name with -o instead.

AI-Generated: Uses Claude Code

---
 ...ths-in-bison-output-defines-directiv.patch | 54 +++++++++++++++++++
 .../recipes-protocols/frr/frr_10.6.2.bb       |  1 +
 2 files changed, 55 insertions(+)
 create mode 100644 meta-networking/recipes-protocols/frr/frr/0001-lib-Don-t-use-paths-in-bison-output-defines-directiv.patch
meta-networking/recipes-protocols/frr/frr/0001-lib-Don-t-use-paths-in-bison-output-defines-directiv.patch new file +54 -0
new file mode 100644
index 0000000000..ac732d9ebc
@@ -0,0 +1,54 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Donatas Abraitis <[email protected]>
+Date: Tue, 13 Oct 2026 15:33:02 +0300
+Subject: [PATCH] lib: Don't use paths in bison %output/%defines directives
+
+The fix for CVE-2026-56390 rejects any '/' in %output and %header (%defines)
+file names. The directive is then ignored, and bison reports an error
+and exits non-zero, which breaks the build:
+
+    invalid %output file name ignored
+    invalid %header file name ignored
+
+We only used these directives to place the generated files in lib/,
+because the .y.c rule ran bison without -o. Drop them and pass
+`-o $@` instead. With -d, bison derives lib/command_parse.h from the
+-o name, so the generated files stay the same.
+
+Signed-off-by: Donatas Abraitis <[email protected]>
+
+Upstream-Status: Backport [https://github.com/FRRouting/frr/commit/b7ad39d799d98c982713b29c1d0ba6d49691308b]
+Signed-off-by: Khem Raj <[email protected]>
+---
+ lib/command_parse.y | 4 ----
+ lib/subdir.am       | 2 +-
+ 2 files changed, 1 insertion(+), 5 deletions(-)
+
+diff --git a/lib/command_parse.y b/lib/command_parse.y
+index bc04bc93bb4c..4d9377e5df87 100644
+--- a/lib/command_parse.y
++++ b/lib/command_parse.y
+@@ -16,10 +16,6 @@
+ %define api.pure full
+ /* define api.prefix {cmd_yy} */
+
+-/* names for generated header and parser files */
+-%defines "lib/command_parse.h"
+-%output  "lib/command_parse.c"
+-
+ /* note: code blocks are output in order, to both .c and .h:
+  *  1. %code requires
+  *  2. %union + bison forward decls
+diff --git a/lib/subdir.am b/lib/subdir.am
+index 20947c306d60..dafebacf8e7a 100644
+--- a/lib/subdir.am
++++ b/lib/subdir.am
+@@ -537,7 +537,7 @@ CLEANFILES += vtysh/vtysh_cmd.c $(vtysh_cmd_split)
+ .l.c:
+ 	$(AM_V_LEX)$(am__skiplex) $(LEXCOMPILE) $<
+ .y.c:
+-	$(AM_V_YACC)$(am__skipyacc) $(YACCCOMPILE) $<
++	$(AM_V_YACC)$(am__skipyacc) $(YACCCOMPILE) -o $@ $<
+
+ #
+ # generated sources & extra foo
meta-networking/recipes-protocols/frr/frr_10.6.2.bb +1 -0
index b72af9f244..f4a98e2471 100644
@@ -12,6 +12,7 @@ LIC_FILES_CHKSUM = "file://doc/licenses/GPL-2.0;md5=b234ee4d69f5fce4486a80fdaf4a
 
 SRC_URI = "git://github.com/FRRouting/frr.git;protocol=https;branch=stable/${@oe.utils.trim_version('${PV}', 2)};tag=frr-${PV} \
            file://frr.pam \
+           file://0001-lib-Don-t-use-paths-in-bison-output-defines-directiv.patch \
            "
 SRCREV = "c8efb33af96f9f1220d0fdda6ac049f9126edd6f"