Skip to contentSkip to conversation

tun: fix a memory leak for tfile->tx_array

PATCH Superseded Cong Wang 2 participants 3 messages Jan 10, 2018

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

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.

Fixes 1576d9860599 ("tun: switch to use skb array for tx")
Cc Jason Wang <[email protected]>
---
 drivers/net/tun.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +2 -1
index 4f4a842a1c9c..1a1f834440a6 100644
@@ -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
#1 lore raw
Quoted 27 lines · Cong Wang

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.

Fixes 1576d9860599 ("tun: switch to use skb array for tx")
Cc Jason Wang <[email protected]>
---
  drivers/net/tun.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +1 -0
index 4f4a842a1c9c..1a1f834440a6 100644
@@ -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

author
#2 lore raw
Quoted 42 lines · Jason Wang
Quoted 30 lines · Cong Wang

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.

Fixes 1576d9860599 ("tun: switch to use skb array for tx")
Cc Jason Wang <[email protected]>
---
  drivers/net/tun.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +1 -0
index 4f4a842a1c9c..1a1f834440a6 100644
@@ -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.