Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: trigger: sysfs: fix possible memory leak in iio_sysfs_trig_init()
@ 2022-10-22  7:42 Yang Yingliang
  2022-10-23 11:02 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Yang Yingliang @ 2022-10-22  7:42 UTC (permalink / raw)
  To: linux-iio; +Cc: jic23, jonathan.cameron, yangyingliang

dev_set_name() allocates memory for name, it need be freed
when device_add() fails, call put_device() to give up the
reference that hold in device_initialize(), so that it can
be freed in kobject_cleanup() when the refcount hit to 0.

Fault injection test can trigger this:

unreferenced object 0xffff8e8340a7b4c0 (size 32):
  comm "modprobe", pid 243, jiffies 4294678145 (age 48.845s)
  hex dump (first 32 bytes):
    69 69 6f 5f 73 79 73 66 73 5f 74 72 69 67 67 65  iio_sysfs_trigge
    72 00 a7 40 83 8e ff ff 00 86 13 c4 f6 ee ff ff  r..@............
  backtrace:
    [<0000000074999de8>] __kmem_cache_alloc_node+0x1e9/0x360
    [<00000000497fd30b>] __kmalloc_node_track_caller+0x44/0x1a0
    [<000000003636c520>] kstrdup+0x2d/0x60
    [<0000000032f84da2>] kobject_set_name_vargs+0x1e/0x90
    [<0000000092efe493>] dev_set_name+0x4e/0x70

Fixes: 1f785681a870 ("staging:iio:trigger sysfs userspace trigger rework.")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/iio/trigger/iio-trig-sysfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/trigger/iio-trig-sysfs.c b/drivers/iio/trigger/iio-trig-sysfs.c
index d6c5e9644738..6b05eed41612 100644
--- a/drivers/iio/trigger/iio-trig-sysfs.c
+++ b/drivers/iio/trigger/iio-trig-sysfs.c
@@ -203,9 +203,13 @@ static int iio_sysfs_trigger_remove(int id)
 
 static int __init iio_sysfs_trig_init(void)
 {
+	int ret;
 	device_initialize(&iio_sysfs_trig_dev);
 	dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger");
-	return device_add(&iio_sysfs_trig_dev);
+	ret = device_add(&iio_sysfs_trig_dev);
+	if (ret)
+		put_device(&iio_sysfs_trig_dev);
+	return ret;
 }
 module_init(iio_sysfs_trig_init);
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] iio: trigger: sysfs: fix possible memory leak in iio_sysfs_trig_init()
  2022-10-22  7:42 [PATCH] iio: trigger: sysfs: fix possible memory leak in iio_sysfs_trig_init() Yang Yingliang
@ 2022-10-23 11:02 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2022-10-23 11:02 UTC (permalink / raw)
  To: Yang Yingliang; +Cc: linux-iio, jonathan.cameron

On Sat, 22 Oct 2022 15:42:12 +0800
Yang Yingliang <yangyingliang@huawei.com> wrote:

> dev_set_name() allocates memory for name, it need be freed
> when device_add() fails, call put_device() to give up the
> reference that hold in device_initialize(), so that it can
> be freed in kobject_cleanup() when the refcount hit to 0.
> 
> Fault injection test can trigger this:
> 
> unreferenced object 0xffff8e8340a7b4c0 (size 32):
>   comm "modprobe", pid 243, jiffies 4294678145 (age 48.845s)
>   hex dump (first 32 bytes):
>     69 69 6f 5f 73 79 73 66 73 5f 74 72 69 67 67 65  iio_sysfs_trigge
>     72 00 a7 40 83 8e ff ff 00 86 13 c4 f6 ee ff ff  r..@............
>   backtrace:
>     [<0000000074999de8>] __kmem_cache_alloc_node+0x1e9/0x360
>     [<00000000497fd30b>] __kmalloc_node_track_caller+0x44/0x1a0
>     [<000000003636c520>] kstrdup+0x2d/0x60
>     [<0000000032f84da2>] kobject_set_name_vargs+0x1e/0x90
>     [<0000000092efe493>] dev_set_name+0x4e/0x70
> 
> Fixes: 1f785681a870 ("staging:iio:trigger sysfs userspace trigger rework.")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Applied to the fixes-togreg branch of iio.git and marked for stable.

Thanks,

Jonathan

> ---
>  drivers/iio/trigger/iio-trig-sysfs.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/trigger/iio-trig-sysfs.c b/drivers/iio/trigger/iio-trig-sysfs.c
> index d6c5e9644738..6b05eed41612 100644
> --- a/drivers/iio/trigger/iio-trig-sysfs.c
> +++ b/drivers/iio/trigger/iio-trig-sysfs.c
> @@ -203,9 +203,13 @@ static int iio_sysfs_trigger_remove(int id)
>  
>  static int __init iio_sysfs_trig_init(void)
>  {
> +	int ret;
>  	device_initialize(&iio_sysfs_trig_dev);
>  	dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger");
> -	return device_add(&iio_sysfs_trig_dev);
> +	ret = device_add(&iio_sysfs_trig_dev);
> +	if (ret)
> +		put_device(&iio_sysfs_trig_dev);
> +	return ret;
>  }
>  module_init(iio_sysfs_trig_init);
>  


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-23 11:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-22  7:42 [PATCH] iio: trigger: sysfs: fix possible memory leak in iio_sysfs_trig_init() Yang Yingliang
2022-10-23 11:02 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox