Skip to content

Commit d22175a

Browse files
committed
Makefile.am: for linuxkm module target, pass through "module" target as such, for compatibility with alt LIBWOLFSSL_NAME.
linuxkm/Makefile: * don't use `readarray -d` -- it's a recent bashism; * rework libwolfssl-user-build/src/.libs/libwolfssl.so recipe to better isolate sub-build settings. * add support for HOSTCC and HOSTCFLAGS in libwolfssl.so build. * deploy $(QFLAG) --no-print-directory --no-silent in several submakes for neatness and resilience. * tweak $(LIBWOLFSSL_NAME).ko.signed recipe to add a "skipping" message and some consistency checking. linuxkm/README.md: update FIPS DRBG /proc/crypto content to show seed source. linuxkm/linuxkm_memory.c: fixes for format character portability in a RELOC_DEBUG_PRINTF() in wc_reloc_normalize_text). linuxkm/linuxkm_wc_port.h: pull in linux/moduleparam.h, and if WC_LINUXKM_SUPPORT_DUMP_TO_FILE, pull in linux/fs.h and linux/uaccess.h. linuxkm/module_hooks.c: implement WC_LINUXKM_SUPPORT_DUMP_TO_FILE: dump_to_file() and module args text_dump_path=... and rodata_dump_path=... linuxkm/patches/7.0/WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-7v0.patch: add to accommodate patch-breaking change in Linux 7dff99b354.
1 parent 76bc6e3 commit d22175a

8 files changed

Lines changed: 660 additions & 52 deletions

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ WC_DILITHIUM_FIXED_ARRAY
637637
WC_DISABLE_RADIX_ZERO_PAD
638638
WC_FLAG_DONT_USE_AESNI
639639
WC_FORCE_LINUXKM_FORTIFY_SOURCE
640+
WC_LINUXKM_SUPPORT_DUMP_TO_FILE
640641
WC_LMS_FULL_HASH
641642
WC_NO_ASYNC_SLEEP
642643
WC_NO_RNG_SIMPLE

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ if BUILD_LINUXKM
229229
FIPS_FLAVOR
230230

231231
module:
232-
+$(MAKE) -C linuxkm libwolfssl.ko
232+
+$(MAKE) -C linuxkm module
233233

234234
module-update-fips-hash:
235235
+$(MAKE) -C linuxkm module-update-fips-hash

linuxkm/Makefile

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ ifndef LIBWOLFSSL_NAME
3333
LIBWOLFSSL_NAME := libwolfssl
3434
endif
3535

36+
module: $(LIBWOLFSSL_NAME).ko
37+
3638
all: $(LIBWOLFSSL_NAME).ko $(LIBWOLFSSL_NAME).ko.signed
3739

3840
ifndef MODULE_TOP
@@ -249,6 +251,7 @@ $(LIBWOLFSSL_NAME).ko:
249251
echo 'CPPFLAGS = "$(CPPFLAGS)"';
250252
echo 'AM_CFLAGS = "$(AM_CFLAGS)"';
251253
echo 'CFLAGS = "$(CFLAGS)"';
254+
echo 'HOSTCFLAGS = "$(HOSTCFLAGS)"';
252255
echo 'KERNEL_EXTRA_CFLAGS = "$(KERNEL_EXTRA_CFLAGS)"';
253256
echo 'FIPS_OPTEST = "$(FIPS_OPTEST)"';
254257
echo 'AM_CCASFLAGS = "$(AM_CCASFLAGS)"';
@@ -261,6 +264,7 @@ $(LIBWOLFSSL_NAME).ko:
261264
echo 'host_triplet = "$(host_triplet)"';
262265
echo 'build_triplet = "$(build_triplet)"';
263266
echo 'CC = "$(CC)"';
267+
echo 'HOSTCC = "$(HOSTCC)"';
264268
echo 'AS = "$(AS)"';
265269
echo 'LD = "$(LD)"';
266270
echo 'READELF = "$(READELF)"';
@@ -302,21 +306,25 @@ ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
302306
@RELOC_TMP=$$(mktemp "$(MAKE_TMPDIR)/wc_linuxkm_pie_reloc_tab.c.XXXXXX")
303307
@trap 'rm "$$RELOC_TMP"' EXIT
304308
@if [[ -f "$@" ]]; then touch -r "$@" "$$RELOC_TMP"; fi
305-
+$(MAKE) ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS) CC_FLAGS_FTRACE=
309+
# --no-silent works around make bug that otherwise leads to "No rule to make target 's'. Stop." (due to a bug around $(MAKEFLAGS)) in --quiet builds.
310+
+$(MAKE) $(QFLAG) --no-print-directory --no-silent ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS) CC_FLAGS_FTRACE=
306311
# if the above make didn't build a fresh libwolfssl.ko, then the module is already up to date and we leave it untouched, assuring stability for purposes of module-update-fips-hash.
307312
@if [[ ! "$@" -nt "$$RELOC_TMP" ]]; then echo ' Module already up-to-date.'; exit 0; fi
308313
@SECTION_MAP=$$(mktemp)
309314
@trap 'rm "$$RELOC_TMP" "$$SECTION_MAP"' EXIT
310315
@export SECTION_MAP
311316
@$(READELF) --wide --sections --symbols "$@" | $(GENERATE_SECTION_MAP)
312317
@$(READELF) --wide --relocs "$@" | $(GENERATE_RELOC_TAB) >| '$(MODULE_TOP)/linuxkm/wc_linuxkm_pie_reloc_tab.c'
313-
+$(MAKE) ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS) CC_FLAGS_FTRACE=
318+
+$(MAKE) $(QFLAG) --no-print-directory --no-silent ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS) CC_FLAGS_FTRACE=
314319
@$(READELF) --wide --relocs "$@" | $(GENERATE_RELOC_TAB) >| "$$RELOC_TMP"
315320
@if diff '$(MODULE_TOP)/linuxkm/wc_linuxkm_pie_reloc_tab.c' "$$RELOC_TMP"; then echo " Relocation table is stable."; else echo "PIE failed: relocation table is unstable." 1>&2; exit 1; fi
316321
else
317-
+$(MAKE) ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS)
322+
# --no-silent works around make bug that otherwise leads to "No rule to make target 's'. Stop." (due to a bug around $(MAKEFLAGS)) in --quiet builds.
323+
+$(MAKE) $(QFLAG) --no-print-directory --no-silent ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS)
318324
endif
319325

326+
$(MODULE_TOP)/$(LIBWOLFSSL_NAME).ko: $(LIBWOLFSSL_NAME).ko
327+
320328
.PHONY: module-update-fips-hash
321329
module-update-fips-hash: $(LIBWOLFSSL_NAME).ko
322330
@set -e
@@ -335,7 +343,7 @@ module-update-fips-hash: $(LIBWOLFSSL_NAME).ko
335343
if [[ '$(FIPS_HASH)' == "$$current_verifyCore" ]]; then echo ' Supplied FIPS_HASH matches existing verifyCore -- no update needed.'; exit 0; fi; \
336344
echo -n '$(FIPS_HASH)' | dd bs=1 conv=notrunc of="$<" seek=$$verifyCore_offset count=64 status=none && \
337345
echo " FIPS verifyCore updated successfully." && \
338-
if [[ -f '$(LIBWOLFSSL_NAME).ko.signed' ]]; then $(MAKE) -C . '$(LIBWOLFSSL_NAME).ko.signed'; fi
346+
if [[ -f '$(LIBWOLFSSL_NAME).ko.signed' ]]; then $(MAKE) $(QFLAG) --no-print-directory --no-silent -C . '$(LIBWOLFSSL_NAME).ko.signed'; fi
339347

340348

341349
# linuxkm-fips-hash implements offline (no-load) FIPS hash calculation and graft-in.
@@ -348,45 +356,61 @@ module-update-fips-hash: $(LIBWOLFSSL_NAME).ko
348356
# depending on changes/config in the source directory. Also, aside from
349357
# FIPS_FLAVOR, inherited configuration settings in the environment are cleansed.
350358

351-
libwolfssl-user-build/src/.libs/libwolfssl.so:
352-
@set -e
353-
@$(RM) -rf '$(MODULE_TOP)/libwolfssl-user-build'
354-
@mkdir '$(MODULE_TOP)/libwolfssl-user-build'
359+
FRESH_MAKEFLAGS := $(patsubst -j%,-j %,$(filter -%,$(filter-out -- --jobserver-auth=%,$(MAKEFLAGS))))
360+
FRESH_ENV := env -i HOME="$$HOME" PATH="/usr/local/bin:/usr/bin:/bin:$$PATH" LANG="$${LANG-C.UTF-8}" LC_ALL="$${LC_ALL-C.UTF-8}" TERM="$${TERM-dumb}"
361+
362+
.PHONY: $(MODULE_TOP)/libwolfssl-user-build/src/.libs/libwolfssl.so
363+
$(MODULE_TOP)/libwolfssl-user-build/src/.libs/libwolfssl.so: $(LIBWOLFSSL_NAME).ko
364+
@set -o errexit -o pipefail
365+
@if [[ '$(SRC_TOP)/configure' -nt '$@' ]]; then
366+
@ echo 'Purging stale libwolfssl-user-build tree.'
367+
@ $(RM) -rf '$(MODULE_TOP)/libwolfssl-user-build'
368+
@fi
369+
@mkdir -p '$(MODULE_TOP)/libwolfssl-user-build'
355370
@cd '$(MODULE_TOP)/libwolfssl-user-build'
356-
@pushd '$(SRC_TOP)' >/dev/null
357-
@echo -n 'Populating tree of symlinks...'
358-
@readarray -d '' -t srcfiles < <(find examples src support tests testsuite wolfcrypt wolfssl configure *.in build-aux debian rpm scripts certs doc mcapi cmake linuxkm/*.[ch] \( -name options.h -o -name user_settings\* \) -prune -o \( ! -type d \) \( -name '*.[chsSi]' -o -name configure -o -name '*.in' -o -name \*.sh -o -path support/\* -o -path build-aux/\* -o -path debian/\* -o -path rpm/\* -o -path scripts/\* -o -path certs/\* -o -path doc/\* -o -path mcapi/\* -o -path cmake/\* \) -print0)
359-
@popd >/dev/null
360-
@for file in "$${srcfiles[@]}"; do if [[ ! -e "$$file" ]]; then mkdir -p "$$(dirname "$$file")" && cp --no-dereference --symbolic-link --no-clobber '$(SRC_TOP)'/"$$file" "$$file"; fi; done
361-
@echo ' done.'
362-
@echo '__attribute__ ((visibility("default"))) extern const char coreKey[];' > user_settings.h
363-
@echo > user_settings_asm.h
364-
@echo -n 'Configuring libwolfssl.so...'
365-
@unset WOLFSSL_CFLAGS WOLFCRYPT_PIE_FILES ASFLAGS_FPUSIMD_ENABLE ASFLAGS_FPU_DISABLE_SIMD_ENABLE src_libwolfssl_la_OBJECTS WOLFSSL_ASFLAGS AM_CFLAGS WOLFSSL_OBJ_FILES ENABLED_LINUXKM_LKCAPI_REGISTER EXTRA_LDFLAGS CC LD
366-
@./configure $(QFLAG) $(VFLAG) --disable-jobserver --enable-cryptonly --enable-fips="$$FIPS_FLAVOR" CFLAGS='-DWC_SYM_RELOC_TABLES_SUPPORT -DWOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE -DWOLFSSL_USER_SETTINGS -DWOLFSSL_USER_SETTINGS_ASM'
371+
@if [[ ! -e '$(MODULE_TOP)/libwolfssl-user-build/configure' ]]; then
372+
@ pushd '$(SRC_TOP)' >/dev/null
373+
@ echo -n 'Populating tree of symlinks for user libwolfssl.so build...'
374+
@ readarray -t srcfiles < <(find examples src support tests testsuite wolfcrypt wolfssl configure *.in build-aux debian rpm scripts certs doc mcapi cmake linuxkm/*.[ch] \( -name options.h -o -name user_settings\* \) -prune -o \( ! -type d \) \( -name '*.[chsSi]' -o -name configure -o -name '*.in' -o -name \*.sh -o -path support/\* -o -path build-aux/\* -o -path debian/\* -o -path rpm/\* -o -path scripts/\* -o -path certs/\* -o -path doc/\* -o -path mcapi/\* -o -path cmake/\* \) -print)
375+
@ popd >/dev/null
376+
@ for file in "$${srcfiles[@]}"; do if [[ ! -e "$$file" ]]; then mkdir -p "$$(dirname "$$file")" && cp --no-dereference --symbolic-link --no-clobber '$(SRC_TOP)'/"$$file" "$$file"; fi; done
377+
@ echo ' done.'
378+
@fi
379+
@if [[ ! -f user_settings.h ]]; then
380+
@ echo '__attribute__ ((visibility("default"))) extern const char coreKey[];' > user_settings.h
381+
@ echo > user_settings_asm.h
382+
@fi
383+
@if [[ -f Makefile ]]; then
384+
@ echo 'Using existing Makefile for libwolfssl.so.'
385+
@else
386+
@ echo -n 'Configuring user libwolfssl.so...'
387+
@ $(FRESH_ENV) ./configure $(QFLAG) $(VFLAG) --disable-jobserver --enable-cryptonly --enable-fips="$$FIPS_FLAVOR" CFLAGS='-DWC_SYM_RELOC_TABLES_SUPPORT -DWOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE -DWOLFSSL_USER_SETTINGS -DWOLFSSL_USER_SETTINGS_ASM' '$(if $(HOSTCC),CC=$(HOSTCC))'
388+
@ echo ' done.'
389+
@fi
390+
@echo -n 'Building user libwolfssl.so...'
391+
@$(FRESH_ENV) $(MAKE) $(QFLAG) $(FRESH_MAKEFLAGS) >/dev/null
367392
@echo ' done.'
368-
@echo -n 'Compiling and linking libwolfssl.so...'
369-
+$(MAKE) $(QFLAG) >/dev/null
370-
@echo ' done.'
371-
@echo -n 'Fixing FIPS hash...'
372-
@userhash=$$(wolfcrypt/test/testwolfcrypt 2>&1 | sed -n -E 's/^hash = (.+)$$/\1/p')
373-
@if [[ -z "$$userhash" ]]; then echo ' FIPS hash not found!' >&2; exit 1; fi
374-
@find wolfcrypt/src -name '*fips_test*o' -delete
375-
+$(MAKE) $(QFLAG) EXTRA_CFLAGS=-DWOLFCRYPT_FIPS_CORE_HASH_VALUE="$$userhash"
376-
@echo ' done.'
377-
378-
linuxkm-fips-hash: libwolfssl-user-build/src/.libs/libwolfssl.so linuxkm-fips-hash.c
393+
@echo -n 'Fixing FIPS hash in user libwolfssl.so...'
394+
@if ! userhash=$$(wolfcrypt/test/testwolfcrypt 2>&1 | sed -n -E 's/^hash = (.+)$$/\1/p'); then
395+
@ if [[ -z "$$userhash" ]]; then echo ' FIPS hash not found!' >&2; exit 1; fi
396+
@ find wolfcrypt/src -name '*fips_test*o' -delete
397+
@ $(FRESH_ENV) $(MAKE) $(QFLAG) $(FRESH_MAKEFLAGS) EXTRA_CFLAGS=-DWOLFCRYPT_FIPS_CORE_HASH_VALUE="$$userhash" >/dev/null
398+
@ echo ' done.'
399+
@else
400+
@ @echo ' already matches (no update needed).'
401+
@fi
402+
403+
linuxkm-fips-hash: $(MODULE_TOP)/libwolfssl-user-build/src/.libs/libwolfssl.so linuxkm-fips-hash.c
379404
@set -e
380405
@echo -n 'Compiling linuxkm-fips-hash...'
381-
# note direct invocation of cc -- we are compiling for the build host, not the target host.
382-
@cc -Wall -Wextra -O2 -I'$(MODULE_TOP)/libwolfssl-user-build' -o linuxkm-fips-hash linuxkm/linuxkm-fips-hash.c -L '$(MODULE_TOP)/libwolfssl-user-build/src/.libs' -Wl,-rpath-link='$(MODULE_TOP)/libwolfssl-user-build/src/.libs' -Wl,-rpath='$(MODULE_TOP)/libwolfssl-user-build/src/.libs' -lwolfssl
406+
@$(or $(HOSTCC),cc) $(or $(HOSTCFLAGS),-Wall -Wextra -O2) -I'$(MODULE_TOP)/libwolfssl-user-build' -o linuxkm-fips-hash linuxkm/linuxkm-fips-hash.c -L '$(MODULE_TOP)/libwolfssl-user-build/src/.libs' -Wl,-rpath-link='$(MODULE_TOP)/libwolfssl-user-build/src/.libs' -Wl,-rpath='$(MODULE_TOP)/libwolfssl-user-build/src/.libs' -lwolfssl
383407
@echo ' done.'
384408

385409
.PHONY: module-with-matching-fips-hash
386410
module-with-matching-fips-hash: $(LIBWOLFSSL_NAME).ko linuxkm-fips-hash
387411
@set -e
388412
@./linuxkm-fips-hash-wrapper.sh "$<" $(QFLAG) $(VFLAG)
389-
+$(MAKE) $(QFLAG) -C . '$(LIBWOLFSSL_NAME).ko.signed'
413+
+$(MAKE) $(QFLAG) --no-print-directory --no-silent -C . '$(LIBWOLFSSL_NAME).ko.signed'
390414

391415
.PHONY: module-with-matching-fips-hash-no-sign
392416
module-with-matching-fips-hash-no-sign: $(LIBWOLFSSL_NAME).ko linuxkm-fips-hash
@@ -406,7 +430,9 @@ else
406430
;;
407431
esac
408432
done < .config
409-
if [[ "$${CONFIG_MODULE_SIG}" = "y" && -n "$${CONFIG_MODULE_SIG_KEY}" && \
433+
if [[ "$${CONFIG_MODULE_SIG}" != "y" ]]; then
434+
echo ' [skipping $@ -- CONFIG_MODULE_SIG is unset in target kernel]'
435+
elif [[ -n "$${CONFIG_MODULE_SIG_KEY}" && \
410436
-n "$${CONFIG_MODULE_SIG_HASH}" && ( ! -f '$(MODULE_TOP)/$@' || \
411437
'$(MODULE_TOP)/$<' -nt '$(MODULE_TOP)/$@' ) ]]; then
412438
CONFIG_MODULE_SIG_KEY="$${CONFIG_MODULE_SIG_KEY#\"}"
@@ -426,19 +452,22 @@ else
426452
if [[ "$(quiet)" != "silent_" ]]; then
427453
echo " Module $@ signed by $${CONFIG_MODULE_SIG_KEY}."
428454
fi
455+
elif [[ ! -f '$(MODULE_TOP)/$@' || '$(MODULE_TOP)/$<' -nt '$(MODULE_TOP)/$@' ]]; then
456+
echo 'Unable to generate $@ from $<: CONFIG_MODULE_SIG_KEY and/or CONFIG_MODULE_SIG_HASH is missing.' >&2
457+
exit 1
429458
fi
430459
endif
431460

432461

433462
.PHONY: install modules_install
434463
install modules_install:
435-
+$(MAKE) -C $(KERNEL_ROOT) M=$(MODULE_TOP) src=$(SRC_TOP) INSTALL_MOD_DIR=wolfssl modules_install
464+
+$(MAKE) $(QFLAG) --no-silent -C $(KERNEL_ROOT) M=$(MODULE_TOP) src=$(SRC_TOP) INSTALL_MOD_DIR=wolfssl modules_install
436465

437466
.PHONY: clean
438467
# note, must supply $(MODULE_TOP) as the src value for clean so that Kbuild is included, else
439468
# the top Makefile (which is not for the kernel build) would be included here.
440469
clean:
441-
+$(MAKE) -C $(KERNEL_ROOT) M=$(MODULE_TOP) src=$(MODULE_TOP) clean
470+
+$(MAKE) $(QFLAG) --no-silent -C $(KERNEL_ROOT) M=$(MODULE_TOP) src=$(MODULE_TOP) clean
442471
$(RM) -rf '$(MODULE_TOP)/linuxkm'
443472
$(RM) -rf '$(MODULE_TOP)/wolfcrypt'
444473
$(RM) -rf '$(MODULE_TOP)/src'

linuxkm/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ libwolfssl: kernel global random_bytes handlers installed.
9292
```
9393

9494
Additionally, `/proc/crypto` will advertise that the FIPS DRBG is installed at
95-
highest priority "-with-global-replace":
95+
highest priority, with "-wolfentropy" and/or "-rdseed", and "-with-global-replace":
9696
```ini
9797
name : stdrng
98-
driver : sha2-256-drbg-nopr-wolfcrypt-fips-140-3-with-global-replace
98+
driver : sha2-256-drbg-nopr-wolfentropy-wolfcrypt-fips-140-3-with-global-replace
9999
module : libwolfssl
100100
priority : 100000
101101
refcnt : 2

linuxkm/linuxkm_memory.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -354,23 +354,23 @@ ssize_t wc_reloc_normalize_text(
354354
#ifdef DEBUG_LINUXKM_PIE_SUPPORT
355355
if (reloc_buf >= seg_end - seg_beg) {
356356
++n_oob_r;
357-
RELOC_DEBUG_PRINTF("WARNING: normalized value is out of bounds (%s0x%lx) at index %ld, text offset 0x%x, reloc type %s, "
358-
"dest seg .%s_wolfcrypt, offset from text to dest segment %s0x%lx, raw dest addr %s0x%lx, "
359-
"seg span 0x%lx - 0x%lx, seg size 0x%lx, text base 0x%lx\n",
360-
(sword64)reloc_buf < 0 ? "-" : "",
361-
(sword64)reloc_buf < 0 ? -reloc_buf : reloc_buf,
362-
i,
357+
RELOC_DEBUG_PRINTF("WARNING: normalized value is out of bounds (%s0x%llx) at index %lld, text offset 0x%x, reloc type %s, "
358+
"dest seg .%s_wolfcrypt, offset from text to dest segment %s0x%llx, raw dest addr %s0x%llx, "
359+
"seg span 0x%llx - 0x%llx, seg size 0x%llx, text base 0x%llx\n",
360+
(long long)reloc_buf < 0 ? "-" : "",
361+
(long long)reloc_buf < 0 ? -(long long)reloc_buf : (long long)reloc_buf,
362+
(long long)i,
363363
next_reloc->offset,
364364
layout->name,
365365
seg_name,
366366
seg_beg < seg_map->text_start ? "-" : "+",
367-
seg_beg < seg_map->text_start ? (word64)seg_map->text_start - seg_beg : seg_beg - (word64)seg_map->text_start,
368-
(layout->is_signed && ((sword64)raw_dest_addr < 0)) ? "-" : "",
369-
(layout->is_signed && ((sword64)raw_dest_addr < 0)) ? (word64)-(sword64)raw_dest_addr : raw_dest_addr,
370-
(word64)seg_beg,
371-
(word64)seg_end,
372-
(word64)(seg_end - seg_beg),
373-
(word64)seg_map->text_start);
367+
seg_beg < seg_map->text_start ? (unsigned long long)seg_map->text_start - seg_beg : seg_beg - (unsigned long long)seg_map->text_start,
368+
(layout->is_signed && ((long long)raw_dest_addr < 0)) ? "-" : "",
369+
(layout->is_signed && ((long long)raw_dest_addr < 0)) ? (unsigned long long)-(long long)raw_dest_addr : raw_dest_addr,
370+
(unsigned long long)seg_beg,
371+
(unsigned long long)seg_end,
372+
(unsigned long long)(seg_end - seg_beg),
373+
(unsigned long long)seg_map->text_start);
374374
}
375375
#endif
376376
}

linuxkm/linuxkm_wc_port.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@
459459
#ifndef WC_CONTAINERIZE_THIS
460460
#include <linux/init.h>
461461
#include <linux/module.h>
462+
#include <linux/moduleparam.h>
462463
#include <linux/delay.h>
463464
#endif
464465

@@ -508,6 +509,10 @@
508509
#endif /* !WOLFCRYPT_ONLY */
509510
#endif /* !WC_CONTAINERIZE_THIS */
510511

512+
#ifdef WC_LINUXKM_SUPPORT_DUMP_TO_FILE
513+
#include <linux/fs.h>
514+
#include <linux/uaccess.h>
515+
#endif
511516
#include <linux/slab.h>
512517
#include <linux/sched.h>
513518
#if __has_include(<linux/sched/task_stack.h>)

linuxkm/module_hooks.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,74 @@ WC_MAYBE_UNUSED static int linuxkm_lkcapi_sysfs_deinstall_node(struct kobj_attri
239239
return 0;
240240
}
241241

242+
#ifdef WC_LINUXKM_SUPPORT_DUMP_TO_FILE
243+
static ssize_t dump_to_file(const char *path, const u8 *buf, size_t buf_len)
244+
{
245+
loff_t pos = 0;
246+
struct file *fp;
247+
ssize_t ret;
248+
char tmp;
249+
250+
if (buf_len == 0) {
251+
pr_info("libwolfssl: dump_to_file() called with buf_len == 0. Not dumping.\n");
252+
return 0;
253+
}
254+
255+
WC_SANITIZE_DISABLE();
256+
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,8,0)
257+
ret = probe_kernel_read(&tmp, buf, 1);
258+
if (ret == 0)
259+
ret = probe_kernel_read(&tmp, buf + buf_len - 1, 1);
260+
#else
261+
ret = copy_from_kernel_nofault(&tmp, buf, 1);
262+
if (ret == 0)
263+
ret = copy_from_kernel_nofault(&tmp, buf + buf_len - 1, 1);
264+
#endif
265+
WC_SANITIZE_ENABLE();
266+
if (ret != 0) {
267+
pr_err("libwolfssl: cannot safely read from buffer %px: %d\n", buf, (int)ret);
268+
return ret;
269+
}
270+
271+
fp = filp_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
272+
if (IS_ERR(fp)) {
273+
pr_err("libwolfssl: cannot open %s: %ld\n", path, PTR_ERR(fp));
274+
return PTR_ERR(fp);
275+
}
276+
277+
WC_SANITIZE_DISABLE();
278+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
279+
/* kernel_write() exported by 7bb307e894d51 */
280+
ret = kernel_write(fp, buf, buf_len, &pos);
281+
#else
282+
ret = vfs_write(fp, buf, buf_len, &pos);
283+
#endif
284+
WC_SANITIZE_ENABLE();
285+
286+
filp_close(fp, NULL);
287+
288+
if (ret < 0)
289+
pr_err("libwolfssl: write to %s failed: %zd\n", path, ret);
290+
else if ((size_t)ret != buf_len)
291+
pr_warn("libwolfssl: short write to %s: %zd of %zu bytes\n", path, ret, buf_len);
292+
293+
return ret;
294+
}
295+
296+
static char *text_dump_path;
297+
static char *rodata_dump_path;
298+
299+
/* indent these so they don't look like flush-left function calls. */
300+
module_param(text_dump_path, charp, 0444);
301+
module_param(rodata_dump_path, charp, 0444);
302+
303+
MODULE_PARM_DESC(text_dump_path,
304+
"Path to dump live .wolfcrypt_text section to (e.g. /tmp/wc_text.bin)");
305+
MODULE_PARM_DESC(rodata_dump_path,
306+
"Path to dump live .wolfcrypt_rodata section to (e.g. /tmp/wc_rodata.bin)");
307+
308+
#endif /* WC_LINUXKM_SUPPORT_DUMP_TO_FILE */
309+
242310
#ifdef HAVE_FIPS
243311
static ssize_t FIPS_rerun_self_test_handler(struct kobject *kobj, struct kobj_attribute *attr,
244312
const char *buf, size_t count);
@@ -503,6 +571,27 @@ static int wolfssl_init(void)
503571
return ret;
504572
#endif
505573

574+
#ifdef WC_LINUXKM_SUPPORT_DUMP_TO_FILE
575+
576+
#ifdef WC_SYM_RELOC_TABLES
577+
if (text_dump_path) {
578+
if (dump_to_file(text_dump_path, (u8 *)__wc_text_start, (size_t)((uintptr_t)__wc_text_end - (uintptr_t)__wc_text_start)) == 0)
579+
pr_info("libwolfssl: dumped .wolfcrypt_text (%zu bytes) to %s.\n", (size_t)((uintptr_t)__wc_text_end - (uintptr_t)__wc_text_start), text_dump_path);
580+
}
581+
if (rodata_dump_path) {
582+
if (dump_to_file(rodata_dump_path, (u8 *)__wc_rodata_start, (size_t)(__wc_rodata_end - __wc_rodata_start)) == 0)
583+
pr_info("libwolfssl: dumped .wolfcrypt_rodata (%zu bytes) to %s.\n", (size_t)((uintptr_t)__wc_rodata_end - (uintptr_t)__wc_rodata_start), text_dump_path);
584+
}
585+
#else
586+
if ((text_dump_path != NULL) ||
587+
(rodata_dump_path != NULL))
588+
{
589+
pr_info("libwolfssl: ignoring module dump path argument(s) -- module lacks WC_SYM_RELOC_TABLES.\n");
590+
}
591+
#endif
592+
593+
#endif /* WC_LINUXKM_SUPPORT_DUMP_TO_FILE */
594+
506595
#ifdef WC_LINUXKM_TEST_INET_PTON
507596
{
508597
const char *src;

0 commit comments

Comments
 (0)