public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [SCSI] esas2r: fix possible buffer overflow caused by bad DMA value in esas2r_process_vda_ioctl()
@ 2024-06-12  9:31 Huai-Yuan Liu
  0 siblings, 0 replies; 3+ messages in thread
From: Huai-Yuan Liu @ 2024-06-12  9:31 UTC (permalink / raw)
  To: linuxdrivers, James.Bottomley, martin.petersen
  Cc: linux-scsi, linux-kernel, baijiaju1990, Huai-Yuan Liu

The value vi->function is stored in DMA memory, so it can be modified at
any time by malicious hardware. In this case, "if (vi->function >= vercnt)"
can be passed, which may cause buffer overflow and other unexpected 
execution results in the following code.

To address this issue, vi->function should be assigned to a local value,
which replaces the use of vi->function.

Fixes: 26780d9e12ed ("[SCSI] esas2r: ATTO Technology ExpressSAS 6G SAS/SATA RAID Adapter Driver")
Signed-off-by: Huai-Yuan Liu <qq810974084@gmail.com>
---
 drivers/scsi/esas2r/esas2r_vda.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/scsi/esas2r/esas2r_vda.c b/drivers/scsi/esas2r/esas2r_vda.c
index 30028e56df63..48af8c05b01d 100644
--- a/drivers/scsi/esas2r/esas2r_vda.c
+++ b/drivers/scsi/esas2r/esas2r_vda.c
@@ -70,16 +70,17 @@ bool esas2r_process_vda_ioctl(struct esas2r_adapter *a,
 	u32 datalen = 0;
 	struct atto_vda_sge *firstsg = NULL;
 	u8 vercnt = (u8)ARRAY_SIZE(esas2r_vdaioctl_versions);
+	u8 vi_function = vi->function;
 
 	vi->status = ATTO_STS_SUCCESS;
 	vi->vda_status = RS_PENDING;
 
-	if (vi->function >= vercnt) {
+	if (vi_function >= vercnt) {
 		vi->status = ATTO_STS_INV_FUNC;
 		return false;
 	}
 
-	if (vi->version > esas2r_vdaioctl_versions[vi->function]) {
+	if (vi->version > esas2r_vdaioctl_versions[vi_function]) {
 		vi->status = ATTO_STS_INV_VERSION;
 		return false;
 	}
@@ -89,14 +90,14 @@ bool esas2r_process_vda_ioctl(struct esas2r_adapter *a,
 		return false;
 	}
 
-	if (vi->function != VDA_FUNC_SCSI)
+	if (vi_function != VDA_FUNC_SCSI)
 		clear_vda_request(rq);
 
-	rq->vrq->scsi.function = vi->function;
+	rq->vrq->scsi.function = vi_function;
 	rq->interrupt_cb = esas2r_complete_vda_ioctl;
 	rq->interrupt_cx = vi;
 
-	switch (vi->function) {
+	switch (vi_function) {
 	case VDA_FUNC_FLASH:
 
 		if (vi->cmd.flash.sub_func != VDA_FLASH_FREAD
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH] [SCSI] esas2r: fix possible buffer overflow caused by bad DMA value in esas2r_process_vda_ioctl()
@ 2024-11-07 11:36 Qiu-ji Chen
  2024-11-07 15:08 ` Greg KH
  0 siblings, 1 reply; 3+ messages in thread
From: Qiu-ji Chen @ 2024-11-07 11:36 UTC (permalink / raw)
  To: linuxdrivers, James.Bottomley, martin.petersen
  Cc: linux-scsi, linux-kernel, baijiaju1990, Qiu-ji Chen, stable

In line 1854 of the file esas2r_ioctl.c, the function 
esas2r_process_vda_ioctl() is called with the parameter vi being assigned 
the value of a->vda_buffer. On line 1892, a->vda_buffer is stored in DMA 
memory with the statement 
a->vda_buffer = dma_alloc_coherent(&a->pcid->dev, ..., indicating that the 
parameter vi passed to the function is also stored in DMA memory. This 
suggests that the parameter vi could be altered at any time by malicious 
hardware. If vi’s value is changed after the first conditional check 
if (vi->function >= vercnt), it is likely that an array out-of-bounds 
access could occur in the subsequent check 
if (vi->version > esas2r_vdaioctl_versions[vi_function]), leading to 
serious issues.

To fix this issue, we will store the value of vi->function in a local 
variable to ensure that the subsequent checks remain valid.

Signed-off-by: Qiu-ji Chen <chenqiuji666@gmail.com>
Cc: stable@vger.kernel.org
Fixes: 26780d9e12ed ("[SCSI] esas2r: ATTO Technology ExpressSAS 6G SAS/SATA RAID Adapter Driver")
---
 drivers/scsi/esas2r/esas2r_vda.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/scsi/esas2r/esas2r_vda.c b/drivers/scsi/esas2r/esas2r_vda.c
index 30028e56df63..48af8c05b01d 100644
--- a/drivers/scsi/esas2r/esas2r_vda.c
+++ b/drivers/scsi/esas2r/esas2r_vda.c
@@ -70,16 +70,17 @@ bool esas2r_process_vda_ioctl(struct esas2r_adapter *a,
 	u32 datalen = 0;
 	struct atto_vda_sge *firstsg = NULL;
 	u8 vercnt = (u8)ARRAY_SIZE(esas2r_vdaioctl_versions);
+	u8 vi_function = vi->function;
 
 	vi->status = ATTO_STS_SUCCESS;
 	vi->vda_status = RS_PENDING;
 
-	if (vi->function >= vercnt) {
+	if (vi_function >= vercnt) {
 		vi->status = ATTO_STS_INV_FUNC;
 		return false;
 	}
 
-	if (vi->version > esas2r_vdaioctl_versions[vi->function]) {
+	if (vi->version > esas2r_vdaioctl_versions[vi_function]) {
 		vi->status = ATTO_STS_INV_VERSION;
 		return false;
 	}
@@ -89,14 +90,14 @@ bool esas2r_process_vda_ioctl(struct esas2r_adapter *a,
 		return false;
 	}
 
-	if (vi->function != VDA_FUNC_SCSI)
+	if (vi_function != VDA_FUNC_SCSI)
 		clear_vda_request(rq);
 
-	rq->vrq->scsi.function = vi->function;
+	rq->vrq->scsi.function = vi_function;
 	rq->interrupt_cb = esas2r_complete_vda_ioctl;
 	rq->interrupt_cx = vi;
 
-	switch (vi->function) {
+	switch (vi_function) {
 	case VDA_FUNC_FLASH:
 
 		if (vi->cmd.flash.sub_func != VDA_FLASH_FREAD
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] [SCSI] esas2r: fix possible buffer overflow caused by bad DMA value in esas2r_process_vda_ioctl()
  2024-11-07 11:36 [PATCH] [SCSI] esas2r: fix possible buffer overflow caused by bad DMA value in esas2r_process_vda_ioctl() Qiu-ji Chen
@ 2024-11-07 15:08 ` Greg KH
  0 siblings, 0 replies; 3+ messages in thread
From: Greg KH @ 2024-11-07 15:08 UTC (permalink / raw)
  To: Qiu-ji Chen
  Cc: linuxdrivers, James.Bottomley, martin.petersen, linux-scsi,
	linux-kernel, baijiaju1990, stable

On Thu, Nov 07, 2024 at 07:36:17PM +0800, Qiu-ji Chen wrote:
> In line 1854 of the file esas2r_ioctl.c, the function 
> esas2r_process_vda_ioctl() is called with the parameter vi being assigned 
> the value of a->vda_buffer. On line 1892, a->vda_buffer is stored in DMA 
> memory with the statement 
> a->vda_buffer = dma_alloc_coherent(&a->pcid->dev, ..., indicating that the 
> parameter vi passed to the function is also stored in DMA memory. This 
> suggests that the parameter vi could be altered at any time by malicious 
> hardware.

As James pointed out, "malicious hardware" is not a threat model that
Linux worries about at this point in time, sorry.

If you wish to have Linux care about this, random driver changes like
this is not going to be the way forward, but rather, major things need
to be rearchitected in order to "protect" the kernel from bad hardware.

But really, if you can't trust the hardware, you have bigger problems,
any software can't protect you from that :)

sorry,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-11-07 15:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-07 11:36 [PATCH] [SCSI] esas2r: fix possible buffer overflow caused by bad DMA value in esas2r_process_vda_ioctl() Qiu-ji Chen
2024-11-07 15:08 ` Greg KH
  -- strict thread matches above, loose matches on Subject: below --
2024-06-12  9:31 Huai-Yuan Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox