Skip to contentSkip to conversation

pull request: fixes for ovpn 2025-05-27

PATCH In review Antonio Quartulli 2 participants 9 messages May 27, 2025 – May 29, 2025

Subsystems (6)
lore
Share & tools
lore

Antonio Quartulli

author maintainer
#0 lore raw

cover pull request: fixes for ovpn 2025-05-27

Hi netdev-team,
I am targetting net-next because ovpn has been merged to net yet.

In this batch you can find two major bugfixes:

Patch 1: when releasing a UDP socket we were wrongly invoking setup_udp_tunnel_sock() with an empty config. This was not properly shutting down the UDP encap state.
This patch fixes this misbehaviour by implementing an "undo" function called cleanup_udp_tunnel_sock() which properly takes care or unwinding the encap state. Implementing this function required some extra changes to the udp/udp_tunnel code, especially to avoid race conditions with udp_destroy_sock().

Patch 2: ovpn was holding a reference to a 'struct socket' without increasing its reference counter. This was intended and worked as expected until we hit a race condition where user space tries to close the socket while kernel space is also releasing it. In this case the (struct socket *)->sk member would disappear under our feet leading to a null-ptr-deref. This patch fixes this issue by having ovpn hold a reference directly to the sk member while also increasing its reference counter.

Patch 3 and 4 are instead fixing minor issues in the ovpn kselftests.

Please pull.

Thanks a lot,
Antonio

The following changes since commit b2908a989c594f9eb1c93016abc1382f97ee02b1:

net: phy: add driver for MaxLinear MxL86110 PHY (2025-05-27 09:36:17 +0200)

are available in the Git repository at:

https://github.com/OpenVPN/ovpn-net-next tags/ovpn-net-next-20250527

for you to fetch changes up to 545e4c173cf94b4e5bbfe8ee09de2fbe556bb75a:

selftest/net/ovpn: fix missing file (2025-05-27 13:50:50 +0200)

----------------------------------------------------------------
This bugfix batch includes the following changes:
* dropped call to setup_udp_tunnel_sock() during cleanup, substituted by new cleanup_udp_tunnel_sock(), which required:
** implementing udp_tunnel_encap_disable()
** implementing udpv6_encap_disable()
** calling udp_test_and_clear_bit() in udp_destroy_socket()
* fixed race condition between peer removal (by kernel space) and socket closing (by user space)
* fixes for ovpn kselftests

----------------------------------------------------------------
Antonio Quartulli (4):
ovpn: properly deconfigure UDP-tunnel
ovpn: ensure sk is still valid during cleanup selftest/net/ovpn: fix TCP socket creation selftest/net/ovpn: fix missing file

 drivers/net/ovpn/io.c                              |  8 +--
 drivers/net/ovpn/netlink.c                         | 25 ++++----
 drivers/net/ovpn/peer.c                            |  4 +-
 drivers/net/ovpn/socket.c                          | 68 ++++++++++++----------
 drivers/net/ovpn/socket.h                          |  4 +-
 drivers/net/ovpn/tcp.c                             | 65 ++++++++++-----------
 drivers/net/ovpn/tcp.h                             |  3 +-
 drivers/net/ovpn/udp.c                             | 37 ++++--------
 drivers/net/ovpn/udp.h                             |  4 +-
 include/linux/udp.h                                |  2 +
 include/net/ipv6_stubs.h                           |  1 +
 include/net/udp.h                                  |  1 +
 include/net/udp_tunnel.h                           | 13 +++++
 net/ipv4/udp.c                                     |  2 +-
 net/ipv4/udp_tunnel_core.c                         | 22 +++++++
 net/ipv6/af_inet6.c                                |  1 +
 net/ipv6/udp.c                                     |  8 ++-
 tools/testing/selftests/net/ovpn/ovpn-cli.c        |  1 +
 tools/testing/selftests/net/ovpn/test-large-mtu.sh |  9 +++
 19 files changed, 167 insertions(+), 111 deletions(-)
 create mode 100755 tools/testing/selftests/net/ovpn/test-large-mtu.sh
author maintainer
#1 lore raw

PATCH 1/4 ovpn: properly deconfigure UDP-tunnel

When deconfiguring a UDP-tunnel from a socket, we cannot call setup_udp_tunnel_sock(), as it is expected to be invoked only during setup.

Implement a new function named cleanup_udp_tunnel_sock(), that reverts was what done during setup, and invoke it.

This new function takes care of reverting everything that was done by setup_udp_tunnel_sock() (i.e. unsetting various members and cleaning up the encap state in the kernel).

Note that cleanup_udp_tunnel_sock() takes 'struct sock' as argument in preparation for a follow up patch, where 'struct ovpn' won't hold any reference to any 'struct socket' any more, but rather to its 'struct sock' member only.

To properly implement cleanup_udp_tunnel_sock(), two extra items were required:

1) implement udpv6_encap_disable() in order to allow udpv6_encap_needed_key to be decreased outside of ipv6.ko (similarly to udp_encap_disable() for IPv4).

2) avoid race conditions with udp_destroy_socket() by atomically testing-and-clearing the ENCAP_ENABLED bit on the socket before decreasing udp_encap_needed_key. This way we prevent accidental double decreses.

Cc Willem de Bruijn <[email protected]>
Cc Simon Horman <[email protected]>
Cc David Ahern <[email protected]>
Cc Sabrina Dubroca <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Fixes ab66abbc769b ("ovpn: implement basic RX path (UDP)")
---
 drivers/net/ovpn/udp.c     |  5 +----
 include/linux/udp.h        |  2 ++
 include/net/ipv6_stubs.h   |  1 +
 include/net/udp.h          |  1 +
 include/net/udp_tunnel.h   | 13 +++++++++++++
 net/ipv4/udp.c             |  2 +-
 net/ipv4/udp_tunnel_core.c | 22 ++++++++++++++++++++++
 net/ipv6/af_inet6.c        |  1 +
 net/ipv6/udp.c             |  8 +++++++-
 9 files changed, 49 insertions(+), 6 deletions(-)
drivers/net/ovpn/udp.c +1 -4
index aef8c0406ec9..c8a81c4d6489 100644
@@ -442,8 +442,5 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock,
  */
 void ovpn_udp_socket_detach(struct ovpn_socket *ovpn_sock)
 {
-	struct udp_tunnel_sock_cfg cfg = { };
-
-	setup_udp_tunnel_sock(sock_net(ovpn_sock->sock->sk), ovpn_sock->sock,
-			      &cfg);
+	cleanup_udp_tunnel_sock(ovpn_sock->sock->sk);
 }
include/linux/udp.h +2 -0
index 4e1a672af4c5..97151f25953d 100644
@@ -116,6 +116,8 @@ struct udp_sock {
 	set_bit(UDP_FLAGS_##nr, &udp_sk(sk)->udp_flags)
 #define udp_test_and_set_bit(nr, sk)		\
 	test_and_set_bit(UDP_FLAGS_##nr, &udp_sk(sk)->udp_flags)
+#define udp_test_and_clear_bit(nr, sk)		\
+	test_and_clear_bit(UDP_FLAGS_##nr, &udp_sk(sk)->udp_flags)
 #define udp_clear_bit(nr, sk)			\
 	clear_bit(UDP_FLAGS_##nr, &udp_sk(sk)->udp_flags)
 #define udp_assign_bit(nr, sk, val)		\
include/net/ipv6_stubs.h +1 -0
index 8a3465c8c2c5..2a57df87d11b 100644
@@ -55,6 +55,7 @@ struct ipv6_stub {
 			       struct nl_info *info);
 
 	void (*udpv6_encap_enable)(void);
+	void (*udpv6_encap_disable)(void);
 	void (*ndisc_send_na)(struct net_device *dev, const struct in6_addr *daddr,
 			      const struct in6_addr *solicited_addr,
 			      bool router, bool solicited, bool override, bool inc_opt);
include/net/udp.h +1 -0
index a772510b2aa5..e3d7b8622f59 100644
@@ -580,6 +580,7 @@ void udp_encap_disable(void);
 #if IS_ENABLED(CONFIG_IPV6)
 DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
 void udpv6_encap_enable(void);
+void udpv6_encap_disable(void);
 #endif
 
 static inline struct sk_buff *udp_rcv_segment(struct sock *sk,
include/net/udp_tunnel.h +13 -0
index 2df3b8344eb5..a14b171e7dd5 100644
@@ -92,6 +92,7 @@ struct udp_tunnel_sock_cfg {
 /* Setup the given (UDP) sock to receive UDP encapsulated packets */
 void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
 			   struct udp_tunnel_sock_cfg *sock_cfg);
+void cleanup_udp_tunnel_sock(struct sock *sk);
 
 /* -- List of parsable UDP tunnel types --
  *
@@ -218,6 +219,18 @@ static inline void udp_tunnel_encap_enable(struct sock *sk)
 	udp_encap_enable();
 }
 
+static inline void udp_tunnel_encap_disable(struct sock *sk)
+{
+	if (!udp_test_and_clear_bit(ENCAP_ENABLED, sk))
+		return;
+
+#if IS_ENABLED(CONFIG_IPV6)
+	if (READ_ONCE(sk->sk_family) == PF_INET6)
+		ipv6_stub->udpv6_encap_disable();
+#endif
+	udp_encap_disable();
+}
+
 #define UDP_TUNNEL_NIC_MAX_TABLES	4
 
 enum udp_tunnel_nic_info_flags {
net/ipv4/udp.c +1 -1
index dde52b8050b8..9ffc4e0b1644 100644
@@ -2900,7 +2900,7 @@ void udp_destroy_sock(struct sock *sk)
 			if (encap_destroy)
 				encap_destroy(sk);
 		}
-		if (udp_test_bit(ENCAP_ENABLED, sk)) {
+		if (udp_test_and_clear_bit(ENCAP_ENABLED, sk)) {
 			static_branch_dec(&udp_encap_needed_key);
 			udp_tunnel_cleanup_gro(sk);
 		}
net/ipv4/udp_tunnel_core.c +22 -0
index 2326548997d3..624b6afcf812 100644
@@ -98,6 +98,28 @@ void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
 }
 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
 
+void cleanup_udp_tunnel_sock(struct sock *sk)
+{
+	/* Re-enable multicast loopback */
+	inet_set_bit(MC_LOOP, sk);
+
+	/* Disable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
+	inet_dec_convert_csum(sk);
+
+	udp_sk(sk)->encap_type = 0;
+	udp_sk(sk)->encap_rcv = NULL;
+	udp_sk(sk)->encap_err_rcv = NULL;
+	udp_sk(sk)->encap_err_lookup = NULL;
+	udp_sk(sk)->encap_destroy = NULL;
+	udp_sk(sk)->gro_receive = NULL;
+	udp_sk(sk)->gro_complete = NULL;
+
+	rcu_assign_sk_user_data(sk, NULL);
+	udp_tunnel_encap_disable(sk);
+	udp_tunnel_cleanup_gro(sk);
+}
+EXPORT_SYMBOL_GPL(cleanup_udp_tunnel_sock);
+
 void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
 			     unsigned short type)
 {
net/ipv6/af_inet6.c +1 -0
index acaff1296783..f3745705a811 100644
@@ -1049,6 +1049,7 @@ static const struct ipv6_stub ipv6_stub_impl = {
 	.fib6_rt_update	   = fib6_rt_update,
 	.ip6_del_rt	   = ip6_del_rt,
 	.udpv6_encap_enable = udpv6_encap_enable,
+	.udpv6_encap_disable = udpv6_encap_disable,
 	.ndisc_send_na = ndisc_send_na,
 #if IS_ENABLED(CONFIG_XFRM)
 	.xfrm6_local_rxpmtu = xfrm6_local_rxpmtu,
net/ipv6/udp.c +7 -1
index 7317f8e053f1..a919458772d6 100644
@@ -602,6 +602,12 @@ void udpv6_encap_enable(void)
 }
 EXPORT_SYMBOL(udpv6_encap_enable);
 
+void udpv6_encap_disable(void)
+{
+	static_branch_dec(&udpv6_encap_needed_key);
+}
+EXPORT_SYMBOL(udpv6_encap_disable);
+
 /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
  * through error handlers in encapsulations looking for a match.
  */
@@ -1823,7 +1829,7 @@ void udpv6_destroy_sock(struct sock *sk)
 			if (encap_destroy)
 				encap_destroy(sk);
 		}
-		if (udp_test_bit(ENCAP_ENABLED, sk)) {
+		if (udp_test_and_clear_bit(ENCAP_ENABLED, sk)) {
 			static_branch_dec(&udpv6_encap_needed_key);
 			udp_encap_disable();
 			udp_tunnel_cleanup_gro(sk);
Signature · 1 line
2.49.0
maintainer
#2 lore raw

Re: [PATCH net-next 1/4] ovpn: properly deconfigure UDP-tunnel

net/ipv4/udp.c void udp_destroy_sock(struct sock *sk) · line 2903

Quoted 10 lines · Antonio Quartulli
net/ipv4/udp.c +1 -1
index dde52b8050b8..9ffc4e0b1644 100644
@@ -2900,7 +2900,7 @@ void udp_destroy_sock(struct sock *sk)
 			if (encap_destroy)
 				encap_destroy(sk);
 		}
-		if (udp_test_bit(ENCAP_ENABLED, sk)) {
+		if (udp_test_and_clear_bit(ENCAP_ENABLED, sk)) {

Nothing should use 'sk' after udp_destroy_sock(), no need to clear the bit (same in ipv6 code)

Quoted 22 lines
net/ipv4/udp.c +0 -0
 			static_branch_dec(&udp_encap_needed_key);
 			udp_tunnel_cleanup_gro(sk);
 		}
net/ipv4/udp_tunnel_core.c +11 -0
index 2326548997d3..624b6afcf812 100644
@@ -98,6 +98,28 @@ void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
 }
 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
 
+void cleanup_udp_tunnel_sock(struct sock *sk)
+{
+	/* Re-enable multicast loopback */
+	inet_set_bit(MC_LOOP, sk);
+
+	/* Disable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
+	inet_dec_convert_csum(sk);
+
+	udp_sk(sk)->encap_type = 0;
+	udp_sk(sk)->encap_rcv = NULL;
+	udp_sk(sk)->encap_err_rcv = NULL;

net/ipv4/udp_tunnel_core.c void setup_udp_tunnel_sock(struct net *net, struct socket *sock, · line 121

Quoted 10 lines
net/ipv4/udp_tunnel_core.c +10 -0
+	udp_sk(sk)->encap_err_lookup = NULL;
+	udp_sk(sk)->encap_destroy = NULL;
+	udp_sk(sk)->gro_receive = NULL;
+	udp_sk(sk)->gro_complete = NULL;
+
+	rcu_assign_sk_user_data(sk, NULL);
+	udp_tunnel_encap_disable(sk);
+	udp_tunnel_cleanup_gro(sk);
+}
+EXPORT_SYMBOL_GPL(cleanup_udp_tunnel_sock);

IMHO the above code should not land into a generic tunnel helper, as the tunnel scope always correspond to the sk scope - except for openvpn.

Also IMHO no need to do udp_tunnel_encap_disable(), it will called as needed at sk close() time. Yep, that means that the stack will have transient additional overhead until the sock is not destroyed, but IMHO it's not worthy the extra complexity (export symbol, additional stub, more files touched...)

/P

author maintainer
#3 lore raw

Re: [PATCH net-next 1/4] ovpn: properly deconfigure UDP-tunnel

Hi Paolo,

Quoted 14 lines · Paolo Abeni

net/ipv4/udp.c void udp_destroy_sock(struct sock *sk) · line 2903

Quoted 10 lines · Antonio Quartulli
net/ipv4/udp.c +1 -1
index dde52b8050b8..9ffc4e0b1644 100644
@@ -2900,7 +2900,7 @@ void udp_destroy_sock(struct sock *sk)
  			if (encap_destroy)
  				encap_destroy(sk);
  		}
-		if (udp_test_bit(ENCAP_ENABLED, sk)) {
+		if (udp_test_and_clear_bit(ENCAP_ENABLED, sk)) {

Nothing should use 'sk' after udp_destroy_sock(), no need to clear the bit (same in ipv6 code)

Ok, then I guess that in ovpn I need to check if SOCK_DEAD is set and, if so, just bail out.

Quoted 28 lines
Quoted 28 lines
net/ipv4/udp.c +0 -0
  			static_branch_dec(&udp_encap_needed_key);
  			udp_tunnel_cleanup_gro(sk);
  		}
net/ipv4/udp_tunnel_core.c +16 -0
index 2326548997d3..624b6afcf812 100644
@@ -98,6 +98,28 @@ void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
  }
  EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
 
+void cleanup_udp_tunnel_sock(struct sock *sk)
+{
+	/* Re-enable multicast loopback */
+	inet_set_bit(MC_LOOP, sk);
+
+	/* Disable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
+	inet_dec_convert_csum(sk);
+
+	udp_sk(sk)->encap_type = 0;
+	udp_sk(sk)->encap_rcv = NULL;
+	udp_sk(sk)->encap_err_rcv = NULL;
+	udp_sk(sk)->encap_err_lookup = NULL;
+	udp_sk(sk)->encap_destroy = NULL;
+	udp_sk(sk)->gro_receive = NULL;
+	udp_sk(sk)->gro_complete = NULL;
+
Quoted 9 lines

net/ipv4/udp_tunnel_core.c void setup_udp_tunnel_sock(struct net *net, struct socket *sock, · line 121

Quoted 5 lines
net/ipv4/udp_tunnel_core.c +5 -0
+	rcu_assign_sk_user_data(sk, NULL);
+	udp_tunnel_encap_disable(sk);
+	udp_tunnel_cleanup_gro(sk);
+}
+EXPORT_SYMBOL_GPL(cleanup_udp_tunnel_sock);

IMHO the above code should not land into a generic tunnel helper, as the tunnel scope always correspond to the sk scope - except for openvpn.

Ok.
I thought udp_tunnel was the right place because this helper is an explicit counterpart for setup_udp_tunnel_sock().

But since there are no other users, I can move this code inside ovpn.

Quoted 5 lines

Also IMHO no need to do udp_tunnel_encap_disable(), it will called as needed at sk close() time. Yep, that means that the stack will have transient additional overhead until the sock is not destroyed, but IMHO it's not worthy the extra complexity (export symbol, additional stub, more files touched...)

That's true: removing that part will greatly simplify the patch.

If we agree that we can live with the transient overhead, then ok, I will remove the udp_tunnel_encap_disable() part and simplify this commit.

Thanks for the feedback, Paolo!

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#4 lore raw

PATCH 2/4 ovpn: ensure sk is still valid during cleanup

In reply to Antonio Quartulli

Removing a peer while userspace attempts to close its transport socket triggers a race condition resulting in the following crash:

Oops: general protection fault, probably for non-canonical address 0xdffffc0000000077: 0000 [#1] SMP KASAN
KASAN: null-ptr-deref in range [0x00000000000003b8-0x00000000000003bf]
CPU: 12 UID: 0 PID: 162 Comm: kworker/12:1 Tainted: G           O        6.15.0-rc2-00635-g521139ac3840 #272 PREEMPT(full)
Tainted: [O]=OOT_MODULE
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-20240910_120124-localhost 04/01/2014
Workqueue: events ovpn_peer_keepalive_work [ovpn]
RIP: 0010:ovpn_socket_release+0x23c/0x500 [ovpn]
Code: ea 03 80 3c 02 00 0f 85 71 02 00 00 48 b8 00 00 00 00 00 fc ff df 4d 8b 64 24 18 49 8d bc 24 be 03 00 00 48 89 fa 48 c1 ea 03 <0f> b6 14 02 48 89 f8 83 e0 07 83 c0 01 38 d0 7c 08 84 d2 0f 85 30
RSP: 0018:ffffc90000c9fb18 EFLAGS: 00010217
RAX: dffffc0000000000 RBX: ffff8881148d7940 RCX: ffffffff817787bb
RDX: 0000000000000077 RSI: 0000000000000008 RDI: 00000000000003be
RBP: ffffc90000c9fb30 R08: 0000000000000000 R09: fffffbfff0d3e840
R10: ffffffff869f4207 R11: 0000000000000000 R12: 0000000000000000
R13: ffff888115eb9300 R14: ffffc90000c9fbc8 R15: 000000000000000c
FS:  0000000000000000(0000) GS:ffff8882b0151000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f37266b6114 CR3: 00000000054a8000 CR4: 0000000000750ef0
PKRU: 55555554
Call Trace:
 <TASK>
 unlock_ovpn+0x8b/0xe0 [ovpn]
 ovpn_peer_keepalive_work+0xe3/0x540 [ovpn]
 ? ovpn_peers_free+0x780/0x780 [ovpn]
 ? lock_acquire+0x56/0x70
 ? process_one_work+0x888/0x1740
 process_one_work+0x933/0x1740
 ? pwq_dec_nr_in_flight+0x10b0/0x10b0
 ? move_linked_works+0x12d/0x2c0
 ? assign_work+0x163/0x270
 worker_thread+0x4d6/0xd90
 ? preempt_count_sub+0x4c/0x70
 ? process_one_work+0x1740/0x1740
 kthread+0x36c/0x710
 ? trace_preempt_on+0x8c/0x1e0
 ? kthread_is_per_cpu+0xc0/0xc0
 ? preempt_count_sub+0x4c/0x70
 ? _raw_spin_unlock_irq+0x36/0x60
 ? calculate_sigpending+0x7b/0xa0
 ? kthread_is_per_cpu+0xc0/0xc0
 ret_from_fork+0x3a/0x80
 ? kthread_is_per_cpu+0xc0/0xc0
 ret_from_fork_asm+0x11/0x20
 </TASK>
Modules linked in: ovpn(O)

This happens because the peer deletion operation reaches ovpn_socket_release() while ovpn_sock->sock (struct socket *) and its sk member (struct sock *) are still both valid. Here synchronize_rcu() is invoked, after which ovpn_sock->sock->sk becomes NULL, due to the concurrent socket closing triggered from userspace.

After having invoked synchronize_rcu(), ovpn_socket_release() will attempt dereferencing ovpn_sock->sock->sk, triggering the crash reported above.

The reason for accessing sk is that we need to retrieve its protocol and continue the cleanup routine accordingly.

This crash can be easily produced by running openvpn userspace in client mode with --keepalive 10 20, while entirely omitting this option on the server side.
After 20 seconds ovpn will assume the peer (server) to be dead, will start removing it and will notify userspace. The latter will receive the notification and close the transport socket, thus triggering the crash.

To fix the race condition for good, we need to refactor struct ovpn_socket. Since ovpn is always only interested in the sock->sk member (struct sock *) we can directly hold a reference to it, raher than accessing it via its struct socket container.

This means changing "struct socket *ovpn_socket->sock" to "struct sock *ovpn_socket->sk".

While acquiring a reference to sk, we can increase its refcounter without affecting the socket close()/destroy() notification (which we rely on when userspace closes a socket we are using).

By increasing sk's refcounter we know we can dereference it in ovpn_socket_release() without incurring in any race condition anymore.

ovpn_socket_release() will ultimately decrease the reference counter.

---
 drivers/net/ovpn/io.c      |  8 ++---
 drivers/net/ovpn/netlink.c | 25 ++++++++------
 drivers/net/ovpn/peer.c    |  4 +--
 drivers/net/ovpn/socket.c  | 68 +++++++++++++++++++++-----------------
 drivers/net/ovpn/socket.h  |  4 +--
 drivers/net/ovpn/tcp.c     | 65 ++++++++++++++++++------------------
 drivers/net/ovpn/tcp.h     |  3 +-
 drivers/net/ovpn/udp.c     | 34 +++++++------------
 drivers/net/ovpn/udp.h     |  4 +--
 9 files changed, 109 insertions(+), 106 deletions(-)
drivers/net/ovpn/io.c +4 -4
index 10d8afecec55..ebf1e849506b 100644
@@ -134,7 +134,7 @@ void ovpn_decrypt_post(void *data, int ret)
 
 	rcu_read_lock();
 	sock = rcu_dereference(peer->sock);
-	if (sock && sock->sock->sk->sk_protocol == IPPROTO_UDP)
+	if (sock && sock->sk->sk_protocol == IPPROTO_UDP)
 		/* check if this peer changed local or remote endpoint */
 		ovpn_peer_endpoints_update(peer, skb);
 	rcu_read_unlock();
@@ -270,12 +270,12 @@ void ovpn_encrypt_post(void *data, int ret)
 	if (unlikely(!sock))
 		goto err_unlock;
 
-	switch (sock->sock->sk->sk_protocol) {
+	switch (sock->sk->sk_protocol) {
 	case IPPROTO_UDP:
-		ovpn_udp_send_skb(peer, sock->sock, skb);
+		ovpn_udp_send_skb(peer, sock->sk, skb);
 		break;
 	case IPPROTO_TCP:
-		ovpn_tcp_send_skb(peer, sock->sock, skb);
+		ovpn_tcp_send_skb(peer, sock->sk, skb);
 		break;
 	default:
 		/* no transport configured yet */
drivers/net/ovpn/netlink.c +15 -10
index bea03913bfb1..1dd7e763c168 100644
@@ -423,9 +423,14 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info)
 	ovpn_sock = ovpn_socket_new(sock, peer);
 	/* at this point we unconditionally drop the reference to the socket:
 	 * - in case of error, the socket has to be dropped
-	 * - if case of success, the socket is configured and let
+	 * - if case of success, the socket is configured and we let
 	 *   userspace own the reference, so that the latter can
-	 *   trigger the final close()
+	 *   trigger the final close().
+	 *
+	 * NOTE: at this point ovpn_socket_new() has acquired a reference
+	 * to sock->sk. That's needed especially to avoid race conditions
+	 * during cleanup, where sock may still be alive, but sock->sk may be
+	 * getting released concurrently.
 	 */
 	sockfd_put(sock);
 	if (IS_ERR(ovpn_sock)) {
@@ -501,7 +506,7 @@ int ovpn_nl_peer_set_doit(struct sk_buff *skb, struct genl_info *info)
 	/* when using a TCP socket the remote IP is not expected */
 	rcu_read_lock();
 	sock = rcu_dereference(peer->sock);
-	if (sock && sock->sock->sk->sk_protocol == IPPROTO_TCP &&
+	if (sock && sock->sk->sk_protocol == IPPROTO_TCP &&
 	    (attrs[OVPN_A_PEER_REMOTE_IPV4] ||
 	     attrs[OVPN_A_PEER_REMOTE_IPV6])) {
 		rcu_read_unlock();
@@ -559,14 +564,14 @@ static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *info,
 		goto err_unlock;
 	}
 
-	if (!net_eq(genl_info_net(info), sock_net(sock->sock->sk))) {
+	if (!net_eq(genl_info_net(info), sock_net(sock->sk))) {
 		id = peernet2id_alloc(genl_info_net(info),
-				      sock_net(sock->sock->sk),
+				      sock_net(sock->sk),
 				      GFP_ATOMIC);
 		if (nla_put_s32(skb, OVPN_A_PEER_SOCKET_NETNSID, id))
 			goto err_unlock;
 	}
-	local_port = inet_sk(sock->sock->sk)->inet_sport;
+	local_port = inet_sk(sock->sk)->inet_sport;
 	rcu_read_unlock();
 
 	if (nla_put_u32(skb, OVPN_A_PEER_ID, peer->id))
@@ -1153,8 +1158,8 @@ int ovpn_nl_peer_del_notify(struct ovpn_peer *peer)
 		ret = -EINVAL;
 		goto err_unlock;
 	}
-	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sock->sk),
-				msg, 0, OVPN_NLGRP_PEERS, GFP_ATOMIC);
+	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg, 0,
+				OVPN_NLGRP_PEERS, GFP_ATOMIC);
 	rcu_read_unlock();
 
 	return 0;
@@ -1218,8 +1223,8 @@ int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id)
 		ret = -EINVAL;
 		goto err_unlock;
 	}
-	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sock->sk),
-				msg, 0, OVPN_NLGRP_PEERS, GFP_ATOMIC);
+	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg, 0,
+				OVPN_NLGRP_PEERS, GFP_ATOMIC);
 	rcu_read_unlock();
 
 	return 0;
drivers/net/ovpn/peer.c +2 -2
index a1fd27b9c038..4bfcab0c8652 100644
@@ -1145,7 +1145,7 @@ static void ovpn_peer_release_p2p(struct ovpn_priv *ovpn, struct sock *sk,
 
 	if (sk) {
 		ovpn_sock = rcu_access_pointer(peer->sock);
-		if (!ovpn_sock || ovpn_sock->sock->sk != sk) {
+		if (!ovpn_sock || ovpn_sock->sk != sk) {
 			spin_unlock_bh(&ovpn->lock);
 			ovpn_peer_put(peer);
 			return;
@@ -1175,7 +1175,7 @@ static void ovpn_peers_release_mp(struct ovpn_priv *ovpn, struct sock *sk,
 		if (sk) {
 			rcu_read_lock();
 			ovpn_sock = rcu_dereference(peer->sock);
-			remove = ovpn_sock && ovpn_sock->sock->sk == sk;
+			remove = ovpn_sock && ovpn_sock->sk == sk;
 			rcu_read_unlock();
 		}
 
drivers/net/ovpn/socket.c +38 -30

162 lines not on this page · raw message

drivers/net/ovpn/socket.h +2 -2
index 00d856b1a5d8..4afcec71040d 100644
@@ -22,7 +22,7 @@ struct ovpn_peer;
  * @ovpn: ovpn instance owning this socket (UDP only)
  * @dev_tracker: reference tracker for associated dev (UDP only)
  * @peer: unique peer transmitting over this socket (TCP only)
- * @sock: the low level sock object
+ * @sk: the low level sock object
  * @refcount: amount of contexts currently referencing this object
  * @work: member used to schedule release routine (it may block)
  * @tcp_tx_work: work for deferring outgoing packet processing (TCP only)
@@ -36,7 +36,7 @@ struct ovpn_socket {
 		struct ovpn_peer *peer;
 	};
 
-	struct socket *sock;
+	struct sock *sk;
 	struct kref refcount;
 	struct work_struct work;
 	struct work_struct tcp_tx_work;
drivers/net/ovpn/tcp.c +32 -33

143 lines not on this page · raw message

drivers/net/ovpn/tcp.h +2 -1
index 10aefa834cf3..a3aa3570ae5e 100644
@@ -30,7 +30,8 @@ void ovpn_tcp_socket_wait_finish(struct ovpn_socket *sock);
  * Required by the OpenVPN protocol in order to extract packets from
  * the TCP stream on the receiver side.
  */
-void ovpn_tcp_send_skb(struct ovpn_peer *peer, struct socket *sock, struct sk_buff *skb);
+void ovpn_tcp_send_skb(struct ovpn_peer *peer, struct sock *sk,
+		       struct sk_buff *skb);
 void ovpn_tcp_tx_work(struct work_struct *work);
 
 #endif /* _NET_OVPN_TCP_H_ */
drivers/net/ovpn/udp.c +12 -22
index c8a81c4d6489..b4fbebad8f45 100644
@@ -43,7 +43,7 @@ static struct ovpn_socket *ovpn_socket_from_udp_sock(struct sock *sk)
 		return NULL;
 
 	/* make sure that sk matches our stored transport socket */
-	if (unlikely(!ovpn_sock->sock || sk != ovpn_sock->sock->sk))
+	if (unlikely(!ovpn_sock->sk || sk != ovpn_sock->sk))
 		return NULL;
 
 	return ovpn_sock;
@@ -335,32 +335,22 @@ static int ovpn_udp_output(struct ovpn_peer *peer, struct dst_cache *cache,
 /**
  * ovpn_udp_send_skb - prepare skb and send it over via UDP
  * @peer: the destination peer
- * @sock: the RCU protected peer socket
+ * @sk: peer socket
  * @skb: the packet to send
  */
-void ovpn_udp_send_skb(struct ovpn_peer *peer, struct socket *sock,
+void ovpn_udp_send_skb(struct ovpn_peer *peer, struct sock *sk,
 		       struct sk_buff *skb)
 {
-	int ret = -1;
+	int ret;
 
 	skb->dev = peer->ovpn->dev;
 	/* no checksum performed at this layer */
 	skb->ip_summed = CHECKSUM_NONE;
 
-	/* get socket info */
-	if (unlikely(!sock)) {
-		net_warn_ratelimited("%s: no sock for remote peer %u\n",
-				     netdev_name(peer->ovpn->dev), peer->id);
-		goto out;
-	}
-
 	/* crypto layer -> transport (UDP) */
-	ret = ovpn_udp_output(peer, &peer->dst_cache, sock->sk, skb);
-out:
-	if (unlikely(ret < 0)) {
+	ret = ovpn_udp_output(peer, &peer->dst_cache, sk, skb);
+	if (unlikely(ret < 0))
 		kfree_skb(skb);
-		return;
-	}
 }
 
 static void ovpn_udp_encap_destroy(struct sock *sk)
@@ -383,6 +373,7 @@ static void ovpn_udp_encap_destroy(struct sock *sk)
 /**
  * ovpn_udp_socket_attach - set udp-tunnel CBs on socket and link it to ovpn
  * @ovpn_sock: socket to configure
+ * @sock: the socket container to be passed to setup_udp_tunnel_sock()
  * @ovpn: the openvp instance to link
  *
  * After invoking this function, the sock will be controlled by ovpn so that
@@ -390,7 +381,7 @@ static void ovpn_udp_encap_destroy(struct sock *sk)
  *
  * Return: 0 on success or a negative error code otherwise
  */
-int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock,
+int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock, struct socket *sock,
 			   struct ovpn_priv *ovpn)
 {
 	struct udp_tunnel_sock_cfg cfg = {
@@ -398,17 +389,16 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock,
 		.encap_rcv = ovpn_udp_encap_recv,
 		.encap_destroy = ovpn_udp_encap_destroy,
 	};
-	struct socket *sock = ovpn_sock->sock;
 	struct ovpn_socket *old_data;
 	int ret;
 
 	/* make sure no pre-existing encapsulation handler exists */
 	rcu_read_lock();
-	old_data = rcu_dereference_sk_user_data(sock->sk);
+	old_data = rcu_dereference_sk_user_data(ovpn_sock->sk);
 	if (!old_data) {
 		/* socket is currently unused - we can take it */
 		rcu_read_unlock();
-		setup_udp_tunnel_sock(sock_net(sock->sk), sock, &cfg);
+		setup_udp_tunnel_sock(sock_net(ovpn_sock->sk), sock, &cfg);
 		return 0;
 	}
 
@@ -421,7 +411,7 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock,
 	 * Unlikely TCP, a single UDP socket can be used to talk to many remote
 	 * hosts and therefore openvpn instantiates one only for all its peers
 	 */
-	if ((READ_ONCE(udp_sk(sock->sk)->encap_type) == UDP_ENCAP_OVPNINUDP) &&
+	if ((READ_ONCE(udp_sk(ovpn_sock->sk)->encap_type) == UDP_ENCAP_OVPNINUDP) &&
 	    old_data->ovpn == ovpn) {
 		netdev_dbg(ovpn->dev,
 			   "provided socket already owned by this interface\n");
@@ -442,5 +432,5 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock,
  */
 void ovpn_udp_socket_detach(struct ovpn_socket *ovpn_sock)
 {
-	cleanup_udp_tunnel_sock(ovpn_sock->sock->sk);
+	cleanup_udp_tunnel_sock(ovpn_sock->sk);
 }
drivers/net/ovpn/udp.h +2 -2
index 9994eb6e0428..fe26fbe25c5a 100644
@@ -15,11 +15,11 @@ struct ovpn_peer;
 struct ovpn_priv;
 struct socket;
 
-int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock,
+int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock, struct socket *sock,
 			   struct ovpn_priv *ovpn);
 void ovpn_udp_socket_detach(struct ovpn_socket *ovpn_sock);
 
-void ovpn_udp_send_skb(struct ovpn_peer *peer, struct socket *sock,
+void ovpn_udp_send_skb(struct ovpn_peer *peer, struct sock *sk,
 		       struct sk_buff *skb);
 
 #endif /* _NET_OVPN_UDP_H_ */
Signature · 1 line
2.49.0
maintainer
#5 lore raw

Re: [PATCH net-next 2/4] ovpn: ensure sk is still valid during cleanup

Quoted 8 lines · Antonio Quartulli
drivers/net/ovpn/netlink.c +0 -0
index bea03913bfb1..1dd7e763c168 100644
@@ -423,9 +423,14 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info)
 	ovpn_sock = ovpn_socket_new(sock, peer);
 	/* at this point we unconditionally drop the reference to the socket:
 	 * - in case of error, the socket has to be dropped

drivers/net/ovpn/netlink.c int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info) · line 433

Quoted 10 lines
drivers/net/ovpn/netlink.c +7 -2
-	 * - if case of success, the socket is configured and let
+	 * - if case of success, the socket is configured and we let
 	 *   userspace own the reference, so that the latter can
-	 *   trigger the final close()
+	 *   trigger the final close().
+	 *
+	 * NOTE: at this point ovpn_socket_new() has acquired a reference
+	 * to sock->sk. That's needed especially to avoid race conditions
+	 * during cleanup, where sock may still be alive, but sock->sk may be
+	 * getting released concurrently.

This comment duplicate some wording from commit message contents and don't add much value IMHO. It could be dropped.

drivers/net/ovpn/netlink.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 196

Quoted 9 lines
drivers/net/ovpn/netlink.c +5 -0
@@ -192,19 +189,30 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)
 		rcu_read_unlock();
 	}
 
+	/* increase sk refcounter as we'll store a reference in
+	 * ovpn_socket.
+	 * ovpn_socket_release() will decrement the refcounter.
+	 */
+	if (!refcount_inc_not_zero(&sk->sk_refcnt)) {

How could sk_refcnt be zero here? likely just sock_hold() is sufficient. Also I think the reference could be acquired a little later, avoiding at least the following chunk.

Also IMHO the comment is not very clear. I think it should state explicitly which entity is retaining the reference (AFAICS the peer hash table).

/P

author maintainer
#6 lore raw

Re: [PATCH net-next 2/4] ovpn: ensure sk is still valid during cleanup

Quoted 14 lines · Paolo Abeni
Quoted 13 lines · Antonio Quartulli
drivers/net/ovpn/netlink.c +2 -2
index bea03913bfb1..1dd7e763c168 100644
@@ -423,9 +423,14 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info)
  	ovpn_sock = ovpn_socket_new(sock, peer);
  	/* at this point we unconditionally drop the reference to the socket:
  	 * - in case of error, the socket has to be dropped
-	 * - if case of success, the socket is configured and let
+	 * - if case of success, the socket is configured and we let
  	 *   userspace own the reference, so that the latter can
-	 *   trigger the final close()
+	 *   trigger the final close().
Quoted 8 lines

drivers/net/ovpn/netlink.c int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info) · line 433

Quoted 5 lines
drivers/net/ovpn/netlink.c +5 -0
+	 *
+	 * NOTE: at this point ovpn_socket_new() has acquired a reference
+	 * to sock->sk. That's needed especially to avoid race conditions
+	 * during cleanup, where sock may still be alive, but sock->sk may be
+	 * getting released concurrently.

This comment duplicate some wording from commit message contents and don't add much value IMHO. It could be dropped.

ACK

Quoted 5 lines
Quoted 5 lines
drivers/net/ovpn/netlink.c +0 -0
@@ -192,19 +189,30 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)
  		rcu_read_unlock();
  	}
 
Quoted 9 lines

drivers/net/ovpn/netlink.c struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer) · line 196

Quoted 5 lines
drivers/net/ovpn/netlink.c +5 -0
+	/* increase sk refcounter as we'll store a reference in
+	 * ovpn_socket.
+	 * ovpn_socket_release() will decrement the refcounter.
+	 */
+	if (!refcount_inc_not_zero(&sk->sk_refcnt)) {

How could sk_refcnt be zero here? likely just sock_hold() is sufficient. Also I think the reference could be acquired a little later, avoiding at least the following chunk.

I just wanted to be safe.
But as you point out I don't see any reason why sk_refcnt would be 0 at this point in time.

If we don't have to check the return value, I can definitely postpone the sock_hold() a bit more.

Quoted 4 lines

Also IMHO the comment is not very clear. I think it should state explicitly which entity is retaining the reference (AFAICS the peer hash table).

Ok, I will elaborate a bit more.
Essentially: sk -> ovpn_socket -> ovpn_peer -> peer hash

Thanks a lot!
Regards,

Quoted 3 lines

/P

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#7 lore raw

PATCH 3/4 selftest/net/ovpn: fix TCP socket creation

In reply to Antonio Quartulli

TCP sockets cannot be created with AF_UNSPEC, but
one among the supported family must be used.

Since commit 944f8b6abab6 ("selftest/net/ovpn: extend coverage with more test cases") the default address family for all tests was changed from AF_INET to AF_UNSPEC, thus breaking all TCP cases.

Restore AF_INET as default address family for TCP listeners.

Fixes 944f8b6abab6 ("selftest/net/ovpn: extend coverage with more test cases")
---
 tools/testing/selftests/net/ovpn/ovpn-cli.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/net/ovpn/ovpn-cli.c +1 -0
index de9c26f98b2e..9201f2905f2c 100644
@@ -2166,6 +2166,7 @@ static int ovpn_parse_cmd_args(struct ovpn_ctx *ovpn, int argc, char *argv[])
 
 		ovpn->peers_file = argv[4];
 
+		ovpn->sa_family = AF_INET;
 		if (argc > 5 && !strcmp(argv[5], "ipv6"))
 			ovpn->sa_family = AF_INET6;
 		break;
Signature · 1 line
2.49.0
author maintainer
#8 lore raw

PATCH 4/4 selftest/net/ovpn: fix missing file

In reply to Antonio Quartulli

test-large-mtu.sh is referenced by the Makefile
but does not exist.

Add it along the other scripts.

Fixes 944f8b6abab6 ("selftest/net/ovpn: extend coverage with more test cases")
---
 tools/testing/selftests/net/ovpn/test-large-mtu.sh | 9 +++++++++
 1 file changed, 9 insertions(+)
 create mode 100755 tools/testing/selftests/net/ovpn/test-large-mtu.sh
tools/testing/selftests/net/ovpn/test-large-mtu.sh new file +9 -0
new file mode 100755
index 000000000000..ce2a2cb64f72
@@ -0,0 +1,9 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2025 OpenVPN, Inc.
+#
+#  Author:	Antonio Quartulli <[email protected]>
+
+MTU="1500"
+
+source test.sh
Signature · 1 line
2.49.0