Skip to contentSkip to conversation

ovpn: set sk_user_data before overriding callbacks

PATCH Superseded Ralf Lici 2 participants 6 messages Jan 28 – Jan 29

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ralf Lici

author
#0 lore raw

PATCH v2 1/3 ovpn: set sk_user_data before overriding callbacks

During initialization, we override socket callbacks and set sk_user_data to an ovpn_socket instance. Currently, these two operations are
decoupled: callbacks are overridden before sk_user_data is set. While existing callbacks perform safety checks for NULL or non-ovpn sk_user_data, this condition causes a "half-formed" state where valid packets arriving during attachment trigger error logs (e.g., "invoked on non ovpn socket").

Set sk_user_data before overriding the callbacks so that it can be accessed safely from them. Since we already check that the socket has no sk_user_data before setting it, this remains safe even if an interrupt accesses the socket after sk_user_data is set but before the callbacks are overridden.

This also requires initializing all protocol-specific fields (such as tcp_tx_work and peer links) before calling ovpn_socket_attach, ensuring the ovpn_socket is fully formed before it becomes visible to any callback.

Fixes f6226ae7a0cd ("ovpn: introduce the ovpn_socket object")

---
Changes since v1:
- reset sk_user_data to NULL in case of error
- removed a redundant goto sock_release in ovpn_socket_new
- expanded commit message with additional information on the purpose of this change
- added explanation in the commit message of why the per-protocol ovpn_sock initialization code in ovpn_socket_new was moved
- added Fixes tag

 drivers/net/ovpn/socket.c | 39 +++++++++++++++++++++------------------
 drivers/net/ovpn/tcp.c    |  9 +++++++--
 drivers/net/ovpn/udp.c    |  1 +
 3 files changed, 29 insertions(+), 20 deletions(-)
drivers/net/ovpn/socket.c +21 -18
index 9750871ab65c..448cee3b3f9f 100644
@@ -200,6 +200,22 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)
 	ovpn_sock->sk = sk;
 	kref_init(&ovpn_sock->refcount);
 
+	/* TCP sockets are per-peer, therefore they are linked to their unique
+	 * peer
+	 */
+	if (sk->sk_protocol == IPPROTO_TCP) {
+		INIT_WORK(&ovpn_sock->tcp_tx_work, ovpn_tcp_tx_work);
+		ovpn_sock->peer = peer;
+		ovpn_peer_hold(peer);
+	} else if (sk->sk_protocol == IPPROTO_UDP) {
+		/* in UDP we only link the ovpn instance since the socket is
+		 * shared among multiple peers
+		 */
+		ovpn_sock->ovpn = peer->ovpn;
+		netdev_hold(peer->ovpn->dev, &ovpn_sock->dev_tracker,
+			    GFP_KERNEL);
+	}
+
 	/* the newly created ovpn_socket is holding reference to sk,
 	 * therefore we increase its refcounter.
 	 *
@@ -212,29 +228,16 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)
 
 	ret = ovpn_socket_attach(ovpn_sock, sock, peer);
 	if (ret < 0) {
+		if (sk->sk_protocol == IPPROTO_TCP)
+			ovpn_peer_put(peer);
+		else if (sk->sk_protocol == IPPROTO_UDP)
+			netdev_put(peer->ovpn->dev, &ovpn_sock->dev_tracker);
+
 		sock_put(sk);
 		kfree(ovpn_sock);
 		ovpn_sock = ERR_PTR(ret);
-		goto sock_release;
-	}
-
-	/* TCP sockets are per-peer, therefore they are linked to their unique
-	 * peer
-	 */
-	if (sk->sk_protocol == IPPROTO_TCP) {
-		INIT_WORK(&ovpn_sock->tcp_tx_work, ovpn_tcp_tx_work);
-		ovpn_sock->peer = peer;
-		ovpn_peer_hold(peer);
-	} else if (sk->sk_protocol == IPPROTO_UDP) {
-		/* in UDP we only link the ovpn instance since the socket is
-		 * shared among multiple peers
-		 */
-		ovpn_sock->ovpn = peer->ovpn;
-		netdev_hold(peer->ovpn->dev, &ovpn_sock->dev_tracker,
-			    GFP_KERNEL);
 	}
 
-	rcu_assign_sk_user_data(sk, ovpn_sock);
 sock_release:
 	release_sock(sk);
 	return ovpn_sock;
drivers/net/ovpn/tcp.c +7 -2
index 0d7f30360d87..f0b4e07ba924 100644
@@ -487,6 +487,7 @@ int ovpn_tcp_socket_attach(struct ovpn_socket *ovpn_sock,
 	/* make sure no pre-existing encapsulation handler exists */
 	if (ovpn_sock->sk->sk_user_data)
 		return -EBUSY;
+	rcu_assign_sk_user_data(ovpn_sock->sk, ovpn_sock);
 
 	/* only a fully connected socket is expected. Connection should be
 	 * handled in userspace
@@ -495,13 +496,14 @@ int ovpn_tcp_socket_attach(struct ovpn_socket *ovpn_sock,
 		net_err_ratelimited("%s: provided TCP socket is not in ESTABLISHED state: %d\n",
 				    netdev_name(peer->ovpn->dev),
 				    ovpn_sock->sk->sk_state);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto err;
 	}
 
 	ret = strp_init(&peer->tcp.strp, ovpn_sock->sk, &cb);
 	if (ret < 0) {
 		DEBUG_NET_WARN_ON_ONCE(1);
-		return ret;
+		goto err;
 	}
 
 	INIT_WORK(&peer->tcp.defer_del_work, ovpn_tcp_peer_del_work);
@@ -536,6 +538,9 @@ int ovpn_tcp_socket_attach(struct ovpn_socket *ovpn_sock,
 	strp_check_rcv(&peer->tcp.strp);
 
 	return 0;
+err:
+	rcu_assign_sk_user_data(ovpn_sock->sk, NULL);
+	return ret;
 }
 
 static void ovpn_tcp_close(struct sock *sk, long timeout)
drivers/net/ovpn/udp.c +1 -0
index d6a0f7a0b75d..272b535ecaad 100644
@@ -386,6 +386,7 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock, struct socket *sock,
 			   struct ovpn_priv *ovpn)
 {
 	struct udp_tunnel_sock_cfg cfg = {
+		.sk_user_data = ovpn_sock,
 		.encap_type = UDP_ENCAP_OVPNINUDP,
 		.encap_rcv = ovpn_udp_encap_recv,
 		.encap_destroy = ovpn_udp_encap_destroy,
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH v2 2/3 ovpn: fix possible use-after-free in ovpn_net_xmit

During GSO fragmentation, skb_share_check may clone the first segment and free the original skb. The current implementation continues to use the stale skb pointer for peer lookup.

Fix this by updating the skb variable to point to the new head of the segment list after the processing loop. Additionally, return early if all segments were dropped during the loop to avoid double-counting statistics and double-freeing memory in the drop path.

Fixes 08857b5ec5d9 ("ovpn: implement basic TX path (UDP)")

---
Changes since v1
- this is a new patch that replaces the previous "ovpn: use sk_buff_head properly in ovpn_net_xmit"

 drivers/net/ovpn/io.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/net/ovpn/io.c +11 -0
index 3e9e7f8444b3..95c3518e067c 100644
@@ -396,6 +396,17 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 
 		__skb_queue_tail(&skb_list, curr);
 	}
+
+	/* no segments survived: don't jump to 'drop' because we already
+	 * incremented the counter for each failure in the loop
+	 */
+	if (unlikely(skb_queue_empty(&skb_list)))
+		return NETDEV_TX_OK;
+
+	/* the original 'skb' might have been freed/cloned in the loop: use the
+	 * first element of our list for the other operations
+	 */
+	skb = skb_list.next;
 	skb_list.prev->next = NULL;
 
 	/* retrieve peer serving the destination IP of this packet */
Signature · 1 line
2.52.0
#2 lore raw

Re: [Openvpn-devel] [PATCH ovpn net v2 2/3] ovpn: fix possible use-after-free in ovpn_net_xmit

Quoted 31 lines · Ralf Lici

During GSO fragmentation, skb_share_check may clone the first segment and free the original skb. The current implementation continues to use the stale skb pointer for peer lookup.

Fix this by updating the skb variable to point to the new head of the segment list after the processing loop. Additionally, return early if all segments were dropped during the loop to avoid double-counting statistics and double-freeing memory in the drop path.

Fixes 08857b5ec5d9 ("ovpn: implement basic TX path (UDP)")

---
Changes since v1
- this is a new patch that replaces the previous "ovpn: use sk_buff_head properly in ovpn_net_xmit"

 drivers/net/ovpn/io.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/net/ovpn/io.c +4 -0
index 3e9e7f8444b3..95c3518e067c 100644
@@ -396,6 +396,17 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 
 		__skb_queue_tail(&skb_list, curr);
 	}
+
+	/* no segments survived: don't jump to 'drop' because we already
+	 * incremented the counter for each failure in the loop
+	 */

drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 412

Quoted 10 lines
drivers/net/ovpn/io.c +7 -0
+	if (unlikely(skb_queue_empty(&skb_list)))
+		return NETDEV_TX_OK;
+
+	/* the original 'skb' might have been freed/cloned in the loop: use the
+	 * first element of our list for the other operations
+	 */
+	skb = skb_list.next;
 	skb_list.prev->next = NULL;
 
 	/* retrieve peer serving the destination IP of this packet */

This patch looks ok, but maybe it would be better to fix this using a chunk of your sk_buff_head rework instead. I think all segments produced by skb_gso_segment will have a dst attached (__copy_skb_header(nskb, head_skb) in skb_segment), so the skb_dst_drop after peer selection will only affect the first segment, and not the others. Doing peer selection + skb_dst_drop before calling skb_gso_segment would solve this as well as the UAF.

(your current patch would also avoid a UAF on ovpn_peer_stats_increment_tx, but you're solving that in the next patch)

Signature · 1 line
Sabrina
author
#3 lore raw

Re: [Openvpn-devel] [PATCH ovpn net v2 2/3] ovpn: fix possible use-after-free in ovpn_net_xmit

Quoted 52 lines · Sabrina Dubroca
Quoted 46 lines · Ralf Lici

During GSO fragmentation, skb_share_check may clone the first segment
and free the original skb. The current implementation continues to use
the stale skb pointer for peer lookup.

Fix this by updating the skb variable to point to the new head of the
segment list after the processing loop. Additionally, return early if
all segments were dropped during the loop to avoid double-counting statistics and double-freeing memory in the drop path.

Fixes 08857b5ec5d9 ("ovpn: implement basic TX path (UDP)")

---
Changes since v1
- this is a new patch that replaces the previous "ovpn: use sk_buff_head
  properly in ovpn_net_xmit"

 drivers/net/ovpn/io.c | 11 +++++++++++

 1 file changed, 11 insertions(+)
drivers/net/ovpn/io.c +0 -0
index 3e9e7f8444b3..95c3518e067c 100644
@@ -396,6 +396,17 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb,
 struct net_device *dev)


  __skb_queue_tail(&skb_list, curr); }

drivers/net/ovpn/io.c +2 -0
+
+	/* no segments survived: don't jump to 'drop' because we

already

drivers/net/ovpn/io.c +6 -0
+	 * incremented the counter for each failure in the loop
+	 */
+	if (unlikely(skb_queue_empty(&skb_list)))
+		return NETDEV_TX_OK;
+
+	/* the original 'skb' might have been freed/cloned in the

loop: use the

drivers/net/ovpn/io.c +3 -0
+	 * first element of our list for the other operations
+	 */
+	skb = skb_list.next;
Quoted 4 lines

  skb_list.prev->next = NULL;

  /* retrieve peer serving the destination IP of this packet */

Quoted 7 lines

This patch looks ok, but maybe it would be better to fix this using a chunk of your sk_buff_head rework instead. I think all segments produced by skb_gso_segment will have a dst attached (__copy_skb_header(nskb, head_skb) in skb_segment), so the skb_dst_drop after peer selection will only affect the first segment, and not the others. Doing peer selection + skb_dst_drop before calling skb_gso_segment would solve this as well as the UAF.

Ah right, the dst is indeed copied to the segments. I will make sure to move the peer selection earlier, while we are still using the original skb, so that we can call skb_dst_drop only on that one.

Quoted 3 lines

(your current patch would also avoid a UAF on
ovpn_peer_stats_increment_tx,
but you're solving that in the next patch)

Yes, now the stats patch is only responsible for fixing the stats count. I have also updated the commit message from v1.

Thanks for the feedback.

Signature · 2 lines
Ralf Lici
Mandelbit Srl
author
#4 lore raw

PATCH v2 3/3 ovpn: fix VPN TX bytes counting

In reply to Ralf Lici

In ovpn_net_xmit, after GSO segmentation and segment processing, the skb variable points only to the first segment of the resulting list. The current code uses skb->len to increment VPN TX statistics, which fails to account for any subsequent segments in the chain.

Fix this by accumulating the length of every segment that successfully passes skb_share_check into a tx_bytes variable. This ensures the peer statistics accurately reflect the total data volume sent, regardless of whether the original packet was segmented.

Fixes 04ca14955f9a ("ovpn: store tunnel and transport statistics")

---
Changes since v1:
- added Fixes tag

 drivers/net/ovpn/io.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/ovpn/io.c +4 -1
index 95c3518e067c..4f3c9c5d00aa 100644
@@ -355,6 +355,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 	struct ovpn_priv *ovpn = netdev_priv(dev);
 	struct sk_buff *segments, *curr, *next;
 	struct sk_buff_head skb_list;
+	unsigned int tx_bytes = 0;
 	struct ovpn_peer *peer;
 	__be16 proto;
 	int ret;
@@ -394,6 +395,8 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 			continue;
 		}
 
+		/* only count what we actually send */
+		tx_bytes += curr->len;
 		__skb_queue_tail(&skb_list, curr);
 	}
 
@@ -429,7 +432,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 	/* dst was needed for peer selection - it can now be dropped */
 	skb_dst_drop(skb);
 
-	ovpn_peer_stats_increment_tx(&peer->vpn_stats, skb->len);
+	ovpn_peer_stats_increment_tx(&peer->vpn_stats, tx_bytes);
 	ovpn_send(ovpn, skb_list.next, peer);
 
 	return NETDEV_TX_OK;
Signature · 1 line
2.52.0
Reviewed-by on v2 1/3
#5 lore raw
In reply to Ralf Lici
Quoted 21 lines · Ralf Lici

During initialization, we override socket callbacks and set sk_user_data to an ovpn_socket instance. Currently, these two operations are
decoupled: callbacks are overridden before sk_user_data is set. While existing callbacks perform safety checks for NULL or non-ovpn sk_user_data, this condition causes a "half-formed" state where valid packets arriving during attachment trigger error logs (e.g., "invoked on non ovpn socket").

Set sk_user_data before overriding the callbacks so that it can be accessed safely from them. Since we already check that the socket has no sk_user_data before setting it, this remains safe even if an interrupt accesses the socket after sk_user_data is set but before the callbacks are overridden.

This also requires initializing all protocol-specific fields (such as tcp_tx_work and peer links) before calling ovpn_socket_attach, ensuring the ovpn_socket is fully formed before it becomes visible to any callback.

Fixes f6226ae7a0cd ("ovpn: introduce the ovpn_socket object")
Quoted 9 lines

---
Changes since v1:
- reset sk_user_data to NULL in case of error
- removed a redundant goto sock_release in ovpn_socket_new
- expanded commit message with additional information on the purpose of this change
- added explanation in the commit message of why the per-protocol ovpn_sock initialization code in ovpn_socket_new was moved
- added Fixes tag

Thanks!

Signature · 1 line
Sabrina