* [PATCH] iio: st_sensors: add __counted_by_ptr to struct st_sensor_settings
@ 2026-09-22 11:21 Bill Wendling
2026-09-22 14:36 ` Nuno Sá
0 siblings, 1 reply; 2+ messages in thread
From: Bill Wendling @ 2026-09-22 11:21 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Kees Cook, Gustavo A. R. Silva, Randy Dunlap,
linux-iio, linux-kernel, linux-hardening, Bill Wendling,
codemender-patching+linux
Annotate the "ch" pointer member of "struct st_sensor_settings" with the
"__counted_by_ptr" attribute. The elements of "ch" are counted by the
"num_ch" member in the same struct.
All instances of "struct st_sensor_settings" are defined as "static
const" arrays across the ST sensor core drivers. For pressure sensors,
"num_ch" is explicitly initialized with the size of the respective
channel array using "ARRAY_SIZE(...)" during static definition.
For accelerometer, gyroscope, and magnetometer sensors, "num_ch" is not
explicitly initialized (and thus defaults to 0). This is because those
drivers hardcode the channel count to "ST_SENSORS_NUMBER_ALL_CHANNELS"
rather than using "num_ch" from the settings struct.
Since these structures are static const, both "ch" and "num_ch" are
fully initialized at compile time and available immediately at boot
time. The only accesses to the "ch" field of "st_sensor_settings" occur
when assigning it to "indio_dev->channels" during device probing.
Because "sensor_settings->ch" is never dereferenced or accessed as an
array, adding the "__counted_by_ptr" annotation does not cause any
runtime panics or false-positive bounds checks under KASAN or UBSAN.
Cc: codemender-patching+linux@google.com
Assisted-by: LLM
Signed-off-by: Bill Wendling <morbo@google.com>
---
include/linux/iio/common/st_sensors.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
index 1ba496f0fea5..6e914ed49529 100644
--- a/include/linux/iio/common/st_sensors.h
+++ b/include/linux/iio/common/st_sensors.h
@@ -206,7 +206,7 @@ struct st_sensor_settings {
u8 wai;
u8 wai_addr;
char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
- struct iio_chan_spec *ch;
+ struct iio_chan_spec *ch __counted_by_ptr(num_ch);
int num_ch;
struct st_sensor_odr odr;
struct st_sensor_power pw;
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] iio: st_sensors: add __counted_by_ptr to struct st_sensor_settings
2026-09-22 11:21 [PATCH] iio: st_sensors: add __counted_by_ptr to struct st_sensor_settings Bill Wendling
@ 2026-09-22 14:36 ` Nuno Sá
0 siblings, 0 replies; 2+ messages in thread
From: Nuno Sá @ 2026-09-22 14:36 UTC (permalink / raw)
To: Bill Wendling
Cc: Jonathan Cameron, David Lechner, Andy Shevchenko, Kees Cook,
Gustavo A. R. Silva, Randy Dunlap, linux-iio, linux-kernel,
linux-hardening, codemender-patching+linux
On Tue, Sep 22, 2026 at 11:21:11AM +0000, Bill Wendling wrote:
> Annotate the "ch" pointer member of "struct st_sensor_settings" with the
> "__counted_by_ptr" attribute. The elements of "ch" are counted by the
> "num_ch" member in the same struct.
>
> All instances of "struct st_sensor_settings" are defined as "static
> const" arrays across the ST sensor core drivers. For pressure sensors,
> "num_ch" is explicitly initialized with the size of the respective
> channel array using "ARRAY_SIZE(...)" during static definition.
>
> For accelerometer, gyroscope, and magnetometer sensors, "num_ch" is not
> explicitly initialized (and thus defaults to 0). This is because those
> drivers hardcode the channel count to "ST_SENSORS_NUMBER_ALL_CHANNELS"
> rather than using "num_ch" from the settings struct.
>
> Since these structures are static const, both "ch" and "num_ch" are
> fully initialized at compile time and available immediately at boot
> time. The only accesses to the "ch" field of "st_sensor_settings" occur
> when assigning it to "indio_dev->channels" during device probing.
>
> Because "sensor_settings->ch" is never dereferenced or accessed as an
> array, adding the "__counted_by_ptr" annotation does not cause any
> runtime panics or false-positive bounds checks under KASAN or UBSAN.
>
> Cc: codemender-patching+linux@google.com
> Assisted-by: LLM
> Signed-off-by: Bill Wendling <morbo@google.com>
> ---
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
> include/linux/iio/common/st_sensors.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
> index 1ba496f0fea5..6e914ed49529 100644
> --- a/include/linux/iio/common/st_sensors.h
> +++ b/include/linux/iio/common/st_sensors.h
> @@ -206,7 +206,7 @@ struct st_sensor_settings {
> u8 wai;
> u8 wai_addr;
> char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
> - struct iio_chan_spec *ch;
> + struct iio_chan_spec *ch __counted_by_ptr(num_ch);
> int num_ch;
> struct st_sensor_odr odr;
> struct st_sensor_power pw;
> --
> 2.55.0.1082.g2b9226bbc0-goog
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-09-22 14:35 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-22 11:21 [PATCH] iio: st_sensors: add __counted_by_ptr to struct st_sensor_settings Bill Wendling
2026-09-22 14:36 ` Nuno Sá
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®