Skip to contentSkip to conversation

net-sysfs: revert wrong bugfix and re-fix

PATCH In review Wang Hai 5 participants 12 messages Apr 12, 2019 – Apr 15, 2019

Subsystems (2)
lore
Share & tools
lore

Wang Hai

author
#0 lore raw

cover net-sysfs: revert wrong bugfix and re-fix

First patch reverts the bugfix which cause another issue. Reported by [email protected]. See https://syzkaller.appspot.com/x/log.txt?x=1737671b200000 for details Second patch improves the first(reverted) patch.
Applied these two patches. I tested, the memory leak issue wasn't reproduced, and it didn't produce the bug which reported by syzkaller.

Wang Hai (2):
Revert "net-sysfs: Fix memory leak in netdev_register_kobject"
net-sysfs: Fix memory leak in netdev_register_kobject

 net/core/net-sysfs.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/2 Revert "net-sysfs: Fix memory leak in netdev_register_kobject"

This reverts commit 6b70fc94afd165342876e53fc4b2f7d085009945.

The reverted bugfix will cause another issue.
Reported by [email protected]. See https://syzkaller.appspot.com/x/log.txt?x=1737671b200000 for details.

---
 net/core/net-sysfs.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
net/core/net-sysfs.c +5 -9
index f8f9430..8f8b7b6 100644
@@ -1747,20 +1747,16 @@ int netdev_register_kobject(struct net_device *ndev)
 
 	error = device_add(dev);
 	if (error)
-		goto error_put_device;
+		return error;
 
 	error = register_queue_kobjects(ndev);
-	if (error)
-		goto error_device_del;
+	if (error) {
+		device_del(dev);
+		return error;
+	}
 
 	pm_runtime_set_memalloc_noio(dev, true);
 
-	return 0;
-
-error_device_del:
-	device_del(dev);
-error_put_device:
-	put_device(dev);
 	return error;
 }
 
Signature · 1 line
1.8.3.1
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] Revert "net-sysfs: Fix memory leak in netdev_register_kobject"

Quoted 7 lines · Wang Hai

This reverts commit 6b70fc94afd165342876e53fc4b2f7d085009945.

The reverted bugfix will cause another issue.
Reported by [email protected]. See https://syzkaller.appspot.com/x/log.txt?x=1737671b200000 for details.

Quoted 38 lines
---
 net/core/net-sysfs.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
net/core/net-sysfs.c +5 -9
index f8f9430..8f8b7b6 100644
@@ -1747,20 +1747,16 @@ int netdev_register_kobject(struct net_device *ndev)
 
 	error = device_add(dev);
 	if (error)
-		goto error_put_device;
+		return error;
 
 	error = register_queue_kobjects(ndev);
-	if (error)
-		goto error_device_del;
+	if (error) {
+		device_del(dev);
+		return error;
+	}
 
 	pm_runtime_set_memalloc_noio(dev, true);
 
-	return 0;
-
-error_device_del:
-	device_del(dev);
-error_put_device:
-	put_device(dev);
 	return error;
 }
 
Signature · 1 line
1.8.3.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw

Re: [PATCH 1/2] Revert "net-sysfs: Fix memory leak in netdev_register_kobject"

In reply to Wang Hai

From: Wang Hai <[email protected]>
Date: Fri, 12 Apr 2019 16:36:33 -0400

Quoted 8 lines

This reverts commit 6b70fc94afd165342876e53fc4b2f7d085009945.

The reverted bugfix will cause another issue.
Reported by [email protected]. See https://syzkaller.appspot.com/x/log.txt?x=1737671b200000 for details.

I'll just apply this by itself.

Thanks Wang.

author
#4 lore raw

PATCH 2/2 net-sysfs: Fix memory leak in netdev_register_kobject

In reply to Wang Hai

When registering struct net_device, it will call

        register_netdevice ->
                netdev_register_kobject ->
                        device_initialize(dev);
                        dev_set_name(dev, "%s", ndev->name)
                        device_add(dev)
                        register_queue_kobjects(ndev)

In netdev_register_kobject(), if device_add(dev) or register_queue_kobjects(ndev) failed. Register_netdevice() will return error, causing netdev_freemem(ndev) to be called to free net_device, however put_device(&dev->dev)->..-> kobject_cleanup() won't be called, resulting in a memory leak which is alloced by dev_set_name()

syzkaller report this:

BUG: memory leak

unreferenced object 0xffff8881f4fad168 (size 8):
comm "syz-executor.0", pid 3575, jiffies 4294778002 (age 20.134s) hex dump (first 8 bytes):
77 70 61 6e 30 00 ff ff wpan0...
backtrace:

  [<000000006d2d91d7>] kstrdup_const+0x3d/0x50 mm/util.c:73
  [<00000000ba9ff953>] kvasprintf_const+0x112/0x170 lib/kasprintf.c:48
  [<000000005555ec09>] kobject_set_name_vargs+0x55/0x130 lib/kobject.c:281
  [<0000000098d28ec3>] dev_set_name+0xbb/0xf0 drivers/base/core.c:1915
  [<00000000b7553017>] netdev_register_kobject+0xc0/0x410 net/core/net-sysfs.c:1727
  [<00000000c826a797>] register_netdevice+0xa51/0xeb0 net/core/dev.c:8711
  [<00000000857bfcfd>] cfg802154_update_iface_num.isra.2+0x13/0x90 [ieee802154]
  [<000000003126e453>] ieee802154_llsec_fill_key_id+0x1d5/0x570 [ieee802154]
  [<00000000e4b3df51>] 0xffffffffc1500e0e
  [<00000000b4319776>] platform_drv_probe+0xc6/0x180 drivers/base/platform.c:614
  [<0000000037669347>] really_probe+0x491/0x7c0 drivers/base/dd.c:509
  [<000000008fed8862>] driver_probe_device+0xdc/0x240 drivers/base/dd.c:671
  [<00000000baf52041>] device_driver_attach+0xf2/0x130 drivers/base/dd.c:945
  [<00000000c7cc8dec>] __driver_attach+0x10e/0x210 drivers/base/dd.c:1022
  [<0000000057a757c2>] bus_for_each_dev+0x154/0x1e0 drivers/base/bus.c:304
  [<000000005f5ae04b>] bus_add_driver+0x427/0x5e0 drivers/base/bus.c:645
Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
 net/core/net-sysfs.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
net/core/net-sysfs.c +9 -5
index 8f8b7b6..095cdce 100644
@@ -1747,16 +1747,20 @@ int netdev_register_kobject(struct net_device *ndev)
 
 	error = device_add(dev);
 	if (error)
-		return error;
+		goto error_device_add;
 
 	error = register_queue_kobjects(ndev);
-	if (error) {
-		device_del(dev);
-		return error;
-	}
+	if (error)
+		goto error_register;
 
 	pm_runtime_set_memalloc_noio(dev, true);
 
+	return 0;
+
+error_register:
+	device_del(dev);
+error_device_add:
+	kfree_const(dev->kobj.name);
 	return error;
 }
 
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [PATCH 2/2] net-sysfs: Fix memory leak in netdev_register_kobject

net/core/net-sysfs.c int netdev_register_kobject(struct net_device *ndev) · line 1764

Quoted 5 lines · Wang Hai
net/core/net-sysfs.c +4 -0
+error_register:
+	device_del(dev);
+error_device_add:
+	kfree_const(dev->kobj.name);
 	return error;

When put_device() will be called on this it will go to double free (in case of dynamically allocated dev->kobj.name.

Al Viro and me suggested earlier that the correct fix is to call put_device() in a places where it is appropriate.

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

Re: [PATCH 2/2] net-sysfs: Fix memory leak in netdev_register_kobject

Quoted 10 lines · Andy Shevchenko

net/core/net-sysfs.c int netdev_register_kobject(struct net_device *ndev) · line 1764

Quoted 5 lines · Wang Hai
net/core/net-sysfs.c +4 -0
+error_register:
+	device_del(dev);
+error_device_add:
+	kfree_const(dev->kobj.name);
  	return error;

When put_device() will be called on this it will go to double free (in case of dynamically allocated dev->kobj.name.

Quoted 2 lines

Al Viro and me suggested earlier that the correct fix is to call put_device() in a places where it is appropriate.

Thanks. I'll take a closer look at the code to see when it's time to call put_device(). It's really not easy to fix.

#7 lore raw

Re: [PATCH 2/2] net-sysfs: Fix memory leak in netdev_register_kobject

Quoted 12 lines · wanghai (M)
Quoted 10 lines · Andy Shevchenko

net/core/net-sysfs.c int netdev_register_kobject(struct net_device *ndev) · line 1764

Quoted 5 lines · Wang Hai
net/core/net-sysfs.c +4 -0
+error_register:
+	device_del(dev);
+error_device_add:
+	kfree_const(dev->kobj.name);
  	return error;

When put_device() will be called on this it will go to double free (in case of dynamically allocated dev->kobj.name.

Quoted 4 lines
Quoted 2 lines

Al Viro and me suggested earlier that the correct fix is to call put_device() in a places where it is appropriate.

Thanks. I'll take a closer look at the code to see when it's time to call put_device(). It's really not easy to fix.

Depends upon the driver, obviously... Note that if it's a built-in with device never destroyed, that allocation is no leak at all - reference to the object remains around.

#8 lore raw

Re: [PATCH 2/2] net-sysfs: Fix memory leak in netdev_register_kobject

In reply to Wang Hai
Quoted 8 lines · Wang Hai

When registering struct net_device, it will call

        register_netdevice ->
                netdev_register_kobject ->
                        device_initialize(dev);
                        dev_set_name(dev, "%s", ndev->name)
                        device_add(dev)
                        register_queue_kobjects(ndev)
Quoted 8 lines

In netdev_register_kobject(), if device_add(dev) or register_queue_kobjects(ndev) failed. Register_netdevice() will return error, causing netdev_freemem(ndev) to be called to free net_device, however put_device(&dev->dev)->..-> kobject_cleanup() won't be called, resulting in a memory leak which is alloced by dev_set_name()

Having two patches with exact same title is rather confusing for bug trackers.

Instead of revert + another_patch, why not just send a cumulative fix ?

#9 lore raw

Re: [PATCH 2/2] net-sysfs: Fix memory leak in netdev_register_kobject

Quoted 19 lines · Eric Dumazet
Quoted 8 lines · Wang Hai

When registering struct net_device, it will call

        register_netdevice ->
                netdev_register_kobject ->
                        device_initialize(dev);
                        dev_set_name(dev, "%s", ndev->name)
                        device_add(dev)
                        register_queue_kobjects(ndev)
Quoted 8 lines

In netdev_register_kobject(), if device_add(dev) or register_queue_kobjects(ndev) failed. Register_netdevice() will return error, causing netdev_freemem(ndev) to be called to free net_device, however put_device(&dev->dev)->..-> kobject_cleanup() won't be called, resulting in a memory leak which is alloced by dev_set_name()

Quoted 3 lines

Having two patches with exact same title is rather confusing for bug trackers.

Instead of revert + another_patch, why not just send a cumulative fix ?

The second patch is a reincarnation of the first version of the fix which has been discussed as not a correct approach. But revert should be applied sooner as the original commit brought a regression.

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

Re: [PATCH 2/2] net-sysfs: Fix memory leak in netdev_register_kobject

Quoted 19 lines · Andy Shevchenko
Quoted 18 lines · Eric Dumazet
Quoted 16 lines · Wang Hai

When registering struct net_device, it will call

         register_netdevice ->
                 netdev_register_kobject ->
                         device_initialize(dev);
                         dev_set_name(dev, "%s", ndev->name)
                         device_add(dev)
                         register_queue_kobjects(ndev)

In netdev_register_kobject(), if device_add(dev) or register_queue_kobjects(ndev) failed. Register_netdevice() will return error, causing netdev_freemem(ndev) to be called to free net_device, however put_device(&dev->dev)->..-> kobject_cleanup() won't be called, resulting in a memory leak which is alloced by dev_set_name()

Quoted 6 lines
Quoted 3 lines

Having two patches with exact same title is rather confusing for bug trackers.

Instead of revert + another_patch, why not just send a cumulative fix ?

The second patch is a reincarnation of the first version of the fix which has been discussed as not a correct approach. But revert should be applied sooner as the original commit brought a regression.

Thanks for letting me know. Now should I resend a separate revert, or do you just review the first patch?

#11 lore raw

Re: [PATCH 2/2] net-sysfs: Fix memory leak in netdev_register_kobject

In reply to Eric Dumazet
Quoted 19 lines · Eric Dumazet
Quoted 8 lines · Wang Hai

When registering struct net_device, it will call

         register_netdevice ->
                 netdev_register_kobject ->
                         device_initialize(dev);
                         dev_set_name(dev, "%s", ndev->name)
                         device_add(dev)
                         register_queue_kobjects(ndev)
Quoted 8 lines

In netdev_register_kobject(), if device_add(dev) or register_queue_kobjects(ndev) failed. Register_netdevice() will return error, causing netdev_freemem(ndev) to be called to free net_device, however put_device(&dev->dev)->..-> kobject_cleanup() won't be called, resulting in a memory leak which is alloced by dev_set_name()

Quoted 4 lines

Having two patches with exact same title is rather confusing for bug trackers.

Instead of revert + another_patch, why not just send a cumulative fix ?

Thanks, if I fix it, I will send a cumulative fix again