net: dpaa2-eth: fix use-after-free in dpaa2_eth_remove
Subsystems (4)
PATCH 1/1 net: dpaa2-eth: fix use-after-free in dpaa2_eth_remove
Access to netdev after free_netdev() will cause use-after-free bug. Move debug log before free_netdev() call to avoid it.
---
Note about Fixes: tag. The commit introduced it was before this driver was moved out from staging. I guess, Fixes tag cannot be used here. Please, let me know if I am wrong.
@Dan, is there a smatch checker for straigthforward use after free bugs? Like acessing pointer after free was called? I think, adding free_netdev() to check list might be good idea
I've skimmed througth smatch source and didn't find one, so can you, please, point out to it if it exists.
With regards,
Pavel Skripkin
--- drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +2 -2
@@ -4540,10 +4540,10 @@ static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)fsl_mc_portal_free (priv->mc_io); -free_netdev (net_dev); -dev_dbg (net_dev->dev.parent,"Removed interface %s\n" , net_dev->name); +free_netdev (net_dev); +return 0 ; }
Signature · 1 line
2.33.1
Quoted 11 lines · Pavel Skripkin
Access to netdev after
free_netdev()will cause use-after-free bug. Move debug log beforefree_netdev()call to avoid it.Cc [email protected] # v4.19+Signed-off-by Pavel Skripkin <[email protected]>---
Note about Fixes: tag. The commit introduced it was before this driver was moved out from staging. I guess, Fixes tag cannot be used here. Please, let me know if I am wrong.
You should still use the Fixes tag.
Quoted 8 lines
Cc Dan Carpenter <[email protected]>@Dan, is there a smatch checker for straigthforward use after free bugs? Like acessing pointer after free was called? I think, adding
free_netdev()to check list might be good ideaI've skimmed througth smatch source and didn't find one, so can you, please, point out to it if it exists.
It's check_free_strict.c.
It does cross function analysis but free_netdev() is tricky because it doesn't free directly, it just drops the reference count. Also it delays freeing in the NETREG_UNREGISTERING path so this check might cause false positives? I'll add free_netdev() to the list of free functions and test it overnight tonight.
register_free_hook("free_netdev", &match_free, 0);regards,
dan carpenter
Jakub Kicinski
maintainerQuoted 7 lines · On Mon, 15 Nov 2021 11:08:17 +0300 Dan Carpenter wrote:
Quoted 6 lines
@Dan, is there a smatch checker for straigthforward use after free bugs? Like acessing pointer after free was called? I think, adding
free_netdev()to check list might be good ideaI've skimmed througth smatch source and didn't find one, so can you, please, point out to it if it exists.
Quoted 6 lines
It's
check_free_strict.c.It does cross function analysis but
free_netdev()is tricky because it doesn't free directly, it just drops the reference count. Also it delays freeing in theNETREG_UNREGISTERINGpath so this check might cause false positives?
I'd ignore that path, it's just special casing that's supposed to keep the driver-visible API sane. Nobody should be touching netdev past free_netdev(). Actually if you can it'd be interesting to add checks for using whatever netdev_priv(ndev) returned past free_netdev(ndev).
Most UAFs that come to mind from the past were people doing something like:
struct my_priv *mine = netdev_priv(ndev); netdev_unregister(ndev); free_netdev(ndev); free(mine->bla); /* UAF, free_netdev() frees the priv */
Quoted 4 lines
I'll add
free_netdev()to the list of free
functions and test it overnight tonight.register_free_hook("free_netdev", &match_free, 0);
Quoted 8 lines · Jakub Kicinski
I'd ignore that path, it's just special casing that's supposed to keep the driver-visible API sane. Nobody should be touching netdev past
free_netdev(). Actually if you can it'd be interesting to add checks for using whatevernetdev_priv(ndev)returned pastfree_netdev(ndev).Most UAFs that come to mind from the past were people doing something like:
Quoted 7 lines
struct my_priv *mine = netdev_priv(ndev); netdev_unregister(ndev); free_netdev(ndev); free(mine->bla); /* UAF, free_netdev() frees the priv */
I've implemented this checker couple of months ago. The latest smatch (v1.72) should warn about this type of bugs. All reported bugs are fixed already :)
My checker warns about using priv pointer after free_netdev() and free_candev() calls. There are a few more wrappers like free_sja1000dev(), so it worth to add them to check list too. Will add them today later
Important thing, that there are complex situations like
struct priv *priv = get_priv_from_smth(smth); free_netdev(priv->netdev); clean_up_priv(priv);
and for now I have no idea how to handle it (ex: ems_usb_disconnect).
With regards,
Pavel Skripkin
PATCH v2 1/1 net: dpaa2-eth: fix use-after-free in dpaa2_eth_remove
In reply to Dan CarpenterAccess to netdev after free_netdev() will cause use-after-free bug. Move debug log before free_netdev() call to avoid it.
---
Changes in v2:
replaced Cc: stable with Fixes: tag
--- drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +2 -2
@@ -4550,10 +4550,10 @@ static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)fsl_mc_portal_free (priv->mc_io); -free_netdev (net_dev); -dev_dbg (net_dev->dev.parent,"Removed interface %s\n" , net_dev->name); +free_netdev (net_dev); +return 0 ; }
Signature · 1 line
2.33.1
This patch was applied to netdev/net.git (master) by David S. Miller <[email protected]>:
Hello:
This patch was applied to netdev/net.git (master)
by David S. Miller <[email protected]>:
Quoted 8 lines · On Tue, 16 Nov 2021 18:17:12 +0300 you wrote:
Access to netdev after
free_netdev()will cause use-after-free bug. Move debug log beforefree_netdev()call to avoid it.Fixes 7472dd9f6499 ("staging: fsl-dpaa2/eth: Move print message")Signed-off-by Pavel Skripkin <[email protected]>---
[...]
Here is the summary with links:
- [v2] net: dpaa2-eth: fix use-after-free in dpaa2_eth_remove
https://git.kernel.org/netdev/net/c/9b5a333272a4
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html