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

wireless: wext-spy: Fix out-of-bounds warning

Fix the following out-of-bounds warning:

net/wireless/wext-spy.c:178:2: warning: 'memcpy' offset [25, 28] from the object at 'threshold' is out of the bounds of referenced subobject 'low' with type 'struct iw_quality' at offset 20 [-Warray-bounds]

The problem is that the original code is trying to copy data into a
couple of struct members adjacent to each other in a single call to
memcpy(). This causes a legitimate compiler warning because memcpy()
overruns the length of &threshold.low and &spydata->spy_thr_low. As
these are just a couple of struct members, fix this by using direct
assignments, instead of memcpy().

This helps with the ongoing efforts to globally enable -Warray-bounds
and get us closer to being able to tighten the FORTIFY_SOURCE routines
on memcpy().

Link: https://github.com/KSPP/linux/issues/109
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20210422200032.GA168995@embeddedor
Signed-off-by: Johannes Berg <johannes.berg@intel.com>

authored by

Gustavo A. R. Silva and committed by
Johannes Berg
e93bdd78 38f75922

+7 -7
+7 -7
net/wireless/wext-spy.c
··· 120 120 return -EOPNOTSUPP; 121 121 122 122 /* Just do it */ 123 - memcpy(&(spydata->spy_thr_low), &(threshold->low), 124 - 2 * sizeof(struct iw_quality)); 123 + spydata->spy_thr_low = threshold->low; 124 + spydata->spy_thr_high = threshold->high; 125 125 126 126 /* Clear flag */ 127 127 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under)); ··· 147 147 return -EOPNOTSUPP; 148 148 149 149 /* Just do it */ 150 - memcpy(&(threshold->low), &(spydata->spy_thr_low), 151 - 2 * sizeof(struct iw_quality)); 150 + threshold->low = spydata->spy_thr_low; 151 + threshold->high = spydata->spy_thr_high; 152 152 153 153 return 0; 154 154 } ··· 173 173 memcpy(threshold.addr.sa_data, address, ETH_ALEN); 174 174 threshold.addr.sa_family = ARPHRD_ETHER; 175 175 /* Copy stats */ 176 - memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality)); 176 + threshold.qual = *wstats; 177 177 /* Copy also thresholds */ 178 - memcpy(&(threshold.low), &(spydata->spy_thr_low), 179 - 2 * sizeof(struct iw_quality)); 178 + threshold.low = spydata->spy_thr_low; 179 + threshold.high = spydata->spy_thr_high; 180 180 181 181 /* Send event to user space */ 182 182 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);