]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
counter: Fix refcount leak in counter_alloc() error path
authorGuangshuo Li <lgs201920130244@gmail.com>
Mon, 13 Apr 2026 13:46:04 +0000 (21:46 +0800)
committerWilliam Breathitt Gray <wbg@kernel.org>
Sun, 3 May 2026 04:48:39 +0000 (13:48 +0900)
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.

The issue was identified by a static analysis tool I developed and
confirmed by manual review.

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>
Link: https://lore.kernel.org/r/20260413134604.2861772-1-lgs201920130244@gmail.com
Signed-off-by: William Breathitt Gray <wbg@kernel.org>
drivers/counter/counter-core.c

index 50bd30ba3d03775b7e67d96d80e8e56d979b684d..0b1dac61b7b56d796160675f711eb084dfb4d577 100644 (file)
@@ -124,7 +124,8 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
 
 err_dev_set_name:
 
-       counter_chrdev_remove(counter);
+       put_device(dev);
+       return NULL;
 err_chrdev_add:
 
        ida_free(&counter_ida, dev->id);