From patchwork Mon Nov 13 19:38:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 164625 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp1432232vqg; Mon, 13 Nov 2023 11:38:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IHWk1y8HHfztOW33PrUyu2L4cyiAVTdi40nkF0OruonUXPZHsU5Trhs7sXLkutG0qCvXtEN X-Received: by 2002:a05:6a20:1612:b0:16b:e2e5:fe55 with SMTP id l18-20020a056a20161200b0016be2e5fe55mr8877170pzj.51.1699904311452; Mon, 13 Nov 2023 11:38:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699904311; cv=none; d=google.com; s=arc-20160816; b=Iu/315peD2Y6pqmWmWIOMgxH+le20LnrcYS50PXK3220Y1ZO7VXfFd85SjY3WHfWAk X8khyGaGKOqA5C68chnvbZY0NuYexjMPdymztIqjmfOhxBTd0cti0KGWsCiNXBFKtDOM ohMgYcsG9abB3VpTeom3Suwds3nhwcU0R8FlP0trfj+VofgSJ0MD4cPvAHIncZH51nW3 p8bpz5TYa5zHbLrqrmJgKWiF0KTDOyk0gL/xS1+FedQTRYYtWZOyXpy1vqCxTjX7Xrfy tiQHOfdv4gm+gdqRkYrN4WW2k4O2Ooz3k+xN8e9baUGHTyXgrjIoUbtDaSMzJp7vlDWG oPuw== 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-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=HI/3uXciBS/Hoy0W/WG2gGywEdeObPOaR/QE80Ixnts=; fh=PbOYNE7MKRH3B91j6Jo3wMap46l3nOy+ME536MRlV6M=; b=RyUMmiccgaBpWxFIbZDhXnDZWkWU3RXK+/MOi4W7Q/p2VMITnMZBzlm/VIgchMjuL4 FL7pHyRR0qo/mVy1BEPuuoQpkDvo6s/3HLU+TQUuxn1FjDSi7H5YmXIaKTLJVmo/GWwu 0+SMFUDWU94qo7CA76LQ6HFuDxNPpjIynbYLRgNLu3qAskUaOEb2xxEbl4KdV3rmaERB GDmjNL54mjGDJVNNzUQluDWjHVP6ytI9kIcd55SH5za21kX0Lrm36y2CBtWFnVaalpCn 6+q3CkCNaPfSmX306MAX8XjUUUlmVAuDQ6YHRmibH0VUGaJmugA3E4qAWBsiZL7yZ5hN uBuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GLS9dTAx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id m9-20020a056a00080900b006c31c0e909dsi6453177pfk.32.2023.11.13.11.38.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Nov 2023 11:38:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GLS9dTAx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id A5C4D805C14B; Mon, 13 Nov 2023 11:38:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229959AbjKMTiU (ORCPT + 30 others); Mon, 13 Nov 2023 14:38:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229511AbjKMTiT (ORCPT ); Mon, 13 Nov 2023 14:38:19 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A5A7D6E for ; Mon, 13 Nov 2023 11:38:16 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 16CCBC433C7; Mon, 13 Nov 2023 19:38:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699904296; bh=9ypxMPYW+WDYaCwDSx2ywc4K+xykb9J09UJjGSMapQ0=; h=Date:From:To:Cc:Subject:From; b=GLS9dTAxfC37erUQoZRNm07gmEetbMSbdz21/QTHmqATocVHj+fiVqy2wC74hZNmw 7IFD8yZQ2cjRhShv6szxx/Dsami6m4C9f9Y+/TaGCEDXL6VCcL8DjEXAmcKtARaPjN mPC9+hLH+MPrqEJuEN4c21The29qeOsqCLDHaPXiEWEiZ+D4MQ8eykPD7XagFSK8W7 255n+Dj8RfSUDZIs3BnheuHtBqGWvb4GTZLJ0TQQZiqdh944QE0w7XadC7qM5rIfRY XjdaZObL4CPLZLjtP1aaPCy0ei1f/Zu0w7mwrGPTuDaU97ZFmj7xIwNDzb7DBgqEAU 8tmHHmYelCDxg== Date: Mon, 13 Nov 2023 13:38:12 -0600 From: "Gustavo A. R. Silva" To: Jean Delvare , Guenter Roeck , Joel Stanley , Andrew Jeffery Cc: linux-hwmon@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] hwmon: (aspeed-pwm-tacho) Fix -Wstringop-overflow warning in aspeed_create_fan_tach_channel() Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 13 Nov 2023 11:38:28 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782478863295305877 X-GMAIL-MSGID: 1782478863295305877 Based on the documentation below, the maximum number of Fan tach channels is 16: Documentation/devicetree/bindings/hwmon/aspeed-pwm-tacho.txt:45: 45 - aspeed,fan-tach-ch : should specify the Fan tach input channel. 46 integer value in the range 0 through 15, with 0 indicating 47 Fan tach channel 0 and 15 indicating Fan tach channel 15. 48 At least one Fan tach input channel is required. However, the compiler doesn't know that, and legitimaly warns about a potential overwrite in array `u8 fan_tach_ch_source[16]` in `struct aspeed_pwm_tacho_data`, in case `index` takes a value outside the boundaries of the array: drivers/hwmon/aspeed-pwm-tacho.c: 179 struct aspeed_pwm_tacho_data { ... 184 bool fan_tach_present[16]; ... 193 u8 fan_tach_ch_source[16]; ... 196 }; In function ‘aspeed_create_fan_tach_channel’, inlined from ‘aspeed_create_fan’ at drivers/hwmon/aspeed-pwm-tacho.c:877:2, inlined from ‘aspeed_pwm_tacho_probe’ at drivers/hwmon/aspeed-pwm-tacho.c:936:9: drivers/hwmon/aspeed-pwm-tacho.c:751:49: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=] 751 | priv->fan_tach_ch_source[index] = pwm_source; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~ drivers/hwmon/aspeed-pwm-tacho.c: In function ‘aspeed_pwm_tacho_probe’: drivers/hwmon/aspeed-pwm-tacho.c:193:12: note: at offset [48, 255] into destination object ‘fan_tach_ch_source’ of size 16 193 | u8 fan_tach_ch_source[16]; | ^~~~~~~~~~~~~~~~~~ Fix this by sanity checking `index` before using it to index arrays of size 16 elements in `struct aspeed_pwm_tacho_data`. Also, and just for completeness, add a `pr_err()` message to display in the unlikely case `0 > index >= 16`. This is probably the last remaining -Wstringop-overflow issue in the kernel, and this patch helps with the ongoing efforts to enable such compiler option globally. Signed-off-by: Gustavo A. R. Silva --- drivers/hwmon/aspeed-pwm-tacho.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/aspeed-pwm-tacho.c b/drivers/hwmon/aspeed-pwm-tacho.c index 997df4b40509..092a81916325 100644 --- a/drivers/hwmon/aspeed-pwm-tacho.c +++ b/drivers/hwmon/aspeed-pwm-tacho.c @@ -166,6 +166,8 @@ #define MAX_CDEV_NAME_LEN 16 +#define MAX_ASPEED_FAN_TACH_CHANNELS 16 + struct aspeed_cooling_device { char name[16]; struct aspeed_pwm_tacho_data *priv; @@ -181,7 +183,7 @@ struct aspeed_pwm_tacho_data { struct reset_control *rst; unsigned long clk_freq; bool pwm_present[8]; - bool fan_tach_present[16]; + bool fan_tach_present[MAX_ASPEED_FAN_TACH_CHANNELS]; u8 type_pwm_clock_unit[3]; u8 type_pwm_clock_division_h[3]; u8 type_pwm_clock_division_l[3]; @@ -190,7 +192,7 @@ struct aspeed_pwm_tacho_data { u16 type_fan_tach_unit[3]; u8 pwm_port_type[8]; u8 pwm_port_fan_ctrl[8]; - u8 fan_tach_ch_source[16]; + u8 fan_tach_ch_source[MAX_ASPEED_FAN_TACH_CHANNELS]; struct aspeed_cooling_device *cdev[8]; const struct attribute_group *groups[3]; }; @@ -746,10 +748,14 @@ static void aspeed_create_fan_tach_channel(struct aspeed_pwm_tacho_data *priv, for (val = 0; val < count; val++) { index = fan_tach_ch[val]; - aspeed_set_fan_tach_ch_enable(priv->regmap, index, true); - priv->fan_tach_present[index] = true; - priv->fan_tach_ch_source[index] = pwm_source; - aspeed_set_fan_tach_ch_source(priv->regmap, index, pwm_source); + if (index < MAX_ASPEED_FAN_TACH_CHANNELS) { + aspeed_set_fan_tach_ch_enable(priv->regmap, index, true); + priv->fan_tach_present[index] = true; + priv->fan_tach_ch_source[index] = pwm_source; + aspeed_set_fan_tach_ch_source(priv->regmap, index, pwm_source); + } else { + pr_err("Invalid Fan Tach input channel %u\n.", index); + } } }