bpf: fix reading neigh ha in bpf_fib_lookup()
Subsystems (5)
PATCH 1/1 bpf: fix reading neigh ha in bpf_fib_lookup()
bpf_ipv4_fib_lookup() and bpf_ipv6_fib_lookup() copy the neighbour's link layer address into params->dmac without any synchronisation, but neigh_update() writes neigh->ha under write_seqlock(&neigh->ha_lock) exactly because there are lockless readers. A BPF program calling bpf_fib_lookup() while the neighbour is being updated can therefore be handed a torn address, and XDP/tc then forwards the packet to a bogus L2 destination.
neigh_ha_snapshot() cannot be used here because it copies dev->addr_len bytes while params->dmac is only ETH_ALEN long (an IPoIB egress device has addr_len 20 and would overflow into params->smac), so open-code the seqlock loop around the ETH_ALEN copy.
Same problem and same fix as commit 57549ab90791 ("net: bridge: arp/nd
proxy: fix reading neigh ha") and commit b824059a673b ("vxlan: fix reading neigh ha").
Reproduced on x86_64 under qemu: a dummy device holds a permanent neighbour whose lladdr is flipped between aa:aa:aa:aa:aa:aa and bb:bb:bb:bb:bb:bb with RTM_NEWNEIGH, while an XDP program driven by BPF_PROG_TEST_RUN calls bpf_fib_lookup() in a loop and checks that all six bytes of the returned dmac are equal. Before this patch: 211 torn addresses out of 16300000 lookups (last one aa:aa:aa:aa:bb:bb). After this patch: 0 out of 38060000 lookups.
--- net/core/filter.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
net/core/filter.c +16 -2
@@ -6298,6 +6298,20 @@ static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {#endif #if IS_ENABLED (CONFIG_INET ) ||IS_ENABLED (CONFIG_IPV6 ) +/* Take a stable snapshot of the neighbour's link layer address. +* neigh_ha_snapshot() can not be used here because it copies dev->addr_len +* bytes while params->dmac is only ETH_ALEN long. +*/ +static void bpf_fib_dmac_snapshot (u8 *dmac,const struct neighbour *neigh) +{ +unsigned int seq; + +do { + seq =read_seqbegin (&neigh->ha_lock); +memcpy (dmac, neigh->ha,ETH_ALEN ); + }while (read_seqretry (&neigh->ha_lock, seq)); +} +static int bpf_fib_set_fwd_params (struct net_device *dev,struct bpf_fib_lookup *params,u32 flags,u32 mtu,u32 in_ifindex)
@@ -6491,7 +6505,7 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,if (!neigh || !(READ_ONCE (neigh->nud_state) &NUD_VALID ))return BPF_FIB_LKUP_RET_NO_NEIGH ; -memcpy (params->dmac, neigh->ha,ETH_ALEN ); +bpf_fib_dmac_snapshot (params->dmac, neigh);memcpy (params->smac, dev->dev_addr,ETH_ALEN ); set_fwd_params:
@@ -6644,7 +6658,7 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params, neigh =__ipv6_neigh_lookup_noref (dev, dst);if (!neigh || !(READ_ONCE (neigh->nud_state) &NUD_VALID ))return BPF_FIB_LKUP_RET_NO_NEIGH ; -memcpy (params->dmac, neigh->ha,ETH_ALEN ); +bpf_fib_dmac_snapshot (params->dmac, neigh);memcpy (params->smac, dev->dev_addr,ETH_ALEN ); set_fwd_params:
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] A softirq deadlock can occur because `bpf_fib_dmac_snapshot` uses `read_seqbegin(&neigh->ha_lock)`…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] A softirq deadlock can occur because bpf_fib_dmac_snapshot uses read_seqbegin(&neigh->ha_lock) in softirq context (XDP/TC), while neigh->ha_lock is updated in process context without disabling softirqs.
- [Low] The multi-line comment introduced in net/core/filter.c violates the BPF subsystem comment style.
--
commit 08a77b636ca52caaeee611a7902f55a43c9d7350
Author: Nikhil <[email protected]>
bpf: fix reading neigh ha in bpf_fib_lookup()
This commit fixes a lockless data tear issue when reading the neighbor hardware address in bpf_fib_lookup(). It introduces bpf_fib_dmac_snapshot() to safely read the MAC address using a seqlock retry loop, avoiding neigh_ha_snapshot() which would overflow params->dmac on some device types.
net/core/filter.c static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = { · line 6303
Quoted 12 lines
net/core/filter.c +4
-0@@ -6297,6 +6297,20 @@ static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {#endif #if IS_ENABLED (CONFIG_INET ) ||IS_ENABLED (CONFIG_IPV6 ) +/* Take a stable snapshot of the neighbour's link layer address. +* neigh_ha_snapshot() can not be used here because it copies dev->addr_len +* bytes while params->dmac is only ETH_ALEN long. +*/
[Severity: Low]
This isn't a bug, but does this violate the BPF subsystem comment style? The guidelines require the opening /* to be on its own line.
net/core/filter.c static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = { · line 6310
Quoted 6 lines
net/core/filter.c +6
-0+static void bpf_fib_dmac_snapshot (u8 *dmac,const struct neighbour *neigh) +{ +unsigned int seq; + + do { + seq = read_seqbegin(&neigh->ha_lock);
[Severity: High]
Can this cause a softirq deadlock?
Since bpf_ipv4_fib_lookup() is called from XDP/TC programs in softirq context, bpf_fib_dmac_snapshot() will spin on read_seqbegin() here.
If a process context function calls __neigh_update() on the same CPU, it acquires neigh->ha_lock using write_seqlock() which only disables preemption, not softirqs.
If a network interrupt arrives and triggers softirq processing on that exact same CPU while the writer holds the lock, read_seqbegin() will loop indefinitely waiting for the sequence count to become even, leading to a CPU lockup.
Quoted 4 lines
net/core/filter.c +4
-0+memcpy (dmac, neigh->ha,ETH_ALEN ); + }while (read_seqretry (&neigh->ha_lock, seq)); +} +
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThe commit message states:
Quoted 4 lines
net/core/filter.c +0
-0
[ ... ]
Quoted 8 lines
net/core/filter.c +4
-0@@ -6297,6 +6297,20 @@ static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {#endif #if IS_ENABLED (CONFIG_INET ) ||IS_ENABLED (CONFIG_IPV6 ) +/* Take a stable snapshot of the neighbour's link layer address. +* neigh_ha_snapshot() can not be used here because it copies dev->addr_len +* bytes while params->dmac is only ETH_ALEN long. +*/
net/core/filter.c static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = { · line 6313
Quoted 10 lines
net/core/filter.c +10
-0+static void bpf_fib_dmac_snapshot (u8 *dmac,const struct neighbour *neigh) +{ +unsigned int seq; + +do { + seq =read_seqbegin (&neigh->ha_lock); +memcpy (dmac, neigh->ha,ETH_ALEN ); + }while (read_seqretry (&neigh->ha_lock, seq)); +} +
The commit message states:
Quoted 4 lines
net/core/filter.c +0
-0neigh_ha_snapshot() cannot be used here because it copies dev->addr_len bytes while params->dmac is only ETH_ALEN long (an IPoIB egress device has addr_len 20 and would overflow into params->smac), so open-code theseqlock loop around the
ETH_ALENcopy.
Is the overflow destination accurate? Looking at struct bpf_fib_lookup in include/uapi/linux/bpf.h, smac precedes dmac in the final union:
union {
struct { __u32 mark; };
struct {
__u8 smac[6]; /* ETH_ALEN */
__u8 dmac[6]; /* ETH_ALEN */
};
};That union is the last member of the struct, so smac sits at offset 52 and dmac at offset 58 in the 64-byte struct.
Calling neigh_ha_snapshot(params->dmac, neigh, dev) with an IPoIB device (addr_len == 20) would write bytes 58..77, which is 6 bytes into dmac and 14 bytes past the end of struct bpf_fib_lookup. It cannot reach params->smac at the lower offset.
Because params points at BPF-program memory that the verifier only sized to sizeof(struct bpf_fib_lookup), the actual consequence is an out-of-bounds write past the program-supplied buffer, not a clobbered sibling field.
The conclusion that neigh_ha_snapshot() is unusable here is correct, and the in-code comment is accurate. Could the changelog's claim about where the overflow lands be corrected before this goes to stable, since the parenthetical is the sole stated justification for open-coding the loop?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/34305501194
Quoted 26 lines · Nikhil
bpf_ipv4_fib_lookup()andbpf_ipv6_fib_lookup()copy the neighbour's link layer address intoparams->dmacwithout any synchronisation, butneigh_update()writesneigh->haunderwrite_seqlock(&neigh->ha_lock)exactly because there are lockless readers. A BPF program callingbpf_fib_lookup()while the neighbour is being updated can therefore be handed a torn address, and XDP/tc then forwards the packet to a bogus L2 destination.
neigh_ha_snapshot()cannot be used here because it copiesdev->addr_lenbytes whileparams->dmacis onlyETH_ALENlong (an IPoIB egress device hasaddr_len20 and would overflow intoparams->smac), so open-code the seqlock loop around theETH_ALENcopy.Same problem and same fix as commit
57549ab90791("net: bridge: arp/nd
proxy: fix reading neigh ha") and commitb824059a673b("vxlan: fix reading neigh ha").Reproduced on
x86_64under qemu: a dummy device holds a permanent neighbour whose lladdr is flipped between aa:aa:aa:aa:aa:aa and bb:bb:bb:bb:bb:bb withRTM_NEWNEIGH, while an XDP program driven byBPF_PROG_TEST_RUNcallsbpf_fib_lookup()in a loop and checks that all six bytes of the returned dmac are equal. Before this patch: 211 torn addresses out of 16300000 lookups (last one aa:aa:aa:aa:bb:bb). After this patch: 0 out of 38060000 lookups.
Quoted 4 lines
Fixes 87f5fc7e48dd ("bpf: Provide helper to do forwarding lookups in kernel FIB table")Signed-off-by Nikhil <[email protected]>---
Bot is right wrt possible lockups, please adjust the seqlock accordingly. Also please
add your full name in the SOB.
pw-bot: cr
Quoted 50 lines
net/core/filter.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)net/core/filter.c +16
-3@@ -6298,6 +6298,20 @@ static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {#endif #if IS_ENABLED (CONFIG_INET ) ||IS_ENABLED (CONFIG_IPV6 ) +/* Take a stable snapshot of the neighbour's link layer address. +* neigh_ha_snapshot() can not be used here because it copies dev->addr_len +* bytes while params->dmac is only ETH_ALEN long. +*/ +static void bpf_fib_dmac_snapshot (u8 *dmac,const struct neighbour *neigh) +{ +unsigned int seq; + +do { + seq =read_seqbegin (&neigh->ha_lock); +memcpy (dmac, neigh->ha,ETH_ALEN ); + }while (read_seqretry (&neigh->ha_lock, seq)); +} +static int bpf_fib_set_fwd_params (struct net_device *dev,struct bpf_fib_lookup *params,u32 flags,u32 mtu,u32 in_ifindex)@@ -6491,7 +6505,7 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,if (!neigh || !(READ_ONCE (neigh->nud_state) &NUD_VALID ))return BPF_FIB_LKUP_RET_NO_NEIGH ; -memcpy (params->dmac, neigh->ha,ETH_ALEN ); +bpf_fib_dmac_snapshot (params->dmac, neigh);memcpy (params->smac, dev->dev_addr,ETH_ALEN ); set_fwd_params:@@ -6644,7 +6658,7 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params, neigh =__ipv6_neigh_lookup_noref (dev, dst);if (!neigh || !(READ_ONCE (neigh->nud_state) &NUD_VALID ))return BPF_FIB_LKUP_RET_NO_NEIGH ; -memcpy (params->dmac, neigh->ha,ETH_ALEN ); +bpf_fib_dmac_snapshot (params->dmac, neigh);memcpy (params->smac, dev->dev_addr,ETH_ALEN ); set_fwd_params: --2.43.0
Quoted 3 lines · Emil Tsalapatis
Bot is right wrt possible lockups, please adjust the seqlock accordingly. Also please
add your full name in the SOB.
Will fix the SOB in v2, thanks.
On the lockup: I don't think it can happen here. neigh->ha_lock has exactly one writer in the tree, __neigh_update() at net/core/neighbour.c:1495, and it already runs with softirqs disabled:write_lock_bh(&neigh->lock) at line 1384 covers it through write_unlock_bh() at line 1524. A reader in softirq context therefore cannot preempt the writer on the same CPU and spin on the sequence count.
The same pattern is already used by the other lockless readers of neigh->ha on the tx path, neigh_resolve_output() and neigh_connected_output() (neighbour.c:1610 and :1639), and by the two recent fixes this patch follows, 57549ab90791 ("net: bridge: arp/nd
proxy: fix reading neigh ha") and b824059a673b ("vxlan: fix reading neigh ha").
Am I missing a writer path? If you would still prefer a different form here, I am happy to respin.
Thanks,
Nikhil
Quoted 6 lines · Nikhil Ludder
Quoted 3 lines · Emil Tsalapatis
Bot is right wrt possible lockups, please adjust the seqlock accordingly. Also please
add your full name in the SOB.Will fix the SOB in v2, thanks.
Quoted 7 lines
On the lockup: I don't think it can happen here.
neigh->ha_lockhas exactly one writer in the tree,__neigh_update()atnet/core/neighbour.c:1495, and it already runs with softirqs disabled:write_lock_bh(&neigh->lock)at line 1384 covers it throughwrite_unlock_bh()at line 1524. A reader in softirq context therefore cannot preempt the writer on the same CPU and spin on the sequence count.
Yes, no deadlock.
BTW, if an IPoIB device can show up here, dmac is already truncated today and
the packet can't be forwarded anyway.
Shouldn't we just reject addr_len != ETH_ALEN instead of open-coding the copy?
Then you can use the native function instead.
Quoted 12 lines
The same pattern is already used by the other lockless readers of
neigh->haon the tx path,neigh_resolve_output()andneigh_connected_output()(neighbour.c:1610 and :1639), and by the two recent fixes this patch follows,57549ab90791("net: bridge: arp/nd
proxy: fix reading neigh ha") andb824059a673b("vxlan: fix reading neigh ha").Am I missing a writer path? If you would still prefer a different form here, I am happy to respin.
Thanks,
Nikhil
Quoted 1 line · Jiayuan Chen
Yes, no deadlock.
Thanks for confirming.
Quoted 4 lines
BTW, if an IPoIB device can show up here, dmac is already truncated today and the packet can't be forwarded anyway.
Shouldn't we just rejectaddr_len!=ETH_ALENinstead of open-coding the copy? Then you can use the native function instead.
You are right, and it is worse than just dmac: the line immediately below copies dev->dev_addr into params->smac with a fixed ETH_ALEN and no addr_len check either, so both addresses are already truncated for such a device. struct bpf_fib_lookup declares smac[6] and dmac[6], so the helper is ethernet-only by contract and a non-ethernet nexthop is already outside it.
I would rather not fold that into this patch though. This one is a race fix with Cc: stable and no behaviour change, whereas rejecting a device that today returns a (garbage) success is uapi visible and does not belong in a stable backport. Would you be happy with the seqlock fix as it stands, and a follow-up for bpf-next that rejects addr_len != ETH_ALEN and covers smac as well? I am happy to write it.
If so, which return code would you want for that? None of the existing BPF_FIB_LKUP_RET_* really fits: NO_NEIGH is untrue since the neighbour is there, NOT_FWDED is vague, and adding a new BPF_FIB_LKUP_RET_* is uapi, which is another reason to keep it out of this patch.
Thanks,
Nikhil
Emil Tsalapatis
reviewerQuoted 4 lines · Nikhil Ludder
Quoted 1 line · Jiayuan Chen
Yes, no deadlock.
Thanks for confirming.
The explanation makes sense, hadn't considered where this is written from. No need to adjust.
Quoted 13 lines
Quoted 5 lines
BTW, if an IPoIB device can show up here, dmac is already truncated today and the packet can't be forwarded anyway.
Shouldn't we just rejectaddr_len!=ETH_ALENinstead of open-coding the copy? Then you can use the native function instead.You are right, and it is worse than just dmac: the line immediately below copies
dev->dev_addrintoparams->smacwith a fixedETH_ALENand noaddr_lencheck either, so both addresses are already truncated for such a device. structbpf_fib_lookupdeclares smac[6] and dmac[6], so the helper is ethernet-only by contract and a non-ethernet nexthop is already outside it.
Quoted 7 lines
I would rather not fold that into this patch though. This one is a race fix with Cc: stable and no behaviour change, whereas rejecting a device that today returns a (garbage) success is uapi visible and does not belong in a stable backport. Would you be happy with the seqlock fix as it stands, and a follow-up for bpf-next that rejects
addr_len!=ETH_ALENand covers smac as well? I am happy to write it.
I think this split makes sense, even if there's the churn of adding the fix then removing it to use the pre-existing helper. It's just a couple lines of temporary duplication. @Jiayuan wdyt?
Quoted 7 lines
If so, which return code would you want for that? None of the existing BPF_FIB_LKUP_RET_* really fits:
NO_NEIGHis untrue since the neighbour is there,NOT_FWDEDis vague, and adding a new BPF_FIB_LKUP_RET_* is uapi, which is another reason to keep it out of this patch.Thanks,
Nikhil