public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] counter: Fix refcount leak in counter_alloc() error path
@ 2026-04-11 13:35 Guangshuo Li
  2026-04-13  7:47 ` Uwe Kleine-König
  0 siblings, 1 reply; 2+ messages in thread
From: Guangshuo Li @ 2026-04-11 13:35 UTC (permalink / raw)
  To: William Breathitt Gray, Greg Kroah-Hartman, Uwe Kleine-König,
	linux-iio, linux-kernel
  Cc: Guangshuo Li, stable

After device_initialize(), the lifetime of the embedded struct device
is expected to be managed through the device core reference counting.

In counter_alloc(), if dev_set_name() fails after device_initialize(),
the error path removes the chrdev, frees the ID, and frees the backing
allocation directly instead of releasing the device reference with
put_device(). This bypasses the normal device lifetime rules and may
leave the reference count of the embedded struct device unbalanced,
resulting in a refcount leak and potentially leading to a use-after-free.

Fix this by using put_device() in the dev_set_name() failure path and
let counter_device_release() handle the final cleanup.

Fixes: 4da08477ea1f ("counter: Set counter device name")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/counter/counter-core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
index 50bd30ba3d03..12dc18c78672 100644
--- a/drivers/counter/counter-core.c
+++ b/drivers/counter/counter-core.c
@@ -123,10 +123,10 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
 	return counter;
 
 err_dev_set_name:
+	put_device(dev);
+	return NULL;
 
-	counter_chrdev_remove(counter);
 err_chrdev_add:
-
 	ida_free(&counter_ida, dev->id);
 err_ida_alloc:
 
-- 
2.43.0


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

end of thread, other threads:[~2026-04-13  7:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-11 13:35 [PATCH] counter: Fix refcount leak in counter_alloc() error path Guangshuo Li
2026-04-13  7:47 ` Uwe Kleine-König

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