[PATCH net v3] atm: fore200e: fix use-after-free in tasklets during device removal

Duoming Zhou posted 1 patch 23 hours ago
drivers/atm/fore200e.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
[PATCH net v3] atm: fore200e: fix use-after-free in tasklets during device removal
Posted by Duoming Zhou 23 hours ago
When the PCA-200E or SBA-200E adapter is being detached, the fore200e
is deallocated. However, the tx_tasklet or rx_tasklet may still be running
or pending, leading to use-after-free bug when the already freed fore200e
is accessed again in fore200e_tx_tasklet() or fore200e_rx_tasklet().

One of the race conditions can occur as follows:

CPU 0 (cleanup)           | CPU 1 (tasklet)
fore200e_pca_remove_one() | fore200e_interrupt()
  fore200e_shutdown()     |   tasklet_schedule()
    kfree(fore200e)       | fore200e_tx_tasklet()
                          |   fore200e-> // UAF

Fix this by ensuring tx_tasklet or rx_tasklet is properly canceled before
the fore200e is released. Add tasklet_kill() in fore200e_shutdown() to
synchronize with any pending or running tasklets. Moreover, since
fore200e_reset() could prevent further interrupts or data transfers,
the tasklet_kill() should be placed after fore200e_reset() to prevent
the tasklet from being rescheduled in fore200e_interrupt(). Finally,
it only needs to do tasklet_kill() when the fore200e state is greater
than or equal to FORE200E_STATE_IRQ, since tasklets are uninitialized
in earlier states.

This bug was identified through static analysis.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@kernel.org
Suggested-by: Jijie Shao <shaojijie@huawei.com>
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
Changes in v3:
  - Only do tasklet_kill() when state >= FORE200E_STATE_IRQ.
  - Make the commit messages clearer.

 drivers/atm/fore200e.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/atm/fore200e.c b/drivers/atm/fore200e.c
index f62e3857144..eb386ec6b5e 100644
--- a/drivers/atm/fore200e.c
+++ b/drivers/atm/fore200e.c
@@ -363,7 +363,12 @@ fore200e_shutdown(struct fore200e* fore200e)
 	/* first, reset the board to prevent further interrupts or data transfers */
 	fore200e_reset(fore200e, 0);
     }
-    
+#ifdef FORE200E_USE_TASKLET
+	if (fore200e->state >= FORE200E_STATE_IRQ) {
+		tasklet_kill(&fore200e->tx_tasklet);
+		tasklet_kill(&fore200e->rx_tasklet);
+	}
+#endif
     /* then, release all allocated resources */
     switch(fore200e->state) {
 
-- 
2.34.1
Re: [PATCH net v3] atm: fore200e: fix use-after-free in tasklets during device removal
Posted by kernel test robot 5 hours ago
Hi Duoming,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Duoming-Zhou/atm-fore200e-fix-use-after-free-in-tasklets-during-device-removal/20260209-174706
base:   net/main
patch link:    https://lore.kernel.org/r/20260209094512.33847-1-duoming%40zju.edu.cn
patch subject: [PATCH net v3] atm: fore200e: fix use-after-free in tasklets during device removal
config: i386-randconfig-141-20260210 (https://download.01.org/0day-ci/archive/20260210/202602101146.NCGz3JHc-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
smatch version: v0.5.0-8994-gd50c5a4c

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202602101146.NCGz3JHc-lkp@intel.com/

smatch warnings:
drivers/atm/fore200e.c:367 fore200e_shutdown() warn: inconsistent indenting

vim +367 drivers/atm/fore200e.c

   353	
   354	
   355	static void
   356	fore200e_shutdown(struct fore200e* fore200e)
   357	{
   358	    printk(FORE200E "removing device %s at 0x%lx, IRQ %s\n",
   359		   fore200e->name, fore200e->phys_base, 
   360		   fore200e_irq_itoa(fore200e->irq));
   361	    
   362	    if (fore200e->state > FORE200E_STATE_RESET) {
   363		/* first, reset the board to prevent further interrupts or data transfers */
   364		fore200e_reset(fore200e, 0);
   365	    }
   366	#ifdef FORE200E_USE_TASKLET
 > 367		if (fore200e->state >= FORE200E_STATE_IRQ) {
   368			tasklet_kill(&fore200e->tx_tasklet);
   369			tasklet_kill(&fore200e->rx_tasklet);
   370		}
   371	#endif
   372	    /* then, release all allocated resources */
   373	    switch(fore200e->state) {
   374	
   375	    case FORE200E_STATE_COMPLETE:
   376		kfree(fore200e->stats);
   377	
   378		fallthrough;
   379	    case FORE200E_STATE_IRQ:
   380		free_irq(fore200e->irq, fore200e->atm_dev);
   381	
   382		fallthrough;
   383	    case FORE200E_STATE_ALLOC_BUF:
   384		fore200e_free_rx_buf(fore200e);
   385	
   386		fallthrough;
   387	    case FORE200E_STATE_INIT_BSQ:
   388		fore200e_uninit_bs_queue(fore200e);
   389	
   390		fallthrough;
   391	    case FORE200E_STATE_INIT_RXQ:
   392		fore200e_dma_chunk_free(fore200e, &fore200e->host_rxq.status);
   393		fore200e_dma_chunk_free(fore200e, &fore200e->host_rxq.rpd);
   394	
   395		fallthrough;
   396	    case FORE200E_STATE_INIT_TXQ:
   397		fore200e_dma_chunk_free(fore200e, &fore200e->host_txq.status);
   398		fore200e_dma_chunk_free(fore200e, &fore200e->host_txq.tpd);
   399	
   400		fallthrough;
   401	    case FORE200E_STATE_INIT_CMDQ:
   402		fore200e_dma_chunk_free(fore200e, &fore200e->host_cmdq.status);
   403	
   404		fallthrough;
   405	    case FORE200E_STATE_INITIALIZE:
   406		/* nothing to do for that state */
   407	
   408	    case FORE200E_STATE_START_FW:
   409		/* nothing to do for that state */
   410	
   411	    case FORE200E_STATE_RESET:
   412		/* nothing to do for that state */
   413	
   414	    case FORE200E_STATE_MAP:
   415		fore200e->bus->unmap(fore200e);
   416	
   417		fallthrough;
   418	    case FORE200E_STATE_CONFIGURE:
   419		/* nothing to do for that state */
   420	
   421	    case FORE200E_STATE_REGISTER:
   422		/* XXX shouldn't we *start* by deregistering the device? */
   423		atm_dev_deregister(fore200e->atm_dev);
   424	
   425		fallthrough;
   426	    case FORE200E_STATE_BLANK:
   427		/* nothing to do for that state */
   428		break;
   429	    }
   430	}
   431	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH net v3] atm: fore200e: fix use-after-free in tasklets during device removal
Posted by Jijie Shao 7 hours ago
on 2026/2/9 17:45, Duoming Zhou wrote:
> When the PCA-200E or SBA-200E adapter is being detached, the fore200e
> is deallocated. However, the tx_tasklet or rx_tasklet may still be running
> or pending, leading to use-after-free bug when the already freed fore200e
> is accessed again in fore200e_tx_tasklet() or fore200e_rx_tasklet().
>
> One of the race conditions can occur as follows:
>
> CPU 0 (cleanup)           | CPU 1 (tasklet)
> fore200e_pca_remove_one() | fore200e_interrupt()
>    fore200e_shutdown()     |   tasklet_schedule()
>      kfree(fore200e)       | fore200e_tx_tasklet()
>                            |   fore200e-> // UAF
>
> Fix this by ensuring tx_tasklet or rx_tasklet is properly canceled before
> the fore200e is released. Add tasklet_kill() in fore200e_shutdown() to
> synchronize with any pending or running tasklets. Moreover, since
> fore200e_reset() could prevent further interrupts or data transfers,
> the tasklet_kill() should be placed after fore200e_reset() to prevent
> the tasklet from being rescheduled in fore200e_interrupt(). Finally,
> it only needs to do tasklet_kill() when the fore200e state is greater
> than or equal to FORE200E_STATE_IRQ, since tasklets are uninitialized
> in earlier states.
>
> This bug was identified through static analysis.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@kernel.org
> Suggested-by: Jijie Shao <shaojijie@huawei.com>
> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> ---
> Changes in v3:
>    - Only do tasklet_kill() when state >= FORE200E_STATE_IRQ.
>    - Make the commit messages clearer.
>
>   drivers/atm/fore200e.c | 7 ++++++-
>   1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/atm/fore200e.c b/drivers/atm/fore200e.c
> index f62e3857144..eb386ec6b5e 100644
> --- a/drivers/atm/fore200e.c
> +++ b/drivers/atm/fore200e.c
> @@ -363,7 +363,12 @@ fore200e_shutdown(struct fore200e* fore200e)
>   	/* first, reset the board to prevent further interrupts or data transfers */
>   	fore200e_reset(fore200e, 0);
>       }
> -
> +#ifdef FORE200E_USE_TASKLET
> +	if (fore200e->state >= FORE200E_STATE_IRQ) {
> +		tasklet_kill(&fore200e->tx_tasklet);
> +		tasklet_kill(&fore200e->rx_tasklet);
> +	}
> +#endif

The code appears to be misaligned.
I see that in the original driver code, the first level of indentation uses 4 spaces,
and the second level uses a TAB (8-character width).
In your code, the first level uses a TAB, and the second level uses two TABs.
Of course, your code complies with the current coding standards.

Additionally, I think a better modification would be to do tasklet_kill()
in the "switch(fore200e->state)-case FORE200E_STATE_IRQ" branch,
because "fore200e->tx_tasklet" is initialized together with "fore200e->irq".

Of course, your current change also looks fine, so
Reviewed-by: Jijie Shao <shaojijie@huawei.com>
Re: [PATCH net v3] atm: fore200e: fix use-after-free in tasklets during device removal
Posted by duoming@zju.edu.cn 3 hours ago
On Tue, 10 Feb 2026 09:46:53 +0800 Jijie Shao wrote:
> > When the PCA-200E or SBA-200E adapter is being detached, the fore200e
> > is deallocated. However, the tx_tasklet or rx_tasklet may still be running
> > or pending, leading to use-after-free bug when the already freed fore200e
> > is accessed again in fore200e_tx_tasklet() or fore200e_rx_tasklet().
> >
> > One of the race conditions can occur as follows:
> >
> > CPU 0 (cleanup)           | CPU 1 (tasklet)
> > fore200e_pca_remove_one() | fore200e_interrupt()
> >    fore200e_shutdown()     |   tasklet_schedule()
> >      kfree(fore200e)       | fore200e_tx_tasklet()
> >                            |   fore200e-> // UAF
> >
> > Fix this by ensuring tx_tasklet or rx_tasklet is properly canceled before
> > the fore200e is released. Add tasklet_kill() in fore200e_shutdown() to
> > synchronize with any pending or running tasklets. Moreover, since
> > fore200e_reset() could prevent further interrupts or data transfers,
> > the tasklet_kill() should be placed after fore200e_reset() to prevent
> > the tasklet from being rescheduled in fore200e_interrupt(). Finally,
> > it only needs to do tasklet_kill() when the fore200e state is greater
> > than or equal to FORE200E_STATE_IRQ, since tasklets are uninitialized
> > in earlier states.
> >
> > This bug was identified through static analysis.
> >
> > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > Cc: stable@kernel.org
> > Suggested-by: Jijie Shao <shaojijie@huawei.com>
> > Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> > ---
> > Changes in v3:
> >    - Only do tasklet_kill() when state >= FORE200E_STATE_IRQ.
> >    - Make the commit messages clearer.
> >
> >   drivers/atm/fore200e.c | 7 ++++++-
> >   1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/atm/fore200e.c b/drivers/atm/fore200e.c
> > index f62e3857144..eb386ec6b5e 100644
> > --- a/drivers/atm/fore200e.c
> > +++ b/drivers/atm/fore200e.c
> > @@ -363,7 +363,12 @@ fore200e_shutdown(struct fore200e* fore200e)
> >   	/* first, reset the board to prevent further interrupts or data transfers */
> >   	fore200e_reset(fore200e, 0);
> >       }
> > -
> > +#ifdef FORE200E_USE_TASKLET
> > +	if (fore200e->state >= FORE200E_STATE_IRQ) {
> > +		tasklet_kill(&fore200e->tx_tasklet);
> > +		tasklet_kill(&fore200e->rx_tasklet);
> > +	}
> > +#endif
> 
> The code appears to be misaligned.
> I see that in the original driver code, the first level of indentation uses 4 spaces,
> and the second level uses a TAB (8-character width).
> In your code, the first level uses a TAB, and the second level uses two TABs.
> Of course, your code complies with the current coding standards.
> 
> Additionally, I think a better modification would be to do tasklet_kill()
> in the "switch(fore200e->state)-case FORE200E_STATE_IRQ" branch,
> because "fore200e->tx_tasklet" is initialized together with "fore200e->irq".

Thank you for your suggestions! I will adjust the tasklet_kill() into the
"case FORE200E_STATE_IRQ" branch. The details are as follows:

diff --git a/drivers/atm/fore200e.c b/drivers/atm/fore200e.c
index f62e3857144..fec081db36d 100644
--- a/drivers/atm/fore200e.c
+++ b/drivers/atm/fore200e.c
@@ -373,6 +373,10 @@ fore200e_shutdown(struct fore200e* fore200e)
        fallthrough;
     case FORE200E_STATE_IRQ:
        free_irq(fore200e->irq, fore200e->atm_dev);
+#ifdef FORE200E_USE_TASKLET
+       tasklet_kill(&fore200e->tx_tasklet);
+       tasklet_kill(&fore200e->rx_tasklet);
+#endif

Best regards,
Duoming Zhou