LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH v3] IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs()
@ 2021-08-06 13:30 Tuo Li
  2021-08-09 13:15 ` Marciniszyn, Mike
  2021-08-19 14:37 ` Jason Gunthorpe
  0 siblings, 2 replies; 3+ messages in thread
From: Tuo Li @ 2021-08-06 13:30 UTC (permalink / raw)
  To: mike.marciniszyn, dennis.dalessandro, dledford, jgg
  Cc: linux-rdma, linux-kernel, baijiaju1990, Tuo Li, TOTE Robot

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.

Fixes: 7724105686e7 ("IB/hfi1: add driver files")
Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
Signed-off-by: Tuo Li <islituo@gmail.com>
---
v3:
* Add fixes line.
  Thank Jason Gunthorpe for helpful advice.
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(-)

diff --git a/drivers/infiniband/hw/hfi1/sdma.c b/drivers/infiniband/hw/hfi1/sdma.c
index eb15c310d63d..6ca8d065f44e 100644
--- a/drivers/infiniband/hw/hfi1/sdma.c
+++ b/drivers/infiniband/hw/hfi1/sdma.c
@@ -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;
-- 
2.25.1


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

* RE: [PATCH v3] IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs()
  2021-08-06 13:30 [PATCH v3] IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs() Tuo Li
@ 2021-08-09 13:15 ` Marciniszyn, Mike
  2021-08-19 14:37 ` Jason Gunthorpe
  1 sibling, 0 replies; 3+ messages in thread
From: Marciniszyn, Mike @ 2021-08-09 13:15 UTC (permalink / raw)
  To: Tuo Li, Dalessandro, Dennis, dledford, jgg
  Cc: linux-rdma, linux-kernel, baijiaju1990, TOTE Robot

> Fixes: 7724105686e7 ("IB/hfi1: add driver files")
> Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
> Signed-off-by: Tuo Li <islituo@gmail.com>
> ---
> v3:
> * Add fixes line.
>   Thank Jason Gunthorpe for helpful advice.
> 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.

Thanks!

I'm curious, did you find this with some fault injection testing?

Tested-by: Mike Marciniszyn <mike.marciniszyn@cornelisnetworks.com>
Acked-by: Mike Marciniszyn <mike.marciniszyn@cornelisnetworks.com>

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

* Re: [PATCH v3] IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs()
  2021-08-06 13:30 [PATCH v3] IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs() Tuo Li
  2021-08-09 13:15 ` Marciniszyn, Mike
@ 2021-08-19 14:37 ` Jason Gunthorpe
  1 sibling, 0 replies; 3+ messages in thread
From: Jason Gunthorpe @ 2021-08-19 14:37 UTC (permalink / raw)
  To: Tuo Li
  Cc: mike.marciniszyn, dennis.dalessandro, dledford, linux-rdma,
	linux-kernel, baijiaju1990, TOTE Robot

On Fri, Aug 06, 2021 at 06:30:29AM -0700, Tuo Li wrote:
> 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.
> 
> Fixes: 7724105686e7 ("IB/hfi1: add driver files")
> Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
> Signed-off-by: Tuo Li <islituo@gmail.com>
> Tested-by: Mike Marciniszyn <mike.marciniszyn@cornelisnetworks.com>
> Acked-by: Mike Marciniszyn <mike.marciniszyn@cornelisnetworks.com>
> ---
> v3:
> * Add fixes line.
>   Thank Jason Gunthorpe for helpful advice.
> 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(-)

I fixed the wonky code formatting and applied to for-rc, thanks

Jason

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

end of thread, other threads:[~2021-08-19 14:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-06 13:30 [PATCH v3] IB/hfi1: Fix possible null-pointer dereference in _extend_sdma_tx_descs() Tuo Li
2021-08-09 13:15 ` Marciniszyn, Mike
2021-08-19 14:37 ` Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).