ovpn: fix issues reported by Sashiko
Subsystems (4)
Ralf Lici
reviewercover ovpn: fix issues reported by Sashiko
Hi,
This series fixes a few issues that Sashiko reported while reviewing earlier ovpn patches and that were still present on current ovpn-net-next main. All of them are pre-existing problems, not regressions introduced by the patches under review, which is why they went unaddressed so far.
Patch 1 resets the peer dst cache before publishing a new bind when the remote or local endpoint changes. With the opposite order a concurrent TX could pair the new bind with a route cached for the old one, and on a cross-family float even get a dst of the wrong family.
Patch 2 drops the netdev reference from the peer RCU callback instead of right after call_rcu, where the peer could already have been freed.
Patch 3 makes the duplicate VPN address selftests require the expected error, so that they cannot pass on an unrelated failure.
Each patch links to the Sashiko report it addresses.
Thanks,
Ralf Lici
Mandelbit Srl
---
Ralf Lici (3):
ovpn: reset dst cache before publishing a new bind
ovpn: release netdev reference from the peer RCU callback
selftests: ovpn: check the error of duplicate VPN address tests
drivers/net/ovpn/netlink.c | 7 +++++- drivers/net/ovpn/peer.c | 15 ++++++++----- tools/testing/selftests/net/ovpn/common.sh | 21 +++++++++++++++++ tools/testing/selftests/net/ovpn/test.sh | 26 ++++++++++++++++------ 4 files changed, 55 insertions(+), 14 deletions(-)
base-commit: 7ccc5e05043b1bb84326b19a2b05938d8f7a2ae6
Signature · 8 lines
2.56.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH 1/3 ovpn: reset dst cache before publishing a new bind
When the peer endpoint changes, the new bind is published with RCU before the dst cache is reset. TX does not take peer->lock before the cache lookup, so a concurrent transmitter can pick up the new bind and still get a hit on the entry cached for the old one.
The packet then goes out over the old route and cached source address but towards the new remote. On a cross-family float, dst_cache_get_ip4 or dst_cache_get_ip6 would also return a dst of the wrong family, which is then passed to the other family's xmit helper.
Reset the cache before publishing the new bind, both when the endpoint is updated from RX and when it is changed via netlink. Both run under peer->lock, which a TX must hold to store a new dst, and the store requires the bind to still be the current one. Therefore, once the new bind is visible no stale entry can be repopulated. A failed bind update at worst costs one extra cache miss.
--- drivers/net/ovpn/netlink.c | 7 ++++++- drivers/net/ovpn/peer.c | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-)
drivers/net/ovpn/netlink.c +6 -1
@@ -333,6 +333,12 @@ static int ovpn_nl_peer_modify(struct ovpn_peer *peer, struct genl_info *info,*/ local_ip =ovpn_nl_attr_local_ip (attrs); +/* reset the cache before publishing the new bind, so that +* concurrent TX cannot pair it with a route cached for the +* old one +*/ +dst_cache_reset (&peer->dst_cache); +/* set peer sockaddr */ ret =ovpn_peer_reset_sockaddr (peer, &ss, local_ip);if (ret <0 ) {
@@ -341,7 +347,6 @@ static int ovpn_nl_peer_modify(struct ovpn_peer *peer, struct genl_info *info, ret); goto err_unlock; } -dst_cache_reset (&peer->dst_cache); }/* In a multipeer-to-multipeer setup we may have asymmetric peer IDs,
drivers/net/ovpn/peer.c +5 -5
@@ -294,16 +294,16 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb) goto unlock; } +/* reset the cache before publishing the new bind, so that concurrent TX +* cannot pair it with a route cached for the old one +*/ +dst_cache_reset (&peer->dst_cache); +if (unlikely (ovpn_peer_reset_sockaddr (peer, (struct sockaddr_storage *)&ss, local_ip) <0 )) goto unlock; -/* reset the cache only after a successful bind update to avoid useless -* cache misses on concurrent TX -*/ -dst_cache_reset (&peer->dst_cache); -/* if only the local address changed, bail out now */ if (!floated) goto unlock;
Signature · 8 lines
2.56.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH 2/3 ovpn: release netdev reference from the peer RCU callback
In reply to Ralf Liciovpn_peer_release queues ovpn_peer_release_rcu, which frees the peer, and only then calls netdev_put, which still dereferences peer->ovpn and peer->dev_tracker.
When the last reference is dropped from preemptible process context, for instance in a netlink handler, the task may be preempted right after call_rcu. If a grace period elapses before it runs again, netdev_put operates on freed memory, and with CONFIG_NET_DEV_REFCNT_TRACKER ref_tracker_free also writes through the stale tracker pointer.
Drop the netdev reference from the RCU callback instead, right before freeing the peer, so that the peer is not accessed once it has been queued for freeing. This also keeps the netdev, and therefore the module, pinned until the callback has run, as ovpn_cleanup expects when it waits for it with rcu_barrier. Simply calling netdev_put before call_rcu would let unregistration and module exit complete before the callback is even queued.
--- drivers/net/ovpn/peer.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/ovpn/peer.c +4 -1
@@ -352,6 +352,10 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)* perform it in the RCU callback, when all contexts are done */ dst_cache_destroy (&peer->dst_cache); +/* drop the netdev reference only now, so that unregistration cannot +* complete before this callback has run +*/ +netdev_put (peer->ovpn->dev, &peer->dev_tracker);kfree (peer); }
@@ -366,7 +370,6 @@ static void ovpn_peer_release(struct ovpn_peer *peer)ovpn_bind_reset (peer,NULL );spin_unlock_bh (&peer->lock);call_rcu (&peer->rcu, ovpn_peer_release_rcu); -netdev_put (peer->ovpn->dev, &peer->dev_tracker); }/**
Signature · 8 lines
2.56.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH 3/3 selftests: ovpn: check the error of duplicate VPN address tests
In reply to Ralf LiciThe duplicate VPN address checks only verify that the command fails, so they also pass if it fails for an unrelated reason.
The creation checks use OVPN_NUM_PEERS + 1 as the new peer ID, but the UDP server loads the whole peers file regardless of OVPN_NUM_PEERS. When the variable is overridden with a smaller value the ID is already in use, and ovpn_peer_add_mp fails with -EEXIST before looking at the VPN address. The update checks use peer 2, which does not exist in TCP mode with OVPN_NUM_PEERS=1, and would fail with -ENOENT.
Use a fixed ID that never clashes with the peers files, skip the update checks when peer 2 is not configured, and make all of them require "Address already in use" in the command output so that they only pass on a real address conflict.
--- tools/testing/selftests/net/ovpn/common.sh | 21 +++++++++++++++++ tools/testing/selftests/net/ovpn/test.sh | 26 ++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-)
tools/testing/selftests/net/ovpn/common.sh +21 -0
@@ -65,6 +65,19 @@ ovpn_cmd_run() { fi case "${mode}" in + fail:*) + # The mode is "fail:<msg>", as set by ovpn_cmd_fail_msg. Strip + # the "fail:" prefix with ${mode#fail:} to get the expected + # error message and look for it in the command output. + if [[ "${rc}" -eq 0 ]]; then + ret=1 + elif ! grep -qF "${mode#fail:}" "${output_file}"; then + cat "${output_file}" + printf '%s\n' \ + "${label}: expected error \"${mode#fail:}\": $*" + ret=1 + fi + ;; ok) if [[ "${rc}" -ne 0 ]]; then cat "${output_file}"
@@ -100,6 +113,14 @@ ovpn_cmd_fail() { ovpn_cmd_run fail "$@" } +# like ovpn_cmd_fail, but the command must also print the given error message +ovpn_cmd_fail_msg() { + local msg="$1" + + shift + ovpn_cmd_run "fail:${msg}" "$@" +} + ovpn_run_bg() { local pid_var="$1"
tools/testing/selftests/net/ovpn/test.sh +19 -7
@@ -74,7 +74,10 @@ ovpn_set_peer_vpn_addr() { ovpn_run_vpn_addr_validation() { local addr local peer1_addr4 - local test_peer_id=$((OVPN_NUM_PEERS + 1)) + # must not match any peer ID in the peers files, regardless of + # OVPN_NUM_PEERS, as peer ID conflicts are checked before VPN addresses + local test_peer_id=100 + local eaddrinuse="Address already in use" local test_peer_addr6="2001:db8::2" # Do not include 0.0.0.0 or :: here. They are invalid on creation, but # clear one address family on update and are valid if the other remains.
@@ -98,7 +101,8 @@ ovpn_run_vpn_addr_validation() { ovpn_new_test_peer "${test_peer_id}" "${addr}" done - ovpn_cmd_fail "reject duplicate IPv4 address on peer creation" \ + ovpn_cmd_fail_msg "${eaddrinuse}" \ + "reject duplicate IPv4 address on peer creation" \ ovpn_new_test_peer "${test_peer_id}" "${peer1_addr4}" ovpn_cmd_fail "reject clearing the last peer VPN address" \ ovpn_set_peer_vpn_addr 1 0.0.0.0
@@ -108,15 +112,23 @@ ovpn_run_vpn_addr_validation() { ovpn_set_peer_vpn_addr 1 "${addr}" done - ovpn_cmd_fail "reject duplicate IPv4 address on peer update" \ - ovpn_set_peer_vpn_addr 2 "${peer1_addr4}" + # peer 2 only exists when at least two peers are configured + if [ "${OVPN_NUM_PEERS}" -ge 2 ]; then + ovpn_cmd_fail_msg "${eaddrinuse}" \ + "reject duplicate IPv4 address on peer update" \ + ovpn_set_peer_vpn_addr 2 "${peer1_addr4}" + fi ovpn_cmd_ok "add peer IPv6 address" \ ovpn_set_peer_vpn_addr 1 "${test_peer_addr6}" - ovpn_cmd_fail "reject duplicate IPv6 address on peer creation" \ + ovpn_cmd_fail_msg "${eaddrinuse}" \ + "reject duplicate IPv6 address on peer creation" \ ovpn_new_test_peer "${test_peer_id}" "${test_peer_addr6}" - ovpn_cmd_fail "reject duplicate IPv6 address on peer update" \ - ovpn_set_peer_vpn_addr 2 "${test_peer_addr6}" + if [ "${OVPN_NUM_PEERS}" -ge 2 ]; then + ovpn_cmd_fail_msg "${eaddrinuse}" \ + "reject duplicate IPv6 address on peer update" \ + ovpn_set_peer_vpn_addr 2 "${test_peer_addr6}" + fi ovpn_cmd_ok "clear peer IPv4 address" \ ovpn_set_peer_vpn_addr 1 0.0.0.0
Signature · 8 lines
2.56.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel