Skip to content

Commit c091c8b

Browse files
authored
Merge pull request #9318 from douzzer/20251017-linuxkm-signal-handling-tweaks
20251017-linuxkm-signal-handling-tweaks
2 parents d475985 + 69f236b commit c091c8b

7 files changed

Lines changed: 226 additions & 76 deletions

File tree

.github/workflows/linuxkm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
echo "preparing target kernel $(uname -r)"
3737
pushd "/lib/modules/$(uname -r)/build" || $(exit 4)
3838
if [ -f /proc/config.gz ]; then gzip -dc /proc/config.gz > /tmp/.config && sudo mv /tmp/.config . || $(exit 5); elif [ -f "/boot/config-$(uname -r)" ]; then sudo cp -p "/boot/config-$(uname -r)" .config || $(exit 6); fi
39-
sudo make -j 4 oldconfig || $(exit 7)
39+
sudo make -j 4 olddefconfig || $(exit 7)
4040
sudo make M="$(pwd)" modules_prepare || $(exit 8)
4141
popd >/dev/null
4242

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ CONFIG_ESP_WOLFSSL_NO_HW_RSA_PRI_MULMOD
105105
CONFIG_ESP_WOLFSSL_NO_STACK_SIZE_BUILD_WARNING
106106
CONFIG_FREERTOS_HZ
107107
CONFIG_FREERTOS_UNICORE
108+
CONFIG_HAVE_KPROBES
108109
CONFIG_IDF_TARGET
109110
CONFIG_IDF_TARGET_ARCH_RISCV
110111
CONFIG_IDF_TARGET_ARCH_XTENSA

configure.ac

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,7 @@ then
653653
AS_CASE([$xxx_ar_flags],[*'use zero for timestamps and uids/gids'*],[AR_FLAGS="Dcr" lt_ar_flags="Dcr"])
654654
AS_CASE([$xxx_ranlib_flags],[*'Use zero for symbol map timestamp'*],[RANLIB="${RANLIB} -D"])
655655

656-
if test "$ENABLED_DEBUG_TRACE_ERRCODES" != "backtrace"
657-
then
658-
AM_CFLAGS="$AM_CFLAGS -DHAVE_REPRODUCIBLE_BUILD -g0"
659-
fi
656+
AM_CFLAGS="$AM_CFLAGS -DHAVE_REPRODUCIBLE_BUILD"
660657

661658
# opportunistically use -ffile-prefix-map (added in GCC8 and LLVM10)
662659

@@ -739,7 +736,10 @@ AC_ARG_WITH([linux-arch],
739736

740737
if test "x$ENABLED_LINUXKM" = "xyes"
741738
then
742-
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_LINUXKM -DWC_SIPHASH_NO_ASM"
739+
# Currently DWARF 5 is the default debug format, but it results in
740+
# "Unsupported DW_TAG_atomic_type(0x47): type: 0x1eefc" in some
741+
# kernel module builds.
742+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_LINUXKM -DWC_SIPHASH_NO_ASM -gdwarf-4"
743743
ENABLED_NO_LIBRARY=yes
744744
ENABLED_BENCHMARK=no
745745
output_objdir="$(realpath "$output_objdir")/linuxkm"

linuxkm/linuxkm_wc_port.h

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,27 @@
129129
#ifndef WC_LINUXKM_INTR_SIGNALS
130130
#define WC_LINUXKM_INTR_SIGNALS { SIGKILL, SIGABRT, SIGHUP, SIGINT }
131131
#endif
132+
extern int wc_linuxkm_sig_ignore_begin(void);
133+
extern int wc_linuxkm_sig_ignore_end(void);
132134
extern int wc_linuxkm_check_for_intr_signals(void);
133135
#ifndef WC_LINUXKM_MAX_NS_WITHOUT_YIELD
134136
#define WC_LINUXKM_MAX_NS_WITHOUT_YIELD 1000000000
135137
#endif
136138
extern void wc_linuxkm_relax_long_loop(void);
137139

140+
#ifndef WC_SIG_IGNORE_BEGIN
141+
#define WC_SIG_IGNORE_BEGIN() wc_linuxkm_sig_ignore_begin()
142+
#endif
143+
#ifndef WC_SIG_IGNORE_END
144+
#define WC_SIG_IGNORE_END() wc_linuxkm_sig_ignore_end()
145+
#endif
146+
#ifndef WC_CHECK_FOR_INTR_SIGNALS
147+
#define WC_CHECK_FOR_INTR_SIGNALS() wc_linuxkm_check_for_intr_signals()
148+
#endif
149+
#ifndef WC_RELAX_LONG_LOOP
150+
#define WC_RELAX_LONG_LOOP() wc_linuxkm_relax_long_loop()
151+
#endif
152+
138153
enum wc_svr_flags {
139154
WC_SVR_FLAG_NONE = 0,
140155
WC_SVR_FLAG_INHIBIT = 1,
@@ -211,7 +226,7 @@
211226
_Pragma("GCC diagnostic push");
212227

213228
/* we include all the needed kernel headers with these masked out. else
214-
* there are profuse warnings.
229+
* there are profuse warnings, especially on older kernels.
215230
*/
216231
_Pragma("GCC diagnostic ignored \"-Wunused-parameter\"");
217232
_Pragma("GCC diagnostic ignored \"-Wpointer-arith\"");
@@ -225,6 +240,7 @@
225240
_Pragma("GCC diagnostic ignored \"-Wtype-limits\"");
226241
_Pragma("GCC diagnostic ignored \"-Wswitch-enum\"");
227242
_Pragma("GCC diagnostic ignored \"-Wcast-function-type\""); /* needed for kernel 4.14.336 */
243+
_Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\""); /* needed for kernel 4.9.282 */
228244

229245
#include <linux/kconfig.h>
230246

@@ -432,10 +448,8 @@
432448
#endif
433449
#include <linux/random.h>
434450

435-
#ifndef __PIE__
436-
#if defined(WOLFSSL_LINUXKM_USE_GET_RANDOM_KPROBES) || defined(FIPS_OPTEST)
437-
#include <linux/kprobes.h>
438-
#endif
451+
#if !defined(__PIE__) && defined(CONFIG_HAVE_KPROBES)
452+
#include <linux/kprobes.h>
439453
#endif
440454

441455
#ifdef LINUXKM_LKCAPI_REGISTER
@@ -489,13 +503,6 @@
489503
#endif /* !__PIE__ */
490504
#endif /* LINUXKM_LKCAPI_REGISTER */
491505

492-
#ifndef WC_CHECK_FOR_INTR_SIGNALS
493-
#define WC_CHECK_FOR_INTR_SIGNALS() wc_linuxkm_check_for_intr_signals()
494-
#endif
495-
#ifndef WC_RELAX_LONG_LOOP
496-
#define WC_RELAX_LONG_LOOP() wc_linuxkm_relax_long_loop()
497-
#endif
498-
499506
/* benchmarks.c uses floating point math, so needs a working
500507
* SAVE_VECTOR_REGISTERS().
501508
*/
@@ -980,6 +987,8 @@
980987
typeof(wc_lkm_LockMutex) *wc_lkm_LockMutex;
981988
#endif
982989

990+
typeof(wc_linuxkm_sig_ignore_begin) *wc_linuxkm_sig_ignore_begin;
991+
typeof(wc_linuxkm_sig_ignore_end) *wc_linuxkm_sig_ignore_end;
983992
typeof(wc_linuxkm_check_for_intr_signals) *wc_linuxkm_check_for_intr_signals;
984993
typeof(wc_linuxkm_relax_long_loop) *wc_linuxkm_relax_long_loop;
985994

@@ -1220,6 +1229,8 @@
12201229
*/
12211230
#define spin_unlock_irqrestore(lock, flags) raw_spin_unlock_irqrestore(&((lock)->rlock), flags)
12221231

1232+
#define wc_linuxkm_sig_ignore_begin WC_LKM_INDIRECT_SYM(wc_linuxkm_sig_ignore_begin);
1233+
#define wc_linuxkm_sig_ignore_end WC_LKM_INDIRECT_SYM(wc_linuxkm_sig_ignore_end);
12231234
#define wc_linuxkm_check_for_intr_signals WC_LKM_INDIRECT_SYM(wc_linuxkm_check_for_intr_signals)
12241235
#define wc_linuxkm_relax_long_loop WC_LKM_INDIRECT_SYM(wc_linuxkm_relax_long_loop)
12251236

linuxkm/lkcapi_glue.c

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,29 @@ static int linuxkm_lkcapi_sysfs_deinstall(void) {
311311
return 0;
312312
}
313313

314-
static volatile int linuxkm_lkcapi_registering_now = 0;
314+
static wolfSSL_Atomic_Int linuxkm_lkcapi_registering_now = WOLFSSL_ATOMIC_INITIALIZER(0);
315315
static int linuxkm_lkcapi_registered = 0;
316316
static int linuxkm_lkcapi_n_registered = 0;
317317

318318
static int linuxkm_lkcapi_register(void)
319319
{
320320
int ret = -1;
321321
int seen_err = 0;
322+
int current_linuxkm_lkcapi_registering_now = 0;
323+
324+
if (! wolfSSL_Atomic_Int_CompareExchange(
325+
&linuxkm_lkcapi_registering_now,
326+
&current_linuxkm_lkcapi_registering_now,
327+
1))
328+
{
329+
return -EDEADLK;
330+
}
322331

323-
linuxkm_lkcapi_registering_now = 1;
332+
if (WC_SIG_IGNORE_BEGIN() < 0) {
333+
ret = -ECANCELED;
334+
pr_err("ERROR: WC_SIG_IGNORE_BEGIN() failed.\n");
335+
goto out_without_sig_ignored;
336+
}
324337

325338
ret = linuxkm_lkcapi_sysfs_install();
326339
if (ret)
@@ -729,7 +742,11 @@ static int linuxkm_lkcapi_register(void)
729742

730743
out:
731744

732-
linuxkm_lkcapi_registering_now = 0;
745+
(void)WC_SIG_IGNORE_END();
746+
747+
out_without_sig_ignored:
748+
749+
WOLFSSL_ATOMIC_STORE(linuxkm_lkcapi_registering_now, 0);
733750

734751
return ret;
735752
}
@@ -738,9 +755,27 @@ static int linuxkm_lkcapi_unregister(void)
738755
{
739756
int seen_err = 0;
740757
int n_deregistered = 0;
758+
int ret;
759+
int current_linuxkm_lkcapi_registering_now = 0;
760+
761+
if (! wolfSSL_Atomic_Int_CompareExchange(
762+
&linuxkm_lkcapi_registering_now,
763+
&current_linuxkm_lkcapi_registering_now,
764+
1))
765+
{
766+
return -EDEADLK;
767+
}
768+
769+
if (WC_SIG_IGNORE_BEGIN() < 0) {
770+
ret = -ECANCELED;
771+
pr_err("ERROR: WC_SIG_IGNORE_BEGIN() failed.\n");
772+
goto out_without_sig_ignored;
773+
}
741774

742-
if (linuxkm_lkcapi_n_registered == 0)
743-
return -ENOENT;
775+
if (linuxkm_lkcapi_n_registered == 0) {
776+
ret = -ENOENT;
777+
goto out;
778+
}
744779

745780
#define UNREGISTER_ALG(alg, alg_class) \
746781
do { \
@@ -863,7 +898,7 @@ static int linuxkm_lkcapi_unregister(void)
863898
* the system-wide default rng.
864899
*/
865900
if (wc_linuxkm_drbg_loaded) {
866-
int ret = wc_linuxkm_drbg_cleanup();
901+
ret = wc_linuxkm_drbg_cleanup();
867902
if (ret == 0)
868903
++n_deregistered;
869904
else
@@ -975,12 +1010,24 @@ static int linuxkm_lkcapi_unregister(void)
9751010
n_deregistered, n_deregistered == 1 ? "" : "s",
9761011
linuxkm_lkcapi_n_registered, linuxkm_lkcapi_n_registered == 1 ? "s" : "");
9771012

978-
if (linuxkm_lkcapi_n_registered > 0)
979-
return -EBUSY;
1013+
if (linuxkm_lkcapi_n_registered > 0) {
1014+
ret = -EBUSY;
1015+
goto out;
1016+
}
9801017

9811018
linuxkm_lkcapi_registered = 0;
9821019

983-
return seen_err;
1020+
ret = seen_err;
1021+
1022+
out:
1023+
1024+
(void)WC_SIG_IGNORE_END();
1025+
1026+
out_without_sig_ignored:
1027+
1028+
WOLFSSL_ATOMIC_STORE(linuxkm_lkcapi_registering_now, 0);
1029+
1030+
return ret;
9841031
}
9851032

9861033
#endif /* !WC_SKIP_INCLUDED_C_FILES */

0 commit comments

Comments
 (0)