From patchwork Sun Jul 7 14:47:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13726021 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A986D2263A for ; Sun, 7 Jul 2024 14:47:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720363661; cv=none; b=t69p7KD1nLRRO11Yb49lSXwl9ZpWAJzZ2XaRsFAM8f1g6FQAnOfNu2H3/1LGHzD/fL+Kixt70u3mP4gEgRqbKx5pAXl5+mDqXUwyGr6TVZY1EDzs6j+vTkd26MFFZxP5KBDvPZUjnf7WYaRf9XqMlmTaUF6vWH0UoLxBbdCJOWw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720363661; c=relaxed/simple; bh=U/oN48LnusEKS0cwQHrYQmZfyy+w2xjXS9PZSQxnZ3I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fDjiG2IlqgdGK6nSfh5w5KwB9q0pIklTe7q/84qrBZUmhqvlBBht4kVeRDagpbjDkt7Zi1x15fPL8YIi5c56Otre37BsTRvPAiduMk1ebOcWWoj3us3TsfM65JEU/TNVKX7NaD6oEivp7Yaf8gsQ8Stl4FogT1klo+H4pG1t3FU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SZ4draT8; arc=none smtp.client-ip=209.85.216.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SZ4draT8" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2c8517aab46so2268897a91.1 for ; Sun, 07 Jul 2024 07:47:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720363658; x=1720968458; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=2tE900apGoG5K8RVzmYNJWODr1eZ3+rfBsCAtQZIY98=; b=SZ4draT8SZQUGlJdTqsg50Bk7IYS0pFQe+rmHy8D7TU79DPyazskTyJjzjLa5EUn/g /y6HnFpJScIsnhudWfMuVdDdmCUpzCu1ALnYbXr4AfMByTsLbbALLOwUtM9AR+xdzxO2 zEYeSQuJGHkyemwutFvIy2Eu6A11nD4TW03VCoFhEGUlujyEG6pFhyahxMwSEhgNksEq Ker1jaxjAbaG/m/poATdVrPFXCyk1f658T93COonr1eTFc4sozOtWdZq0joHmJgOpkxt i6Farf498Fe6mwMDJJuv/OHh4rmobdQD+G9iNDTVakCsb2UXuRaRWOP/OPJlFGeYL/pt eBxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720363658; x=1720968458; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=2tE900apGoG5K8RVzmYNJWODr1eZ3+rfBsCAtQZIY98=; b=dcnDUXPCWzHi4ZEkJ0N/eqollzrTWKSR+Aq4C5tiJAGIuL1Y3t9MZOtfP92NizqKRF +q3nTsRxuHVsz1rZgBRrSyNj60awHbqg5kuPzQebqmHL+nTLS/RNalE/y6QQ/G273YkR 2q2BoeJw8xAaIjwuzEF2D51tp0T5UhpzGJUIq5ido+zP2+XLnIpjLNgjSDtyCDGbCHfT jmzDq6TzIl3eBWY6nHKCBQ2N49qO/bPOQlrFva/X8ZthVv8thXteVMVkjJa6oFcVAaI+ 50auU61eb2kkuwP2nVrceHLV5chjjPxTVoEwP3KSrgFbkFz9vBhRQrk/fPFjRgV8ExQP rpVA== X-Gm-Message-State: AOJu0Yzzb6F6w/OqaWz0lGBeSWclu8r2JKPgrZHuw/lHM5Zsx7jWFecQ Jy2W77/+JNX1Fb15XQa0rDCHIOkOFmd3QnL3B2zwLyjg6u6BvJN01wwY/Q== X-Google-Smtp-Source: AGHT+IGgURUunbjqy5kFRY0tgNvEV1b0sw4LmFMPBVsLR0z4ef2pOAKLdp9TssgkarMeYJZv7xiPYA== X-Received: by 2002:a17:90a:69a5:b0:2c8:5cb3:42b6 with SMTP id 98e67ed59e1d1-2c99c5057e5mr7343132a91.8.1720363658013; Sun, 07 Jul 2024 07:47:38 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97a7d8sm6540656a91.22.2024.07.07.07.47.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jul 2024 07:47:37 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 1/4] hwmon: (adc128d818) Fix underflows seen when writing limit attributes Date: Sun, 7 Jul 2024 07:47:27 -0700 Message-Id: <20240707144730.1490208-2-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240707144730.1490208-1-linux@roeck-us.net> References: <20240707144730.1490208-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations. Signed-off-by: Guenter Roeck --- drivers/hwmon/adc128d818.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/adc128d818.c b/drivers/hwmon/adc128d818.c index 8ac6e735ec5c..5e805d4ee76a 100644 --- a/drivers/hwmon/adc128d818.c +++ b/drivers/hwmon/adc128d818.c @@ -175,7 +175,7 @@ static ssize_t adc128_in_store(struct device *dev, mutex_lock(&data->update_lock); /* 10 mV LSB on limit registers */ - regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255); + regval = DIV_ROUND_CLOSEST(clamp_val(val, 0, 2550), 10); data->in[index][nr] = regval << 4; reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr); i2c_smbus_write_byte_data(data->client, reg, regval); @@ -213,7 +213,7 @@ static ssize_t adc128_temp_store(struct device *dev, return err; mutex_lock(&data->update_lock); - regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); + regval = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000); data->temp[index] = regval << 1; i2c_smbus_write_byte_data(data->client, index == 1 ? ADC128_REG_TEMP_MAX From patchwork Sun Jul 7 14:47:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13726022 Received: from mail-il1-f176.google.com (mail-il1-f176.google.com [209.85.166.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D50312BAE8 for ; Sun, 7 Jul 2024 14:47:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720363663; cv=none; b=f7SFUbG91T7eG22RpDrlyqBkdfKe583/8m7+xOXWjGPBUkucAaH8+yNvSoaVbXeG3A/KV0bm7/glwDe9teetCY9U7AVUSVuLe2aU+oGnHT1r5HNXdwCepqwWIktXVZEC8vFMB/Rk+dd4JXGv9FVixgyRvnwT97hQLwuINEzsFhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720363663; c=relaxed/simple; bh=OwotWA2iEZm9EHtic7oOoo/bz6skHoHRlN6LPZFvBxw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DefVYLUo/ph0f2S7WM5UHt9EZ5aUdEMOhenyYfmJlUdO16K/Uci0kqMzQwtfIfL9XDIjMooncD+xP9rt/P3TFxmJnSYJcJpahJ0noCsjlZ6w+2sIxsTF/kncZLbnfEsw5d7+/zdgxYHQIz78W0ul66wOwkuqSUr3Ruome3naGe4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=e7BT3wDQ; arc=none smtp.client-ip=209.85.166.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="e7BT3wDQ" Received: by mail-il1-f176.google.com with SMTP id e9e14a558f8ab-375af3538f2so13416505ab.3 for ; Sun, 07 Jul 2024 07:47:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720363660; x=1720968460; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=64fBQHR2i8u9oR3UY62ZgTI2mEitUHDWKavLr8Dpk18=; b=e7BT3wDQKXD8Q1ZvMcxPBcqwqADymwc+SZbdo3cOmscmhZZ70k4nAaannIdORAC+7t okwRrmDB0rrU/SXF52lz1l+TqJsvhnkuWFS0xHnC5wCKbOG3adUs+Auak9B7vtYXlLnB 49FCOkKwi5dDE6bC7yp0JAMAlyyWSvhakr0TeQjqjC+F20f6kgJ1pziHQDM0xEicmri3 pcCyLsS+NmBi/HEp0sbaAeYqvMtlECODbuOh9wbam7/N8XhU8m8o2VjdWEj2HV8xrPRk 090tBTzWutv0+2aE6cMepVX1R1vjSwv8h/gtD9VU6RG4OQkL1ZuCVckY+rl6r5xF4Dyn fF5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720363660; x=1720968460; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=64fBQHR2i8u9oR3UY62ZgTI2mEitUHDWKavLr8Dpk18=; b=kjjuRnYQ5ZGb8vfZFEjWbm3R8zNcP4NXxrt4AEgmO6rfSZW7/lHc6BqNsIXwuGRvG4 CoTk3A+LDQvdwEIo7/vJBb/fVuZJdJxNghvs57/z8X7hpO+pdHYGPP/+KMCECeQkKx4s mtBLhgnA9Of3kBvY+4IIkkgpS5UH2xHq+hOmBr4KYFm/hMsblXh97oK070oc9wMQS06B 92xKTGqHyqwRqb2K1qmDMLSOkpDwhMT2UkG/isfynDGeFTRhpvpL1dBaLrp2YusRa3G4 mm/B/2P3RXao6v9Kzzr85wAVrhNc7B4kEMDYjaZghNaP+fB3yHU6ru7mEDTqe4HaWx+P NBJg== X-Gm-Message-State: AOJu0Yyjg7K29dwtqgGcauN9LleyhbUUHEcLEa225X5JH7l8f8thHxOw t7qttD9hQgHV/bC8+8uD5t1vpfTcrKt3DUWNydnkqhCtG6gUE98puf6k9g== X-Google-Smtp-Source: AGHT+IH04wBcsepKQWIhjRmN5DDXUnwFdHQNyNwGndvu3r6YH9QVYqe1vv/IWrmImB52MezDXl4CKg== X-Received: by 2002:a05:6e02:1ca8:b0:382:64aa:d8d2 with SMTP id e9e14a558f8ab-3839acbc2cemr133887395ab.25.1720363660049; Sun, 07 Jul 2024 07:47:40 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99aa895ecsm6467655a91.47.2024.07.07.07.47.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jul 2024 07:47:39 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 2/4] hwmon: (lm95234) Fix underflows seen when writing limit attributes Date: Sun, 7 Jul 2024 07:47:28 -0700 Message-Id: <20240707144730.1490208-3-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240707144730.1490208-1-linux@roeck-us.net> References: <20240707144730.1490208-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations. Signed-off-by: Guenter Roeck --- drivers/hwmon/lm95234.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/hwmon/lm95234.c b/drivers/hwmon/lm95234.c index 67b9d7636ee4..37e8e9679aeb 100644 --- a/drivers/hwmon/lm95234.c +++ b/drivers/hwmon/lm95234.c @@ -301,7 +301,8 @@ static ssize_t tcrit2_store(struct device *dev, struct device_attribute *attr, if (ret < 0) return ret; - val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127); + val = DIV_ROUND_CLOSEST(clamp_val(val, 0, (index ? 255 : 127) * 1000), + 1000); mutex_lock(&data->update_lock); data->tcrit2[index] = val; @@ -350,7 +351,7 @@ static ssize_t tcrit1_store(struct device *dev, struct device_attribute *attr, if (ret < 0) return ret; - val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255); + val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 255000), 1000); mutex_lock(&data->update_lock); data->tcrit1[index] = val; @@ -391,7 +392,7 @@ static ssize_t tcrit1_hyst_store(struct device *dev, if (ret < 0) return ret; - val = DIV_ROUND_CLOSEST(val, 1000); + val = DIV_ROUND_CLOSEST(clamp_val(val, -255000, 255000), 1000); val = clamp_val((int)data->tcrit1[index] - val, 0, 31); mutex_lock(&data->update_lock); @@ -431,7 +432,7 @@ static ssize_t offset_store(struct device *dev, struct device_attribute *attr, return ret; /* Accuracy is 1/2 degrees C */ - val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127); + val = DIV_ROUND_CLOSEST(clamp_val(val, -64000, 63500), 500); mutex_lock(&data->update_lock); data->toffset[index] = val; From patchwork Sun Jul 7 14:47:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13726023 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00F422BAE9 for ; Sun, 7 Jul 2024 14:47:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720363664; cv=none; b=X6kNseWqwuxdTgQaFlRzdCA+37baNYQYdNccN/XXXY1+W9auThgU0FRxhDAEBB09R1GyTp/znpVdXjfXRsGFEsPqCk8XL2QznKJ+e81wxPT8KrRf68Cipwg/p6t7gz1/Gp/iXXHhDn5kdIRhliYo25uuaIz4Ii8OUXUNrrryKNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720363664; c=relaxed/simple; bh=JyUXSmcWXRmr74TVanMDnIjVO8uLxqaxJ6dHRkFVQdw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qv13tN7/8K1JJYACNVrX6xi6ntdEnzbdprCTOw+NnCeqVFKEx5WvFRkD+eeesWfdp510J7jQUfKlXIoUzeHXJirUPBcYmAXCq9cyDh8Cf3CYuezgUKFvtynrO/SZv3Py+GS46dU0AcUQD78poe4tIPQ6FOEupF7E06Lyi7RnkqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G8Y1mMS+; arc=none smtp.client-ip=209.85.210.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G8Y1mMS+" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-70af81e8439so2581944b3a.0 for ; Sun, 07 Jul 2024 07:47:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720363662; x=1720968462; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=fytkPyetvofip46m/hlgw8/3Snawnk4ucvawG1atrYM=; b=G8Y1mMS+SyiE2b5u6WiG5QI3J/ZYX90lydWj8e21Uh3ghduv7lCBfU9LyuSWwH7dS0 Kw5AKrT+bkozdwJ0Wc0KnST+XCzEnsjSgF8NsUSdBC3CBuQMIgBhOUx9l0Yp5BAH0+fc 0BwFO690V0ZDC8/46feWzZkRl5scC485A7Df7bsaY1/2mKuVROqR/trmZvDvDHXtKIfT cN7y9SKofE69zs1lq51zh6Hbt0PUdrC6psZy2xUHv+T/VvmURznoQR3BnXoG3Dft//S+ qRzF1qyC2CDlCBI+dd0ElwFzy//+QDLYQyNOKCg0/2wyZUySnHwVRRrUwXYsiRthFQwi DQAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720363662; x=1720968462; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fytkPyetvofip46m/hlgw8/3Snawnk4ucvawG1atrYM=; b=oLq3lA5Y9JJfRkyZcTe9hLNQiDTA2vgsMnxPhua0WtKRaI3JeLwRzLZxzp9f5UU0p9 m1YOSO33qseg1BrfBHCjBf6ZNeMoh+CFjQnLg5aN7Xf4wM3nyrPWYFlAxxJkYNBuxO4z 7T+93Q4CZPmG/OEB1ZVcV9i9vKYiNYT29AQ16JaZ7v2IK7Uvyk7CxxZV3EO+3Nd2Ga0X c6l8n0LBqq/0Ng4U4q8TIIIWGPkus6qI5UODjUlGdZpSuLqYNvdk7flnXdTZ1Z6v1X4C gim4hNrg/kMzbzsh7yLvAiVLXlGQ65g8edSqicMfNc6InH3Q5GucH02mbI8Up47A7WSA Ex0Q== X-Gm-Message-State: AOJu0YzYp9DzsaBw5ql4aay+0O/0xGEMR5lXkYwR+I8eWeGIRSmmWUrT VxA8S1N/9Qt+Oe1bfvo31bv8IR60rrTxz68snEotoV/y8qPiDq7gf1aKnA== X-Google-Smtp-Source: AGHT+IHXHNVXB2lXPXA2O91GBU3oIJ1yZ9ETBD+2Z2MCVpLYY/XXJD0hL4PtJcbXNUl1QpK9GJr4nQ== X-Received: by 2002:a05:6a20:12d5:b0:1be:c929:e269 with SMTP id adf61e73a8af0-1c0cc74f803mr12879175637.34.1720363661819; Sun, 07 Jul 2024 07:47:41 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fb1e50f63asm76907205ad.138.2024.07.07.07.47.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jul 2024 07:47:41 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 3/4] hwmon: (nct6775-core) Fix underflows seen when writing limit attributes Date: Sun, 7 Jul 2024 07:47:29 -0700 Message-Id: <20240707144730.1490208-4-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240707144730.1490208-1-linux@roeck-us.net> References: <20240707144730.1490208-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations. Signed-off-by: Guenter Roeck --- drivers/hwmon/nct6775-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hwmon/nct6775-core.c b/drivers/hwmon/nct6775-core.c index 9fbab8f02334..934fed3dd586 100644 --- a/drivers/hwmon/nct6775-core.c +++ b/drivers/hwmon/nct6775-core.c @@ -2262,7 +2262,7 @@ store_temp_offset(struct device *dev, struct device_attribute *attr, if (err < 0) return err; - val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); + val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000); mutex_lock(&data->update_lock); data->temp_offset[nr] = val; From patchwork Sun Jul 7 14:47:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13726024 Received: from mail-io1-f53.google.com (mail-io1-f53.google.com [209.85.166.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69F01249F5 for ; Sun, 7 Jul 2024 14:47:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720363666; cv=none; b=CLVRq/Mp6zDuz58h2lxY5JWsjZKQy0+Uh9pqCDjtDzaQ5nAhcSOFtGSG3xpslf1z7XzG436yTx6YL/nWsAujS8fZJKX4bS5DUVt9V1eufIYn5dq0LCxP/oOVUDM38eMdX1R49tNK12DH0StdqWzAyWn8qBNlc+VX0Ro3g7kjOv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720363666; c=relaxed/simple; bh=OxMxHKY/7EcDMXWNJgMSRcvEDpp8+Cu6kzSCm5Rnns4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZaNFNVilrsT4DUwQUpe5E1LUQGI1kh/i8MCTu3L2yF3Hq69IAy4jtMKwwYqPZQs3gKYQ5a1TK81+GOcRrD4lWrlTqwQbpQLfZStk2xp3D6p/IVTTzsFq90rgV9wO3zx0ClJ9n8sXRn0eAWWg0wUJREcYHzi7jePN+57zWnyNrbE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=R33hAM+E; arc=none smtp.client-ip=209.85.166.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R33hAM+E" Received: by mail-io1-f53.google.com with SMTP id ca18e2360f4ac-7fa8ffd03aaso23544439f.0 for ; Sun, 07 Jul 2024 07:47:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720363664; x=1720968464; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=uXhLjdmO1CjATRhU5W7zEDwOJSaHfyV1bCOmj/qlkIA=; b=R33hAM+EwTpGBewdl9KHaUb1zh62+EkzkWl6bTg6Z68eIg78bjvVcJpWuuJaIuJbOL /IiWEWRNSs24nL9718yRKwjg6SkGoE1TaG4vSjwufvNeNvC0vpaiiTH0TYpPk7MJE/Mw uYNUhfoKdFcLVpDqe7hHxRajjnKfrrKidy6JfRkHr4lrMfPlYUNRs10Fc90V8kCfDrP4 hYbZjs80MbilzMCcB4VrLxx2ktScbAlFL05ily0eO8A7xpWCyrf6/MwNjNinHq2/WgUP kg/zseoVR3RfME7fA7BxFRmUaOVQT3zFbmkHhM6WNv36F7RxGjIBOVM4L9K9Xss6vGvE SLcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720363664; x=1720968464; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=uXhLjdmO1CjATRhU5W7zEDwOJSaHfyV1bCOmj/qlkIA=; b=b21TAYFFHryMVF3kVhY1jexHwbms28IDJpRfh2vYehJSWjBsmj6XBduQy4GRxj4SLw nTfhvI32twfOak9y30X8jwoD7ICjAlkHE/cC+4z8KX7Van5u+626z3Clc/b6fpHuLsqO sos1xBrkyUmLQ6NDaI2phvbvB0T6hDTRwEVzPyRMYTkrfMf0GJAFdOHgY3V9gfnplseH hYeKIAv/8mQ5VIxwInA6y0Xu1qTz0yBZYBEHeVXzxhUu4UoF8qP+bU51t1wRwfFscUXf L+wb6tiWr0ROIh5qMxzrWxcBhIFOqzBEA1ok7RJHn32d0XjjFIX+zyqH7oqJp5LE6K1+ w0IQ== X-Gm-Message-State: AOJu0YyBI6fKGQZ1BhMwjsJ9SuwIUYQKmXuJHIHoQK7XZ976Z5ADKenP n7kz3oZt15CakaCwf1L6guFeGE5r7Pwjn9XIIQWADmHV+1niClHdRPKqlA== X-Google-Smtp-Source: AGHT+IEHqMqQxgN11Sg5yZWE1Pxj6jX0/HgIgJFoUK89ssp1Gp72THdQ//lN2OGXXIxlHANOgpfngA== X-Received: by 2002:a05:6e02:1d04:b0:375:a6cd:dff2 with SMTP id e9e14a558f8ab-3839871068dmr130491465ab.5.1720363663697; Sun, 07 Jul 2024 07:47:43 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a9977cfsm6456774a91.34.2024.07.07.07.47.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jul 2024 07:47:43 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 4/4] hwmon: (w83627ehf) Fix underflows seen when writing limit attributes Date: Sun, 7 Jul 2024 07:47:30 -0700 Message-Id: <20240707144730.1490208-5-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240707144730.1490208-1-linux@roeck-us.net> References: <20240707144730.1490208-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations. Signed-off-by: Guenter Roeck --- drivers/hwmon/w83627ehf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/w83627ehf.c b/drivers/hwmon/w83627ehf.c index fe960c0a624f..7d7d70afde65 100644 --- a/drivers/hwmon/w83627ehf.c +++ b/drivers/hwmon/w83627ehf.c @@ -895,7 +895,7 @@ store_target_temp(struct device *dev, struct device_attribute *attr, if (err < 0) return err; - val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 127); + val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 127000), 1000); mutex_lock(&data->update_lock); data->target_temp[nr] = val; @@ -920,7 +920,7 @@ store_tolerance(struct device *dev, struct device_attribute *attr, return err; /* Limit the temp to 0C - 15C */ - val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 15); + val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 15000), 1000); mutex_lock(&data->update_lock); reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);