| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/11/08 13:37 | flow | patch-triage |
4mArgs:null Results: {
"EnableConfigs": [
"TLS",
"INET"
],
"FocusSymbols": [
"tls_hkdf_expand_label",
"tls_keyupdate_rekey",
"tls_keyupdate_rx",
"tls_sw_key_update",
"tls_tx_key_update",
"tls_tx_maybe_update_key",
"do_tls_setsockopt_secret",
"do_tls_setsockopt_key_update",
"tls_check_traffic_secret",
"tls_check_pending_rekey"
],
"KMSANReasoning": "The patch series introduces in-kernel TLS 1.3 KeyUpdate support, including HKDF key derivation (tls_hkdf.c), rekeying routines (tls_keyupdate.c), and new setsockopts (TLS_TX_TRAFFIC_SECRET, TLS_RX_TRAFFIC_SECRET, TLS_TX_KEY_UPDATE).\n\nEvaluating uninitialized memory risks (KMSAN vs KASAN):\n1. No kernel-to-user information leaks: The new options are strictly write-only setsockopts (secrets cannot be read back and getsockopt does not implement them). Existing getsockopt(TLS_TX/TLS_RX) copies crypto_info, which is populated during rekeying from a stack structure (tmp) that is explicitly zeroed via memset(\u0026tmp, 0, sizeof(tmp)) before keys and IVs are copied in.\n2. Structure allocations: All newly added fields in struct tls_context and struct tls_sw_context_rx reside in heap objects allocated with kzalloc_obj, ensuring all new state flags and counters start zeroed.\n3. Stack buffers: Local buffers in HKDF expansion, setsockopt handlers, and message construction (such as info in tls_hkdf_expand_label, body/ctrl in tls_tx_key_update, and data in do_tls_setsockopt_secret) are either explicit zero-initialized, copied from verified sockptr/skb lengths, or sequentially filled before use.\n4. Surrounding buffer accesses: Decrypted KeyUpdate records are consumed and freed (consume_skb) rather than forwarded to userspace.\n\nAny potential bugs in this patch (e.g., buffer indexing/overflows during crypto operations, skb reference counting or use-after-free, or locking races) are under the purview of KASAN and LOCKDEP. KMSAN is not needed as no uninitialized memory read or info-leak risks are introduced.",
"NeedsKMSAN": false,
"Reasoning": "The patch series implements in-kernel TLS 1.3 KeyUpdate support in kTLS (net/tls). It introduces new setsockopt operations (TLS_TX_TRAFFIC_SECRET, TLS_RX_TRAFFIC_SECRET, TLS_TX_KEY_UPDATE), cryptographic key derivation routines using HKDF-Expand-Label (RFC 9846), automatic rekeying logic on TX and RX, and rate-limiting / validation state machines. These changes operate in a core networking subsystem accessible to user space via standard sockets and deserve focused fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/11/08 13:37 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 72afd7682f8c161f815cd82a531d5464e63bdf6e\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sun Nov 8 13:37:35 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/networking/tls.rst b/Documentation/networking/tls.rst\nindex 980c442d7161ae..69752a6d67df47 100644\n--- a/Documentation/networking/tls.rst\n+++ b/Documentation/networking/tls.rst\n@@ -300,6 +300,49 @@ extra byte used by the ContentType field.\n \n [1] https://datatracker.ietf.org/doc/html/rfc8449\n \n+TLS_TX_TRAFFIC_SECRET and TLS_RX_TRAFFIC_SECRET\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+\n+``TLS_TX_TRAFFIC_SECRET`` registers the TLS 1.3 application traffic secret\n+of the transmit direction, ``TLS_RX_TRAFFIC_SECRET`` that of the receive\n+direction. With a registered secret, the kernel performs KeyUpdate for that\n+direction itself (RFC 9846, section 4.7.3). The value is the secret itself.\n+TLS 1.3 only, software path only.\n+\n+Install the key of the direction first, with the ``TLS_TX`` or ``TLS_RX``\n+socket option described above. The cipher of that key selects the hash, and\n+the secret must have the length of that hash: 32 bytes for SHA-256\n+(AES-128-GCM, AES-128-CCM, ChaCha20-Poly1305) or 48 bytes for SHA-384\n+(AES-256-GCM). Other ciphers return ``EOPNOTSUPP``.\n+\n+Each secret can be registered once. ``TLS_RX_TRAFFIC_SECRET`` needs\n+``TLS_TX_TRAFFIC_SECRET`` first: a received KeyUpdate can ask for one in\n+reply, and only the kernel sees it. ``TLS_TX_TRAFFIC_SECRET`` alone is\n+allowed.\n+\n+If the application installs a new key itself with ``TLS_TX``, both secrets\n+are removed. With ``TLS_RX``, the RX secret is removed. KeyUpdate for that\n+direction is then left to the application again.\n+\n+The secrets cannot be read back.\n+\n+With a registered TX secret, the kernel sends a KeyUpdate before the key\n+reaches its record limit (RFC 9846, section 5.5): after 2^24 records, except\n+for ChaCha20-Poly1305, which has no limit. It also sends one before the next\n+record when a received KeyUpdate asks for it. With a registered RX secret,\n+the kernel processes a received KeyUpdate and does not pass the record to\n+the application. A peer that sends more than 8 KeyUpdates in one second ends\n+the connection.\n+\n+TLS_TX_KEY_UPDATE\n+~~~~~~~~~~~~~~~~~\n+\n+TLS 1.3 only. Send a KeyUpdate now and install the next TX key. Needs a\n+registered TX secret. The value is an ``int``: 0 to send a KeyUpdate, 1 to\n+also ask the peer to update its key. Asking the peer needs a registered RX\n+secret, and fails with ``EBUSY`` until the peer has answered the previous\n+request.\n+\n Statistics\n ==========\n \ndiff --git a/include/net/tls.h b/include/net/tls.h\nindex e57bef58851eac..d3123b17030cfc 100644\n--- a/include/net/tls.h\n+++ b/include/net/tls.h\n@@ -63,8 +63,16 @@ struct tls_rec;\n \n #define TLS_HANDSHAKE_KEYUPDATE\t\t24\t/* rfc8446 B.3: Key update */\n \n+/* KeyUpdate: msg_type, 24-bit length, request_update (RFC 9846 4.7.3) */\n+#define TLS_KEYUPDATE_MSG_SIZE\t\t5\n+#define TLS_KEY_UPDATE_NOT_REQUESTED\t0\n+#define TLS_KEY_UPDATE_REQUESTED\t1\n+\n #define TLS_AAD_SPACE_SIZE\t\t13\n \n+/* SHA-384, the longest hash of a TLS 1.3 cipher suite kTLS supports */\n+#define TLS_MAX_TRAFFIC_SECRET_SIZE\t48\n+\n #define TLS_MAX_IV_SIZE\t\t\t16\n #define TLS_MAX_SALT_SIZE\t\t4\n #define TLS_TAG_SIZE\t\t\t16\n@@ -139,6 +147,8 @@ struct tls_sw_context_rx {\n \tu8 zc_capable:1;\n \tu8 reader_contended:1;\n \tbool key_update_pending;\n+\tu8 key_update_count;\t\t/* received in this window */\n+\tunsigned long key_update_window;\t/* jiffies */\n \n \tstruct tls_strparser strp;\n \n@@ -224,6 +234,12 @@ struct tls_prot_info {\n \tu16 tail_size;\n };\n \n+/* A TLS 1.3 application traffic secret. len is 0 if none is registered. */\n+struct tls_traffic_secret {\n+\tu8 data[TLS_MAX_TRAFFIC_SECRET_SIZE];\n+\tu8 len;\n+};\n+\n struct tls_context {\n \t/* read-only cache line */\n \tstruct tls_prot_info prot_info;\n@@ -266,6 +282,13 @@ struct tls_context {\n \tunion tls_crypto_context crypto_send;\n \tunion tls_crypto_context crypto_recv;\n \n+\t/* KeyUpdate state, protected by the socket lock */\n+\tstruct tls_traffic_secret tx_secret;\n+\tstruct tls_traffic_secret rx_secret;\n+\tu64 tx_key_updates;\t\t/* KeyUpdates sent */\n+\tbool tx_key_update_reply;\t/* peer set update_requested */\n+\tbool tx_key_update_asked;\t/* we set it, no answer yet */\n+\n \tstruct list_head list;\n \trefcount_t refcount;\n \tstruct rcu_head rcu;\ndiff --git a/include/uapi/linux/tls.h b/include/uapi/linux/tls.h\nindex 1245ab38afc144..c85408cb6ce76f 100644\n--- a/include/uapi/linux/tls.h\n+++ b/include/uapi/linux/tls.h\n@@ -42,6 +42,9 @@\n #define TLS_TX_ZEROCOPY_RO\t3\t/* TX zerocopy (only sendfile now) */\n #define TLS_RX_EXPECT_NO_PAD\t4\t/* Attempt opportunistic zero-copy */\n #define TLS_TX_MAX_PAYLOAD_LEN\t5\t/* Maximum plaintext size */\n+#define TLS_TX_TRAFFIC_SECRET\t6\t/* TLS 1.3 TX traffic secret */\n+#define TLS_RX_TRAFFIC_SECRET\t7\t/* TLS 1.3 RX traffic secret */\n+#define TLS_TX_KEY_UPDATE\t8\t/* Send a TLS 1.3 KeyUpdate now */\n \n /* Supported versions */\n #define TLS_VERSION_MINOR(ver)\t((ver) \u0026 0xFF)\ndiff --git a/net/tls/Kconfig b/net/tls/Kconfig\nindex 4f4d5973a28f49..6183ebe954ee83 100644\n--- a/net/tls/Kconfig\n+++ b/net/tls/Kconfig\n@@ -8,6 +8,8 @@ config TLS\n \tselect CRYPTO\n \tselect CRYPTO_AES\n \tselect CRYPTO_GCM\n+\tselect CRYPTO_LIB_SHA256\n+\tselect CRYPTO_LIB_SHA512\n \tselect NET_SOCK_MSG\n \tdefault n\n \thelp\ndiff --git a/net/tls/Makefile b/net/tls/Makefile\nindex 4c7d296081ee66..ce8ac6a59f8ced 100644\n--- a/net/tls/Makefile\n+++ b/net/tls/Makefile\n@@ -7,6 +7,7 @@ CFLAGS_trace.o := -I$(src)\n \n obj-$(CONFIG_TLS) += tls.o\n \n-tls-y := tls_main.o tls_sw.o tls_proc.o trace.o tls_strp.o\n+tls-y := tls_main.o tls_sw.o tls_proc.o trace.o tls_strp.o tls_hkdf.o \\\n+\t tls_keyupdate.o\n \n tls-$(CONFIG_TLS_DEVICE) += tls_device.o tls_device_fallback.o\ndiff --git a/net/tls/tls.h b/net/tls/tls.h\nindex 60a37bdaaa2506..d12c9278c8143a 100644\n--- a/net/tls/tls.h\n+++ b/net/tls/tls.h\n@@ -147,6 +147,11 @@ void tls_strp_abort_strp(struct tls_strparser *strp, int err);\n int init_prot_info(struct tls_prot_info *prot,\n \t\t const struct tls_crypto_info *crypto_info,\n \t\t const struct tls_cipher_desc *cipher_desc);\n+int tls_hkdf_expand_label(const u8 *secret, size_t secret_len,\n+\t\t\t const char *label, u8 *out, size_t out_len);\n+int tls_keyupdate_rekey(struct sock *sk, int tx);\n+int tls_keyupdate_rx(struct sock *sk, struct sk_buff *skb);\n+int tls_sw_key_update(struct sock *sk, int request);\n int tls_set_sw_offload(struct sock *sk, int tx,\n \t\t struct tls_crypto_info *new_crypto_info);\n void tls_update_rx_zc_capable(struct tls_context *tls_ctx);\ndiff --git a/net/tls/tls_hkdf.c b/net/tls/tls_hkdf.c\nnew file mode 100644\nindex 00000000000000..49e33146ac7ef2\n--- /dev/null\n+++ b/net/tls/tls_hkdf.c\n@@ -0,0 +1,82 @@\n+// SPDX-License-Identifier: GPL-2.0-only\n+/* Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries.\n+ *\n+ * TLS 1.3 HKDF-Expand-Label, RFC 9846 section 7.1.\n+ *\n+ * TLS 1.3 derives all of its secrets and keys with HKDF (RFC 5869). The\n+ * TLS handshake runs in user space and gives the kernel one application\n+ * traffic secret for each direction. For a KeyUpdate, the kernel derives\n+ * the next traffic secret from the current one (section 7.2), and the\n+ * new key and IV from the next traffic secret (section 7.3). Both steps\n+ * use only HKDF-Expand-Label, so this file implements only that\n+ * function, not the full key schedule of the handshake.\n+ *\n+ * The input is always a traffic secret. It is already a random key of\n+ * the hash length, so HKDF-Extract is not needed. No output is longer\n+ * than the hash, so HKDF-Expand is a single HMAC.\n+ */\n+\n+#include \u003ccrypto/sha2.h\u003e\n+#include \u003clinux/errno.h\u003e\n+#include \u003clinux/string.h\u003e\n+#include \u003clinux/unaligned.h\u003e\n+\n+#include \"tls.h\"\n+\n+#define TLS_HKDF_LABEL_PREFIX\t\t\"tls13 \"\n+#define TLS_HKDF_LABEL_PREFIX_LEN\t(sizeof(TLS_HKDF_LABEL_PREFIX) - 1)\n+#define TLS_HKDF_LABEL_MAX\t\t32\n+\n+/**\n+ * tls_hkdf_expand_label - HKDF-Expand-Label with an empty context\n+ * @secret: the secret\n+ * @secret_len: length of @secret, which selects the hash: SHA-256 for\n+ *\tSHA256_DIGEST_SIZE, SHA-384 for SHA384_DIGEST_SIZE\n+ * @label: the label, without the \"tls13 \" prefix\n+ * @out: output buffer\n+ * @out_len: number of bytes to derive, at most @secret_len\n+ *\n+ * Return: 0 on success, -EINVAL if a length is not valid.\n+ */\n+int tls_hkdf_expand_label(const u8 *secret, size_t secret_len,\n+\t\t\t const char *label, u8 *out, size_t out_len)\n+{\n+\tu8 info[2 + 1 + TLS_HKDF_LABEL_PREFIX_LEN + TLS_HKDF_LABEL_MAX + 1 + 1];\n+\tsize_t label_len = strlen(label);\n+\tu8 tmp[SHA384_DIGEST_SIZE];\n+\tsize_t pos = 0;\n+\n+\tif (secret_len != SHA256_DIGEST_SIZE \u0026\u0026\n+\t secret_len != SHA384_DIGEST_SIZE)\n+\t\treturn -EINVAL;\n+\tif (!out_len || out_len \u003e secret_len || label_len \u003e TLS_HKDF_LABEL_MAX)\n+\t\treturn -EINVAL;\n+\n+\t/* struct {\n+\t *\tuint16 length;\n+\t *\topaque label\u003c7..255\u003e;\n+\t *\topaque context\u003c0..255\u003e;\n+\t * } HkdfLabel;\n+\t *\n+\t * followed by the HKDF-Expand counter for the first output block.\n+\t */\n+\tput_unaligned_be16(out_len, info);\n+\tpos += 2;\n+\tinfo[pos++] = TLS_HKDF_LABEL_PREFIX_LEN + label_len;\n+\tmemcpy(info + pos, TLS_HKDF_LABEL_PREFIX, TLS_HKDF_LABEL_PREFIX_LEN);\n+\tpos += TLS_HKDF_LABEL_PREFIX_LEN;\n+\tmemcpy(info + pos, label, label_len);\n+\tpos += label_len;\n+\tinfo[pos++] = 0;\t/* empty context */\n+\tinfo[pos++] = 1;\t/* counter */\n+\n+\tif (secret_len == SHA256_DIGEST_SIZE)\n+\t\thmac_sha256_usingrawkey(secret, secret_len, info, pos, tmp);\n+\telse\n+\t\thmac_sha384_usingrawkey(secret, secret_len, info, pos, tmp);\n+\n+\tmemcpy(out, tmp, out_len);\n+\tmemzero_explicit(tmp, sizeof(tmp));\n+\n+\treturn 0;\n+}\ndiff --git a/net/tls/tls_keyupdate.c b/net/tls/tls_keyupdate.c\nnew file mode 100644\nindex 00000000000000..b43874b0839353\n--- /dev/null\n+++ b/net/tls/tls_keyupdate.c\n@@ -0,0 +1,160 @@\n+// SPDX-License-Identifier: GPL-2.0-only\n+/* Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries.\n+ *\n+ * TLS 1.3 KeyUpdate, RFC 9846 sections 4.7.3 and 7.2.\n+ *\n+ * A TLS 1.3 peer can change the key of the direction it sends in at any\n+ * time. It sends a KeyUpdate message, then encrypts all later records\n+ * with the next key. A sender must also change its key before the key\n+ * has protected too many records (section 5.5).\n+ *\n+ * The next key is derived from the application traffic secret, not from\n+ * the current key. User space gives the kernel only the key and IV, so\n+ * the kernel cannot derive the next key on its own. Without the secret,\n+ * a received KeyUpdate goes to the application, and the kernel stops\n+ * decrypting until the application installs the next key with TLS_RX.\n+ *\n+ * With TLS_TX_TRAFFIC_SECRET or TLS_RX_TRAFFIC_SECRET, user space also\n+ * gives the kernel the secret of that direction. The kernel then handles\n+ * KeyUpdate itself. This file derives and installs the next key, and\n+ * processes a received KeyUpdate. tls_sw.c sends KeyUpdate messages.\n+ */\n+\n+#include \u003clinux/jiffies.h\u003e\n+#include \u003clinux/skbuff.h\u003e\n+#include \u003cnet/strparser.h\u003e\n+#include \u003cnet/tls.h\u003e\n+\n+#include \"tls.h\"\n+\n+/* A peer that sends more KeyUpdates than this in one second ends the\n+ * connection. Each one costs three HMACs and a new key, and carries no\n+ * data.\n+ */\n+#define TLS_KEYUPDATE_RX_MAX_PER_SEC\t8\n+\n+/**\n+ * tls_keyupdate_rekey - install the next key of one direction\n+ * @sk: socket\n+ * @tx: non-zero for the transmit direction\n+ *\n+ * Derive the next application traffic secret from the registered one,\n+ * then its key and IV, and install them. The record sequence number\n+ * restarts at zero.\n+ *\n+ * Return: 0 on success, or a negative errno. On error the installed key\n+ * and the registered secret do not change.\n+ */\n+int tls_keyupdate_rekey(struct sock *sk, int tx)\n+{\n+\tstruct tls_context *ctx = tls_get_ctx(sk);\n+\tstruct tls_traffic_secret *secret;\n+\tconst struct tls_cipher_desc *cipher_desc;\n+\tu8 iv[TLS_MAX_SALT_SIZE + TLS_MAX_IV_SIZE];\n+\tu8 next[TLS_MAX_TRAFFIC_SECRET_SIZE];\n+\tunion tls_crypto_context tmp;\n+\tstruct tls_crypto_info *info = (void *)\u0026tmp;\n+\tint rc;\n+\n+\tsecret = tx ? \u0026ctx-\u003etx_secret : \u0026ctx-\u003erx_secret;\n+\tcipher_desc = get_cipher_desc(ctx-\u003eprot_info.cipher_type);\n+\tif (!cipher_desc || !secret-\u003elen ||\n+\t cipher_desc-\u003esalt + cipher_desc-\u003eiv \u003e sizeof(iv))\n+\t\treturn -EINVAL;\n+\n+\t/* version and cipher_type; the rest of tmp stays zero */\n+\tmemset(\u0026tmp, 0, sizeof(tmp));\n+\t*info = tx ? ctx-\u003ecrypto_send.info : ctx-\u003ecrypto_recv.info;\n+\n+\trc = tls_hkdf_expand_label(secret-\u003edata, secret-\u003elen, \"traffic upd\",\n+\t\t\t\t next, secret-\u003elen);\n+\tif (rc)\n+\t\tgoto out;\n+\n+\trc = tls_hkdf_expand_label(next, secret-\u003elen, \"key\",\n+\t\t\t\t crypto_info_key(info, cipher_desc),\n+\t\t\t\t cipher_desc-\u003ekey);\n+\tif (rc)\n+\t\tgoto out;\n+\n+\trc = tls_hkdf_expand_label(next, secret-\u003elen, \"iv\", iv,\n+\t\t\t\t cipher_desc-\u003esalt + cipher_desc-\u003eiv);\n+\tif (rc)\n+\t\tgoto out;\n+\n+\tmemcpy(crypto_info_salt(info, cipher_desc), iv, cipher_desc-\u003esalt);\n+\tmemcpy(crypto_info_iv(info, cipher_desc), iv + cipher_desc-\u003esalt,\n+\t cipher_desc-\u003eiv);\n+\n+\trc = tls_set_sw_offload(sk, tx, info);\n+\tif (rc)\n+\t\tgoto out;\n+\n+\tmemcpy(secret-\u003edata, next, secret-\u003elen);\n+\n+out:\n+\tmemzero_explicit(next, sizeof(next));\n+\tmemzero_explicit(iv, sizeof(iv));\n+\tmemzero_explicit(\u0026tmp, sizeof(tmp));\n+\n+\treturn rc;\n+}\n+\n+/**\n+ * tls_keyupdate_rx - process a received KeyUpdate\n+ * @sk: socket\n+ * @skb: the decrypted record, which starts with a KeyUpdate message\n+ *\n+ * Called with the socket locked, for a direction with a registered\n+ * secret. The caller does not pass the record to the user.\n+ *\n+ * Return: 0 on success, or a negative errno if the connection must end.\n+ */\n+int tls_keyupdate_rx(struct sock *sk, struct sk_buff *skb)\n+{\n+\tstruct tls_context *ctx = tls_get_ctx(sk);\n+\tstruct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);\n+\tconst struct strp_msg *rxm = strp_msg(skb);\n+\tu8 msg[TLS_KEYUPDATE_MSG_SIZE];\n+\tint rc;\n+\n+\t/* A KeyUpdate must end its record (section 5.1). Another message\n+\t * in the same record is not supported.\n+\t */\n+\tif (rxm-\u003efull_len != sizeof(msg) ||\n+\t skb_copy_bits(skb, rxm-\u003eoffset, msg, sizeof(msg)))\n+\t\treturn -EBADMSG;\n+\n+\t/* msg_type, a 24-bit length of 1, then request_update */\n+\tif (msg[1] || msg[2] || msg[3] != 1)\n+\t\treturn -EBADMSG;\n+\tif (msg[4] != TLS_KEY_UPDATE_NOT_REQUESTED \u0026\u0026\n+\t msg[4] != TLS_KEY_UPDATE_REQUESTED)\n+\t\treturn -EBADMSG;\n+\n+\tif (!rx_ctx-\u003ekey_update_count ||\n+\t time_after(jiffies, rx_ctx-\u003ekey_update_window + HZ)) {\n+\t\trx_ctx-\u003ekey_update_window = jiffies;\n+\t\trx_ctx-\u003ekey_update_count = 0;\n+\t}\n+\tif (++rx_ctx-\u003ekey_update_count \u003e TLS_KEYUPDATE_RX_MAX_PER_SEC) {\n+\t\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYERROR);\n+\t\tnet_warn_ratelimited(\"tls: peer sent more than %d KeyUpdates in one second\\n\",\n+\t\t\t\t TLS_KEYUPDATE_RX_MAX_PER_SEC);\n+\t\treturn -EBADMSG;\n+\t}\n+\n+\trc = tls_keyupdate_rekey(sk, 0);\n+\tif (rc) {\n+\t\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYERROR);\n+\t\treturn rc;\n+\t}\n+\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYOK);\n+\n+\t/* Any KeyUpdate from the peer answers our update_requested */\n+\tctx-\u003etx_key_update_asked = false;\n+\tif (msg[4] == TLS_KEY_UPDATE_REQUESTED)\n+\t\tctx-\u003etx_key_update_reply = true;\n+\n+\treturn 0;\n+}\ndiff --git a/net/tls/tls_main.c b/net/tls/tls_main.c\nindex b8d5a843f76171..b19e6946dbaed9 100644\n--- a/net/tls/tls_main.c\n+++ b/net/tls/tls_main.c\n@@ -31,6 +31,8 @@\n * SOFTWARE.\n */\n \n+#include \u003ccrypto/sha2.h\u003e\n+#include \u003ccrypto/utils.h\u003e\n #include \u003clinux/module.h\u003e\n \n #include \u003cnet/tcp.h\u003e\n@@ -325,6 +327,8 @@ void tls_ctx_free(struct sock *sk, struct tls_context *ctx)\n \n \tmemzero_explicit(\u0026ctx-\u003ecrypto_send, sizeof(ctx-\u003ecrypto_send));\n \tmemzero_explicit(\u0026ctx-\u003ecrypto_recv, sizeof(ctx-\u003ecrypto_recv));\n+\tmemzero_explicit(\u0026ctx-\u003etx_secret, sizeof(ctx-\u003etx_secret));\n+\tmemzero_explicit(\u0026ctx-\u003erx_secret, sizeof(ctx-\u003erx_secret));\n \tmutex_destroy(\u0026ctx-\u003etx_lock);\n \n \tif (sk)\n@@ -757,8 +761,20 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,\n \t\tctx-\u003erx_conf = conf;\n \tupdate_sk_prot(sk, ctx);\n \n-\tif (update)\n+\tif (update) {\n+\t\t/* The registered secret no longer matches the key. The RX\n+\t\t * secret also needs the TX one, see do_tls_setsockopt_secret().\n+\t\t */\n+\t\tif (tx) {\n+\t\t\tmemzero_explicit(\u0026ctx-\u003etx_secret,\n+\t\t\t\t\t sizeof(ctx-\u003etx_secret));\n+\t\t\tctx-\u003etx_key_update_reply = false;\n+\t\t}\n+\t\tmemzero_explicit(\u0026ctx-\u003erx_secret, sizeof(ctx-\u003erx_secret));\n+\t\t/* the answer now goes to user space */\n+\t\tctx-\u003etx_key_update_asked = false;\n \t\treturn 0;\n+\t}\n \n \tif (tx) {\n \t\tctx-\u003esk_write_space = sk-\u003esk_write_space;\n@@ -868,6 +884,159 @@ static int do_tls_setsockopt_tx_payload_len(struct sock *sk, sockptr_t optval,\n \treturn 0;\n }\n \n+/* Length of the TLS 1.3 traffic secret for @cipher_type, the size of\n+ * its hash. 0 if KeyUpdate is not supported for the cipher.\n+ */\n+static int tls_traffic_secret_len(u16 cipher_type)\n+{\n+\tswitch (cipher_type) {\n+\tcase TLS_CIPHER_AES_GCM_128:\n+\tcase TLS_CIPHER_AES_CCM_128:\n+\tcase TLS_CIPHER_CHACHA20_POLY1305:\n+\t\treturn SHA256_DIGEST_SIZE;\n+\tcase TLS_CIPHER_AES_GCM_256:\n+\t\treturn SHA384_DIGEST_SIZE;\n+\tdefault:\n+\t\treturn 0;\n+\t}\n+}\n+\n+/* The installed key and IV must be the ones that @secret gives\n+ * (RFC 9846 section 7.3):\n+ *\n+ * key = HKDF-Expand-Label(secret, \"key\", \"\", key_length)\n+ * iv = HKDF-Expand-Label(secret, \"iv\", \"\", iv_length)\n+ *\n+ * crypto_info holds the iv as salt followed by iv. Each KeyUpdate\n+ * derives the next key from the secret, so a wrong secret fails only\n+ * at the first KeyUpdate, with no clear cause.\n+ */\n+static int tls_check_traffic_secret(struct tls_crypto_info *crypto_info,\n+\t\t\t\t const u8 *secret, int len)\n+{\n+\tconst struct tls_cipher_desc *cipher_desc;\n+\tu8 iv[TLS_MAX_SALT_SIZE + TLS_MAX_IV_SIZE];\n+\tu8 key[TLS_CIPHER_AES_GCM_256_KEY_SIZE];\n+\tint rc;\n+\n+\tcipher_desc = get_cipher_desc(crypto_info-\u003ecipher_type);\n+\tif (!cipher_desc || cipher_desc-\u003ekey \u003e sizeof(key) ||\n+\t cipher_desc-\u003esalt + cipher_desc-\u003eiv \u003e sizeof(iv))\n+\t\treturn -EINVAL;\n+\n+\trc = tls_hkdf_expand_label(secret, len, \"key\", key, cipher_desc-\u003ekey);\n+\tif (rc)\n+\t\tgoto out;\n+\n+\trc = tls_hkdf_expand_label(secret, len, \"iv\", iv,\n+\t\t\t\t cipher_desc-\u003esalt + cipher_desc-\u003eiv);\n+\tif (rc)\n+\t\tgoto out;\n+\n+\tif (crypto_memneq(key, crypto_info_key(crypto_info, cipher_desc),\n+\t\t\t cipher_desc-\u003ekey) ||\n+\t crypto_memneq(iv, crypto_info_salt(crypto_info, cipher_desc),\n+\t\t\t cipher_desc-\u003esalt) ||\n+\t crypto_memneq(iv + cipher_desc-\u003esalt,\n+\t\t\t crypto_info_iv(crypto_info, cipher_desc),\n+\t\t\t cipher_desc-\u003eiv))\n+\t\trc = -EINVAL;\n+\n+out:\n+\tmemzero_explicit(key, sizeof(key));\n+\tmemzero_explicit(iv, sizeof(iv));\n+\n+\treturn rc;\n+}\n+\n+static int do_tls_setsockopt_secret(struct sock *sk, sockptr_t optval,\n+\t\t\t\t unsigned int optlen, int tx)\n+{\n+\tstruct tls_context *ctx = tls_get_ctx(sk);\n+\tstruct tls_crypto_info *crypto_info;\n+\tstruct tls_traffic_secret *secret;\n+\tu8 data[TLS_MAX_TRAFFIC_SECRET_SIZE];\n+\tint len, rc;\n+\n+\tif (tx) {\n+\t\tcrypto_info = \u0026ctx-\u003ecrypto_send.info;\n+\t\tsecret = \u0026ctx-\u003etx_secret;\n+\t} else {\n+\t\tcrypto_info = \u0026ctx-\u003ecrypto_recv.info;\n+\t\tsecret = \u0026ctx-\u003erx_secret;\n+\t}\n+\n+\t/* The key must be installed first: its cipher selects the hash */\n+\tif (!TLS_CRYPTO_INFO_READY(crypto_info) ||\n+\t crypto_info-\u003eversion != TLS_1_3_VERSION)\n+\t\treturn -EINVAL;\n+\n+\tif ((tx ? ctx-\u003etx_conf : ctx-\u003erx_conf) != TLS_SW)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tlen = tls_traffic_secret_len(crypto_info-\u003ecipher_type);\n+\tif (!len)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tif (sockptr_is_null(optval) || optlen != len)\n+\t\treturn -EINVAL;\n+\n+\t/* A received KeyUpdate may ask for one in reply. The kernel\n+\t * consumes the record, so only the kernel can send the reply.\n+\t */\n+\tif (!tx \u0026\u0026 !ctx-\u003etx_secret.len)\n+\t\treturn -EINVAL;\n+\n+\tif (secret-\u003elen)\n+\t\treturn -EBUSY;\n+\n+\tif (copy_from_sockptr(data, optval, len)) {\n+\t\trc = -EFAULT;\n+\t\tgoto out;\n+\t}\n+\n+\trc = tls_check_traffic_secret(crypto_info, data, len);\n+\tif (rc)\n+\t\tgoto out;\n+\n+\tmemcpy(secret-\u003edata, data, len);\n+\tsecret-\u003elen = len;\n+\n+out:\n+\tmemzero_explicit(data, sizeof(data));\n+\n+\treturn rc;\n+}\n+\n+static int do_tls_setsockopt_key_update(struct sock *sk, sockptr_t optval,\n+\t\t\t\t\tunsigned int optlen)\n+{\n+\tstruct tls_context *ctx = tls_get_ctx(sk);\n+\tint value, rc;\n+\n+\tif (sockptr_is_null(optval) || optlen != sizeof(value))\n+\t\treturn -EINVAL;\n+\n+\tif (copy_from_sockptr(\u0026value, optval, sizeof(value)))\n+\t\treturn -EFAULT;\n+\n+\tif (value != 0 \u0026\u0026 value != 1)\n+\t\treturn -EINVAL;\n+\n+\trc = mutex_lock_interruptible(\u0026ctx-\u003etx_lock);\n+\tif (rc)\n+\t\treturn rc;\n+\tlock_sock(sk);\n+\tif (ctx-\u003etx_conf == TLS_SW)\n+\t\trc = tls_sw_key_update(sk, value);\n+\telse\n+\t\trc = -EINVAL;\n+\trelease_sock(sk);\n+\tmutex_unlock(\u0026ctx-\u003etx_lock);\n+\n+\treturn rc;\n+}\n+\n static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,\n \t\t\t unsigned int optlen)\n {\n@@ -901,6 +1070,16 @@ static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,\n \t\trelease_sock(sk);\n \t\tmutex_unlock(\u0026ctx-\u003etx_lock);\n \t\tbreak;\n+\tcase TLS_TX_TRAFFIC_SECRET:\n+\tcase TLS_RX_TRAFFIC_SECRET:\n+\t\tlock_sock(sk);\n+\t\trc = do_tls_setsockopt_secret(sk, optval, optlen,\n+\t\t\t\t\t optname == TLS_TX_TRAFFIC_SECRET);\n+\t\trelease_sock(sk);\n+\t\tbreak;\n+\tcase TLS_TX_KEY_UPDATE:\n+\t\trc = do_tls_setsockopt_key_update(sk, optval, optlen);\n+\t\tbreak;\n \tdefault:\n \t\trc = -ENOPROTOOPT;\n \t\tbreak;\ndiff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c\nindex 771e02bff51999..433a43d13a8729 100644\n--- a/net/tls/tls_sw.c\n+++ b/net/tls/tls_sw.c\n@@ -40,6 +40,7 @@\n #include \u003clinux/module.h\u003e\n #include \u003clinux/kernel.h\u003e\n #include \u003clinux/splice.h\u003e\n+#include \u003clinux/unaligned.h\u003e\n #include \u003ccrypto/aead.h\u003e\n \n #include \u003cnet/strparser.h\u003e\n@@ -763,6 +764,8 @@ static int tls_sw_sendmsg_splice(struct sock *sk, struct msghdr *msg,\n \treturn 0;\n }\n \n+static int tls_tx_maybe_update_key(struct sock *sk, int flags);\n+\n static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,\n \t\t\t\t size_t size)\n {\n@@ -805,6 +808,19 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,\n \t\t\tgoto send_end;\n \t\t}\n \n+\t\t/* Check before each new record: one call can write many\n+\t\t * records, and a KeyUpdate reply must go before the next\n+\t\t * one. A send that failed before it copied any data leaves\n+\t\t * an empty open record. The KeyUpdate itself is a handshake\n+\t\t * record.\n+\t\t */\n+\t\tif ((!ctx-\u003eopen_rec || !ctx-\u003eopen_rec-\u003emsg_plaintext.sg.size) \u0026\u0026\n+\t\t record_type == TLS_RECORD_TYPE_DATA) {\n+\t\t\tret = tls_tx_maybe_update_key(sk, msg-\u003emsg_flags);\n+\t\t\tif (ret)\n+\t\t\t\tgoto send_end;\n+\t\t}\n+\n \t\tif (ctx-\u003eopen_rec)\n \t\t\trec = ctx-\u003eopen_rec;\n \t\telse\n@@ -1005,6 +1021,153 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,\n \treturn copied \u003e 0 ? copied : ret;\n }\n \n+/* RFC 9846 section 5.5 allows 2^24.5 full-size records per AES-GCM key.\n+ * Send the KeyUpdate a little earlier.\n+ */\n+#define TLS_KEYUPDATE_TX_RECORDS\tBIT_ULL(24)\n+\n+/* Section 4.7.3: the epoch must not exceed 2^48-1. The application\n+ * traffic secrets start at epoch 3.\n+ */\n+#define TLS_KEYUPDATE_MAX\t\t(BIT_ULL(48) - 4)\n+\n+/* Send a KeyUpdate with the current key, then install the next key.\n+ * Called with tx_lock held and the socket locked.\n+ */\n+static int tls_tx_key_update(struct sock *sk, u8 request, int flags)\n+{\n+\tu8 body[TLS_KEYUPDATE_MSG_SIZE] = {\n+\t\tTLS_HANDSHAKE_KEYUPDATE, 0, 0, 1, request\n+\t};\n+\tstruct tls_context *tls_ctx = tls_get_ctx(sk);\n+\tstruct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);\n+\tu8 record_type = TLS_RECORD_TYPE_HANDSHAKE;\n+\tunion {\n+\t\tstruct cmsghdr cmsg;\n+\t\tu8 buf[CMSG_SPACE(sizeof(record_type))];\n+\t} ctrl = {};\n+\tstruct msghdr msg = {};\n+\tstruct cmsghdr *cmsg;\n+\tstruct kvec iov;\n+\tint ret;\n+\n+\tmsg.msg_control = \u0026ctrl;\n+\tmsg.msg_controllen = sizeof(ctrl);\n+\tmsg.msg_flags = flags \u0026 (MSG_DONTWAIT | MSG_NOSIGNAL);\n+\n+\tcmsg = CMSG_FIRSTHDR(\u0026msg);\n+\tcmsg-\u003ecmsg_level = SOL_TLS;\n+\tcmsg-\u003ecmsg_type = TLS_SET_RECORD_TYPE;\n+\tcmsg-\u003ecmsg_len = CMSG_LEN(sizeof(record_type));\n+\tmemcpy(CMSG_DATA(cmsg), \u0026record_type, sizeof(record_type));\n+\n+\tiov.iov_base = body;\n+\tiov.iov_len = sizeof(body);\n+\tiov_iter_kvec(\u0026msg.msg_iter, ITER_SOURCE, \u0026iov, 1, sizeof(body));\n+\n+\t/* A control record closes the open record first, so all data\n+\t * written so far goes out with the current key, ahead of the\n+\t * KeyUpdate.\n+\t */\n+\tret = tls_sw_sendmsg_locked(sk, \u0026msg, sizeof(body));\n+\tif (ret \u003c 0)\n+\t\treturn ret;\n+\n+\t/* Nothing may be encrypted with the next key before the KeyUpdate\n+\t * is encrypted with the current one.\n+\t */\n+\tret = tls_encrypt_async_wait(ctx);\n+\tif (!ret)\n+\t\tret = tls_keyupdate_rekey(sk, 1);\n+\tif (ret) {\n+\t\t/* The peer expects the next key: the connection is lost */\n+\t\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXREKEYERROR);\n+\t\ttls_err_abort(sk, ret);\n+\t\treturn ret;\n+\t}\n+\n+\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXREKEYOK);\n+\ttls_ctx-\u003etx_key_updates++;\n+\tif (request == TLS_KEY_UPDATE_NOT_REQUESTED)\n+\t\ttls_ctx-\u003etx_key_update_reply = false;\n+\n+\treturn 0;\n+}\n+\n+/* Send a KeyUpdate if one is due: in reply to the peer's\n+ * update_requested (RFC 9846 section 4.7.3), or before the key reaches\n+ * its record limit (section 5.5). ChaCha20-Poly1305 has no record limit:\n+ * the sequence number would wrap first.\n+ */\n+static int tls_tx_maybe_update_key(struct sock *sk, int flags)\n+{\n+\tstruct tls_context *ctx = tls_get_ctx(sk);\n+\tbool limit;\n+\n+\tif (likely(!ctx-\u003etx_secret.len))\n+\t\treturn 0;\n+\n+\tlimit = ctx-\u003eprot_info.cipher_type != TLS_CIPHER_CHACHA20_POLY1305 \u0026\u0026\n+\t\tget_unaligned_be64(ctx-\u003etx.rec_seq) \u003e= TLS_KEYUPDATE_TX_RECORDS;\n+\tif (!limit \u0026\u0026 !ctx-\u003etx_key_update_reply)\n+\t\treturn 0;\n+\n+\tif (ctx-\u003etx_key_updates \u003e= TLS_KEYUPDATE_MAX) {\n+\t\t/* Ignore update_requested. At the record limit the\n+\t\t * connection must end.\n+\t\t */\n+\t\tctx-\u003etx_key_update_reply = false;\n+\t\treturn limit ? -EKEYEXPIRED : 0;\n+\t}\n+\n+\treturn tls_tx_key_update(sk, TLS_KEY_UPDATE_NOT_REQUESTED, flags);\n+}\n+\n+/**\n+ * tls_sw_key_update - send a KeyUpdate now, for TLS_TX_KEY_UPDATE\n+ * @sk: socket\n+ * @request: non-zero to also ask the peer to update its key\n+ *\n+ * Called with tx_lock held and the socket locked.\n+ *\n+ * Return: 0 on success, or a negative errno.\n+ */\n+int tls_sw_key_update(struct sock *sk, int request)\n+{\n+\tstruct tls_context *ctx = tls_get_ctx(sk);\n+\tint ret;\n+\n+\tif (!ctx-\u003etx_secret.len)\n+\t\treturn -EINVAL;\n+\n+\tif (request) {\n+\t\t/* Only the kernel sees the answer */\n+\t\tif (!ctx-\u003erx_secret.len)\n+\t\t\treturn -EINVAL;\n+\t\t/* Section 4.7.3: wait for the answer to the previous one */\n+\t\tif (ctx-\u003etx_key_update_asked)\n+\t\t\treturn -EBUSY;\n+\t}\n+\n+\tif (ctx-\u003etx_key_updates \u003e= TLS_KEYUPDATE_MAX - 1)\n+\t\treturn -EKEYEXPIRED;\n+\n+\t/* A reply must carry update_not_requested */\n+\tif (request \u0026\u0026 ctx-\u003etx_key_update_reply) {\n+\t\tret = tls_tx_key_update(sk, TLS_KEY_UPDATE_NOT_REQUESTED,\n+\t\t\t\t\tMSG_NOSIGNAL);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\n+\tret = tls_tx_key_update(sk, request ? TLS_KEY_UPDATE_REQUESTED :\n+\t\t\t\tTLS_KEY_UPDATE_NOT_REQUESTED, MSG_NOSIGNAL);\n+\tif (!ret \u0026\u0026 request)\n+\t\tctx-\u003etx_key_update_asked = true;\n+\n+\treturn ret;\n+}\n+\n int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)\n {\n \tstruct tls_context *tls_ctx = tls_get_ctx(sk);\n@@ -1551,15 +1714,28 @@ static int tls_check_pending_rekey(struct sock *sk, struct tls_context *ctx,\n \tif (hs_type == TLS_HANDSHAKE_KEYUPDATE) {\n \t\tstruct tls_sw_context_rx *rx_ctx = ctx-\u003epriv_ctx_rx;\n \n-\t\tWRITE_ONCE(rx_ctx-\u003ekey_update_pending, true);\n \t\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYRECEIVED);\n+\n+\t\tif (ctx-\u003erx_secret.len) {\n+\t\t\terr = tls_keyupdate_rx(sk, skb);\n+\t\t\tif (err) {\n+\t\t\t\ttls_err_abort(sk, err);\n+\t\t\t\t/* callers free the record only on success */\n+\t\t\t\tconsume_skb(skb);\n+\t\t\t\treturn err;\n+\t\t\t}\n+\t\t\treturn 1;\n+\t\t}\n+\n+\t\tWRITE_ONCE(rx_ctx-\u003ekey_update_pending, true);\n \t}\n \n \treturn 0;\n }\n \n /* On decrypt failure the connection is aborted (sk_err set) before\n- * returning a negative errno.\n+ * returning a negative errno. Return 1 for a record the kernel consumed\n+ * (a KeyUpdate): the caller frees it and does not pass it to the user.\n */\n static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,\n \t\t\t struct tls_decrypt_arg *darg)\n@@ -1883,6 +2059,11 @@ int tls_sw_recvmsg(struct sock *sk,\n \t\terr = tls_rx_one_record(sk, msg, \u0026darg);\n \t\tif (err \u003c 0)\n \t\t\tgoto recv_end;\n+\t\tif (err \u003e 0) {\n+\t\t\ttls_rx_rec_done(ctx);\n+\t\t\tconsume_skb(darg.skb);\n+\t\t\tcontinue;\n+\t\t}\n \n \t\tasync |= darg.async;\n \n@@ -2023,9 +2204,8 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,\n \tif (err)\n \t\tgoto splice_read_end;\n \n-\tif (!skb_queue_empty(\u0026ctx-\u003erx_list)) {\n-\t\tskb = __skb_dequeue(\u0026ctx-\u003erx_list);\n-\t} else {\n+\tskb = __skb_dequeue(\u0026ctx-\u003erx_list);\n+\twhile (!skb) {\n \t\tstruct tls_decrypt_arg darg;\n \n \t\terr = tls_rx_rec_wait(sk, nonblock, true, false);\n@@ -2039,7 +2219,10 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,\n \t\t\tgoto splice_read_end;\n \n \t\ttls_rx_rec_done(ctx);\n-\t\tskb = darg.skb;\n+\t\tif (err \u003e 0)\n+\t\t\tconsume_skb(darg.skb);\n+\t\telse\n+\t\t\tskb = darg.skb;\n \t}\n \n \trxm = strp_msg(skb);\n@@ -2121,6 +2304,11 @@ int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,\n \t\t\terr = tls_rx_one_record(sk, NULL, \u0026darg);\n \t\t\tif (err \u003c 0)\n \t\t\t\tgoto read_sock_end;\n+\t\t\tif (err \u003e 0) {\n+\t\t\t\ttls_rx_rec_done(ctx);\n+\t\t\t\tconsume_skb(darg.skb);\n+\t\t\t\tcontinue;\n+\t\t\t}\n \n \t\t\treleased = tls_read_flush_backlog(sk, prot, INT_MAX,\n \t\t\t\t\t\t\t 0, decrypted,\n@@ -2520,6 +2708,12 @@ static void tls_finish_key_update(struct sock *sk, struct tls_context *tls_ctx)\n {\n \tstruct tls_sw_context_rx *ctx = tls_ctx-\u003epriv_ctx_rx;\n \n+\t/* Only a pending KeyUpdate holds poll() back. The in-kernel rekey\n+\t * runs while the KeyUpdate record is still queued.\n+\t */\n+\tif (!READ_ONCE(ctx-\u003ekey_update_pending))\n+\t\treturn;\n+\n \tWRITE_ONCE(ctx-\u003ekey_update_pending, false);\n \t/* wake-up pre-existing poll() */\n \tctx-\u003esaved_data_ready(sk);\ndiff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config\nindex 7a89e2618d4c45..b75692f2576597 100644\n--- a/tools/testing/selftests/net/config\n+++ b/tools/testing/selftests/net/config\n@@ -10,8 +10,12 @@ CONFIG_CAN_VXCAN=m\n CONFIG_CGROUPS=y\n CONFIG_CRYPTO_ARIA=y\n CONFIG_CRYPTO_CHACHA20POLY1305=m\n+CONFIG_CRYPTO_HMAC=y\n CONFIG_CRYPTO_SHA1=y\n+CONFIG_CRYPTO_SHA256=y\n+CONFIG_CRYPTO_SHA512=y\n CONFIG_CRYPTO_SM4_GENERIC=y\n+CONFIG_CRYPTO_USER_API_HASH=y\n CONFIG_DEBUG_FS=y\n CONFIG_DEBUG_INFO_BTF=y\n CONFIG_DEBUG_INFO_BTF_MODULES=n\ndiff --git a/tools/testing/selftests/net/tls.c b/tools/testing/selftests/net/tls.c\nindex c4203401c4bfc2..ed3b510a5ef6fe 100644\n--- a/tools/testing/selftests/net/tls.c\n+++ b/tools/testing/selftests/net/tls.c\n@@ -11,6 +11,7 @@\n #include \u003cstdlib.h\u003e\n #include \u003cunistd.h\u003e\n \n+#include \u003clinux/if_alg.h\u003e\n #include \u003clinux/tls.h\u003e\n #include \u003clinux/tcp.h\u003e\n #include \u003clinux/socket.h\u003e\n@@ -2421,6 +2422,525 @@ TEST_F(tls, rekey_poll_delay)\n \t}\n }\n \n+#ifndef SOL_ALG\n+#define SOL_ALG 279\n+#endif\n+\n+/* RFC 9846 section 7.1, HKDF-Expand-Label with an empty context, for an\n+ * output no longer than the hash. The HMAC comes from the kernel.\n+ */\n+static bool hkdf_expand_label(const unsigned char *secret, int secret_len,\n+\t\t\t const char *label, unsigned char *out,\n+\t\t\t int out_len)\n+{\n+\tstruct sockaddr_alg sa = {\n+\t\t.salg_family = AF_ALG,\n+\t\t.salg_type = \"hash\",\n+\t};\n+\tunsigned char info[2 + 1 + 255 + 1 + 1], md[48];\n+\tint tfm, op, n = 0, label_len = 6 + strlen(label);\n+\tbool ok = false;\n+\n+\tstrcpy((char *)sa.salg_name,\n+\t secret_len == 48 ? \"hmac(sha384)\" : \"hmac(sha256)\");\n+\n+\tinfo[n++] = out_len \u003e\u003e 8;\n+\tinfo[n++] = out_len;\n+\tinfo[n++] = label_len;\n+\tmemcpy(info + n, \"tls13 \", 6);\n+\tmemcpy(info + n + 6, label, label_len - 6);\n+\tn += label_len;\n+\tinfo[n++] = 0;\t/* context */\n+\tinfo[n++] = 1;\t/* HKDF-Expand counter */\n+\n+\ttfm = socket(AF_ALG, SOCK_SEQPACKET, 0);\n+\tif (tfm \u003c 0)\n+\t\treturn false;\n+\tif (bind(tfm, (struct sockaddr *)\u0026sa, sizeof(sa)) ||\n+\t setsockopt(tfm, SOL_ALG, ALG_SET_KEY, secret, secret_len))\n+\t\tgoto out;\n+\n+\top = accept(tfm, NULL, 0);\n+\tif (op \u003c 0)\n+\t\tgoto out;\n+\tok = write(op, info, n) == n \u0026\u0026 read(op, md, secret_len) == secret_len;\n+\tclose(op);\n+\tif (ok)\n+\t\tmemcpy(out, md, out_len);\n+out:\n+\tclose(tfm);\n+\treturn ok;\n+}\n+\n+/* Fill @keys with the key and IV that @secret gives, record number 0 */\n+static bool keyupdate_keys(uint16_t cipher_type, const unsigned char *secret,\n+\t\t\t int secret_len, struct tls_crypto_info_keys *keys)\n+{\n+\tunsigned char *key, *salt, *iv;\n+\tint key_len, salt_len, iv_len;\n+\tunsigned char nonce[12];\n+\n+\ttls_crypto_info_init(TLS_1_3_VERSION, cipher_type, keys, 0);\n+\n+\tswitch (cipher_type) {\n+\tcase TLS_CIPHER_AES_GCM_128:\n+\t\tkey = keys-\u003eaes128.key;\n+\t\tsalt = keys-\u003eaes128.salt;\n+\t\tiv = keys-\u003eaes128.iv;\n+\t\tkey_len = sizeof(keys-\u003eaes128.key);\n+\t\tsalt_len = sizeof(keys-\u003eaes128.salt);\n+\t\tiv_len = sizeof(keys-\u003eaes128.iv);\n+\t\tbreak;\n+\tcase TLS_CIPHER_AES_GCM_256:\n+\t\tkey = keys-\u003eaesgcm256.key;\n+\t\tsalt = keys-\u003eaesgcm256.salt;\n+\t\tiv = keys-\u003eaesgcm256.iv;\n+\t\tkey_len = sizeof(keys-\u003eaesgcm256.key);\n+\t\tsalt_len = sizeof(keys-\u003eaesgcm256.salt);\n+\t\tiv_len = sizeof(keys-\u003eaesgcm256.iv);\n+\t\tbreak;\n+\tcase TLS_CIPHER_AES_CCM_128:\n+\t\tkey = keys-\u003eaesccm128.key;\n+\t\tsalt = keys-\u003eaesccm128.salt;\n+\t\tiv = keys-\u003eaesccm128.iv;\n+\t\tkey_len = sizeof(keys-\u003eaesccm128.key);\n+\t\tsalt_len = sizeof(keys-\u003eaesccm128.salt);\n+\t\tiv_len = sizeof(keys-\u003eaesccm128.iv);\n+\t\tbreak;\n+\tcase TLS_CIPHER_CHACHA20_POLY1305:\n+\t\tkey = keys-\u003echacha20.key;\n+\t\tsalt = keys-\u003echacha20.salt;\n+\t\tiv = keys-\u003echacha20.iv;\n+\t\tkey_len = sizeof(keys-\u003echacha20.key);\n+\t\tsalt_len = 0;\n+\t\tiv_len = sizeof(keys-\u003echacha20.iv);\n+\t\tbreak;\n+\tdefault:\n+\t\treturn false;\n+\t}\n+\n+\tif (!hkdf_expand_label(secret, secret_len, \"key\", key, key_len) ||\n+\t !hkdf_expand_label(secret, secret_len, \"iv\", nonce,\n+\t\t\t salt_len + iv_len))\n+\t\treturn false;\n+\n+\tmemcpy(salt, nonce, salt_len);\n+\tmemcpy(iv, nonce + salt_len, iv_len);\n+\treturn true;\n+}\n+\n+/* In-kernel KeyUpdate: fd and cfd both register the TX and RX traffic\n+ * secrets of a bidirectional connection. fd -\u003e cfd starts at record\n+ * number @seq. Returns false if the variant does not support it.\n+ */\n+static bool keyupdate_setup_seq(struct __test_metadata *_metadata,\n+\t\t\t\tFIXTURE_DATA(tls) *self,\n+\t\t\t\tconst FIXTURE_VARIANT(tls) *variant,\n+\t\t\t\tuint64_t seq)\n+{\n+\tstruct tls_crypto_info_keys keys_a, keys_b;\n+\tunsigned char secret_a[48], secret_b[48];\n+\tunsigned char *rec_seq;\n+\tint len, i;\n+\n+\tif (self-\u003enotls || variant-\u003etls_version != TLS_1_3_VERSION)\n+\t\treturn false;\n+\n+\tswitch (variant-\u003ecipher_type) {\n+\tcase TLS_CIPHER_AES_GCM_128:\n+\tcase TLS_CIPHER_AES_CCM_128:\n+\tcase TLS_CIPHER_CHACHA20_POLY1305:\n+\t\tlen = 32;\n+\t\tbreak;\n+\tcase TLS_CIPHER_AES_GCM_256:\n+\t\tlen = 48;\n+\t\tbreak;\n+\tdefault:\n+\t\treturn false;\n+\t}\n+\n+\tmemset(secret_a, 0xa5, sizeof(secret_a));\n+\tmemset(secret_b, 0x5a, sizeof(secret_b));\n+\n+\t/* the keys must be the ones that the secrets give */\n+\tASSERT_TRUE(keyupdate_keys(variant-\u003ecipher_type, secret_a, len,\n+\t\t\t\t \u0026keys_a));\n+\tASSERT_TRUE(keyupdate_keys(variant-\u003ecipher_type, secret_b, len,\n+\t\t\t\t \u0026keys_b));\n+\n+\t/* rec_seq is the last member of each crypto_info */\n+\trec_seq = (unsigned char *)\u0026keys_a + keys_a.len - 8;\n+\tfor (i = 0; i \u003c 8; i++)\n+\t\trec_seq[i] = seq \u003e\u003e (56 - 8 * i);\n+\n+\t/* fd -\u003e cfd: replace the keys of the fixture */\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX, \u0026keys_a,\n+\t\t\t keys_a.len), 0);\n+\tEXPECT_EQ(setsockopt(self-\u003ecfd, SOL_TLS, TLS_RX, \u0026keys_a,\n+\t\t\t keys_a.len), 0);\n+\t/* cfd -\u003e fd */\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_RX, \u0026keys_b,\n+\t\t\t keys_b.len), 0);\n+\tEXPECT_EQ(setsockopt(self-\u003ecfd, SOL_TLS, TLS_TX, \u0026keys_b,\n+\t\t\t keys_b.len), 0);\n+\n+\t/* the RX secret needs the TX secret first */\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,\n+\t\t\t secret_b, len), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,\n+\t\t\t secret_a, len), 0);\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,\n+\t\t\t secret_b, len), 0);\n+\tEXPECT_EQ(setsockopt(self-\u003ecfd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,\n+\t\t\t secret_b, len), 0);\n+\tEXPECT_EQ(setsockopt(self-\u003ecfd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,\n+\t\t\t secret_a, len), 0);\n+\n+\treturn true;\n+}\n+\n+static bool keyupdate_setup(struct __test_metadata *_metadata,\n+\t\t\t FIXTURE_DATA(tls) *self,\n+\t\t\t const FIXTURE_VARIANT(tls) *variant)\n+{\n+\treturn keyupdate_setup_seq(_metadata, self, variant, 0);\n+}\n+\n+static void keyupdate_send(struct __test_metadata *_metadata, int fd,\n+\t\t\t int request)\n+{\n+\tEXPECT_EQ(setsockopt(fd, SOL_TLS, TLS_TX_KEY_UPDATE,\n+\t\t\t \u0026request, sizeof(request)), 0);\n+}\n+\n+static void send_recv_str(struct __test_metadata *_metadata, int from, int to,\n+\t\t\t const char *str)\n+{\n+\tint len = strlen(str) + 1;\n+\tchar buf[64];\n+\n+\tEXPECT_EQ(send(from, str, len, 0), len);\n+\tEXPECT_EQ(recv(to, buf, sizeof(buf), 0), len);\n+\tEXPECT_EQ(memcmp(buf, str, len), 0);\n+}\n+\n+TEST_F(tls, keyupdate_secret_errors)\n+{\n+\tunsigned char secret[48] = {};\n+\tint one = 1;\n+\n+\tif (self-\u003enotls)\n+\t\tSKIP(return, \"no TLS support\");\n+\n+\t/* no secret registered */\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_KEY_UPDATE,\n+\t\t\t \u0026one, sizeof(one)), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+\n+\tif (variant-\u003etls_version != TLS_1_3_VERSION) {\n+\t\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,\n+\t\t\t\t secret, 32), -1);\n+\t\tEXPECT_EQ(errno, EINVAL);\n+\t\treturn;\n+\t}\n+\n+\tif (variant-\u003ecipher_type == TLS_CIPHER_SM4_GCM ||\n+\t variant-\u003ecipher_type == TLS_CIPHER_SM4_CCM) {\n+\t\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,\n+\t\t\t\t secret, 32), -1);\n+\t\tEXPECT_EQ(errno, EOPNOTSUPP);\n+\t\treturn;\n+\t}\n+\n+\t/* the RX key is not installed on fd */\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,\n+\t\t\t secret, 32), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+\n+\t/* wrong length */\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,\n+\t\t\t secret, 16), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+\n+\t/* the installed key does not come from this secret */\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_TRAFFIC_SECRET, secret,\n+\t\t\t variant-\u003ecipher_type == TLS_CIPHER_AES_GCM_256 ?\n+\t\t\t 48 : 32), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+}\n+\n+TEST_F(tls, keyupdate)\n+{\n+\tint i;\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\tsend_recv_str(_metadata, self-\u003efd, self-\u003ecfd, \"before\");\n+\n+\t/* stay below the limit of 8 received KeyUpdates per second */\n+\tfor (i = 0; i \u003c 4; i++) {\n+\t\tkeyupdate_send(_metadata, self-\u003efd, 0);\n+\t\tsend_recv_str(_metadata, self-\u003efd, self-\u003ecfd, \"after\");\n+\t\tkeyupdate_send(_metadata, self-\u003ecfd, 0);\n+\t\tsend_recv_str(_metadata, self-\u003ecfd, self-\u003efd, \"reverse\");\n+\t}\n+}\n+\n+TEST_F(tls, keyupdate_queued)\n+{\n+\tconst char *str = \"queued behind a KeyUpdate\";\n+\tint len = strlen(str) + 1;\n+\tchar buf[64];\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\t/* the record before the KeyUpdate, the KeyUpdate and the record\n+\t * after it are all queued before the first read\n+\t */\n+\tEXPECT_EQ(send(self-\u003efd, str, len, MSG_MORE), len);\n+\tkeyupdate_send(_metadata, self-\u003efd, 0);\n+\tEXPECT_EQ(send(self-\u003efd, str, len, 0), len);\n+\n+\tEXPECT_EQ(recv(self-\u003ecfd, buf, len, MSG_WAITALL), len);\n+\tEXPECT_EQ(memcmp(buf, str, len), 0);\n+\tEXPECT_EQ(recv(self-\u003ecfd, buf, len, MSG_WAITALL), len);\n+\tEXPECT_EQ(memcmp(buf, str, len), 0);\n+}\n+\n+TEST_F(tls, keyupdate_request)\n+{\n+\tint one = 1;\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\tkeyupdate_send(_metadata, self-\u003efd, 1);\n+\n+\t/* no answer yet */\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_KEY_UPDATE,\n+\t\t\t \u0026one, sizeof(one)), -1);\n+\tEXPECT_EQ(errno, EBUSY);\n+\n+\t/* cfd processes the request, then answers ahead of its data */\n+\tsend_recv_str(_metadata, self-\u003efd, self-\u003ecfd, \"request\");\n+\tsend_recv_str(_metadata, self-\u003ecfd, self-\u003efd, \"answer\");\n+\n+\t/* answered */\n+\tkeyupdate_send(_metadata, self-\u003efd, 1);\n+\tsend_recv_str(_metadata, self-\u003efd, self-\u003ecfd, \"request\");\n+\tsend_recv_str(_metadata, self-\u003ecfd, self-\u003efd, \"answer\");\n+}\n+\n+TEST_F(tls, keyupdate_splice)\n+{\n+\tconst char *str = \"spliced after a KeyUpdate\";\n+\tint len = strlen(str) + 1;\n+\tchar buf[64];\n+\tint p[2];\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\tASSERT_GE(pipe(p), 0);\n+\n+\tkeyupdate_send(_metadata, self-\u003efd, 0);\n+\tEXPECT_EQ(send(self-\u003efd, str, len, 0), len);\n+\n+\tEXPECT_EQ(splice(self-\u003ecfd, NULL, p[1], NULL, len, 0), len);\n+\tEXPECT_EQ(read(p[0], buf, len), len);\n+\tEXPECT_EQ(memcmp(buf, str, len), 0);\n+\n+\tclose(p[0]);\n+\tclose(p[1]);\n+}\n+\n+TEST_F(tls, keyupdate_manual_rekey)\n+{\n+\tstruct tls_crypto_info_keys tls12;\n+\tint zero = 0;\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\t/* a key installed by the user removes the registered secrets */\n+\ttls_crypto_info_init(variant-\u003etls_version, variant-\u003ecipher_type,\n+\t\t\t \u0026tls12, 2);\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX, \u0026tls12, tls12.len), 0);\n+\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_KEY_UPDATE,\n+\t\t\t \u0026zero, sizeof(zero)), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+}\n+\n+TEST_F(tls, keyupdate_manual_rekey_request)\n+{\n+\tstruct tls_crypto_info_keys keys;\n+\tunsigned char secret[48];\n+\tint len;\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\tkeyupdate_send(_metadata, self-\u003efd, 1);\n+\n+\t/* the answer to the request now goes to user space */\n+\tlen = variant-\u003ecipher_type == TLS_CIPHER_AES_GCM_256 ? 48 : 32;\n+\tmemset(secret, 0x3c, sizeof(secret));\n+\tASSERT_TRUE(keyupdate_keys(variant-\u003ecipher_type, secret, len, \u0026keys));\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_RX, \u0026keys, keys.len), 0);\n+\n+\tEXPECT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,\n+\t\t\t secret, len), 0);\n+\n+\tkeyupdate_send(_metadata, self-\u003efd, 1);\n+}\n+\n+TEST_F(tls, keyupdate_nosignal)\n+{\n+\tint zero = 0;\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\t/* The default action of SIGPIPE ends the test. The result is not\n+\t * checked: as for sendmsg(), a send error may not reach the caller.\n+\t */\n+\tEXPECT_EQ(shutdown(self-\u003efd, SHUT_WR), 0);\n+\tsetsockopt(self-\u003efd, SOL_TLS, TLS_TX_KEY_UPDATE, \u0026zero, sizeof(zero));\n+}\n+\n+TEST_F(tls, keyupdate_rate_limit)\n+{\n+\tchar buf[64];\n+\tint i;\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\t/* the ninth KeyUpdate in one second ends the connection */\n+\tfor (i = 0; i \u003c 9; i++)\n+\t\tkeyupdate_send(_metadata, self-\u003efd, 0);\n+\tEXPECT_EQ(send(self-\u003efd, \"x\", 1, 0), 1);\n+\n+\tEXPECT_EQ(recv(self-\u003ecfd, buf, sizeof(buf), 0), -1);\n+\tEXPECT_EQ(errno, EBADMSG);\n+}\n+\n+TEST_F(tls, keyupdate_record_limit)\n+{\n+\tstruct tls_crypto_info_keys get;\n+\tchar sbuf[1024], rbuf[1024];\n+\tsocklen_t optlen;\n+\tunsigned char *rec_seq;\n+\t__u16 limit = 128;\n+\tint i;\n+\n+\tif (variant-\u003ecipher_type == TLS_CIPHER_CHACHA20_POLY1305)\n+\t\tSKIP(return, \"no record limit for ChaCha20-Poly1305\");\n+\tif (!keyupdate_setup_seq(_metadata, self, variant, (1 \u003c\u003c 24) - 2))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\tASSERT_EQ(setsockopt(self-\u003efd, SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN,\n+\t\t\t \u0026limit, sizeof(limit)), 0);\n+\tfor (i = 0; i \u003c sizeof(sbuf); i++)\n+\t\tsbuf[i] = i;\n+\n+\t/* one call writes 8 records or more, past the record limit */\n+\tEXPECT_EQ(send(self-\u003efd, sbuf, sizeof(sbuf), 0), sizeof(sbuf));\n+\tEXPECT_EQ(recv(self-\u003ecfd, rbuf, sizeof(rbuf), MSG_WAITALL),\n+\t\t sizeof(rbuf));\n+\tEXPECT_EQ(memcmp(sbuf, rbuf, sizeof(sbuf)), 0);\n+\n+\t/* the KeyUpdate in the middle of the call restarted at 0 */\n+\ttls_crypto_info_init(variant-\u003etls_version, variant-\u003ecipher_type,\n+\t\t\t \u0026get, 0);\n+\toptlen = get.len;\n+\tASSERT_EQ(getsockopt(self-\u003efd, SOL_TLS, TLS_TX, \u0026get, \u0026optlen), 0);\n+\trec_seq = (unsigned char *)\u0026get + optlen - 8;\n+\tfor (i = 0; i \u003c 5; i++)\n+\t\tEXPECT_EQ(rec_seq[i], 0);\n+}\n+\n+/* A send that fails with EAGAIN before it copies any data leaves an\n+ * empty open record. The next send must still answer a KeyUpdate\n+ * request before its data.\n+ */\n+TEST_F(tls, keyupdate_request_after_eagain)\n+{\n+\tstruct tls_crypto_info_keys get;\n+\tsize_t sent = 0, rcvd = 0;\n+\tchar buf[16384];\n+\tunsigned char *rec_seq;\n+\tsocklen_t optlen;\n+\tint size = 4096;\n+\tssize_t ret;\n+\tint i;\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\tASSERT_EQ(setsockopt(self-\u003efd, SOL_SOCKET, SO_SNDBUF, \u0026size,\n+\t\t\t sizeof(size)), 0);\n+\tASSERT_EQ(setsockopt(self-\u003ecfd, SOL_SOCKET, SO_RCVBUF, \u0026size,\n+\t\t\t sizeof(size)), 0);\n+\tmemset(buf, 0x42, sizeof(buf));\n+\n+\t/* fill the send buffer until a send copies nothing */\n+\tfor (;;) {\n+\t\tret = send(self-\u003efd, buf, sizeof(buf), MSG_DONTWAIT);\n+\t\tif (ret \u003c 0)\n+\t\t\tbreak;\n+\t\tsent += ret;\n+\t}\n+\tASSERT_EQ(errno, EAGAIN);\n+\n+\t/* fd reads the request; its answer is now due */\n+\tkeyupdate_send(_metadata, self-\u003ecfd, 1);\n+\tEXPECT_EQ(recv(self-\u003efd, buf, sizeof(buf), MSG_DONTWAIT), -1);\n+\tEXPECT_EQ(errno, EAGAIN);\n+\n+\twhile (rcvd \u003c sent) {\n+\t\tret = recv(self-\u003ecfd, buf, sizeof(buf), 0);\n+\t\tASSERT_GT(ret, 0);\n+\t\trcvd += ret;\n+\t}\n+\n+\t/* the answer goes out before this data */\n+\tsend_recv_str(_metadata, self-\u003efd, self-\u003ecfd, \"data\");\n+\n+\ttls_crypto_info_init(variant-\u003etls_version, variant-\u003ecipher_type,\n+\t\t\t \u0026get, 0);\n+\toptlen = get.len;\n+\tASSERT_EQ(getsockopt(self-\u003efd, SOL_TLS, TLS_TX, \u0026get, \u0026optlen), 0);\n+\trec_seq = (unsigned char *)\u0026get + optlen - 8;\n+\tfor (i = 0; i \u003c 7; i++)\n+\t\tEXPECT_EQ(rec_seq[i], 0);\n+\tEXPECT_EQ(rec_seq[7], 1);\n+\n+\t/* cfd got the answer, so it can ask again */\n+\tkeyupdate_send(_metadata, self-\u003ecfd, 1);\n+}\n+\n+TEST_F(tls, keyupdate_invalid)\n+{\n+\t/* key_update, length 1, request_update 2 */\n+\tstatic const char bad[] = \"\\x18\\x00\\x00\\x01\\x02\";\n+\tchar buf[64];\n+\n+\tif (!keyupdate_setup(_metadata, self, variant))\n+\t\tSKIP(return, \"no in-kernel KeyUpdate for this variant\");\n+\n+\tEXPECT_EQ(tls_send_cmsg(self-\u003efd, TLS_RECORD_TYPE_HANDSHAKE,\n+\t\t\t\t(char *)bad, 5, 0), 5);\n+\n+\tEXPECT_EQ(recv(self-\u003ecfd, buf, sizeof(buf), 0), -1);\n+\tEXPECT_EQ(errno, EBADMSG);\n+}\n+\n struct raw_rec {\n \tunsigned int plain_len;\n \tunsigned char plain_data[100];\n"
} |
| 2/1 | 2026/11/08 13:37 | agent | patch-evaluator |
1mModel:core Args: null Results: {
"EnableConfigs": [
"TLS",
"INET"
],
"FocusSymbols": [
"tls_hkdf_expand_label",
"tls_keyupdate_rekey",
"tls_keyupdate_rx",
"tls_sw_key_update",
"tls_tx_key_update",
"tls_tx_maybe_update_key",
"do_tls_setsockopt_secret",
"do_tls_setsockopt_key_update",
"tls_check_traffic_secret",
"tls_check_pending_rekey"
],
"Reasoning": "The patch series implements in-kernel TLS 1.3 KeyUpdate support in kTLS (net/tls). It introduces new setsockopt operations (TLS_TX_TRAFFIC_SECRET, TLS_RX_TRAFFIC_SECRET, TLS_TX_KEY_UPDATE), cryptographic key derivation routines using HKDF-Expand-Label (RFC 9846), automatic rekeying logic on TX and RX, and rate-limiting / validation state machines. These changes operate in a core networking subsystem accessible to user space via standard sockets and deserve focused fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 72afd7682f8c161f815cd82a531d5464e63bdf6e
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 8 13:37:35 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/networking/tls.rst b/Documentation/networking/tls.rst
index 980c442d7161ae..69752a6d67df47 100644
--- a/Documentation/networking/tls.rst
+++ b/Documentation/networking/tls.rst
@@ -300,6 +300,49 @@ extra byte used by the ContentType field.
[1] https://datatracker.ietf.org/doc/html/rfc8449
+TLS_TX_TRAFFIC_SECRET and TLS_RX_TRAFFIC_SECRET
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+``TLS_TX_TRAFFIC_SECRET`` registers the TLS 1.3 application traffic secret
+of the transmit direction, ``TLS_RX_TRAFFIC_SECRET`` that of the receive
+direction. With a registered secret, the kernel performs KeyUpdate for that
+direction itself (RFC 9846, section 4.7.3). The value is the secret itself.
+TLS 1.3 only, software path only.
+
+Install the key of the direction first, with the ``TLS_TX`` or ``TLS_RX``
+socket option described above. The cipher of that key selects the hash, and
+the secret must have the length of that hash: 32 bytes for SHA-256
+(AES-128-GCM, AES-128-CCM, ChaCha20-Poly1305) or 48 bytes for SHA-384
+(AES-256-GCM). Other ciphers return ``EOPNOTSUPP``.
+
+Each secret can be registered once. ``TLS_RX_TRAFFIC_SECRET`` needs
+``TLS_TX_TRAFFIC_SECRET`` first: a received KeyUpdate can ask for one in
+reply, and only the kernel sees it. ``TLS_TX_TRAFFIC_SECRET`` alone is
+allowed.
+
+If the application installs a new key itself with ``TLS_TX``, both secrets
+are removed. With ``TLS_RX``, the RX secret is removed. KeyUpdate for that
+direction is then left to the application again.
+
+The secrets cannot be read back.
+
+With a registered TX secret, the kernel sends a KeyUpdate before the key
+reaches its record limit (RFC 9846, section 5.5): after 2^24 records, except
+for ChaCha20-Poly1305, which has no limit. It also sends one before the next
+record when a received KeyUpdate asks for it. With a registered RX secret,
+the kernel processes a received KeyUpdate and does not pass the record to
+the application. A peer that sends more than 8 KeyUpdates in one second ends
+the connection.
+
+TLS_TX_KEY_UPDATE
+~~~~~~~~~~~~~~~~~
+
+TLS 1.3 only. Send a KeyUpdate now and install the next TX key. Needs a
+registered TX secret. The value is an ``int``: 0 to send a KeyUpdate, 1 to
+also ask the peer to update its key. Asking the peer needs a registered RX
+secret, and fails with ``EBUSY`` until the peer has answered the previous
+request.
+
Statistics
==========
diff --git a/include/net/tls.h b/include/net/tls.h
index e57bef58851eac..d3123b17030cfc 100644
--- a/include/net/tls.h
+++ b/include/net/tls.h
@@ -63,8 +63,16 @@ struct tls_rec;
#define TLS_HANDSHAKE_KEYUPDATE 24 /* rfc8446 B.3: Key update */
+/* KeyUpdate: msg_type, 24-bit length, request_update (RFC 9846 4.7.3) */
+#define TLS_KEYUPDATE_MSG_SIZE 5
+#define TLS_KEY_UPDATE_NOT_REQUESTED 0
+#define TLS_KEY_UPDATE_REQUESTED 1
+
#define TLS_AAD_SPACE_SIZE 13
+/* SHA-384, the longest hash of a TLS 1.3 cipher suite kTLS supports */
+#define TLS_MAX_TRAFFIC_SECRET_SIZE 48
+
#define TLS_MAX_IV_SIZE 16
#define TLS_MAX_SALT_SIZE 4
#define TLS_TAG_SIZE 16
@@ -139,6 +147,8 @@ struct tls_sw_context_rx {
u8 zc_capable:1;
u8 reader_contended:1;
bool key_update_pending;
+ u8 key_update_count; /* received in this window */
+ unsigned long key_update_window; /* jiffies */
struct tls_strparser strp;
@@ -224,6 +234,12 @@ struct tls_prot_info {
u16 tail_size;
};
+/* A TLS 1.3 application traffic secret. len is 0 if none is registered. */
+struct tls_traffic_secret {
+ u8 data[TLS_MAX_TRAFFIC_SECRET_SIZE];
+ u8 len;
+};
+
struct tls_context {
/* read-only cache line */
struct tls_prot_info prot_info;
@@ -266,6 +282,13 @@ struct tls_context {
union tls_crypto_context crypto_send;
union tls_crypto_context crypto_recv;
+ /* KeyUpdate state, protected by the socket lock */
+ struct tls_traffic_secret tx_secret;
+ struct tls_traffic_secret rx_secret;
+ u64 tx_key_updates; /* KeyUpdates sent */
+ bool tx_key_update_reply; /* peer set update_requested */
+ bool tx_key_update_asked; /* we set it, no answer yet */
+
struct list_head list;
refcount_t refcount;
struct rcu_head rcu;
diff --git a/include/uapi/linux/tls.h b/include/uapi/linux/tls.h
index 1245ab38afc144..c85408cb6ce76f 100644
--- a/include/uapi/linux/tls.h
+++ b/include/uapi/linux/tls.h
@@ -42,6 +42,9 @@
#define TLS_TX_ZEROCOPY_RO 3 /* TX zerocopy (only sendfile now) */
#define TLS_RX_EXPECT_NO_PAD 4 /* Attempt opportunistic zero-copy */
#define TLS_TX_MAX_PAYLOAD_LEN 5 /* Maximum plaintext size */
+#define TLS_TX_TRAFFIC_SECRET 6 /* TLS 1.3 TX traffic secret */
+#define TLS_RX_TRAFFIC_SECRET 7 /* TLS 1.3 RX traffic secret */
+#define TLS_TX_KEY_UPDATE 8 /* Send a TLS 1.3 KeyUpdate now */
/* Supported versions */
#define TLS_VERSION_MINOR(ver) ((ver) & 0xFF)
diff --git a/net/tls/Kconfig b/net/tls/Kconfig
index 4f4d5973a28f49..6183ebe954ee83 100644
--- a/net/tls/Kconfig
+++ b/net/tls/Kconfig
@@ -8,6 +8,8 @@ config TLS
select CRYPTO
select CRYPTO_AES
select CRYPTO_GCM
+ select CRYPTO_LIB_SHA256
+ select CRYPTO_LIB_SHA512
select NET_SOCK_MSG
default n
help
diff --git a/net/tls/Makefile b/net/tls/Makefile
index 4c7d296081ee66..ce8ac6a59f8ced 100644
--- a/net/tls/Makefile
+++ b/net/tls/Makefile
@@ -7,6 +7,7 @@ CFLAGS_trace.o := -I$(src)
obj-$(CONFIG_TLS) += tls.o
-tls-y := tls_main.o tls_sw.o tls_proc.o trace.o tls_strp.o
+tls-y := tls_main.o tls_sw.o tls_proc.o trace.o tls_strp.o tls_hkdf.o \
+ tls_keyupdate.o
tls-$(CONFIG_TLS_DEVICE) += tls_device.o tls_device_fallback.o
diff --git a/net/tls/tls.h b/net/tls/tls.h
index 60a37bdaaa2506..d12c9278c8143a 100644
--- a/net/tls/tls.h
+++ b/net/tls/tls.h
@@ -147,6 +147,11 @@ void tls_strp_abort_strp(struct tls_strparser *strp, int err);
int init_prot_info(struct tls_prot_info *prot,
const struct tls_crypto_info *crypto_info,
const struct tls_cipher_desc *cipher_desc);
+int tls_hkdf_expand_label(const u8 *secret, size_t secret_len,
+ const char *label, u8 *out, size_t out_len);
+int tls_keyupdate_rekey(struct sock *sk, int tx);
+int tls_keyupdate_rx(struct sock *sk, struct sk_buff *skb);
+int tls_sw_key_update(struct sock *sk, int request);
int tls_set_sw_offload(struct sock *sk, int tx,
struct tls_crypto_info *new_crypto_info);
void tls_update_rx_zc_capable(struct tls_context *tls_ctx);
diff --git a/net/tls/tls_hkdf.c b/net/tls/tls_hkdf.c
new file mode 100644
index 00000000000000..49e33146ac7ef2
--- /dev/null
+++ b/net/tls/tls_hkdf.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries.
+ *
+ * TLS 1.3 HKDF-Expand-Label, RFC 9846 section 7.1.
+ *
+ * TLS 1.3 derives all of its secrets and keys with HKDF (RFC 5869). The
+ * TLS handshake runs in user space and gives the kernel one application
+ * traffic secret for each direction. For a KeyUpdate, the kernel derives
+ * the next traffic secret from the current one (section 7.2), and the
+ * new key and IV from the next traffic secret (section 7.3). Both steps
+ * use only HKDF-Expand-Label, so this file implements only that
+ * function, not the full key schedule of the handshake.
+ *
+ * The input is always a traffic secret. It is already a random key of
+ * the hash length, so HKDF-Extract is not needed. No output is longer
+ * than the hash, so HKDF-Expand is a single HMAC.
+ */
+
+#include <crypto/sha2.h>
+#include <linux/errno.h>
+#include <linux/string.h>
+#include <linux/unaligned.h>
+
+#include "tls.h"
+
+#define TLS_HKDF_LABEL_PREFIX "tls13 "
+#define TLS_HKDF_LABEL_PREFIX_LEN (sizeof(TLS_HKDF_LABEL_PREFIX) - 1)
+#define TLS_HKDF_LABEL_MAX 32
+
+/**
+ * tls_hkdf_expand_label - HKDF-Expand-Label with an empty context
+ * @secret: the secret
+ * @secret_len: length of @secret, which selects the hash: SHA-256 for
+ * SHA256_DIGEST_SIZE, SHA-384 for SHA384_DIGEST_SIZE
+ * @label: the label, without the "tls13 " prefix
+ * @out: output buffer
+ * @out_len: number of bytes to derive, at most @secret_len
+ *
+ * Return: 0 on success, -EINVAL if a length is not valid.
+ */
+int tls_hkdf_expand_label(const u8 *secret, size_t secret_len,
+ const char *label, u8 *out, size_t out_len)
+{
+ u8 info[2 + 1 + TLS_HKDF_LABEL_PREFIX_LEN + TLS_HKDF_LABEL_MAX + 1 + 1];
+ size_t label_len = strlen(label);
+ u8 tmp[SHA384_DIGEST_SIZE];
+ size_t pos = 0;
+
+ if (secret_len != SHA256_DIGEST_SIZE &&
+ secret_len != SHA384_DIGEST_SIZE)
+ return -EINVAL;
+ if (!out_len || out_len > secret_len || label_len > TLS_HKDF_LABEL_MAX)
+ return -EINVAL;
+
+ /* struct {
+ * uint16 length;
+ * opaque label<7..255>;
+ * opaque context<0..255>;
+ * } HkdfLabel;
+ *
+ * followed by the HKDF-Expand counter for the first output block.
+ */
+ put_unaligned_be16(out_len, info);
+ pos += 2;
+ info[pos++] = TLS_HKDF_LABEL_PREFIX_LEN + label_len;
+ memcpy(info + pos, TLS_HKDF_LABEL_PREFIX, TLS_HKDF_LABEL_PREFIX_LEN);
+ pos += TLS_HKDF_LABEL_PREFIX_LEN;
+ memcpy(info + pos, label, label_len);
+ pos += label_len;
+ info[pos++] = 0; /* empty context */
+ info[pos++] = 1; /* counter */
+
+ if (secret_len == SHA256_DIGEST_SIZE)
+ hmac_sha256_usingrawkey(secret, secret_len, info, pos, tmp);
+ else
+ hmac_sha384_usingrawkey(secret, secret_len, info, pos, tmp);
+
+ memcpy(out, tmp, out_len);
+ memzero_explicit(tmp, sizeof(tmp));
+
+ return 0;
+}
diff --git a/net/tls/tls_keyupdate.c b/net/tls/tls_keyupdate.c
new file mode 100644
index 00000000000000..b43874b0839353
--- /dev/null
+++ b/net/tls/tls_keyupdate.c
@@ -0,0 +1,160 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries.
+ *
+ * TLS 1.3 KeyUpdate, RFC 9846 sections 4.7.3 and 7.2.
+ *
+ * A TLS 1.3 peer can change the key of the direction it sends in at any
+ * time. It sends a KeyUpdate message, then encrypts all later records
+ * with the next key. A sender must also change its key before the key
+ * has protected too many records (section 5.5).
+ *
+ * The next key is derived from the application traffic secret, not from
+ * the current key. User space gives the kernel only the key and IV, so
+ * the kernel cannot derive the next key on its own. Without the secret,
+ * a received KeyUpdate goes to the application, and the kernel stops
+ * decrypting until the application installs the next key with TLS_RX.
+ *
+ * With TLS_TX_TRAFFIC_SECRET or TLS_RX_TRAFFIC_SECRET, user space also
+ * gives the kernel the secret of that direction. The kernel then handles
+ * KeyUpdate itself. This file derives and installs the next key, and
+ * processes a received KeyUpdate. tls_sw.c sends KeyUpdate messages.
+ */
+
+#include <linux/jiffies.h>
+#include <linux/skbuff.h>
+#include <net/strparser.h>
+#include <net/tls.h>
+
+#include "tls.h"
+
+/* A peer that sends more KeyUpdates than this in one second ends the
+ * connection. Each one costs three HMACs and a new key, and carries no
+ * data.
+ */
+#define TLS_KEYUPDATE_RX_MAX_PER_SEC 8
+
+/**
+ * tls_keyupdate_rekey - install the next key of one direction
+ * @sk: socket
+ * @tx: non-zero for the transmit direction
+ *
+ * Derive the next application traffic secret from the registered one,
+ * then its key and IV, and install them. The record sequence number
+ * restarts at zero.
+ *
+ * Return: 0 on success, or a negative errno. On error the installed key
+ * and the registered secret do not change.
+ */
+int tls_keyupdate_rekey(struct sock *sk, int tx)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ struct tls_traffic_secret *secret;
+ const struct tls_cipher_desc *cipher_desc;
+ u8 iv[TLS_MAX_SALT_SIZE + TLS_MAX_IV_SIZE];
+ u8 next[TLS_MAX_TRAFFIC_SECRET_SIZE];
+ union tls_crypto_context tmp;
+ struct tls_crypto_info *info = (void *)&tmp;
+ int rc;
+
+ secret = tx ? &ctx->tx_secret : &ctx->rx_secret;
+ cipher_desc = get_cipher_desc(ctx->prot_info.cipher_type);
+ if (!cipher_desc || !secret->len ||
+ cipher_desc->salt + cipher_desc->iv > sizeof(iv))
+ return -EINVAL;
+
+ /* version and cipher_type; the rest of tmp stays zero */
+ memset(&tmp, 0, sizeof(tmp));
+ *info = tx ? ctx->crypto_send.info : ctx->crypto_recv.info;
+
+ rc = tls_hkdf_expand_label(secret->data, secret->len, "traffic upd",
+ next, secret->len);
+ if (rc)
+ goto out;
+
+ rc = tls_hkdf_expand_label(next, secret->len, "key",
+ crypto_info_key(info, cipher_desc),
+ cipher_desc->key);
+ if (rc)
+ goto out;
+
+ rc = tls_hkdf_expand_label(next, secret->len, "iv", iv,
+ cipher_desc->salt + cipher_desc->iv);
+ if (rc)
+ goto out;
+
+ memcpy(crypto_info_salt(info, cipher_desc), iv, cipher_desc->salt);
+ memcpy(crypto_info_iv(info, cipher_desc), iv + cipher_desc->salt,
+ cipher_desc->iv);
+
+ rc = tls_set_sw_offload(sk, tx, info);
+ if (rc)
+ goto out;
+
+ memcpy(secret->data, next, secret->len);
+
+out:
+ memzero_explicit(next, sizeof(next));
+ memzero_explicit(iv, sizeof(iv));
+ memzero_explicit(&tmp, sizeof(tmp));
+
+ return rc;
+}
+
+/**
+ * tls_keyupdate_rx - process a received KeyUpdate
+ * @sk: socket
+ * @skb: the decrypted record, which starts with a KeyUpdate message
+ *
+ * Called with the socket locked, for a direction with a registered
+ * secret. The caller does not pass the record to the user.
+ *
+ * Return: 0 on success, or a negative errno if the connection must end.
+ */
+int tls_keyupdate_rx(struct sock *sk, struct sk_buff *skb)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);
+ const struct strp_msg *rxm = strp_msg(skb);
+ u8 msg[TLS_KEYUPDATE_MSG_SIZE];
+ int rc;
+
+ /* A KeyUpdate must end its record (section 5.1). Another message
+ * in the same record is not supported.
+ */
+ if (rxm->full_len != sizeof(msg) ||
+ skb_copy_bits(skb, rxm->offset, msg, sizeof(msg)))
+ return -EBADMSG;
+
+ /* msg_type, a 24-bit length of 1, then request_update */
+ if (msg[1] || msg[2] || msg[3] != 1)
+ return -EBADMSG;
+ if (msg[4] != TLS_KEY_UPDATE_NOT_REQUESTED &&
+ msg[4] != TLS_KEY_UPDATE_REQUESTED)
+ return -EBADMSG;
+
+ if (!rx_ctx->key_update_count ||
+ time_after(jiffies, rx_ctx->key_update_window + HZ)) {
+ rx_ctx->key_update_window = jiffies;
+ rx_ctx->key_update_count = 0;
+ }
+ if (++rx_ctx->key_update_count > TLS_KEYUPDATE_RX_MAX_PER_SEC) {
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYERROR);
+ net_warn_ratelimited("tls: peer sent more than %d KeyUpdates in one second\n",
+ TLS_KEYUPDATE_RX_MAX_PER_SEC);
+ return -EBADMSG;
+ }
+
+ rc = tls_keyupdate_rekey(sk, 0);
+ if (rc) {
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYERROR);
+ return rc;
+ }
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYOK);
+
+ /* Any KeyUpdate from the peer answers our update_requested */
+ ctx->tx_key_update_asked = false;
+ if (msg[4] == TLS_KEY_UPDATE_REQUESTED)
+ ctx->tx_key_update_reply = true;
+
+ return 0;
+}
diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index b8d5a843f76171..b19e6946dbaed9 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c
@@ -31,6 +31,8 @@
* SOFTWARE.
*/
+#include <crypto/sha2.h>
+#include <crypto/utils.h>
#include <linux/module.h>
#include <net/tcp.h>
@@ -325,6 +327,8 @@ void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
+ memzero_explicit(&ctx->tx_secret, sizeof(ctx->tx_secret));
+ memzero_explicit(&ctx->rx_secret, sizeof(ctx->rx_secret));
mutex_destroy(&ctx->tx_lock);
if (sk)
@@ -757,8 +761,20 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
ctx->rx_conf = conf;
update_sk_prot(sk, ctx);
- if (update)
+ if (update) {
+ /* The registered secret no longer matches the key. The RX
+ * secret also needs the TX one, see do_tls_setsockopt_secret().
+ */
+ if (tx) {
+ memzero_explicit(&ctx->tx_secret,
+ sizeof(ctx->tx_secret));
+ ctx->tx_key_update_reply = false;
+ }
+ memzero_explicit(&ctx->rx_secret, sizeof(ctx->rx_secret));
+ /* the answer now goes to user space */
+ ctx->tx_key_update_asked = false;
return 0;
+ }
if (tx) {
ctx->sk_write_space = sk->sk_write_space;
@@ -868,6 +884,159 @@ static int do_tls_setsockopt_tx_payload_len(struct sock *sk, sockptr_t optval,
return 0;
}
+/* Length of the TLS 1.3 traffic secret for @cipher_type, the size of
+ * its hash. 0 if KeyUpdate is not supported for the cipher.
+ */
+static int tls_traffic_secret_len(u16 cipher_type)
+{
+ switch (cipher_type) {
+ case TLS_CIPHER_AES_GCM_128:
+ case TLS_CIPHER_AES_CCM_128:
+ case TLS_CIPHER_CHACHA20_POLY1305:
+ return SHA256_DIGEST_SIZE;
+ case TLS_CIPHER_AES_GCM_256:
+ return SHA384_DIGEST_SIZE;
+ default:
+ return 0;
+ }
+}
+
+/* The installed key and IV must be the ones that @secret gives
+ * (RFC 9846 section 7.3):
+ *
+ * key = HKDF-Expand-Label(secret, "key", "", key_length)
+ * iv = HKDF-Expand-Label(secret, "iv", "", iv_length)
+ *
+ * crypto_info holds the iv as salt followed by iv. Each KeyUpdate
+ * derives the next key from the secret, so a wrong secret fails only
+ * at the first KeyUpdate, with no clear cause.
+ */
+static int tls_check_traffic_secret(struct tls_crypto_info *crypto_info,
+ const u8 *secret, int len)
+{
+ const struct tls_cipher_desc *cipher_desc;
+ u8 iv[TLS_MAX_SALT_SIZE + TLS_MAX_IV_SIZE];
+ u8 key[TLS_CIPHER_AES_GCM_256_KEY_SIZE];
+ int rc;
+
+ cipher_desc = get_cipher_desc(crypto_info->cipher_type);
+ if (!cipher_desc || cipher_desc->key > sizeof(key) ||
+ cipher_desc->salt + cipher_desc->iv > sizeof(iv))
+ return -EINVAL;
+
+ rc = tls_hkdf_expand_label(secret, len, "key", key, cipher_desc->key);
+ if (rc)
+ goto out;
+
+ rc = tls_hkdf_expand_label(secret, len, "iv", iv,
+ cipher_desc->salt + cipher_desc->iv);
+ if (rc)
+ goto out;
+
+ if (crypto_memneq(key, crypto_info_key(crypto_info, cipher_desc),
+ cipher_desc->key) ||
+ crypto_memneq(iv, crypto_info_salt(crypto_info, cipher_desc),
+ cipher_desc->salt) ||
+ crypto_memneq(iv + cipher_desc->salt,
+ crypto_info_iv(crypto_info, cipher_desc),
+ cipher_desc->iv))
+ rc = -EINVAL;
+
+out:
+ memzero_explicit(key, sizeof(key));
+ memzero_explicit(iv, sizeof(iv));
+
+ return rc;
+}
+
+static int do_tls_setsockopt_secret(struct sock *sk, sockptr_t optval,
+ unsigned int optlen, int tx)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ struct tls_crypto_info *crypto_info;
+ struct tls_traffic_secret *secret;
+ u8 data[TLS_MAX_TRAFFIC_SECRET_SIZE];
+ int len, rc;
+
+ if (tx) {
+ crypto_info = &ctx->crypto_send.info;
+ secret = &ctx->tx_secret;
+ } else {
+ crypto_info = &ctx->crypto_recv.info;
+ secret = &ctx->rx_secret;
+ }
+
+ /* The key must be installed first: its cipher selects the hash */
+ if (!TLS_CRYPTO_INFO_READY(crypto_info) ||
+ crypto_info->version != TLS_1_3_VERSION)
+ return -EINVAL;
+
+ if ((tx ? ctx->tx_conf : ctx->rx_conf) != TLS_SW)
+ return -EOPNOTSUPP;
+
+ len = tls_traffic_secret_len(crypto_info->cipher_type);
+ if (!len)
+ return -EOPNOTSUPP;
+
+ if (sockptr_is_null(optval) || optlen != len)
+ return -EINVAL;
+
+ /* A received KeyUpdate may ask for one in reply. The kernel
+ * consumes the record, so only the kernel can send the reply.
+ */
+ if (!tx && !ctx->tx_secret.len)
+ return -EINVAL;
+
+ if (secret->len)
+ return -EBUSY;
+
+ if (copy_from_sockptr(data, optval, len)) {
+ rc = -EFAULT;
+ goto out;
+ }
+
+ rc = tls_check_traffic_secret(crypto_info, data, len);
+ if (rc)
+ goto out;
+
+ memcpy(secret->data, data, len);
+ secret->len = len;
+
+out:
+ memzero_explicit(data, sizeof(data));
+
+ return rc;
+}
+
+static int do_tls_setsockopt_key_update(struct sock *sk, sockptr_t optval,
+ unsigned int optlen)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ int value, rc;
+
+ if (sockptr_is_null(optval) || optlen != sizeof(value))
+ return -EINVAL;
+
+ if (copy_from_sockptr(&value, optval, sizeof(value)))
+ return -EFAULT;
+
+ if (value != 0 && value != 1)
+ return -EINVAL;
+
+ rc = mutex_lock_interruptible(&ctx->tx_lock);
+ if (rc)
+ return rc;
+ lock_sock(sk);
+ if (ctx->tx_conf == TLS_SW)
+ rc = tls_sw_key_update(sk, value);
+ else
+ rc = -EINVAL;
+ release_sock(sk);
+ mutex_unlock(&ctx->tx_lock);
+
+ return rc;
+}
+
static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
unsigned int optlen)
{
@@ -901,6 +1070,16 @@ static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
release_sock(sk);
mutex_unlock(&ctx->tx_lock);
break;
+ case TLS_TX_TRAFFIC_SECRET:
+ case TLS_RX_TRAFFIC_SECRET:
+ lock_sock(sk);
+ rc = do_tls_setsockopt_secret(sk, optval, optlen,
+ optname == TLS_TX_TRAFFIC_SECRET);
+ release_sock(sk);
+ break;
+ case TLS_TX_KEY_UPDATE:
+ rc = do_tls_setsockopt_key_update(sk, optval, optlen);
+ break;
default:
rc = -ENOPROTOOPT;
break;
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 771e02bff51999..433a43d13a8729 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -40,6 +40,7 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/splice.h>
+#include <linux/unaligned.h>
#include <crypto/aead.h>
#include <net/strparser.h>
@@ -763,6 +764,8 @@ static int tls_sw_sendmsg_splice(struct sock *sk, struct msghdr *msg,
return 0;
}
+static int tls_tx_maybe_update_key(struct sock *sk, int flags);
+
static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
size_t size)
{
@@ -805,6 +808,19 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
goto send_end;
}
+ /* Check before each new record: one call can write many
+ * records, and a KeyUpdate reply must go before the next
+ * one. A send that failed before it copied any data leaves
+ * an empty open record. The KeyUpdate itself is a handshake
+ * record.
+ */
+ if ((!ctx->open_rec || !ctx->open_rec->msg_plaintext.sg.size) &&
+ record_type == TLS_RECORD_TYPE_DATA) {
+ ret = tls_tx_maybe_update_key(sk, msg->msg_flags);
+ if (ret)
+ goto send_end;
+ }
+
if (ctx->open_rec)
rec = ctx->open_rec;
else
@@ -1005,6 +1021,153 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
return copied > 0 ? copied : ret;
}
+/* RFC 9846 section 5.5 allows 2^24.5 full-size records per AES-GCM key.
+ * Send the KeyUpdate a little earlier.
+ */
+#define TLS_KEYUPDATE_TX_RECORDS BIT_ULL(24)
+
+/* Section 4.7.3: the epoch must not exceed 2^48-1. The application
+ * traffic secrets start at epoch 3.
+ */
+#define TLS_KEYUPDATE_MAX (BIT_ULL(48) - 4)
+
+/* Send a KeyUpdate with the current key, then install the next key.
+ * Called with tx_lock held and the socket locked.
+ */
+static int tls_tx_key_update(struct sock *sk, u8 request, int flags)
+{
+ u8 body[TLS_KEYUPDATE_MSG_SIZE] = {
+ TLS_HANDSHAKE_KEYUPDATE, 0, 0, 1, request
+ };
+ struct tls_context *tls_ctx = tls_get_ctx(sk);
+ struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+ u8 record_type = TLS_RECORD_TYPE_HANDSHAKE;
+ union {
+ struct cmsghdr cmsg;
+ u8 buf[CMSG_SPACE(sizeof(record_type))];
+ } ctrl = {};
+ struct msghdr msg = {};
+ struct cmsghdr *cmsg;
+ struct kvec iov;
+ int ret;
+
+ msg.msg_control = &ctrl;
+ msg.msg_controllen = sizeof(ctrl);
+ msg.msg_flags = flags & (MSG_DONTWAIT | MSG_NOSIGNAL);
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_TLS;
+ cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(record_type));
+ memcpy(CMSG_DATA(cmsg), &record_type, sizeof(record_type));
+
+ iov.iov_base = body;
+ iov.iov_len = sizeof(body);
+ iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, sizeof(body));
+
+ /* A control record closes the open record first, so all data
+ * written so far goes out with the current key, ahead of the
+ * KeyUpdate.
+ */
+ ret = tls_sw_sendmsg_locked(sk, &msg, sizeof(body));
+ if (ret < 0)
+ return ret;
+
+ /* Nothing may be encrypted with the next key before the KeyUpdate
+ * is encrypted with the current one.
+ */
+ ret = tls_encrypt_async_wait(ctx);
+ if (!ret)
+ ret = tls_keyupdate_rekey(sk, 1);
+ if (ret) {
+ /* The peer expects the next key: the connection is lost */
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXREKEYERROR);
+ tls_err_abort(sk, ret);
+ return ret;
+ }
+
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXREKEYOK);
+ tls_ctx->tx_key_updates++;
+ if (request == TLS_KEY_UPDATE_NOT_REQUESTED)
+ tls_ctx->tx_key_update_reply = false;
+
+ return 0;
+}
+
+/* Send a KeyUpdate if one is due: in reply to the peer's
+ * update_requested (RFC 9846 section 4.7.3), or before the key reaches
+ * its record limit (section 5.5). ChaCha20-Poly1305 has no record limit:
+ * the sequence number would wrap first.
+ */
+static int tls_tx_maybe_update_key(struct sock *sk, int flags)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ bool limit;
+
+ if (likely(!ctx->tx_secret.len))
+ return 0;
+
+ limit = ctx->prot_info.cipher_type != TLS_CIPHER_CHACHA20_POLY1305 &&
+ get_unaligned_be64(ctx->tx.rec_seq) >= TLS_KEYUPDATE_TX_RECORDS;
+ if (!limit && !ctx->tx_key_update_reply)
+ return 0;
+
+ if (ctx->tx_key_updates >= TLS_KEYUPDATE_MAX) {
+ /* Ignore update_requested. At the record limit the
+ * connection must end.
+ */
+ ctx->tx_key_update_reply = false;
+ return limit ? -EKEYEXPIRED : 0;
+ }
+
+ return tls_tx_key_update(sk, TLS_KEY_UPDATE_NOT_REQUESTED, flags);
+}
+
+/**
+ * tls_sw_key_update - send a KeyUpdate now, for TLS_TX_KEY_UPDATE
+ * @sk: socket
+ * @request: non-zero to also ask the peer to update its key
+ *
+ * Called with tx_lock held and the socket locked.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+int tls_sw_key_update(struct sock *sk, int request)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ int ret;
+
+ if (!ctx->tx_secret.len)
+ return -EINVAL;
+
+ if (request) {
+ /* Only the kernel sees the answer */
+ if (!ctx->rx_secret.len)
+ return -EINVAL;
+ /* Section 4.7.3: wait for the answer to the previous one */
+ if (ctx->tx_key_update_asked)
+ return -EBUSY;
+ }
+
+ if (ctx->tx_key_updates >= TLS_KEYUPDATE_MAX - 1)
+ return -EKEYEXPIRED;
+
+ /* A reply must carry update_not_requested */
+ if (request && ctx->tx_key_update_reply) {
+ ret = tls_tx_key_update(sk, TLS_KEY_UPDATE_NOT_REQUESTED,
+ MSG_NOSIGNAL);
+ if (ret)
+ return ret;
+ }
+
+ ret = tls_tx_key_update(sk, request ? TLS_KEY_UPDATE_REQUESTED :
+ TLS_KEY_UPDATE_NOT_REQUESTED, MSG_NOSIGNAL);
+ if (!ret && request)
+ ctx->tx_key_update_asked = true;
+
+ return ret;
+}
+
int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
{
struct tls_context *tls_ctx = tls_get_ctx(sk);
@@ -1551,15 +1714,28 @@ static int tls_check_pending_rekey(struct sock *sk, struct tls_context *ctx,
if (hs_type == TLS_HANDSHAKE_KEYUPDATE) {
struct tls_sw_context_rx *rx_ctx = ctx->priv_ctx_rx;
- WRITE_ONCE(rx_ctx->key_update_pending, true);
TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYRECEIVED);
+
+ if (ctx->rx_secret.len) {
+ err = tls_keyupdate_rx(sk, skb);
+ if (err) {
+ tls_err_abort(sk, err);
+ /* callers free the record only on success */
+ consume_skb(skb);
+ return err;
+ }
+ return 1;
+ }
+
+ WRITE_ONCE(rx_ctx->key_update_pending, true);
}
return 0;
}
/* On decrypt failure the connection is aborted (sk_err set) before
- * returning a negative errno.
+ * returning a negative errno. Return 1 for a record the kernel consumed
+ * (a KeyUpdate): the caller frees it and does not pass it to the user.
*/
static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,
struct tls_decrypt_arg *darg)
@@ -1883,6 +2059,11 @@ int tls_sw_recvmsg(struct sock *sk,
err = tls_rx_one_record(sk, msg, &darg);
if (err < 0)
goto recv_end;
+ if (err > 0) {
+ tls_rx_rec_done(ctx);
+ consume_skb(darg.skb);
+ continue;
+ }
async |= darg.async;
@@ -2023,9 +2204,8 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
if (err)
goto splice_read_end;
- if (!skb_queue_empty(&ctx->rx_list)) {
- skb = __skb_dequeue(&ctx->rx_list);
- } else {
+ skb = __skb_dequeue(&ctx->rx_list);
+ while (!skb) {
struct tls_decrypt_arg darg;
err = tls_rx_rec_wait(sk, nonblock, true, false);
@@ -2039,7 +2219,10 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
goto splice_read_end;
tls_rx_rec_done(ctx);
- skb = darg.skb;
+ if (err > 0)
+ consume_skb(darg.skb);
+ else
+ skb = darg.skb;
}
rxm = strp_msg(skb);
@@ -2121,6 +2304,11 @@ int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
err = tls_rx_one_record(sk, NULL, &darg);
if (err < 0)
goto read_sock_end;
+ if (err > 0) {
+ tls_rx_rec_done(ctx);
+ consume_skb(darg.skb);
+ continue;
+ }
released = tls_read_flush_backlog(sk, prot, INT_MAX,
0, decrypted,
@@ -2520,6 +2708,12 @@ static void tls_finish_key_update(struct sock *sk, struct tls_context *tls_ctx)
{
struct tls_sw_context_rx *ctx = tls_ctx->priv_ctx_rx;
+ /* Only a pending KeyUpdate holds poll() back. The in-kernel rekey
+ * runs while the KeyUpdate record is still queued.
+ */
+ if (!READ_ONCE(ctx->key_update_pending))
+ return;
+
WRITE_ONCE(ctx->key_update_pending, false);
/* wake-up pre-existing poll() */
ctx->saved_data_ready(sk);
diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config
index 7a89e2618d4c45..b75692f2576597 100644
--- a/tools/testing/selftests/net/config
+++ b/tools/testing/selftests/net/config
@@ -10,8 +10,12 @@ CONFIG_CAN_VXCAN=m
CONFIG_CGROUPS=y
CONFIG_CRYPTO_ARIA=y
CONFIG_CRYPTO_CHACHA20POLY1305=m
+CONFIG_CRYPTO_HMAC=y
CONFIG_CRYPTO_SHA1=y
+CONFIG_CRYPTO_SHA256=y
+CONFIG_CRYPTO_SHA512=y
CONFIG_CRYPTO_SM4_GENERIC=y
+CONFIG_CRYPTO_USER_API_HASH=y
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_INFO_BTF=y
CONFIG_DEBUG_INFO_BTF_MODULES=n
diff --git a/tools/testing/selftests/net/tls.c b/tools/testing/selftests/net/tls.c
index c4203401c4bfc2..ed3b510a5ef6fe 100644
--- a/tools/testing/selftests/net/tls.c
+++ b/tools/testing/selftests/net/tls.c
@@ -11,6 +11,7 @@
#include <stdlib.h>
#include <unistd.h>
+#include <linux/if_alg.h>
#include <linux/tls.h>
#include <linux/tcp.h>
#include <linux/socket.h>
@@ -2421,6 +2422,525 @@ TEST_F(tls, rekey_poll_delay)
}
}
+#ifndef SOL_ALG
+#define SOL_ALG 279
+#endif
+
+/* RFC 9846 section 7.1, HKDF-Expand-Label with an empty context, for an
+ * output no longer than the hash. The HMAC comes from the kernel.
+ */
+static bool hkdf_expand_label(const unsigned char *secret, int secret_len,
+ const char *label, unsigned char *out,
+ int out_len)
+{
+ struct sockaddr_alg sa = {
+ .salg_family = AF_ALG,
+ .salg_type = "hash",
+ };
+ unsigned char info[2 + 1 + 255 + 1 + 1], md[48];
+ int tfm, op, n = 0, label_len = 6 + strlen(label);
+ bool ok = false;
+
+ strcpy((char *)sa.salg_name,
+ secret_len == 48 ? "hmac(sha384)" : "hmac(sha256)");
+
+ info[n++] = out_len >> 8;
+ info[n++] = out_len;
+ info[n++] = label_len;
+ memcpy(info + n, "tls13 ", 6);
+ memcpy(info + n + 6, label, label_len - 6);
+ n += label_len;
+ info[n++] = 0; /* context */
+ info[n++] = 1; /* HKDF-Expand counter */
+
+ tfm = socket(AF_ALG, SOCK_SEQPACKET, 0);
+ if (tfm < 0)
+ return false;
+ if (bind(tfm, (struct sockaddr *)&sa, sizeof(sa)) ||
+ setsockopt(tfm, SOL_ALG, ALG_SET_KEY, secret, secret_len))
+ goto out;
+
+ op = accept(tfm, NULL, 0);
+ if (op < 0)
+ goto out;
+ ok = write(op, info, n) == n && read(op, md, secret_len) == secret_len;
+ close(op);
+ if (ok)
+ memcpy(out, md, out_len);
+out:
+ close(tfm);
+ return ok;
+}
+
+/* Fill @keys with the key and IV that @secret gives, record number 0 */
+static bool keyupdate_keys(uint16_t cipher_type, const unsigned char *secret,
+ int secret_len, struct tls_crypto_info_keys *keys)
+{
+ unsigned char *key, *salt, *iv;
+ int key_len, salt_len, iv_len;
+ unsigned char nonce[12];
+
+ tls_crypto_info_init(TLS_1_3_VERSION, cipher_type, keys, 0);
+
+ switch (cipher_type) {
+ case TLS_CIPHER_AES_GCM_128:
+ key = keys->aes128.key;
+ salt = keys->aes128.salt;
+ iv = keys->aes128.iv;
+ key_len = sizeof(keys->aes128.key);
+ salt_len = sizeof(keys->aes128.salt);
+ iv_len = sizeof(keys->aes128.iv);
+ break;
+ case TLS_CIPHER_AES_GCM_256:
+ key = keys->aesgcm256.key;
+ salt = keys->aesgcm256.salt;
+ iv = keys->aesgcm256.iv;
+ key_len = sizeof(keys->aesgcm256.key);
+ salt_len = sizeof(keys->aesgcm256.salt);
+ iv_len = sizeof(keys->aesgcm256.iv);
+ break;
+ case TLS_CIPHER_AES_CCM_128:
+ key = keys->aesccm128.key;
+ salt = keys->aesccm128.salt;
+ iv = keys->aesccm128.iv;
+ key_len = sizeof(keys->aesccm128.key);
+ salt_len = sizeof(keys->aesccm128.salt);
+ iv_len = sizeof(keys->aesccm128.iv);
+ break;
+ case TLS_CIPHER_CHACHA20_POLY1305:
+ key = keys->chacha20.key;
+ salt = keys->chacha20.salt;
+ iv = keys->chacha20.iv;
+ key_len = sizeof(keys->chacha20.key);
+ salt_len = 0;
+ iv_len = sizeof(keys->chacha20.iv);
+ break;
+ default:
+ return false;
+ }
+
+ if (!hkdf_expand_label(secret, secret_len, "key", key, key_len) ||
+ !hkdf_expand_label(secret, secret_len, "iv", nonce,
+ salt_len + iv_len))
+ return false;
+
+ memcpy(salt, nonce, salt_len);
+ memcpy(iv, nonce + salt_len, iv_len);
+ return true;
+}
+
+/* In-kernel KeyUpdate: fd and cfd both register the TX and RX traffic
+ * secrets of a bidirectional connection. fd -> cfd starts at record
+ * number @seq. Returns false if the variant does not support it.
+ */
+static bool keyupdate_setup_seq(struct __test_metadata *_metadata,
+ FIXTURE_DATA(tls) *self,
+ const FIXTURE_VARIANT(tls) *variant,
+ uint64_t seq)
+{
+ struct tls_crypto_info_keys keys_a, keys_b;
+ unsigned char secret_a[48], secret_b[48];
+ unsigned char *rec_seq;
+ int len, i;
+
+ if (self->notls || variant->tls_version != TLS_1_3_VERSION)
+ return false;
+
+ switch (variant->cipher_type) {
+ case TLS_CIPHER_AES_GCM_128:
+ case TLS_CIPHER_AES_CCM_128:
+ case TLS_CIPHER_CHACHA20_POLY1305:
+ len = 32;
+ break;
+ case TLS_CIPHER_AES_GCM_256:
+ len = 48;
+ break;
+ default:
+ return false;
+ }
+
+ memset(secret_a, 0xa5, sizeof(secret_a));
+ memset(secret_b, 0x5a, sizeof(secret_b));
+
+ /* the keys must be the ones that the secrets give */
+ ASSERT_TRUE(keyupdate_keys(variant->cipher_type, secret_a, len,
+ &keys_a));
+ ASSERT_TRUE(keyupdate_keys(variant->cipher_type, secret_b, len,
+ &keys_b));
+
+ /* rec_seq is the last member of each crypto_info */
+ rec_seq = (unsigned char *)&keys_a + keys_a.len - 8;
+ for (i = 0; i < 8; i++)
+ rec_seq[i] = seq >> (56 - 8 * i);
+
+ /* fd -> cfd: replace the keys of the fixture */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &keys_a,
+ keys_a.len), 0);
+ EXPECT_EQ(setsockopt(self->cfd, SOL_TLS, TLS_RX, &keys_a,
+ keys_a.len), 0);
+ /* cfd -> fd */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX, &keys_b,
+ keys_b.len), 0);
+ EXPECT_EQ(setsockopt(self->cfd, SOL_TLS, TLS_TX, &keys_b,
+ keys_b.len), 0);
+
+ /* the RX secret needs the TX secret first */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret_b, len), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret_a, len), 0);
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret_b, len), 0);
+ EXPECT_EQ(setsockopt(self->cfd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret_b, len), 0);
+ EXPECT_EQ(setsockopt(self->cfd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret_a, len), 0);
+
+ return true;
+}
+
+static bool keyupdate_setup(struct __test_metadata *_metadata,
+ FIXTURE_DATA(tls) *self,
+ const FIXTURE_VARIANT(tls) *variant)
+{
+ return keyupdate_setup_seq(_metadata, self, variant, 0);
+}
+
+static void keyupdate_send(struct __test_metadata *_metadata, int fd,
+ int request)
+{
+ EXPECT_EQ(setsockopt(fd, SOL_TLS, TLS_TX_KEY_UPDATE,
+ &request, sizeof(request)), 0);
+}
+
+static void send_recv_str(struct __test_metadata *_metadata, int from, int to,
+ const char *str)
+{
+ int len = strlen(str) + 1;
+ char buf[64];
+
+ EXPECT_EQ(send(from, str, len, 0), len);
+ EXPECT_EQ(recv(to, buf, sizeof(buf), 0), len);
+ EXPECT_EQ(memcmp(buf, str, len), 0);
+}
+
+TEST_F(tls, keyupdate_secret_errors)
+{
+ unsigned char secret[48] = {};
+ int one = 1;
+
+ if (self->notls)
+ SKIP(return, "no TLS support");
+
+ /* no secret registered */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_KEY_UPDATE,
+ &one, sizeof(one)), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ if (variant->tls_version != TLS_1_3_VERSION) {
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret, 32), -1);
+ EXPECT_EQ(errno, EINVAL);
+ return;
+ }
+
+ if (variant->cipher_type == TLS_CIPHER_SM4_GCM ||
+ variant->cipher_type == TLS_CIPHER_SM4_CCM) {
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret, 32), -1);
+ EXPECT_EQ(errno, EOPNOTSUPP);
+ return;
+ }
+
+ /* the RX key is not installed on fd */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret, 32), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ /* wrong length */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret, 16), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ /* the installed key does not come from this secret */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET, secret,
+ variant->cipher_type == TLS_CIPHER_AES_GCM_256 ?
+ 48 : 32), -1);
+ EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(tls, keyupdate)
+{
+ int i;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ send_recv_str(_metadata, self->fd, self->cfd, "before");
+
+ /* stay below the limit of 8 received KeyUpdates per second */
+ for (i = 0; i < 4; i++) {
+ keyupdate_send(_metadata, self->fd, 0);
+ send_recv_str(_metadata, self->fd, self->cfd, "after");
+ keyupdate_send(_metadata, self->cfd, 0);
+ send_recv_str(_metadata, self->cfd, self->fd, "reverse");
+ }
+}
+
+TEST_F(tls, keyupdate_queued)
+{
+ const char *str = "queued behind a KeyUpdate";
+ int len = strlen(str) + 1;
+ char buf[64];
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ /* the record before the KeyUpdate, the KeyUpdate and the record
+ * after it are all queued before the first read
+ */
+ EXPECT_EQ(send(self->fd, str, len, MSG_MORE), len);
+ keyupdate_send(_metadata, self->fd, 0);
+ EXPECT_EQ(send(self->fd, str, len, 0), len);
+
+ EXPECT_EQ(recv(self->cfd, buf, len, MSG_WAITALL), len);
+ EXPECT_EQ(memcmp(buf, str, len), 0);
+ EXPECT_EQ(recv(self->cfd, buf, len, MSG_WAITALL), len);
+ EXPECT_EQ(memcmp(buf, str, len), 0);
+}
+
+TEST_F(tls, keyupdate_request)
+{
+ int one = 1;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ keyupdate_send(_metadata, self->fd, 1);
+
+ /* no answer yet */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_KEY_UPDATE,
+ &one, sizeof(one)), -1);
+ EXPECT_EQ(errno, EBUSY);
+
+ /* cfd processes the request, then answers ahead of its data */
+ send_recv_str(_metadata, self->fd, self->cfd, "request");
+ send_recv_str(_metadata, self->cfd, self->fd, "answer");
+
+ /* answered */
+ keyupdate_send(_metadata, self->fd, 1);
+ send_recv_str(_metadata, self->fd, self->cfd, "request");
+ send_recv_str(_metadata, self->cfd, self->fd, "answer");
+}
+
+TEST_F(tls, keyupdate_splice)
+{
+ const char *str = "spliced after a KeyUpdate";
+ int len = strlen(str) + 1;
+ char buf[64];
+ int p[2];
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ ASSERT_GE(pipe(p), 0);
+
+ keyupdate_send(_metadata, self->fd, 0);
+ EXPECT_EQ(send(self->fd, str, len, 0), len);
+
+ EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, len, 0), len);
+ EXPECT_EQ(read(p[0], buf, len), len);
+ EXPECT_EQ(memcmp(buf, str, len), 0);
+
+ close(p[0]);
+ close(p[1]);
+}
+
+TEST_F(tls, keyupdate_manual_rekey)
+{
+ struct tls_crypto_info_keys tls12;
+ int zero = 0;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ /* a key installed by the user removes the registered secrets */
+ tls_crypto_info_init(variant->tls_version, variant->cipher_type,
+ &tls12, 2);
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len), 0);
+
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_KEY_UPDATE,
+ &zero, sizeof(zero)), -1);
+ EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(tls, keyupdate_manual_rekey_request)
+{
+ struct tls_crypto_info_keys keys;
+ unsigned char secret[48];
+ int len;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ keyupdate_send(_metadata, self->fd, 1);
+
+ /* the answer to the request now goes to user space */
+ len = variant->cipher_type == TLS_CIPHER_AES_GCM_256 ? 48 : 32;
+ memset(secret, 0x3c, sizeof(secret));
+ ASSERT_TRUE(keyupdate_keys(variant->cipher_type, secret, len, &keys));
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX, &keys, keys.len), 0);
+
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret, len), 0);
+
+ keyupdate_send(_metadata, self->fd, 1);
+}
+
+TEST_F(tls, keyupdate_nosignal)
+{
+ int zero = 0;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ /* The default action of SIGPIPE ends the test. The result is not
+ * checked: as for sendmsg(), a send error may not reach the caller.
+ */
+ EXPECT_EQ(shutdown(self->fd, SHUT_WR), 0);
+ setsockopt(self->fd, SOL_TLS, TLS_TX_KEY_UPDATE, &zero, sizeof(zero));
+}
+
+TEST_F(tls, keyupdate_rate_limit)
+{
+ char buf[64];
+ int i;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ /* the ninth KeyUpdate in one second ends the connection */
+ for (i = 0; i < 9; i++)
+ keyupdate_send(_metadata, self->fd, 0);
+ EXPECT_EQ(send(self->fd, "x", 1, 0), 1);
+
+ EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), 0), -1);
+ EXPECT_EQ(errno, EBADMSG);
+}
+
+TEST_F(tls, keyupdate_record_limit)
+{
+ struct tls_crypto_info_keys get;
+ char sbuf[1024], rbuf[1024];
+ socklen_t optlen;
+ unsigned char *rec_seq;
+ __u16 limit = 128;
+ int i;
+
+ if (variant->cipher_type == TLS_CIPHER_CHACHA20_POLY1305)
+ SKIP(return, "no record limit for ChaCha20-Poly1305");
+ if (!keyupdate_setup_seq(_metadata, self, variant, (1 << 24) - 2))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ ASSERT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN,
+ &limit, sizeof(limit)), 0);
+ for (i = 0; i < sizeof(sbuf); i++)
+ sbuf[i] = i;
+
+ /* one call writes 8 records or more, past the record limit */
+ EXPECT_EQ(send(self->fd, sbuf, sizeof(sbuf), 0), sizeof(sbuf));
+ EXPECT_EQ(recv(self->cfd, rbuf, sizeof(rbuf), MSG_WAITALL),
+ sizeof(rbuf));
+ EXPECT_EQ(memcmp(sbuf, rbuf, sizeof(sbuf)), 0);
+
+ /* the KeyUpdate in the middle of the call restarted at 0 */
+ tls_crypto_info_init(variant->tls_version, variant->cipher_type,
+ &get, 0);
+ optlen = get.len;
+ ASSERT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &optlen), 0);
+ rec_seq = (unsigned char *)&get + optlen - 8;
+ for (i = 0; i < 5; i++)
+ EXPECT_EQ(rec_seq[i], 0);
+}
+
+/* A send that fails with EAGAIN before it copies any data leaves an
+ * empty open record. The next send must still answer a KeyUpdate
+ * request before its data.
+ */
+TEST_F(tls, keyupdate_request_after_eagain)
+{
+ struct tls_crypto_info_keys get;
+ size_t sent = 0, rcvd = 0;
+ char buf[16384];
+ unsigned char *rec_seq;
+ socklen_t optlen;
+ int size = 4096;
+ ssize_t ret;
+ int i;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ ASSERT_EQ(setsockopt(self->fd, SOL_SOCKET, SO_SNDBUF, &size,
+ sizeof(size)), 0);
+ ASSERT_EQ(setsockopt(self->cfd, SOL_SOCKET, SO_RCVBUF, &size,
+ sizeof(size)), 0);
+ memset(buf, 0x42, sizeof(buf));
+
+ /* fill the send buffer until a send copies nothing */
+ for (;;) {
+ ret = send(self->fd, buf, sizeof(buf), MSG_DONTWAIT);
+ if (ret < 0)
+ break;
+ sent += ret;
+ }
+ ASSERT_EQ(errno, EAGAIN);
+
+ /* fd reads the request; its answer is now due */
+ keyupdate_send(_metadata, self->cfd, 1);
+ EXPECT_EQ(recv(self->fd, buf, sizeof(buf), MSG_DONTWAIT), -1);
+ EXPECT_EQ(errno, EAGAIN);
+
+ while (rcvd < sent) {
+ ret = recv(self->cfd, buf, sizeof(buf), 0);
+ ASSERT_GT(ret, 0);
+ rcvd += ret;
+ }
+
+ /* the answer goes out before this data */
+ send_recv_str(_metadata, self->fd, self->cfd, "data");
+
+ tls_crypto_info_init(variant->tls_version, variant->cipher_type,
+ &get, 0);
+ optlen = get.len;
+ ASSERT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &optlen), 0);
+ rec_seq = (unsigned char *)&get + optlen - 8;
+ for (i = 0; i < 7; i++)
+ EXPECT_EQ(rec_seq[i], 0);
+ EXPECT_EQ(rec_seq[7], 1);
+
+ /* cfd got the answer, so it can ask again */
+ keyupdate_send(_metadata, self->cfd, 1);
+}
+
+TEST_F(tls, keyupdate_invalid)
+{
+ /* key_update, length 1, request_update 2 */
+ static const char bad[] = "\x18\x00\x00\x01\x02";
+ char buf[64];
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ EXPECT_EQ(tls_send_cmsg(self->fd, TLS_RECORD_TYPE_HANDSHAKE,
+ (char *)bad, 5, 0), 5);
+
+ EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), 0), -1);
+ EXPECT_EQ(errno, EBADMSG);
+}
+
struct raw_rec {
unsigned int plain_len;
unsigned char plain_data[100];
|
| 3/2 | 2026/11/08 13:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 21535 output: 32 thoughts: 697 Thoughts: **My Initial Assessment of the Kernel Patch for TLS 1.3 KeyUpdate Support on AMD64** Okay, so I'm looking at this patch series aimed at adding in-kernel TLS 1.3 KeyUpdate support to kTLS, specifically for the amd64 architecture. My first step is always to get a handle on the scope of the changes. I can see it touches a bunch of files: documentation, include files, the Kconfig, the Makefile, several source files in the `net/tls` directory, and even some selftests. Seems like a fairly involved addition, which is what I'd expect for something as complex as key rekeying within the kernel. Now, let's break down the core functionalities being introduced. It's adding TLS 1.3 in-kernel rekeying, which is a major enhancement. We're talking about deriving keys using HKDF-Expand-Label, which is standard, and sending handshake records directly during `sendmsg` or via `setsockopt`. More importantly, it seems to also handle decryption/interception of `KeyUpdate` records during `recvmsg`, `splice_read`, and `read_sock`, which is a more critical part of the process. The code seems to interact with socket locks and mutexes too, which always requires careful review. We are looking at `ctx->tx_lock` and `lock_sock(sk)`, so synchronization issues are something I'll need to keep in mind. I can see there are multiple states like `tx_key_update_reply`, `tx_key_update_asked`, `key_update_window`, which tells me the developers clearly are trying to do things in a secure way. The good news is that kTLS is reachable and fuzzed in standard virtualized environments, like QEMU and syzkaller. I know this because it’s a pure software networking subsystem based on TCP sockets and using `setsockopt` with the `TCP_ULP` option. Syzkaller is actively fuzzing kTLS with syzlang descriptions. This is excellent news, as it means we have the tools to thoroughly test this new functionality. Given the introduction of this complex functionality, it's a no-brainer that this is *WorthFuzzing*. Now, let's dig into the specific configurations and symbols, starting with that `net/tls/Kconfig` file. I'll need to get a clear picture of what's enabled, disabled, and how these changes impact the overall build configurations. |
| 4/2 | 2026/11/08 13:37 | tool | read-file |
0mArgs:{
"File": "net/tls/Kconfig",
"FirstLine": 1,
"LineCount": 40
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0-only\n 2:\t#\n 3:\t# TLS configuration\n 4:\t#\n 5:\tconfig TLS\n 6:\t\ttristate \"Transport Layer Security support\"\n 7:\t\tdepends on INET\n 8:\t\tselect CRYPTO\n 9:\t\tselect CRYPTO_AES\n 10:\t\tselect CRYPTO_GCM\n 11:\t\tselect CRYPTO_LIB_SHA256\n 12:\t\tselect CRYPTO_LIB_SHA512\n 13:\t\tselect NET_SOCK_MSG\n 14:\t\tdefault n\n 15:\t\thelp\n 16:\t\tEnable kernel support for TLS protocol. This allows symmetric\n 17:\t\tencryption handling of the TLS protocol to be done in-kernel.\n 18:\t\n 19:\t\tIf unsure, say N.\n 20:\t\n 21:\tconfig TLS_DEVICE\n 22:\t\tbool \"Transport Layer Security HW offload\"\n 23:\t\tdepends on TLS\n 24:\t\tselect SKB_DECRYPTED\n 25:\t\tselect SOCK_VALIDATE_XMIT\n 26:\t\tselect SOCK_RX_QUEUE_MAPPING\n 27:\t\tdefault n\n 28:\t\thelp\n 29:\t\tEnable kernel support for HW offload of the TLS protocol.\n 30:\t\n 31:\t\tIf unsure, say N.\n"
} |
| 5/2 | 2026/11/08 13:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23159 output: 33 thoughts: 53 Thoughts: Okay, let's break this down. **Dependencies and Scope Check for TLS Configuration** Right, so `CONFIG_TLS` clearly hinges on `CONFIG_INET` – that makes sense. Now, before I get too deep into this specific TLS configuration, let's make sure I haven't missed any obvious dependencies. Is there anything else that might be required, like, say, `CONFIG_TLS_TOE`? I know these things can be nested and have their own dependencies. Best to be thorough. Okay, time to dive into the `net/tls/Kconfig` file and take a look at the rest of the configuration options defined there. Get the whole picture before I start making assumptions. |
| 6/2 | 2026/11/08 13:37 | tool | read-file |
0mArgs:{
"File": "net/tls/Kconfig",
"FirstLine": 30,
"LineCount": 30
}Results: {
"Contents": " 30:\t\n 31:\t\tIf unsure, say N.\n"
} |
| 7/2 | 2026/11/08 13:37 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23425 output: 30 thoughts: 1124 Thoughts: **Analyzing TLS Key Update Functionality** Okay, so I'm diving into this TLS key update implementation. First, a quick glance at `net/tls/Kconfig` – it's pretty concise, only 31 lines. I wonder, are specific ciphers mandated for TLS 1.3? Looking at the config, `TLS` selects `CRYPTO_AES`, `CRYPTO_GCM`, `CRYPTO_LIB_SHA256`, and `CRYPTO_LIB_SHA512`. What about `CRYPTO_CHACHA20POLY1305` or `CRYPTO_CCM`? Perhaps those are optional based on the ciphers chosen, though AES-GCM is standard. The main configuration seems to be driven by `CONFIG_TLS`. Now, let's get into the heart of things and check the `FocusSymbols`. I need to identify the key functions that have been added or modified. My focus is on those that are non-static (publicly visible), are critical entrypoints, or directly implement the changed logic. Here’s what I've found after looking at the modified files: In `net/tls/tls_hkdf.c`: * `tls_hkdf_expand_label` (non-static) - this seems relevant for key derivation. In `net/tls/tls_keyupdate.c`: * `tls_keyupdate_rekey` (non-static) - A direct indication of rekeying. * `tls_keyupdate_rx` (non-static) - Handling the receiving side of a key update. In `net/tls/tls_main.c`: * `tls_ctx_free` (modified, but just a generic cleanup) - not a primary focus. * `do_tls_setsockopt_conf` (modified) - setting socket options, could be important. * `tls_traffic_secret_len` (static) - Not as important for this check, but noted. * `tls_check_traffic_secret` (static) - also less important for this check. * `do_tls_setsockopt_key_update` (static) - also less important for this check. * `do_tls_setsockopt` (modified) - very important, potentially. In `net/tls/tls_sw.c`: * `tls_tx_key_update` (static) - less important for this check * `tls_tx_maybe_update_key` (static) - less important for this check. * `tls_sw_key_update` (non-static) - seems to be an important entrypoint for the key update process. * `tls_check_pending_rekey` (static) - less important for this check. * `tls_sw_sendmsg_locked` (static) - less important for this check. * `tls_sw_recvmsg` (non-static) - could be important for the update process on the receiving side. * `tls_sw_splice_read` (non-static) - likely less important, generic interface. * `tls_sw_read_sock` (non-static) - likely less important, generic interface. * `tls_finish_key_update` (static) - less important for this check. The static functions might be inlined. I need to figure out which are directly visible in the symbol table, since `FocusSymbols` are used to drive fuzzing coverage with syzkaller. The goal is to target specific features and entrypoints. The rules for `FocusSymbols` are clear: Avoid ubiquitous functions that are called almost everywhere, focus on the feature logic, and handle static inline functions by targeting feature-specific *callers*. So, are these candidates good for `FocusSymbols`: `do_tls_setsockopt`, `tls_sw_key_update`, `tls_keyupdate_rekey`, `tls_keyupdate_rx`, `tls_hkdf_expand_label`, and potentially `tls_check_traffic_secret`, since this is key update logic. Are the static ones ( `tls_tx_key_update`, `tls_tx_maybe_update_key`, `do_tls_setsockopt_secret`, `do_tls_setsockopt_key_update` ) inlined? The non-static symbols are what I need to target. The non-static symbols are guaranteed to be in the symbol table, so they are |
| 8/2 | 2026/11/08 13:38 | tool | grepper |
0mArgs:{
"Expression": "tls_setsockopt",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "net/tls/tls_main.c=600=static int validate_crypto_info(const struct tls_crypto_info *crypto_info,\n--\nnet/tls/tls_main.c-624-\nnet/tls/tls_main.c:625:static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,\nnet/tls/tls_main.c-626-\t\t\t\t unsigned int optlen, int tx)\n--\nnet/tls/tls_main.c-765-\t\t/* The registered secret no longer matches the key. The RX\nnet/tls/tls_main.c:766:\t\t * secret also needs the TX one, see do_tls_setsockopt_secret().\nnet/tls/tls_main.c-767-\t\t */\n--\nnet/tls/tls_main.c-797-\nnet/tls/tls_main.c:798:static int do_tls_setsockopt_tx_zc(struct sock *sk, sockptr_t optval,\nnet/tls/tls_main.c-799-\t\t\t\t unsigned int optlen)\n--\nnet/tls/tls_main.c-817-\nnet/tls/tls_main.c:818:static int do_tls_setsockopt_no_pad(struct sock *sk, sockptr_t optval,\nnet/tls/tls_main.c-819-\t\t\t\t unsigned int optlen)\n--\nnet/tls/tls_main.c=850=static bool tls_tx_record_is_open(struct tls_context *ctx)\n--\nnet/tls/tls_main.c-861-\nnet/tls/tls_main.c:862:static int do_tls_setsockopt_tx_payload_len(struct sock *sk, sockptr_t optval,\nnet/tls/tls_main.c-863-\t\t\t\t\t unsigned int optlen)\n--\nnet/tls/tls_main.c=914=static int tls_check_traffic_secret(struct tls_crypto_info *crypto_info,\n--\nnet/tls/tls_main.c-951-\nnet/tls/tls_main.c:952:static int do_tls_setsockopt_secret(struct sock *sk, sockptr_t optval,\nnet/tls/tls_main.c-953-\t\t\t\t unsigned int optlen, int tx)\n--\nnet/tls/tls_main.c-1010-\nnet/tls/tls_main.c:1011:static int do_tls_setsockopt_key_update(struct sock *sk, sockptr_t optval,\nnet/tls/tls_main.c-1012-\t\t\t\t\tunsigned int optlen)\n--\nnet/tls/tls_main.c-1039-\nnet/tls/tls_main.c:1040:static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,\nnet/tls/tls_main.c-1041-\t\t\t unsigned int optlen)\n--\nnet/tls/tls_main.c-1049-\t\tlock_sock(sk);\nnet/tls/tls_main.c:1050:\t\trc = do_tls_setsockopt_conf(sk, optval, optlen,\nnet/tls/tls_main.c-1051-\t\t\t\t\t optname == TLS_TX);\n--\nnet/tls/tls_main.c-1055-\t\tlock_sock(sk);\nnet/tls/tls_main.c:1056:\t\trc = do_tls_setsockopt_tx_zc(sk, optval, optlen);\nnet/tls/tls_main.c-1057-\t\trelease_sock(sk);\n--\nnet/tls/tls_main.c-1059-\tcase TLS_RX_EXPECT_NO_PAD:\nnet/tls/tls_main.c:1060:\t\trc = do_tls_setsockopt_no_pad(sk, optval, optlen);\nnet/tls/tls_main.c-1061-\t\tbreak;\n--\nnet/tls/tls_main.c-1068-\t\tlock_sock(sk);\nnet/tls/tls_main.c:1069:\t\trc = do_tls_setsockopt_tx_payload_len(sk, optval, optlen);\nnet/tls/tls_main.c-1070-\t\trelease_sock(sk);\n--\nnet/tls/tls_main.c-1075-\t\tlock_sock(sk);\nnet/tls/tls_main.c:1076:\t\trc = do_tls_setsockopt_secret(sk, optval, optlen,\nnet/tls/tls_main.c-1077-\t\t\t\t\t optname == TLS_TX_TRAFFIC_SECRET);\n--\nnet/tls/tls_main.c-1080-\tcase TLS_TX_KEY_UPDATE:\nnet/tls/tls_main.c:1081:\t\trc = do_tls_setsockopt_key_update(sk, optval, optlen);\nnet/tls/tls_main.c-1082-\t\tbreak;\n--\nnet/tls/tls_main.c-1089-\nnet/tls/tls_main.c:1090:static int tls_setsockopt(struct sock *sk, int level, int optname,\nnet/tls/tls_main.c-1091-\t\t\t sockptr_t optval, unsigned int optlen)\n--\nnet/tls/tls_main.c-1098-\nnet/tls/tls_main.c:1099:\treturn do_tls_setsockopt(sk, optname, optval, optlen);\nnet/tls/tls_main.c-1100-}\n--\nnet/tls/tls_main.c=1193=static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],\n--\nnet/tls/tls_main.c-1196-\tprot[TLS_BASE][TLS_BASE] = *base;\nnet/tls/tls_main.c:1197:\tprot[TLS_BASE][TLS_BASE].setsockopt\t= tls_setsockopt;\nnet/tls/tls_main.c-1198-\tprot[TLS_BASE][TLS_BASE].getsockopt\t= tls_getsockopt;\n--\nnet/tls/tls_sw.c=2722=int tls_set_sw_offload(struct sock *sk, int tx,\n--\nnet/tls/tls_sw.c-2830-\t\t\t cipher_desc-\u003ecrypto_info,\nnet/tls/tls_sw.c:2831:\t\t\t /* size was checked in do_tls_setsockopt_conf */);\nnet/tls/tls_sw.c-2832-\t\tmemzero_explicit(new_crypto_info, cipher_desc-\u003ecrypto_info);\n"
} |
| 9/2 | 2026/11/08 13:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27318 output: 33 thoughts: 414 Thoughts: **Analyzing Recent TLS Code Changes** Okay, let's get down to brass tacks. I need to get a handle on the recent modifications to our TLS implementation. First, let me quickly review the functions that have been added or modified. Alright, here's the rundown: 1. `tls_hkdf_expand_label` in `net/tls/tls_hkdf.c`. Seems to be an internal utility function, exported via `tls.h`, which is used by `tls_check_traffic_secret` and `tls_keyupdate_rekey`. 2. `tls_keyupdate_rekey` is in `net/tls/tls_keyupdate.c`, also in `tls.h`, called by `tls_keyupdate_rx` and `tls_tx_key_update`. 3. `tls_keyupdate_rx` which is also in `net/tls/tls_keyupdate.c` and is used by `tls_check_pending_rekey`. 4. `tls_sw_key_update` in `net/tls/tls_sw.c` - exported via `tls.h`, called by `do_tls_setsockopt_key_update`. 5. `do_tls_setsockopt_secret` is a static function in `net/tls/tls_main.c`. 6. `do_tls_setsockopt_key_update` is a static function in `net/tls/tls_main.c`. 7. `tls_check_traffic_secret` is a static function in `net/tls/tls_main.c`. 8. `tls_tx_key_update` is a static function in `net/tls/tls_sw.c`. 9. `tls_tx_maybe_update_key` is also a static function in `net/tls/tls_sw.c`. 10. `tls_check_pending_rekey` is another static function in `net/tls/tls_sw.c`. Now, let's take a closer look at `tls_check_pending_rekey`. I'll want to see exactly how it functions, so let's start by figuring out where it's being called. I need to understand the control flow surrounding this rekeying logic. |
| 10/2 | 2026/11/08 13:38 | tool | grepper |
0mArgs:{
"Expression": "tls_check_pending_rekey",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "net/tls/tls_sw.c=1645=tls_decrypt_device(struct sock *sk, struct msghdr *msg,\n--\nnet/tls/tls_sw.c-1693-\nnet/tls/tls_sw.c:1694:static int tls_check_pending_rekey(struct sock *sk, struct tls_context *ctx,\nnet/tls/tls_sw.c-1695-\t\t\t\t struct sk_buff *skb)\n--\nnet/tls/tls_sw.c=1740=static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,\n--\nnet/tls/tls_sw.c-1760-\nnet/tls/tls_sw.c:1761:\treturn tls_check_pending_rekey(sk, tls_ctx, darg-\u003eskb);\nnet/tls/tls_sw.c-1762-}\n"
} |
| 11/2 | 2026/11/08 13:38 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28519 output: 262 thoughts: 822 Thoughts: **Key Update Feature Assessment** Alright, let's break this down. First, I see that `tls_rx_one_record` calls `tls_check_pending_rekey`. And it's important to remember that `tls_rx_one_record` is invoked by `tls_sw_recvmsg`, `tls_sw_read_sock`, and `tls_sw_splice_read`. Now, I need to figure out if we should include certain symbols in `FocusSymbols`. I'm thinking about including a specific set of functions: `tls_hkdf_expand_label`, `tls_keyupdate_rekey`, `tls_keyupdate_rx`, `tls_sw_key_update`, `do_tls_setsockopt_secret`, `do_tls_setsockopt_key_update`, `tls_tx_key_update`, `tls_tx_maybe_update_key`, `tls_check_traffic_secret`, and `tls_check_pending_rekey`. The key here is that all of these functions are specific to this *new* Key Update feature, they're not ubiquitous, general-purpose functions that are widely used across the codebase. `tls_sw_recvmsg` *is* a common entry point. Next, I need to check the required configurations. I see `config TLS` in `net/tls/Kconfig`. It depends on `INET`, which is probably standard, plus some crypto selections: `CRYPTO`, `CRYPTO_AES`, `CRYPTO_GCM`, `CRYPTO_LIB_SHA256`, `CRYPTO_LIB_SHA512`, and `NET_SOCK_MSG`. So `TLS` (CONFIG_TLS) and `INET` are the important ones. I'll double check if `INET` is standard in EnableConfigs, and it is. Now, let's confirm the requirements for fuzzing: 1. **WorthFuzzing? Absolutely!** This patch adds a *complete* in-kernel KeyUpdate mechanism for TLS 1.3 in kTLS. It covers HKDF cryptography, state machines for KeyUpdate, rate limits, record counts, socket options (`TLS_TX_TRAFFIC_SECRET`, `TLS_RX_TRAFFIC_SECRET`, `TLS_TX_KEY_UPDATE`), and TX (`tls_tx_key_update`, `tls_tx_maybe_update_key`) and RX (`tls_keyupdate_rx`, `tls_check_pending_rekey`) packet processing. It's software-based and is accessible through `setsockopt`, `sendmsg`, and `recvmsg`. It's a prime target. 2. **EnableConfigs:** `TLS`, `INET` 3. **FocusSymbols:** As determined previously: `tls_hkdf_expand_label`, `tls_keyupdate_rekey`, `tls_keyupdate_rx`, `tls_sw_key_update`, `do_tls_setsockopt_secret`, `do_tls_setsockopt_key_update`, `tls_tx_key_update`, `tls_tx_maybe_update_key`, `tls_check_traffic_secret`, `tls_check_pending_rekey` 4. **Reasoning:** The reasoning is concise: it's a new feature, a complete implementation, with cryptography, state machines, and socket interactions – perfect for fuzzing. Okay, that's everything. Let's call `set-results`. |
| 12/2 | 2026/11/08 13:38 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"TLS",
"INET"
],
"FocusSymbols": [
"tls_hkdf_expand_label",
"tls_keyupdate_rekey",
"tls_keyupdate_rx",
"tls_sw_key_update",
"tls_tx_key_update",
"tls_tx_maybe_update_key",
"do_tls_setsockopt_secret",
"do_tls_setsockopt_key_update",
"tls_check_traffic_secret",
"tls_check_pending_rekey"
],
"Reasoning": "The patch series implements in-kernel TLS 1.3 KeyUpdate support in kTLS (net/tls). It introduces new setsockopt operations (TLS_TX_TRAFFIC_SECRET, TLS_RX_TRAFFIC_SECRET, TLS_TX_KEY_UPDATE), cryptographic key derivation routines using HKDF-Expand-Label (RFC 9846), automatic rekeying logic on TX and RX, and rate-limiting / validation state machines. These changes operate in a core networking subsystem accessible to user space via standard sockets and deserve focused fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"TLS",
"INET"
],
"FocusSymbols": [
"tls_hkdf_expand_label",
"tls_keyupdate_rekey",
"tls_keyupdate_rx",
"tls_sw_key_update",
"tls_tx_key_update",
"tls_tx_maybe_update_key",
"do_tls_setsockopt_secret",
"do_tls_setsockopt_key_update",
"tls_check_traffic_secret",
"tls_check_pending_rekey"
],
"Reasoning": "The patch series implements in-kernel TLS 1.3 KeyUpdate support in kTLS (net/tls). It introduces new setsockopt operations (TLS_TX_TRAFFIC_SECRET, TLS_RX_TRAFFIC_SECRET, TLS_TX_KEY_UPDATE), cryptographic key derivation routines using HKDF-Expand-Label (RFC 9846), automatic rekeying logic on TX and RX, and rate-limiting / validation state machines. These changes operate in a core networking subsystem accessible to user space via standard sockets and deserve focused fuzzing.",
"WorthFuzzing": true
} |
| 13/1 | 2026/11/08 13:38 | action | If |
3mArgs:{
"WorthFuzzing": true
}Results: null |
| 14/2 | 2026/11/08 13:38 | agent | kmsan-evaluator |
3mModel:core Args: null Results: {
"KMSANReasoning": "The patch series introduces in-kernel TLS 1.3 KeyUpdate support, including HKDF key derivation (tls_hkdf.c), rekeying routines (tls_keyupdate.c), and new setsockopts (TLS_TX_TRAFFIC_SECRET, TLS_RX_TRAFFIC_SECRET, TLS_TX_KEY_UPDATE).\n\nEvaluating uninitialized memory risks (KMSAN vs KASAN):\n1. No kernel-to-user information leaks: The new options are strictly write-only setsockopts (secrets cannot be read back and getsockopt does not implement them). Existing getsockopt(TLS_TX/TLS_RX) copies crypto_info, which is populated during rekeying from a stack structure (tmp) that is explicitly zeroed via memset(\u0026tmp, 0, sizeof(tmp)) before keys and IVs are copied in.\n2. Structure allocations: All newly added fields in struct tls_context and struct tls_sw_context_rx reside in heap objects allocated with kzalloc_obj, ensuring all new state flags and counters start zeroed.\n3. Stack buffers: Local buffers in HKDF expansion, setsockopt handlers, and message construction (such as info in tls_hkdf_expand_label, body/ctrl in tls_tx_key_update, and data in do_tls_setsockopt_secret) are either explicit zero-initialized, copied from verified sockptr/skb lengths, or sequentially filled before use.\n4. Surrounding buffer accesses: Decrypted KeyUpdate records are consumed and freed (consume_skb) rather than forwarded to userspace.\n\nAny potential bugs in this patch (e.g., buffer indexing/overflows during crypto operations, skb reference counting or use-after-free, or locking races) are under the purview of KASAN and LOCKDEP. KMSAN is not needed as no uninitialized memory read or info-leak risks are introduced.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 72afd7682f8c161f815cd82a531d5464e63bdf6e
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 8 13:37:35 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/networking/tls.rst b/Documentation/networking/tls.rst
index 980c442d7161ae..69752a6d67df47 100644
--- a/Documentation/networking/tls.rst
+++ b/Documentation/networking/tls.rst
@@ -300,6 +300,49 @@ extra byte used by the ContentType field.
[1] https://datatracker.ietf.org/doc/html/rfc8449
+TLS_TX_TRAFFIC_SECRET and TLS_RX_TRAFFIC_SECRET
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+``TLS_TX_TRAFFIC_SECRET`` registers the TLS 1.3 application traffic secret
+of the transmit direction, ``TLS_RX_TRAFFIC_SECRET`` that of the receive
+direction. With a registered secret, the kernel performs KeyUpdate for that
+direction itself (RFC 9846, section 4.7.3). The value is the secret itself.
+TLS 1.3 only, software path only.
+
+Install the key of the direction first, with the ``TLS_TX`` or ``TLS_RX``
+socket option described above. The cipher of that key selects the hash, and
+the secret must have the length of that hash: 32 bytes for SHA-256
+(AES-128-GCM, AES-128-CCM, ChaCha20-Poly1305) or 48 bytes for SHA-384
+(AES-256-GCM). Other ciphers return ``EOPNOTSUPP``.
+
+Each secret can be registered once. ``TLS_RX_TRAFFIC_SECRET`` needs
+``TLS_TX_TRAFFIC_SECRET`` first: a received KeyUpdate can ask for one in
+reply, and only the kernel sees it. ``TLS_TX_TRAFFIC_SECRET`` alone is
+allowed.
+
+If the application installs a new key itself with ``TLS_TX``, both secrets
+are removed. With ``TLS_RX``, the RX secret is removed. KeyUpdate for that
+direction is then left to the application again.
+
+The secrets cannot be read back.
+
+With a registered TX secret, the kernel sends a KeyUpdate before the key
+reaches its record limit (RFC 9846, section 5.5): after 2^24 records, except
+for ChaCha20-Poly1305, which has no limit. It also sends one before the next
+record when a received KeyUpdate asks for it. With a registered RX secret,
+the kernel processes a received KeyUpdate and does not pass the record to
+the application. A peer that sends more than 8 KeyUpdates in one second ends
+the connection.
+
+TLS_TX_KEY_UPDATE
+~~~~~~~~~~~~~~~~~
+
+TLS 1.3 only. Send a KeyUpdate now and install the next TX key. Needs a
+registered TX secret. The value is an ``int``: 0 to send a KeyUpdate, 1 to
+also ask the peer to update its key. Asking the peer needs a registered RX
+secret, and fails with ``EBUSY`` until the peer has answered the previous
+request.
+
Statistics
==========
diff --git a/include/net/tls.h b/include/net/tls.h
index e57bef58851eac..d3123b17030cfc 100644
--- a/include/net/tls.h
+++ b/include/net/tls.h
@@ -63,8 +63,16 @@ struct tls_rec;
#define TLS_HANDSHAKE_KEYUPDATE 24 /* rfc8446 B.3: Key update */
+/* KeyUpdate: msg_type, 24-bit length, request_update (RFC 9846 4.7.3) */
+#define TLS_KEYUPDATE_MSG_SIZE 5
+#define TLS_KEY_UPDATE_NOT_REQUESTED 0
+#define TLS_KEY_UPDATE_REQUESTED 1
+
#define TLS_AAD_SPACE_SIZE 13
+/* SHA-384, the longest hash of a TLS 1.3 cipher suite kTLS supports */
+#define TLS_MAX_TRAFFIC_SECRET_SIZE 48
+
#define TLS_MAX_IV_SIZE 16
#define TLS_MAX_SALT_SIZE 4
#define TLS_TAG_SIZE 16
@@ -139,6 +147,8 @@ struct tls_sw_context_rx {
u8 zc_capable:1;
u8 reader_contended:1;
bool key_update_pending;
+ u8 key_update_count; /* received in this window */
+ unsigned long key_update_window; /* jiffies */
struct tls_strparser strp;
@@ -224,6 +234,12 @@ struct tls_prot_info {
u16 tail_size;
};
+/* A TLS 1.3 application traffic secret. len is 0 if none is registered. */
+struct tls_traffic_secret {
+ u8 data[TLS_MAX_TRAFFIC_SECRET_SIZE];
+ u8 len;
+};
+
struct tls_context {
/* read-only cache line */
struct tls_prot_info prot_info;
@@ -266,6 +282,13 @@ struct tls_context {
union tls_crypto_context crypto_send;
union tls_crypto_context crypto_recv;
+ /* KeyUpdate state, protected by the socket lock */
+ struct tls_traffic_secret tx_secret;
+ struct tls_traffic_secret rx_secret;
+ u64 tx_key_updates; /* KeyUpdates sent */
+ bool tx_key_update_reply; /* peer set update_requested */
+ bool tx_key_update_asked; /* we set it, no answer yet */
+
struct list_head list;
refcount_t refcount;
struct rcu_head rcu;
diff --git a/include/uapi/linux/tls.h b/include/uapi/linux/tls.h
index 1245ab38afc144..c85408cb6ce76f 100644
--- a/include/uapi/linux/tls.h
+++ b/include/uapi/linux/tls.h
@@ -42,6 +42,9 @@
#define TLS_TX_ZEROCOPY_RO 3 /* TX zerocopy (only sendfile now) */
#define TLS_RX_EXPECT_NO_PAD 4 /* Attempt opportunistic zero-copy */
#define TLS_TX_MAX_PAYLOAD_LEN 5 /* Maximum plaintext size */
+#define TLS_TX_TRAFFIC_SECRET 6 /* TLS 1.3 TX traffic secret */
+#define TLS_RX_TRAFFIC_SECRET 7 /* TLS 1.3 RX traffic secret */
+#define TLS_TX_KEY_UPDATE 8 /* Send a TLS 1.3 KeyUpdate now */
/* Supported versions */
#define TLS_VERSION_MINOR(ver) ((ver) & 0xFF)
diff --git a/net/tls/Kconfig b/net/tls/Kconfig
index 4f4d5973a28f49..6183ebe954ee83 100644
--- a/net/tls/Kconfig
+++ b/net/tls/Kconfig
@@ -8,6 +8,8 @@ config TLS
select CRYPTO
select CRYPTO_AES
select CRYPTO_GCM
+ select CRYPTO_LIB_SHA256
+ select CRYPTO_LIB_SHA512
select NET_SOCK_MSG
default n
help
diff --git a/net/tls/Makefile b/net/tls/Makefile
index 4c7d296081ee66..ce8ac6a59f8ced 100644
--- a/net/tls/Makefile
+++ b/net/tls/Makefile
@@ -7,6 +7,7 @@ CFLAGS_trace.o := -I$(src)
obj-$(CONFIG_TLS) += tls.o
-tls-y := tls_main.o tls_sw.o tls_proc.o trace.o tls_strp.o
+tls-y := tls_main.o tls_sw.o tls_proc.o trace.o tls_strp.o tls_hkdf.o \
+ tls_keyupdate.o
tls-$(CONFIG_TLS_DEVICE) += tls_device.o tls_device_fallback.o
diff --git a/net/tls/tls.h b/net/tls/tls.h
index 60a37bdaaa2506..d12c9278c8143a 100644
--- a/net/tls/tls.h
+++ b/net/tls/tls.h
@@ -147,6 +147,11 @@ void tls_strp_abort_strp(struct tls_strparser *strp, int err);
int init_prot_info(struct tls_prot_info *prot,
const struct tls_crypto_info *crypto_info,
const struct tls_cipher_desc *cipher_desc);
+int tls_hkdf_expand_label(const u8 *secret, size_t secret_len,
+ const char *label, u8 *out, size_t out_len);
+int tls_keyupdate_rekey(struct sock *sk, int tx);
+int tls_keyupdate_rx(struct sock *sk, struct sk_buff *skb);
+int tls_sw_key_update(struct sock *sk, int request);
int tls_set_sw_offload(struct sock *sk, int tx,
struct tls_crypto_info *new_crypto_info);
void tls_update_rx_zc_capable(struct tls_context *tls_ctx);
diff --git a/net/tls/tls_hkdf.c b/net/tls/tls_hkdf.c
new file mode 100644
index 00000000000000..49e33146ac7ef2
--- /dev/null
+++ b/net/tls/tls_hkdf.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries.
+ *
+ * TLS 1.3 HKDF-Expand-Label, RFC 9846 section 7.1.
+ *
+ * TLS 1.3 derives all of its secrets and keys with HKDF (RFC 5869). The
+ * TLS handshake runs in user space and gives the kernel one application
+ * traffic secret for each direction. For a KeyUpdate, the kernel derives
+ * the next traffic secret from the current one (section 7.2), and the
+ * new key and IV from the next traffic secret (section 7.3). Both steps
+ * use only HKDF-Expand-Label, so this file implements only that
+ * function, not the full key schedule of the handshake.
+ *
+ * The input is always a traffic secret. It is already a random key of
+ * the hash length, so HKDF-Extract is not needed. No output is longer
+ * than the hash, so HKDF-Expand is a single HMAC.
+ */
+
+#include <crypto/sha2.h>
+#include <linux/errno.h>
+#include <linux/string.h>
+#include <linux/unaligned.h>
+
+#include "tls.h"
+
+#define TLS_HKDF_LABEL_PREFIX "tls13 "
+#define TLS_HKDF_LABEL_PREFIX_LEN (sizeof(TLS_HKDF_LABEL_PREFIX) - 1)
+#define TLS_HKDF_LABEL_MAX 32
+
+/**
+ * tls_hkdf_expand_label - HKDF-Expand-Label with an empty context
+ * @secret: the secret
+ * @secret_len: length of @secret, which selects the hash: SHA-256 for
+ * SHA256_DIGEST_SIZE, SHA-384 for SHA384_DIGEST_SIZE
+ * @label: the label, without the "tls13 " prefix
+ * @out: output buffer
+ * @out_len: number of bytes to derive, at most @secret_len
+ *
+ * Return: 0 on success, -EINVAL if a length is not valid.
+ */
+int tls_hkdf_expand_label(const u8 *secret, size_t secret_len,
+ const char *label, u8 *out, size_t out_len)
+{
+ u8 info[2 + 1 + TLS_HKDF_LABEL_PREFIX_LEN + TLS_HKDF_LABEL_MAX + 1 + 1];
+ size_t label_len = strlen(label);
+ u8 tmp[SHA384_DIGEST_SIZE];
+ size_t pos = 0;
+
+ if (secret_len != SHA256_DIGEST_SIZE &&
+ secret_len != SHA384_DIGEST_SIZE)
+ return -EINVAL;
+ if (!out_len || out_len > secret_len || label_len > TLS_HKDF_LABEL_MAX)
+ return -EINVAL;
+
+ /* struct {
+ * uint16 length;
+ * opaque label<7..255>;
+ * opaque context<0..255>;
+ * } HkdfLabel;
+ *
+ * followed by the HKDF-Expand counter for the first output block.
+ */
+ put_unaligned_be16(out_len, info);
+ pos += 2;
+ info[pos++] = TLS_HKDF_LABEL_PREFIX_LEN + label_len;
+ memcpy(info + pos, TLS_HKDF_LABEL_PREFIX, TLS_HKDF_LABEL_PREFIX_LEN);
+ pos += TLS_HKDF_LABEL_PREFIX_LEN;
+ memcpy(info + pos, label, label_len);
+ pos += label_len;
+ info[pos++] = 0; /* empty context */
+ info[pos++] = 1; /* counter */
+
+ if (secret_len == SHA256_DIGEST_SIZE)
+ hmac_sha256_usingrawkey(secret, secret_len, info, pos, tmp);
+ else
+ hmac_sha384_usingrawkey(secret, secret_len, info, pos, tmp);
+
+ memcpy(out, tmp, out_len);
+ memzero_explicit(tmp, sizeof(tmp));
+
+ return 0;
+}
diff --git a/net/tls/tls_keyupdate.c b/net/tls/tls_keyupdate.c
new file mode 100644
index 00000000000000..b43874b0839353
--- /dev/null
+++ b/net/tls/tls_keyupdate.c
@@ -0,0 +1,160 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries.
+ *
+ * TLS 1.3 KeyUpdate, RFC 9846 sections 4.7.3 and 7.2.
+ *
+ * A TLS 1.3 peer can change the key of the direction it sends in at any
+ * time. It sends a KeyUpdate message, then encrypts all later records
+ * with the next key. A sender must also change its key before the key
+ * has protected too many records (section 5.5).
+ *
+ * The next key is derived from the application traffic secret, not from
+ * the current key. User space gives the kernel only the key and IV, so
+ * the kernel cannot derive the next key on its own. Without the secret,
+ * a received KeyUpdate goes to the application, and the kernel stops
+ * decrypting until the application installs the next key with TLS_RX.
+ *
+ * With TLS_TX_TRAFFIC_SECRET or TLS_RX_TRAFFIC_SECRET, user space also
+ * gives the kernel the secret of that direction. The kernel then handles
+ * KeyUpdate itself. This file derives and installs the next key, and
+ * processes a received KeyUpdate. tls_sw.c sends KeyUpdate messages.
+ */
+
+#include <linux/jiffies.h>
+#include <linux/skbuff.h>
+#include <net/strparser.h>
+#include <net/tls.h>
+
+#include "tls.h"
+
+/* A peer that sends more KeyUpdates than this in one second ends the
+ * connection. Each one costs three HMACs and a new key, and carries no
+ * data.
+ */
+#define TLS_KEYUPDATE_RX_MAX_PER_SEC 8
+
+/**
+ * tls_keyupdate_rekey - install the next key of one direction
+ * @sk: socket
+ * @tx: non-zero for the transmit direction
+ *
+ * Derive the next application traffic secret from the registered one,
+ * then its key and IV, and install them. The record sequence number
+ * restarts at zero.
+ *
+ * Return: 0 on success, or a negative errno. On error the installed key
+ * and the registered secret do not change.
+ */
+int tls_keyupdate_rekey(struct sock *sk, int tx)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ struct tls_traffic_secret *secret;
+ const struct tls_cipher_desc *cipher_desc;
+ u8 iv[TLS_MAX_SALT_SIZE + TLS_MAX_IV_SIZE];
+ u8 next[TLS_MAX_TRAFFIC_SECRET_SIZE];
+ union tls_crypto_context tmp;
+ struct tls_crypto_info *info = (void *)&tmp;
+ int rc;
+
+ secret = tx ? &ctx->tx_secret : &ctx->rx_secret;
+ cipher_desc = get_cipher_desc(ctx->prot_info.cipher_type);
+ if (!cipher_desc || !secret->len ||
+ cipher_desc->salt + cipher_desc->iv > sizeof(iv))
+ return -EINVAL;
+
+ /* version and cipher_type; the rest of tmp stays zero */
+ memset(&tmp, 0, sizeof(tmp));
+ *info = tx ? ctx->crypto_send.info : ctx->crypto_recv.info;
+
+ rc = tls_hkdf_expand_label(secret->data, secret->len, "traffic upd",
+ next, secret->len);
+ if (rc)
+ goto out;
+
+ rc = tls_hkdf_expand_label(next, secret->len, "key",
+ crypto_info_key(info, cipher_desc),
+ cipher_desc->key);
+ if (rc)
+ goto out;
+
+ rc = tls_hkdf_expand_label(next, secret->len, "iv", iv,
+ cipher_desc->salt + cipher_desc->iv);
+ if (rc)
+ goto out;
+
+ memcpy(crypto_info_salt(info, cipher_desc), iv, cipher_desc->salt);
+ memcpy(crypto_info_iv(info, cipher_desc), iv + cipher_desc->salt,
+ cipher_desc->iv);
+
+ rc = tls_set_sw_offload(sk, tx, info);
+ if (rc)
+ goto out;
+
+ memcpy(secret->data, next, secret->len);
+
+out:
+ memzero_explicit(next, sizeof(next));
+ memzero_explicit(iv, sizeof(iv));
+ memzero_explicit(&tmp, sizeof(tmp));
+
+ return rc;
+}
+
+/**
+ * tls_keyupdate_rx - process a received KeyUpdate
+ * @sk: socket
+ * @skb: the decrypted record, which starts with a KeyUpdate message
+ *
+ * Called with the socket locked, for a direction with a registered
+ * secret. The caller does not pass the record to the user.
+ *
+ * Return: 0 on success, or a negative errno if the connection must end.
+ */
+int tls_keyupdate_rx(struct sock *sk, struct sk_buff *skb)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);
+ const struct strp_msg *rxm = strp_msg(skb);
+ u8 msg[TLS_KEYUPDATE_MSG_SIZE];
+ int rc;
+
+ /* A KeyUpdate must end its record (section 5.1). Another message
+ * in the same record is not supported.
+ */
+ if (rxm->full_len != sizeof(msg) ||
+ skb_copy_bits(skb, rxm->offset, msg, sizeof(msg)))
+ return -EBADMSG;
+
+ /* msg_type, a 24-bit length of 1, then request_update */
+ if (msg[1] || msg[2] || msg[3] != 1)
+ return -EBADMSG;
+ if (msg[4] != TLS_KEY_UPDATE_NOT_REQUESTED &&
+ msg[4] != TLS_KEY_UPDATE_REQUESTED)
+ return -EBADMSG;
+
+ if (!rx_ctx->key_update_count ||
+ time_after(jiffies, rx_ctx->key_update_window + HZ)) {
+ rx_ctx->key_update_window = jiffies;
+ rx_ctx->key_update_count = 0;
+ }
+ if (++rx_ctx->key_update_count > TLS_KEYUPDATE_RX_MAX_PER_SEC) {
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYERROR);
+ net_warn_ratelimited("tls: peer sent more than %d KeyUpdates in one second\n",
+ TLS_KEYUPDATE_RX_MAX_PER_SEC);
+ return -EBADMSG;
+ }
+
+ rc = tls_keyupdate_rekey(sk, 0);
+ if (rc) {
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYERROR);
+ return rc;
+ }
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYOK);
+
+ /* Any KeyUpdate from the peer answers our update_requested */
+ ctx->tx_key_update_asked = false;
+ if (msg[4] == TLS_KEY_UPDATE_REQUESTED)
+ ctx->tx_key_update_reply = true;
+
+ return 0;
+}
diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index b8d5a843f76171..b19e6946dbaed9 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c
@@ -31,6 +31,8 @@
* SOFTWARE.
*/
+#include <crypto/sha2.h>
+#include <crypto/utils.h>
#include <linux/module.h>
#include <net/tcp.h>
@@ -325,6 +327,8 @@ void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
+ memzero_explicit(&ctx->tx_secret, sizeof(ctx->tx_secret));
+ memzero_explicit(&ctx->rx_secret, sizeof(ctx->rx_secret));
mutex_destroy(&ctx->tx_lock);
if (sk)
@@ -757,8 +761,20 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
ctx->rx_conf = conf;
update_sk_prot(sk, ctx);
- if (update)
+ if (update) {
+ /* The registered secret no longer matches the key. The RX
+ * secret also needs the TX one, see do_tls_setsockopt_secret().
+ */
+ if (tx) {
+ memzero_explicit(&ctx->tx_secret,
+ sizeof(ctx->tx_secret));
+ ctx->tx_key_update_reply = false;
+ }
+ memzero_explicit(&ctx->rx_secret, sizeof(ctx->rx_secret));
+ /* the answer now goes to user space */
+ ctx->tx_key_update_asked = false;
return 0;
+ }
if (tx) {
ctx->sk_write_space = sk->sk_write_space;
@@ -868,6 +884,159 @@ static int do_tls_setsockopt_tx_payload_len(struct sock *sk, sockptr_t optval,
return 0;
}
+/* Length of the TLS 1.3 traffic secret for @cipher_type, the size of
+ * its hash. 0 if KeyUpdate is not supported for the cipher.
+ */
+static int tls_traffic_secret_len(u16 cipher_type)
+{
+ switch (cipher_type) {
+ case TLS_CIPHER_AES_GCM_128:
+ case TLS_CIPHER_AES_CCM_128:
+ case TLS_CIPHER_CHACHA20_POLY1305:
+ return SHA256_DIGEST_SIZE;
+ case TLS_CIPHER_AES_GCM_256:
+ return SHA384_DIGEST_SIZE;
+ default:
+ return 0;
+ }
+}
+
+/* The installed key and IV must be the ones that @secret gives
+ * (RFC 9846 section 7.3):
+ *
+ * key = HKDF-Expand-Label(secret, "key", "", key_length)
+ * iv = HKDF-Expand-Label(secret, "iv", "", iv_length)
+ *
+ * crypto_info holds the iv as salt followed by iv. Each KeyUpdate
+ * derives the next key from the secret, so a wrong secret fails only
+ * at the first KeyUpdate, with no clear cause.
+ */
+static int tls_check_traffic_secret(struct tls_crypto_info *crypto_info,
+ const u8 *secret, int len)
+{
+ const struct tls_cipher_desc *cipher_desc;
+ u8 iv[TLS_MAX_SALT_SIZE + TLS_MAX_IV_SIZE];
+ u8 key[TLS_CIPHER_AES_GCM_256_KEY_SIZE];
+ int rc;
+
+ cipher_desc = get_cipher_desc(crypto_info->cipher_type);
+ if (!cipher_desc || cipher_desc->key > sizeof(key) ||
+ cipher_desc->salt + cipher_desc->iv > sizeof(iv))
+ return -EINVAL;
+
+ rc = tls_hkdf_expand_label(secret, len, "key", key, cipher_desc->key);
+ if (rc)
+ goto out;
+
+ rc = tls_hkdf_expand_label(secret, len, "iv", iv,
+ cipher_desc->salt + cipher_desc->iv);
+ if (rc)
+ goto out;
+
+ if (crypto_memneq(key, crypto_info_key(crypto_info, cipher_desc),
+ cipher_desc->key) ||
+ crypto_memneq(iv, crypto_info_salt(crypto_info, cipher_desc),
+ cipher_desc->salt) ||
+ crypto_memneq(iv + cipher_desc->salt,
+ crypto_info_iv(crypto_info, cipher_desc),
+ cipher_desc->iv))
+ rc = -EINVAL;
+
+out:
+ memzero_explicit(key, sizeof(key));
+ memzero_explicit(iv, sizeof(iv));
+
+ return rc;
+}
+
+static int do_tls_setsockopt_secret(struct sock *sk, sockptr_t optval,
+ unsigned int optlen, int tx)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ struct tls_crypto_info *crypto_info;
+ struct tls_traffic_secret *secret;
+ u8 data[TLS_MAX_TRAFFIC_SECRET_SIZE];
+ int len, rc;
+
+ if (tx) {
+ crypto_info = &ctx->crypto_send.info;
+ secret = &ctx->tx_secret;
+ } else {
+ crypto_info = &ctx->crypto_recv.info;
+ secret = &ctx->rx_secret;
+ }
+
+ /* The key must be installed first: its cipher selects the hash */
+ if (!TLS_CRYPTO_INFO_READY(crypto_info) ||
+ crypto_info->version != TLS_1_3_VERSION)
+ return -EINVAL;
+
+ if ((tx ? ctx->tx_conf : ctx->rx_conf) != TLS_SW)
+ return -EOPNOTSUPP;
+
+ len = tls_traffic_secret_len(crypto_info->cipher_type);
+ if (!len)
+ return -EOPNOTSUPP;
+
+ if (sockptr_is_null(optval) || optlen != len)
+ return -EINVAL;
+
+ /* A received KeyUpdate may ask for one in reply. The kernel
+ * consumes the record, so only the kernel can send the reply.
+ */
+ if (!tx && !ctx->tx_secret.len)
+ return -EINVAL;
+
+ if (secret->len)
+ return -EBUSY;
+
+ if (copy_from_sockptr(data, optval, len)) {
+ rc = -EFAULT;
+ goto out;
+ }
+
+ rc = tls_check_traffic_secret(crypto_info, data, len);
+ if (rc)
+ goto out;
+
+ memcpy(secret->data, data, len);
+ secret->len = len;
+
+out:
+ memzero_explicit(data, sizeof(data));
+
+ return rc;
+}
+
+static int do_tls_setsockopt_key_update(struct sock *sk, sockptr_t optval,
+ unsigned int optlen)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ int value, rc;
+
+ if (sockptr_is_null(optval) || optlen != sizeof(value))
+ return -EINVAL;
+
+ if (copy_from_sockptr(&value, optval, sizeof(value)))
+ return -EFAULT;
+
+ if (value != 0 && value != 1)
+ return -EINVAL;
+
+ rc = mutex_lock_interruptible(&ctx->tx_lock);
+ if (rc)
+ return rc;
+ lock_sock(sk);
+ if (ctx->tx_conf == TLS_SW)
+ rc = tls_sw_key_update(sk, value);
+ else
+ rc = -EINVAL;
+ release_sock(sk);
+ mutex_unlock(&ctx->tx_lock);
+
+ return rc;
+}
+
static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
unsigned int optlen)
{
@@ -901,6 +1070,16 @@ static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
release_sock(sk);
mutex_unlock(&ctx->tx_lock);
break;
+ case TLS_TX_TRAFFIC_SECRET:
+ case TLS_RX_TRAFFIC_SECRET:
+ lock_sock(sk);
+ rc = do_tls_setsockopt_secret(sk, optval, optlen,
+ optname == TLS_TX_TRAFFIC_SECRET);
+ release_sock(sk);
+ break;
+ case TLS_TX_KEY_UPDATE:
+ rc = do_tls_setsockopt_key_update(sk, optval, optlen);
+ break;
default:
rc = -ENOPROTOOPT;
break;
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 771e02bff51999..433a43d13a8729 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -40,6 +40,7 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/splice.h>
+#include <linux/unaligned.h>
#include <crypto/aead.h>
#include <net/strparser.h>
@@ -763,6 +764,8 @@ static int tls_sw_sendmsg_splice(struct sock *sk, struct msghdr *msg,
return 0;
}
+static int tls_tx_maybe_update_key(struct sock *sk, int flags);
+
static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
size_t size)
{
@@ -805,6 +808,19 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
goto send_end;
}
+ /* Check before each new record: one call can write many
+ * records, and a KeyUpdate reply must go before the next
+ * one. A send that failed before it copied any data leaves
+ * an empty open record. The KeyUpdate itself is a handshake
+ * record.
+ */
+ if ((!ctx->open_rec || !ctx->open_rec->msg_plaintext.sg.size) &&
+ record_type == TLS_RECORD_TYPE_DATA) {
+ ret = tls_tx_maybe_update_key(sk, msg->msg_flags);
+ if (ret)
+ goto send_end;
+ }
+
if (ctx->open_rec)
rec = ctx->open_rec;
else
@@ -1005,6 +1021,153 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
return copied > 0 ? copied : ret;
}
+/* RFC 9846 section 5.5 allows 2^24.5 full-size records per AES-GCM key.
+ * Send the KeyUpdate a little earlier.
+ */
+#define TLS_KEYUPDATE_TX_RECORDS BIT_ULL(24)
+
+/* Section 4.7.3: the epoch must not exceed 2^48-1. The application
+ * traffic secrets start at epoch 3.
+ */
+#define TLS_KEYUPDATE_MAX (BIT_ULL(48) - 4)
+
+/* Send a KeyUpdate with the current key, then install the next key.
+ * Called with tx_lock held and the socket locked.
+ */
+static int tls_tx_key_update(struct sock *sk, u8 request, int flags)
+{
+ u8 body[TLS_KEYUPDATE_MSG_SIZE] = {
+ TLS_HANDSHAKE_KEYUPDATE, 0, 0, 1, request
+ };
+ struct tls_context *tls_ctx = tls_get_ctx(sk);
+ struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+ u8 record_type = TLS_RECORD_TYPE_HANDSHAKE;
+ union {
+ struct cmsghdr cmsg;
+ u8 buf[CMSG_SPACE(sizeof(record_type))];
+ } ctrl = {};
+ struct msghdr msg = {};
+ struct cmsghdr *cmsg;
+ struct kvec iov;
+ int ret;
+
+ msg.msg_control = &ctrl;
+ msg.msg_controllen = sizeof(ctrl);
+ msg.msg_flags = flags & (MSG_DONTWAIT | MSG_NOSIGNAL);
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_TLS;
+ cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(record_type));
+ memcpy(CMSG_DATA(cmsg), &record_type, sizeof(record_type));
+
+ iov.iov_base = body;
+ iov.iov_len = sizeof(body);
+ iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, sizeof(body));
+
+ /* A control record closes the open record first, so all data
+ * written so far goes out with the current key, ahead of the
+ * KeyUpdate.
+ */
+ ret = tls_sw_sendmsg_locked(sk, &msg, sizeof(body));
+ if (ret < 0)
+ return ret;
+
+ /* Nothing may be encrypted with the next key before the KeyUpdate
+ * is encrypted with the current one.
+ */
+ ret = tls_encrypt_async_wait(ctx);
+ if (!ret)
+ ret = tls_keyupdate_rekey(sk, 1);
+ if (ret) {
+ /* The peer expects the next key: the connection is lost */
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXREKEYERROR);
+ tls_err_abort(sk, ret);
+ return ret;
+ }
+
+ TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXREKEYOK);
+ tls_ctx->tx_key_updates++;
+ if (request == TLS_KEY_UPDATE_NOT_REQUESTED)
+ tls_ctx->tx_key_update_reply = false;
+
+ return 0;
+}
+
+/* Send a KeyUpdate if one is due: in reply to the peer's
+ * update_requested (RFC 9846 section 4.7.3), or before the key reaches
+ * its record limit (section 5.5). ChaCha20-Poly1305 has no record limit:
+ * the sequence number would wrap first.
+ */
+static int tls_tx_maybe_update_key(struct sock *sk, int flags)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ bool limit;
+
+ if (likely(!ctx->tx_secret.len))
+ return 0;
+
+ limit = ctx->prot_info.cipher_type != TLS_CIPHER_CHACHA20_POLY1305 &&
+ get_unaligned_be64(ctx->tx.rec_seq) >= TLS_KEYUPDATE_TX_RECORDS;
+ if (!limit && !ctx->tx_key_update_reply)
+ return 0;
+
+ if (ctx->tx_key_updates >= TLS_KEYUPDATE_MAX) {
+ /* Ignore update_requested. At the record limit the
+ * connection must end.
+ */
+ ctx->tx_key_update_reply = false;
+ return limit ? -EKEYEXPIRED : 0;
+ }
+
+ return tls_tx_key_update(sk, TLS_KEY_UPDATE_NOT_REQUESTED, flags);
+}
+
+/**
+ * tls_sw_key_update - send a KeyUpdate now, for TLS_TX_KEY_UPDATE
+ * @sk: socket
+ * @request: non-zero to also ask the peer to update its key
+ *
+ * Called with tx_lock held and the socket locked.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+int tls_sw_key_update(struct sock *sk, int request)
+{
+ struct tls_context *ctx = tls_get_ctx(sk);
+ int ret;
+
+ if (!ctx->tx_secret.len)
+ return -EINVAL;
+
+ if (request) {
+ /* Only the kernel sees the answer */
+ if (!ctx->rx_secret.len)
+ return -EINVAL;
+ /* Section 4.7.3: wait for the answer to the previous one */
+ if (ctx->tx_key_update_asked)
+ return -EBUSY;
+ }
+
+ if (ctx->tx_key_updates >= TLS_KEYUPDATE_MAX - 1)
+ return -EKEYEXPIRED;
+
+ /* A reply must carry update_not_requested */
+ if (request && ctx->tx_key_update_reply) {
+ ret = tls_tx_key_update(sk, TLS_KEY_UPDATE_NOT_REQUESTED,
+ MSG_NOSIGNAL);
+ if (ret)
+ return ret;
+ }
+
+ ret = tls_tx_key_update(sk, request ? TLS_KEY_UPDATE_REQUESTED :
+ TLS_KEY_UPDATE_NOT_REQUESTED, MSG_NOSIGNAL);
+ if (!ret && request)
+ ctx->tx_key_update_asked = true;
+
+ return ret;
+}
+
int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
{
struct tls_context *tls_ctx = tls_get_ctx(sk);
@@ -1551,15 +1714,28 @@ static int tls_check_pending_rekey(struct sock *sk, struct tls_context *ctx,
if (hs_type == TLS_HANDSHAKE_KEYUPDATE) {
struct tls_sw_context_rx *rx_ctx = ctx->priv_ctx_rx;
- WRITE_ONCE(rx_ctx->key_update_pending, true);
TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYRECEIVED);
+
+ if (ctx->rx_secret.len) {
+ err = tls_keyupdate_rx(sk, skb);
+ if (err) {
+ tls_err_abort(sk, err);
+ /* callers free the record only on success */
+ consume_skb(skb);
+ return err;
+ }
+ return 1;
+ }
+
+ WRITE_ONCE(rx_ctx->key_update_pending, true);
}
return 0;
}
/* On decrypt failure the connection is aborted (sk_err set) before
- * returning a negative errno.
+ * returning a negative errno. Return 1 for a record the kernel consumed
+ * (a KeyUpdate): the caller frees it and does not pass it to the user.
*/
static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,
struct tls_decrypt_arg *darg)
@@ -1883,6 +2059,11 @@ int tls_sw_recvmsg(struct sock *sk,
err = tls_rx_one_record(sk, msg, &darg);
if (err < 0)
goto recv_end;
+ if (err > 0) {
+ tls_rx_rec_done(ctx);
+ consume_skb(darg.skb);
+ continue;
+ }
async |= darg.async;
@@ -2023,9 +2204,8 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
if (err)
goto splice_read_end;
- if (!skb_queue_empty(&ctx->rx_list)) {
- skb = __skb_dequeue(&ctx->rx_list);
- } else {
+ skb = __skb_dequeue(&ctx->rx_list);
+ while (!skb) {
struct tls_decrypt_arg darg;
err = tls_rx_rec_wait(sk, nonblock, true, false);
@@ -2039,7 +2219,10 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
goto splice_read_end;
tls_rx_rec_done(ctx);
- skb = darg.skb;
+ if (err > 0)
+ consume_skb(darg.skb);
+ else
+ skb = darg.skb;
}
rxm = strp_msg(skb);
@@ -2121,6 +2304,11 @@ int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
err = tls_rx_one_record(sk, NULL, &darg);
if (err < 0)
goto read_sock_end;
+ if (err > 0) {
+ tls_rx_rec_done(ctx);
+ consume_skb(darg.skb);
+ continue;
+ }
released = tls_read_flush_backlog(sk, prot, INT_MAX,
0, decrypted,
@@ -2520,6 +2708,12 @@ static void tls_finish_key_update(struct sock *sk, struct tls_context *tls_ctx)
{
struct tls_sw_context_rx *ctx = tls_ctx->priv_ctx_rx;
+ /* Only a pending KeyUpdate holds poll() back. The in-kernel rekey
+ * runs while the KeyUpdate record is still queued.
+ */
+ if (!READ_ONCE(ctx->key_update_pending))
+ return;
+
WRITE_ONCE(ctx->key_update_pending, false);
/* wake-up pre-existing poll() */
ctx->saved_data_ready(sk);
diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config
index 7a89e2618d4c45..b75692f2576597 100644
--- a/tools/testing/selftests/net/config
+++ b/tools/testing/selftests/net/config
@@ -10,8 +10,12 @@ CONFIG_CAN_VXCAN=m
CONFIG_CGROUPS=y
CONFIG_CRYPTO_ARIA=y
CONFIG_CRYPTO_CHACHA20POLY1305=m
+CONFIG_CRYPTO_HMAC=y
CONFIG_CRYPTO_SHA1=y
+CONFIG_CRYPTO_SHA256=y
+CONFIG_CRYPTO_SHA512=y
CONFIG_CRYPTO_SM4_GENERIC=y
+CONFIG_CRYPTO_USER_API_HASH=y
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_INFO_BTF=y
CONFIG_DEBUG_INFO_BTF_MODULES=n
diff --git a/tools/testing/selftests/net/tls.c b/tools/testing/selftests/net/tls.c
index c4203401c4bfc2..ed3b510a5ef6fe 100644
--- a/tools/testing/selftests/net/tls.c
+++ b/tools/testing/selftests/net/tls.c
@@ -11,6 +11,7 @@
#include <stdlib.h>
#include <unistd.h>
+#include <linux/if_alg.h>
#include <linux/tls.h>
#include <linux/tcp.h>
#include <linux/socket.h>
@@ -2421,6 +2422,525 @@ TEST_F(tls, rekey_poll_delay)
}
}
+#ifndef SOL_ALG
+#define SOL_ALG 279
+#endif
+
+/* RFC 9846 section 7.1, HKDF-Expand-Label with an empty context, for an
+ * output no longer than the hash. The HMAC comes from the kernel.
+ */
+static bool hkdf_expand_label(const unsigned char *secret, int secret_len,
+ const char *label, unsigned char *out,
+ int out_len)
+{
+ struct sockaddr_alg sa = {
+ .salg_family = AF_ALG,
+ .salg_type = "hash",
+ };
+ unsigned char info[2 + 1 + 255 + 1 + 1], md[48];
+ int tfm, op, n = 0, label_len = 6 + strlen(label);
+ bool ok = false;
+
+ strcpy((char *)sa.salg_name,
+ secret_len == 48 ? "hmac(sha384)" : "hmac(sha256)");
+
+ info[n++] = out_len >> 8;
+ info[n++] = out_len;
+ info[n++] = label_len;
+ memcpy(info + n, "tls13 ", 6);
+ memcpy(info + n + 6, label, label_len - 6);
+ n += label_len;
+ info[n++] = 0; /* context */
+ info[n++] = 1; /* HKDF-Expand counter */
+
+ tfm = socket(AF_ALG, SOCK_SEQPACKET, 0);
+ if (tfm < 0)
+ return false;
+ if (bind(tfm, (struct sockaddr *)&sa, sizeof(sa)) ||
+ setsockopt(tfm, SOL_ALG, ALG_SET_KEY, secret, secret_len))
+ goto out;
+
+ op = accept(tfm, NULL, 0);
+ if (op < 0)
+ goto out;
+ ok = write(op, info, n) == n && read(op, md, secret_len) == secret_len;
+ close(op);
+ if (ok)
+ memcpy(out, md, out_len);
+out:
+ close(tfm);
+ return ok;
+}
+
+/* Fill @keys with the key and IV that @secret gives, record number 0 */
+static bool keyupdate_keys(uint16_t cipher_type, const unsigned char *secret,
+ int secret_len, struct tls_crypto_info_keys *keys)
+{
+ unsigned char *key, *salt, *iv;
+ int key_len, salt_len, iv_len;
+ unsigned char nonce[12];
+
+ tls_crypto_info_init(TLS_1_3_VERSION, cipher_type, keys, 0);
+
+ switch (cipher_type) {
+ case TLS_CIPHER_AES_GCM_128:
+ key = keys->aes128.key;
+ salt = keys->aes128.salt;
+ iv = keys->aes128.iv;
+ key_len = sizeof(keys->aes128.key);
+ salt_len = sizeof(keys->aes128.salt);
+ iv_len = sizeof(keys->aes128.iv);
+ break;
+ case TLS_CIPHER_AES_GCM_256:
+ key = keys->aesgcm256.key;
+ salt = keys->aesgcm256.salt;
+ iv = keys->aesgcm256.iv;
+ key_len = sizeof(keys->aesgcm256.key);
+ salt_len = sizeof(keys->aesgcm256.salt);
+ iv_len = sizeof(keys->aesgcm256.iv);
+ break;
+ case TLS_CIPHER_AES_CCM_128:
+ key = keys->aesccm128.key;
+ salt = keys->aesccm128.salt;
+ iv = keys->aesccm128.iv;
+ key_len = sizeof(keys->aesccm128.key);
+ salt_len = sizeof(keys->aesccm128.salt);
+ iv_len = sizeof(keys->aesccm128.iv);
+ break;
+ case TLS_CIPHER_CHACHA20_POLY1305:
+ key = keys->chacha20.key;
+ salt = keys->chacha20.salt;
+ iv = keys->chacha20.iv;
+ key_len = sizeof(keys->chacha20.key);
+ salt_len = 0;
+ iv_len = sizeof(keys->chacha20.iv);
+ break;
+ default:
+ return false;
+ }
+
+ if (!hkdf_expand_label(secret, secret_len, "key", key, key_len) ||
+ !hkdf_expand_label(secret, secret_len, "iv", nonce,
+ salt_len + iv_len))
+ return false;
+
+ memcpy(salt, nonce, salt_len);
+ memcpy(iv, nonce + salt_len, iv_len);
+ return true;
+}
+
+/* In-kernel KeyUpdate: fd and cfd both register the TX and RX traffic
+ * secrets of a bidirectional connection. fd -> cfd starts at record
+ * number @seq. Returns false if the variant does not support it.
+ */
+static bool keyupdate_setup_seq(struct __test_metadata *_metadata,
+ FIXTURE_DATA(tls) *self,
+ const FIXTURE_VARIANT(tls) *variant,
+ uint64_t seq)
+{
+ struct tls_crypto_info_keys keys_a, keys_b;
+ unsigned char secret_a[48], secret_b[48];
+ unsigned char *rec_seq;
+ int len, i;
+
+ if (self->notls || variant->tls_version != TLS_1_3_VERSION)
+ return false;
+
+ switch (variant->cipher_type) {
+ case TLS_CIPHER_AES_GCM_128:
+ case TLS_CIPHER_AES_CCM_128:
+ case TLS_CIPHER_CHACHA20_POLY1305:
+ len = 32;
+ break;
+ case TLS_CIPHER_AES_GCM_256:
+ len = 48;
+ break;
+ default:
+ return false;
+ }
+
+ memset(secret_a, 0xa5, sizeof(secret_a));
+ memset(secret_b, 0x5a, sizeof(secret_b));
+
+ /* the keys must be the ones that the secrets give */
+ ASSERT_TRUE(keyupdate_keys(variant->cipher_type, secret_a, len,
+ &keys_a));
+ ASSERT_TRUE(keyupdate_keys(variant->cipher_type, secret_b, len,
+ &keys_b));
+
+ /* rec_seq is the last member of each crypto_info */
+ rec_seq = (unsigned char *)&keys_a + keys_a.len - 8;
+ for (i = 0; i < 8; i++)
+ rec_seq[i] = seq >> (56 - 8 * i);
+
+ /* fd -> cfd: replace the keys of the fixture */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &keys_a,
+ keys_a.len), 0);
+ EXPECT_EQ(setsockopt(self->cfd, SOL_TLS, TLS_RX, &keys_a,
+ keys_a.len), 0);
+ /* cfd -> fd */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX, &keys_b,
+ keys_b.len), 0);
+ EXPECT_EQ(setsockopt(self->cfd, SOL_TLS, TLS_TX, &keys_b,
+ keys_b.len), 0);
+
+ /* the RX secret needs the TX secret first */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret_b, len), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret_a, len), 0);
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret_b, len), 0);
+ EXPECT_EQ(setsockopt(self->cfd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret_b, len), 0);
+ EXPECT_EQ(setsockopt(self->cfd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret_a, len), 0);
+
+ return true;
+}
+
+static bool keyupdate_setup(struct __test_metadata *_metadata,
+ FIXTURE_DATA(tls) *self,
+ const FIXTURE_VARIANT(tls) *variant)
+{
+ return keyupdate_setup_seq(_metadata, self, variant, 0);
+}
+
+static void keyupdate_send(struct __test_metadata *_metadata, int fd,
+ int request)
+{
+ EXPECT_EQ(setsockopt(fd, SOL_TLS, TLS_TX_KEY_UPDATE,
+ &request, sizeof(request)), 0);
+}
+
+static void send_recv_str(struct __test_metadata *_metadata, int from, int to,
+ const char *str)
+{
+ int len = strlen(str) + 1;
+ char buf[64];
+
+ EXPECT_EQ(send(from, str, len, 0), len);
+ EXPECT_EQ(recv(to, buf, sizeof(buf), 0), len);
+ EXPECT_EQ(memcmp(buf, str, len), 0);
+}
+
+TEST_F(tls, keyupdate_secret_errors)
+{
+ unsigned char secret[48] = {};
+ int one = 1;
+
+ if (self->notls)
+ SKIP(return, "no TLS support");
+
+ /* no secret registered */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_KEY_UPDATE,
+ &one, sizeof(one)), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ if (variant->tls_version != TLS_1_3_VERSION) {
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret, 32), -1);
+ EXPECT_EQ(errno, EINVAL);
+ return;
+ }
+
+ if (variant->cipher_type == TLS_CIPHER_SM4_GCM ||
+ variant->cipher_type == TLS_CIPHER_SM4_CCM) {
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret, 32), -1);
+ EXPECT_EQ(errno, EOPNOTSUPP);
+ return;
+ }
+
+ /* the RX key is not installed on fd */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret, 32), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ /* wrong length */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET,
+ secret, 16), -1);
+ EXPECT_EQ(errno, EINVAL);
+
+ /* the installed key does not come from this secret */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_TRAFFIC_SECRET, secret,
+ variant->cipher_type == TLS_CIPHER_AES_GCM_256 ?
+ 48 : 32), -1);
+ EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(tls, keyupdate)
+{
+ int i;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ send_recv_str(_metadata, self->fd, self->cfd, "before");
+
+ /* stay below the limit of 8 received KeyUpdates per second */
+ for (i = 0; i < 4; i++) {
+ keyupdate_send(_metadata, self->fd, 0);
+ send_recv_str(_metadata, self->fd, self->cfd, "after");
+ keyupdate_send(_metadata, self->cfd, 0);
+ send_recv_str(_metadata, self->cfd, self->fd, "reverse");
+ }
+}
+
+TEST_F(tls, keyupdate_queued)
+{
+ const char *str = "queued behind a KeyUpdate";
+ int len = strlen(str) + 1;
+ char buf[64];
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ /* the record before the KeyUpdate, the KeyUpdate and the record
+ * after it are all queued before the first read
+ */
+ EXPECT_EQ(send(self->fd, str, len, MSG_MORE), len);
+ keyupdate_send(_metadata, self->fd, 0);
+ EXPECT_EQ(send(self->fd, str, len, 0), len);
+
+ EXPECT_EQ(recv(self->cfd, buf, len, MSG_WAITALL), len);
+ EXPECT_EQ(memcmp(buf, str, len), 0);
+ EXPECT_EQ(recv(self->cfd, buf, len, MSG_WAITALL), len);
+ EXPECT_EQ(memcmp(buf, str, len), 0);
+}
+
+TEST_F(tls, keyupdate_request)
+{
+ int one = 1;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ keyupdate_send(_metadata, self->fd, 1);
+
+ /* no answer yet */
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_KEY_UPDATE,
+ &one, sizeof(one)), -1);
+ EXPECT_EQ(errno, EBUSY);
+
+ /* cfd processes the request, then answers ahead of its data */
+ send_recv_str(_metadata, self->fd, self->cfd, "request");
+ send_recv_str(_metadata, self->cfd, self->fd, "answer");
+
+ /* answered */
+ keyupdate_send(_metadata, self->fd, 1);
+ send_recv_str(_metadata, self->fd, self->cfd, "request");
+ send_recv_str(_metadata, self->cfd, self->fd, "answer");
+}
+
+TEST_F(tls, keyupdate_splice)
+{
+ const char *str = "spliced after a KeyUpdate";
+ int len = strlen(str) + 1;
+ char buf[64];
+ int p[2];
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ ASSERT_GE(pipe(p), 0);
+
+ keyupdate_send(_metadata, self->fd, 0);
+ EXPECT_EQ(send(self->fd, str, len, 0), len);
+
+ EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, len, 0), len);
+ EXPECT_EQ(read(p[0], buf, len), len);
+ EXPECT_EQ(memcmp(buf, str, len), 0);
+
+ close(p[0]);
+ close(p[1]);
+}
+
+TEST_F(tls, keyupdate_manual_rekey)
+{
+ struct tls_crypto_info_keys tls12;
+ int zero = 0;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ /* a key installed by the user removes the registered secrets */
+ tls_crypto_info_init(variant->tls_version, variant->cipher_type,
+ &tls12, 2);
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len), 0);
+
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_KEY_UPDATE,
+ &zero, sizeof(zero)), -1);
+ EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(tls, keyupdate_manual_rekey_request)
+{
+ struct tls_crypto_info_keys keys;
+ unsigned char secret[48];
+ int len;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ keyupdate_send(_metadata, self->fd, 1);
+
+ /* the answer to the request now goes to user space */
+ len = variant->cipher_type == TLS_CIPHER_AES_GCM_256 ? 48 : 32;
+ memset(secret, 0x3c, sizeof(secret));
+ ASSERT_TRUE(keyupdate_keys(variant->cipher_type, secret, len, &keys));
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX, &keys, keys.len), 0);
+
+ EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_RX_TRAFFIC_SECRET,
+ secret, len), 0);
+
+ keyupdate_send(_metadata, self->fd, 1);
+}
+
+TEST_F(tls, keyupdate_nosignal)
+{
+ int zero = 0;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ /* The default action of SIGPIPE ends the test. The result is not
+ * checked: as for sendmsg(), a send error may not reach the caller.
+ */
+ EXPECT_EQ(shutdown(self->fd, SHUT_WR), 0);
+ setsockopt(self->fd, SOL_TLS, TLS_TX_KEY_UPDATE, &zero, sizeof(zero));
+}
+
+TEST_F(tls, keyupdate_rate_limit)
+{
+ char buf[64];
+ int i;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ /* the ninth KeyUpdate in one second ends the connection */
+ for (i = 0; i < 9; i++)
+ keyupdate_send(_metadata, self->fd, 0);
+ EXPECT_EQ(send(self->fd, "x", 1, 0), 1);
+
+ EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), 0), -1);
+ EXPECT_EQ(errno, EBADMSG);
+}
+
+TEST_F(tls, keyupdate_record_limit)
+{
+ struct tls_crypto_info_keys get;
+ char sbuf[1024], rbuf[1024];
+ socklen_t optlen;
+ unsigned char *rec_seq;
+ __u16 limit = 128;
+ int i;
+
+ if (variant->cipher_type == TLS_CIPHER_CHACHA20_POLY1305)
+ SKIP(return, "no record limit for ChaCha20-Poly1305");
+ if (!keyupdate_setup_seq(_metadata, self, variant, (1 << 24) - 2))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ ASSERT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN,
+ &limit, sizeof(limit)), 0);
+ for (i = 0; i < sizeof(sbuf); i++)
+ sbuf[i] = i;
+
+ /* one call writes 8 records or more, past the record limit */
+ EXPECT_EQ(send(self->fd, sbuf, sizeof(sbuf), 0), sizeof(sbuf));
+ EXPECT_EQ(recv(self->cfd, rbuf, sizeof(rbuf), MSG_WAITALL),
+ sizeof(rbuf));
+ EXPECT_EQ(memcmp(sbuf, rbuf, sizeof(sbuf)), 0);
+
+ /* the KeyUpdate in the middle of the call restarted at 0 */
+ tls_crypto_info_init(variant->tls_version, variant->cipher_type,
+ &get, 0);
+ optlen = get.len;
+ ASSERT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &optlen), 0);
+ rec_seq = (unsigned char *)&get + optlen - 8;
+ for (i = 0; i < 5; i++)
+ EXPECT_EQ(rec_seq[i], 0);
+}
+
+/* A send that fails with EAGAIN before it copies any data leaves an
+ * empty open record. The next send must still answer a KeyUpdate
+ * request before its data.
+ */
+TEST_F(tls, keyupdate_request_after_eagain)
+{
+ struct tls_crypto_info_keys get;
+ size_t sent = 0, rcvd = 0;
+ char buf[16384];
+ unsigned char *rec_seq;
+ socklen_t optlen;
+ int size = 4096;
+ ssize_t ret;
+ int i;
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ ASSERT_EQ(setsockopt(self->fd, SOL_SOCKET, SO_SNDBUF, &size,
+ sizeof(size)), 0);
+ ASSERT_EQ(setsockopt(self->cfd, SOL_SOCKET, SO_RCVBUF, &size,
+ sizeof(size)), 0);
+ memset(buf, 0x42, sizeof(buf));
+
+ /* fill the send buffer until a send copies nothing */
+ for (;;) {
+ ret = send(self->fd, buf, sizeof(buf), MSG_DONTWAIT);
+ if (ret < 0)
+ break;
+ sent += ret;
+ }
+ ASSERT_EQ(errno, EAGAIN);
+
+ /* fd reads the request; its answer is now due */
+ keyupdate_send(_metadata, self->cfd, 1);
+ EXPECT_EQ(recv(self->fd, buf, sizeof(buf), MSG_DONTWAIT), -1);
+ EXPECT_EQ(errno, EAGAIN);
+
+ while (rcvd < sent) {
+ ret = recv(self->cfd, buf, sizeof(buf), 0);
+ ASSERT_GT(ret, 0);
+ rcvd += ret;
+ }
+
+ /* the answer goes out before this data */
+ send_recv_str(_metadata, self->fd, self->cfd, "data");
+
+ tls_crypto_info_init(variant->tls_version, variant->cipher_type,
+ &get, 0);
+ optlen = get.len;
+ ASSERT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &optlen), 0);
+ rec_seq = (unsigned char *)&get + optlen - 8;
+ for (i = 0; i < 7; i++)
+ EXPECT_EQ(rec_seq[i], 0);
+ EXPECT_EQ(rec_seq[7], 1);
+
+ /* cfd got the answer, so it can ask again */
+ keyupdate_send(_metadata, self->cfd, 1);
+}
+
+TEST_F(tls, keyupdate_invalid)
+{
+ /* key_update, length 1, request_update 2 */
+ static const char bad[] = "\x18\x00\x00\x01\x02";
+ char buf[64];
+
+ if (!keyupdate_setup(_metadata, self, variant))
+ SKIP(return, "no in-kernel KeyUpdate for this variant");
+
+ EXPECT_EQ(tls_send_cmsg(self->fd, TLS_RECORD_TYPE_HANDSHAKE,
+ (char *)bad, 5, 0), 5);
+
+ EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), 0), -1);
+ EXPECT_EQ(errno, EBADMSG);
+}
+
struct raw_rec {
unsigned int plain_len;
unsigned char plain_data[100];
|
| 15/3 | 2026/11/08 13:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 20747 output: 32 thoughts: 510 Thoughts: **KMSAN Fuzzing Session Evaluation** Okay, so I need to decide if this patch series warrants a dedicated KMSAN fuzzing session. Let's break this down systematically, keeping in mind the established rules. I know that KASAN catches a wide range of memory errors, like out-of-bounds accesses, use-after-free, and locking bugs. KMSAN, on the other hand, is specifically about uninitialized memory reads and kernel-to-user memory leaks. My rule of thumb is: if KASAN or another standard detector can likely catch the bug, skip the KMSAN session. I've got a checklist for when KMSAN becomes necessary: patches that introduce or modify kernel structures sent to userspace, conditional logic on potentially uninitialized variables, allocation and initialization of complex structures where uninitialized fields could be read later, and changes to bounds checks that might expose uninitialized bytes. Now, let's look at this patch. It's adding TLS 1.3 KeyUpdate support in kTLS, which involves new socket options: `TLS_TX_TRAFFIC_SECRET`, `TLS_RX_TRAFFIC_SECRET`, and `TLS_TX_KEY_UPDATE`. The documentation specifically states that the secrets cannot be read back. My immediate concern is: does `getsockopt` somehow get modified to allow leaking these secrets? Let me make sure that `getsockopt` *isn't* touched by this patch, or that if it is, the patch handles the potential leak. I know the patch primarily deals with `do_tls_setsockopt` for setting these new options. But, just to be thorough, I'm going to search `do_tls_getsockopt` in `net/tls/tls_main.c` just in case. |
| 16/3 | 2026/11/08 13:38 | tool | grepper |
0mArgs:{
"Expression": "do_tls_getsockopt",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "net/tls/tls_main.c=401=static __poll_t tls_sk_poll(struct file *file, struct socket *sock,\n--\nnet/tls/tls_main.c-428-\nnet/tls/tls_main.c:429:static int do_tls_getsockopt_conf(struct sock *sk, sockopt_t *opt, int tx)\nnet/tls/tls_main.c-430-{\n--\nnet/tls/tls_main.c-487-\nnet/tls/tls_main.c:488:static int do_tls_getsockopt_tx_zc(struct sock *sk, sockopt_t *opt)\nnet/tls/tls_main.c-489-{\n--\nnet/tls/tls_main.c-503-\nnet/tls/tls_main.c:504:static int do_tls_getsockopt_no_pad(struct sock *sk, sockopt_t *opt)\nnet/tls/tls_main.c-505-{\n--\nnet/tls/tls_main.c-527-\nnet/tls/tls_main.c:528:static int do_tls_getsockopt_tx_payload_len(struct sock *sk, sockopt_t *opt)\nnet/tls/tls_main.c-529-{\n--\nnet/tls/tls_main.c-544-\nnet/tls/tls_main.c:545:static int do_tls_getsockopt(struct sock *sk, int optname, sockopt_t *opt)\nnet/tls/tls_main.c-546-{\n--\nnet/tls/tls_main.c-553-\tcase TLS_RX:\nnet/tls/tls_main.c:554:\t\trc = do_tls_getsockopt_conf(sk, opt, optname == TLS_TX);\nnet/tls/tls_main.c-555-\t\tbreak;\nnet/tls/tls_main.c-556-\tcase TLS_TX_ZEROCOPY_RO:\nnet/tls/tls_main.c:557:\t\trc = do_tls_getsockopt_tx_zc(sk, opt);\nnet/tls/tls_main.c-558-\t\tbreak;\nnet/tls/tls_main.c-559-\tcase TLS_RX_EXPECT_NO_PAD:\nnet/tls/tls_main.c:560:\t\trc = do_tls_getsockopt_no_pad(sk, opt);\nnet/tls/tls_main.c-561-\t\tbreak;\nnet/tls/tls_main.c-562-\tcase TLS_TX_MAX_PAYLOAD_LEN:\nnet/tls/tls_main.c:563:\t\trc = do_tls_getsockopt_tx_payload_len(sk, opt);\nnet/tls/tls_main.c-564-\t\tbreak;\n--\nnet/tls/tls_main.c=575=static int tls_getsockopt(struct sock *sk, int level, int optname,\n--\nnet/tls/tls_main.c-589-\nnet/tls/tls_main.c:590:\terr = do_tls_getsockopt(sk, optname, \u0026opt);\nnet/tls/tls_main.c-591-\tif (err)\n"
} |
| 17/3 | 2026/11/08 13:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22511 output: 37 thoughts: 176 Thoughts: **Investigating Potential Data Corruption in TLS_TX Getsockopt** Okay, so I'm looking at this code and immediately something jumps out at me. The standard `getsockopt` function should be returning `-ENOPROTOOPT` for options it doesn't recognize – and the implication is that this part of the code hasn't been modified to handle some of these newer options. But then, hang on a second... what about the `TLS_TX` getsockopt? Is it possible this is going to be a problem? Now I see this comment that's raising my alarm: "/* the KeyUpdate in the middle of the call restarted at 0 */". That suggests something is happening within the `KeyUpdate` operation. I can tell from the surrounding code that `tls_crypto_info_init` is called, initializes the structure. Then, the important part: we call `getsockopt` with `TLS_TX`, and, vitally, after a `KeyUpdate` has restarted the sequence. We're using `TLS_TX` to read information about the sequence. It's reading the information after a reset. The `rec_seq` variable appears to be pointing to something that's supposed to hold the new sequence number, but is it being updated properly? This is my central question. So, does `getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &optlen)` actually read anything that has been updated by `KeyUpdate`? My gut tells me something might be going wrong here, that we may be potentially misreading the sequence. Time to dive into the `do_tls_getsockopt_conf` function and see what's actually happening under the hood. I need to understand what `TLS_TX` is supposed to be returning in this scenario. |
| 18/3 | 2026/11/08 13:38 | tool | read-file |
0mArgs:{
"File": "net/tls/tls_main.c",
"FirstLine": 428,
"LineCount": 55
}Results: {
"Contents": " 428:\t\n 429:\tstatic int do_tls_getsockopt_conf(struct sock *sk, sockopt_t *opt, int tx)\n 430:\t{\n 431:\t\tint rc = 0;\n 432:\t\tconst struct tls_cipher_desc *cipher_desc;\n 433:\t\tstruct tls_context *ctx = tls_get_ctx(sk);\n 434:\t\tstruct tls_crypto_info *crypto_info;\n 435:\t\tstruct cipher_context *cctx;\n 436:\t\tint len = opt-\u003eoptlen;\n 437:\t\n 438:\t\tif (!opt-\u003eiter_out.ubuf || len \u003c sizeof(*crypto_info)) {\n 439:\t\t\trc = -EINVAL;\n 440:\t\t\tgoto out;\n 441:\t\t}\n 442:\t\n 443:\t\tif (!ctx) {\n 444:\t\t\trc = -EBUSY;\n 445:\t\t\tgoto out;\n 446:\t\t}\n 447:\t\n 448:\t\t/* get user crypto info */\n 449:\t\tif (tx) {\n 450:\t\t\tcrypto_info = \u0026ctx-\u003ecrypto_send.info;\n 451:\t\t\tcctx = \u0026ctx-\u003etx;\n 452:\t\t} else {\n 453:\t\t\tcrypto_info = \u0026ctx-\u003ecrypto_recv.info;\n 454:\t\t\tcctx = \u0026ctx-\u003erx;\n 455:\t\t}\n 456:\t\n 457:\t\tif (!TLS_CRYPTO_INFO_READY(crypto_info)) {\n 458:\t\t\trc = -EBUSY;\n 459:\t\t\tgoto out;\n 460:\t\t}\n 461:\t\n 462:\t\tif (len == sizeof(*crypto_info)) {\n 463:\t\t\tif (copy_to_iter(crypto_info, sizeof(*crypto_info),\n 464:\t\t\t\t\t \u0026opt-\u003eiter_out) != sizeof(*crypto_info))\n 465:\t\t\t\trc = -EFAULT;\n 466:\t\t\tgoto out;\n 467:\t\t}\n 468:\t\n 469:\t\tcipher_desc = get_cipher_desc(crypto_info-\u003ecipher_type);\n 470:\t\tif (!cipher_desc || len != cipher_desc-\u003ecrypto_info) {\n 471:\t\t\trc = -EINVAL;\n 472:\t\t\tgoto out;\n 473:\t\t}\n 474:\t\n 475:\t\tmemcpy(crypto_info_iv(crypto_info, cipher_desc),\n 476:\t\t cctx-\u003eiv + cipher_desc-\u003esalt, cipher_desc-\u003eiv);\n 477:\t\tmemcpy(crypto_info_rec_seq(crypto_info, cipher_desc),\n 478:\t\t cctx-\u003erec_seq, cipher_desc-\u003erec_seq);\n 479:\t\n 480:\t\tif (copy_to_iter(crypto_info, cipher_desc-\u003ecrypto_info,\n 481:\t\t\t\t \u0026opt-\u003eiter_out) != cipher_desc-\u003ecrypto_info)\n 482:\t\t\trc = -EFAULT;\n"
} |
| 19/3 | 2026/11/08 13:38 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23935 output: 34 thoughts: 613 Thoughts: **Analyzing TLS Key Update and Offload** Okay, let's break this down. I'm examining `do_tls_getsockopt_conf` and it seems to be copying `crypto_info` to the user space. Now, let's shift focus to `tls_keyupdate_rekey` to understand how it updates the `crypto_send` and `crypto_recv` structures. I can see the code retrieves the TLS context, the traffic secret, and cipher description. It validates some parameters, ensuring the cipher description and secret length are valid. It looks like the code initializes a temporary crypto context `tmp` to store intermediate values. It appears to be copying an existing `crypto_info` into `tmp`, which is interesting. The core of the key update logic seems to be the calls to `tls_hkdf_expand_label`. It's expanding the current traffic secret to derive a new traffic secret, then deriving a key and IV from that. I see it uses the derived key and IV to update the `crypto_info`. Specifically the salt and IV are derived, then copied, from the IV buffer. Now, let's step back and consider where this `crypto_info` is actually used. Ah, the `tls_set_sw_offload(sk, tx, info)` function. I'm going to have to dive into that function to understand where this updated crypto information is actually set. It's time to inspect what `tls_set_sw_offload` is doing. |
| 20/3 | 2026/11/08 13:39 | tool | grepper |
0mArgs:{
"Expression": "int tls_set_sw_offload",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "net/tls/tls.h=154=int tls_sw_key_update(struct sock *sk, int request);\nnet/tls/tls.h:155:int tls_set_sw_offload(struct sock *sk, int tx,\nnet/tls/tls.h-156-\t\t struct tls_crypto_info *new_crypto_info);\n--\nnet/tls/tls_sw.c=2707=static void tls_finish_key_update(struct sock *sk, struct tls_context *tls_ctx)\n--\nnet/tls/tls_sw.c-2721-\nnet/tls/tls_sw.c:2722:int tls_set_sw_offload(struct sock *sk, int tx,\nnet/tls/tls_sw.c-2723-\t\t struct tls_crypto_info *new_crypto_info)\n"
} |
| 21/3 | 2026/11/08 13:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 25131 output: 38 thoughts: 26 Thoughts: Okay, here's my interpretation of that thought process, assuming I'm already quite familiar with networking and kernel internals: **Diving into TLS Software Offload Configuration** Alright, I'm thinking I need to refresh my memory on how the software offload mechanisms for TLS are configured, specifically within the kernel. The goal is to understand how the system decides to use, or *not* use, this offload path. I'm going to jump into the `tls_set_sw_offload` function. That function is in `net/tls/tls_sw.c`, so I'll be referencing that specific file. It probably contains the core logic for setting up and potentially disabling the software offload path for TLS processing within the kernel's networking stack. I'll need to focus on what inputs it takes, how it determines the offload state, and any flags or data structures it manipulates. My experience tells me that this function likely interacts with other parts of the TLS stack to either enable or disable the software-based acceleration of the TLS process. Time to take a closer look. |
| 22/3 | 2026/11/08 13:39 | tool | read-file |
0mArgs:{
"File": "net/tls/tls_sw.c",
"FirstLine": 2720,
"LineCount": 80
}Results: {
"Contents": "2720:\t}\n2721:\t\n2722:\tint tls_set_sw_offload(struct sock *sk, int tx,\n2723:\t\t\t struct tls_crypto_info *new_crypto_info)\n2724:\t{\n2725:\t\tstruct tls_crypto_info *crypto_info, *src_crypto_info;\n2726:\t\tstruct tls_sw_context_tx *sw_ctx_tx = NULL;\n2727:\t\tstruct tls_sw_context_rx *sw_ctx_rx = NULL;\n2728:\t\tconst struct tls_cipher_desc *cipher_desc;\n2729:\t\tchar *iv, *rec_seq, *key, *salt;\n2730:\t\tstruct cipher_context *cctx;\n2731:\t\tstruct tls_prot_info *prot;\n2732:\t\tstruct crypto_aead **aead;\n2733:\t\tstruct tls_context *ctx;\n2734:\t\tstruct crypto_tfm *tfm;\n2735:\t\tint rc = 0;\n2736:\t\n2737:\t\tctx = tls_get_ctx(sk);\n2738:\t\tprot = \u0026ctx-\u003eprot_info;\n2739:\t\n2740:\t\t/* new_crypto_info != NULL means rekey */\n2741:\t\tif (!new_crypto_info) {\n2742:\t\t\tif (tx) {\n2743:\t\t\t\tctx-\u003epriv_ctx_tx = init_ctx_tx(ctx, sk);\n2744:\t\t\t\tif (!ctx-\u003epriv_ctx_tx)\n2745:\t\t\t\t\treturn -ENOMEM;\n2746:\t\t\t} else {\n2747:\t\t\t\tctx-\u003epriv_ctx_rx = init_ctx_rx(ctx);\n2748:\t\t\t\tif (!ctx-\u003epriv_ctx_rx)\n2749:\t\t\t\t\treturn -ENOMEM;\n2750:\t\t\t}\n2751:\t\t}\n2752:\t\n2753:\t\tif (tx) {\n2754:\t\t\tsw_ctx_tx = ctx-\u003epriv_ctx_tx;\n2755:\t\t\tcrypto_info = \u0026ctx-\u003ecrypto_send.info;\n2756:\t\t\tcctx = \u0026ctx-\u003etx;\n2757:\t\t\taead = \u0026sw_ctx_tx-\u003eaead_send;\n2758:\t\t} else {\n2759:\t\t\tsw_ctx_rx = ctx-\u003epriv_ctx_rx;\n2760:\t\t\tcrypto_info = \u0026ctx-\u003ecrypto_recv.info;\n2761:\t\t\tcctx = \u0026ctx-\u003erx;\n2762:\t\t\taead = \u0026sw_ctx_rx-\u003eaead_recv;\n2763:\t\t}\n2764:\t\n2765:\t\tsrc_crypto_info = new_crypto_info ?: crypto_info;\n2766:\t\n2767:\t\tcipher_desc = get_cipher_desc(src_crypto_info-\u003ecipher_type);\n2768:\t\tif (!cipher_desc) {\n2769:\t\t\trc = -EINVAL;\n2770:\t\t\tgoto free_priv;\n2771:\t\t}\n2772:\t\n2773:\t\trc = init_prot_info(prot, src_crypto_info, cipher_desc);\n2774:\t\tif (rc)\n2775:\t\t\tgoto free_priv;\n2776:\t\n2777:\t\tiv = crypto_info_iv(src_crypto_info, cipher_desc);\n2778:\t\tkey = crypto_info_key(src_crypto_info, cipher_desc);\n2779:\t\tsalt = crypto_info_salt(src_crypto_info, cipher_desc);\n2780:\t\trec_seq = crypto_info_rec_seq(src_crypto_info, cipher_desc);\n2781:\t\n2782:\t\tif (!*aead) {\n2783:\t\t\t*aead = crypto_alloc_aead(cipher_desc-\u003ecipher_name, 0, 0);\n2784:\t\t\tif (IS_ERR(*aead)) {\n2785:\t\t\t\trc = PTR_ERR(*aead);\n2786:\t\t\t\t*aead = NULL;\n2787:\t\t\t\tgoto free_priv;\n2788:\t\t\t}\n2789:\t\t}\n2790:\t\n2791:\t\tctx-\u003epush_pending_record = tls_sw_push_pending_record;\n2792:\t\n2793:\t\t/* setkey is the last operation that could fail during a\n2794:\t\t * rekey. if it succeeds, we can start modifying the\n2795:\t\t * context.\n2796:\t\t */\n2797:\t\trc = crypto_aead_setkey(*aead, key, cipher_desc-\u003ekey);\n2798:\t\tif (rc) {\n2799:\t\t\tif (new_crypto_info)\n"
} |
| 23/3 | 2026/11/08 13:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26690 output: 38 thoughts: 17 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, and written as if they were my own: **Diving Deeper into `tls_set_sw_offload`** Alright, so the discussion has led us to `tls_set_sw_offload`. My initial instinct is to dig directly into the implementation. What precisely is this function doing? I need to understand its role within the TLS stack, especially concerning the software offload aspect. I suspect we're looking at a function responsible for configuring or enabling some level of software-based acceleration for TLS operations. Is it a flag, a configuration structure, or something more involved? I need to quickly grasp how it interacts with the hardware offload capabilities, if any. Is it used to *replace* hardware offload, *supplement* it, or simply *prepare* the environment for software operations when hardware is unavailable or unsuitable? The goal, of course, is optimal performance. I'll need to look for any logic related to detecting available hardware resources and making a decision based on that. Any conditional branches within the function are going to be key to that understanding. The specific software operations this function might be involved in are important. I'll be looking carefully at the data structures it manipulates. Are we dealing with flags, bitmasks, configuration parameters, or pointers to function tables? The naming conventions within the function will be telling - does it give insight into which operations are being offloaded to software? Understanding the potential side effects are going to be critical to making decisions further down the line. I'll start reading. |
| 24/3 | 2026/11/08 13:39 | tool | read-file |
0mArgs:{
"File": "net/tls/tls_sw.c",
"FirstLine": 2800,
"LineCount": 50
}Results: {
"Contents": "2800:\t\t\t\tgoto out;\n2801:\t\t\telse\n2802:\t\t\t\tgoto free_aead;\n2803:\t\t}\n2804:\t\n2805:\t\tif (!new_crypto_info) {\n2806:\t\t\trc = crypto_aead_setauthsize(*aead, prot-\u003etag_size);\n2807:\t\t\tif (rc)\n2808:\t\t\t\tgoto free_aead;\n2809:\t\t}\n2810:\t\n2811:\t\tif (!tx \u0026\u0026 !new_crypto_info) {\n2812:\t\t\ttfm = crypto_aead_tfm(sw_ctx_rx-\u003eaead_recv);\n2813:\t\n2814:\t\t\ttls_update_rx_zc_capable(ctx);\n2815:\t\t\tsw_ctx_rx-\u003easync_capable =\n2816:\t\t\t\tsrc_crypto_info-\u003eversion != TLS_1_3_VERSION \u0026\u0026\n2817:\t\t\t\t!!(tfm-\u003e__crt_alg-\u003ecra_flags \u0026 CRYPTO_ALG_ASYNC);\n2818:\t\n2819:\t\t\trc = tls_strp_init(\u0026sw_ctx_rx-\u003estrp, sk);\n2820:\t\t\tif (rc)\n2821:\t\t\t\tgoto free_aead;\n2822:\t\t}\n2823:\t\n2824:\t\tmemcpy(cctx-\u003eiv, salt, cipher_desc-\u003esalt);\n2825:\t\tmemcpy(cctx-\u003eiv + cipher_desc-\u003esalt, iv, cipher_desc-\u003eiv);\n2826:\t\tmemcpy(cctx-\u003erec_seq, rec_seq, cipher_desc-\u003erec_seq);\n2827:\t\n2828:\t\tif (new_crypto_info) {\n2829:\t\t\tunsafe_memcpy(crypto_info, new_crypto_info,\n2830:\t\t\t\t cipher_desc-\u003ecrypto_info,\n2831:\t\t\t\t /* size was checked in do_tls_setsockopt_conf */);\n2832:\t\t\tmemzero_explicit(new_crypto_info, cipher_desc-\u003ecrypto_info);\n2833:\t\t\tif (!tx)\n2834:\t\t\t\ttls_finish_key_update(sk, ctx);\n2835:\t\t}\n2836:\t\n2837:\t\tgoto out;\n2838:\t\n2839:\tfree_aead:\n2840:\t\tcrypto_free_aead(*aead);\n2841:\t\t*aead = NULL;\n2842:\tfree_priv:\n2843:\t\tif (!new_crypto_info) {\n2844:\t\t\tif (tx) {\n2845:\t\t\t\tkfree(ctx-\u003epriv_ctx_tx);\n2846:\t\t\t\tctx-\u003epriv_ctx_tx = NULL;\n2847:\t\t\t} else {\n2848:\t\t\t\tkfree(ctx-\u003epriv_ctx_rx);\n2849:\t\t\t\tctx-\u003epriv_ctx_rx = NULL;\n"
} |
| 25/3 | 2026/11/08 13:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27847 output: 31 thoughts: 3746 Thoughts: **Deep Dive into TLS Key Update and Memory Safety** Okay, so I've been tracing through the TLS key update and management code, and I need to make sure I understand the memory management and initialization. I started at the `tls_set_sw_offload` function, where the `rec_seq` is initialized. I noticed that the `rec_seq` is copied from the `crypto_info_rec_seq` but that the original `rec_seq` is zeroed out in `tls_keyupdate_rekey`, and this zeroes out everything in `tmp`, including `rec_seq`. This makes sure that the sequence numbers restart at zero, which is required by the RFC. That's good; it confirms that the initial zeroing and subsequent copying in `tls_set_sw_offload` are intentional and compliant. Then I moved to `tls_hkdf_expand_label`, ensuring it's not reading uninitialized memory. I meticulously traced the `info` buffer's lifecycle within that function. I confirmed that `info` is populated sequentially and completely, so there's no risk of reading uninitialized data. Specifically, I'm checking that the lengths and offsets used for the `memcpy` operations within the `tls_hkdf_expand_label` function are correct and that everything is written before it's read. Everything looks good. I then went through `tls_tx_key_update` and confirmed the `body`, `ctrl`, and `msg` structures are initialized before use, either directly or through zero-initialization (where applicable). The use of the zero-initialized `ctrl` and `msg` structures, and the explicitly initialized `body` ensures that the data being sent is well-defined. I double-checked the `tls_keyupdate_rx` to ensure that incoming data is fully copied into a buffer before any operations on it and that there is proper error checking and handling for any invalid incoming message. This ensures no uninitialized data is read. Finally, I got to the `do_tls_setsockopt_secret` function and `tls_check_traffic_secret`. This is where the secrets are installed. I have verified the memory copy logic from user space to the kernel space, the initialization of the `data` buffer, and the expansion via `tls_hkdf_expand_label`. I made absolutely sure that the secret is only written if it is valid. Then, I checked to see that the `secret` buffer is checked for having a key length, that `data` is copied from `sockptr`, and that `tls_check_traffic_secret` is called to validate the secret. Finally, I'm sure that any sensitive data (e.g. `key`, `iv`) used internally is zeroed before the function returns. That is good. The traffic secret in `do_tls_setsockopt_secret` checks are comprehensive and avoid any uninitialized memory reads or writes. This is important to note since `struct tls_traffic_secret` is allocated within `struct tls_context`, which is allocated using `tls_ctx_create`. |
| 26/3 | 2026/11/08 13:39 | tool | grepper |
0mArgs:{
"Expression": "kzalloc.*tls_context",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "no matches"
} |
| 27/3 | 2026/11/08 13:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32297 output: 30 thoughts: 15 Thoughts: Okay, here's my summary, tailored for an expert audience, as if I'm thinking these things myself: **Tracing the Context Creation** Alright, I need to understand the lifecycle of this TLS context. I'm going to start by zeroing in on the `tls_ctx_create` function. I need to know precisely where it's defined. Locating the definition is the critical first step in tracing how this object is built and initialized, and consequently, how it behaves within the system. The documentation should have already provided insight, but a concrete definition would provide more information. I need to understand not only the function signature, but also the internals of where the memory is allocated. |
| 28/3 | 2026/11/08 13:39 | tool | grepper |
0mArgs:{
"Expression": "tls_ctx_create",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "net/tls/tls.h=137=void __net_exit tls_proc_fini(struct net *net);\nnet/tls/tls.h-138-\nnet/tls/tls.h:139:struct tls_context *tls_ctx_create(struct sock *sk);\nnet/tls/tls.h-140-void tls_ctx_free(struct sock *sk, struct tls_context *ctx);\n--\nnet/tls/tls_main.c=1102=static int tls_disconnect(struct sock *sk, int flags)\n--\nnet/tls/tls_main.c-1106-\nnet/tls/tls_main.c:1107:struct tls_context *tls_ctx_create(struct sock *sk)\nnet/tls/tls_main.c-1108-{\n--\nnet/tls/tls_main.c=1233=static int tls_init(struct sock *sk)\n--\nnet/tls/tls_main.c-1250-\twrite_lock_bh(\u0026sk-\u003esk_callback_lock);\nnet/tls/tls_main.c:1251:\tctx = tls_ctx_create(sk);\nnet/tls/tls_main.c-1252-\tif (!ctx) {\n"
} |
| 29/3 | 2026/11/08 13:39 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32811 output: 38 thoughts: 23 Thoughts: Here's my summary, as though I'm reading and thinking about the code: **Diving into the TLS Context Creation** Okay, let's get down to brass tacks and dissect `tls_ctx_create` in `net/tls/tls_main.c`. I need to understand exactly how the TLS context is initialized. I expect this to be the core of the TLS connection setup, and therefore crucial for my understanding of the system's security posture. I'll be looking for memory allocation strategies, initialization of key structures, and any potential security vulnerabilities in this initial setup. I want to see how the cipher suites are handled and how the underlying crypto library is integrated. Any pre-shared key setup? SNI handling? Certificate loading? Let's see what they've done and if it aligns with best practices. I'm especially keen to see how they handle error conditions, because that's where a lot of subtle vulnerabilities often hide. I'll be paying attention to any calls to the underlying crypto library and ensuring they are used correctly and securely. |
| 30/3 | 2026/11/08 13:40 | tool | read-file |
0mArgs:{
"File": "net/tls/tls_main.c",
"FirstLine": 1107,
"LineCount": 25
}Results: {
"Contents": "1107:\tstruct tls_context *tls_ctx_create(struct sock *sk)\n1108:\t{\n1109:\t\tstruct inet_connection_sock *icsk = inet_csk(sk);\n1110:\t\tstruct tls_context *ctx;\n1111:\t\n1112:\t\tctx = kzalloc_obj(*ctx, GFP_ATOMIC);\n1113:\t\tif (!ctx)\n1114:\t\t\treturn NULL;\n1115:\t\n1116:\t\tmutex_init(\u0026ctx-\u003etx_lock);\n1117:\t\tctx-\u003esk_proto = READ_ONCE(sk-\u003esk_prot);\n1118:\t\tctx-\u003esk = sk;\n1119:\t\t/* Release semantic of rcu_assign_pointer() ensures that\n1120:\t\t * ctx-\u003esk_proto is visible before changing sk-\u003esk_prot in\n1121:\t\t * update_sk_prot(), and prevents reading uninitialized value in\n1122:\t\t * tls_{getsockopt, setsockopt}. Note that we do not need a\n1123:\t\t * read barrier in tls_{getsockopt,setsockopt} as there is an\n1124:\t\t * address dependency between sk-\u003esk_proto-\u003e{getsockopt,setsockopt}\n1125:\t\t * and ctx-\u003esk_proto.\n1126:\t\t */\n1127:\t\trcu_assign_pointer(icsk-\u003eicsk_ulp_data, ctx);\n1128:\t\treturn ctx;\n1129:\t}\n1130:\t\n1131:\tstatic void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],\n"
} |
| 31/3 | 2026/11/08 13:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33536 output: 30 thoughts: 51 Thoughts: Here's my thought process, as requested: **Initialization Concerns: Memory and Structure** Okay, so I'm looking at this initialization routine, specifically this `kzalloc_obj(*ctx, GFP_ATOMIC)` call. Hmm, `kzalloc` is pretty handy for zeroing out the allocated memory, that's good practice. It'll ensure a clean slate, especially in a concurrent environment where stale data could be a disaster. But, there's a struct involved here... it looks like it's taking the `ctx` struct as a pointer. Let's make sure that's zeroed correctly, and we're not running into any alignment issues, that could be a headache down the line. I know the standard `kzalloc` function guarantees zeroing, but there might be some subtlety in our custom allocation. And the next thing that pops into my head is `struct tls_sw_context_rx`. I need to ensure that this struct is correctly initialized as well. I'm going to quickly check how `init_ctx_rx` is being used, so that I can ensure the proper use of the zeroed memory, specifically, is the initialization routine setting the values I expect? I want to make sure the structure is correctly zeroed out before using it, and that any subsequent initialization is overwriting the proper fields, not garbage. |
| 32/3 | 2026/11/08 13:40 | tool | grepper |
0mArgs:{
"Expression": "init_ctx_rx",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "net/tls/tls_sw.c=2634=static struct tls_sw_context_tx *init_ctx_tx(struct tls_context *ctx, struct sock *sk)\n--\nnet/tls/tls_sw.c-2654-\nnet/tls/tls_sw.c:2655:static struct tls_sw_context_rx *init_ctx_rx(struct tls_context *ctx)\nnet/tls/tls_sw.c-2656-{\n--\nnet/tls/tls_sw.c=2722=int tls_set_sw_offload(struct sock *sk, int tx,\n--\nnet/tls/tls_sw.c-2746-\t\t} else {\nnet/tls/tls_sw.c:2747:\t\t\tctx-\u003epriv_ctx_rx = init_ctx_rx(ctx);\nnet/tls/tls_sw.c-2748-\t\t\tif (!ctx-\u003epriv_ctx_rx)\n"
} |
| 33/3 | 2026/11/08 13:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34150 output: 38 thoughts: 19 Thoughts: Here's a summary of my thought process: **Diving into the `tls_sw.c` Context Initialization** Okay, so I need to understand how the context is initialized for this TLS implementation. My immediate thought is to zero in on `init_ctx_rx` within `tls_sw.c`. This function is likely the starting point for setting up the receive-side context, which is crucial for handling incoming TLS connections and data. I'll need to carefully examine what data structures it manipulates, what parameters it takes, and how it interacts with the underlying hardware or cryptographic engines. This function is probably where crucial security parameters are set. I need to understand this to have any chance of making progress. I suspect this is going to be my entry point into understanding the architecture. |
| 34/3 | 2026/11/08 13:40 | tool | read-file |
0mArgs:{
"File": "net/tls/tls_sw.c",
"FirstLine": 2655,
"LineCount": 20
}Results: {
"Contents": "2655:\tstatic struct tls_sw_context_rx *init_ctx_rx(struct tls_context *ctx)\n2656:\t{\n2657:\t\tstruct tls_sw_context_rx *sw_ctx_rx;\n2658:\t\n2659:\t\tif (!ctx-\u003epriv_ctx_rx) {\n2660:\t\t\tsw_ctx_rx = kzalloc_obj(*sw_ctx_rx);\n2661:\t\t\tif (!sw_ctx_rx)\n2662:\t\t\t\treturn NULL;\n2663:\t\t} else {\n2664:\t\t\tsw_ctx_rx = ctx-\u003epriv_ctx_rx;\n2665:\t\t}\n2666:\t\n2667:\t\tcrypto_init_wait(\u0026sw_ctx_rx-\u003easync_wait);\n2668:\t\tatomic_set(\u0026sw_ctx_rx-\u003edecrypt_pending, 1);\n2669:\t\tinit_waitqueue_head(\u0026sw_ctx_rx-\u003ewq);\n2670:\t\tskb_queue_head_init(\u0026sw_ctx_rx-\u003erx_list);\n2671:\t\tskb_queue_head_init(\u0026sw_ctx_rx-\u003easync_hold);\n2672:\t\n2673:\t\treturn sw_ctx_rx;\n2674:\t}\n"
} |
| 35/3 | 2026/11/08 13:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34715 output: 33 thoughts: 658 Thoughts: **Analyzing Key Update Handling in TLS** Okay, so `sw_ctx_rx` gets allocated using `kzalloc_obj`, which means both `key_update_count` and `key_update_window` start at zero. That's the baseline. Now, I need to understand how key updates are handled, particularly in relation to `do_tls_setsockopt_conf`. When keys are updated within that function, if `update` is true, a few things happen: the TX secret is zeroed out (if TX is involved), and both the RX and TX key update flags are set to false. Also the RX secret is zeroed out. This makes sense; old secrets are discarded after a key update. Then, at context free time, the `crypto_send`, `crypto_recv`, `tx_secret`, and `rx_secret` are all zeroed out using `memzero_explicit`. A very clear cleanup process. Now I need to understand what happens during a rekeying event: specifically within `tls_check_pending_rekey`. I noticed the logic changed significantly. Initially, when a `TLS_HANDSHAKE_KEYUPDATE` is received, `key_update_pending` was simply set to `true`. But now, there's a crucial check involving the length of `ctx->rx_secret`. If `ctx->rx_secret.len` is non-zero, it calls `tls_keyupdate_rx()`. If `tls_keyupdate_rx` is successful, it consumes the SKB and returns 1. If it fails, it calls `tls_err_abort`. If it returns 1, the caller also knows that it's consumed the SKB. But if `ctx->rx_secret.len` is zero, then the older behavior is used: `WRITE_ONCE(rx_ctx->key_update_pending, true)`. So, it's a two-stage process; first, we check if there's an rx_secret and if so, handle the rekey immediately, consuming the skb. Otherwise, we set the pending flag. My next step is to examine the callers of `tls_check_pending_rekey` to determine how the return value of 1 is interpreted and handled in the overall flow. This will tell me what happens after this function call returns and confirm where the skb is consumed. |
| 36/3 | 2026/11/08 13:40 | tool | grepper |
0mArgs:{
"Expression": "tls_check_pending_rekey",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "net/tls/tls_sw.c=1645=tls_decrypt_device(struct sock *sk, struct msghdr *msg,\n--\nnet/tls/tls_sw.c-1693-\nnet/tls/tls_sw.c:1694:static int tls_check_pending_rekey(struct sock *sk, struct tls_context *ctx,\nnet/tls/tls_sw.c-1695-\t\t\t\t struct sk_buff *skb)\n--\nnet/tls/tls_sw.c=1740=static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,\n--\nnet/tls/tls_sw.c-1760-\nnet/tls/tls_sw.c:1761:\treturn tls_check_pending_rekey(sk, tls_ctx, darg-\u003eskb);\nnet/tls/tls_sw.c-1762-}\n"
} |
| 37/3 | 2026/11/08 13:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 36158 output: 39 thoughts: 1125 Thoughts: **Analyzing TLS Key Update and Potential Uninitialized Data** Okay, let's break this down. My focus here is on understanding how key updates are handled in this TLS implementation and whether there are any potential vulnerabilities related to uninitialized data. First, I'm tracing the data flow through the receive path. I see that `tls_rx_one_record` is central to handling incoming records. The code explicitly notes that if `err > 0` from `tls_rx_one_record`, it indicates a "consumed" record, likely a KeyUpdate. These records are *not* passed to user space and are handled internally. This seems correct. The calls in `tls_sw_recvmsg`, `tls_sw_splice_read`, and `tls_sw_read_sock` all consistently handle positive `err` values this way, consuming the skb. This means that a KeyUpdate record will not be presented to the user. Now, on the transmit side, `tls_sw_sendmsg_locked` catches my eye. Specifically, the check within that function, where it verifies that the `record_type` is `TLS_RECORD_TYPE_DATA` and there is no open record or the open record's size is zero before calling `tls_tx_maybe_update_key`. This makes sense; Key updates are potentially interleaved with data, so you would need to process a key update before writing the next data record. `tls_tx_maybe_update_key` seems to manage sequence numbers and a key update reply flag - it's a critical point for key management. My next step is to examine the specific functions used within the context of TLS. I identified two potential areas of concern: 1. `tls_hkdf_expand_label`: I see that the `info` buffer, used in the key derivation function, is allocated on the stack. The code appears to track initialization status (`pos`) to ensure the correct amount of data is passed to the HMAC function. `tmp` buffer is used as an output buffer, and the HMAC functions (such as `hmac_sha256_usingrawkey` and `hmac_sha384_usingrawkey`) should fully initialize its buffer. My conclusion is that I need to double-check that `hmac_sha256_usingrawkey` and `hmac_sha384_usingrawkey` do indeed fully initialize the `tmp` buffer. I must also trace the use of `info` to be 100% sure that all data in it is initialized prior to use. This is where I'll concentrate my further review. |
| 38/3 | 2026/11/08 13:40 | tool | grepper |
0mArgs:{
"Expression": "hmac_sha256_usingrawkey",
"PathPrefixes": [
"include/",
"crypto/"
]
}Results: {
"Output": "include/crypto/sha2.h=485=static inline void hmac_sha256_init(struct hmac_sha256_ctx *ctx,\n--\ninclude/crypto/sha2.h-497- *\ninclude/crypto/sha2.h:498: * If you don't need incremental computation, consider hmac_sha256_usingrawkey()\ninclude/crypto/sha2.h-499- * instead.\n--\ninclude/crypto/sha2.h=531=void hmac_sha256_final(struct hmac_sha256_ctx *ctx,\n--\ninclude/crypto/sha2.h-540- *\ninclude/crypto/sha2.h:541: * If you're using the key only once, consider using hmac_sha256_usingrawkey().\ninclude/crypto/sha2.h-542- *\n--\ninclude/crypto/sha2.h=545=void hmac_sha256(const struct hmac_sha256_key *key,\n--\ninclude/crypto/sha2.h-549-/**\ninclude/crypto/sha2.h:550: * hmac_sha256_usingrawkey() - Compute HMAC-SHA256 in one shot, using a raw key\ninclude/crypto/sha2.h-551- * @raw_key: the raw HMAC-SHA256 key\n--\ninclude/crypto/sha2.h-561- */\ninclude/crypto/sha2.h:562:void hmac_sha256_usingrawkey(const u8 *raw_key, size_t raw_key_len,\ninclude/crypto/sha2.h-563-\t\t\t const u8 *data, size_t data_len,\n"
} |
| 39/3 | 2026/11/08 13:40 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38337 output: 30 thoughts: 2012 Thoughts: **Analysis of Potential Uninitialized Data Leaks in TLS Key Updates** Alright, let's break down this code and ensure we're not leaking any sensitive information due to uninitialized memory. First, I've confirmed that the `hmac_sha256_usingrawkey` and `hmac_sha384_usingrawkey` functions are safe: the `out_len` parameter ensures only the HMAC-produced bytes are copied, and the `tmp` buffer is explicitly zeroed afterward. Good, that's what I expected. Next, I examined `tls_keyupdate_rekey`. We're using `tls_hkdf_expand_label` to derive keys and IVs. The crucial part is that the `iv`, `next`, and `tmp` buffers are properly initialized (zeroed) before the HKDF expansion and subsequent `memcpy` operations. This confirms that all data written to `info`, `next` and `iv` are fully initialized, and the `memcpy` operations are safe. Furthermore, `tls_set_sw_offload` sets up the derived key, and zeroed `tmp`. Finally, `memcpy(secret->data, next, secret->len)` writes over the old secret. In `tls_tx_key_update`, the 5-byte `body` is fully initialized, the control message structure `ctrl` is zero-initialized, and the `msghdr` struct `msg` is also zero-initialized. The function sends the message via `tls_sw_sendmsg_locked`, and there are no uninitialized bytes in the sent record. `do_tls_setsockopt_secret` and `tls_check_traffic_secret` utilize HKDF to expand and compare secrets. The code copies only the required amount of data into the `data` buffer, and the expansion of the key and IV using `tls_hkdf_expand_label` appears sound, with all compared bytes initialized. `do_tls_setsockopt_key_update` is simple and checks the value of the `int` option. Now, the important part: confirming there are no kernel-to-user copies that could leak data. After a thorough review, I've verified that the diff doesn't contain a single `copy_to_user`, `put_user`, `copy_to_iter`, or any netlink/ioctl-like operations that copy data from kernel to user space. This confirms the documentation statement about the secrets being non-readable. Also, the `getsockopt` calls are *not* implemented for traffic secrets, which is good. But, let's examine the existing `getsockopt` implementation in `do_tls_getsockopt_conf`. After a `KeyUpdate` and `tls_set_sw_offload`, the key and iv have been set, and the buffers are zeroed before being updated. Therefore the entire `crypto_info` is fully initialized, so the existing `getsockopt` implementation is safe, and will not leak data. Finally, regarding `tls_sw_splice_read`, even if a key update record is consumed and `darg.skb` is non-null, the function only returns to read a data record once it's available. The `darg` is initialized in `tls_rx_rec_wait` or `tls_rx_one_record`, so this should be fine. It looks like the change adds an extra `while` loop that consumes the key update record. In summary, I'm confident that these changes do not introduce any new uninitialized memory leaks. All sensitive data is properly initialized before use and there is no route from kernel to user-space in the diff that could expose secrets. This implementation aligns with the security expectations for this functionality. |
| 40/3 | 2026/11/08 13:41 | tool | grepper |
0mArgs:{
"Expression": "tls_decrypt_arg",
"PathPrefixes": [
"net/tls/"
]
}Results: {
"Output": "net/tls/tls_sw.c-51-\nnet/tls/tls_sw.c:52:struct tls_decrypt_arg {\nnet/tls/tls_sw.c-53-\tstruct_group(inargs,\n--\nnet/tls/tls_sw.c=158=static int tls_padding_length(struct tls_prot_info *prot, struct sk_buff *skb,\nnet/tls/tls_sw.c:159:\t\t\t struct tls_decrypt_arg *darg)\nnet/tls/tls_sw.c-160-{\n--\nnet/tls/tls_sw.c=254=static int tls_do_decryption(struct sock *sk,\n--\nnet/tls/tls_sw.c-259-\t\t\t struct aead_request *aead_req,\nnet/tls/tls_sw.c:260:\t\t\t struct tls_decrypt_arg *darg)\nnet/tls/tls_sw.c-261-{\n--\nnet/tls/tls_sw.c=1428=static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,\nnet/tls/tls_sw.c-1429-\t\t\t struct scatterlist *out_sg,\nnet/tls/tls_sw.c:1430:\t\t\t struct tls_decrypt_arg *darg)\nnet/tls/tls_sw.c-1431-{\n--\nnet/tls/tls_sw.c=1603=tls_decrypt_sw(struct sock *sk, struct tls_context *tls_ctx,\nnet/tls/tls_sw.c:1604:\t struct msghdr *msg, struct tls_decrypt_arg *darg)\nnet/tls/tls_sw.c-1605-{\n--\nnet/tls/tls_sw.c=1645=tls_decrypt_device(struct sock *sk, struct msghdr *msg,\nnet/tls/tls_sw.c:1646:\t\t struct tls_context *tls_ctx, struct tls_decrypt_arg *darg)\nnet/tls/tls_sw.c-1647-{\n--\nnet/tls/tls_sw.c=1740=static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,\nnet/tls/tls_sw.c:1741:\t\t\t struct tls_decrypt_arg *darg)\nnet/tls/tls_sw.c-1742-{\n--\nnet/tls/tls_sw.c=1764=int decrypt_skb(struct sock *sk, struct scatterlist *sgout)\nnet/tls/tls_sw.c-1765-{\nnet/tls/tls_sw.c:1766:\tstruct tls_decrypt_arg darg = { .zc = true, };\nnet/tls/tls_sw.c-1767-\n--\nnet/tls/tls_sw.c=1982=int tls_sw_recvmsg(struct sock *sk,\n--\nnet/tls/tls_sw.c-2033-\twhile (len \u0026\u0026 (decrypted + copied \u003c target || tls_strp_msg_ready(ctx))) {\nnet/tls/tls_sw.c:2034:\t\tstruct tls_decrypt_arg darg;\nnet/tls/tls_sw.c-2035-\t\tint to_decrypt, chunk;\n--\nnet/tls/tls_sw.c=2180=ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,\n--\nnet/tls/tls_sw.c-2208-\twhile (!skb) {\nnet/tls/tls_sw.c:2209:\t\tstruct tls_decrypt_arg darg;\nnet/tls/tls_sw.c-2210-\n--\nnet/tls/tls_sw.c=2259=int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,\n--\nnet/tls/tls_sw.c-2295-\t\t} else {\nnet/tls/tls_sw.c:2296:\t\t\tstruct tls_decrypt_arg darg;\nnet/tls/tls_sw.c-2297-\n"
} |
| 41/3 | 2026/11/08 13:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42254 output: 38 thoughts: 21 Thoughts: Here's a summary of my thought process, as requested: **Diving into `darg` in `tls_rx_one_record`** Okay, so I'm looking at `tls_rx_one_record` and the first thing that's hitting me is the need to understand how the `darg` variable is initialized within this function. It's a key piece of the puzzle here, and without understanding its starting point, I'm going to struggle to fully grasp the function's overall behavior. I need to trace back and determine how `darg` is set up. This will probably involve checking the function arguments or variables declared at the higher scopes or finding where this is called from, and see how this value is passed along. My goal is to know how `darg`'s initial value impacts everything downstream, and what assumptions are being made about its state when it enters this specific function. Let's get to it. |
| 42/3 | 2026/11/08 13:41 | tool | read-file |
0mArgs:{
"File": "net/tls/tls_sw.c",
"FirstLine": 1740,
"LineCount": 25
}Results: {
"Contents": "1740:\tstatic int tls_rx_one_record(struct sock *sk, struct msghdr *msg,\n1741:\t\t\t\t struct tls_decrypt_arg *darg)\n1742:\t{\n1743:\t\tstruct tls_context *tls_ctx = tls_get_ctx(sk);\n1744:\t\tstruct tls_prot_info *prot = \u0026tls_ctx-\u003eprot_info;\n1745:\t\tstruct strp_msg *rxm;\n1746:\t\tint err;\n1747:\t\n1748:\t\terr = tls_decrypt_device(sk, msg, tls_ctx, darg);\n1749:\t\tif (!err)\n1750:\t\t\terr = tls_decrypt_sw(sk, tls_ctx, msg, darg);\n1751:\t\tif (err \u003c 0) {\n1752:\t\t\ttls_err_abort(sk, -EBADMSG);\n1753:\t\t\treturn err;\n1754:\t\t}\n1755:\t\n1756:\t\trxm = strp_msg(darg-\u003eskb);\n1757:\t\trxm-\u003eoffset += prot-\u003eprepend_size;\n1758:\t\trxm-\u003efull_len -= prot-\u003eoverhead_size;\n1759:\t\ttls_advance_record_sn(sk, prot, \u0026tls_ctx-\u003erx);\n1760:\t\n1761:\t\treturn tls_check_pending_rekey(sk, tls_ctx, darg-\u003eskb);\n1762:\t}\n1763:\t\n1764:\tint decrypt_skb(struct sock *sk, struct scatterlist *sgout)\n"
} |
| 43/3 | 2026/11/08 13:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42946 output: 38 thoughts: 18 Thoughts: Okay, here's my attempt at summarizing that thought process, keeping in mind I'm speaking to a fellow expert: **Investigating `tls_decrypt_sw` and `darg`** Alright, let's dive into this. I need to understand the interaction between `tls_decrypt_sw` and the variable or data structure, let's call it `darg`. I'm guessing that `darg` is some sort of input, perhaps parameters, data, or configuration information that gets passed *into* `tls_decrypt_sw`. I'm specifically curious about *how* `tls_decrypt_sw` utilizes it. Is `darg` providing encryption keys, the ciphertext itself, or maybe some metadata about the TLS connection? My next step is to examine the source code of `tls_decrypt_sw` to figure out the exact nature of this relationship. I'm looking for the places where `darg` is accessed, read, and modified, if at all. Understanding this interaction is key to grasping the overall decryption flow. |
| 44/3 | 2026/11/08 13:41 | tool | read-file |
0mArgs:{
"File": "net/tls/tls_sw.c",
"FirstLine": 1600,
"LineCount": 40
}Results: {
"Contents": "1600:\t}\n1601:\t\n1602:\tstatic int\n1603:\ttls_decrypt_sw(struct sock *sk, struct tls_context *tls_ctx,\n1604:\t\t struct msghdr *msg, struct tls_decrypt_arg *darg)\n1605:\t{\n1606:\t\tstruct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);\n1607:\t\tstruct tls_prot_info *prot = \u0026tls_ctx-\u003eprot_info;\n1608:\t\tstruct strp_msg *rxm;\n1609:\t\tint pad, err;\n1610:\t\n1611:\t\terr = tls_decrypt_sg(sk, \u0026msg-\u003emsg_iter, NULL, darg);\n1612:\t\tif (err \u003c 0) {\n1613:\t\t\tif (err == -EBADMSG)\n1614:\t\t\t\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);\n1615:\t\t\treturn err;\n1616:\t\t}\n1617:\t\t/* keep going even for -\u003easync, the code below is TLS 1.3 */\n1618:\t\n1619:\t\t/* If opportunistic TLS 1.3 ZC failed retry without ZC */\n1620:\t\tif (unlikely(darg-\u003ezc \u0026\u0026 prot-\u003eversion == TLS_1_3_VERSION \u0026\u0026\n1621:\t\t\t darg-\u003etail != TLS_RECORD_TYPE_DATA)) {\n1622:\t\t\tiov_iter_revert(\u0026msg-\u003emsg_iter, strp_msg(darg-\u003eskb)-\u003efull_len -\n1623:\t\t\t\t\tprot-\u003eoverhead_size);\n1624:\t\t\tdarg-\u003ezc = false;\n1625:\t\t\tif (!darg-\u003etail)\n1626:\t\t\t\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXNOPADVIOL);\n1627:\t\t\tTLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTRETRY);\n1628:\t\t\treturn tls_decrypt_sw(sk, tls_ctx, msg, darg);\n1629:\t\t}\n1630:\t\n1631:\t\tpad = tls_padding_length(prot, darg-\u003eskb, darg);\n1632:\t\tif (pad \u003c 0) {\n1633:\t\t\tif (darg-\u003eskb != tls_strp_msg(ctx))\n1634:\t\t\t\tconsume_skb(darg-\u003eskb);\n1635:\t\t\treturn pad;\n1636:\t\t}\n1637:\t\n1638:\t\trxm = strp_msg(darg-\u003eskb);\n1639:\t\trxm-\u003efull_len -= pad;\n"
} |
| 45/3 | 2026/11/08 13:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43946 output: 436 thoughts: 984 Thoughts: **KMSAN Assessment: TLS 1.3 KeyUpdate Implementation** Okay, let's break this down. My main concern is ensuring this new TLS 1.3 KeyUpdate implementation is robust and doesn't introduce any memory safety issues that KMSAN might catch, even if KASAN doesn't. My focus is on potential uninitialized memory reads or leaks. First, I see `darg->skb` is set in `tls_decrypt_sg`. That's the starting point. Now, to determine if `NeedsKMSAN=true`, I'm walking through the criteria. 1. **Kernel-to-User Space Exposure:** This patch only deals with `setsockopt`, no `getsockopt`. The existing `getsockopt` code was already checked, and memory set by rekey is fully zeroed before writing fields. So, no risk here. 2. **Conditional Logic:** All newly added fields in `struct tls_context` (`tx_secret`, `rx_secret`, `tx_key_updates`, etc.) and `struct tls_sw_context_rx` (`key_update_count`, `key_update_window`) are zeroed using `kzalloc_obj`, and that includes the key material. The stack variables are either initialized or explicitly zeroed before use, like `info` in hkdf, `tmp`, `iv`, `msg`, `body`, `ctrl`, `value`. No worries there. 3. **Complex Data Structures:** Again, `kzalloc_obj` is used extensively for the context structures. Crypto context structures are cleared with `memset(&tmp, 0, sizeof(tmp))`, everything is zeroed. No concerns here. 4. **Bounds Checks:** Lengths are strictly checked in `tls_hkdf_expand_label` and the `msg` in `tls_keyupdate_rx` is verified. Record consumption properly discards the `skb` with `consume_skb(darg.skb)`. Solid. So, from these criteria, it seems `NeedsKMSAN` is likely `false`. Let's do a KASAN vs. KMSAN sanity check. This patch is about crypto logic, rekeying, socket options, and packet consumption for KeyUpdate. KASAN is better suited to memory corruption, buffer overflows (e.g., in HKDF), use-after-free, and concurrency issues. KMSAN is for detecting access to uninitialized memory. There are no kernel-to-user copies of uninitialized fields, no leaks of uninitialized padding, and no branching on uninitialized data. Let's look at one final potential area - subtle edge cases: * The `tls_finish_key_update` function deals with `key_update_pending`, a boolean field in `struct tls_sw_context_rx`. It was zeroed during allocation. This is a non-issue. * `tls_keyupdate_rx` copies `msg` from `skb`. If the copy fails, it returns `-EBADMSG`. If it succeeds, the entire `msg` array is filled. The subsequent reads of `msg[1]`, `msg[2]`, `msg[3]`, `msg[4]` are safe because they are now initialized. Everything is clean, initialized, and well-handled. **Therefore, I'm confident in setting:** * **`NeedsKMSAN: false`** * **`KMSANReasoning: Detailed explanation contrasting KASAN vs KMSAN applicability for this patch series.`** |
| 46/3 | 2026/11/08 13:41 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series introduces in-kernel TLS 1.3 KeyUpdate support, including HKDF key derivation (tls_hkdf.c), rekeying routines (tls_keyupdate.c), and new setsockopts (TLS_TX_TRAFFIC_SECRET, TLS_RX_TRAFFIC_SECRET, TLS_TX_KEY_UPDATE).\n\nEvaluating uninitialized memory risks (KMSAN vs KASAN):\n1. No kernel-to-user information leaks: The new options are strictly write-only setsockopts (secrets cannot be read back and getsockopt does not implement them). Existing getsockopt(TLS_TX/TLS_RX) copies crypto_info, which is populated during rekeying from a stack structure (tmp) that is explicitly zeroed via memset(\u0026tmp, 0, sizeof(tmp)) before keys and IVs are copied in.\n2. Structure allocations: All newly added fields in struct tls_context and struct tls_sw_context_rx reside in heap objects allocated with kzalloc_obj, ensuring all new state flags and counters start zeroed.\n3. Stack buffers: Local buffers in HKDF expansion, setsockopt handlers, and message construction (such as info in tls_hkdf_expand_label, body/ctrl in tls_tx_key_update, and data in do_tls_setsockopt_secret) are either explicit zero-initialized, copied from verified sockptr/skb lengths, or sequentially filled before use.\n4. Surrounding buffer accesses: Decrypted KeyUpdate records are consumed and freed (consume_skb) rather than forwarded to userspace.\n\nAny potential bugs in this patch (e.g., buffer indexing/overflows during crypto operations, skb reference counting or use-after-free, or locking races) are under the purview of KASAN and LOCKDEP. KMSAN is not needed as no uninitialized memory read or info-leak risks are introduced.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series introduces in-kernel TLS 1.3 KeyUpdate support, including HKDF key derivation (tls_hkdf.c), rekeying routines (tls_keyupdate.c), and new setsockopts (TLS_TX_TRAFFIC_SECRET, TLS_RX_TRAFFIC_SECRET, TLS_TX_KEY_UPDATE).\n\nEvaluating uninitialized memory risks (KMSAN vs KASAN):\n1. No kernel-to-user information leaks: The new options are strictly write-only setsockopts (secrets cannot be read back and getsockopt does not implement them). Existing getsockopt(TLS_TX/TLS_RX) copies crypto_info, which is populated during rekeying from a stack structure (tmp) that is explicitly zeroed via memset(\u0026tmp, 0, sizeof(tmp)) before keys and IVs are copied in.\n2. Structure allocations: All newly added fields in struct tls_context and struct tls_sw_context_rx reside in heap objects allocated with kzalloc_obj, ensuring all new state flags and counters start zeroed.\n3. Stack buffers: Local buffers in HKDF expansion, setsockopt handlers, and message construction (such as info in tls_hkdf_expand_label, body/ctrl in tls_tx_key_update, and data in do_tls_setsockopt_secret) are either explicit zero-initialized, copied from verified sockptr/skb lengths, or sequentially filled before use.\n4. Surrounding buffer accesses: Decrypted KeyUpdate records are consumed and freed (consume_skb) rather than forwarded to userspace.\n\nAny potential bugs in this patch (e.g., buffer indexing/overflows during crypto operations, skb reference counting or use-after-free, or locking races) are under the purview of KASAN and LOCKDEP. KMSAN is not needed as no uninitialized memory read or info-leak risks are introduced.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|