public inbox for linux-crypto@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] crypto: chacha20poly1305 - Annotate struct chachapoly_ctx with __counted_by()
@ 2024-08-05 17:52 Thorsten Blum
  2024-08-05 17:59 ` Eric Biggers
  2024-08-05 20:49 ` Kees Cook
  0 siblings, 2 replies; 4+ messages in thread
From: Thorsten Blum @ 2024-08-05 17:52 UTC (permalink / raw)
  To: herbert, davem, kees, gustavoars
  Cc: linux-crypto, linux-kernel, linux-hardening, Thorsten Blum

Add the __counted_by compiler attribute to the flexible array member
salt to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
CONFIG_FORTIFY_SOURCE.

Use struct_size_t() instead of manually calculating the struct's size.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 crypto/chacha20poly1305.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/crypto/chacha20poly1305.c b/crypto/chacha20poly1305.c
index 9e4651330852..b37f59a8280a 100644
--- a/crypto/chacha20poly1305.c
+++ b/crypto/chacha20poly1305.c
@@ -27,7 +27,7 @@ struct chachapoly_ctx {
 	struct crypto_ahash *poly;
 	/* key bytes we use for the ChaCha20 IV */
 	unsigned int saltlen;
-	u8 salt[];
+	u8 salt[] __counted_by(saltlen);
 };
 
 struct poly_req {
@@ -611,8 +611,8 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
 				       poly->base.cra_priority) / 2;
 	inst->alg.base.cra_blocksize = 1;
 	inst->alg.base.cra_alignmask = chacha->base.cra_alignmask;
-	inst->alg.base.cra_ctxsize = sizeof(struct chachapoly_ctx) +
-				     ctx->saltlen;
+	inst->alg.base.cra_ctxsize = struct_size_t(struct chachapoly_ctx, salt,
+						   ctx->saltlen);
 	inst->alg.ivsize = ivsize;
 	inst->alg.chunksize = chacha->chunksize;
 	inst->alg.maxauthsize = POLY1305_DIGEST_SIZE;
-- 
2.45.2


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

end of thread, other threads:[~2024-08-05 20:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-05 17:52 [PATCH] crypto: chacha20poly1305 - Annotate struct chachapoly_ctx with __counted_by() Thorsten Blum
2024-08-05 17:59 ` Eric Biggers
2024-08-05 20:32   ` Thorsten Blum
2024-08-05 20:49 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox