Skip to contentSkip to conversation

bridge: Fix error path for kobject_init_and_add()

PATCH In review Tobin C. Harding 4 participants 5 messages May 10, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bridge: Fix error path for kobject_init_and_add()

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak. We currently set p to NULL so that kfree() may be called on it as a noop, the code is arguably clearer if we move the kfree() up closer to where it is called (instead of after goto jump).

Remove a goto label 'err1' and jump to call to kobject_put() in error return from kobject_init_and_add() fixing the memory leak. Re-name goto label 'put_back' to 'err1' now that we don't use err1, following current nomenclature (err1, err2 ...). Move call to kfree out of the error code at bottom of function up to closer to where memory was allocated. Add comment to clarify call to kfree().

---

v1 was a part of a set. I have dropped the other patch until I can work out a correct solution.

 net/bridge/br_if.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
net/bridge/br_if.c +6 -7
index 41f0a696a65f..0cb0aa0313a8 100644
@@ -602,13 +602,15 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 	call_netdevice_notifiers(NETDEV_JOIN, dev);
 
 	err = dev_set_allmulti(dev, 1);
-	if (err)
-		goto put_back;
+	if (err) {
+		kfree(p);	/* kobject not yet init'd, manually free */
+		goto err1;
+	}
 
 	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
 				   SYSFS_BRIDGE_PORT_ATTR);
 	if (err)
-		goto err1;
+		goto err2;
 
 	err = br_sysfs_addif(p);
 	if (err)
@@ -700,12 +702,9 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 	sysfs_remove_link(br->ifobj, p->dev->name);
 err2:
 	kobject_put(&p->kobj);
-	p = NULL; /* kobject_put frees */
-err1:
 	dev_set_allmulti(dev, -1);
-put_back:
+err1:
 	dev_put(dev);
-	kfree(p);
 	return err;
 }
 
Signature · 1 line
2.21.0
#1 lore raw

On Fri, May 10, 2019 at 12:52:12PM +1000, Tobin C. Harding wrote:

Please ignore - I forgot about netdev procedure and the merge window. My bad.

Will re-send when you are open.

thanks,
Tobin.

#2 lore raw
Quoted 9 lines · On Fri, 10 May 2019 12:57:02 +1000 "Tobin C. Harding" <[email protected]> wrote:

On Fri, May 10, 2019 at 12:52:12PM +1000, Tobin C. Harding wrote:

Please ignore - I forgot about netdev procedure and the merge window. My bad.

Will re-send when you are open.

thanks,
Tobin.

That only applies to new features, bug fixes are allowed all the time. Also please dont send networking stuff to the greater linux-kernel mailing list.

#3 lore raw
Quoted 8 lines · Stephen Hemminger
Quoted 5 lines · On Fri, 10 May 2019 12:57:02 +1000 "Tobin C. Harding" <[email protected]> wrote:

On Fri, May 10, 2019 at 12:52:12PM +1000, Tobin C. Harding wrote:

Please ignore - I forgot about netdev procedure and the merge window. My bad.

Quoted 7 lines
Quoted 4 lines

Will re-send when you are open.

thanks,
Tobin.

That only applies to new features, bug fixes are allowed all the time. Also please dont send networking stuff to the greater linux-kernel mailing list.

Oh cool, thanks.

maintainer
#4 lore raw
In reply to Tobin C. Harding

From: "Tobin C. Harding" <[email protected]>
Date: Fri, 10 May 2019 12:52:12 +1000

Quoted 13 lines

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak. We currently set p to NULL so that kfree() may be called on it as a noop, the code is arguably clearer if we move the kfree() up closer to where it is called (instead of after goto jump).

Remove a goto label 'err1' and jump to call to kobject_put() in error return from kobject_init_and_add() fixing the memory leak. Re-name goto label 'put_back' to 'err1' now that we don't use err1, following current nomenclature (err1, err2 ...). Move call to kfree out of the error code at bottom of function up to closer to where memory was allocated. Add comment to clarify call to kfree().

Quoted 5 lines

---

v1 was a part of a set. I have dropped the other patch until I can work out a correct solution.

Applied and queued up for -stable, thanks.