tun: fix a memory leak for tfile->tx_array
Subsystems (3)
PATCH 1/1 tun: fix a memory leak for tfile->tx_array
tfile->tun could be detached before we close the tun fd, via tun_detach_all(), so it should not be used to check for tfile->tx_array.
Use the same logic as in tun_attach(), just test !tfile->deatched.
--- drivers/net/tun.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +2 -1
@@ -613,6 +613,7 @@ static void tun_queue_purge(struct tun_file *tfile)static void __tun_detach (struct tun_file *tfile,bool clean) { +bool clean_tx_array = !tfile->detached;struct tun_file *ntfile;struct tun_struct *tun;
@@ -657,7 +658,7 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->dev->reg_state == NETREG_REGISTERED)unregister_netdevice (tun->dev); } -if (tun) +if (clean_tx_array)skb_array_cleanup (&tfile->tx_array);sock_put (&tfile->sk); }
Signature · 1 line
2.13.0
Quoted 27 lines · Cong Wang
tfile->tuncould be detached before we close the tun fd, viatun_detach_all(), so it should not be used to check fortfile->tx_array.Use the same logic as in
tun_attach(), just test !tfile->deatched.Reported-by Dmitry Vyukov <[email protected]>Fixes 1576d9860599 ("tun: switch to use skb array for tx")Cc Jason Wang <[email protected]>Signed-off-by Cong Wang <[email protected]>--- drivers/net/tun.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/net/tun.c +1
-0@@ -613,6 +613,7 @@ static void tun_queue_purge(struct tun_file *tfile)static void __tun_detach (struct tun_file *tfile,bool clean) { +bool clean_tx_array = !tfile->detached;struct tun_file *ntfile;struct tun_struct *tun;
drivers/net/tun.c static void __tun_detach(struct tun_file *tfile, bool clean) · line 664
Quoted 8 lines
drivers/net/tun.c +1
-1@@ -657,7 +658,7 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->dev->reg_state == NETREG_REGISTERED)unregister_netdevice (tun->dev); } -if (tun) +if (clean_tx_array)skb_array_cleanup (&tfile->tx_array);sock_put (&tfile->sk); }
Looks like we may still leak if we do
open
attach
detach
close
Should we do cleanup unconditionally?
Thanks
Quoted 42 lines · Jason Wang
Quoted 30 lines · Cong Wang
tfile->tuncould be detached before we close the tun fd, viatun_detach_all(), so it should not be used to check fortfile->tx_array.Use the same logic as in
tun_attach(), just test !tfile->deatched.Reported-by Dmitry Vyukov <[email protected]>Fixes 1576d9860599 ("tun: switch to use skb array for tx")Cc Jason Wang <[email protected]>Signed-off-by Cong Wang <[email protected]>--- drivers/net/tun.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/net/tun.c +1
-0@@ -613,6 +613,7 @@ static void tun_queue_purge(struct tun_file *tfile)static void __tun_detach (struct tun_file *tfile,bool clean) { +bool clean_tx_array = !tfile->detached;struct tun_file *ntfile;struct tun_struct *tun; @@ -657,7 +658,7 @@ static void __tun_detach(struct tun_file *tfile,bool clean)tun->dev->reg_state == NETREG_REGISTERED) unregister_netdevice(tun->dev); }drivers/net/tun.c static void __tun_detach(struct tun_file *tfile, bool clean) · line 664
Quoted 5 lines
drivers/net/tun.c +1
-1-if (tun) +if (clean_tx_array)skb_array_cleanup (&tfile->tx_array);sock_put (&tfile->sk); }Looks like we may still leak if we do
Quoted 4 lines
open
attach
detach
close
Good catch.
Quoted 2 lines
Should we do cleanup unconditionally?
It doesn't look like we can, because tfile is not zero'ed at least, it is allocated by sk_alloc() so tfile->tx_array could be some random value which prevents skb_array_cleanup() functioning correctly. Zero'ing tfile->tx_array in open() is not sufficient, as spinlocks are not yet initialized.
I think we probably need to test tfile->tx_array.ring.queue, it is ugly but I don't see there is an API for it.