ovpn: set sk_user_data before overriding callbacks
Subsystems (2)
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.
---
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 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.
---
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
@@ -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
PATCH v2 3/3 ovpn: fix VPN TX bytes counting
In reply to Ralf LiciIn 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.
---
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;
@@ -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
Re: [Openvpn-devel] [PATCH ovpn net v2 2/3] ovpn: fix possible use-after-free in ovpn_net_xmit
In reply to Ralf LiciQuoted 31 lines · Ralf Lici
During GSO fragmentation,
skb_share_checkmay 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)")Signed-off-by Ralf Lici <[email protected]...>---
Changes since v1
- this is a new patch that replaces the previous "ovpn: usesk_buff_headproperly inovpn_net_xmit"drivers/net/ovpn/io.c | 11 +++++++++++ 1 file changed, 11 insertions(+)drivers/net/ovpn/io.c +4
-0@@ -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
Quoted 21 lines · Ralf Lici
During initialization, we override socket callbacks and set
sk_user_datato anovpn_socketinstance. Currently, these two operations are
decoupled: callbacks are overridden beforesk_user_datais set. While existing callbacks perform safety checks for NULL or non-ovpnsk_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_databefore overriding the callbacks so that it can be accessed safely from them. Since we already check that the socket has nosk_user_databefore setting it, this remains safe even if an interrupt accesses the socket aftersk_user_datais set but before the callbacks are overridden.This also requires initializing all protocol-specific fields (such as
tcp_tx_workand peer links) before callingovpn_socket_attach, ensuring theovpn_socketis fully formed before it becomes visible to any callback.Fixes f6226ae7a0cd ("ovpn: introduce the ovpn_socket object")Signed-off-by Ralf Lici <[email protected]...>
Quoted 9 lines
---
Changes since v1:
- resetsk_user_datato NULL in case of error
- removed a redundant gotosock_releaseinovpn_socket_new
- expanded commit message with additional information on the purpose of this change
- added explanation in the commit message of why the per-protocolovpn_sockinitialization code inovpn_socket_newwas moved
- added Fixes tag
Thanks!
Signature · 1 line
Sabrina
Re: [Openvpn-devel] [PATCH ovpn net v2 2/3] ovpn: fix possible use-after-free in ovpn_net_xmit
In reply to Sabrina DubrocaQuoted 52 lines · Sabrina Dubroca
Quoted 46 lines · Ralf Lici
During GSO fragmentation,
skb_share_checkmay 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)")Signed-off-by Ralf Lici <[email protected]...>---
Changes since v1
- this is a new patch that replaces the previous "ovpn: usesk_buff_head
properly inovpn_net_xmit"drivers/net/ovpn/io.c | 11 +++++++++++
1 file changed, 11 insertions(+)drivers/net/ovpn/io.c +0
-0@@ -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_headrework instead. I think all segments produced byskb_gso_segmentwill have a dst attached (__copy_skb_header(nskb, head_skb)inskb_segment), so theskb_dst_dropafter peer selection will only affect the first segment, and not the others. Doing peer selection +skb_dst_dropbefore callingskb_gso_segmentwould 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