ovpn: fix peer refcount leak in TCP error paths
Subsystems (4)
PATCH v2 1/1 ovpn: fix peer refcount leak in TCP error paths
When either the TCP RX or TX error path calls ovpn_peer_hold() followed by schedule_work(&peer->tcp.defer_del_work), and the work item is already pending from the other path, schedule_work() returns false and the work runs only once. Since ovpn_tcp_peer_del_work() calls ovpn_peer_put() exactly once, the extra reference taken by the losing path is never dropped, leaking the peer object.
The race window:
CPU0 (strparser/RX error): CPU1 (tcp_tx_work/TX error):
ovpn_peer_hold() <- refcnt+1 ovpn_peer_hold() <- refcnt+2
schedule_work() <- queued schedule_work() <- NO-OP
(work already pending)
ovpn_tcp_peer_del_work runs:
ovpn_peer_del()
ovpn_peer_put() <- refcnt+1
<- peer never freedFix by checking the return value of schedule_work() in both paths and calling ovpn_peer_put() to drop the extra reference if the work was already pending. ovpn_peer_hold() is kept unconditional in the TX path as it cannot fail at that point.
---
Changes since v1:
- TX path: keep ovpn_peer_hold() unconditional per Antonio Quartulli's review; only check schedule_work() return value
- Link: https://lore.kernel.org/netdev/[email protected]/
--- drivers/net/ovpn/tcp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/ovpn/tcp.c +3 -3
@@ -283,9 +283,9 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk)/* in case of TCP error we can't recover the VPN * stream therefore we abort the connection */ -if (ovpn_peer_hold (peer)) -if (!schedule_work (&peer->tcp.defer_del_work)) -ovpn_peer_put (peer); +ovpn_peer_hold (peer); +if (!schedule_work (&peer->tcp.defer_del_work)) +ovpn_peer_put (peer);/* we bail out immediately and keep tx_in_progress set * to true. This way we prevent more TX attempts
Signature · 1 line
2.53.0
Antonio Quartulli
maintainerHi,
Quoted 7 lines · Pavitra Jha
drivers/net/ovpn/tcp.c +0
-0@@ -283,9 +283,9 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk)/* in case of TCP error we can't recover the VPN * stream therefore we abort the connection
drivers/net/ovpn/tcp.c static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk) · line 291
Quoted 10 lines
drivers/net/ovpn/tcp.c +3
-3*/ -if (ovpn_peer_hold (peer)) -if (!schedule_work (&peer->tcp.defer_del_work)) -ovpn_peer_put (peer); +ovpn_peer_hold (peer); +if (!schedule_work (&peer->tcp.defer_del_work)) +ovpn_peer_put (peer);/* we bail out immediately and keep tx_in_progress set * to true. This way we prevent more TX attempts
This patch now lacks the RX part.
Please wait 24h between submissions (i.e. before sending v3). More comments may come in in the meantime.
Regards,
Signature · 2 lines
Antonio Quartulli OpenVPN Inc.