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

netfilter: ebtables: fix fortify warnings in size_entry_mwt()

When compiling with gcc 13 and CONFIG_FORTIFY_SOURCE=y, the following
warning appears:

In function ‘fortify_memcpy_chk’,
inlined from ‘size_entry_mwt’ at net/bridge/netfilter/ebtables.c:2118:2:
./include/linux/fortify-string.h:592:25: error: call to ‘__read_overflow2_field’
declared with attribute warning: detected read beyond size of field (2nd parameter);
maybe use struct_group()? [-Werror=attribute-warning]
592 | __read_overflow2_field(q_size_field, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The compiler is complaining:

memcpy(&offsets[1], &entry->watchers_offset,
sizeof(offsets) - sizeof(offsets[0]));

where memcpy reads beyong &entry->watchers_offset to copy
{watchers,target,next}_offset altogether into offsets[]. Silence the
warning by wrapping these three up via struct_group().

Signed-off-by: GONG, Ruiqi <gongruiqi1@huawei.com>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Florian Westphal <fw@strlen.de>

authored by

GONG, Ruiqi and committed by
Florian Westphal
a7ed3465 43c28172

+9 -8
+8 -6
include/uapi/linux/netfilter_bridge/ebtables.h
··· 182 182 unsigned char sourcemsk[ETH_ALEN]; 183 183 unsigned char destmac[ETH_ALEN]; 184 184 unsigned char destmsk[ETH_ALEN]; 185 - /* sizeof ebt_entry + matches */ 186 - unsigned int watchers_offset; 187 - /* sizeof ebt_entry + matches + watchers */ 188 - unsigned int target_offset; 189 - /* sizeof ebt_entry + matches + watchers + target */ 190 - unsigned int next_offset; 185 + __struct_group(/* no tag */, offsets, /* no attrs */, 186 + /* sizeof ebt_entry + matches */ 187 + unsigned int watchers_offset; 188 + /* sizeof ebt_entry + matches + watchers */ 189 + unsigned int target_offset; 190 + /* sizeof ebt_entry + matches + watchers + target */ 191 + unsigned int next_offset; 192 + ); 191 193 unsigned char elems[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); 192 194 }; 193 195
+1 -2
net/bridge/netfilter/ebtables.c
··· 2115 2115 return ret; 2116 2116 2117 2117 offsets[0] = sizeof(struct ebt_entry); /* matches come first */ 2118 - memcpy(&offsets[1], &entry->watchers_offset, 2119 - sizeof(offsets) - sizeof(offsets[0])); 2118 + memcpy(&offsets[1], &entry->offsets, sizeof(entry->offsets)); 2120 2119 2121 2120 if (state->buf_kern_start) { 2122 2121 buf_start = state->buf_kern_start + state->buf_kern_offset;