IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs()
Subsystems (2)
PATCH 1/1 IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs()
kmalloc_array() is called to allocate memory for tx->descp. If it fails, the function __sdma_txclean() is called:
__sdma_txclean(dd, tx);
However, in the function __sdma_txclean(), tx-descp is dereferenced if tx->num_desc is not zero:
sdma_unmap_desc(dd, &tx->descp[0]);
To fix this possible null-pointer dereference, assign 0 to tx->num_desc if kmalloc_array() returns NULL.
--- drivers/infiniband/hw/hfi1/sdma.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/infiniband/hw/hfi1/sdma.c +3 -1
@@ -3079,8 +3079,10 @@ static int _extend_sdma_tx_descs(struct hfi1_devdata *dd, struct sdma_txreq *tx)MAX_DESC ,sizeof (struct sdma_desc ), GFP_ATOMIC); -if (!tx->descp) +if (!tx->descp) { + tx->num_desc =0 ; goto enomem; + }/* reserve last descriptor for coalescing */ tx->desc_limit =MAX_DESC -1 ;
Signature · 1 line
2.25.1
Quoted 37 lines
Subject: [PATCH] IB/hfi1: Fix possible null-pointer dereference in
_extend_sdma_tx_descs()
kmalloc_array()is called to allocate memory fortx->descp. If it fails, the function__sdma_txclean()is called:
__sdma_txclean(dd, tx);However, in the function
__sdma_txclean(), tx-descp is dereferenced iftx->num_descis not zero:
sdma_unmap_desc(dd, &tx->descp[0]);To fix this possible null-pointer dereference, assign 0 to
tx->num_descifkmalloc_array()returns NULL.Reported-by TOTE Robot <[email protected]>Signed-off-by Tuo Li <[email protected]>--- drivers/infiniband/hw/hfi1/sdma.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/infiniband/hw/hfi1/sdma.c +3
-1@@ -3079,8 +3079,10 @@ static int _extend_sdma_tx_descs(struct hfi1_devdata *dd, struct sdma_txreq *tx)MAX_DESC , sizeof(struct sdma_desc), GFP_ATOMIC); -if (!tx->descp) +if (!tx->descp) { + tx->num_desc =0 ; goto enomem; + }/* reserve last descriptor for coalescing */
Quoted 1 line
tx->desc_limit=MAX_DESC- 1;
Thanks for seeing an issue here, but the correct solution is to store descp locally first.
Once the allocation is shown to be non-NULL, then store to tx-descp:
drivers/infiniband/hw/hfi1/sdma.c +6 -5
@@ -3055,6 +3055,7 @@ static void __sdma_process_event(struct sdma_engine *sde,static int _extend_sdma_tx_descs (struct hfi1_devdata *dd,struct sdma_txreq *tx) {int i; +struct sdma_desc *descp;/* Handle last descriptor */ if (unlikely ((tx->num_desc == (MAX_DESC -1 )))) {
@@ -3075,12 +3076,12 @@ static int _extend_sdma_tx_descs(struct hfi1_devdata *dd, struct sdma_txreq *tx)if (unlikely (tx->num_desc ==MAX_DESC )) goto enomem; - tx->descp =kmalloc_array ( -MAX_DESC , -sizeof (struct sdma_desc ), -GFP_ATOMIC ); -if (!tx->descp) + descp =kmalloc_array (MAX_DESC , +sizeof (struct sdma_desc ), +GFP_ATOMIC ); +if (!descp) goto enomem; + tx->descp = descp;/* reserve last descriptor for coalescing */ tx->desc_limit =MAX_DESC -1 ;