* [PATCH -next] scsi: be2iscsi: fix null-ptr-deref in beiscsi_alloc_mem
@ 2022-07-27 7:21 Ye Bin
2022-08-25 11:26 ` yebin
0 siblings, 1 reply; 2+ messages in thread
From: Ye Bin @ 2022-07-27 7:21 UTC (permalink / raw)
To: jejb, martin.petersen, linux-scsi, linux-kernel; +Cc: Ye Bin
Coccicheck report follow issue:
./drivers/scsi/be2iscsi/be_main.c:2561:37-41: ERROR: mem_descr -> mem_array is NULL but dereferenced.
./drivers/scsi/be2iscsi/be_main.c:2563:9-24: ERROR: mem_descr -> mem_array is NULL but dereferenced.
./drivers/scsi/be2iscsi/be_main.c:2566:9-20: ERROR: mem_descr -> mem_array is NULL but dereferenced.
If goto free_mem, that's mean last "mem_descr->mem_array" is NULL. However,
to avoid resource there will set "mem_descr->num_elements" with "j" which
maybe non-zero. Then will lead to null-ptr-deref when free dma resource.
To solved above issue set "mem_descr->mem_array" with "mem_arr_orig"
when free memory.
Signed-off-by: Ye Bin <yebin10@huawei.com>
---
drivers/scsi/be2iscsi/be_main.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/drivers/scsi/be2iscsi/be_main.c b/drivers/scsi/be2iscsi/be_main.c
index 50a577ac3bb4..08f856447b4a 100644
--- a/drivers/scsi/be2iscsi/be_main.c
+++ b/drivers/scsi/be2iscsi/be_main.c
@@ -2540,13 +2540,13 @@ static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
mem_arr++;
}
} while (alloc_size);
- mem_descr->num_elements = j;
- mem_descr->size_in_bytes = phba->mem_req[i];
mem_descr->mem_array = kmalloc_array(j, sizeof(*mem_arr),
GFP_KERNEL);
if (!mem_descr->mem_array)
goto free_mem;
+ mem_descr->num_elements = j;
+ mem_descr->size_in_bytes = phba->mem_req[i];
memcpy(mem_descr->mem_array, mem_arr_orig,
sizeof(struct mem_array) * j);
mem_descr++;
@@ -2554,7 +2554,11 @@ static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
kfree(mem_arr_orig);
return 0;
free_mem:
- mem_descr->num_elements = j;
+ if (j) {
+ mem_descr->num_elements = j;
+ mem_descr->mem_array = mem_arr_orig;
+ mem_arr_orig = NULL;
+ }
while ((i) || (j)) {
for (j = mem_descr->num_elements; j > 0; j--) {
dma_free_coherent(&phba->pcidev->dev,
--
2.31.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH -next] scsi: be2iscsi: fix null-ptr-deref in beiscsi_alloc_mem
2022-07-27 7:21 [PATCH -next] scsi: be2iscsi: fix null-ptr-deref in beiscsi_alloc_mem Ye Bin
@ 2022-08-25 11:26 ` yebin
0 siblings, 0 replies; 2+ messages in thread
From: yebin @ 2022-08-25 11:26 UTC (permalink / raw)
To: jejb, martin.petersen, linux-scsi, linux-kernel
ping...
On 2022/7/27 15:21, Ye Bin wrote:
> Coccicheck report follow issue:
> ./drivers/scsi/be2iscsi/be_main.c:2561:37-41: ERROR: mem_descr -> mem_array is NULL but dereferenced.
> ./drivers/scsi/be2iscsi/be_main.c:2563:9-24: ERROR: mem_descr -> mem_array is NULL but dereferenced.
> ./drivers/scsi/be2iscsi/be_main.c:2566:9-20: ERROR: mem_descr -> mem_array is NULL but dereferenced.
>
> If goto free_mem, that's mean last "mem_descr->mem_array" is NULL. However,
> to avoid resource there will set "mem_descr->num_elements" with "j" which
> maybe non-zero. Then will lead to null-ptr-deref when free dma resource.
>
> To solved above issue set "mem_descr->mem_array" with "mem_arr_orig"
> when free memory.
>
> Signed-off-by: Ye Bin <yebin10@huawei.com>
> ---
> drivers/scsi/be2iscsi/be_main.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/scsi/be2iscsi/be_main.c b/drivers/scsi/be2iscsi/be_main.c
> index 50a577ac3bb4..08f856447b4a 100644
> --- a/drivers/scsi/be2iscsi/be_main.c
> +++ b/drivers/scsi/be2iscsi/be_main.c
> @@ -2540,13 +2540,13 @@ static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
> mem_arr++;
> }
> } while (alloc_size);
> - mem_descr->num_elements = j;
> - mem_descr->size_in_bytes = phba->mem_req[i];
> mem_descr->mem_array = kmalloc_array(j, sizeof(*mem_arr),
> GFP_KERNEL);
> if (!mem_descr->mem_array)
> goto free_mem;
>
> + mem_descr->num_elements = j;
> + mem_descr->size_in_bytes = phba->mem_req[i];
> memcpy(mem_descr->mem_array, mem_arr_orig,
> sizeof(struct mem_array) * j);
> mem_descr++;
> @@ -2554,7 +2554,11 @@ static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
> kfree(mem_arr_orig);
> return 0;
> free_mem:
> - mem_descr->num_elements = j;
> + if (j) {
> + mem_descr->num_elements = j;
> + mem_descr->mem_array = mem_arr_orig;
> + mem_arr_orig = NULL;
> + }
> while ((i) || (j)) {
> for (j = mem_descr->num_elements; j > 0; j--) {
> dma_free_coherent(&phba->pcidev->dev,
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-08-25 11:26 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-27 7:21 [PATCH -next] scsi: be2iscsi: fix null-ptr-deref in beiscsi_alloc_mem Ye Bin
2022-08-25 11:26 ` yebin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox