[PATCH v2 1/8] scsi: target: take COMPARE AND WRITE data from the write half
From: Jia Jia
Date: Tue Nov 17 2026 - 23:14:13 EST
For block sizes smaller than a page, compare_and_write_callback() adds
block_size to the first data sg's offset to locate the write half.
This assumes that both halves fit in that sg. If the compare buffer
ends at a page boundary, the write entry points past the page even
though the write data is available in the following sg.
vhost-scsi splits guest buffers into sg entries that each stay within
one page, so the backend can read beyond the supplied data.
Skip the compare bytes across the data sgs and build a table covering
the write half. Pass its actual entry count to the backend and release
it with sg_free_table(), including any chained allocations.
KASAN reports:
BUG: KASAN: slab-use-after-free in copy_folio_from_iter_atomic
Read of size 512
compare_and_write_callback
__target_execute_cmd
sbc_execute_rw
fd_execute_rw
fd_do_rw
vfs_iter_write
copy_folio_from_iter_atomic
Fixes: d94e5a61357a ("target: fix COMPARE_AND_WRITE non zero SGL offset data corruption")
Signed-off-by: Jia Jia <physicalmtea@xxxxxxxxx>
---
drivers/target/target_core_sbc.c | 100 +++++++++++++++++--------
drivers/target/target_core_transport.c | 11 ++-
2 files changed, 80 insertions(+), 31 deletions(-)
diff --git a/drivers/target/target_core_sbc.c b/drivers/target/target_core_sbc.c
index 21f5cb86d70c0..adef903652ac2 100644
--- a/drivers/target/target_core_sbc.c
+++ b/drivers/target/target_core_sbc.c
@@ -433,19 +433,74 @@ out:
return ret;
}
+/*
+ * Data-out is compare bytes followed by write bytes. Take the write
+ * half from whatever sg entries hold it. Do not add block_size onto
+ * the first sg offset: that page may end before the write half.
+ */
+static int sbc_caw_build_write_sg(struct sg_table *tbl,
+ struct scatterlist *data_sg,
+ unsigned int data_nents, unsigned int skip,
+ unsigned int len)
+{
+ struct scatterlist *sg, *out;
+ unsigned int nents = 0, left = len, left_skip = skip;
+ unsigned int avail, chunk;
+ int i;
+
+ for_each_sg(data_sg, sg, data_nents, i) {
+ if (!sg->length)
+ continue;
+ if (!left)
+ break;
+ if (left_skip >= sg->length) {
+ left_skip -= sg->length;
+ continue;
+ }
+ avail = sg->length - left_skip;
+ chunk = min(left, avail);
+ nents++;
+ left -= chunk;
+ left_skip = 0;
+ }
+ if (!nents || left)
+ return -EINVAL;
+
+ if (sg_alloc_table(tbl, nents, GFP_KERNEL))
+ return -ENOMEM;
+
+ left = len;
+ left_skip = skip;
+ out = tbl->sgl;
+ for_each_sg(data_sg, sg, data_nents, i) {
+ if (!sg->length)
+ continue;
+ if (!left)
+ break;
+ if (left_skip >= sg->length) {
+ left_skip -= sg->length;
+ continue;
+ }
+ avail = sg->length - left_skip;
+ chunk = min(left, avail);
+ sg_set_page(out, sg_page(sg), chunk, sg->offset + left_skip);
+ left -= chunk;
+ left_skip = 0;
+ out = sg_next(out);
+ }
+ return 0;
+}
+
static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success,
int *post_ret)
{
struct se_device *dev = cmd->se_dev;
struct sg_table write_tbl = { };
struct scatterlist *write_sg;
- struct sg_mapping_iter m;
- unsigned int len;
- unsigned int block_size = dev->dev_attrib.block_size;
- unsigned int compare_len = (cmd->t_task_nolb * block_size);
+ unsigned int compare_len = (cmd->t_task_nolb * dev->dev_attrib.block_size);
unsigned int miscmp_off = 0;
sense_reason_t ret = TCM_NO_SENSE;
- int i;
+ int rc;
if (!success) {
/*
@@ -499,34 +554,19 @@ static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool succes
} else if (ret)
goto out;
- if (sg_alloc_table(&write_tbl, cmd->t_data_nents, GFP_KERNEL) < 0) {
- pr_err("Unable to allocate compare_and_write sg\n");
- ret = TCM_OUT_OF_RESOURCES;
+ rc = sbc_caw_build_write_sg(&write_tbl, cmd->t_data_sg,
+ cmd->t_data_nents, compare_len,
+ compare_len);
+ if (rc) {
+ pr_err("Unable to build compare_and_write sg\n");
+ if (rc == -ENOMEM)
+ ret = TCM_OUT_OF_RESOURCES;
+ else
+ ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
goto out;
}
write_sg = write_tbl.sgl;
- i = 0;
- len = compare_len;
- sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG);
- /*
- * Currently assumes NoLB=1 and SGLs are PAGE_SIZE..
- */
- while (len) {
- sg_miter_next(&m);
-
- if (block_size < PAGE_SIZE) {
- sg_set_page(&write_sg[i], m.page, block_size,
- m.piter.sg->offset + block_size);
- } else {
- sg_miter_next(&m);
- sg_set_page(&write_sg[i], m.page, block_size,
- m.piter.sg->offset);
- }
- len -= block_size;
- i++;
- }
- sg_miter_stop(&m);
/*
* Save the original SGL + nents values before updating to new
* assignments, to be released in transport_free_pages() ->
@@ -535,7 +575,7 @@ static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool succes
cmd->t_data_sg_orig = cmd->t_data_sg;
cmd->t_data_sg = write_sg;
cmd->t_data_nents_orig = cmd->t_data_nents;
- cmd->t_data_nents = 1;
+ cmd->t_data_nents = write_tbl.nents;
cmd->sam_task_attr = TCM_HEAD_TAG;
cmd->transport_complete_callback = compare_and_write_post;
diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c
index dcfe945949167..3cae5ed67d417 100644
--- a/drivers/target/target_core_transport.c
+++ b/drivers/target/target_core_transport.c
@@ -22,6 +22,7 @@
#include <linux/module.h>
#include <linux/ratelimit.h>
#include <linux/vmalloc.h>
+#include <linux/scatterlist.h>
#include <linux/unaligned.h>
#include <net/sock.h>
#include <net/tcp.h>
@@ -2724,10 +2725,18 @@ static inline void transport_reset_sgl_orig(struct se_cmd *cmd)
* Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
* emulation, and free + reset pointers if necessary..
*/
+ struct sg_table table = { };
+
if (!cmd->t_data_sg_orig)
return;
- kfree(cmd->t_data_sg);
+ /*
+ * compare_and_write_callback() built this with sg_alloc_table().
+ * Above SG_MAX_SINGLE_ALLOC the tail is a separate allocation.
+ */
+ table.sgl = cmd->t_data_sg;
+ table.orig_nents = cmd->t_data_nents;
+ sg_free_table(&table);
cmd->t_data_sg = cmd->t_data_sg_orig;
cmd->t_data_sg_orig = NULL;
cmd->t_data_nents = cmd->t_data_nents_orig;