Skip to content

Commit 8356671

Browse files
diandersgregkh
authored andcommitted
nvmem: Enforce nvmem stride in the sysfs interface
The 'struct nvmem_config' has a stride attribute that specifies the needed alignment for accesses into the nvmem. This is used in nvmem_cell_info_to_nvmem_cell() but not in the sysfs read/write functions. If the alignment is important in one place it's important everywhere, so let's add enforcement. For now we'll consider it totally invalid to access with the wrong alignment. We could relax this in the read case where we could just read some extra bytes and throw them away. Relaxing it in the write case seems harder (and less safe?) since we'd have to read some data first and then write it back. To keep it symmetric we'll just disallow it in both cases. Reported-by: Ravi Kumar Bokka <[email protected]> Signed-off-by: Douglas Anderson <[email protected]> Reviewed-by: Ravi Kumar Bokka <[email protected]> Tested-by: Ravi Kumar Bokka <[email protected]> Signed-off-by: Srinivas Kandagatla <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent a9c4a15 commit 8356671

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

drivers/nvmem/core.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
135135
if (pos >= nvmem->size)
136136
return 0;
137137

138+
if (!IS_ALIGNED(pos, nvmem->stride))
139+
return -EINVAL;
140+
138141
if (count < nvmem->word_size)
139142
return -EINVAL;
140143

@@ -172,6 +175,9 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
172175
if (pos >= nvmem->size)
173176
return -EFBIG;
174177

178+
if (!IS_ALIGNED(pos, nvmem->stride))
179+
return -EINVAL;
180+
175181
if (count < nvmem->word_size)
176182
return -EINVAL;
177183

0 commit comments

Comments
 (0)