Skip to content

Commit 5f77ddb

Browse files
Qing Minggregkh
authored andcommitted
gtp: serialize PDP context updates
commit 498386b upstream. 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 simple 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 gtp_pdp_add(), gtp_genl_del_pdp(), and gtp_dellink() with a shared mutex. Keep the mutex held until the final use of a PDP context in the NEWPDP path, and keep the RCU read-side section around the complete PDP context use in the DELPDP path. Fixes: 459aa66 ("gtp: add initial driver for datapath of GPRS Tunneling Protocol (GTP-U)") Cc: stable@vger.kernel.org Signed-off-by: Qing Ming <a0yami@mailbox.org> Link: https://patch.msgid.link/20260818150000.7670-1-a0yami@mailbox.org Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b7f10d4 commit 5f77ddb

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

drivers/net/gtp.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1313

1414
#include <linux/module.h>
15+
#include <linux/mutex.h>
1516
#include <linux/skbuff.h>
1617
#include <linux/udp.h>
1718
#include <linux/rculist.h>
@@ -105,6 +106,7 @@ struct gtp_net {
105106
};
106107

107108
static u32 gtp_h_initval;
109+
static DEFINE_MUTEX(gtp_pdp_lock);
108110

109111
static struct genl_family gtp_genl_family;
110112

@@ -148,7 +150,8 @@ static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid, u16 family)
148150

149151
head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
150152

151-
hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
153+
hlist_for_each_entry_rcu(pdp, head, hlist_tid,
154+
lockdep_is_held(&gtp_pdp_lock)) {
152155
if (pdp->af == family &&
153156
pdp->gtp_version == GTP_V0 &&
154157
pdp->u.v0.tid == tid)
@@ -165,7 +168,8 @@ static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid, u16 family)
165168

166169
head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
167170

168-
hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
171+
hlist_for_each_entry_rcu(pdp, head, hlist_tid,
172+
lockdep_is_held(&gtp_pdp_lock)) {
169173
if (pdp->af == family &&
170174
pdp->gtp_version == GTP_V1 &&
171175
pdp->u.v1.i_tei == tid)
@@ -182,7 +186,8 @@ static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
182186

183187
head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
184188

185-
hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
189+
hlist_for_each_entry_rcu(pdp, head, hlist_addr,
190+
lockdep_is_held(&gtp_pdp_lock)) {
186191
if (pdp->af == AF_INET &&
187192
pdp->ms.addr.s_addr == ms_addr)
188193
return pdp;
@@ -217,7 +222,8 @@ static struct pdp_ctx *ipv6_pdp_find(struct gtp_dev *gtp,
217222

218223
head = &gtp->addr_hash[ipv6_hashfn(ms_addr) % gtp->hash_size];
219224

220-
hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
225+
hlist_for_each_entry_rcu(pdp, head, hlist_addr,
226+
lockdep_is_held(&gtp_pdp_lock)) {
221227
if (pdp->af == AF_INET6 &&
222228
ipv6_pdp_addr_equal(&pdp->ms.addr6, ms_addr))
223229
return pdp;
@@ -1550,9 +1556,11 @@ static void gtp_dellink(struct net_device *dev, struct list_head *head)
15501556
struct pdp_ctx *pctx;
15511557
int i;
15521558

1559+
mutex_lock(&gtp_pdp_lock);
15531560
for (i = 0; i < gtp->hash_size; i++)
15541561
hlist_for_each_entry_safe(pctx, next, &gtp->tid_hash[i], hlist_tid)
15551562
pdp_context_delete(pctx);
1563+
mutex_unlock(&gtp_pdp_lock);
15561564

15571565
list_del(&gtp->list);
15581566
unregister_netdevice_queue(dev, head);
@@ -2051,13 +2059,15 @@ static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
20512059
goto out_unlock;
20522060
}
20532061

2062+
mutex_lock(&gtp_pdp_lock);
20542063
pctx = gtp_pdp_add(gtp, sk, info);
20552064
if (IS_ERR(pctx)) {
20562065
err = PTR_ERR(pctx);
20572066
} else {
20582067
gtp_tunnel_notify(pctx, GTP_CMD_NEWPDP, GFP_KERNEL);
20592068
err = 0;
20602069
}
2070+
mutex_unlock(&gtp_pdp_lock);
20612071

20622072
out_unlock:
20632073
rtnl_unlock();
@@ -2135,6 +2145,8 @@ static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
21352145
if (!info->attrs[GTPA_VERSION])
21362146
return -EINVAL;
21372147

2148+
mutex_lock(&gtp_pdp_lock);
2149+
21382150
rcu_read_lock();
21392151

21402152
pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
@@ -2155,6 +2167,7 @@ static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
21552167

21562168
out_unlock:
21572169
rcu_read_unlock();
2170+
mutex_unlock(&gtp_pdp_lock);
21582171
return err;
21592172
}
21602173

0 commit comments

Comments
 (0)