scsi: be2iscsi: fix cleanup on memory allocation failure
Subsystems (2)
PATCH 1/1 scsi: be2iscsi: fix cleanup on memory allocation failure
beiscsi_alloc_mem() builds each memory descriptor from DMA chunks recorded in the temporary mem_arr_orig array. After all chunks for the current descriptor have been allocated, the code allocates mem_descr->mem_array and copies the temporary descriptors into it.
If mem_descr->mem_array allocation fails, or if DMA allocation fails after some chunks have been allocated for the current descriptor, the error path records the current chunk count in mem_descr->num_elements and then dereferences mem_descr->mem_array. That pointer has not been allocated for the current descriptor, so the cleanup path can dereference NULL and also fail to release the current DMA chunks.
Release the current descriptor chunks from mem_arr_orig first, then walk backward over the descriptors that were fully initialized earlier.
--- drivers/scsi/be2iscsi/be_main.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/scsi/be2iscsi/be_main.c +13 -7
@@ -2549,8 +2549,18 @@ static int beiscsi_alloc_mem(struct beiscsi_hba *phba)kfree (mem_arr_orig);return 0 ; free_mem: - mem_descr->num_elements = j; -while ((i) || (j)) { +while (j) { + j--; + bus_add = mem_arr_orig[j].bus_address.u.a64.address; +dma_free_coherent (&phba->pcidev->dev, + mem_arr_orig[j].size, + mem_arr_orig[j].virtual_address, + bus_add); + } + +while (i) { + i--; + mem_descr--;for (j = mem_descr->num_elements; j >0 ; j--) { dma_free_coherent(&phba->pcidev->dev, mem_descr->mem_array[j - 1].size,
@@ -2560,11 +2570,7 @@ free_mem: mem_array[j -1 ]. bus_address.u.a64.address); } -if (i) { - i--; -kfree (mem_descr->mem_array); - mem_descr--; - } +kfree (mem_descr->mem_array); }kfree (mem_arr_orig);kfree (phba->init_mem);
Signature · 1 line
2.51.0