Skip to contentSkip to conversation

iio: st_sensors: add __counted_by_ptr to struct st_sensor_settings

PATCH In review Bill Wendling 5 participants 7 messages Sep 22 – Sep 25 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: st_sensors: add __counted_by_ptr to struct st_sensor_settings

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.

---
 include/linux/iio/common/st_sensors.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/common/st_sensors.h +1 -1
index 1ba496f0fea5..6e914ed49529 100644
@@ -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;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Bill Wendling

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.

Quoted 8 lines

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.

---

Quoted 19 lines
 include/linux/iio/common/st_sensors.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/common/st_sensors.h +1 -1
index 1ba496f0fea5..6e914ed49529 100644
@@ -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;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Reviewed-by on v1 1/1
#2 lore raw
In reply to Bill Wendling
Quoted 19 lines · Bill Wendling

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.

Quoted 7 lines

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.

Thanks
-Gustavo

Quoted 17 lines
---
  include/linux/iio/common/st_sensors.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/common/st_sensors.h +1 -1
index 1ba496f0fea5..6e914ed49529 100644
@@ -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;
#3 lore raw
In reply to Bill Wendling
Quoted 8 lines · Bill Wendling

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.

I wanted to understand this so, in drivers/iio/accel/st_accel_core.c:

static const struct st_sensor_settings st_accel_sensors_settings[] = {
...
.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
...
}

static const struct iio_chan_spec st_accel_12bit_channels[] = {
        ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
                        BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
                        ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
                        ST_ACCEL_DEFAULT_OUT_X_L_ADDR,
                        st_accel_mount_matrix_ext_info),
	...
        IIO_CHAN_SOFT_TIMESTAMP(3)
};
#define IIO_CHAN_SOFT_TIMESTAMP(_si) (struct iio_chan_spec) {           \
        .type = IIO_TIMESTAMP,                                          \
        .channel = -1,                                                  \
        .scan_index = _si,                                              \
        .scan_type = {                                                  \
                .sign = 's',                                            \
                .realbits = 64,                                         \
                .storagebits = 64,                                      \
        },                                                              \

}

So .ch is assigned an array of items which looks to end with a termination sentinel. And .num_ch is unassigned:

$ git grep '\.ch = ' drivers/iio/accel/st_accel_core.c | wc -l
16
$ git grep '\.num_ch = ' drivers/iio/accel/st_accel_core.c | wc -l
0
Quoted 4 lines

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.

Yeah, checks out:

$ git grep '\.ch = ' | cut -d: -f1 | sort | uniq -c
     16 accel/st_accel_core.c
      4 gyro/st_gyro_core.c
      5 magnetometer/st_magn_core.c
      6 pressure/st_pressure_core.c
$ git grep '\.num_ch = ' | cut -d: -f1 | sort | uniq -c
      6 pressure/st_pressure_core.c

Interestingly, the pressure/st_pressure_core.c uses both assigned .num_ch _and_ a sentinel.

Quoted 4 lines

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.

Heh, so the bounds check gets laundered. :) Even pressure:

pressure/st_pressure_core.c:    press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
pressure/st_pressure_core.c:    indio_dev->num_channels = press_data->sensor_settings->num_ch;
Quoted 3 lines

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.

I would argue that either __counted_by_ptr has no use here ("accidentally safe because no one dereferences .ch" isn't a great justification) or that the accelerometer, gyroscope, and magnetometer sensors should have their .num_ch assigned correctly so that the "contract" is correct, even if nothing does the deref.

-Kees

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 79 lines · Kees Cook
Quoted 8 lines · Bill Wendling

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.

I wanted to understand this so, in drivers/iio/accel/st_accel_core.c:

static const struct st_sensor_settings st_accel_sensors_settings[] = {
...
.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
...
}

static const struct iio_chan_spec st_accel_12bit_channels[] = {
        ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
                        BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
                        ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
                        ST_ACCEL_DEFAULT_OUT_X_L_ADDR,
                        st_accel_mount_matrix_ext_info),
        ...
        IIO_CHAN_SOFT_TIMESTAMP(3)
};
#define IIO_CHAN_SOFT_TIMESTAMP(_si) (struct iio_chan_spec) {           \
        .type = IIO_TIMESTAMP,                                          \
        .channel = -1,                                                  \
        .scan_index = _si,                                              \
        .scan_type = {                                                  \
                .sign = 's',                                            \
                .realbits = 64,                                         \
                .storagebits = 64,                                      \
        },                                                              \

}

So .ch is assigned an array of items which looks to end with a termination sentinel. And .num_ch is unassigned:

$ git grep '\.ch = ' drivers/iio/accel/st_accel_core.c | wc -l
16
$ git grep '\.num_ch = ' drivers/iio/accel/st_accel_core.c | wc -l
0
Quoted 4 lines

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.

Yeah, checks out:

$ git grep '\.ch = ' | cut -d: -f1 | sort | uniq -c
     16 accel/st_accel_core.c
      4 gyro/st_gyro_core.c
      5 magnetometer/st_magn_core.c
      6 pressure/st_pressure_core.c
$ git grep '\.num_ch = ' | cut -d: -f1 | sort | uniq -c
      6 pressure/st_pressure_core.c

Interestingly, the pressure/st_pressure_core.c uses both assigned .num_ch _and_ a sentinel.

Quoted 4 lines

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.

Heh, so the bounds check gets laundered. :) Even pressure:

pressure/st_pressure_core.c:    press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
pressure/st_pressure_core.c:    indio_dev->num_channels = press_data->sensor_settings->num_ch;
Quoted 3 lines

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.

Quoted 6 lines

I would argue that either __counted_by_ptr has no use here ("accidentally safe because no one dereferences .ch" isn't a great justification) or that the accelerometer, gyroscope, and magnetometer sensors should have their .num_ch assigned correctly so that the "contract" is correct, even if nothing does the deref.

This is all very weird. Below is the full output from CodeMender. It might give some insight into this, but perhaps we should just NACK this patch?

-bw

-----------------8<----------------
iio: st_sensors: Add __counted_by_ptr to struct st_sensor_settings

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:
- drivers/iio/accel/st_accel_core.c: st_accel_sensors_settings
- drivers/iio/gyro/st_gyro_core.c: st_gyro_sensors_settings
- drivers/iio/magnetometer/st_magn_core.c: st_magn_sensors_settings
- drivers/iio/pressure/st_pressure_core.c: st_press_sensors_settings

For pressure sensors, num_ch is explicitly initialized with the size of the respective channel array using ARRAY_SIZE(...) during static definition:

```c
static const struct st_sensor_settings st_press_sensors_settings[] = {
{
...
.ch = (struct iio_chan_spec *)st_press_1_channels,
.num_ch = ARRAY_SIZE(st_press_1_channels),
...
},
```

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 (4) 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, such as:

```c
indio_dev->channels = adata->sensor_settings->ch;
```

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.

HUMAN REVIEWER INSTRUCTIONS: REMOVE BEFORE SUBMITTING

This change annotates the ch pointer in struct st_sensor_settings with __counted_by_ptr(num_ch).

Comprehensive Testing Plan:

- Compile the kernel and IIO ST sensors driver files to verify there are no build errors or warnings.
- Run:
make_kernel_build_single_file \
drivers/iio/common/st_sensors/st_sensors_core.c
make_kernel_build_single_file \
drivers/iio/accel/st_accel_core.c
make_kernel_build_single_file \
drivers/iio/gyro/st_gyro_core.c
make_kernel_build_single_file \
drivers/iio/magnetometer/st_magn_core.c
make_kernel_build_single_file \
drivers/iio/pressure/st_pressure_core.c
- This driver cannot be KUnit tested because it strictly requires physical ST sensor hardware devices (I2C/SPI) and Regmap register accesses (e.g. WHOAMI .wai_addr check), which cannot be mocked in a UML environment.

Sashiko: https://sashiko.example.com/placeholder

END HUMAN REVIEWER INSTRUCTIONS

Assisted-by LLM

Full summary:
## Walkthrough of Analysis & Root Cause

### 1. Structure Analysis
We analyzed the structure struct st_sensor_settings defined in include/linux/iio/common/st_sensors.h. The structure contains a pointer field struct iio_chan_spec *ch and an integer field int num_ch representing the element count of the array pointed to by ch:

```c
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 __counted_by_ptr(num_ch);
int num_ch;
...
```

### 2. Allocation & Assignment Verification
All instances of struct st_sensor_settings are declared as static, global arrays of constants within the subsystem core files:
- drivers/iio/accel/st_accel_core.c: st_accel_sensors_settings
- drivers/iio/gyro/st_gyro_core.c: st_gyro_sensors_settings
- drivers/iio/magnetometer/st_magn_core.c:
st_magn_sensors_settings
- drivers/iio/pressure/st_pressure_core.c:
st_press_sensors_settings

At boot/probe time, the settings are retrieved from these static arrays and assigned to pointers inside the device private data (e.g. adata->sensor_settings, press_data->sensor_settings). There are no dynamic allocations, reallocations, or post-allocation assignments.

### 3. Execution Path & Access Invariants
The only reads of the ch field of st_sensor_settings occur when assigning it to indio_dev->channels during device probing:
- Accelerometer (drivers/iio/accel/st_accel_core.c):

  ```c
  indio_dev->channels = adata->sensor_settings->ch;
  indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  ```
- Gyroscope (`drivers/iio/gyro/st_gyro_core.c`):
  ```c
  indio_dev->channels = gdata->sensor_settings->ch;
  indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  ```
- Magnetometer (`drivers/iio/magnetometer/st_magn_core.c`):
  ```c
  indio_dev->channels = mdata->sensor_settings->ch;
  indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  ```
- Pressure (`drivers/iio/pressure/st_pressure_core.c`):
  ```c
  indio_dev->channels = press_data->sensor_settings->ch;
  indio_dev->num_channels = press_data->sensor_settings->num_ch;
  ```

Because sensor_settings->ch is never dereferenced or accessed as an array within the drivers, adding the __counted_by_ptr annotation does not cause any runtime false-positives or panics under KASAN or UBSAN, even though num_ch defaults to 0 for accelerometer, gyroscope, and magnetometer settings. For pressure sensors, num_ch is correctly initialized to the exact size of the array pointed to by ch using ARRAY_SIZE(...).

### 4. Build and Style Verification
The modified files were successfully compiled and verified:

```bash
make_kernel_build_single_file drivers/iio/common/st_sensors/st_sensors_core.c
make_kernel_build_single_file drivers/iio/accel/st_accel_core.c
make_kernel_build_single_file drivers/iio/gyro/st_gyro_core.c
make_kernel_build_single_file drivers/iio/magnetometer/st_magn_core.c
make_kernel_build_single_file drivers/iio/pressure/st_pressure_core.c
```

The patch was also validated with checkpatch_run to ensure standard coding style compliance.

### 5. Test Exemption Justification
The modified code strictly requires hardware registers or specific driver state to run. Mocking the STMicroelectronics sensor chips (like LSM303D, LIS3DH, LSM330D, LPS331AP, etc.), their respective i2c_client / spi_device interfaces, and Regmap register reads/writes (e.g., WHOAMI .wai_addr checks) in a User Mode Linux (UML) unit test suite is not feasible due to the lack of hardware mocks for these physical ST sensor chips. Hence, a test exemption applies under the specified guidelines.

author NAK
#5 lore raw
Quoted 86 lines · Bill Wendling
Quoted 79 lines · Kees Cook
Quoted 8 lines · Bill Wendling

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.

I wanted to understand this so, in drivers/iio/accel/st_accel_core.c:

static const struct st_sensor_settings st_accel_sensors_settings[] = {
...
.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
...
}

static const struct iio_chan_spec st_accel_12bit_channels[] = {
        ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
                        BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
                        ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
                        ST_ACCEL_DEFAULT_OUT_X_L_ADDR,
                        st_accel_mount_matrix_ext_info),
        ...
        IIO_CHAN_SOFT_TIMESTAMP(3)
};
#define IIO_CHAN_SOFT_TIMESTAMP(_si) (struct iio_chan_spec) {           \
        .type = IIO_TIMESTAMP,                                          \
        .channel = -1,                                                  \
        .scan_index = _si,                                              \
        .scan_type = {                                                  \
                .sign = 's',                                            \
                .realbits = 64,                                         \
                .storagebits = 64,                                      \
        },                                                              \

}

So .ch is assigned an array of items which looks to end with a termination sentinel. And .num_ch is unassigned:

$ git grep '\.ch = ' drivers/iio/accel/st_accel_core.c | wc -l
16
$ git grep '\.num_ch = ' drivers/iio/accel/st_accel_core.c | wc -l
0
Quoted 4 lines

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.

Yeah, checks out:

$ git grep '\.ch = ' | cut -d: -f1 | sort | uniq -c
     16 accel/st_accel_core.c
      4 gyro/st_gyro_core.c
      5 magnetometer/st_magn_core.c
      6 pressure/st_pressure_core.c
$ git grep '\.num_ch = ' | cut -d: -f1 | sort | uniq -c
      6 pressure/st_pressure_core.c

Interestingly, the pressure/st_pressure_core.c uses both assigned .num_ch _and_ a sentinel.

Quoted 4 lines

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.

Heh, so the bounds check gets laundered. :) Even pressure:

pressure/st_pressure_core.c:    press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
pressure/st_pressure_core.c:    indio_dev->num_channels = press_data->sensor_settings->num_ch;
Quoted 3 lines

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.

Quoted 6 lines

I would argue that either __counted_by_ptr has no use here ("accidentally safe because no one dereferences .ch" isn't a great justification) or that the accelerometer, gyroscope, and magnetometer sensors should have their .num_ch assigned correctly so that the "contract" is correct, even if nothing does the deref.

Quoted 4 lines

This is all very weird. Below is the full output from CodeMender. It might give some insight into this, but perhaps we should just NACK this patch?

After further review, I'm going to NACK this patch as having no effect. I think this makes me a real kernel developer now? :-)

NACK: Bill Wendling <[email protected]>

-bw

maintainer
#6 lore raw
Quoted 87 lines · On Wed, 23 Sep 2026 15:06:03 -0700 Bill Wendling <[email protected]> wrote:
Quoted 86 lines · Bill Wendling
Quoted 85 lines · Kees Cook
Quoted 8 lines · Bill Wendling

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.

I wanted to understand this so, in drivers/iio/accel/st_accel_core.c:

static const struct st_sensor_settings st_accel_sensors_settings[] = {
...
.ch = (struct iio_chan_spec *)st_accel_12bit_channels,
...
}

static const struct iio_chan_spec st_accel_12bit_channels[] = {
        ST_SENSORS_LSM_CHANNELS_EXT(IIO_ACCEL,
                        BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
                        ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16,
                        ST_ACCEL_DEFAULT_OUT_X_L_ADDR,
                        st_accel_mount_matrix_ext_info),
        ...
        IIO_CHAN_SOFT_TIMESTAMP(3)
};
#define IIO_CHAN_SOFT_TIMESTAMP(_si) (struct iio_chan_spec) {           \
        .type = IIO_TIMESTAMP,                                          \
        .channel = -1,                                                  \
        .scan_index = _si,                                              \
        .scan_type = {                                                  \
                .sign = 's',                                            \
                .realbits = 64,                                         \
                .storagebits = 64,                                      \
        },                                                              \

}

So .ch is assigned an array of items which looks to end with a termination sentinel. And .num_ch is unassigned:

$ git grep '\.ch = ' drivers/iio/accel/st_accel_core.c | wc -l
16
$ git grep '\.num_ch = ' drivers/iio/accel/st_accel_core.c | wc -l
0
Quoted 4 lines

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.

Yeah, checks out:

$ git grep '\.ch = ' | cut -d: -f1 | sort | uniq -c
     16 accel/st_accel_core.c
      4 gyro/st_gyro_core.c
      5 magnetometer/st_magn_core.c
      6 pressure/st_pressure_core.c
$ git grep '\.num_ch = ' | cut -d: -f1 | sort | uniq -c
      6 pressure/st_pressure_core.c

Interestingly, the pressure/st_pressure_core.c uses both assigned .num_ch _and_ a sentinel.

Quoted 4 lines

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.

Heh, so the bounds check gets laundered. :) Even pressure:

pressure/st_pressure_core.c:    press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
pressure/st_pressure_core.c:    indio_dev->num_channels = press_data->sensor_settings->num_ch;
Quoted 3 lines

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.

I would argue that either __counted_by_ptr has no use here ("accidentally safe because no one dereferences .ch" isn't a great justification) or that the accelerometer, gyroscope, and magnetometer sensors should have their .num_ch assigned correctly so that the "contract" is correct, even if nothing does the deref.

Quoted 8 lines
Quoted 4 lines

This is all very weird. Below is the full output from CodeMender. It might give some insight into this, but perhaps we should just NACK this patch?

After further review, I'm going to NACK this patch as having no effect. I think this makes me a real kernel developer now? :-)

NACK: Bill Wendling <[email protected]>

:)
Patchwork doesn't have a button for Nacked by the submitter!

Rejected it is I guess!

Jonathan

Quoted 2 lines

-bw