All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvmem: core: fix error path in nvmem_add_cells()
@ 2016-02-08 21:04 Rasmus Villemoes
  2016-02-17 10:48 ` Srinivas Kandagatla
  0 siblings, 1 reply; 4+ messages in thread
From: Rasmus Villemoes @ 2016-02-08 21:04 UTC (permalink / raw)
  To: Srinivas Kandagatla, Maxime Ripard
  Cc: Greg Kroah-Hartman, Rasmus Villemoes, linux-kernel

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.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/nvmem/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 6fd4e5a5ef4a..1e65eccfea83 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -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;
 }
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvmem: core: fix error path in nvmem_add_cells()
  2016-02-08 21:04 [PATCH] nvmem: core: fix error path in nvmem_add_cells() Rasmus Villemoes
@ 2016-02-17 10:48 ` Srinivas Kandagatla
  2016-02-18  5:35   ` Rasmus Villemoes
  0 siblings, 1 reply; 4+ messages in thread
From: Srinivas Kandagatla @ 2016-02-17 10:48 UTC (permalink / raw)
  To: Rasmus Villemoes, Maxime Ripard; +Cc: Greg Kroah-Hartman, linux-kernel

Hi Rasmus,

Thanks for the patch,

On 08/02/16 21:04, Rasmus Villemoes wrote:
> 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.
I agree, it would fail in case of zero.
>
> Also, we're not freeing the temporary backing array cells on the error
> path.
>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>   drivers/nvmem/core.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 6fd4e5a5ef4a..1e65eccfea83 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -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)

>
> +	kfree(cells);
This change looks good.
> +
>   	return rval;
>   }
>
>

--srini

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvmem: core: fix error path in nvmem_add_cells()
  2016-02-17 10:48 ` Srinivas Kandagatla
@ 2016-02-18  5:35   ` Rasmus Villemoes
  2016-02-18  9:07     ` Srinivas Kandagatla
  0 siblings, 1 reply; 4+ messages in thread
From: Rasmus Villemoes @ 2016-02-18  5:35 UTC (permalink / raw)
  To: Srinivas Kandagatla; +Cc: Maxime Ripard, Greg Kroah-Hartman, linux-kernel

On Wed, Feb 17 2016, Srinivas Kandagatla <srinivas.kandagatla@linaro.org> wrote:

>>   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)
>

Sigh. http://thread.gmane.org/gmane.linux.kernel.mm/146058/focus=2149595

TL;DR: They're equivalent.

Rasmus

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvmem: core: fix error path in nvmem_add_cells()
  2016-02-18  5:35   ` Rasmus Villemoes
@ 2016-02-18  9:07     ` Srinivas Kandagatla
  0 siblings, 0 replies; 4+ messages in thread
From: Srinivas Kandagatla @ 2016-02-18  9:07 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: Maxime Ripard, Greg Kroah-Hartman, linux-kernel



On 18/02/16 05:35, Rasmus Villemoes wrote:
> On Wed, Feb 17 2016, Srinivas Kandagatla <srinivas.kandagatla@linaro.org> wrote:
>
>>>    err:
>>> -	while (--i)
>>> +	while (i--)

>> 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
Yes, You are correct! :-)
I will queue this patch.

--srini


>
> TL;DR: They're equivalent.
>
> Rasmus
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-02-18  9:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-08 21:04 [PATCH] nvmem: core: fix error path in nvmem_add_cells() Rasmus Villemoes
2016-02-17 10:48 ` Srinivas Kandagatla
2016-02-18  5:35   ` Rasmus Villemoes
2016-02-18  9:07     ` Srinivas Kandagatla

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.