net: macb: fix use after free on rmmod
Subsystems (3)
PATCH 1/1 net: macb: fix use after free on rmmod
plat_dev->dev->platform_data is released by platform_device_unregister(), use of pclk and hclk is use after free. This patch keeps a copy to fix the issue.
[ 31.261225] BUG: KASAN: use-after-free in macb_remove+0x77/0xc6 [macb_pci] [ 31.275563] Freed by task 306: [ 30.276782] platform_device_release+0x25/0x80
--- drivers/net/ethernet/cadence/macb_pci.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/cadence/macb_pci.c +4 -2
@@ -110,10 +110,12 @@ static void macb_remove(struct pci_dev *pdev) {struct platform_device *plat_dev =pci_get_drvdata (pdev);struct macb_platform_data *plat_data =dev_get_platdata (&plat_dev->dev); +struct clk *pclk = plat_data->pclk; +struct clk *hclk = plat_data->hclk;platform_device_unregister (plat_dev); -clk_unregister (plat_data->pclk); -clk_unregister (plat_data->hclk); +clk_unregister (pclk); +clk_unregister (hclk); }static const struct pci_device_id dev_id_table[] = {
Signature · 1 line
2.25.1
Quoted 19 lines · Tong Zhang
plat_dev->dev->platform_datais released byplatform_device_unregister(), use of pclk and hclk is use after free. This patch keeps a copy to fix the issue.[ 31.261225] BUG: KASAN: use-after-free in macb_remove+0x77/0xc6 [macb_pci] [ 31.275563] Freed by task 306: [ 30.276782] platform_device_release+0x25/0x80Signed-off-by Tong Zhang <[email protected]>--- drivers/net/ethernet/cadence/macb_pci.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/net/ethernet/cadence/macb_pci.c +0
-0@@ -110,10 +110,12 @@ static void macb_remove(struct pci_dev *pdev) {
drivers/net/ethernet/cadence/macb_pci.c static void macb_remove(struct pci_dev *pdev) · line 118
Quoted 10 lines
drivers/net/ethernet/cadence/macb_pci.c +4
-2struct platform_device *plat_dev =pci_get_drvdata (pdev);struct macb_platform_data *plat_data =dev_get_platdata (&plat_dev->dev); +struct clk *pclk = plat_data->pclk; +struct clk *hclk = plat_data->hclk;platform_device_unregister (plat_dev); -clk_unregister (plat_data->pclk); -clk_unregister (plat_data->hclk); +clk_unregister (pclk); +clk_unregister (hclk);
NACK, I would prefer that you switch lines and do clock clk unregister
before: this way we avoid the problem and I think that you don't need clocks for unregistering the platform device anyway.
Please change accordingly or tell me what could go bad.
Regards,
Nicolas
Quoted 6 lines
drivers/net/ethernet/cadence/macb_pci.c +0
-1}static const struct pci_device_id dev_id_table[] = { --2.25.1
Signature · 1 line
Nicolas Ferre
PATCH v2 1/1 net: macb: fix use after free on rmmod
plat_dev->dev->platform_data is released by platform_device_unregister(), use of pclk and hclk is a use-after-free. Since device unregister won't need a clk device we adjust the function call sequence to fix this issue.
[ 31.261225] BUG: KASAN: use-after-free in macb_remove+0x77/0xc6 [macb_pci] [ 31.275563] Freed by task 306: [ 30.276782] platform_device_release+0x25/0x80
---
v2: switch lines to fix the issue instead
drivers/net/ethernet/cadence/macb_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/cadence/macb_pci.c +1 -1
@@ -111,9 +111,9 @@ static void macb_remove(struct pci_dev *pdev)struct platform_device *plat_dev =pci_get_drvdata (pdev);struct macb_platform_data *plat_data =dev_get_platdata (&plat_dev->dev); -platform_device_unregister (plat_dev);clk_unregister (plat_data->pclk);clk_unregister (plat_data->hclk); +platform_device_unregister (plat_dev); }static const struct pci_device_id dev_id_table[] = {
Signature · 1 line
2.25.1
Quoted 6 lines · Tong Zhang
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
plat_dev->dev->platform_datais released byplatform_device_unregister(), use of pclk and hclk is a use-after-free. Since device unregister won't need a clk device we adjust the function call sequence to fix this issue.
Quoted 6 lines
[ 31.261225] BUG: KASAN: use-after-free in macb_remove+0x77/0xc6 [macb_pci] [ 31.275563] Freed by task 306: [ 30.276782] platform_device_release+0x25/0x80Suggested-by Nicolas Ferre <[email protected]>Signed-off-by Tong Zhang <[email protected]>
Thanks Tong Zhang.
Regards,
Nicolas
Quoted 24 lines
---
v2: switch lines to fix the issue insteaddrivers/net/ethernet/cadence/macb_pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/ethernet/cadence/macb_pci.c +1
-2@@ -111,9 +111,9 @@ static void macb_remove(struct pci_dev *pdev)struct platform_device *plat_dev =pci_get_drvdata (pdev);struct macb_platform_data *plat_data =dev_get_platdata (&plat_dev->dev); -platform_device_unregister (plat_dev);clk_unregister (plat_data->pclk);clk_unregister (plat_data->hclk); +platform_device_unregister (plat_dev); }static const struct pci_device_id dev_id_table[] = { --2.25.1
Signature · 1 line
Nicolas Ferre
This patch was applied to netdev/net.git (refs/heads/master):
Hello:
This patch was applied to netdev/net.git (refs/heads/master):
Quoted 9 lines · On Wed, 8 Sep 2021 12:02:32 -0700 you wrote:
plat_dev->dev->platform_datais released byplatform_device_unregister(), use of pclk and hclk is a use-after-free. Since device unregister won't need a clk device we adjust the function call sequence to fix this issue.[ 31.261225] BUG: KASAN: use-after-free in macb_remove+0x77/0xc6 [macb_pci] [ 31.275563] Freed by task 306: [ 30.276782] platform_device_release+0x25/0x80[...]
Here is the summary with links:
- [v2] net: macb: fix use after free on rmmod
https://git.kernel.org/netdev/net/c/d82d5303c4c5
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thanks Nicolas, sent a v2 as suggested.
- Tong
Quoted 52 lines · On Wed, Sep 8, 2021 at 12:27 AM <[email protected]> wrote:
Quoted 19 lines · Tong Zhang
plat_dev->dev->platform_datais released byplatform_device_unregister(), use of pclk and hclk is use after free. This patch keeps a copy to fix the issue.[ 31.261225] BUG: KASAN: use-after-free in macb_remove+0x77/0xc6 [macb_pci] [ 31.275563] Freed by task 306: [ 30.276782] platform_device_release+0x25/0x80Signed-off-by Tong Zhang <[email protected]>--- drivers/net/ethernet/cadence/macb_pci.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/net/ethernet/cadence/macb_pci.c +0
-0@@ -110,10 +110,12 @@ static void macb_remove(struct pci_dev *pdev) {drivers/net/ethernet/cadence/macb_pci.c static void macb_remove(struct pci_dev *pdev) · line 118
Quoted 10 lines
drivers/net/ethernet/cadence/macb_pci.c +4
-2struct platform_device *plat_dev =pci_get_drvdata (pdev);struct macb_platform_data *plat_data =dev_get_platdata (&plat_dev->dev); +struct clk *pclk = plat_data->pclk; +struct clk *hclk = plat_data->hclk;platform_device_unregister (plat_dev); -clk_unregister (plat_data->pclk); -clk_unregister (plat_data->hclk); +clk_unregister (pclk); +clk_unregister (hclk);NACK, I would prefer that you switch lines and do clock clk unregister
before: this way we avoid the problem and I think that you don't need clocks for unregistering the platform device anyway.Please change accordingly or tell me what could go bad.
Regards,
NicolasQuoted 6 lines
drivers/net/ethernet/cadence/macb_pci.c +0
-1}static const struct pci_device_id dev_id_table[] = { --2.25.1
--
Nicolas Ferre