Skip to contentSkip to conversation

RDMA/rxe: Fix Use-After-Free problem in rxe_net_del

PATCH Superseded Zhu Yanjun 2 participants 3 messages May 17 – May 18

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Zhu Yanjun

author
#0 lore raw

PATCH 1/1 RDMA/rxe: Fix Use-After-Free problem in rxe_net_del

syzbot reported a general protection fault (KASAN: null-ptr-deref) in kernel_sock_shutdown() called during the software RoCE (rxe) link deletion path (rxe_dellink -> rxe_net_del).

The root cause is a TOCTOU (Time-of-Check to Time-of-Use) race condition in rxe_net_del(). Previously, the function fetched the socket pointer via rxe_ns_pernet_sk4/6() outside the critical section, and then acquired the lock to release it via rxe_sock_put().

In a highly concurrent teardown environment, another thread could close and clear the pernet socket after it was fetched but before the lock was acquired. This causes rxe_sock_put() to operate on a dangling or already cleared socket pointer, leading to a NULL pointer dereference when kernel_sock_shutdown() attempts to access sock->sk.

Fix this by introducing a dedicated, per-netns mutex 'release_lock' and extending its scope. The socket pointers are now fetched, checked, and released entirely within the same locked critical section. This ensures the atomicity of the socket lookup and teardown sequence.

Reported-by [email protected]
Fixes f1327abd6abe ("RDMA/rxe: Support RDMA link creation and destruction per net namespace")
---
 drivers/infiniband/sw/rxe/rxe_net.c |  4 ++++
 drivers/infiniband/sw/rxe/rxe_ns.c  | 22 ++++++++++++++++++++++
 drivers/infiniband/sw/rxe/rxe_ns.h  |  3 +++
 3 files changed, 29 insertions(+)
drivers/infiniband/sw/rxe/rxe_net.c +4 -0
index 50a2cb5405e2..b689ba085da4 100644
@@ -655,6 +655,8 @@ void rxe_net_del(struct ib_device *dev)
 
 	net = dev_net(ndev);
 
+	rxe_ns_lock(net);
+
 	sk = rxe_ns_pernet_sk4(net);
 	if (sk)
 		rxe_sock_put(sk, rxe_ns_pernet_set_sk4, net);
@@ -663,6 +665,8 @@ void rxe_net_del(struct ib_device *dev)
 	if (sk)
 		rxe_sock_put(sk, rxe_ns_pernet_set_sk6, net);
 
+	rxe_ns_unlock(net);
+
 	dev_put(ndev);
 }
 
drivers/infiniband/sw/rxe/rxe_ns.c +22 -0
index 8b9d734229b2..799a727bc1fe 100644
@@ -16,6 +16,7 @@
 struct rxe_ns_sock {
 	struct sock __rcu *rxe_sk4;
 	struct sock __rcu *rxe_sk6;
+	struct mutex	release_lock;
 };
 
 /*
@@ -31,10 +32,26 @@ static int rxe_ns_init(struct net *net)
 	/* defer socket create in the namespace to the first
 	 * device create.
 	 */
+	struct rxe_ns_sock *ns_sk = net_generic(net, rxe_pernet_id);
 
+	mutex_init(&ns_sk->release_lock);
 	return 0;
 }
 
+void rxe_ns_lock(struct net *net)
+{
+	struct rxe_ns_sock *ns_sk = net_generic(net, rxe_pernet_id);
+
+	mutex_lock(&ns_sk->release_lock);
+}
+
+void rxe_ns_unlock(struct net *net)
+{
+	struct rxe_ns_sock *ns_sk = net_generic(net, rxe_pernet_id);
+
+	mutex_unlock(&ns_sk->release_lock);
+}
+
 static void rxe_ns_exit(struct net *net)
 {
 	/* called when the network namespace is removed
@@ -42,6 +59,7 @@ static void rxe_ns_exit(struct net *net)
 	struct rxe_ns_sock *ns_sk = net_generic(net, rxe_pernet_id);
 	struct sock *sk;
 
+	rxe_ns_lock(net);
 	rcu_read_lock();
 	sk = rcu_dereference(ns_sk->rxe_sk4);
 	rcu_read_unlock();
@@ -59,6 +77,10 @@ static void rxe_ns_exit(struct net *net)
 		udp_tunnel_sock_release(sk->sk_socket);
 	}
 #endif
+
+	rxe_ns_unlock(net);
+
+	mutex_destroy(&ns_sk->release_lock);
 }
 
 /*
drivers/infiniband/sw/rxe/rxe_ns.h +3 -0
index 4da2709e6b71..e6cc6b5a4806 100644
@@ -20,6 +20,9 @@ static inline void rxe_ns_pernet_set_sk6(struct net *net, struct sock *sk)
 }
 #endif /* IPv6 */
 
+void rxe_ns_lock(struct net *net);
+void rxe_ns_unlock(struct net *net);
+
 int rxe_namespace_init(void);
 void rxe_namespace_exit(void);
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 57 lines · Zhu Yanjun

syzbot reported a general protection fault (KASAN: null-ptr-deref) in kernel_sock_shutdown() called during the software RoCE (rxe) link deletion path (rxe_dellink -> rxe_net_del).

The root cause is a TOCTOU (Time-of-Check to Time-of-Use) race condition in rxe_net_del(). Previously, the function fetched the socket pointer via rxe_ns_pernet_sk4/6() outside the critical section, and then acquired the lock to release it via rxe_sock_put().

In a highly concurrent teardown environment, another thread could close and clear the pernet socket after it was fetched but before the lock was acquired. This causes rxe_sock_put() to operate on a dangling or already cleared socket pointer, leading to a NULL pointer dereference when kernel_sock_shutdown() attempts to access sock->sk.

Fix this by introducing a dedicated, per-netns mutex 'release_lock' and extending its scope. The socket pointers are now fetched, checked, and released entirely within the same locked critical section. This ensures the atomicity of the socket lookup and teardown sequence.

Reported-by [email protected]
Fixes f1327abd6abe ("RDMA/rxe: Support RDMA link creation and destruction per net namespace")
---
 drivers/infiniband/sw/rxe/rxe_net.c |  4 ++++
 drivers/infiniband/sw/rxe/rxe_ns.c  | 22 ++++++++++++++++++++++
 drivers/infiniband/sw/rxe/rxe_ns.h  |  3 +++
 3 files changed, 29 insertions(+)
drivers/infiniband/sw/rxe/rxe_net.c +4 -0
index 50a2cb5405e2..b689ba085da4 100644
@@ -655,6 +655,8 @@ void rxe_net_del(struct ib_device *dev)
 
 	net = dev_net(ndev);
 
+	rxe_ns_lock(net);
+
 	sk = rxe_ns_pernet_sk4(net);
 	if (sk)
 		rxe_sock_put(sk, rxe_ns_pernet_set_sk4, net);
@@ -663,6 +665,8 @@ void rxe_net_del(struct ib_device *dev)
 	if (sk)
 		rxe_sock_put(sk, rxe_ns_pernet_set_sk6, net);
 
+	rxe_ns_unlock(net);
+
 	dev_put(ndev);
 }
 
drivers/infiniband/sw/rxe/rxe_ns.c +0 -0
index 8b9d734229b2..799a727bc1fe 100644

drivers/infiniband/sw/rxe/rxe_ns.c line 19

Quoted 4 lines
drivers/infiniband/sw/rxe/rxe_ns.c +1 -0
@@ -16,6 +16,7 @@
 struct rxe_ns_sock {
 	struct sock __rcu *rxe_sk4;
 	struct sock __rcu *rxe_sk6;
+	struct mutex	release_lock;

This change renders the existing rcu_read_lock() and rcu_read_unlock() calls unnecessary.

Thanks

author
#2 lore raw
Quoted 15 lines · Leon Romanovsky
Quoted 14 lines · Zhu Yanjun
drivers/infiniband/sw/rxe/rxe_net.c +4 -0
+	rxe_ns_lock(net);
+
  	sk = rxe_ns_pernet_sk4(net);
  	if (sk)
  		rxe_sock_put(sk, rxe_ns_pernet_set_sk4, net);
@@ -663,6 +665,8 @@ void rxe_net_del(struct ib_device *dev)
  	if (sk)
  		rxe_sock_put(sk, rxe_ns_pernet_set_sk6, net);
 
+	rxe_ns_unlock(net);
+
  	dev_put(ndev);
  }
 
Quoted 12 lines

drivers/infiniband/sw/rxe/rxe_ns.c line 19

Quoted 9 lines
drivers/infiniband/sw/rxe/rxe_ns.c +1 -0
index 8b9d734229b2..799a727bc1fe 100644
@@ -16,6 +16,7 @@
  struct rxe_ns_sock {
  	struct sock __rcu *rxe_sk4;
  	struct sock __rcu *rxe_sk6;
+	struct mutex	release_lock;

This change renders the existing rcu_read_lock() and rcu_read_unlock() calls unnecessary.

Thanks, Leon. I fully agree with you.
In the next version, I will remove the existing rcu locks.

Zhu Yanjun

Quoted 2 lines

Thanks