Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] crypto: zstd - Annotate struct zstd_ctx with __counted_by
@ 2025-11-08 12:07 Thorsten Blum
  2025-11-13 11:32 ` David Sterba
  2025-11-14 10:24 ` Herbert Xu
  0 siblings, 2 replies; 3+ messages in thread
From: Thorsten Blum @ 2025-11-08 12:07 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller, Nick Terrell, David Sterba,
	Kees Cook, Gustavo A. R. Silva
  Cc: Thorsten Blum, linux-crypto, linux-kernel, linux-hardening

Add the __counted_by() compiler attribute to the flexible array member
'wksp' to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
CONFIG_FORTIFY_SOURCE.

Use struct_size(), which provides additional compile-time checks for
structures with flexible array members (e.g., __must_be_array()), for
the allocation size for a new 'zstd_ctx' while we're at it.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 crypto/zstd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/crypto/zstd.c b/crypto/zstd.c
index ac318d333b68..ace7a82ea45a 100644
--- a/crypto/zstd.c
+++ b/crypto/zstd.c
@@ -10,6 +10,7 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
+#include <linux/overflow.h>
 #include <linux/vmalloc.h>
 #include <linux/zstd.h>
 #include <crypto/internal/acompress.h>
@@ -25,7 +26,7 @@ struct zstd_ctx {
 	zstd_dctx *dctx;
 	size_t wksp_size;
 	zstd_parameters params;
-	u8 wksp[] __aligned(8);
+	u8 wksp[] __aligned(8) __counted_by(wksp_size);
 };
 
 static DEFINE_MUTEX(zstd_stream_lock);
@@ -44,7 +45,7 @@ static void *zstd_alloc_stream(void)
 	if (!wksp_size)
 		return ERR_PTR(-EINVAL);
 
-	ctx = kvmalloc(sizeof(*ctx) + wksp_size, GFP_KERNEL);
+	ctx = kvmalloc(struct_size(ctx, wksp, wksp_size), GFP_KERNEL);
 	if (!ctx)
 		return ERR_PTR(-ENOMEM);
 
-- 
2.51.1


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

* Re: [PATCH] crypto: zstd - Annotate struct zstd_ctx with __counted_by
  2025-11-08 12:07 [PATCH] crypto: zstd - Annotate struct zstd_ctx with __counted_by Thorsten Blum
@ 2025-11-13 11:32 ` David Sterba
  2025-11-14 10:24 ` Herbert Xu
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2025-11-13 11:32 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Herbert Xu, David S. Miller, Nick Terrell, David Sterba,
	Kees Cook, Gustavo A. R. Silva, linux-crypto, linux-kernel,
	linux-hardening

On Sat, Nov 08, 2025 at 01:07:40PM +0100, Thorsten Blum wrote:
> Add the __counted_by() compiler attribute to the flexible array member
> 'wksp' to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
> CONFIG_FORTIFY_SOURCE.
> 
> Use struct_size(), which provides additional compile-time checks for
> structures with flexible array members (e.g., __must_be_array()), for
> the allocation size for a new 'zstd_ctx' while we're at it.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>

Reviewed-by: David Sterba <dsterba@suse.com>

As this is crypto/zstd.c there's no need to sync with upstream zstd code
so it can go via the crypto tree.

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

* Re: [PATCH] crypto: zstd - Annotate struct zstd_ctx with __counted_by
  2025-11-08 12:07 [PATCH] crypto: zstd - Annotate struct zstd_ctx with __counted_by Thorsten Blum
  2025-11-13 11:32 ` David Sterba
@ 2025-11-14 10:24 ` Herbert Xu
  1 sibling, 0 replies; 3+ messages in thread
From: Herbert Xu @ 2025-11-14 10:24 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: David S. Miller, Nick Terrell, David Sterba, Kees Cook,
	Gustavo A. R. Silva, linux-crypto, linux-kernel, linux-hardening

On Sat, Nov 08, 2025 at 01:07:40PM +0100, Thorsten Blum wrote:
> Add the __counted_by() compiler attribute to the flexible array member
> 'wksp' to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
> CONFIG_FORTIFY_SOURCE.
> 
> Use struct_size(), which provides additional compile-time checks for
> structures with flexible array members (e.g., __must_be_array()), for
> the allocation size for a new 'zstd_ctx' while we're at it.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  crypto/zstd.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

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

end of thread, other threads:[~2025-11-14 10:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-08 12:07 [PATCH] crypto: zstd - Annotate struct zstd_ctx with __counted_by Thorsten Blum
2025-11-13 11:32 ` David Sterba
2025-11-14 10:24 ` Herbert Xu

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