bpf: Clamp the trace length in __bpf_get_stack() again

get_perf_callchain() and get_callchain_entry_for_task() store a trace in a
per-CPU entry, one per context. They release the entry before they return
it. Thus a second user in the same context can write to the entry while the
first user reads it. preempt_disable() stops only other tasks.

__bpf_get_stack() requests at most max_depth frames. This is skip plus the
frames that fit in the buffer, but not more than perf_event_max_stack.
Since the commit below, it copies 'trace->nr - skip' frames with no limit.
But a second user can store a longer trace. The unwinders of PowerPC add
context markers that max_depth does not limit. In both cases the copy
writes after the end of the buffer.

Read trace->nr one time. Clamp the value to max_depth before the skip test,
as __bpf_get_stack_pe() does. Thus when max_depth is less than skip, the
helper returns -EFAULT, and 'trace_nr - skip' does not underflow. The
copied frames can still be those of the second user.

Fixes: e17d62fedd ("bpf: Refactor stack map trace depth calculation into helper function")
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://patch.msgid.link/20261101190535.1454146-2-alexei.starovoitov@gmail.com
This commit is contained in:
Josef Bacik 2026-11-01 19:05:09 +00:00 committed by Alexei Starovoitov
parent 1d55411e8b
commit 007fd46079
No known key found for this signature in database

View File

@ -822,14 +822,20 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
crosstask, false, 0);
}
if (unlikely(!trace) || trace->nr < skip) {
if (may_fault)
rcu_read_unlock();
preempt_enable();
goto err_fault;
}
if (unlikely(!trace))
goto err_unlock;
trace_nr = callchain_store(trace, trace->nr, buf, elem_size, flags);
/*
* trace->nr can be above max_depth. The per-CPU entry is released
* already: a tracing program or an NMI which takes a callchain in the
* same context before the copy below reuses it. And the unwinders of
* PowerPC store context markers on top of max_depth.
*/
trace_nr = min_t(u32, READ_ONCE(trace->nr), max_depth);
if (trace_nr < skip)
goto err_unlock;
trace_nr = callchain_store(trace, trace_nr, buf, elem_size, flags);
/* trace should not be dereferenced after this point */
if (may_fault)
@ -838,6 +844,10 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
return callchain_finalize(buf, size, trace_nr, elem_size, flags, may_fault);
err_unlock:
if (may_fault)
rcu_read_unlock();
preempt_enable();
err_fault:
err = -EFAULT;
clear: