linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] KEYS: asymmetric: Copy sig and digest in public_key_verify_signature()
@ 2022-12-09 15:06 Roberto Sassu
  2022-12-09 19:04 ` Eric Biggers
  0 siblings, 1 reply; 11+ messages in thread
From: Roberto Sassu @ 2022-12-09 15:06 UTC (permalink / raw)
  To: dhowells, herbert, davem, zohar, dmitry.kasatkin, paul, jmorris,
	serge
  Cc: linux-integrity, linux-security-module, keyrings, linux-crypto,
	linux-kernel, Roberto Sassu, stable, Eric Biggers

From: Roberto Sassu <roberto.sassu@huawei.com>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear
mapping") checks that both the signature and the digest reside in the
linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced
stack support"), made it possible to move the stack in the vmalloc area,
which is not contiguous, and thus not suitable for sg_set_buf() which needs
adjacent pages.

Always make a copy of the signature and digest in the same buffer used to
store the key and its parameters, and pass them to sg_set_buf(). Prefer it
to conditionally doing the copy if necessary, to keep the code simple. The
buffer allocated with kmalloc() is in the linear mapping area.

Cc: stable@vger.kernel.org # 4.9.x
Fixes: ba14a194a434 ("fork: Add generic vmalloced stack support")
Link: https://lore.kernel.org/linux-integrity/Y4pIpxbjBdajymBJ@sol.localdomain/
Suggested-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 crypto/asymmetric_keys/public_key.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c
index 2f8352e88860..ccc091119972 100644
--- a/crypto/asymmetric_keys/public_key.c
+++ b/crypto/asymmetric_keys/public_key.c
@@ -363,6 +363,7 @@ int public_key_verify_signature(const struct public_key *pkey,
 	struct scatterlist src_sg[2];
 	char alg_name[CRYPTO_MAX_ALG_NAME];
 	char *key, *ptr;
+	u32 key_max_len;
 	int ret;
 
 	pr_devel("==>%s()\n", __func__);
@@ -400,8 +401,12 @@ int public_key_verify_signature(const struct public_key *pkey,
 	if (!req)
 		goto error_free_tfm;
 
-	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
-		      GFP_KERNEL);
+	key_max_len = max_t(u32,
+			    pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
+			    sig->s_size + sig->digest_size);
+
+	/* key is used to store the sig and digest too. */
+	key = kmalloc(key_max_len, GFP_KERNEL);
 	if (!key)
 		goto error_free_req;
 
@@ -424,9 +429,13 @@ int public_key_verify_signature(const struct public_key *pkey,
 			goto error_free_key;
 	}
 
+	memcpy(key, sig->s, sig->s_size);
+	memcpy(key + sig->s_size, sig->digest, sig->digest_size);
+
 	sg_init_table(src_sg, 2);
-	sg_set_buf(&src_sg[0], sig->s, sig->s_size);
-	sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
+	/* Cannot use one scatterlist. The first needs to be s->s_size long. */
+	sg_set_buf(&src_sg[0], key, sig->s_size);
+	sg_set_buf(&src_sg[1], key + sig->s_size, sig->digest_size);
 	akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
 				   sig->digest_size);
 	crypto_init_wait(&cwait);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-12-23  6:25 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-09 15:06 [PATCH v2] KEYS: asymmetric: Copy sig and digest in public_key_verify_signature() Roberto Sassu
2022-12-09 19:04 ` Eric Biggers
2022-12-12  9:07   ` Roberto Sassu
2022-12-12  9:15     ` Herbert Xu
2022-12-19  8:49       ` Roberto Sassu
2022-12-20  7:24         ` [PATCH] lib/mpi: Fix buffer overrun when SG is too long Herbert Xu
2022-12-20 10:36           ` Roberto Sassu
2022-12-20 20:30             ` Eric Biggers
2022-12-21  6:53               ` [v2 PATCH] " Herbert Xu
2022-12-21 20:53                 ` Eric Biggers
2022-12-23  6:25                   ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).