drivers: net: prevent tun_build_skb() to exceed the packet size limit
Subsystems (5)
PATCH v5 1/2 drivers: net: prevent tun_build_skb() to exceed the packet size limit
Using the syzkaller repro with reduced packet size it was discovered that XDP_PACKET_HEADROOM is not checked in tun_can_build_skb(), although pad may be incremented in tun_build_skb(). This may end up with exceeding the PAGE_SIZE limit in tun_build_skb().
Jason Wang <[email protected]> proposed to count XDP_PACKET_HEADROOM always (e.g. without rcu_access_pointer(tun->xdp_prog)) in tun_can_build_skb() since there's a window during which XDP program might be attached between tun_can_build_skb() and tun_build_skb().
---
Notes (akanner):
v5:
- always count XDP_PACKET_HEADROOM in tun_can_build_skb() as there's a window between tun_can_build_skb() and tun_build_skb() and XDP program might be attached there.
- rcu_read_lock/unlock() for tun->xdp_prog were completely removed and there's no need to use rcu_access_pointer() instead which was noted by David Ahern <[email protected]>.
v4: https://lore.kernel.org/all/[email protected]/T/
- fall back to v1, fixing only missing XDP_PACKET_HEADROOM in pad and removing bpf_xdp_adjust_tail() check for frame_sz.
- added rcu read lock, noted by Jason Wang <[email protected]> in v1
- I decided to leave the packet length check in tun_can_build_skb() instead of moving to tun_build_skb() suggested by Jason Wang <[email protected]>. Otherwise extra packets will be dropped without falling back to tun_alloc_skb(). And in the discussion of v3 Jesper Dangaard Brouer <[email protected]> noticed that XDP is ok with a higher order pages if it's a contiguous physical memory allocation, so falling to tun_alloc_skb() -> do_xdp_generic() should be ok.
v3: https://lore.kernel.org/all/[email protected]/T/
- attach the forgotten changelog
v2: https://lore.kernel.org/all/[email protected]/T/
- merged 2 patches in 1, fixing both issues: WARN_ON_ONCE with syzkaller repro and missing XDP_PACKET_HEADROOM in pad
- changed the title and description of the execution path, suggested by Jason Wang <[email protected]>
- move the limit check from tun_can_build_skb() to tun_build_skb() to remove duplication and locking issue, and also drop the packet in case of a failed check - noted by Jason Wang <[email protected]>
v1: https://lore.kernel.org/all/[email protected]/T/
drivers/net/tun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/tun.c +1 -1
@@ -1594,7 +1594,7 @@ static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,if (zerocopy)return false; -if (SKB_DATA_ALIGN (len +TUN_RX_PAD ) + +if (SKB_DATA_ALIGN (len +TUN_RX_PAD +XDP_PACKET_HEADROOM ) +SKB_DATA_ALIGN (sizeof (struct skb_shared_info )) >PAGE_SIZE )return false;
Signature · 1 line
2.39.3
PATCH v5 2/2 net: core: remove unnecessary frame_sz check in bpf_xdp_adjust_tail()
Syzkaller reported the following issue:
=======================================
Too BIG xdp->frame_sz = 131072
WARNING: CPU: 0 PID: 5020 at net/core/filter.c:4121 ____bpf_xdp_adjust_tail net/core/filter.c:4121 [inline] WARNING: CPU: 0 PID: 5020 at net/core/filter.c:4121 bpf_xdp_adjust_tail+0x466/0xa10 net/core/filter.c:4103 ... Call Trace: <TASK> bpf_prog_4add87e5301a4105+0x1a/0x1c __bpf_prog_run include/linux/filter.h:600 [inline] bpf_prog_run_xdp include/linux/filter.h:775 [inline] bpf_prog_run_generic_xdp+0x57e/0x11e0 net/core/dev.c:4721 netif_receive_generic_xdp net/core/dev.c:4807 [inline] do_xdp_generic+0x35c/0x770 net/core/dev.c:4866 tun_get_user+0x2340/0x3ca0 drivers/net/tun.c:1919 tun_chr_write_iter+0xe8/0x210 drivers/net/tun.c:2043 call_write_iter include/linux/fs.h:1871 [inline] new_sync_write fs/read_write.c:491 [inline] vfs_write+0x650/0xe40 fs/read_write.c:584 ksys_write+0x12f/0x250 fs/read_write.c:637 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x38/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd
xdp->frame_sz > PAGE_SIZE check was introduced in commit c8741e2bfe87 ("xdp: Allow bpf_xdp_adjust_tail() to grow packet size"). But Jesper Dangaard Brouer <[email protected]> noted that after introducing the xdp_init_buff() which all XDP driver use - it's safe to remove this check. The original intend was to catch cases where XDP drivers have not been updated to use xdp.frame_sz, but that is not longer a concern (since xdp_init_buff).
Running the initial syzkaller repro it was discovered that the contiguous physical memory allocation is used for both xdp paths in tun_get_user(), e.g. tun_build_skb() and tun_alloc_skb(). It was also stated by Jesper Dangaard Brouer <[email protected]> that XDP can work on higher order pages, as long as this is contiguous physical memory (e.g. a page).
---
Notes (akanner):
v5:
- same as v4, but cc-ed [email protected] according to v3->v4
change
v4: https://lore.kernel.org/all/[email protected]/T/
- remove bpf_xdp_adjust_tail() check for frame_sz instead.
v3: https://lore.kernel.org/all/[email protected]/T/
v2: https://lore.kernel.org/all/[email protected]/T/
v1: https://lore.kernel.org/all/[email protected]/T/
- initial attempts to fix drivers/net/tun.c:tun_get_user(),
e.g. tun_build_skb() or tun_alloc_skb(), to not exceed
xdp->frame_sz check from net/core/filter.cnet/core/filter.c | 6 ------ 1 file changed, 6 deletions(-)
net/core/filter.c +0 -6
@@ -4116,12 +4116,6 @@ BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)if (unlikely (data_end > data_hard_end))return -EINVAL ; -/* ALL drivers MUST init xdp->frame_sz, chicken check below */ -if (unlikely (xdp->frame_sz >PAGE_SIZE )) { -WARN_ONCE (1 ,"Too BIG xdp->frame_sz = %d\n" , xdp->frame_sz); -return -EINVAL ; - } -if (unlikely (data_end < xdp->data +ETH_HLEN ))return -EINVAL ;
Signature · 6 lines
2.39.3 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Thu, 3 Aug 2023 20:59:48 +0200 you wrote:
Using the syzkaller repro with reduced packet size it was discovered that
XDP_PACKET_HEADROOMis not checked intun_can_build_skb(), although pad may be incremented intun_build_skb(). This may end up with exceeding thePAGE_SIZElimit intun_build_skb().Jason Wang <[email protected]> proposed to count
XDP_PACKET_HEADROOMalways (e.g. withoutrcu_access_pointer(tun->xdp_prog)) intun_can_build_skb()since there's a window during which XDP program might be attached betweentun_can_build_skb()andtun_build_skb().[...]
Here is the summary with links:
- [net-next,v5,1/2] drivers: net: prevent tun_build_skb() to exceed the packet size limit
https://git.kernel.org/netdev/net/c/59eeb2329405 - [net-next,v5,2/2] net: core: remove unnecessary frame_sz check in bpf_xdp_adjust_tail() https://git.kernel.org/netdev/net/c/d14eea09edf4
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html