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

netfilter: nf_tables: fix destination register zeroing

Following bug was reported via irc:
nft list ruleset
set knock_candidates_ipv4 {
type ipv4_addr . inet_service
size 65535
elements = { 127.0.0.1 . 123,
127.0.0.1 . 123 }
}
..
udp dport 123 add @knock_candidates_ipv4 { ip saddr . 123 }
udp dport 123 add @knock_candidates_ipv4 { ip saddr . udp dport }

It should not have been possible to add a duplicate set entry.

After some debugging it turned out that the problem is the immediate
value (123) in the second-to-last rule.

Concatenations use 32bit registers, i.e. the elements are 8 bytes each,
not 6 and it turns out the kernel inserted

inet firewall @knock_candidates_ipv4
element 0100007f ffff7b00 : 0 [end]
element 0100007f 00007b00 : 0 [end]

Note the non-zero upper bits of the first element. It turns out that
nft_immediate doesn't zero the destination register, but this is needed
when the length isn't a multiple of 4.

Furthermore, the zeroing in nft_payload is broken. We can't use
[len / 4] = 0 -- if len is a multiple of 4, index is off by one.

Skip zeroing in this case and use a conditional instead of (len -1) / 4.

Fixes: 49499c3e6e18 ("netfilter: nf_tables: switch registers to 32 bit addressing")
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

authored by

Florian Westphal and committed by
Pablo Neira Ayuso
1e105e6a da9125df

+5 -1
+2
include/net/netfilter/nf_tables.h
··· 143 143 static inline void nft_data_copy(u32 *dst, const struct nft_data *src, 144 144 unsigned int len) 145 145 { 146 + if (len % NFT_REG32_SIZE) 147 + dst[len / NFT_REG32_SIZE] = 0; 146 148 memcpy(dst, src, len); 147 149 } 148 150
+3 -1
net/netfilter/nft_payload.c
··· 87 87 u32 *dest = &regs->data[priv->dreg]; 88 88 int offset; 89 89 90 - dest[priv->len / NFT_REG32_SIZE] = 0; 90 + if (priv->len % NFT_REG32_SIZE) 91 + dest[priv->len / NFT_REG32_SIZE] = 0; 92 + 91 93 switch (priv->base) { 92 94 case NFT_PAYLOAD_LL_HEADER: 93 95 if (!skb_mac_header_was_set(skb))