From: Antonio Quartulli <[email protected]>

A TCP transport socket may transmit while its spinlock is held, e.g. in
ovpn_tcp_send_skb(), ovpn_tcp_release() or the TCP RX/timer paths. 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.

The originating socket cannot be reliably identified from the skb, so
use spin_trylock() and defer transmission to the TX worker when the
socket lock is busy, including on plain contention. out_queue becomes
the generic deferred TX queue, protected by its own lock since packets
are now added without the socket lock. New packets are queued behind
deferred ones to keep ordering, and the queue is drained by the next
ovpn sender, the TX worker or release_cb. Nothing is sent on a socket
that was meanwhile detached or attached to another peer.

Under contention, e.g. while the RX path holds the socket lock, some TX
now goes through the worker, at the cost of some latency and CPU.

Fixes: 11851cbd60ea ("ovpn: implement TCP transport")
Cc: [email protected]
Cc: Ji WANG <[email protected]>
Reported-by: Bernhard Schmidt <[email protected]>
Closes: 
https://lore.kernel.org/openvpn-devel/[email protected]/
Signed-off-by: Antonio Quartulli <[email protected]>
---

v5:
- don't send on a socket that was detached or attached to another peer
  meanwhile; check it in the TX worker too (Sashiko)
- keep dropping packets while TX is blocked instead of queueing them
- mention the cost under contention in the commit message
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

 drivers/net/ovpn/io.c   |   2 +-
 drivers/net/ovpn/peer.h |   2 +-
 drivers/net/ovpn/tcp.c  | 117 +++++++++++++++++++++++++++++-----------
 drivers/net/ovpn/tcp.h  |   2 +-
 4 files changed, 88 insertions(+), 35 deletions(-)

diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c
index f14eb4e6c46a..3282c3b0bf58 100644
--- a/drivers/net/ovpn/io.c
+++ b/drivers/net/ovpn/io.c
@@ -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 */
diff --git a/drivers/net/ovpn/peer.h b/drivers/net/ovpn/peer.h
index 1879bfb76992..8256c628f769 100644
--- a/drivers/net/ovpn/peer.h
+++ b/drivers/net/ovpn/peer.h
@@ -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)
diff --git a/drivers/net/ovpn/tcp.c b/drivers/net/ovpn/tcp.c
index 8fe8a8e750a4..36f3ff237990 100644
--- a/drivers/net/ovpn/tcp.c
+++ b/drivers/net/ovpn/tcp.c
@@ -23,11 +23,6 @@
 #include "skb.h"
 #include "tcp.h"
 
-#define OVPN_TCP_DEPTH_NESTING 2
-#if OVPN_TCP_DEPTH_NESTING == SINGLE_DEPTH_NESTING
-#error "OVPN TCP requires its own lockdep subclass"
-#endif
-
 static struct proto ovpn_tcp_prot __ro_after_init;
 static struct proto_ops ovpn_tcp_ops __ro_after_init;
 static struct proto ovpn_tcp6_prot __ro_after_init;
@@ -313,6 +308,49 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, 
struct sock *sk)
        peer->tcp.tx_in_progress = false;
 }
 
+static void ovpn_tcp_send_sock_skb(struct ovpn_peer *peer, struct sock *sk,
+                                  struct sk_buff *skb)
+{
+       if (peer->tcp.out_msg.skb)
+               ovpn_tcp_send_sock(peer, sk);
+
+       if (peer->tcp.out_msg.skb) {
+               ovpn_dev_dstats_tx_dropped(peer->ovpn->dev);
+               kfree_skb(skb);
+               return;
+       }
+
+       peer->tcp.out_msg.skb = skb;
+       peer->tcp.out_msg.len = skb->len;
+       peer->tcp.out_msg.offset = 0;
+       ovpn_tcp_send_sock(peer, sk);
+}
+
+/* send queued packets, dropping them while TX is blocked. Only packets queued
+ * before the call are sent: a packet routed back to this socket is
+ * re-queued while sending, and would otherwise make this loop endless
+ */
+static void ovpn_tcp_flush_queue(struct ovpn_peer *peer, struct sock *sk)
+{
+       unsigned int n = skb_queue_len_lockless(&peer->tcp.out_queue);
+       struct sk_buff *skb;
+
+       while (n-- && (skb = skb_dequeue(&peer->tcp.out_queue)))
+               ovpn_tcp_send_sock_skb(peer, sk, skb);
+}
+
+/* the socket may have been detached or attached to another peer */
+static bool ovpn_tcp_sock_is_attached(struct ovpn_socket *sock)
+{
+       bool ret;
+
+       rcu_read_lock();
+       ret = rcu_dereference_sk_user_data(sock->sk) == sock;
+       rcu_read_unlock();
+
+       return ret;
+}
+
 void ovpn_tcp_tx_work(struct work_struct *work)
 {
        struct ovpn_socket *sock;
@@ -320,58 +358,77 @@ void ovpn_tcp_tx_work(struct work_struct *work)
        sock = container_of(work, struct ovpn_socket, tcp_tx_work);
 
        lock_sock(sock->sk);
-       if (sock->peer)
+       if (sock->peer && ovpn_tcp_sock_is_attached(sock)) {
                ovpn_tcp_send_sock(sock->peer, sock->sk);
+               ovpn_tcp_flush_queue(sock->peer, sock->sk);
+       }
        release_sock(sock->sk);
 }
 
-static void ovpn_tcp_send_sock_skb(struct ovpn_peer *peer, struct sock *sk,
-                                  struct sk_buff *skb)
+static bool ovpn_tcp_enqueue(struct ovpn_peer *peer, struct sk_buff *skb)
 {
-       if (peer->tcp.out_msg.skb)
-               ovpn_tcp_send_sock(peer, sk);
+       struct sk_buff_head *queue = &peer->tcp.out_queue;
+       unsigned long flags;
+       bool ret = false;
+
+       spin_lock_irqsave(&queue->lock, flags);
+       if (skb_queue_len(queue) < READ_ONCE(net_hotdata.max_backlog)) {
+               __skb_queue_tail(queue, skb);
+               ret = true;
+       }
+       spin_unlock_irqrestore(&queue->lock, flags);
 
-       if (peer->tcp.out_msg.skb) {
+       if (!ret) {
                ovpn_dev_dstats_tx_dropped(peer->ovpn->dev);
                kfree_skb(skb);
-               return;
        }
 
-       peer->tcp.out_msg.skb = skb;
-       peer->tcp.out_msg.len = skb->len;
-       peer->tcp.out_msg.offset = 0;
-       ovpn_tcp_send_sock(peer, sk);
+       return ret;
 }
 
-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)
 {
+       struct sock *sk = sock->sk;
        u16 len = skb->len;
 
        *(__be16 *)__skb_push(skb, sizeof(u16)) = htons(len);
 
-       spin_lock_nested(&sk->sk_lock.slock, OVPN_TCP_DEPTH_NESTING);
+       /* we may be called while a transport socket spinlock is held (even
+        * this same one): never spin, defer to the worker instead
+        */
+       if (!spin_trylock(&sk->sk_lock.slock)) {
+               if (ovpn_tcp_enqueue(peer, skb))
+                       queue_work(ovpn_wq, &sock->tcp_tx_work);
+               return;
+       }
+
+       if (unlikely(!ovpn_tcp_sock_is_attached(sock))) {
+               spin_unlock(&sk->sk_lock.slock);
+               ovpn_dev_dstats_tx_dropped(peer->ovpn->dev);
+               kfree_skb(skb);
+               return;
+       }
+
        if (sock_owned_by_user(sk)) {
-               if (skb_queue_len(&peer->tcp.out_queue) >=
-                   READ_ONCE(net_hotdata.max_backlog)) {
-                       ovpn_dev_dstats_tx_dropped(peer->ovpn->dev);
-                       kfree_skb(skb);
-                       goto unlock;
+               /* flushed by ovpn_tcp_release() */
+               ovpn_tcp_enqueue(peer, skb);
+       } else if (!skb_queue_empty_lockless(&peer->tcp.out_queue)) {
+               /* preserve ordering with deferred packets and drain them here 
*/
+               if (ovpn_tcp_enqueue(peer, skb)) {
+                       ovpn_tcp_send_sock(peer, sk);
+                       ovpn_tcp_flush_queue(peer, sk);
                }
-               __skb_queue_tail(&peer->tcp.out_queue, skb);
        } else {
                ovpn_tcp_send_sock_skb(peer, sk, skb);
        }
-unlock:
        spin_unlock(&sk->sk_lock.slock);
 }
 
 static void ovpn_tcp_release(struct sock *sk)
 {
-       struct sk_buff_head queue;
        struct ovpn_socket *sock;
        struct ovpn_peer *peer;
-       struct sk_buff *skb;
 
        rcu_read_lock();
        sock = rcu_dereference_sk_user_data(sk);
@@ -391,11 +448,7 @@ static void ovpn_tcp_release(struct sock *sk)
        }
        rcu_read_unlock();
 
-       __skb_queue_head_init(&queue);
-       skb_queue_splice_init(&peer->tcp.out_queue, &queue);
-
-       while ((skb = __skb_dequeue(&queue)))
-               ovpn_tcp_send_sock_skb(peer, sk, skb);
+       ovpn_tcp_flush_queue(peer, sk);
 
        peer->tcp.sk_cb.prot->release_cb(sk);
        ovpn_peer_put(peer);
diff --git a/drivers/net/ovpn/tcp.h b/drivers/net/ovpn/tcp.h
index a3aa3570ae5e..9f2b5efcbd3a 100644
--- a/drivers/net/ovpn/tcp.h
+++ b/drivers/net/ovpn/tcp.h
@@ -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);
 
-- 
2.56.1



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

Reply via email to