The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Cen Zhang (Microsoft)" <blbllhy@gmail.com>
To: pablo@netfilter.org, laforge@gnumonks.org, andrew+netdev@lunn.ch,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com
Cc: horms@kernel.org, anthony.l.nguyen@intel.com,
	wojciech.drewek@intel.com, osmocom-net-gprs@lists.osmocom.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	AutonomousCodeSecurity@microsoft.com, xmei5@asu.edu,
	tgopinath@linux.microsoft.com, kys@microsoft.com,
	blbllhy@gmail.com
Subject: [PATCH net v2 1/2] gtp: fix sk_created publication race in gtp_create_sockets()
Date: Tue, 25 Aug 2026 01:24:03 -0400	[thread overview]
Message-ID: <20260825052404.45665-2-blbllhy@gmail.com> (raw)
In-Reply-To: <20260825052404.45665-1-blbllhy@gmail.com>

In gtp_create_sockets(), gtp->sk_created is set to true before gtp->sk0
and gtp->sk1u are assigned. Without memory ordering guarantees, a
concurrent GTP Echo packet on another CPU can observe sk_created == true
while gtp->sk0 is still NULL, causing a kernel panic.

  KASAN: null-ptr-deref in range [0x0000000000000010-0x0000000000000017]
  RIP: 0010:gtp_encap_recv  (drivers/net/gtp.c:542 gtp0_handle_echo_resp)
  Call Trace:
   <IRQ>
   udp_queue_rcv_one_skb
   ip_protocol_deliver_rcu
   ip_local_deliver
  Kernel panic - not syncing: Fatal exception in interrupt

Use smp_store_release() when publishing sk_created and
smp_load_acquire() on every lockless read. This ensures that all prior
stores (sk0, sk1u assignments) are visible before any reader can observe
the flag as true, on all architectures. Annotate all remaining lockless
sk_created accesses with smp_store_release()/smp_load_acquire().

Suggested-by: Simon Horman <horms@kernel.org>
Fixes: b20dc3c68458 ("gtp: Allow to create GTP device without FDs")
Reported-by: AutonomousCodeSecurity@microsoft.com
Reported-by: Xiang Mei (Microsoft) <xmei5@asu.edu>
Reported-by: Cen Zhang (Microsoft) <blbllhy@gmail.com>
Signed-off-by: Cen Zhang (Microsoft) <blbllhy@gmail.com>
---
v2: Use smp_store_release()/smp_load_acquire() to provide proper memory
    ordering as suggested by Simon Horman.
v1: https://lore.kernel.org/netdev/20260816035205.57966-1-blbllhy@gmail.com/
---
 drivers/net/gtp.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/drivers/net/gtp.c b/drivers/net/gtp.c
index 298efc76a56b..ead519ee18d1 100644
--- a/drivers/net/gtp.c
+++ b/drivers/net/gtp.c
@@ -603,10 +603,12 @@ static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
 	 * there is no daemon running in userspace which would
 	 * handle echo request.
 	 */
-	if (gtp0->type == GTP_ECHO_REQ && gtp->sk_created)
+	/* Pairs with smp_store_release() in gtp_create_sockets(). */
+	if (gtp0->type == GTP_ECHO_REQ && smp_load_acquire(&gtp->sk_created))
 		return gtp0_send_echo_resp(gtp, skb);
 
-	if (gtp0->type == GTP_ECHO_RSP && gtp->sk_created)
+	/* Pairs with smp_store_release() in gtp_create_sockets(). */
+	if (gtp0->type == GTP_ECHO_RSP && smp_load_acquire(&gtp->sk_created))
 		return gtp0_handle_echo_resp(gtp, skb);
 
 	if (gtp0->type != GTP_TPDU)
@@ -811,10 +813,12 @@ static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
 	 * there is no daemon running in userspace which would
 	 * handle echo request.
 	 */
-	if (gtp1->type == GTP_ECHO_REQ && gtp->sk_created)
+	/* Pairs with smp_store_release() in gtp_create_sockets(). */
+	if (gtp1->type == GTP_ECHO_REQ && smp_load_acquire(&gtp->sk_created))
 		return gtp1u_send_echo_resp(gtp, skb);
 
-	if (gtp1->type == GTP_ECHO_RSP && gtp->sk_created)
+	/* Pairs with smp_store_release() in gtp_create_sockets(). */
+	if (gtp1->type == GTP_ECHO_RSP && smp_load_acquire(&gtp->sk_created))
 		return gtp1u_handle_echo_resp(gtp, skb);
 
 	if (gtp1->type != GTP_TPDU)
@@ -894,7 +898,10 @@ static void gtp_encap_disable(struct gtp_dev *gtp)
 	if (gtp->sk_created) {
 		udp_tunnel_sock_release(gtp->sk0);
 		udp_tunnel_sock_release(gtp->sk1u);
-		gtp->sk_created = false;
+		/* Pairs with smp_load_acquire() in the RX and
+		 * genl echo paths.
+		 */
+		smp_store_release(&gtp->sk_created, false);
 		gtp->sk0 = NULL;
 		gtp->sk1u = NULL;
 	} else {
@@ -1462,10 +1469,15 @@ static int gtp_create_sockets(struct gtp_dev *gtp, const struct nlattr *nla,
 		return PTR_ERR(sk1u);
 	}
 
-	gtp->sk_created = true;
 	gtp->sk0 = sk0;
 	gtp->sk1u = sk1u;
 
+	/* Ensure sk0/sk1u are visible before sk_created is set.
+	 * Pairs with smp_load_acquire() in the RX and genl
+	 * echo paths.
+	 */
+	smp_store_release(&gtp->sk_created, true);
+
 	return 0;
 }
 
@@ -2365,7 +2377,10 @@ static int gtp_genl_send_echo_req(struct sk_buff *skb, struct genl_info *info)
 	if (!gtp)
 		return -ENODEV;
 
-	if (!gtp->sk_created)
+	/* Pairs with smp_store_release() in gtp_create_sockets()
+	 * and gtp_encap_disable().
+	 */
+	if (!smp_load_acquire(&gtp->sk_created))
 		return -EOPNOTSUPP;
 	if (!(gtp->dev->flags & IFF_UP))
 		return -ENETDOWN;
-- 
2.55.0

  reply	other threads:[~2026-08-25  5:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-25  5:24 [PATCH net v2 0/2] gtp: fix sk_created race conditions Cen Zhang (Microsoft)
2026-08-25  5:24 ` Cen Zhang (Microsoft) [this message]
2026-08-25  5:24 ` [PATCH net v2 2/2] gtp: fix use-after-free during GTP device teardown Cen Zhang (Microsoft)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260825052404.45665-2-blbllhy@gmail.com \
    --to=blbllhy@gmail.com \
    --cc=AutonomousCodeSecurity@microsoft.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=anthony.l.nguyen@intel.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kys@microsoft.com \
    --cc=laforge@gnumonks.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=osmocom-net-gprs@lists.osmocom.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=tgopinath@linux.microsoft.com \
    --cc=wojciech.drewek@intel.com \
    --cc=xmei5@asu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox