Skip to contentSkip to conversation

ovpn-dco: hold peer ref in ovpn_tcp_read_sock under RCU

PATCH Superseded liuchong via Openvpn-devel 2 participants 2 messages Nov 18 Network drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ovpn-dco: hold peer ref in ovpn_tcp_read_sock under RCU

ovpn_tcp_read_sock() uses sock->peer without taking a reference. ovpn_peer_del() can drop the last hash-table reference while the TCP data_ready callback is still attached, so peer->refcount may already be zero when ovpn_peer_hold() is later called for ovpn_recv(). That leads to a WARN_ON and a refcount underflow when the async path puts the peer.

Take the peer reference under rcu_read_lock() before leaving the RCU critical section, and drop it on all exit paths. If the peer is already being destroyed, stop consuming the stream quietly.

Fixes 209247cf6d47 ("ovpn-dco: implement TCP transport support")
---
 drivers/net/ovpn-dco/tcp.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/net/ovpn-dco/tcp.c +18 -6
index fe095f9..d8f9afd 100644
@@ -35,15 +35,26 @@ static int ovpn_tcp_read_sock(read_descriptor_t *desc, struct sk_buff *in_skb,
 
 	rcu_read_lock();
 	sock = rcu_dereference_sk_user_data(sk);
-	rcu_read_unlock();
-
 	if (unlikely(!sock || !sock->peer)) {
+		rcu_read_unlock();
 		pr_err("ovpn: read_sock triggered for socket with no metadata\n");
 		desc->error = -EINVAL;
 		return 0;
 	}
 
+	/* Take a reference while still under RCU: ovpn_peer_del() may drop the
+	 * last hash-table reference before socket callbacks are restored in
+	 * ovpn_peer_release(). Without this hold, a concurrent delete leaves
+	 * peer->refcount at 0 while this callback still runs, causing
+	 * ovpn_peer_hold() for ovpn_recv() to fail and a later put to underflow.
+	 */
 	peer = sock->peer;
+	if (unlikely(!ovpn_peer_hold(peer))) {
+		rcu_read_unlock();
+		/* Peer is already being destroyed; stop consuming the stream. */
+		return 0;
+	}
+	rcu_read_unlock();
 
 	while (in_len > 0) {
 		/* no skb allocated means that we have to read (or finish reading) the 2 bytes
@@ -103,11 +114,10 @@ static int ovpn_tcp_read_sock(read_descriptor_t *desc, struct sk_buff *in_skb,
 			 * Queue skb for sending to userspace via recvmsg on the socket
 			 */
 			if (likely(ovpn_opcode_from_skb(peer->tcp.skb, 0) == OVPN_DATA_V2)) {
-				/* hold reference to peer as required by ovpn_recv().
+				/* Extra reference required by ovpn_recv() / decrypt_work.
 				 *
-				 * NOTE: in this context we should already be holding a
-				 * reference to this peer, therefore ovpn_peer_hold() is
-				 * not expected to fail
+				 * NOTE: this cannot fail as this function already holds a
+				 * reference to the peer.
 				 */
 				WARN_ON(!ovpn_peer_hold(peer));
 				status = ovpn_recv(peer->ovpn, peer, peer->tcp.skb);
@@ -140,10 +150,12 @@ next_read:
 		copied += chunk;
 	}
 
+	ovpn_peer_put(peer);
 	return copied;
 err:
 	netdev_err(peer->ovpn->dev, "cannot process incoming TCP data: %d\n", desc->error);
 	ovpn_peer_del(peer, OVPN_DEL_PEER_REASON_TRANSPORT_ERROR);
+	ovpn_peer_put(peer);
 	return 0;
 }
 
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Thanks for the patch.

Quoted 7 lines · liuchong via Openvpn-devel

ovpn_tcp_read_sock() uses sock->peer without taking a reference. ovpn_peer_del() can drop the last hash-table reference while the TCP data_ready callback is still attached, so peer->refcount may already be zero when ovpn_peer_hold() is later called for ovpn_recv(). That leads to a WARN_ON and a refcount underflow when the async path puts the peer.

Quoted 6 lines

Take the peer reference under rcu_read_lock() before leaving the RCU critical section, and drop it on all exit paths. If the peer is already being destroyed, stop consuming the stream quietly.

Fixes 209247cf6d47 ("ovpn-dco: implement TCP transport support")

I'm not sure this SoB tag is fully compliant with the Developer Certificate of Origin. If you can, please use your real name, as the DCO asks for a known identity.

Quoted 17 lines
---
 drivers/net/ovpn-dco/tcp.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/net/ovpn-dco/tcp.c +1 -2
index fe095f9..d8f9afd 100644
@@ -35,15 +35,26 @@ static int ovpn_tcp_read_sock(read_descriptor_t *desc, struct sk_buff *in_skb,
 
 	rcu_read_lock();
 	sock = rcu_dereference_sk_user_data(sk);
-	rcu_read_unlock();
-
 	if (unlikely(!sock || !sock->peer)) {
+		rcu_read_unlock();
 		pr_err("ovpn: read_sock triggered for socket with no metadata\n");

drivers/net/ovpn-dco/tcp.c static int ovpn_tcp_read_sock(read_descriptor_t *desc, struct sk_buff *in_skb, · line 50

Quoted 10 lines
drivers/net/ovpn-dco/tcp.c +6 -0
 		desc->error = -EINVAL;
 		return 0;
 	}
 
+	/* Take a reference while still under RCU: ovpn_peer_del() may drop the
+	 * last hash-table reference before socket callbacks are restored in
+	 * ovpn_peer_release(). Without this hold, a concurrent delete leaves
+	 * peer->refcount at 0 while this callback still runs, causing
+	 * ovpn_peer_hold() for ovpn_recv() to fail and a later put to underflow.
+	 */

The new comment is quite long for what it says, and the commit message already carries the same explanation.
Something like

    /* hold the peer while still under RCU, it may be getting deleted */

would be enough.

Aside from these two minor nits, neither of which needs a respin on its own account, the code LGTM.

Regards,

Signature · 8 lines
Ralf Lici
Mandelbit Srl


_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel