Skip to contentSkip to conversation

nvme: In-kernel TLS support for TCP

PATCH Superseded Hannes Reinecke 7 participants 47 messages Apr 19, 2023 – May 17, 2023

Subsystems (8)

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v4 cover nvme: In-kernel TLS support for TCP

Hi all,

finally I've managed to put all things together and enable in-kernel TLS support for NVMe-over-TCP.

The patchset is based on the TLS upcall mechanism from Chuck Lever (cf '[PATCH v7 0/2] Another crack at a handshake upcall mechanism' posted to the linux netdev list), and requires the 'tlshd' userspace daemon (https://github.com/oracle/ktls-utils) for the actual TLS handshake. Changes for nvme-cli are already included in the upstream repository.

Theory of operation:
A dedicated '.nvme' keyring is created to hold the pre-shared keys (PSKs) for the TLS handshake. Keys will have to be provisioned before TLS handshake is attempted; that can be done with the 'nvme gen-tls-key' command for nvme-cli (patches are already merged upstream).
After connection to the remote TCP port the client side will use the 'best' PSK (as inferred from the NVMe TCP spec) or the PSK specified by the '--tls_key' option to nvme-cli and call the TLS userspace daemon to initiate a TLS handshake.
The server side will then invoke the TLS userspace daemon to run the TLS handshake.
If the TLS handshake succeeds the userspace daemon will be activating kTLS on the socket, and control is passed back to the kernel.

To make this work I had to implement the 'read_sock()' functionality for TLS; it seems to be holding up well enough (for me), but it really could do with reviews from persons with more network stack knowledge.

Patchset can be found at:
git.kernel.org/pub/scm/linux/kernel/git/hare/nvme.git branch tls.v4

As usual, comments and reviews are welcome.

Changes to v3:
- Really handle MSG_EOR for TLS
- Fixup MSG_SENDPAGE_NOTLAST handling
- Conditionally disable fabric option

Changes to v2:
- Included reviews from Sagi
- Removed MSG_SENDPAGE_NOTLAST
- Improved MSG_EOR handling for TLS
- Add config options NVME_TCP_TLS
and NVME_TARGET_TCP_TLS

Changes to the original RFC:
- Add a CONFIG_NVME_TLS config option
- Use a single PSK for the TLS handshake
- Make TLS connections mandatory
- Do not peek messages for the server
- Simplify data_ready callback
- Implement read_sock() for TLS

Hannes Reinecke (17):
nvme-keyring: register '.nvme' keyring
nvme-keyring: define a 'psk' keytype
nvme: add TCP TSAS definitions
nvme-tcp: add definitions for TLS cipher suites
nvme-keyring: implement nvme_tls_psk_default() net/tls: implement ->read_sock()
net/tls: handle MSG_EOR for tls_sw TX flow
nvme-tcp: fixup MSG_SENDPAGE_NOTLAST security/keys: export key_lookup() nvme/tcp: allocate socket file
nvme-tcp: enable TLS handshake upcall
nvme-tcp: control message handling for recvmsg()
nvme-fabrics: parse options 'keyring' and 'tls_key'
nvmet: make TCP sectype settable via configfs
nvmet-tcp: allocate socket file
nvmet-tcp: enable TLS handshake upcall
nvmet-tcp: control messages for recvmsg()

 drivers/nvme/common/Kconfig    |   4 +
 drivers/nvme/common/Makefile   |   3 +-
 drivers/nvme/common/keyring.c  | 182 ++++++++++++++++++++++++++++
 drivers/nvme/host/Kconfig      |  14 +++
 drivers/nvme/host/core.c       |  33 ++++-
 drivers/nvme/host/fabrics.c    |  81 ++++++++++++-
 drivers/nvme/host/fabrics.h    |   9 ++
 drivers/nvme/host/nvme.h       |   1 +
 drivers/nvme/host/tcp.c        | 189 +++++++++++++++++++++++++++--
 drivers/nvme/target/Kconfig    |  14 +++
 drivers/nvme/target/configfs.c | 128 +++++++++++++++++++-
 drivers/nvme/target/nvmet.h    |   1 +
 drivers/nvme/target/tcp.c      | 213 ++++++++++++++++++++++++++++++---
 include/linux/nvme-keyring.h   |  36 ++++++
 include/linux/nvme-tcp.h       |   6 +
 include/linux/nvme.h           |  10 ++
 net/tls/tls.h                  |   2 +
 net/tls/tls_main.c             |   2 +
 net/tls/tls_sw.c               |  82 ++++++++++++-
 security/keys/key.c            |   1 +
 20 files changed, 969 insertions(+), 42 deletions(-)
 create mode 100644 drivers/nvme/common/keyring.c
 create mode 100644 include/linux/nvme-keyring.h
Signature · 1 line
2.35.3
#3 lore raw

PATCH v4 2/17 nvme-keyring: define a 'psk' keytype

Define a 'psk' keytype to hold the NVMe TLS PSKs.

---
 drivers/nvme/common/keyring.c | 94 +++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
drivers/nvme/common/keyring.c +94 -0
index 5cf64b278119..494dd365052e 100644
@@ -8,6 +8,8 @@
 #include <linux/key-type.h>
 #include <keys/user-type.h>
 #include <linux/nvme.h>
+#include <linux/nvme-tcp.h>
+#include <linux/nvme-keyring.h>
 
 static struct key *nvme_keyring;
 
@@ -17,8 +19,94 @@ key_serial_t nvme_keyring_id(void)
 }
 EXPORT_SYMBOL_GPL(nvme_keyring_id);
 
+static void nvme_tls_psk_describe(const struct key *key, struct seq_file *m)
+{
+	seq_puts(m, key->description);
+	seq_printf(m, ": %u", key->datalen);
+}
+
+static bool nvme_tls_psk_match(const struct key *key,
+			       const struct key_match_data *match_data)
+{
+	const char *match_id;
+	size_t match_len;
+
+	if (!key->description) {
+		pr_debug("%s: no key description\n", __func__);
+		return false;
+	}
+	match_len = strlen(key->description);
+	pr_debug("%s: id %s len %zd\n", __func__, key->description, match_len);
+
+	if (!match_data->raw_data) {
+		pr_debug("%s: no match data\n", __func__);
+		return false;
+	}
+	match_id = match_data->raw_data;
+	pr_debug("%s: match '%s' '%s' len %zd\n",
+		 __func__, match_id, key->description, match_len);
+	return !memcmp(key->description, match_id, match_len);
+}
+
+static int nvme_tls_psk_match_preparse(struct key_match_data *match_data)
+{
+	match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
+	match_data->cmp = nvme_tls_psk_match;
+	return 0;
+}
+
+static struct key_type nvme_tls_psk_key_type = {
+	.name           = "psk",
+	.flags          = KEY_TYPE_NET_DOMAIN,
+	.preparse       = user_preparse,
+	.free_preparse  = user_free_preparse,
+	.match_preparse = nvme_tls_psk_match_preparse,
+	.instantiate    = generic_key_instantiate,
+	.revoke         = user_revoke,
+	.destroy        = user_destroy,
+	.describe       = nvme_tls_psk_describe,
+	.read           = user_read,
+};
+
+static struct key *nvme_tls_psk_lookup(struct key *keyring,
+		const char *hostnqn, const char *subnqn,
+		int hmac, bool generated)
+{
+	char *identity;
+	size_t identity_len = (NVMF_NQN_SIZE) * 2 + 11;
+	key_ref_t keyref;
+	key_serial_t keyring_id;
+
+	identity = kzalloc(identity_len, GFP_KERNEL);
+	if (!identity)
+		return ERR_PTR(-ENOMEM);
+
+	snprintf(identity, identity_len, "NVMe0%c%02d %s %s",
+		 generated ? 'G' : 'R', hmac, hostnqn, subnqn);
+
+	if (!keyring)
+		keyring = nvme_keyring;
+	keyring_id = key_serial(keyring);
+	pr_debug("keyring %x lookup tls psk '%s'\n",
+		 keyring_id, identity);
+	keyref = keyring_search(make_key_ref(keyring, true),
+				&nvme_tls_psk_key_type,
+				identity, false);
+	if (IS_ERR(keyref)) {
+		pr_debug("lookup tls psk '%s' failed, error %ld\n",
+			 identity, PTR_ERR(keyref));
+		kfree(identity);
+		return ERR_PTR(-ENOKEY);
+	}
+	kfree(identity);
+
+	return key_ref_to_ptr(keyref);
+}
+
 int nvme_keyring_init(void)
 {
+	int err;
+
 	nvme_keyring = keyring_alloc(".nvme",
 				     GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
 				     current_cred(),
@@ -28,12 +116,18 @@ int nvme_keyring_init(void)
 	if (IS_ERR(nvme_keyring))
 		return PTR_ERR(nvme_keyring);
 
+	err = register_key_type(&nvme_tls_psk_key_type);
+	if (err) {
+		key_put(nvme_keyring);
+		return err;
+	}
 	return 0;
 }
 EXPORT_SYMBOL_GPL(nvme_keyring_init);
 
 void nvme_keyring_exit(void)
 {
+	unregister_key_type(&nvme_tls_psk_key_type);
 	key_revoke(nvme_keyring);
 	key_put(nvme_keyring);
 }
Signature · 1 line
2.35.3
#9 lore raw

PATCH v4 4/17 nvme-tcp: add definitions for TLS cipher suites

In reply to Hannes Reinecke
---
 include/linux/nvme-tcp.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/nvme-tcp.h +6 -0
index 75470159a194..19ca863813f1 100644
@@ -18,6 +18,12 @@ enum nvme_tcp_pfv {
 	NVME_TCP_PFV_1_0 = 0x0,
 };
 
+enum nvme_tcp_tls_cipher {
+	NVME_TCP_TLS_CIPHER_INVALID     = 0,
+	NVME_TCP_TLS_CIPHER_SHA256      = 1,
+	NVME_TCP_TLS_CIPHER_SHA384      = 2,
+};
+
 enum nvme_tcp_fatal_error_status {
 	NVME_TCP_FES_INVALID_PDU_HDR		= 0x01,
 	NVME_TCP_FES_PDU_SEQ_ERR		= 0x02,
Signature · 1 line
2.35.3
#1 lore raw

PATCH v4 1/17 nvme-keyring: register '.nvme' keyring

In reply to Hannes Reinecke

Register a '.nvme' keyring to hold keys for TLS and DH-HMAC-CHAP and add a new config option NVME_KEYRING.
We need a separate keyring for NVMe as the configuration is done via individual commands (eg for configfs), and the usual per-session or per-process keyrings can't be used.

---
 drivers/nvme/common/Kconfig   |  4 ++++
 drivers/nvme/common/Makefile  |  3 ++-
 drivers/nvme/common/keyring.c | 40 +++++++++++++++++++++++++++++++++++
 drivers/nvme/host/core.c      | 10 +++++++--
 include/linux/nvme-keyring.h  | 28 ++++++++++++++++++++++++
 5 files changed, 82 insertions(+), 3 deletions(-)
 create mode 100644 drivers/nvme/common/keyring.c
 create mode 100644 include/linux/nvme-keyring.h
drivers/nvme/common/Kconfig +4 -0
index 4514f44362dd..641b27adb047 100644
@@ -2,3 +2,7 @@
 
 config NVME_COMMON
        tristate
+
+config NVME_KEYRING
+       bool
+       select KEYS
drivers/nvme/common/Makefile +2 -1
index 720c625b8a52..0cbd0b0b8d49 100644
@@ -4,4 +4,5 @@ ccflags-y			+= -I$(src)
 
 obj-$(CONFIG_NVME_COMMON)	+= nvme-common.o
 
-nvme-common-y			+= auth.o
+nvme-common-$(CONFIG_NVME_AUTH)	+= auth.o
+nvme-common-$(CONFIG_NVME_KEYRING) += keyring.o
drivers/nvme/common/keyring.c new file +40 -0
new file mode 100644
index 000000000000..5cf64b278119
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2023 Hannes Reinecke, SUSE Labs
+ */
+
+#include <linux/module.h>
+#include <linux/seq_file.h>
+#include <linux/key-type.h>
+#include <keys/user-type.h>
+#include <linux/nvme.h>
+
+static struct key *nvme_keyring;
+
+key_serial_t nvme_keyring_id(void)
+{
+	return nvme_keyring->serial;
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_id);
+
+int nvme_keyring_init(void)
+{
+	nvme_keyring = keyring_alloc(".nvme",
+				     GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
+				     current_cred(),
+				     (KEY_POS_ALL & ~KEY_POS_SETATTR) |
+				     (KEY_USR_ALL & ~KEY_USR_SETATTR),
+				     KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
+	if (IS_ERR(nvme_keyring))
+		return PTR_ERR(nvme_keyring);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_init);
+
+void nvme_keyring_exit(void)
+{
+	key_revoke(nvme_keyring);
+	key_put(nvme_keyring);
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_exit);
drivers/nvme/host/core.c +8 -2
index 6c1e7d6709e0..79aa215aec76 100644
@@ -25,6 +25,7 @@
 #include "nvme.h"
 #include "fabrics.h"
 #include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
@@ -5401,12 +5402,16 @@ static int __init nvme_core_init(void)
 		result = PTR_ERR(nvme_ns_chr_class);
 		goto unregister_generic_ns;
 	}
-
-	result = nvme_init_auth();
+	result = nvme_keyring_init();
 	if (result)
 		goto destroy_ns_chr;
+	result = nvme_init_auth();
+	if (result)
+		goto keyring_exit;
 	return 0;
 
+keyring_exit:
+	nvme_keyring_exit();
 destroy_ns_chr:
 	class_destroy(nvme_ns_chr_class);
 unregister_generic_ns:
@@ -5430,6 +5435,7 @@ static int __init nvme_core_init(void)
 static void __exit nvme_core_exit(void)
 {
 	nvme_exit_auth();
+	nvme_keyring_exit();
 	class_destroy(nvme_ns_chr_class);
 	class_destroy(nvme_subsys_class);
 	class_destroy(nvme_class);
include/linux/nvme-keyring.h new file +28 -0
new file mode 100644
index 000000000000..32bd264a71e6
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2023 Hannes Reinecke, SUSE Labs
+ */
+
+#ifndef _NVME_KEYRING_H
+#define _NVME_KEYRING_H
+
+#ifdef CONFIG_NVME_KEYRING
+
+key_serial_t nvme_keyring_id(void);
+int nvme_keyring_init(void);
+void nvme_keyring_exit(void);
+
+#else
+
+static inline key_serial_t nvme_keyring_id(void)
+{
+	return 0;
+}
+static inline int nvme_keyring_init(void)
+{
+	return 0;
+}
+static inline void nvme_keyring_exit(void) {}
+
+#endif /* !CONFIG_NVME_KEYRING */
+#endif /* _NVME_KEYRING_H */
Signature · 1 line
2.35.3
#6 lore raw

PATCH v4 3/17 nvme: add TCP TSAS definitions

In reply to Hannes Reinecke
---
 include/linux/nvme.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/nvme.h +10 -0
index 4fad4aa245fb..6e55a7a701e6 100644
@@ -108,6 +108,13 @@ enum {
 	NVMF_RDMA_CMS_RDMA_CM	= 1, /* Sockets based endpoint addressing */
 };
 
+/* TSAS SECTYPE for TCP transport */
+enum {
+	NVMF_TCP_SECTYPE_NONE = 0, /* No Security */
+	NVMF_TCP_SECTYPE_TLS12 = 1, /* TLSv1.2, NVMe-oF 1.1 and NVMe-TCP 3.6.1.1 */
+	NVMF_TCP_SECTYPE_TLS13 = 2, /* TLSv1.3, NVMe-oF 1.1 and NVMe-TCP 3.6.1.1 */
+};
+
 #define NVME_AQ_DEPTH		32
 #define NVME_NR_AEN_COMMANDS	1
 #define NVME_AQ_BLK_MQ_DEPTH	(NVME_AQ_DEPTH - NVME_NR_AEN_COMMANDS)
@@ -1453,6 +1460,9 @@ struct nvmf_disc_rsp_page_entry {
 			__u16	pkey;
 			__u8	resv10[246];
 		} rdma;
+		struct tcp {
+			__u8	sectype;
+		} tcp;
 	} tsas;
 };
 
Signature · 1 line
2.35.3
#22 lore raw

PATCH v4 7/17 net/tls: handle MSG_EOR for tls_sw TX flow

In reply to Hannes Reinecke

tls_sw_sendmsg() / tls_do_sw_sendpage() already checks for an 'end of record' by evaluating the 'MSG_MORE' / 'MSG_SENDPAGE_NOTLAST' setting. So MSG_EOR can easily be handled by treating it as the opposite of MSG_MORE / MSG_SENDPAGE_NOTLAST.

---
 net/tls/tls_sw.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
net/tls/tls_sw.c +8 -3
index 827292e29f99..9bee2dcd55bf 100644
@@ -953,9 +953,12 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 	int pending;
 
 	if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
-			       MSG_CMSG_COMPAT))
+			       MSG_EOR | MSG_CMSG_COMPAT))
 		return -EOPNOTSUPP;
 
+	if (msg->msg_flags & MSG_EOR)
+		eor = true;
+
 	ret = mutex_lock_interruptible(&tls_ctx->tx_lock);
 	if (ret)
 		return ret;
@@ -1173,6 +1176,8 @@ static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
 	bool eor;
 
 	eor = !(flags & MSG_SENDPAGE_NOTLAST);
+	if (flags & MSG_EOR)
+		eor = true;
 	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
 
 	/* Call the sk_stream functions to manage the sndbuf mem. */
@@ -1274,7 +1279,7 @@ static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
 int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
 			   int offset, size_t size, int flags)
 {
-	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
+	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_EOR |
 		      MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY |
 		      MSG_NO_SHARED_FRAGS))
 		return -EOPNOTSUPP;
@@ -1288,7 +1293,7 @@ int tls_sw_sendpage(struct sock *sk, struct page *page,
 	struct tls_context *tls_ctx = tls_get_ctx(sk);
 	int ret;
 
-	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
+	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_EOR |
 		      MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
 		return -EOPNOTSUPP;
 
Signature · 1 line
2.35.3
#28 lore raw

PATCH v4 8/17 nvme-tcp: fixup MSG_SENDPAGE_NOTLAST

In reply to Hannes Reinecke

We can only set MSG_SENDPAGE_NOTLAST when sending the command PDU if the actual data is transferred via sendpage(), too.
If we use sendmsg() to transfer the data we end up with a TX stall on TLS as it implements different code paths for sendmsg() and sendpage(). So check in nvme_tcp_try_send_pdu() if inline data is present and revert to sendmsg if the inline data cannot be sent via sendpage().

---
 drivers/nvme/host/tcp.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/nvme/host/tcp.c +14 -4
index 273c1f2760a4..9ecd7db2cd98 100644
@@ -995,9 +995,11 @@ static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
 		if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
 			flags |= MSG_EOR;
 		else
-			flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
+			flags |= MSG_MORE;
 
 		if (sendpage_ok(page)) {
+			if (flags & MSG_MORE)
+				flags |= MSG_SENDPAGE_NOTLAST;
 			ret = kernel_sendpage(queue->sock, page, offset, len,
 					flags);
 		} else {
@@ -1049,15 +1051,23 @@ static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
 	int ret;
 
 	if (inline_data || nvme_tcp_queue_more(queue))
-		flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
+		flags |= MSG_MORE;
 	else
 		flags |= MSG_EOR;
 
 	if (queue->hdr_digest && !req->offset)
 		nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
 
-	ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
-			offset_in_page(pdu) + req->offset, len,  flags);
+	if (!inline_data || sendpage_ok(nvme_tcp_req_cur_page(req))) {
+		if (flags & MSG_MORE)
+			flags |= MSG_SENDPAGE_NOTLAST;
+		ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
+				offset_in_page(pdu) + req->offset, len,  flags);
+	} else {
+		ret = sock_no_sendpage(queue->sock, virt_to_page(pdu),
+				offset_in_page(pdu) + req->offset,
+				len, flags);
+	}
 	if (unlikely(ret <= 0))
 		return ret;
 
Signature · 1 line
2.35.3
#31 lore raw

PATCH v4 10/17 nvme/tcp: allocate socket file

In reply to Hannes Reinecke

When using the TLS upcall we need to allocate a socket file such that the userspace daemon is able to use the socket.

---
 drivers/nvme/host/tcp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/nvme/host/tcp.c +12 -2
index 9ecd7db2cd98..9deba481ae6a 100644
@@ -1331,7 +1331,9 @@ static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
 	}
 
 	noreclaim_flag = memalloc_noreclaim_save();
-	sock_release(queue->sock);
+	/* ->sock will be released by fput() */
+	fput(queue->sock->file);
+	queue->sock = NULL;
 	memalloc_noreclaim_restore(noreclaim_flag);
 
 	kfree(queue->pdu);
@@ -1505,6 +1507,7 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
 	int ret, rcv_pdu_size;
+	struct file *sock_file;
 
 	mutex_init(&queue->queue_lock);
 	queue->ctrl = ctrl;
@@ -1527,6 +1530,12 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
 		goto err_destroy_mutex;
 	}
 
+	sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL);
+	if (IS_ERR(sock_file)) {
+		sock_release(queue->sock);
+		ret = PTR_ERR(sock_file);
+		goto err_sock;
+	}
 	nvme_tcp_reclassify_socket(queue->sock);
 
 	/* Single syn retry */
@@ -1648,7 +1657,8 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
 	if (queue->hdr_digest || queue->data_digest)
 		nvme_tcp_free_crypto(queue);
 err_sock:
-	sock_release(queue->sock);
+	/* ->sock will be released by fput() */
+	fput(queue->sock->file);
 	queue->sock = NULL;
 err_destroy_mutex:
 	mutex_destroy(&queue->send_mutex);
Signature · 1 line
2.35.3
#39 lore raw

PATCH v4 13/17 nvme-fabrics: parse options 'keyring' and 'tls_key'

In reply to Hannes Reinecke

Parse the fabrics options 'keyring' and 'tls_key' and store the referenced keys in the options structure.

---
 drivers/nvme/host/fabrics.c | 69 ++++++++++++++++++++++++++++++++++++-
 drivers/nvme/host/fabrics.h |  6 ++++
 drivers/nvme/host/tcp.c     | 11 ++++--
 3 files changed, 82 insertions(+), 4 deletions(-)
drivers/nvme/host/fabrics.c +68 -1
index 2aa8fb991455..8fb80ccc38e1 100644
@@ -605,6 +605,10 @@ static const match_table_t opt_tokens = {
 	{ NVMF_OPT_NR_WRITE_QUEUES,	"nr_write_queues=%d"	},
 	{ NVMF_OPT_NR_POLL_QUEUES,	"nr_poll_queues=%d"	},
 	{ NVMF_OPT_TOS,			"tos=%d"		},
+#ifdef CONFIG_NVME_TCP_TLS
+	{ NVMF_OPT_KEYRING,		"keyring=%d"		},
+	{ NVMF_OPT_TLS_KEY,		"tls_key=%d"		},
+#endif
 	{ NVMF_OPT_FAIL_FAST_TMO,	"fast_io_fail_tmo=%d"	},
 	{ NVMF_OPT_DISCOVERY,		"discovery"		},
 	{ NVMF_OPT_DHCHAP_SECRET,	"dhchap_secret=%s"	},
@@ -622,8 +626,9 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 	char *options, *o, *p;
 	int token, ret = 0;
 	size_t nqnlen  = 0;
-	int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
+	int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO, key_id;
 	uuid_t hostid;
+	struct key *key = NULL;
 
 	/* Set defaults */
 	opts->queue_size = NVMF_DEF_QUEUE_SIZE;
@@ -891,6 +896,66 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 			}
 			opts->tos = token;
 			break;
+		case NVMF_OPT_KEYRING:
+			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+				pr_err("TLS is not supported\n");
+				ret = -EINVAL;
+				goto out;
+			}
+			if (match_int(args, &key_id)) {
+				ret = -EINVAL;
+				goto out;
+			}
+			if (key_id < 0) {
+				pr_err("Invalid keyring id %d\n", key_id);
+				ret = -EINVAL;
+				goto out;
+			}
+			if (!key_id) {
+				pr_debug("Using default keyring\n");
+				key_put(opts->keyring);
+				opts->keyring = NULL;
+				break;
+			}
+			key = key_lookup(key_id);
+			if (!key) {
+				pr_err("Keyring id %08x not found\n", key_id);
+				ret = -ENOKEY;
+				goto out;
+			}
+			key_put(opts->keyring);
+			opts->keyring = key;
+			break;
+		case NVMF_OPT_TLS_KEY:
+			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+				pr_err("TLS is not supported\n");
+				ret = -EINVAL;
+				goto out;
+			}
+			if (match_int(args, &key_id)) {
+				ret = -EINVAL;
+				goto out;
+			}
+			if (key_id < 0) {
+				pr_err("Invalid key id %d\n", key_id);
+				ret = -EINVAL;
+				goto out;
+			}
+			if (!key_id) {
+				pr_debug("Using 'best' PSK\n");
+				key_put(opts->tls_key);
+				opts->tls_key = NULL;
+				break;
+			}
+			key = key_lookup(key_id);
+			if (!key) {
+				pr_err("Key id %08x not found\n", key_id);
+				ret = -ENOKEY;
+				goto out;
+			}
+			key_put(opts->tls_key);
+			opts->tls_key = key;
+			break;
 		case NVMF_OPT_DISCOVERY:
 			opts->discovery_nqn = true;
 			break;
@@ -1055,6 +1120,8 @@ static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
 void nvmf_free_options(struct nvmf_ctrl_options *opts)
 {
 	nvmf_host_put(opts->host);
+	key_put(opts->keyring);
+	key_put(opts->tls_key);
 	kfree(opts->transport);
 	kfree(opts->traddr);
 	kfree(opts->trsvcid);
drivers/nvme/host/fabrics.h +6 -0
index 5db36e250e7a..2ff7b7168a40 100644
@@ -71,6 +71,8 @@ enum {
 	NVMF_OPT_DHCHAP_SECRET	= 1 << 23,
 	NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
 	NVMF_OPT_TLS		= 1 << 25,
+	NVMF_OPT_KEYRING	= 1 << 26,
+	NVMF_OPT_TLS_KEY	= 1 << 27,
 };
 
 /**
@@ -103,6 +105,8 @@ enum {
  * @dhchap_secret: DH-HMAC-CHAP secret
  * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
  *              authentication
+ * @keyring:    Keyring to use for key lookups
+ * @tls_key:    TLS key for encrypted connections (TCP)
  * @tls:        Start TLS encrypted connections (TCP)
  * @disable_sqflow: disable controller sq flow control
  * @hdr_digest: generate/verify header digest (TCP)
@@ -130,6 +134,8 @@ struct nvmf_ctrl_options {
 	int			max_reconnects;
 	char			*dhchap_secret;
 	char			*dhchap_ctrl_secret;
+	struct key		*keyring;
+	struct key		*tls_key;
 	bool			tls;
 	bool			disable_sqflow;
 	bool			hdr_digest;
drivers/nvme/host/tcp.c +8 -3
index a3ac512fd1cc..84bf6c80cc7c 100644
@@ -1582,6 +1582,8 @@ static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
 
 	dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
 		qid, pskid);
+	if (nctrl->opts->keyring)
+		keyring = key_serial(nctrl->opts->keyring);
 	args.ta_sock = queue->sock;
 	args.ta_done = nvme_tcp_tls_done;
 	args.ta_data = queue;
@@ -1890,9 +1892,12 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
 	key_serial_t psk_id = 0;
 
 	if (ctrl->opts->tls) {
-		psk_id = nvme_tls_psk_default(NULL,
-					      ctrl->opts->host->nqn,
-					      ctrl->opts->subsysnqn);
+		if (ctrl->opts->tls_key)
+			psk_id = key_serial(ctrl->opts->tls_key);
+		else
+			psk_id = nvme_tls_psk_default(ctrl->opts->keyring,
+						      ctrl->opts->host->nqn,
+						      ctrl->opts->subsysnqn);
 		if (!psk_id) {
 			dev_err(ctrl->device, "no valid PSK found\n");
 			ret = -ENOKEY;
Signature · 1 line
2.35.3
#43 lore raw

PATCH v4 14/17 nvmet: make TCP sectype settable via configfs

In reply to Hannes Reinecke

Add a new configfs attribute 'addr_tsas' to make the TCP sectype settable via configfs.

---
 drivers/nvme/target/configfs.c | 65 ++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
drivers/nvme/target/configfs.c +65 -0
index 907143870da5..7f826ac8b75c 100644
@@ -303,6 +303,11 @@ static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
 }
 
+static void nvmet_port_init_tsas_tcp(struct nvmet_port *port, int sectype)
+{
+	port->disc_addr.tsas.tcp.sectype = sectype;
+}
+
 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
 		const char *page, size_t count)
 {
@@ -325,11 +330,70 @@ static ssize_t nvmet_addr_trtype_store(struct config_item *item,
 	port->disc_addr.trtype = nvmet_transport[i].type;
 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
 		nvmet_port_init_tsas_rdma(port);
+	else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP)
+		nvmet_port_init_tsas_tcp(port, NVMF_TCP_SECTYPE_NONE);
 	return count;
 }
 
 CONFIGFS_ATTR(nvmet_, addr_trtype);
 
+static const struct nvmet_type_name_map nvmet_addr_tsas_tcp[] = {
+	{ NVMF_TCP_SECTYPE_NONE,	"none" },
+	{ NVMF_TCP_SECTYPE_TLS13,	"tls1.3" },
+};
+
+static const struct nvmet_type_name_map nvmet_addr_tsas_rdma[] = {
+	{ NVMF_RDMA_QPTYPE_CONNECTED,	"connected" },
+	{ NVMF_RDMA_QPTYPE_DATAGRAM,	"datagram"  },
+};
+
+static ssize_t nvmet_addr_tsas_show(struct config_item *item,
+		char *page)
+{
+	struct nvmet_port *port = to_nvmet_port(item);
+	int i;
+
+	if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
+		for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
+			if (port->disc_addr.tsas.tcp.sectype == nvmet_addr_tsas_tcp[i].type)
+				return sprintf(page, "%s\n", nvmet_addr_tsas_tcp[i].name);
+		}
+	} else if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
+		for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
+			if (port->disc_addr.tsas.rdma.qptype == nvmet_addr_tsas_rdma[i].type)
+				return sprintf(page, "%s\n", nvmet_addr_tsas_rdma[i].name);
+		}
+	}
+	return sprintf(page, "reserved\n");
+}
+
+static ssize_t nvmet_addr_tsas_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_port *port = to_nvmet_port(item);
+	int i;
+
+	if (nvmet_is_port_enabled(port, __func__))
+		return -EACCES;
+
+	if (port->disc_addr.trtype != NVMF_TRTYPE_TCP)
+		return -EINVAL;
+
+	for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
+		if (sysfs_streq(page, nvmet_addr_tsas_tcp[i].name))
+			goto found;
+	}
+
+	pr_err("Invalid value '%s' for tsas\n", page);
+	return -EINVAL;
+
+found:
+	nvmet_port_init_tsas_tcp(port, nvmet_addr_tsas_tcp[i].type);
+	return count;
+}
+
+CONFIGFS_ATTR(nvmet_, addr_tsas);
+
 /*
  * Namespace structures & file operation functions below
  */
@@ -1741,6 +1805,7 @@ static struct configfs_attribute *nvmet_port_attrs[] = {
 	&nvmet_attr_addr_traddr,
 	&nvmet_attr_addr_trsvcid,
 	&nvmet_attr_addr_trtype,
+	&nvmet_attr_addr_tsas,
 	&nvmet_attr_param_inline_data_size,
 #ifdef CONFIG_BLK_DEV_INTEGRITY
 	&nvmet_attr_param_pi_enable,
Signature · 1 line
2.35.3
#10 lore raw

PATCH v4 5/17 nvme-keyring: implement nvme_tls_psk_default()

In reply to Hannes Reinecke

Implement a function to select the preferred PSK for TLS.

---
 drivers/nvme/common/keyring.c | 48 +++++++++++++++++++++++++++++++++++
 include/linux/nvme-keyring.h  |  8 ++++++
 2 files changed, 56 insertions(+)
drivers/nvme/common/keyring.c +48 -0
index 494dd365052e..f8d9a208397b 100644
@@ -5,6 +5,7 @@
 
 #include <linux/module.h>
 #include <linux/seq_file.h>
+#include <linux/key.h>
 #include <linux/key-type.h>
 #include <keys/user-type.h>
 #include <linux/nvme.h>
@@ -103,6 +104,53 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
 	return key_ref_to_ptr(keyref);
 }
 
+/*
+ * NVMe PSK priority list
+ *
+ * 'Retained' PSKs (ie 'generated == false')
+ * should be preferred to 'generated' PSKs,
+ * and SHA-384 should be preferred to SHA-256.
+ */
+struct nvme_tls_psk_priority_list {
+	bool generated;
+	enum nvme_tcp_tls_cipher cipher;
+} nvme_tls_psk_prio[] = {
+	{ .generated = false,
+	  .cipher = NVME_TCP_TLS_CIPHER_SHA384, },
+	{ .generated = false,
+	  .cipher = NVME_TCP_TLS_CIPHER_SHA256, },
+	{ .generated = true,
+	  .cipher = NVME_TCP_TLS_CIPHER_SHA384, },
+	{ .generated = true,
+	  .cipher = NVME_TCP_TLS_CIPHER_SHA256, },
+};
+
+/*
+ * nvme_tls_psk_default - Return the preferred PSK to use for TLS ClientHello
+ */
+key_serial_t nvme_tls_psk_default(struct key *keyring,
+		      const char *hostnqn, const char *subnqn)
+{
+	struct key *tls_key;
+	key_serial_t tls_key_id;
+	int prio;
+
+	for (prio = 0; prio < ARRAY_SIZE(nvme_tls_psk_prio); prio++) {
+		bool generated = nvme_tls_psk_prio[prio].generated;
+		enum nvme_tcp_tls_cipher cipher = nvme_tls_psk_prio[prio].cipher;
+
+		tls_key = nvme_tls_psk_lookup(keyring, hostnqn, subnqn,
+					      cipher, generated);
+		if (!IS_ERR(tls_key)) {
+			tls_key_id = tls_key->serial;
+			key_put(tls_key);
+			return tls_key_id;
+		}
+	}
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nvme_tls_psk_default);
+
 int nvme_keyring_init(void)
 {
 	int err;
include/linux/nvme-keyring.h +8 -0
index 32bd264a71e6..4efea9dd967c 100644
@@ -8,12 +8,20 @@
 
 #ifdef CONFIG_NVME_KEYRING
 
+key_serial_t nvme_tls_psk_default(struct key *keyring,
+		const char *hostnqn, const char *subnqn);
+
 key_serial_t nvme_keyring_id(void);
 int nvme_keyring_init(void);
 void nvme_keyring_exit(void);
 
 #else
 
+static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
+		const char *hostnqn, const char *subnqn)
+{
+	return 0;
+}
 static inline key_serial_t nvme_keyring_id(void)
 {
 	return 0;
Signature · 1 line
2.35.3
#13 lore raw

PATCH v4 6/17 net/tls: implement ->read_sock()

In reply to Hannes Reinecke

Implement ->read_sock() function for use with nvme-tcp.

Cc Boris Pismenny <[email protected]>
Cc Jakub Kicinski <[email protected]>
---
 net/tls/tls.h      |  2 ++
 net/tls/tls_main.c |  2 ++
 net/tls/tls_sw.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+)
net/tls/tls.h +2 -0
index 804c3880d028..a5bf3a9ce142 100644
@@ -113,6 +113,8 @@ bool tls_sw_sock_is_readable(struct sock *sk);
 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
 			   struct pipe_inode_info *pipe,
 			   size_t len, unsigned int flags);
+int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
+		     sk_read_actor_t read_actor);
 
 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
 int tls_device_sendpage(struct sock *sk, struct page *page,
net/tls/tls_main.c +2 -0
index b32c112984dd..0b51b19aa46d 100644
@@ -921,9 +921,11 @@ static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG]
 
 	ops[TLS_BASE][TLS_SW  ] = ops[TLS_BASE][TLS_BASE];
 	ops[TLS_BASE][TLS_SW  ].splice_read	= tls_sw_splice_read;
+	ops[TLS_BASE][TLS_SW  ].read_sock	= tls_sw_read_sock;
 
 	ops[TLS_SW  ][TLS_SW  ] = ops[TLS_SW  ][TLS_BASE];
 	ops[TLS_SW  ][TLS_SW  ].splice_read	= tls_sw_splice_read;
+	ops[TLS_SW  ][TLS_SW  ].read_sock	= tls_sw_read_sock;
 
 #ifdef CONFIG_TLS_DEVICE
 	ops[TLS_HW  ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
net/tls/tls_sw.c +71 -0

77 lines not on this page · raw message

Signature · 1 line
2.35.3
#30 lore raw

PATCH v4 9/17 security/keys: export key_lookup()

In reply to Hannes Reinecke

For in-kernel consumers one cannot readily assign a user (eg when running from a workqueue), so the normal key search permissions cannot be applied.
This patch exports the 'key_lookup()' function for a simple lookup of keys without checking for permissions.

---
 security/keys/key.c | 1 +
 1 file changed, 1 insertion(+)
security/keys/key.c +1 -0
index 5c0c7df833f8..bd1b7d45df90 100644
@@ -693,6 +693,7 @@ struct key *key_lookup(key_serial_t id)
 	spin_unlock(&key_serial_lock);
 	return key;
 }
+EXPORT_SYMBOL_GPL(key_lookup);
 
 /*
  * Find and lock the specified key type against removal.
Signature · 1 line
2.35.3
#38 lore raw

PATCH v4 12/17 nvme-tcp: control message handling for recvmsg()

In reply to Hannes Reinecke

kTLS is sending TLS ALERT messages as control messages for recvmsg(). As we can't do anything sensible with it just abort the connection and let the userspace agent to a re-negotiation.

---
 drivers/nvme/host/tcp.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/nvme/host/tcp.c +22 -1
index d25695cf4e03..a3ac512fd1cc 100644
@@ -1362,6 +1362,11 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
 {
 	struct nvme_tcp_icreq_pdu *icreq;
 	struct nvme_tcp_icresp_pdu *icresp;
+#ifdef CONFIG_NVME_TCP_TLS
+	char cbuf[CMSG_LEN(sizeof(char))] = {};
+	struct cmsghdr *cmsg;
+	unsigned char ctype;
+#endif
 	struct msghdr msg = {};
 	struct kvec iov;
 	bool ctrl_hdgst, ctrl_ddgst;
@@ -1399,11 +1404,27 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
 	memset(&msg, 0, sizeof(msg));
 	iov.iov_base = icresp;
 	iov.iov_len = sizeof(*icresp);
+#ifdef CONFIG_NVME_TCP_TLS
+	msg.msg_control = cbuf;
+	msg.msg_controllen = sizeof(cbuf);
+#endif
 	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
 			iov.iov_len, msg.msg_flags);
 	if (ret < 0)
 		goto free_icresp;
-
+#ifdef CONFIG_NVME_TCP_TLS
+	cmsg = (struct cmsghdr *)cbuf;
+	if (CMSG_OK(&msg, cmsg) &&
+	    cmsg->cmsg_level == SOL_TLS &&
+	    cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
+		ctype = *((unsigned char *)CMSG_DATA(cmsg));
+		if (ctype != TLS_RECORD_TYPE_DATA) {
+			pr_err("queue %d: unhandled TLS record %d\n",
+			       nvme_tcp_queue_id(queue), ctype);
+			return -ENOTCONN;
+		}
+	}
+#endif
 	ret = -EINVAL;
 	if (icresp->hdr.type != nvme_tcp_icresp) {
 		pr_err("queue %d: bad type returned %d\n",
Signature · 1 line
2.35.3
#32 lore raw

PATCH v4 11/17 nvme-tcp: enable TLS handshake upcall

In reply to Hannes Reinecke

Add a fabrics option 'tls' and start the TLS handshake upcall with the default PSK. When TLS is started the PSK key serial number is displayed in the sysfs attribute 'tls_key'

---
 drivers/nvme/host/Kconfig   |  14 ++++
 drivers/nvme/host/core.c    |  23 ++++++-
 drivers/nvme/host/fabrics.c |  12 ++++
 drivers/nvme/host/fabrics.h |   3 +
 drivers/nvme/host/nvme.h    |   1 +
 drivers/nvme/host/tcp.c     | 129 ++++++++++++++++++++++++++++++++++--
 6 files changed, 174 insertions(+), 8 deletions(-)
drivers/nvme/host/Kconfig +14 -0
index 2f6a7f8c94e8..96a74041bb0a 100644
@@ -92,6 +92,20 @@ config NVME_TCP
 
 	  If unsure, say N.
 
+config NVME_TCP_TLS
+	bool "NVMe over Fabrics TCP TLS encryption support"
+	depends on NVME_TCP
+	select NVME_COMMON
+	select NVME_KEYRING
+	select NET_HANDSHAKE
+	help
+	  Enables TLS encryption for NVMe TCP using the netlink handshake API.
+
+	  The TLS handshake daemon is availble at
+	  https://github.com/oracle/ktls-utils.
+
+	  If unsure, say N.
+
 config NVME_AUTH
 	bool "NVM Express over Fabrics In-Band Authentication"
 	depends on NVME_CORE
drivers/nvme/host/core.c +21 -2
index 79aa215aec76..937013127c91 100644
@@ -3889,6 +3889,19 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR,
 	nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
 #endif
 
+#ifdef CONFIG_NVME_TCP_TLS
+static ssize_t tls_key_show(struct device *dev,
+			    struct device_attribute *attr, char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+	if (!ctrl->tls_key)
+		return 0;
+	return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
+}
+static DEVICE_ATTR_RO(tls_key);
+#endif
+
 static struct attribute *nvme_dev_attrs[] = {
 	&dev_attr_reset_controller.attr,
 	&dev_attr_rescan_controller.attr,
@@ -3915,6 +3928,9 @@ static struct attribute *nvme_dev_attrs[] = {
 #ifdef CONFIG_NVME_AUTH
 	&dev_attr_dhchap_secret.attr,
 	&dev_attr_dhchap_ctrl_secret.attr,
+#endif
+#ifdef CONFIG_NVME_TCP_TLS
+	&dev_attr_tls_key.attr,
 #endif
 	NULL
 };
@@ -3945,7 +3961,10 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
 	if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
 		return 0;
 #endif
-
+#ifdef CONFIG_NVME_TCP_TLS
+	if (a == &dev_attr_tls_key.attr && !ctrl->opts)
+		return 0;
+#endif
 	return a->mode;
 }
 
@@ -5080,7 +5099,7 @@ static void nvme_free_ctrl(struct device *dev)
 
 	if (!subsys || ctrl->instance != subsys->instance)
 		ida_free(&nvme_instance_ida, ctrl->instance);
-
+	key_put(ctrl->tls_key);
 	nvme_free_cels(ctrl);
 	nvme_mpath_uninit(ctrl);
 	nvme_auth_stop(ctrl);
drivers/nvme/host/fabrics.c +12 -0
index bbaa04a0c502..2aa8fb991455 100644
@@ -609,6 +609,9 @@ static const match_table_t opt_tokens = {
 	{ NVMF_OPT_DISCOVERY,		"discovery"		},
 	{ NVMF_OPT_DHCHAP_SECRET,	"dhchap_secret=%s"	},
 	{ NVMF_OPT_DHCHAP_CTRL_SECRET,	"dhchap_ctrl_secret=%s"	},
+#ifdef CONFIG_NVME_TCP_TLS
+	{ NVMF_OPT_TLS,			"tls"			},
+#endif
 	{ NVMF_OPT_ERR,			NULL			}
 };
 
@@ -632,6 +635,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 	opts->hdr_digest = false;
 	opts->data_digest = false;
 	opts->tos = -1; /* < 0 == use transport default */
+	opts->tls = false;
 
 	options = o = kstrdup(buf, GFP_KERNEL);
 	if (!options)
@@ -918,6 +922,14 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 			kfree(opts->dhchap_ctrl_secret);
 			opts->dhchap_ctrl_secret = p;
 			break;
+		case NVMF_OPT_TLS:
+			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+				pr_err("TLS is not supported\n");
+				ret = -EINVAL;
+				goto out;
+			}
+			opts->tls = true;
+			break;
 		default:
 			pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
 				p);
drivers/nvme/host/fabrics.h +3 -0
index dcac3df8a5f7..5db36e250e7a 100644
@@ -70,6 +70,7 @@ enum {
 	NVMF_OPT_DISCOVERY	= 1 << 22,
 	NVMF_OPT_DHCHAP_SECRET	= 1 << 23,
 	NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
+	NVMF_OPT_TLS		= 1 << 25,
 };
 
 /**
@@ -102,6 +103,7 @@ enum {
  * @dhchap_secret: DH-HMAC-CHAP secret
  * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
  *              authentication
+ * @tls:        Start TLS encrypted connections (TCP)
  * @disable_sqflow: disable controller sq flow control
  * @hdr_digest: generate/verify header digest (TCP)
  * @data_digest: generate/verify data digest (TCP)
@@ -128,6 +130,7 @@ struct nvmf_ctrl_options {
 	int			max_reconnects;
 	char			*dhchap_secret;
 	char			*dhchap_ctrl_secret;
+	bool			tls;
 	bool			disable_sqflow;
 	bool			hdr_digest;
 	bool			data_digest;
drivers/nvme/host/nvme.h +1 -0
index bf46f122e9e1..fbcc0ccf2ef2 100644
@@ -347,6 +347,7 @@ struct nvme_ctrl {
 	struct nvme_dhchap_key *ctrl_key;
 	u16 transaction;
 #endif
+	struct key *tls_key;
 
 	/* Power saving configuration */
 	u64 ps_max_latency_us;
drivers/nvme/host/tcp.c +123 -6

184 lines not on this page · raw message

Signature · 1 line
2.35.3
#44 lore raw

PATCH v4 15/17 nvmet-tcp: allocate socket file

In reply to Hannes Reinecke

For the TLS upcall we need to allocate a socket file such that the userspace daemon is able to use the socket.

---
 drivers/nvme/target/tcp.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
drivers/nvme/target/tcp.c +17 -12
index ed98df72c76b..b53b1163e75b 100644
@@ -1481,12 +1481,12 @@ static void nvmet_tcp_release_queue_work(struct work_struct *w)
 	nvmet_sq_destroy(&queue->nvme_sq);
 	cancel_work_sync(&queue->io_work);
 	nvmet_tcp_free_cmd_data_in_buffers(queue);
-	sock_release(queue->sock);
+	/* ->sock will be released by fput() */
+	fput(queue->sock->file);
 	nvmet_tcp_free_cmds(queue);
 	if (queue->hdr_digest || queue->data_digest)
 		nvmet_tcp_free_crypto(queue);
 	ida_free(&nvmet_tcp_queue_ida, queue->idx);
-
 	page = virt_to_head_page(queue->pf_cache.va);
 	__page_frag_cache_drain(page, queue->pf_cache.pagecnt_bias);
 	kfree(queue);
@@ -1609,15 +1609,16 @@ static int nvmet_tcp_set_queue_sock(struct nvmet_tcp_queue *queue)
 	return ret;
 }
 
-static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
+static void nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
 		struct socket *newsock)
 {
 	struct nvmet_tcp_queue *queue;
+	struct file *sock_file;
 	int ret;
 
 	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
 	if (!queue)
-		return -ENOMEM;
+		return;
 
 	INIT_WORK(&queue->release_work, nvmet_tcp_release_queue_work);
 	INIT_WORK(&queue->io_work, nvmet_tcp_io_work);
@@ -1630,10 +1631,16 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
 	init_llist_head(&queue->resp_list);
 	INIT_LIST_HEAD(&queue->resp_send_list);
 
+	sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL);
+	if (IS_ERR(sock_file)) {
+		ret = PTR_ERR(sock_file);
+		goto out_free_queue;
+	}
+
 	queue->idx = ida_alloc(&nvmet_tcp_queue_ida, GFP_KERNEL);
 	if (queue->idx < 0) {
 		ret = queue->idx;
-		goto out_free_queue;
+		goto out_sock;
 	}
 
 	ret = nvmet_tcp_alloc_cmd(queue, &queue->connect);
@@ -1654,7 +1661,7 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
 	if (ret)
 		goto out_destroy_sq;
 
-	return 0;
+	return;
 out_destroy_sq:
 	mutex_lock(&nvmet_tcp_queue_mutex);
 	list_del_init(&queue->queue_list);
@@ -1664,9 +1671,11 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
 	nvmet_tcp_free_cmd(&queue->connect);
 out_ida_remove:
 	ida_free(&nvmet_tcp_queue_ida, queue->idx);
+out_sock:
+	fput(queue->sock->file);
 out_free_queue:
 	kfree(queue);
-	return ret;
+	pr_err("failed to allocate queue");
 }
 
 static void nvmet_tcp_accept_work(struct work_struct *w)
@@ -1683,11 +1692,7 @@ static void nvmet_tcp_accept_work(struct work_struct *w)
 				pr_warn("failed to accept err=%d\n", ret);
 			return;
 		}
-		ret = nvmet_tcp_alloc_queue(port, newsock);
-		if (ret) {
-			pr_err("failed to allocate queue\n");
-			sock_release(newsock);
-		}
+		nvmet_tcp_alloc_queue(port, newsock);
 	}
 }
 
Signature · 1 line
2.35.3
#45 lore raw

PATCH v4 16/17 nvmet-tcp: enable TLS handshake upcall

In reply to Hannes Reinecke

Add functions to start the TLS handshake upcall when the TCP TSAS sectype is set to 'tls1.3' and add a config option NVME_TARGET_TCP_TLS.

---
 drivers/nvme/target/Kconfig    |  14 ++++
 drivers/nvme/target/configfs.c |  63 +++++++++++++++-
 drivers/nvme/target/nvmet.h    |   1 +
 drivers/nvme/target/tcp.c      | 134 ++++++++++++++++++++++++++++++++-
 4 files changed, 206 insertions(+), 6 deletions(-)
drivers/nvme/target/Kconfig +14 -0
index 79fc64035ee3..15af2b4341f6 100644
@@ -84,6 +84,20 @@ config NVME_TARGET_TCP
 
 	  If unsure, say N.
 
+config NVME_TARGET_TCP_TLS
+	bool "NVMe over Fabrics TCP target TLS encryption support"
+	depends on NVME_TARGET_TCP
+	select NVME_COMMON
+	select NVME_KEYRING
+	select NET_HANDSHAKE
+	help
+	  Enables TLS encryption for the NVMe TCP target using the netlink handshake API.
+
+	  The TLS handshake daemon is availble at
+	  https://github.com/oracle/ktls-utils.
+
+	  If unsure, say N.
+
 config NVME_TARGET_AUTH
 	bool "NVMe over Fabrics In-band Authentication support"
 	depends on NVME_TARGET
drivers/nvme/target/configfs.c +60 -3

123 lines not on this page · raw message

drivers/nvme/target/nvmet.h +1 -0
index dc60a22646f7..a22f817f5935 100644
@@ -158,6 +158,7 @@ struct nvmet_port {
 	struct config_group		ana_groups_group;
 	struct nvmet_ana_group		ana_default_group;
 	enum nvme_ana_state		*ana_state;
+	struct key			*keyring;
 	void				*priv;
 	bool				enabled;
 	int				inline_data_size;
drivers/nvme/target/tcp.c +131 -3

191 lines not on this page · raw message

Signature · 1 line
2.35.3
#46 lore raw

PATCH v4 17/17 nvmet-tcp: control messages for recvmsg()

In reply to Hannes Reinecke

kTLS requires control messages for recvmsg() to relay any out-of-band TLS messages (eg TLS alerts) to the caller.

---
 drivers/nvme/target/tcp.c | 50 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)
drivers/nvme/target/tcp.c +48 -2
index 26c911ddc2de..2974abd30b45 100644
@@ -118,6 +118,7 @@ struct nvmet_tcp_cmd {
 	u32				pdu_len;
 	u32				pdu_recv;
 	int				sg_idx;
+	char				recv_cbuf[CMSG_LEN(sizeof(char))];
 	struct msghdr			recv_msg;
 	struct bio_vec			*iov;
 	u32				flags;
@@ -1104,12 +1105,39 @@ static inline bool nvmet_tcp_pdu_valid(u8 type)
 	return false;
 }
 
+static bool nvmet_tcp_tls_record_ok(struct msghdr *msg, char *cbuf)
+{
+	struct cmsghdr *cmsg = (struct cmsghdr *)cbuf;
+	unsigned char ctype;
+
+	if (!IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS))
+		return true;
+
+	if (CMSG_OK(msg, cmsg) &&
+	    cmsg->cmsg_level == SOL_TLS &&
+	    cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
+		ctype = *((unsigned char *)CMSG_DATA(cmsg));
+		if (ctype != TLS_RECORD_TYPE_DATA) {
+			pr_err("unhandled TLS record %d\n", ctype);
+			return false;
+		}
+	}
+	return true;
+}
+
 static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
 {
 	struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;
 	int len;
 	struct kvec iov;
-	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
+	char cbuf[CMSG_LEN(sizeof(char))] = {};
+	struct msghdr msg = {
+#ifdef CONFIG_NVME_TARGET_TCP_TLS
+		.msg_control = cbuf,
+		.msg_controllen = sizeof(cbuf),
+#endif
+		.msg_flags = MSG_DONTWAIT
+	};
 
 recv:
 	iov.iov_base = (void *)&queue->pdu + queue->offset;
@@ -1118,6 +1146,8 @@ static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
 			iov.iov_len, msg.msg_flags);
 	if (unlikely(len < 0))
 		return len;
+	if (!nvmet_tcp_tls_record_ok(&msg, cbuf))
+		return -ENOTCONN;
 
 	queue->offset += len;
 	queue->left -= len;
@@ -1177,6 +1207,9 @@ static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
 			cmd->recv_msg.msg_flags);
 		if (ret <= 0)
 			return ret;
+		if (!nvmet_tcp_tls_record_ok(&cmd->recv_msg,
+					     cmd->recv_cbuf))
+			return -ENOTCONN;
 
 		cmd->pdu_recv += ret;
 		cmd->rbytes_done += ret;
@@ -1198,7 +1231,14 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
 {
 	struct nvmet_tcp_cmd *cmd = queue->cmd;
 	int ret;
-	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
+	char cbuf[CMSG_LEN(sizeof(char))] = {};
+	struct msghdr msg = {
+#ifdef CONFIG_NVME_TARGET_TCP_TLS
+		.msg_control = cbuf,
+		.msg_controllen = sizeof(cbuf),
+#endif
+		.msg_flags = MSG_DONTWAIT
+	};
 	struct kvec iov = {
 		.iov_base = (void *)&cmd->recv_ddgst + queue->offset,
 		.iov_len = queue->left
@@ -1208,6 +1248,8 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
 			iov.iov_len, msg.msg_flags);
 	if (unlikely(ret < 0))
 		return ret;
+	if (!nvmet_tcp_tls_record_ok(&msg, cbuf))
+		return -ENOTCONN;
 
 	queue->offset += ret;
 	queue->left -= ret;
@@ -1377,6 +1419,10 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,
 	if (!c->r2t_pdu)
 		goto out_free_data;
 
+	if (IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS)) {
+		c->recv_msg.msg_control = c->recv_cbuf;
+		c->recv_msg.msg_controllen = sizeof(c->recv_cbuf);
+	}
 	c->recv_msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
 
 	list_add_tail(&c->entry, &queue->free_list);
Signature · 1 line
2.35.3
maintainer
#29 lore raw

Re: [PATCH 08/17] nvme-tcp: fixup MSG_SENDPAGE_NOTLAST

In reply to Hannes Reinecke

Please CC Jakub and Vadim in the next iterations.
I'd also CC netdev to let the networking folks this is going on...

Quoted 6 lines

We can only set MSG_SENDPAGE_NOTLAST when sending the command PDU if the actual data is transferred via sendpage(), too.
If we use sendmsg() to transfer the data we end up with a TX stall on TLS as it implements different code paths for sendmsg() and sendpage(). So check in nvme_tcp_try_send_pdu() if inline data is present and revert to sendmsg if the inline data cannot be sent via sendpage().

ifaiu, the TX stalls are not related to sendpage and sendmsg being separate code paths. The stalls are just a bug in an untested scenario of sendpage(NOTLAST)+sendmsg..

It needs to be documented in the change log:
"Due to a bug in the tls code, we do xxx to workaround it"

In a separate patch.

Quoted 13 lines
---
  drivers/nvme/host/tcp.c | 18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)
drivers/nvme/host/tcp.c +0 -0
index 273c1f2760a4..9ecd7db2cd98 100644
@@ -995,9 +995,11 @@ static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
  		if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
  			flags |= MSG_EOR;

drivers/nvme/host/tcp.c static int nvme_tcp_try_send_data(struct nvme_tcp_request *req) · line 1005

Quoted 10 lines
drivers/nvme/host/tcp.c +3 -1
  		else
-			flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
+			flags |= MSG_MORE;
 
  		if (sendpage_ok(page)) {
+			if (flags & MSG_MORE)
+				flags |= MSG_SENDPAGE_NOTLAST;
  			ret = kernel_sendpage(queue->sock, page, offset, len,
  					flags);
  		} else {

This is fine, lets move this to its own patch.

Quoted 5 lines
drivers/nvme/host/tcp.c +0 -1
@@ -1049,15 +1051,23 @@ static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
  	int ret;
 
  	if (inline_data || nvme_tcp_queue_more(queue))
-		flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;

drivers/nvme/host/tcp.c static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req) · line 1061

Quoted 10 lines
drivers/nvme/host/tcp.c +2 -2
+		flags |= MSG_MORE;
  	else
  		flags |= MSG_EOR;
 
  	if (queue->hdr_digest && !req->offset)
  		nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
 
-	ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
-			offset_in_page(pdu) + req->offset, len,  flags);
+	if (!inline_data || sendpage_ok(nvme_tcp_req_cur_page(req))) {

This needs a big fat comment to why we are doing this bizzare and untrivial condition:

Something like:
--

drivers/nvme/host/tcp.c +20 -4
index 7723a4989524..cd8610836de9 100644
@@ -1046,15 +1046,32 @@ static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
        int ret;
 
        if (inline_data || nvme_tcp_queue_more(queue))
-               flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
+               flags |= MSG_MORE;
        else
                flags |= MSG_EOR;
 
        if (queue->hdr_digest && !req->offset)
                nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
 
-       ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
-                       offset_in_page(pdu) + req->offset, len,  flags);
+       /**
+        * FIXME: Due to a bug in the tls code, issuing sendpage with msg
+        * flag MSG_SENDPAGE_NOTLAST and then issuing sendmsg, we can hit
+        * some TX stalls. Our connect data (which is the request inline_data)
+        * is a slab allocated page and hence we must not use sendpage to
+        * send it, and this hits exactly the tls issue. workaround it by
+        * sending sendmsg on the command itself as if we are running with
+        * a tls socket and the inline data cannot be sendpage'd.
+        * When the tls code is resolve, this workaround needs to be removed.
+        */
+       if (nvme_tcp_tls_enabled(queue) && inline_data &&
+           sendpage_ok(nvme_tcp_req_cur_page(req))) {
+               ret = sock_no_sendpage(queue->sock, virt_to_page(pdu),
+                               offset_in_page(pdu) + req->offset, len, flags);
+       } else {
+               flags |= MSG_SENDPAGE_NOTLAST;
+               ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
+                               offset_in_page(pdu) + req->offset, len, flags);
+       }
        if (unlikely(ret <= 0))
                return ret;
--
Quoted 12 lines
drivers/nvme/host/tcp.c +9 -0
+		if (flags & MSG_MORE)
+			flags |= MSG_SENDPAGE_NOTLAST;
+		ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
+				offset_in_page(pdu) + req->offset, len,  flags);
+	} else {
+		ret = sock_no_sendpage(queue->sock, virt_to_page(pdu),
+				offset_in_page(pdu) + req->offset,
+				len, flags);
+	}
  	if (unlikely(ret <= 0))
  		return ret;
 
maintainer
#14 lore raw

Re: [PATCH 06/17] net/tls: implement ->read_sock()

In reply to Hannes Reinecke

We probably need a wider exposure for this one because someone else needs to approve it.

Quoted 122 lines · Hannes Reinecke

Implement ->read_sock() function for use with nvme-tcp.

Cc Boris Pismenny <[email protected]>
Cc Jakub Kicinski <[email protected]>
---
  net/tls/tls.h      |  2 ++
  net/tls/tls_main.c |  2 ++
  net/tls/tls_sw.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 75 insertions(+)
net/tls/tls.h +2 -0
index 804c3880d028..a5bf3a9ce142 100644
@@ -113,6 +113,8 @@ bool tls_sw_sock_is_readable(struct sock *sk);
  ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
  			   struct pipe_inode_info *pipe,
  			   size_t len, unsigned int flags);
+int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
+		     sk_read_actor_t read_actor);
 
  int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
  int tls_device_sendpage(struct sock *sk, struct page *page,
net/tls/tls_main.c +2 -0
index b32c112984dd..0b51b19aa46d 100644
@@ -921,9 +921,11 @@ static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG]
 
  	ops[TLS_BASE][TLS_SW  ] = ops[TLS_BASE][TLS_BASE];
  	ops[TLS_BASE][TLS_SW  ].splice_read	= tls_sw_splice_read;
+	ops[TLS_BASE][TLS_SW  ].read_sock	= tls_sw_read_sock;
 
  	ops[TLS_SW  ][TLS_SW  ] = ops[TLS_SW  ][TLS_BASE];
  	ops[TLS_SW  ][TLS_SW  ].splice_read	= tls_sw_splice_read;
+	ops[TLS_SW  ][TLS_SW  ].read_sock	= tls_sw_read_sock;
 
  #ifdef CONFIG_TLS_DEVICE
  	ops[TLS_HW  ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
net/tls/tls_sw.c +71 -0
index 635b8bf6b937..827292e29f99 100644
@@ -2214,6 +2214,77 @@ ssize_t tls_sw_splice_read(struct socket *sock,  loff_t *ppos,
  	goto splice_read_end;
  }
 
+int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
+		     sk_read_actor_t read_actor)
+{
+	struct tls_context *tls_ctx = tls_get_ctx(sk);
+	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
+	struct strp_msg *rxm = NULL;
+	struct tls_msg *tlm;
+	struct sk_buff *skb;
+	ssize_t copied = 0;
+	int err, used;
+
+	if (!skb_queue_empty(&ctx->rx_list)) {
+		skb = __skb_dequeue(&ctx->rx_list);
+	} else {
+		struct tls_decrypt_arg darg;
+
+		err = tls_rx_rec_wait(sk, NULL, true, true);
+		if (err <= 0)
+			return err;
+
+		memset(&darg.inargs, 0, sizeof(darg.inargs));
+
+		err = tls_rx_one_record(sk, NULL, &darg);
+		if (err < 0) {
+			tls_err_abort(sk, -EBADMSG);
+			return err;
+		}
+
+		tls_rx_rec_done(ctx);
+		skb = darg.skb;
+	}
+
+	do {
+		rxm = strp_msg(skb);
+		tlm = tls_msg(skb);
+
+		/* read_sock does not support reading control messages */
+		if (tlm->control != TLS_RECORD_TYPE_DATA) {
+			err = -EINVAL;
+			goto read_sock_requeue;
+		}
+
+		used = read_actor(desc, skb, rxm->offset, rxm->full_len);
+		if (used <= 0) {
+			err = used;
+			goto read_sock_end;
+		}
+
+		copied += used;
+		if (used < rxm->full_len) {
+			rxm->offset += used;
+			rxm->full_len -= used;
+			if (!desc->count)
+				goto read_sock_requeue;
+		} else {
+			consume_skb(skb);
+			if (desc->count && !skb_queue_empty(&ctx->rx_list))
+				skb = __skb_dequeue(&ctx->rx_list);
+			else
+				skb = NULL;
+		}
+	} while (skb);
+
+read_sock_end:
+	return copied ? : err;
+
+read_sock_requeue:
+	__skb_queue_head(&ctx->rx_list, skb);
+	goto read_sock_end;
+}
+
  bool tls_sw_sock_is_readable(struct sock *sk)
  {
  	struct tls_context *tls_ctx = tls_get_ctx(sk);
#2 lore raw

Re: [PATCH 01/17] nvme-keyring: register '.nvme' keyring

In reply to Hannes Reinecke

Hi Hannes,

Just a heads up for anyone looking at this:

Quoted 2 lines · Hannes Reinecke

Register a '.nvme' keyring to hold keys for TLS and DH-HMAC-CHAP and add a new config option NVME_KEYRING.

I'm not sure what the config deps should be but this series fails to build if CONFIG_NVME_AUTH is not set:

ERROR: modpost: missing MODULE_LICENSE() in drivers/nvme/common/nvme-common.o

Cheers,

#4 lore raw

Re: [PATCH 02/17] nvme-keyring: define a 'psk' keytype

In reply to Hannes Reinecke

Hi Hannes,

Quoted 116 lines · Hannes Reinecke

Define a 'psk' keytype to hold the NVMe TLS PSKs.

---
  drivers/nvme/common/keyring.c | 94 +++++++++++++++++++++++++++++++++++
  1 file changed, 94 insertions(+)
drivers/nvme/common/keyring.c +88 -0
index 5cf64b278119..494dd365052e 100644
@@ -8,6 +8,8 @@
  #include <linux/key-type.h>
  #include <keys/user-type.h>
  #include <linux/nvme.h>
+#include <linux/nvme-tcp.h>
+#include <linux/nvme-keyring.h>
 
  static struct key *nvme_keyring;
 
@@ -17,8 +19,94 @@ key_serial_t nvme_keyring_id(void)
  }
  EXPORT_SYMBOL_GPL(nvme_keyring_id);
 
+static void nvme_tls_psk_describe(const struct key *key, struct seq_file *m)
+{
+	seq_puts(m, key->description);
+	seq_printf(m, ": %u", key->datalen);
+}
+
+static bool nvme_tls_psk_match(const struct key *key,
+			       const struct key_match_data *match_data)
+{
+	const char *match_id;
+	size_t match_len;
+
+	if (!key->description) {
+		pr_debug("%s: no key description\n", __func__);
+		return false;
+	}
+	match_len = strlen(key->description);
+	pr_debug("%s: id %s len %zd\n", __func__, key->description, match_len);
+
+	if (!match_data->raw_data) {
+		pr_debug("%s: no match data\n", __func__);
+		return false;
+	}
+	match_id = match_data->raw_data;
+	pr_debug("%s: match '%s' '%s' len %zd\n",
+		 __func__, match_id, key->description, match_len);
+	return !memcmp(key->description, match_id, match_len);
+}
+
+static int nvme_tls_psk_match_preparse(struct key_match_data *match_data)
+{
+	match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
+	match_data->cmp = nvme_tls_psk_match;
+	return 0;
+}
+
+static struct key_type nvme_tls_psk_key_type = {
+	.name           = "psk",
+	.flags          = KEY_TYPE_NET_DOMAIN,
+	.preparse       = user_preparse,
+	.free_preparse  = user_free_preparse,
+	.match_preparse = nvme_tls_psk_match_preparse,
+	.instantiate    = generic_key_instantiate,
+	.revoke         = user_revoke,
+	.destroy        = user_destroy,
+	.describe       = nvme_tls_psk_describe,
+	.read           = user_read,
+};
+
+static struct key *nvme_tls_psk_lookup(struct key *keyring,
+		const char *hostnqn, const char *subnqn,
+		int hmac, bool generated)
+{
+	char *identity;
+	size_t identity_len = (NVMF_NQN_SIZE) * 2 + 11;
+	key_ref_t keyref;
+	key_serial_t keyring_id;
+
+	identity = kzalloc(identity_len, GFP_KERNEL);
+	if (!identity)
+		return ERR_PTR(-ENOMEM);
+
+	snprintf(identity, identity_len, "NVMe0%c%02d %s %s",
+		 generated ? 'G' : 'R', hmac, hostnqn, subnqn);
+
+	if (!keyring)
+		keyring = nvme_keyring;
+	keyring_id = key_serial(keyring);
+	pr_debug("keyring %x lookup tls psk '%s'\n",
+		 keyring_id, identity);
+	keyref = keyring_search(make_key_ref(keyring, true),
+				&nvme_tls_psk_key_type,
+				identity, false);
+	if (IS_ERR(keyref)) {
+		pr_debug("lookup tls psk '%s' failed, error %ld\n",
+			 identity, PTR_ERR(keyref));
+		kfree(identity);
+		return ERR_PTR(-ENOKEY);
+	}
+	kfree(identity);
+
+	return key_ref_to_ptr(keyref);
+}
+
  int nvme_keyring_init(void)
  {
+	int err;
+
  	nvme_keyring = keyring_alloc(".nvme",
  				     GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
  				     current_cred(),

drivers/nvme/common/keyring.c int nvme_keyring_init(void) · line 121

Quoted 6 lines
drivers/nvme/common/keyring.c +3 -0
@@ -28,12 +116,18 @@ int nvme_keyring_init(void)
  	if (IS_ERR(nvme_keyring))
  		return PTR_ERR(nvme_keyring);
 
+	err = register_key_type(&nvme_tls_psk_key_type);
+	if (err) {
+		key_put(nvme_keyring);

I see in the nvme_keyring_exit you call "key_revoke(nvme_keyring);" Should we call it here as well ?

Quoted 12 lines
drivers/nvme/common/keyring.c +3 -0
+		return err;
+	}
  	return 0;
  }
  EXPORT_SYMBOL_GPL(nvme_keyring_init);
 
  void nvme_keyring_exit(void)
  {
+	unregister_key_type(&nvme_tls_psk_key_type);
  	key_revoke(nvme_keyring);
  	key_put(nvme_keyring);
  }
#5 lore raw

Re: [PATCH 02/17] nvme-keyring: define a 'psk' keytype

Quoted 119 lines · Max Gurtovoy

Hi Hannes,

Quoted 116 lines · Hannes Reinecke

Define a 'psk' keytype to hold the NVMe TLS PSKs.

---
  drivers/nvme/common/keyring.c | 94 +++++++++++++++++++++++++++++++++++

  1 file changed, 94 insertions(+)
drivers/nvme/common/keyring.c +88 -0
index 5cf64b278119..494dd365052e 100644
@@ -8,6 +8,8 @@
   #include <linux/key-type.h>
   #include <keys/user-type.h>
   #include <linux/nvme.h>
+#include <linux/nvme-tcp.h>
+#include <linux/nvme-keyring.h>
   static struct key *nvme_keyring;
@@ -17,8 +19,94 @@ key_serial_t nvme_keyring_id(void)
   }
   EXPORT_SYMBOL_GPL(nvme_keyring_id);
+static void nvme_tls_psk_describe(const struct key *key, struct 
 seq_file *m)
+{
+    seq_puts(m, key->description);
+    seq_printf(m, ": %u", key->datalen);
+}
+
+static bool nvme_tls_psk_match(const struct key *key,
+                   const struct key_match_data *match_data)
+{
+    const char *match_id;
+    size_t match_len;
+
+    if (!key->description) {
+        pr_debug("%s: no key description\n", __func__);
+        return false;
+    }
+    match_len = strlen(key->description);
+    pr_debug("%s: id %s len %zd\n", __func__, key->description, 
 match_len);
+
+    if (!match_data->raw_data) {
+        pr_debug("%s: no match data\n", __func__);
+        return false;
+    }
+    match_id = match_data->raw_data;
+    pr_debug("%s: match '%s' '%s' len %zd\n",
+         __func__, match_id, key->description, match_len);
+    return !memcmp(key->description, match_id, match_len);
+}
+
+static int nvme_tls_psk_match_preparse(struct key_match_data 
 *match_data)
+{
+    match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
+    match_data->cmp = nvme_tls_psk_match;
+    return 0;
+}
+
+static struct key_type nvme_tls_psk_key_type = {
+    .name           = "psk",
+    .flags          = KEY_TYPE_NET_DOMAIN,
+    .preparse       = user_preparse,
+    .free_preparse  = user_free_preparse,
+    .match_preparse = nvme_tls_psk_match_preparse,
+    .instantiate    = generic_key_instantiate,
+    .revoke         = user_revoke,
+    .destroy        = user_destroy,
+    .describe       = nvme_tls_psk_describe,
+    .read           = user_read,
+};
+
+static struct key *nvme_tls_psk_lookup(struct key *keyring,
+        const char *hostnqn, const char *subnqn,
+        int hmac, bool generated)
+{
+    char *identity;
+    size_t identity_len = (NVMF_NQN_SIZE) * 2 + 11;
+    key_ref_t keyref;
+    key_serial_t keyring_id;
+
+    identity = kzalloc(identity_len, GFP_KERNEL);
+    if (!identity)
+        return ERR_PTR(-ENOMEM);
+
+    snprintf(identity, identity_len, "NVMe0%c%02d %s %s",
+         generated ? 'G' : 'R', hmac, hostnqn, subnqn);
+
+    if (!keyring)
+        keyring = nvme_keyring;
+    keyring_id = key_serial(keyring);
+    pr_debug("keyring %x lookup tls psk '%s'\n",
+         keyring_id, identity);
+    keyref = keyring_search(make_key_ref(keyring, true),
+                &nvme_tls_psk_key_type,
+                identity, false);
+    if (IS_ERR(keyref)) {
+        pr_debug("lookup tls psk '%s' failed, error %ld\n",
+             identity, PTR_ERR(keyref));
+        kfree(identity);
+        return ERR_PTR(-ENOKEY);
+    }
+    kfree(identity);
+
+    return key_ref_to_ptr(keyref);
+}
+
   int nvme_keyring_init(void)
   {
+    int err;
+
       nvme_keyring = keyring_alloc(".nvme",

                       GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),

drivers/nvme/common/keyring.c +0 -0
@@ -28,12 +116,18 @@ int nvme_keyring_init(void)
Quoted 9 lines

drivers/nvme/common/keyring.c int nvme_keyring_init(void) · line 121

Quoted 5 lines
drivers/nvme/common/keyring.c +3 -0
       if (IS_ERR(nvme_keyring))
           return PTR_ERR(nvme_keyring);
+    err = register_key_type(&nvme_tls_psk_key_type);
+    if (err) {
+        key_put(nvme_keyring);

I see in the nvme_keyring_exit you call "key_revoke(nvme_keyring);" Should we call it here as well ?

Mysteries of the keyring subsystem.
But yeah, I guess we should.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#7 lore raw

Re: [PATCH 03/17] nvme: add TCP TSAS definitions

In reply to Hannes Reinecke

Hi Hannes,

Quoted 3 lines · Hannes Reinecke

---

can we find one sentence for the commit message please ? Maybe describe about the handshake and sectype ?

Quoted 31 lines
  include/linux/nvme.h | 10 ++++++++++
  1 file changed, 10 insertions(+)
include/linux/nvme.h +10 -0
index 4fad4aa245fb..6e55a7a701e6 100644
@@ -108,6 +108,13 @@ enum {
  	NVMF_RDMA_CMS_RDMA_CM	= 1, /* Sockets based endpoint addressing */
  };
 
+/* TSAS SECTYPE for TCP transport */
+enum {
+	NVMF_TCP_SECTYPE_NONE = 0, /* No Security */
+	NVMF_TCP_SECTYPE_TLS12 = 1, /* TLSv1.2, NVMe-oF 1.1 and NVMe-TCP 3.6.1.1 */
+	NVMF_TCP_SECTYPE_TLS13 = 2, /* TLSv1.3, NVMe-oF 1.1 and NVMe-TCP 3.6.1.1 */
+};
+
  #define NVME_AQ_DEPTH		32
  #define NVME_NR_AEN_COMMANDS	1
  #define NVME_AQ_BLK_MQ_DEPTH	(NVME_AQ_DEPTH - NVME_NR_AEN_COMMANDS)
@@ -1453,6 +1460,9 @@ struct nvmf_disc_rsp_page_entry {
  			__u16	pkey;
  			__u8	resv10[246];
  		} rdma;
+		struct tcp {
+			__u8	sectype;
+		} tcp;
  	} tsas;
  };
 
#8 lore raw

Re: [PATCH 03/17] nvme: add TCP TSAS definitions

Quoted 10 lines · Max Gurtovoy

Hi Hannes,

Quoted 3 lines · Hannes Reinecke

---

can we find one sentence for the commit message please ? Maybe describe about the handshake and sectype ?

Sure.

'Add TCP TSAS definition from NVMe TCP Transport specification 1.0' ?

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#11 lore raw

Re: [PATCH 05/17] nvme-keyring: implement nvme_tls_psk_default()

In reply to Hannes Reinecke

Hannes,
I wonder if we can squash the keyring patches 1/17 + 2/17 + 5/17 to a single patch ? And start the series with some preparations in the nvme/tcp for it to compile.. This will reduce the amount of commits and simplify the review.

Quoted 99 lines · Hannes Reinecke

Implement a function to select the preferred PSK for TLS.

---
  drivers/nvme/common/keyring.c | 48 +++++++++++++++++++++++++++++++++++
  include/linux/nvme-keyring.h  |  8 ++++++
  2 files changed, 56 insertions(+)
drivers/nvme/common/keyring.c +48 -0
index 494dd365052e..f8d9a208397b 100644
@@ -5,6 +5,7 @@
 
  #include <linux/module.h>
  #include <linux/seq_file.h>
+#include <linux/key.h>
  #include <linux/key-type.h>
  #include <keys/user-type.h>
  #include <linux/nvme.h>
@@ -103,6 +104,53 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
  	return key_ref_to_ptr(keyref);
  }
 
+/*
+ * NVMe PSK priority list
+ *
+ * 'Retained' PSKs (ie 'generated == false')
+ * should be preferred to 'generated' PSKs,
+ * and SHA-384 should be preferred to SHA-256.
+ */
+struct nvme_tls_psk_priority_list {
+	bool generated;
+	enum nvme_tcp_tls_cipher cipher;
+} nvme_tls_psk_prio[] = {
+	{ .generated = false,
+	  .cipher = NVME_TCP_TLS_CIPHER_SHA384, },
+	{ .generated = false,
+	  .cipher = NVME_TCP_TLS_CIPHER_SHA256, },
+	{ .generated = true,
+	  .cipher = NVME_TCP_TLS_CIPHER_SHA384, },
+	{ .generated = true,
+	  .cipher = NVME_TCP_TLS_CIPHER_SHA256, },
+};
+
+/*
+ * nvme_tls_psk_default - Return the preferred PSK to use for TLS ClientHello
+ */
+key_serial_t nvme_tls_psk_default(struct key *keyring,
+		      const char *hostnqn, const char *subnqn)
+{
+	struct key *tls_key;
+	key_serial_t tls_key_id;
+	int prio;
+
+	for (prio = 0; prio < ARRAY_SIZE(nvme_tls_psk_prio); prio++) {
+		bool generated = nvme_tls_psk_prio[prio].generated;
+		enum nvme_tcp_tls_cipher cipher = nvme_tls_psk_prio[prio].cipher;
+
+		tls_key = nvme_tls_psk_lookup(keyring, hostnqn, subnqn,
+					      cipher, generated);
+		if (!IS_ERR(tls_key)) {
+			tls_key_id = tls_key->serial;
+			key_put(tls_key);
+			return tls_key_id;
+		}
+	}
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nvme_tls_psk_default);
+
  int nvme_keyring_init(void)
  {
  	int err;
include/linux/nvme-keyring.h +8 -0
index 32bd264a71e6..4efea9dd967c 100644
@@ -8,12 +8,20 @@
 
  #ifdef CONFIG_NVME_KEYRING
 
+key_serial_t nvme_tls_psk_default(struct key *keyring,
+		const char *hostnqn, const char *subnqn);
+
  key_serial_t nvme_keyring_id(void);
  int nvme_keyring_init(void);
  void nvme_keyring_exit(void);
 
  #else
 
+static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
+		const char *hostnqn, const char *subnqn)
+{
+	return 0;
+}
  static inline key_serial_t nvme_keyring_id(void)
  {
  	return 0;
#23 lore raw

Re: [PATCH 07/17] net/tls: handle MSG_EOR for tls_sw TX flow

In reply to Hannes Reinecke
Quoted 5 lines · Hannes Reinecke

tls_sw_sendmsg() / tls_do_sw_sendpage() already checks for an 'end of record' by evaluating the 'MSG_MORE' / 'MSG_SENDPAGE_NOTLAST' setting. So MSG_EOR can easily be handled by treating it as the opposite of MSG_MORE / MSG_SENDPAGE_NOTLAST.

This seems like a nice optimization but seems not mandatory for the acceptance of TLS support in nvme/tcp.

I wonder if this can go to net/tls as a standalone patch ?

Quoted 51 lines
---
  net/tls/tls_sw.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
net/tls/tls_sw.c +8 -3
index 827292e29f99..9bee2dcd55bf 100644
@@ -953,9 +953,12 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  	int pending;
 
  	if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
-			       MSG_CMSG_COMPAT))
+			       MSG_EOR | MSG_CMSG_COMPAT))
  		return -EOPNOTSUPP;
 
+	if (msg->msg_flags & MSG_EOR)
+		eor = true;
+
  	ret = mutex_lock_interruptible(&tls_ctx->tx_lock);
  	if (ret)
  		return ret;
@@ -1173,6 +1176,8 @@ static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
  	bool eor;
 
  	eor = !(flags & MSG_SENDPAGE_NOTLAST);
+	if (flags & MSG_EOR)
+		eor = true;
  	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
 
  	/* Call the sk_stream functions to manage the sndbuf mem. */
@@ -1274,7 +1279,7 @@ static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
  int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
  			   int offset, size_t size, int flags)
  {
-	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
+	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_EOR |
  		      MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY |
  		      MSG_NO_SHARED_FRAGS))
  		return -EOPNOTSUPP;
@@ -1288,7 +1293,7 @@ int tls_sw_sendpage(struct sock *sk, struct page *page,
  	struct tls_context *tls_ctx = tls_get_ctx(sk);
  	int ret;
 
-	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
+	if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_EOR |
  		      MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
  		return -EOPNOTSUPP;
 
#33 lore raw

Re: [PATCH 11/17] nvme-tcp: enable TLS handshake upcall

In reply to Hannes Reinecke
Quoted 3 lines · Hannes Reinecke

Add a fabrics option 'tls' and start the TLS handshake upcall with the default PSK. When TLS is started the PSK key serial number is displayed in the sysfs attribute 'tls_key'

Can you please explain the flow for the TLS handshake in the cover letter ?

Quoted 96 lines
---
  drivers/nvme/host/Kconfig   |  14 ++++
  drivers/nvme/host/core.c    |  23 ++++++-
  drivers/nvme/host/fabrics.c |  12 ++++
  drivers/nvme/host/fabrics.h |   3 +
  drivers/nvme/host/nvme.h    |   1 +
  drivers/nvme/host/tcp.c     | 129 ++++++++++++++++++++++++++++++++++--
  6 files changed, 174 insertions(+), 8 deletions(-)
drivers/nvme/host/Kconfig +14 -0
index 2f6a7f8c94e8..96a74041bb0a 100644
@@ -92,6 +92,20 @@ config NVME_TCP
 
  	  If unsure, say N.
 
+config NVME_TCP_TLS
+	bool "NVMe over Fabrics TCP TLS encryption support"
+	depends on NVME_TCP
+	select NVME_COMMON
+	select NVME_KEYRING
+	select NET_HANDSHAKE
+	help
+	  Enables TLS encryption for NVMe TCP using the netlink handshake API.
+
+	  The TLS handshake daemon is availble at
+	  https://github.com/oracle/ktls-utils.
+
+	  If unsure, say N.
+
  config NVME_AUTH
  	bool "NVM Express over Fabrics In-Band Authentication"
  	depends on NVME_CORE
drivers/nvme/host/core.c +21 -2
index 79aa215aec76..937013127c91 100644
@@ -3889,6 +3889,19 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR,
  	nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
  #endif
 
+#ifdef CONFIG_NVME_TCP_TLS
+static ssize_t tls_key_show(struct device *dev,
+			    struct device_attribute *attr, char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+	if (!ctrl->tls_key)
+		return 0;
+	return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
+}
+static DEVICE_ATTR_RO(tls_key);
+#endif
+
  static struct attribute *nvme_dev_attrs[] = {
  	&dev_attr_reset_controller.attr,
  	&dev_attr_rescan_controller.attr,
@@ -3915,6 +3928,9 @@ static struct attribute *nvme_dev_attrs[] = {
  #ifdef CONFIG_NVME_AUTH
  	&dev_attr_dhchap_secret.attr,
  	&dev_attr_dhchap_ctrl_secret.attr,
+#endif
+#ifdef CONFIG_NVME_TCP_TLS
+	&dev_attr_tls_key.attr,
  #endif
  	NULL
  };
@@ -3945,7 +3961,10 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
  	if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
  		return 0;
  #endif
-
+#ifdef CONFIG_NVME_TCP_TLS
+	if (a == &dev_attr_tls_key.attr && !ctrl->opts)
+		return 0;
+#endif
  	return a->mode;
  }
 
@@ -5080,7 +5099,7 @@ static void nvme_free_ctrl(struct device *dev)
 
  	if (!subsys || ctrl->instance != subsys->instance)
  		ida_free(&nvme_instance_ida, ctrl->instance);
-
+	key_put(ctrl->tls_key);
  	nvme_free_cels(ctrl);
  	nvme_mpath_uninit(ctrl);
  	nvme_auth_stop(ctrl);
drivers/nvme/host/fabrics.c +0 -0
index bbaa04a0c502..2aa8fb991455 100644

drivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 612

Quoted 4 lines
drivers/nvme/host/fabrics.c +1 -0
@@ -609,6 +609,9 @@ static const match_table_t opt_tokens = {
  	{ NVMF_OPT_DISCOVERY,		"discovery"		},
  	{ NVMF_OPT_DHCHAP_SECRET,	"dhchap_secret=%s"	},
  	{ NVMF_OPT_DHCHAP_CTRL_SECRET,	"dhchap_ctrl_secret=%s"	},
+#ifdef CONFIG_NVME_TCP_TLS

This ifdef is redundant IMO.
We have to many of those already in the code and we should reduce to be able to keep the code maintainable.

Quoted 89 lines
drivers/nvme/host/fabrics.c +11 -0
+	{ NVMF_OPT_TLS,			"tls"			},
+#endif
  	{ NVMF_OPT_ERR,			NULL			}
  };
 
@@ -632,6 +635,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  	opts->hdr_digest = false;
  	opts->data_digest = false;
  	opts->tos = -1; /* < 0 == use transport default */
+	opts->tls = false;
 
  	options = o = kstrdup(buf, GFP_KERNEL);
  	if (!options)
@@ -918,6 +922,14 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  			kfree(opts->dhchap_ctrl_secret);
  			opts->dhchap_ctrl_secret = p;
  			break;
+		case NVMF_OPT_TLS:
+			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+				pr_err("TLS is not supported\n");
+				ret = -EINVAL;
+				goto out;
+			}
+			opts->tls = true;
+			break;
  		default:
  			pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
  				p);
drivers/nvme/host/fabrics.h +3 -0
index dcac3df8a5f7..5db36e250e7a 100644
@@ -70,6 +70,7 @@ enum {
  	NVMF_OPT_DISCOVERY	= 1 << 22,
  	NVMF_OPT_DHCHAP_SECRET	= 1 << 23,
  	NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
+	NVMF_OPT_TLS		= 1 << 25,
  };
 
  /**
@@ -102,6 +103,7 @@ enum {
   * @dhchap_secret: DH-HMAC-CHAP secret
   * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
   *              authentication
+ * @tls:        Start TLS encrypted connections (TCP)
   * @disable_sqflow: disable controller sq flow control
   * @hdr_digest: generate/verify header digest (TCP)
   * @data_digest: generate/verify data digest (TCP)
@@ -128,6 +130,7 @@ struct nvmf_ctrl_options {
  	int			max_reconnects;
  	char			*dhchap_secret;
  	char			*dhchap_ctrl_secret;
+	bool			tls;
  	bool			disable_sqflow;
  	bool			hdr_digest;
  	bool			data_digest;
drivers/nvme/host/nvme.h +1 -0
index bf46f122e9e1..fbcc0ccf2ef2 100644
@@ -347,6 +347,7 @@ struct nvme_ctrl {
  	struct nvme_dhchap_key *ctrl_key;
  	u16 transaction;
  #endif
+	struct key *tls_key;
 
  	/* Power saving configuration */
  	u64 ps_max_latency_us;
drivers/nvme/host/tcp.c +4 -0
index 9deba481ae6a..d25695cf4e03 100644
@@ -8,9 +8,13 @@
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/err.h>
+#include <linux/key.h>
  #include <linux/nvme-tcp.h>
+#include <linux/nvme-keyring.h>
  #include <net/sock.h>
  #include <net/tcp.h>
+#include <net/tls.h>
+#include <net/handshake.h>
  #include <linux/blk-mq.h>
  #include <crypto/hash.h>
  #include <net/busy_poll.h>
@@ -31,6 +35,16 @@ static int so_priority;
  module_param(so_priority, int, 0644);
  MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");

drivers/nvme/host/tcp.c static int so_priority; · line 46

Quoted 10 lines
drivers/nvme/host/tcp.c +9 -0
 
+#ifdef CONFIG_NVME_TCP_TLS
+/*
+ * TLS handshake timeout
+ */
+static int tls_handshake_timeout = 10;
+module_param(tls_handshake_timeout, int, 0644);
+MODULE_PARM_DESC(tls_handshake_timeout,
+		 "nvme TLS handshake timeout in seconds (default 10)");
+#endif

Module param under ifdef also sounds redundant.

Quoted 99 lines
drivers/nvme/host/tcp.c +78 -2
+
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  /* lockdep can detect a circular dependency of the form
   *   sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
@@ -146,7 +160,10 @@ struct nvme_tcp_queue {
  	struct ahash_request	*snd_hash;
  	__le32			exp_ddgst;
  	__le32			recv_ddgst;
-
+#ifdef CONFIG_NVME_TCP_TLS
+	struct completion       tls_complete;
+	int                     tls_err;
+#endif
  	struct page_frag_cache	pf_cache;
 
  	void (*state_change)(struct sock *);
@@ -1502,7 +1519,79 @@ static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
  	queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
  }
 
-static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
+#ifdef CONFIG_NVME_TCP_TLS
+static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid)
+{
+	struct nvme_tcp_queue *queue = data;
+	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
+	int qid = nvme_tcp_queue_id(queue);
+	struct key *tls_key;
+
+	dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, status %d\n",
+		qid, pskid, status);
+
+	if (status) {
+		queue->tls_err = -status;
+		goto out_complete;
+	}
+
+	tls_key = key_lookup(pskid);
+	if (IS_ERR(tls_key)) {
+		dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n",
+			 qid, pskid);
+		queue->tls_err = -ENOKEY;
+	} else {
+		ctrl->ctrl.tls_key = tls_key;
+		queue->tls_err = 0;
+	}
+
+out_complete:
+	complete(&queue->tls_complete);
+}
+
+static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
+			      struct nvme_tcp_queue *queue,
+			      key_serial_t pskid)
+{
+	int qid = nvme_tcp_queue_id(queue);
+	int ret;
+	struct tls_handshake_args args;
+	unsigned long tmo = tls_handshake_timeout * HZ;
+	key_serial_t keyring = nvme_keyring_id();
+
+	dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
+		qid, pskid);
+	args.ta_sock = queue->sock;
+	args.ta_done = nvme_tcp_tls_done;
+	args.ta_data = queue;
+	args.ta_my_peerids[0] = pskid;
+	args.ta_num_peerids = 1;
+	args.ta_keyring = keyring;
+	args.ta_timeout_ms = tls_handshake_timeout * 1000;
+	queue->tls_err = -EOPNOTSUPP;
+	init_completion(&queue->tls_complete);
+	ret = tls_client_hello_psk(&args, GFP_KERNEL);
+	if (ret) {
+		dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n",
+			qid, ret);
+		return ret;
+	}
+	if (wait_for_completion_timeout(&queue->tls_complete, tmo) == 0) {
+		dev_err(nctrl->device,
+			"queue %d: TLS handshake timeout\n", qid);
+		ret = -ETIMEDOUT;
+	} else {
+		dev_dbg(nctrl->device,
+			"queue %d: TLS handshake complete, error %d\n",
+			qid, queue->tls_err);
+		ret = queue->tls_err;
+	}
+	return ret;
+}
+#endif
+
+static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
+				key_serial_t pskid)
  {
  	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
@@ -1626,6 +1715,14 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
  		goto err_rcv_pdu;

drivers/nvme/host/tcp.c static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid) · line 1725

Quoted 10 lines
drivers/nvme/host/tcp.c +8 -0
  	}
 
+#ifdef CONFIG_NVME_TCP_TLS
+	/* If PSKs are configured try to start TLS */
+	if (pskid) {
+		ret = nvme_tcp_start_tls(nctrl, queue, pskid);
+		if (ret)
+			goto err_init_connect;
+	}
+#endif

Maybe reduce ifdef here and use IS_ENABLED ? (might be done in nvme_tcp_start_tls too)

Quoted 56 lines
drivers/nvme/host/tcp.c +24 -4
  	ret = nvme_tcp_init_connection(queue);
  	if (ret)
  		goto err_init_connect;
@@ -1769,10 +1866,22 @@ static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl,
  static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
  {
  	int ret;
+	key_serial_t psk_id = 0;
+
+	if (ctrl->opts->tls) {
+		psk_id = nvme_tls_psk_default(NULL,
+					      ctrl->opts->host->nqn,
+					      ctrl->opts->subsysnqn);
+		if (!psk_id) {
+			dev_err(ctrl->device, "no valid PSK found\n");
+			ret = -ENOKEY;
+			goto out_free_queue;
+		}
+	}
 
-	ret = nvme_tcp_alloc_queue(ctrl, 0);
+	ret = nvme_tcp_alloc_queue(ctrl, 0, psk_id);
  	if (ret)
-		return ret;
+		goto out_free_queue;
 
  	ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
  	if (ret)
@@ -1788,9 +1897,17 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
  static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
  {
  	int i, ret;
+	key_serial_t psk_id = 0;
 
+	if (ctrl->opts->tls) {
+		if (!ctrl->tls_key) {
+			dev_err(ctrl->device, "no PSK negotiated\n");
+			return -ENOKEY;
+		}
+		psk_id = key_serial(ctrl->tls_key);
+	}
  	for (i = 1; i < ctrl->queue_count; i++) {
-		ret = nvme_tcp_alloc_queue(ctrl, i);
+		ret = nvme_tcp_alloc_queue(ctrl, i, psk_id);
  		if (ret)
  			goto out_free_queues;
  	}
@@ -2699,7 +2816,7 @@ static struct nvmf_transport_ops nvme_tcp_transport = {
  			  NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
  			  NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
  			  NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
-			  NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE,
+			  NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS,
  	.create_ctrl	= nvme_tcp_create_ctrl,
  };
 
#41 lore raw

Re: [PATCH 13/17] nvme-fabrics: parse options 'keyring' and 'tls_key'

In reply to Hannes Reinecke
Quoted 2 lines · Hannes Reinecke

Parse the fabrics options 'keyring' and 'tls_key' and store the referenced keys in the options structure.

Please add an example of how the nvme-cli will issue a connect command with these new args.

Quoted 13 lines
---
  drivers/nvme/host/fabrics.c | 69 ++++++++++++++++++++++++++++++++++++-
  drivers/nvme/host/fabrics.h |  6 ++++
  drivers/nvme/host/tcp.c     | 11 ++++--
  3 files changed, 82 insertions(+), 4 deletions(-)
drivers/nvme/host/fabrics.c +0 -0
index 2aa8fb991455..8fb80ccc38e1 100644

drivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 611

Quoted 7 lines
drivers/nvme/host/fabrics.c +4 -0
@@ -605,6 +605,10 @@ static const match_table_t opt_tokens = {
  	{ NVMF_OPT_NR_WRITE_QUEUES,	"nr_write_queues=%d"	},
  	{ NVMF_OPT_NR_POLL_QUEUES,	"nr_poll_queues=%d"	},
  	{ NVMF_OPT_TOS,			"tos=%d"		},
+#ifdef CONFIG_NVME_TCP_TLS
+	{ NVMF_OPT_KEYRING,		"keyring=%d"		},
+	{ NVMF_OPT_TLS_KEY,		"tls_key=%d"		},
+#endif

redundant ifdef here.

Quoted 104 lines
drivers/nvme/host/fabrics.c +64 -1
  	{ NVMF_OPT_FAIL_FAST_TMO,	"fast_io_fail_tmo=%d"	},
  	{ NVMF_OPT_DISCOVERY,		"discovery"		},
  	{ NVMF_OPT_DHCHAP_SECRET,	"dhchap_secret=%s"	},
@@ -622,8 +626,9 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  	char *options, *o, *p;
  	int token, ret = 0;
  	size_t nqnlen  = 0;
-	int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
+	int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO, key_id;
  	uuid_t hostid;
+	struct key *key = NULL;
 
  	/* Set defaults */
  	opts->queue_size = NVMF_DEF_QUEUE_SIZE;
@@ -891,6 +896,66 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  			}
  			opts->tos = token;
  			break;
+		case NVMF_OPT_KEYRING:
+			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+				pr_err("TLS is not supported\n");
+				ret = -EINVAL;
+				goto out;
+			}
+			if (match_int(args, &key_id)) {
+				ret = -EINVAL;
+				goto out;
+			}
+			if (key_id < 0) {
+				pr_err("Invalid keyring id %d\n", key_id);
+				ret = -EINVAL;
+				goto out;
+			}
+			if (!key_id) {
+				pr_debug("Using default keyring\n");
+				key_put(opts->keyring);
+				opts->keyring = NULL;
+				break;
+			}
+			key = key_lookup(key_id);
+			if (!key) {
+				pr_err("Keyring id %08x not found\n", key_id);
+				ret = -ENOKEY;
+				goto out;
+			}
+			key_put(opts->keyring);
+			opts->keyring = key;
+			break;
+		case NVMF_OPT_TLS_KEY:
+			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+				pr_err("TLS is not supported\n");
+				ret = -EINVAL;
+				goto out;
+			}
+			if (match_int(args, &key_id)) {
+				ret = -EINVAL;
+				goto out;
+			}
+			if (key_id < 0) {
+				pr_err("Invalid key id %d\n", key_id);
+				ret = -EINVAL;
+				goto out;
+			}
+			if (!key_id) {
+				pr_debug("Using 'best' PSK\n");
+				key_put(opts->tls_key);
+				opts->tls_key = NULL;
+				break;
+			}
+			key = key_lookup(key_id);
+			if (!key) {
+				pr_err("Key id %08x not found\n", key_id);
+				ret = -ENOKEY;
+				goto out;
+			}
+			key_put(opts->tls_key);
+			opts->tls_key = key;
+			break;
  		case NVMF_OPT_DISCOVERY:
  			opts->discovery_nqn = true;
  			break;
@@ -1055,6 +1120,8 @@ static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
  void nvmf_free_options(struct nvmf_ctrl_options *opts)
  {
  	nvmf_host_put(opts->host);
+	key_put(opts->keyring);
+	key_put(opts->tls_key);
  	kfree(opts->transport);
  	kfree(opts->traddr);
  	kfree(opts->trsvcid);
drivers/nvme/host/fabrics.h +2 -0
index 5db36e250e7a..2ff7b7168a40 100644
@@ -71,6 +71,8 @@ enum {
  	NVMF_OPT_DHCHAP_SECRET	= 1 << 23,
  	NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
  	NVMF_OPT_TLS		= 1 << 25,
+	NVMF_OPT_KEYRING	= 1 << 26,
+	NVMF_OPT_TLS_KEY	= 1 << 27,
  };
 
  /**

drivers/nvme/host/fabrics.h enum { · line 108

Quoted 4 lines
drivers/nvme/host/fabrics.h +1 -0
@@ -103,6 +105,8 @@ enum {
   * @dhchap_secret: DH-HMAC-CHAP secret
   * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
   *              authentication
+ * @keyring:    Keyring to use for key lookups

is it only for TLS ? if it does please reflect in the comment and in the name of the member.

Quoted 29 lines
drivers/nvme/host/fabrics.h +3 -0
+ * @tls_key:    TLS key for encrypted connections (TCP)
   * @tls:        Start TLS encrypted connections (TCP)
   * @disable_sqflow: disable controller sq flow control
   * @hdr_digest: generate/verify header digest (TCP)
@@ -130,6 +134,8 @@ struct nvmf_ctrl_options {
  	int			max_reconnects;
  	char			*dhchap_secret;
  	char			*dhchap_ctrl_secret;
+	struct key		*keyring;
+	struct key		*tls_key;
  	bool			tls;
  	bool			disable_sqflow;
  	bool			hdr_digest;
drivers/nvme/host/tcp.c +2 -0
index a3ac512fd1cc..84bf6c80cc7c 100644
@@ -1582,6 +1582,8 @@ static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
 
  	dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
  		qid, pskid);
+	if (nctrl->opts->keyring)
+		keyring = key_serial(nctrl->opts->keyring);
  	args.ta_sock = queue->sock;
  	args.ta_done = nvme_tcp_tls_done;
  	args.ta_data = queue;
@@ -1890,9 +1892,12 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
  	key_serial_t psk_id = 0;
 

drivers/nvme/host/tcp.c static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) · line 1900

Quoted 10 lines
drivers/nvme/host/tcp.c +6 -3
  	if (ctrl->opts->tls) {
-		psk_id = nvme_tls_psk_default(NULL,
-					      ctrl->opts->host->nqn,
-					      ctrl->opts->subsysnqn);
+		if (ctrl->opts->tls_key)
+			psk_id = key_serial(ctrl->opts->tls_key);
+		else
+			psk_id = nvme_tls_psk_default(ctrl->opts->keyring,
+						      ctrl->opts->host->nqn,
+						      ctrl->opts->subsysnqn);

I don't understand why this patch can't be squashed to patch 11/17 ? If possible please do before next version submission.

Quoted 3 lines
drivers/nvme/host/tcp.c +0 -0
  		if (!psk_id) {
  			dev_err(ctrl->device, "no valid PSK found\n");
  			ret = -ENOKEY;
#12 lore raw

Re: [PATCH 05/17] nvme-keyring: implement nvme_tls_psk_default()

In reply to Max Gurtovoy
Quoted 5 lines · Max Gurtovoy

Hannes,
I wonder if we can squash the keyring patches 1/17 + 2/17 + 5/17 to a single patch ? And start the series with some preparations in the nvme/tcp for it to compile.. This will reduce the amount of commits and simplify the review.

Sure, will do so for the next round.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#24 lore raw

Re: [PATCH 07/17] net/tls: handle MSG_EOR for tls_sw TX flow

In reply to Max Gurtovoy
Quoted 9 lines · Max Gurtovoy
Quoted 5 lines · Hannes Reinecke

tls_sw_sendmsg() / tls_do_sw_sendpage() already checks for an 'end of record' by evaluating the 'MSG_MORE' / 'MSG_SENDPAGE_NOTLAST' setting. So MSG_EOR can easily be handled by treating it as the opposite of MSG_MORE / MSG_SENDPAGE_NOTLAST.

Quoted 4 lines

This seems like a nice optimization but seems not mandatory for the acceptance of TLS support in nvme/tcp.

I wonder if this can go to net/tls as a standalone patch ?

Errm. Without this NVMe/TLS will not work as sendmsg/sendpage will bail out.
So yes, surely it can be applied as a standalone patch, but that only makes sense if it will be applied _before_ the rest of the nvme/tls patches.

Not sure how best to coordinate this.

Quoted 50 lines
Quoted 50 lines

---
  net/tls/tls_sw.c | 11 ++++++++---

  1 file changed, 8 insertions(+), 3 deletions(-)
net/tls/tls_sw.c +0 -0
index 827292e29f99..9bee2dcd55bf 100644
@@ -953,9 +953,12 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr 
 *msg, size_t size)
       int pending;

      if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | -                   MSG_CMSG_COMPAT))
+                   MSG_EOR | MSG_CMSG_COMPAT))
          return -EOPNOTSUPP;
+    if (msg->msg_flags & MSG_EOR)
+        eor = true;
+
      ret = mutex_lock_interruptible(&tls_ctx->tx_lock); if (ret)
          return ret;

net/tls/tls_sw.c +3 -1
@@ -1173,6 +1176,8 @@ static int tls_sw_do_sendpage(struct sock *sk, 
 struct page *page,
       bool eor;
       eor = !(flags & MSG_SENDPAGE_NOTLAST);
+    if (flags & MSG_EOR)
+        eor = true;
       sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
       /* Call the sk_stream functions to manage the sndbuf mem. */
@@ -1274,7 +1279,7 @@ static int tls_sw_do_sendpage(struct sock *sk, 
 struct page *page,
   int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
                  int offset, size_t size, int flags)
   {
-    if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
+    if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_EOR |

                MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY | MSG_NO_SHARED_FRAGS))
          return -EOPNOTSUPP;

net/tls/tls_sw.c +1 -1
@@ -1288,7 +1293,7 @@ int tls_sw_sendpage(struct sock *sk, struct page 
 *page,
       struct tls_context *tls_ctx = tls_get_ctx(sk);
       int ret;
-    if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
+    if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_EOR |
                 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
           return -EOPNOTSUPP;
Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#34 lore raw

Re: [PATCH 11/17] nvme-tcp: enable TLS handshake upcall

In reply to Max Gurtovoy
Quoted 8 lines · Max Gurtovoy
Quoted 3 lines · Hannes Reinecke

Add a fabrics option 'tls' and start the TLS handshake upcall with the default PSK. When TLS is started the PSK key serial number is displayed in the sysfs attribute 'tls_key'

Can you please explain the flow for the TLS handshake in the cover letter ?

Sure. Will do so in the next round.

Quoted 93 lines
Quoted 93 lines

---
  drivers/nvme/host/Kconfig   |  14 ++++ drivers/nvme/host/core.c    |  23 ++++++- drivers/nvme/host/fabrics.c |  12 ++++ drivers/nvme/host/fabrics.h |   3 + drivers/nvme/host/nvme.h    |   1 + drivers/nvme/host/tcp.c     | 129 ++++++++++++++++++++++++++++++++++--

  6 files changed, 174 insertions(+), 8 deletions(-)
drivers/nvme/host/Kconfig +0 -0
index 2f6a7f8c94e8..96a74041bb0a 100644
@@ -92,6 +92,20 @@ config NVME_TCP

        If unsure, say N.
+config NVME_TCP_TLS
+    bool "NVMe over Fabrics TCP TLS encryption support"
+    depends on NVME_TCP
+    select NVME_COMMON
+    select NVME_KEYRING
+    select NET_HANDSHAKE
+    help
+      Enables TLS encryption for NVMe TCP using the netlink handshake API.

drivers/nvme/host/Kconfig +6 -0
+
+      The TLS handshake daemon is availble at
+      https://github.com/oracle/ktls-utils.
+
+      If unsure, say N.
+

  config NVME_AUTH
      bool "NVM Express over Fabrics In-Band Authentication" depends on NVME_CORE

drivers/nvme/host/core.c +16 -0
index 79aa215aec76..937013127c91 100644
@@ -3889,6 +3889,19 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO 
 | S_IWUSR,
       nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
   #endif
+#ifdef CONFIG_NVME_TCP_TLS
+static ssize_t tls_key_show(struct device *dev,
+                struct device_attribute *attr, char *buf)
+{
+    struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+    if (!ctrl->tls_key)
+        return 0;
+    return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
+}
+static DEVICE_ATTR_RO(tls_key);
+#endif
+
   static struct attribute *nvme_dev_attrs[] = {
       &dev_attr_reset_controller.attr,
       &dev_attr_rescan_controller.attr,
@@ -3915,6 +3928,9 @@ static struct attribute *nvme_dev_attrs[] = {
   #ifdef CONFIG_NVME_AUTH
       &dev_attr_dhchap_secret.attr,
       &dev_attr_dhchap_ctrl_secret.attr,
+#endif
+#ifdef CONFIG_NVME_TCP_TLS
+    &dev_attr_tls_key.attr,
   #endif

      NULL
  };

drivers/nvme/host/core.c +5 -2
@@ -3945,7 +3961,10 @@ static umode_t 
 nvme_dev_attrs_are_visible(struct kobject *kobj,
       if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
           return 0;
   #endif
-
+#ifdef CONFIG_NVME_TCP_TLS
+    if (a == &dev_attr_tls_key.attr && !ctrl->opts)
+        return 0;
+#endif
       return a->mode;
   }
@@ -5080,7 +5099,7 @@ static void nvme_free_ctrl(struct device *dev)
       if (!subsys || ctrl->instance != subsys->instance)
           ida_free(&nvme_instance_ida, ctrl->instance);
-
+    key_put(ctrl->tls_key);
       nvme_free_cels(ctrl);
       nvme_mpath_uninit(ctrl);
       nvme_auth_stop(ctrl);
drivers/nvme/host/fabrics.c +0 -0
index bbaa04a0c502..2aa8fb991455 100644
Quoted 9 lines

drivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 612

Quoted 5 lines
drivers/nvme/host/fabrics.c +1 -0
@@ -609,6 +609,9 @@ static const match_table_t opt_tokens = {
       { NVMF_OPT_DISCOVERY,        "discovery"        },
       { NVMF_OPT_DHCHAP_SECRET,    "dhchap_secret=%s"    },
       { NVMF_OPT_DHCHAP_CTRL_SECRET,    "dhchap_ctrl_secret=%s"    },
+#ifdef CONFIG_NVME_TCP_TLS

This ifdef is redundant IMO.
We have to many of those already in the code and we should reduce to be able to keep the code maintainable.

We have been going back and forth on this, and the consensus was to _have_ a compilation flag here. This allows the userland utility nvme-cli to determine whether tls support is available on any given kernel.

Quoted 202 lines
Quoted 86 lines
drivers/nvme/host/fabrics.c +2 -0
+    { NVMF_OPT_TLS,            "tls"            },
+#endif
       { NVMF_OPT_ERR,            NULL            }
   };
@@ -632,6 +635,7 @@ static int nvmf_parse_options(struct 
 nvmf_ctrl_options *opts,
       opts->hdr_digest = false;
       opts->data_digest = false;

      opts->tos = -1; /* < 0 == use transport default */ +    opts->tls = false;
      options = o = kstrdup(buf, GFP_KERNEL); if (!options)

drivers/nvme/host/fabrics.c +8 -0
@@ -918,6 +922,14 @@ static int nvmf_parse_options(struct 
 nvmf_ctrl_options *opts,
               kfree(opts->dhchap_ctrl_secret);
               opts->dhchap_ctrl_secret = p;
               break;
+        case NVMF_OPT_TLS:
+            if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+                pr_err("TLS is not supported\n");
+                ret = -EINVAL;
+                goto out;
+            }
+            opts->tls = true;
+            break;

          default:
              pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n", p);

drivers/nvme/host/fabrics.h +3 -0
index dcac3df8a5f7..5db36e250e7a 100644
@@ -70,6 +70,7 @@ enum {
       NVMF_OPT_DISCOVERY    = 1 << 22,
       NVMF_OPT_DHCHAP_SECRET    = 1 << 23,
       NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
+    NVMF_OPT_TLS        = 1 << 25,
   };
   /**
@@ -102,6 +103,7 @@ enum {
    * @dhchap_secret: DH-HMAC-CHAP secret
    * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
    *              authentication
+ * @tls:        Start TLS encrypted connections (TCP)
    * @disable_sqflow: disable controller sq flow control
    * @hdr_digest: generate/verify header digest (TCP)
    * @data_digest: generate/verify data digest (TCP)
@@ -128,6 +130,7 @@ struct nvmf_ctrl_options {
       int            max_reconnects;
       char            *dhchap_secret;
       char            *dhchap_ctrl_secret;
+    bool            tls;
       bool            disable_sqflow;
       bool            hdr_digest;
       bool            data_digest;
drivers/nvme/host/nvme.h +1 -0
index bf46f122e9e1..fbcc0ccf2ef2 100644
@@ -347,6 +347,7 @@ struct nvme_ctrl {
       struct nvme_dhchap_key *ctrl_key;
       u16 transaction;
   #endif
+    struct key *tls_key;
       /* Power saving configuration */
       u64 ps_max_latency_us;
drivers/nvme/host/tcp.c +4 -0
index 9deba481ae6a..d25695cf4e03 100644
@@ -8,9 +8,13 @@
   #include <linux/init.h>
   #include <linux/slab.h>
   #include <linux/err.h>
+#include <linux/key.h>
   #include <linux/nvme-tcp.h>
+#include <linux/nvme-keyring.h>
   #include <net/sock.h>
   #include <net/tcp.h>
+#include <net/tls.h>
+#include <net/handshake.h>
   #include <linux/blk-mq.h>
   #include <crypto/hash.h>
   #include <net/busy_poll.h>
@@ -31,6 +35,16 @@ static int so_priority;
   module_param(so_priority, int, 0644);

drivers/nvme/host/tcp.c static int so_priority; · line 45

Quoted 10 lines
drivers/nvme/host/tcp.c +9 -0
   MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
+#ifdef CONFIG_NVME_TCP_TLS
+/*
+ * TLS handshake timeout
+ */
+static int tls_handshake_timeout = 10;
+module_param(tls_handshake_timeout, int, 0644);
+MODULE_PARM_DESC(tls_handshake_timeout,
+         "nvme TLS handshake timeout in seconds (default 10)");
+#endif

Module param under ifdef also sounds redundant.

Quoted 104 lines
drivers/nvme/host/tcp.c +78 -2
+
   #ifdef CONFIG_DEBUG_LOCK_ALLOC
   /* lockdep can detect a circular dependency of the form
    *   sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
@@ -146,7 +160,10 @@ struct nvme_tcp_queue {
       struct ahash_request    *snd_hash;
       __le32            exp_ddgst;
       __le32            recv_ddgst;
-
+#ifdef CONFIG_NVME_TCP_TLS
+    struct completion       tls_complete;
+    int                     tls_err;
+#endif
       struct page_frag_cache    pf_cache;
       void (*state_change)(struct sock *);
@@ -1502,7 +1519,79 @@ static void nvme_tcp_set_queue_io_cpu(struct 
 nvme_tcp_queue *queue)
       queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
   }
-static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
+#ifdef CONFIG_NVME_TCP_TLS
+static void nvme_tcp_tls_done(void *data, int status, key_serial_t 
 pskid)
+{
+    struct nvme_tcp_queue *queue = data;
+    struct nvme_tcp_ctrl *ctrl = queue->ctrl;
+    int qid = nvme_tcp_queue_id(queue);
+    struct key *tls_key;
+
+    dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, 
 status %d\n",
+        qid, pskid, status);
+
+    if (status) {
+        queue->tls_err = -status;
+        goto out_complete;
+    }
+
+    tls_key = key_lookup(pskid);
+    if (IS_ERR(tls_key)) {
+        dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n",
+             qid, pskid);
+        queue->tls_err = -ENOKEY;
+    } else {
+        ctrl->ctrl.tls_key = tls_key;
+        queue->tls_err = 0;
+    }
+
+out_complete:
+    complete(&queue->tls_complete);
+}
+
+static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
+                  struct nvme_tcp_queue *queue,
+                  key_serial_t pskid)
+{
+    int qid = nvme_tcp_queue_id(queue);
+    int ret;
+    struct tls_handshake_args args;
+    unsigned long tmo = tls_handshake_timeout * HZ;
+    key_serial_t keyring = nvme_keyring_id();
+
+    dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
+        qid, pskid);
+    args.ta_sock = queue->sock;
+    args.ta_done = nvme_tcp_tls_done;
+    args.ta_data = queue;
+    args.ta_my_peerids[0] = pskid;
+    args.ta_num_peerids = 1;
+    args.ta_keyring = keyring;
+    args.ta_timeout_ms = tls_handshake_timeout * 1000;
+    queue->tls_err = -EOPNOTSUPP;
+    init_completion(&queue->tls_complete);
+    ret = tls_client_hello_psk(&args, GFP_KERNEL);
+    if (ret) {
+        dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n",
+            qid, ret);
+        return ret;
+    }
+    if (wait_for_completion_timeout(&queue->tls_complete, tmo) == 0) {
+        dev_err(nctrl->device,
+            "queue %d: TLS handshake timeout\n", qid);
+        ret = -ETIMEDOUT;
+    } else {
+        dev_dbg(nctrl->device,
+            "queue %d: TLS handshake complete, error %d\n",
+            qid, queue->tls_err);
+        ret = queue->tls_err;
+    }
+    return ret;
+}
+#endif
+
+static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
+                key_serial_t pskid)
   {

      struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); struct nvme_tcp_queue *queue = &ctrl->queues[qid];

drivers/nvme/host/tcp.c +2 -0
@@ -1626,6 +1715,14 @@ static int nvme_tcp_alloc_queue(struct 
 nvme_ctrl *nctrl, int qid)
           goto err_rcv_pdu;
       }
+#ifdef CONFIG_NVME_TCP_TLS
+    /* If PSKs are configured try to start TLS */
Quoted 9 lines

drivers/nvme/host/tcp.c static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid) · line 1725

Quoted 6 lines
drivers/nvme/host/tcp.c +6 -0
+    if (pskid) {
+        ret = nvme_tcp_start_tls(nctrl, queue, pskid);
+        if (ret)
+            goto err_init_connect;
+    }
+#endif

Maybe reduce ifdef here and use IS_ENABLED ? (might be done in nvme_tcp_start_tls too)

Then I would need to have a dummy definition for 'nvme_tcp_start_tls'; but yeah, could do.

Quoted 47 lines
Quoted 47 lines
drivers/nvme/host/tcp.c +23 -3
       ret = nvme_tcp_init_connection(queue);
       if (ret)
           goto err_init_connect;
@@ -1769,10 +1866,22 @@ static int nvme_tcp_start_io_queues(struct 
 nvme_ctrl *ctrl,
   static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
   {
       int ret;
+    key_serial_t psk_id = 0;
+
+    if (ctrl->opts->tls) {
+        psk_id = nvme_tls_psk_default(NULL,
+                          ctrl->opts->host->nqn,
+                          ctrl->opts->subsysnqn);
+        if (!psk_id) {
+            dev_err(ctrl->device, "no valid PSK found\n");
+            ret = -ENOKEY;
+            goto out_free_queue;
+        }
+    }
-    ret = nvme_tcp_alloc_queue(ctrl, 0);
+    ret = nvme_tcp_alloc_queue(ctrl, 0, psk_id);
       if (ret)
-        return ret;
+        goto out_free_queue;
       ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
       if (ret)
@@ -1788,9 +1897,17 @@ static int nvme_tcp_alloc_admin_queue(struct 
 nvme_ctrl *ctrl)
   static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
   {
       int i, ret;
+    key_serial_t psk_id = 0;
+    if (ctrl->opts->tls) {
+        if (!ctrl->tls_key) {
+            dev_err(ctrl->device, "no PSK negotiated\n");
+            return -ENOKEY;
+        }
+        psk_id = key_serial(ctrl->tls_key);
+    }
       for (i = 1; i < ctrl->queue_count; i++) {
-        ret = nvme_tcp_alloc_queue(ctrl, i);
+        ret = nvme_tcp_alloc_queue(ctrl, i, psk_id);
           if (ret)
               goto out_free_queues;
       }
@@ -2699,7 +2816,7 @@ static struct nvmf_transport_ops 
Quoted 8 lines
Quoted 8 lines
drivers/nvme/host/tcp.c +0 -0
 nvme_tcp_transport = {

                NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST | NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | -              NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE,
+              NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS, .create_ctrl    = nvme_tcp_create_ctrl,
  };

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#42 lore raw

Re: [PATCH 13/17] nvme-fabrics: parse options 'keyring' and 'tls_key'

In reply to Max Gurtovoy
Quoted 8 lines · Max Gurtovoy
Quoted 2 lines · Hannes Reinecke

Parse the fabrics options 'keyring' and 'tls_key' and store the referenced keys in the options structure.

Please add an example of how the nvme-cli will issue a connect command with these new args.

Okay.

Quoted 8 lines
Quoted 8 lines

---
  drivers/nvme/host/fabrics.c | 69 ++++++++++++++++++++++++++++++++++++- drivers/nvme/host/fabrics.h |  6 ++++ drivers/nvme/host/tcp.c     | 11 ++++--

  3 files changed, 82 insertions(+), 4 deletions(-)
Quoted 15 lines

drivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 611

Quoted 12 lines
drivers/nvme/host/fabrics.c +4 -0
index 2aa8fb991455..8fb80ccc38e1 100644
@@ -605,6 +605,10 @@ static const match_table_t opt_tokens = {
       { NVMF_OPT_NR_WRITE_QUEUES,    "nr_write_queues=%d"    },
       { NVMF_OPT_NR_POLL_QUEUES,    "nr_poll_queues=%d"    },
       { NVMF_OPT_TOS,            "tos=%d"        },
+#ifdef CONFIG_NVME_TCP_TLS
+    { NVMF_OPT_KEYRING,        "keyring=%d"        },
+    { NVMF_OPT_TLS_KEY,        "tls_key=%d"        },
+#endif

redundant ifdef here.

See explanation to the other patch. This ifdef is required to allow nvme-cli to check for support of these option.

Quoted 103 lines
Quoted 103 lines
drivers/nvme/host/fabrics.c +62 -1
       { NVMF_OPT_FAIL_FAST_TMO,    "fast_io_fail_tmo=%d"    },
       { NVMF_OPT_DISCOVERY,        "discovery"        },
       { NVMF_OPT_DHCHAP_SECRET,    "dhchap_secret=%s"    },
@@ -622,8 +626,9 @@ static int nvmf_parse_options(struct 
 nvmf_ctrl_options *opts,
       char *options, *o, *p;
       int token, ret = 0;
       size_t nqnlen  = 0;
-    int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
+    int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO, key_id;
       uuid_t hostid;
+    struct key *key = NULL;
       /* Set defaults */
       opts->queue_size = NVMF_DEF_QUEUE_SIZE;
@@ -891,6 +896,66 @@ static int nvmf_parse_options(struct 
 nvmf_ctrl_options *opts,
               }
               opts->tos = token;
               break;
+        case NVMF_OPT_KEYRING:
+            if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+                pr_err("TLS is not supported\n");
+                ret = -EINVAL;
+                goto out;
+            }
+            if (match_int(args, &key_id)) {
+                ret = -EINVAL;
+                goto out;
+            }
+            if (key_id < 0) {
+                pr_err("Invalid keyring id %d\n", key_id);
+                ret = -EINVAL;
+                goto out;
+            }
+            if (!key_id) {
+                pr_debug("Using default keyring\n");
+                key_put(opts->keyring);
+                opts->keyring = NULL;
+                break;
+            }
+            key = key_lookup(key_id);
+            if (!key) {
+                pr_err("Keyring id %08x not found\n", key_id);
+                ret = -ENOKEY;
+                goto out;
+            }
+            key_put(opts->keyring);
+            opts->keyring = key;
+            break;
+        case NVMF_OPT_TLS_KEY:
+            if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+                pr_err("TLS is not supported\n");
+                ret = -EINVAL;
+                goto out;
+            }
+            if (match_int(args, &key_id)) {
+                ret = -EINVAL;
+                goto out;
+            }
+            if (key_id < 0) {
+                pr_err("Invalid key id %d\n", key_id);
+                ret = -EINVAL;
+                goto out;
+            }
+            if (!key_id) {
+                pr_debug("Using 'best' PSK\n");
+                key_put(opts->tls_key);
+                opts->tls_key = NULL;
+                break;
+            }
+            key = key_lookup(key_id);
+            if (!key) {
+                pr_err("Key id %08x not found\n", key_id);
+                ret = -ENOKEY;
+                goto out;
+            }
+            key_put(opts->tls_key);
+            opts->tls_key = key;
+            break;

          case NVMF_OPT_DISCOVERY:
              opts->discovery_nqn = true;
              break;

drivers/nvme/host/fabrics.c +2 -0
@@ -1055,6 +1120,8 @@ static int nvmf_check_allowed_opts(struct 
 nvmf_ctrl_options *opts,
   void nvmf_free_options(struct nvmf_ctrl_options *opts)
   {
       nvmf_host_put(opts->host);
+    key_put(opts->keyring);
+    key_put(opts->tls_key);
       kfree(opts->transport);
       kfree(opts->traddr);

      kfree(opts->trsvcid);

drivers/nvme/host/fabrics.h +2 -0
index 5db36e250e7a..2ff7b7168a40 100644
@@ -71,6 +71,8 @@ enum {
       NVMF_OPT_DHCHAP_SECRET    = 1 << 23,
       NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
       NVMF_OPT_TLS        = 1 << 25,
+    NVMF_OPT_KEYRING    = 1 << 26,
+    NVMF_OPT_TLS_KEY    = 1 << 27,
   };
Quoted 9 lines

drivers/nvme/host/fabrics.h enum { · line 108

Quoted 6 lines
drivers/nvme/host/fabrics.h +1 -0
   /**
@@ -103,6 +105,8 @@ enum {
    * @dhchap_secret: DH-HMAC-CHAP secret
    * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
    *              authentication
+ * @keyring:    Keyring to use for key lookups

is it only for TLS ? if it does please reflect in the comment and in the name of the member.

Yes and no. Currently it's just for TLS, but I've got patches to convert DH-HMAC-CHAP over to use this keyring, too.

Quoted 34 lines
Quoted 34 lines
drivers/nvme/host/fabrics.h +3 -0
+ * @tls_key:    TLS key for encrypted connections (TCP)
    * @tls:        Start TLS encrypted connections (TCP)
    * @disable_sqflow: disable controller sq flow control
    * @hdr_digest: generate/verify header digest (TCP)
@@ -130,6 +134,8 @@ struct nvmf_ctrl_options {
       int            max_reconnects;
       char            *dhchap_secret;
       char            *dhchap_ctrl_secret;
+    struct key        *keyring;
+    struct key        *tls_key;
       bool            tls;
       bool            disable_sqflow;
       bool            hdr_digest;
drivers/nvme/host/tcp.c +3 -3
index a3ac512fd1cc..84bf6c80cc7c 100644
@@ -1582,6 +1582,8 @@ static int nvme_tcp_start_tls(struct nvme_ctrl 
 *nctrl,
       dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
           qid, pskid);
+    if (nctrl->opts->keyring)
+        keyring = key_serial(nctrl->opts->keyring);
       args.ta_sock = queue->sock;
       args.ta_done = nvme_tcp_tls_done;
       args.ta_data = queue;
@@ -1890,9 +1892,12 @@ static int nvme_tcp_alloc_admin_queue(struct 
 nvme_ctrl *ctrl)
       key_serial_t psk_id = 0;
       if (ctrl->opts->tls) {
-        psk_id = nvme_tls_psk_default(NULL,
-                          ctrl->opts->host->nqn,
-                          ctrl->opts->subsysnqn);
+        if (ctrl->opts->tls_key)
Quoted 9 lines

drivers/nvme/host/tcp.c static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) · line 1900

Quoted 5 lines
drivers/nvme/host/tcp.c +5 -0
+            psk_id = key_serial(ctrl->opts->tls_key);
+        else
+            psk_id = nvme_tls_psk_default(ctrl->opts->keyring,
+                              ctrl->opts->host->nqn,
+                              ctrl->opts->subsysnqn);

I don't understand why this patch can't be squashed to patch 11/17 ? If possible please do before next version submission.

Oh, sure, it can.
Will do with the next round.

Quoted 3 lines

drivers/nvme/host/tcp.c static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) · line 1903

Quoted 3 lines
drivers/nvme/host/tcp.c +0 -0
           if (!psk_id) {
               dev_err(ctrl->device, "no valid PSK found\n");
               ret = -ENOKEY;

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
maintainer
#25 lore raw

Re: [PATCH 07/17] net/tls: handle MSG_EOR for tls_sw TX flow

In reply to Hannes Reinecke
Quoted 5 lines · On Tue, 9 May 2023 16:18:30 +0200 Hannes Reinecke wrote:
Quoted 4 lines

This seems like a nice optimization but seems not mandatory for the acceptance of TLS support in nvme/tcp.

I wonder if this can go to net/tls as a standalone patch ?

Quoted 7 lines

Errm. Without this NVMe/TLS will not work as sendmsg/sendpage will bail out.
So yes, surely it can be applied as a standalone patch, but that only makes sense if it will be applied _before_ the rest of the nvme/tls patches.

Not sure how best to coordinate this.

You should apply it on a branch based on -rc1 and then both us and $appropriate-nvme-maintainer can pull it in.

#26 lore raw

Re: [PATCH 07/17] net/tls: handle MSG_EOR for tls_sw TX flow

Quoted 11 lines · Jakub Kicinski
Quoted 10 lines · On Tue, 9 May 2023 16:18:30 +0200 Hannes Reinecke wrote:
Quoted 4 lines

This seems like a nice optimization but seems not mandatory for the acceptance of TLS support in nvme/tcp.

I wonder if this can go to net/tls as a standalone patch ?

Errm. Without this NVMe/TLS will not work as sendmsg/sendpage will bail out.
So yes, surely it can be applied as a standalone patch, but that only makes sense if it will be applied _before_ the rest of the nvme/tls patches.

how come it will bail out only for NVMe/TLS and not for Other_user/TLS ?

Quoted 5 lines
Quoted 2 lines

Not sure how best to coordinate this.

You should apply it on a branch based on -rc1 and then both us and $appropriate-nvme-maintainer can pull it in.

#27 lore raw

Re: [PATCH 07/17] net/tls: handle MSG_EOR for tls_sw TX flow

Quoted 9 lines · Max Gurtovoy
Quoted 6 lines · Jakub Kicinski
Quoted 5 lines · On Tue, 9 May 2023 16:18:30 +0200 Hannes Reinecke wrote:
Quoted 4 lines

This seems like a nice optimization but seems not mandatory for the acceptance of TLS support in nvme/tcp.

I wonder if this can go to net/tls as a standalone patch ?

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Errm. Without this NVMe/TLS will not work as sendmsg/sendpage will bail out.
So yes, surely it can be applied as a standalone patch, but that only makes sense if it will be applied _before_ the rest of the nvme/tls patches.

how come it will bail out only for NVMe/TLS and not for Other_user/TLS ?

Oh, it will bail for other TLS users as well.
Point is it will not bail for _non_ TLS connections, causing the issue.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#35 lore raw

Re: [PATCH 11/17] nvme-tcp: enable TLS handshake upcall

In reply to Hannes Reinecke
Quoted 114 lines · Hannes Reinecke
Quoted 8 lines · Max Gurtovoy
Quoted 3 lines · Hannes Reinecke

Add a fabrics option 'tls' and start the TLS handshake upcall with the default PSK. When TLS is started the PSK key serial number is displayed in the sysfs attribute 'tls_key'

Can you please explain the flow for the TLS handshake in the cover letter ?

Sure. Will do so in the next round.

Quoted 93 lines
Quoted 93 lines

---
  drivers/nvme/host/Kconfig   |  14 ++++ drivers/nvme/host/core.c    |  23 ++++++- drivers/nvme/host/fabrics.c |  12 ++++ drivers/nvme/host/fabrics.h |   3 + drivers/nvme/host/nvme.h    |   1 + drivers/nvme/host/tcp.c     | 129 ++++++++++++++++++++++++++++++++++--

  6 files changed, 174 insertions(+), 8 deletions(-)
drivers/nvme/host/Kconfig +0 -0
index 2f6a7f8c94e8..96a74041bb0a 100644
@@ -92,6 +92,20 @@ config NVME_TCP

        If unsure, say N.
+config NVME_TCP_TLS
+    bool "NVMe over Fabrics TCP TLS encryption support" +    depends on NVME_TCP
+    select NVME_COMMON
+    select NVME_KEYRING
+    select NET_HANDSHAKE
+    help
+      Enables TLS encryption for NVMe TCP using the netlink handshake API.

drivers/nvme/host/Kconfig +6 -0
+
+      The TLS handshake daemon is availble at
+      https://github.com/oracle/ktls-utils.
+
+      If unsure, say N.
+

  config NVME_AUTH
      bool "NVM Express over Fabrics In-Band Authentication" depends on NVME_CORE

drivers/nvme/host/core.c +16 -0
index 79aa215aec76..937013127c91 100644
@@ -3889,6 +3889,19 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO 
 | S_IWUSR,
       nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
   #endif
+#ifdef CONFIG_NVME_TCP_TLS
+static ssize_t tls_key_show(struct device *dev,
+                struct device_attribute *attr, char *buf)
+{
+    struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+    if (!ctrl->tls_key)
+        return 0;
+    return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
+}
+static DEVICE_ATTR_RO(tls_key);
+#endif
+
   static struct attribute *nvme_dev_attrs[] = {
       &dev_attr_reset_controller.attr,
       &dev_attr_rescan_controller.attr,
@@ -3915,6 +3928,9 @@ static struct attribute *nvme_dev_attrs[] = {
   #ifdef CONFIG_NVME_AUTH
       &dev_attr_dhchap_secret.attr,
       &dev_attr_dhchap_ctrl_secret.attr,
+#endif
+#ifdef CONFIG_NVME_TCP_TLS
+    &dev_attr_tls_key.attr,
   #endif

      NULL
  };

drivers/nvme/host/core.c +5 -2
@@ -3945,7 +3961,10 @@ static umode_t 
 nvme_dev_attrs_are_visible(struct kobject *kobj,
       if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
           return 0;
   #endif
-
+#ifdef CONFIG_NVME_TCP_TLS
+    if (a == &dev_attr_tls_key.attr && !ctrl->opts)
+        return 0;
+#endif
       return a->mode;
   }
@@ -5080,7 +5099,7 @@ static void nvme_free_ctrl(struct device *dev)
       if (!subsys || ctrl->instance != subsys->instance)
           ida_free(&nvme_instance_ida, ctrl->instance);
-
+    key_put(ctrl->tls_key);
       nvme_free_cels(ctrl);
       nvme_mpath_uninit(ctrl);
       nvme_auth_stop(ctrl);
drivers/nvme/host/fabrics.c +0 -0
index bbaa04a0c502..2aa8fb991455 100644
Quoted 9 lines

drivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 612

Quoted 5 lines
drivers/nvme/host/fabrics.c +1 -0
@@ -609,6 +609,9 @@ static const match_table_t opt_tokens = {
       { NVMF_OPT_DISCOVERY,        "discovery"        },
       { NVMF_OPT_DHCHAP_SECRET,    "dhchap_secret=%s"    },
       { NVMF_OPT_DHCHAP_CTRL_SECRET,    "dhchap_ctrl_secret=%s"    },
+#ifdef CONFIG_NVME_TCP_TLS

This ifdef is redundant IMO.
We have to many of those already in the code and we should reduce to be able to keep the code maintainable.

Quoted 1 line

We have been going back and forth on this, and the consensus was to _have_ a compilation flag here. This allows the userland utility nvme-cli to determine whether tls support is available on any given kernel.

nvme-cli will determine tls support using check for "static const match_table_t opt_tokens" ?

Quoted 215 lines
Quoted 203 lines
Quoted 87 lines
drivers/nvme/host/fabrics.c +2 -0
+    { NVMF_OPT_TLS,            "tls"            },
+#endif
       { NVMF_OPT_ERR,            NULL            }
   };
@@ -632,6 +635,7 @@ static int nvmf_parse_options(struct 
 nvmf_ctrl_options *opts,
       opts->hdr_digest = false;
       opts->data_digest = false;

      opts->tos = -1; /* < 0 == use transport default */ +    opts->tls = false;
      options = o = kstrdup(buf, GFP_KERNEL); if (!options)

drivers/nvme/host/fabrics.c +8 -0
@@ -918,6 +922,14 @@ static int nvmf_parse_options(struct 
 nvmf_ctrl_options *opts,
               kfree(opts->dhchap_ctrl_secret);
               opts->dhchap_ctrl_secret = p;
               break;
+        case NVMF_OPT_TLS:
+            if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+                pr_err("TLS is not supported\n");
+                ret = -EINVAL;
+                goto out;
+            }
+            opts->tls = true;
+            break;

          default:
              pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n", p);

drivers/nvme/host/fabrics.h +3 -0
index dcac3df8a5f7..5db36e250e7a 100644
@@ -70,6 +70,7 @@ enum {
       NVMF_OPT_DISCOVERY    = 1 << 22,
       NVMF_OPT_DHCHAP_SECRET    = 1 << 23,
       NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
+    NVMF_OPT_TLS        = 1 << 25,
   };
   /**
@@ -102,6 +103,7 @@ enum {
    * @dhchap_secret: DH-HMAC-CHAP secret
    * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
    *              authentication
+ * @tls:        Start TLS encrypted connections (TCP)
    * @disable_sqflow: disable controller sq flow control
    * @hdr_digest: generate/verify header digest (TCP)
    * @data_digest: generate/verify data digest (TCP)
@@ -128,6 +130,7 @@ struct nvmf_ctrl_options {
       int            max_reconnects;
       char            *dhchap_secret;
       char            *dhchap_ctrl_secret;
+    bool            tls;
       bool            disable_sqflow;
       bool            hdr_digest;
       bool            data_digest;
drivers/nvme/host/nvme.h +1 -0
index bf46f122e9e1..fbcc0ccf2ef2 100644
@@ -347,6 +347,7 @@ struct nvme_ctrl {
       struct nvme_dhchap_key *ctrl_key;
       u16 transaction;
   #endif
+    struct key *tls_key;
       /* Power saving configuration */
       u64 ps_max_latency_us;
drivers/nvme/host/tcp.c +4 -0
index 9deba481ae6a..d25695cf4e03 100644
@@ -8,9 +8,13 @@
   #include <linux/init.h>
   #include <linux/slab.h>
   #include <linux/err.h>
+#include <linux/key.h>
   #include <linux/nvme-tcp.h>
+#include <linux/nvme-keyring.h>
   #include <net/sock.h>
   #include <net/tcp.h>
+#include <net/tls.h>
+#include <net/handshake.h>
   #include <linux/blk-mq.h>
   #include <crypto/hash.h>
   #include <net/busy_poll.h>
@@ -31,6 +35,16 @@ static int so_priority;
   module_param(so_priority, int, 0644);

drivers/nvme/host/tcp.c static int so_priority; · line 45

Quoted 10 lines
drivers/nvme/host/tcp.c +9 -0
   MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
+#ifdef CONFIG_NVME_TCP_TLS
+/*
+ * TLS handshake timeout
+ */
+static int tls_handshake_timeout = 10;
+module_param(tls_handshake_timeout, int, 0644);
+MODULE_PARM_DESC(tls_handshake_timeout,
+         "nvme TLS handshake timeout in seconds (default 10)");
+#endif

Module param under ifdef also sounds redundant.

Quoted 104 lines
drivers/nvme/host/tcp.c +78 -2
+
   #ifdef CONFIG_DEBUG_LOCK_ALLOC
   /* lockdep can detect a circular dependency of the form
    *   sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
@@ -146,7 +160,10 @@ struct nvme_tcp_queue {
       struct ahash_request    *snd_hash;
       __le32            exp_ddgst;
       __le32            recv_ddgst;
-
+#ifdef CONFIG_NVME_TCP_TLS
+    struct completion       tls_complete;
+    int                     tls_err;
+#endif
       struct page_frag_cache    pf_cache;
       void (*state_change)(struct sock *);
@@ -1502,7 +1519,79 @@ static void nvme_tcp_set_queue_io_cpu(struct 
 nvme_tcp_queue *queue)
       queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
   }
-static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
+#ifdef CONFIG_NVME_TCP_TLS
+static void nvme_tcp_tls_done(void *data, int status, key_serial_t 
 pskid)
+{
+    struct nvme_tcp_queue *queue = data;
+    struct nvme_tcp_ctrl *ctrl = queue->ctrl;
+    int qid = nvme_tcp_queue_id(queue);
+    struct key *tls_key;
+
+    dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key 
 %x, status %d\n",
+        qid, pskid, status);
+
+    if (status) {
+        queue->tls_err = -status;
+        goto out_complete;
+    }
+
+    tls_key = key_lookup(pskid);
+    if (IS_ERR(tls_key)) {
+        dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n",
+             qid, pskid);
+        queue->tls_err = -ENOKEY;
+    } else {
+        ctrl->ctrl.tls_key = tls_key;
+        queue->tls_err = 0;
+    }
+
+out_complete:
+    complete(&queue->tls_complete);
+}
+
+static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
+                  struct nvme_tcp_queue *queue,
+                  key_serial_t pskid)
+{
+    int qid = nvme_tcp_queue_id(queue);
+    int ret;
+    struct tls_handshake_args args;
+    unsigned long tmo = tls_handshake_timeout * HZ;
+    key_serial_t keyring = nvme_keyring_id();
+
+    dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
+        qid, pskid);
+    args.ta_sock = queue->sock;
+    args.ta_done = nvme_tcp_tls_done;
+    args.ta_data = queue;
+    args.ta_my_peerids[0] = pskid;
+    args.ta_num_peerids = 1;
+    args.ta_keyring = keyring;
+    args.ta_timeout_ms = tls_handshake_timeout * 1000;
+    queue->tls_err = -EOPNOTSUPP;
+    init_completion(&queue->tls_complete);
+    ret = tls_client_hello_psk(&args, GFP_KERNEL);
+    if (ret) {
+        dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n",
+            qid, ret);
+        return ret;
+    }
+    if (wait_for_completion_timeout(&queue->tls_complete, tmo) == 0) {
+        dev_err(nctrl->device,
+            "queue %d: TLS handshake timeout\n", qid);
+        ret = -ETIMEDOUT;
+    } else {
+        dev_dbg(nctrl->device,
+            "queue %d: TLS handshake complete, error %d\n",
+            qid, queue->tls_err);
+        ret = queue->tls_err;
+    }
+    return ret;
+}
+#endif
+
+static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
+                key_serial_t pskid)
   {

      struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); struct nvme_tcp_queue *queue = &ctrl->queues[qid];

drivers/nvme/host/tcp.c +2 -0
@@ -1626,6 +1715,14 @@ static int nvme_tcp_alloc_queue(struct 
 nvme_ctrl *nctrl, int qid)
           goto err_rcv_pdu;
       }
+#ifdef CONFIG_NVME_TCP_TLS
+    /* If PSKs are configured try to start TLS */
Quoted 9 lines

drivers/nvme/host/tcp.c static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid) · line 1725

Quoted 6 lines
drivers/nvme/host/tcp.c +6 -0
+    if (pskid) {
+        ret = nvme_tcp_start_tls(nctrl, queue, pskid);
+        if (ret)
+            goto err_init_connect;
+    }
+#endif

Maybe reduce ifdef here and use IS_ENABLED ? (might be done in nvme_tcp_start_tls too)

Then I would need to have a dummy definition for 'nvme_tcp_start_tls'; but yeah, could do.

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
drivers/nvme/host/tcp.c +0 -0
       ret = nvme_tcp_init_connection(queue);
       if (ret)
           goto err_init_connect;
@@ -1769,10 +1866,22 @@ static int nvme_tcp_start_io_queues(struct 
 nvme_ctrl *ctrl,
   static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
Quoted 8 lines

drivers/nvme/host/tcp.c static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl, · line 1874

Quoted 8 lines
drivers/nvme/host/tcp.c +6 -0
   {
       int ret;
+    key_serial_t psk_id = 0;
+
+    if (ctrl->opts->tls) {
+        psk_id = nvme_tls_psk_default(NULL,
+                          ctrl->opts->host->nqn,
+                          ctrl->opts->subsysnqn);

After the patch squash you will not need this defaulk psk and also the opts->tls. Why not ask the user provide it explicitly always ? why do we need a default ? I'm not TLS expert, but seem redundant..

Quoted 45 lines
Quoted 33 lines
Quoted 33 lines
drivers/nvme/host/tcp.c +17 -3
+        if (!psk_id) {
+            dev_err(ctrl->device, "no valid PSK found\n");
+            ret = -ENOKEY;
+            goto out_free_queue;
+        }
+    }
-    ret = nvme_tcp_alloc_queue(ctrl, 0);
+    ret = nvme_tcp_alloc_queue(ctrl, 0, psk_id);
       if (ret)
-        return ret;
+        goto out_free_queue;
       ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
       if (ret)
@@ -1788,9 +1897,17 @@ static int nvme_tcp_alloc_admin_queue(struct 
 nvme_ctrl *ctrl)
   static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
   {
       int i, ret;
+    key_serial_t psk_id = 0;
+    if (ctrl->opts->tls) {
+        if (!ctrl->tls_key) {
+            dev_err(ctrl->device, "no PSK negotiated\n");
+            return -ENOKEY;
+        }
+        psk_id = key_serial(ctrl->tls_key);
+    }
       for (i = 1; i < ctrl->queue_count; i++) {
-        ret = nvme_tcp_alloc_queue(ctrl, i);
+        ret = nvme_tcp_alloc_queue(ctrl, i, psk_id);
           if (ret)
               goto out_free_queues;
       }
@@ -2699,7 +2816,7 @@ static struct nvmf_transport_ops 
Quoted 8 lines
Quoted 8 lines
drivers/nvme/host/tcp.c +0 -0
 nvme_tcp_transport = {

                NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST | NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | -              NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE,
+              NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS, .create_ctrl    = nvme_tcp_create_ctrl,
  };

Cheers,

Hannes

maintainer
#15 lore raw

Re: [PATCH 06/17] net/tls: implement ->read_sock()

In reply to Hannes Reinecke
Quoted 6 lines

Implement ->read_sock() function for use with nvme-tcp.

Cc Boris Pismenny <[email protected]>
Cc Jakub Kicinski <[email protected]>

Jakub,

Can we get an ack (or review) from you on this?

The only consumer of this is nvme, and seems to at least work on the limited testing this series has seen.

We can take this piece from the nvme tree or not, whatever you prefer.

maintainer
#16 lore raw

Re: [PATCH 06/17] net/tls: implement ->read_sock()

Quoted 7 lines · On Tue, 9 May 2023 16:30:03 -0700 Sagi Grimberg wrote:

Can we get an ack (or review) from you on this?

The only consumer of this is nvme, and seems to at least work on the limited testing this series has seen.

We can take this piece from the nvme tree or not, whatever you prefer.

Let's get all the TLS patches posted to netdev, reviewed there, then we can put them on a branch on top of -rc1 and they can be pulled into both netdev and nvme tree.

#36 lore raw

Re: [PATCH 11/17] nvme-tcp: enable TLS handshake upcall

In reply to Max Gurtovoy
Quoted 118 lines · Max Gurtovoy
Quoted 115 lines · Hannes Reinecke
Quoted 8 lines · Max Gurtovoy
Quoted 3 lines · Hannes Reinecke

Add a fabrics option 'tls' and start the TLS handshake upcall with the default PSK. When TLS is started the PSK key serial number is displayed in the sysfs attribute 'tls_key'

Can you please explain the flow for the TLS handshake in the cover letter ?

Sure. Will do so in the next round.

Quoted 103 lines
Quoted 95 lines

---
  drivers/nvme/host/Kconfig   |  14 ++++
  drivers/nvme/host/core.c    |  23 ++++++-
  drivers/nvme/host/fabrics.c |  12 ++++
  drivers/nvme/host/fabrics.h |   3 +
  drivers/nvme/host/nvme.h    |   1 +
  drivers/nvme/host/tcp.c     | 129
++++++++++++++++++++++++++++++++++--

  6 files changed, 174 insertions(+), 8 deletions(-)
drivers/nvme/host/Kconfig +0 -0
index 2f6a7f8c94e8..96a74041bb0a 100644
@@ -92,6 +92,20 @@ config NVME_TCP

        If unsure, say N.
+config NVME_TCP_TLS
+    bool "NVMe over Fabrics TCP TLS encryption support" +    depends on NVME_TCP
+    select NVME_COMMON
+    select NVME_KEYRING
+    select NET_HANDSHAKE
+    help
+      Enables TLS encryption for NVMe TCP using the netlink handshake API.

drivers/nvme/host/Kconfig +6 -0
+
+      The TLS handshake daemon is availble at
+      https://github.com/oracle/ktls-utils.
+
+      If unsure, say N.
+

  config NVME_AUTH
      bool "NVM Express over Fabrics In-Band Authentication" depends on NVME_CORE

drivers/nvme/host/core.c +16 -0
index 79aa215aec76..937013127c91 100644
@@ -3889,6 +3889,19 @@ static DEVICE_ATTR(dhchap_ctrl_secret, 
 S_IRUGO | S_IWUSR,
       nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
   #endif
+#ifdef CONFIG_NVME_TCP_TLS
+static ssize_t tls_key_show(struct device *dev,
+                struct device_attribute *attr, char *buf)
+{
+    struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+    if (!ctrl->tls_key)
+        return 0;
+    return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
+}
+static DEVICE_ATTR_RO(tls_key);
+#endif
+
   static struct attribute *nvme_dev_attrs[] = {
       &dev_attr_reset_controller.attr,
       &dev_attr_rescan_controller.attr,
@@ -3915,6 +3928,9 @@ static struct attribute *nvme_dev_attrs[] = {
   #ifdef CONFIG_NVME_AUTH
       &dev_attr_dhchap_secret.attr,
       &dev_attr_dhchap_ctrl_secret.attr,
+#endif
+#ifdef CONFIG_NVME_TCP_TLS
+    &dev_attr_tls_key.attr,
   #endif

      NULL
  };

drivers/nvme/host/core.c +5 -2
@@ -3945,7 +3961,10 @@ static umode_t 
 nvme_dev_attrs_are_visible(struct kobject *kobj,
       if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
           return 0;
   #endif
-
+#ifdef CONFIG_NVME_TCP_TLS
+    if (a == &dev_attr_tls_key.attr && !ctrl->opts)
+        return 0;
+#endif
       return a->mode;
   }
@@ -5080,7 +5099,7 @@ static void nvme_free_ctrl(struct device *dev)
       if (!subsys || ctrl->instance != subsys->instance)
           ida_free(&nvme_instance_ida, ctrl->instance);
-
+    key_put(ctrl->tls_key);
       nvme_free_cels(ctrl);
       nvme_mpath_uninit(ctrl);
       nvme_auth_stop(ctrl);
drivers/nvme/host/fabrics.c +0 -0
index bbaa04a0c502..2aa8fb991455 100644

drivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 612

Quoted 4 lines
drivers/nvme/host/fabrics.c +1 -0
@@ -609,6 +609,9 @@ static const match_table_t opt_tokens = {
       { NVMF_OPT_DISCOVERY,        "discovery"        },
       { NVMF_OPT_DHCHAP_SECRET,    "dhchap_secret=%s"    },
       { NVMF_OPT_DHCHAP_CTRL_SECRET,    "dhchap_ctrl_secret=%s"    },
+#ifdef CONFIG_NVME_TCP_TLS

This ifdef is redundant IMO.
We have to many of those already in the code and we should reduce to be able to keep the code maintainable.

Quoted 4 lines
Quoted 1 line

We have been going back and forth on this, and the consensus was to _have_ a compilation flag here. This allows the userland utility nvme-cli to determine whether tls support is available on any given kernel.

nvme-cli will determine tls support using check for "static const match_table_t opt_tokens" ?

Yes. The table is displayed by doing a 'cat' on /dev/nvme-fabrics, and nvme-cli will be parsing that to figure out if a particular option is supported.

Quoted 225 lines
Quoted 225 lines
Quoted 203 lines
Quoted 87 lines
drivers/nvme/host/fabrics.c +2 -0
+    { NVMF_OPT_TLS,            "tls"            },
+#endif
       { NVMF_OPT_ERR,            NULL            }
   };
@@ -632,6 +635,7 @@ static int nvmf_parse_options(struct 
 nvmf_ctrl_options *opts,
       opts->hdr_digest = false;
       opts->data_digest = false;

      opts->tos = -1; /* < 0 == use transport default */ +    opts->tls = false;
      options = o = kstrdup(buf, GFP_KERNEL); if (!options)

drivers/nvme/host/fabrics.c +8 -0
@@ -918,6 +922,14 @@ static int nvmf_parse_options(struct 
 nvmf_ctrl_options *opts,
               kfree(opts->dhchap_ctrl_secret);
               opts->dhchap_ctrl_secret = p;
               break;
+        case NVMF_OPT_TLS:
+            if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
+                pr_err("TLS is not supported\n");
+                ret = -EINVAL;
+                goto out;
+            }
+            opts->tls = true;
+            break;

          default:
              pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n", p);

drivers/nvme/host/fabrics.h +3 -0
index dcac3df8a5f7..5db36e250e7a 100644
@@ -70,6 +70,7 @@ enum {
       NVMF_OPT_DISCOVERY    = 1 << 22,
       NVMF_OPT_DHCHAP_SECRET    = 1 << 23,
       NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24,
+    NVMF_OPT_TLS        = 1 << 25,
   };
   /**
@@ -102,6 +103,7 @@ enum {
    * @dhchap_secret: DH-HMAC-CHAP secret
    * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
    *              authentication
+ * @tls:        Start TLS encrypted connections (TCP)
    * @disable_sqflow: disable controller sq flow control
    * @hdr_digest: generate/verify header digest (TCP)
    * @data_digest: generate/verify data digest (TCP)
@@ -128,6 +130,7 @@ struct nvmf_ctrl_options {
       int            max_reconnects;
       char            *dhchap_secret;
       char            *dhchap_ctrl_secret;
+    bool            tls;
       bool            disable_sqflow;
       bool            hdr_digest;
       bool            data_digest;
drivers/nvme/host/nvme.h +1 -0
index bf46f122e9e1..fbcc0ccf2ef2 100644
@@ -347,6 +347,7 @@ struct nvme_ctrl {
       struct nvme_dhchap_key *ctrl_key;
       u16 transaction;
   #endif
+    struct key *tls_key;
       /* Power saving configuration */
       u64 ps_max_latency_us;
drivers/nvme/host/tcp.c +4 -0
index 9deba481ae6a..d25695cf4e03 100644
@@ -8,9 +8,13 @@
   #include <linux/init.h>
   #include <linux/slab.h>
   #include <linux/err.h>
+#include <linux/key.h>
   #include <linux/nvme-tcp.h>
+#include <linux/nvme-keyring.h>
   #include <net/sock.h>
   #include <net/tcp.h>
+#include <net/tls.h>
+#include <net/handshake.h>
   #include <linux/blk-mq.h>
   #include <crypto/hash.h>
   #include <net/busy_poll.h>
@@ -31,6 +35,16 @@ static int so_priority;
   module_param(so_priority, int, 0644);

drivers/nvme/host/tcp.c static int so_priority; · line 45

Quoted 10 lines
drivers/nvme/host/tcp.c +9 -0
   MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
+#ifdef CONFIG_NVME_TCP_TLS
+/*
+ * TLS handshake timeout
+ */
+static int tls_handshake_timeout = 10;
+module_param(tls_handshake_timeout, int, 0644);
+MODULE_PARM_DESC(tls_handshake_timeout,
+         "nvme TLS handshake timeout in seconds (default 10)");
+#endif

Module param under ifdef also sounds redundant.

Quoted 104 lines
drivers/nvme/host/tcp.c +78 -2
+
   #ifdef CONFIG_DEBUG_LOCK_ALLOC
   /* lockdep can detect a circular dependency of the form
    *   sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
@@ -146,7 +160,10 @@ struct nvme_tcp_queue {
       struct ahash_request    *snd_hash;
       __le32            exp_ddgst;
       __le32            recv_ddgst;
-
+#ifdef CONFIG_NVME_TCP_TLS
+    struct completion       tls_complete;
+    int                     tls_err;
+#endif
       struct page_frag_cache    pf_cache;
       void (*state_change)(struct sock *);
@@ -1502,7 +1519,79 @@ static void nvme_tcp_set_queue_io_cpu(struct 
 nvme_tcp_queue *queue)
       queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
   }
-static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
+#ifdef CONFIG_NVME_TCP_TLS
+static void nvme_tcp_tls_done(void *data, int status, key_serial_t 
 pskid)
+{
+    struct nvme_tcp_queue *queue = data;
+    struct nvme_tcp_ctrl *ctrl = queue->ctrl;
+    int qid = nvme_tcp_queue_id(queue);
+    struct key *tls_key;
+
+    dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key 
 %x, status %d\n",
+        qid, pskid, status);
+
+    if (status) {
+        queue->tls_err = -status;
+        goto out_complete;
+    }
+
+    tls_key = key_lookup(pskid);
+    if (IS_ERR(tls_key)) {
+        dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n",
+             qid, pskid);
+        queue->tls_err = -ENOKEY;
+    } else {
+        ctrl->ctrl.tls_key = tls_key;
+        queue->tls_err = 0;
+    }
+
+out_complete:
+    complete(&queue->tls_complete);
+}
+
+static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
+                  struct nvme_tcp_queue *queue,
+                  key_serial_t pskid)
+{
+    int qid = nvme_tcp_queue_id(queue);
+    int ret;
+    struct tls_handshake_args args;
+    unsigned long tmo = tls_handshake_timeout * HZ;
+    key_serial_t keyring = nvme_keyring_id();
+
+    dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
+        qid, pskid);
+    args.ta_sock = queue->sock;
+    args.ta_done = nvme_tcp_tls_done;
+    args.ta_data = queue;
+    args.ta_my_peerids[0] = pskid;
+    args.ta_num_peerids = 1;
+    args.ta_keyring = keyring;
+    args.ta_timeout_ms = tls_handshake_timeout * 1000;
+    queue->tls_err = -EOPNOTSUPP;
+    init_completion(&queue->tls_complete);
+    ret = tls_client_hello_psk(&args, GFP_KERNEL);
+    if (ret) {
+        dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n",
+            qid, ret);
+        return ret;
+    }
+    if (wait_for_completion_timeout(&queue->tls_complete, tmo) == 0) {
+        dev_err(nctrl->device,
+            "queue %d: TLS handshake timeout\n", qid);
+        ret = -ETIMEDOUT;
+    } else {
+        dev_dbg(nctrl->device,
+            "queue %d: TLS handshake complete, error %d\n",
+            qid, queue->tls_err);
+        ret = queue->tls_err;
+    }
+    return ret;
+}
+#endif
+
+static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
+                key_serial_t pskid)
   {

      struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); struct nvme_tcp_queue *queue = &ctrl->queues[qid];

drivers/nvme/host/tcp.c +2 -0
@@ -1626,6 +1715,14 @@ static int nvme_tcp_alloc_queue(struct 
 nvme_ctrl *nctrl, int qid)
           goto err_rcv_pdu;
       }
+#ifdef CONFIG_NVME_TCP_TLS
+    /* If PSKs are configured try to start TLS */
Quoted 9 lines

drivers/nvme/host/tcp.c static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid) · line 1725

Quoted 6 lines
drivers/nvme/host/tcp.c +6 -0
+    if (pskid) {
+        ret = nvme_tcp_start_tls(nctrl, queue, pskid);
+        if (ret)
+            goto err_init_connect;
+    }
+#endif

Maybe reduce ifdef here and use IS_ENABLED ? (might be done in nvme_tcp_start_tls too)

Then I would need to have a dummy definition for 'nvme_tcp_start_tls'; but yeah, could do.

Quoted 10 lines
Quoted 10 lines
drivers/nvme/host/tcp.c +2 -0
       ret = nvme_tcp_init_connection(queue);
       if (ret)
           goto err_init_connect;
@@ -1769,10 +1866,22 @@ static int nvme_tcp_start_io_queues(struct 
 nvme_ctrl *ctrl,
   static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
   {
       int ret;
+    key_serial_t psk_id = 0;
+
Quoted 10 lines
Quoted 4 lines
Quoted 4 lines

drivers/nvme/host/tcp.c static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl, · line 1874

Quoted 4 lines
drivers/nvme/host/tcp.c +4 -0
+    if (ctrl->opts->tls) {
+        psk_id = nvme_tls_psk_default(NULL,
+                          ctrl->opts->host->nqn,
+                          ctrl->opts->subsysnqn);

After the patch squash you will not need this defaulk psk and also the opts->tls. Why not ask the user provide it explicitly always ? why do we need a default ? I'm not TLS expert, but seem redundant..

No. 'tls' enables a TLS connection with the credential from the keyring which matches the host and controller identification.
We could ask the user, but the information would be the same. They only can differ if the user specifies a different keyring, and then we might need to specify which key we want to use.

Quoted 44 lines
Quoted 44 lines
Quoted 33 lines
Quoted 33 lines
drivers/nvme/host/tcp.c +17 -3
+        if (!psk_id) {
+            dev_err(ctrl->device, "no valid PSK found\n");
+            ret = -ENOKEY;
+            goto out_free_queue;
+        }
+    }
-    ret = nvme_tcp_alloc_queue(ctrl, 0);
+    ret = nvme_tcp_alloc_queue(ctrl, 0, psk_id);
       if (ret)
-        return ret;
+        goto out_free_queue;
       ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
       if (ret)
@@ -1788,9 +1897,17 @@ static int nvme_tcp_alloc_admin_queue(struct 
 nvme_ctrl *ctrl)
   static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
   {
       int i, ret;
+    key_serial_t psk_id = 0;
+    if (ctrl->opts->tls) {
+        if (!ctrl->tls_key) {
+            dev_err(ctrl->device, "no PSK negotiated\n");
+            return -ENOKEY;
+        }
+        psk_id = key_serial(ctrl->tls_key);
+    }
       for (i = 1; i < ctrl->queue_count; i++) {
-        ret = nvme_tcp_alloc_queue(ctrl, i);
+        ret = nvme_tcp_alloc_queue(ctrl, i, psk_id);
           if (ret)
               goto out_free_queues;
       }
@@ -2699,7 +2816,7 @@ static struct nvmf_transport_ops 
Quoted 8 lines
Quoted 8 lines
drivers/nvme/host/tcp.c +0 -0
 nvme_tcp_transport = {

                NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST | NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | -              NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE,
+              NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS, .create_ctrl    = nvme_tcp_create_ctrl,
  };

Cheers,

Quoted 1 line
Quoted 1 line

Hannes

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#37 lore raw

Re: [PATCH 11/17] nvme-tcp: enable TLS handshake upcall

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 13 lines

index bbaa04a0c502..2aa8fb991455 100644

drivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 612

Quoted 7 lines
drivers/nvme/host/fabrics.c +1 -0
@@ -609,6 +609,9 @@ static const match_table_t opt_tokens = {
       { NVMF_OPT_DISCOVERY,        "discovery"        },
       { NVMF_OPT_DHCHAP_SECRET,    "dhchap_secret=%s"    },
       { NVMF_OPT_DHCHAP_CTRL_SECRET,    "dhchap_ctrl_secret=%s"    },
+#ifdef CONFIG_NVME_TCP_TLS

This ifdef is redundant IMO.
We have to many of those already in the code and we should reduce to be able to keep the code maintainable.

We have been going back and forth on this, and the consensus was to _have_ a compilation flag here. This allows the userland utility nvme-cli to determine whether tls support is available on any given kernel.

Quoted 6 lines
Quoted 3 lines

nvme-cli will determine tls support using check for "static const match_table_t opt_tokens" ?

Yes. The table is displayed by doing a 'cat' on /dev/nvme-fabrics, and nvme-cli will be parsing that to figure out if a particular option is supported.

FWIW, here the filter code:

https://github.com/linux-nvme/libnvme/commit/d123131f2e542e5a4c046cb65a68fc1fb97ea384

maintainer
#17 lore raw

Re: [PATCH 06/17] net/tls: implement ->read_sock()

In reply to Jakub Kicinski
Quoted 6 lines
Quoted 6 lines

Can we get an ack (or review) from you on this?

The only consumer of this is nvme, and seems to at least work on the limited testing this series has seen.

Quoted 6 lines
Quoted 2 lines

We can take this piece from the nvme tree or not, whatever you prefer.

Let's get all the TLS patches posted to netdev, reviewed there, then we can put them on a branch on top of -rc1 and they can be pulled into both netdev and nvme tree.

Hannes,

Are you planning to resubmit the series soon? Please make sure to CC netdev on the next round.

#18 lore raw

Re: [PATCH 06/17] net/tls: implement ->read_sock()

Quoted 9 lines · Sagi Grimberg
Quoted 9 lines
Quoted 8 lines

Can we get an ack (or review) from you on this?

The only consumer of this is nvme, and seems to at least work on the limited testing this series has seen.

We can take this piece from the nvme tree or not, whatever you prefer.

Quoted 8 lines
Quoted 3 lines

Let's get all the TLS patches posted to netdev, reviewed there, then we can put them on a branch on top of -rc1 and they can be pulled into both netdev and nvme tree.

Hannes,

Are you planning to resubmit the series soon? Please make sure to CC netdev on the next round.

Yes, and no.
Problem is the MSG_SPLIC patchset from David Howells; that essentially does away with ->sendpage().
So I'm not sure how to go from here; wait for the patchset to land, and rebase my patches on top of that, or proactively switch away from ->sendpage() and resubmit directly. Thoughts?

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#19 lore raw

Re: [PATCH 06/17] net/tls: implement ->read_sock()

Quoted 5 lines · Hannes Reinecke

Problem is the MSG_SPLIC patchset from David Howells; that essentially does away with ->sendpage().
So I'm not sure how to go from here; wait for the patchset to land, and rebase my patches on top of that, or proactively switch away from ->sendpage() and resubmit directly.

I'd wait for it. It should make life a lot simpler.

#20 lore raw

Re: [PATCH 06/17] net/tls: implement ->read_sock()

Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Hannes Reinecke

Problem is the MSG_SPLIC patchset from David Howells; that essentially does away with ->sendpage().
So I'm not sure how to go from here; wait for the patchset to land, and rebase my patches on top of that, or proactively switch away from ->sendpage() and resubmit directly.

I'd wait for it. It should make life a lot simpler.

Same thought here.
Let's see.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
maintainer
#21 lore raw

Re: [PATCH 06/17] net/tls: implement ->read_sock()

Quoted 9 lines · Christoph Hellwig
Quoted 7 lines · Hannes Reinecke
Quoted 5 lines

Problem is the MSG_SPLIC patchset from David Howells; that essentially does away with ->sendpage().
So I'm not sure how to go from here; wait for the patchset to land, and rebase my patches on top of that, or proactively switch away from ->sendpage() and resubmit directly.

Quoted 4 lines
Quoted 2 lines

I'd wait for it.  It should make life a lot simpler.

Same thought here.
Let's see.

Did you test that you don't need to change anything with nvme-tcp on top of the SPLICE_PAGES series from David?