kernel: bump 5.10 to 5.10.167
[openwrt/openwrt.git] / target / linux / generic / backport-5.10 / 811-v6.1-0001-nvmem-core-Fix-memleak-in-nvmem_register.patch
1 From bd1244561fa2a4531ded40dbf09c9599084f8b29 Mon Sep 17 00:00:00 2001
2 From: Gaosheng Cui <cuigaosheng1@huawei.com>
3 Date: Fri, 16 Sep 2022 13:04:02 +0100
4 Subject: [PATCH] nvmem: core: Fix memleak in nvmem_register()
5
6 dev_set_name will alloc memory for nvmem->dev.kobj.name in
7 nvmem_register, when nvmem_validate_keepouts failed, nvmem's
8 memory will be freed and return, but nobody will free memory
9 for nvmem->dev.kobj.name, there will be memleak, so moving
10 nvmem_validate_keepouts() after device_register() and let
11 the device core deal with cleaning name in error cases.
12
13 Fixes: de0534df9347 ("nvmem: core: fix error handling while validating keepout regions")
14 Cc: stable@vger.kernel.org
15 Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
16 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
17 Link: https://lore.kernel.org/r/20220916120402.38753-1-srinivas.kandagatla@linaro.org
18 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
19 ---
20 drivers/nvmem/core.c | 15 ++++++---------
21 1 file changed, 6 insertions(+), 9 deletions(-)
22
23 --- a/drivers/nvmem/core.c
24 +++ b/drivers/nvmem/core.c
25 @@ -829,21 +829,18 @@ struct nvmem_device *nvmem_register(cons
26 nvmem->dev.groups = nvmem_dev_groups;
27 #endif
28
29 - if (nvmem->nkeepout) {
30 - rval = nvmem_validate_keepouts(nvmem);
31 - if (rval) {
32 - ida_free(&nvmem_ida, nvmem->id);
33 - kfree(nvmem);
34 - return ERR_PTR(rval);
35 - }
36 - }
37 -
38 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
39
40 rval = device_register(&nvmem->dev);
41 if (rval)
42 goto err_put_device;
43
44 + if (nvmem->nkeepout) {
45 + rval = nvmem_validate_keepouts(nvmem);
46 + if (rval)
47 + goto err_device_del;
48 + }
49 +
50 if (config->compat) {
51 rval = nvmem_sysfs_setup_compat(nvmem, config);
52 if (rval)