nvmem: core: fix error path in nvmem_add_cells()
PATCH 1/1 nvmem: core: fix error path in nvmem_add_cells()
The current code fails to nvmem_cell_drop(cells[0]) - even worse, if the loop above fails already at i==0, we'll enter an essentially infinite loop doing nvmem_cell_drop on cells[-1], cells[-2], ... which is unlikely to end well.
Also, we're not freeing the temporary backing array cells on the error path.
--- drivers/nvmem/core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvmem/core.c +3 -1
@@ -288,9 +288,11 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,return 0 ; err: -while (--i) +while (i--)nvmem_cell_drop (cells[i]); +kfree (cells); +return rval; }
Signature · 1 line
2.1.4
Hi Rasmus,
Thanks for the patch,
Quoted 4 lines · Rasmus Villemoes
The current code fails to
nvmem_cell_drop(cells[0])- even worse, if the loop above fails already at i==0, we'll enter an essentially infinite loop doingnvmem_cell_dropon cells[-1], cells[-2], ... which is unlikely to end well.
I agree, it would fail in case of zero.
Quoted 14 lines
Also, we're not freeing the temporary backing array cells on the error path.
Signed-off-by Rasmus Villemoes <[email protected]>--- drivers/nvmem/core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/nvmem/core.c +0
-0
drivers/nvmem/core.c static int nvmem_add_cells(struct nvmem_device *nvmem, · line 292
Quoted 6 lines
drivers/nvmem/core.c +1
-1@@ -288,9 +288,11 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,return 0 ; err: -while (--i) +while (i--)nvmem_cell_drop (cells[i]);
No, this will not work.
3 issues,
1> If we enter this err path from nvmem_cell_info_to_nvmem_cell() failures, you would be accessing already freed cells[i].
2> accessing un-allocated cells[i].
3> you would be trying to drop cells which are not in the list.
This is what you need here to fix it correctly.
while (--i >= 0)
drivers/nvmem/core.c static int nvmem_add_cells(struct nvmem_device *nvmem, · line 294
Quoted 2 lines
drivers/nvmem/core.c +1
-0+kfree (cells);
This change looks good.
drivers/nvmem/core.c static int nvmem_add_cells(struct nvmem_device *nvmem,
Quoted 5 lines
drivers/nvmem/core.c +1
-0+return rval; }
--srini
Quoted 11 lines · Srinivas Kandagatla
drivers/nvmem/core.c static int nvmem_add_cells(struct nvmem_device *nvmem, · line 292
Quoted 4 lines
err:
drivers/nvmem/core.c +1
-1-while (--i) +while (i--)nvmem_cell_drop (cells[i]);No, this will not work.
3 issues,
1> If we enter this err path from
nvmem_cell_info_to_nvmem_cell()failures, you would be accessing already freed cells[i].
Quoted 9 lines
2> accessing un-allocated cells[i].
3> you would be trying to drop cells which are not in the list.
This is what you need here to fix it correctly.
while (--i >= 0)
Sigh. http://thread.gmane.org/gmane.linux.kernel.mm/146058/focus=2149595
TL;DR: They're equivalent.
Rasmus
Quoted 8 lines · Rasmus Villemoes
Quoted 6 lines · Srinivas Kandagatla
drivers/nvmem/core.c static int nvmem_add_cells(struct nvmem_device *nvmem, · line 291
Quoted 3 lines
err:
drivers/nvmem/core.c +1
-1-while (--i) +while (i--)This is what you need here to fix it correctly.
Quoted 4 lines
Quoted 2 lines
while (--i >= 0)
Sigh. http://thread.gmane.org/gmane.linux.kernel.mm/146058/focus=2149595
Yes, You are correct! :-)
I will queue this patch.
--srini
Quoted 5 lines
TL;DR: They're equivalent.
Rasmus