linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Kees Cook <keescook@chromium.org>
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: Fri, 3 Feb 2023 17:57:00 -0800	[thread overview]
Message-ID: <20230204015700.GG3089769@roeck-us.net> (raw)
In-Reply-To: <63dd8c1a.170a0220.d3456.3451@mx.google.com>

On Fri, Feb 03, 2023 at 10:35:05PM +0000, Kees Cook wrote:
> On Sat, Jan 28, 2023 at 05:13:19AM -0800, Guenter Roeck wrote:
> > On Fri, Jan 27, 2023 at 02:37:45PM -0800, Kees Cook wrote:
> > > The index into various register arrays was not bounds checked. Add
> > > checking. Seen under GCC 13:
> > > 
> > > drivers/hwmon/lm85.c: In function 'pwm_auto_pwm_minctl_store':
> > > drivers/hwmon/lm85.c:1110:26: warning: array subscript [0, 31] is outside array bounds of 'struct lm85_autofan[3]' [-Warray-bounds=]
> > >  1110 |         if (data->autofan[nr].min_off)
> > >       |             ~~~~~~~~~~~~~^~~~
> > > drivers/hwmon/lm85.c:317:29: note: while referencing 'autofan'
> > >   317 |         struct lm85_autofan autofan[3];
> > >       |                             ^~~~~~~
> > > 
> > 
> > This is a false positive. The value can never be >= 3.
> > It is derived from the last value of the following
> > SENSOR_DEVICE_ATTR_RW() entries.
> > 
> > I resist making changes like this to the code just because
> > the compiler can not determine the range of a variable.
> > It blows up code size amd makes it hard to read just to
> > make the compiler happy.
> 
> I think it's worth it given the index is an "int" and it'd be very easy
> for things to go wrong in the face of other memory corruption, etc. I've
> sent a v2 that I think is much more readable and non-invasive but
> provides similar robustness.
> 

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.

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.

Guenter

  reply	other threads:[~2023-02-04  1:57 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 [this message]
2023-02-04 18:55       ` Kees Cook
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=20230204015700.GG3089769@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=jdelvare@suse.com \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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).