Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bridge: fix potential use-after-free when hook returns QUEUE or STOLEN verdict

Zefir Kurtisi reported kernel panic with an openwrt specific patch.
However, it turns out that mainline has a similar bug waiting to happen.

Once NF_HOOK() returns the skb is in undefined state and must not be
used. Moreover, the okfn must consume the skb to support async
processing (NF_QUEUE).

Current okfn in this spot doesn't consume it and caller assumes that
NF_HOOK return value tells us if skb was freed or not, but thats wrong.

It "works" because no in-tree user registers a NFPROTO_BRIDGE hook at
LOCAL_IN that returns STOLEN or NF_QUEUE verdicts.

Once we add NF_QUEUE support for nftables bridge this will break --
NF_QUEUE holds the skb for async processing, caller will erronoulsy
return RX_HANDLER_PASS and on reinject netfilter will access free'd skb.

Fix this by pushing skb up the stack in the okfn instead.

NB: It also seems dubious to use LOCAL_IN while bypassing PRE_ROUTING
completely in this case but this is how its been forever so it seems
preferable to not change this.

Cc: Felix Fietkau <nbd@openwrt.org>
Cc: Zefir Kurtisi <zefir.kurtisi@neratec.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Tested-by: Zefir Kurtisi <zefir.kurtisi@neratec.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Florian Westphal and committed by
David S. Miller
8626c56c 180a2c54

+7 -9
+7 -9
net/bridge/br_input.c
··· 222 222 /* check if vlan is allowed, to avoid spoofing */ 223 223 if (p->flags & BR_LEARNING && br_should_learn(p, skb, &vid)) 224 224 br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false); 225 - return 0; /* process further */ 225 + 226 + BR_INPUT_SKB_CB(skb)->brdev = p->br->dev; 227 + br_pass_frame_up(skb); 228 + return 0; 226 229 } 227 230 228 231 /* ··· 287 284 } 288 285 289 286 /* Deliver packet to local host only */ 290 - if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, 291 - dev_net(skb->dev), NULL, skb, skb->dev, NULL, 292 - br_handle_local_finish)) { 293 - return RX_HANDLER_CONSUMED; /* consumed by filter */ 294 - } else { 295 - *pskb = skb; 296 - return RX_HANDLER_PASS; /* continue processing */ 297 - } 287 + NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, dev_net(skb->dev), 288 + NULL, skb, skb->dev, NULL, br_handle_local_finish); 289 + return RX_HANDLER_CONSUMED; 298 290 } 299 291 300 292 forward: