mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Maoyi Xie <maoyixie.tju@gmail.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
	"Song Qiang" <songqiang1304521@gmail.com>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Praveen Kakkolangara" <praveen.kakkolangara@aumovio.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] iio: magnetometer: rm3100: check TMRC in rm3100_get_samp_freq()
Date: Thu, 19 Nov 2026 11:48:57 +0200	[thread overview]
Message-ID: <av7GCa_copMyGP4F@ashevche-desk.local> (raw)
In-Reply-To: <20261118164106.4108794-1-maoyixie.tju@gmail.com>

On Thu, Nov 19, 2026 at 12:41:06AM +0800, Maoyi Xie wrote:
> rm3100_get_samp_freq() reads TMRC from the sensor and uses the value
> minus RM3100_TMRC_OFFSET as an index into rm3100_samp_rates[] without a
> check. A value outside 0x92 to 0x9f reads outside the table. The driver
> only writes values in that range, so this needs a chip or bus that
> misbehaves after probe.
> 
> Commit 792595bab492 ("iio: magnetometer: rm3100: add boundary check for
> the value read from RM3100_REG_TMRC") added the check to
> rm3100_common_probe() after a crash on failing hardware. Do the same
> here and return -EINVAL.
> 
> Found with a CodeQL-based static analysis tool that we are developing.
> Reproduced in a userspace harness under ASan and UBSan, which report
> nothing with this patch. Not tested on hardware, only compile-tested
> with W=1 on next-20261116.

...

> static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)
>  {
>  	unsigned int tmp;
>  	int ret;
> +	int samp_rate_index;

Preserve reversed xmas tree order.

...

> -	*val = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][0];
> -	*val2 = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][1];
> +	samp_rate_index = tmp - RM3100_TMRC_OFFSET;
> +	if (samp_rate_index < 0 || samp_rate_index >= RM3100_SAMP_NUM)

Wouldn't be better to use ARRAY_SIZE() if possible?

> +		return -EINVAL;

Maybe -ERANGE?

> +	*val = rm3100_samp_rates[samp_rate_index][0];
> +	*val2 = rm3100_samp_rates[samp_rate_index][1];

-- 
With Best Regards,
Andy Shevchenko



      reply	other threads:[~2026-11-19  9:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 16:41 Maoyi Xie
2026-11-19  9:48 ` Andy Shevchenko [this message]

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=av7GCa_copMyGP4F@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maoyixie.tju@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=praveen.kakkolangara@aumovio.com \
    --cc=songqiang1304521@gmail.com \
    /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

all inboxes | Powered by JetHome®