Skip to contentSkip to conversation

tuntap: correctly handle error in tun_set_iff()

PATCH Superseded Jason Wang 2 participants 6 messages Sep 11, 2013

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 tuntap: correctly handle error in tun_set_iff()

Commit c8d68e6be1c3b242f1c598595830890b65cea64a (tuntap: multiqueue support) only call free_netdev() on err in tun_set_iff(). This causes several issues:

- memory of tun security were leaked
- use after free since the flow gc timer was not deleted and the tfile were not detached

This patch solves the above issues.

---
The patch were also needed for stable 3.8+.

---
 drivers/net/tun.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
drivers/net/tun.c +7 -2
index a639de8..e5fb5d3 100644
@@ -1641,11 +1641,11 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 		INIT_LIST_HEAD(&tun->disabled);
 		err = tun_attach(tun, file, false);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_free_flow;
 
 		err = register_netdevice(tun->dev);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_detach;
 
 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
@@ -1689,6 +1689,11 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 	strcpy(ifr->ifr_name, tun->dev->name);
 	return 0;
 
+err_detach:
+	tun_detach_all(dev);
+err_free_flow:
+	tun_flow_uninit(tun);
+	security_tun_dev_free_security(tun->security);
  err_free_dev:
 	free_netdev(dev);
 	return err;
Signature · 1 line
1.7.1
#1 lore raw
Quoted 4 lines · Jason Wang

Commit c8d68e6be1c3b242f1c598595830890b65cea64a (tuntap: multiqueue support) only call free_netdev() on err in tun_set_iff(). This causes several issues:

- memory of tun security were leaked

Not just tun security - sock reference too (didn't detach)

Quoted 33 lines

- use after free since the flow gc timer was not deleted and the tfile were not detached

This patch solves the above issues.

---
The patch were also needed for stable 3.8+.

---
 drivers/net/tun.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
drivers/net/tun.c +2 -2
index a639de8..e5fb5d3 100644
@@ -1641,11 +1641,11 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 		INIT_LIST_HEAD(&tun->disabled);
 		err = tun_attach(tun, file, false);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_free_flow;
 
 		err = register_netdevice(tun->dev);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_detach;
 
 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) · line 1696

Quoted 8 lines
drivers/net/tun.c +5 -0
@@ -1689,6 +1689,11 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 	strcpy(ifr->ifr_name, tun->dev->name);
 	return 0;
 
+err_detach:
+	tun_detach_all(dev);
+err_free_flow:
+	tun_flow_uninit(tun);
+	security_tun_dev_free_security(tun->security);

This bit looks wrong: if flow_init fails, we
need security_tun_dev_free_security, don't we?
I think we need:

	+err_free_sec:	security_tun_dev_free_security(tun->security);

and goto here on flow_init failures.

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) · line 1697

Quoted 1 line
drivers/net/tun.c +0 -0
  err_free_dev:

Pls shift this one 1 space left for consistency.

Quoted 4 lines
drivers/net/tun.c +0 -0
 	free_netdev(dev);
 	return err;
Signature · 1 line
1.7.1
author
#2 lore raw

----- Original Message -----

Quoted 10 lines · Jason Wang
Quoted 6 lines

Commit c8d68e6be1c3b242f1c598595830890b65cea64a (tuntap: multiqueue support)
only call free_netdev() on err in tun_set_iff(). This causes several issues:

- memory of tun security were leaked

Not just tun security - sock reference too (didn't detach)

Yes, I mention it in the next item.

Quoted 48 lines
Quoted 35 lines

- use after free since the flow gc timer was not deleted and the tfile were not
detached

This patch solves the above issues.

---
The patch were also needed for stable 3.8+.

---
 drivers/net/tun.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
drivers/net/tun.c +2 -2
index a639de8..e5fb5d3 100644
@@ -1641,11 +1641,11 @@ static int tun_set_iff(struct net *net, struct file
 *file, struct ifreq *ifr)
 		INIT_LIST_HEAD(&tun->disabled);
 		err = tun_attach(tun, file, false);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_free_flow;
 
 		err = register_netdevice(tun->dev);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_detach;
 
 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
drivers/net/tun.c +0 -0
 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file · line 1697

Quoted 9 lines
drivers/net/tun.c +5 -0
@@ -1689,6 +1689,11 @@ static int tun_set_iff(struct net *net, struct file
 *file, struct ifreq *ifr)
 	strcpy(ifr->ifr_name, tun->dev->name);
 	return 0;
 
+err_detach:
+	tun_detach_all(dev);
+err_free_flow:
+	tun_flow_uninit(tun);
+	security_tun_dev_free_security(tun->security);

This bit looks wrong: if flow_init fails, we
need security_tun_dev_free_security, don't we?
I think we need:

Quoted 2 lines
	+err_free_sec:	security_tun_dev_free_security(tun->security);

and goto here on flow_init failures.

tun_flow_init() always succeed. So we're ok here.

Quoted 5 lines

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) · line 1697

Quoted 2 lines
drivers/net/tun.c +0 -0
  err_free_dev:

Pls shift this one 1 space left for consistency.

Ok.

Quoted 10 lines
Quoted 4 lines
drivers/net/tun.c +0 -0
 	free_netdev(dev);

return err;
--
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#3 lore raw
Quoted 9 lines · Jason Wang

----- Original Message -----

Quoted 6 lines · Jason Wang
Quoted 5 lines

Commit c8d68e6be1c3b242f1c598595830890b65cea64a (tuntap: multiqueue support)
only call free_netdev() on err in tun_set_iff(). This causes several issues:

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

- memory of tun security were leaked

Not just tun security - sock reference too (didn't detach)

Yes, I mention it in the next item.

That line's a bit too long btw.
Keep it under 70 chars for commit logs.

Quoted 51 lines
Quoted 48 lines
Quoted 35 lines

- use after free since the flow gc timer was not deleted and the tfile were not
detached

This patch solves the above issues.

---
The patch were also needed for stable 3.8+.

---
 drivers/net/tun.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
drivers/net/tun.c +2 -2
index a639de8..e5fb5d3 100644
@@ -1641,11 +1641,11 @@ static int tun_set_iff(struct net *net, struct file
 *file, struct ifreq *ifr)
 		INIT_LIST_HEAD(&tun->disabled);
 		err = tun_attach(tun, file, false);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_free_flow;
 
 		err = register_netdevice(tun->dev);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_detach;
 
 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
drivers/net/tun.c +0 -0
 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file · line 1697

Quoted 9 lines
drivers/net/tun.c +5 -0
@@ -1689,6 +1689,11 @@ static int tun_set_iff(struct net *net, struct file
 *file, struct ifreq *ifr)
 	strcpy(ifr->ifr_name, tun->dev->name);
 	return 0;
 
+err_detach:
+	tun_detach_all(dev);
+err_free_flow:
+	tun_flow_uninit(tun);
+	security_tun_dev_free_security(tun->security);

This bit looks wrong: if flow_init fails, we
need security_tun_dev_free_security, don't we?
I think we need:

Quoted 2 lines
	+err_free_sec:	security_tun_dev_free_security(tun->security);

and goto here on flow_init failures.

Quoted 1 line

tun_flow_init() always succeed. So we're ok here.

True. So please make it return void then.

Quoted 17 lines
Quoted 5 lines

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) · line 1697

Quoted 2 lines
drivers/net/tun.c +0 -0
  err_free_dev:

Pls shift this one 1 space left for consistency.

Ok.

Quoted 10 lines
Quoted 4 lines
drivers/net/tun.c +0 -0
 	free_netdev(dev);

return err;
--
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author
#4 lore raw

----- Original Message -----

Quoted 12 lines · Jason Wang
Quoted 11 lines

----- Original Message -----

Quoted 8 lines · Jason Wang
Quoted 6 lines

Commit c8d68e6be1c3b242f1c598595830890b65cea64a (tuntap: multiqueue support)
only call free_netdev() on err in tun_set_iff(). This causes several issues:

- memory of tun security were leaked

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Not just tun security - sock reference too (didn't detach)

Yes, I mention it in the next item.

That line's a bit too long btw.
Keep it under 70 chars for commit logs.

Ok.

Quoted 54 lines
Quoted 54 lines
Quoted 53 lines
Quoted 39 lines

- use after free since the flow gc timer was not deleted and the tfile were
not
detached

This patch solves the above issues.

---
The patch were also needed for stable 3.8+.

---
 drivers/net/tun.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
drivers/net/tun.c +0 -0
index a639de8..e5fb5d3 100644
@@ -1641,11 +1641,11 @@ static int tun_set_iff(struct net *net, struct

file
*file, struct ifreq *ifr)

drivers/net/tun.c +2 -2
 		INIT_LIST_HEAD(&tun->disabled);
 		err = tun_attach(tun, file, false);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_free_flow;
 
 		err = register_netdevice(tun->dev);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_detach;
 
 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
@@ -1689,6 +1689,11 @@ static int tun_set_iff(struct net *net, struct

file
*file, struct ifreq *ifr)

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) · line 1696

Quoted 8 lines
drivers/net/tun.c +5 -0
 	strcpy(ifr->ifr_name, tun->dev->name);
 	return 0;
 
+err_detach:
+	tun_detach_all(dev);
+err_free_flow:
+	tun_flow_uninit(tun);
+	security_tun_dev_free_security(tun->security);

This bit looks wrong: if flow_init fails, we
need security_tun_dev_free_security, don't we?
I think we need:

	+err_free_sec:	security_tun_dev_free_security(tun->security);

and goto here on flow_init failures.

Quoted 4 lines
Quoted 1 line

tun_flow_init() always succeed. So we're ok here.

True. So please make it return void then.

It has already returned void.

Quoted 24 lines
Quoted 18 lines
Quoted 5 lines
Quoted 2 lines

err_free_dev:

Pls shift this one 1 space left for consistency.

Ok.

Quoted 11 lines
Quoted 4 lines
drivers/net/tun.c +0 -1
 	free_netdev(dev);
 	return err;
--

1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#5 lore raw
In reply to Michael S. Tsirkin
Quoted 71 lines · Michael S. Tsirkin
Quoted 9 lines · Jason Wang

----- Original Message -----

Quoted 6 lines · Jason Wang
Quoted 5 lines

Commit c8d68e6be1c3b242f1c598595830890b65cea64a (tuntap: multiqueue support)
only call free_netdev() on err in tun_set_iff(). This causes several issues:

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

- memory of tun security were leaked

Not just tun security - sock reference too (didn't detach)

Yes, I mention it in the next item.

That line's a bit too long btw.
Keep it under 70 chars for commit logs.

Quoted 51 lines
Quoted 50 lines
Quoted 35 lines

- use after free since the flow gc timer was not deleted and the tfile were not
detached

This patch solves the above issues.

---
The patch were also needed for stable 3.8+.

---
 drivers/net/tun.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
drivers/net/tun.c +2 -2
index a639de8..e5fb5d3 100644
@@ -1641,11 +1641,11 @@ static int tun_set_iff(struct net *net, struct file
 *file, struct ifreq *ifr)
 		INIT_LIST_HEAD(&tun->disabled);
 		err = tun_attach(tun, file, false);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_free_flow;
 
 		err = register_netdevice(tun->dev);
 		if (err < 0)
-			goto err_free_dev;
+			goto err_detach;
 
 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
drivers/net/tun.c +0 -0
 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file · line 1697

Quoted 9 lines
drivers/net/tun.c +5 -0
@@ -1689,6 +1689,11 @@ static int tun_set_iff(struct net *net, struct file
 *file, struct ifreq *ifr)
 	strcpy(ifr->ifr_name, tun->dev->name);
 	return 0;
 
+err_detach:
+	tun_detach_all(dev);
+err_free_flow:
+	tun_flow_uninit(tun);
+	security_tun_dev_free_security(tun->security);

This bit looks wrong: if flow_init fails, we
need security_tun_dev_free_security, don't we?
I think we need:

	+err_free_sec:	security_tun_dev_free_security(tun->security);

and goto here on flow_init failures.

Quoted 3 lines
Quoted 1 line

tun_flow_init() always succeed. So we're ok here.

True. So please make it return void then.

Heh I was looking at a stale tree, this is already fixed in commit 944a1376b.

Quoted 17 lines
Quoted 17 lines
Quoted 5 lines

drivers/net/tun.c static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) · line 1697

Quoted 2 lines
drivers/net/tun.c +0 -0
  err_free_dev:

Pls shift this one 1 space left for consistency.

Ok.

Quoted 10 lines
Quoted 4 lines
drivers/net/tun.c +0 -0
 	free_netdev(dev);

return err;
--
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/