Skip to content

Commit 7aab2f3

Browse files
authored
Merge pull request #9126 from douzzer/20250823-linuxkm-reloc-bikeshedding
20250823-linuxkm-reloc-bikeshedding
2 parents 1c2fb10 + a67d1a8 commit 7aab2f3

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

configure.ac

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,6 @@ then
12931293
test "$enable_certext" = "" && enable_certext=yes
12941294
test "$enable_sep" = "" && enable_sep=yes
12951295
test "$enable_hkdf" = "" && enable_hkdf=yes
1296-
test "$enable_fpecc" = "" && test "$enable_ecc" != "no" && enable_fpecc=yes
12971296
test "$enable_eccencrypt" = "" && test "$enable_ecc" != "no" && enable_eccencrypt=yes
12981297
test "$enable_psk" = "" && enable_psk=yes
12991298
test "$enable_cmac" = "" && enable_cmac=yes
@@ -1323,6 +1322,13 @@ then
13231322
test "$enable_anon" = "" && enable_anon=yes
13241323
test "$enable_ssh" = "" && test "$enable_hmac" != "no" && enable_ssh=yes
13251324

1325+
# the compiler optimizer generates a weird out-of-bounds bss reference for
1326+
# find_hole() in the FP_ECC implementation.
1327+
if test "$ENABLED_LINUXKM_PIE" != yes
1328+
then
1329+
test "$enable_fpecc" = "" && test "$enable_ecc" != "no" && enable_fpecc=yes
1330+
fi
1331+
13261332
if test "x$FIPS_VERSION" != "xv1"
13271333
then
13281334
test "$enable_rsapss" = "" && enable_rsapss=yes

linuxkm/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
105105
@$(READELF) --wide -r libwolfssl.ko | \
106106
$(AWK) 'BEGIN { \
107107
n=0; \
108+
bad_relocs=0; \
108109
printf("%s\n ", \
109-
"const unsigned int wc_linuxkm_pie_reloc_tab[] = { "); \
110+
"const unsigned int wc_linuxkm_pie_reloc_tab[] = { "); \
110111
} \
111112
/^Relocation section '\''\.rela\.text\.wolfcrypt'\''/ { \
112113
p=1; \
@@ -117,12 +118,20 @@ ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
117118
} \
118119
/^0/ { \
119120
if (p) { \
121+
if ($$3 !~ "^(R_X86_64_PLT32|R_X86_64_PC32|R_AARCH64_.*)$$") { \
122+
print "Unexpected relocation type:\n" $$0 >"/dev/stderr"; \
123+
++bad_relocs; \
124+
} \
120125
printf("0x%s%s", \
121126
gensub("^0*","",1,$$1), \
122127
((++n%8) ? ", " : ",\n ")); \
123128
} \
124129
} \
125130
END { \
131+
if (bad_relocs) { \
132+
print "Found " bad_relocs " unexpected relocations." >"/dev/stderr"; \
133+
exit(1); \
134+
} \
126135
print "~0U };\nconst size_t wc_linuxkm_pie_reloc_tab_length = sizeof wc_linuxkm_pie_reloc_tab / sizeof wc_linuxkm_pie_reloc_tab[0];";\
127136
}' > wc_linuxkm_pie_reloc_tab.c
128137
+$(MAKE) ARCH='$(KERNEL_ARCH)' $(OVERRIDE_PATHS) $(CROSS_COMPILE) -C '$(KERNEL_ROOT)' M='$(MODULE_TOP)' $(KBUILD_EXTRA_FLAGS) CC_FLAGS_FTRACE=

linuxkm/module_hooks.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,9 @@ ssize_t wc_linuxkm_normalize_relocations(
710710
int n_text_r = 0, n_rodata_r = 0, n_rwdata_r = 0, n_bss_r = 0, n_other_r = 0;
711711
#endif
712712

713-
if ((text_in < __wc_text_start) ||
714-
(text_in >= __wc_text_end))
713+
if ((text_in_len == 0) ||
714+
(text_in < __wc_text_start) ||
715+
(text_in + text_in_len >= __wc_text_end))
715716
{
716717
return -1;
717718
}
@@ -762,7 +763,7 @@ ssize_t wc_linuxkm_normalize_relocations(
762763
abs_ptr = (uintptr_t)text_in + next_reloc + 4 + reloc_buf;
763764

764765
if ((abs_ptr >= (uintptr_t)__wc_text_start) &&
765-
(abs_ptr < (uintptr_t)__wc_text_end))
766+
(abs_ptr <= (uintptr_t)__wc_text_end))
766767
{
767768
/* internal references in the .wolfcrypt.text segment don't need
768769
* normalization.
@@ -773,7 +774,7 @@ ssize_t wc_linuxkm_normalize_relocations(
773774
continue;
774775
}
775776
else if ((abs_ptr >= (uintptr_t)__wc_rodata_start) &&
776-
(abs_ptr < (uintptr_t)__wc_rodata_end))
777+
(abs_ptr <= (uintptr_t)__wc_rodata_end))
777778
{
778779
#ifdef DEBUG_LINUXKM_PIE_SUPPORT
779780
++n_rodata_r;
@@ -783,7 +784,7 @@ ssize_t wc_linuxkm_normalize_relocations(
783784
reloc_buf |= WC_RODATA_TAG;
784785
}
785786
else if ((abs_ptr >= (uintptr_t)__wc_rwdata_start) &&
786-
(abs_ptr < (uintptr_t)__wc_rwdata_end))
787+
(abs_ptr <= (uintptr_t)__wc_rwdata_end))
787788
{
788789
#ifdef DEBUG_LINUXKM_PIE_SUPPORT
789790
++n_rwdata_r;
@@ -793,7 +794,7 @@ ssize_t wc_linuxkm_normalize_relocations(
793794
reloc_buf |= WC_RWDATA_TAG;
794795
}
795796
else if ((abs_ptr >= (uintptr_t)__wc_bss_start) &&
796-
(abs_ptr < (uintptr_t)__wc_bss_end))
797+
(abs_ptr <= (uintptr_t)__wc_bss_end))
797798
{
798799
#ifdef DEBUG_LINUXKM_PIE_SUPPORT
799800
++n_bss_r;
@@ -809,28 +810,33 @@ ssize_t wc_linuxkm_normalize_relocations(
809810
reloc_buf = WC_OTHER_TAG;
810811
#ifdef DEBUG_LINUXKM_PIE_SUPPORT
811812
++n_other_r;
813+
/* we're currently only handling 32 bit relocations (R_X86_64_PLT32
814+
* and R_X86_64_PC32) so the top half of the word64 is padding we
815+
* can lop off for rendering.
816+
*/
812817
pr_notice("found non-wolfcrypt relocation at text offset 0x%x to "
813-
"addr 0x%lx, text=%px-%px, rodata=%px-%px, "
814-
"rwdata=%px-%px, bss=%px-%px\n",
818+
"addr 0x%x, text=%x-%x, rodata=%x-%x, "
819+
"rwdata=%x-%x, bss=%x-%x\n",
815820
wc_linuxkm_pie_reloc_tab[i],
816-
abs_ptr,
817-
__wc_text_start,
818-
__wc_text_end,
819-
__wc_rodata_start,
820-
__wc_rodata_end,
821-
__wc_rwdata_start,
822-
__wc_rwdata_end,
823-
__wc_bss_start,
824-
__wc_bss_end);
821+
(unsigned)(uintptr_t)abs_ptr,
822+
(unsigned)(uintptr_t)__wc_text_start,
823+
(unsigned)(uintptr_t)__wc_text_end,
824+
(unsigned)(uintptr_t)__wc_rodata_start,
825+
(unsigned)(uintptr_t)__wc_rodata_end,
826+
(unsigned)(uintptr_t)__wc_rwdata_start,
827+
(unsigned)(uintptr_t)__wc_rwdata_end,
828+
(unsigned)(uintptr_t)__wc_bss_start,
829+
(unsigned)(uintptr_t)__wc_bss_end);
825830
#endif
826831
}
827832
put_unaligned((u32)reloc_buf, (int32_t *)&text_out[next_reloc]);
828833
}
829834

830835
#ifdef DEBUG_LINUXKM_PIE_SUPPORT
831836
if (n_other_r > 0)
832-
pr_notice("text_in=%px relocs=%d/%d/%d/%d/%d ret = %zu\n",
833-
text_in, n_text_r, n_rodata_r, n_rwdata_r, n_bss_r, n_other_r,
837+
pr_notice("text_in=%x relocs=%d/%d/%d/%d/%d ret = %zu\n",
838+
(unsigned)(uintptr_t)text_in, n_text_r, n_rodata_r,
839+
n_rwdata_r, n_bss_r, n_other_r,
834840
text_in_len);
835841
#endif
836842

linuxkm/wolfcrypt.lds

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@ SECTIONS {
33
.text.wolfcrypt : {
44
__wc_text_start = .;
55
*(.text.wolfcrypt)
6+
. = ALIGN(4096);
67
__wc_text_end = .;
78
}
89
. = ALIGN(4096);
910
.rodata.wolfcrypt : {
1011
__wc_rodata_start = .;
1112
*(.rodata.wolfcrypt)
13+
. = ALIGN(4096);
1214
__wc_rodata_end = .;
1315
}
1416
. = ALIGN(4096);
1517
.data.wolfcrypt : {
1618
__wc_rwdata_start = .;
1719
*(.data.wolfcrypt)
20+
. = ALIGN(4096);
1821
__wc_rwdata_end = .;
1922
}
2023
. = ALIGN(4096);
2124
.bss.wolfcrypt : {
2225
__wc_bss_start = .;
2326
*(.bss.wolfcrypt)
27+
. = ALIGN(4096);
2428
__wc_bss_end = .;
2529
}
2630
. = ALIGN(4096);

0 commit comments

Comments
 (0)