linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Jean Delvare <jdelvare@suse.com>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH] lm85: Bounds check to_sensor_dev_attr()->index usage
Date: Sat, 4 Feb 2023 10:55:07 -0800	[thread overview]
Message-ID: <63deaa0b.050a0220.bf1e1.6c26@mx.google.com> (raw)
In-Reply-To: <20230204015700.GG3089769@roeck-us.net>

On Fri, Feb 03, 2023 at 05:57:00PM -0800, Guenter Roeck wrote:
> That line of argument would suggest that we should perform parameter checks
> on each function entry all over the place, no matter if the range is known
> to be valid or not. Maybe that is the way things are going, but I don't
> like it at all. I have seen that kind of code before, in the telco space,
> where it typically at least doubled code size and resulted in mediocre
> performance, just because of a rule that mandated checking all parameters
> at the beginning of each function.

Well, I doubt I'll be able to change your opinion of telco code, but I
do think robustness is not an unreasonable default state for software,
and that GCC and Clang do a pretty good job with optimization, etc.

> I assume this is just one of many many patches you plan to send to add
> parameter checks to similar hwmon code ? I _really_ don't want to have
> the hwmon code cluttered with such unnecessary checks.

I was trying to provide complete coverage inspired by the specific
complaint GCC had, but this would also silence the warning:

diff --git a/drivers/hwmon/lm85.c b/drivers/hwmon/lm85.c
index 8d33c2484755..87d2455e721f 100644
--- a/drivers/hwmon/lm85.c
+++ b/drivers/hwmon/lm85.c
@@ -1106,6 +1106,7 @@ static ssize_t pwm_auto_pwm_minctl_store(struct device *dev,
 	mutex_lock(&data->update_lock);
 	data->autofan[nr].min_off = val;
 	tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
+	nr = clamp_t(int, nr, 0, ARRAY_SIZE(data->autofan) - 1);
 	tmp &= ~(0x20 << nr);
 	if (data->autofan[nr].min_off)
 		tmp |= 0x20 << nr;

What's happening is GCC see that "nr" is used as a shift argument, so it
believes (not unreasonably) that this otherwise unknown value could be
up to 32. Here we can give it the bounded range ahead of time, keeping
it happy.

-- 
Kees Cook

  reply	other threads:[~2023-02-04 18:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-27 22:37 [PATCH] lm85: Bounds check to_sensor_dev_attr()->index usage Kees Cook
2023-01-28 13:13 ` Guenter Roeck
2023-02-03 22:35   ` Kees Cook
2023-02-04  1:57     ` Guenter Roeck
2023-02-04 18:55       ` Kees Cook [this message]
2023-02-05 17:37         ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=63deaa0b.050a0220.bf1e1.6c26@mx.google.com \
    --to=keescook@chromium.org \
    --cc=jdelvare@suse.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).