[net-next,v5,1/2] drivers: net: prevent tun_build_skb() to exceed the packet size limit

Message ID 20230803185947.2379988-1-andrew.kanner@gmail.com
State New
Headers
Series [net-next,v5,1/2] drivers: net: prevent tun_build_skb() to exceed the packet size limit |

Commit Message

Andrew Kanner Aug. 3, 2023, 6:59 p.m. UTC
  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 <jasowang@redhat.com> 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().

Fixes: 7df13219d757 ("tun: reserve extra headroom only when XDP is set")
Link: https://syzkaller.appspot.com/bug?extid=f817490f5bd20541b90a
Signed-off-by: Andrew Kanner <andrew.kanner@gmail.com>
---

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 <dsahern@gmail.com>.
    v4: https://lore.kernel.org/all/20230801220710.464-1-andrew.kanner@gmail.com/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 <jasowang@redhat.com> 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
        <jasowang@redhat.com>. Otherwise extra packets will be dropped
        without falling back to tun_alloc_skb(). And in the discussion of
        v3 Jesper Dangaard Brouer <jbrouer@redhat.com> 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/20230725155403.796-1-andrew.kanner@gmail.com/T/
      - attach the forgotten changelog
    v2: https://lore.kernel.org/all/20230725153941.653-1-andrew.kanner@gmail.com/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 <jasowang@redhat.com>
      - 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
        <jasowang@redhat.com>
    v1: https://lore.kernel.org/all/20230724221326.384-1-andrew.kanner@gmail.com/T/

 drivers/net/tun.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

patchwork-bot+netdevbpf@kernel.org Aug. 8, 2023, 2:20 a.m. UTC | #1
Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

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_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 <jasowang@redhat.com> 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().
> 
> [...]

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!
  

Patch

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 25f0191df00b..100339bc8b04 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -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;