[PATCH 07/11] nvmem: distinguish layout cells from provider cells

sashiko-bot at kernel.org sashiko-bot at kernel.org
Tue Nov 17 11:51:58 PST 2026


Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 2 related pre-existing issues it does not
introduce:
- [Medium] nvmem: core: memory leak on error path in
  nvmem_add_cells_from_dt() [1]
- [Critical] nvmem: layouts: onie-tlv: out-of-bounds read in
  onie_tlv_add_cells() [2]
--

commit 0c2f98425a6e2037eca692b433e46249d579c5ee
Author: James Hilliard <james.hilliard1 at gmail.com>

nvmem: distinguish layout cells from provider cells

This commit records the owning layout on NVMEM cell entries and adds
layout-specific cell registration helpers so layout cells can be
distinguished from provider cells.

> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 14cc70e8ce785..57c1da5ab547c 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c

[ ... ]

> @@ -882,7 +907,8 @@ static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
>  	return 0;
>  }
>  
> -int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
> +static int __nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np,
> +				     struct nvmem_layout *layout)
>  {
>  	struct device *dev = &nvmem->dev;
>  	const __be32 *addr;
> @@ -924,7 +950,7 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
>  		if (nvmem->fixup_dt_cell_info)
>  			nvmem->fixup_dt_cell_info(nvmem, &info);
>  
> -		ret = nvmem_add_one_cell(nvmem, &info);
> +		ret = __nvmem_add_one_cell(nvmem, &info, layout);
>  		kfree(info.name);
>  		if (ret) {
>  			of_node_put(info.np);

[Severity: Medium]
This problem wasn't introduced by this patch, but does
__nvmem_add_cells_from_dt() leak info.name [1] when the "bits"
property is invalid?

drivers/nvmem/core.c:__nvmem_add_cells_from_dt() {
	...
		info.name = kasprintf(GFP_KERNEL, "%pOFn", child);

		addr = of_get_property(child, "bits", &len);
		if (addr && len == (2 * sizeof(u32))) {
			info.bit_offset = be32_to_cpup(addr++);
			info.nbits = be32_to_cpup(addr);
			if (info.bit_offset >= BITS_PER_BYTE * info.bytes ||
			    info.nbits < 1 ||
			    info.bit_offset + info.nbits > BITS_PER_BYTE * info.bytes) {
				dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
				return -EINVAL;
			}
		}
	...
}

Returning -EINVAL directly skips kfree(info.name).

[ ... ]

> diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-tlv.c
> index 4da3a704388e9..99e7a5406b4ce 100644
> --- a/drivers/nvmem/layouts/onie-tlv.c
> +++ b/drivers/nvmem/layouts/onie-tlv.c
> @@ -95,18 +95,19 @@ static nvmem_cell_post_process_t onie_tlv_read_cb(u8 type, u8 *buf)
>  	return NULL;
>  }
>  
> -static int onie_tlv_add_cells(struct device *dev, struct nvmem_device *nvmem,
> +static int onie_tlv_add_cells(struct nvmem_layout *layout,
>  			      size_t data_len, u8 *data)
>  {
> +	struct device *dev = &layout->dev;
>  	struct nvmem_cell_info cell = {};
> -	struct device_node *layout;
> +	struct device_node *layout_np;
>  	struct onie_tlv tlv;
>  	unsigned int hdr_len = sizeof(struct onie_tlv_hdr);
>  	unsigned int offset = 0;
>  	int ret;
>  
> -	layout = of_nvmem_layout_get_container(nvmem);
> -	if (!layout)
> +	layout_np = of_nvmem_layout_get_container(layout->nvmem);
> +	if (!layout_np)
>  		return -ENOENT;
>  
>  	while (offset < data_len) {
> @@ -123,13 +124,13 @@ static int onie_tlv_add_cells(struct device *dev, struct nvmem_device *nvmem,
>  
>  		cell.offset = hdr_len + offset + sizeof(tlv.type) + sizeof(tlv.len);
>  		cell.bytes = tlv.len;
> -		cell.np = of_get_child_by_name(layout, cell.name);
> +		cell.np = of_get_child_by_name(layout_np, cell.name);
>  		cell.read_post_process = onie_tlv_read_cb(tlv.type, data + offset + sizeof(tlv));
>  
> -		ret = nvmem_add_one_cell(nvmem, &cell);
> +		ret = nvmem_layout_add_cell(layout, &cell);

[Severity: Critical]
This problem wasn't introduced by this patch, but can a malformed ONIE TLV
table trigger an out-of-bounds read in onie_tlv_add_cells() [2]?

drivers/nvmem/layouts/onie-tlv.c:onie_tlv_add_cells() {
	...
	while (offset < data_len) {
		memcpy(&tlv, data + offset, sizeof(tlv));
		if (offset + tlv.len >= data_len) {
	...
}

Neither the loop condition nor the bounds check accounts for sizeof(tlv).
If fewer than sizeof(tlv) bytes remain in data, memcpy() reads past the end
of the heap buffer, and a truncated entry with offset + sizeof(tlv) +
tlv.len > data_len can still pass the check and register a cell extending
past the table.

[1] https://sashiko.dev/#/bug/linux-c0ac748f-5eb4-43f2-ad9b-c1ed0e5f7c40
[2] https://sashiko.dev/#/bug/linux-dabf598f-cde5-44cc-a226-fd0a93723e20

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com?part=7



More information about the linux-mtd mailing list