Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4509345pxb; Tue, 5 Oct 2021 04:47:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1sqdL8w6OJPyqX7f9UUU9sYGw0xsUWXABfLtLJe1W4g3J85xdhDAWXXjyV9R+4UuZK90n X-Received: by 2002:a50:e0c9:: with SMTP id j9mr25279747edl.336.1633434421879; Tue, 05 Oct 2021 04:47:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633434421; cv=none; d=google.com; s=arc-20160816; b=jT2d11KquKZd0WbwhGcmTZkyMMC8bbJiQQAGxXH1R46dmjKUZJwhOT1uVQG2XeuFmm KqvAVaVyfbwewupX+rklB5eqGxLky0/yfyz8Kg/ZqQza2gusrRWpCrw8lWrrZu2bBUvX s+4HIhPddUrwMw6D+P61P3UJgDjWN9o1iSFav4O/qCrq1G4Ton31IJUs3MW6IEEdK3aT a3XO2aAUOTQOjT8cdmqG0uzheN+i8lUagTECfZ+y7aM51o3JQu3AerDXOMlDSAYLRnU2 iwKWnILtlDF2ZnqOnJfkDy17y58V8eH3Smx5h8WPhD/a2miLWoHGUN9q1r1jDsqsAkUn YnGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date; bh=Wf26rPjwZfYGjaKl9EiKM2z69nJ8A05AEXaeJQ8WPAg=; b=JKXxRlgbGGNf7MeZess87KmJRmg1JO9tLvCm87WFaSzG1e8k7shA+00ZzD9XAWd3io 2J4tRF3DZwl4Ky4xlsJVHrJZfXd7awB4k8NLTFrZ/r5GoPLdfoaiszG6F15s+4XuMXzu vTds/SVlB6MjUttlOUdCSQkPRgtJwyO+dG3W8mOX4n3vtRoarueQ+Y6QT8jniBD06Urg TtY3SKuTdiI58VaHUHbiWvZa6B2R+oLHZ59y6sIIFgviXBna5ZtOyEIm42kZWbRpyp5O lvoh2TSPtIvSxRRIkYVxa5PVT66EBTtrnQNkv9STl+wB5+eAcz6QulmWLns0eRnoMku7 gMVg== 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 di3si25305926ejc.728.2021.10.05.04.46.38; Tue, 05 Oct 2021 04:47:01 -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 S230500AbhJELqa (ORCPT + 99 others); Tue, 5 Oct 2021 07:46:30 -0400 Received: from m-r2.th.seeweb.it ([5.144.164.171]:59069 "EHLO m-r2.th.seeweb.it" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234068AbhJELq3 (ORCPT ); Tue, 5 Oct 2021 07:46:29 -0400 Received: from SoMainline.org (94-209-165-62.cable.dynamic.v4.ziggo.nl [94.209.165.62]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by m-r2.th.seeweb.it (Postfix) with ESMTPSA id F3D703EE3F; Tue, 5 Oct 2021 13:44:36 +0200 (CEST) Date: Tue, 5 Oct 2021 13:44:35 +0200 From: Marijn Suijten To: Daniel Thompson Cc: phone-devel@vger.kernel.org, Andy Gross , Bjorn Andersson , Lee Jones , Jingoo Han , ~postmarketos/upstreaming@lists.sr.ht, AngeloGioacchino Del Regno , Konrad Dybcio , Martin Botka , Jami Kettunen , Pavel Dubrova , Kiran Gunda , Courtney Cavin , Bryan Wu , linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 05/10] backlight: qcom-wled: Fix off-by-one maximum with default num_strings Message-ID: <20211005114435.phyq2jsbdyroa6kn@SoMainline.org> Mail-Followup-To: Marijn Suijten , Daniel Thompson , phone-devel@vger.kernel.org, Andy Gross , Bjorn Andersson , Lee Jones , Jingoo Han , ~postmarketos/upstreaming@lists.sr.ht, AngeloGioacchino Del Regno , Konrad Dybcio , Martin Botka , Jami Kettunen , Pavel Dubrova , Kiran Gunda , Courtney Cavin , Bryan Wu , linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org References: <20211004192741.621870-1-marijn.suijten@somainline.org> <20211004192741.621870-6-marijn.suijten@somainline.org> <20211005091947.7msztp5l554c7cy4@maple.lan> <20211005100606.faxra73mzkvjd4f6@SoMainline.org> <20211005103843.heufyonycnudxnzd@maple.lan> <20211005105312.kqiyzoqtzzjxayhg@maple.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211005105312.kqiyzoqtzzjxayhg@maple.lan> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-10-05 11:53:12, Daniel Thompson wrote: > On Tue, Oct 05, 2021 at 11:38:43AM +0100, Daniel Thompson wrote: > > On Tue, Oct 05, 2021 at 12:06:06PM +0200, Marijn Suijten wrote: > > > On 2021-10-05 10:19:47, Daniel Thompson wrote: > > > > On Mon, Oct 04, 2021 at 09:27:36PM +0200, Marijn Suijten wrote: > > > > > When not specifying num-strings in the DT the default is used, but +1 is > > > > > added to it which turns wled3 into 4 and wled4/5 into 5 strings instead > > > > > of 3 and 4 respectively, causing out of bounds reads and register > > > > > read/writes. This +1 exists for a deficiency in the DT parsing code, > > > > > and is simply omitted entirely - solving this oob issue - by allowing > > > > > one extra iteration of the wled_var_cfg function parsing this particular > > > > > property. > > > > > > > > > > Fixes: 93c64f1ea1e8 ("leds: add Qualcomm PM8941 WLED driver") > > > > > Signed-off-by: Marijn Suijten > > > > > Reviewed-by: AngeloGioacchino Del Regno > > > > > --- > > > > > drivers/video/backlight/qcom-wled.c | 8 +++----- > > > > > 1 file changed, 3 insertions(+), 5 deletions(-) > > > > > > > > > > diff --git a/drivers/video/backlight/qcom-wled.c b/drivers/video/backlight/qcom-wled.c > > > > > index 27e8949c7922..66ce77ee3099 100644 > > > > > --- a/drivers/video/backlight/qcom-wled.c > > > > > +++ b/drivers/video/backlight/qcom-wled.c > > > > > @@ -1255,17 +1255,17 @@ static const struct wled_var_cfg wled5_ovp_cfg = { > > > > > > > > > > static u32 wled3_num_strings_values_fn(u32 idx) > > > > > { > > > > > - return idx + 1; > > > > > + return idx; > > > > > } > > > > > > > > > > static const struct wled_var_cfg wled3_num_strings_cfg = { > > > > > .fn = wled3_num_strings_values_fn, > > > > > - .size = 3, > > > > > + .size = 4, /* [0, 3] */ > > > > > > > > 0 is not a valid value for this property. > > > > > > These comments represent the possible loop iterations the DT "cfg > > > parser" runs through, starting at j=0 and running up until and including > > > j=3. Should I make that more clear or omit these comments entirely? > > > > The role of wled3_num_strings_values_fn() is to enumerate the list of > > legal values for the property [ 1, 2, 3 ]. Your changes cause the > > enumeration to include a non-legal value so that you can have an > > identity mapping between the symbol and the enumerate value. > > > > An alternative approach would be to leave the enumeration logic > > alone but set the num_string default to UINT_MAX in all cases: > > > > - cfg->num_strings = cfg->num_strings + 1; > > + if (cfg->num_strings == UINT_MAX) > > + cfg->num_strings = > > Oops... looks like I missed the cfg->max_string_count here. > > > > + else > > + /* Convert from enumerated to numeric form */ > > + cfg->num_strings = wled3_num_strings_values_fn( > > + cfg->num_strings); > > > PS the alternative option is not to treat num-strings as an enumerated > value at all and just read it directly without using wled_values()... I much prefer doing that instead of trying to wrangle enumeration parsing around integer values that are supposed to be used as-is. After all this variable is already named to set the `+ 1` override currently, and `qcom,enabled_strings` has "custom" handling as well. I'll extend the validation to ensure num_strings>=1 too. In addition, and this needs some investigation on the dt-bindings side too, it might be beneficial to make both properties mutually exclusive. When specifying qcom,enabled_strings it makes little sense to also provide qcom,num_strings and we want the former to take precedence. At that point one might ask why qcom,num_strings remains at all when DT can use qcom,enabled_strings instead. We will supposedly have to keep backwards compatibility with DTs in mind so none of this can be removed or made mutually exclusive from a driver standpoint, that all has to be done in dt-bindings yaml to be enforced on checked-in DTs. - Marijn