Skip to content

Commit d4fc8c3

Browse files
committed
linuxkm/: null out pointers with PTR_ERR()-encoded values before jumping to cleanup;
linuxkm/lkcapi_rsa_glue.c: in km_rsa_init(), implement error-path cleanup; linuxkm/module_hooks.c: nix CONFIG_MODULE_SIG requirement in WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE builds; wolfssl/wolfcrypt/settings.h: in WOLFSSL_LINUXKM setup, define WOLFSSL_ASN_INT_LEAD_0_ANY if LINUXKM_LKCAPI_REGISTER (required for kernel 5.10 crypto manager); wolfcrypt/src/memory.c: add WC_NO_ERR_TRACE() to mock error returns in SAVE_VECTOR_REGISTERS2_fuzzer().
1 parent 25cd009 commit d4fc8c3

File tree

8 files changed

+72
-15
lines changed

8 files changed

+72
-15
lines changed

.wolfssl_known_macro_extras

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,6 @@ WOLFSSL_ARM_ARCH_NEON_64BIT
592592
WOLFSSL_ASCON_UNROLL
593593
WOLFSSL_ASNC_CRYPT
594594
WOLFSSL_ASN_EXTRA
595-
WOLFSSL_ASN_INT_LEAD_0_ANY
596595
WOLFSSL_ASN_TEMPLATE_NEED_SET_INT32
597596
WOLFSSL_ASN_TEMPLATE_TYPE_CHECK
598597
WOLFSSL_ATECC508

linuxkm/lkcapi_aes_glue.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,7 @@ static int AesGcmCrypt_1(struct aead_request *req, int decrypt_p, int rfc4106_p)
12041204
pr_err("%s: scatterwalk_map failed: %ld\n",
12051205
crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)),
12061206
PTR_ERR(assoc));
1207+
in_map = NULL;
12071208
goto out;
12081209
}
12091210
assoc = in_map;
@@ -1220,6 +1221,7 @@ static int AesGcmCrypt_1(struct aead_request *req, int decrypt_p, int rfc4106_p)
12201221
pr_err("%s: scatterwalk_map failed: %ld\n",
12211222
crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)),
12221223
PTR_ERR(assoc));
1224+
out_map = NULL;
12231225
goto out;
12241226
}
12251227
out_text = out_map + req->assoclen;
@@ -2329,6 +2331,7 @@ static int linuxkm_test_aescbc(void)
23292331
if (IS_ERR(tfm)) {
23302332
pr_err("error: allocating AES skcipher algorithm %s failed: %ld\n",
23312333
WOLFKM_AESCBC_DRIVER, PTR_ERR(tfm));
2334+
tfm = NULL;
23322335
goto test_cbc_end;
23332336
}
23342337

@@ -2355,6 +2358,7 @@ static int linuxkm_test_aescbc(void)
23552358
if (IS_ERR(req)) {
23562359
pr_err("error: allocating AES skcipher request %s failed\n",
23572360
WOLFKM_AESCBC_DRIVER);
2361+
req = NULL;
23582362
goto test_cbc_end;
23592363
}
23602364

@@ -2538,6 +2542,7 @@ static int linuxkm_test_aescfb(void)
25382542
if (IS_ERR(tfm)) {
25392543
pr_err("error: allocating AES skcipher algorithm %s failed: %ld\n",
25402544
WOLFKM_AESCFB_DRIVER, PTR_ERR(tfm));
2545+
tfm = NULL;
25412546
goto test_cfb_end;
25422547
}
25432548

@@ -2555,6 +2560,7 @@ static int linuxkm_test_aescfb(void)
25552560
if (IS_ERR(req)) {
25562561
pr_err("error: allocating AES skcipher request %s failed\n",
25572562
WOLFKM_AESCFB_DRIVER);
2563+
req = NULL;
25582564
goto test_cfb_end;
25592565
}
25602566

@@ -2763,6 +2769,7 @@ static int linuxkm_test_aesgcm(void)
27632769
assoc2 = malloc(sizeof(assoc));
27642770
if (IS_ERR(assoc2)) {
27652771
pr_err("error: malloc failed\n");
2772+
assoc2 = NULL;
27662773
goto test_gcm_end;
27672774
}
27682775
memset(assoc2, 0, sizeof(assoc));
@@ -2771,6 +2778,7 @@ static int linuxkm_test_aesgcm(void)
27712778
iv = malloc(WC_AES_BLOCK_SIZE);
27722779
if (IS_ERR(iv)) {
27732780
pr_err("error: malloc failed\n");
2781+
iv = NULL;
27742782
goto test_gcm_end;
27752783
}
27762784
memset(iv, 0, WC_AES_BLOCK_SIZE);
@@ -2779,12 +2787,14 @@ static int linuxkm_test_aesgcm(void)
27792787
enc2 = malloc(decryptLen);
27802788
if (IS_ERR(enc2)) {
27812789
pr_err("error: malloc failed\n");
2790+
enc2 = NULL;
27822791
goto test_gcm_end;
27832792
}
27842793

27852794
dec2 = malloc(decryptLen);
27862795
if (IS_ERR(dec2)) {
27872796
pr_err("error: malloc failed\n");
2797+
dec2 = NULL;
27882798
goto test_gcm_end;
27892799
}
27902800

@@ -2796,6 +2806,7 @@ static int linuxkm_test_aesgcm(void)
27962806
if (IS_ERR(tfm)) {
27972807
pr_err("error: allocating AES skcipher algorithm %s failed: %ld\n",
27982808
WOLFKM_AESGCM_DRIVER, PTR_ERR(tfm));
2809+
tfm = NULL;
27992810
goto test_gcm_end;
28002811
}
28012812

@@ -2819,15 +2830,25 @@ static int linuxkm_test_aesgcm(void)
28192830
if (IS_ERR(req)) {
28202831
pr_err("error: allocating AES aead request %s failed: %ld\n",
28212832
WOLFKM_AESCBC_DRIVER, PTR_ERR(req));
2833+
req = NULL;
28222834
goto test_gcm_end;
28232835
}
28242836

28252837
src = malloc(sizeof(struct scatterlist) * 2);
2838+
2839+
if (IS_ERR(src)) {
2840+
pr_err("error: malloc src failed: %ld\n",
2841+
PTR_ERR(src));
2842+
src = NULL;
2843+
goto test_gcm_end;
2844+
}
2845+
28262846
dst = malloc(sizeof(struct scatterlist) * 2);
28272847

2828-
if (IS_ERR(src) || IS_ERR(dst)) {
2829-
pr_err("error: malloc src or dst failed: %ld, %ld\n",
2830-
PTR_ERR(src), PTR_ERR(dst));
2848+
if (IS_ERR(dst)) {
2849+
pr_err("error: malloc dst failed: %ld\n",
2850+
PTR_ERR(dst));
2851+
dst = NULL;
28312852
goto test_gcm_end;
28322853
}
28332854

@@ -3367,6 +3388,7 @@ static int aes_xts_128_test(void)
33673388
ret = PTR_ERR(tfm);
33683389
pr_err("error: allocating AES skcipher algorithm %s failed: %d\n",
33693390
WOLFKM_AESXTS_DRIVER, ret);
3391+
tfm = NULL;
33703392
goto test_xts_end;
33713393
}
33723394

@@ -3404,6 +3426,7 @@ static int aes_xts_128_test(void)
34043426
ret = PTR_ERR(req);
34053427
pr_err("error: allocating AES skcipher request %s failed: %d\n",
34063428
WOLFKM_AESXTS_DRIVER, ret);
3429+
req = NULL;
34073430
goto test_xts_end;
34083431
}
34093432

@@ -3847,6 +3870,7 @@ static int aes_xts_256_test(void)
38473870
ret = PTR_ERR(tfm);
38483871
pr_err("error: allocating AES skcipher algorithm %s failed: %d\n",
38493872
WOLFKM_AESXTS_DRIVER, ret);
3873+
tfm = NULL;
38503874
goto test_xts_end;
38513875
}
38523876

@@ -3883,6 +3907,7 @@ static int aes_xts_256_test(void)
38833907
ret = PTR_ERR(req);
38843908
pr_err("error: allocating AES skcipher request %s failed: %d\n",
38853909
WOLFKM_AESXTS_DRIVER, ret);
3910+
req = NULL;
38863911
goto test_xts_end;
38873912
}
38883913

linuxkm/lkcapi_ecdh_glue.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,13 +804,15 @@ static int linuxkm_test_ecdh_nist_driver(const char * driver,
804804
if (IS_ERR(tfm)) {
805805
pr_err("error: allocating kpp algorithm %s failed: %ld\n",
806806
driver, PTR_ERR(tfm));
807+
tfm = NULL;
807808
goto test_ecdh_nist_end;
808809
}
809810

810811
req = kpp_request_alloc(tfm, GFP_KERNEL);
811812
if (IS_ERR(req)) {
812813
pr_err("error: allocating kpp request %s failed\n",
813814
driver);
815+
req = NULL;
814816
goto test_ecdh_nist_end;
815817
}
816818

linuxkm/lkcapi_ecdsa_glue.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,15 @@ static int linuxkm_test_ecdsa_nist_driver(const char * driver,
679679
if (IS_ERR(tfm)) {
680680
pr_err("error: allocating akcipher algorithm %s failed: %ld\n",
681681
driver, PTR_ERR(tfm));
682+
tfm = NULL;
682683
goto test_ecdsa_nist_end;
683684
}
684685

685686
req = akcipher_request_alloc(tfm, GFP_KERNEL);
686687
if (IS_ERR(req)) {
687688
pr_err("error: allocating akcipher request %s failed\n",
688689
driver);
690+
req = NULL;
689691
goto test_ecdsa_nist_end;
690692
}
691693

linuxkm/lkcapi_rsa_glue.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,32 @@ static int km_rsa_init(struct crypto_akcipher *tfm, int hash_oid)
180180

181181
ctx->key = (RsaKey *)malloc(sizeof(RsaKey));
182182
if (!ctx->key) {
183-
return -ENOMEM;
183+
ret = -ENOMEM;
184+
goto out;
184185
}
185186

186187
ret = wc_InitRng(&ctx->rng);
187188
if (ret) {
188189
pr_err("%s: init rng returned: %d\n", WOLFKM_RSA_DRIVER, ret);
189-
return -ENOMEM;
190+
if (ret == WC_NO_ERR_TRACE(MEMORY_E))
191+
ret = -ENOMEM;
192+
else
193+
ret = -EINVAL;
194+
goto out;
190195
}
191196

192197
ret = wc_InitRsaKey(ctx->key, NULL);
193198
if (ret) {
194199
pr_err("%s: init rsa key returned: %d\n", WOLFKM_RSA_DRIVER, ret);
195-
return -ENOMEM;
200+
ret = -EINVAL;
201+
goto out;
196202
}
197203

198204
#ifdef WC_RSA_BLINDING
199205
ret = wc_RsaSetRNG(ctx->key, &ctx->rng);
200206
if (ret) {
201-
return -ENOMEM;
207+
ret = -EINVAL;
208+
goto out;
202209
}
203210
#endif /* WC_RSA_BLINDING */
204211

@@ -221,13 +228,25 @@ static int km_rsa_init(struct crypto_akcipher *tfm, int hash_oid)
221228
default:
222229
pr_err("%s: init: unhandled hash_oid: %d\n", WOLFKM_RSA_DRIVER,
223230
hash_oid);
224-
return -ENOMEM;
231+
ret = -EINVAL;
232+
goto out;
225233
}
226234

227235
#ifdef WOLFKM_DEBUG_RSA
228236
pr_info("info: exiting km_rsa_init: hash_oid %d\n", ctx->hash_oid);
229237
#endif /* WOLFKM_DEBUG_RSA */
230-
return 0;
238+
239+
out:
240+
241+
if (ret != 0) {
242+
if (ctx->key) {
243+
free(ctx->key);
244+
ctx->key = NULL;
245+
}
246+
wc_FreeRng(&ctx->rng);
247+
}
248+
249+
return ret;
231250
}
232251

233252
#if defined(LINUXKM_DIRECT_RSA)
@@ -1260,13 +1279,15 @@ static int linuxkm_test_rsa_driver(const char * driver, int nbits)
12601279
if (IS_ERR(tfm)) {
12611280
pr_err("error: allocating akcipher algorithm %s failed: %ld\n",
12621281
driver, PTR_ERR(tfm));
1282+
tfm = NULL;
12631283
goto test_rsa_end;
12641284
}
12651285

12661286
req = akcipher_request_alloc(tfm, GFP_KERNEL);
12671287
if (IS_ERR(req)) {
12681288
pr_err("error: allocating akcipher request %s failed\n",
12691289
driver);
1290+
req = NULL;
12701291
goto test_rsa_end;
12711292
}
12721293

@@ -1609,13 +1630,15 @@ static int linuxkm_test_pkcs1_driver(const char * driver, int nbits,
16091630
if (IS_ERR(tfm)) {
16101631
pr_err("error: allocating akcipher algorithm %s failed: %ld\n",
16111632
driver, PTR_ERR(tfm));
1633+
tfm = NULL;
16121634
goto test_pkcs1_end;
16131635
}
16141636

16151637
req = akcipher_request_alloc(tfm, GFP_KERNEL);
16161638
if (IS_ERR(req)) {
16171639
pr_err("error: allocating akcipher request %s failed\n",
16181640
driver);
1641+
req = NULL;
16191642
goto test_pkcs1_end;
16201643
}
16211644

linuxkm/module_hooks.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ static void lkmFipsCb(int ok, int err, const char* hash)
110110
#endif
111111

112112
#ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
113-
#ifndef CONFIG_MODULE_SIG
114-
#error WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE requires a CONFIG_MODULE_SIG kernel.
115-
#endif
116113
static int updateFipsHash(void);
117114
#endif
118115

@@ -137,10 +134,12 @@ static int wolfssl_init(void)
137134
int ret;
138135

139136
#ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
137+
#ifdef CONFIG_MODULE_SIG
140138
if (THIS_MODULE->sig_ok == false) {
141139
pr_err("wolfSSL module load aborted -- bad or missing module signature with FIPS dynamic hash.\n");
142140
return -ECANCELED;
143141
}
142+
#endif
144143
ret = updateFipsHash();
145144
if (ret < 0) {
146145
pr_err("wolfSSL module load aborted -- updateFipsHash: %s\n",wc_GetErrorString(ret));

wolfcrypt/src/memory.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ WOLFSSL_LOCAL int SAVE_VECTOR_REGISTERS2_fuzzer(void) {
17541754
}
17551755
(void)lrand48_r(&wc_svr_fuzzing_state, &result);
17561756
if (result & 1)
1757-
return IO_FAILED_E;
1757+
return WC_NO_ERR_TRACE(IO_FAILED_E);
17581758
else
17591759
return 0;
17601760
}
@@ -1794,7 +1794,7 @@ WOLFSSL_LOCAL int SAVE_VECTOR_REGISTERS2_fuzzer(void) {
17941794

17951795
balance_bit = !balance_bit;
17961796

1797-
return ((prn & 1) ^ balance_bit) ? IO_FAILED_E : 0;
1797+
return ((prn & 1) ^ balance_bit) ? WC_NO_ERR_TRACE(IO_FAILED_E) : 0;
17981798
}
17991799

18001800
#endif /* !HAVE_THREAD_LS */

wolfssl/wolfcrypt/settings.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,13 @@ extern void uITRON4_free(void *p) ;
36743674
#undef WOLFSSL_MIN_AUTH_TAG_SZ
36753675
#define WOLFSSL_MIN_AUTH_TAG_SZ 4
36763676

3677+
#if defined(LINUXKM_LKCAPI_REGISTER) && !defined(WOLFSSL_ASN_INT_LEAD_0_ANY)
3678+
/* kernel 5.10 crypto manager tests key(s) that fail unless leading
3679+
* bytes are tolerated in GetASN_Integer().
3680+
*/
3681+
#define WOLFSSL_ASN_INT_LEAD_0_ANY
3682+
#endif
3683+
36773684
#ifdef CONFIG_KASAN
36783685
#ifndef WC_SANITIZE_DISABLE
36793686
#define WC_SANITIZE_DISABLE() kasan_disable_current()

0 commit comments

Comments
 (0)