Skip to contentSkip to conversation

nvmem: core: add sanity check in nvmem_device_read()

PATCH New Bingbu Cao 1 participant 1 message Aug 14, 2020

Subsystems (2)
lore
Share & tools
lore

Bingbu Cao

author
#0 lore raw

PATCH v3 1/1 nvmem: core: add sanity check in nvmem_device_read()

nvmem device read/write could be called directly once nvmem device registered, the sanity check should be done before each nvmem_reg_read/write().

---
 drivers/nvmem/core.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/nvmem/core.c +6 -0
index 927eb5f6003f..09ad5a06efee 100644
@@ -69,6 +69,9 @@ static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
 			  void *val, size_t bytes)
 {
+	if (bytes + offset > nvmem->size)
+		return -EINVAL;
+
 	if (nvmem->reg_read)
 		return nvmem->reg_read(nvmem->priv, offset, val, bytes);
 
@@ -80,6 +83,9 @@ static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
 {
 	int ret;
 
+	if (bytes + offset > nvmem->size)
+		return -EINVAL;
+
 	if (nvmem->reg_write) {
 		gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
 		ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
Signature · 1 line
2.7.4