Skip to contentSkip to conversation

nvmem: core: add sanity check in nvmem_device_read()

PATCH Superseded Bingbu Cao 4 participants 5 messages Aug 4, 2020 NVMEM framework

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Bingbu Cao

author
#0 lore raw

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

nvmem_device_read() could be called directly once nvmem device registered, the sanity check should be done before call nvmem_reg_read() as cell and sysfs read did now.

---
 drivers/nvmem/core.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/nvmem/core.c +7 -0
index 927eb5f6003f..c9a77993f008 100644
@@ -1491,6 +1491,13 @@ int nvmem_device_read(struct nvmem_device *nvmem,
 	if (!nvmem)
 		return -EINVAL;
 
+	if (offset >= nvmem->size || bytes < nvmem->word_size)
+		return 0;
+
+	if (bytes + offset > nvmem->size)
+		bytes = nvmem->size - offset;
+
+	bytes = round_down(bytes, nvmem->word_size);
 	rc = nvmem_reg_read(nvmem, offset, buf, bytes);
 
 	if (rc)
Signature · 1 line
2.7.4
#1 lore raw

Hi Bingbu,

Thank you for the patch.

Quoted 14 lines · Bingbu Cao

nvmem_device_read() could be called directly once nvmem device registered, the sanity check should be done before call nvmem_reg_read() as cell and sysfs read did now.

---
 drivers/nvmem/core.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/nvmem/core.c +0 -0
index 927eb5f6003f..c9a77993f008 100644

drivers/nvmem/core.c int nvmem_device_read(struct nvmem_device *nvmem, · line 1498

Quoted 8 lines
drivers/nvmem/core.c +5 -0
@@ -1491,6 +1491,13 @@ int nvmem_device_read(struct nvmem_device *nvmem,
 	if (!nvmem)
 		return -EINVAL;
 
+	if (offset >= nvmem->size || bytes < nvmem->word_size)
+		return 0;
+
+	if (bytes + offset > nvmem->size)
+		bytes = nvmem->size - offset;

The check is relevant for nvmem_device_write(), too.

There are also other ways to access nvmem devices such as nvmem_cell_read and others alike. Should they be considered as well?

Quoted 5 lines
drivers/nvmem/core.c +2 -0
+
+	bytes = round_down(bytes, nvmem->word_size);
 	rc = nvmem_reg_read(nvmem, offset, buf, bytes);
 
 	if (rc)
Signature · 3 lines
Kind regards,

Sakari Ailus
#2 lore raw
Quoted 30 lines · Sakari Ailus

Hi Bingbu,

Thank you for the patch.

Quoted 14 lines · Bingbu Cao

nvmem_device_read() could be called directly once nvmem device registered, the sanity check should be done before call nvmem_reg_read() as cell and sysfs read did now.

---
  drivers/nvmem/core.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/nvmem/core.c +0 -0
index 927eb5f6003f..c9a77993f008 100644

drivers/nvmem/core.c int nvmem_device_read(struct nvmem_device *nvmem, · line 1498

Quoted 8 lines
drivers/nvmem/core.c +5 -0
@@ -1491,6 +1491,13 @@ int nvmem_device_read(struct nvmem_device *nvmem,
  	if (!nvmem)
  		return -EINVAL;
 
+	if (offset >= nvmem->size || bytes < nvmem->word_size)
+		return 0;
+
+	if (bytes + offset > nvmem->size)
+		bytes = nvmem->size - offset;

The check is relevant for nvmem_device_write(), too.

Quoted 2 lines

There are also other ways to access nvmem devices such as nvmem_cell_read and others alike. Should they be considered as well?

We should probably move these sanity checks to a common place like nvmem_reg_read() and nvmem_reg_write(), so the callers need not duplicate the same!

--srini

Quoted 7 lines
Quoted 6 lines
drivers/nvmem/core.c +2 -0
+
+	bytes = round_down(bytes, nvmem->word_size);
  	rc = nvmem_reg_read(nvmem, offset, buf, bytes);
 
  	if (rc)
#3 lore raw
Quoted 32 lines · Srinivas Kandagatla
Quoted 29 lines · Sakari Ailus

Hi Bingbu,

Thank you for the patch.

Quoted 14 lines · Bingbu Cao

nvmem_device_read() could be called directly once nvmem device registered, the sanity check should be done before call nvmem_reg_read() as cell and sysfs read did now.

---
  drivers/nvmem/core.c | 7 +++++++

  1 file changed, 7 insertions(+)
drivers/nvmem/core.c +0 -0
index 927eb5f6003f..c9a77993f008 100644

drivers/nvmem/core.c int nvmem_device_read(struct nvmem_device *nvmem, · line 1497

Quoted 7 lines
drivers/nvmem/core.c +4 -0
@@ -1491,6 +1491,13 @@ int nvmem_device_read(struct nvmem_device *nvmem,
       if (!nvmem)
           return -EINVAL;
   +    if (offset >= nvmem->size || bytes < nvmem->word_size)
+        return 0;
+
+    if (bytes + offset > nvmem->size)
+        bytes = nvmem->size - offset;

The check is relevant for nvmem_device_write(), too.

Quoted 6 lines
Quoted 2 lines

There are also other ways to access nvmem devices such as nvmem_cell_read and others alike. Should they be considered as well?

We should probably move these sanity checks to a common place like nvmem_reg_read() and nvmem_reg_write(), so the callers need not duplicate the same!

Srini and Sakari, thanks for your review.

Is it OK just return INVAL with simple check like below?

if (bytes + offset > nvmem->size ||

    bytes != round_down(bytes, nvmem->word_size))
        return -EINVAL;
Quoted 8 lines

--srini

Quoted 6 lines
Quoted 5 lines
drivers/nvmem/core.c +2 -0
+
+    bytes = round_down(bytes, nvmem->word_size);
       rc = nvmem_reg_read(nvmem, offset, buf, bytes);
         if (rc)
Signature · 2 lines
Best regards,
Bingbu Cao
#4 lore raw
Quoted 40 lines · Bingbu Cao
Quoted 32 lines · Srinivas Kandagatla
Quoted 29 lines · Sakari Ailus

Hi Bingbu,

Thank you for the patch.

Quoted 14 lines · Bingbu Cao

nvmem_device_read() could be called directly once nvmem device registered, the sanity check should be done before call nvmem_reg_read() as cell and sysfs read did now.

---
  drivers/nvmem/core.c | 7 +++++++

  1 file changed, 7 insertions(+)
drivers/nvmem/core.c +0 -0
index 927eb5f6003f..c9a77993f008 100644

drivers/nvmem/core.c int nvmem_device_read(struct nvmem_device *nvmem, · line 1497

Quoted 7 lines
drivers/nvmem/core.c +4 -0
@@ -1491,6 +1491,13 @@ int nvmem_device_read(struct nvmem_device *nvmem,
       if (!nvmem)
           return -EINVAL;
   +    if (offset >= nvmem->size || bytes < nvmem->word_size)
+        return 0;
+
+    if (bytes + offset > nvmem->size)
+        bytes = nvmem->size - offset;

The check is relevant for nvmem_device_write(), too.

Quoted 6 lines
Quoted 2 lines

There are also other ways to access nvmem devices such as nvmem_cell_read and others alike. Should they be considered as well?

We should probably move these sanity checks to a common place like nvmem_reg_read() and nvmem_reg_write(), so the callers need not duplicate the same!

Quoted 7 lines

Srini and Sakari, thanks for your review.

Is it OK just return INVAL with simple check like below?

if (bytes + offset > nvmem->size ||

    bytes != round_down(bytes, nvmem->word_size))
        return -EINVAL;

This changes what is currently supported so I'd say no.

Signature · 1 line
Sakari Ailus