Skip to content

Commit 3638957

Browse files
Hannes Reineckekeithbusch
authored andcommitted
nvme-tcp: sanitize TLS key handling
There is a difference between TLS configured (ie the user has provisioned/requested a key) and TLS enabled (ie the connection is encrypted with TLS). This becomes important for secure concatenation, where the initial authentication is run on an unencrypted connection (ie with TLS configured, but not enabled), and then the queue is reset to run over TLS (ie TLS configured _and_ enabled). So to differentiate between those two states store the generated key in opts->tls_key (as we're using the same TLS key for all queues), the key serial of the resulting TLS handshake in ctrl->tls_pskid (to signal that TLS on the admin queue is enabled), and a simple flag for the queues to indicated that TLS has been enabled. Signed-off-by: Hannes Reinecke <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Keith Busch <[email protected]>
1 parent 79559c7 commit 3638957

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

drivers/nvme/host/core.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4715,7 +4715,6 @@ static void nvme_free_ctrl(struct device *dev)
47154715

47164716
if (!subsys || ctrl->instance != subsys->instance)
47174717
ida_free(&nvme_instance_ida, ctrl->instance);
4718-
key_put(ctrl->tls_key);
47194718
nvme_free_cels(ctrl);
47204719
nvme_mpath_uninit(ctrl);
47214720
cleanup_srcu_struct(&ctrl->srcu);

drivers/nvme/host/nvme.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ struct nvme_ctrl {
373373
struct nvme_dhchap_key *ctrl_key;
374374
u16 transaction;
375375
#endif
376-
struct key *tls_key;
376+
key_serial_t tls_pskid;
377377

378378
/* Power saving configuration */
379379
u64 ps_max_latency_us;

drivers/nvme/host/sysfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,9 @@ static ssize_t tls_key_show(struct device *dev,
670670
{
671671
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
672672

673-
if (!ctrl->tls_key)
673+
if (!ctrl->tls_pskid)
674674
return 0;
675-
return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
675+
return sysfs_emit(buf, "%08x", ctrl->tls_pskid);
676676
}
677677
static DEVICE_ATTR_RO(tls_key);
678678
#endif

drivers/nvme/host/tcp.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ struct nvme_tcp_queue {
165165

166166
bool hdr_digest;
167167
bool data_digest;
168+
bool tls_enabled;
168169
struct ahash_request *rcv_hash;
169170
struct ahash_request *snd_hash;
170171
__le32 exp_ddgst;
@@ -213,7 +214,21 @@ static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
213214
return queue - queue->ctrl->queues;
214215
}
215216

216-
static inline bool nvme_tcp_tls(struct nvme_ctrl *ctrl)
217+
/*
218+
* Check if the queue is TLS encrypted
219+
*/
220+
static inline bool nvme_tcp_queue_tls(struct nvme_tcp_queue *queue)
221+
{
222+
if (!IS_ENABLED(CONFIG_NVME_TCP_TLS))
223+
return 0;
224+
225+
return queue->tls_enabled;
226+
}
227+
228+
/*
229+
* Check if TLS is configured for the controller.
230+
*/
231+
static inline bool nvme_tcp_tls_configured(struct nvme_ctrl *ctrl)
217232
{
218233
if (!IS_ENABLED(CONFIG_NVME_TCP_TLS))
219234
return 0;
@@ -368,7 +383,7 @@ static inline bool nvme_tcp_queue_has_pending(struct nvme_tcp_queue *queue)
368383

369384
static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue)
370385
{
371-
return !nvme_tcp_tls(&queue->ctrl->ctrl) &&
386+
return !nvme_tcp_queue_tls(queue) &&
372387
nvme_tcp_queue_has_pending(queue);
373388
}
374389

@@ -1427,7 +1442,7 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
14271442
memset(&msg, 0, sizeof(msg));
14281443
iov.iov_base = icresp;
14291444
iov.iov_len = sizeof(*icresp);
1430-
if (nvme_tcp_tls(&queue->ctrl->ctrl)) {
1445+
if (nvme_tcp_queue_tls(queue)) {
14311446
msg.msg_control = cbuf;
14321447
msg.msg_controllen = sizeof(cbuf);
14331448
}
@@ -1439,7 +1454,7 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
14391454
goto free_icresp;
14401455
}
14411456
ret = -ENOTCONN;
1442-
if (nvme_tcp_tls(&queue->ctrl->ctrl)) {
1457+
if (nvme_tcp_queue_tls(queue)) {
14431458
ctype = tls_get_record_type(queue->sock->sk,
14441459
(struct cmsghdr *)cbuf);
14451460
if (ctype != TLS_RECORD_TYPE_DATA) {
@@ -1587,7 +1602,10 @@ static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid)
15871602
qid, pskid);
15881603
queue->tls_err = -ENOKEY;
15891604
} else {
1590-
ctrl->ctrl.tls_key = tls_key;
1605+
queue->tls_enabled = true;
1606+
if (qid == 0)
1607+
ctrl->ctrl.tls_pskid = key_serial(tls_key);
1608+
key_put(tls_key);
15911609
queue->tls_err = 0;
15921610
}
15931611

@@ -1768,7 +1786,7 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
17681786
}
17691787

17701788
/* If PSKs are configured try to start TLS */
1771-
if (IS_ENABLED(CONFIG_NVME_TCP_TLS) && pskid) {
1789+
if (nvme_tcp_tls_configured(nctrl) && pskid) {
17721790
ret = nvme_tcp_start_tls(nctrl, queue, pskid);
17731791
if (ret)
17741792
goto err_init_connect;
@@ -1829,6 +1847,8 @@ static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
18291847
mutex_lock(&queue->queue_lock);
18301848
if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
18311849
__nvme_tcp_stop_queue(queue);
1850+
/* Stopping the queue will disable TLS */
1851+
queue->tls_enabled = false;
18321852
mutex_unlock(&queue->queue_lock);
18331853
}
18341854

@@ -1925,16 +1945,17 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
19251945
int ret;
19261946
key_serial_t pskid = 0;
19271947

1928-
if (nvme_tcp_tls(ctrl)) {
1948+
if (nvme_tcp_tls_configured(ctrl)) {
19291949
if (ctrl->opts->tls_key)
19301950
pskid = key_serial(ctrl->opts->tls_key);
1931-
else
1951+
else {
19321952
pskid = nvme_tls_psk_default(ctrl->opts->keyring,
19331953
ctrl->opts->host->nqn,
19341954
ctrl->opts->subsysnqn);
1935-
if (!pskid) {
1936-
dev_err(ctrl->device, "no valid PSK found\n");
1937-
return -ENOKEY;
1955+
if (!pskid) {
1956+
dev_err(ctrl->device, "no valid PSK found\n");
1957+
return -ENOKEY;
1958+
}
19381959
}
19391960
}
19401961

@@ -1957,13 +1978,14 @@ static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
19571978
{
19581979
int i, ret;
19591980

1960-
if (nvme_tcp_tls(ctrl) && !ctrl->tls_key) {
1981+
if (nvme_tcp_tls_configured(ctrl) && !ctrl->tls_pskid) {
19611982
dev_err(ctrl->device, "no PSK negotiated\n");
19621983
return -ENOKEY;
19631984
}
1985+
19641986
for (i = 1; i < ctrl->queue_count; i++) {
19651987
ret = nvme_tcp_alloc_queue(ctrl, i,
1966-
key_serial(ctrl->tls_key));
1988+
ctrl->tls_pskid);
19671989
if (ret)
19681990
goto out_free_queues;
19691991
}
@@ -2144,6 +2166,11 @@ static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
21442166
if (remove)
21452167
nvme_unquiesce_admin_queue(ctrl);
21462168
nvme_tcp_destroy_admin_queue(ctrl, remove);
2169+
if (ctrl->tls_pskid) {
2170+
dev_dbg(ctrl->device, "Wipe negotiated TLS_PSK %08x\n",
2171+
ctrl->tls_pskid);
2172+
ctrl->tls_pskid = 0;
2173+
}
21472174
}
21482175

21492176
static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,

0 commit comments

Comments
 (0)