Skip to contentSkip to conversation

ovpn: drop packets looping back into their own TCP transport socket

PATCH New Antonio Quartulli 1 participant 2 messages Oct 28 – Oct 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ovpn: drop packets looping back into their own TCP transport socket

From: Antonio Quartulli <[email protected]>

If the outer packets of a TCP peer are routed into the ovpn interface, ovpn_net_xmit() selects that same peer and ovpn_tcp_send_skb() tries to take sk->sk_lock.slock, which the TCP transmit path already holds. The CPU deadlocks.

Drop such packets in ovpn_net_xmit().

Fixes 11851cbd60ea ("ovpn: implement TCP transport")
Assisted-by Claude:claude-opus-5-5
---
 drivers/net/ovpn/io.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
drivers/net/ovpn/io.c +32 -0
index f14eb4e6c46a..57cdea45dce7 100644
@@ -349,6 +349,31 @@ static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb,
 	ovpn_peer_put(peer);
 }
 
+/**
+ * ovpn_skb_is_looping - check if skb comes from the peer's own socket
+ * @peer: the peer the skb is about to be sent to
+ * @skb: the packet to check
+ *
+ * Return: true if @skb was generated by the transport socket of @peer,
+ * false otherwise
+ */
+static bool ovpn_skb_is_looping(struct ovpn_peer *peer,
+				const struct sk_buff *skb)
+{
+	struct ovpn_socket *sock;
+	bool ret;
+
+	if (!skb->sk)
+		return false;
+
+	rcu_read_lock();
+	sock = rcu_dereference(peer->sock);
+	ret = sock && sock->sk == skb->sk;
+	rcu_read_unlock();
+
+	return ret;
+}
+
 /* Send user data to the network
  */
 netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -386,6 +411,13 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 		}
 		goto drop_no_peer;
 	}
+
+	if (unlikely(ovpn_skb_is_looping(peer, skb))) {
+		net_warn_ratelimited("%s: recursive routing detected, dropping TCP packet to peer %u\n",
+				     netdev_name(ovpn->dev), peer->id);
+		goto drop;
+	}
+
 	/* dst was needed for peer selection - it can now be dropped */
 	skb_dst_drop(skb);
 
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/1 ovpn: avoid nesting TCP transport socket locks

From: Antonio Quartulli <[email protected]>

ovpn_tcp_send_skb() and ovpn_tcp_release() transmit while holding the transport socket spinlock. If the outer packet is routed into an ovpn interface, ovpn_tcp_send_skb() takes the spinlock of the selected peer's socket while the first one is still held. This deadlocks when it is the same socket or when two peers are routed through each other.

Defer transmission to the TX worker when the skb comes from an ovpn TCP transport socket.

---

v2:
- handle nesting across different transport sockets too (two peers routed through each other, cross-CPU ABBA, lockdep splat), as pointed out by Sashiko
- defer such transmissions to the TX worker instead of dropping self-looped packets in ovpn_net_xmit()

 drivers/net/ovpn/io.c   |   2 +-
 drivers/net/ovpn/peer.h |   2 +-
 drivers/net/ovpn/tcp.c  | 103 +++++++++++++++++++++++++++++-----------
 drivers/net/ovpn/tcp.h  |   2 +-
 4 files changed, 79 insertions(+), 30 deletions(-)
drivers/net/ovpn/io.c +1 -1
index f14eb4e6c46a..3282c3b0bf58 100644
@@ -284,7 +284,7 @@ void ovpn_encrypt_post(void *data, int ret)
 		ovpn_udp_send_skb(peer, sock->sk, skb);
 		break;
 	case IPPROTO_TCP:
-		ovpn_tcp_send_skb(peer, sock->sk, skb);
+		ovpn_tcp_send_skb(peer, sock, skb);
 		break;
 	default:
 		/* no transport configured yet */
drivers/net/ovpn/peer.h +1 -1
index 1879bfb76992..8256c628f769 100644
@@ -45,7 +45,7 @@ struct ovpn_route_key {
  * @tcp: keeps track of TCP specific state
  * @tcp.strp: stream parser context (TCP only)
  * @tcp.user_queue: received packets that have to go to userspace (TCP only)
- * @tcp.out_queue: packets on hold while socket is taken by user (TCP only)
+ * @tcp.out_queue: packets deferred for later transmission (TCP only)
  * @tcp.tx_in_progress: true if TX is already ongoing (TCP only)
  * @tcp.out_msg.skb: packet scheduled for sending (TCP only)
  * @tcp.out_msg.offset: offset where next send should start (TCP only)
drivers/net/ovpn/tcp.c +76 -27

151 lines not on this page · raw message

drivers/net/ovpn/tcp.h +1 -1
index a3aa3570ae5e..9f2b5efcbd3a 100644
@@ -30,7 +30,7 @@ void ovpn_tcp_socket_wait_finish(struct ovpn_socket *sock);
  * Required by the OpenVPN protocol in order to extract packets from
  * the TCP stream on the receiver side.
  */
-void ovpn_tcp_send_skb(struct ovpn_peer *peer, struct sock *sk,
+void ovpn_tcp_send_skb(struct ovpn_peer *peer, struct ovpn_socket *sock,
 		       struct sk_buff *skb);
 void ovpn_tcp_tx_work(struct work_struct *work);
 
Signature · 8 lines
2.55.0



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