Skip to content

Commit f67c29a

Browse files
committed
linuxkm/Kbuild:
* for aarch64/arm64, only add -mno-outline-atomics if the compiler supports it. * in ENABLED_LINUXKM_PIE setup, avoid -fPIE on arm32 <5.11 (missing reloc support). linuxkm/linuxkm_wc_port.h, linuxkm/module_hooks.c, and wolfcrypt/src/wc_port.c: gate interception of alt_cb_patch_nops() on kernel >= 6.1. linuxkm/linuxkm_wc_port.h: define WC_LINUXKM_SUPPORT_DUMP_TO_FILE implicitly when WC_SYM_RELOC_TABLES && DEBUG_LINUXKM_PIE_SUPPORT. linuxkm/module_hooks.c: fixes for text_dump_path and rodata_dump_path handler code.
1 parent 8d1b825 commit f67c29a

5 files changed

Lines changed: 44 additions & 8 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,6 @@ 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
641640
WC_LMS_FULL_HASH
642641
WC_NO_ASYNC_SLEEP
643642
WC_NO_RNG_SIMPLE

linuxkm/Kbuild

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ endif
3939

4040
WOLFSSL_CFLAGS += -ffreestanding -Wframe-larger-than=$(MAX_STACK_FRAME_SIZE) -isystem $(shell $(CC) -print-file-name=include)
4141

42+
# -moutline-atomics added in gcc 10.1 for ARMv8.0.
43+
AARCH64_NO_OUTLINE_ATOMICS := $(shell { echo -e 'int f(void) {\n return 0;\n}\n' | $(CC) -mno-outline-atomics -x c -c - -o /dev/null 2>/dev/null; } && echo -mno-outline-atomics)
44+
4245
ifeq "$(KERNEL_ARCH)" "aarch64"
43-
WOLFSSL_CFLAGS += -mno-outline-atomics
46+
WOLFSSL_CFLAGS += $(AARCH64_NO_OUTLINE_ATOMICS)
4447
else ifeq "$(KERNEL_ARCH)" "arm64"
45-
WOLFSSL_CFLAGS += -mno-outline-atomics
48+
WOLFSSL_CFLAGS += $(AARCH64_NO_OUTLINE_ATOMICS)
4649
else ifeq "$(KERNEL_ARCH)" "arm"
4750
# avoids R_ARM_THM_JUMP11 relocations, including a stubborn tail recursion
4851
# optimization from wc_sp_cmp to wc_sp_cmp_mag:
@@ -111,7 +114,21 @@ ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
111114
# note, we need -fno-stack-protector to avoid references to
112115
# "__stack_chk_fail" from the wolfCrypt container.
113116
PIE_FLAGS := -DWC_CONTAINERIZE_THIS -fno-stack-protector -fno-toplevel-reorder
114-
# some targets can't handle -fpie. E.g. ARM32 on kernel <=5.10 has no handling for R_ARM_REL32.
117+
118+
ifndef NO_PIE_FLAG
119+
ifeq ($(KERNEL_ARCH),arm)
120+
ifeq ($(intcmp $(VERSION),5,1,0,0),1)
121+
NO_PIE_FLAG :=
122+
$(info Note: disabling -fPIE to avoid R_ARM_REL32 on pre-5.11 target kernel.)
123+
else
124+
ifeq ($(intcmp $(VERSION),5,0,1,0)-$(intcmp $(PATCHLEVEL),11,1,0,0),1-1)
125+
NO_PIE_FLAG :=
126+
$(info Note: disabling -fPIE to avoid R_ARM_REL32 on pre-5.11 target kernel.)
127+
endif
128+
endif
129+
endif
130+
endif
131+
115132
ifdef NO_PIE_FLAG
116133
PIE_FLAGS += -DWC_NO_PIE_FLAG
117134
else

linuxkm/linuxkm_wc_port.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,13 @@
301301
#endif
302302

303303
#if defined(WC_CONTAINERIZE_THIS) && defined(CONFIG_ARM64)
304-
#define alt_cb_patch_nops my__alt_cb_patch_nops
304+
/* alt_cb_patch_nops and queued_spin_lock_slowpath are defined early
305+
* to allow shimming in system headers.
306+
*/
307+
/* alt_cb_patch_nops added by d926079f17, release 6.1 */
308+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
309+
#define alt_cb_patch_nops my__alt_cb_patch_nops
310+
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0) */
305311
#define queued_spin_lock_slowpath my__queued_spin_lock_slowpath
306312
#endif
307313

@@ -509,6 +515,11 @@
509515
#endif /* !WOLFCRYPT_ONLY */
510516
#endif /* !WC_CONTAINERIZE_THIS */
511517

518+
#if defined(WC_SYM_RELOC_TABLES) && defined(DEBUG_LINUXKM_PIE_SUPPORT) && \
519+
!defined(WC_LINUXKM_SUPPORT_DUMP_TO_FILE)
520+
#define WC_LINUXKM_SUPPORT_DUMP_TO_FILE
521+
#endif
522+
512523
#ifdef WC_LINUXKM_SUPPORT_DUMP_TO_FILE
513524
#include <linux/fs.h>
514525
#include <linux/uaccess.h>
@@ -1116,12 +1127,16 @@
11161127
* to allow shimming in system headers, but now we need the native
11171128
* ones.
11181129
*/
1130+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
11191131
#undef alt_cb_patch_nops
11201132
typeof(my__alt_cb_patch_nops) *alt_cb_patch_nops;
1133+
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0) */
11211134
#undef queued_spin_lock_slowpath
11221135
typeof(my__queued_spin_lock_slowpath) *queued_spin_lock_slowpath;
11231136
#else
1137+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
11241138
typeof(alt_cb_patch_nops) *alt_cb_patch_nops;
1139+
#endif
11251140
typeof(queued_spin_lock_slowpath) *queued_spin_lock_slowpath;
11261141
#endif
11271142
#endif

linuxkm/module_hooks.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,12 @@ static int wolfssl_init(void)
575575

576576
#ifdef WC_SYM_RELOC_TABLES
577577
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)
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)
579579
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);
580580
}
581581
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);
582+
if (dump_to_file(rodata_dump_path, (u8 *)__wc_rodata_start, (size_t)((uintptr_t)__wc_rodata_end - (uintptr_t)__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), rodata_dump_path);
584584
}
585585
#else
586586
if ((text_dump_path != NULL) ||
@@ -1536,7 +1536,9 @@ static int set_up_wolfssl_linuxkm_pie_redirect_table(void) {
15361536

15371537
#ifdef CONFIG_ARM64
15381538
#ifndef CONFIG_ARCH_TEGRA
1539+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
15391540
wolfssl_linuxkm_pie_redirect_table.alt_cb_patch_nops = alt_cb_patch_nops;
1541+
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0) */
15401542
wolfssl_linuxkm_pie_redirect_table.queued_spin_lock_slowpath = queued_spin_lock_slowpath;
15411543
#endif
15421544
#endif

wolfcrypt/src/wc_port.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5008,12 +5008,15 @@ char* wolfSSL_strnstr(const char* s1, const char* s2, unsigned int n)
50085008
#if defined(WOLFSSL_LINUXKM) && defined(CONFIG_ARM64) && \
50095009
defined(WC_SYM_RELOC_TABLES)
50105010
#ifndef CONFIG_ARCH_TEGRA
5011+
5012+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
50115013
noinstr void my__alt_cb_patch_nops(struct alt_instr *alt, __le32 *origptr,
50125014
__le32 *updptr, int nr_inst)
50135015
{
50145016
return WC_PIE_INDIRECT_SYM(alt_cb_patch_nops)
50155017
(alt, origptr, updptr, nr_inst);
50165018
}
5019+
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0) */
50175020

50185021
void my__queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
50195022
{

0 commit comments

Comments
 (0)