Skip to content

Commit f49e583

Browse files
committed
linuxkm/Kbuild: skip "section(s) missed by containerization" test unless KERNEL_ARCH_X86;
linuxkm/linuxkm_wc_port.h: fixes for legacy kernels, particularly: when building TLS stack (!WOLFCRYPT_ONLY), use the best heap with a functioning realloc(), else use kvmalloc() and friends if available, even if kvrealloc() is unavailable. also, provide for XMALLOC_USER and XMALLOC_OVERRIDE; linuxkm/lkcapi_glue.c: recognize the new CONFIG_CRYPTO_SELFTESTS_FULL alongside the old CONFIG_CRYPTO_MANAGER_EXTRA_TESTS; linuxkm/linuxkm_memory.c: restore my__show_free_areas() in case it's still needed.
1 parent 01e8815 commit f49e583

File tree

5 files changed

+128
-64
lines changed

5 files changed

+128
-64
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ CONFIG_CRYPTO_GCM
6565
CONFIG_CRYPTO_HMAC
6666
CONFIG_CRYPTO_MANAGER
6767
CONFIG_CRYPTO_RSA
68+
CONFIG_CRYPTO_SELFTESTS_FULL
6869
CONFIG_CRYPTO_SHA1
6970
CONFIG_CRYPTO_SHA256
7071
CONFIG_CRYPTO_SHA3
@@ -774,7 +775,6 @@ WOLFSSL_NO_KCAPI_SHA224
774775
WOLFSSL_NO_OCSP_DATE_CHECK
775776
WOLFSSL_NO_OCSP_ISSUER_CHAIN_CHECK
776777
WOLFSSL_NO_OCSP_OPTIONAL_CERTS
777-
WOLFSSL_NO_PUBLIC_FFDHE
778778
WOLFSSL_NO_RSA_KEY_CHECK
779779
WOLFSSL_NO_SERVER_GROUPS_EXT
780780
WOLFSSL_NO_SESSION_STATS

linuxkm/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ endif
197197
--rename-section .data.rel.local=.data.wolfcrypt \
198198
--rename-section .bss=.bss.wolfcrypt "$$file" || exit $$?
199199
done
200+
[ "$(KERNEL_ARCH_X86)" != "yes" ] || \
200201
{ $(READELF) --syms $(WOLFCRYPT_PIE_FILES) | \
201202
$(AWK) -v obj="$(obj)" ' \
202203
/File:/ { \

linuxkm/linuxkm_memory.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@
2121

2222
/* included by wolfcrypt/src/memory.c */
2323

24+
#if defined(__PIE__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
25+
/* needed in 6.1+ because show_free_areas() static definition in mm.h calls
26+
* __show_free_areas(), which isn't exported (neither was show_free_areas()).
27+
*/
28+
void my__show_free_areas(
29+
unsigned int flags,
30+
nodemask_t *nodemask,
31+
int max_zone_idx)
32+
{
33+
(void)flags;
34+
(void)nodemask;
35+
(void)max_zone_idx;
36+
return;
37+
}
38+
#endif
39+
2440
#if defined(__PIE__) && defined(CONFIG_FORTIFY_SOURCE)
2541
/* needed because FORTIFY_SOURCE inline implementations call fortify_panic(). */
2642
void __my_fortify_panic(const char *name) {

linuxkm/linuxkm_wc_port.h

Lines changed: 104 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@
9191
#define HAVE_KVREALLOC
9292
#endif
9393

94+
#ifdef WOLFCRYPT_ONLY
95+
#ifdef HAVE_KVMALLOC
96+
#define USE_KVMALLOC
97+
#endif
98+
#ifdef HAVE_KVREALLOC
99+
#define USE_KVREALLOC
100+
#endif
101+
#else
102+
/* functioning realloc() is needed for the TLS stack. */
103+
#if defined(HAVE_KVMALLOC) && defined(HAVE_KVREALLOC)
104+
#define USE_KVMALLOC
105+
#define USE_KVREALLOC
106+
#endif
107+
#endif
108+
94109
/* kernel printf doesn't implement fp. */
95110
#ifndef WOLFSSL_NO_FLOAT_FMT
96111
#define WOLFSSL_NO_FLOAT_FMT
@@ -273,6 +288,52 @@
273288

274289
#endif /* !CONFIG_FORTIFY_SOURCE */
275290

291+
#if defined(__PIE__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)) && \
292+
defined(CONFIG_X86)
293+
/* linux/slab.h will recursively bring in linux/page-flags.h, polluting the
294+
* wolfCrypt container objects with static functions const_folio_flags() and
295+
* folio_flags(), unless we kludge it off thusly.
296+
*/
297+
#define PAGE_FLAGS_H
298+
#endif
299+
300+
#include <linux/init.h>
301+
#include <linux/module.h>
302+
#include <linux/delay.h>
303+
304+
#ifdef __PIE__
305+
/* without this, mm.h brings in static, but not inline, pmd_to_page(),
306+
* with direct references to global vmem variables.
307+
*/
308+
#undef USE_SPLIT_PMD_PTLOCKS
309+
#define USE_SPLIT_PMD_PTLOCKS 0
310+
311+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
312+
/* without this, static show_free_areas() mm.h brings in direct
313+
* reference to unexported __show_free_areas().
314+
*/
315+
#define __show_free_areas my__show_free_areas
316+
void my__show_free_areas(
317+
unsigned int flags,
318+
nodemask_t *nodemask,
319+
int max_zone_idx);
320+
#endif
321+
#endif
322+
323+
#if !defined(__PIE__) || (LINUX_VERSION_CODE < KERNEL_VERSION(6, 1, 0))
324+
#include <linux/mm.h>
325+
#endif
326+
327+
#ifndef SINGLE_THREADED
328+
#include <linux/kthread.h>
329+
#endif
330+
#ifndef __PIE__
331+
#include <linux/net.h>
332+
#endif
333+
#include <linux/slab.h>
334+
#include <linux/sched.h>
335+
#include <linux/random.h>
336+
276337
#ifdef LINUXKM_LKCAPI_REGISTER
277338
/* the LKCAPI assumes that expanded encrypt and decrypt keys will stay
278339
* loaded simultaneously, and the Linux in-tree implementations have two
@@ -290,57 +351,31 @@
290351
#ifndef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
291352
#define WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
292353
#endif
293-
#endif /* LINUXKM_LKCAPI_REGISTER */
294354

295-
#include <linux/init.h>
296-
#ifndef __PIE__
297-
#include <linux/module.h>
298-
#include <linux/delay.h>
299-
#endif
300-
301-
#ifdef __PIE__
302-
/* linux/slab.h will recursively bring in linux/page-flags.h, polluting the
303-
* wolfCrypt container objects with static functions const_folio_flags() and
304-
* folio_flags(), unless we kludge it off thusly.
305-
*/
306-
#define PAGE_FLAGS_H
307-
#else
308-
#include <linux/mm.h>
309-
#endif
310-
311-
#include <linux/slab.h>
312-
#include <linux/sched.h>
313-
314-
#ifndef __PIE__
315-
#ifndef SINGLE_THREADED
316-
#include <linux/kthread.h>
317-
#endif
318-
#include <linux/net.h>
319-
320-
#ifdef LINUXKM_LKCAPI_REGISTER
321-
#include <linux/crypto.h>
322-
#include <linux/scatterlist.h>
323-
#include <crypto/scatterwalk.h>
324-
#include <crypto/internal/aead.h>
325-
#include <crypto/internal/hash.h>
326-
#include <crypto/internal/rng.h>
327-
#include <crypto/internal/skcipher.h>
328-
#include <crypto/internal/akcipher.h>
329-
#include <crypto/internal/kpp.h>
330-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 13, 0)
331-
#include <crypto/internal/sig.h>
332-
#endif /* linux ver >= 6.13 */
333-
#ifdef WOLFSSL_LINUXKM_USE_GET_RANDOM_KPROBES
334-
#include <linux/kprobes.h>
335-
#endif
355+
#ifndef __PIE__
356+
#include <linux/crypto.h>
357+
#include <linux/scatterlist.h>
358+
#include <crypto/scatterwalk.h>
359+
#include <crypto/internal/aead.h>
360+
#include <crypto/internal/hash.h>
361+
#include <crypto/internal/rng.h>
362+
#include <crypto/internal/skcipher.h>
363+
#include <crypto/internal/akcipher.h>
364+
#include <crypto/internal/kpp.h>
365+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 13, 0)
366+
#include <crypto/internal/sig.h>
367+
#endif /* linux ver >= 6.13 */
368+
#ifdef WOLFSSL_LINUXKM_USE_GET_RANDOM_KPROBES
369+
#include <linux/kprobes.h>
370+
#endif
336371

337-
#if defined(_LINUX_REFCOUNT_H) || defined(_LINUX_REFCOUNT_TYPES_H)
338-
#define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount.refs)))
339-
#else
340-
#define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount)))
341-
#endif
342-
#endif
343-
#endif /* !__PIE__ */
372+
#if defined(_LINUX_REFCOUNT_H) || defined(_LINUX_REFCOUNT_TYPES_H)
373+
#define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount.refs)))
374+
#else
375+
#define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount)))
376+
#endif
377+
#endif /* !__PIE__ */
378+
#endif /* LINUXKM_LKCAPI_REGISTER */
344379

345380
#if defined(WOLFSSL_AESNI) || defined(USE_INTEL_SPEEDUP) || \
346381
defined(WOLFSSL_SP_X86_64_ASM)
@@ -1171,6 +1206,13 @@
11711206
*/
11721207
#define _MM_MALLOC_H_INCLUDED
11731208

1209+
#ifndef BUILDING_WOLFSSL
1210+
#include <linux/slab.h>
1211+
#if defined(USE_KVMALLOC) && (LINUX_VERSION_CODE < KERNEL_VERSION(5, 16, 0))
1212+
#include <linux/mm.h>
1213+
#endif
1214+
#endif
1215+
11741216
/* fun fact: since linux commit 59bb47985c, kmalloc with power-of-2 size is
11751217
* aligned to the size.
11761218
*/
@@ -1182,10 +1224,10 @@
11821224
((sizeof(_alloc_sz) * 8UL) - __builtin_clzl(_alloc_sz - 1)); \
11831225
_alloc_sz; \
11841226
})
1185-
#ifdef HAVE_KVMALLOC
1227+
#ifdef USE_KVMALLOC
11861228
#define malloc(size) kvmalloc_node(WC_LINUXKM_ROUND_UP_P_OF_2(size), (preempt_count() == 0 ? GFP_KERNEL : GFP_ATOMIC), NUMA_NO_NODE)
11871229
#define free(ptr) kvfree(ptr)
1188-
#ifdef HAVE_KVREALLOC
1230+
#ifdef USE_KVREALLOC
11891231
#define realloc(ptr, newsize) kvrealloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize), (preempt_count() == 0 ? GFP_KERNEL : GFP_ATOMIC))
11901232
#else
11911233
#define realloc(ptr, newsize) ((void)(ptr), (void)(newsize), NULL)
@@ -1212,15 +1254,17 @@
12121254
#endif
12131255
#define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); wolfSSL_Realloc(p, n);})
12141256
#else
1215-
#define XMALLOC(s, h, t) ({(void)(h); (void)(t); malloc(s);})
1216-
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
1217-
#define XFREE(p, h, t) ({(void)(h); (void)(t); free(p);})
1218-
#else
1219-
#define XFREE(p, h, t) ({void* _xp; (void)(h); (void)(t); _xp = (p); if(_xp) free(_xp);})
1220-
#endif
1221-
#if defined(HAVE_KVREALLOC) || !defined(HAVE_KVMALLOC)
1222-
#define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); realloc(p, n);})
1223-
#endif
1257+
#if !defined(XMALLOC_USER) && !defined(XMALLOC_OVERRIDE)
1258+
#define XMALLOC(s, h, t) ({(void)(h); (void)(t); malloc(s);})
1259+
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
1260+
#define XFREE(p, h, t) ({(void)(h); (void)(t); free(p);})
1261+
#else
1262+
#define XFREE(p, h, t) ({void* _xp; (void)(h); (void)(t); _xp = (p); if(_xp) free(_xp);})
1263+
#endif
1264+
#if defined(USE_KVREALLOC) || !defined(USE_KVMALLOC)
1265+
#define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); realloc(p, n);})
1266+
#endif
1267+
#endif /* !XMALLOC_USER && !XMALLOC_OVERRIDE */
12241268
#endif
12251269

12261270
#include <linux/limits.h>

linuxkm/lkcapi_glue.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
#define WOLFSSL_LINUXKM_LKCAPI_PRIORITY 100000
6565
#endif
6666

67-
#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
67+
#if defined(CONFIG_CRYPTO_MANAGER_EXTRA_TESTS) || \
68+
defined(CONFIG_CRYPTO_SELFTESTS_FULL)
6869
static int disable_setkey_warnings = 0;
6970
#else
7071
#define disable_setkey_warnings 0
@@ -321,7 +322,8 @@ static int linuxkm_lkcapi_register(void)
321322
if (ret)
322323
return ret;
323324

324-
#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
325+
#if defined(CONFIG_CRYPTO_MANAGER_EXTRA_TESTS) || \
326+
defined(CONFIG_CRYPTO_SELFTESTS_FULL)
325327
/* temporarily disable warnings around setkey failures, which are expected
326328
* from the crypto fuzzer in FIPS configs, and potentially in others.
327329
* unexpected setkey failures are fatal errors returned by the fuzzer.
@@ -692,7 +694,8 @@ static int linuxkm_lkcapi_register(void)
692694
#undef REGISTER_ALG
693695
#undef REGISTER_ALG_OPTIONAL
694696

695-
#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
697+
#if defined(CONFIG_CRYPTO_MANAGER_EXTRA_TESTS) || \
698+
defined(CONFIG_CRYPTO_SELFTESTS_FULL)
696699
disable_setkey_warnings = 0;
697700
#endif
698701

0 commit comments

Comments
 (0)