Skip to contentSkip to conversation

gtp: serialize PDP context updates

PATCH Superseded Qing Ming 3 participants 5 messages Aug 11 – Aug 13

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Qing Ming

author
#0 lore raw

PATCH v3 1/1 gtp: serialize PDP context updates

PDP contexts can be deleted through GTP_CMD_DELPDP or while the GTP network device is being unregistered. The latter is serialized by RTNL, but the generic-netlink delete path only holds RCU.

Running both paths concurrently can therefore make both paths delete the same PDP context. The issue was found through static analysis and reproduced on a KASAN-enabled kernel by a two-thread program racing GTP_CMD_DELPDP against RTM_DELLINK:

  Oops: general protection fault, probably for non-canonical address
  KASAN: maybe wild-memory-access in range
         [0xdead000000000120-0xdead000000000127]
  RIP: gtp_genl_del_pdp+0x1c1/0x420 [gtp]
  RBP: dead000000000122

The second deletion dereferenced the poisoned hlist pprev pointer.

Serialize PDP context updates with a mutex shared by gtp_pdp_add(), gtp_genl_del_pdp(), and gtp_dellink().

Fixes 459aa660eb1d ("gtp: add initial driver for datapath of GPRS Tunneling Protocol (GTP-U)")
Assisted-by Codex:gpt-5

---
v3:
- Rebase onto net/main.

v2: https://lore.kernel.org/netdev/[email protected]/
- Use a dedicated mutex instead of RTNL.
- Protect PDP add, delete, and link teardown with the same mutex.

 drivers/net/gtp.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/gtp.c +9 -0
index 9a12cc53da00..8ee516debeed 100644
@@ -12,6 +12,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/skbuff.h>
 #include <linux/udp.h>
 #include <linux/rculist.h>
@@ -108,6 +109,7 @@ struct gtp_net {
 };
 
 static u32 gtp_h_initval;
+static DEFINE_MUTEX(gtp_pdp_lock);
 
 static struct genl_family gtp_genl_family;
 
@@ -1555,9 +1557,11 @@ static void gtp_dellink(struct net_device *dev, struct list_head *head)
 	struct pdp_ctx *pctx;
 	int i;
 
+	mutex_lock(&gtp_pdp_lock);
 	for (i = 0; i < gtp->hash_size; i++)
 		hlist_for_each_entry_safe(pctx, next, &gtp->tid_hash[i], hlist_tid)
 			pdp_context_delete(pctx);
+	mutex_unlock(&gtp_pdp_lock);
 
 	list_del(&gtp->list);
 	unregister_netdevice_queue(dev, head);
@@ -1833,6 +1837,8 @@ static struct pdp_ctx *gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
 	__be32 ms_addr;
 	int family;
 
+	guard(mutex)(&gtp_pdp_lock);
+
 	version = nla_get_u32(info->attrs[GTPA_VERSION]);
 
 	family = nla_get_u8_default(info->attrs[GTPA_FAMILY], AF_INET);
@@ -2134,6 +2140,8 @@ static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
 	if (!info->attrs[GTPA_VERSION])
 		return -EINVAL;
 
+	mutex_lock(&gtp_pdp_lock);
+
 	rcu_read_lock();
 
 	pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
@@ -2154,6 +2162,7 @@ static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
 
 out_unlock:
 	rcu_read_unlock();
+	mutex_unlock(&gtp_pdp_lock);
 	return err;
 }
 
Signature · 1 line
2.53.0
#1 lore raw

From: Qing Ming <[email protected]>
Date: Tue, 11 Aug 2026 10:20:12 +0800

Quoted 67 lines

PDP contexts can be deleted through GTP_CMD_DELPDP or while the GTP network device is being unregistered. The latter is serialized by RTNL, but the generic-netlink delete path only holds RCU.

Running both paths concurrently can therefore make both paths delete the same PDP context. The issue was found through static analysis and reproduced on a KASAN-enabled kernel by a two-thread program racing GTP_CMD_DELPDP against RTM_DELLINK:

  Oops: general protection fault, probably for non-canonical address
  KASAN: maybe wild-memory-access in range
         [0xdead000000000120-0xdead000000000127]
  RIP: gtp_genl_del_pdp+0x1c1/0x420 [gtp]
  RBP: dead000000000122

The second deletion dereferenced the poisoned hlist pprev pointer.

Serialize PDP context updates with a mutex shared by gtp_pdp_add(), gtp_genl_del_pdp(), and gtp_dellink().

Fixes 459aa660eb1d ("gtp: add initial driver for datapath of GPRS Tunneling Protocol (GTP-U)")
Assisted-by Codex:gpt-5

---
v3:
- Rebase onto net/main.

v2: https://lore.kernel.org/netdev/[email protected]/
- Use a dedicated mutex instead of RTNL.
- Protect PDP add, delete, and link teardown with the same mutex.

 drivers/net/gtp.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/gtp.c +4 -0
index 9a12cc53da00..8ee516debeed 100644
@@ -12,6 +12,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/skbuff.h>
 #include <linux/udp.h>
 #include <linux/rculist.h>
@@ -108,6 +109,7 @@ struct gtp_net {
 };
 
 static u32 gtp_h_initval;
+static DEFINE_MUTEX(gtp_pdp_lock);
 
 static struct genl_family gtp_genl_family;
 
@@ -1555,9 +1557,11 @@ static void gtp_dellink(struct net_device *dev, struct list_head *head)
 	struct pdp_ctx *pctx;
 	int i;
 
+	mutex_lock(&gtp_pdp_lock);
 	for (i = 0; i < gtp->hash_size; i++)
 		hlist_for_each_entry_safe(pctx, next, &gtp->tid_hash[i], hlist_tid)
 			pdp_context_delete(pctx);
+	mutex_unlock(&gtp_pdp_lock);
 
 	list_del(&gtp->list);
 	unregister_netdevice_queue(dev, head);

drivers/net/gtp.c static struct pdp_ctx *gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk, · line 1841

Quoted 5 lines
drivers/net/gtp.c +2 -0
@@ -1833,6 +1837,8 @@ static struct pdp_ctx *gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
 	__be32 ms_addr;
 	int family;
 
+	guard(mutex)(&gtp_pdp_lock);
+

Please use the plain mutex_lock() / mutex_unlock().

---8<---

$ cat Documentation/process/maintainer-netdev.rst
...
Using device-managed and cleanup.h constructs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Netdev remains skeptical about promises of all "auto-cleanup" APIs, including even ``devm_`` helpers, historically. They are not the preferred style of implementation, merely an acceptable one.

Use of ``guard()`` is discouraged within any function longer than 20 lines, ``scoped_guard()`` is considered more readable. Using normal lock/unlock is still (weakly) preferred.
---8<---

#2 lore raw
In reply to Qing Ming
Quoted 38 lines · Qing Ming

PDP contexts can be deleted through GTP_CMD_DELPDP or while the GTP network device is being unregistered. The latter is serialized by RTNL, but the generic-netlink delete path only holds RCU.

Running both paths concurrently can therefore make both paths delete the same PDP context. The issue was found through static analysis and reproduced on a KASAN-enabled kernel by a two-thread program racing GTP_CMD_DELPDP against RTM_DELLINK:

  Oops: general protection fault, probably for non-canonical address
  KASAN: maybe wild-memory-access in range
         [0xdead000000000120-0xdead000000000127]
  RIP: gtp_genl_del_pdp+0x1c1/0x420 [gtp]
  RBP: dead000000000122

The second deletion dereferenced the poisoned hlist pprev pointer.

Serialize PDP context updates with a mutex shared by gtp_pdp_add(), gtp_genl_del_pdp(), and gtp_dellink().

Fixes 459aa660eb1d ("gtp: add initial driver for datapath of GPRS Tunneling Protocol (GTP-U)")
Assisted-by Codex:gpt-5

---
v3:
- Rebase onto net/main.

v2: https://lore.kernel.org/netdev/[email protected]/
- Use a dedicated mutex instead of RTNL.
- Protect PDP add, delete, and link teardown with the same mutex.

 drivers/net/gtp.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/gtp.c +0 -0
index 9a12cc53da00..8ee516debeed 100644

[...]

drivers/net/gtp.c static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info) · line 2145

Quoted 7 lines
drivers/net/gtp.c +2 -0
@@ -2134,6 +2140,8 @@ static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
 	if (!info->attrs[GTPA_VERSION])
 		return -EINVAL;
 
+	mutex_lock(&gtp_pdp_lock);
+
 	rcu_read_lock();

This rcu_read_lock() can go away after adding this new mutex.

drivers/net/gtp.c static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info) · line 2164

Quoted 5 lines
drivers/net/gtp.c +0 -0
 	pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
@@ -2154,6 +2162,7 @@ static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
 
 out_unlock:
 	rcu_read_unlock();

This can go away too, for gtp_genl_del_pdp(), the mutex replaces this incorrect rcu read size lock.

Thanks.

Quoted 7 lines
drivers/net/gtp.c +1 -0
+	mutex_unlock(&gtp_pdp_lock);
 	return err;
 }
 
Signature · 1 line
2.53.0
author
#3 lore raw
Quoted 7 lines · 在 2026/08/11 17:20 CST Pablo Neira Ayuso <[email protected]> 写道:

This can go away too, for gtp_genl_del_pdp(), the mutex replaces this incorrect rcu read size lock.

Thanks.

Quoted 7 lines
Quoted 7 lines
drivers/net/gtp.c +1 -0
+	mutex_unlock(&gtp_pdp_lock);
 	return err;
 }
Signature · 2 lines
-- 
2.53.0

gtp_find_pdp() calls gtp_find_dev(), which uses dev_get_by_index_rcu(). Should I keep a narrow RCU read-side section around gtp_find_pdp(), while using the mutex to protect the PDP context?

Thanks,
Qing

#4 lore raw
Quoted 11 lines · Ming Qing
Quoted 11 lines · 在 2026/08/11 17:20 CST Pablo Neira Ayuso <[email protected]> 写道:

This can go away too, for gtp_genl_del_pdp(), the mutex replaces this incorrect rcu read size lock.

Thanks.

Quoted 4 lines
drivers/net/gtp.c +1 -0
+	mutex_unlock(&gtp_pdp_lock);
 	return err;
 }
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Signature · 1 line
2.53.0

gtp_find_pdp() calls gtp_find_dev(), which uses dev_get_by_index_rcu(). Should I keep a narrow RCU read-side section around gtp_find_pdp(), while using the mutex to protect the PDP context?

Indeed, I forgot about gtp_dev uses rcu. Thanks for explaining.