From patchwork Thu Dec 8 19:26:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 9467025 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A9CE66071E for ; Thu, 8 Dec 2016 19:26:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 97A84285B1 for ; Thu, 8 Dec 2016 19:26:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8C357285EE; Thu, 8 Dec 2016 19:26:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 36031285B1 for ; Thu, 8 Dec 2016 19:26:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752199AbcLHT0w (ORCPT ); Thu, 8 Dec 2016 14:26:52 -0500 Received: from bh-25.webhostbox.net ([208.91.199.152]:50239 "EHLO bh-25.webhostbox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752126AbcLHT0w (ORCPT ); Thu, 8 Dec 2016 14:26:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=roeck-us.net; s=default; h=Message-Id:Date:Subject:Cc:To:From; bh=5PBnYGI8RUSsq5pyFM1SLVleec9SMIh/yeBPvZ40Sfo=; b=RD8k83GO658jQfMBTp4CvauGPz aUC2uACXyecvrMVvAVNbPAWyiCKAeisJZ88i+RfRZS7gvfOJr/d+n9zjhE0dTGaw2TR5sQDwF3ZeB CswJhYpGuF9IBMGqVM67nA2lfbjmjMzoKk/GYlBk/uXoZgX2KRAR/zYSYEO2uZ/tHEDjWcj+u+uGP i++wlHeeqsr1sxP73dXEzwhAHlsUmmPUlaw1zInUXocnmDDqmuJGDc7n2gvJW4S/674P+3IsIYORk A/GJdBu60+uYCXNGCCOuul8LpGX6tRbtcVbcZLJMghnnRBf760GivXgJ2DYZcQSGxLxTdRc0pr+S3 Fg4ZzpTQ==; Received: from 108-223-40-66.lightspeed.sntcca.sbcglobal.net ([108.223.40.66]:35420 helo=localhost) by bh-25.webhostbox.net with esmtpa (Exim 4.86_1) (envelope-from ) id 1cF4LB-001LdK-B0; Thu, 08 Dec 2016 19:26:49 +0000 From: Guenter Roeck To: Hardware Monitoring Cc: Jean Delvare , Guenter Roeck Subject: [PATCH v2] hwmon: (adt7462) Fix overflows seen when writing into limit attributes Date: Thu, 8 Dec 2016 11:26:49 -0800 Message-Id: <1481225209-12936-1-git-send-email-linux@roeck-us.net> X-Mailer: git-send-email 2.5.0 X-Authenticated_sender: guenter@roeck-us.net X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bh-25.webhostbox.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - roeck-us.net X-Get-Message-Sender-Via: bh-25.webhostbox.net: authenticated_id: guenter@roeck-us.net X-Authenticated-Sender: bh-25.webhostbox.net: guenter@roeck-us.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Fix overflows seen when writing large values into temperature limit, voltage limit, and pwm hysteresis attributes. The input parameter to DIV_ROUND_CLOSEST() needs to be clamped to avoid such overflows. Signed-off-by: Guenter Roeck Reviewed-by: Jean Delvare --- v2: Simplified clamping Separate clamp_val() and DIV_ROUND_CLOSEST() into two lines. drivers/hwmon/adt7462.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/adt7462.c b/drivers/hwmon/adt7462.c index 5929e126da63..19f2a6d48bac 100644 --- a/drivers/hwmon/adt7462.c +++ b/drivers/hwmon/adt7462.c @@ -810,8 +810,8 @@ static ssize_t set_temp_min(struct device *dev, if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index)) return -EINVAL; + temp = clamp_val(temp, -64000, 191000); temp = DIV_ROUND_CLOSEST(temp, 1000) + 64; - temp = clamp_val(temp, 0, 255); mutex_lock(&data->lock); data->temp_min[attr->index] = temp; @@ -848,8 +848,8 @@ static ssize_t set_temp_max(struct device *dev, if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index)) return -EINVAL; + temp = clamp_val(temp, -64000, 191000); temp = DIV_ROUND_CLOSEST(temp, 1000) + 64; - temp = clamp_val(temp, 0, 255); mutex_lock(&data->lock); data->temp_max[attr->index] = temp; @@ -912,9 +912,9 @@ static ssize_t set_volt_max(struct device *dev, if (kstrtol(buf, 10, &temp) || !x) return -EINVAL; + temp = clamp_val(temp, 0, 255 * x / 1000); temp *= 1000; /* convert mV to uV */ temp = DIV_ROUND_CLOSEST(temp, x); - temp = clamp_val(temp, 0, 255); mutex_lock(&data->lock); data->volt_max[attr->index] = temp; @@ -954,9 +954,9 @@ static ssize_t set_volt_min(struct device *dev, if (kstrtol(buf, 10, &temp) || !x) return -EINVAL; + temp = clamp_val(temp, 0, 255 * x / 1000); temp *= 1000; /* convert mV to uV */ temp = DIV_ROUND_CLOSEST(temp, x); - temp = clamp_val(temp, 0, 255); mutex_lock(&data->lock); data->volt_min[attr->index] = temp; @@ -1220,8 +1220,8 @@ static ssize_t set_pwm_hyst(struct device *dev, if (kstrtol(buf, 10, &temp)) return -EINVAL; + temp = clamp_val(temp, 0, 15000); temp = DIV_ROUND_CLOSEST(temp, 1000); - temp = clamp_val(temp, 0, 15); /* package things up */ temp &= ADT7462_PWM_HYST_MASK; @@ -1306,8 +1306,8 @@ static ssize_t set_pwm_tmin(struct device *dev, if (kstrtol(buf, 10, &temp)) return -EINVAL; + temp = clamp_val(temp, -64000, 191000); temp = DIV_ROUND_CLOSEST(temp, 1000) + 64; - temp = clamp_val(temp, 0, 255); mutex_lock(&data->lock); data->pwm_tmin[attr->index] = temp;