Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (aspeed-pwm-tacho) Fix -Wstringop-overflow warning in aspeed_create_fan_tach_channel()

Based on the documentation below, the maximum number of Fan tach
channels is 16:

Documentation/devicetree/bindings/hwmon/aspeed-pwm-tacho.txt:45:
45 - aspeed,fan-tach-ch : should specify the Fan tach input channel.
46 integer value in the range 0 through 15, with 0 indicating
47 Fan tach channel 0 and 15 indicating Fan tach channel 15.
48 At least one Fan tach input channel is required.

However, the compiler doesn't know that, and legitimaly warns about a potential
overwrite in array `u8 fan_tach_ch_source[16]` in `struct aspeed_pwm_tacho_data`,
in case `index` takes a value outside the boundaries of the array:

drivers/hwmon/aspeed-pwm-tacho.c:
179 struct aspeed_pwm_tacho_data {
...
184 bool fan_tach_present[16];
...
193 u8 fan_tach_ch_source[16];
196 };

In function ‘aspeed_create_fan_tach_channel’,
inlined from ‘aspeed_create_fan’ at drivers/hwmon/aspeed-pwm-tacho.c:877:2,
inlined from ‘aspeed_pwm_tacho_probe’ at drivers/hwmon/aspeed-pwm-tacho.c:936:9:
drivers/hwmon/aspeed-pwm-tacho.c:751:49: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
751 | priv->fan_tach_ch_source[index] = pwm_source;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
drivers/hwmon/aspeed-pwm-tacho.c: In function ‘aspeed_pwm_tacho_probe’:
drivers/hwmon/aspeed-pwm-tacho.c:193:12: note: at offset [48, 255] into destination object ‘fan_tach_ch_source’ of size 16
193 | u8 fan_tach_ch_source[16];
| ^~~~~~~~~~~~~~~~~~

Fix this by sanity checking `index` before using it to index arrays of
size 16 elements in `struct aspeed_pwm_tacho_data`. Also, pass `dev` as
argument to function `aspeed_create_fan_tach_channel()`, and add an error
message in case `index` is out-of-bounds, in which case return `-EINVAL`.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/ZVPQJIP26dIzRAr6@work
[groeck: Fixed continuation line alignment]
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Gustavo A. R. Silva and committed by
Guenter Roeck
b0d51ada 10bd80e0

+19 -7
+19 -7
drivers/hwmon/aspeed-pwm-tacho.c
··· 166 166 167 167 #define MAX_CDEV_NAME_LEN 16 168 168 169 + #define MAX_ASPEED_FAN_TACH_CHANNELS 16 170 + 169 171 struct aspeed_cooling_device { 170 172 char name[16]; 171 173 struct aspeed_pwm_tacho_data *priv; ··· 183 181 struct reset_control *rst; 184 182 unsigned long clk_freq; 185 183 bool pwm_present[8]; 186 - bool fan_tach_present[16]; 184 + bool fan_tach_present[MAX_ASPEED_FAN_TACH_CHANNELS]; 187 185 u8 type_pwm_clock_unit[3]; 188 186 u8 type_pwm_clock_division_h[3]; 189 187 u8 type_pwm_clock_division_l[3]; ··· 192 190 u16 type_fan_tach_unit[3]; 193 191 u8 pwm_port_type[8]; 194 192 u8 pwm_port_fan_ctrl[8]; 195 - u8 fan_tach_ch_source[16]; 193 + u8 fan_tach_ch_source[MAX_ASPEED_FAN_TACH_CHANNELS]; 196 194 struct aspeed_cooling_device *cdev[8]; 197 195 const struct attribute_group *groups[3]; 198 196 }; ··· 739 737 aspeed_set_pwm_port_fan_ctrl(priv, pwm_port, INIT_FAN_CTRL); 740 738 } 741 739 742 - static void aspeed_create_fan_tach_channel(struct aspeed_pwm_tacho_data *priv, 743 - u8 *fan_tach_ch, 744 - int count, 745 - u8 pwm_source) 740 + static int aspeed_create_fan_tach_channel(struct device *dev, 741 + struct aspeed_pwm_tacho_data *priv, 742 + u8 *fan_tach_ch, 743 + int count, 744 + u8 pwm_source) 746 745 { 747 746 u8 val, index; 748 747 749 748 for (val = 0; val < count; val++) { 750 749 index = fan_tach_ch[val]; 750 + if (index >= MAX_ASPEED_FAN_TACH_CHANNELS) { 751 + dev_err(dev, "Invalid Fan Tach input channel %u\n.", index); 752 + return -EINVAL; 753 + } 751 754 aspeed_set_fan_tach_ch_enable(priv->regmap, index, true); 752 755 priv->fan_tach_present[index] = true; 753 756 priv->fan_tach_ch_source[index] = pwm_source; 754 757 aspeed_set_fan_tach_ch_source(priv->regmap, index, pwm_source); 755 758 } 759 + 760 + return 0; 756 761 } 757 762 758 763 static int ··· 883 874 fan_tach_ch, count); 884 875 if (ret) 885 876 return ret; 886 - aspeed_create_fan_tach_channel(priv, fan_tach_ch, count, pwm_port); 877 + 878 + ret = aspeed_create_fan_tach_channel(dev, priv, fan_tach_ch, count, pwm_port); 879 + if (ret) 880 + return ret; 887 881 888 882 return 0; 889 883 }