ovpn: skip rehash for peers already removed from by_id
Subsystems (2)
PATCH 1/9 ovpn: skip rehash for peers already removed from by_id
From: Antonio Quartulli <[email protected]>
ovpn_nl_peer_set_doit() resolves the target peer via ovpn_peer_get_by_id() before taking ovpn->lock. In the window between the lookup (which only takes a refcount) and the subsequent spin_lock_bh(&ovpn->lock), a concurrent OVPN_CMD_PEER_DEL, keepalive expiry, or socket teardown can take ovpn->lock first, run ovpn_peer_remove() to unhash the peer from all four tables (by_id, by_vpn_addr4/6, by_transp_addr) and release the lock. set_doit then acquires ovpn->lock and calls ovpn_peer_hash_vpn_ip(), which re-inserts the now-removed peer back into the rehashing tables.
The same race affects the float path: ovpn_peer_endpoints_update() holds only a refcount and acquires ovpn->lock very late (after async AEAD decrypt and a netlink notification), then rehashes the peer in the by_transp_addr table.
The resurrected peer becomes reachable again from the RX lookup (ovpn_peer_get_by_transp_addr) and the TX VPN-IP lookup, even though userspace believes it is gone. Once the data-path refcount drops the peer is freed via call_rcu while the hash entries embedded in it remain linked, opening a UAF window.
Bail out of the rehash when hash_entry_id is unhashed, mirroring the sentinel already used by ovpn_peer_remove() to detect the already-removed state. The check is safe under ovpn->lock, which serializes every mutation of hash_entry_id, and is a no-op for the add path because ovpn_peer_add_mp() inserts hash_entry_id before calling ovpn_peer_hash_vpn_ip().
--- drivers/net/ovpn/peer.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/net/ovpn/peer.c +17 -0
@@ -307,6 +307,16 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)return ; } +/* peer may have been concurrently removed between the caller's +* initial lookup and our acquisition of ovpn->lock; skip the +* rehash so we don't re-insert a removed peer +*/ +if (unlikely (hlist_unhashed (&peer->hash_entry_id))) { +spin_unlock_bh (&peer->lock); +spin_unlock_bh (&peer->ovpn->lock); +return ; + } +/* This function may be invoked concurrently, therefore another * float may have happened in parallel: perform rehashing * using the peer->bind->remote directly as key
@@ -905,6 +915,13 @@ void ovpn_peer_hash_vpn_ip(struct ovpn_peer *peer)if (peer->ovpn->mode !=OVPN_MODE_MP )return ; +/* peer may have been concurrently removed between the caller's +* initial lookup and our acquisition of ovpn->lock; skip the +* rehash so we don't re-insert a removed peer +*/ +if (hlist_unhashed (&peer->hash_entry_id)) +return ; +if (peer->vpn_addrs.ipv4.s_addr !=htonl (INADDR_ANY )) {/* remove potential old hashing */ hlist_nulls_del_init_rcu (&peer->hash_entry_addr4);
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH 2/9 ovpn: rehash peer in by_transp_addr table on CMD_PEER_SET
From: Antonio Quartulli <[email protected]>
When userspace updates a peer's remote endpoint via OVPN_CMD_PEER_SET, ovpn_nl_peer_modify() installs a new ovpn_bind through ovpn_peer_reset_sockaddr(), but ovpn_nl_peer_set_doit() only calls ovpn_peer_hash_vpn_ip() to refresh the VPN-IP hashtables. The peer is left in the bucket of peers->by_transp_addr corresponding to its old remote address.
As a consequence, datagrams arriving at the UDP RX path from the newly configured remote hash to a different slot and the lockless lookup in ovpn_peer_get_by_transp_addr() (called from ovpn_udp_encap_recv()) does not find the peer, until either a float event or a peer re-add fixes the bucket.
Introduce ovpn_peer_hash_transp_addr() (modeled after ovpn_peer_hash_vpn_ip()) and invoke it from ovpn_nl_peer_set_doit() whenever the request carried a new remote address. The helper bails out in P2P mode and on peers without a bind (TCP), and relies on hlist_nulls_del_init_rcu()'s pprev==NULL short-circuit to handle the case of an entry not currently linked in the table.
--- drivers/net/ovpn/netlink.c | 6 +++ drivers/net/ovpn/peer.c | 106 ++++++++++++++++++++++++------------- drivers/net/ovpn/peer.h | 1 + 3 files changed, 75 insertions(+), 38 deletions(-)
drivers/net/ovpn/netlink.c +6 -0
@@ -534,6 +534,12 @@ int ovpn_nl_peer_set_doit(struct sk_buff *skb, struct genl_info *info)*/ if (ret >0 )ovpn_peer_hash_vpn_ip (peer); +/* if the remote endpoint was updated, the by_transp_addr hash bucket +* also needs to be refreshed, otherwise incoming packets from the new +* remote address would fail the lockless lookup +*/ +if (attrs[OVPN_A_PEER_REMOTE_IPV4 ] || attrs[OVPN_A_PEER_REMOTE_IPV6 ]) +ovpn_peer_hash_transp_addr (peer);spin_unlock_bh (&ovpn->lock);ovpn_peer_put (peer);
drivers/net/ovpn/peer.c +68 -38
136 lines not on this page · raw message
drivers/net/ovpn/peer.h +1 -0
@@ -150,6 +150,7 @@ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_priv *ovpn, u32 peer_id);struct ovpn_peer *ovpn_peer_get_by_dst (struct ovpn_priv *ovpn,struct sk_buff *skb);void ovpn_peer_hash_vpn_ip (struct ovpn_peer *peer); +void ovpn_peer_hash_transp_addr (struct ovpn_peer *peer);bool ovpn_peer_check_by_src (struct ovpn_priv *ovpn,struct sk_buff *skb,struct ovpn_peer *peer);
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH 3/9 ovpn: fix data race reading cached local endpoint on TX path
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]>
The cached local source address bind->local is updated in place on a live, RCU-published ovpn_bind while holding peer->lock: the UDP output error paths reset it (ovpn_udp4_output()/ovpn_udp6_output()) and the RX float path learns it (ovpn_peer_endpoints_update()). The UDP TX fast path and the netlink dump, however, read bind->local holding only rcu_read_lock(), never peer->lock.
For bind->local.ipv6 this is a torn read: struct in6_addr is 128 bit and is copied as multiple words, so a concurrent in-place update can make a reader observe a mix of the old and new address. The mangled source address then feeds ip6_dst_lookup_flow() and udp_tunnel6_xmit_skb(). For bind->local.ipv4 (a single aligned word) it is a data race without tearing.
A spinlock on the per-packet TX path is not acceptable, and READ_ONCE()/WRITE_ONCE() cannot atomically access the 128-bit IPv6 address (the >8-byte access is rejected at build time and per-word accesses still can't yield a consistent snapshot).
Serialize the IPv6 field with a per-peer seqcount_spinlock_t tied to the existing peer->lock: the in-place writers (already under peer->lock) bump it, and readers take a lock-free read_seqcount_begin()/retry() snapshot via the new ovpn_peer_local_ipv6() helper. The single-word IPv4 field is handled with plain READ_ONCE()/WRITE_ONCE(). bind->remote is untouched:
it is immutable for a given bind object (only swapped via whole-bind RCU replacement), so reading it locklessly remains safe.
--- drivers/net/ovpn/netlink.c | 13 +++++++++++-- drivers/net/ovpn/peer.c | 26 +++++++++++++++++++++++++- drivers/net/ovpn/peer.h | 6 ++++++ drivers/net/ovpn/udp.c | 17 +++++++++++++---- 4 files changed, 55 insertions(+), 7 deletions(-)
drivers/net/ovpn/netlink.c +11 -2
@@ -610,14 +610,23 @@ static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *info, bind = rcu_dereference(peer->bind); if (bind) { if (bind->remote.in4.sin_family == AF_INET) { + /* bind->local is updated in place under peer->lock; + * READ_ONCE() pairs with the WRITE_ONCE() updaters + */ if (nla_put_in_addr(skb, OVPN_A_PEER_REMOTE_IPV4, bind->remote.in4.sin_addr.s_addr) || nla_put_net16(skb, OVPN_A_PEER_REMOTE_PORT, bind->remote.in4.sin_port) || nla_put_in_addr(skb, OVPN_A_PEER_LOCAL_IPV4, - bind->local.ipv4.s_addr)) + READ_ONCE(bind->local.ipv4.s_addr))) goto err_unlock; } else if (bind->remote.in4.sin_family == AF_INET6) { + struct in6_addr local_ipv6; + + /* read the 128-bit local address under the peer + * seqcount to avoid a torn read + */ + ovpn_peer_local_ipv6(peer, bind, &local_ipv6); if (nla_put_in6_addr(skb, OVPN_A_PEER_REMOTE_IPV6, &bind->remote.in6.sin6_addr) || nla_put_u32(skb, OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID,
@@ -625,7 +634,7 @@ static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *info,nla_put_net16 (skb,OVPN_A_PEER_REMOTE_PORT , bind->remote.in6.sin6_port) ||nla_put_in6_addr (skb,OVPN_A_PEER_LOCAL_IPV6 , - &bind->local.ipv6)) + &local_ipv6)) goto err_unlock; } }
drivers/net/ovpn/peer.c +25 -1
@@ -112,6 +112,7 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *ovpn, u32 id)RCU_INIT_POINTER (peer->bind,NULL );ovpn_crypto_state_init (&peer->crypto);spin_lock_init (&peer->lock); +seqcount_spinlock_init (&peer->bind_local_seq, &peer->lock);kref_init (&peer->refcount);ovpn_peer_stats_init (&peer->vpn_stats);ovpn_peer_stats_init (&peer->link_stats);
@@ -175,6 +176,27 @@ int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer,return 0 ; } +/** +* ovpn_peer_local_ipv6 - read the cached local IPv6 endpoint of a peer +* @peer: the peer owning the binding +* @bind: the binding to read the local address from +* @dst: where the local IPv6 address is copied to +* +* bind->local is updated in place under peer->lock (TX error path and RX +* float path). Read the 128-bit address under the peer seqcount so that +* lockless readers never observe a torn value. +*/ +void ovpn_peer_local_ipv6 (const struct ovpn_peer *peer, +const struct ovpn_bind *bind,struct in6_addr *dst) +{ +unsigned int seq; + +do { + seq =read_seqcount_begin (&peer->bind_local_seq); + *dst = bind->local.ipv6; + }while (read_seqcount_retry (&peer->bind_local_seq, seq)); +} +/* variable name __tbl2 needs to be different from __tbl1 * in the macro below to avoid confusing clang */
@@ -237,7 +259,7 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)netdev_name (peer->ovpn->dev), peer->id, &bind->local.ipv4.s_addr, &ip_hdr(skb)->daddr); - bind->local.ipv4.s_addr =ip_hdr (skb)->daddr; +WRITE_ONCE (bind->local.ipv4.s_addr,ip_hdr (skb)->daddr); reset_cache = true; }break ;
@@ -268,7 +290,9 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)netdev_name (peer->ovpn->dev), peer->id, &bind->local.ipv6, &ipv6_hdr(skb)->daddr); +write_seqcount_begin (&peer->bind_local_seq); bind->local.ipv6 =ipv6_hdr (skb)->daddr; +write_seqcount_end (&peer->bind_local_seq); reset_cache = true; }break ;
drivers/net/ovpn/peer.h +6 -0
@@ -10,6 +10,7 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_ +#include <linux/seqlock.h> #include <net/dst_cache.h> #include <net/strparser.h>
@@ -56,6 +57,8 @@* @link_stats: per-peer link/transport TX/RX stats * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..) * @lock: protects binding to peer (bind) and keepalive* fields +* @bind_local_seq: seqcount serializing in-place updates of bind->local +* (done under @lock) against lockless readers on the TX path * @refcount: reference counter * @rcu: used to free peer in an RCU safe way * @release_entry: entry for the socket release list
@@ -110,6 +113,7 @@ struct ovpn_peer {struct ovpn_peer_stats link_stats;enum ovpn_del_peer_reason delete_reason;spinlock_t lock;/* protects bind and keepalive* */ +seqcount_spinlock_t bind_local_seq;/* protects bind->local */ struct kref refcount;struct rcu_head rcu;struct llist_node release_entry;
@@ -151,6 +155,8 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *ovpn, struct sk_buff *skb);void ovpn_peer_hash_vpn_ip (struct ovpn_peer *peer);void ovpn_peer_hash_transp_addr (struct ovpn_peer *peer); +void ovpn_peer_local_ipv6 (const struct ovpn_peer *peer, +const struct ovpn_bind *bind,struct in6_addr *dst);bool ovpn_peer_check_by_src (struct ovpn_priv *ovpn,struct sk_buff *skb,struct ovpn_peer *peer);
drivers/net/ovpn/udp.c +13 -4
@@ -147,7 +147,10 @@ static int ovpn_udp4_output(struct ovpn_peer *peer, struct ovpn_bind *bind, {struct rtable *rt;struct flowi4 fl = { - .saddr = bind->local.ipv4.s_addr, +/* bind->local is updated in place under peer->lock; a single +* aligned word is read/written atomically via {READ,WRITE}_ONCE +*/ + .saddr =READ_ONCE (bind->local.ipv4.s_addr), .daddr = bind->remote.in4.sin_addr.s_addr, .fl4_sport =inet_sk (sk)->inet_sport, .fl4_dport = bind->remote.in4.sin_port,
@@ -169,7 +172,7 @@ static int ovpn_udp4_output(struct ovpn_peer *peer, struct ovpn_bind *bind,*/ fl.saddr =0 ;spin_lock_bh (&peer->lock); - bind->local.ipv4.s_addr =0 ; +WRITE_ONCE (bind->local.ipv4.s_addr,0 );spin_unlock_bh (&peer->lock);dst_cache_reset (cache); }
@@ -178,7 +181,7 @@ static int ovpn_udp4_output(struct ovpn_peer *peer, struct ovpn_bind *bind,if (IS_ERR (rt) &&PTR_ERR (rt) == -EINVAL ) { fl.saddr =0 ;spin_lock_bh (&peer->lock); - bind->local.ipv4.s_addr =0 ; +WRITE_ONCE (bind->local.ipv4.s_addr,0 );spin_unlock_bh (&peer->lock);dst_cache_reset (cache);
@@ -224,7 +227,6 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,int ret;struct flowi6 fl = { - .saddr = bind->local.ipv6, .daddr = bind->remote.in6.sin6_addr, .fl6_sport =inet_sk (sk)->inet_sport, .fl6_dport = bind->remote.in6.sin6_port,
@@ -233,6 +235,11 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind, .flowi6_oif = bind->remote.in6.sin6_scope_id, }; +/* bind->local is updated in place under peer->lock; read the 128-bit +* address under the peer seqcount to avoid a torn read +*/ +ovpn_peer_local_ipv6 (peer, bind, &fl.saddr); +local_bh_disable (); dst =dst_cache_get_ip6 (cache, &fl.saddr);if (dst)
@@ -245,7 +252,9 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,*/ fl.saddr = in6addr_any;spin_lock_bh (&peer->lock); +write_seqcount_begin (&peer->bind_local_seq); bind->local.ipv6 = in6addr_any; +write_seqcount_end (&peer->bind_local_seq);spin_unlock_bh (&peer->lock);dst_cache_reset (cache); }
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Re: [Openvpn-devel] [PATCH ovpn net 3/9] ovpn: fix data race reading cached local endpoint on TX path
Quoted 29 lines · Antonio Quartulli
From: Antonio Quartulli <[email protected]>
The cached local source address
bind->localis updated in place on a live, RCU-publishedovpn_bindwhile holdingpeer->lock: the UDP output error paths reset it (ovpn_udp4_output()/ovpn_udp6_output()) and the RX float path learns it (ovpn_peer_endpoints_update()). The UDP TX fast path and the netlink dump, however, readbind->localholding onlyrcu_read_lock(), neverpeer->lock.For
bind->local.ipv6this is a torn read: structin6_addris 128 bit and is copied as multiple words, so a concurrent in-place update can make a reader observe a mix of the old and new address. The mangled source address then feedsip6_dst_lookup_flow()andudp_tunnel6_xmit_skb(). Forbind->local.ipv4(a single aligned word) it is a data race without tearing.A spinlock on the per-packet TX path is not acceptable, and READ_ONCE()/WRITE_ONCE() cannot atomically access the 128-bit IPv6 address (the >8-byte access is rejected at build time and per-word accesses still can't yield a consistent snapshot).
Serialize the IPv6 field with a per-peer
seqcount_spinlock_ttied to the existingpeer->lock: the in-place writers (already underpeer->lock) bump it, and readers take a lock-free read_seqcount_begin()/retry() snapshot via the newovpn_peer_local_ipv6()helper. The single-word IPv4 field is handled with plain READ_ONCE()/WRITE_ONCE().bind->remoteis untouched:
it is immutable for a given bind object (only swapped via whole-bind RCU replacement), so reading it locklessly remains safe.
Quoted 2 lines
Fixes 08857b5ec5d9 ("ovpn: implement basic TX path (UDP)")Signed-off-by Antonio Quartulli <[email protected]>
Using seqcount_spinlock may be a bit overkill and complicated.
Ralf is attempting a fix by turning the ovpn_bind object to an RCU protected pointer.
This way any modification will be made by creating a new bind object and purging the old one, rather than modifying the current one.
We'll see how large the patch happens to be and possibly replace this one.
Regards,
Signature · 8 lines
Antonio Quartulli _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH 4/9 ovpn: ensure socket is owned by ovpn before deref sk_user_data
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]>
Some subsystems, like BPF SOCKMAP, set sk_user_data without actually setting the encap_type.
For this reason, we must make sure that the type is the one ovpn expects before dereferencing sk_user_data.
Failing to do so may lead to out-of-bounds reads.
--- drivers/net/ovpn/socket.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/net/ovpn/socket.c +9 -0
@@ -162,6 +162,15 @@ struct ovpn_socket *ovpn_socket_new(struct socket *sock, struct ovpn_peer *peer)rcu_read_lock (); ovpn_sock =rcu_dereference_sk_user_data (sk);if (ovpn_sock) { +/* something else filled the sk_user_data without +* setting the encap_type. Reject the socket. +*/ +if (!type) { + ovpn_sock =ERR_PTR (-EBUSY ); +rcu_read_unlock (); + goto sock_release; + } +/* socket owned by another ovpn instance, we can't use it */ if (ovpn_sock->ovpn != peer->ovpn) { ovpn_sock =ERR_PTR (-EBUSY );
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH 5/9 ovpn: tcp - fix peer reference leak on deferred deletion
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]>
The TCP error paths in ovpn_tcp_rcv() and ovpn_tcp_send_sock() take a peer reference and then schedule the deferred-delete work:
ovpn_peer_hold(peer); schedule_work(&peer->tcp.defer_del_work);
ovpn_tcp_peer_del_work() drops exactly one reference per run, but schedule_work() returns false and does not re-queue when the work is already pending. The reference, however, was taken unconditionally, so every hold+schedule that lands on an already-pending work leaks one peer reference.
ovpn_tcp_rcv() is the strparser receive callback and has no guard against
this: a TCP segment packed with packets whose length header is valid for the stream parser but whose payload is smaller than the opcode size passes ovpn_tcp_parse() and hits the error path. strparser delivers all complete messages in a loop, so many error invocations run before the scheduled work executes, leaking one reference each. A remote peer can exploit this to pin the peer (and the netdev reference it holds) forever, preventing interface teardown - a denial of service.
Take the reference only when schedule_work() actually queues the work. schedule_work() flips the work pending bit atomically, so exactly one caller - even across the concurrent RX and TX paths - observes the idle->pending transition and acquires the single reference that the lone ovpn_peer_put() in the worker balances. ovpn_peer_del() is idempotent (ovpn_peer_remove() bails on an already-unhashed peer), so a work item re-queued while running stays refcount-balanced.
--- drivers/net/ovpn/tcp.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-)
drivers/net/ovpn/tcp.c +19 -10
@@ -148,10 +148,14 @@ 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; -schedule_work (&peer->tcp.defer_del_work); +/* schedule deferred peer deletion and take a reference only if the +* work was actually queued: the matching ovpn_peer_put() in +* ovpn_tcp_peer_del_work() runs once per queued work, so re-arming an +* already-pending work must not take another reference (it would be +* leaked, e.g. on a flood of invalid packets) +*/ +if (schedule_work (&peer->tcp.defer_del_work)) +ovpn_peer_hold (peer);ovpn_dev_dstats_rx_dropped (peer->ovpn->dev); err_nopeer:kfree_skb (skb);
@@ -280,15 +284,20 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk) peer->id, ret);/* in case of TCP error we can't recover the VPN -* stream therefore we abort the connection +* stream therefore we abort the connection. +* +* Take a reference only if the work was actually +* queued: ovpn_tcp_peer_del_work() drops exactly one +* reference per run, so re-arming an already-pending +* work (e.g. already scheduled from the RX path) must +* not take another reference (it would be leaked). */ -ovpn_peer_hold (peer); -schedule_work (&peer->tcp.defer_del_work); +if (schedule_work (&peer->tcp.defer_del_work)) +ovpn_peer_hold (peer);/* we bail out immediately and keep tx_in_progress set -* to true. This way we prevent more TX attempts -* which would lead to more invocations of -* schedule_work() +* to true, so that no further TX is attempted on the +* aborted stream */ return ; }
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH 6/9 ovpn: zero-initialize sockaddr before learning a floated endpoint
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]>
ovpn_peer_endpoints_update() builds the new remote endpoint in an on-stack struct sockaddr_storage that is left uninitialized. For IPv4 only sin_family/sin_addr/sin_port are written, leaving the 8-byte sin_zero padding as stack garbage (for IPv6, sin6_flowinfo is left uninitialized likewise).
ovpn_peer_reset_sockaddr() -> ovpn_bind_from_sockaddr() then memcpy()s sizeof(struct sockaddr_in)/sizeof(struct sockaddr_in6) bytes - padding included - into bind->remote. That buffer is later hashed with jhash() over the same length to place the peer in the by_transp_addr table, so the garbage padding lands the floated peer in an essentially random bucket. Lockless lookups in ovpn_peer_get_by_transp_addr() build their key from a zero-initialized sockaddr_storage, compute a different bucket and fail to find the peer.
This is also a plain use of uninitialized stack memory in jhash().
Zero-initialize the sockaddr_storage, matching what the lookup and netlink paths already do.
--- drivers/net/ovpn/peer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ovpn/peer.c +1 -1
@@ -220,7 +220,7 @@ static void __ovpn_peer_hash_transp_addr(struct ovpn_peer *peer,*/ void ovpn_peer_endpoints_update (struct ovpn_peer *peer,struct sk_buff *skb) { -struct sockaddr_storage ss; +struct sockaddr_storage ss = {};struct sockaddr_in6 *sa6;bool reset_cache = false;struct sockaddr_in *sa;
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH 7/9 ovpn: hash floated peer by transport identity only
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]>
The by_transp_addr table is keyed on the peer's remote transport address, but the float rehash hashed bind->remote directly, while the two other sites that touch the table build a clean key first:ovpn_peer_add_mp() and the lookup in ovpn_peer_get_by_transp_addr() both hash a sockaddr holding only family/address/port.
For a link-local IPv6 peer, bind->remote carries sin6_scope_id (set from ipv6_iface_scope_id() when the endpoint is learned), and that field is folded into the jhash() over sizeof(struct sockaddr_in6). The lookup never sets sin6_scope_id, so after such a peer floats it is rehashed into a scope_id-dependent bucket that lookups (scope_id 0) never visit, making the peer unreachable through the by_transp_addr fallback. ovpn_peer_transp_match() only compares address and port, so the hash was keying on a field the match ignores.
sin6_scope_id must stay in bind->remote because the TX path uses it as flowi6_oif, so it cannot just be cleared there. Instead build the hash key from family/address/port only, exactly like ovpn_peer_add_mp() and the lookup, so all three sites agree on the bucket.
--- drivers/net/ovpn/peer.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/net/ovpn/peer.c +21 -4
@@ -907,7 +907,10 @@ bool ovpn_peer_check_by_src(struct ovpn_priv *ovpn, struct sk_buff *skb,static void __ovpn_peer_hash_transp_addr (struct ovpn_peer *peer,const struct ovpn_bind *bind) { +struct sockaddr_storage sa = {};struct hlist_nulls_head *nhead; +struct sockaddr_in6 *sa6; +struct sockaddr_in *sa4;size_t salen;lockdep_assert_held (&peer->ovpn->lock);
@@ -923,12 +926,26 @@ static void __ovpn_peer_hash_transp_addr(struct ovpn_peer *peer,if (unlikely (hlist_unhashed (&peer->hash_entry_id)))return ; +/* Build the hash key from the transport identity only +* (family/address/port), matching ovpn_peer_add_mp() and the lookup +* in ovpn_peer_get_by_transp_addr(). Hashing bind->remote directly +* would fold in sin6_scope_id (set on the float path but never by the +* lookup), scattering the peer into a bucket lookups cannot reach. +*/ switch (bind->remote.in4.sin_family) {case AF_INET : - salen =sizeof (struct sockaddr_in ); + sa4 = (struct sockaddr_in *)&sa; + sa4->sin_family =AF_INET ; + sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr; + sa4->sin_port = bind->remote.in4.sin_port; + salen =sizeof (*sa4);break ;case AF_INET6 : - salen =sizeof (struct sockaddr_in6 ); + sa6 = (struct sockaddr_in6 *)&sa; + sa6->sin6_family =AF_INET6 ; + sa6->sin6_addr = bind->remote.in6.sin6_addr; + sa6->sin6_port = bind->remote.in6.sin6_port; + salen =sizeof (*sa6);break ;default :return ;
@@ -937,8 +954,8 @@ static void __ovpn_peer_hash_transp_addr(struct ovpn_peer *peer,/* remove old hashing (no-op if entry is not currently linked) */ hlist_nulls_del_init_rcu (&peer->hash_entry_transp_addr);/* re-add with current transport address */ - nhead =ovpn_get_hash_head (peer->ovpn->peers->by_transp_addr, - &bind->remote, salen); + nhead =ovpn_get_hash_head (peer->ovpn->peers->by_transp_addr, &sa, + salen);hlist_nulls_add_head_rcu (&peer->hash_entry_transp_addr, nhead); }
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH 8/9 ovpn: disable IPv4 redirects on MP interface after registration
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]>
ovpn_mp_alloc() tried to disable SEND_REDIRECTS on a multipeer interface, but it runs from ovpn_net_init() (->ndo_init), which register_netdevice() invokes before the NETDEV_REGISTER notifier chain. The IPv4 in_device is only created when that notifier reaches inetdev_event() -> inetdev_init(), so __in_dev_get_rtnl() always returned NULL at ndo_init time and the whole redirect-disabling block (both the per-device and the per-netns IPV4_DEVCONF_ALL write) was dead. MP interfaces therefore kept emitting ICMP redirects.
Move the redirect-disabling to ovpn_newlink(), right after a successful register_netdevice(): at that point the NETDEV_REGISTER notifier has run and the in_device exists, and RTNL is held by the newlink path so __in_dev_get_rtnl() is safe. A successful register_netdevice() guarantees the in_device was created (otherwise the notifier would have failed and registration rolled back), so the in_device check is now a real guard rather than dead code.
The peer-table allocation stays in ovpn_mp_alloc()/->ndo_init, where it belongs (it does not depend on the in_device and is freed in ->ndo_uninit).
--- drivers/net/ovpn/main.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-)
drivers/net/ovpn/main.c +26 -15
@@ -35,25 +35,11 @@ static void ovpn_priv_free(struct net_device *net)static int ovpn_mp_alloc (struct ovpn_priv *ovpn) { -struct in_device *dev_v4;int i;if (ovpn->mode !=OVPN_MODE_MP )return 0 ; - dev_v4 =__in_dev_get_rtnl (ovpn->dev); -if (dev_v4) { -/* disable redirects as Linux gets confused by ovpn -* handling same-LAN routing. -* This happens because a multipeer interface is used as -* relay point between hosts in the same subnet, while -* in a classic LAN this would not be needed because the -* two hosts would be able to talk directly. -*/ -IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); -IPV4_DEVCONF_ALL (dev_net (ovpn->dev),SEND_REDIRECTS ) = false; - } -/* the peer container is fairly large, therefore we allocate it only in * MP mode */
@@ -183,6 +169,8 @@ static int ovpn_newlink(struct net_device *dev,struct ovpn_priv *ovpn =netdev_priv (dev);struct nlattr **data = params->data;enum ovpn_mode mode =OVPN_MODE_P2P ; +struct in_device *dev_v4; +int ret;if (data && data[IFLA_OVPN_MODE ]) { mode =nla_get_u8 (data[IFLA_OVPN_MODE ]);
@@ -207,7 +195,30 @@ static int ovpn_newlink(struct net_device *dev,else netif_carrier_off (dev); -return register_netdevice (dev); + ret =register_netdevice (dev); +if (ret <0 ) +return ret; + +/* The IPv4 in_device is created by the NETDEV_REGISTER notifier, which +* fires inside register_netdevice() above, so this cannot be done +* earlier (e.g. in ndo_init). RTNL is held by the newlink path. +*/ +if (ovpn->mode ==OVPN_MODE_MP ) { + dev_v4 =__in_dev_get_rtnl (dev); +if (dev_v4) { +/* disable redirects as Linux gets confused by ovpn +* handling same-LAN routing. +* This happens because a multipeer interface is used as +* relay point between hosts in the same subnet, while +* in a classic LAN this would not be needed because the +* two hosts would be able to talk directly. +*/ +IN_DEV_CONF_SET (dev_v4,SEND_REDIRECTS , false); +IPV4_DEVCONF_ALL (dev_net (dev),SEND_REDIRECTS ) = false; + } + } + +return 0 ; }static int ovpn_fill_info (struct sk_buff *skb,const struct net_device *dev)
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH 9/9 ovpn: ensure TCP vars are initialized first
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]>
Netlink calls may access TCP global vars (i.e. when attaching a TCP socket), therefore we need to make sure the latters are initialized beforehand.
For this reason move the global TCP initialization at the top of the module init function.
--- drivers/net/ovpn/main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/ovpn/main.c +7 -3
@@ -244,8 +244,14 @@ static struct rtnl_link_ops ovpn_link_ops = {static int __init ovpn_init (void ) { -int err =rtnl_link_register (&ovpn_link_ops); +int err; +/* init TCP first so that any subsequent netlink operation +* is ensured to access initialized TCP global vars +*/ +ovpn_tcp_init (); + + err =rtnl_link_register (&ovpn_link_ops);if (err) {pr_err ("ovpn: can't register rtnl link ops: %d\n" , err);return err;
@@ -257,8 +263,6 @@ static int __init ovpn_init(void) goto unreg_rtnl; } -ovpn_tcp_init (); -return 0 ; unreg_rtnl:
Signature · 8 lines
2.53.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Sabrina Dubroca
reviewerQuoted 30 lines · Antonio Quartulli
From: Antonio Quartulli <[email protected]>
ovpn_nl_peer_set_doit()resolves the target peer viaovpn_peer_get_by_id()before takingovpn->lock. In the window between the lookup (which only takes a refcount) and the subsequentspin_lock_bh(&ovpn->lock), a concurrentOVPN_CMD_PEER_DEL, keepalive expiry, or socket teardown can takeovpn->lockfirst, runovpn_peer_remove()to unhash the peer from all four tables (by_id, by_vpn_addr4/6,by_transp_addr) and release the lock.set_doitthen acquiresovpn->lockand callsovpn_peer_hash_vpn_ip(), which re-inserts the now-removed peer back into the rehashing tables.The same race affects the float path:
ovpn_peer_endpoints_update()holds only a refcount and acquiresovpn->lockvery late (after async AEAD decrypt and a netlink notification), then rehashes the peer in theby_transp_addrtable.The resurrected peer becomes reachable again from the RX lookup (
ovpn_peer_get_by_transp_addr) and the TX VPN-IP lookup, even though userspace believes it is gone. Once the data-path refcount drops the peer is freed viacall_rcuwhile the hash entries embedded in it remain linked, opening a UAF window.Bail out of the rehash when
hash_entry_idis unhashed, mirroring the sentinel already used byovpn_peer_remove()to detect the already-removed state. The check is safe underovpn->lock, which serializes every mutation ofhash_entry_id, and is a no-op for the add path becauseovpn_peer_add_mp()insertshash_entry_idbefore callingovpn_peer_hash_vpn_ip().
Quoted 5 lines
Fixes 1d36a36f6d53 ("ovpn: implement peer add/get/dump/delete via netlink")Signed-off-by Antonio Quartulli <[email protected]>--- drivers/net/ovpn/peer.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
This looks correct to me, just one small thing:
Quoted 7 lines
drivers/net/ovpn/peer.c +0
-0@@ -307,6 +307,16 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)return ; }
drivers/net/ovpn/peer.c void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb) · line 318
Quoted 10 lines
drivers/net/ovpn/peer.c +9
-0+/* peer may have been concurrently removed between the caller's +* initial lookup and our acquisition of ovpn->lock; skip the +* rehash so we don't re-insert a removed peer +*/ +if (unlikely (hlist_unhashed (&peer->hash_entry_id))) { +spin_unlock_bh (&peer->lock); +spin_unlock_bh (&peer->ovpn->lock); +return ; + }
I'd add an "unlock2" label (or "unlock_both") to unlock both (maybe just after the "hlist_nulls_add_head_rcu", before the existing unlock). The unlock/return is a bit verbose and we have it 3 times after this patch.
Signature · 7 lines
Sabrina _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Quoted 8 lines · Sabrina Dubroca
Quoted 8 lines
drivers/net/ovpn/peer.c +0
-0@@ -307,6 +307,16 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)return ; }
Quoted 14 lines
drivers/net/ovpn/peer.c void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb) · line 318
Quoted 9 lines
drivers/net/ovpn/peer.c +9
-0+/* peer may have been concurrently removed between the caller's +* initial lookup and our acquisition of ovpn->lock; skip the +* rehash so we don't re-insert a removed peer +*/ +if (unlikely (hlist_unhashed (&peer->hash_entry_id))) { +spin_unlock_bh (&peer->lock); +spin_unlock_bh (&peer->ovpn->lock); +return ; + }I'd add an "unlock2" label (or "
unlock_both") to unlock both (maybe just after the "hlist_nulls_add_head_rcu", before the existing unlock). The unlock/return is a bit verbose and we have it 3 times after this patch.
Yap, make sense!
I'll respin the patch with this change.
Cheers,
Signature · 8 lines
Antonio Quartulli _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel