From patchwork Thu Apr 4 13:11:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13617832 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96FA384FDC for ; Thu, 4 Apr 2024 13:11:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712236290; cv=none; b=K1GOeOfchFNMxi10kncc0r12kTHxPl2BqoFsPUtr6teKGksc9CufG0o6CZ8rQlVJ5ZDLcMalUw3hsN3duZiygshGcxX6VAXoqsxETyggImK+9NMdPcEQBmcgnujUfxu7sV0uj6xeC/eEF066Jj7qvssEsM7JM/0PPMiwKxrdQZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712236290; c=relaxed/simple; bh=2XGETqMTHK2M+McL6wwFeA/m6DDbNuMZCHmcvpzYH0E=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=Repg+wDhdkstTBrfdYGo8D5KpF/J13XzeSiIT18eYmckJt+ut8p2hmTTfSEt4wscPLJYyHvYZdpTm9rqcFfKKvjQB85kXS1djHbJ8mgC6goiRgiZrkCPyiI76zCpCShU4IRDmZ3+tcWSiQ6YcTKAxfla+BXCpGQyLpHEa/yzbvA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RgOMkDyL; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RgOMkDyL" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6156b93c768so18637947b3.1 for ; Thu, 04 Apr 2024 06:11:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712236287; x=1712841087; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=BAn9+1D0RFQ6YM7aZHUb6hUml8LWK/l7TsZudQjysFo=; b=RgOMkDyLAbW3t8k+xEH5JGfq/lfHaA6bln1R6lq6MaoE6hU1u6OsWO4MhvCAjanHbA EMFftVJl3AdqkHSx4JcukVnOTtNgVDY2xRJzJJTP/Pr9H+Fx/XjpvJL2UDIG4MUbVTsQ 5tc5DmT/p4wCEINoC8YtLZdptcC1OGn8RN4iGVvqsLlj+3+Fy9o678zehiIw5aJBTjAm 3Hx61l7fN5DjEHv7eADSzf4Q3L6T/1I6r40xk2XMcISD90ychJBsOrgEkN0zvSZAFI+x PA12y+TYZFmVvUjYpOH5wgxf49ZIODhbf6h93OKVjBENBnnTflQBgyogISeUXKRVgo56 X2xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712236287; x=1712841087; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=BAn9+1D0RFQ6YM7aZHUb6hUml8LWK/l7TsZudQjysFo=; b=LxkPW5Az2OilwN35jFzrRORnt2IgdbMKbJjDexxchJv3yWTHPWY1K522DyZi7TcAgh xlcrIk1t71EKmrct5ypStMCyiTUmN3T6kmnuD0n8aAaTJsBX9Y46jh1E0sM46lpLuMhy 8r5P/9psH5AmEf5Y1fr122PEvb9aK7tub1Bt+bYrR8o3YVWPaKXTGZMxjX6kZJ+IikFF y3havUTIzJAWtXz0m2udP6QgO9F+8amgxnSmTf24MoAW87SGKs5ZMJO1Fa9+tUMcMXy0 UFt1CTf+ugaiKAkwvHqCGKLjA5wrTJJvfpY0cnfNOyrcj3T8badgapdE8eq7akerBALm Hd2g== X-Gm-Message-State: AOJu0YxJvQsjk5uZPE4PDG9aH0QvASn4VmPBywExZiOsyPQljkQVjgi3 0y2ED2TDF1lTpkmYasXIQeWwLjoRZ9Lzw5Xdf7I6Rfe9y39TBJMUlT4draxbw5ph7/AFb9C+wcO H+hfzt30lFg== X-Google-Smtp-Source: AGHT+IFOiT8EmsLEJTxkzq4pGB4GkA4oFCAIrHNmeqvG8Ly0vn8e/TtzMkjcWn95hVi/i7CKS+NC1efmc6jX3Q== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a0d:cc51:0:b0:614:e20c:d3ef with SMTP id o78-20020a0dcc51000000b00614e20cd3efmr661720ywd.10.1712236287662; Thu, 04 Apr 2024 06:11:27 -0700 (PDT) Date: Thu, 4 Apr 2024 13:11:26 +0000 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240404131126.2534400-1-edumazet@google.com> Subject: [PATCH net] geneve: fix header validation in geneve[6]_xmit_skb From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet , syzbot+9ee20ec1de7b3168db09@syzkaller.appspotmail.com, Phillip Potter , Sabrina Dubroca X-Patchwork-Delegate: kuba@kernel.org syzbot is able to trigger an uninit-value in geneve_xmit() [1] Problem : While most ip tunnel helpers (like ip_tunnel_get_dsfield()) uses skb_protocol(skb, true), pskb_inet_may_pull() is only using skb->protocol. If anything else than ETH_P_IPV6 or ETH_P_IP is found in skb->protocol, pskb_inet_may_pull() does nothing at all. If a vlan tag was provided by the caller (af_packet in the syzbot case), the network header might not point to the correct location, and skb linear part could be smaller than expected. Add skb_vlan_inet_prepare() to perform a complete mac validation. Use this in geneve for the moment, I suspect we need to adopt this more broadly. v2,v3 - Addressed Sabrina comments on v1 and v2 Link: https://lore.kernel.org/netdev/Zg1l9L2BNoZWZDZG@hog/ [1] BUG: KMSAN: uninit-value in geneve_xmit_skb drivers/net/geneve.c:910 [inline] BUG: KMSAN: uninit-value in geneve_xmit+0x302d/0x5420 drivers/net/geneve.c:1030 geneve_xmit_skb drivers/net/geneve.c:910 [inline] geneve_xmit+0x302d/0x5420 drivers/net/geneve.c:1030 __netdev_start_xmit include/linux/netdevice.h:4903 [inline] netdev_start_xmit include/linux/netdevice.h:4917 [inline] xmit_one net/core/dev.c:3531 [inline] dev_hard_start_xmit+0x247/0xa20 net/core/dev.c:3547 __dev_queue_xmit+0x348d/0x52c0 net/core/dev.c:4335 dev_queue_xmit include/linux/netdevice.h:3091 [inline] packet_xmit+0x9c/0x6c0 net/packet/af_packet.c:276 packet_snd net/packet/af_packet.c:3081 [inline] packet_sendmsg+0x8bb0/0x9ef0 net/packet/af_packet.c:3113 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg+0x30f/0x380 net/socket.c:745 __sys_sendto+0x685/0x830 net/socket.c:2191 __do_sys_sendto net/socket.c:2203 [inline] __se_sys_sendto net/socket.c:2199 [inline] __x64_sys_sendto+0x125/0x1d0 net/socket.c:2199 do_syscall_64+0xd5/0x1f0 entry_SYSCALL_64_after_hwframe+0x6d/0x75 Uninit was created at: slab_post_alloc_hook mm/slub.c:3804 [inline] slab_alloc_node mm/slub.c:3845 [inline] kmem_cache_alloc_node+0x613/0xc50 mm/slub.c:3888 kmalloc_reserve+0x13d/0x4a0 net/core/skbuff.c:577 __alloc_skb+0x35b/0x7a0 net/core/skbuff.c:668 alloc_skb include/linux/skbuff.h:1318 [inline] alloc_skb_with_frags+0xc8/0xbf0 net/core/skbuff.c:6504 sock_alloc_send_pskb+0xa81/0xbf0 net/core/sock.c:2795 packet_alloc_skb net/packet/af_packet.c:2930 [inline] packet_snd net/packet/af_packet.c:3024 [inline] packet_sendmsg+0x722d/0x9ef0 net/packet/af_packet.c:3113 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg+0x30f/0x380 net/socket.c:745 __sys_sendto+0x685/0x830 net/socket.c:2191 __do_sys_sendto net/socket.c:2203 [inline] __se_sys_sendto net/socket.c:2199 [inline] __x64_sys_sendto+0x125/0x1d0 net/socket.c:2199 do_syscall_64+0xd5/0x1f0 entry_SYSCALL_64_after_hwframe+0x6d/0x75 CPU: 0 PID: 5033 Comm: syz-executor346 Not tainted 6.9.0-rc1-syzkaller-00005-g928a87efa423 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 02/29/2024 Fixes: d13f048dd40e ("net: geneve: modify IP header check in geneve6_xmit_skb and geneve_xmit_skb") Reported-by: syzbot+9ee20ec1de7b3168db09@syzkaller.appspotmail.com Closes: https://lore.kernel.org/netdev/000000000000d19c3a06152f9ee4@google.com/ Signed-off-by: Eric Dumazet Cc: Phillip Potter Cc: Sabrina Dubroca --- drivers/net/geneve.c | 4 ++-- include/net/ip_tunnels.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/drivers/net/geneve.c b/drivers/net/geneve.c index 2f6739fe78af2e8e90c0a3b474c2e99c83e02994..6c2835086b57eacbcddb44a3c507e26d5a944427 100644 --- a/drivers/net/geneve.c +++ b/drivers/net/geneve.c @@ -822,7 +822,7 @@ static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, __be16 sport; int err; - if (!pskb_inet_may_pull(skb)) + if (!skb_vlan_inet_prepare(skb)) return -EINVAL; if (!gs4) @@ -929,7 +929,7 @@ static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, __be16 sport; int err; - if (!pskb_inet_may_pull(skb)) + if (!skb_vlan_inet_prepare(skb)) return -EINVAL; if (!gs6) diff --git a/include/net/ip_tunnels.h b/include/net/ip_tunnels.h index 5cd64bb2104df389250fb3c518ba00a3826c53f7..6898d0b8306f803c57065f6e134f96dcc8517cad 100644 --- a/include/net/ip_tunnels.h +++ b/include/net/ip_tunnels.h @@ -361,6 +361,41 @@ static inline bool pskb_inet_may_pull(struct sk_buff *skb) return pskb_network_may_pull(skb, nhlen); } +/* Variant of pskb_inet_may_pull(). + */ +static inline bool skb_vlan_inet_prepare(struct sk_buff *skb) +{ + int nhlen, maclen; + __be16 type; + + /* Essentially this is skb_protocol(skb, true) + * And we get MAC len. + */ + type = __vlan_get_protocol(skb, skb->protocol, &maclen); + + switch (type) { +#if IS_ENABLED(CONFIG_IPV6) + case htons(ETH_P_IPV6): + nhlen = sizeof(struct ipv6hdr); + break; +#endif + case htons(ETH_P_IP): + nhlen = sizeof(struct iphdr); + break; + + default: + nhlen = 0; + } + /* For ETH_P_IPV6/ETH_P_IP we make sure to pull + * a base network header in skb->head. + */ + if (!pskb_may_pull(skb, maclen + nhlen)) + return false; + + skb_set_network_header(skb, maclen); + return true; +} + static inline int ip_encap_hlen(struct ip_tunnel_encap *e) { const struct ip_tunnel_encap_ops *ops;