xdp: Reset bpf_redirect_info before running a xdp's BPF prog.
Subsystems (4)
PATCH 1/1 xdp: Reset bpf_redirect_info before running a xdp's BPF prog.
Ricardo reported a KASAN discovered use after free in v6.6-stable.
The syzbot starts a BPF program via xdp_test_run_batch() which assigns ri->tgt_value via dev_hash_map_redirect() and the return code isn't XDP_REDIRECT it looks like nonsense. So the output in bpf_warn_invalid_xdp_action() appears once.
Then the TUN driver runs another BPF program (on the same CPU) which returns XDP_REDIRECT without setting ri->tgt_value first. It invokes bpf_trace_printk() to print four characters and obtain the required return value. This is enough to get xdp_do_redirect() invoked which then accesses the pointer in tgt_value which might have been already deallocated.
This problem does not affect upstream because since commit
401cb7dae8130 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")
the per-CPU variable is referenced via task's task_struct and exists on the stack during NAPI callback. Therefore it is cleared once before the first invocation and remains valid within the RCU section of the NAPI callback.
Instead of performing the huge backport of the commit (plus its fix ups) here is an alternative version which only resets the variable in question prior invoking the BPF program.
---
I discussed this with Toke, thread starts at
https://lore.kernel.org/all/[email protected]/
The commit, which this by accident, is part of v6.11-rc1. I added the commit introducing map redirects as the origin of the problem which is v4.14-rc1. The code is a bit different there but it seems to work similar.
Affected kernels would be from v4.14 to v6.10.
include/net/xdp.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
include/net/xdp.h +8 -1
@@ -486,7 +486,14 @@ static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,* under local_bh_disable(), which provides the needed RCU protection * for accessing map entries. */ -u32 act =__bpf_prog_run (prog, xdp,BPF_DISPATCHER_FUNC (xdp)); +struct bpf_redirect_info *ri =this_cpu_ptr (&bpf_redirect_info); +u32 act; + +if (ri->map_id || ri->map_type) { + ri->map_id =0 ; + ri->map_type =BPF_MAP_TYPE_UNSPEC ; + } + act =__bpf_prog_run (prog, xdp,BPF_DISPATCHER_FUNC (xdp));if (static_branch_unlikely (&bpf_master_redirect_enabled_key)) {if (act ==XDP_TX &&netif_is_bond_slave (xdp->rxq->dev))
Signature · 1 line
2.49.0
Greg KH
maintainerQuoted 32 lines · Sebastian Andrzej Siewior
Ricardo reported a KASAN discovered use after free in v6.6-stable.
The syzbot starts a BPF program via
xdp_test_run_batch()which assignsri->tgt_valueviadev_hash_map_redirect()and the return code isn'tXDP_REDIRECTit looks like nonsense. So the output inbpf_warn_invalid_xdp_action()appears once.
Then the TUN driver runs another BPF program (on the same CPU) which returnsXDP_REDIRECTwithout settingri->tgt_valuefirst. It invokesbpf_trace_printk()to print four characters and obtain the required return value. This is enough to getxdp_do_redirect()invoked which then accesses the pointer intgt_valuewhich might have been already deallocated.This problem does not affect upstream because since commit
401cb7dae8130("net: Referencebpf_redirect_infoviatask_structonPREEMPT_RT.")the per-CPU variable is referenced via task's
task_structand exists on the stack during NAPI callback. Therefore it is cleared once before the first invocation and remains valid within the RCU section of the NAPI callback.Instead of performing the huge backport of the commit (plus its fix ups) here is an alternative version which only resets the variable in question prior invoking the BPF program.
Reported-by Ricardo Cañuelo Navarro <[email protected]>Closes https://lore.kernel.org/all/20250226-20250204-kasan-slab-use-after-free-read-in-dev_map_enqueue__submit-v3-0-360efec441ba@igalia.com/ open hereFixes 97f91a7cf04ff ("bpf: add bpf_redirect_map helper routine")Signed-off-by Sebastian Andrzej Siewior <[email protected]>---
Quoted 8 lines
I discussed this with Toke, thread starts at
https://lore.kernel.org/all/[email protected]/The commit, which this by accident, is part of v6.11-rc1. I added the commit introducing map redirects as the origin of the problem which is v4.14-rc1. The code is a bit different there but it seems to work similar.
Affected kernels would be from v4.14 to v6.10.
Does not apply to any tree other than 6.6.y :(
PATCH 1/1 xdp: Reset bpf_redirect_info before running a xdp's BPF prog.
Ricardo reported a KASAN discovered use after free in v6.6-stable.
The syzbot starts a BPF program via xdp_test_run_batch() which assigns ri->tgt_value via dev_hash_map_redirect() and the return code isn't XDP_REDIRECT it looks like nonsense. So the output in bpf_warn_invalid_xdp_action() appears once.
Then the TUN driver runs another BPF program (on the same CPU) which returns XDP_REDIRECT without setting ri->tgt_value first. It invokes bpf_trace_printk() to print four characters and obtain the required return value. This is enough to get xdp_do_redirect() invoked which then accesses the pointer in tgt_value which might have been already deallocated.
This problem does not affect upstream because since commit
401cb7dae8130 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")
the per-CPU variable is referenced via task's task_struct and exists on the stack during NAPI callback. Therefore it is cleared once before the first invocation and remains valid within the RCU section of the NAPI callback.
Instead of performing the huge backport of the commit (plus its fix ups) here is an alternative version which only resets the variable in question prior invoking the BPF program.
---
backport: function moved to filter.h from xdp.h
include/linux/filter.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
include/linux/filter.h +8 -1
@@ -775,7 +775,14 @@ static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,* under local_bh_disable(), which provides the needed RCU protection * for accessing map entries. */ -u32 act =__bpf_prog_run (prog, xdp,BPF_DISPATCHER_FUNC (xdp)); +struct bpf_redirect_info *ri =this_cpu_ptr (&bpf_redirect_info); +u32 act; + +if (ri->map_id || ri->map_type) { + ri->map_id =0 ; + ri->map_type =BPF_MAP_TYPE_UNSPEC ; + } + act =__bpf_prog_run (prog, xdp,BPF_DISPATCHER_FUNC (xdp));if (static_branch_unlikely (&bpf_master_redirect_enabled_key)) {if (act ==XDP_TX &&netif_is_bond_slave (xdp->rxq->dev))
Signature · 1 line
2.49.0
Sasha Levin
maintainer[ Sasha's backport helper bot ]
Hi,
Summary of potential issues:
⚠️ Could not find matching upstream commit
No upstream commit was identified. Using temporary commit for testing.
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-5.15.y | Success | Success | | stable/linux-6.1.y | Success | Success |
PATCH 1/1 xdp: Reset bpf_redirect_info before running a xdp's BPF prog.
In reply to Greg KHRicardo reported a KASAN discovered use after free in v6.6-stable.
The syzbot starts a BPF program via xdp_test_run_batch() which assigns ri->tgt_value via dev_hash_map_redirect() and the return code isn't XDP_REDIRECT it looks like nonsense. So the output in bpf_warn_invalid_xdp_action() appears once.
Then the TUN driver runs another BPF program (on the same CPU) which returns XDP_REDIRECT without setting ri->tgt_value first. It invokes bpf_trace_printk() to print four characters and obtain the required return value. This is enough to get xdp_do_redirect() invoked which then accesses the pointer in tgt_value which might have been already deallocated.
This problem does not affect upstream because since commit
401cb7dae8130 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")
the per-CPU variable is referenced via task's task_struct and exists on the stack during NAPI callback. Therefore it is cleared once before the first invocation and remains valid within the RCU section of the NAPI callback.
Instead of performing the huge backport of the commit (plus its fix ups) here is an alternative version which only resets the variable in question prior invoking the BPF program.
---
backport: moved to filter.h, replaced map_id and map_type with map
include/linux/filter.h | 4 ++++ 1 file changed, 4 insertions(+)
include/linux/filter.h +4 -0
@@ -758,6 +758,10 @@ static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,* already takes rcu_read_lock() when fetching the program, so * it's not necessary here anymore. */ +struct bpf_redirect_info *ri =this_cpu_ptr (&bpf_redirect_info); + +if (ri->map) + ri->map =NULL ;return __BPF_PROG_RUN (prog, xdp,BPF_DISPATCHER_FUNC (xdp)); }
Signature · 1 line
2.49.0
Sasha Levin
maintainer[ Sasha's backport helper bot ]
Hi,
Summary of potential issues:
⚠️ Could not find matching upstream commit
No upstream commit was identified. Using temporary commit for testing.
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-5.10.y | Success | Success |
PATCH 1/1 xdp: Reset bpf_redirect_info before running a xdp's BPF prog.
In reply to Greg KHRicardo reported a KASAN discovered use after free in v6.6-stable.
The syzbot starts a BPF program via xdp_test_run_batch() which assigns ri->tgt_value via dev_hash_map_redirect() and the return code isn't XDP_REDIRECT it looks like nonsense. So the output in bpf_warn_invalid_xdp_action() appears once.
Then the TUN driver runs another BPF program (on the same CPU) which returns XDP_REDIRECT without setting ri->tgt_value first. It invokes bpf_trace_printk() to print four characters and obtain the required return value. This is enough to get xdp_do_redirect() invoked which then accesses the pointer in tgt_value which might have been already deallocated.
This problem does not affect upstream because since commit
401cb7dae8130 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")
the per-CPU variable is referenced via task's task_struct and exists on the stack during NAPI callback. Therefore it is cleared once before the first invocation and remains valid within the RCU section of the NAPI callback.
Instead of performing the huge backport of the commit (plus its fix ups) here is an alternative version which only resets the variable in question prior invoking the BPF program.
---
backport: moved to filter.h, replaced map_id and map_type with map
include/linux/filter.h | 4 ++++ 1 file changed, 4 insertions(+)
include/linux/filter.h +4 -0
@@ -721,6 +721,10 @@ static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,* already takes rcu_read_lock() when fetching the program, so * it's not necessary here anymore. */ +struct bpf_redirect_info *ri =this_cpu_ptr (&bpf_redirect_info); + +if (ri->map) + ri->map =NULL ;return BPF_PROG_RUN (prog, xdp); }
Signature · 1 line
2.49.0
Sasha Levin
maintainer[ Sasha's backport helper bot ]
Hi,
Summary of potential issues:
⚠️ Could not find matching upstream commit
No upstream commit was identified. Using temporary commit for testing.
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-5.4.y | Success | Success |