Skip to content

Commit fc9eec4

Browse files
vkochan-plvgregkh
authored andcommitted
nvmem: core: fix possibly memleak when use nvmem_cell_info_to_nvmem_cell()
Fix missing 'kfree_const(cell->name)' when call to nvmem_cell_info_to_nvmem_cell() in several places: * after nvmem_cell_info_to_nvmem_cell() failed during nvmem_add_cells() * during nvmem_device_cell_{read,write} when cell->name is kstrdup'ed() without calling kfree_const() at the end, but really there is no reason to do that 'dup, because the cell instance is allocated on the stack for some short period to be read/write without exposing it to the caller. So the new nvmem_cell_info_to_nvmem_cell_nodup() helper is introduced which is used to convert cell_info -> cell without name duplication as a lighweight version of nvmem_cell_info_to_nvmem_cell(). Fixes: e2a5402 ("nvmem: Add nvmem_device based consumer apis.") Reviewed-by: Srinivas Kandagatla <[email protected]> Acked-by: Srinivas Kandagatla <[email protected]> Signed-off-by: Vadym Kochan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 709ec3f commit fc9eec4

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

drivers/nvmem/core.c

+24-9
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,14 @@ static void nvmem_cell_add(struct nvmem_cell *cell)
361361
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
362362
}
363363

364-
static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
365-
const struct nvmem_cell_info *info,
366-
struct nvmem_cell *cell)
364+
static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem,
365+
const struct nvmem_cell_info *info,
366+
struct nvmem_cell *cell)
367367
{
368368
cell->nvmem = nvmem;
369369
cell->offset = info->offset;
370370
cell->bytes = info->bytes;
371-
cell->name = kstrdup_const(info->name, GFP_KERNEL);
372-
if (!cell->name)
373-
return -ENOMEM;
371+
cell->name = info->name;
374372

375373
cell->bit_offset = info->bit_offset;
376374
cell->nbits = info->nbits;
@@ -382,13 +380,30 @@ static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
382380
if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
383381
dev_err(&nvmem->dev,
384382
"cell %s unaligned to nvmem stride %d\n",
385-
cell->name, nvmem->stride);
383+
cell->name ?: "<unknown>", nvmem->stride);
386384
return -EINVAL;
387385
}
388386

389387
return 0;
390388
}
391389

390+
static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
391+
const struct nvmem_cell_info *info,
392+
struct nvmem_cell *cell)
393+
{
394+
int err;
395+
396+
err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell);
397+
if (err)
398+
return err;
399+
400+
cell->name = kstrdup_const(info->name, GFP_KERNEL);
401+
if (!cell->name)
402+
return -ENOMEM;
403+
404+
return 0;
405+
}
406+
392407
/**
393408
* nvmem_add_cells() - Add cell information to an nvmem device
394409
*
@@ -1463,7 +1478,7 @@ ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
14631478
if (!nvmem)
14641479
return -EINVAL;
14651480

1466-
rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1481+
rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
14671482
if (rc)
14681483
return rc;
14691484

@@ -1493,7 +1508,7 @@ int nvmem_device_cell_write(struct nvmem_device *nvmem,
14931508
if (!nvmem)
14941509
return -EINVAL;
14951510

1496-
rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1511+
rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
14971512
if (rc)
14981513
return rc;
14991514

0 commit comments

Comments
 (0)