Skip to content

Commit db7a04a

Browse files
committed
improvements spurred by peer review for 20260204-linuxkm-fips-hash:
configure.ac: add --enable-kernel-verbose-debug and --enable-kernel-stack-debug; linuxkm/Makefile: * add QFLAG and VFLAG setup, and pass their values appropriately; * add missing `@set -e` and `-Wall -Wextra` to the linuxkm-fips-hash recipe; * use +$(MAKE), not @$(MAKE), for proper dry run recursion. linuxkm/README.md: update to reflect new goodies, and generally revise+extend remarks. linuxkm/linuxkm-fips-hash-wrapper.sh: add copyright header; pass through extra caller arguments to ./linuxkm-fips-hash. linuxkm/linuxkm-fips-hash.c: * add copyright header; * fix code around user_coreKey; * add explicit wolfCrypt_Cleanup() and cleanup of mod_fd and mod_map at end; * remove unused reloc_tab_len * fix a couple -Wsign-compares; * add missing fprintf arguments * properly set ret = -1 in a couple failure paths. linuxkm/linuxkm_wc_port.h: set WOLFSSL_LINUXKM_VERBOSE_DEBUG when WOLFSSL_KERNEL_VERBOSE_DEBUG, and recognize WOLFSSL_KERNEL_STACK_DEBUG as a synonym for WC_LINUXKM_STACK_DEBUG. linuxkm/linuxkm_memory.c and linuxkm/linuxkm_memory.h: add brief explanatory comments.
1 parent f376ae2 commit db7a04a

9 files changed

Lines changed: 224 additions & 47 deletions

configure.ac

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,34 @@ then
131131
fi
132132

133133
# Kernel module benchmark
134-
ENABLED_KERNEL_BENCHMARKS=""
135134
AC_ARG_ENABLE([kernel-benchmarks],
136135
[AS_HELP_STRING([--enable-kernel-benchmarks],[Enable crypto benchmarking autorun at module load time for kernel module (default: disabled)])],
137-
[ENABLED_KERNEL_BENCHMARKS=$enableval])
136+
[ENABLED_KERNEL_BENCHMARKS=$enableval],
137+
[ENABLED_KERNEL_BENCHMARKS="no"])
138138
if test "$ENABLED_KERNEL_BENCHMARKS" = "yes"
139139
then
140140
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_KERNEL_BENCHMARKS"
141141
fi
142142
AC_SUBST([ENABLED_KERNEL_BENCHMARKS])
143143

144+
AC_ARG_ENABLE([kernel-verbose-debug],
145+
[AS_HELP_STRING([--enable-kernel-verbose-debug],[Enable supplementary runtime debugging messages for kernel module (default: disabled)])],
146+
[ENABLED_KERNEL_VERBOSE_DEBUG=$enableval],
147+
[ENABLED_KERNEL_VERBOSE_DEBUG="no"])
148+
if test "$ENABLED_KERNEL_VERBOSE_DEBUG" = "yes"
149+
then
150+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_KERNEL_VERBOSE_DEBUG"
151+
fi
152+
153+
AC_ARG_ENABLE([kernel-stack-debug],
154+
[AS_HELP_STRING([--enable-kernel-stack-debug],[Enable runtime reporting of stack usage in kernel module (default: disabled)])],
155+
[ENABLED_KERNEL_STACK_DEBUG=$enableval],
156+
[ENABLED_KERNEL_STACK_DEBUG="no"])
157+
if test "$ENABLED_KERNEL_STACK_DEBUG" = "yes"
158+
then
159+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_KERNEL_STACK_DEBUG"
160+
fi
161+
144162
# Linux Kernel Module options (more options later)
145163
AC_ARG_ENABLE([linuxkm],
146164
[AS_HELP_STRING([--enable-linuxkm],[Enable Linux Kernel Module (default: disabled)])],

linuxkm/Makefile

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
.ONESHELL:
2222
SHELL=bash
2323

24+
ifeq "$(quiet)" "silent_"
25+
QFLAG := --quiet
26+
else ifeq "$(V)" "1"
27+
VFLAG := --verbose
28+
else
29+
QFLAG := --quiet
30+
endif
31+
2432
ifndef LIBWOLFSSL_NAME
2533
LIBWOLFSSL_NAME := libwolfssl
2634
endif
@@ -355,34 +363,35 @@ libwolfssl-user-build/src/.libs/libwolfssl.so:
355363
@echo > user_settings_asm.h
356364
@echo -n 'Configuring libwolfssl.so...'
357365
@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
358-
@./configure --quiet --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'
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'
359367
@echo ' done.'
360368
@echo -n 'Compiling and linking libwolfssl.so...'
361-
@$(MAKE) >/dev/null
369+
+$(MAKE) $(QFLAG) >/dev/null
362370
@echo ' done.'
363371
@echo -n 'Fixing FIPS hash...'
364372
@userhash=$$(wolfcrypt/test/testwolfcrypt 2>&1 | sed -n -E 's/^hash = (.+)$$/\1/p')
365373
@if [[ -z "$$userhash" ]]; then echo ' FIPS hash not found!' >&2; exit 1; fi
366374
@find wolfcrypt/src -name '*fips_test*o' -delete
367-
@$(MAKE) EXTRA_CFLAGS=-DWOLFCRYPT_FIPS_CORE_HASH_VALUE="$$userhash"
375+
+$(MAKE) $(QFLAG) EXTRA_CFLAGS=-DWOLFCRYPT_FIPS_CORE_HASH_VALUE="$$userhash"
368376
@echo ' done.'
369377

370378
linuxkm-fips-hash: libwolfssl-user-build/src/.libs/libwolfssl.so linuxkm-fips-hash.c
379+
@set -e
371380
@echo -n 'Compiling linuxkm-fips-hash...'
372381
# note direct invocation of cc -- we are compiling for the build host, not the target host.
373-
@cc -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
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
374383
@echo ' done.'
375384

376385
.PHONY: module-with-matching-fips-hash
377386
module-with-matching-fips-hash: $(LIBWOLFSSL_NAME).ko linuxkm-fips-hash
378387
@set -e
379-
./linuxkm-fips-hash-wrapper.sh "$<"
380-
$(MAKE) -C . '$(LIBWOLFSSL_NAME).ko.signed'
388+
@./linuxkm-fips-hash-wrapper.sh "$<" $(QFLAG) $(VFLAG)
389+
+$(MAKE) $(QFLAG) -C . '$(LIBWOLFSSL_NAME).ko.signed'
381390

382391
.PHONY: module-with-matching-fips-hash-no-sign
383392
module-with-matching-fips-hash-no-sign: $(LIBWOLFSSL_NAME).ko linuxkm-fips-hash
384393
@set -e
385-
./linuxkm-fips-hash-wrapper.sh "$<"
394+
@./linuxkm-fips-hash-wrapper.sh "$<"
386395

387396
$(LIBWOLFSSL_NAME).ko.signed: $(LIBWOLFSSL_NAME).ko
388397
ifdef FORCE_NO_MODULE_SIG

linuxkm/README.md

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ when wolfCrypt-FIPS is used, this provides a simple recipe for FIPS-compliant
1212
kernels.
1313

1414
Supported features:
15+
1516
- crypto acceleration: AES-NI, AVX, etc.
1617
- kernel crypto API registration (wolfCrypt algs appear as drivers in `/proc/crypto`.).
1718
- `CONFIG_CRYPTO_FIPS`, and crypto-manager self-tests.
@@ -22,71 +23,124 @@ Supported features:
2223

2324
## Building and Installing
2425

25-
Build linuxkm with:
26+
Build `libwolfssl.ko` with:
2627

2728
```sh
2829
$ ./configure --enable-linuxkm --with-linux-source=/usr/src/linux
2930
$ make -j module
3031
```
3132

32-
note: replace `/usr/src/linux` with a path to your fully configured and built
33+
Note: Replace `/usr/src/linux` with a path to your fully configured and built
3334
target kernel source tree.
3435

36+
If building from a FIPS kernel module bundle, build `libwolfssl.ko` with:
37+
```sh
38+
$ ./configure --enable-fips=fips_flavor --enable-linuxkm --with-linux-source=/usr/src/linux
39+
$ make -j module-with-matching-fips-hash
40+
```
41+
42+
Note: Replace `fips_flavor` with the correct value.
43+
3544
Assuming you are targeting your native system, install with:
3645

3746
```sh
3847
$ sudo make install
3948
$ sudo modprobe libwolfssl
4049
```
4150

42-
### options
51+
### Key additional Linux kernel module configuration options
52+
53+
| option | description |
54+
| :------------------------------- | :----------------------------------------- |
55+
| `--enable-linuxkm-lkcapi-register` | Register wolfcrypt algs with linux kernel crypto API. <br> Optional value is 'all', 'all-kconfig', 'none', or a comma separated list of algs. |
56+
| `--enable-all-crypto` | Enable extra crypto algorithms |
57+
| `--enable-intelasm` | x86/amd64 crypto acceleration |
58+
| `--enable-cryptonly` | Omit TLS/DTLS implementation (normally recommended) |
59+
60+
### Additional configuration options for verification, performance evaluation, and troubleshooting
61+
62+
| option | description |
63+
| :------------------------------- | :----------------------------------------- |
64+
| `--enable-crypttests` | Run `wolfcrypt_test()` at module load (not recommended for production) |
65+
| `--enable-kernel-benchmarks` | Run crypto benchmark at module load (_not appropriate for production_) |
66+
| `--enable-kernel-verbose-debug` | Extra runtime diagnostic and informational messages |
67+
| `--enable-kernel-stack-debug` | Report stack usage during module startup |
68+
| `--enable-debug-trace-errcodes` | Profuse debug logging (_not appropriate for production_) |
69+
| `--enable-debug-trace-errcodes=backtrace` | Even more profuse debug logging (_not appropriate for production_) |
4370

44-
| linuxkm option | description |
45-
| :------------------------------- | :--------------------------------------- |
46-
| --enable-linuxkm-lkcapi-register | Register wolfcrypt algs with linux kernel <br> crypto API. Options are 'all', 'none', or <br> comma separated list of algs. |
47-
| --enable-linuxkm-pie | Enable relocatable object build of module|
48-
| --enable-linuxkm-benchmarks | Run crypto benchmark at module load |
4971

5072
## Kernel Patches
5173

52-
The dir `linuxkm/patches` contains a patch to the linux kernel CRNG. The
74+
The `linuxkm/patches` directory in the source distribution contains a patch to the linux kernel CRNG. The
5375
CRNG provides the implementation for `/dev/random`, `/dev/urandom`, and
54-
`getrandom()`.
76+
`getrandom()`, and for internal RNG APIs such as `get_random_bytes()`,
77+
`get_random_u32()`, etc.
78+
79+
The patch applies to these two sources:
5580

56-
The patch updates these two sources
5781
- `drivers/char/random.c`
5882
- `include/linux/random.h`
5983

84+
It adds a callback facility to the core kernel code that allows `libwolfssl.ko`
85+
to register FIPS-compliant algorithms in place of the native implementation
86+
(which is based on non-FIPS ChaCha20 and blake2s algorithms). When `libwolfssl.ko` is configured with
87+
`--enable-linuxkm-lkcapi-register` and loaded into a patched kernel, it
88+
automatically registers the FIPS callbacks. At startup, the module will report
6089

61-
to use FIPS-compliant algorithms, instead of chacha and blake2s.
90+
```
91+
libwolfssl: kernel global random_bytes handlers installed.
92+
```
6293

63-
Patches are provided for several kernel versions, ranging from `5.10.x` to
64-
`6.15`.
94+
Additionally, `/proc/crypto` will advertise that the FIPS DRBG is installed at
95+
highest priority "-with-global-replace":
96+
```ini
97+
name : stdrng
98+
driver : sha2-256-drbg-nopr-wolfcrypt-fips-140-3-with-global-replace
99+
module : libwolfssl
100+
priority : 100000
101+
refcnt : 2
102+
selftest : passed
103+
internal : no
104+
fips : yes
105+
type : rng
106+
seedsize : 0
107+
```
65108

66-
### patch procedure
67109

68-
1. Ensure kernel src tree is clean before patching:
110+
Patches are provided for several kernel versions, ranging from `5.10.x` to
111+
`6.15`, with the most recent patchset tested nightly with the latest Linux
112+
release and RC kernels, and with the latest linux-next snapshot. Use the
113+
patchset with the most recent target kernel version not greater than that of the
114+
kernel you're targeting.
69115

70-
```sh
71-
cd ~/kernelsrc/
72-
make mrproper
73-
```
116+
### Patch procedure
74117

75-
2. Verify patches will apply clean with a dry run check:
118+
1. Verify that the patcheset applies cleanly, using a dry run:
76119

77-
```sh
78-
patch -p1 --dry-run <~/wolfssl-5.8.2/linuxkm/patches/6.12/WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v12.patch
120+
```console
121+
$ cd ~/kernelsrc/
122+
$ patch -p1 --dry-run < ~/wolfssl-5.8.2/linuxkm/patches/6.12/WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v12.patch
79123
checking file drivers/char/random.c
80124
checking file include/linux/random.h
81125
```
82126

83-
3. Finally patch the kernel:
127+
2. Optionally, clean the kernel src tree before patching:
84128

85-
```sh
86-
patch -p1 <~/wolfssl-5.8.2/linuxkm/patches/6.12/WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v12.patch
129+
```console
130+
$ make mrproper
131+
```
132+
133+
3. Patch the kernel:
134+
135+
```console
136+
$ patch -p1 < ~/wolfssl-5.8.2/linuxkm/patches/6.12/WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v12.patch
87137
patching file drivers/char/random.c
88138
patching file include/linux/random.h
89139
```
90140

91-
4. Build kernel.
92-
141+
4. Build and optionally install the patched kernel:
142+
```console
143+
$ make -j
144+
# make modules_install
145+
# make install
146+
```

linuxkm/linuxkm-fips-hash-wrapper.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
#!/bin/bash
22

3+
# linuxkm-fips-hash-wrapper.sh -- Wrapper for linuxkm-fips-hash -- looks up the
4+
# fencepost values using readelf, and assembles the argument list from them.
5+
#
6+
# Copyright (C) 2006-2026 wolfSSL Inc.
7+
#
8+
# This file is part of wolfSSL.
9+
#
10+
# wolfSSL is free software; you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation; either version 3 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# wolfSSL is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with this program; if not, write to the Free Software
22+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
23+
324
set -o noclobber -o nounset -o pipefail -o errexit
425

526
mod_path=$1
27+
shift
628

729
readarray -t fenceposts < <(readelf --wide --sections --symbols "$mod_path" | awk '
830
BEGIN {
@@ -64,4 +86,4 @@ BEGIN {
6486
}
6587
}')
6688

67-
./linuxkm-fips-hash "${fenceposts[@]}" --mod-path "$mod_path" --in-place --quiet
89+
./linuxkm-fips-hash "${fenceposts[@]}" --mod-path "$mod_path" --in-place "$@"

0 commit comments

Comments
 (0)