[PATCH v4] RDMA/srpt: Prevent uninitialized read buffer disclosure
From: liuyong
Date: Thu Nov 12 2026 - 04:56:23 EST
From: Yong Liu <pkfxxxing@xxxxxxxxx>
SRPT allocates read buffers and creates RDMA contexts from the
initiator's descriptors before parsing the CDB. When target core reduces
cmd->data_length, a backend such as rd_mcp fills only the shorter
length, but the prebuilt RDMA transfer still sends the full buffer.
Control CDB handlers can also leave bytes within cmd->data_length
unwritten, while target core only clears the first SG entry of these
preallocated buffers. Both cases can disclose stale target kernel memory
to a remote initiator with access to the LUN.
Reject indirect descriptor tables whose direct descriptor lengths do not
sum to the advertised total before allocating RDMA contexts. Also
initialize commands rejected during descriptor parsing so the BUSY
response path has valid callbacks and a command reference.
Save the original buffer length and zero the entire control read buffer
before command execution. For data READs, zero any tail beyond
cmd->data_length before posting the existing RDMA transfers. Synchronize
each mapped SG list for device access in the response path.
The issue was discovered by Qihoo 360's Tulongfeng multi-agent system.
Fixes: b99f8e4d7bcd ("IB/srpt: convert to the generic RDMA READ/WRITE API")
Fixes: 8b8807b9e982 ("scsi: RDMA/srpt: Fix handling of command / TMF submission failure")
Cc: stable@xxxxxxxxxxxxxxx
Suggested-by: Bart Van Assche <bvanassche@xxxxxxx>
Signed-off-by: Yong Liu <pkfxxxing@xxxxxxxxx>
---
Changes in v4 (addressing the three issues reported by sashiko-bot):
- Zero the entire original control read buffer before command execution.
Limit tail zeroing to data READs and synchronize mapped SG lists in
the RDMA submission loop.
- Use Yong Liu in the author and Signed-off-by fields.
- Add the Fixes tag for the descriptor-parsing BUSY path initialization.
Developed against f66469d9905b39d2c260a15d7d6db450f4db67c1 (7.4-rc1).
drivers/infiniband/ulp/srpt/ib_srpt.c | 48 +++++++++++++++++++++++++++
drivers/infiniband/ulp/srpt/ib_srpt.h | 2 ++
2 files changed, 50 insertions(+)
diff --git a/drivers/infiniband/ulp/srpt/ib_srpt.c b/drivers/infiniband/ulp/srpt/ib_srpt.c
index 43c3ff1ad8..0ec230950b 100644
--- a/drivers/infiniband/ulp/srpt/ib_srpt.c
+++ b/drivers/infiniband/ulp/srpt/ib_srpt.c
@@ -41,6 +41,7 @@
#include <linux/kthread.h>
#include <linux/string.h>
#include <linux/delay.h>
+#include <linux/dma-mapping.h>
#include <linux/atomic.h>
#include <linux/inet.h>
#include <rdma/ib_cache.h>
@@ -1115,8 +1116,10 @@ static int srpt_get_desc_tbl(struct srpt_recv_ioctx *recv_ioctx,
} else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
+ u64 total_len = 0;
int nbufs = be32_to_cpu(idb->table_desc.len) /
sizeof(struct srp_direct_buf);
+ int i;
if (nbufs >
(srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
@@ -1128,6 +1131,11 @@ static int srpt_get_desc_tbl(struct srpt_recv_ioctx *recv_ioctx,
return -EINVAL;
}
+ for (i = 0; i < nbufs; i++)
+ total_len += be32_to_cpu(idb->desc_list[i].len);
+ if (total_len != be32_to_cpu(idb->len))
+ return -EINVAL;
+
*data_len = be32_to_cpu(idb->len);
return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
sg, sg_cnt);
@@ -1304,6 +1312,7 @@ static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
WARN_ON_ONCE(ioctx->recv_ioctx);
ioctx->n_rdma = 0;
ioctx->n_rw_ctx = 0;
+ ioctx->buf_len = 0;
ioctx->queue_status_only = false;
/*
* transport_init_se_cmd() does not initialize all fields, so do it
@@ -1597,9 +1606,13 @@ static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
pr_err("0x%llx: parsing SRP descriptor table failed.\n",
srp_cmd->tag);
}
+ target_init_cmd(cmd, ch->sess, &send_ioctx->sense_data[0],
+ scsilun_to_int(&srp_cmd->lun), 0,
+ cmd->sam_task_attr, dir, 0);
goto busy;
}
+ send_ioctx->buf_len = data_len;
rc = target_init_cmd(cmd, ch->sess, &send_ioctx->sense_data[0],
scsilun_to_int(&srp_cmd->lun), data_len,
cmd->sam_task_attr, dir, TARGET_SCF_ACK_KREF);
@@ -1613,6 +1626,11 @@ static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
GFP_KERNEL))
return;
+ /* Control CDB handlers may leave part of the response untouched. */
+ if (cmd->data_direction == DMA_FROM_DEVICE &&
+ !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
+ sg_zero_buffer(sg, sg_cnt, send_ioctx->buf_len, 0);
+
target_submit(cmd);
return;
@@ -2866,6 +2884,26 @@ static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
return SRP_TSK_MGMT_FAILED;
}
+static void srpt_zero_read_tail(struct srpt_send_ioctx *ioctx)
+{
+ u32 data_len = ioctx->cmd.data_length;
+ int i;
+
+ for (i = 0; i < ioctx->n_rw_ctx; i++) {
+ struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
+ struct scatterlist *sg;
+ unsigned int j;
+
+ for_each_sg(ctx->sg, sg, ctx->nents, j) {
+ u32 skip = min(data_len, sg->length);
+
+ data_len -= skip;
+ if (skip < sg->length)
+ sg_zero_buffer(sg, 1, sg->length - skip, skip);
+ }
+ }
+}
+
/**
* srpt_queue_response - transmit the response to a SCSI command
* @cmd: SCSI target command.
@@ -2907,9 +2945,19 @@ static void srpt_queue_response(struct se_cmd *cmd)
if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
ioctx->cmd.data_length &&
!ioctx->queue_status_only) {
+ if ((cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
+ ioctx->buf_len > cmd->data_length)
+ srpt_zero_read_tail(ioctx);
+
for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
+ /* Make CPU writes and zeroed data visible to the RDMA device. */
+ if (!ib_uses_virt_dma(sdev->device))
+ dma_sync_sg_for_device(sdev->device->dma_device,
+ ctx->sg, ctx->nents,
+ DMA_TO_DEVICE);
+
first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
ch->sport->port, NULL, first_wr);
}
diff --git a/drivers/infiniband/ulp/srpt/ib_srpt.h b/drivers/infiniband/ulp/srpt/ib_srpt.h
index 506c11b843..78bba74148 100644
--- a/drivers/infiniband/ulp/srpt/ib_srpt.h
+++ b/drivers/infiniband/ulp/srpt/ib_srpt.h
@@ -204,6 +204,7 @@ struct srpt_rw_ctx {
* @rdma_cqe: RDMA completion queue element.
* @state: I/O context state.
* @cmd: Target core command data structure.
+ * @buf_len: Original SRP data buffer length.
* @sense_data: SCSI sense data.
* @n_rdma: Number of work requests needed to transfer this ioctx.
* @n_rw_ctx: Size of rw_ctxs array.
@@ -223,6 +224,7 @@ struct srpt_send_ioctx {
struct ib_cqe rdma_cqe;
enum srpt_command_state state;
struct se_cmd cmd;
+ u32 buf_len;
u8 n_rdma;
u8 n_rw_ctx;
bool queue_status_only;
--
2.43.0