]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
driver core: faux: fix root device registration
authorJohan Hovold <johan@kernel.org>
Fri, 24 Apr 2026 15:31:26 +0000 (17:31 +0200)
committerDanilo Krummrich <dakr@kernel.org>
Sat, 2 May 2026 15:50:31 +0000 (17:50 +0200)
A recent change made the faux bus root device be allocated dynamically
but failed to provide a release function to free the memory when the
last reference is dropped (on theoretical failure to register the device
or bus).

Fix this by using root_device_register() instead of open coding.

Also add the missing sanity check when registering faux devices to avoid
use-after-free if the bus failed to register (which would previously
have triggered a bunch of use-after-free warnings).

Fixes: 61b76d07d2b4 ("driver core: faux: stop using static struct device")
Cc: stable@vger.kernel.org # 7.0
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Johan Hovold <johan@kernel.org>
Link: https://patch.msgid.link/20260424153127.2647405-2-johan@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
drivers/base/faux.c

index fb3e42f213624ac8854764f58e81b9c2bc58cc86..3d1d1eafb47330548707c8a70d957c63be06dfeb 100644 (file)
@@ -133,6 +133,9 @@ struct faux_device *faux_device_create_with_groups(const char *name,
        struct device *dev;
        int ret;
 
+       if (!faux_bus_root)
+               return NULL;
+
        faux_obj = kzalloc_obj(*faux_obj);
        if (!faux_obj)
                return NULL;
@@ -232,19 +235,12 @@ EXPORT_SYMBOL_GPL(faux_device_destroy);
 
 int __init faux_bus_init(void)
 {
+       struct device *root;
        int ret;
 
-       faux_bus_root = kzalloc_obj(*faux_bus_root);
-       if (!faux_bus_root)
-               return -ENOMEM;
-
-       dev_set_name(faux_bus_root, "faux");
-
-       ret = device_register(faux_bus_root);
-       if (ret) {
-               put_device(faux_bus_root);
-               return ret;
-       }
+       root = root_device_register("faux");
+       if (IS_ERR(root))
+               return PTR_ERR(root);
 
        ret = bus_register(&faux_bus_type);
        if (ret)
@@ -254,12 +250,14 @@ int __init faux_bus_init(void)
        if (ret)
                goto error_driver;
 
+       faux_bus_root = root;
+
        return ret;
 
 error_driver:
        bus_unregister(&faux_bus_type);
 
 error_bus:
-       device_unregister(faux_bus_root);
+       root_device_unregister(root);
        return ret;
 }