]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
crypto: zstd - Annotate struct zstd_ctx with __counted_by
authorThorsten Blum <thorsten.blum@linux.dev>
Sat, 8 Nov 2025 12:07:40 +0000 (13:07 +0100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 14 Nov 2025 10:15:49 +0000 (18:15 +0800)
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>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/zstd.c

index ac318d333b684787f0eac3f190dabf5606ef478c..ace7a82ea45a9a4f4a528fabf59574ea7192b496 100644 (file)
@@ -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);