ovpn: harden UDP TX against mutable socket state
Subsystems (2)
Ralf Lici
reviewerv3 cover ovpn: harden UDP TX against mutable socket state
Hi,
This is a rebase of v2 so no functional changes are included in this v3.
v1 added setup-time validation for userspace-provided sockets and TX-time checks for socket state that can still change after the socket is attached to ovpn.
In v1, ovpn_udp_send_skb consumed the error internally, so ovpn_encrypt_post could still update link TX stats and last_sent for a packet that was dropped before transmit. v2 propagates those errors back to the common TX completion path, so TX-side checks are handled as local transmit failures, not as successful handoff to the UDP stack.
v2 also makes socket/remote address-family mismatches fatal for the peer. Those mismatches mean the peer can no longer transmit with the socket it was configured with, so keeping it around would just keep dropping packets. The peer deletion is deferred through a common transport-error work item, which is also reused by the TCP transport-error paths.
The source-port-zero case is kept as drop+warning for now. It is still a broken socket state for ovpn TX, but it is not treated as a peer-fatal address-family mismatch in this series.
Thanks,
Ralf Lici
Mandelbit Srl
---
Changes since v2 https://lore.kernel.org/openvpn-devel/[email protected]/
- Rebase on top of OpenVPN/ovpn-net-next/development-net.
Changes since v1 https://lore.kernel.org/openvpn-devel/[email protected]/T/
- Add ratelimited warnings for TX-side socket state failures.
- Propagate local UDP TX errors to ovpn_encrypt_post, so local drops do
not update link TX stats or last_sent.
- Delete peers with TRANSPORT_ERROR on UDP socket/remote address-family
mismatches.
- Add a common deferred transport-error deletion helper shared by TCP and
UDP.
- Clarify that netlink socket/remote validation is setup-time diagnostics;
the TX path remains the runtime gate for mutable socket state.
- Use a single READ_ONCE() snapshot of sk->sk_family in the netlink helper.
- Fix the IPV6_V6ONLY typo.
Ralf Lici (4):
ovpn: avoid sending UDP packets with source port 0
ovpn: validate sockets before attaching peer transports
ovpn: reject UDP remotes incompatible with socket family
ovpn: recheck UDP socket family before transmit
drivers/net/ovpn/io.c | 4 +- drivers/net/ovpn/netlink.c | 133 ++++++++++++++++++++++++++++--------- drivers/net/ovpn/peer.c | 19 ++++++ drivers/net/ovpn/peer.h | 5 +- drivers/net/ovpn/socket.c | 16 +++-- drivers/net/ovpn/tcp.c | 23 +------ drivers/net/ovpn/udp.c | 57 ++++++++++++---- drivers/net/ovpn/udp.h | 4 +- 8 files changed, 183 insertions(+), 78 deletions(-)
base-commit: a6b96b20e356138d932e2e44eebe15bf01506c15
Signature · 8 lines
2.54.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v3 1/4 ovpn: avoid sending UDP packets with source port 0
ovpn operates on a userspace-owned UDP socket, which may be manipulated in various ways by userspace. If the socket is never bound, connected, or used for communication, it may not have a source port assigned. Similarly, if the socket was connect()'ed to AF_INET or AF_INET6, it can be disconnected by connect() with AF_UNSPEC, which resets the source port unless the socket was explicitly bound.
Since we must not transmit packets with source port 0, gate UDP TX on the presence of a valid source port and drop packets otherwise. To avoid ambiguity, sample the current source port once before route lookup and header build and enforce the check on that value.
Emit a ratelimited warning when this drop path is hit so the broken socket state is visible. Return local UDP output errors to the common TX completion path so locally dropped packets are not counted as successful transport TX and do not refresh the peer keepalive timer.
---
No functional changes since v2 https://lore.kernel.org/openvpn-devel/97aecfd6a289211b3a1e3ed03ebd80bd896dde9b.1780663425.git.ralf@mandelbit.com/
Changes since v1 https://lore.kernel.org/openvpn-devel/[email protected]/
- Emit a ratelimited warning when UDP TX sees source port 0.
- Make ovpn_udp_send_skb return local UDP output errors instead of
freeing the skb internally.
- Propagate local UDP TX errors to ovpn_encrypt_post so failed local
drops do not update link TX stats or last_sent.
- Update UDP TX kdoc to document skb ownership on success/error.
drivers/net/ovpn/io.c | 4 +++- drivers/net/ovpn/udp.c | 33 +++++++++++++++++++++++---------- drivers/net/ovpn/udp.h | 4 ++-- 3 files changed, 28 insertions(+), 13 deletions(-)
drivers/net/ovpn/io.c +3 -1
@@ -282,7 +282,9 @@ void ovpn_encrypt_post(void *data, int ret)switch (sock->sk->sk_protocol) {case IPPROTO_UDP : -ovpn_udp_send_skb (peer, sock->sk, skb); + ret =ovpn_udp_send_skb (peer, sock->sk, skb); +if (unlikely (ret <0 )) + goto err_unlock;break ;case IPPROTO_TCP :ovpn_tcp_send_skb (peer, sock->sk, skb);
drivers/net/ovpn/udp.c +23 -10
@@ -152,13 +152,20 @@ static int ovpn_udp4_output(struct ovpn_peer *peer, struct ovpn_bind *bind,*/ .saddr = READ_ONCE(bind->local.ipv4.s_addr), .daddr = bind->remote.in4.sin_addr.s_addr, - .fl4_sport = inet_sk(sk)->inet_sport, + .fl4_sport = READ_ONCE(inet_sk(sk)->inet_sport), .fl4_dport = bind->remote.in4.sin_port, .flowi4_proto = sk->sk_protocol, .flowi4_mark = sk->sk_mark, };int ret; +/* an uninitialized socket or connect(AF_UNSPEC) can cause this */ +if (unlikely (!fl.fl4_sport)) { +net_warn_ratelimited ("%s: peer %u: UDP source port is 0\n" , +netdev_name (peer->ovpn->dev), peer->id); +return -EADDRNOTAVAIL ; + } +local_bh_disable (); rt =dst_cache_get_ip4 (cache, &fl.saddr);if (rt)
@@ -228,7 +235,7 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,struct flowi6 fl = { .daddr = bind->remote.in6.sin6_addr, - .fl6_sport =inet_sk (sk)->inet_sport, + .fl6_sport =READ_ONCE (inet_sk (sk)->inet_sport), .fl6_dport = bind->remote.in6.sin6_port, .flowi6_proto = sk->sk_protocol, .flowi6_mark = sk->sk_mark,
@@ -240,6 +247,13 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,*/ ovpn_peer_local_ipv6 (peer, bind, &fl.saddr); +/* an uninitialized socket or connect(AF_UNSPEC) can cause this */ +if (unlikely (!fl.fl6_sport)) { +net_warn_ratelimited ("%s: peer %u: UDP source port is 0\n" , +netdev_name (peer->ovpn->dev), peer->id); +return -EADDRNOTAVAIL ; + } +local_bh_disable (); dst =dst_cache_get_ip6 (cache, &fl.saddr);if (dst)
@@ -298,7 +312,8 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,* @skb: the packet to send * * rcu_read_lock should be held on entry. -* On return, the skb is consumed. +* On success, the skb is passed to the transport stack and consumed. On +* error, ownership remains with the caller. * * Return: 0 on success or a negative error code otherwise */
@@ -345,21 +360,19 @@ static int ovpn_udp_output(struct ovpn_peer *peer, struct dst_cache *cache,* @peer: the destination peer * @sk: peer socket * @skb: the packet to send +* +* Return: 0 on success or a negative error code otherwise */ -void ovpn_udp_send_skb (struct ovpn_peer *peer,struct sock *sk, -struct sk_buff *skb) +int ovpn_udp_send_skb (struct ovpn_peer *peer,struct sock *sk, +struct sk_buff *skb) { -int ret; - skb->dev = peer->ovpn->dev; skb->mark =READ_ONCE (sk->sk_mark);/* no checksum performed at this layer */ skb->ip_summed =CHECKSUM_NONE ;/* crypto layer -> transport (UDP) */ - ret =ovpn_udp_output (peer, &peer->dst_cache, sk, skb); -if (unlikely (ret <0 )) -kfree_skb (skb); +return ovpn_udp_output (peer, &peer->dst_cache, sk, skb); }static void ovpn_udp_encap_destroy (struct sock *sk)
drivers/net/ovpn/udp.h +2 -2
@@ -19,7 +19,7 @@ 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 sock *sk, -struct sk_buff *skb); +int ovpn_udp_send_skb (struct ovpn_peer *peer,struct sock *sk, +struct sk_buff *skb);#endif /* _NET_OVPN_UDP_H_ */
Signature · 8 lines
2.54.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v3 2/4 ovpn: validate sockets before attaching peer transports
In reply to Ralf Liciovpn accepts a userspace-provided socket and attaches transport-specific state to it. The current checks use sk_protocol to select the UDP or TCP attach path, but sk_protocol alone does not identify the socket layout. For example, a raw socket can have sk_protocol set to IPPROTO_UDP while its storage is not a struct udp_sock. Passing such a socket to the UDP attach path would make ovpn read and write udp_sock fields on the wrong object, potentially accessing memory beyond the actual socket storage.
Reject sockets unless they are real UDP datagram or TCP stream sockets before attaching them to ovpn in the peer creation path. This lets netlink report a clear error before calling the socket attach helper.
Also switch ovpn_socket_new to sk_is_tcp and sk_is_udp, matching the netlink validation performed before the helper is called. This does not change the accepted socket types, but makes the helper's assumptions explicit.
---
No changes since v2 https://lore.kernel.org/openvpn-devel/8e0904081feaec3e49972fa34ace74a9e8c1397f.1780663425.git.ralf@mandelbit.com/
No changes since v1 https://lore.kernel.org/openvpn-devel/[email protected]/
drivers/net/ovpn/netlink.c | 15 +++++++++++++-- drivers/net/ovpn/socket.c | 16 +++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-)
drivers/net/ovpn/netlink.c +13 -2
@@ -400,10 +400,21 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info) goto peer_release; } +/* sk_protocol is not enough to determine if this is a real UDP or TCP +* socket +*/ +if (!sk_is_udp (sock->sk) && !sk_is_tcp (sock->sk)) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"socket is not TCP or UDP" ); +sockfd_put (sock); + ret = -EOPNOTSUPP ; + goto peer_release; + } +/* Only when using UDP as transport protocol the remote endpoint * can be configured so that ovpn knows where to send packets to. */ -if (sock->sk->sk_protocol ==IPPROTO_UDP && +if (sk_is_udp (sock->sk) && !attrs[OVPN_A_PEER_REMOTE_IPV4 ] && !attrs[OVPN_A_PEER_REMOTE_IPV6 ]) { NL_SET_ERR_MSG_FMT_MOD(info->extack,
@@ -417,7 +428,7 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info)* will just send bytes over it, without the need to specify a * destination. */ -if (sock->sk->sk_protocol ==IPPROTO_TCP && +if (sk_is_tcp (sock->sk) && (attrs[OVPN_A_PEER_REMOTE_IPV4 ] || attrs[OVPN_A_PEER_REMOTE_IPV6 ])) { NL_SET_ERR_MSG_FMT_MOD(info->extack,
drivers/net/ovpn/socket.c +9 -7
@@ -126,13 +126,15 @@ static int ovpn_socket_attach(struct ovpn_socket *ovpn_sock,/** * ovpn_socket_new - create a new socket and initialize it -* @sock: the kernel socket to embed +* @sock: the kernel socket to embed; must be a real UDP or TCP socket * @peer: the peer reachable via this socket * * Return: an openvpn socket on success or a negative error code otherwise */ struct ovpn_socket *ovpn_socket_new (struct socket *sock,struct ovpn_peer *peer) { +const bool tcp =sk_is_tcp (sock->sk); +const bool udp =sk_is_udp (sock->sk);struct ovpn_socket *ovpn_sock;struct sock *sk = sock->sk;int ret;
@@ -142,7 +144,7 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)/* a TCP socket can only be owned by a single peer, therefore there * can't be any other user */ -if (sk->sk_protocol ==IPPROTO_TCP && sk->sk_user_data) { +if (tcp && sk->sk_user_data) { ovpn_sock =ERR_PTR (-EBUSY ); goto sock_release; }
@@ -150,7 +152,7 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)/* a UDP socket can be shared across multiple peers, but we must make * sure it is not owned by something else */ -if (sk->sk_protocol ==IPPROTO_UDP ) { +if (udp) {u8 type =READ_ONCE (udp_sk (sk)->encap_type);/* socket owned by other encapsulation module */
@@ -212,11 +214,11 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)/* TCP sockets are per-peer, therefore they are linked to their unique * peer */ -if (sk->sk_protocol ==IPPROTO_TCP ) { +if (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 ) { + }else if (udp) {/* in UDP we only link the ovpn instance since the socket is * shared among multiple peers */
@@ -237,9 +239,9 @@ 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 ) +if (tcp)ovpn_peer_put (peer); -else if (sk->sk_protocol ==IPPROTO_UDP ) +else if (udp)netdev_put (peer->ovpn->dev, &ovpn_sock->dev_tracker);sock_put (sk);
Signature · 8 lines
2.54.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v3 3/4 ovpn: reject UDP remotes incompatible with socket family
In reply to Ralf Liciovpn accepts a userspace-provided socket and a peer remote endpoint through netlink. For UDP peers, the remote endpoint family selects the transmit path used later by ovpn_udp_output.
Reject UDP peer remotes that are incompatible with the socket state when they are configured through netlink. An IPv4 UDP socket cannot be used with an IPv6 remote endpoint, and an IPv6-only UDP socket cannot send to an IPv4 remote endpoint. Reporting this at setup time gives userspace a clear extack and avoids installing a peer that would immediately be unable to transmit.
This validation only covers the socket state visible while the netlink request is handled. The socket remains owned by userspace after it is attached to ovpn, so userspace can later change properties such as IPV6_V6ONLY or IPV6_ADDRFORM. The transmit path therefore still has to re-check socket/remote compatibility before sending.
Parse the remote endpoint once in the peer new/set paths and reject UDP remotes when the provided socket cannot send to them. Pass the parsed endpoint into the common peer modify helper so the validation and the stored endpoint use the same normalized sockaddr.
---
No changes since v2 https://lore.kernel.org/openvpn-devel/b871663007ed51e7a64a94ed2a85aca6ba9aaa17.1780663425.git.ralf@mandelbit.com/
Changes since v1 https://lore.kernel.org/openvpn-devel/[email protected]/
- Reword the commit message to clarify that netlink validation is only
setup-time diagnostics.
- Use a single READ_ONCE snapshot of sk->sk_family in ovpn_nl_udp_remote_compatible.
drivers/net/ovpn/netlink.c | 132 ++++++++++++++++++++++++++----------- 1 file changed, 94 insertions(+), 38 deletions(-)
drivers/net/ovpn/netlink.c +94 -38
@@ -271,15 +271,17 @@ static int ovpn_nl_peer_precheck(struct ovpn_priv *ovpn,* @peer: the peer to modify * @info: generic netlink info from the user request * @attrs: the attributes from the user request +* @remote: remote address, if provided * * Return: a negative error code in case of failure, 0 on success or 1 on * success and the VPN IPs have been modified (requires rehashing in MP * mode) */ static int ovpn_nl_peer_modify (struct ovpn_peer *peer,struct genl_info *info, -struct nlattr **attrs) +struct nlattr **attrs, +const struct sockaddr_storage *remote) { -struct sockaddr_storage ss = {}; +struct sockaddr_storage empty_remote = {};void *local_ip =NULL ;u32 interv, timeout;bool rehash = false;
@@ -287,15 +289,15 @@ static int ovpn_nl_peer_modify(struct ovpn_peer *peer, struct genl_info *info,spin_lock_bh (&peer->lock); -if (ovpn_nl_attr_sockaddr_remote (attrs, &ss)) { +if (remote) {/* we carry the local IP in a generic container. * ovpn_peer_reset_sockaddr() will properly interpret it -* based on ss.ss_family +* based on remote->ss_family */ local_ip =ovpn_nl_attr_local_ip (attrs);/* set peer sockaddr */ - ret =ovpn_peer_reset_sockaddr (peer, &ss, local_ip); + ret =ovpn_peer_reset_sockaddr (peer, remote, local_ip);if (ret <0 ) { NL_SET_ERR_MSG_FMT_MOD(info->extack, "cannot set peer sockaddr: %d",
@@ -333,7 +335,7 @@ static int ovpn_nl_peer_modify(struct ovpn_peer *peer, struct genl_info *info,netdev_dbg (peer->ovpn->dev,"modify peer id=%u tx_id=%u endpoint=%pIScp VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n" , - peer->id, peer->tx_id, &ss, + peer->id, peer->tx_id, remote ?: &empty_remote, &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6);spin_unlock_bh (&peer->lock);
@@ -344,10 +346,43 @@ static int ovpn_nl_peer_modify(struct ovpn_peer *peer, struct genl_info *info,return ret; } +/** +* ovpn_nl_udp_remote_compatible - check if a UDP socket can use a remote +* @sk: UDP socket to validate +* @remote: remote endpoint to validate against the socket +* @extack: netlink extended ACK for reporting validation errors +* +* Return: 0 if the remote endpoint is compatible with the socket or a negative +* error code otherwise. +*/ +static int ovpn_nl_udp_remote_compatible (const struct sock *sk, +const struct sockaddr_storage *remote, +struct netlink_ext_ack *extack) +{ +const unsigned short sock_family =READ_ONCE (sk->sk_family); + +if (sock_family ==AF_INET && remote->ss_family ==AF_INET6 ) { +NL_SET_ERR_MSG_FMT_MOD (extack, +"UDP socket is IPv4 but remote is IPv6" ); +return -EAFNOSUPPORT ; + } + +if (sock_family ==AF_INET6 &&ipv6_only_sock (sk) && + remote->ss_family ==AF_INET ) { +NL_SET_ERR_MSG_FMT_MOD (extack, +"UDP socket is IPv6-only but remote is IPv4" ); +return -EAFNOSUPPORT ; + } + +return 0 ; +} +int ovpn_nl_peer_new_doit (struct sk_buff *skb,struct genl_info *info) { -struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +const struct sockaddr_storage *remote =NULL ;struct ovpn_priv *ovpn = info->user_ptr[0 ]; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct sockaddr_storage ss = {};struct ovpn_socket *ovpn_sock;struct socket *sock =NULL ;struct ovpn_peer *peer;
@@ -411,26 +446,34 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info) goto peer_release; } -/* Only when using UDP as transport protocol the remote endpoint -* can be configured so that ovpn knows where to send packets to. -*/ -if (sk_is_udp (sock->sk) && - !attrs[OVPN_A_PEER_REMOTE_IPV4 ] && - !attrs[OVPN_A_PEER_REMOTE_IPV6 ]) { -NL_SET_ERR_MSG_FMT_MOD (info->extack, -"missing remote IP address for UDP socket" ); -sockfd_put (sock); - ret = -EINVAL ; - goto peer_release; - } +if (ovpn_nl_attr_sockaddr_remote (attrs, &ss)) + remote = &ss; -/* In case of TCP, the socket is connected to the peer and ovpn -* will just send bytes over it, without the need to specify a -* destination. -*/ -if (sk_is_tcp (sock->sk) && - (attrs[OVPN_A_PEER_REMOTE_IPV4 ] || - attrs[OVPN_A_PEER_REMOTE_IPV6 ])) { +if (sk_is_udp (sock->sk)) { +/* Only when using UDP as transport protocol the remote +* endpoint can be configured so that ovpn knows where to send +* packets to. +*/ +if (!remote) { +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"missing remote IP address for UDP socket" ); +sockfd_put (sock); + ret = -EINVAL ; + goto peer_release; + } + +/* can the socket be used with this remote? */ + ret =ovpn_nl_udp_remote_compatible (sock->sk, remote, + info->extack); +if (ret <0 ) { +sockfd_put (sock); + goto peer_release; + } + }else if (remote) { +/* In case of TCP, the socket is connected to the peer and ovpn +* will just send bytes over it, without the need to specify a +* destination. +*/ NL_SET_ERR_MSG_FMT_MOD (info->extack,"unexpected remote IP address with TCP socket" );sockfd_put (sock);
@@ -456,7 +499,7 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info)rcu_assign_pointer (peer->sock, ovpn_sock); - ret =ovpn_nl_peer_modify (peer, info, attrs); + ret =ovpn_nl_peer_modify (peer, info, attrs, remote);if (ret <0 ) goto sock_release;
@@ -485,8 +528,10 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info)int ovpn_nl_peer_set_doit (struct sk_buff *skb,struct genl_info *info) { -struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +const struct sockaddr_storage *remote =NULL ;struct ovpn_priv *ovpn = info->user_ptr[0 ]; +struct nlattr *attrs[OVPN_A_PEER_MAX +1 ]; +struct sockaddr_storage ss = {};struct ovpn_socket *sock;struct ovpn_peer *peer;u32 peer_id;
@@ -518,22 +563,33 @@ int ovpn_nl_peer_set_doit(struct sk_buff *skb, struct genl_info *info)return -ENOENT ; } -/* when using a TCP socket the remote IP is not expected */ +if (ovpn_nl_attr_sockaddr_remote (attrs, &ss)) + remote = &ss; +rcu_read_lock (); sock =rcu_dereference (peer->sock); -if (sock && sock->sk->sk_protocol ==IPPROTO_TCP && - (attrs[OVPN_A_PEER_REMOTE_IPV4 ] || - attrs[OVPN_A_PEER_REMOTE_IPV6 ])) { -rcu_read_unlock (); -NL_SET_ERR_MSG_FMT_MOD (info->extack, -"unexpected remote IP address with TCP socket" ); -ovpn_peer_put (peer); -return -EINVAL ; +if (sock && remote) { +if (sk_is_udp (sock->sk)) { + ret =ovpn_nl_udp_remote_compatible (sock->sk, remote, + info->extack); +if (ret <0 ) { +rcu_read_unlock (); +ovpn_peer_put (peer); +return ret; + } + }else if (sk_is_tcp (sock->sk)) { +/* when using a TCP socket remote IP is not expected */ +NL_SET_ERR_MSG_FMT_MOD (info->extack, +"unexpected remote IP address with TCP socket" ); +rcu_read_unlock (); +ovpn_peer_put (peer); +return -EINVAL ; + } }rcu_read_unlock ();spin_lock_bh (&ovpn->lock); - ret =ovpn_nl_peer_modify (peer, info, attrs); + ret =ovpn_nl_peer_modify (peer, info, attrs, remote);if (ret <0 ) {spin_unlock_bh (&ovpn->lock);ovpn_peer_put (peer);
Signature · 8 lines
2.54.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v3 4/4 ovpn: recheck UDP socket family before transmit
In reply to Ralf Liciovpn validates UDP peer remotes against the socket family when the remote endpoint is configured through netlink. The socket itself, however, remains owned by userspace and some socket options can still change the family seen by the transmit path.
For example, IPV6_ADDRFORM can turn an AF_INET6 socket into AF_INET after ovpn accepted an IPv6 remote. Conversely, IPV6_V6ONLY can make a dual-stack AF_INET6 socket unable to send to an IPv4 remote.
Recheck the socket family in ovpn_udp_output before selecting the UDP transmit path. When the peer remote family no longer matches the socket state, emit a ratelimited warning, drop the packet with -EAFNOSUPPORT and delete the peer with TRANSPORT_ERROR.
Peer deletion is deferred through a common transport-error work item, because the UDP transmit path can run from non-sleepable contexts while ovpn_peer_del may release sockets and sleep. Use the same helper for TCP transport errors instead of keeping a TCP-only deferred delete work item.
---
No functional changes since v2 https://lore.kernel.org/openvpn-devel/c5392b0ea96d2c79c2d32470526004e2b0ff1d42.1780663425.git.ralf@mandelbit.com/
Changes since v1 https://lore.kernel.org/openvpn-devel/[email protected]/
- Emit ratelimited warnings when UDP TX detects a socket/remote address
family mismatch.
- Delete the peer with TRANSPORT_ERROR on UDP socket/remote family
mismatches, using deferred work because TX can run from non-sleepable
contexts.
- Move the TCP deferred transport-error deletion work into a common ovpn_peer_del_transport_error helper and reuse it from TCP and UDP.
- Read sk->sk_family once before selecting the UDP TX path.
- Fix the IPV6_V6ONLY comment typo.
drivers/net/ovpn/peer.c | 19 +++++++++++++++++++ drivers/net/ovpn/peer.h | 5 +++-- drivers/net/ovpn/tcp.c | 23 +++-------------------- drivers/net/ovpn/udp.c | 24 +++++++++++++++++++++--- 4 files changed, 46 insertions(+), 25 deletions(-)
drivers/net/ovpn/peer.c +19 -0
@@ -83,6 +83,24 @@ static void ovpn_peer_keepalive_send(struct work_struct *work)local_bh_enable (); } +static void ovpn_peer_transport_error_work (struct work_struct *work) +{ + struct ovpn_peer *peer = container_of(work, struct ovpn_peer, + transport_error_work); + +ovpn_peer_del (peer,OVPN_DEL_PEER_REASON_TRANSPORT_ERROR ); +ovpn_peer_put (peer); +} + +void ovpn_peer_del_transport_error (struct ovpn_peer *peer) +{ +if (WARN_ON (!ovpn_peer_hold (peer))) +return ; + +if (!schedule_work (&peer->transport_error_work)) +ovpn_peer_put (peer); +} +/** * ovpn_peer_new - allocate and initialize a new peer object * @ovpn: the openvpn instance inside which the peer should be created
@@ -117,6 +135,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *ovpn, u32 id)kref_init (&peer->refcount);ovpn_peer_stats_init (&peer->vpn_stats);ovpn_peer_stats_init (&peer->link_stats); +INIT_WORK (&peer->transport_error_work, ovpn_peer_transport_error_work);INIT_WORK (&peer->keepalive_work, ovpn_peer_keepalive_send); ret =dst_cache_init (&peer->dst_cache,GFP_KERNEL );
drivers/net/ovpn/peer.h +3 -2
@@ -62,6 +62,7 @@* @refcount: reference counter * @rcu: used to free peer in an RCU safe way * @release_entry: entry for the socket release list +* @transport_error_work: work used to delete peer on transport error * @keepalive_work: used to schedule keepalive sending */ struct ovpn_peer {
@@ -97,8 +98,6 @@ struct ovpn_peer {struct proto *prot;const struct proto_ops *ops; } sk_cb; - -struct work_struct defer_del_work; } tcp;struct ovpn_crypto_state crypto;struct dst_cache dst_cache;
@@ -117,6 +116,7 @@ struct ovpn_peer {struct kref refcount;struct rcu_head rcu;struct llist_node release_entry; +struct work_struct transport_error_work;struct work_struct keepalive_work; };
@@ -145,6 +145,7 @@ static inline void ovpn_peer_put(struct ovpn_peer *peer)struct ovpn_peer *ovpn_peer_new (struct ovpn_priv *ovpn,u32 id);int ovpn_peer_add (struct ovpn_priv *ovpn,struct ovpn_peer *peer);int ovpn_peer_del (struct ovpn_peer *peer,enum ovpn_del_peer_reason reason); +void ovpn_peer_del_transport_error (struct ovpn_peer *peer);void ovpn_peers_free (struct ovpn_priv *ovpn,struct sock *sock,enum ovpn_del_peer_reason reason);
drivers/net/ovpn/tcp.c +3 -20
@@ -148,11 +148,7 @@ static void ovpn_tcp_rcv(struct strparser *strp, struct sk_buff *skb)ovpn_recv (peer, skb);return ; err: -/* take reference for deferred peer deletion. should never fail */ -if (WARN_ON (!ovpn_peer_hold (peer))) - goto err_nopeer; -if (!schedule_work (&peer->tcp.defer_del_work)) -ovpn_peer_put (peer); +ovpn_peer_del_transport_error (peer);ovpn_dev_dstats_rx_dropped (peer->ovpn->dev); err_nopeer:kfree_skb (skb);
@@ -240,7 +236,7 @@ void ovpn_tcp_socket_wait_finish(struct ovpn_socket *sock) {struct ovpn_peer *peer = sock->peer; -/* NOTE: we don't wait for peer->tcp.defer_del_work to finish: +/* NOTE: we don't wait for peer->transport_error_work to finish: * either the worker is not running or this function * was invoked by that worker. */
@@ -283,9 +279,7 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk)/* in case of TCP error we can't recover the VPN * stream therefore we abort the connection */ -ovpn_peer_hold (peer); -if (!schedule_work (&peer->tcp.defer_del_work)) -ovpn_peer_put (peer); +ovpn_peer_del_transport_error (peer);/* we bail out immediately and keep tx_in_progress set * to true. This way we prevent more TX attempts
@@ -498,15 +492,6 @@ static void ovpn_tcp_build_protos(struct proto *new_prot,const struct proto *orig_prot, const struct proto_ops *orig_ops); -static void ovpn_tcp_peer_del_work (struct work_struct *work) -{ - struct ovpn_peer *peer = container_of(work, struct ovpn_peer, - tcp.defer_del_work); - -ovpn_peer_del (peer,OVPN_DEL_PEER_REASON_TRANSPORT_ERROR ); -ovpn_peer_put (peer); -} -/* Set TCP encapsulation callbacks */ int ovpn_tcp_socket_attach (struct ovpn_socket *ovpn_sock,struct ovpn_peer *peer)
@@ -539,8 +524,6 @@ int ovpn_tcp_socket_attach(struct ovpn_socket *ovpn_sock, goto err; } -INIT_WORK (&peer->tcp.defer_del_work, ovpn_tcp_peer_del_work); -__sk_dst_reset (ovpn_sock->sk);skb_queue_head_init (&peer->tcp.user_queue);skb_queue_head_init (&peer->tcp.out_queue);
drivers/net/ovpn/udp.c +21 -3
@@ -320,6 +320,7 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,static int ovpn_udp_output (struct ovpn_peer *peer,struct dst_cache *cache,struct sock *sk,struct sk_buff *skb) { +unsigned short sock_family;struct ovpn_bind *bind;int ret;
@@ -336,18 +337,35 @@ static int ovpn_udp_output(struct ovpn_peer *peer, struct dst_cache *cache, goto out; } + sock_family =READ_ONCE (sk->sk_family); + ret = -EAFNOSUPPORT ;switch (bind->remote.in4.sin_family) {case AF_INET : +/* userspace might have set IPV6_V6ONLY */ +if (unlikely (sock_family ==AF_INET6 &&ipv6_only_sock (sk))) { +net_warn_ratelimited ("%s: peer %u: IPv4 remote, IPv6-only socket\n" , +netdev_name (peer->ovpn->dev), + peer->id); +ovpn_peer_del_transport_error (peer); +break ; + } + ret =ovpn_udp4_output (peer, bind, cache, sk, skb);break ;#if IS_ENABLED (CONFIG_IPV6 )case AF_INET6 : +/* userspace might have set IPV6_ADDRFORM */ +if (unlikely (sock_family !=AF_INET6 )) { +net_warn_ratelimited ("%s: peer %u: IPv6 remote, IPv4 socket\n" , +netdev_name (peer->ovpn->dev), + peer->id); +ovpn_peer_del_transport_error (peer); +break ; + } + ret =ovpn_udp6_output (peer, bind, cache, sk, skb);break ;#endif -default : - ret = -EAFNOSUPPORT ; -break ; } out:
Signature · 8 lines
2.54.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel