hwmon: (pc87360) Bounds check data->innr usage
authorKees Cook <keescook@chromium.org>
Thu, 30 Nov 2023 20:02:07 +0000 (12:02 -0800)
committerGuenter Roeck <linux@roeck-us.net>
Mon, 11 Dec 2023 14:21:01 +0000 (06:21 -0800)
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@gmail.com>
Cc: Jean Delvare <jdelvare@suse.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: linux-hwmon@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20231130200207.work.679-kees@kernel.org
[groeck: Added comment into code clarifying context]
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/pc87360.c

index 926ea1fe133c615cab4b99cd0f428178d5373089..9e9681b2e8c55ee85198acb85c9759b774beaa10 100644 (file)
@@ -323,7 +323,11 @@ static struct pc87360_data *pc87360_update_device(struct device *dev)
                }
 
                /* Voltages */
-               for (i = 0; i < data->innr; i++) {
+               /*
+                * The min() below does not have any practical meaning and is
+                * only needed to silence a warning observed with gcc 12+.
+                */
+               for (i = 0; i < min(data->innr, ARRAY_SIZE(data->in)); i++) {
                        data->in_status[i] = pc87360_read_value(data, LD_IN, i,
                                             PC87365_REG_IN_STATUS);
                        /* Clear bits */