[PATCH 03/10] spi: ch341: Limit transfers to the hardware packet size
From: Lee Jones
Date: Thu Nov 19 2026 - 04:18:49 EST
ch341_transfer_one() clamps each transfer to CH341_PACKET_LENGTH bytes,
of which the first is the command byte, so at most 31 data bytes are
moved. It nevertheless reports success for the whole transfer.
The driver does not provide a max_transfer_size() callback, so the SPI
core never splits longer transfers. The remainder of the data is
silently dropped, and on the receive side the caller sees whatever was
already in its buffer. For spidev this means stale or uninitialised
kernel heap from its bounce buffer is copied out to user space.
Report the real limit through max_transfer_size() so that the SPI core
splits long transfers into chunks the hardware can handle.
Fixes: 8846739f52af ("spi: add ch341a usb2spi driver")
Closes: https://sashiko.dev/#/bug/linux-a21e23dc-14b5-4ed1-bfba-f508132e11a0
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/spi/spi-ch341.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/spi/spi-ch341.c b/drivers/spi/spi-ch341.c
index 56a5caa5975a..0783a2d39a61 100644
--- a/drivers/spi/spi-ch341.c
+++ b/drivers/spi/spi-ch341.c
@@ -63,6 +63,11 @@ static void ch341_set_cs(struct spi_device *spi, bool is_high)
"error sending USB message for setting CS (%d)\n", err);
}
+static size_t ch341_max_transfer_size(struct spi_device *spi)
+{
+ return CH341_PACKET_LENGTH - 1;
+}
+
static int ch341_transfer_one(struct spi_controller *host,
struct spi_device *spi,
struct spi_transfer *trans)
@@ -187,6 +192,7 @@ static int ch341_probe(struct usb_interface *intf,
ctrl->bus_num = -1;
ctrl->mode_bits = SPI_CPHA;
ctrl->transfer_one = ch341_transfer_one;
+ ctrl->max_transfer_size = ch341_max_transfer_size;
ctrl->set_cs = ch341_set_cs;
ctrl->auto_runtime_pm = false;
/*
--
2.56.1.708.g77570a6a1d-goog