Skip to contentSkip to conversation

Fix error path for kobject_init_and_add()

PATCH Superseded Tobin C. Harding 6 participants 14 messages Apr 30, 2019 – May 8, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix error path for kobject_init_and_add()

Hi Dave,

There are a few places in net/ that are not correctly handling the error path after calls to kobject_init_and_add(). This set fixes all of these for net/

This corrects a memory leak if kobject_init() is not followed by a call to kobject_put()

This set is part of an effort to check/fix all of these mem leaks across the kernel tree.

For reference this is the behaviour that we are trying to achieve

void fn(void)

{
	int ret;

	ret = kobject_init_and_add(kobj, ktype, NULL, "foo");
	if (ret) {
		kobject_put(kobj);
		return ret;
	}

	ret = some_init_fn();
	if (ret)
		goto err;

	ret = some_other_init_fn();
	if (ret)
		goto other_err;

	kobject_uevent(kobj, KOBJ_ADD);
	return 0;

other_err:

	other_clean_up_fn();
err:
	kobject_del(kobj);
	return ret;
}

Testing: No testing done, built with config options

CONFIG_NET=y
CONFIG_SYSFS=y
CONFIG_BRIDGE=y

thanks,
Tobin.

Tobin C. Harding (3):
bridge: Fix error path for kobject_init_and_add()
bridge: Use correct cleanup function
net-sysfs: Fix error path for kobject_init_and_add()

 net/bridge/br_if.c   | 6 ++++--
 net/core/net-sysfs.c | 8 ++++++--
 2 files changed, 10 insertions(+), 4 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/3 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.

Add call to kobject_put() in error path of kobject_init_and_add().

---
 net/bridge/br_if.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/bridge/br_if.c +3 -1
index 41f0a696a65f..e5c8c9941c51 100644
@@ -607,8 +607,10 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 
 	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
 				   SYSFS_BRIDGE_PORT_ATTR);
-	if (err)
+	if (err) {
+		kobject_put(&p->kobj);
 		goto err1;
+	}
 
 	err = br_sysfs_addif(p);
 	if (err)
Signature · 1 line
2.21.0
#7 lore raw

PATCH 2/3 bridge: Use correct cleanup function

In reply to Tobin C. Harding

The correct cleanup function if a call to kobject_init_and_add() has returned _successfully_ is kobject_del(). kobject_put() is used if the call to kobject_init_and_add() fails. kobject_del() calls kobject_put().

Use correct cleanup function in error path.

---
 net/bridge/br_if.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bridge/br_if.c +1 -1
index e5c8c9941c51..d3a1554ccff4 100644
@@ -701,7 +701,7 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 err3:
 	sysfs_remove_link(br->ifobj, p->dev->name);
 err2:
-	kobject_put(&p->kobj);
+	kobject_del(&p->kobj);
 	p = NULL; /* kobject_put frees */
 err1:
 	dev_set_allmulti(dev, -1);
Signature · 1 line
2.21.0
#9 lore raw

PATCH 3/3 net-sysfs: Fix error path for kobject_init_and_add()

In reply to Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

---
 net/core/net-sysfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/core/net-sysfs.c +6 -2
index 8f8b7b6c2945..9d4e3f47b789 100644
@@ -925,8 +925,10 @@ static int rx_queue_add_kobject(struct net_device *dev, int index)
 	kobj->kset = dev->queues_kset;
 	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
 				     "rx-%u", index);
-	if (error)
+	if (error) {
+		kobject_put(kobj);
 		return error;
+	}
 
 	dev_hold(queue->dev);
 
@@ -1462,8 +1464,10 @@ static int netdev_queue_add_kobject(struct net_device *dev, int index)
 	kobj->kset = dev->queues_kset;
 	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
 				     "tx-%u", index);
-	if (error)
+	if (error) {
+		kobject_put(kobj);
 		return error;
+	}
 
 	dev_hold(queue->dev);
 
Signature · 1 line
2.21.0
#2 lore raw

Re: [PATCH 1/3] bridge: Fix error path for kobject_init_and_add()

In reply to Tobin C. Harding

On Tue, Apr 30, 2019 at 10:28:15AM +1000, Tobin C. Harding wrote:

[snip]

The cover letter appears to have gotten lost, I can resend if it makes things better for you Dave.

FTR this is the contents from it:

Hi Dave,

There are a few places in net/ that are not correctly handling the error path after calls to kobject_init_and_add(). This set fixes all of these for net/

This corrects a memory leak if kobject_init() is not followed by a call to kobject_put()

This set is part of an effort to check/fix all of these mem leaks across the kernel tree.

For reference this is the behaviour that we are trying to achieve

void fn(void)

{
	int ret;

	ret = kobject_init_and_add(kobj, ktype, NULL, "foo");
	if (ret) {
		kobject_put(kobj);
		return ret;
	}

	ret = some_init_fn();
	if (ret)
		goto err;

	ret = some_other_init_fn();
	if (ret)
		goto other_err;

	kobject_uevent(kobj, KOBJ_ADD);
	return 0;

other_err:

	other_clean_up_fn();
err:
	kobject_del(kobj);
	return ret;
}

Testing: No testing done, built with config options

CONFIG_NET=y
CONFIG_SYSFS=y
CONFIG_BRIDGE=y

thanks,
Tobin.

Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] bridge: Fix error path for kobject_init_and_add()

In reply to Tobin C. Harding
Quoted 6 lines · Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

Reviewed-by on v1 2/3
#8 lore raw

Re: [PATCH 2/3] bridge: Use correct cleanup function

In reply to Tobin C. Harding
Quoted 7 lines · Tobin C. Harding

The correct cleanup function if a call to kobject_init_and_add() has returned _successfully_ is kobject_del(). kobject_put() is used if the call to kobject_init_and_add() fails. kobject_del() calls kobject_put().

Use correct cleanup function in error path.

Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] net-sysfs: Fix error path for kobject_init_and_add()

In reply to Tobin C. Harding
Quoted 6 lines · Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

#4 lore raw

Re: [PATCH 1/3] bridge: Fix error path for kobject_init_and_add()

In reply to Tobin C. Harding
Quoted 15 lines · Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

---
 net/bridge/br_if.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/bridge/br_if.c +0 -0
index 41f0a696a65f..e5c8c9941c51 100644

net/bridge/br_if.c int br_add_if(struct net_bridge *br, struct net_device *dev, · line 613

Quoted 8 lines
net/bridge/br_if.c +3 -1
@@ -607,8 +607,10 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 
 	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
 				   SYSFS_BRIDGE_PORT_ATTR);
-	if (err)
+	if (err) {
+		kobject_put(&p->kobj);
 		goto err1;
+	}

I think this is duplicating the code under the err2 label and doing so in a way that would introduce a double free.

If the refcount hits 0 in the kobject_put(), release_nbp() is called which calls kfree() on the p pointer. However, the p pointer is never set to NULL like what's done under the err2 label. Once we're back in br_add_if(), kfree() is called on the p pointer again just before returning.

I think it would be better if you just jumped to the err2 label instead of err1. err1 will no longer be used so, unfortunately, you'll have to refactor all the labels at the same time.

Tyler

Quoted 6 lines
net/bridge/br_if.c +0 -0
 	err = br_sysfs_addif(p);
 	if (err)
Signature · 1 line
2.21.0
#5 lore raw

Re: [PATCH 1/3] bridge: Fix error path for kobject_init_and_add()

Quoted 34 lines · Tyler Hicks
Quoted 15 lines · Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

---
 net/bridge/br_if.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/bridge/br_if.c +0 -0
index 41f0a696a65f..e5c8c9941c51 100644

net/bridge/br_if.c int br_add_if(struct net_bridge *br, struct net_device *dev, · line 613

Quoted 8 lines
net/bridge/br_if.c +3 -1
@@ -607,8 +607,10 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 
 	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
 				   SYSFS_BRIDGE_PORT_ATTR);
-	if (err)
+	if (err) {
+		kobject_put(&p->kobj);
 		goto err1;
+	}

I think this is duplicating the code under the err2 label and doing so in a way that would introduce a double free.

If the refcount hits 0 in the kobject_put(), release_nbp() is called which calls kfree() on the p pointer. However, the p pointer is never set to NULL like what's done under the err2 label. Once we're back in br_add_if(), kfree() is called on the p pointer again just before returning.

Quoted 3 lines

I think it would be better if you just jumped to the err2 label instead of err1. err1 will no longer be used so, unfortunately, you'll have to refactor all the labels at the same time.

I noticed that the last paragraph is bad advice after looking at patch #2 in this series. I guess you'd be better off calling kobject_put(), setting p to NULL (the only thing that's missing), and jumping to err1 in this patch.

Tyler

Quoted 9 lines

Tyler

Quoted 6 lines
net/bridge/br_if.c +0 -0
 	err = br_sysfs_addif(p);
 	if (err)
Signature · 1 line
2.21.0
#11 lore raw

Re: [PATCH 3/3] net-sysfs: Fix error path for kobject_init_and_add()

In reply to Tobin C. Harding
Quoted 15 lines · Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

---
 net/core/net-sysfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/core/net-sysfs.c +0 -0
index 8f8b7b6c2945..9d4e3f47b789 100644

net/core/net-sysfs.c static int rx_queue_add_kobject(struct net_device *dev, int index) · line 931

Quoted 8 lines
net/core/net-sysfs.c +3 -1
@@ -925,8 +925,10 @@ static int rx_queue_add_kobject(struct net_device *dev, int index)
 	kobj->kset = dev->queues_kset;
 	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
 				     "rx-%u", index);
-	if (error)
+	if (error) {
+		kobject_put(kobj);
 		return error;
+	}

The commit message of the second patch in this series states, "The correct cleanup function if a call to kobject_init_and_add() has returned _successfully_ is kobject_del()." Doesn't that mean that kobject_del() needs to be called instead of kobject_put() when sysfs_create_group() fails a little lower in this function?

Quoted 4 lines
net/core/net-sysfs.c +0 -0
 	dev_hold(queue->dev);
 

net/core/net-sysfs.c static int netdev_queue_add_kobject(struct net_device *dev, int index) · line 1472

Quoted 10 lines
net/core/net-sysfs.c +3 -1
@@ -1462,8 +1464,10 @@ static int netdev_queue_add_kobject(struct net_device *dev, int index)
 	kobj->kset = dev->queues_kset;
 	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
 				     "tx-%u", index);
-	if (error)
+	if (error) {
+		kobject_put(kobj);
 		return error;
+	}
 
 	dev_hold(queue->dev);

I think the same s/kobject_put/kobject_del/ substitution may be needed in this function when sysfs_create_group() fails.

Tyler

Quoted 4 lines
Signature · 2 lines
-- 
2.21.0
#12 lore raw

Re: [PATCH 3/3] net-sysfs: Fix error path for kobject_init_and_add()

In reply to Tobin C. Harding
Quoted 4 lines · Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

It's not obvious to me if this will help to fix what is stated in the (reverted) commit 6b70fc94afd1 ("net-sysfs: Fix memory leak in netdev_register_kobject")?

If so, perhaps we need to tell syzkaller guys about this.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH 1/3] bridge: Fix error path for kobject_init_and_add()

In reply to Tobin C. Harding

On Tue, Apr 30, 2019 at 10:28:15AM +1000, Tobin C. Harding wrote:

[snip]

Please do not consider this series for merge. There is a bit of confusion here.

There are a few of theses patches live on various LKML lists. Have to consolidate all the knowledge. When I _actually_ know how to use kobject correctly I'll re-spin.

Thanks for your patience.

Tobin

#13 lore raw

Re: [PATCH 3/3] net-sysfs: Fix error path for kobject_init_and_add()

In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 4 lines · Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

It's not obvious to me if this will help to fix what is stated in the (reverted) commit 6b70fc94afd1 ("net-sysfs: Fix memory leak in netdev_register_kobject")?

If so, perhaps we need to tell syzkaller guys about this.

Thanks for reminding. It seems that the bug has not been completely fixed.

in netdev_register_kobject():

1746         error = device_add(dev);
1747         if (error)
1748                 return error;
1749
1750         error = register_queue_kobjects(ndev); 1751         if (error) {
1752                 device_del(dev);
1753                 return error;
1754         }

This only fixes a memory leak after a failure in register_queue_kobjects(). If device_add() fails, kobject_put() still cann't be called, and the memory leak still exists.