Skip to contentSkip to conversation

IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs()

PATCH Superseded Tuo Li 2 participants 2 messages Aug 6, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tuo Li

author
#0 lore raw

PATCH v2 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 the return value of kmalloc_array() to a local variable descp, and then assign it to tx->descp if it is not NULL. Otherwise, go to enomem.

---
v2:
* Assign the return value of kmalloc_array() to a local variable and then check it instead of assigning 0 to tx->num_desc when memory allocation fails.
Thank Mike Marciniszyn for helpful advice.

---
 drivers/infiniband/hw/hfi1/sdma.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/infiniband/hw/hfi1/sdma.c +6 -5
index eb15c310d63d..6ca8d065f44e 100644
@@ -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;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 20 lines · Tuo Li

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 the return value of kmalloc_array() to a local variable descp, and then assign it to tx->descp if it is not NULL. Otherwise, go to enomem.

---
v2:
* Assign the return value of kmalloc_array() to a local variable and then check it instead of assigning 0 to tx->num_desc when memory allocation fails.
Thank Mike Marciniszyn for helpful advice.

Quoted 3 lines
---
 drivers/infiniband/hw/hfi1/sdma.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Fixes line?

Jason