ovpn: avoid sending UDP packets with source port 0
Subsystems (2)
Ralf Lici
reviewerPATCH 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.
--- drivers/net/ovpn/udp.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/net/ovpn/udp.c +10 -2
@@ -149,13 +149,17 @@ static int ovpn_udp4_output(struct ovpn_peer *peer, struct ovpn_bind *bind,struct flowi4 fl = { .saddr = 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)) +return -EADDRNOTAVAIL ; +local_bh_disable (); rt =dst_cache_get_ip4 (cache, &fl.saddr);if (rt)
@@ -226,13 +230,17 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,struct flowi6 fl = { .saddr = bind->local.ipv6, .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, .flowi6_oif = bind->remote.in6.sin6_scope_id, }; +/* an uninitialized socket or connect(AF_UNSPEC) can cause this */ +if (unlikely (!fl.fl6_sport)) +return -EADDRNOTAVAIL ; +local_bh_disable (); dst =dst_cache_get_ip6 (cache, &fl.saddr);if (dst)
Signature · 8 lines
2.54.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH 2/4 ovpn: validate sockets before attaching peer transports
ovpn 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.
--- 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 */
@@ -203,11 +205,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 */
@@ -228,9 +230,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 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.
An IPv4 UDP socket cannot be used with an IPv6 remote endpoint. If accepted, the transmit path may reach IPv6 routing and UDP tunnel helpers with an IPv4 socket. Similarly, an IPv6-only UDP socket cannot be used with an IPv4 remote endpoint. Otherwise ovpn may enter the IPv4 UDP transmit path, where IPv4 routing treats the unspecified source as a normal source-selection request and may choose an IPv4 source address, bypassing the check that udpv6_sendmsg would normally enforce.
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.
--- drivers/net/ovpn/netlink.c | 130 ++++++++++++++++++++++++++----------- 1 file changed, 92 insertions(+), 38 deletions(-)
drivers/net/ovpn/netlink.c +92 -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,41 @@ 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) +{ +if (sk->sk_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 (sk->sk_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 +444,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 +497,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;
@@ -483,8 +524,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;
@@ -516,22 +559,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
Sabrina Dubroca
reviewerRe: [Openvpn-devel] [PATCH ovpn net 3/4] ovpn: reject UDP remotes incompatible with socket family
Hi Ralf,
Quoted 12 lines · Ralf Lici
ovpn 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.An IPv4 UDP socket cannot be used with an IPv6 remote endpoint. If accepted, the transmit path may reach IPv6 routing and UDP tunnel helpers with an IPv4 socket. Similarly, an IPv6-only UDP socket cannot be used with an IPv4 remote endpoint. Otherwise ovpn may enter the IPv4 UDP transmit path, where IPv4 routing treats the unspecified source as a normal source-selection request and may choose an IPv4 source address, bypassing the check that
udpv6_sendmsgwould normally enforce.
Quoted 4 lines
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.
Since this can be changed at any time by userspace, I'm not convinced checking it at setup time is very useful.
AFAIU, the next patch fixes the actual issue. This one seems to mainly help userspace avoid drops in case they incorrectly set up their socket (as long as they do that in the "right order", ie all setsockopt before passing the socket to ovpn).
Signature · 7 lines
Sabrina _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerRe: [Openvpn-devel] [PATCH ovpn net 3/4] ovpn: reject UDP remotes incompatible with socket family
Quoted 20 lines · Sabrina Dubroca
Hi Ralf,
Quoted 12 lines · Ralf Lici
ovpn 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.An IPv4 UDP socket cannot be used with an IPv6 remote endpoint. If accepted, the transmit path may reach IPv6 routing and UDP tunnel helpers with an IPv4 socket. Similarly, an IPv6-only UDP socket cannot be used with an IPv4 remote endpoint. Otherwise ovpn may enter the IPv4 UDP transmit path, where IPv4 routing treats the unspecified source as a normal source-selection request and may choose an IPv4 source address, bypassing the check that
udpv6_sendmsgwould normally enforce.Quoted 4 lines
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.
Quoted 8 lines
Since this can be changed at any time by userspace, I'm not convinced checking it at setup time is very useful.
AFAIU, the next patch fixes the actual issue. This one seems to mainly help userspace avoid drops in case they incorrectly set up their socket (as long as they do that in the "right order", ie all setsockopt before passing the socket to ovpn).
Hi Sabrina,
Yes, your understanding is correct: the new/set peer netlink messages give us a chance to detect a broken configuration at setup time, but we cannot prevent userspace from changing the socket properties later on. In this sense this patch is not the actual correctness fix.
The next patch enforces the real gate on TX, but the error is ultimately swallowed by ovpn_udp_send_skb after freeing the skb. Antonio suggested adding a netdev_warn_once on these new socket-based failure points (including the sport == 0 case), and I think that would be useful too. But, aside from that, I don't think there's a clean way to report an error to userspace once such misconfigurations are hit from the TX path.
So the rationale for the current patch is to at least report the specific socket/remote mismatch through extack when it is already present in the netlink request, and avoid installing a peer that would immediately drop traffic.
That said, I see that the commit message may be selling the patch as solving the issue at its core. I can reword it to make clear that this is only early validation/reporting, or drop the patch if you think the value is too low.
Thanks for the input!
Signature · 8 lines
Ralf Lici Mandelbit Srl _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Sabrina Dubroca
reviewerRe: [Openvpn-devel] [PATCH ovpn net 3/4] ovpn: reject UDP remotes incompatible with socket family
Sorry Ralf, I wanted to reply to this earlier.
Quoted 30 lines · Ralf Lici
Quoted 20 lines · Sabrina Dubroca
Hi Ralf,
Quoted 12 lines · Ralf Lici
ovpn 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.An IPv4 UDP socket cannot be used with an IPv6 remote endpoint. If accepted, the transmit path may reach IPv6 routing and UDP tunnel helpers with an IPv4 socket. Similarly, an IPv6-only UDP socket cannot be used with an IPv4 remote endpoint. Otherwise ovpn may enter the IPv4 UDP transmit path, where IPv4 routing treats the unspecified source as a normal source-selection request and may choose an IPv4 source address, bypassing the check that
udpv6_sendmsgwould normally enforce.Quoted 4 lines
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.
Quoted 8 lines
Since this can be changed at any time by userspace, I'm not convinced checking it at setup time is very useful.
AFAIU, the next patch fixes the actual issue. This one seems to mainly help userspace avoid drops in case they incorrectly set up their socket (as long as they do that in the "right order", ie all setsockopt before passing the socket to ovpn).
Quoted 6 lines
Hi Sabrina,
Yes, your understanding is correct: the new/set peer netlink messages give us a chance to detect a broken configuration at setup time, but we cannot prevent userspace from changing the socket properties later on. In this sense this patch is not the actual correctness fix.
Thanks for confirming.
Quoted 6 lines
The next patch enforces the real gate on TX, but the error is ultimately swallowed by
ovpn_udp_send_skbafter freeing the skb. Antonio suggested adding anetdev_warn_onceon these new socket-based failure points (including the sport == 0 case), and I think that would be useful too. But, aside from that, I don't think there's a clean way to report an error to userspace once such misconfigurations are hit from the TX path.
I think adding stats counters could help (probably per-socket, and maybe a separate one for each case in patch 4/4). A _once/_ratelimited message also sounds reasonable.
Quoted 4 lines
So the rationale for the current patch is to at least report the specific socket/remote mismatch through extack when it is already present in the netlink request, and avoid installing a peer that would immediately drop traffic.
Yup, that makes sense.
Quoted 3 lines
That said, I see that the commit message may be selling the patch as solving the issue at its core. I can reword it to make clear that this is only early validation/reporting, or drop the patch if you think the value is too low.
A small note to explain that this patch doesn't fully solve the issue would be useful.
This patch is a bit large, but I guess it would solve the issue entirely for "well-behaved" userspace (not doing weird setsockopts after "gifting" the socket to the kernel module). Is the current openvpn client hitting the code in next patch once this one is applied (ie finishing config, and then doing V6ONLY/ADDRFORM afterwards)?
Signature · 7 lines
Sabrina _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerRe: [Openvpn-devel] [PATCH ovpn net 3/4] ovpn: reject UDP remotes incompatible with socket family
Quoted 1 line · Sabrina Dubroca
Sorry Ralf, I wanted to reply to this earlier.
No worries, thanks for taking another look.
Quoted 48 lines
Quoted 30 lines · Ralf Lici
Quoted 20 lines · Sabrina Dubroca
Hi Ralf,
Quoted 12 lines · Ralf Lici
ovpn 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.An IPv4 UDP socket cannot be used with an IPv6 remote endpoint. If accepted, the transmit path may reach IPv6 routing and UDP tunnel helpers with an IPv4 socket. Similarly, an IPv6-only UDP socket cannot be used with an IPv4 remote endpoint. Otherwise ovpn may enter the IPv4 UDP transmit path, where IPv4 routing treats the unspecified source as a normal source-selection request and may choose an IPv4 source address, bypassing the check that
udpv6_sendmsgwould normally enforce.Quoted 4 lines
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.
Quoted 8 lines
Since this can be changed at any time by userspace, I'm not convinced checking it at setup time is very useful.
AFAIU, the next patch fixes the actual issue. This one seems to mainly help userspace avoid drops in case they incorrectly set up their socket (as long as they do that in the "right order", ie all setsockopt before passing the socket to ovpn).
Quoted 6 lines
Hi Sabrina,
Yes, your understanding is correct: the new/set peer netlink messages give us a chance to detect a broken configuration at setup time, but we cannot prevent userspace from changing the socket properties later on. In this sense this patch is not the actual correctness fix.
Thanks for confirming.
Quoted 6 lines
The next patch enforces the real gate on TX, but the error is ultimately swallowed by
ovpn_udp_send_skbafter freeing the skb. Antonio suggested adding anetdev_warn_onceon these new socket-based failure points (including the sport == 0 case), and I think that would be useful too. But, aside from that, I don't think there's a clean way to report an error to userspace once such misconfigurations are hit from the TX path.
Quoted 4 lines
I think adding stats counters could help (probably per-socket, and maybe a separate one for each case in patch 4/4). A _once/_ratelimited message also sounds reasonable.
I agree that counters would improve observability, and I am not opposed to adding them later if we find they are useful in practice. For this series, though, I am not sure they are worth adding UAPI for (netlink operations are currently all peer-related).
These drops are not expected to be normal packet-loss events: they mean userspace handed ovpn a socket, then left it in a state where that peer cannot use it anymore. In that case the most useful immediate signal is the reason why the peer became unusable, rather than a counter of how many packets were already dropped because of it.
Antonio and I discussed this more in depth and our plan for the respin is to delete the peer with TRANSPORT_ERROR via deferred work for socket/remote address family mismatches, instead of keeping a peer around that can no longer transmit. We would also add a _ratelimited warning on those TX-side failures (_once appears to be once per call site, so one bad peer/socket would silence the warning for all later cases).
For sport == 0 I'd keep dropping and warning for now and think about a more systematic approach in net-next. For example, we could reject this at socket setup time and override the UDP socket protocol callbacks to intercept and prevent disconnect (though that needs some caution because IPV6_ADDRFORM can apparently switch the sk_prot back to udp_prot).
Does that make sense to you?
Quoted 5 lines
Quoted 4 lines
So the rationale for the current patch is to at least report the specific socket/remote mismatch through extack when it is already present in the netlink request, and avoid installing a peer that would immediately drop traffic.
Quoted 8 lines
Yup, that makes sense.
Quoted 3 lines
That said, I see that the commit message may be selling the patch as solving the issue at its core. I can reword it to make clear that this is only early validation/reporting, or drop the patch if you think the value is too low.
A small note to explain that this patch doesn't fully solve the issue would be useful.
Sure, I will add it.
Quoted 6 lines
This patch is a bit large, but I guess it would solve the issue entirely for "well-behaved" userspace (not doing weird setsockopts after "gifting" the socket to the kernel module). Is the current openvpn client hitting the code in next patch once this one is applied (ie finishing config, and then doing V6ONLY/ADDRFORM afterwards)?
No, luckily current openvpn userspace does not change these socket properties after handing the socket to ovpn. The setup time checks should therefore be enough for well-behaved userspace. The TX-side checks are still needed because the socket remains mutable after attach.
Quoted 4 lines
Signature · 2 lines
-- Sabrina
Signature · 8 lines
Ralf Lici Mandelbit Srl _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH 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. Drop the packet with -EAFNOSUPPORT when the peer remote family no longer matches the socket state.
--- drivers/net/ovpn/udp.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/net/ovpn/udp.c +10 -3
@@ -320,18 +320,25 @@ static int ovpn_udp_output(struct ovpn_peer *peer, struct dst_cache *cache, goto out; } + ret = -EAFNOSUPPORT ;switch (bind->remote.in4.sin_family) {case AF_INET : +/* userspace might have set IPV6_ONLY */ +if (unlikely (READ_ONCE (sk->sk_family) ==AF_INET6 && +ipv6_only_sock (sk))) +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 (READ_ONCE (sk->sk_family) !=AF_INET6 )) +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