* [iptables PATCH] nft: Fix for broken address mask match detection
@ 2020-09-28 17:05 Phil Sutter
2020-09-30 9:58 ` Pablo Neira Ayuso
0 siblings, 1 reply; 2+ messages in thread
From: Phil Sutter @ 2020-09-28 17:05 UTC (permalink / raw)
To: Pablo Neira Ayuso; +Cc: Florian Westphal, Eric Garver, netfilter-devel
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 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/iptables/nft-shared.c b/iptables/nft-shared.c
index c5a8f3fcc051d..7741d23befc5a 100644
--- a/iptables/nft-shared.c
+++ b/iptables/nft-shared.c
@@ -165,7 +165,7 @@ void add_outiface(struct nftnl_rule *r, char *iface, uint32_t op)
void add_addr(struct nftnl_rule *r, int offset,
void *data, void *mask, size_t len, uint32_t op)
{
- const char *m = mask;
+ const unsigned char *m = mask;
int i;
add_payload(r, offset, len, NFT_PAYLOAD_NETWORK_HEADER);
--
2.28.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [iptables PATCH] nft: Fix for broken address mask match detection
2020-09-28 17:05 [iptables PATCH] nft: Fix for broken address mask match detection Phil Sutter
@ 2020-09-30 9:58 ` Pablo Neira Ayuso
0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2020-09-30 9:58 UTC (permalink / raw)
To: Phil Sutter; +Cc: Florian Westphal, Eric Garver, netfilter-devel
On Mon, Sep 28, 2020 at 07:05:47PM +0200, Phil Sutter wrote:
> 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.
LGTM.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-09-30 9:58 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-28 17:05 [iptables PATCH] nft: Fix for broken address mask match detection Phil Sutter
2020-09-30 9:58 ` Pablo Neira Ayuso
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).