Skip to contentSkip to conversation

nvmem: core: improve range check for nvmem_cell_write()

PATCH Superseded Jennifer Berringer 2 participants 4 messages Oct 17, 2024 – Oct 19, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 nvmem: core: improve range check for nvmem_cell_write()

When __nvmem_cell_entry_write() is called for an nvmem cell that does not need bit shifting, it requires that the len parameter exactly matches the nvmem cell size. However, when the nvmem cell has a nonzero bit_offset, it was skipping this check.

Accepting values of len larger than the cell size results in nvmem_cell_prepare_write_buffer() trying to write past the end of a heap buffer that it allocates. This patch adds a check to avoid that problem and instead return -EINVAL when len is too large.

Rather than unconditionally checking that len exactly matches the nvmem cell size, allowing len to be smaller when bit shifts are involved may be helpful because some devices have nvmem cells that are less than 8 bits but span two bytes, although no current devices or drivers that do this seem to rely on nvmem_cell_write(). This possibility can be handled by nvmem_cell_prepare_write_buffer() because it allocates an appropriately-sized heap buffer and avoids reading past the end of buf.

Fixes 69aba7948cbe ("nvmem: Add a simple NVMEM framework for consumers")
---
 drivers/nvmem/core.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/nvmem/core.c +5 -2
index 33ffa2aa4c11..74bf4d35a7a7 100644
@@ -1767,8 +1767,7 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
 	struct nvmem_device *nvmem = cell->nvmem;
 	int rc;
 
-	if (!nvmem || nvmem->read_only ||
-	    (cell->bit_offset == 0 && len != cell->bytes))
+	if (!nvmem || nvmem->read_only)
 		return -EINVAL;
 
 	/*
@@ -1780,9 +1779,13 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
 		return -EINVAL;
 
 	if (cell->bit_offset || cell->nbits) {
+		if (len > cell->bytes)
+			return -EINVAL;
 		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
 		if (IS_ERR(buf))
 			return PTR_ERR(buf);
+	} else if (len != cell->bytes) {
+		return -EINVAL;
 	}
 
 	rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);

base-commit: 98f7e32f20d28ec452afb208f9cffc08448a2652

Signature · 1 line
2.46.2
#1 lore raw

PATCH 2/3 nvmem: core: add nvmem_cell_write_variable_u32()

This function allows nvmem consumers to write values of different sizes (1-4 bytes) to an nvmem cell without knowing the exact size, akin to a write counterpart to nvmem_cell_read_variable_le_32(). It discards the higher order bytes of the passed u32 value based on CPU endianness as necessary before writing to a cell smaller than 4 bytes.

---
 drivers/nvmem/core.c           | 24 ++++++++++++++++++++++++
 include/linux/nvmem-consumer.h |  1 +
 2 files changed, 25 insertions(+)
drivers/nvmem/core.c +24 -0
index 74bf4d35a7a7..6f7aa2beb457 100644
@@ -1816,6 +1816,30 @@ int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
 
 EXPORT_SYMBOL_GPL(nvmem_cell_write);
 
+/**
+ * nvmem_cell_write_variable_u32() - Write up to 32-bits of data as a host-endian number
+ *
+ * @cell: nvmem cell to be written.
+ * @val: Value to be written which may be truncated.
+ *
+ * Return: length of bytes written or negative on failure.
+ */
+int nvmem_cell_write_variable_u32(struct nvmem_cell *cell, u32 val)
+{
+	struct nvmem_cell_entry *entry = cell->entry;
+	u8 *buf = (u8 *) &val;
+
+	if (!entry || entry->bytes > sizeof(u32))
+		return -EINVAL;
+
+#ifdef __BIG_ENDIAN
+	buf += sizeof(u32) - entry->bytes;
+#endif
+
+	return __nvmem_cell_entry_write(entry, buf, entry->bytes);
+}
+EXPORT_SYMBOL_GPL(nvmem_cell_write_variable_u32);
+
 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
 				  void *val, size_t count)
 {
include/linux/nvmem-consumer.h +1 -0
index 34c0e58dfa26..d3ede6916ecb 100644
@@ -56,6 +56,7 @@ void nvmem_cell_put(struct nvmem_cell *cell);
 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len);
 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len);
+int nvmem_cell_write_variable_u32(struct nvmem_cell *cell, u32 val);
 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val);
 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val);
 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val);
Signature · 1 line
2.46.2
#2 lore raw

PATCH 3/3 power: reset: nvmem-reboot-mode: fix write for small cells

In reply to Jennifer Berringer

Some devices, such as Qualcomm sa8775p, have an nvmem reboot mode cell that is smaller than 32 bits, which resulted in nvmem_reboot_mode_write() failing. Using nvmem_cell_write_variable_u32() fixes this by writing only the least-significant byte of the magic value when the size specified in device tree is only one byte.

---
 drivers/power/reset/nvmem-reboot-mode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/reset/nvmem-reboot-mode.c +1 -1
index 41530b70cfc4..b52eb879d1c1 100644
@@ -24,7 +24,7 @@ static int nvmem_reboot_mode_write(struct reboot_mode_driver *reboot,
 
 	nvmem_rbm = container_of(reboot, struct nvmem_reboot_mode, reboot);
 
-	ret = nvmem_cell_write(nvmem_rbm->cell, &magic, sizeof(magic));
+	ret = nvmem_cell_write_variable_u32(nvmem_rbm->cell, magic);
 	if (ret < 0)
 		dev_err(reboot->dev, "update reboot mode bits failed\n");
 
Signature · 1 line
2.46.2
#3 lore raw

Re: [PATCH 3/3] power: reset: nvmem-reboot-mode: fix write for small cells

kernel test robot noticed the following build errors:

Hi Jennifer,

kernel test robot noticed the following build errors:

[auto build test ERROR on 98f7e32f20d28ec452afb208f9cffc08448a2652]

url:    https://github.com/intel-lab-lkp/linux/commits/Jennifer-Berringer/nvmem-core-add-nvmem_cell_write_variable_u32/20241018-001140
base:   98f7e32f20d28ec452afb208f9cffc08448a2652
patch link:    https://lore.kernel.org/r/20241017160904.2803663-3-jberring%40redhat.com
patch subject: [PATCH 3/3] power: reset: nvmem-reboot-mode: fix write for small cells
config: i386-randconfig-015-20241019 (https://download.01.org/0day-ci/archive/20241019/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241019/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/power/reset/nvmem-reboot-mode.c: In function 'nvmem_reboot_mode_write':

>> drivers/power/reset/nvmem-reboot-mode.c:27:15: error: implicit declaration of function 'nvmem_cell_write_variable_u32'; did you mean 'nvmem_cell_read_variable_le_u32'? [-Werror=implicit-function-declaration]
      27 |         ret = nvmem_cell_write_variable_u32(nvmem_rbm->cell, magic);
         |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |               nvmem_cell_read_variable_le_u32

cc1: some warnings being treated as errors

vim +27 drivers/power/reset/nvmem-reboot-mode.c

    18	
    19	static int nvmem_reboot_mode_write(struct reboot_mode_driver *reboot,
    20					    unsigned int magic)
    21	{
    22		int ret;
    23		struct nvmem_reboot_mode *nvmem_rbm;
    24	
    25		nvmem_rbm = container_of(reboot, struct nvmem_reboot_mode, reboot);
    26	
  > 27		ret = nvmem_cell_write_variable_u32(nvmem_rbm->cell, magic);
    28		if (ret < 0)
    29			dev_err(reboot->dev, "update reboot mode bits failed\n");
    30	
    31		return ret;
    32	}
    33	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki