Skip to content

Commit 7c6afeb

Browse files
committed
add linuxkm/wolfcrypt.lds module linker script, explicitly grouping wolfcrypt sections together;
linuxkm/Kbuild: add linker script flag, containerize several more previously-missed ELF sections, and add a test verifying no sections were missed; linuxkm/linuxkm_memory.c: remove obsolete lkm_realloc() shim and unneeded my__show_free_areas() wrapper; linuxkm/linuxkm_wc_port.h: add new mapping from realloc() to native kvrealloc(), and gate out a slew of headers when __PIE__ to avoid polluting wolfCrypt objects with various unneeded header-implemented functions with associated awkward symbols references; linuxkm/lkcapi_glue.c: harmonize gate for REGISTER_ALG_OPTIONAL(); linuxkm/module_hooks.c: add "ERROR:" prefixes on pr_err()s; add wc_RunAllCast_fips() at shutdown to send confidence verification to the kernel log; remove section bounds checks now that layout is unreliable; wolfssl/wolfcrypt/settings.h: for WOLFSSL_LINUXKM && HAVE_LINUXKM_PIE_SUPPORT, #define WOLFSSL_ECC_CURVE_STATIC and WOLFSSL_NAMES_STATIC; wolfssl/wolfcrypt/types.h: refactor the typedef for wcchar from a pointer to a char[]; wolfcrypt/src/wc_xmss.c and wolfssl/wolfcrypt/wc_lms.h: add WOLFSSL_NAMES_STATIC code paths for struct wc_XmssString and struct wc_LmsParamsMap; wolfcrypt/src/asn.c: add WOLFSSL_NAMES_STATIC code paths for struct CertNameData, and add static attribute to a slew of wcchars not used or declared outside asn.c.
1 parent f733ade commit 7c6afeb

11 files changed

Lines changed: 270 additions & 272 deletions

File tree

linuxkm/Kbuild

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ endif
151151

152152
ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
153153

154+
LDFLAGS_libwolfssl.o += -T $(src)/wolfcrypt.lds
155+
154156
rename-pie-text-and-data-sections: $(WOLFSSL_OBJ_TARGETS)
155157

156158
ifndef NM
@@ -186,8 +188,39 @@ ifneq "$(quiet)" "silent_"
186188
endif
187189
cd "$(obj)" || exit $$?
188190
for file in $(WOLFCRYPT_PIE_FILES); do
189-
$(OBJCOPY) --rename-section .text=.text.wolfcrypt --rename-section .data=.data.wolfcrypt --rename-section .rodata=.rodata.wolfcrypt "$$file" || exit $$?
191+
$(OBJCOPY) --rename-section .text=.text.wolfcrypt \
192+
--rename-section .text.unlikely=.text.wolfcrypt \
193+
--rename-section .rodata=.rodata.wolfcrypt \
194+
--rename-section .rodata.str1.1=.rodata.wolfcrypt \
195+
--rename-section .rodata.str1.8=.rodata.wolfcrypt \
196+
--rename-section .data=.data.wolfcrypt \
197+
--rename-section .data.rel.local=.data.wolfcrypt \
198+
--rename-section .bss=.bss.wolfcrypt "$$file" || exit $$?
190199
done
200+
{ $(READELF) --syms $(WOLFCRYPT_PIE_FILES) | \
201+
$(AWK) -v obj="$(obj)" ' \
202+
/File:/ { \
203+
if (substr($$2, 1, length(obj)) == obj) { \
204+
curfile = substr($$2, length(obj) + 2); \
205+
} else { \
206+
curfile=$$2; \
207+
} \
208+
next; \
209+
} \
210+
{ \
211+
if (($$4 == "SECTION") && ($$8 !~ "wolfcrypt")) {\
212+
if (! ((curfile ";" $$8) in warned_on)) { \
213+
print curfile ": " $$8 >"/dev/stderr"; \
214+
warned_on[curfile ": " $$8] = 1; \
215+
++warnings; \
216+
}}} \
217+
END { \
218+
if (warnings) { \
219+
exit(1); \
220+
} else { \
221+
exit(0); \
222+
}}'; } || \
223+
{ echo 'Error: section(s) missed by containerization.' >&2; exit 1; }
191224
ifneq "$(quiet)" "silent_"
192225
echo ' wolfCrypt .{text,data,rodata} sections containerized to .{text,data,rodata}.wolfcrypt'
193226
endif

linuxkm/linuxkm_memory.c

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

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

24-
#ifdef HAVE_KVMALLOC
25-
/* adapted from kvrealloc() draft by Changli Gao, 2010-05-13 */
26-
void *lkm_realloc(void *ptr, size_t newsize) {
27-
void *nptr;
28-
size_t oldsize;
29-
30-
if (unlikely(newsize == 0)) {
31-
kvfree(ptr);
32-
return ZERO_SIZE_PTR;
33-
}
34-
35-
if (unlikely(ptr == NULL))
36-
return kvmalloc_node(newsize, GFP_KERNEL, NUMA_NO_NODE);
37-
38-
if (is_vmalloc_addr(ptr)) {
39-
/* no way to discern the size of the old allocation,
40-
* because the kernel doesn't export find_vm_area(). if
41-
* it did, we could then call get_vm_area_size() on the
42-
* returned struct vm_struct.
43-
*/
44-
return NULL;
45-
} else {
46-
#ifndef __PIE__
47-
struct page *page;
48-
49-
page = virt_to_head_page(ptr);
50-
if (PageSlab(page) || PageCompound(page)) {
51-
if (newsize < PAGE_SIZE)
52-
#endif /* ! __PIE__ */
53-
return krealloc(ptr, newsize, GFP_KERNEL);
54-
#ifndef __PIE__
55-
oldsize = ksize(ptr);
56-
} else {
57-
oldsize = page->private;
58-
if (newsize <= oldsize)
59-
return ptr;
60-
}
61-
#endif /* ! __PIE__ */
62-
}
63-
64-
nptr = kvmalloc_node(newsize, GFP_KERNEL, NUMA_NO_NODE);
65-
if (nptr != NULL) {
66-
memcpy(nptr, ptr, oldsize);
67-
kvfree(ptr);
68-
}
69-
70-
return nptr;
71-
}
72-
#endif /* HAVE_KVMALLOC */
73-
74-
#if defined(__PIE__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
75-
/* needed in 6.1+ because show_free_areas() static definition in mm.h calls
76-
* __show_free_areas(), which isn't exported (neither was show_free_areas()).
77-
*/
78-
void my__show_free_areas(
79-
unsigned int flags,
80-
nodemask_t *nodemask,
81-
int max_zone_idx)
82-
{
83-
(void)flags;
84-
(void)nodemask;
85-
(void)max_zone_idx;
86-
return;
87-
}
88-
#endif
89-
9024
#if defined(__PIE__) && defined(CONFIG_FORTIFY_SOURCE)
9125
/* needed because FORTIFY_SOURCE inline implementations call fortify_panic(). */
9226
void __my_fortify_panic(const char *name) {

linuxkm/linuxkm_wc_port.h

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@
7777
#define ALIGN16 __attribute__ ( (aligned (32)))
7878
#endif
7979

80-
/* kvmalloc()/kvfree() and friends added in linux commit a7c3e901 */
81-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
80+
/* kvmalloc()/kvfree() and friends added in linux commit a7c3e901, merged for 4.12.
81+
* kvrealloc() added in de2860f463, merged for 5.15, backported to 5.10.137.
82+
* moved to ultimate home (slab.h) in 8587ca6f34, merged for 5.16.
83+
84+
*/
85+
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)) || \
86+
((LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 137)) && \
87+
(LINUX_VERSION_CODE < KERNEL_VERSION(5, 11, 90)))
8288
#define HAVE_KVMALLOC
8389
#endif
8490

@@ -258,36 +264,54 @@
258264
#undef memmove
259265
#define memmove my_memmove
260266

261-
#endif /* CONFIG_FORTIFY_SOURCE */
267+
#else /* !CONFIG_FORTIFY_SOURCE */
268+
269+
#include <linux/string.h>
270+
271+
#endif /* !CONFIG_FORTIFY_SOURCE */
272+
273+
#ifdef LINUXKM_LKCAPI_REGISTER
274+
/* the LKCAPI assumes that expanded encrypt and decrypt keys will stay
275+
* loaded simultaneously, and the Linux in-tree implementations have two
276+
* AES key structs in each context, one for each direction. in
277+
* linuxkm/lkcapi_aes_glue.c, we do the same
278+
* thing with "struct km_AesCtx". however, wolfCrypt struct AesXts
279+
* already has two AES expanded keys, the main and tweak, and the tweak
280+
* is always used in the encrypt direction regardless of the main
281+
* direction. to avoid allocating and computing a duplicate second
282+
* tweak encrypt key, we set
283+
* WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS, which adds a second
284+
* Aes slot to wolfCrypt's struct AesXts, and activates support for
285+
* AES_ENCRYPTION_AND_DECRYPTION on AES-XTS.
286+
*/
287+
#ifndef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
288+
#define WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
289+
#endif
290+
#endif /* LINUXKM_LKCAPI_REGISTER */
262291

263292
#include <linux/init.h>
293+
#ifndef __PIE__
264294
#include <linux/module.h>
265295
#include <linux/delay.h>
296+
#endif
266297

267-
#ifdef __PIE__
268-
/* without this, mm.h brings in static, but not inline, pmd_to_page(),
269-
* with direct references to global vmem variables.
270-
*/
271-
#undef USE_SPLIT_PMD_PTLOCKS
272-
#define USE_SPLIT_PMD_PTLOCKS 0
273-
274-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
275-
/* without this, static show_free_areas() mm.h brings in direct
276-
* reference to unexported __show_free_areas().
277-
*/
278-
#define __show_free_areas my__show_free_areas
279-
void my__show_free_areas(
280-
unsigned int flags,
281-
nodemask_t *nodemask,
282-
int max_zone_idx);
283-
#endif
284-
#endif
298+
#ifdef __PIE__
299+
/* linux/slab.h will recursively bring in linux/page-flags.h, polluting the
300+
* wolfCrypt container objects with static functions const_folio_flags() and
301+
* folio_flags(), unless we kludge it off thusly.
302+
*/
303+
#define PAGE_FLAGS_H
304+
#else
285305
#include <linux/mm.h>
306+
#endif
307+
308+
#include <linux/slab.h>
309+
310+
#ifndef __PIE__
286311
#ifndef SINGLE_THREADED
287312
#include <linux/kthread.h>
288313
#endif
289314
#include <linux/net.h>
290-
#include <linux/slab.h>
291315

292316
#ifdef LINUXKM_LKCAPI_REGISTER
293317
#include <linux/crypto.h>
@@ -306,29 +330,13 @@
306330
#include <linux/kprobes.h>
307331
#endif
308332

309-
/* the LKCAPI assumes that expanded encrypt and decrypt keys will stay
310-
* loaded simultaneously, and the Linux in-tree implementations have two
311-
* AES key structs in each context, one for each direction. in
312-
* linuxkm/lkcapi_aes_glue.c, we do the same
313-
* thing with "struct km_AesCtx". however, wolfCrypt struct AesXts
314-
* already has two AES expanded keys, the main and tweak, and the tweak
315-
* is always used in the encrypt direction regardless of the main
316-
* direction. to avoid allocating and computing a duplicate second
317-
* tweak encrypt key, we set
318-
* WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS, which adds a second
319-
* Aes slot to wolfCrypt's struct AesXts, and activates support for
320-
* AES_ENCRYPTION_AND_DECRYPTION on AES-XTS.
321-
*/
322-
#ifndef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
323-
#define WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
324-
#endif
325-
326333
#if defined(_LINUX_REFCOUNT_H) || defined(_LINUX_REFCOUNT_TYPES_H)
327334
#define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount.refs)))
328335
#else
329336
#define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount)))
330337
#endif
331338
#endif
339+
#endif /* !__PIE__ */
332340

333341
#if defined(WOLFSSL_AESNI) || defined(USE_INTEL_SPEEDUP) || \
334342
defined(WOLFSSL_SP_X86_64_ASM)
@@ -622,17 +630,20 @@
622630
typeof(kzalloc_noprof) *kzalloc_noprof;
623631
typeof(__kvmalloc_node_noprof) *__kvmalloc_node_noprof;
624632
typeof(__kmalloc_cache_noprof) *__kmalloc_cache_noprof;
633+
typeof(kvrealloc_noprof) *kvrealloc_noprof;
625634
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
626635
typeof(kmalloc_noprof) *kmalloc_noprof;
627636
typeof(krealloc_noprof) *krealloc_noprof;
628637
typeof(kzalloc_noprof) *kzalloc_noprof;
629638
typeof(kvmalloc_node_noprof) *kvmalloc_node_noprof;
630639
typeof(kmalloc_trace_noprof) *kmalloc_trace_noprof;
640+
typeof(kvrealloc_noprof) *kvrealloc_noprof;
631641
#else /* <6.10.0 */
632642
typeof(kmalloc) *kmalloc;
633643
typeof(krealloc) *krealloc;
634644
#ifdef HAVE_KVMALLOC
635-
typeof(kvmalloc_node) *kvmalloc_node;
645+
typeof(kvmalloc_node) *kvmalloc_node;
646+
typeof(kvrealloc) *kvrealloc;
636647
#endif
637648
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
638649
typeof(kmalloc_trace) *kmalloc_trace;
@@ -646,7 +657,6 @@
646657
#endif
647658
typeof(kfree) *kfree;
648659
typeof(ksize) *ksize;
649-
typeof(is_vmalloc_addr) *is_vmalloc_addr;
650660

651661
typeof(get_random_bytes) *get_random_bytes;
652662
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
@@ -854,19 +864,22 @@
854864
#define kzalloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kzalloc_noprof)
855865
#define __kvmalloc_node_noprof (wolfssl_linuxkm_get_pie_redirect_table()->__kvmalloc_node_noprof)
856866
#define __kmalloc_cache_noprof (wolfssl_linuxkm_get_pie_redirect_table()->__kmalloc_cache_noprof)
867+
#define kvrealloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kvrealloc_noprof)
857868
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
858869
/* see include/linux/alloc_tag.h and include/linux/slab.h */
859870
#define kmalloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_noprof)
860871
#define krealloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->krealloc_noprof)
861872
#define kzalloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kzalloc_noprof)
862873
#define kvmalloc_node_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kvmalloc_node_noprof)
863874
#define kmalloc_trace_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_trace_noprof)
875+
#define kvrealloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kvrealloc_noprof)
864876
#else /* <6.10.0 */
865877
#define kmalloc (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc)
866878
#define krealloc (wolfssl_linuxkm_get_pie_redirect_table()->krealloc)
867879
#define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO)
868880
#ifdef HAVE_KVMALLOC
869881
#define kvmalloc_node (wolfssl_linuxkm_get_pie_redirect_table()->kvmalloc_node)
882+
#define kvrealloc (wolfssl_linuxkm_get_pie_redirect_table()->kvrealloc)
870883
#endif
871884
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
872885
#define kmalloc_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_trace)
@@ -882,8 +895,6 @@
882895
#endif
883896
#define ksize (wolfssl_linuxkm_get_pie_redirect_table()->ksize)
884897

885-
#define is_vmalloc_addr (wolfssl_linuxkm_get_pie_redirect_table()->is_vmalloc_addr)
886-
887898
#define get_random_bytes (wolfssl_linuxkm_get_pie_redirect_table()->get_random_bytes)
888899
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
889900
#define getnstimeofday (wolfssl_linuxkm_get_pie_redirect_table()->getnstimeofday)
@@ -1162,10 +1173,9 @@
11621173
_alloc_sz; \
11631174
})
11641175
#ifdef HAVE_KVMALLOC
1165-
#define malloc(size) kvmalloc_node(WC_LINUXKM_ROUND_UP_P_OF_2(size), (preempt_count() == 0 ? GFP_KERNEL : GFP_ATOMIC), NUMA_NO_NODE)
1176+
#define malloc(size) kvmalloc_node(WC_LINUXKM_ROUND_UP_P_OF_2(size), (preempt_count() == 0 ? GFP_KERNEL : GFP_ATOMIC), NUMA_NO_NODE)
11661177
#define free(ptr) kvfree(ptr)
1167-
void *lkm_realloc(void *ptr, size_t newsize);
1168-
#define realloc(ptr, newsize) lkm_realloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize))
1178+
#define realloc(ptr, newsize) kvrealloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize), (preempt_count() == 0 ? GFP_KERNEL : GFP_ATOMIC))
11691179
#else
11701180
#define malloc(size) kmalloc(WC_LINUXKM_ROUND_UP_P_OF_2(size), (preempt_count() == 0 ? GFP_KERNEL : GFP_ATOMIC))
11711181
#define free(ptr) kfree(ptr)

linuxkm/lkcapi_glue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ static int linuxkm_lkcapi_register(void)
591591
* on here is for ECDH loading to be optional when fips and fips tests are
592592
* enabled. Failures because of !fips_allowed are skipped over.
593593
*/
594-
#if defined(CONFIG_CRYPTO_FIPS) && \
594+
#if defined(HAVE_FIPS) && defined(CONFIG_CRYPTO_FIPS) && \
595595
defined(CONFIG_CRYPTO_MANAGER) && \
596596
!defined(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS)
597597
#if defined(LINUXKM_ECC192)

0 commit comments

Comments
 (0)