Skip to content

Commit 283792c

Browse files
committed
linuxkm/lkcapi_sha_glue.c: in wc_linuxkm_drbg_startup(), deinstall the callbacks and stdrng first before checking refcnt.
1 parent 3b3ddd1 commit 283792c

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

linuxkm/lkcapi_sha_glue.c

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,25 +2150,18 @@ static int wc_linuxkm_drbg_startup(void)
21502150
}
21512151

21522152
static int wc_linuxkm_drbg_cleanup(void) {
2153-
int cur_refcnt = WC_LKM_REFCOUNT_TO_INT(wc_linuxkm_drbg.base.cra_refcnt);
2153+
int cur_refcnt;
21542154

21552155
if (! wc_linuxkm_drbg_loaded) {
21562156
pr_err("ERROR: wc_linuxkm_drbg_cleanup called with ! wc_linuxkm_drbg_loaded");
21572157
return -EINVAL;
21582158
}
21592159

2160-
if (cur_refcnt - wc_linuxkm_drbg_default_instance_registered != 1) {
2161-
pr_err("ERROR: wc_linuxkm_drbg_cleanup called with refcnt = %d, with wc_linuxkm_drbg %sset as default rng",
2162-
cur_refcnt, wc_linuxkm_drbg_default_instance_registered ? "" : "not ");
2163-
return -EBUSY;
2164-
}
2165-
2166-
/* The below is racey, but the kernel doesn't provide any other way. It's
2167-
* written to be retryable.
2168-
*/
2169-
21702160
#ifdef LINUXKM_LKCAPI_REGISTER_HASH_DRBG_DEFAULT
21712161
if (wc_linuxkm_drbg_default_instance_registered) {
2162+
/* These deinstallations are racey, but the kernel doesn't provide any other
2163+
* way. It's written to be retryable.
2164+
*/
21722165
int ret;
21732166

21742167
#ifdef LINUXKM_DRBG_GET_RANDOM_BYTES
@@ -2193,16 +2186,16 @@ static int wc_linuxkm_drbg_cleanup(void) {
21932186
#elif defined(WOLFSSL_LINUXKM_USE_GET_RANDOM_KPROBES)
21942187

21952188
if (wc_get_random_bytes_kprobe_installed) {
2196-
wc_get_random_bytes_kprobe_installed = 0;
2197-
barrier();
21982189
unregister_kprobe(&wc_get_random_bytes_kprobe);
2190+
barrier();
2191+
wc_get_random_bytes_kprobe_installed = 0;
21992192
pr_info("libwolfssl: wc_get_random_bytes_kprobe uninstalled\n");
22002193
}
22012194
#ifdef WOLFSSL_LINUXKM_USE_GET_RANDOM_USER_KRETPROBE
22022195
if (wc_get_random_bytes_user_kretprobe_installed) {
2203-
wc_get_random_bytes_user_kretprobe_installed = 0;
2204-
barrier();
22052196
unregister_kretprobe(&wc_get_random_bytes_user_kretprobe);
2197+
barrier();
2198+
wc_get_random_bytes_user_kretprobe_installed = 0;
22062199
pr_info("libwolfssl: wc_get_random_bytes_user_kretprobe uninstalled\n");
22072200
}
22082201
#endif /* WOLFSSL_LINUXKM_USE_GET_RANDOM_USER_KRETPROBE */
@@ -2218,25 +2211,25 @@ static int wc_linuxkm_drbg_cleanup(void) {
22182211
pr_err("ERROR: crypto_del_default_rng failed: %d", ret);
22192212
return ret;
22202213
}
2221-
cur_refcnt = WC_LKM_REFCOUNT_TO_INT(wc_linuxkm_drbg.base.cra_refcnt);
2222-
if (cur_refcnt != 1) {
2223-
pr_warn("WARNING: wc_linuxkm_drbg refcnt = %d after crypto_del_default_rng()", cur_refcnt);
2224-
return -EINVAL;
2225-
}
2214+
2215+
wc_linuxkm_drbg_default_instance_registered = 0;
22262216
}
22272217
#endif /* LINUXKM_LKCAPI_REGISTER_HASH_DRBG_DEFAULT */
22282218

2219+
cur_refcnt = WC_LKM_REFCOUNT_TO_INT(wc_linuxkm_drbg.base.cra_refcnt);
2220+
2221+
if (cur_refcnt != 1) {
2222+
pr_err("ERROR: wc_linuxkm_drbg_cleanup called with refcnt = %d", cur_refcnt);
2223+
return -EBUSY;
2224+
}
2225+
22292226
crypto_unregister_rng(&wc_linuxkm_drbg);
22302227

22312228
if (! (wc_linuxkm_drbg.base.cra_flags & CRYPTO_ALG_DEAD)) {
22322229
pr_warn("WARNING: wc_linuxkm_drbg_cleanup: after crypto_unregister_rng, wc_linuxkm_drbg isn't dead.");
22332230
return -EBUSY;
22342231
}
22352232

2236-
#ifdef LINUXKM_LKCAPI_REGISTER_HASH_DRBG_DEFAULT
2237-
wc_linuxkm_drbg_default_instance_registered = 0;
2238-
#endif /* LINUXKM_LKCAPI_REGISTER_HASH_DRBG_DEFAULT */
2239-
22402233
wc_linuxkm_drbg_loaded = 0;
22412234

22422235
return 0;

0 commit comments

Comments
 (0)