]> git.ipfire.org Git - thirdparty/ipset.git/log
thirdparty/ipset.git
2 years agonetfilter: ipset: fix hash:net,port,net hang with /0 subnet
Jozsef Kadlecsik [Thu, 29 Dec 2022 14:00:21 +0000 (15:00 +0100)] 
netfilter: ipset: fix hash:net,port,net hang with /0 subnet

The hash:net,port,net set type supports /0 subnets. However, the patch
commit 5f7b51bf09baca8e titled "netfilter: ipset: Limit the maximal range
of consecutive elements to add/delete" did not take into account it and
resulted in an endless loop. The bug is actually older but the patch
5f7b51bf09baca8e brings it out earlier.

Handle /0 subnets properly in hash:net,port,net set types.

Reported-by: Марк Коренберг <socketpair@gmail.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agoMakefile: Create LZMA-compressed dist-files
Phil Sutter [Thu, 8 Dec 2022 00:16:05 +0000 (01:16 +0100)] 
Makefile: Create LZMA-compressed dist-files

Use a more modern alternative to gzip.

Suggested-by: Jan Engelhardt <jengelh@inai.de>
Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agoipset 7.16 released v7.16
Jozsef Kadlecsik [Mon, 21 Nov 2022 12:39:47 +0000 (13:39 +0100)] 
ipset 7.16 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agoAdd new ipset_parse_bitmask() function to the library interface.
Jozsef Kadlecsik [Mon, 21 Nov 2022 12:37:38 +0000 (13:37 +0100)] 
Add new ipset_parse_bitmask() function to the library interface.

2 years agotest: Make sure no more than 64 clashing elements can be added to hash:net,iface...
Jozsef Kadlecsik [Mon, 21 Nov 2022 12:20:05 +0000 (13:20 +0100)] 
test: Make sure no more than 64 clashing elements can be added to hash:net,iface sets

2 years agonetfilter: ipset: restore allowing 64 clashing elements in hash:net,iface
Jozsef Kadlecsik [Mon, 21 Nov 2022 12:16:56 +0000 (13:16 +0100)] 
netfilter: ipset: restore allowing 64 clashing elements in hash:net,iface

The patch "netfilter: ipset: enforce documented limit to prevent allocating
huge memory" was too strict and prevented to add up to 64 clashing elements
to a hash:net,iface type of set. This patch fixes the issue and now the type
behaves as documented.

2 years agoFix all debug mode warnings
Jozsef Kadlecsik [Sun, 20 Nov 2022 21:43:59 +0000 (22:43 +0100)] 
Fix all debug mode warnings

2 years agonetfilter: ipset: add tests for the new bitmask feature
Vishwanath Pai [Thu, 10 Nov 2022 21:31:31 +0000 (16:31 -0500)] 
netfilter: ipset: add tests for the new bitmask feature

The hash:ip type had a test for netmask, add a similar test for bitmask
feature as well, and add another test where bitmask is not a valid
netmask.

Repeat the same three tests for hash:ip,port and hash:net,net.

Add a test to make sure bitmask and netmask options cannot be added at the
same time.

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: ipset: Update the man page to include netmask/bitmask options
Vishwanath Pai [Thu, 10 Nov 2022 21:31:30 +0000 (16:31 -0500)] 
netfilter: ipset: Update the man page to include netmask/bitmask options

We added bitmask support to hash:ip and added both netmask and bitmask
to hash:net,net and hash:ip,port

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: ipset: Add bitmask support to hash:netnet
Vishwanath Pai [Thu, 10 Nov 2022 21:31:29 +0000 (16:31 -0500)] 
netfilter: ipset: Add bitmask support to hash:netnet

Create a new revision of hash:netnet and add support for bitmask
parameter. The set did not support netmask so we'll add both netmask and
bitmask.

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Joshua Hunt <johunt@akamai.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: ipset: Add bitmask support to hash:ipport
Vishwanath Pai [Thu, 10 Nov 2022 21:31:28 +0000 (16:31 -0500)] 
netfilter: ipset: Add bitmask support to hash:ipport

Create a new revision of hash:ipport and add support for bitmask
parameter. The set did not support netmask so we'll add both netmask and
bitmask.

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Joshua Hunt <johunt@akamai.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: ipset: Add bitmask support to hash:ip
Vishwanath Pai [Thu, 10 Nov 2022 21:31:27 +0000 (16:31 -0500)] 
netfilter: ipset: Add bitmask support to hash:ip

Create a new revision of hash:ip and add support for bitmask parameter.
The set already had support for netmask so only add bitmask here.

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Joshua Hunt <johunt@akamai.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: ipset: Add support for new bitmask parameter
Vishwanath Pai [Thu, 10 Nov 2022 21:31:26 +0000 (16:31 -0500)] 
netfilter: ipset: Add support for new bitmask parameter

Add a new parameter to complement the existing 'netmask' option. The
main difference between netmask and bitmask is that bitmask takes any
arbitrary ip address as input, it does not have to be a valid netmask.

The name of the new parameter is 'bitmask'. This lets us mask out
arbitrary bits in the ip address, for example:
ipset create set1 hash:ip bitmask 255.128.255.0
ipset create set2 hash:ip,port family inet6 bitmask ffff::ff80

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Joshua Hunt <johunt@akamai.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: ipset: Add support for new bitmask parameter
Vishwanath Pai [Thu, 10 Nov 2022 21:30:26 +0000 (16:30 -0500)] 
netfilter: ipset: Add support for new bitmask parameter

Add a new parameter to complement the existing 'netmask' option. The
main difference between netmask and bitmask is that bitmask takes any
arbitrary ip address as input, it does not have to be a valid netmask.

The name of the new parameter is 'bitmask'. This lets us mask out
arbitrary bits in the ip address, for example:
ipset create set1 hash:ip bitmask 255.128.255.0
ipset create set2 hash:ip,port family inet6 bitmask ffff::ff80

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Joshua Hunt <johunt@akamai.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: ipset: regression in ip_set_hash_ip.c
Vishwanath Pai [Wed, 28 Sep 2022 18:26:50 +0000 (14:26 -0400)] 
netfilter: ipset: regression in ip_set_hash_ip.c

This patch introduced a regression: commit 48596a8ddc46 ("netfilter:
ipset: Fix adding an IPv4 range containing more than 2^31 addresses")

The variable e.ip is passed to adtfn() function which finally adds the
ip address to the set. The patch above refactored the for loop and moved
e.ip = htonl(ip) to the end of the for loop.

What this means is that if the value of "ip" changes between the first
assignement of e.ip and the forloop, then e.ip is pointing to a
different ip address than "ip".

Test case:
$ ipset create jdtest_tmp hash:ip family inet hashsize 2048 maxelem 100000
$ ipset add jdtest_tmp 10.0.1.1/31
ipset v6.21.1: Element cannot be added to the set: it's already added

The value of ip gets updated inside the  "else if (tb[IPSET_ATTR_CIDR])"
block but e.ip is still pointing to the old value.

Reviewed-by: Joshua Hunt <johunt@akamai.com>
Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: move from strlcpy with unused retval to strscpy
Wolfram Sang [Mon, 7 Nov 2022 21:09:04 +0000 (22:09 +0100)] 
netfilter: move from strlcpy with unused retval to strscpy

Follow the advice of the below link and prefer 'strscpy' in this
subsystem. Conversion is 1:1 because the return value is not used.
Generated by a coccinelle script.

Link: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw@mail.gmail.com/
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Reviewed-by: Simon Horman <horms@verge.net.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
2 years agocompatibility: handle unsafe_memcpy()
Jozsef Kadlecsik [Mon, 7 Nov 2022 21:03:36 +0000 (22:03 +0100)] 
compatibility: handle unsafe_memcpy()

2 years agonetlink: Bounds-check struct nlmsgerr creation
Kees Cook [Mon, 7 Nov 2022 20:58:52 +0000 (21:58 +0100)] 
netlink: Bounds-check struct nlmsgerr creation

In preparation for FORTIFY_SOURCE doing bounds-check on memcpy(),
switch from __nlmsg_put to nlmsg_put(), and explain the bounds check
for dealing with the memcpy() across a composite flexible array struct.
Avoids this future run-time warning:

  memcpy: detected field-spanning write (size 32) of single field "&errmsg->msg" at net/netlink/af_netlink.c:2447 (size 16)

Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: Jozsef Kadlecsik <kadlec@netfilter.org>
Cc: Florian Westphal <fw@strlen.de>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: syzbot <syzkaller@googlegroups.com>
Cc: netfilter-devel@vger.kernel.org
Cc: coreteam@netfilter.org
Cc: netdev@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20220901071336.1418572-1-keescook@chromium.org
Signed-off-by: David S. Miller <davem@davemloft.net>
2 years agocompatibility: move to skb_protocol in the code from tc_skb_protocol
Jozsef Kadlecsik [Mon, 7 Nov 2022 20:50:05 +0000 (21:50 +0100)] 
compatibility: move to skb_protocol in the code from tc_skb_protocol

And fix a typo committed by me in em_sched.c too.

2 years agoCompatibility: check kvcalloc, kvfree, kvzalloc in slab.h too
Jozsef Kadlecsik [Mon, 7 Nov 2022 20:47:23 +0000 (21:47 +0100)] 
Compatibility: check kvcalloc, kvfree, kvzalloc in slab.h too

2 years agosched: consistently handle layer3 header accesses in the presence of VLANs
Toke Høiland-Jørgensen [Mon, 7 Nov 2022 20:26:00 +0000 (21:26 +0100)] 
sched: consistently handle layer3 header accesses in the presence of VLANs

There are a couple of places in net/sched/ that check skb->protocol and act
on the value there. However, in the presence of VLAN tags, the value stored
in skb->protocol can be inconsistent based on whether VLAN acceleration is
enabled. The commit quoted in the Fixes tag below fixed the users of
skb->protocol to use a helper that will always see the VLAN ethertype.

However, most of the callers don't actually handle the VLAN ethertype, but
expect to find the IP header type in the protocol field. This means that
things like changing the ECN field, or parsing diffserv values, stops
working if there's a VLAN tag, or if there are multiple nested VLAN
tags (QinQ).

To fix this, change the helper to take an argument that indicates whether
the caller wants to skip the VLAN tags or not. When skipping VLAN tags, we
make sure to skip all of them, so behaviour is consistent even in QinQ
mode.

To make the helper usable from the ECN code, move it to if_vlan.h instead
of pkt_sched.h.

v3:
- Remove empty lines
- Move vlan variable definitions inside loop in skb_protocol()
- Also use skb_protocol() helper in IP{,6}_ECN_decapsulate() and
  bpf_skb_ecn_set_ce()

v2:
- Use eth_type_vlan() helper in skb_protocol()
- Also fix code that reads skb->protocol directly
- Change a couple of 'if/else if' statements to switch constructs to avoid
  calling the helper twice

Reported-by: Ilya Ponetayev <i.ponetaev@ndmsystems.com>
Fixes: d8b9605d2697 ("net: sched: fix skb->protocol use in case of accelerated vlan path")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2 years agotreewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
Thomas Gleixner [Mon, 7 Nov 2022 20:20:10 +0000 (21:20 +0100)] 
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500

em_sched.c was left out, fix it now.

2 years agoheaders: Remove some left-over license text in include/uapi/linux/netfilter/
Christophe JAILLET [Mon, 7 Nov 2022 20:11:28 +0000 (21:11 +0100)] 
headers: Remove some left-over license text in include/uapi/linux/netfilter/

When the SPDX-License-Identifier tag has been added, the corresponding
license text has not been removed.

Remove it now.

Also, in xt_connmark.h, move the copyright text at the top of the file
which is a much more common pattern.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: Florian Westphal <fw@strlen.de>
2 years agonetfilter: ipset: enforce documented limit to prevent allocating huge memory
Jozsef Kadlecsik [Mon, 7 Nov 2022 20:06:50 +0000 (21:06 +0100)] 
netfilter: ipset: enforce documented limit to prevent allocating huge memory

Daniel Xu reported that the hash:net,iface type of the ipset subsystem does
not limit adding the same network with different interfaces to a set, which
can lead to huge memory usage or allocation failure.

The quick reproducer is

$ ipset create ACL.IN.ALL_PERMIT hash:net,iface hashsize 1048576 timeout 0
$ for i in $(seq 0 100); do /sbin/ipset add ACL.IN.ALL_PERMIT 0.0.0.0/0,kaf_$i timeout 0 -exist; done

The backtrace when vmalloc fails:

        [Tue Oct 25 00:13:08 2022] ipset: vmalloc error: size 1073741848, exceeds total pages
        <...>
        [Tue Oct 25 00:13:08 2022] Call Trace:
        [Tue Oct 25 00:13:08 2022]  <TASK>
        [Tue Oct 25 00:13:08 2022]  dump_stack_lvl+0x48/0x60
        [Tue Oct 25 00:13:08 2022]  warn_alloc+0x155/0x180
        [Tue Oct 25 00:13:08 2022]  __vmalloc_node_range+0x72a/0x760
        [Tue Oct 25 00:13:08 2022]  ? hash_netiface4_add+0x7c0/0xb20
        [Tue Oct 25 00:13:08 2022]  ? __kmalloc_large_node+0x4a/0x90
        [Tue Oct 25 00:13:08 2022]  kvmalloc_node+0xa6/0xd0
        [Tue Oct 25 00:13:08 2022]  ? hash_netiface4_resize+0x99/0x710
        <...>

The fix is to enforce the limit documented in the ipset(8) manpage:

>  The internal restriction of the hash:net,iface set type is that the same
>  network prefix cannot be stored with more than 64 different interfaces
>  in a single set.

Reported-by: Daniel Xu <dxu@dxuuu.xyz>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agonetfilter: ipset: Fix oversized kvmalloc() calls
Jozsef Kadlecsik [Mon, 6 Sep 2021 16:03:37 +0000 (18:03 +0200)] 
netfilter: ipset: Fix oversized kvmalloc() calls

commit 7661809d493b426e979f39ab512e3adf41fbcc69
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Wed Jul 14 09:45:49 2021 -0700

    mm: don't allow oversized kvmalloc() calls

limits the max allocatable memory via kvmalloc() to MAX_INT. Apply the
same limit in ipset.

Reported-by: syzbot+3493b1873fb3ea827986@syzkaller.appspotmail.com
Reported-by: syzbot+2b8443c35458a617c904@syzkaller.appspotmail.com
Reported-by: syzbot+ee5cb15f4a0e85e0d54e@syzkaller.appspotmail.com
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
2 years agoipset-translate: allow invoking with a path name
Quentin Armitage [Thu, 11 Aug 2022 16:52:18 +0000 (17:52 +0100)] 
ipset-translate: allow invoking with a path name

Executing /usr/sbin/ipset-translate results in the ipset functionality being run, rather than the ipset-translate functionality.

 # ipset-translate destroy fred
 This command is not supported, use `ipset-translate restore < file'

 # /usr/sbin/ipset-translate destroy fred
 ipset v7.15: The set with the given name does not exist

use basename() to resolve the issue.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1626
Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoFix IPv6 sets nftables translation
Pablo Neira Ayuso [Mon, 28 Feb 2022 18:52:57 +0000 (19:52 +0100)] 
Fix IPv6 sets nftables translation

The parser assumes the set is an IPv4 ipset because IPSET_OPT_FAMILY is
not set.

 # ipset-translate restore < ./ipset-mwan3_set_connected_ipv6.dump
 add table inet global
 add set inet global mwan3_connected_v6 { type ipv6_addr; flags interval; }
 flush set inet global mwan3_connected_v6
 ipset v7.15: Error in line 4: Syntax error: '64' is out of range 0-32

Remove ipset_xlate_type_get(), call ipset_xlate_set_get() instead to
obtain the set type and family.

Reported-by: Florian Eckert <fe@dev.tdt.de>
Fixes: 325af556cd3a ("add ipset to nftables translation infrastructure")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoFix typo in ipset-translate man page
Bernhard M. Wiedemann [Wed, 29 Sep 2021 07:55:43 +0000 (09:55 +0200)] 
Fix typo in ipset-translate man page

originally reported in
https://lists.opensuse.org/archives/list/factory@lists.opensuse.org/thread/ZIXKNQHSSCQ4ZLEGYYKLAXQ4PQ5EYFGZ/
by Larry Len Rainey

Signed-off-by: Bernhard M. Wiedemann <bwiedemann@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoipset 7.15 released v7.15
Jozsef Kadlecsik [Wed, 4 Aug 2021 06:18:14 +0000 (08:18 +0200)] 
ipset 7.15 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
3 years agonetfilter: ipset: Fix maximal range check in hash_ipportnet4_uadt()
Nathan Chancellor [Tue, 3 Aug 2021 19:31:02 +0000 (21:31 +0200)] 
netfilter: ipset: Fix maximal range check in hash_ipportnet4_uadt()

Clang warns:

net/netfilter/ipset/ip_set_hash_ipportnet.c:249:29: warning: variable
'port_to' is uninitialized when used here [-Wuninitialized]
        if (((u64)ip_to - ip + 1)*(port_to - port + 1) > IPSET_MAX_RANGE)
                                   ^~~~~~~
net/netfilter/ipset/ip_set_hash_ipportnet.c:167:45: note: initialize the
variable 'port_to' to silence this warning
        u32 ip = 0, ip_to = 0, p = 0, port, port_to;
                                                   ^
                                                    = 0
net/netfilter/ipset/ip_set_hash_ipportnet.c:249:39: warning: variable
'port' is uninitialized when used here [-Wuninitialized]
        if (((u64)ip_to - ip + 1)*(port_to - port + 1) > IPSET_MAX_RANGE)
                                             ^~~~
net/netfilter/ipset/ip_set_hash_ipportnet.c:167:36: note: initialize the
variable 'port' to silence this warning
        u32 ip = 0, ip_to = 0, p = 0, port, port_to;
                                          ^
                                           = 0
2 warnings generated.

The range check was added before port and port_to are initialized.
Shuffle the check after the initialization so that the check works
properly.

Fixes: 7fb6c63025ff ("netfilter: ipset: Limit the maximal range of consecutive elements to
add/delete")

Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset 7.14 released v7.14
Jozsef Kadlecsik [Wed, 28 Jul 2021 14:23:06 +0000 (16:23 +0200)] 
ipset 7.14 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years ago64bit division isn't allowed on 32bit, replace it with shift
Jozsef Kadlecsik [Wed, 28 Jul 2021 14:06:46 +0000 (16:06 +0200)] 
64bit division isn't allowed on 32bit, replace it with shift

The number of hosts in a netblock must be a power of two,
so use shift instead of division.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoAdd missing function to libipset.map and bump library version
Jozsef Kadlecsik [Wed, 28 Jul 2021 14:02:49 +0000 (16:02 +0200)] 
Add missing function to libipset.map and bump library version

A new function was not added to libipset.map at the previous release,
fix it. Reported by Jan Engelhardt.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset 7.13 released v7.13
Jozsef Kadlecsik [Tue, 27 Jul 2021 10:19:02 +0000 (12:19 +0200)] 
ipset 7.13 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoWhen parsing protocols by number, do not check it in /etc/protocols.
Jozsef Kadlecsik [Tue, 27 Jul 2021 10:16:47 +0000 (12:16 +0200)] 
When parsing protocols by number, do not check it in /etc/protocols.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoAdd missing hunk to patch "Allow specifying protocols by number"
Jozsef Kadlecsik [Fri, 16 Jul 2021 12:53:26 +0000 (14:53 +0200)] 
Add missing hunk to patch "Allow specifying protocols by number"

Actually, this is the part of it which allows specifying protocols
by number :-)

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoLimit the maximal range of consecutive elements to add/delete fix
Jozsef Kadlecsik [Fri, 16 Jul 2021 12:36:45 +0000 (14:36 +0200)] 
Limit the maximal range of consecutive elements to add/delete fix

Avoid possible number overflows when calculating the number of
consecutive elements. Also, compute properly the consecutive
elements in the case of hash:net* types.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset 7.12 released v7.12
Jozsef Kadlecsik [Wed, 14 Jul 2021 11:33:39 +0000 (13:33 +0200)] 
ipset 7.12 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoAllow specifying protocols by number
Haw Loeung [Wed, 14 Jul 2021 11:05:34 +0000 (13:05 +0200)] 
Allow specifying protocols by number

This allows us to optimise and reduce restore time by specifying
protocol numbers, especially for large ipsets.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoLimit the maximal range of consecutive elements to add/delete
Jozsef Kadlecsik [Wed, 14 Jul 2021 10:37:07 +0000 (12:37 +0200)] 
Limit the maximal range of consecutive elements to add/delete

The range size of consecutive elements were not limited. Thus one
could define a huge range which may result soft lockup errors due
to the long execution time. Now the range size is limited to 2^20
entries. Reported by Brad Spengler.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoFix example in ipset.8 manpage discovered by Pablo Neira Ayuso.
Jozsef Kadlecsik [Sun, 27 Jun 2021 19:13:46 +0000 (21:13 +0200)] 
Fix example in ipset.8 manpage discovered by Pablo Neira Ayuso.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agotests: add tests ipset to nftables
Pablo Neira Ayuso [Fri, 25 Jun 2021 20:30:43 +0000 (22:30 +0200)] 
tests: add tests ipset to nftables

This test checks that the translation from ipset to nftables is correct.

term$ cd tests/xlate
term$ ./runtest.sh

in case that the translation is not correct, it shows the diff with expected
translation output.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoadd ipset to nftables translation infrastructure
Pablo Neira Ayuso [Fri, 25 Jun 2021 20:30:42 +0000 (22:30 +0200)] 
add ipset to nftables translation infrastructure

This patch provides the ipset-translate utility which allows you to
translate your existing ipset file to nftables.

The ipset-translate utility is actually a symlink to ipset, which checks
for 'argv[0] == ipset-translate' to exercise the translation path.

You can translate your ipset file through:

ipset-translate restore < sets.ipt

This patch reuses the existing parser and API to represent the sets and
the elements.

There is a new ipset_xlate_set dummy object that allows to store a
created set to fetch the type without interactions with the kernel.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agolib: Detach restore routine from parser
Pablo Neira Ayuso [Fri, 25 Jun 2021 20:30:41 +0000 (22:30 +0200)] 
lib: Detach restore routine from parser

Do not call restore() from ipset_parser(). Instead, ipset_parser()
returns the IPSET_CMD_RESTORE command and the caller invokes restore().

This patch comes in preparation for the ipset to nftables translation
infrastructure.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agolib: split parser from command execution
Pablo Neira Ayuso [Fri, 25 Jun 2021 20:30:40 +0000 (22:30 +0200)] 
lib: split parser from command execution

ipset_parse_argv() parses, builds and send the netlink messages to the
kernel. This patch extracts the parser and wrap it around the new
ipset_parser() function.

This patch comes is preparation for the ipset to nftables translation
infrastructure.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoBackport "netfilter: use nfnetlink_unicast()"
Jozsef Kadlecsik [Sat, 26 Jun 2021 21:06:20 +0000 (23:06 +0200)] 
Backport "netfilter: use nfnetlink_unicast()"

Backport patch "netfilter: use nfnetlink_unicast()" from
Pablo Neira Ayuso <pablo@netfilter.org>.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoBackport "netfilter: nfnetlink: consolidate callback type"
Jozsef Kadlecsik [Sat, 26 Jun 2021 20:21:05 +0000 (22:21 +0200)] 
Backport "netfilter: nfnetlink: consolidate callback type"

Backport patch "netfilter: nfnetlink: consolidate callback type"
from Pablo Neira Ayuso <pablo@netfilter.org>.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoBackport "netfilter: nfnetlink: add struct nfnl_info and pass it to callbacks"
Jozsef Kadlecsik [Thu, 24 Jun 2021 11:24:09 +0000 (13:24 +0200)] 
Backport "netfilter: nfnetlink: add struct nfnl_info and pass it to callbacks"

Backport patch "netfilter: nfnetlink: add struct nfnl_info and pass it
to callbacks" from Pablo Neira Ayuso <pablo@netfilter.org>.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoBackport "netfilter: add helper function to set up the nfnetlink header and use it"
Jozsef Kadlecsik [Sat, 26 Jun 2021 20:17:58 +0000 (22:17 +0200)] 
Backport "netfilter: add helper function to set up the nfnetlink header and use it"

Backport patch "netfilter: add helper function to set up the nfnetlink
header and use it" from Pablo Neira Ayuso <pablo@netfilter.org>.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoFix patch "Parse port before trying by service name"
Jozsef Kadlecsik [Sat, 26 Jun 2021 20:14:38 +0000 (22:14 +0200)] 
Fix patch "Parse port before trying by service name"

The patch broke parsing service names: number parsing failures
are hard errors which erase data, thus making impossible to
parse input as a string. Fix it by enabling soft (warning)
failures in the case of port number parsing.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset 7.11 released v7.11
Jozsef Kadlecsik [Fri, 19 Feb 2021 20:30:58 +0000 (21:30 +0100)] 
ipset 7.11 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoParse port before trying by service name
Haw Loeung [Wed, 17 Feb 2021 09:15:07 +0000 (10:15 +0100)] 
Parse port before trying by service name

This improves performance for loading IP sets using port numbers,
especially large ones.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoSilence unused-but-set-variable warnings
Jozsef Kadlecsik [Wed, 17 Feb 2021 09:07:27 +0000 (10:07 +0100)] 
Silence unused-but-set-variable warnings

When ipset is compiled in non-debug mode, in some environments warnings
treated as errors emitted:

  session.c: In function 'build_msg':
  session.c:1985:28: warning: variable 'type' set but not used
[-Wunused-but-set-variable]
     const struct ipset_type *type;
                            ^
  session.c:2030:28: warning: variable 'type' set but not used
[-Wunused-but-set-variable]
     const struct ipset_type *type;
                            ^
Fix it by hiding the unused variable definitions/settings in non-debug mode.

Reported by Serhey Popovych.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoHandle -Werror=implicit-fallthrough= in debug mode compiling
Jozsef Kadlecsik [Tue, 9 Feb 2021 18:41:23 +0000 (19:41 +0100)] 
Handle -Werror=implicit-fallthrough= in debug mode compiling

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset: fix print format warning
Neutron Soutmun [Mon, 18 Jan 2021 04:58:30 +0000 (11:58 +0700)] 
ipset: fix print format warning

* Use PRIx64 for portablility over various architectures.
* The format string for the 64bit number printing is incorrect,
  the `%` sign is missing.
* The force types casting over the uint32_t and uint64_t are unnecessary
  which warned by the compiler on different architecture.

Signed-off-by: Neutron Soutmun <neo.neutron@gmail.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoUpdated utilities
Jozsef Kadlecsik [Tue, 19 Jan 2021 07:53:40 +0000 (08:53 +0100)] 
Updated utilities

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoArgument parsing buffer overflow in ipset_parse_argv fixed
Jozsef Kadlecsik [Tue, 19 Jan 2021 07:39:50 +0000 (08:39 +0100)] 
Argument parsing buffer overflow in ipset_parse_argv fixed

Argument length checking was simply missing. Fixes netfilter
bugzilla #1492, reported by Marshall Whittaker.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset 7.10 released v7.10
Jozsef Kadlecsik [Sun, 20 Dec 2020 14:44:11 +0000 (15:44 +0100)] 
ipset 7.10 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoFix patch "Handle false warning from -Wstringop-overflow"
Jozsef Kadlecsik [Sun, 20 Dec 2020 14:07:21 +0000 (15:07 +0100)] 
Fix patch "Handle false warning from -Wstringop-overflow"

Return code of strscpy() was not handled properly.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoBackward compatibility: handle renaming nla_strlcpy to nla_strscpy
Jozsef Kadlecsik [Sun, 20 Dec 2020 13:11:06 +0000 (14:11 +0100)] 
Backward compatibility: handle renaming nla_strlcpy to nla_strscpy

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agotreewide: rename nla_strlcpy to nla_strscpy.
Francis Laniel [Sun, 20 Dec 2020 12:31:40 +0000 (13:31 +0100)] 
treewide: rename nla_strlcpy to nla_strscpy.

Calls to nla_strlcpy are now replaced by calls to nla_strscpy which is the new
name of this function.

Signed-off-by: Francis Laniel <laniel_francis@privacyrequired.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
4 years agonetfilter: ipset: fix shift-out-of-bounds in htable_bits()
Vasily Averin [Sun, 20 Dec 2020 12:21:13 +0000 (13:21 +0100)] 
netfilter: ipset: fix shift-out-of-bounds in htable_bits()

htable_bits() can call jhash_size(32) and trigger shift-out-of-bounds

UBSAN: shift-out-of-bounds in net/netfilter/ipset/ip_set_hash_gen.h:151:6
shift exponent 32 is too large for 32-bit type 'unsigned int'
CPU: 0 PID: 8498 Comm: syz-executor519
 Not tainted 5.10.0-rc7-next-20201208-syzkaller #0
Call Trace:
 __dump_stack lib/dump_stack.c:79 [inline]
 dump_stack+0x107/0x163 lib/dump_stack.c:120
 ubsan_epilogue+0xb/0x5a lib/ubsan.c:148
 __ubsan_handle_shift_out_of_bounds.cold+0xb1/0x181 lib/ubsan.c:395
 htable_bits net/netfilter/ipset/ip_set_hash_gen.h:151 [inline]
 hash_mac_create.cold+0x58/0x9b net/netfilter/ipset/ip_set_hash_gen.h:1524
 ip_set_create+0x610/0x1380 net/netfilter/ipset/ip_set_core.c:1115
 nfnetlink_rcv_msg+0xecc/0x1180 net/netfilter/nfnetlink.c:252
 netlink_rcv_skb+0x153/0x420 net/netlink/af_netlink.c:2494
 nfnetlink_rcv+0x1ac/0x420 net/netfilter/nfnetlink.c:600
 netlink_unicast_kernel net/netlink/af_netlink.c:1304 [inline]
 netlink_unicast+0x533/0x7d0 net/netlink/af_netlink.c:1330
 netlink_sendmsg+0x907/0xe40 net/netlink/af_netlink.c:1919
 sock_sendmsg_nosec net/socket.c:652 [inline]
 sock_sendmsg+0xcf/0x120 net/socket.c:672
 ____sys_sendmsg+0x6e8/0x810 net/socket.c:2345
  __sys_sendmsg+0xe5/0x1b0 net/socket.c:2432
 do_syscall_64+0x2d/0x70 arch/x86/entry/common.c:46
 entry_SYSCALL_64_after_hwframe+0x44/0xa9

This patch replaces htable_bits() by simple fls(hashsize - 1) call:
it alone returns valid nbits both for round and non-round hashsizes.
It is normal to set any nbits here because it is validated inside
following htable_size() call which returns 0 for nbits>31.

Fixes: 1feab10d7e6d("netfilter: ipset: Unified hash type generation")
Reported-by: syzbot+d66bfadebca46cf61a2b@syzkaller.appspotmail.com
Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfilter: ipset: fixes possible oops in mtype_resize
Vasily Averin [Sun, 20 Dec 2020 12:17:01 +0000 (13:17 +0100)] 
netfilter: ipset: fixes possible oops in mtype_resize

currently mtype_resize() can cause oops

        t = ip_set_alloc(htable_size(htable_bits));
        if (!t) {
                ret = -ENOMEM;
                goto out;
        }
        t->hregion = ip_set_alloc(ahash_sizeof_regions(htable_bits));

Increased htable_bits can force htable_size() to return 0.
In own turn ip_set_alloc(0) returns not 0 but ZERO_SIZE_PTR,
so follwoing access to t->hregion should trigger an OOPS.

Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoHandle false warning from -Wstringop-overflow
Jozsef Kadlecsik [Mon, 14 Dec 2020 09:19:34 +0000 (10:19 +0100)] 
Handle false warning from -Wstringop-overflow

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoBackward compatibility: handle missing strscpy with a wrapper of strlcpy.
Jozsef Kadlecsik [Mon, 14 Dec 2020 09:12:57 +0000 (10:12 +0100)] 
Backward compatibility: handle missing strscpy with a wrapper of strlcpy.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoMove compiler specific compatibility support to separated file
Jozsef Kadlecsik [Mon, 7 Dec 2020 14:35:56 +0000 (15:35 +0100)] 
Move compiler specific compatibility support to separated file

Kernel compatibility support was broken in 7.9, reported by Ed W.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset 7.9 released v7.9
Jozsef Kadlecsik [Thu, 19 Nov 2020 21:42:19 +0000 (22:42 +0100)] 
ipset 7.9 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoFix library versioning (Jan Engelhardt)
Jozsef Kadlecsik [Thu, 19 Nov 2020 21:41:22 +0000 (22:41 +0100)] 
Fix library versioning (Jan Engelhardt)

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset 7.8 released v7.8
Jozsef Kadlecsik [Thu, 19 Nov 2020 20:34:57 +0000 (21:34 +0100)] 
ipset 7.8 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoComplete backward compatibility fix for package copy of <linux/jhash.h>
Jozsef Kadlecsik [Thu, 19 Nov 2020 20:26:13 +0000 (21:26 +0100)] 
Complete backward compatibility fix for package copy of <linux/jhash.h>

An unnecessary condition prevented to compile pfxlen.c with the patch
202cfef66b3a1e0988d applied, it's fixed.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoCompatibility: check for kvzalloc() and GFP_KERNEL_ACCOUNT
Jozsef Kadlecsik [Thu, 19 Nov 2020 14:01:37 +0000 (15:01 +0100)] 
Compatibility: check for kvzalloc() and GFP_KERNEL_ACCOUNT

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfilter: ipset: enable memory accounting for ipset allocations
Vasily Averin [Thu, 19 Nov 2020 13:59:51 +0000 (14:59 +0100)] 
netfilter: ipset: enable memory accounting for ipset allocations

Currently netadmin inside non-trusted container can quickly allocate
whole node's memory via request of huge ipset hashtable.
Other ipset-related memory allocations should be restricted too.

v2: fixed typo ALLOC -> ACCOUNT

Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfilter: ipset: prevent uninit-value in hash_ip6_add
Eric Dumazet [Thu, 19 Nov 2020 13:07:48 +0000 (14:07 +0100)] 
netfilter: ipset: prevent uninit-value in hash_ip6_add

syzbot found that we are not validating user input properly
before copying 16 bytes [1].

Using NLA_BINARY in ipaddr_policy[] for IPv6 address is not correct,
since it ensures at most 16 bytes were provided.

We should instead make sure user provided exactly 16 bytes.

In old kernels (before v4.20), fix would be to remove the NLA_BINARY,
since NLA_POLICY_EXACT_LEN() was not yet available.

[1]
BUG: KMSAN: uninit-value in hash_ip6_add+0x1cba/0x3a50
net/netfilter/ipset/ip_set_hash_gen.h:892
CPU: 1 PID: 11611 Comm: syz-executor.0 Not tainted 5.10.0-rc4-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
 __dump_stack lib/dump_stack.c:77 [inline]
 dump_stack+0x21c/0x280 lib/dump_stack.c:118
 kmsan_report+0xf7/0x1e0 mm/kmsan/kmsan_report.c:118
 __msan_warning+0x5f/0xa0 mm/kmsan/kmsan_instr.c:197
 hash_ip6_add+0x1cba/0x3a50 net/netfilter/ipset/ip_set_hash_gen.h:892
 hash_ip6_uadt+0x976/0xbd0 net/netfilter/ipset/ip_set_hash_ip.c:267
 call_ad+0x329/0xd00 net/netfilter/ipset/ip_set_core.c:1720
 ip_set_ad+0x111f/0x1440 net/netfilter/ipset/ip_set_core.c:1808
 ip_set_uadd+0xf6/0x110 net/netfilter/ipset/ip_set_core.c:1833
 nfnetlink_rcv_msg+0xc7d/0xdf0 net/netfilter/nfnetlink.c:252
 netlink_rcv_skb+0x70a/0x820 net/netlink/af_netlink.c:2494
 nfnetlink_rcv+0x4f0/0x4380 net/netfilter/nfnetlink.c:600
 netlink_unicast_kernel net/netlink/af_netlink.c:1304 [inline]
 netlink_unicast+0x11da/0x14b0 net/netlink/af_netlink.c:1330
 netlink_sendmsg+0x173c/0x1840 net/netlink/af_netlink.c:1919
 sock_sendmsg_nosec net/socket.c:651 [inline]
 sock_sendmsg net/socket.c:671 [inline]
 ____sys_sendmsg+0xc7a/0x1240 net/socket.c:2353
 ___sys_sendmsg net/socket.c:2407 [inline]
 __sys_sendmsg+0x6d5/0x830 net/socket.c:2440
 __do_sys_sendmsg net/socket.c:2449 [inline]
 __se_sys_sendmsg+0x97/0xb0 net/socket.c:2447
 __x64_sys_sendmsg+0x4a/0x70 net/socket.c:2447
 do_syscall_64+0x9f/0x140 arch/x86/entry/common.c:48
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
RIP: 0033:0x45deb9
Code: 0d b4 fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 89 f8 48 89 f7 48 89
d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 0f 83 db b3
fb ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:00007fe2e503fc78 EFLAGS: 00000246 ORIG_RAX: 000000000000002e
RAX: ffffffffffffffda RBX: 0000000000029ec0 RCX: 000000000045deb9
RDX: 0000000000000000 RSI: 0000000020000140 RDI: 0000000000000003
RBP: 000000000118bf60 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 000000000118bf2c
R13: 000000000169fb7f R14: 00007fe2e50409c0 R15: 000000000118bf2c

Uninit was stored to memory at:
 kmsan_save_stack_with_flags mm/kmsan/kmsan.c:121 [inline]
 kmsan_internal_chain_origin+0xad/0x130 mm/kmsan/kmsan.c:289
 __msan_chain_origin+0x57/0xa0 mm/kmsan/kmsan_instr.c:147
 ip6_netmask include/linux/netfilter/ipset/pfxlen.h:49 [inline]
 hash_ip6_netmask net/netfilter/ipset/ip_set_hash_ip.c:185 [inline]
 hash_ip6_uadt+0xb1c/0xbd0 net/netfilter/ipset/ip_set_hash_ip.c:263
 call_ad+0x329/0xd00 net/netfilter/ipset/ip_set_core.c:1720
 ip_set_ad+0x111f/0x1440 net/netfilter/ipset/ip_set_core.c:1808
 ip_set_uadd+0xf6/0x110 net/netfilter/ipset/ip_set_core.c:1833
 nfnetlink_rcv_msg+0xc7d/0xdf0 net/netfilter/nfnetlink.c:252
 netlink_rcv_skb+0x70a/0x820 net/netlink/af_netlink.c:2494
 nfnetlink_rcv+0x4f0/0x4380 net/netfilter/nfnetlink.c:600
 netlink_unicast_kernel net/netlink/af_netlink.c:1304 [inline]
 netlink_unicast+0x11da/0x14b0 net/netlink/af_netlink.c:1330
 netlink_sendmsg+0x173c/0x1840 net/netlink/af_netlink.c:1919
 sock_sendmsg_nosec net/socket.c:651 [inline]
 sock_sendmsg net/socket.c:671 [inline]
 ____sys_sendmsg+0xc7a/0x1240 net/socket.c:2353
 ___sys_sendmsg net/socket.c:2407 [inline]
 __sys_sendmsg+0x6d5/0x830 net/socket.c:2440
 __do_sys_sendmsg net/socket.c:2449 [inline]
 __se_sys_sendmsg+0x97/0xb0 net/socket.c:2447
 __x64_sys_sendmsg+0x4a/0x70 net/socket.c:2447
 do_syscall_64+0x9f/0x140 arch/x86/entry/common.c:48
 entry_SYSCALL_64_after_hwframe+0x44/0xa9

Uninit was stored to memory at:
 kmsan_save_stack_with_flags mm/kmsan/kmsan.c:121 [inline]
 kmsan_internal_chain_origin+0xad/0x130 mm/kmsan/kmsan.c:289
 kmsan_memcpy_memmove_metadata+0x25e/0x2d0 mm/kmsan/kmsan.c:226
 kmsan_memcpy_metadata+0xb/0x10 mm/kmsan/kmsan.c:246
 __msan_memcpy+0x46/0x60 mm/kmsan/kmsan_instr.c:110
 ip_set_get_ipaddr6+0x2cb/0x370 net/netfilter/ipset/ip_set_core.c:310
 hash_ip6_uadt+0x439/0xbd0 net/netfilter/ipset/ip_set_hash_ip.c:255
 call_ad+0x329/0xd00 net/netfilter/ipset/ip_set_core.c:1720
 ip_set_ad+0x111f/0x1440 net/netfilter/ipset/ip_set_core.c:1808
 ip_set_uadd+0xf6/0x110 net/netfilter/ipset/ip_set_core.c:1833
 nfnetlink_rcv_msg+0xc7d/0xdf0 net/netfilter/nfnetlink.c:252
 netlink_rcv_skb+0x70a/0x820 net/netlink/af_netlink.c:2494
 nfnetlink_rcv+0x4f0/0x4380 net/netfilter/nfnetlink.c:600
 netlink_unicast_kernel net/netlink/af_netlink.c:1304 [inline]
 netlink_unicast+0x11da/0x14b0 net/netlink/af_netlink.c:1330
 netlink_sendmsg+0x173c/0x1840 net/netlink/af_netlink.c:1919
 sock_sendmsg_nosec net/socket.c:651 [inline]
 sock_sendmsg net/socket.c:671 [inline]
 ____sys_sendmsg+0xc7a/0x1240 net/socket.c:2353
 ___sys_sendmsg net/socket.c:2407 [inline]
 __sys_sendmsg+0x6d5/0x830 net/socket.c:2440
 __do_sys_sendmsg net/socket.c:2449 [inline]
 __se_sys_sendmsg+0x97/0xb0 net/socket.c:2447
 __x64_sys_sendmsg+0x4a/0x70 net/socket.c:2447
 do_syscall_64+0x9f/0x140 arch/x86/entry/common.c:48
 entry_SYSCALL_64_after_hwframe+0x44/0xa9

Uninit was created at:
 kmsan_save_stack_with_flags mm/kmsan/kmsan.c:121 [inline]
 kmsan_internal_poison_shadow+0x5c/0xf0 mm/kmsan/kmsan.c:104
 kmsan_slab_alloc+0x8d/0xe0 mm/kmsan/kmsan_hooks.c:76
 slab_alloc_node mm/slub.c:2906 [inline]
 __kmalloc_node_track_caller+0xc61/0x15f0 mm/slub.c:4512
 __kmalloc_reserve net/core/skbuff.c:142 [inline]
 __alloc_skb+0x309/0xae0 net/core/skbuff.c:210
 alloc_skb include/linux/skbuff.h:1094 [inline]
 netlink_alloc_large_skb net/netlink/af_netlink.c:1176 [inline]
 netlink_sendmsg+0xdb8/0x1840 net/netlink/af_netlink.c:1894
 sock_sendmsg_nosec net/socket.c:651 [inline]
 sock_sendmsg net/socket.c:671 [inline]
 ____sys_sendmsg+0xc7a/0x1240 net/socket.c:2353
 ___sys_sendmsg net/socket.c:2407 [inline]
 __sys_sendmsg+0x6d5/0x830 net/socket.c:2440
 __do_sys_sendmsg net/socket.c:2449 [inline]
 __se_sys_sendmsg+0x97/0xb0 net/socket.c:2447
 __x64_sys_sendmsg+0x4a/0x70 net/socket.c:2447
 do_syscall_64+0x9f/0x140 arch/x86/entry/common.c:48
 entry_SYSCALL_64_after_hwframe+0x44/0xa9

Fixes: a7b4f989a629 ("netfilter: ipset: IP set core support")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reported-by: syzbot <syzkaller@googlegroups.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoCompatibility: use skb_policy() from if_vlan.h if available
Jozsef Kadlecsik [Thu, 19 Nov 2020 12:50:02 +0000 (13:50 +0100)] 
Compatibility: use skb_policy() from if_vlan.h if available

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoCompatibility: Check for the fourth arg of list_for_each_entry_rcu()
Jozsef Kadlecsik [Thu, 19 Nov 2020 12:05:45 +0000 (13:05 +0100)] 
Compatibility: Check for the fourth arg of list_for_each_entry_rcu()

A forth argument of list_for_each_entry_rcu() was introduced, handle the
compatibility issue.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoBackward compatibility fix for the package copy of <linux/jhash.h>
Jozsef Kadlecsik [Sun, 8 Nov 2020 20:54:01 +0000 (21:54 +0100)] 
Backward compatibility fix for the package copy of <linux/jhash.h>

The patch "Use fallthrough pseudo-keyword in the package copy of
<linux/jhash.h> too" missed to include the compatibility header file.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset 7.7 released v7.7
Jozsef Kadlecsik [Tue, 22 Sep 2020 07:31:19 +0000 (09:31 +0200)] 
ipset 7.7 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoExpose the initval hash parameter to userspace
Jozsef Kadlecsik [Mon, 21 Sep 2020 18:13:07 +0000 (20:13 +0200)] 
Expose the initval hash parameter to userspace

It makes possible to reproduce exactly the same set after a save/restore.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoHandle all variable header parts in helper scripts instead ot test tasks
Jozsef Kadlecsik [Mon, 21 Sep 2020 18:12:23 +0000 (20:12 +0200)] 
Handle all variable header parts in helper scripts instead ot test tasks

Thus the tests tasks can be simplified and all exceptions can be handled in
the helper scripts.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoAdd bucketsize parameter to all hash types
Jozsef Kadlecsik [Mon, 21 Sep 2020 08:21:39 +0000 (10:21 +0200)] 
Add bucketsize parameter to all hash types

The parameter defines the upper limit in any hash bucket at adding new entries
from userspace - if the limit would be exceeded, ipset doubles the hash size
and rehashes. It means the set may consume more memory but gives faster
evaluation at matching in the set.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoUse fallthrough pseudo-keyword in the package copy of <linux/jhash.h> too
Jozsef Kadlecsik [Sun, 20 Sep 2020 20:48:28 +0000 (22:48 +0200)] 
Use fallthrough pseudo-keyword in the package copy of <linux/jhash.h> too

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoSupport the -exist flag with the destroy command
Jozsef Kadlecsik [Sun, 20 Sep 2020 11:06:59 +0000 (13:06 +0200)] 
Support the -exist flag with the destroy command

The -exist flag was supported with the create, add and delete commands.
In order to gracefully handle the destroy command with nonexistent sets,
the -exist flag is added to destroy too.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfilter: Use fallthrough pseudo-keyword
Gustavo A. R. Silva [Wed, 8 Jul 2020 20:09:39 +0000 (15:09 -0500)] 
netfilter: Use fallthrough pseudo-keyword

Replace the existing /* fall through */ comments and its variants with
the new pseudo-keyword macro fallthrough[1]. Also, remove unnecessary
fall-through markings when it is the case.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html?highlight=fallthrough#implicit-switch-case-fall-through

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfilter: Replace zero-length array with flexible-array member
Gustavo A. R. Silva [Thu, 20 Feb 2020 13:59:14 +0000 (07:59 -0600)] 
netfilter: Replace zero-length array with flexible-array member

The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:

struct foo {
        int stuff;
        struct boo array[];
};

By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.

Also, notice that, dynamic memory allocations won't be affected by
this change:

"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]

Lastly, fix checkpatch.pl warning
WARNING: __aligned(size) is preferred over __attribute__((aligned(size)))
in net/bridge/netfilter/ebtables.c

This issue was found with the help of Coccinelle.

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour")

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfilter: ipset: call ip_set_free() instead of kfree()
Eric Dumazet [Tue, 30 Jun 2020 00:04:17 +0000 (17:04 -0700)] 
netfilter: ipset: call ip_set_free() instead of kfree()

Whenever ip_set_alloc() is used, allocated memory can either
use kmalloc() or vmalloc(). We should call kvfree() or
ip_set_free()

invalid opcode: 0000 [#1] PREEMPT SMP KASAN
CPU: 0 PID: 21935 Comm: syz-executor.3 Not tainted 5.8.0-rc2-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
RIP: 0010:__phys_addr+0xa7/0x110 arch/x86/mm/physaddr.c:28
Code: 1d 7a 09 4c 89 e3 31 ff 48 d3 eb 48 89 de e8 d0 58 3f 00 48 85 db 75 0d e8 26 5c 3f 00 4c 89 e0 5b 5d 41 5c c3 e8 19 5c 3f 00 <0f> 0b e8 12 5c 3f 00 48 c7 c0 10 10 a8 89 48 ba 00 00 00 00 00 fc
RSP: 0000:ffffc900018572c0 EFLAGS: 00010046
RAX: 0000000000040000 RBX: 0000000000000001 RCX: ffffc9000fac3000
RDX: 0000000000040000 RSI: ffffffff8133f437 RDI: 0000000000000007
RBP: ffffc90098aff000 R08: 0000000000000000 R09: ffff8880ae636cdb
R10: 0000000000000000 R11: 0000000000000000 R12: 0000408018aff000
R13: 0000000000080000 R14: 000000000000001d R15: ffffc900018573d8
FS:  00007fc540c66700(0000) GS:ffff8880ae600000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fc9dcd67200 CR3: 0000000059411000 CR4: 00000000001406f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
 virt_to_head_page include/linux/mm.h:841 [inline]
 virt_to_cache mm/slab.h:474 [inline]
 kfree+0x77/0x2c0 mm/slab.c:3749
 hash_net_create+0xbb2/0xd70 net/netfilter/ipset/ip_set_hash_gen.h:1536
 ip_set_create+0x6a2/0x13c0 net/netfilter/ipset/ip_set_core.c:1128
 nfnetlink_rcv_msg+0xbe8/0xea0 net/netfilter/nfnetlink.c:230
 netlink_rcv_skb+0x15a/0x430 net/netlink/af_netlink.c:2469
 nfnetlink_rcv+0x1ac/0x420 net/netfilter/nfnetlink.c:564
 netlink_unicast_kernel net/netlink/af_netlink.c:1303 [inline]
 netlink_unicast+0x533/0x7d0 net/netlink/af_netlink.c:1329
 netlink_sendmsg+0x856/0xd90 net/netlink/af_netlink.c:1918
 sock_sendmsg_nosec net/socket.c:652 [inline]
 sock_sendmsg+0xcf/0x120 net/socket.c:672
 ____sys_sendmsg+0x6e8/0x810 net/socket.c:2352
 ___sys_sendmsg+0xf3/0x170 net/socket.c:2406
 __sys_sendmsg+0xe5/0x1b0 net/socket.c:2439
 do_syscall_64+0x60/0xe0 arch/x86/entry/common.c:359
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
RIP: 0033:0x45cb19
Code: Bad RIP value.
RSP: 002b:00007fc540c65c78 EFLAGS: 00000246 ORIG_RAX: 000000000000002e
RAX: ffffffffffffffda RBX: 00000000004fed80 RCX: 000000000045cb19
RDX: 0000000000000000 RSI: 0000000020001080 RDI: 0000000000000003
RBP: 000000000078bf00 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 00000000ffffffff
R13: 000000000000095e R14: 00000000004cc295 R15: 00007fc540c666d4

Fixes: f66ee0410b1c ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
Fixes: 03c8b234e61a ("netfilter: ipset: Generalize extensions support")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reported-by: syzbot <syzkaller@googlegroups.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfiler: ipset: fix unaligned atomic access
Russell King [Wed, 10 Jun 2020 20:51:11 +0000 (21:51 +0100)] 
netfiler: ipset: fix unaligned atomic access

When using ip_set with counters and comment, traffic causes the kernel
to panic on 32-bit ARM:

Alignment trap: not handling instruction e1b82f9f at [<bf01b0dc>]
Unhandled fault: alignment exception (0x221) at 0xea08133c
PC is at ip_set_match_extensions+0xe0/0x224 [ip_set]

The problem occurs when we try to update the 64-bit counters - the
faulting address above is not 64-bit aligned.  The problem occurs
due to the way elements are allocated, for example:

set->dsize = ip_set_elem_len(set, tb, 0, 0);
map = ip_set_alloc(sizeof(*map) + elements * set->dsize);

If the element has a requirement for a member to be 64-bit aligned,
and set->dsize is not a multiple of 8, but is a multiple of four,
then every odd numbered elements will be misaligned - and hitting
an atomic64_add() on that element will cause the kernel to panic.

ip_set_elem_len() must return a size that is rounded to the maximum
alignment of any extension field stored in the element.  This change
ensures that is the case.

Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfilter: ipset: Fix subcounter update skip
Phil Sutter [Thu, 14 May 2020 11:31:21 +0000 (13:31 +0200)] 
netfilter: ipset: Fix subcounter update skip

If IPSET_FLAG_SKIP_SUBCOUNTER_UPDATE is set, user requested to not
update counters in sub sets. Therefore IPSET_FLAG_SKIP_COUNTER_UPDATE
must be set, not unset.

Fixes: 6e01781d1c80e ("netfilter: ipset: set match: add support to match the counters")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoipset: Update byte and packet counters regardless of whether they match
Stefano Brivio [Mon, 24 Feb 2020 17:52:43 +0000 (18:52 +0100)] 
ipset: Update byte and packet counters regardless of whether they match

In ip_set_match_extensions(), for sets with counters, we take care of
updating counters themselves by calling ip_set_update_counter(), and of
checking if the given comparison and values match, by calling
ip_set_match_counter() if needed.

However, if a given comparison on counters doesn't match the configured
values, that doesn't mean the set entry itself isn't matching.

This fix restores the behaviour we had before commit 4750005a85f7
("netfilter: ipset: Fix "don't update counters" mode when counters used
at the matching"), without reintroducing the issue fixed there: back
then, mtype_data_match() first updated counters in any case, and then
took care of matching on counters.

Now, if the IPSET_FLAG_SKIP_COUNTER_UPDATE flag is set,
ip_set_update_counter() will anyway skip counter updates if desired.

The issue observed is illustrated by this reproducer:

  ipset create c hash:ip counters
  ipset add c 192.0.2.1
  iptables -I INPUT -m set --match-set c src --bytes-gt 800 -j DROP

if we now send packets from 192.0.2.1, bytes and packets counters
for the entry as shown by 'ipset list' are always zero, and, no
matter how many bytes we send, the rule will never match, because
counters themselves are not updated.

Reported-by: Mithil Mhatre <mmhatre@redhat.com>
Fixes: 4750005a85f7 ("netfilter: ipset: Fix "don't update counters" mode when counters used at the matching")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agonetfilter: ipset: Pass lockdep expression to RCU lists
Amol Grover [Sun, 16 Feb 2020 17:26:54 +0000 (22:56 +0530)] 
netfilter: ipset: Pass lockdep expression to RCU lists

ip_set_type_list is traversed using list_for_each_entry_rcu
outside an RCU read-side critical section but under the protection
of ip_set_type_mutex.

Hence, add corresponding lockdep expression to silence false-positive
warnings, and harden RCU lists.

Signed-off-by: Amol Grover <frextrite@gmail.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
4 years agoRevert "Introduce --update-counters-first flag for the set target"
Jozsef Kadlecsik [Sun, 20 Sep 2020 08:21:15 +0000 (10:21 +0200)] 
Revert "Introduce --update-counters-first flag for the set target"

This reverts commit c2dfb6432aea5bb0a3522901b0c44f42d8adbd49.

5 years agoip_set: Fix compatibility with kernels between v3.3 and v4.5
Serhey Popovych [Thu, 5 Mar 2020 15:28:24 +0000 (17:28 +0200)] 
ip_set: Fix compatibility with kernels between v3.3 and v4.5

These kernels does not have in their @struct netlink_dump_control method
that is used to prepare for netlink dump ->start(). This affects all
kernels that does not contain commit fc9e50f5a5a4 ("netlink: add a start
callback for starting a netlink dump").

Introduce fake value of HAVE_NETLINK_DUMP_START_ARGS equal to 7 that
never spot in the wild and set HAVE_NETLINK_DUMP_START_ARGS to 4 only
after explicit test if ->start() is available.

Fixes: 7725bf5ba041 ("netfilter: ipset: fix suspicious RCU usage in find_set_and_id")
Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
5 years agoIntroduce --update-counters-first flag for the set target
Jozsef Kadlecsik [Mon, 9 Mar 2020 11:01:42 +0000 (12:01 +0100)] 
Introduce --update-counters-first flag for the set target

Stefano Brivio reported that the patch 'netfilter: ipset:
Fix "don't update counters" mode when counters used at the matching'
changed the semantic of when the counters are updated. Before the patch
the counters were updated regardless of the results of the counter
matches, after the patch the counters were updated only if the counter
match conditions (if specified) matched the packet. In order to handle
both ways, the --update-counters-first flag is introduced: when the flag
is specified, the counters are updated before checking the counter match
conditions. Without the flag the current evaluation path (i.e. update
only if counter conditions match) works.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
5 years agoip_set: Fix build on kernels without INIT_DEFERRABLE_WORK
Serhey Popovych [Thu, 5 Mar 2020 15:28:23 +0000 (17:28 +0200)] 
ip_set: Fix build on kernels without INIT_DEFERRABLE_WORK

There was macro rename in kernel with commit 203b42f73174 ("workqueue:
make deferrable delayed_work initializer names consistent") that renames
INIT_DELAYED_WORK_DEFERRABLE() to INIT_DEFERRABLE_WORK().

Fixes: 33f08da28324 ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
5 years agoipset: Support kernels with at least system_wq support
Jozsef Kadlecsik [Mon, 9 Mar 2020 10:54:08 +0000 (11:54 +0100)] 
ipset: Support kernels with at least system_wq support

5 years agoip_set: Fix build on kernels without system_power_efficient_wq
Serhey Popovych [Thu, 5 Mar 2020 15:28:22 +0000 (17:28 +0200)] 
ip_set: Fix build on kernels without system_power_efficient_wq

This type of workqueue according to comment in <linux/workqueue.h> is
same as regular @system_wq when power efficiency scheduler options
disabled.

For old kernels where @system_power_efficient_wq is not available use
regular @system_wq to support ipset on kernels below 3.11.

Fixes: 33f08da28324 ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
5 years agoMerge branch 'region_lock'
Jozsef Kadlecsik [Sat, 22 Feb 2020 12:01:35 +0000 (13:01 +0100)] 
Merge branch 'region_lock'

5 years agoipset 7.6 released region_lock v7.6
Jozsef Kadlecsik [Sat, 22 Feb 2020 11:53:21 +0000 (12:53 +0100)] 
ipset 7.6 released

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
5 years agoAdd checking system_power_efficient_wq in the kernel source tree
Jozsef Kadlecsik [Sat, 22 Feb 2020 11:51:24 +0000 (12:51 +0100)] 
Add checking system_power_efficient_wq in the kernel source tree

5 years agonetfilter: ipset: Fix forceadd evaluation path
Jozsef Kadlecsik [Sat, 22 Feb 2020 10:24:20 +0000 (11:24 +0100)] 
netfilter: ipset: Fix forceadd evaluation path

When the forceadd option is enabled, the hash:* types should find and replace
the first entry in the bucket with the new one if there are no reuseable
(deleted or timed out) entries. However, the position index was just not set
to zero and remained the invalid -1 if there were no reuseable entries.

Reported-by: syzbot+6a86565c74ebe30aea18@syzkaller.appspotmail.com
Fixes: 23c42a403a9c ("netfilter: ipset: Introduction of new commands and protocol version 7")
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
5 years agonetfilter: ipset: Correct the reported memory size
Jozsef Kadlecsik [Fri, 21 Feb 2020 20:53:11 +0000 (21:53 +0100)] 
netfilter: ipset: Correct the reported memory size

The patch

netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports

did not include the size of the comment extensions from the memory size
for set listing. Add it, so the proper size is printed.

Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>