Skip to contentSkip to conversation

crypto: fix null-ptr-dereference in rng_setentropy

PATCH New Dave Sun 2 participants 2 messages Sep 30, 2025 – Oct 2, 2025 Crypto API

lore
Share & tools
lore

Dave Sun

author
#0 lore raw

PATCH 1/1 crypto: fix null-ptr-dereference in rng_setentropy

When CONFIG_CRYPTO_USER_API_RNG_CAVP is enable, algif_type_rng->setentropy = rng_setentropy. If we setsockopt on a af_alg socket binding a rng algorithm with ALG_SET_DRBG_ENTROPY opt, kernel will run to ->set_ent in rng_setentropy.

Since struct rng_alg like jent_alg dose not set set_ent which default value is 0, null-ptr-dereference will happen.

Check ->set_ent before call it.

---
 crypto/algif_rng.c | 3 +++
 1 file changed, 3 insertions(+)
crypto/algif_rng.c +3 -0
index 10c41adac3b1..26a75c54192b 100644
@@ -303,6 +303,9 @@ static int __maybe_unused rng_setentropy(void *private, sockptr_t entropy,
 			return PTR_ERR(kentropy);
 	}
 
+	if (!crypto_rng_alg(pctx->drng)->set_ent)
+		return -EOPNOTSUPP;
+
 	crypto_rng_alg(pctx->drng)->set_ent(pctx->drng, kentropy, len);
 	/*
 	 * Since rng doesn't perform any memory management for the entropy
Signature · 1 line
2.34.1
maintainer
#1 lore raw

PATCH 1/1 crypto: rng - Ensure set_ent is always present

Quoted 9 lines · Dave Sun

When CONFIG_CRYPTO_USER_API_RNG_CAVP is enable, algif_type_rng->setentropy = rng_setentropy. If we setsockopt on a af_alg socket binding a rng algorithm with ALG_SET_DRBG_ENTROPY opt, kernel will run to ->set_ent in rng_setentropy.

Since struct rng_alg like jent_alg dose not set set_ent which default value is 0, null-ptr-dereference will happen.

Quoted 6 lines

Check ->set_ent before call it.

---
 crypto/algif_rng.c | 3 +++
 1 file changed, 3 insertions(+)

Thanks for the report. I'd prefer to make set_ent always present:

---8<---
Ensure that set_ent is always set since only drbg provides it.

Fixes 77ebdabe8de7 ("crypto: af_alg - add extra parameters for DRBG interface")
crypto/rng.c +8 -0
index b8ae6ebc091d..ee1768c5a400 100644
@@ -168,6 +168,11 @@ int crypto_del_default_rng(void)
 EXPORT_SYMBOL_GPL(crypto_del_default_rng);
 #endif
 
+static void rng_default_set_ent(struct crypto_rng *tfm, const u8 *data,
+				unsigned int len)
+{
+}
+
 int crypto_register_rng(struct rng_alg *alg)
 {
 	struct crypto_alg *base = &alg->base;
@@ -179,6 +184,9 @@ int crypto_register_rng(struct rng_alg *alg)
 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
 	base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
 
+	if (!alg->set_ent)
+		alg->set_ent = rng_default_set_ent;
+
 	return crypto_register_alg(base);
 }
 EXPORT_SYMBOL_GPL(crypto_register_rng);
Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt