All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ihor Solodrai" <ihor.solodrai@linux.dev>
Cc: linux-input@vger.kernel.org, bpf@vger.kernel.org,
	dmitry.torokhov@gmail.com
Subject: Re: [PATCH bpf-next v1 1/8] resolve_btfids: Implement generic ensure_mem() to grow arrays
Date: Wed, 22 Jul 2026 23:48:41 +0000	[thread overview]
Message-ID: <20260722234842.68B271F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260722233518.778854-2-ihor.solodrai@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Strict aliasing violation via `void **` casting leading to potential Use-After-Free.
--

--- Patch [1]: [PATCH bpf-next v1 1/8] resolve_btfids: Implement generic ensure_mem() to grow arrays ---
commit b5970d9bf6b1262c4ad812706df67f4a14ee5411
Author: Ihor Solodrai <ihor.solodrai@linux.dev>

resolve_btfids: Implement generic ensure_mem() to grow arrays

This commit factors out array growth logic from push_*() helpers into a
generic ensure_mem() macro and function. It uses a void ** cast to update
the array pointer upon reallocation.

> diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
> index f8a91fa7584f4..183466ddc5f97 100644
> --- a/tools/bpf/resolve_btfids/main.c
> +++ b/tools/bpf/resolve_btfids/main.c
> @@ -201,6 +201,35 @@ static int eprintf(int level, int var, const char *fmt, ...)
>  #define pr_info(fmt, ...) \
>  	eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
>  
> +/*
> + * Grow *data so it can hold at least cnt elements of elem_sz bytes each.
> + * *cap is the capacity in elements and is updated on growth.
> + */
> +static int __ensure_mem(void **data, u32 *cap, u32 cnt, size_t elem_sz)
> +{
> +	u32 new_cap, old_cap = *cap;
> +	void *arr;
> +
> +	if (cnt <= old_cap)
> +		return 0;
> +
> +	new_cap = max(old_cap + 256, old_cap * 2);
> +	if (new_cap < cnt)
> +		new_cap = cnt;
> +
> +	arr = realloc(*data, elem_sz * new_cap);
> +	if (!arr)
> +		return -ENOMEM;
> +
> +	*data = arr;

[Severity: Medium]
Will this assignment violate strict aliasing rules? Because void * and u32 *
are incompatible types, modifying the pointer through a void ** pointer
violates C strict aliasing.

While core kernel code is compiled with -fno-strict-aliasing, programs under
tools/ are compiled with standard optimizations including -fstrict-aliasing.
If __ensure_mem() is inlined, could the compiler assume that this assignment
does not modify the original typed pointer?

> +	*cap = new_cap;
> +
> +	return 0;
> +}
> +
> +#define ensure_mem(arr_ptr, cap_ptr, cnt) \
> +	__ensure_mem((void **)(arr_ptr), (cap_ptr), (cnt), sizeof(**(arr_ptr)))

[Severity: Medium]
Does casting a typed double pointer (like &ctx->decl_tags which is u32 **) to
void ** here set up the strict aliasing violation mentioned above?

[ ... ]

> @@ -890,17 +919,8 @@ static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, s3
>  
>  static int push_decl_tag_id(struct btf2btf_context *ctx, u32 decl_tag_id)
>  {
> -	u32 *arr = ctx->decl_tags;
> -	u32 cap = ctx->max_decl_tags;
> -
> -	if (ctx->nr_decl_tags + 1 > cap) {
> -		cap = max(cap + 256, cap * 2);
> -		arr = realloc(arr, sizeof(u32) * cap);
> -		if (!arr)
> -			return -ENOMEM;
> -		ctx->max_decl_tags = cap;
> -		ctx->decl_tags = arr;
> -	}
> +	if (ensure_mem(&ctx->decl_tags, &ctx->max_decl_tags, ctx->nr_decl_tags + 1))
> +		return -ENOMEM;
>  
>  	ctx->decl_tags[ctx->nr_decl_tags++] = decl_tag_id;

[Severity: Medium]
Could this assignment result in a use-after-free? If the compiler's strict
aliasing optimization assumes ctx->decl_tags was not modified by ensure_mem(),
it might use a cached pointer that was freed by realloc() during the array
assignment.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260722233518.778854-1-ihor.solodrai@linux.dev?part=1

  reply	other threads:[~2026-07-22 23:48 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22 23:35 [PATCH bpf-next v1 0/8] resolve_btfids: Discover kfuncs from BTF ID sets Ihor Solodrai
2026-07-22 23:35 ` [PATCH bpf-next v1 1/8] resolve_btfids: Implement generic ensure_mem() to grow arrays Ihor Solodrai
2026-07-22 23:48   ` sashiko-bot [this message]
2026-07-23  0:50     ` Ihor Solodrai
2026-07-22 23:35 ` [PATCH bpf-next v1 2/8] resolve_btfids: Index BTF ID symbols by address Ihor Solodrai
2026-07-22 23:35 ` [PATCH bpf-next v1 3/8] resolve_btfids: Keep collected kfuncs in a rbtree Ihor Solodrai
2026-07-22 23:50   ` sashiko-bot
2026-07-23  0:51     ` Ihor Solodrai
2026-07-22 23:35 ` [PATCH bpf-next v1 4/8] libbpf: Export btf__find_by_name_kind_own() Ihor Solodrai
2026-07-22 23:43   ` sashiko-bot
2026-07-23  0:45     ` Ihor Solodrai
2026-07-22 23:35 ` [PATCH bpf-next v1 5/8] resolve_btfids: Fix the _impl lookup for module BTF Ihor Solodrai
2026-07-23  0:46   ` bot+bpf-ci
2026-07-22 23:35 ` [PATCH bpf-next v1 6/8] HID: bpf: Make syscall kfunc flags match the struct_ops set Ihor Solodrai
2026-07-22 23:49   ` sashiko-bot
2026-07-23  0:52     ` Ihor Solodrai
2026-07-22 23:35 ` [PATCH bpf-next v1 7/8] resolve_btfids: Discover kfuncs from BTF ID sets Ihor Solodrai
2026-07-23  0:32   ` bot+bpf-ci
2026-07-23  0:57     ` Ihor Solodrai
2026-07-22 23:35 ` [PATCH bpf-next v1 8/8] resolve_btfids: Enforce consistent kfunc flags across " Ihor Solodrai
2026-07-23  0:32   ` bot+bpf-ci

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260722234842.68B271F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.