Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Jean Delvare <jdelvare@suse.com>,
	Chris Packham <chris.packham@alliedtelesis.co.nz>,
	linux-hwmon@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] hwmon: (adt7470) Prevent divide by zero in adt7470_fan_write()
Date: Fri, 21 Jan 2022 10:37:52 +0300	[thread overview]
Message-ID: <20220121073752.GE1978@kadam> (raw)
In-Reply-To: <4ef54638-1b51-2507-1e45-ff9050c3711e@roeck-us.net>

On Thu, Jan 20, 2022 at 10:10:45PM -0800, Guenter Roeck wrote:
> On 1/20/22 9:39 PM, Dan Carpenter wrote:
> > The "val" variable is controlled by the user and comes from
> > hwmon_attr_store().  The FAN_RPM_TO_PERIOD() macro divides by "val"
> > so a zero will crash the system.  Check for that and return -EINVAL.
> > 
> > Fixes: fc958a61ff6d ("hwmon: (adt7470) Convert to devm_hwmon_device_register_with_info API")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >   drivers/hwmon/adt7470.c | 3 +++
> >   1 file changed, 3 insertions(+)
> > 
> > diff --git a/drivers/hwmon/adt7470.c b/drivers/hwmon/adt7470.c
> > index d519aca4a9d6..cd474584dc0b 100644
> > --- a/drivers/hwmon/adt7470.c
> > +++ b/drivers/hwmon/adt7470.c
> > @@ -662,6 +662,9 @@ static int adt7470_fan_write(struct device *dev, u32 attr, int channel, long val
> >   	struct adt7470_data *data = dev_get_drvdata(dev);
> >   	int err;
> > +	if (!val)
> > +		return -EINVAL;
> > +
> 
> Technically that restores old (pre-fc958a61ff6d) behavior, but it is still bad:
> Userspace can provide a value of -1 (or any other negative number), and it will
> translate to 5400000 RPM. So it should either be
> 
> 	if (val <= 0)
> 		return -EINVAL;
> 
> or
> 	if (val <= 0)
> 		val = 1;

There is a clamp() which does already turn invalid values into something
valid.

	val = FAN_RPM_TO_PERIOD(val);
	val = clamp_val(val, 1, 65534);

But I will make the <= 0 return -EINVAL change and resend.

regards,
dan carpenter


  reply	other threads:[~2022-01-21  7:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-21  5:39 [PATCH] hwmon: (adt7470) Prevent divide by zero in adt7470_fan_write() Dan Carpenter
2022-01-21  6:10 ` Guenter Roeck
2022-01-21  7:37   ` Dan Carpenter [this message]
2022-01-21 14:03     ` 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=20220121073752.GE1978@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=chris.packham@alliedtelesis.co.nz \
    --cc=jdelvare@suse.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-hwmon@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