public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rdmavt: avoid NULL pointer dereference in rvt_qp_exit()
@ 2022-10-17 14:26 Natalia Petrova
  2022-10-18  8:41 ` Leon Romanovsky
  0 siblings, 1 reply; 5+ messages in thread
From: Natalia Petrova @ 2022-10-17 14:26 UTC (permalink / raw)
  To: Dennis Dalessandro, Jason Gunthorpe, Leon Romanovsky
  Cc: Natalia Petrova, linux-rdma, linux-kernel, ldv-project,
	Alexey Khoroshilov

rvt_qp_exit() checks 'rdi->qp_dev' for NULL, but the pointer is
dereferenced before that in rvt_free_all_qps().

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: f92e48718889 ("IB/rdmavt: Reset all QPs when the device is shut
down")
Signed-off-by: Natalia Petrova <n.petrova@fintech.ru>
Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
---
 drivers/infiniband/sw/rdmavt/qp.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/infiniband/sw/rdmavt/qp.c b/drivers/infiniband/sw/rdmavt/qp.c
index 3acab569fbb9..06e755975f61 100644
--- a/drivers/infiniband/sw/rdmavt/qp.c
+++ b/drivers/infiniband/sw/rdmavt/qp.c
@@ -459,13 +459,16 @@ static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
  */
 void rvt_qp_exit(struct rvt_dev_info *rdi)
 {
-	u32 qps_inuse = rvt_free_all_qps(rdi);
+	u32 qps_inuse = 0;
+
+	if (!rdi->qp_dev)
+		return;
+
+	qps_inuse = rvt_free_all_qps(rdi);
 
 	if (qps_inuse)
 		rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
 			   qps_inuse);
-	if (!rdi->qp_dev)
-		return;
 
 	kfree(rdi->qp_dev->qp_table);
 	free_qpn_table(&rdi->qp_dev->qpn_table);
-- 
2.34.1


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

* Re: [PATCH] rdmavt: avoid NULL pointer dereference in rvt_qp_exit()
  2022-10-17 14:26 [PATCH] rdmavt: avoid NULL pointer dereference in rvt_qp_exit() Natalia Petrova
@ 2022-10-18  8:41 ` Leon Romanovsky
  2022-10-18 14:24   ` Dennis Dalessandro
  0 siblings, 1 reply; 5+ messages in thread
From: Leon Romanovsky @ 2022-10-18  8:41 UTC (permalink / raw)
  To: Natalia Petrova
  Cc: Dennis Dalessandro, Jason Gunthorpe, linux-rdma, linux-kernel,
	ldv-project, Alexey Khoroshilov

On Mon, Oct 17, 2022 at 05:26:52PM +0300, Natalia Petrova wrote:
> rvt_qp_exit() checks 'rdi->qp_dev' for NULL, but the pointer is
> dereferenced before that in rvt_free_all_qps().
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Fixes: f92e48718889 ("IB/rdmavt: Reset all QPs when the device is shut
> down")

Please never break fixes line.

> Signed-off-by: Natalia Petrova <n.petrova@fintech.ru>
> Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
> ---
>  drivers/infiniband/sw/rdmavt/qp.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/infiniband/sw/rdmavt/qp.c b/drivers/infiniband/sw/rdmavt/qp.c
> index 3acab569fbb9..06e755975f61 100644
> --- a/drivers/infiniband/sw/rdmavt/qp.c
> +++ b/drivers/infiniband/sw/rdmavt/qp.c
> @@ -459,13 +459,16 @@ static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
>   */
>  void rvt_qp_exit(struct rvt_dev_info *rdi)
>  {
> -	u32 qps_inuse = rvt_free_all_qps(rdi);
> +	u32 qps_inuse = 0;
> +
> +	if (!rdi->qp_dev)
> +		return;
> +
> +	qps_inuse = rvt_free_all_qps(rdi);

These lines are not needed.

>  
>  	if (qps_inuse)
>  		rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
>  			   qps_inuse);
> -	if (!rdi->qp_dev)
> -		return;

It is enough to delete these two lines. At this stage, rdi->qp_dev always
exists as it was created in rvt_register_device().

Thanks

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

* Re: [PATCH] rdmavt: avoid NULL pointer dereference in rvt_qp_exit()
  2022-10-18  8:41 ` Leon Romanovsky
@ 2022-10-18 14:24   ` Dennis Dalessandro
  2023-03-03 12:44     ` [PATCH v2] rdmavt: delete unnecessary NULL check Natalia Petrova
  0 siblings, 1 reply; 5+ messages in thread
From: Dennis Dalessandro @ 2022-10-18 14:24 UTC (permalink / raw)
  To: Leon Romanovsky, Natalia Petrova
  Cc: Jason Gunthorpe, linux-rdma, linux-kernel, ldv-project,
	Alexey Khoroshilov

On 10/18/22 4:41 AM, Leon Romanovsky wrote:
> On Mon, Oct 17, 2022 at 05:26:52PM +0300, Natalia Petrova wrote:
>> rvt_qp_exit() checks 'rdi->qp_dev' for NULL, but the pointer is
>> dereferenced before that in rvt_free_all_qps().
>>
>> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>>
>> Fixes: f92e48718889 ("IB/rdmavt: Reset all QPs when the device is shut
>> down")
> 
> Please never break fixes line.
> 
>> Signed-off-by: Natalia Petrova <n.petrova@fintech.ru>
>> Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
>> ---
>>  drivers/infiniband/sw/rdmavt/qp.c | 9 ++++++---
>>  1 file changed, 6 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/infiniband/sw/rdmavt/qp.c b/drivers/infiniband/sw/rdmavt/qp.c
>> index 3acab569fbb9..06e755975f61 100644
>> --- a/drivers/infiniband/sw/rdmavt/qp.c
>> +++ b/drivers/infiniband/sw/rdmavt/qp.c
>> @@ -459,13 +459,16 @@ static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
>>   */
>>  void rvt_qp_exit(struct rvt_dev_info *rdi)
>>  {
>> -	u32 qps_inuse = rvt_free_all_qps(rdi);
>> +	u32 qps_inuse = 0;
>> +
>> +	if (!rdi->qp_dev)
>> +		return;
>> +
>> +	qps_inuse = rvt_free_all_qps(rdi);
> 
> These lines are not needed.
> 
>>  
>>  	if (qps_inuse)
>>  		rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
>>  			   qps_inuse);
>> -	if (!rdi->qp_dev)
>> -		return;
> 
> It is enough to delete these two lines. At this stage, rdi->qp_dev always
> exists as it was created in rvt_register_device().
> 

Agree with Leon here. qp_dev is created in rvt_register_device which will fail
if the qp dev allocation fails in rvt_driver_qp_init().

-Denny

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

* [PATCH v2] rdmavt: delete unnecessary NULL check
  2022-10-18 14:24   ` Dennis Dalessandro
@ 2023-03-03 12:44     ` Natalia Petrova
  2023-03-14  9:26       ` Leon Romanovsky
  0 siblings, 1 reply; 5+ messages in thread
From: Natalia Petrova @ 2023-03-03 12:44 UTC (permalink / raw)
  To: Dennis Dalessandro
  Cc: Natalia Petrova, Jason Gunthorpe, Leon Romanovsky, linux-rdma,
	linux-kernel, ldv-project

There is no need to check 'rdi->qp_dev' for NULL. The field 'qp_dev'
is created in rvt_register_device() which will fail if the 'qp_dev'
allocation fails in rvt_driver_qp_init(). Overwise this pointer
doesn't changed and passed to rvt_qp_exit() by the next step.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 0acb0cc7ecc1 ("IB/rdmavt: Initialize and teardown of qpn table")
Signed-off-by: Natalia Petrova <n.petrova@fintech.ru>
---
v2: The remark about non-null value of 'rdi->qp_dev' by Leon Romanovsky <leon@kernel.org>
and Dennis Dalessandro <dennis.dalessandro@cornelisnetworks.com> was taken into account.
 drivers/infiniband/sw/rdmavt/qp.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/drivers/infiniband/sw/rdmavt/qp.c b/drivers/infiniband/sw/rdmavt/qp.c
index 3acab569fbb9..2bdc4486c3da 100644
--- a/drivers/infiniband/sw/rdmavt/qp.c
+++ b/drivers/infiniband/sw/rdmavt/qp.c
@@ -464,8 +464,6 @@ void rvt_qp_exit(struct rvt_dev_info *rdi)
 	if (qps_inuse)
 		rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
 			   qps_inuse);
-	if (!rdi->qp_dev)
-		return;
 
 	kfree(rdi->qp_dev->qp_table);
 	free_qpn_table(&rdi->qp_dev->qpn_table);
-- 
2.34.1


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

* Re: [PATCH v2] rdmavt: delete unnecessary NULL check
  2023-03-03 12:44     ` [PATCH v2] rdmavt: delete unnecessary NULL check Natalia Petrova
@ 2023-03-14  9:26       ` Leon Romanovsky
  0 siblings, 0 replies; 5+ messages in thread
From: Leon Romanovsky @ 2023-03-14  9:26 UTC (permalink / raw)
  To: Dennis Dalessandro, Natalia Petrova
  Cc: Jason Gunthorpe, linux-rdma, linux-kernel, ldv-project


On Fri, 03 Mar 2023 15:44:08 +0300, Natalia Petrova wrote:
> There is no need to check 'rdi->qp_dev' for NULL. The field 'qp_dev'
> is created in rvt_register_device() which will fail if the 'qp_dev'
> allocation fails in rvt_driver_qp_init(). Overwise this pointer
> doesn't changed and passed to rvt_qp_exit() by the next step.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> [...]

Applied, thanks!

[1/1] rdmavt: delete unnecessary NULL check
      https://git.kernel.org/rdma/rdma/c/9b3366ec12f0d7

Best regards,
-- 
Leon Romanovsky <leon@kernel.org>

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

end of thread, other threads:[~2023-03-14  9:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-17 14:26 [PATCH] rdmavt: avoid NULL pointer dereference in rvt_qp_exit() Natalia Petrova
2022-10-18  8:41 ` Leon Romanovsky
2022-10-18 14:24   ` Dennis Dalessandro
2023-03-03 12:44     ` [PATCH v2] rdmavt: delete unnecessary NULL check Natalia Petrova
2023-03-14  9:26       ` Leon Romanovsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox