Skip to content

net,bpf: fix null-ptr-deref in xdp_master_redirect() for bonding and add selftest

Jiayuan Chen bpf linux-kselftest lkml netdev 7 versions – Applied

Status from this message: This series was applied to netdev/net.git (main)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v7 Latest2 patches 1 replyApplied 2/2
  2. v62 patches 3 repliesSuperseded
  3. v52 patches 11 repliesSuperseded
Earlier versions · 4
VersionPostedPatchesRepliesStatus
v42 patches10 repliesSuperseded
v32 patches5 repliesSuperseded
v22 patches4 repliesSuperseded
v12 patches2 repliesSuperseded

Changes in v7 from the cover letter

https://lore.kernel.org/netdev/[email protected]/T/#t
- selftest: drop the redundant ASSERT_GE checks on bpf_program__fd() and inline the fd into the bpf_xdp_attach() call (Suggested by Daniel Borkmann)
- patch 1: add Acked-by from Daniel Borkmann

Patches in v7 All 2 applied

#Subject and filesSizeRepliesApplied
1/2net, bpf: fix null-ptr-deref in xdp_master_redirect() for down masternet/core/filter.c+2 -01 replyApplied
2/2selftests/bpf: add test for xdp_master_redirect with bond not uptools/testing/selftests/bpf/prog_tests/xdp_bonding.c+94 -20 repliesApplied

Reviews trailers per patch

1 of 2 patches has a Reviewed-by or Acked-by.

PatchDaniel Borkmann
1/2A-b
2/2

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v7 thread

Cover letter [PATCH net v7 0/2] net,bpf: fix null-ptr-deref in xdp_master_redirect() for bonding and add selftest

From: Jiayuan Chen <[email protected]>

This series has gone through several rounds of discussion and the maintainers hold different views on where the fix should live (in the generic xdp_master_redirect() path vs. inside bonding). I respect all of the suggestions, but I would like to get the crash fixed first, so this version takes the approach of checking whether the master device is up in xdp_master_redirect(), as suggested by Daniel Borkmann. If a different shape is preferred later it can be done as a follow-up, but the null-ptr-deref should not linger.

syzkaller reported a kernel panic, full decoded trace here:
https://syzkaller.appspot.com/bug?extid=80e046b8da2820b6ba73

Problem Description

bond_rr_gen_slave_id() dereferences bond->rr_tx_counter without a NULL check. rr_tx_counter is a per-CPU counter that bonding only allocates in bond_open() when the mode is round-robin. If the bond device was never brought up, rr_tx_counter stays NULL.

The XDP redirect path can still reach that code on a bond that was never opened: bpf_master_redirect_enabled_key is a global static key, so as soon as any bond device has native XDP attached, the XDP_TX -> xdp_master_redirect() interception is enabled for every slave system-wide. The path xdp_master_redirect() -> bond_xdp_get_xmit_slave() -> bond_xdp_xmit_roundrobin_slave_get() -> bond_rr_gen_slave_id() then runs against a bond that has no rr_tx_counter and crashes.

Solution

Patch 1: Fix this in the generic xdp_master_redirect() by skipping master interception when the master device is not running. Returning XDP_TX keeps the original XDP_TX behaviour on the receiving slave, and avoids calling into any master ->ndo_xdp_get_xmit_slave() on a device that has not fully initialized its XDP state. This is not specific to
bonding: any current or future master that defers XDP state allocation to ->ndo_open() is protected.
Patch 2: Add a selftest that reproduces the above scenario.

Changes since v6:
https://lore.kernel.org/netdev/[email protected]/T/#t
- selftest: drop the redundant ASSERT_GE checks on bpf_program__fd() and inline the fd into the bpf_xdp_attach() call (Suggested by Daniel Borkmann)
- patch 1: add Acked-by from Daniel Borkmann

Changes since v5:
https://lore.kernel.org/netdev/[email protected]/
- Moved the fix back into xdp_master_redirect() and check IFF_UP on the master device; return XDP_ABORTED when the master is not up (Suggested by Daniel Borkmann, seconded by Paolo Abeni and Eric Dumazet)

Changes since v4:
https://lore.kernel.org/netdev/[email protected]/
- Reverted unconditional alloc in bond_init(); instead add a NULL check with unlikely()/READ_ONCE() in bond_rr_gen_slave_id() and WRITE_ONCE() in bond_open(), avoiding memory waste for non-RR modes (Suggested by Nikolay Aleksandrov, patch by Jay Vosburgh)

Changes since v3:
https://lore.kernel.org/netdev/[email protected]/T/#t
- Added code comment and commit log explaining why rr_tx_counter is allocated unconditionally for all modes (Suggested by Jay Vosburgh)

Changes since v2:
https://lore.kernel.org/netdev/[email protected]/T/#t
- Moved allocation from bond_create_init() helper into bond_init() (ndo_init), which is the natural single point covering both creation paths and also handles post-creation mode changes to round-robin

Changes since v1:
https://lore.kernel.org/netdev/[email protected]/T/#t
- Moved the guard for NULL rr_tx_counter from xdp_master_redirect() into the bonding subsystem itself
(Suggested by Sebastian Andrzej Siewior <[email protected]>)

[1] https://syzkaller.appspot.com/bug?extid=80e046b8da2820b6ba73

Jiayuan Chen (2):
net, bpf: fix null-ptr-deref in xdp_master_redirect() for down master selftests/bpf: add test for xdp_master_redirect with bond not up

 net/core/filter.c                             |  2 +
 .../selftests/bpf/prog_tests/xdp_bonding.c    | 96 ++++++++++++++++++-
 2 files changed, 96 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0