linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scsi: libsas: fix use-after-free bug in smp_execute_task_sg
@ 2022-09-20 14:42 Duoming Zhou
  2022-09-22 14:06 ` Jason Yan
  2022-09-25 16:56 ` Martin K. Petersen
  0 siblings, 2 replies; 4+ messages in thread
From: Duoming Zhou @ 2022-09-20 14:42 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-scsi, jejb, martin.petersen, kuba, john.garry, gregkh,
	davem, Duoming Zhou

When executing SMP task failed, the smp_execute_task_sg()
calls del_timer() to delete the "slow_task->timer". However,
if the timer handler sas_task_internal_timedout() is running,
the del_timer() in smp_execute_task_sg() will not stop it
and the UAF bug will happen. The process is shown below:

      (thread 1)               |        (thread 2)
smp_execute_task_sg()          | sas_task_internal_timedout()
 ...                           |
 del_timer()                   |
 ...                           |  ...
 sas_free_task(task)           |
  kfree(task->slow_task) //FREE|
                               |  task->slow_task->... //USE

Fix by calling del_timer_sync() in smp_execute_task_sg(),
which makes sure the timer handler have finished before
the "task->slow_task" is deallocated.

Fixes: 2908d778ab3e ("[SCSI] aic94xx: new driver")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
 drivers/scsi/libsas/sas_expander.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
index fa2209080cc..5ce25183010 100644
--- a/drivers/scsi/libsas/sas_expander.c
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -67,7 +67,7 @@ static int smp_execute_task_sg(struct domain_device *dev,
 		res = i->dft->lldd_execute_task(task, GFP_KERNEL);
 
 		if (res) {
-			del_timer(&task->slow_task->timer);
+			del_timer_sync(&task->slow_task->timer);
 			pr_notice("executing SMP task failed:%d\n", res);
 			break;
 		}
-- 
2.17.1


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

* Re: [PATCH] scsi: libsas: fix use-after-free bug in smp_execute_task_sg
  2022-09-20 14:42 [PATCH] scsi: libsas: fix use-after-free bug in smp_execute_task_sg Duoming Zhou
@ 2022-09-22 14:06 ` Jason Yan
  2022-09-24  2:46   ` duoming
  2022-09-25 16:56 ` Martin K. Petersen
  1 sibling, 1 reply; 4+ messages in thread
From: Jason Yan @ 2022-09-22 14:06 UTC (permalink / raw)
  To: Duoming Zhou, linux-kernel
  Cc: linux-scsi, jejb, martin.petersen, kuba, john.garry, gregkh, davem


On 2022/9/20 22:42, Duoming Zhou wrote:
> When executing SMP task failed, the smp_execute_task_sg()
> calls del_timer() to delete the "slow_task->timer". However,
> if the timer handler sas_task_internal_timedout() is running,
> the del_timer() in smp_execute_task_sg() will not stop it
> and the UAF bug will happen. The process is shown below:
> 
>        (thread 1)               |        (thread 2)
> smp_execute_task_sg()          | sas_task_internal_timedout()
>   ...                           |
>   del_timer()                   |
>   ...                           |  ...
>   sas_free_task(task)           |
>    kfree(task->slow_task) //FREE|
>                                 |  task->slow_task->... //USE
> 
> Fix by calling del_timer_sync() in smp_execute_task_sg(),
> which makes sure the timer handler have finished before
> the "task->slow_task" is deallocated.
> 
> Fixes: 2908d778ab3e ("[SCSI] aic94xx: new driver")
> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> ---
>   drivers/scsi/libsas/sas_expander.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
> index fa2209080cc..5ce25183010 100644
> --- a/drivers/scsi/libsas/sas_expander.c
> +++ b/drivers/scsi/libsas/sas_expander.c
> @@ -67,7 +67,7 @@ static int smp_execute_task_sg(struct domain_device *dev,
>   		res = i->dft->lldd_execute_task(task, GFP_KERNEL);
>   
>   		if (res) {
> -			del_timer(&task->slow_task->timer);
> +			del_timer_sync(&task->slow_task->timer);
>   			pr_notice("executing SMP task failed:%d\n", res);
>   			break;
>   		}
> 

The timer should be triggered 10 seconds later. I am curious why we stay
this long before del_timer().

However this change looks good to me.
Reviewed-by: Jason Yan <yanaijie@huawei.com>

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

* Re: [PATCH] scsi: libsas: fix use-after-free bug in smp_execute_task_sg
  2022-09-22 14:06 ` Jason Yan
@ 2022-09-24  2:46   ` duoming
  0 siblings, 0 replies; 4+ messages in thread
From: duoming @ 2022-09-24  2:46 UTC (permalink / raw)
  To: Jason Yan
  Cc: linux-kernel, linux-scsi, jejb, martin.petersen, kuba,
	john.garry, gregkh, davem

Hello,

On Thu, 22 Sep 2022 22:06:30 +0800 Jason Yan wrote:

> On 2022/9/20 22:42, Duoming Zhou wrote:
> > When executing SMP task failed, the smp_execute_task_sg()
> > calls del_timer() to delete the "slow_task->timer". However,
> > if the timer handler sas_task_internal_timedout() is running,
> > the del_timer() in smp_execute_task_sg() will not stop it
> > and the UAF bug will happen. The process is shown below:
> > 
> >        (thread 1)               |        (thread 2)
> > smp_execute_task_sg()          | sas_task_internal_timedout()
> >   ...                           |
> >   del_timer()                   |
> >   ...                           |  ...
> >   sas_free_task(task)           |
> >    kfree(task->slow_task) //FREE|
> >                                 |  task->slow_task->... //USE
> > 
> > Fix by calling del_timer_sync() in smp_execute_task_sg(),
> > which makes sure the timer handler have finished before
> > the "task->slow_task" is deallocated.
> > 
> > Fixes: 2908d778ab3e ("[SCSI] aic94xx: new driver")
> > Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> > ---
> >   drivers/scsi/libsas/sas_expander.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
> > index fa2209080cc..5ce25183010 100644
> > --- a/drivers/scsi/libsas/sas_expander.c
> > +++ b/drivers/scsi/libsas/sas_expander.c
> > @@ -67,7 +67,7 @@ static int smp_execute_task_sg(struct domain_device *dev,
> >   		res = i->dft->lldd_execute_task(task, GFP_KERNEL);
> >   
> >   		if (res) {
> > -			del_timer(&task->slow_task->timer);
> > +			del_timer_sync(&task->slow_task->timer);
> >   			pr_notice("executing SMP task failed:%d\n", res);
> >   			break;
> >   		}
> > 
> 
> The timer should be triggered 10 seconds later. I am curious why we stay
> this long before del_timer().

I think the hacker may create a large number of hardware interrupts to
increase the delayed time.

> However this change looks good to me.
> Reviewed-by: Jason Yan <yanaijie@huawei.com>

Thank you!

Best regards,
Duoming Zhou

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

* Re: [PATCH] scsi: libsas: fix use-after-free bug in smp_execute_task_sg
  2022-09-20 14:42 [PATCH] scsi: libsas: fix use-after-free bug in smp_execute_task_sg Duoming Zhou
  2022-09-22 14:06 ` Jason Yan
@ 2022-09-25 16:56 ` Martin K. Petersen
  1 sibling, 0 replies; 4+ messages in thread
From: Martin K. Petersen @ 2022-09-25 16:56 UTC (permalink / raw)
  To: Duoming Zhou
  Cc: linux-kernel, linux-scsi, jejb, martin.petersen, kuba,
	john.garry, gregkh, davem


Duoming,

> When executing SMP task failed, the smp_execute_task_sg() calls
> del_timer() to delete the "slow_task->timer". However, if the timer
> handler sas_task_internal_timedout() is running, the del_timer() in
> smp_execute_task_sg() will not stop it and the UAF bug will happen.

Applied to 6.1/scsi-staging, thanks!

-- 
Martin K. Petersen	Oracle Linux Engineering

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

end of thread, other threads:[~2022-09-25 16:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-20 14:42 [PATCH] scsi: libsas: fix use-after-free bug in smp_execute_task_sg Duoming Zhou
2022-09-22 14:06 ` Jason Yan
2022-09-24  2:46   ` duoming
2022-09-25 16:56 ` Martin K. Petersen

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).