]> git.ipfire.org Git - thirdparty/iptables.git/commit
nft: Fix for broken address mask match detection
authorPhil Sutter <phil@nwl.cc>
Mon, 28 Sep 2020 16:57:18 +0000 (18:57 +0200)
committerPhil Sutter <phil@nwl.cc>
Wed, 30 Sep 2020 10:47:27 +0000 (12:47 +0200)
commit72ed608bf1ea550ac13b5b880afc7ad3ffa0afd0
tree056709e879cc5fbbd06483a2ea044cd3a6e78bbb
parentf75750ff9d5c49808812c41081cd2a3700878941
nft: Fix for broken address mask match detection

Trying to decide whether a bitwise expression is needed to match parts
of a source or destination address only, add_addr() checks if all bytes
in 'mask' are 0xff or not. The check is apparently broken though as each
byte in 'mask' is cast to a signed char before comparing against 0xff,
therefore the bitwise is always added:

| # ./bad/iptables-nft -A foo -s 10.0.0.1 -j ACCEPT
| # ./good/iptables-nft -A foo -s 10.0.0.2 -j ACCEPT
| # nft --debug=netlink list chain ip filter foo
| ip filter foo 5
|   [ payload load 4b @ network header + 12 => reg 1 ]
|   [ bitwise reg 1 = (reg=1 & 0xffffffff ) ^ 0x00000000 ]
|   [ cmp eq reg 1 0x0100000a ]
|   [ counter pkts 0 bytes 0 ]
|   [ immediate reg 0 accept ]
|
| ip filter foo 6 5
|   [ payload load 4b @ network header + 12 => reg 1 ]
|   [ cmp eq reg 1 0x0200000a ]
|   [ counter pkts 0 bytes 0 ]
|   [ immediate reg 0 accept ]
|
| table ip filter {
|  chain foo {
|  ip saddr 10.0.0.1 counter packets 0 bytes 0 accept
|  ip saddr 10.0.0.2 counter packets 0 bytes 0 accept
|  }
| }

Fix the cast, safe an extra op and gain 100% performance in ideal cases.

Fixes: 56859380eb328 ("xtables-compat: avoid unneeded bitwise ops")
Signed-off-by: Phil Sutter <phil@nwl.cc>
iptables/nft-shared.c