nvmem: core: add sanity check in nvmem_device_read()
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
@@ -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
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 callnvmem_reg_read()as cell and sysfs read did now.Signed-off-by Bingbu Cao <[email protected]>--- drivers/nvmem/core.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/nvmem/core.c +0
-0
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
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 callnvmem_reg_read()as cell and sysfs read did now.Signed-off-by Bingbu Cao <[email protected]>--- drivers/nvmem/core.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/nvmem/core.c +0
-0drivers/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_readand 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)
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 callnvmem_reg_read()as cell and sysfs read did now.Signed-off-by Bingbu Cao <[email protected]>---
drivers/nvmem/core.c| 7 +++++++1 file changed, 7 insertions(+)drivers/nvmem/core.c +0
-0drivers/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_readand others alike. Should they be considered as well?We should probably move these sanity checks to a common place like
nvmem_reg_read()andnvmem_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
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 callnvmem_reg_read()as cell and sysfs read did now.Signed-off-by Bingbu Cao <[email protected]>---
drivers/nvmem/core.c| 7 +++++++1 file changed, 7 insertions(+)drivers/nvmem/core.c +0
-0drivers/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_readand others alike. Should they be considered as well?We should probably move these sanity checks to a common place like
nvmem_reg_read()andnvmem_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