From patchwork Tue Nov 14 19:53:08 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: 165062 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:a59:b0:164:83eb:24d7 with SMTP id 25csp2164464rwb; Tue, 14 Nov 2023 11:53:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IECgfWPt9RrSeoKeU7MvqdG19IjbITtA/jIH/ipwVDmw/lCqiZNrHzbDLzZexN7WbMibIoS X-Received: by 2002:a05:6a20:8403:b0:186:aac2:26b9 with SMTP id c3-20020a056a20840300b00186aac226b9mr7334136pzd.30.1699991613906; Tue, 14 Nov 2023 11:53:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699991613; cv=none; d=google.com; s=arc-20160816; b=LtfQwogk69sfeRfbJK9jyt2GrCjDpi4Oqcyw3gGOF+RlknK8u3urB/85YrqA/sa7EW 60VGWRP4TytJuNOJepv7knZ2kWpQ6u/yRBOyxuKoXSkcTjZwBl1W97Ad7xZgfvTA0aSa A58/rwgzWr9whUF6nPKHvFLbQziYZ7Yy/vQQ2+8L9n3QJb5UJ6AuFHQ03HUjCYS9IJMz OTCombE4jVYEkPH4U3+s2rPsTSY2p+Hxe8LNfgRNCU6FYaHWBm/Jnefdue5B4omhBZI+ TZq70gTOs9EZPiO2PC1G5+LRj69AfDfR0Dpig3+g9SHnHzAlGXpG0Te6r2sv63mNUvXb lJjg== 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=CbQgjVtTbv/8FJ9u2FzZCxn6otfEpLUObBSbAFGhdn8=; fh=PbOYNE7MKRH3B91j6Jo3wMap46l3nOy+ME536MRlV6M=; b=f8canp0JSZ/w/rHWS1ZGGHjNRZASv90DKDEeOKNsUhrAqZkf19kk7eDdVmJ8T1cXf1 /0xkAIWdUHahSU5XdK9r+vVpAUB2VphXcAagY/M2t+kr+i76BmdxWNu94aG5J08tICcd 2HSa0yQpRppf5G61IwkDXPD/cXRVPxubaHddnnq6sz1REkGQVLumlrLy+r0PNJ3n+imJ AsqRbEYrL/C0R6MQKCKbmln+kyzEWe31rXV42exsSA3NR2YJwmxXyPx0jh2QvFDOrj1Y IlFdMtWP2IgqMVZMJ6DSPltBOOzJ5J7QAxhy1IkrG7mhLAkwa010lfzPFuGl/4V1B6+B y9Iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Xu1CUwPs; 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 i184-20020a6387c1000000b005be27346472si7984282pge.582.2023.11.14.11.53.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 11:53:33 -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=Xu1CUwPs; 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 2A5BE80B1A05; Tue, 14 Nov 2023 11:53:31 -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 S232196AbjKNTxU (ORCPT + 29 others); Tue, 14 Nov 2023 14:53:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229569AbjKNTxS (ORCPT ); Tue, 14 Nov 2023 14:53:18 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FAF1EB for ; Tue, 14 Nov 2023 11:53:14 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 473DBC433C8; Tue, 14 Nov 2023 19:53:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699991592; bh=p5bXNYWZQPXVZtSAziETIAWqwitA5MwvAafu84FIsRc=; h=Date:From:To:Cc:Subject:From; b=Xu1CUwPsb8NHywQsuf0ZjveiUXv6X56buMrIIKqhntvic2H0v69ZAGSIpnJd4JGXq x62nDwx5EpPDgApZdmHcRyahh3cgVMVMcqSQuN0tHGMd5q9TjcJIwdd/AJWnvOybct BxyPFjHQ59HKLzh3aSNwATPGQCZXSmpoR6nVlFQUmAr7+xOGmRx3sX2RfXekyk5/gt +j28UfzElp+1DtYEdQl6NrFRsLL/3A9eJGxe64yy2d+x6FMpIrgIhI3mVMPK7Dt7jN wbXRG4wrJ9T76vOS1vvmbqI5iODtF/BUyde4KI/XcWhpaW/SpEkp2mTjDlnDjaMqCo VYw7dCdZvPMYQ== Date: Tue, 14 Nov 2023 13:53:08 -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 v2][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]); Tue, 14 Nov 2023 11:53:31 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782570406339726646 X-GMAIL-MSGID: 1782570406339726646 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, pass `dev` as argument to function `aspeed_create_fan_tach_channel()`, and add an error message in case `index` is out-of-bounds, in which case return `-EINVAL`. Signed-off-by: Gustavo A. R. Silva Reviewed-by: Kees Cook --- Changes in v2: - Pass `dev` to function aspeed_create_fan_tach_channel() and return -EINVAL in case of error. (Guenter) - Update changelog text. v1: - Link: https://lore.kernel.org/linux-hardening/ZVJ7JBFoULzY3VGx@work/ drivers/hwmon/aspeed-pwm-tacho.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/hwmon/aspeed-pwm-tacho.c b/drivers/hwmon/aspeed-pwm-tacho.c index 997df4b40509..9a209e064e46 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]; }; @@ -737,7 +739,8 @@ static void aspeed_create_pwm_port(struct aspeed_pwm_tacho_data *priv, aspeed_set_pwm_port_fan_ctrl(priv, pwm_port, INIT_FAN_CTRL); } -static void aspeed_create_fan_tach_channel(struct aspeed_pwm_tacho_data *priv, +static int aspeed_create_fan_tach_channel(struct device *dev, + struct aspeed_pwm_tacho_data *priv, u8 *fan_tach_ch, int count, u8 pwm_source) @@ -746,11 +749,17 @@ static void aspeed_create_fan_tach_channel(struct aspeed_pwm_tacho_data *priv, for (val = 0; val < count; val++) { index = fan_tach_ch[val]; + if (index >= MAX_ASPEED_FAN_TACH_CHANNELS) { + dev_err(dev, "Invalid Fan Tach input channel %u\n.", index); + return -EINVAL; + } 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); } + + return 0; } static int @@ -874,7 +883,10 @@ static int aspeed_create_fan(struct device *dev, fan_tach_ch, count); if (ret) return ret; - aspeed_create_fan_tach_channel(priv, fan_tach_ch, count, pwm_port); + + ret = aspeed_create_fan_tach_channel(dev, priv, fan_tach_ch, count, pwm_port); + if (ret) + return ret; return 0; }