netfilter: nf_queue: hold bridge skb->dev while queued
Subsystems (3)
PATCH 1/1 netfilter: nf_queue: hold bridge skb->dev while queued
In reply to a message not in the archiveFrom: Haoze Xie <[email protected]>
br_pass_frame_up() rewrites skb->dev from the ingress port to the bridge master before queueing bridge LOCAL_IN packets. NFQUEUE only holds references on state.in/out and bridge physdevs, so a queued bridge packet can retain a freed bridge master in skb->dev until reinjection.
When the verdict is reinjected later, br_netif_receive_skb() re-enters the receive path with skb->dev still pointing at the freed bridge master, triggering a use-after-free.
Store skb->dev in the queue entry for bridge builds, hold a reference on it for the queue lifetime, and use the saved device when dropping queued packets during NETDEV_DOWN handling.
--- include/net/netfilter/nf_queue.h | 1 + net/netfilter/nf_queue.c | 5 +++++ net/netfilter/nfnetlink_queue.c | 3 +++ 3 files changed, 9 insertions(+)
include/net/netfilter/nf_queue.h +1 -0
@@ -17,6 +17,7 @@ struct nf_queue_entry {unsigned int id;unsigned int hook_index;/* index in hook_entries->hook[] */ #if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) +struct net_device *skb_dev;struct net_device *physin;struct net_device *physout;#endif
net/netfilter/nf_queue.c +5 -0
@@ -67,6 +67,7 @@ static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)nf_queue_sock_put (state->sk);#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) +dev_put (entry->skb_dev);dev_put (entry->physin);dev_put (entry->physout);#endif
@@ -106,6 +107,7 @@ bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)dev_hold (state->out);#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) +dev_hold (entry->skb_dev);dev_hold (entry->physin);dev_hold (entry->physout);#endif
@@ -207,6 +209,9 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state, .size = sizeof(*entry) + route_key_size, }; +#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) + entry->skb_dev = skb->dev; +#endif __nf_queue_entry_init_physdevs (entry);if (!nf_queue_entry_get_refs (entry)) {
net/netfilter/nfnetlink_queue.c +3 -0
@@ -1206,6 +1206,9 @@ dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )int physinif, physoutif; +if (entry->skb_dev && entry->skb_dev->ifindex == ifindex) +return 1 ; + physinif =nf_bridge_get_physinif (entry->skb); physoutif =nf_bridge_get_physoutif (entry->skb);
Signature · 1 line
2.53.0
Pablo Neira Ayuso
maintainerQuoted 35 lines · Ren Wei
From: Haoze Xie <[email protected]>
br_pass_frame_up()rewritesskb->devfrom the ingress port to the bridge master before queueing bridgeLOCAL_INpackets. NFQUEUE only holds references on state.in/out and bridge physdevs, so a queued bridge packet can retain a freed bridge master inskb->devuntil reinjection.When the verdict is reinjected later,
br_netif_receive_skb()re-enters the receive path withskb->devstill pointing at the freed bridge master, triggering a use-after-free.Store
skb->devin the queue entry for bridge builds, hold a reference on it for the queue lifetime, and use the saved device when dropping queued packets duringNETDEV_DOWNhandling.Fixes ac2863445686 ("netfilter: bridge: add nf_afinfo to enable queuing to userspace")Reported-by Yuan Tan <[email protected]>Reported-by Yifan Wu <[email protected]>Reported-by Juefei Pu <[email protected]>Reported-by Xin Liu <[email protected]>Tested-by Haoze Xie <[email protected]>Signed-off-by Haoze Xie <[email protected]>Signed-off-by Ren Wei <[email protected]>--- include/net/netfilter/nf_queue.h | 1 + net/netfilter/nf_queue.c | 5 +++++ net/netfilter/nfnetlink_queue.c | 3 +++ 3 files changed, 9 insertions(+)include/net/netfilter/nf_queue.h +0
-0
include/net/netfilter/nf_queue.h struct nf_queue_entry { · line 20
Quoted 4 lines
include/net/netfilter/nf_queue.h +1
-0@@ -17,6 +17,7 @@ struct nf_queue_entry {unsigned int id;unsigned int hook_index;/* index in hook_entries->hook[] */ #if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) +struct net_device *skb_dev;
patch is not correct, this is only fixing it for br_netfilter.
include/net/netfilter/nf_queue.h struct nf_queue_entry { · line 23
Quoted 3 lines
include/net/netfilter/nf_queue.h +0
-0struct net_device *physin;struct net_device *physout;#endif
Maybe normalize this special case with this patch instead? I will propose it to the bridge maintainer.
It is strange that skb->dev != indev.
I have to take a second look, but I don't a usecase where skb->dev is used in the netfilter tree can could break.
net/bridge/br_input.c +4 -1
@@ -26,7 +26,11 @@static int br_netif_receive_skb (struct net *net,struct sock *sk,struct sk_buff *skb) { +struct net_device *brdev =BR_INPUT_SKB_CB (skb)->brdev; + + skb->dev = brdev;br_drop_fake_rtable (skb); +return netif_receive_skb (skb); }
@@ -57,7 +61,6 @@ static int br_pass_frame_up(struct sk_buff *skb, bool promisc) } indev = skb->dev; - skb->dev = brdev; skb =br_handle_vlan (br,NULL , vg, skb);if (!skb)return NET_RX_DROP ;
net/bridge/br_vlan.c +2 -1
@@ -482,6 +482,7 @@ struct sk_buff *br_handle_vlan(struct net_bridge *br,struct net_bridge_vlan_group *vg, struct sk_buff *skb) { + struct net_device *brdev = BR_INPUT_SKB_CB(skb)->brdev;struct pcpu_sw_netstats *stats;struct net_bridge_vlan *v;u16 vid;
@@ -502,7 +503,7 @@ struct sk_buff *br_handle_vlan(struct net_bridge *br,* pass the packet as is. */ if (!v || !br_vlan_should_use (v)) { -if ((br->dev->flags &IFF_PROMISC ) && skb->dev == br->dev) { +if ((br->dev->flags &IFF_PROMISC ) && brdev == br->dev) { goto out; }else {kfree_skb (skb);
Pablo Neira Ayuso
maintainerQuoted 47 lines · Pablo Neira Ayuso
Quoted 35 lines · Ren Wei
From: Haoze Xie <[email protected]>
br_pass_frame_up()rewritesskb->devfrom the ingress port to the bridge master before queueing bridgeLOCAL_INpackets. NFQUEUE only holds references on state.in/out and bridge physdevs, so a queued bridge packet can retain a freed bridge master inskb->devuntil reinjection.When the verdict is reinjected later,
br_netif_receive_skb()re-enters the receive path withskb->devstill pointing at the freed bridge master, triggering a use-after-free.Store
skb->devin the queue entry for bridge builds, hold a reference on it for the queue lifetime, and use the saved device when dropping queued packets duringNETDEV_DOWNhandling.Fixes ac2863445686 ("netfilter: bridge: add nf_afinfo to enable queuing to userspace")Reported-by Yuan Tan <[email protected]>Reported-by Yifan Wu <[email protected]>Reported-by Juefei Pu <[email protected]>Reported-by Xin Liu <[email protected]>Tested-by Haoze Xie <[email protected]>Signed-off-by Haoze Xie <[email protected]>Signed-off-by Ren Wei <[email protected]>--- include/net/netfilter/nf_queue.h | 1 + net/netfilter/nf_queue.c | 5 +++++ net/netfilter/nfnetlink_queue.c | 3 +++ 3 files changed, 9 insertions(+)include/net/netfilter/nf_queue.h +0
-0include/net/netfilter/nf_queue.h struct nf_queue_entry { · line 20
Quoted 4 lines
include/net/netfilter/nf_queue.h +1
-0@@ -17,6 +17,7 @@ struct nf_queue_entry {unsigned int id;unsigned int hook_index;/* index in hook_entries->hook[] */ #if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) +struct net_device *skb_dev;patch is not correct, this is only fixing it for
br_netfilter.include/net/netfilter/nf_queue.h struct nf_queue_entry { · line 23
Quoted 3 lines
include/net/netfilter/nf_queue.h +0
-0struct net_device *physin;struct net_device *physout;#endif
Quoted 7 lines
Maybe normalize this special case with this patch instead? I will propose it to the bridge maintainer.
It is strange that
skb->dev!= indev.I have to take a second look, but I don't a usecase where
skb->devis used in the netfilter tree can could break.
Scratch this proposal, it also breaks.
Pablo Neira Ayuso
maintainerQuoted 7 lines · Ren Wei
From: Haoze Xie <[email protected]>
br_pass_frame_up()rewritesskb->devfrom the ingress port to the bridge master before queueing bridgeLOCAL_INpackets. NFQUEUE only holds references on state.in/out and bridge physdevs, so a queued bridge packet can retain a freed bridge master inskb->devuntil reinjection.
Quoted 7 lines
When the verdict is reinjected later,
br_netif_receive_skb()re-enters the receive path withskb->devstill pointing at the freed bridge master, triggering a use-after-free.Store
skb->devin the queue entry for bridge builds, hold a reference on it for the queue lifetime, and use the saved device when dropping queued packets duringNETDEV_DOWNhandling.
Next attempt: Maybe hold reference on skb->dev...
net/netfilter/nf_queue.c +2 -0
@@ -66,6 +66,7 @@ static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)if (state->sk)nf_queue_sock_put (state->sk); +dev_put (entry->skb->dev);#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )dev_put (entry->physin);dev_put (entry->physout);
@@ -104,6 +105,7 @@ bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)dev_hold (state->in);dev_hold (state->out); +dev_hold (entry->skb->dev);#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )dev_hold (entry->physin);
Florian Westphal
maintainerQuoted 31 lines · Pablo Neira Ayuso
Quoted 7 lines · Ren Wei
From: Haoze Xie <[email protected]>
br_pass_frame_up()rewritesskb->devfrom the ingress port to the bridge master before queueing bridgeLOCAL_INpackets. NFQUEUE only holds references on state.in/out and bridge physdevs, so a queued bridge packet can retain a freed bridge master inskb->devuntil reinjection.Quoted 7 lines
When the verdict is reinjected later,
br_netif_receive_skb()re-enters the receive path withskb->devstill pointing at the freed bridge master, triggering a use-after-free.Store
skb->devin the queue entry for bridge builds, hold a reference on it for the queue lifetime, and use the saved device when dropping queued packets duringNETDEV_DOWNhandling.Next attempt: Maybe hold reference on
skb->dev...net/netfilter/nf_queue.c +1
-0@@ -66,6 +66,7 @@ static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)if (state->sk)nf_queue_sock_put (state->sk); +dev_put (entry->skb->dev);#if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )dev_put (entry->physin);dev_put (entry->physout);
net/netfilter/nf_queue.c bool nf_queue_entry_get_refs(struct nf_queue_entry *entry) · line 108
Quoted 4 lines
net/netfilter/nf_queue.c +1
-0@@ -104,6 +105,7 @@ bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)dev_hold (state->in);dev_hold (state->out); +dev_hold (entry->skb->dev);
We should also extend
net/netfilter/nfnetlink_queue.c:dev_cmp() to consider skb->dev, if set.
And I think skb->dev can be NULL here in output path.