ovpn: set sk_user_data before overriding callbacks
Subsystems (2)
PATCH 1/3 ovpn: set sk_user_data before overriding callbacks
During initialization, we override some socket callbacks and set sk_user_data to ovpn_sock. Currently these two operations are decoupled:
the callbacks are overridden before sk_user_data is set, leaving a potentially ill-formed state while socket ownership is not yet complete. For example, if a packet arrives after ovpn_udp_socket_attach has been called but before ovpn_socket_new finishes, ovpn_udp_encap_recv may be invoked without a configured sk_user_data pointer.
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.
--- drivers/net/ovpn/socket.c | 38 +++++++++++++++++++++----------------- drivers/net/ovpn/tcp.c | 1 + drivers/net/ovpn/udp.c | 1 + 3 files changed, 23 insertions(+), 17 deletions(-)
drivers/net/ovpn/socket.c +21 -17
@@ -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,17 @@ 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 +1 -0
@@ -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
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 2/3 ovpn: fix VPN TX bytes counting
In ovpn_net_xmit, after GSO segmentation, skb points to the head of a list of segments. The current code uses skb->len to increment VPN TX statistics, but this only accounts for the first segment's length, ignoring all subsequent segments.
More critically, if the first segment fails skb_share_check, the skb is freed but the pointer remains. The subsequent skb->len access results in a use-after-free.
Fix both issues by accumulating the length of each segment that successfully passes skb_share_check and is queued for transmission.
--- 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); } skb_list.prev->next =NULL ;
@@ -418,7 +421,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
PATCH 3/3 ovpn: use sk_buff_head properly in ovpn_net_xmit
In reply to Ralf LiciThe current code builds an sk_buff_head after GSO segmentation but then treats it as a raw skb list: accessing elements via skb_list.next and breaking the list circularity by setting skb_list.prev->next to NULL.
Clean this up by changing ovpn_send to take an sk_buff_head parameter and use standard sk_buff_head APIs. Introduce ovpn_send_one helper to wrap single skbs in an sk_buff_head for ovpn_xmit_special.
--- drivers/net/ovpn/io.c | 74 +++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 28 deletions(-)
drivers/net/ovpn/io.c +46 -28
@@ -329,8 +329,8 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)return true; } -/* send skb to connected peer, if any */ -static void ovpn_send (struct ovpn_priv *ovpn,struct sk_buff *skb, +/* send skb_list to connected peer, if any */ +static void ovpn_send (struct ovpn_priv *ovpn,struct sk_buff_head *skb_list,struct ovpn_peer *peer) {struct sk_buff *curr, *next;
@@ -338,7 +338,8 @@ static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb,/* this might be a GSO-segmented skb list: process each skb * independently */ - skb_list_walk_safe(skb, curr, next) { + skb_queue_walk_safe(skb_list, curr, next) { +__skb_unlink (curr, skb_list);if (unlikely (!ovpn_encrypt_one (peer, curr))) {dev_dstats_tx_dropped (ovpn->dev);kfree_skb (curr);
@@ -368,6 +369,26 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)if (unlikely (!proto || skb->protocol != proto)) goto drop; +/* 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; + } +/* 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 );if (IS_ERR (segments)) {
@@ -381,8 +402,9 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) skb = segments; } -/* from this moment on, "skb" might be a list */ - +/* "skb" might be a raw list of skbs, transform it into a proper +* sk_buff_head list +*/ __skb_queue_head_init (&skb_list); skb_list_walk_safe(skb, curr, next) {skb_mark_not_on_list (curr);
@@ -399,40 +421,36 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) tx_bytes += curr->len;__skb_queue_tail (&skb_list, curr); } - skb_list.prev->next =NULL ; + skb =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 ; - } +if (unlikely (skb_queue_empty (&skb_list))) goto drop; - } -/* dst was needed for peer selection - it can now be dropped */ -skb_dst_drop (skb);ovpn_peer_stats_increment_tx (&peer->vpn_stats, tx_bytes); -ovpn_send (ovpn, skb_list.next, peer); +ovpn_send (ovpn, &skb_list, peer);return NETDEV_TX_OK ; drop:dev_dstats_tx_dropped (ovpn->dev); -skb_tx_error (skb); -kfree_skb_list (skb); +if (skb) { +skb_tx_error (skb); +kfree_skb_list (skb); + }return NETDEV_TX_OK ; } +/* wrap a single skb in a list in order to pass it to ovpn_send */ +static void ovpn_send_one (struct ovpn_priv *ovpn,struct sk_buff *skb, +struct ovpn_peer *peer) +{ +struct sk_buff_head list; + +__skb_queue_head_init (&list); +__skb_queue_tail (&list, skb); +ovpn_send (ovpn, &list, peer); +} +/** * ovpn_xmit_special - encrypt and transmit an out-of-band message to peer * @peer: peer to send the message to
@@ -464,5 +482,5 @@ void ovpn_xmit_special(struct ovpn_peer *peer, const void *data, skb->priority =TC_PRIO_BESTEFFORT ;__skb_put_data (skb, data, len); -ovpn_send (ovpn, skb, peer); +ovpn_send_one (ovpn, skb, peer); }
Signature · 1 line
2.52.0
Re: [Openvpn-devel] [PATCH ovpn net 3/3] ovpn: use sk_buff_head properly in ovpn_net_xmit
You're submitting this for "net" but this looks more like a clean up that should be sent to net-next. I don't know how Antonio will handle this, but for netdev submissions this distinction matters, see
https://docs.kernel.org/process/maintainer-netdev.html
Quoted 7 lines · Ralf Lici
The current code builds an
sk_buff_headafter GSO segmentation but then treats it as a raw skb list: accessing elements via skb_list.next and breaking the list circularity by settingskb_list.prev->nextto NULL.Clean this up by changing
ovpn_sendto take ansk_buff_headparameter and use standardsk_buff_headAPIs. Introduceovpn_send_onehelper to wrap single skbs in ansk_buff_headforovpn_xmit_special.
Not a strong objection, but I'm not so convinced by this clean up. Using a sk_buff_head is maybe a little bit nicer conceptually, but it doesn't result in a code improvement, especially since you have to add ovpn_send_one().
A few small comments on the implementation:
Quoted 21 lines
Signed-off-by Ralf Lici <[email protected]...>--- drivers/net/ovpn/io.c | 74 +++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 28 deletions(-)drivers/net/ovpn/io.c +2
-2@@ -329,8 +329,8 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)return true; } -/* send skb to connected peer, if any */ -static void ovpn_send (struct ovpn_priv *ovpn,struct sk_buff *skb, +/* send skb_list to connected peer, if any */ +static void ovpn_send (struct ovpn_priv *ovpn,struct sk_buff_head *skb_list,struct ovpn_peer *peer) {struct sk_buff *curr, *next;
drivers/net/ovpn/io.c static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb, · line 342
Quoted 6 lines
drivers/net/ovpn/io.c +2
-1@@ -338,7 +338,8 @@ static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb,/* this might be a GSO-segmented skb list: process each skb * independently */ - skb_list_walk_safe(skb, curr, next) { + skb_queue_walk_safe(skb_list, curr, next) { +__skb_unlink (curr, skb_list);
Why is this needed now but wasn't before?
Quoted 16 lines
drivers/net/ovpn/io.c +9
-0if (unlikely (!ovpn_encrypt_one (peer, curr))) {dev_dstats_tx_dropped (ovpn->dev);kfree_skb (curr);@@ -368,6 +369,26 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)if (unlikely (!proto || skb->protocol != proto)) goto drop; +/* 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 ;
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 390
Quoted 10 lines
drivers/net/ovpn/io.c +10
-0+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; + } +/* dst was needed for peer selection - it can now be dropped */ +skb_dst_drop (skb);
Moving this code looks like a separate clean up? Or is this needed for the conversion to sk_buff_head?
Signature · 1 line
Sabrina
Re: [Openvpn-devel] [PATCH ovpn net 3/3] ovpn: use sk_buff_head properly in ovpn_net_xmit
Quoted 4 lines · Sabrina Dubroca
You're submitting this for "net" but this looks more like a clean up that should be sent to net-next. I don't know how Antonio will handle this, but for netdev submissions this distinction matters, see
https://docs.kernel.org/process/maintainer-netdev.html
I think you are right. I initially considered this a fix, but I agree it's more appropriate for net-next. I'll leave it to Antonio to place it in the right PR.
Quoted 14 lines
Quoted 7 lines · Ralf Lici
The current code builds an
sk_buff_headafter GSO segmentation but then
treats it as a raw skb list: accessing elements via skb_list.next and
breaking the list circularity by settingskb_list.prev->nextto NULL.Quoted 5 lines
Clean this up by changing
ovpn_sendto take ansk_buff_headparameter
and use standardsk_buff_headAPIs. Introduceovpn_send_onehelper to
wrap single skbs in ansk_buff_headforovpn_xmit_special.
Quoted 5 lines
Not a strong objection, but I'm not so convinced by this clean up. Using a
sk_buff_headis maybe a little bit nicer conceptually, but it doesn't result in a code improvement, especially since you have to addovpn_send_one().
I agree this adds a small helper, but what motivated this change is that the current usage of sk_buff_head in ovpn_net_xmit looks a bit like a hack or an API violation. In the current code, we build an sk_buff_head but then manually break the ring (skb_list.prev->next = NULL) to treat it as a raw singly linked chain. This relies on implementation details rather than the API. Even though we do not (and likely will not) need the circularity, staying within the API feels safer and more maintainable.
Also, the overhead added by ovpn_send_one should be negligible, since it is only used for keepalives.
Hopefully that makes sense.
Quoted 27 lines
A few small comments on the implementation:
Quoted 25 lines
Signed-off-by Ralf Lici <[email protected]...>---
drivers/net/ovpn/io.c | 74 +++++++++++++++++++++++++++-------------
---1 file changed, 46 insertions(+), 28 deletions(-)drivers/net/ovpn/io.c +0
-0@@ -329,8 +329,8 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) return true; }drivers/net/ovpn/io.c +2
-2-/* send skb to connected peer, if any */ -static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb, +/* send skb_list to connected peer, if any */ +static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff_head
*skb_list,
structovpn_peer*peer)
{
structsk_buff*curr, *next;drivers/net/ovpn/io.c +0
-0@@ -338,7 +338,8 @@ static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb,
Quoted 8 lines
drivers/net/ovpn/io.c static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb, · line 342
Quoted 6 lines
drivers/net/ovpn/io.c +2
-1/* this might be a GSO-segmented skb list: process each skb* independently */ - skb_list_walk_safe(skb, curr, next) { + skb_queue_walk_safe(skb_list, curr, next) { +__skb_unlink (curr, skb_list);Why is this needed now but wasn't before?
Since the new code aims to use skb_list as a proper circular doubly linked list and to comply with the sk_buff_head API, the unlinking is necessary to maintain a valid skb_list while processing each of its elements. This also seems to be a common pattern throughout the kernel.
It is true, though, that after ovpn_send we do not use skb_list anymore, so this is not strictly necessary. Let me know if you think I should remove it.
Quoted 26 lines
Quoted 26 lines
drivers/net/ovpn/io.c +0
-0if (unlikely(!ovpn_encrypt_one(peer, curr))) { dev_dstats_tx_dropped(ovpn->dev);
kfree_skb(curr);drivers/net/ovpn/io.c +0
-0@@ -368,6 +369,26 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) if (unlikely(!proto || skb->protocol != proto)) goto drop;drivers/net/ovpn/io.c +1
-0+/* retrieve peer serving the destination IP of this packet */
drivers/net/ovpn/io.c +5
-0+ 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 senddata to dst=%pI4\n",
drivers/net/ovpn/io.c +5
-0+ netdev_name(ovpn->dev), + &ip_hdr(skb)->daddr); +break ; +case htons (ETH_P_IPV6 ): + net_dbg_ratelimited("%s: no peer to senddata to dst=%pI6c\n",
drivers/net/ovpn/io.c +4
-0+ netdev_name(ovpn->dev), + &ipv6_hdr(skb)->daddr); +break ; + }
Quoted 8 lines
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 390
Quoted 5 lines
drivers/net/ovpn/io.c +3
-0+ goto drop; + } +/* dst was needed for peer selection - it can now be dropped */
drivers/net/ovpn/io.c +1
-0+skb_dst_drop (skb);Moving this code looks like a separate clean up? Or is this needed for the conversion to
sk_buff_head?
While this could technically be a separate cleanup, I included it here because it reorganizes the function to accommodate the new lifecycle of the skb variable. Once we commit to populating the sk_buff_head and nullifying the original skb, the peer lookup logically has to happen earlier. Grouping these changes keeps the function coherent throughout the refactoring. Otherwise we would have had to pass skb_list.next to ovpn_peer_get_by_dst, which would have defeated the purpose of the patch.
That said, I recognize it could also be viewed as a standalone patch. If you prefer a 2-step cleanup for better atomicity, I am happy to split this into two commits: one for the peer retrieval move and another for the sk_buff_head API transition. Please let me know your preference.
Signature · 2 lines
Ralf Lici Mandelbit Srl
Re: [Openvpn-devel] [PATCH ovpn net 3/3] ovpn: use sk_buff_head properly in ovpn_net_xmit
Quoted 30 lines · Ralf Lici
Quoted 4 lines · Sabrina Dubroca
You're submitting this for "net" but this looks more like a clean up that should be sent to net-next. I don't know how Antonio will handle this, but for netdev submissions this distinction matters, see
https://docs.kernel.org/process/maintainer-netdev.htmlI think you are right. I initially considered this a fix, but I agree it's more appropriate for net-next. I'll leave it to Antonio to place it in the right PR.
Quoted 14 lines
Quoted 7 lines · Ralf Lici
The current code builds an
sk_buff_headafter GSO segmentation but then
treats it as a raw skb list: accessing elements via skb_list.next and
breaking the list circularity by settingskb_list.prev->nextto NULL.Quoted 5 lines
Clean this up by changing
ovpn_sendto take ansk_buff_headparameter
and use standardsk_buff_headAPIs. Introduceovpn_send_onehelper to
wrap single skbs in ansk_buff_headforovpn_xmit_special.Quoted 5 lines
Not a strong objection, but I'm not so convinced by this clean up. Using a
sk_buff_headis maybe a little bit nicer conceptually, but it doesn't result in a code improvement, especially since you have to addovpn_send_one().
Quoted 5 lines
I agree this adds a small helper, but what motivated this change is that the current usage of
sk_buff_headinovpn_net_xmitlooks a bit like a hack or an API violation. In the current code, we build ansk_buff_headbut then manually break the ring (skb_list.prev->next= NULL) to treat it as a raw singly linked chain.
I'd say we're building a skb list and happen to use an sk_buff_head to make it easier. So I'm not really convinced by the benefit of this patch.
Quoted 7 lines
This relies on implementation details
rather than the API. Even though we do not (and likely will not) need the circularity, staying within the API feels safer and more maintainable.Also, the overhead added by
ovpn_send_oneshould be negligible, since it is only used for keepalives.
Sure, my comment was more about code simplicity than runtime overhead.
Quoted 39 lines
Hopefully that makes sense.
Quoted 27 lines
A few small comments on the implementation:
Quoted 25 lines
Signed-off-by Ralf Lici <[email protected]...>---
drivers/net/ovpn/io.c | 74 +++++++++++++++++++++++++++-------------
---1 file changed, 46 insertions(+), 28 deletions(-)drivers/net/ovpn/io.c +0
-0@@ -329,8 +329,8 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb) return true; }drivers/net/ovpn/io.c +2
-2-/* send skb to connected peer, if any */ -static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb, +/* send skb_list to connected peer, if any */ +static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff_head
*skb_list,
structovpn_peer*peer)
{
structsk_buff*curr, *next;drivers/net/ovpn/io.c +0
-0@@ -338,7 +338,8 @@ static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb,Quoted 8 lines
drivers/net/ovpn/io.c static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb, · line 342
Quoted 6 lines
drivers/net/ovpn/io.c +2
-1/* this might be a GSO-segmented skb list: process each skb* independently */ - skb_list_walk_safe(skb, curr, next) { + skb_queue_walk_safe(skb_list, curr, next) { +__skb_unlink (curr, skb_list);Why is this needed now but wasn't before?
Quoted 8 lines
Since the new code aims to use
skb_listas a proper circular doubly linked list and to comply with thesk_buff_headAPI, the unlinking is necessary to maintain a validskb_listwhile processing each of its elements. This also seems to be a common pattern throughout the kernel.It is true, though, that after
ovpn_sendwe do not useskb_listanymore, so this is not strictly necessary. Let me know if you think I should remove it.
If the aim is to properly use the sk_buff_head API, then I guess it makes sense.
The skb_mark_not_on_list() call in ovpn_encrypt_post is not needed anymore.
Quoted 44 lines
Quoted 26 lines
Quoted 26 lines
drivers/net/ovpn/io.c +0
-0if (unlikely(!ovpn_encrypt_one(peer, curr))) { dev_dstats_tx_dropped(ovpn->dev);
kfree_skb(curr);drivers/net/ovpn/io.c +0
-0@@ -368,6 +369,26 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) if (unlikely(!proto || skb->protocol != proto)) goto drop;drivers/net/ovpn/io.c +1
-0+/* retrieve peer serving the destination IP of this packet */
drivers/net/ovpn/io.c +5
-0+ 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 senddata to dst=%pI4\n",
drivers/net/ovpn/io.c +5
-0+ netdev_name(ovpn->dev), + &ip_hdr(skb)->daddr); +break ; +case htons (ETH_P_IPV6 ): + net_dbg_ratelimited("%s: no peer to senddata to dst=%pI6c\n",
drivers/net/ovpn/io.c +4
-0+ netdev_name(ovpn->dev), + &ipv6_hdr(skb)->daddr); +break ; + }Quoted 8 lines
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 390
Quoted 5 lines
drivers/net/ovpn/io.c +3
-0+ goto drop; + } +/* dst was needed for peer selection - it can now be dropped */
drivers/net/ovpn/io.c +1
-0+skb_dst_drop (skb);Moving this code looks like a separate clean up? Or is this needed for the conversion to
sk_buff_head?While this could technically be a separate cleanup, I included it here because it reorganizes the function to accommodate the new lifecycle of the skb variable. Once we commit to populating the
sk_buff_headand nullifying the original skb, the peer lookup logically has to happen earlier. Grouping these changes keeps the function coherent throughout the refactoring. Otherwise we would have had to pass skb_list.next toovpn_peer_get_by_dst, which would have defeated the purpose of the patch.
Quoted 4 lines
That said, I recognize it could also be viewed as a standalone patch. If you prefer a 2-step cleanup for better atomicity, I am happy to split this into two commits: one for the peer retrieval move and another for the
sk_buff_headAPI transition. Please let me know your preference.
Maybe. Either way, I think you're missing a call to ovpn_peer_put() in the error cases that happen after fetching the peer. Before this patch there was no possible failure once we'd grabbed the peer.
One more semi-nitpicky comment:
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 427
Quoted 2 lines
drivers/net/ovpn/io.c +1
-0+if (unlikely (skb_queue_empty (&skb_list))) goto drop;
Is this needed? If the queue is empty we've already counted one drop for each segment during the list-to-sk_buff_head loop, and ovpn_send should handle an empty queue without problem?
Signature · 1 line
Sabrina
Re: [Openvpn-devel] [PATCH ovpn net 3/3] ovpn: use sk_buff_head properly in ovpn_net_xmit
Quoted 50 lines · Sabrina Dubroca
Quoted 39 lines · Ralf Lici
Quoted 6 lines · Sabrina Dubroca
You're submitting this for "net" but this looks more like a clean up
that should be sent to net-next. I don't know how Antonio will handle
this, but for netdev submissions this distinction matters, see
https://docs.kernel.org/process/maintainer-netdev.htmlI think you are right. I initially considered this a fix, but I agree
it's more appropriate for net-next. I'll leave it to Antonio to place it
in the right PR.Quoted 17 lines
Quoted 9 lines · Ralf Lici
The current code builds an
sk_buff_headafter GSO segmentation but
then
treats it as a raw skb list: accessing elements via skb_list.next
and
breaking the list circularity by settingskb_list.prev->nextto NULL.Quoted 6 lines
Clean this up by changing
ovpn_sendto take ansk_buff_headparameter
and use standardsk_buff_headAPIs. Introduceovpn_send_onehelper
to
wrap single skbs in ansk_buff_headforovpn_xmit_special.Quoted 7 lines
Not a strong objection, but I'm not so convinced by this clean up. Using a
sk_buff_headis maybe a little bit nicer conceptually, but
it doesn't result in a code improvement, especially since you have to
addovpn_send_one().Quoted 9 lines
I agree this adds a small helper, but what motivated this change is that
the current usage ofsk_buff_headinovpn_net_xmitlooks a bit like a
hack or an API violation. In the current code, we build ansk_buff_head
but then manually break the ring (skb_list.prev->next= NULL) to treat
it as a raw singly linked chain.
Quoted 3 lines
I'd say we're building a skb list and happen to use an
sk_buff_headto make it easier. So I'm not really convinced by the benefit of this patch.
I understand your point regarding the simplicity and convenience of the original implementation. After discussing this further with Antonio, we have decided to drop this change and maintain the current structure and sequence of operations in ovpn_net_xmit.
Anyway, since there's currently a risk of UAF when using the 'skb' variable after calling skb_share_check, I'll add a patch to address that specific issue maintaining the current pattern.
Thank you for the feedback.
Quoted 154 lines
Quoted 10 lines
This relies on implementation details
rather than the API. Even though we do not (and likely will not) need
the circularity, staying within the API feels safer and more maintainable.Also, the overhead added by
ovpn_send_oneshould be negligible, since it
is only used for keepalives.Sure, my comment was more about code simplicity than runtime overhead.
Quoted 52 lines
Hopefully that makes sense.
Quoted 33 lines
A few small comments on the implementation:
Quoted 31 lines
Signed-off-by Ralf Lici <[email protected]...>---
drivers/net/ovpn/io.c | 74 +++++++++++++++++++++++++++---------
----
---1 file changed, 46 insertions(+), 28 deletions(-)drivers/net/ovpn/io.c +0
-0@@ -329,8 +329,8 @@ static bool ovpn_encrypt_one(struct
ovpn_peer
*peer, structsk_buff*skb)
return true;
}drivers/net/ovpn/io.c +0
-2-/* send skb to connected peer, if any */ -static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff*skb,
drivers/net/ovpn/io.c +2
-0+/* send skb_list to connected peer, if any */ +static void ovpn_send(struct ovpn_priv *ovpn, struct
sk_buff_head*skb_list,
structovpn_peer*peer)
{
structsk_buff*curr, *next;drivers/net/ovpn/io.c +0
-0@@ -338,7 +338,8 @@ static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb, /* this might be a GSO-segmented skb list: process eachQuoted 8 lines
drivers/net/ovpn/io.c static void ovpn_send(struct ovpn_priv *ovpn, struct sk_buff *skb, · line 342
Quoted 6 lines
skb
* independently
*/drivers/net/ovpn/io.c +2
-1- skb_list_walk_safe(skb, curr, next) { + skb_queue_walk_safe(skb_list, curr, next) { +__skb_unlink (curr, skb_list);Why is this needed now but wasn't before?
Since the new code aims to use
skb_listas a proper circular doubly linked list and to comply with thesk_buff_headAPI, the unlinking is
necessary to maintain a validskb_listwhile processing each of its elements. This also seems to be a common pattern throughout the kernel.Quoted 4 lines
It is true, though, that after
ovpn_sendwe do not useskb_listanymore,
so this is not strictly necessary. Let me know if you think I should remove it.If the aim is to properly use the
sk_buff_headAPI, then I guess it makes sense.The
skb_mark_not_on_list()call inovpn_encrypt_postis not needed anymore.Quoted 57 lines
Quoted 32 lines
Quoted 32 lines
if (
unlikely(!ovpn_encrypt_one(peer, curr))) {dev_dstats_tx_dropped(ovpn->dev);kfree_skb(curr);drivers/net/ovpn/io.c +0
-0@@ -368,6 +369,26 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) if (unlikely(!proto || skb->protocol != proto)) goto drop;drivers/net/ovpn/io.c +1
-0+/* retrieve peer serving the destination IP of this packet
*/drivers/net/ovpn/io.c +5
-0+ 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 tosend
data to dst=%pI4\n",drivers/net/ovpn/io.c +1
-0+ netdev_name(ovpn-Quoted 1 line
dev),
drivers/net/ovpn/io.c +1
-0+ &ip_hdr(skb)-Quoted 1 line
daddr);
drivers/net/ovpn/io.c +3
-0+break ; +case htons (ETH_P_IPV6 ): + net_dbg_ratelimited("%s: no peer tosend
data to dst=%pI6c\n",drivers/net/ovpn/io.c +1
-0+ netdev_name(ovpn-Quoted 1 line
dev),
drivers/net/ovpn/io.c +1
-0+ &ipv6_hdr(skb)-Quoted 1 line
daddr);
Quoted 11 lines
drivers/net/ovpn/io.c netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 390
Quoted 7 lines
drivers/net/ovpn/io.c +5
-0+break ; + } + goto drop; + } +/* dst was needed for peer selection - it can now be dropped */
drivers/net/ovpn/io.c +1
-0+skb_dst_drop (skb);Moving this code looks like a separate clean up? Or is this needed for
the conversion tosk_buff_head?While this could technically be a separate cleanup, I included it here
because it reorganizes the function to accommodate the new lifecycle of
the skb variable. Once we commit to populating thesk_buff_headand nullifying the original skb, the peer lookup logically has to happen earlier. Grouping these changes keeps the function coherent throughout
the refactoring. Otherwise we would have had to pass skb_list.next toovpn_peer_get_by_dst, which would have defeated the purpose of the patch.Quoted 7 lines
That said, I recognize it could also be viewed as a standalone patch. If
you prefer a 2-step cleanup for better atomicity, I am happy to split
this into two commits: one for the peer retrieval move and another for
thesk_buff_headAPI transition. Please let me know your preference.Maybe. Either way, I think you're missing a call to
ovpn_peer_put()in the error cases that happen after fetching the peer. Before this patch there was no possible failure once we'd grabbed the peer.One more semi-nitpicky comment:
Quoted 2 lines
drivers/net/ovpn/io.c +1
-0+if (unlikely (skb_queue_empty (&skb_list)))goto drop;
Is this needed? If the queue is empty we've already counted one drop for each segment during the list-to-sk_buff_head loop, and
ovpn_sendshould handle an empty queue without problem?
Signature · 2 lines
Ralf Lici Mandelbit Srl
Quoted 7 lines · Ralf Lici
During initialization, we override some socket callbacks and set
sk_user_datatoovpn_sock. Currently these two operations are decoupled:
the callbacks are overridden beforesk_user_datais set, leaving a potentially ill-formed state while socket ownership is not yet complete. For example, if a packet arrives afterovpn_udp_socket_attachhas been called but beforeovpn_socket_newfinishes,ovpn_udp_encap_recvmay be invoked without a configuredsk_user_datapointer.
Since all the callbacks check whether sk_user_data is set before doing anything, can you describe the problem?
Quoted 5 lines
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.
A Fixes: tag would be good to add here. (probably f6226ae7a0cd ("ovpn:
introduce the ovpn_socket object"), don't worry too much about which exact patch within the initial series is best)
Quoted 21 lines
Signed-off-by Ralf Lici <[email protected]...>--- drivers/net/ovpn/socket.c | 38 +++++++++++++++++++++----------------- drivers/net/ovpn/tcp.c | 1 + drivers/net/ovpn/udp.c | 1 + 3 files changed, 23 insertions(+), 17 deletions(-)drivers/net/ovpn/socket.c +6
-0@@ -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;
drivers/net/ovpn/socket.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 218
Quoted 10 lines
drivers/net/ovpn/socket.c +10
-0+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 ); + } +
Can you add a few words in the commit message to describe why this needs to be moved?
Quoted 43 lines
drivers/net/ovpn/socket.c +5
-17/* the newly created ovpn_socket is holding reference to sk, * therefore we increase its refcounter. * @@ -212,29 +228,17 @@ 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 +0
-0
drivers/net/ovpn/tcp.c int ovpn_tcp_socket_attach(struct ovpn_socket *ovpn_sock, · line 490
Quoted 4 lines
drivers/net/ovpn/tcp.c +1
-0@@ -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);
And reset sk_user_data to NULL in the error cases that can happen later in ovpn_tcp_socket_attach?
Quoted 17 lines
drivers/net/ovpn/tcp.c +0
-0/* only a fully connected socket is expected. Connection should be * handled in userspace 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
Signature · 1 line
Sabrina
Quoted 13 lines · Sabrina Dubroca
Quoted 11 lines · Ralf Lici
During initialization, we override some socket callbacks and set
sk_user_datatoovpn_sock. Currently these two operations are decoupled:
the callbacks are overridden beforesk_user_datais set, leaving a potentially ill-formed state while socket ownership is not yet complete.
For example, if a packet arrives afterovpn_udp_socket_attachhas been
called but beforeovpn_socket_newfinishes,ovpn_udp_encap_recvmay be
invoked without a configuredsk_user_datapointer.
Quoted 2 lines
Since all the callbacks check whether
sk_user_datais set before doing anything, can you describe the problem?
True, but if we receive UDP traffic before the socket setup is completed, we get some net_err_ratelimited messages in the logs. While this is not necessarily a real bug, setting user_data before the callbacks ensures we do not hit this situation and avoids polluting the logs. This should also be safe, since no other user is "owning" the socket at that point.
I will update the commit message with this info.
Quoted 9 lines
Quoted 8 lines
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.
Quoted 3 lines
A Fixes: tag would be good to add here. (probably
f6226ae7a0cd("ovpn:
introduce theovpn_socketobject"), don't worry too much about which exact patch within the initial series is best)
Sure, I'll add it.
Quoted 33 lines
Quoted 33 lines
Signed-off-by Ralf Lici <[email protected]...>---
drivers/net/ovpn/socket.c | 38 +++++++++++++++++++++--------------- --
drivers/net/ovpn/tcp.c | 1 +
drivers/net/ovpn/udp.c | 1 +3 files changed, 23 insertions(+), 17 deletions(-)drivers/net/ovpn/socket.c +0
-0@@ -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);drivers/net/ovpn/socket.c +1
-0+/* TCP sockets are per-peer, therefore they are linked to their unique
drivers/net/ovpn/socket.c +4
-0+* peer +*/ +if (sk->sk_protocol ==IPPROTO_TCP ) { + INIT_WORK(&ovpn_sock->tcp_tx_work,
ovpn_tcp_tx_work);drivers/net/ovpn/socket.c +4
-0+ 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
drivers/net/ovpn/socket.c +4
-0+* shared among multiple peers +*/ + ovpn_sock->ovpn = peer->ovpn; + netdev_hold(peer->ovpn->dev, &ovpn_sock-
Quoted 7 lines
drivers/net/ovpn/socket.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 218
Quoted 4 lines
Quoted 1 line
dev_tracker,drivers/net/ovpn/socket.c +3
-0+ GFP_KERNEL); + } +Can you add a few words in the commit message to describe why this needs to be moved?
Of course, I'll specify why this was moved.
Quoted 51 lines
Quoted 51 lines
/* the newly created
ovpn_socketis holding reference to sk,
* therefore we increase its refcounter.
*drivers/net/ovpn/socket.c +0
-0@@ -212,29 +228,17 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)
ret =ovpn_socket_attach(ovpn_sock, sock, peer); if (ret < 0) {drivers/net/ovpn/socket.c +4
-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-Quoted 1 line
dev_tracker);+
sock_put(sk);
kfree(ovpn_sock);
ovpn_sock=ERR_PTR(ret);
gotosock_release;
}drivers/net/ovpn/socket.c +0
-1-/* TCP sockets are per-peer, therefore they are linked to their unique
drivers/net/ovpn/socket.c +0
-4-* peer -*/ -if (sk->sk_protocol ==IPPROTO_TCP ) { - INIT_WORK(&ovpn_sock->tcp_tx_work,
ovpn_tcp_tx_work);drivers/net/ovpn/socket.c +0
-4- 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
drivers/net/ovpn/socket.c +0
-4-* shared among multiple peers -*/ - ovpn_sock->ovpn = peer->ovpn; - netdev_hold(peer->ovpn->dev, &ovpn_sock-Quoted 1 line
dev_tracker,drivers/net/ovpn/socket.c +0
-4- GFP_KERNEL); - } - -rcu_assign_sk_user_data (sk, ovpn_sock);sock_release:
release_sock(sk);
returnovpn_sock;drivers/net/ovpn/tcp.c +0
-0@@ -487,6 +487,7 @@ int ovpn_tcp_socket_attach(struct ovpn_socket *ovpn_sock,
Quoted 8 lines
drivers/net/ovpn/tcp.c int ovpn_tcp_socket_attach(struct ovpn_socket *ovpn_sock, · line 490
Quoted 5 lines
drivers/net/ovpn/tcp.c +1
-0/* 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);And reset
sk_user_datato NULL in the error cases that can happen later inovpn_tcp_socket_attach?
Right, will fix.
Quoted 1 line
Signature · 2 lines
Ralf Lici Mandelbit Srl