BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jiri Olsa" <jolsa@kernel.org>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCHv4 bpf-next 01/12] bpf: Factor stackid_init function from __bpf_get_stackid
Date: Wed, 05 Aug 2026 09:40:37 +0000	[thread overview]
Message-ID: <20260805094038.50F0C1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260805092843.516315-2-jolsa@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Integer underflow in stack_map_calculate_max_depth causes a heap buffer overflow when sysctl_perf_event_max_stack is concurrently modified.
--

--- Patch [1]: [PATCHv4 bpf-next 01/12] bpf: Factor stackid_init function from __bpf_get_stackid ---
commit 036b334d8e7b51a2339ee19596a116e90ed56c01
Author: Jiri Olsa <jolsa@kernel.org>

bpf: Factor stackid_init function from __bpf_get_stackid

The new stackid_init function stores all the necessary bits for stackid
trace and it will be used by other functions in following changes.

> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c

[ ... ]

> +static int stackid_init(struct stackid *stackid, struct bpf_map *map,
> +			struct perf_callchain_entry *trace, u64 flags)
>  {
>  	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
>  	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
> +	u32 max_depth;
>  
>  	if (trace->nr <= skip)
>  		/* skipping more than usable stack trace */
>  		return -EFAULT;
>  
>  	max_depth = stack_map_calculate_max_depth(map->value_size, stack_map_data_size(map), flags);
> +	stackid->nr = min_t(u32, trace->nr - skip, max_depth - skip);

[Severity: High]
This is a pre-existing issue, but can this max_depth - skip calculation
underflow?

If sysctl_perf_event_max_stack is decreased concurrently,
stack_map_calculate_max_depth() can return a max_depth smaller than skip:

kernel/bpf/stackmap.c:stack_map_calculate_max_depth() {
	...
	max_depth += skip;
	if (max_depth > curr_sysctl_max_stack)
		return curr_sysctl_max_stack;
	...
}

This would cause max_depth - skip to underflow to a large integer. If
trace->nr (which may have been captured with the older, larger sysctl) is
greater than skip, it passes the early return check.

The min_t() call would then select trace->nr - skip, which can exceed the
stack map's element capacity and lead to a heap buffer overflow later when
copying entries into the bucket buffer in __bpf_get_stackid():

kernel/bpf/stackmap.c:__bpf_get_stackid() {
	...
		memcpy(new_bucket->data, stackid->ips, stackid->len);
	...
}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260805092843.516315-2-jolsa@kernel.org?part=1

  reply	other threads:[~2026-08-05  9:40 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-05  9:28 [PATCHv4 bpf-next 00/12] bpf: Disable preemption in stack map code Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 01/12] bpf: Factor stackid_init function from __bpf_get_stackid Jiri Olsa
2026-08-05  9:40   ` sashiko-bot [this message]
2026-08-05  9:28 ` [PATCHv4 bpf-next 02/12] bpf: Factor stackid_fastpath " Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 03/12] bpf: Factor stackid_new_bucket " Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 04/12] bpf: Use stack id functions instead of __bpf_get_stackid Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 05/12] bpf: Disable preemption in bpf_get_stackid Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 06/12] bpf: Factor callchain_store function from __bpf_get_stack Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 07/12] bpf: Factor callchain_finalize " Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 08/12] bpf: Remove trace_in argument " Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 09/12] bpf: Clear buf on error in __bpf_get_task_stack Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 10/12] bpf: Disable preemption in __bpf_get_stack Jiri Olsa
2026-08-05 18:34   ` Andrii Nakryiko
2026-08-05 20:29     ` Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 11/12] bpf: Avoid changing callchain in bpf_get_stack_pe Jiri Olsa
2026-08-05  9:28 ` [PATCHv4 bpf-next 12/12] bpf: Avoid changing callchain in bpf_get_stackid_pe Jiri Olsa
2026-08-05 18:36 ` [PATCHv4 bpf-next 00/12] bpf: Disable preemption in stack map code Andrii Nakryiko
2026-08-05 18:40 ` patchwork-bot+netdevbpf

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=20260805094038.50F0C1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jolsa@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox