ovpn: set sk_user_data before overriding callbacks
Subsystems (2)
PATCH v3 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.
---
Changes since v2: none
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
@@ -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
@@ -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
@@ -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
PATCH v3 2/3 ovpn: fix possible use-after-free in ovpn_net_xmit
When building the skb_list in ovpn_net_xmit, skb_share_check will free the original skb if it is shared. The current implementation continues to use the stale skb pointer for subsequent operations:
- peer lookup,
- skb_dst_drop (even though all segments produced by skb_gso_segment will have a dst attached),
- ovpn_peer_stats_increment_tx.
Fix this by moving the peer lookup and skb_dst_drop before segmentation so that the original skb is still valid when used. Return early if all segments fail skb_share_check and the list ends up empty. Also switch ovpn_peer_stats_increment_tx to use skb_list.next; the next patch fixes the stats logic.
---
Changes since v2:
- moved peer selection and skb_dst_drop before skb_gso_segment
- added goto to drop the peer reference on error
- stopped using 'skb' after building 'skb_list' and switched to skb_list.next for the stats update
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 | 52 ++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 21 deletions(-)
drivers/net/ovpn/io.c +31 -21
@@ -365,7 +365,27 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)/* verify IP header size in network packet */ proto =ovpn_ip_check_protocol (skb);if (unlikely (!proto || skb->protocol != proto)) - goto drop; + goto drop_no_peer; + +/* retrieve peer serving the destination IP of this packet */ + peer =ovpn_peer_get_by_dst (ovpn, skb); +if (unlikely (!peer)) { +switch (skb->protocol) { +case htons (ETH_P_IP ): +net_dbg_ratelimited ("%s: no peer to send data to dst=%pI4\n" , +netdev_name (ovpn->dev), + &ip_hdr (skb)->daddr); +break ; +case htons (ETH_P_IPV6 ): +net_dbg_ratelimited ("%s: no peer to send data to dst=%pI6c\n" , +netdev_name (ovpn->dev), + &ipv6_hdr (skb)->daddr); +break ; + } + goto drop_no_peer; + } +/* dst was needed for peer selection - it can now be dropped */ +skb_dst_drop (skb);if (skb_is_gso (skb)) { segments =skb_gso_segment (skb,0 );
@@ -396,34 +416,24 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)__skb_queue_tail (&skb_list, curr); } - skb_list.prev->next =NULL ; -/* retrieve peer serving the destination IP of this packet */ - peer =ovpn_peer_get_by_dst (ovpn, skb); -if (unlikely (!peer)) { -switch (skb->protocol) { -case htons (ETH_P_IP ): -net_dbg_ratelimited ("%s: no peer to send data to dst=%pI4\n" , -netdev_name (ovpn->dev), - &ip_hdr (skb)->daddr); -break ; -case htons (ETH_P_IPV6 ): -net_dbg_ratelimited ("%s: no peer to send data to dst=%pI6c\n" , -netdev_name (ovpn->dev), - &ipv6_hdr (skb)->daddr); -break ; - } - goto drop; +/* 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))) { +ovpn_peer_put (peer); +return NETDEV_TX_OK ; } -/* dst was needed for peer selection - it can now be dropped */ -skb_dst_drop (skb); + skb_list.prev->next =NULL ; -ovpn_peer_stats_increment_tx (&peer->vpn_stats, skb->len); +ovpn_peer_stats_increment_tx (&peer->vpn_stats, skb_list.next->len);ovpn_send (ovpn, skb_list.next, peer);return NETDEV_TX_OK ; drop: +ovpn_peer_put (peer); +drop_no_peer:dev_dstats_tx_dropped (ovpn->dev);skb_tx_error (skb);kfree_skb_list (skb);
Signature · 1 line
2.52.0
Re: [Openvpn-devel] [PATCH ovpn net v3 2/3] ovpn: fix possible use-after-free in ovpn_net_xmit
Quoted 8 lines · Ralf Lici
When building the
skb_listinovpn_net_xmit,skb_share_checkwill free the original skb if it is shared. The current implementation continues to use the stale skb pointer for subsequent operations:
- peer lookup,
-skb_dst_drop(even though all segments produced byskb_gso_segmentwill have a dst attached),
-ovpn_peer_stats_increment_tx.
Quoted 8 lines
Fix this by moving the peer lookup and
skb_dst_dropbefore segmentation so that the original skb is still valid when used. Return early if all segments failskb_share_checkand the list ends up empty. Also switchovpn_peer_stats_increment_txto use skb_list.next; the next patch fixes the stats logic.Fixes 08857b5ec5d9 ("ovpn: implement basic TX path (UDP)")Signed-off-by Ralf Lici <[email protected]...>
Thanks Ralf.
Signature · 1 line
Sabrina
PATCH v3 3/3 ovpn: fix VPN TX bytes counting
In reply to Ralf LiciIn ovpn_net_xmit, after GSO segmentation and segment processing, the first segment on the list is used 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.
---
Changes since v2: none (except for trivial rebase)
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
@@ -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;
@@ -414,6 +415,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); }
@@ -426,7 +429,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) } skb_list.prev->next =NULL ; -ovpn_peer_stats_increment_tx (&peer->vpn_stats, skb_list.next->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
Re: [Openvpn-devel] [PATCH ovpn net v3 3/3] ovpn: fix VPN TX bytes counting
Quoted 11 lines · Ralf Lici
In
ovpn_net_xmit, after GSO segmentation and segment processing, the first segment on the list is used 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_checkinto atx_bytesvariable. 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")Signed-off-by Ralf Lici <[email protected]...>
Signature · 1 line
Sabrina
Thanks Ralf for your hard work and thank you, Sabrina, for the review! I have queued these patches for sending to net.
I'll try to wait for two more fixes (one is already on the mailing list) and then ship everything to netdev.
Cheers,
Signature · 1 line
Antonio Quartulli