Skip to content

Commit 6cf3260

Browse files
tobluxherbertx
authored andcommitted
crypto: zstd - Annotate struct zstd_ctx with __counted_by
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>
1 parent af3852c commit 6cf3260

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

crypto/zstd.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/mm.h>
1111
#include <linux/module.h>
1212
#include <linux/net.h>
13+
#include <linux/overflow.h>
1314
#include <linux/vmalloc.h>
1415
#include <linux/zstd.h>
1516
#include <crypto/internal/acompress.h>
@@ -25,7 +26,7 @@ struct zstd_ctx {
2526
zstd_dctx *dctx;
2627
size_t wksp_size;
2728
zstd_parameters params;
28-
u8 wksp[] __aligned(8);
29+
u8 wksp[] __aligned(8) __counted_by(wksp_size);
2930
};
3031

3132
static DEFINE_MUTEX(zstd_stream_lock);
@@ -44,7 +45,7 @@ static void *zstd_alloc_stream(void)
4445
if (!wksp_size)
4546
return ERR_PTR(-EINVAL);
4647

47-
ctx = kvmalloc(sizeof(*ctx) + wksp_size, GFP_KERNEL);
48+
ctx = kvmalloc(struct_size(ctx, wksp, wksp_size), GFP_KERNEL);
4849
if (!ctx)
4950
return ERR_PTR(-ENOMEM);
5051

0 commit comments

Comments
 (0)