netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bpf-next: Prevent out of bound buffer write in __bpf_get_stack
@ 2026-01-04 20:52 Arnaud Lecomte
  2026-01-05  5:50 ` Yonghong Song
  2026-01-06  0:51 ` Andrii Nakryiko
  0 siblings, 2 replies; 5+ messages in thread
From: Arnaud Lecomte @ 2026-01-04 20:52 UTC (permalink / raw)
  To: syzbot+d1b7fa1092def3628bd7
  Cc: andrii, ast, bpf, contact, daniel, eddyz87, haoluo,
	john.fastabend, jolsa, kpsingh, linux-kernel, martin.lau, netdev,
	sdf, song, syzkaller-bugs, yonghong.song, Brahmajit Das

Syzkaller reported a KASAN slab-out-of-bounds write in __bpf_get_stack()
during stack trace copying.

The issue occurs when: the callchain entry (stored as a per-cpu variable)
grow between collection and buffer copy, causing it to exceed the initially
calculated buffer size based on max_depth.

The callchain collection intentionally avoids locking for performance
reasons, but this creates a window where concurrent modifications can
occur during the copy operation.

To prevent this from happening, we clamp the trace len to the max
depth initially calculated with the buffer size and the size of
a trace.

Reported-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/691231dc.a70a0220.22f260.0101.GAE@google.com/T/
Fixes: e17d62fedd10 ("bpf: Refactor stack map trace depth calculation into helper function")
Tested-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
Cc: Brahmajit Das <listout@listout.xyz>
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
---
Thanks Brahmajit Das for the initial fix he proposed that I tweaked
with the correct justification and a better implementation in my
opinion.
---
 kernel/bpf/stackmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index da3d328f5c15..e56752a9a891 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -465,7 +465,6 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
 
 	if (trace_in) {
 		trace = trace_in;
-		trace->nr = min_t(u32, trace->nr, max_depth);
 	} else if (kernel && task) {
 		trace = get_callchain_entry_for_task(task, max_depth);
 	} else {
@@ -479,7 +478,8 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
 		goto err_fault;
 	}
 
-	trace_nr = trace->nr - skip;
+	trace_nr = min(trace->nr, max_depth);
+	trace_nr = trace_nr - skip;
 	copy_len = trace_nr * elem_size;
 
 	ips = trace->ip + skip;
-- 
2.43.0


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

* Re: [PATCH] bpf-next: Prevent out of bound buffer write in __bpf_get_stack
  2026-01-04 20:52 [PATCH] bpf-next: Prevent out of bound buffer write in __bpf_get_stack Arnaud Lecomte
@ 2026-01-05  5:50 ` Yonghong Song
  2026-01-06  0:51 ` Andrii Nakryiko
  1 sibling, 0 replies; 5+ messages in thread
From: Yonghong Song @ 2026-01-05  5:50 UTC (permalink / raw)
  To: Arnaud Lecomte, syzbot+d1b7fa1092def3628bd7
  Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
	kpsingh, linux-kernel, martin.lau, netdev, sdf, song,
	syzkaller-bugs, Brahmajit Das



On 1/4/26 12:52 PM, Arnaud Lecomte wrote:
> Syzkaller reported a KASAN slab-out-of-bounds write in __bpf_get_stack()
> during stack trace copying.
>
> The issue occurs when: the callchain entry (stored as a per-cpu variable)
> grow between collection and buffer copy, causing it to exceed the initially
> calculated buffer size based on max_depth.
>
> The callchain collection intentionally avoids locking for performance
> reasons, but this creates a window where concurrent modifications can
> occur during the copy operation.
>
> To prevent this from happening, we clamp the trace len to the max
> depth initially calculated with the buffer size and the size of
> a trace.
>
> Reported-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/all/691231dc.a70a0220.22f260.0101.GAE@google.com/T/
> Fixes: e17d62fedd10 ("bpf: Refactor stack map trace depth calculation into helper function")
> Tested-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
> Cc: Brahmajit Das <listout@listout.xyz>
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>

LGTM.

Acked-by: Yonghong Song <yonghong.song@linux.dev>


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

* Re: [PATCH] bpf-next: Prevent out of bound buffer write in __bpf_get_stack
  2026-01-04 20:52 [PATCH] bpf-next: Prevent out of bound buffer write in __bpf_get_stack Arnaud Lecomte
  2026-01-05  5:50 ` Yonghong Song
@ 2026-01-06  0:51 ` Andrii Nakryiko
  2026-01-07 18:08   ` Lecomte, Arnaud
  1 sibling, 1 reply; 5+ messages in thread
From: Andrii Nakryiko @ 2026-01-06  0:51 UTC (permalink / raw)
  To: Arnaud Lecomte
  Cc: syzbot+d1b7fa1092def3628bd7, andrii, ast, bpf, daniel, eddyz87,
	haoluo, john.fastabend, jolsa, kpsingh, linux-kernel, martin.lau,
	netdev, sdf, song, syzkaller-bugs, yonghong.song, Brahmajit Das

On Sun, Jan 4, 2026 at 12:52 PM Arnaud Lecomte <contact@arnaud-lcm.com> wrote:
>
> Syzkaller reported a KASAN slab-out-of-bounds write in __bpf_get_stack()
> during stack trace copying.
>
> The issue occurs when: the callchain entry (stored as a per-cpu variable)
> grow between collection and buffer copy, causing it to exceed the initially
> calculated buffer size based on max_depth.
>
> The callchain collection intentionally avoids locking for performance
> reasons, but this creates a window where concurrent modifications can
> occur during the copy operation.
>
> To prevent this from happening, we clamp the trace len to the max
> depth initially calculated with the buffer size and the size of
> a trace.
>
> Reported-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/all/691231dc.a70a0220.22f260.0101.GAE@google.com/T/
> Fixes: e17d62fedd10 ("bpf: Refactor stack map trace depth calculation into helper function")
> Tested-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
> Cc: Brahmajit Das <listout@listout.xyz>
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> ---
> Thanks Brahmajit Das for the initial fix he proposed that I tweaked
> with the correct justification and a better implementation in my
> opinion.
> ---
>  kernel/bpf/stackmap.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index da3d328f5c15..e56752a9a891 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -465,7 +465,6 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
>
>         if (trace_in) {
>                 trace = trace_in;
> -               trace->nr = min_t(u32, trace->nr, max_depth);
>         } else if (kernel && task) {
>                 trace = get_callchain_entry_for_task(task, max_depth);
>         } else {
> @@ -479,7 +478,8 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
>                 goto err_fault;
>         }
>
> -       trace_nr = trace->nr - skip;
> +       trace_nr = min(trace->nr, max_depth);

there is `trace->nr < skip` check right above, should it be moved here
and done against adjusted trace_nr (but before we subtract skip, of
course)?

> +       trace_nr = trace_nr - skip;
>         copy_len = trace_nr * elem_size;
>
>         ips = trace->ip + skip;
> --
> 2.43.0
>

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

* Re: [PATCH] bpf-next: Prevent out of bound buffer write in __bpf_get_stack
  2026-01-06  0:51 ` Andrii Nakryiko
@ 2026-01-07 18:08   ` Lecomte, Arnaud
  2026-01-07 18:35     ` Lecomte, Arnaud
  0 siblings, 1 reply; 5+ messages in thread
From: Lecomte, Arnaud @ 2026-01-07 18:08 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: syzbot+d1b7fa1092def3628bd7, andrii, ast, bpf, daniel, eddyz87,
	haoluo, john.fastabend, jolsa, kpsingh, linux-kernel, martin.lau,
	netdev, sdf, song, syzkaller-bugs, yonghong.song, Brahmajit Das


On 06/01/2026 01:51, Andrii Nakryiko wrote:
> On Sun, Jan 4, 2026 at 12:52 PM Arnaud Lecomte <contact@arnaud-lcm.com> wrote:
>> Syzkaller reported a KASAN slab-out-of-bounds write in __bpf_get_stack()
>> during stack trace copying.
>>
>> The issue occurs when: the callchain entry (stored as a per-cpu variable)
>> grow between collection and buffer copy, causing it to exceed the initially
>> calculated buffer size based on max_depth.
>>
>> The callchain collection intentionally avoids locking for performance
>> reasons, but this creates a window where concurrent modifications can
>> occur during the copy operation.
>>
>> To prevent this from happening, we clamp the trace len to the max
>> depth initially calculated with the buffer size and the size of
>> a trace.
>>
>> Reported-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
>> Closes: https://lore.kernel.org/all/691231dc.a70a0220.22f260.0101.GAE@google.com/T/
>> Fixes: e17d62fedd10 ("bpf: Refactor stack map trace depth calculation into helper function")
>> Tested-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
>> Cc: Brahmajit Das <listout@listout.xyz>
>> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
>> ---
>> Thanks Brahmajit Das for the initial fix he proposed that I tweaked
>> with the correct justification and a better implementation in my
>> opinion.
>> ---
>>   kernel/bpf/stackmap.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
>> index da3d328f5c15..e56752a9a891 100644
>> --- a/kernel/bpf/stackmap.c
>> +++ b/kernel/bpf/stackmap.c
>> @@ -465,7 +465,6 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
>>
>>          if (trace_in) {
>>                  trace = trace_in;
>> -               trace->nr = min_t(u32, trace->nr, max_depth);
>>          } else if (kernel && task) {
>>                  trace = get_callchain_entry_for_task(task, max_depth);
>>          } else {
>> @@ -479,7 +478,8 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
>>                  goto err_fault;
>>          }
>>
>> -       trace_nr = trace->nr - skip;
>> +       trace_nr = min(trace->nr, max_depth);
> there is `trace->nr < skip` check right above, should it be moved here
> and done against adjusted trace_nr (but before we subtract skip, of
> course)?
We could indeed be more proactive on the clamping even-though I would
  say it does not fundamentally change anything in my opinion.
Happy to raise a new rev.
>> +       trace_nr = trace_nr - skip;
>>          copy_len = trace_nr * elem_size;
>>
>>          ips = trace->ip + skip;
>> --
>> 2.43.0
>>
Thanks for the review !
Arnaud

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

* Re: [PATCH] bpf-next: Prevent out of bound buffer write in __bpf_get_stack
  2026-01-07 18:08   ` Lecomte, Arnaud
@ 2026-01-07 18:35     ` Lecomte, Arnaud
  0 siblings, 0 replies; 5+ messages in thread
From: Lecomte, Arnaud @ 2026-01-07 18:35 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: syzbot+d1b7fa1092def3628bd7, andrii, ast, bpf, daniel, eddyz87,
	haoluo, john.fastabend, jolsa, kpsingh, linux-kernel, martin.lau,
	netdev, sdf, song, syzkaller-bugs, yonghong.song, Brahmajit Das


On 07/01/2026 19:08, Lecomte, Arnaud wrote:
>
> On 06/01/2026 01:51, Andrii Nakryiko wrote:
>> On Sun, Jan 4, 2026 at 12:52 PM Arnaud Lecomte 
>> <contact@arnaud-lcm.com> wrote:
>>> Syzkaller reported a KASAN slab-out-of-bounds write in 
>>> __bpf_get_stack()
>>> during stack trace copying.
>>>
>>> The issue occurs when: the callchain entry (stored as a per-cpu 
>>> variable)
>>> grow between collection and buffer copy, causing it to exceed the 
>>> initially
>>> calculated buffer size based on max_depth.
>>>
>>> The callchain collection intentionally avoids locking for performance
>>> reasons, but this creates a window where concurrent modifications can
>>> occur during the copy operation.
>>>
>>> To prevent this from happening, we clamp the trace len to the max
>>> depth initially calculated with the buffer size and the size of
>>> a trace.
>>>
>>> Reported-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
>>> Closes: 
>>> https://lore.kernel.org/all/691231dc.a70a0220.22f260.0101.GAE@google.com/T/
>>> Fixes: e17d62fedd10 ("bpf: Refactor stack map trace depth 
>>> calculation into helper function")
>>> Tested-by: syzbot+d1b7fa1092def3628bd7@syzkaller.appspotmail.com
>>> Cc: Brahmajit Das <listout@listout.xyz>
>>> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
>>> ---
>>> Thanks Brahmajit Das for the initial fix he proposed that I tweaked
>>> with the correct justification and a better implementation in my
>>> opinion.
>>> ---
>>>   kernel/bpf/stackmap.c | 4 ++--
>>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
>>> index da3d328f5c15..e56752a9a891 100644
>>> --- a/kernel/bpf/stackmap.c
>>> +++ b/kernel/bpf/stackmap.c
>>> @@ -465,7 +465,6 @@ static long __bpf_get_stack(struct pt_regs 
>>> *regs, struct task_struct *task,
>>>
>>>          if (trace_in) {
>>>                  trace = trace_in;
>>> -               trace->nr = min_t(u32, trace->nr, max_depth);
>>>          } else if (kernel && task) {
>>>                  trace = get_callchain_entry_for_task(task, max_depth);
>>>          } else {
>>> @@ -479,7 +478,8 @@ static long __bpf_get_stack(struct pt_regs 
>>> *regs, struct task_struct *task,
>>>                  goto err_fault;
>>>          }
>>>
>>> -       trace_nr = trace->nr - skip;
>>> +       trace_nr = min(trace->nr, max_depth);
>> there is `trace->nr < skip` check right above, should it be moved here
>> and done against adjusted trace_nr (but before we subtract skip, of
>> course)?
> We could indeed be more proactive on the clamping even-though I would
>  say it does not fundamentally change anything in my opinion.
> Happy to raise a new rev.
Nvm, this is not really possible as we are checking that the trace is 
not NULL.
Moving it above could lead to a NULL dereference.
>>> +       trace_nr = trace_nr - skip;
>>>          copy_len = trace_nr * elem_size;
>>>
>>>          ips = trace->ip + skip;
>>> -- 
>>> 2.43.0
>>>
> Thanks for the review !
> Arnaud

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

end of thread, other threads:[~2026-01-07 18:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-04 20:52 [PATCH] bpf-next: Prevent out of bound buffer write in __bpf_get_stack Arnaud Lecomte
2026-01-05  5:50 ` Yonghong Song
2026-01-06  0:51 ` Andrii Nakryiko
2026-01-07 18:08   ` Lecomte, Arnaud
2026-01-07 18:35     ` Lecomte, Arnaud

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).