Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp9719pxj; Wed, 23 Jun 2021 14:16:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRjxFGyxyKvjgwGxGrsSGDuyVC2SD7WGsPIDRJvZZHnyoYKhhVKuNugRlyQbMVoIlFlFrZ X-Received: by 2002:a17:906:1815:: with SMTP id v21mr1967195eje.376.1624482997173; Wed, 23 Jun 2021 14:16:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624482997; cv=none; d=google.com; s=arc-20160816; b=nLSqgJJjipHP4k6VLSGSxjKyLTlmvnbseFknu52Q6lP5uC0gIcgme0+KWiyjyOWa2E yG889L3PDx6B/HdkwJ4H6BkP5u0/CDx7ZXac/WIDPQLij8uEemav72Lbe6GLUMPvuMSh nXS0iE6IhQwp3O1vQkRo1Fb1phenkudoqv9u69P6itGjOkCSXD8p1WUvOgy8bxRc4fJn zYxte/VFdMS9kNF2no1k2ufRWBN1Uof2a7WB5C/uiOQ5O+G/0w3+EQ5dDs8qJScL1vJd /XRU0TX25qrqG9bHJDdQY3fVGIS/II0MQ4dwhi52e10NoiSHGrlNXK9QL1nolkTj6+7C p0mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:content-language:in-reply-to:cc:user-agent :content-transfer-encoding:mime-version:references:message-id:date :subject:from:to; bh=hk8mFGwZWEZIZ034eUshZ/izYov+T6eRvoTs06ruwoQ=; b=fB97rfm3YOaEf9bNElO7B2DPuX+bSE98vemMw0H+o9f4d4UmojHo50jqO8GodF1aQD 8i09MKXOlI54p7F2ZiYBHexpHChoWlEuntbj4e2JCXm6bElLjHi8W8/P4+2Xqik1nerw 9IXPl93Vo16vf1U/d53qStfcWO+bjfeonQ/1J91XGDA7LAJUmrXGVYAJeC7JtCJC1Kg8 cRPDSLLDZ3E1hSksRgYwSVYtY3VtdmJjvIgvcvhmuYr8DTl8NFU6pkQOge66NE1fDRmG 5rialm9mAJBceXf6HB39XowL3wvXv3wkqFk6AKm/v0UnveCMqAvMD1tza88K+pOTDr5E kg7w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hv6si719157ejc.390.2021.06.23.14.16.02; Wed, 23 Jun 2021 14:16:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229890AbhFWVQ5 (ORCPT + 99 others); Wed, 23 Jun 2021 17:16:57 -0400 Received: from ciao.gmane.io ([116.202.254.214]:46402 "EHLO ciao.gmane.io" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229844AbhFWVQ4 (ORCPT ); Wed, 23 Jun 2021 17:16:56 -0400 X-Greylist: delayed 302 seconds by postgrey-1.27 at vger.kernel.org; Wed, 23 Jun 2021 17:16:55 EDT Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1lwA7o-0008Qs-HV for linux-kernel@vger.kernel.org; Wed, 23 Jun 2021 23:09:32 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: linux-kernel@vger.kernel.org From: Christophe JAILLET Subject: Re: [PATCH] ASoC: da7219: Fix an out-of-bound read in an error handling path Date: Wed, 23 Jun 2021 23:09:26 +0200 Message-ID: References: <4fdde55198294a07f04933f7cef937fcb654c901.1624425670.git.christophe.jaillet@wanadoo.fr> <20210623094655.GB2116@kadam> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 Cc: alsa-devel@alsa-project.org, kernel-janitors@vger.kernel.org In-Reply-To: <20210623094655.GB2116@kadam> Content-Language: en-US Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 >> --- >> 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 > >