Skip to contentSkip to conversation

drivers/spi/mpc52xx_psc_spi.c, potential use after free

DISCUSSION Sebastian Siewior 3 participants 4 messages Jan 7, 2008 – Jan 15, 2008 2 messages not in the archive SPI

lore
Share & tools
lore
#0 lore raw

Hello Dragos,

Your mpc52xx_psc_spi_do_remove() from current git:

| static int __exit mpc52xx_psc_spi_do_remove(struct device *dev)
| {
|         struct spi_master *master = dev_get_drvdata(dev);
|         struct mpc52xx_psc_spi *mps = spi_master_get_devdata(master);
| 
|         flush_workqueue(mps->workqueue);
|         destroy_workqueue(mps->workqueue);
|         spi_unregister_master(master);
|         free_irq(mps->irq, mps);
|         if (mps->psc)
|                 iounmap(mps->psc);
| 
|         return 0;
| }

It looks to me that spi_unregister_master() frees your mps struct and the following free_irq() and iounmap() touches deallocated memory. Might this be correct?

Sebastian

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

A message that is not in the archive (sent off-list or not yet received): 20080107143025.GA21720-N26foeg2n7CSQxDm3FYtNwe+0vtP5+jfMa5aj3AfGnc@public.gmane.org

#2 lore raw

Fix mpc52xx_psc_spi_do_remove() use of drvdata after spi master unregister.

---

 mpc52xx_psc_spi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/spi/mpc52xx_psc_spi.c
index 7051e6c..a4463ba 100644

drivers/spi/mpc52xx_psc_spi.c +0 -0
@@ -534,10 +534,10 @@ static int __exit mpc52xx_psc_spi_do_remove(struct

device *dev)

        flush_workqueue(mps->workqueue);
        destroy_workqueue(mps->workqueue);
-       spi_unregister_master(master);
        free_irq(mps->irq, mps);
        if (mps->psc)
                iounmap(mps->psc);

+ spi_unregister_master(master);

        return 0;

}

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace

A message that is not in the archive (sent off-list or not yet received): 1199974746.3075.14.camel-8qF8gS5z7PwcZLk9x1mbI9BPR1lH4CV8@public.gmane.org

#4 lore raw
Quoted 4 lines · * Dragos Carp | 2008-01-10 15:19:06 [+0100]:

Fix mpc52xx_psc_spi_do_remove() use of drvdata after spi master unregister.

Acked-by Sebastian Siewior <[email protected]>

Quoted 22 lines

---

mpc52xx_psc_spi.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/mpc52xx_psc_spi.c +1 -1
index 7051e6c..a4463ba 100644
@@ -534,10 +534,10 @@ static int __exit mpc52xx_psc_spi_do_remove(struct
 device *dev)
 
       flush_workqueue(mps->workqueue);
       destroy_workqueue(mps->workqueue);
-       spi_unregister_master(master);
       free_irq(mps->irq, mps);
       if (mps->psc)
               iounmap(mps->psc);
+       spi_unregister_master(master);
 
       return 0;
Quoted 1 line

}

This should work since you don't touch HW in cleanup. Another question: It seems you don't use any release_mem_region() / request_mem_region(). Are those hidden somewhere behind DT parsing or you don't do them at all?

Sebastian

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace

#5 lore raw
In reply to a message not in the archive

drivers/spi/mpc52xx_psc_spi.c static int __exit mpc52xx_psc_spi_do_remove(struct · line 536

Quoted 6 lines · Dragos Carp
drivers/spi/mpc52xx_psc_spi.c +0 -0
@@ -534,10 +534,10 @@ static int __exit mpc52xx_psc_spi_do_remove(struct
 device *dev)
 
        flush_workqueue(mps->workqueue);

Between here ...

drivers/spi/mpc52xx_psc_spi.c static int __exit mpc52xx_psc_spi_do_remove(struct · line 540

Quoted 5 lines
drivers/spi/mpc52xx_psc_spi.c +0 -1
        destroy_workqueue(mps->workqueue);
-       spi_unregister_master(master);
        free_irq(mps->irq, mps);
        if (mps->psc)
                iounmap(mps->psc);

... and here, drivers can submit I/O requests. Which will then be completely lost!!

Can you provide a proper shutdown sequence for this driver?

ISTR that some other adapters grab an extra reference to the adapter so that they can unregister first (getting rid of the spi_device children, unbinding their drivers, and ensuring no more incoming requests) and then clean up resources stored with the driver, and then finally spi_master_put().

- Dave

Quoted 7 lines
drivers/spi/mpc52xx_psc_spi.c +1 -0
+       spi_unregister_master(master);
 
        return 0;

}

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace