kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ASoC: da7219: Fix an out-of-bound read in an error handling path
@ 2021-06-23  5:22 Christophe JAILLET
  2021-06-23  9:46 ` Dan Carpenter
  0 siblings, 1 reply; 4+ messages in thread
From: Christophe JAILLET @ 2021-06-23  5:22 UTC (permalink / raw)
  To: support.opensource, lgirdwood, broonie, perex, tiwai,
	Adam.Thomson.Opensource
  Cc: alsa-devel, linux-kernel, kernel-janitors, Christophe JAILLET

If 'of_clk_add_hw_provider()' fails, the previous 'for' loop will have
run completely and 'i' is know to be 'DA7219_DAI_NUM_CLKS'.

In such a case, there will be an out-of-bounds access when using
'da7219->dai_clks_lookup[i]' and '&da7219->dai_clks_hw[i]'.

To avoid that, add a new label, 'err_free_all', which set the expected
value of 'i' in such a case.

Fixes: 78013a1cf297 ("ASoC: da7219: Fix clock handling around codec level probe")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 sound/soc/codecs/da7219.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/sound/soc/codecs/da7219.c b/sound/soc/codecs/da7219.c
index 13009d08b09a..1e8b491d1fd3 100644
--- a/sound/soc/codecs/da7219.c
+++ b/sound/soc/codecs/da7219.c
@@ -2204,12 +2204,14 @@ static int da7219_register_dai_clks(struct snd_soc_component *component)
 					     da7219->clk_hw_data);
 		if (ret) {
 			dev_err(dev, "Failed to register clock provider\n");
-			goto err;
+			goto err_free_all;
 		}
 	}
 
 	return 0;
 
+err_free_all:
+	i = DA7219_DAI_NUM_CLKS - 1;
 err:
 	do {
 		if (da7219->dai_clks_lookup[i])
-- 
2.30.2


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

* Re: [PATCH] ASoC: da7219: Fix an out-of-bound read in an error handling path
  2021-06-23  5:22 [PATCH] ASoC: da7219: Fix an out-of-bound read in an error handling path Christophe JAILLET
@ 2021-06-23  9:46 ` Dan Carpenter
  2021-06-23 21:13   ` Christophe JAILLET
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2021-06-23  9:46 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: support.opensource, lgirdwood, broonie, perex, tiwai,
	Adam.Thomson.Opensource, alsa-devel, linux-kernel,
	kernel-janitors

On Wed, Jun 23, 2021 at 07:22:45AM +0200, Christophe JAILLET wrote:
> If 'of_clk_add_hw_provider()' fails, the previous 'for' loop will have
> run completely and 'i' is know to be 'DA7219_DAI_NUM_CLKS'.
> 
> In such a case, there will be an out-of-bounds access when using
> 'da7219->dai_clks_lookup[i]' and '&da7219->dai_clks_hw[i]'.
> 
> To avoid that, add a new label, 'err_free_all', which set the expected
> value of 'i' in such a case.
> 
> Fixes: 78013a1cf297 ("ASoC: da7219: Fix clock handling around codec level probe")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
>  sound/soc/codecs/da7219.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/sound/soc/codecs/da7219.c b/sound/soc/codecs/da7219.c
> index 13009d08b09a..1e8b491d1fd3 100644
> --- a/sound/soc/codecs/da7219.c
> +++ b/sound/soc/codecs/da7219.c
> @@ -2204,12 +2204,14 @@ static int da7219_register_dai_clks(struct snd_soc_component *component)
>  					     da7219->clk_hw_data);
>  		if (ret) {
>  			dev_err(dev, "Failed to register clock provider\n");
> -			goto err;
> +			goto err_free_all;
>  		}
>  	}
>  
>  	return 0;
>  
> +err_free_all:
> +	i = DA7219_DAI_NUM_CLKS - 1;
>  err:
>  	do {
>  		if (da7219->dai_clks_lookup[i])

This do while statement is wrong and it leads to potentially calling
clk_hw_unregister() on clks that haven't been registered.

I think that calling clk_hw_unregister() on unregistered clocks is
supposed to okay but I found a case where it leads to a WARN_ON()
(Nothing else harmful).  It's in __clk_register() if the alloc_clk()
fails:

	hw->clk = alloc_clk(core, NULL, NULL);
        if (IS_ERR(hw->clk)) {
                ret = PTR_ERR(hw->clk);
                goto fail_create_clk;  // <- forgot to set hw->clk = NULL
        }

The better way to handle errors from loops is to clean up partial
iterations before doing the goto.  So add a clk_hw_unregister() if the
dai_clk_lookup = clkdev_hw_create() assignment fails.  Then use a
while (--i >= 0) loop in the unwind section:

err_free_all:
	i = DA7219_DAI_NUM_CLKS;
err:
	while (--i >= 0) {

regards,
dan carpenter


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

* Re: [PATCH] ASoC: da7219: Fix an out-of-bound read in an error handling path
  2021-06-23  9:46 ` Dan Carpenter
@ 2021-06-23 21:13   ` Christophe JAILLET
  2021-06-23 21:13     ` Christophe JAILLET
  0 siblings, 1 reply; 4+ messages in thread
From: Christophe JAILLET @ 2021-06-23 21:13 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: alsa-devel, support.opensource, lgirdwood, linux-kernel,
	kernel-janitors, tiwai, broonie, Adam.Thomson.Opensource

Le 23/06/2021 à 11:46, Dan Carpenter a écrit :
> On Wed, Jun 23, 2021 at 07:22:45AM +0200, Christophe JAILLET wrote:
>> If 'of_clk_add_hw_provider()' fails, the previous 'for' loop will have
>> run completely and 'i' is know to be 'DA7219_DAI_NUM_CLKS'.
>>
>> In such a case, there will be an out-of-bounds access when using
>> 'da7219->dai_clks_lookup[i]' and '&da7219->dai_clks_hw[i]'.
>>
>> To avoid that, add a new label, 'err_free_all', which set the expected
>> value of 'i' in such a case.
>>
>> Fixes: 78013a1cf297 ("ASoC: da7219: Fix clock handling around codec level probe")
>> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>> ---
>>   sound/soc/codecs/da7219.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/sound/soc/codecs/da7219.c b/sound/soc/codecs/da7219.c
>> index 13009d08b09a..1e8b491d1fd3 100644
>> --- a/sound/soc/codecs/da7219.c
>> +++ b/sound/soc/codecs/da7219.c
>> @@ -2204,12 +2204,14 @@ static int da7219_register_dai_clks(struct snd_soc_component *component)
>>   					     da7219->clk_hw_data);
>>   		if (ret) {
>>   			dev_err(dev, "Failed to register clock provider\n");
>> -			goto err;
>> +			goto err_free_all;
>>   		}
>>   	}
>>   
>>   	return 0;
>>   
>> +err_free_all:
>> +	i = DA7219_DAI_NUM_CLKS - 1;
>>   err:
>>   	do {
>>   		if (da7219->dai_clks_lookup[i])
> 
> This do while statement is wrong and it leads to potentially calling
> clk_hw_unregister() on clks that haven't been registered.

Obviously right.

Thanks for the review Dan.
I'll send a v2 in the coming days.

CJ
> 
> I think that calling clk_hw_unregister() on unregistered clocks is
> supposed to okay but I found a case where it leads to a WARN_ON()
> (Nothing else harmful).  It's in __clk_register() if the alloc_clk()
> fails:
> 
> 	hw->clk = alloc_clk(core, NULL, NULL);
>          if (IS_ERR(hw->clk)) {
>                  ret = PTR_ERR(hw->clk);
>                  goto fail_create_clk;  // <- forgot to set hw->clk = NULL
>          }
> 
> The better way to handle errors from loops is to clean up partial
> iterations before doing the goto.  So add a clk_hw_unregister() if the
> dai_clk_lookup = clkdev_hw_create() assignment fails.  Then use a
> while (--i >= 0) loop in the unwind section:
> 
> err_free_all:
> 	i = DA7219_DAI_NUM_CLKS;
> err:
> 	while (--i >= 0) {
> 
> regards,
> dan carpenter
> 
> 


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

* Re: [PATCH] ASoC: da7219: Fix an out-of-bound read in an error handling path
  2021-06-23 21:13   ` Christophe JAILLET
@ 2021-06-23 21:13     ` Christophe JAILLET
  0 siblings, 0 replies; 4+ messages in thread
From: Christophe JAILLET @ 2021-06-23 21:13 UTC (permalink / raw)
  To: kernel-janitors; +Cc: alsa-devel, linux-kernel, linux-kernel, kernel-janitors

Le 23/06/2021 à 11:46, Dan Carpenter a écrit :
> On Wed, Jun 23, 2021 at 07:22:45AM +0200, Christophe JAILLET wrote:
>> If 'of_clk_add_hw_provider()' fails, the previous 'for' loop will have
>> run completely and 'i' is know to be 'DA7219_DAI_NUM_CLKS'.
>>
>> In such a case, there will be an out-of-bounds access when using
>> 'da7219->dai_clks_lookup[i]' and '&da7219->dai_clks_hw[i]'.
>>
>> To avoid that, add a new label, 'err_free_all', which set the expected
>> value of 'i' in such a case.
>>
>> Fixes: 78013a1cf297 ("ASoC: da7219: Fix clock handling around codec level probe")
>> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>> ---
>>   sound/soc/codecs/da7219.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/sound/soc/codecs/da7219.c b/sound/soc/codecs/da7219.c
>> index 13009d08b09a..1e8b491d1fd3 100644
>> --- a/sound/soc/codecs/da7219.c
>> +++ b/sound/soc/codecs/da7219.c
>> @@ -2204,12 +2204,14 @@ static int da7219_register_dai_clks(struct snd_soc_component *component)
>>   					     da7219->clk_hw_data);
>>   		if (ret) {
>>   			dev_err(dev, "Failed to register clock provider\n");
>> -			goto err;
>> +			goto err_free_all;
>>   		}
>>   	}
>>   
>>   	return 0;
>>   
>> +err_free_all:
>> +	i = DA7219_DAI_NUM_CLKS - 1;
>>   err:
>>   	do {
>>   		if (da7219->dai_clks_lookup[i])
> 
> This do while statement is wrong and it leads to potentially calling
> clk_hw_unregister() on clks that haven't been registered.

Obviously right.

Thanks for the review Dan.
I'll send a v2 in the coming days.

CJ
> 
> I think that calling clk_hw_unregister() on unregistered clocks is
> supposed to okay but I found a case where it leads to a WARN_ON()
> (Nothing else harmful).  It's in __clk_register() if the alloc_clk()
> fails:
> 
> 	hw->clk = alloc_clk(core, NULL, NULL);
>          if (IS_ERR(hw->clk)) {
>                  ret = PTR_ERR(hw->clk);
>                  goto fail_create_clk;  // <- forgot to set hw->clk = NULL
>          }
> 
> The better way to handle errors from loops is to clean up partial
> iterations before doing the goto.  So add a clk_hw_unregister() if the
> dai_clk_lookup = clkdev_hw_create() assignment fails.  Then use a
> while (--i >= 0) loop in the unwind section:
> 
> err_free_all:
> 	i = DA7219_DAI_NUM_CLKS;
> err:
> 	while (--i >= 0) {
> 
> regards,
> dan carpenter
> 
> 



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

end of thread, other threads:[~2021-06-23 21:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-23  5:22 [PATCH] ASoC: da7219: Fix an out-of-bound read in an error handling path Christophe JAILLET
2021-06-23  9:46 ` Dan Carpenter
2021-06-23 21:13   ` Christophe JAILLET
2021-06-23 21:13     ` Christophe JAILLET

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).