Skip to content

Commit efa5630

Browse files
maurizio-lombardikeithbusch
authored andcommitted
nvmet-tcp: Fix a kernel panic when host sends an invalid H2C PDU length
If the host sends an H2CData command with an invalid DATAL, the kernel may crash in nvmet_tcp_build_pdu_iovec(). Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 lr : nvmet_tcp_io_work+0x6ac/0x718 [nvmet_tcp] Call trace: process_one_work+0x174/0x3c8 worker_thread+0x2d0/0x3e8 kthread+0x104/0x110 Fix the bug by raising a fatal error if DATAL isn't coherent with the packet size. Also, the PDU length should never exceed the MAXH2CDATA parameter which has been communicated to the host in nvmet_tcp_handle_icreq(). Fixes: 872d26a ("nvmet-tcp: add NVMe over TCP target driver") Signed-off-by: Maurizio Lombardi <mlombard@redhat.com> Reviewed-by: Sagi Grimberg <sagi@grimberg.me> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent 31e4fac commit efa5630

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

drivers/nvme/target/tcp.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "nvmet.h"
2525

2626
#define NVMET_TCP_DEF_INLINE_DATA_SIZE (4 * PAGE_SIZE)
27+
#define NVMET_TCP_MAXH2CDATA 0x400000 /* 16M arbitrary limit */
2728

2829
static int param_store_val(const char *str, int *val, int min, int max)
2930
{
@@ -923,7 +924,7 @@ static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue)
923924
icresp->hdr.pdo = 0;
924925
icresp->hdr.plen = cpu_to_le32(icresp->hdr.hlen);
925926
icresp->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
926-
icresp->maxdata = cpu_to_le32(0x400000); /* 16M arbitrary limit */
927+
icresp->maxdata = cpu_to_le32(NVMET_TCP_MAXH2CDATA);
927928
icresp->cpda = 0;
928929
if (queue->hdr_digest)
929930
icresp->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
@@ -978,6 +979,7 @@ static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue)
978979
{
979980
struct nvme_tcp_data_pdu *data = &queue->pdu.data;
980981
struct nvmet_tcp_cmd *cmd;
982+
unsigned int plen;
981983

982984
if (likely(queue->nr_cmds)) {
983985
if (unlikely(data->ttag >= queue->nr_cmds)) {
@@ -1001,7 +1003,16 @@ static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue)
10011003
return -EPROTO;
10021004
}
10031005

1006+
plen = le32_to_cpu(data->hdr.plen);
10041007
cmd->pdu_len = le32_to_cpu(data->data_length);
1008+
if (unlikely(cmd->pdu_len != (plen - sizeof(*data)) ||
1009+
cmd->pdu_len == 0 ||
1010+
cmd->pdu_len > NVMET_TCP_MAXH2CDATA)) {
1011+
pr_err("H2CData PDU len %u is invalid\n", cmd->pdu_len);
1012+
/* FIXME: use proper transport errors */
1013+
nvmet_tcp_fatal_error(queue);
1014+
return -EPROTO;
1015+
}
10051016
cmd->pdu_recv = 0;
10061017
nvmet_tcp_build_pdu_iovec(cmd);
10071018
queue->cmd = cmd;

0 commit comments

Comments
 (0)