netfilter: nft_set_hash: disable fast_ops for 2-len keys
authorAnatole Denis <anatole@rezel.net>
Wed, 4 Oct 2017 00:17:14 +0000 (01:17 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 25 Oct 2017 12:46:44 +0000 (14:46 +0200)
commit0414c78f14861cb704d6e6888efd53dd36e3bdde
tree22bc6a7959cf72a8e022bda9653262c5a2a97b6a
parent28e33f9d78eefe98ea86673ab31e988b37a9a738
netfilter: nft_set_hash: disable fast_ops for 2-len keys

jhash_1word of a u16 is a different value from jhash of the same u16 with
length 2.
Since elements are always inserted in sets using jhash over the actual
klen, this would lead to incorrect lookups on fixed-size sets with a key
length of 2, as they would be inserted with hash value jhash(key, 2) and
looked up with hash value jhash_1word(key), which is different.

Example reproducer(v4.13+), using anonymous sets which always have a
fixed size:

  table inet t {
      chain c {
                  type filter hook output priority 0; policy accept;
                  tcp dport { 10001, 10003, 10005, 10007, 10009 } counter packets 4 bytes 240 reject
                  tcp dport 10001 counter packets 4 bytes 240 reject
                  tcp dport 10003 counter packets 4 bytes 240 reject
                  tcp dport 10005 counter packets 4 bytes 240 reject
                  tcp dport 10007 counter packets 0 bytes 0 reject
                  tcp dport 10009 counter packets 4 bytes 240 reject
          }
  }

then use nc -z localhost <port> to probe; incorrectly hashed ports will
pass through the set lookup and increment the counter of an individual
rule.

jhash being seeded with a random value, it is not deterministic which
ports will incorrectly hash, but in testing with 5 ports in the set I
always had 4 or 5 with an incorrect hash value.

Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
net/netfilter/nft_set_hash.c