[PATCH] scsi: esas2r: Enforce minimum header length bound in esas2r_write_fw()
From: Elkin Cruz
Date: Wed Nov 25 2026 - 17:20:37 EST
esas2r_write_fw() is reachable via EXPRESS_IOCTL_RW_FIRMWARE on
/proc/scsi/esas2r/ATTONode (created with mode 0444 and no capability
check).
In esas2r_write_fw(), the driver validates that the input buffer count is
at least min_size and caps header->length at 1 MiB, but does not ensure
that header->length is at least sizeof(struct esas2r_flash_img) on
FI_ACT_DOWN and FI_ACT_UP transfers, nor does it bound the header cache
memcpy() to min_size for FI_VERSION_0 headers.
If a caller supplies a non-zero header->length smaller than
sizeof(struct esas2r_flash_img) for FI_ACT_DOWN or FI_ACT_UP,
allocate_fw_buffers() allocates a DMA buffer smaller than
a->firmware.header, causing subsequent memcpy() and do_fm_api() calls in
esas2r_write_fw() and esas2r_read_fw() to read and write past the end of
a->firmware.data. Meanwhile, FI_ACT_UPSZ queries upload sizes directly on
a->firmware.header without allocating a->firmware.data, so header->length
may legitimately be zero for FI_ACT_UPSZ.
Require header->length >= sizeof(*header) for non-FI_ACT_UPSZ commands
and zero-pad a->firmware.header when copying min_size bytes from buf.
Fixes: 26780d9e12ed ("[SCSI] esas2r: ATTO Technology ExpressSAS 6G SAS/SATA RAID Adapter Driver")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Elkin Cruz <elkin@xxxxxxxxxx>
---
I am trying out new Clang static analysis checks for kernel bug
classes, and they flagged this code as possibly buggy. The findings
were triaged/fixed and commit descriptions were drafted with help of AI,
then checked against linux-next (not runtime-tested on hardware).
Thanks,
Elkin
drivers/scsi/esas2r/esas2r_ioctl.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/drivers/scsi/esas2r/esas2r_ioctl.c b/drivers/scsi/esas2r/esas2r_ioctl.c
index 901c5d40cda8..4a4489a2485b 100644
--- a/drivers/scsi/esas2r/esas2r_ioctl.c
+++ b/drivers/scsi/esas2r/esas2r_ioctl.c
@@ -1708,7 +1708,9 @@ int esas2r_write_fw(struct esas2r_adapter *a, const char *buf, long off,
/* Make sure the size is reasonable. */
length = header->length;
- if (length > 1024 * 1024) {
+ if (length > 1024 * 1024 ||
+ (header->action != FI_ACT_UPSZ &&
+ length < sizeof(*header))) {
esas2r_debug(
"esas2r_write_fw: hosed, length %d fi_version %d",
length, header->fi_version);
@@ -1729,15 +1731,15 @@ int esas2r_write_fw(struct esas2r_adapter *a, const char *buf, long off,
* Store the command, so there is context on subsequent
* calls.
*/
- memcpy(&a->firmware.header,
- buf,
- sizeof(*header));
+ memset(&a->firmware.header, 0,
+ sizeof(a->firmware.header));
+ memcpy(&a->firmware.header, buf, min_size);
} else if (header->action == FI_ACT_UP
|| header->action == FI_ACT_UPSZ) {
/* Save the command, result will be picked up on read */
- memcpy(&a->firmware.header,
- buf,
- sizeof(*header));
+ memset(&a->firmware.header, 0,
+ sizeof(a->firmware.header));
+ memcpy(&a->firmware.header, buf, min_size);
a->firmware.state = FW_COMMAND_ST;
--
2.56.1.839.g22a8290db9-goog