ovpn: fix key and workqueue lifetime issues
Subsystems (2)
Ralf Lici
reviewerv5 cover ovpn: fix key and workqueue lifetime issues
Hi,
This series collects the ovpn fixes we discussed around key-slot handling, netlink key notifications, TCP initialization ordering, and teardown of deferred work.
The first two patches were sent separately before. They are included here again so the whole set can be picked in the intended order.
The last three patches fix lifetime problems where callback/workqueue cleanup could continue after the reference that pins netdev/module teardown had already been dropped. The series first makes crypto callbacks finish their own cleanup before releasing the peer, then moves ovpn work items to a module-owned workqueue that is drained during module exit, and finally defers key-slot AEAD transform freeing to that workqueue so crypto_free_aead() does not run from an RCU callback.
This series is based on top of these pending ovpn net fixes, which are not included here:
commit 746ed3312a64 ("ovpn: hold peer before scheduling keepalive work") commit 2e32df6613f3 ("ovpn: fix peer refcount leak in TCP error paths")
Thanks,
Ralf Lici
Mandelbit Srl
---
Changes since v4 https://lore.kernel.org/openvpn-devel/981d2ea51cca45138210aa52c6e5a0e55c0da7a0.1783099626.git.ralf@mandelbit.com/
- Include the NULL-slot and key-id notification fixes in this series so
the whole set can be picked in order.
- Add the TCP initialization ordering fix before the teardown/workqueue
fixes.
- Rebase the workqueue conversion on the pending keepalive and TCP
deferred-work refcount fixes listed above.
Ralf Lici (6):
ovpn: fix NULL dereference when killing missing key
ovpn: use nla_put_u32 for the key ID attribute in
ovpn_nl_key_swap_notify
ovpn: initialize TCP state before registering UAPI
ovpn: finish crypto callback cleanup before peer release
ovpn: run deferred work on a module-owned workqueue
ovpn: defer key slot crypto freeing to workqueue
drivers/net/ovpn/crypto.c | 25 +++++++++++-------------- drivers/net/ovpn/crypto.h | 3 +++ drivers/net/ovpn/crypto_aead.c | 20 ++++++++++++++------ drivers/net/ovpn/crypto_aead.h | 1 - drivers/net/ovpn/io.c | 10 +++++----- drivers/net/ovpn/main.c | 27 +++++++++++++++++++++++---- drivers/net/ovpn/netlink.c | 2 +- drivers/net/ovpn/ovpnpriv.h | 4 ++++ drivers/net/ovpn/peer.c | 8 ++++---- drivers/net/ovpn/tcp.c | 9 ++++----- 10 files changed, 69 insertions(+), 40 deletions(-)
Signature · 7 lines
2.55.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v5 1/6 ovpn: fix NULL dereference when killing missing key
ovpn_crypto_kill_key assumes both crypto slots are populated and dereferences each slot before checking it. That is not guaranteed: a peer can have only one installed key, and the kill path may be asked to remove a key that is not present.
Read each slot once while holding the crypto state lock, check for NULL before looking at key_id, and only replace the slot that actually matches.
---
Changes since v4 of this series https://lore.kernel.org/openvpn-devel/981d2ea51cca45138210aa52c6e5a0e55c0da7a0.1783099626.git.ralf@mandelbit.com/
- Add this previously posted standalone fix to the series so the whole
set can be picked in order.
- No changes since v2 of the original single patch
https://lore.kernel.org/openvpn-devel/19318904cf077d067cd4ec628a22bab03ed7dd29.1782993857.git.ralf@mandelbit.com/
Changes since v1 of the original single patch https://lore.kernel.org/openvpn-devel/9fc33e6f9fae10b9e372a3e06934d697edf5b024.1782829171.git.ralf@mandelbit.com/
- Remove unnecessary braces around single-statement if/else branches.
drivers/net/ovpn/crypto.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/ovpn/crypto.c +7 -3
@@ -58,15 +58,19 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs)bool ovpn_crypto_kill_key (struct ovpn_crypto_state *cs,u8 key_id) {struct ovpn_crypto_key_slot *ks =NULL ; +struct ovpn_crypto_key_slot *tmp;spin_lock_bh (&cs->lock); -if (rcu_access_pointer (cs->slots[0 ])->key_id == key_id) { + tmp =rcu_access_pointer (cs->slots[0 ]); +if (tmp && tmp->key_id == key_id) ks =rcu_replace_pointer (cs->slots[0 ],NULL ,lockdep_is_held (&cs->lock)); - }else if (rcu_access_pointer (cs->slots[1 ])->key_id == key_id) { +else + tmp =rcu_access_pointer (cs->slots[1 ]); + +if (!ks && tmp && tmp->key_id == key_id) ks =rcu_replace_pointer (cs->slots[1 ],NULL ,lockdep_is_held (&cs->lock)); - }spin_unlock_bh (&cs->lock);if (ks)
Signature · 7 lines
2.55.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v5 4/6 ovpn: finish crypto callback cleanup before peer release
In reply to Ralf LiciCrypto completion callbacks hold both key-slot and peer references. The peer reference pins the netdev, and dropping the last peer reference can let netdev unregistration and module removal make progress.
Do not release that peer reference before the callback has finished its own cleanup. If ovpn_crypto_key_slot_put runs after ovpn_peer_put, it can schedule an RCU callback backed by module text after ovpn_cleanup rcu_barrier has already run. The TX error path also freed the remaining skb after ovpn_peer_put, leaving callback cleanup outside the peer/netdev lifetime window.
Release the key slot and free any remaining skb first, then drop the peer reference as the last callback action.
---
No changes since v4 https://lore.kernel.org/openvpn-devel/981d2ea51cca45138210aa52c6e5a0e55c0da7a0.1783099626.git.ralf@mandelbit.com/
No changes since v3 https://lore.kernel.org/openvpn-devel/f367b736f2597edb1677794173997d5a0f9f599c.1783080055.git.ralf@mandelbit.com/
Changes since v2 https://lore.kernel.org/openvpn-devel/567de7a9371ce72b0632158799dd11bb543cae08.1783068961.git.ralf@mandelbit.com/
- Also free the remaining TX skb before dropping the peer reference, so
crypto callbacks do not continue cleanup after peer release.
drivers/net/ovpn/io.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/ovpn/io.c +5 -5
@@ -204,10 +204,10 @@ void ovpn_decrypt_post(void *data, int ret)ovpn_dev_dstats_rx_dropped (peer->ovpn->dev);kfree_skb (skb); drop_nocount: -if (likely (peer)) -ovpn_peer_put (peer);if (likely (ks))ovpn_crypto_key_slot_put (ks); +if (likely (peer)) +ovpn_peer_put (peer); }/* RX path entry point: decrypt packet and forward it to the device */
@@ -302,11 +302,11 @@ void ovpn_encrypt_post(void *data, int ret) err:if (unlikely (skb))ovpn_dev_dstats_tx_dropped (peer->ovpn->dev); -if (likely (peer)) -ovpn_peer_put (peer); +kfree_skb (skb);if (likely (ks))ovpn_crypto_key_slot_put (ks); -kfree_skb (skb); +if (likely (peer)) +ovpn_peer_put (peer); }static bool ovpn_encrypt_one (struct ovpn_peer *peer,struct sk_buff *skb)
Signature · 7 lines
2.55.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v5 5/6 ovpn: run deferred work on a module-owned workqueue
In reply to Ralf Liciovpn queues several work items whose callbacks execute module text. These works currently run on the global system workqueues, so module exit has no driver-owned drain point that guarantees the callbacks have fully returned before the module text can be freed.
Object references protect the objects used by the callbacks, but they do not prove that a workqueue function has returned. In particular, a worker can drop the final reference that unblocks device teardown while it is still executing ovpn code.
Add a module-owned workqueue and queue all ovpn work items on it. Destroy the workqueue during module exit, after unregistering the rtnl link ops and netlink family and before the final RCU barrier, so no ovpn work item can outlive the module text. The per-device delayed keepalive work remains explicitly disabled during netdev teardown (disable_delayed_work_sync in ndo_uninit), since destroy_workqueue does not flush delayed work that is still only pending on its timer.
---
Changes since v4 https://lore.kernel.org/openvpn-devel/6edfcc51e0855bfd34286b86d4e7f26bb3bcd3f7.1783099626.git.ralf@mandelbit.com/
- Rebase on the pending keepalive and TCP deferred-work refcount fixes,
preserving their hold-before-queue and queue-failure put handling when
converting schedule_work to queue_work.
Changes since v3 https://lore.kernel.org/openvpn-devel/49f38f89340e18ed30543d3990a7a7e20595b6af.1783080055.git.ralf@mandelbit.com/
- Replace the RCU-deferred peer netdev reference release with a
module-owned workqueue that drains all ovpn work callbacks before
module text can be freed.
drivers/net/ovpn/main.c | 17 ++++++++++++++++- drivers/net/ovpn/ovpnpriv.h | 4 ++++ drivers/net/ovpn/peer.c | 8 ++++---- drivers/net/ovpn/tcp.c | 9 ++++----- 4 files changed, 28 insertions(+), 10 deletions(-)
drivers/net/ovpn/main.c +16 -1
@@ -12,6 +12,7 @@#include <linux/module.h> #include <linux/netdevice.h> #include <linux/inetdevice.h> +#include <linux/workqueue.h> #include <net/gro_cells.h> #include <net/ip.h> #include <net/rtnetlink.h>
@@ -26,6 +27,9 @@#include "tcp.h" #include "udp.h" +/* module-owned workqueue on which all ovpn-specific work is queued */ +struct workqueue_struct *ovpn_wq; +static void ovpn_priv_free (struct net_device *net) {struct ovpn_priv *ovpn =netdev_priv (net);
@@ -240,10 +244,16 @@ static int __init ovpn_init(void)*/ ovpn_tcp_init (); + ovpn_wq =alloc_workqueue ("ovpn" ,0 ,0 ); +if (!ovpn_wq) { +pr_err ("ovpn: cannot allocate workqueue\n" ); +return -ENOMEM ; + } + err =rtnl_link_register (&ovpn_link_ops);if (err) {pr_err ("ovpn: can't register rtnl link ops: %d\n" , err); -return err; + goto destroy_wq; } err =ovpn_nl_register ();
@@ -256,6 +266,9 @@ static int __init ovpn_init(void) unreg_rtnl:rtnl_link_unregister (&ovpn_link_ops); +destroy_wq: +destroy_workqueue (ovpn_wq); + ovpn_wq =NULL ;return err; }
@@ -263,6 +276,8 @@ static __exit void ovpn_cleanup(void) {ovpn_nl_unregister ();rtnl_link_unregister (&ovpn_link_ops); +destroy_workqueue (ovpn_wq); + ovpn_wq =NULL ;rcu_barrier (); }
drivers/net/ovpn/ovpnpriv.h +4 -0
@@ -15,6 +15,10 @@#include <uapi/linux/if_link.h> #include <uapi/linux/ovpn.h> +struct workqueue_struct ; + +extern struct workqueue_struct *ovpn_wq; +/** * struct ovpn_peer_collection - container of peers for MultiPeer mode * @by_id: table of peers index by ID
drivers/net/ovpn/peer.c +4 -4
@@ -61,7 +61,7 @@ void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 timeout)/* now that interval and timeout have been changed, kick * off the worker so that the next delay can be recomputed */ -mod_delayed_work (system_percpu_wq, &peer->ovpn->keepalive_work,0 ); +mod_delayed_work (ovpn_wq, &peer->ovpn->keepalive_work,0 ); }/**
@@ -1287,7 +1287,7 @@ static time64_t ovpn_peer_keepalive_work_single(struct ovpn_peer *peer, peer->id);if (WARN_ON (!ovpn_peer_hold (peer)))return 0 ; -if (!schedule_work (&peer->keepalive_work)) +if (!queue_work (ovpn_wq, &peer->keepalive_work))ovpn_peer_put (peer); }
@@ -1379,8 +1379,8 @@ void ovpn_peer_keepalive_work(struct work_struct *work)netdev_dbg (ovpn->dev,"scheduling keepalive work: now=%llu next_run=%llu delta=%llu\n" , next_run, now, next_run - now); -schedule_delayed_work (&ovpn->keepalive_work, - (next_run - now) *HZ ); +queue_delayed_work (ovpn_wq, &ovpn->keepalive_work, + (next_run - now) *HZ ); }unlock_ovpn (ovpn, &release_list); }
drivers/net/ovpn/tcp.c +4 -5
@@ -151,7 +151,7 @@ static void ovpn_tcp_rcv(struct strparser *strp, struct sk_buff *skb)/* 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)) +if (!queue_work (ovpn_wq, &peer->tcp.defer_del_work))ovpn_peer_put (peer);ovpn_dev_dstats_rx_dropped (peer->ovpn->dev); err_nopeer:
@@ -284,13 +284,12 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk)* stream therefore we abort the connection */ ovpn_peer_hold (peer); -if (!schedule_work (&peer->tcp.defer_del_work)) +if (!queue_work (ovpn_wq, &peer->tcp.defer_del_work))ovpn_peer_put (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() +* which would lead to more invocations of queue_work() */ return ; }
@@ -487,7 +486,7 @@ static void ovpn_tcp_write_space(struct sock *sk)rcu_read_lock (); sock =rcu_dereference_sk_user_data (sk);if (likely (sock && sock->peer)) { -schedule_work (&sock->tcp_tx_work); +queue_work (ovpn_wq, &sock->tcp_tx_work); sock->peer->tcp.sk_cb.sk_write_space (sk); }rcu_read_unlock ();
Signature · 7 lines
2.55.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v5 3/6 ovpn: initialize TCP state before registering UAPI
In reply to Ralf Liciovpn_tcp_init initializes the TCP proto and proto_ops templates used when a TCP socket is attached to an ovpn peer but ovpn_init registers the rtnl link ops and generic netlink family before initializing those templates. Once either interface is visible, userspace can create an ovpn device and configure a TCP socket while the TCP templates are still zero-initialized.
Initialize the TCP templates before publishing the rtnl and netlink interfaces, so externally reachable setup paths can only observe initialized TCP state.
---
New patch added in v5.
drivers/net/ovpn/main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/ovpn/main.c +7 -3
@@ -233,8 +233,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; +/* initialize the TCP protos before the module is exposed through rtnl +* or netlink +*/ +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;
@@ -246,8 +252,6 @@ static int __init ovpn_init(void) goto unreg_rtnl; } -ovpn_tcp_init (); -return 0 ; unreg_rtnl:
Signature · 7 lines
2.55.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v5 2/6 ovpn: use nla_put_u32 for the key ID attribute in ovpn_nl_key_swap_notify
In reply to Ralf LiciCurrently, in ovpn_nl_key_swap_notify, the OVPN_A_KEYCONF_KEY_ID attribute is packed as a 16-bit value despite being defined as a 32-bit u32 attribute in the YAML policy.
Fix this inconsistency by using nla_put_u32.
---
Changes since v4 of this series https://lore.kernel.org/openvpn-devel/981d2ea51cca45138210aa52c6e5a0e55c0da7a0.1783099626.git.ralf@mandelbit.com/
- Add this previously posted standalone fix to the series so the whole
set can be picked in order.
- No changes since v1 of the original single patch
https://lore.kernel.org/openvpn-devel/8577555cc95646d0bd58a5b78e59c7e6a9b1a0c6.1783058844.git.ralf@mandelbit.com/
drivers/net/ovpn/netlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ovpn/netlink.c +1 -1
@@ -1335,7 +1335,7 @@ int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id)if (nla_put_u32 (msg,OVPN_A_KEYCONF_PEER_ID , peer->id)) goto err_cancel_msg; -if (nla_put_u16 (msg,OVPN_A_KEYCONF_KEY_ID , key_id)) +if (nla_put_u32 (msg,OVPN_A_KEYCONF_KEY_ID , key_id)) goto err_cancel_msg;nla_nest_end (msg, k_attr);
Signature · 7 lines
2.55.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerPATCH v5 6/6 ovpn: defer key slot crypto freeing to workqueue
In reply to Ralf LiciKey slots are released through a kref and the existing release path frees the AEAD transforms from an RCU callback. That is not safe for all crypto
implementations: crypto_free_aead can sleep, for example when an async or hardware implementation has teardown work to complete.
The AEAD transforms are only used while a key-slot reference is held, so the final kref put is enough to prove that no transform user remains. Queue the final crypto teardown on the ovpn workqueue, where sleeping is allowed, and keep the slot memory itself RCU-freed after the transforms have been released.
ovpn_crypto_config_get was the remaining lockless reader of transform state through ovpn_aead_crypto_alg. Make it take a key-slot reference before reading that state, so the transform lifetime is consistently tied to key-slot references. The previous patch drains ovpn_wq during module exit, so queued key-slot teardown work cannot outlive module text.
---
No changes since v4 https://lore.kernel.org/openvpn-devel/b53ae1a9714bae7081c71bdab7b30623ce3cb77b.1783099626.git.ralf@mandelbit.com/
Changes since v3 https://lore.kernel.org/openvpn-devel/ac2842c8e759849c51447126343376dcc793c7ae.1783080055.git.ralf@mandelbit.com/
- Reuse the module-owned ovpn workqueue instead of allocating a
crypto-specific workqueue for key-slot teardown.
Changes since v2 https://lore.kernel.org/openvpn-devel/b5dfebec718f230783ff47aa354c3b3fa1aa2ed7.1783068961.git.ralf@mandelbit.com/
- Reword the message to note the dependency on patch 1.
Changes since v1 https://lore.kernel.org/openvpn-devel/350f6b48c363dde5a8d0bdf7a1b9fd2abb8b1034.1783057762.git.ralf@mandelbit.com/
- Fix a potential AEAD transform UAF in ovpn_crypto_config_get by
holding a slot kref while reading the cipher algorithm.
drivers/net/ovpn/crypto.c | 15 ++++----------- drivers/net/ovpn/crypto.h | 3 +++ drivers/net/ovpn/crypto_aead.c | 20 ++++++++++++++------ drivers/net/ovpn/crypto_aead.h | 1 - 4 files changed, 21 insertions(+), 18 deletions(-)
drivers/net/ovpn/crypto.c +4 -11
@@ -18,20 +18,12 @@#include "crypto_aead.h" #include "crypto.h" -static void ovpn_ks_destroy_rcu (struct rcu_head *head) -{ -struct ovpn_crypto_key_slot *ks; - - ks = container_of(head, struct ovpn_crypto_key_slot, rcu); -ovpn_aead_crypto_key_slot_destroy (ks); -} -void ovpn_crypto_key_slot_release (struct kref *kref) {struct ovpn_crypto_key_slot *ks; ks = container_of(kref, struct ovpn_crypto_key_slot, refcount); -call_rcu (&ks->rcu, ovpn_ks_destroy_rcu); +queue_work (ovpn_wq, &ks->free_work); }/* can only be invoked when all peer references have been dropped (i.e. RCU
@@ -201,14 +193,15 @@ int ovpn_crypto_config_get(struct ovpn_crypto_state *cs,rcu_read_lock (); ks =rcu_dereference (cs->slots[idx]); -if (!ks) { +if (!ks || !ovpn_crypto_key_slot_hold (ks)) {rcu_read_unlock ();return -ENOENT ; } +rcu_read_unlock (); keyconf->cipher_alg =ovpn_aead_crypto_alg (ks); keyconf->key_id = ks->key_id; -rcu_read_unlock (); +ovpn_crypto_key_slot_put (ks);return 0 ; }
drivers/net/ovpn/crypto.h +3 -0
@@ -10,6 +10,8 @@#ifndef _NET_OVPN_OVPNCRYPTO_H_#define _NET_OVPN_OVPNCRYPTO_H_ +#include <linux/workqueue_types.h> +#include "pktid.h" #include "proto.h"
@@ -45,6 +47,7 @@ struct ovpn_crypto_key_slot {struct ovpn_pktid_recv pid_recv____cacheline_aligned_in_smp ;struct ovpn_pktid_xmit pid_xmit____cacheline_aligned_in_smp ; +struct work_struct free_work;struct kref refcount;struct rcu_head rcu; };
drivers/net/ovpn/crypto_aead.c +14 -6
@@ -9,6 +9,7 @@#include <crypto/aead.h> #include <linux/skbuff.h> +#include <linux/workqueue.h> #include <net/ip.h> #include <net/ipv6.h> #include <net/udp.h>
@@ -380,14 +381,19 @@ static struct crypto_aead *ovpn_aead_init(const char *title,return ERR_PTR (ret); } -void ovpn_aead_crypto_key_slot_destroy (struct ovpn_crypto_key_slot *ks) +static void ovpn_aead_crypto_key_slot_free (struct ovpn_crypto_key_slot *ks) { -if (!ks) -return ; -crypto_free_aead (ks->encrypt);crypto_free_aead (ks->decrypt); -kfree (ks); +} + +static void ovpn_aead_crypto_key_slot_free_work (struct work_struct *work) +{ +struct ovpn_crypto_key_slot *ks; + + ks = container_of(work, struct ovpn_crypto_key_slot, free_work); +ovpn_aead_crypto_key_slot_free (ks); +kfree_rcu (ks, rcu); } struct ovpn_crypto_key_slot *
@@ -420,6 +426,7 @@ ovpn_aead_crypto_key_slot_new(const struct ovpn_key_config *kc) ks->encrypt =NULL ; ks->decrypt =NULL ; +INIT_WORK (&ks->free_work, ovpn_aead_crypto_key_slot_free_work);kref_init (&ks->refcount); ks->key_id = kc->key_id;
@@ -453,7 +460,8 @@ ovpn_aead_crypto_key_slot_new(const struct ovpn_key_config *kc)return ks; destroy_ks: -ovpn_aead_crypto_key_slot_destroy (ks); +ovpn_aead_crypto_key_slot_free (ks); +kfree (ks);return ERR_PTR (ret); }
drivers/net/ovpn/crypto_aead.h +0 -1
@@ -22,7 +22,6 @@ int ovpn_aead_decrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot *ks,struct ovpn_crypto_key_slot *ovpn_aead_crypto_key_slot_new (const struct ovpn_key_config *kc); -void ovpn_aead_crypto_key_slot_destroy (struct ovpn_crypto_key_slot *ks);enum ovpn_cipher_alg ovpn_aead_crypto_alg (struct ovpn_crypto_key_slot *ks);
Signature · 7 lines
2.55.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Re: [Openvpn-devel] [PATCH ovpn net v5 2/6] ovpn: use nla_put_u32 for the key ID attribute in ovpn_nl_key_swap_notify
In reply to Ralf LiciQuoted 5 lines · Ralf Lici
Currently, in
ovpn_nl_key_swap_notify, theOVPN_A_KEYCONF_KEY_IDattribute is packed as a 16-bit value despite being defined as a 32-bit u32 attribute in the YAML policy.Fix this inconsistency by using
nla_put_u32.
Signature · 7 lines
Sabrina _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Sabrina Dubroca
reviewerRe: [Openvpn-devel] [PATCH ovpn net v5 1/6] ovpn: fix NULL dereference when killing missing key
In reply to Ralf LiciHi Ralf,
Sorry, I'm slowly catching up with the recent activity here.
Quoted 38 lines · Ralf Lici
ovpn_crypto_kill_keyassumes both crypto slots are populated and dereferences each slot before checking it. That is not guaranteed: a peer can have only one installed key, and the kill path may be asked to remove a key that is not present.Read each slot once while holding the crypto state lock, check for NULL before looking at
key_id, and only replace the slot that actually matches.Fixes 89d3c0e4612a ("ovpn: kill key and notify userspace in case of IV exhaustion")Signed-off-by Ralf Lici <[email protected]>---
Changes since v4 of this series https://lore.kernel.org/openvpn-devel/981d2ea51cca45138210aa52c6e5a0e55c0da7a0.1783099626.git.ralf@mandelbit.com/
- Add this previously posted standalone fix to the series so the whole
set can be picked in order.
- No changes since v2 of the original single patch
https://lore.kernel.org/openvpn-devel/19318904cf077d067cd4ec628a22bab03ed7dd29.1782993857.git.ralf@mandelbit.com/Changes since v1 of the original single patch https://lore.kernel.org/openvpn-devel/9fc33e6f9fae10b9e372a3e06934d697edf5b024.1782829171.git.ralf@mandelbit.com/
- Remove unnecessary braces around single-statement if/else branches.drivers/net/ovpn/crypto.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)drivers/net/ovpn/crypto.c +3
-1@@ -58,15 +58,19 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs)bool ovpn_crypto_kill_key (struct ovpn_crypto_state *cs,u8 key_id) {struct ovpn_crypto_key_slot *ks =NULL ; +struct ovpn_crypto_key_slot *tmp;spin_lock_bh (&cs->lock); -if (rcu_access_pointer (cs->slots[0 ])->key_id == key_id) { + tmp =rcu_access_pointer (cs->slots[0 ]); +if (tmp && tmp->key_id == key_id)
drivers/net/ovpn/crypto.c void ovpn_crypto_state_release(struct ovpn_crypto_state *cs) · line 69
Quoted 10 lines
drivers/net/ovpn/crypto.c +4
-2ks =rcu_replace_pointer (cs->slots[0 ],NULL ,lockdep_is_held (&cs->lock)); - }else if (rcu_access_pointer (cs->slots[1 ])->key_id == key_id) { + else + tmp =rcu_access_pointer (cs->slots[1 ]); + +if (!ks && tmp && tmp->key_id == key_id) ks =rcu_replace_pointer (cs->slots[1 ],NULL ,lockdep_is_held (&cs->lock)); - }
I find the "!ks && tmp" logic really confusing. Maybe something like this (untested) would be more readable?
int slot;
spin_lock_bh(&cs->lock);
slot = 0;
tmp = rcu_access_pointer(cs->slots[slot]);
if (!tmp || tmp->key_id != key_id) {
slot = 1;
tmp = rcu_access_pointer(cs->slots[slot]);
}
if (tmp && tmp->key_id == key_id)
ks = rcu_replace_pointer(cs->slots[slot], NULL,
lockdep_is_held(&cs->lock));
spin_unlock_bh(&cs->lock);[...]
Signature · 7 lines
Sabrina _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Sabrina Dubroca
reviewerRe: [Openvpn-devel] [PATCH ovpn net v5 3/6] ovpn: initialize TCP state before registering UAPI
In reply to Ralf LiciThis fix looks good, just a few small comments:
Quoted 4 lines · Ralf Lici
ovpn_tcp_initinitializes the TCP proto andproto_opstemplates used when a TCP socket is attached to an ovpn peer butovpn_initregisters the rtnl link ops and generic netlink family before initializing those templates. Once either interface is visible, userspace can create an
If only the rtnl part is registered, we can create a netdevice but not attach a socket to it, so the ovpn_tcp_* ops should not be reachable.
Quoted 20 lines
ovpn device and configure a TCP socket while the TCP templates are still zero-initialized.
Initialize the TCP templates before publishing the rtnl and netlink interfaces, so externally reachable setup paths can only observe initialized TCP state.
Fixes 11851cbd60ea ("ovpn: implement TCP transport")Signed-off-by Ralf Lici <[email protected]>---
New patch added in v5.drivers/net/ovpn/main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)drivers/net/ovpn/main.c +0
-0
drivers/net/ovpn/main.c static struct rtnl_link_ops ovpn_link_ops = { · line 240
Quoted 9 lines
drivers/net/ovpn/main.c +4
-1@@ -233,8 +233,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; +/* initialize the TCP protos before the module is exposed through rtnl +* or netlink +*/
I don't think that comment adds much. The commit message explains why this needs to be moved (and why this had to be moved, if someone touches this later)
Quoted 3 lines
drivers/net/ovpn/main.c +3
-0+ovpn_tcp_init (); + + err =rtnl_link_register (&ovpn_link_ops);
Signature · 7 lines
Sabrina _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Sabrina Dubroca
reviewerRe: [Openvpn-devel] [PATCH ovpn net v5 6/6] ovpn: defer key slot crypto freeing to workqueue
In reply to Ralf LiciQuoted 5 lines · Ralf Lici
Key slots are released through a kref and the existing release path frees the AEAD transforms from an RCU callback. That is not safe for all crypto
implementations:crypto_free_aeadcan sleep, for example when an async or hardware implementation has teardown work to complete.
Quoted 7 lines
The AEAD transforms are only used while a key-slot reference is held, so the final kref put is enough to prove that no transform user remains. Queue the final crypto teardown on the ovpn workqueue, where sleeping is allowed, and keep the slot memory itself RCU-freed after the transforms have been released.
ovpn_crypto_config_getwas the remaining lockless reader of transform
nit: encrypt/decrypt is also lockless, the difference is only the refcount, no?
Quoted 3 lines
state through
ovpn_aead_crypto_alg. Make it take a key-slot reference before reading that state, so the transform lifetime is consistently tied to key-slot references. The previous patch drainsovpn_wqduring module
Using rcu_work/queue_rcu_work (see commit 6624bba469a3 ("macsec: use rcu_work to defer RX SA crypto cleanup out of softirq")) would avoid some of the refactoring of the destroy helpers, and the (kind of weird and ugly IMO) refcount dance in ovpn_crypto_config_get (with a fairly big risk of "well it's dumb to take a refcount here, we could just do everything under RCU").
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 v5 1/6] ovpn: fix NULL dereference when killing missing key
In reply to Sabrina DubrocaQuoted 4 lines · Sabrina Dubroca
Hi Ralf,
Sorry, I'm slowly catching up with the recent activity here.
Hi Sabrina,
No worries, thanks for your time!
Quoted 68 lines
Quoted 38 lines · Ralf Lici
ovpn_crypto_kill_keyassumes both crypto slots are populated and dereferences each slot before checking it. That is not guaranteed: a peer can have only one installed key, and the kill path may be asked to remove a key that is not present.Read each slot once while holding the crypto state lock, check for NULL before looking at
key_id, and only replace the slot that actually matches.Fixes 89d3c0e4612a ("ovpn: kill key and notify userspace in case of IV exhaustion")Signed-off-by Ralf Lici <[email protected]>---
Changes since v4 of this series https://lore.kernel.org/openvpn-devel/981d2ea51cca45138210aa52c6e5a0e55c0da7a0.1783099626.git.ralf@mandelbit.com/
- Add this previously posted standalone fix to the series so the whole
set can be picked in order.
- No changes since v2 of the original single patch
https://lore.kernel.org/openvpn-devel/19318904cf077d067cd4ec628a22bab03ed7dd29.1782993857.git.ralf@mandelbit.com/Changes since v1 of the original single patch https://lore.kernel.org/openvpn-devel/9fc33e6f9fae10b9e372a3e06934d697edf5b024.1782829171.git.ralf@mandelbit.com/
- Remove unnecessary braces around single-statement if/else branches.drivers/net/ovpn/crypto.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)drivers/net/ovpn/crypto.c +3
-1@@ -58,15 +58,19 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs)bool ovpn_crypto_kill_key (struct ovpn_crypto_state *cs,u8 key_id) {struct ovpn_crypto_key_slot *ks =NULL ; +struct ovpn_crypto_key_slot *tmp;spin_lock_bh (&cs->lock); -if (rcu_access_pointer (cs->slots[0 ])->key_id == key_id) { + tmp =rcu_access_pointer (cs->slots[0 ]); +if (tmp && tmp->key_id == key_id)drivers/net/ovpn/crypto.c void ovpn_crypto_state_release(struct ovpn_crypto_state *cs) · line 69
Quoted 10 lines
drivers/net/ovpn/crypto.c +4
-2ks =rcu_replace_pointer (cs->slots[0 ],NULL ,lockdep_is_held (&cs->lock)); - }else if (rcu_access_pointer (cs->slots[1 ])->key_id == key_id) { + else + tmp =rcu_access_pointer (cs->slots[1 ]); + +if (!ks && tmp && tmp->key_id == key_id) ks =rcu_replace_pointer (cs->slots[1 ],NULL ,lockdep_is_held (&cs->lock)); - }I find the "!ks && tmp" logic really confusing. Maybe something like this (untested) would be more readable?
int slot; spin_lock_bh(&cs->lock); slot = 0; tmp = rcu_access_pointer(cs->slots[slot]); if (!tmp || tmp->key_id != key_id) { slot = 1; tmp = rcu_access_pointer(cs->slots[slot]); } if (tmp && tmp->key_id == key_id) ks = rcu_replace_pointer(cs->slots[slot], NULL, lockdep_is_held(&cs->lock)); spin_unlock_bh(&cs->lock);
Quoted 2 lines
[...]
Agreed, this is cleaner. I'll adopt this shape for the next version.
Signature · 8 lines
Ralf Lici Mandelbit Srl _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerRe: [Openvpn-devel] [PATCH ovpn net v5 3/6] ovpn: initialize TCP state before registering UAPI
In reply to Sabrina DubrocaQuoted 9 lines · Sabrina Dubroca
Quoted 4 lines · Ralf Lici
ovpn_tcp_initinitializes the TCP proto andproto_opstemplates used when a TCP socket is attached to an ovpn peer butovpn_initregisters the rtnl link ops and generic netlink family before initializing those templates. Once either interface is visible, userspace can create anIf only the rtnl part is registered, we can create a netdevice but not attach a socket to it, so the ovpn_tcp_* ops should not be reachable.
Right, thanks for catching the imprecise wording. I'll reword the commit message to make that clear.
Quoted 26 lines
Quoted 26 lines
ovpn device and configure a TCP socket while the TCP templates are still zero-initialized.
Initialize the TCP templates before publishing the rtnl and netlink interfaces, so externally reachable setup paths can only observe initialized TCP state.
Fixes 11851cbd60ea ("ovpn: implement TCP transport")Signed-off-by Ralf Lici <[email protected]>---
New patch added in v5.drivers/net/ovpn/main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)drivers/net/ovpn/main.c +1
-1@@ -233,8 +233,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;
Quoted 8 lines
drivers/net/ovpn/main.c static struct rtnl_link_ops ovpn_link_ops = { · line 240
Quoted 3 lines
drivers/net/ovpn/main.c +3
-0+/* initialize the TCP protos before the module is exposed through rtnl +* or netlink +*/ I don't think that comment adds much. The commit message explains why this needs to be moved (and why this had to be moved, if someone touches this later)
Agreed, I'll drop the comment. I added it as a warning for future changes, but the commit message and history should be enough context.
Signature · 8 lines
Ralf Lici Mandelbit Srl _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Ralf Lici
reviewerRe: [Openvpn-devel] [PATCH ovpn net v5 6/6] ovpn: defer key slot crypto freeing to workqueue
In reply to Sabrina DubrocaQuoted 12 lines · Sabrina Dubroca
Quoted 11 lines · Ralf Lici
Key slots are released through a kref and the existing release path frees the AEAD transforms from an RCU callback. That is not safe for all crypto
implementations:crypto_free_aeadcan sleep, for example when an async or hardware implementation has teardown work to complete.The AEAD transforms are only used while a key-slot reference is held, so the final kref put is enough to prove that no transform user remains. Queue the final crypto teardown on the ovpn workqueue, where sleeping is allowed, and keep the slot memory itself RCU-freed after the transforms have been released.
Quoted 5 lines
Quoted 1 line
ovpn_crypto_config_getwas the remaining lockless reader of transformnit: encrypt/decrypt is also lockless, the difference is only the refcount, no?
Correct, I meant the remaining reader of transform state without holding a key-slot reference. I'll reword that.
Quoted 11 lines
Quoted 3 lines
state through
ovpn_aead_crypto_alg. Make it take a key-slot reference before reading that state, so the transform lifetime is consistently tied to key-slot references. The previous patch drainsovpn_wqduring moduleUsing rcu_work/queue_rcu_work (see commit
6624bba469a3("macsec: usercu_workto defer RX SA crypto cleanup out of softirq")) would avoid some of the refactoring of the destroy helpers, and the (kind of weird and ugly IMO) refcount dance inovpn_crypto_config_get(with a fairly big risk of "well it's dumb to take a refcount here, we could just do everything under RCU").
One thing I am not fully sure about in this case is the module-exit ordering. If key-slot release uses queue_rcu_work, the work item is not queued immediately. The final put first schedules an RCU callback, and that callback later queues the actual work on ovpn_wq, right?
Does that mean ovpn_cleanup would need a different ordering from the current destroy_workqueue; rcu_barrier shape?
In particular, with this series ovpn_wq also runs ordinary ovpn work items, such as the TCP deferred-delete worker and keepalive work. Those can drop peer references, and the final peer put can release the crypto state and queue a key-slot rcu_work. So if we only move rcu_barrier before destroy_workqueue, maybe an already pending/running ovpn worker could still queue a new rcu_work after the barrier has returned.
Would the safe shape be something like:
drain_workqueue(ovpn_wq);
rcu_barrier();
destroy_workqueue(ovpn_wq);so that ordinary ovpn work is drained first, including chained work queued by already running ovpn workers, then pending rcu_work callbacks are forced to queue their actual work, and finally the workqueue drains that cleanup work before being destroyed?
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 v5 6/6] ovpn: defer key slot crypto freeing to workqueue
Quoted 34 lines · Ralf Lici
Quoted 12 lines · Sabrina Dubroca
Quoted 11 lines · Ralf Lici
Key slots are released through a kref and the existing release path frees the AEAD transforms from an RCU callback. That is not safe for all crypto
implementations:crypto_free_aeadcan sleep, for example when an async or hardware implementation has teardown work to complete.The AEAD transforms are only used while a key-slot reference is held, so the final kref put is enough to prove that no transform user remains. Queue the final crypto teardown on the ovpn workqueue, where sleeping is allowed, and keep the slot memory itself RCU-freed after the transforms have been released.
Quoted 5 lines
Quoted 1 line
ovpn_crypto_config_getwas the remaining lockless reader of transformnit: encrypt/decrypt is also lockless, the difference is only the refcount, no?
Correct, I meant the remaining reader of transform state without holding a key-slot reference. I'll reword that.
Quoted 11 lines
Quoted 3 lines
state through
ovpn_aead_crypto_alg. Make it take a key-slot reference before reading that state, so the transform lifetime is consistently tied to key-slot references. The previous patch drainsovpn_wqduring moduleUsing rcu_work/queue_rcu_work (see commit
6624bba469a3("macsec: usercu_workto defer RX SA crypto cleanup out of softirq")) would avoid some of the refactoring of the destroy helpers, and the (kind of weird and ugly IMO) refcount dance inovpn_crypto_config_get(with a fairly big risk of "well it's dumb to take a refcount here, we could just do everything under RCU").
Quoted 4 lines
One thing I am not fully sure about in this case is the module-exit ordering. If key-slot release uses
queue_rcu_work, the work item is not queued immediately. The final put first schedules an RCU callback, and that callback later queues the actual work onovpn_wq, right?
AFAIU, yes.
Quoted 9 lines
Does that mean
ovpn_cleanupwould need a different ordering from the currentdestroy_workqueue;rcu_barriershape?In particular, with this series
ovpn_wqalso runs ordinary ovpn work items, such as the TCP deferred-delete worker and keepalive work. Those can drop peer references, and the final peer put can release the crypto state and queue a key-slotrcu_work. So if we only movercu_barrierbeforedestroy_workqueue, maybe an already pending/running ovpn worker could still queue a newrcu_workafter the barrier has returned.
Right, that's... a bit hairy.
Quoted 10 lines
Would the safe shape be something like:
drain_workqueue(ovpn_wq); rcu_barrier(); destroy_workqueue(ovpn_wq);so that ordinary ovpn work is drained first, including chained work queued by already running ovpn workers, then pending
rcu_workcallbacks are forced to queue their actual work, and finally the workqueue drains that cleanup work before being destroyed?
I'm not sure. The comment for drain_workqueue() says:
* Wait until the workqueue becomes empty. While draining is in progress,
* only chain queueing is allowed. IOW, only currently pending or running
* work items on @wq can queue further work items on it. [...]
So any attempt to queue from other contexts will fail (and actually trigger a WARN it seems, __queue_work -> __WQ_DRAINING), for example from TCP. Even without the warning, for things like keepalive or tcp_tx, it shouldn't matter, since we're already cleaning up. Maybe also defer_del_work, since the peer should already be going away with its netdevice. But it we can't queue key slot freeing, it's going to leak.
Would flush_workqueue() be enough? By the time rtnl_link_unregister() returns, ndo_uninit() has been called for all ovpn devices (__rtnl_kill_links() -> unregister_netdevice_many_notify() -> ->ndo_uninit()) so ovpn_peers_free() has removed all the peers from the hashtable/p2p slot.
ovpn_peer_keepalive_work_{p2p,mp} should probably copy the "prevent double remove" check from ovpn_peer_remove() as "don't do anything for already deleted peers". tcp_tx_work can queue defer_del_work but the same check could be used. With this, after rtnl_link_unregister() returns, there should be no work capable of requeueing work on ovpn_wq, so no more keepalive_work or defer_del_work running once flush_workqueue() returns, or maybe only "empty" ones that will just return immediately without doing anything.
The final destroy_workqueue() will take care of any keyslot cleanup that has been queued after flush_workqueue() started (whether it was while flush_workqueue() was running, or during the rcu_barrier()).
Am I making any sense? (it's quite possible I need another 3 cups of coffee for this type of reasoning :))
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 v5 6/6] ovpn: defer key slot crypto freeing to workqueue
Quoted 71 lines · Sabrina Dubroca
Quoted 34 lines · Ralf Lici
Quoted 12 lines · Sabrina Dubroca
Quoted 11 lines · Ralf Lici
Key slots are released through a kref and the existing release path frees the AEAD transforms from an RCU callback. That is not safe for all crypto
implementations:crypto_free_aeadcan sleep, for example when an async or hardware implementation has teardown work to complete.The AEAD transforms are only used while a key-slot reference is held, so the final kref put is enough to prove that no transform user remains. Queue the final crypto teardown on the ovpn workqueue, where sleeping is allowed, and keep the slot memory itself RCU-freed after the transforms have been released.
Quoted 5 lines
Quoted 1 line
ovpn_crypto_config_getwas the remaining lockless reader of transformnit: encrypt/decrypt is also lockless, the difference is only the refcount, no?
Correct, I meant the remaining reader of transform state without holding a key-slot reference. I'll reword that.
Quoted 11 lines
Quoted 3 lines
state through
ovpn_aead_crypto_alg. Make it take a key-slot reference before reading that state, so the transform lifetime is consistently tied to key-slot references. The previous patch drainsovpn_wqduring moduleUsing rcu_work/queue_rcu_work (see commit
6624bba469a3("macsec: usercu_workto defer RX SA crypto cleanup out of softirq")) would avoid some of the refactoring of the destroy helpers, and the (kind of weird and ugly IMO) refcount dance inovpn_crypto_config_get(with a fairly big risk of "well it's dumb to take a refcount here, we could just do everything under RCU").Quoted 4 lines
One thing I am not fully sure about in this case is the module-exit ordering. If key-slot release uses
queue_rcu_work, the work item is not queued immediately. The final put first schedules an RCU callback, and that callback later queues the actual work onovpn_wq, right?AFAIU, yes.
Quoted 9 lines
Does that mean
ovpn_cleanupwould need a different ordering from the currentdestroy_workqueue;rcu_barriershape?In particular, with this series
ovpn_wqalso runs ordinary ovpn work items, such as the TCP deferred-delete worker and keepalive work. Those can drop peer references, and the final peer put can release the crypto state and queue a key-slotrcu_work. So if we only movercu_barrierbeforedestroy_workqueue, maybe an already pending/running ovpn worker could still queue a newrcu_workafter the barrier has returned.Right, that's... a bit hairy.
Quoted 10 lines
Would the safe shape be something like:
drain_workqueue(ovpn_wq); rcu_barrier(); destroy_workqueue(ovpn_wq);so that ordinary ovpn work is drained first, including chained work queued by already running ovpn workers, then pending
rcu_workcallbacks are forced to queue their actual work, and finally the workqueue drains that cleanup work before being destroyed?I'm not sure. The comment for
drain_workqueue()says:* Wait until the workqueue becomes empty. While draining is in progress,
* only chain queueing is allowed. IOW, only currently pending or running
* work items on @wq can queue further work items on it. [...]
Quoted 8 lines
So any attempt to queue from other contexts will fail (and actually trigger a WARN it seems,
__queue_work->__WQ_DRAINING), for example from TCP. Even without the warning, for things like keepalive ortcp_tx, it shouldn't matter, since we're already cleaning up. Maybe alsodefer_del_work, since the peer should already be going away with its netdevice. But it we can't queue key slot freeing, it's going to leak.
Right, I wrongly assumed queue_rcu_work was "chain queueing" but it queues from an RCU callback, not from a worker currently running on ovpn_wq.
Quoted 16 lines
Would
flush_workqueue()be enough? By the timertnl_link_unregister()returns,ndo_uninit()has been called for all ovpn devices (__rtnl_kill_links()->unregister_netdevice_many_notify()-> ->ndo_uninit()) soovpn_peers_free()has removed all the peers from the hashtable/p2p slot.ovpn_peer_keepalive_work_{p2p,mp} should probably copy the "prevent double remove" check from
ovpn_peer_remove()as "don't do anything for already deleted peers".tcp_tx_workcan queuedefer_del_workbut the same check could be used. With this, afterrtnl_link_unregister()returns, there should be no work capable of requeueing work onovpn_wq, so no morekeepalive_workordefer_del_workrunning onceflush_workqueue()returns, or maybe only "empty" ones that will just return immediately without doing anything.
Quoted 8 lines
The final
destroy_workqueue()will take care of any keyslot cleanup that has been queued afterflush_workqueue()started (whether it was whileflush_workqueue()was running, or during thercu_barrier()).Am I making any sense? (it's quite possible I need another 3 cups of coffee for this type of reasoning :))
Yes, definitely it makes sense.
I went through the ovpn_wq producers:
- ovpn->keepalive_work: disabled from ovpn_net_uninit before peers are freed;
- peer->keepalive_work: queued by the scanner, it holds a peer reference and can indirectly hit the TCP deferred-delete path through TX failure;
- peer->tcp.defer_del_work: queued from TCP RX/TX error paths, each successfully queued instance owns a peer reference and peer release drops crypto state before dropping the netdev reference;
- sock->tcp_tx_work: queued from TCP write_space, socket teardown cancels it;
- key-slot free work/rcu_work: queued from final key-slot put.
So after rtnl_link_unregister has returned, I don't see a peer-owned producer that can create new key-slot rcu_work without first holding a peer reference that would have kept unregister_netdevice waiting. Then flush_workqueue waits for the remaining worker tails that may still be executing module text. After that, rcu_barrier forces pending rcu_work callbacks to queue their actual work, and destroy_workqueue drains that cleanup work before module text goes away.
So the ordering I'd use with queue_rcu_work is:
ovpn_nl_unregister();
rtnl_link_unregister(&ovpn_link_ops);
flush_workqueue(ovpn_wq);
rcu_barrier();
destroy_workqueue(ovpn_wq);Makes sense?
Avoiding work on deleted peers is nice cleanup, but I don't think the unload safety depends on it because the stronger invariant is that any peer-owned work that can queue more work either already owns a peer ref or takes one before queueing.
WDYT?
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 v5 6/6] ovpn: defer key slot crypto freeing to workqueue
Quoted 20 lines · Ralf Lici
Quoted 3 lines · Sabrina Dubroca
Am I making any sense? (it's quite possible I need another 3 cups of coffee for this type of reasoning :))
Yes, definitely it makes sense.
I went through the
ovpn_wqproducers:
-ovpn->keepalive_work: disabled fromovpn_net_uninitbefore peers are freed;
-peer->keepalive_work: queued by the scanner, it holds a peer reference and can indirectly hit the TCP deferred-delete path through TX failure;
-peer->tcp.defer_del_work: queued from TCP RX/TX error paths, each successfully queued instance owns a peer reference and peer release drops crypto state before dropping the netdev reference;
-sock->tcp_tx_work: queued from TCPwrite_space, socket teardown cancels it;
- key-slot free work/rcu_work: queued from final key-slot put.
Quoted 3 lines
So after
rtnl_link_unregisterhas returned, I don't see a peer-owned producer that can create new key-slotrcu_workwithout first holding a peer reference that would have keptunregister_netdevicewaiting. Then
All those objects referencing each other make it really hard to reason about correctness :/
Quoted 13 lines
flush_workqueuewaits for the remaining worker tails that may still be executing module text. After that,rcu_barrierforces pendingrcu_workcallbacks to queue their actual work, anddestroy_workqueuedrains that cleanup work before module text goes away.So the ordering I'd use with
queue_rcu_workis:ovpn_nl_unregister(); rtnl_link_unregister(&ovpn_link_ops); flush_workqueue(ovpn_wq); rcu_barrier(); destroy_workqueue(ovpn_wq);
Quoted 8 lines
Makes sense?
Avoiding work on deleted peers is nice cleanup, but I don't think the unload safety depends on it because the stronger invariant is that any peer-owned work that can queue more work either already owns a peer ref or takes one before queueing.
WDYT?
All this looks correct. Thanks.
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 v5 6/6] ovpn: defer key slot crypto freeing to workqueue
Quoted 21 lines · Sabrina Dubroca
Quoted 20 lines · Ralf Lici
Quoted 3 lines · Sabrina Dubroca
Am I making any sense? (it's quite possible I need another 3 cups of coffee for this type of reasoning :))
Yes, definitely it makes sense.
I went through the
ovpn_wqproducers:
-ovpn->keepalive_work: disabled fromovpn_net_uninitbefore peers are freed;
-peer->keepalive_work: queued by the scanner, it holds a peer reference and can indirectly hit the TCP deferred-delete path through TX failure;
-peer->tcp.defer_del_work: queued from TCP RX/TX error paths, each successfully queued instance owns a peer reference and peer release drops crypto state before dropping the netdev reference;
-sock->tcp_tx_work: queued from TCPwrite_space, socket teardown cancels it;
- key-slot free work/rcu_work: queued from final key-slot put.
Quoted 7 lines
Quoted 3 lines
So after
rtnl_link_unregisterhas returned, I don't see a peer-owned producer that can create new key-slotrcu_workwithout first holding a peer reference that would have keptunregister_netdevicewaiting. ThenAll those objects referencing each other make it really hard to reason about correctness :/
Yeah, same. I wasn't expecting it to be such a maze either at first.
Quoted 15 lines
Quoted 15 lines
flush_workqueuewaits for the remaining worker tails that may still be executing module text. After that,rcu_barrierforces pendingrcu_workcallbacks to queue their actual work, anddestroy_workqueuedrains that cleanup work before module text goes away.So the ordering I'd use with
queue_rcu_workis:ovpn_nl_unregister(); rtnl_link_unregister(&ovpn_link_ops); flush_workqueue(ovpn_wq); rcu_barrier(); destroy_workqueue(ovpn_wq);Makes sense?
Quoted 9 lines
Quoted 6 lines
Avoiding work on deleted peers is nice cleanup, but I don't think the unload safety depends on it because the stronger invariant is that any peer-owned work that can queue more work either already owns a peer ref or takes one before queueing.
WDYT?
All this looks correct. Thanks.
Perfect, then I'll respin the series with the fixes we agreed on.
Thanks!
Signature · 8 lines
Ralf Lici Mandelbit Srl _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel