Message ID | 20230510143537.145060-1-n.zhandarovich@fintech.ru |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3685045vqo; Wed, 10 May 2023 07:56:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6n2yISDDWQaLzISRvpD/xZq1GQ32h8wuwDEMoFNQBq7iyq50NjIyIwSB73FX4lF9//fZeb X-Received: by 2002:a17:90b:2305:b0:247:6022:9595 with SMTP id mt5-20020a17090b230500b0024760229595mr18316332pjb.45.1683730576354; Wed, 10 May 2023 07:56:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683730576; cv=none; d=google.com; s=arc-20160816; b=pJlvpZfhMAf+n0PDkmEaqcG21B0ZScFwlsjkGOGe8MWfpjtosVb6aUuMLyVxL0qyH9 SQ2zHGuOR0knGS9ZTlJ0R+R+kCn6kV/NMvcOjvRLjBUUUGbptpv5r7plSrwjEYqQm6Et HAkVyWkkm/9wGGnXm4elvtD60iC3I7UWhpkzTYDG3ny7KcLKUNlShkxaYlm/URYt5V/I 7Q5VjbuRpMCVBnexr910vLE2cnwKVGmbe8NUoZ/eY+1dF1ec+Ji/XblkqDhHImCe2Wev oWzzZIT9INJSb3DQsVsJWAsqmJ8+N7DXoUBobIYB3+ug1QWotmQjj14jjo+ZBRwlJbCk KCQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=4mryp54DyEbb0RPmv+DFH+ShxeMpbBTmXxA/ginSD7M=; b=wzxzEr1/FrzMgBp0v1eGZD4BkNmeq8fz97GXPY/Kv8lM0K5WyP6RCUyDb++DR2+O98 D6CBL1uzUhBqBMdk6R27EKmNeP9EhIHUnW9Y7+ty4FWQZuhTtSyihwPA9IDNfxA0HsSR JIz1IVt8pBAVYLnH4geoH19b6F35StYEy0MLr1UJvW1nGhpIEVmuDCaubP46JMB1WtFK Zbd82djntv8Mjz7B7IEHdbK6BaNhE68c8xqeT11XNCDWgBt7KF6ehddDGv9ItFnxED8L jkXtk13IUNnklteWPHt89rrgV1bZeRIvK7rWsRVYEUmfi/rykKDJ7GtABYxWSP1V14Zk exSA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l2-20020a170902d34200b0019e6b2ed4basi4014983plk.483.2023.05.10.07.55.59; Wed, 10 May 2023 07:56:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237166AbjEJOgA (ORCPT <rfc822;jeantsuru.cumc.mandola@gmail.com> + 99 others); Wed, 10 May 2023 10:36:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237479AbjEJOf4 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 10 May 2023 10:35:56 -0400 Received: from exchange.fintech.ru (e10edge.fintech.ru [195.54.195.159]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0860786A2; Wed, 10 May 2023 07:35:44 -0700 (PDT) Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.159) with Microsoft SMTP Server (TLS) id 14.3.498.0; Wed, 10 May 2023 17:35:41 +0300 Received: from localhost (10.0.253.138) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Wed, 10 May 2023 17:35:41 +0300 From: Nikita Zhandarovich <n.zhandarovich@fintech.ru> To: Jean Delvare <jdelvare@suse.com> CC: Nikita Zhandarovich <n.zhandarovich@fintech.ru>, Guenter Roeck <linux@roeck-us.net>, =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= <u.kleine-koenig@pengutronix.de>, <linux-hwmon@vger.kernel.org>, <linux-kernel@vger.kernel.org>, <lvc-project@linuxtesting.org> Subject: [PATCH] hwmon: (f71882fg) prevent possible division by zero Date: Wed, 10 May 2023 07:35:37 -0700 Message-ID: <20230510143537.145060-1-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.0.253.138] X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765519472583062076?= X-GMAIL-MSGID: =?utf-8?q?1765519472583062076?= |
Series |
hwmon: (f71882fg) prevent possible division by zero
|
|
Commit Message
Nikita Zhandarovich
May 10, 2023, 2:35 p.m. UTC
In the unlikely event that something goes wrong with the device and
its registers, the fan_from_reg() function may return 0. This value
will cause a division-by-zero error in the show_pwm() function.
To prevent this, test the value of
fan_from_reg(data->fan_full_speed[nr]) against 0 before performing
the division. If the division-by-zero error is avoided, assign 0 to
the val variable.
Found by Linux Verification Center (linuxtesting.org) with static
analysis tool SVACE.
Fixes: df9ec2dae094 ("hwmon: (f71882fg) Reorder symbols to get rid of a few forward declarations")
Signed-off-by: Nikita Zhandarovich <n.zhandarovich@fintech.ru>
---
drivers/hwmon/f71882fg.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
Comments
On Wed, May 10, 2023 at 07:35:37AM -0700, Nikita Zhandarovich wrote: > In the unlikely event that something goes wrong with the device and > its registers, the fan_from_reg() function may return 0. This value > will cause a division-by-zero error in the show_pwm() function. > > To prevent this, test the value of > fan_from_reg(data->fan_full_speed[nr]) against 0 before performing > the division. If the division-by-zero error is avoided, assign 0 to > the val variable. > > Found by Linux Verification Center (linuxtesting.org) with static > analysis tool SVACE. > > Fixes: df9ec2dae094 ("hwmon: (f71882fg) Reorder symbols to get rid of a few forward declarations") > Signed-off-by: Nikita Zhandarovich <n.zhandarovich@fintech.ru> Applied. Thanks, Guenter > --- > drivers/hwmon/f71882fg.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/drivers/hwmon/f71882fg.c b/drivers/hwmon/f71882fg.c > index 70121482a617..27207ec6f7fe 100644 > --- a/drivers/hwmon/f71882fg.c > +++ b/drivers/hwmon/f71882fg.c > @@ -1096,8 +1096,11 @@ static ssize_t show_pwm(struct device *dev, > val = data->pwm[nr]; > else { > /* RPM mode */ > - val = 255 * fan_from_reg(data->fan_target[nr]) > - / fan_from_reg(data->fan_full_speed[nr]); > + if (fan_from_reg(data->fan_full_speed[nr])) > + val = 255 * fan_from_reg(data->fan_target[nr]) > + / fan_from_reg(data->fan_full_speed[nr]); > + else > + val = 0; > } > mutex_unlock(&data->update_lock); > return sprintf(buf, "%d\n", val);
diff --git a/drivers/hwmon/f71882fg.c b/drivers/hwmon/f71882fg.c index 70121482a617..27207ec6f7fe 100644 --- a/drivers/hwmon/f71882fg.c +++ b/drivers/hwmon/f71882fg.c @@ -1096,8 +1096,11 @@ static ssize_t show_pwm(struct device *dev, val = data->pwm[nr]; else { /* RPM mode */ - val = 255 * fan_from_reg(data->fan_target[nr]) - / fan_from_reg(data->fan_full_speed[nr]); + if (fan_from_reg(data->fan_full_speed[nr])) + val = 255 * fan_from_reg(data->fan_target[nr]) + / fan_from_reg(data->fan_full_speed[nr]); + else + val = 0; } mutex_unlock(&data->update_lock); return sprintf(buf, "%d\n", val);