Re: [PATCH] hwmon: (pc87360) Bounds check data->innr usage

From: Guenter Roeck
Date: Thu Nov 30 2023 - 15:31:44 EST


On 11/30/23 12:11, Gustavo A. R. Silva wrote:


On 11/30/23 14:02, Kees Cook wrote:
Without visibility into the initializers for data->innr, GCC suspects
using it as an index could walk off the end of the various 14-element
arrays in data. Perform an explicit clamp to the array size. Silences
the following warning with GCC 12+:

../drivers/hwmon/pc87360.c: In function 'pc87360_update_device':
../drivers/hwmon/pc87360.c:341:49: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
   341 |                                 data->in_max[i] = pc87360_read_value(data,
       |                                 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
   342 |                                                   LD_IN, i,
       |                                                   ~~~~~~~~~
   343 |                                                   PC87365_REG_IN_MAX);
       |                                                   ~~~~~~~~~~~~~~~~~~~
../drivers/hwmon/pc87360.c:209:12: note: at offset 255 into destination object 'in_max' of size 14
   209 |         u8 in_max[14];          /* Register value */
       |            ^~~~~~

Cc: Jim Cromie <jim.cromie@xxxxxxxxx>
Cc: Jean Delvare <jdelvare@xxxxxxxx>
Cc: Guenter Roeck <linux@xxxxxxxxxxxx>
Cc: linux-hwmon@xxxxxxxxxxxxxxx
Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>

Looks good to me.

Reviewed-by: Gustavo A. R. Silva <gustavoars@xxxxxxxxxx>


Guess I'll apply it, even though it is quite pointless. But arguing against
such changes seems like shouting into the wind, so whatever.

There are several other similar "unchecked" loops, including loops for
fannr and tempnr. The driver would misbehave badly if any of those would
ever be outside the valid range, both when accessing the hardware and
writing into various arrays.

Guenter