The stride and word_size attributes in the nvmem_config struct are
currently only used when reading/writing through sysfs functions
bin_attr_nvmem_read/bin_attr_nvmem_write and in the nvmem_cell api.
Reads and writes with nvmem_device_write/nvmem_device_read still allow
unaligned access.

Add a check to these functions to enforce word_size and stride_length
aligned reads and writes.

Signed-off-by: Gregor Herburger <[email protected]>
---
 drivers/nvmem/core.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 311cb2e5a5c02..6b313f63d07ef 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -2068,6 +2068,12 @@ int nvmem_device_read(struct nvmem_device *nvmem,
        if (!nvmem)
                return -EINVAL;
 
+       if (!IS_ALIGNED(offset, nvmem->stride))
+               return -EINVAL;
+
+       if (!IS_ALIGNED(bytes, nvmem->word_size))
+               return -EINVAL;
+
        rc = nvmem_reg_read(nvmem, offset, buf, bytes);
 
        if (rc)
@@ -2096,6 +2102,12 @@ int nvmem_device_write(struct nvmem_device *nvmem,
        if (!nvmem)
                return -EINVAL;
 
+       if (!IS_ALIGNED(offset, nvmem->stride))
+               return -EINVAL;
+
+       if (!IS_ALIGNED(bytes, nvmem->word_size))
+               return -EINVAL;
+
        rc = nvmem_reg_write(nvmem, offset, buf, bytes);
 
        if (rc)

-- 
2.47.3


Reply via email to