Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp8848pxj; Wed, 23 Jun 2021 14:15:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTY6ddYQ/OVCOo7gOIRafmBqAsB+e63YAFw8+wx6hdjXX+PoVQLw4uuyxXvartnjmcxeaZ X-Received: by 2002:a17:906:ae99:: with SMTP id md25mr1970984ejb.14.1624482918282; Wed, 23 Jun 2021 14:15:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624482918; cv=none; d=google.com; s=arc-20160816; b=sUuKyRd5K2uWhKhM1M0eH9UafuInUXCBZlzWmKzDlHxwmqWyku4w6cz0QrnwB36EaJ M632mw6HbyZgouzSP0EF9nKazZuIqyjs52Dnm213YdnqKLyg+E7ChOF5WW7lx4LS3jfX CIel4gNa1Q8dt2vbw/jLxZ45nk913VwGHgr/t+El4dknNQ5VM1qEi2XPDCLXnKe7tKyn PbvLXlmv1aeDOr1/4Lr/ZPtwFJ1WVfy/jTM1tCIqTAHDXG6PfiQl4Bu89Q2tOkaHnVqk gbSKTkvnP3Z4FQsgHVb+3/eyHuTlmPapclV4wxAcBE/XoNzwVOj9YPaNMSMIzZeC/gAK BOTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :newsgroups:cc:to:subject; bh=hk8mFGwZWEZIZ034eUshZ/izYov+T6eRvoTs06ruwoQ=; b=L9Oa66I3wc3qQ8yRQLMVLwz4mXb1J9SUdoVSskblRxS6ZAqbW4RAY8z9TGfnDALs0O VhzXV8nHC8VmeJJfa2f35n+4lwhkTCPSTIT6mZsHDNZcPjqwI7FwdxjhIYN1mTJsCbYL IbyysH29NmlDqAAJjtx4OgEvWHc/2ElulEICLCNODngQl1r8WXRaSqdYOMrPzJkxeZJx DLLs+b7yge7bu0RdwLHw1+Q5MP4fhQwKgJTdPzPMlGT9juALHUb4aVpBkrxanokJz/3U MnNvQQ1sdfocjpXJjeO1CT391PiQGhsHBS6psFRec23hyZ6oS5FWPKeZceayc9tN5OTF 1c2g== 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 v5si845276edi.582.2021.06.23.14.14.54; Wed, 23 Jun 2021 14:15:18 -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 S229759AbhFWVQJ (ORCPT + 99 others); Wed, 23 Jun 2021 17:16:09 -0400 Received: from smtp11.smtpout.orange.fr ([80.12.242.133]:45092 "EHLO smtp.smtpout.orange.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229660AbhFWVQJ (ORCPT ); Wed, 23 Jun 2021 17:16:09 -0400 Received: from [192.168.1.18] ([86.243.172.93]) by mwinf5d90 with ME id LlDn2500Q21Fzsu03lDnbG; Wed, 23 Jun 2021 23:13:49 +0200 X-ME-Helo: [192.168.1.18] X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Wed, 23 Jun 2021 23:13:49 +0200 X-ME-IP: 86.243.172.93 Subject: Re: [PATCH] ASoC: da7219: Fix an out-of-bound read in an error handling path To: Dan Carpenter Cc: alsa-devel@alsa-project.org, support.opensource@diasemi.com, lgirdwood@gmail.com, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, tiwai@suse.com, broonie@kernel.org, Adam.Thomson.Opensource@diasemi.com Newsgroups: gmane.linux.alsa.devel,gmane.linux.kernel,gmane.linux.kernel.janitors References: <4fdde55198294a07f04933f7cef937fcb654c901.1624425670.git.christophe.jaillet@wanadoo.fr> <20210623094655.GB2116@kadam> From: Christophe JAILLET Message-ID: <8e0d461a-c633-0162-b9e6-c2166bc013fe@wanadoo.fr> Date: Wed, 23 Jun 2021 23:13:46 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210623094655.GB2116@kadam> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit 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 > >