From: Antonio Quartulli <antonio@openvpn.net>
To: netdev@vger.kernel.org, Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>,
Donald Hunter <donald.hunter@gmail.com>,
Antonio Quartulli <antonio@openvpn.net>,
Shuah Khan <shuah@kernel.org>,
sd@queasysnail.net, ryazanov.s.a@gmail.com,
Andrew Lunn <andrew+netdev@lunn.ch>
Cc: Simon Horman <horms@kernel.org>,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
Xiao Liang <shaw.leon@gmail.com>
Subject: [PATCH net-next v24 20/23] ovpn: kill key and notify userspace in case of IV exhaustion
Date: Tue, 18 Mar 2025 02:40:55 +0100 [thread overview]
Message-ID: <20250318-b4-ovpn-v24-20-3ec4ab5c4a77@openvpn.net> (raw)
In-Reply-To: <20250318-b4-ovpn-v24-0-3ec4ab5c4a77@openvpn.net>
IV wrap-around is cryptographically dangerous for a number of ciphers,
therefore kill the key and inform userspace (via netlink) should the
IV space go exhausted.
Userspace has two ways of deciding when the key has to be renewed before
exhausting the IV space:
1) time based approach:
after X seconds/minutes userspace generates a new key and sends it
to the kernel. This is based on guestimate and normally default
timer value works well.
2) packet count based approach:
after X packets/bytes userspace generates a new key and sends it to
the kernel. Userspace keeps track of the amount of traffic by
periodically polling GET_PEER and fetching the VPN/LINK stats.
Signed-off-by: Antonio Quartulli <antonio@openvpn.net>
---
drivers/net/ovpn/crypto.c | 19 ++++++++++++++
drivers/net/ovpn/crypto.h | 2 ++
drivers/net/ovpn/io.c | 13 ++++++++++
drivers/net/ovpn/netlink.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++
drivers/net/ovpn/netlink.h | 2 ++
5 files changed, 100 insertions(+)
diff --git a/drivers/net/ovpn/crypto.c b/drivers/net/ovpn/crypto.c
index 3c15d6d78f0f5ceeeb5ee39bfaaa7005d01a87fc..f6cb14c9ec4a8f12a99fe449f011bd0d633bf904 100644
--- a/drivers/net/ovpn/crypto.c
+++ b/drivers/net/ovpn/crypto.c
@@ -54,6 +54,25 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs)
}
}
+/* removes the key matching the specified id from the crypto context */
+void ovpn_crypto_kill_key(struct ovpn_crypto_state *cs, u8 key_id)
+{
+ struct ovpn_crypto_key_slot *ks = NULL;
+
+ spin_lock_bh(&cs->lock);
+ if (rcu_access_pointer(cs->slots[0])->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) {
+ ks = rcu_replace_pointer(cs->slots[1], NULL,
+ lockdep_is_held(&cs->lock));
+ }
+ spin_unlock_bh(&cs->lock);
+
+ if (ks)
+ ovpn_crypto_key_slot_put(ks);
+}
+
/* Reset the ovpn_crypto_state object in a way that is atomic
* to RCU readers.
*/
diff --git a/drivers/net/ovpn/crypto.h b/drivers/net/ovpn/crypto.h
index 487d24a7d26635c9ca0fd66c75717502f60e7a0c..4397253893ff2638b86b9d55bd21e11d0aeacf80 100644
--- a/drivers/net/ovpn/crypto.h
+++ b/drivers/net/ovpn/crypto.h
@@ -140,4 +140,6 @@ int ovpn_crypto_config_get(struct ovpn_crypto_state *cs,
enum ovpn_key_slot slot,
struct ovpn_key_config *keyconf);
+void ovpn_crypto_kill_key(struct ovpn_crypto_state *cs, u8 key_id);
+
#endif /* _NET_OVPN_OVPNCRYPTO_H_ */
diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c
index 49bb58868053e2d1542a8a5849c4bbc256083efc..c11ed75c62a2599bf68150be897693cc4806e4f6 100644
--- a/drivers/net/ovpn/io.c
+++ b/drivers/net/ovpn/io.c
@@ -255,6 +255,19 @@ void ovpn_encrypt_post(void *data, int ret)
if (likely(ovpn_skb_cb(skb)->req))
aead_request_free(ovpn_skb_cb(skb)->req);
+ if (unlikely(ret == -ERANGE)) {
+ /* we ran out of IVs and we must kill the key as it can't be
+ * use anymore
+ */
+ netdev_warn(peer->ovpn->dev,
+ "killing key %u for peer %u\n", ks->key_id,
+ peer->id);
+ ovpn_crypto_kill_key(&peer->crypto, ks->key_id);
+ /* let userspace know so that a new key must be negotiated */
+ ovpn_nl_key_swap_notify(peer, ks->key_id);
+ goto err;
+ }
+
if (unlikely(ret < 0))
goto err;
diff --git a/drivers/net/ovpn/netlink.c b/drivers/net/ovpn/netlink.c
index 21784b44a897a5590bd8dc02754ca66cef841520..93152734098948b58ec1ba8a2ed05f8bb40c91eb 100644
--- a/drivers/net/ovpn/netlink.c
+++ b/drivers/net/ovpn/netlink.c
@@ -1095,6 +1095,70 @@ int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info)
return 0;
}
+/**
+ * ovpn_nl_key_swap_notify - notify userspace peer's key must be renewed
+ * @peer: the peer whose key needs to be renewed
+ * @key_id: the ID of the key that needs to be renewed
+ *
+ * Return: 0 on success or a negative error code otherwise
+ */
+int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id)
+{
+ struct ovpn_socket *sock;
+ struct nlattr *k_attr;
+ struct sk_buff *msg;
+ int ret = -EMSGSIZE;
+ void *hdr;
+
+ netdev_info(peer->ovpn->dev, "peer with id %u must rekey - primary key unusable.\n",
+ peer->id);
+
+ msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
+ if (!msg)
+ return -ENOMEM;
+
+ hdr = genlmsg_put(msg, 0, 0, &ovpn_nl_family, 0, OVPN_CMD_KEY_SWAP_NTF);
+ if (!hdr) {
+ ret = -ENOBUFS;
+ goto err_free_msg;
+ }
+
+ if (nla_put_u32(msg, OVPN_A_IFINDEX, peer->ovpn->dev->ifindex))
+ goto err_cancel_msg;
+
+ k_attr = nla_nest_start(msg, OVPN_A_KEYCONF);
+ if (!k_attr)
+ goto err_cancel_msg;
+
+ 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))
+ goto err_cancel_msg;
+
+ nla_nest_end(msg, k_attr);
+ genlmsg_end(msg, hdr);
+
+ rcu_read_lock();
+ sock = rcu_dereference(peer->sock);
+ if (!sock) {
+ ret = -EINVAL;
+ goto err_unlock;
+ }
+ genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sock->sk),
+ msg, 0, OVPN_NLGRP_PEERS, GFP_ATOMIC);
+ rcu_read_unlock();
+
+ return 0;
+err_unlock:
+ rcu_read_unlock();
+err_cancel_msg:
+ genlmsg_cancel(msg, hdr);
+err_free_msg:
+ nlmsg_free(msg);
+ return ret;
+}
+
/**
* ovpn_nl_register - perform any needed registration in the NL subsustem
*
diff --git a/drivers/net/ovpn/netlink.h b/drivers/net/ovpn/netlink.h
index 0d6c34e17082cc7c52dd9c5d5ed1e964925b3f4b..5dc84c8e5e803014053faa0d892fc3a7259d40e5 100644
--- a/drivers/net/ovpn/netlink.h
+++ b/drivers/net/ovpn/netlink.h
@@ -12,4 +12,6 @@
int ovpn_nl_register(void);
void ovpn_nl_unregister(void);
+int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id);
+
#endif /* _NET_OVPN_NETLINK_H_ */
--
2.48.1
next prev parent reply other threads:[~2025-03-18 1:41 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-18 1:40 [PATCH net-next v24 00/23] Introducing OpenVPN Data Channel Offload Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 01/23] net: introduce OpenVPN Data Channel Offload (ovpn) Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 02/23] ovpn: add basic netlink support Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 03/23] ovpn: add basic interface creation/destruction/management routines Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 04/23] ovpn: keep carrier always on for MP interfaces Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 05/23] ovpn: introduce the ovpn_peer object Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 06/23] ovpn: introduce the ovpn_socket object Antonio Quartulli
2025-04-01 13:05 ` Sabrina Dubroca
2025-04-02 23:37 ` Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 07/23] ovpn: implement basic TX path (UDP) Antonio Quartulli
2025-04-01 13:49 ` Sabrina Dubroca
2025-04-02 12:01 ` Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 08/23] ovpn: implement basic RX " Antonio Quartulli
2025-04-01 9:47 ` Sabrina Dubroca
2025-04-02 12:04 ` Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 09/23] ovpn: implement packet processing Antonio Quartulli
2025-03-24 11:02 ` Sabrina Dubroca
2025-03-24 20:53 ` Antonio Quartulli
2025-03-25 9:40 ` Sabrina Dubroca
2025-03-25 2:07 ` Qingfang Deng
2025-03-26 9:41 ` Antonio Quartulli
2025-03-26 10:03 ` Qingfang Deng
2025-03-26 10:22 ` Antonio Quartulli
2025-03-26 12:43 ` Qingfang Deng
2025-03-26 13:54 ` Antonio Quartulli
2025-04-01 9:59 ` Sabrina Dubroca
2025-04-02 12:08 ` Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 10/23] ovpn: store tunnel and transport statistics Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 11/23] ovpn: implement TCP transport Antonio Quartulli
2025-04-01 10:02 ` Sabrina Dubroca
2025-04-02 12:09 ` Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 12/23] skb: implement skb_send_sock_locked_with_flags() Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 13/23] ovpn: add support for MSG_NOSIGNAL in tcp_sendmsg Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 14/23] ovpn: implement multi-peer support Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 15/23] ovpn: implement peer lookup logic Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 16/23] ovpn: implement keepalive mechanism Antonio Quartulli
2025-04-01 12:51 ` Sabrina Dubroca
2025-04-02 12:11 ` Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 17/23] ovpn: add support for updating local or remote UDP endpoint Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 18/23] ovpn: implement peer add/get/dump/delete via netlink Antonio Quartulli
2025-03-24 10:48 ` Sabrina Dubroca
2025-03-24 23:15 ` Antonio Quartulli
2025-03-25 10:56 ` Sabrina Dubroca
2025-03-26 0:41 ` Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 19/23] ovpn: implement key add/get/del/swap " Antonio Quartulli
2025-03-18 1:40 ` Antonio Quartulli [this message]
2025-03-18 1:40 ` [PATCH net-next v24 21/23] ovpn: notify userspace when a peer is deleted Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 22/23] ovpn: add basic ethtool support Antonio Quartulli
2025-03-18 1:40 ` [PATCH net-next v24 23/23] testing/selftests: add test tool and scripts for ovpn module Antonio Quartulli
2025-03-28 9:14 ` [PATCH net-next v24 00/23] Introducing OpenVPN Data Channel Offload Antonio Quartulli
2025-03-31 14:47 ` Sabrina Dubroca
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250318-b4-ovpn-v24-20-3ec4ab5c4a77@openvpn.net \
--to=antonio@openvpn.net \
--cc=andrew+netdev@lunn.ch \
--cc=donald.hunter@gmail.com \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=ryazanov.s.a@gmail.com \
--cc=sd@queasysnail.net \
--cc=shaw.leon@gmail.com \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).