nvmem: core: initialise nvmem->id early
authorRussell King (Oracle) <rmk+kernel@armlinux.org.uk>
Fri, 27 Jan 2023 10:40:08 +0000 (10:40 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 9 Feb 2023 10:26:45 +0000 (11:26 +0100)
commit 3bd747c7ea13cb145f0d84444e00df928b0842d9 upstream.

The error path for wp_gpio attempts to free the IDA nvmem->id, but
this has yet to be assigned, so will always be zero - leaking the
ID allocated by ida_alloc(). Fix this by moving the initialisation
of nvmem->id earlier.

Fixes: f7d8d7dcd978 ("nvmem: fix memory leak in error path")
Cc: stable@vger.kernel.org
Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Link: https://lore.kernel.org/r/20230127104015.23839-4-srinivas.kandagatla@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/nvmem/core.c

index 38bab84f3c8ae11be3eb5002c8032a63494916c1..2aa57dcf6a8a7da46d8c831a999f9604945746be 100644 (file)
@@ -766,6 +766,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
                return ERR_PTR(rval);
        }
 
+       nvmem->id = rval;
+
        if (config->wp_gpio)
                nvmem->wp_gpio = config->wp_gpio;
        else if (!config->ignore_wp)
@@ -781,7 +783,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
        kref_init(&nvmem->refcnt);
        INIT_LIST_HEAD(&nvmem->cells);
 
-       nvmem->id = rval;
        nvmem->owner = config->owner;
        if (!nvmem->owner && config->dev->driver)
                nvmem->owner = config->dev->driver->owner;