]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
2 days agoAmend "tests: shell: Fix ifname_based_hooks feature check" 1.0.6.y
Pablo Neira Ayuso [Mon, 28 Jul 2025 22:52:55 +0000 (00:52 +0200)] 
Amend "tests: shell: Fix ifname_based_hooks feature check"

commit 646acfaceb1f550c982c31ba6e60996b0bb012d7 upstream.

The following syntax:

  type filter hook ingress priority 0; devices = { d0 };

is not parsed in 1.0.6.y.

And remove device deletion, this is only available since 1.0.8 in:

commit a66b5ad9540dd64c7c67006201b8b3ccf8e4316b
Author: Pablo Neira Ayuso <pablo@netfilter.org>
Date:   Wed Apr 19 11:50:01 2023 +0200

    src: allow for updating devices on existing netdev chain

skip netdev_chain_dormant_autoremove for new kernels.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoRevert "tests: shell: move flowtable with bogus priority to correct location"
Pablo Neira Ayuso [Mon, 28 Jul 2025 21:36:43 +0000 (23:36 +0200)] 
Revert "tests: shell: move flowtable with bogus priority to correct location"

Revert 156b5936b3b7a0b1ee590a02874beaba5235f758.

It fails for unknown reason to me.

2 days agoPartial revert "tests: py: move meter tests to tests/shell"
Pablo Neira Ayuso [Mon, 28 Jul 2025 20:34:39 +0000 (22:34 +0200)] 
Partial revert "tests: py: move meter tests to tests/shell"

c4c740117f6fbf39dd67dd87635ea8b497718ad7

Only tests/shell, translation of meter to dynamic set is not
available in 1.0.6.y

2 days agoRevert "evaluate: translate meter into dynamic set"
Pablo Neira Ayuso [Mon, 28 Jul 2025 20:15:16 +0000 (22:15 +0200)] 
Revert "evaluate: translate meter into dynamic set"

This reverts commit b8f8ddfff7335d3a8bebf5d85085974ae36f4099.

Only tests/shell, since translation of meter to dynamic set is not
available in 1.0.6.y

2 days agoPartial revert in testcase/sets/set_stmt to remove last statement coverage
Pablo Neira Ayuso [Sun, 27 Jul 2025 21:50:57 +0000 (23:50 +0200)] 
Partial revert in testcase/sets/set_stmt to remove last statement coverage

This last statement is not supported in 1.0.6.y, remove it from

1f3d0b9cf9cc tests: shell: extend coverage for set element statements

to make this test pass.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoRevert mptcp tests for sets/typeof_sets_0
Pablo Neira Ayuso [Sun, 27 Jul 2025 21:45:43 +0000 (23:45 +0200)] 
Revert mptcp tests for sets/typeof_sets_0

Revert tests:

9832c2c347eb ("tcpopt: add symbol table for mptcp suboptions")
01fe0f07a0ed ("netlink_delinearize: also consider exthdr type when trimming binops")

These commits are not backported, revert these tests.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoRevert "evaluate: allow to re-use existing metered set"
Pablo Neira Ayuso [Wed, 18 Jun 2025 19:57:46 +0000 (21:57 +0200)] 
Revert "evaluate: allow to re-use existing metered set"

This reverts commit 639a111e91341cffdc6d86b847aa654646c799cf.

Conversion to dynamic set is not available in 1.0.6.y

2 days agoRevert "src: print set element with multi-word description in single one line"
Pablo Neira Ayuso [Wed, 18 Jun 2025 19:56:29 +0000 (21:56 +0200)] 
Revert "src: print set element with multi-word description in single one line"

This reverts commit f133180ee0c72f4a0de4a7acad8a9d8476914bd4.

2 days agoRevert "json: Print single set flag as non-array"
Pablo Neira Ayuso [Wed, 18 Jun 2025 19:55:51 +0000 (21:55 +0200)] 
Revert "json: Print single set flag as non-array"

This reverts commit 6bedb12af1658562f277ca68d74cf1e9e7433a08.

This more compact json representation for flags is not present in 1.0.6.y

2 days agoBackport nftables tests/shell from 2a38f458f12b
Pablo Neira Ayuso [Sun, 27 Jul 2025 21:27:14 +0000 (23:27 +0200)] 
Backport nftables tests/shell from 2a38f458f12b

Originally, the plan was to use tests/shell from git HEAD, but this
does not work well in practise because slight changes in the test
output break checks resulting in lots of [DUMP FAIL] errors.

It is easier to test infrastructure self-contained in this 1.0.6.y
branch.

However, backporting the tests/shell into 1.0.6.y turns out to be more
complicated than expected, so I decided to follow the opposite, which is
to (brute) force a copy of tests/shell from
2a38f458f12bc032dac1b3ba63f95ca5a3c03fbd into this branch.

This also requires a number follow up partial reverts on tests/shell
updates to work with 1.0.6.y.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoevalute: make vlan pcp updates work
Florian Westphal [Sat, 19 Apr 2025 11:44:39 +0000 (13:44 +0200)] 
evalute: make vlan pcp updates work

commit 2c41c82296c70203f4781fb976ee0f05629fba44 upstream.

On kernel side, nft_payload_set_vlan() requires a 2 or 4 byte
write to the vlan header.

As-is, nft emits a 1 byte write:
  [ payload load 1b @ link header + 14 => reg 1 ]
  [ bitwise reg 1 = ( reg 1 & 0x0000001f ) ^ 0x00000020 ]

... which the kernel doesn't support.  Expand all vlan header updates to
a 2 or 4 byte write and update the existing vlan id test case.

Reported-by: Kevin Vigouroux <ke.vigouroux@laposte.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoevaluate: only allow stateful statements in set and map definitions
Florian Westphal [Mon, 31 Mar 2025 15:23:20 +0000 (17:23 +0200)] 
evaluate: only allow stateful statements in set and map definitions

commit 0acd81559ec9efe2cc3d869bfc8e5a0b4d888456 upstream.

The bison parser doesn't allow this to happen due to grammar
restrictions, but the json input has no such issues.

The bogon input assigns 'notrack' which triggers:
BUG: unknown stateful statement type 19
nft: src/netlink_linearize.c:1061: netlink_gen_stmt_stateful: Assertion `0' failed.

After patch, we get:
Error: map statement must be stateful

Fixes: 07958ec53830 ("json: add set statement list support")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoevaluate: compact STMT_F_STATEFUL checks
Florian Westphal [Mon, 31 Mar 2025 15:23:19 +0000 (17:23 +0200)] 
evaluate: compact STMT_F_STATEFUL checks

commit 36bd6d0088bca1087aeccfe14aaa786200d755bc upstream.

We'll gain another F_STATEFUL check in a followup patch,
so lets condense the pattern into a helper to reduce copypaste.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoparser_json: only allow concatenations with 2 or more expressions
Florian Westphal [Wed, 2 Apr 2025 05:18:18 +0000 (07:18 +0200)] 
parser_json: only allow concatenations with 2 or more expressions

commit 713592c6008a8c589a00d3d3d2e49709ff2de62c upstream.

The bison parser enforces this implicitly by grammar rules.
Because subkeys have to be conatenated via ".", notation, e.g.
"mark . ip saddr", all concatenation expressions always consist of at
least two elements.

But this doesn't apply to the json frontend which just uses an array:
it can be empty or only contain one element.

The included reproducer makes the eval stage set the "concatenation" flag
on the interval set.  This prevents the needed conversion code to turn the
element values into ranges from getting run.

The reproducer asserts with:
nft: src/intervals.c:786: setelem_to_interval: Assertion `key->etype == EXPR_RANGE_VALUE' failed.

Convert the assertion to BUG() so we can see what element type got passed
to the set interval code in case we have further issues in this area.

Reject 0-or-1-element concatenations from the json parser.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoevaluate: fix crash when generating reject statement error
Florian Westphal [Mon, 31 Mar 2025 12:43:34 +0000 (14:43 +0200)] 
evaluate: fix crash when generating reject statement error

commit 9b7346d1eac2eb90a2baf589affafec5b1a033b6 upstream.

After patch, this gets rejected with:
internal:0:0-0: Error: conflicting protocols specified: ip vs ip6

Without patch, we crash with a NULL dereference: we cannot use
reject.expr->location unconditionally.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agojson: disallow empty concatenation
Pablo Neira Ayuso [Thu, 27 Mar 2025 15:32:16 +0000 (16:32 +0100)] 
json: disallow empty concatenation

commit ab4ad2e12a059b31c4e65a8900e33bc88f2f5531 upstream.

Disallow empty concatenation in set declaration in json.

 internal:0:0-0: Error: Empty concatenation
 internal:0:0-0: Error: Invalid set type.
 internal:0:0-0: Error: Parsing command array at index 1 failed.

Joint work with Florian Westphal.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agoexpression: don't try to import empty string
Florian Westphal [Thu, 27 Mar 2025 15:17:11 +0000 (16:17 +0100)] 
expression: don't try to import empty string

commit 8a9581d5d66ca2f920730441c82ac14fe9d9a427 upstream.

The bogon will trigger the assertion in mpz_import_data:
src/expression.c:418: constant_expr_alloc: Assertion `(((len) + (8) - 1) / (8)) > 0' failed.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agojson: fix error propagation when parsing binop lhs/rhs
Florian Westphal [Mon, 31 Mar 2025 12:27:47 +0000 (14:27 +0200)] 
json: fix error propagation when parsing binop lhs/rhs

commit 1b6470ab1c4eff46986e65db1b69278f13c26666 upstream.

Malformed input returns NULL when decoding left/right side of binop.
This causes a NULL dereference in expr_evaluate_binop; left/right must
point to a valid expression.

Fix this in the parser, else would have to sprinkle NULL checks all over
the evaluation code.

After fix, loading the bogon yields:
internal:0:0-0: Error: Malformed object (too many properties): '{}'.
internal:0:0-0: Error: could not decode binop rhs, '<<'.
internal:0:0-0: Error: Invalid mangle statement value
internal:0:0-0: Error: Parsing expr array at index 1 failed.
internal:0:0-0: Error: Parsing command array at index 3 failed.

Fixes: 0ac39384fd9e ("json: Accept more than two operands in binary expressions")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 days agojson: work around fuzzer-induced assert crashes
Florian Westphal [Mon, 31 Mar 2025 14:47:11 +0000 (16:47 +0200)] 
json: work around fuzzer-induced assert crashes

commit 18c24d89b9977ddc0900be64fe9e95e7ac1ce896 upstream.

fuzzer can cause assert failures due to json_pack() returning a NULL
value and therefore triggering the assert(out) in __json_pack macro.

All instances I saw are due to invalid UTF-8 strings, i.e., table/chain
names with non-text characters in them.

Work around this for now, replace the assert with a plaintext error
message and return NULL instead of abort().

Signed-off-by: Florian Westphal <fw@strlen.de>
3 days agometa: fix tc classid parsing out-of-bounds access
Florian Westphal [Wed, 13 Dec 2023 16:37:11 +0000 (17:37 +0100)] 
meta: fix tc classid parsing out-of-bounds access

commit 7008b1200fb4988b7cd7ee1c5399cae071688d50 upstream.

AddressSanitizer: heap-buffer-overflow on address 0x6020000003af ...
  #0 0x7f9a83cbb402 in tchandle_type_parse src/meta.c:89
  #1 0x7f9a83c6753f in symbol_parse src/datatype.c:138

strlen() - 1 can underflow if length was 0.

Simplify the function, there is no need to duplicate the string
while scanning it.

Expect the first strtol to stop at ':', scan for the minor number next.
The second scan is required to stop at '\0'.

Fixes: 6f2eb8548e0d ("src: meta priority support using tc classid")
Signed-off-by: Florian Westphal <fw@strlen.de>
3 days agoevaluate: don't update cache for anonymous chains
Florian Westphal [Wed, 19 Mar 2025 20:05:53 +0000 (21:05 +0100)] 
evaluate: don't update cache for anonymous chains

commit bd1fc6c740535d1ad3f38f8ba9c539c14f1732f3 upstream.

Chain lookup needs a name, not a numerical id.
After patch, loading bogon gives following errors:

Error: No symbol type information a b index 1 10.1.26.a

v2: Don't return an error, just make it a no-op (Pablo Neira Ayuso)

Fixes: c330152b7f77 ("src: support for implicit chain bindings")
Signed-off-by: Florian Westphal <fw@strlen.de>
3 days agoevaluate: reject sets with no key
Florian Westphal [Thu, 30 Nov 2023 20:29:52 +0000 (21:29 +0100)] 
evaluate: reject sets with no key

commit 09c573053ff03ad0a2eeb12c2957881648062f50 upstream.

nft --check -f tests/shell/testcases/bogons/nft-f/set_without_key
Segmentation fault (core dumped)

Fixes: 56c90a2dd2eb ("evaluate: expand sets and maps before evaluation")
Signed-off-by: Florian Westphal <fw@strlen.de>
3 days agoevaluate: fix assertion failure with malformed map definitions
Florian Westphal [Thu, 20 Mar 2025 13:33:05 +0000 (14:33 +0100)] 
evaluate: fix assertion failure with malformed map definitions

commit 7fa22984d7841a0feeaaeb0c2ed5d3cb637097e0 upstream.

Included bogon triggers:
nft: src/evaluate.c:2267: expr_evaluate_mapping: Assertion `set->data != NULL' failed.

After this fix, following errors will be shown:
Error: unqualified type invalid specified in map definition. Try "typeof expression" instead of "type datatype".
map m {
    ^
map m {
    ^
Error: map has no mapping data

Fixes: 343a51702656 ("src: store expr, not dtype to track data in sets")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 days agorule: return error if table does not exist
Florian Westphal [Thu, 20 Mar 2025 13:31:42 +0000 (14:31 +0100)] 
rule: return error if table does not exist

commit 853d3a2d3cbdc7aab16d3d33999d00b32a6db7ce upstream.

The bogon triggers segfault due to NULL dereference.  Error out and set
errno to ENOENT; caller uses strerror() in the errmsg.

After fix, loading reproducer results in:
/tmp/A:2:1-18: Error: Could not process rule: No such file or directory
list table inet p
^^^^^^^^^^^^^^^^^^

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 days agoparser_bison: add selector_expr rule to restrict typeof_expr
Pablo Neira Ayuso [Thu, 10 Apr 2025 21:23:58 +0000 (23:23 +0200)] 
parser_bison: add selector_expr rule to restrict typeof_expr

commit 8bbdcb7346788a067968e3aa62ac7e5a670b08af upstream.

typeof_expr allows for symbol, constant and bitwise expressions,
restrict it to selector expressions.

After this patch, input generated by fuzzer is rejected upfront:

 # nft -f test.nft
 test.nft:3:53-53: Error: syntax error, unexpected number
               typeof numgen inc mod 2 : ip daddr . 0
                                                    ^
 test.nft:2:12-13: Error: set definition does not specify key
       map t2 {
           ^^
 test.nft:8:65-67: Error: No such file or directory
               meta l4proto tcp dnat ip to numgen inc mod 2 map @t2
                                                                ^^^
 test.nft:8:65-67: Error: No such file or directory
               meta l4proto tcp dnat ip to numgen inc mod 2 map @t2
                                                                ^^^

Revisit 4ab1e5e60779 ("src: allow use of 'verdict' in typeof
definitions") to handle verdict as string, later a token can be added
to the scanner and enable it via flex start conditions.

Fixes: 14357cff40ed ("parser: add typeof keyword for declarations")
Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 days agoparser_bison: reject non-serializeable typeof expressions
Florian Westphal [Sun, 16 Mar 2025 21:39:10 +0000 (22:39 +0100)] 
parser_bison: reject non-serializeable typeof expressions

commit a1bb1814148c5011d50cb566a92b3b30fff118b0 upstream.

Included bogon asserts with:
BUG: unhandled key type 13
nft: src/intervals.c:73: setelem_expr_to_range: Assertion `0' failed.

This should be rejected at parser stage, but the check for udata
support was only done on the first item in a concatenation.

After fix, parser rejects this with:
Error: primary expression type 'symbol' lacks typeof serialization

Fixes: 6e48df5329ea ("src: add "typeof" build/parse/print support")
Signed-off-by: Florian Westphal <fw@strlen.de>
3 days agoevaluate: move interval flag compat check after set key evaluation
Florian Westphal [Mon, 17 Mar 2025 11:56:36 +0000 (12:56 +0100)] 
evaluate: move interval flag compat check after set key evaluation

commit 3e50cd6b063d64c2e72b0e32bc36dd5a22f75c06 upstream.

Without this, included bogon asserts with:
BUG: unhandled key type 13
nft: src/intervals.c:73: setelem_expr_to_range: Assertion `0' failed.

... because we no longer evaluate set->key/data.

Move the check to the tail of the function, right before assiging
set->existing_set, so that set->key has been evaluated.

Fixes: ceab53cee499 ("evaluate: don't allow merging interval set/map with non-interval one")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 days agoevaluate: don't allow merging interval set/map with non-interval one
Florian Westphal [Thu, 13 Mar 2025 09:38:25 +0000 (10:38 +0100)] 
evaluate: don't allow merging interval set/map with non-interval one

commit ceab53cee4999debd64ab29414b918746209ba7b upstream.

Included bogon asserts with:
BUG: invalid data expression type range_value

Pablo says: "Reject because flags interval is lacking".
Make it so.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 days agosrc: do not allow to chain more than 16 binops
Florian Westphal [Thu, 21 Dec 2023 10:25:14 +0000 (11:25 +0100)] 
src: do not allow to chain more than 16 binops

commit dcb199544563ded462cb7151134278f82a9e6cfd upstream.

netlink_linearize.c has never supported more than 16 chained binops.
Adding more is possible but overwrites the stack in
netlink_gen_bitwise().

Add a recursion counter to catch this at eval stage.

Its not enough to just abort once the counter hits
NFT_MAX_EXPR_RECURSION.

This is because there are valid test cases that exceed this.
For example, evaluation of 1 | 2 will merge the constans, so even
if there are a dozen recursive eval calls this will not end up
with large binop chain post-evaluation.

v2: allow more than 16 binops iff the evaluation function
    did constant-merging.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 days agoparser_bison: ensure all timeout policy names are released
Florian Westphal [Tue, 12 Dec 2023 12:32:24 +0000 (13:32 +0100)] 
parser_bison: ensure all timeout policy names are released

commit 86a496928420046e9d32317f09db050e8351b10e upstream.

We need to add a custom destructor for this structure, it
contains the dynamically allocated names.

a:5:55-55: Error: syntax error, unexpected '}', expecting string
policy = { estabQisheestablished : 2m3s, cd : 2m3s, }

==562373==ERROR: LeakSanitizer: detected memory leaks

Indirect leak of 160 byte(s) in 2 object(s) allocated from:
    #1 0x5a565b in xmalloc src/utils.c:31:8
    #2 0x5a565b in xzalloc src/utils.c:70:8
    #3 0x3d9352 in nft_parse_bison_filename src/libnftables.c:520:8
[..]

Fixes: c7c94802679c ("src: add ct timeout support")
Signed-off-by: Florian Westphal <fw@strlen.de>
3 days agojson: make sure timeout list is initialised
Florian Westphal [Fri, 21 Mar 2025 11:53:40 +0000 (12:53 +0100)] 
json: make sure timeout list is initialised

commit 0298bc012e020b2fca8ecc60b0091798d091e1fd upstream.

On parser error, obj_free will iterate this list.
Included json bogon crashes due to null deref because
list head initialisation did not yet happen.

Fixes: c82a26ebf7e9 ("json: Add ct timeout support")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 days agonetlink: fix stack buffer overrun when emitting ranged expressions
Florian Westphal [Fri, 14 Mar 2025 06:50:54 +0000 (07:50 +0100)] 
netlink: fix stack buffer overrun when emitting ranged expressions

commit 37dfb1972cae061c09f278933af998a7c4fc2696 upstream.

Included bogon input generates following Sanitizer splat:

AddressSanitizer: dynamic-stack-buffer-overflow on address 0x7...
WRITE of size 2 at 0x7fffffffcbe4 thread T0
    #0 0x0000003a68b8 in __asan_memset (src/nft+0x3a68b8) (BuildId: 3678ff51a5405c77e3e0492b9a985910efee73b8)
    #1 0x0000004eb603 in __mpz_export_data src/gmputil.c:108:2
    #2 0x0000004eb603 in netlink_export_pad src/netlink.c:256:2
    #3 0x0000004eb603 in netlink_gen_range src/netlink.c:471:2
    #4 0x0000004ea250 in __netlink_gen_data src/netlink.c:523:10
    #5 0x0000004e8ee3 in alloc_nftnl_setelem src/netlink.c:205:3
    #6 0x0000004d4541 in mnl_nft_setelem_batch src/mnl.c:1816:11

Problem is that the range end is emitted to the buffer at the *padded*
location (rounded up to next register size), but buffer sizing is
based of the expression length, not the padded length.

Also extend the test script: Capture stderr and if we see
AddressSanitizer warning, make it fail.

Same bug as the one fixed in 600b84631410 ("netlink: fix stack buffer overflow with sub-reg sized prefixes"),
just in a different function.

Apply same fix: no dynamic array + add a range check.

Joint work with Pablo Neira Ayuso.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 days agoevaluate: don't allow nat map with specified protocol
Florian Westphal [Thu, 20 Mar 2025 08:39:20 +0000 (09:39 +0100)] 
evaluate: don't allow nat map with specified protocol

commit 43cf4a2973ee9e3ab20edce47c6a054485707592 upstream.

Included bogon asserts:
src/netlink_linearize.c:1305: netlink_gen_nat_stmt: Assertion `stmt->nat.proto == NULL' failed.

The comment right above the assertion says:
  nat_stmt evaluation step doesn't allow
  STMT_NAT_F_CONCAT && stmt->nat.proto.

... except it does allow it.  Disable this.

Fixes: c68314dd4263 ("src: infer NAT mapping with concatenation from set")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 days agotests: shell: move flowtable with bogus priority to correct location
Florian Westphal [Thu, 8 Aug 2024 09:24:26 +0000 (11:24 +0200)] 
tests: shell: move flowtable with bogus priority to correct location

commit 156b5936b3b7a0b1ee590a02874beaba5235f758 upstream.

This is an input file to be processed by "assert_failures" script.

Fixes: b40bebbcee36 ("rule: do not crash if to-be-printed flowtable lacks priority")
Signed-off-by: Florian Westphal <fw@strlen.de>
6 weeks agonetlink: Avoid crash upon missing NFTNL_OBJ_CT_TIMEOUT_ARRAY attribute
Phil Sutter [Thu, 12 Jun 2025 18:17:22 +0000 (20:17 +0200)] 
netlink: Avoid crash upon missing NFTNL_OBJ_CT_TIMEOUT_ARRAY attribute

commit 2a38f458f12bc032dac1b3ba63f95ca5a3c03fbd upstream.

If missing, the memcpy call ends up reading from address zero.

Fixes: c7c94802679cd ("src: add ct timeout support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agonetlink: Fix for potential crash parsing a flowtable
Phil Sutter [Wed, 11 Jun 2025 11:12:56 +0000 (13:12 +0200)] 
netlink: Fix for potential crash parsing a flowtable

commit d5ef04441eb1de3efc27aa70193fe3d7f0b5c408 upstream.

Kernel's flowtable message might not contain the
NFTA_FLOWTABLE_HOOK_DEVS attribute. In that case, nftnl_flowtable_get()
will return NULL for the respective nftnl attribute.

Fixes: db0697ce7f602 ("src: support for flowtable listing")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agonetlink: Do not allocate a bogus flowtable priority expr
Phil Sutter [Wed, 11 Jun 2025 12:15:38 +0000 (14:15 +0200)] 
netlink: Do not allocate a bogus flowtable priority expr

commit 10b9a85b3278e0933bf47226588fede8c9fcbcc8 upstream.

Code accidentally treats missing NFTNL_FLOWTABLE_PRIO attribute as zero
prio value which may not be correct.

Fixes: db0697ce7f602 ("src: support for flowtable listing")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agojson: prevent null deref if chain->policy is not set
Florian Westphal [Mon, 2 Jun 2025 12:22:33 +0000 (14:22 +0200)] 
json: prevent null deref if chain->policy is not set

commit 69b90023c7220fe283ee38686c758e3494e853d9 upstream.

The two commits mentioned below resolved null dererence crashes when the
policy resp. priority keyword was missing in the chain/flowtable
specification.

Same issue exists in the json output path, so apply similar fix there
and extend the existing test cases.

Fixes: 5b37479b42b3 ("nftables: don't crash in 'list ruleset' if policy is not set")
Fixes: b40bebbcee36 ("rule: do not crash if to-be-printed flowtable lacks priority")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Phil Sutter <phil@nwl.cc>
6 weeks agooptimize: invalidate merge in case of duplicated key in set/map
Pablo Neira Ayuso [Wed, 9 Apr 2025 09:38:17 +0000 (11:38 +0200)] 
optimize: invalidate merge in case of duplicated key in set/map

commit tests/shell/testcases/optimizations/nomerge_vmap upstream.

-o/--optimize results in EEXIST error when merging two rules that lead
to ambiguous set/map, for instance:

 table ip x {
        chain v4icmp {}
        chain v4icmpc {}

        chain y {
                ip protocol icmp jump v4icmp
                ip protocol icmp goto v4icmpc
        }
 }

which is not possible because duplicated keys are not possible in
set/map. This is how it shows when running a test:

 Merging:
 testcases/sets/dumps/sets_with_ifnames.nft:56:3-30:            ip protocol icmp jump v4icmp
 testcases/sets/dumps/sets_with_ifnames.nft:57:3-31:            ip protocol icmp goto v4icmpc
 into:
       ip protocol vmap { icmp : jump v4icmp, icmp : goto v4icmpc }
 internal:0:0-0: Error: Could not process rule: File exists

Add a new step to compare rules that are candidate to be merged to
detect colissions in set/map keys in order to skip them in the next
final merging step.

Add tests/shell unit to improve coverage.

Fixes: fb298877ece2 ("src: add ruleset optimization infrastructure")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agooptimize: expand expression list when merging into concatenation
Pablo Neira Ayuso [Tue, 1 Apr 2025 16:11:45 +0000 (18:11 +0200)] 
optimize: expand expression list when merging into concatenation

commit 0d17d28bb06bf2a04862d5cd879a14bcb9a2d2dc upstream.

The following rules:

    udp dport 137 ct state new,untracked accept
    udp dport 138 ct state new,untracked accept

results in:

  nft: src/optimize.c:670: __merge_concat: Assertion `0' failed.

The logic to expand to the new,untracked list in the concatenation is
missing.

Fixes: 187c6d01d357 ("optimize: expand implicit set element when merging into concatenation")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agooptimize: compact bitmask matching in set/map
Pablo Neira Ayuso [Wed, 26 Mar 2025 20:54:06 +0000 (21:54 +0100)] 
optimize: compact bitmask matching in set/map

commit 447ac8a3e13f4706b0900d26c5c89dfcaa6773aa upstream.

Check if right hand side of relational is a bitmask, ie.

     relational
       /   \
    ...     or
           /  \
       value   or
              /  \
         value    value

then, if left hand side is a binop expression, compare left and right
hand sides (not only left hand of this binop expression) to check for
redundant matches in consecutive rules, ie.

        relational
          /   \
       and     ...
      /   \
 payload  value

before this patch, only payload in the binop expression was compared.

This allows to compact several rules matching tcp flags in a set/map, eg.

 # nft -c -o -f ruleset.nft
 Merging:
 ruleset.nft:7:17-76:                 tcp flags & (fin | syn | rst | ack | urg) == fin | ack | urg
 ruleset.nft:8:17-70:                 tcp flags & (fin | syn | rst | ack | urg) == fin | ack
 ruleset.nft:9:17-64:                 tcp flags & (fin | syn | rst | ack | urg) == fin
 ruleset.nft:10:17-70:                 tcp flags & (fin | syn | rst | ack | urg) == syn | ack
 ruleset.nft:11:17-64:                 tcp flags & (fin | syn | rst | ack | urg) == syn
 ruleset.nft:12:17-70:                 tcp flags & (fin | syn | rst | ack | urg) == rst | ack
 ruleset.nft:13:17-64:                 tcp flags & (fin | syn | rst | ack | urg) == rst
 ruleset.nft:14:17-70:                 tcp flags & (fin | syn | rst | ack | urg) == ack | urg
 ruleset.nft:15:17-64:                 tcp flags & (fin | syn | rst | ack | urg) == ack
 into:
        tcp flags & (fin | syn | rst | ack | urg) == { fin | ack | urg, fin | ack, fin, syn | ack, syn, rst | ack, rst, ack | urg, ack }
 Merging:
 ruleset.nft:17:17-61:                 tcp flags & (ack | urg) == ack jump ack_chain
 ruleset.bft:18:17-61:                 tcp flags & (ack | urg) == urg jump urg_chain
 into:
        tcp flags & (ack | urg) vmap { ack : jump ack_chain, urg : jump urg_chain }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agooptimize: incorrect comparison for reject statement
Pablo Neira Ayuso [Wed, 26 Mar 2025 20:54:04 +0000 (21:54 +0100)] 
optimize: incorrect comparison for reject statement

commit abab6e60c755aef7e1ab9d3320effa714a0b49e2 upstream.

Logic is reverse, this should returns false if the compared reject
expressions are not the same.

Fixes: 38d48fe57fff ("optimize: fix reject statement")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agoexpression: tolerate named set protocol dependency
Florian Westphal [Thu, 20 Mar 2025 08:34:45 +0000 (09:34 +0100)] 
expression: tolerate named set protocol dependency

commit b00fc8cd1379f6e403538943d55d297b624f185b upstream.

Included test will fail with:
/dev/stdin:8:38-52: Error: Transparent proxy support requires transport protocol match
   meta l4proto @protos tproxy to :1088
                        ^^^^^^^^^^^^^^^
Tolerate a set reference too.  Because the set can be empty (or there
can be removals later), add a fake 0-rhs value.

This will make pctx_update assign proto_unknown as the transport protocol
in use, Thats enough to avoid 'requires transport protocol' error.

v2: restrict it to meta lhs for now (Pablo Neira Ayuso)

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1686
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agonetlink: bogus concatenated set ranges with netlink message overrun
Pablo Neira Ayuso [Thu, 17 Apr 2025 19:40:23 +0000 (21:40 +0200)] 
netlink: bogus concatenated set ranges with netlink message overrun

commit 2fbade3cd9900fe7f87ac660b6ac44544e238206 upstream.

When building each component of the set element key, a late byteorder
switch is performed to ensure that all components in the interval are
represented in big endian, as required by the pipapo backend.

In case that the set element does not fit into the netlink message, the
byteorder switch happens twice, leading to inserting an element with a
bogus component with large sets, so instead:

      "lo" . 00:11:22:33:44:55 . 10.1.2.3 comment "123456789012345678901234567890"

listing reports:

  16777216 . 00:11:22:33:44:55 . 10.1.2.3 comment "123456789012345678901234567890"

Note that 16777216 is 0x1000000, which should instead be 0x00000001 to
represent "lo" as u32.

Fix this by switching the value in a temporary variable and use it to
set the set element key attribute in the netlink message.

Later, revisit this to perform this byteorder switch from evaluation
step.

Add tests/shell unit to cover for this bug.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1792
Fixes: 8ac2f3b2fca3 ("src: Add support for concatenated set ranges")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agosegtree: incomplete output in get element command with maps
Pablo Neira Ayuso [Thu, 6 Mar 2025 17:49:21 +0000 (18:49 +0100)] 
segtree: incomplete output in get element command with maps

commit 6db28b2d71e7f61c64338787be5d82edfdb62a21 upstream.

get element command displays an incomplete range.

Using this simple test ruleset:

 table ip x {
        map y {
                typeof ip saddr : meta mark
                counter
                flags interval,timeout
                elements = { 1.1.1.1-1.1.1.10 timeout 10m : 20, 2.2.2.2-2.2.2.5 timeout 10m : 30}
        }

then, invoking the get element command:

 # nft get element x y { 1.1.1.2 }

results in, before (incomplete output):

 table ip x {
        map y {
                type ipv4_addr : mark
                flags interval,timeout
                elements = { 1.1.1.1 counter packets 0 bytes 0 timeout 10m expires 1m24s160ms : 0x00000014 }
        }
 }

Note that it displays 1.1.1.1, instead of 1.1.1.1-1.1.1.10.

After this fix:

 table ip x {
        map y {
                type ipv4_addr : mark
                flags interval,timeout
                elements = { 1.1.1.1-1.1.1.10 counter packets 0 bytes 0 timeout 10m expires 1m24s160ms : 0x00000014 }
        }
 }

Fixes: a43cc8d53096 ("src: support for get element command")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agosegtree: fix string data initialisation
Florian Westphal [Wed, 5 Mar 2025 15:01:48 +0000 (16:01 +0100)] 
segtree: fix string data initialisation

commit 63e3d5953c144abbc4ead2665ad7cec799c4cb64 upstream.

This uses the wrong length.  This must re-use the length of the datatype,
not the string length.

The added test cases will fail without the fix due to erroneous
overlap detection, which in itself is due to incorrect sorting of
the elements.

Example error:
 netlink: Error: interval overlaps with an existing one
 add element inet testifsets simple_wild {  "2-1" } failed.
 table inet testifsets {
      ...       elements = { "1-1", "abcdef*", "othername", "ppp0" }

... but clearly "2-1" doesn't overlap with any existing members.
The false detection is because of the "acvdef*" wildcard getting sorted
at the beginning of the list which is because its erronously initialised
as a 64bit number instead of 128 bits (16 bytes / IFNAMSIZ).

Fixes: 5e393ea1fc0a ("segtree: add string "range" reversal support")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agoevaluate: release existing datatype when evaluating unary expression
Pablo Neira Ayuso [Fri, 28 Feb 2025 14:54:55 +0000 (15:54 +0100)] 
evaluate: release existing datatype when evaluating unary expression

commit 494a6ed120065b764f07acd05789b816625e8e13 upstream.

Use __datatype_set() to release the existing datatype before assigning
the new one, otherwise ASAN reports the following memleak:

Direct leak of 104 byte(s) in 1 object(s) allocated from:
    #0 0x7fbc8a2b89cf in __interceptor_malloc ../../../../src/libsa
    #1 0x7fbc898c96c2 in xmalloc src/utils.c:31
    #2 0x7fbc8971a182 in datatype_clone src/datatype.c:1406
    #3 0x7fbc89737c35 in expr_evaluate_unary src/evaluate.c:1366
    #4 0x7fbc89758ae9 in expr_evaluate src/evaluate.c:3057
    #5 0x7fbc89726bd9 in byteorder_conversion src/evaluate.c:243
    #6 0x7fbc89739ff0 in expr_evaluate_bitwise src/evaluate.c:1491
    #7 0x7fbc8973b4f8 in expr_evaluate_binop src/evaluate.c:1600
    #8 0x7fbc89758b01 in expr_evaluate src/evaluate.c:3059
    #9 0x7fbc8975ae0e in stmt_evaluate_arg src/evaluate.c:3198
    #10 0x7fbc8975c51d in stmt_evaluate_payload src/evaluate.c:330

Fixes: faa6908fad60 ("evaluate: clone unary expression datatype to deal with dynamic datatype")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agopayload: return early if dependency is not a payload expression
Florian Westphal [Tue, 25 Feb 2025 20:13:33 +0000 (21:13 +0100)] 
payload: return early if dependency is not a payload expression

commit 50f45c004adbab6a077609088becf62d2651101f upstream.

 if (dep->left->payload.base != PROTO_BASE_TRANSPORT_HDR)

is legal only after checking that ->left points to an
EXPR_PAYLOAD expression. The dependency store can also contain
EXPR_META, in this case we access a bogus part of the union.

The payload_may_dependency_kill_icmp helper can't handle a META
dep either, so return early.

Fixes: 533565244d88 ("payload: check icmp dependency before removing previous icmp expression")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agoevaluate: optimize zero length range
Pablo Neira Ayuso [Fri, 21 Feb 2025 23:32:11 +0000 (00:32 +0100)] 
evaluate: optimize zero length range

commit deda274293f80f9718de4cbb416bd2b2bf296709 upstream.

A rule like the following:

  ... tcp dport 22-22 ...

results in a range expression to match from 22 to 22.

Simplify to singleton value so a cmp is used instead.

This optimization already exists in set elements which might explain
this overlook.

Fixes: 7a6e16040d65 ("evaluate: allow for zero length ranges")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agofib: Change data type of fib oifname to "ifname"
Xiao Liang [Tue, 25 Feb 2025 10:02:17 +0000 (18:02 +0800)] 
fib: Change data type of fib oifname to "ifname"

commit 316d99246644268e5e0453afa3ade163fda21d7f upstream.

Change data type of fib oifname from "string" to "ifname", so that it
can be matched against a set of ifnames:

    set x {
            type ifname
    }
    chain y {
            fib saddr oifname @x drop
    }

Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 weeks agoevaluate: auto-merge is only available for singleton interval sets
Pablo Neira Ayuso [Thu, 20 Feb 2025 16:55:15 +0000 (17:55 +0100)] 
evaluate: auto-merge is only available for singleton interval sets

commit 65382b888e266e2e3d49a418073fd76dcc4815a7 upstream.

auto-merge is only available to interval sets with one value only,
untoggle this flag for concatenation with intervals.

Later, this can be hardened to reject it.

Fixes: 30f667920601 ("src: add 'auto-merge' option to sets")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agoparser_bison: turn redundant ip option type field match into boolean
Pablo Neira Ayuso [Fri, 31 Jan 2025 10:14:22 +0000 (11:14 +0100)] 
parser_bison: turn redundant ip option type field match into boolean

commit f9a48ce2f9c252bf74d98d10412b1f72585a45ec upstream.

The ip option expression allows for non-sense matching like:

ip option lsrr type 1

because 'lsrr' already provides the type field, this never results in a
matching.

Turn this expression into:

ip option lsrr exists

And update documentation to hide this redundant type field.

Fixes: 226a0e072d5c ("exthdr: add support for matching IPv4 options")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agoipopt: use ipv4 address datatype for address field in ip options
Pablo Neira Ayuso [Thu, 30 Jan 2025 18:39:20 +0000 (19:39 +0100)] 
ipopt: use ipv4 address datatype for address field in ip options

commit 5faccb0681acb3b0175c4190eeaecf62f0bd12d4 upstream.

So user does not have to play integer arithmetics to match on IPv4
address.

Before:

 # nft describe ip option lsrr addr
 exthdr expression, datatype integer (integer), 32 bits

After:

 # nft describe ip option lsrr addr
 exthdr expression, datatype ipv4_addr (IPv4 address) (basetype integer), 32 bits

Fixes: 226a0e072d5c ("exthdr: add support for matching IPv4 options")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agodatatype: clamp boolean value to 0 and 1
Pablo Neira Ayuso [Fri, 31 Jan 2025 11:54:32 +0000 (12:54 +0100)] 
datatype: clamp boolean value to 0 and 1

commit afb6a8e66a11178cbdbfc152c4aa9dda961b2140 upstream.

If user provides a numeric value larger than 0 or 1, match never
happens:

 # nft --debug=netlink add rule x y tcp option sack-perm 4
 ip x y
  [ exthdr load tcpopt 1b @ 4 + 0 present => reg 1 ]
  [ cmp eq reg 1 0x00000004 ]

After this update:

 # nft --debug=netlink add rule x y tcp option sack-perm 4
 ip x y
  [ exthdr load tcpopt 1b @ 4 + 0 present => reg 1 ]
  [ cmp eq reg 1 0x00000001 ]

This is to address a rare corner case, in case user specifies the
boolean value through the integer base type.

Fixes: 9fd9baba43c8 ("Introduce boolean datatype and boolean expression")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agoexthdr: incomplete type 2 routing header definition
Pablo Neira Ayuso [Tue, 28 Jan 2025 20:48:19 +0000 (21:48 +0100)] 
exthdr: incomplete type 2 routing header definition

commit c029dcb14940936dbeddc2947316c9dbc5b93656 upstream.

Add missing type 2 routing header definition.

Listing is not correct because these IPv6 extension header are still
lacking context to properly delinearize the listing, but at least this
does not crash anymore.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agosrc: add and use payload_expr_trim_force
Florian Westphal [Thu, 30 Jan 2025 17:47:13 +0000 (18:47 +0100)] 
src: add and use payload_expr_trim_force

commit 4f046ae450cbe2567022575c11dd65a9d9ea272d upstream.

Previous commit fixed erroneous handling of raw expressions when RHS sets
a zero value.

Input: @ih,58,6 set 0 @ih,86,6 set 0 @ih,170,22 set 0
Output:@ih,48,16 set @ih,48,16 & 0xffc0 @ih,80,16 set \
@ih,80,16 & 0xfc0f @ih,160,32 set @ih,160,32 & 0xffc00000

After this patch, this will instead display:

@ih,58,6 set 0x0 @ih,86,6 set 0x0 @ih,170,22 set 0x0

payload_expr_trim_force() only works when the payload has no known
protocol (template) attached, i.e. will be printed as raw payload syntax.

It performs sanity checks on @mask and then adjusts the payload expression
length and offset according to the mask.

Also add this check in __binop_postprocess() so we can also discard masks
when matching, e.g.

'@ih,7,5 2' becomes '@ih,7,5 0x2', not '@ih,0,16 & 0xffc0 == 0x20'.

binop_postprocess now returns if it performed an action or not; if this
returns true then arguments might have been freed so callers must no longer
refer to any of the expressions attached to the binop.

Next patch adds test cases for this.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agonetlink_delinarize: fix bogus munging of mask value
Florian Westphal [Thu, 30 Jan 2025 17:47:12 +0000 (18:47 +0100)] 
netlink_delinarize: fix bogus munging of mask value

commit 44c803015a1e0bca54fb7b92fdc154d162f9dbfd upstream.

Given following input:
table ip t {
 chain c {
  @ih,58,6 set 0 @ih,86,6 set 0 @ih,170,22 set 0
 }
}

nft will produce following output:
chain c {
 @ih,48,16 set @ih,48,16 & 0x3f @ih,80,16 set @ih,80,16 & 0x3f0 @ih,160,32 set @ih,160,32 & 0x3fffff
}

The input side is correct, the generated expressions sent to kernel are:

1  [ payload load 2b @ inner header + 6 => reg 1 ]
2  [ bitwise reg 1 = ( reg 1 & 0x0000c0ff ) ^ 0x00000000 ]
3  [ payload write reg 1 => 2b @ inner header + 6 .. ]
4  [ payload load 2b @ inner header + 10 => reg 1 ]
5  [ bitwise reg 1 = ( reg 1 & 0x00000ffc ) ^ 0x00000000 ]
6  [ payload write reg 1 => 2b @ inner header + 10 .. ]
7  [ payload load 4b @ inner header + 20 => reg 1 ]
8  [ bitwise reg 1 = ( reg 1 & 0x0000c0ff ) ^ 0x00000000 ]
9  [ payload write reg 1 => 4b @ inner header + 20 .. ]

@ih,58,6 set 0 <- Zero 6 bits, starting with bit 58

Changes to inner header mandate a checksum update, which only works for
even byte counts (except for last byte in the payload).

Thus, we load 2b at offet 6. (16bits, offset 48).

Because we want to zero 6 bits, we need a mask that retains 10 bits and
clears 6: b1111111111000000 (first 8 bit retains 48-57, last 6 bit clear
58-63).  The '0xc0ff' is not correct, but thats because debug output comes
from libnftnl which prints values in host byte order, the value will be
interpreted as big endian on kernel side, so this will do the right thing.

Next, same problem:

@ih,86,6 set 0 <- Zero 6 bits, starting with bit 86.

nft needs to round down to even-sized byte offset, 10, then retain first
6 bits (80 + 6 == 86), then clear 6 bits (86-91), then keep 4 more as-is
(92-95).

So mask is 0xfc0f (in big endian) would be correct (b1111110000001111).

Last expression, @ih,170,22 set 0, asks to clear 22 bits starting with bit
170, nft correctly rounds this down to a 32 bit read at offset 160.

Required mask keeps first 10 bits, then clears 22
(b11111111110000000000000000000000).  Required mask would be 0xffc00000,
which corresponds to the wrong-endian-printed value in line 8 above.

Now that we convinced ourselves that the input side is correct, fix up
netlink delinearize to undo the mask alterations if we can't find a
template to print a human-readable payload expression.

With this patch, we get this output:

  @ih,48,16 set @ih,48,16 & 0xffc0 @ih,80,16 set @ih,80,16 & 0xfc0f @ih,160,32 set @ih,160,32 & 0xffc00000

... which isn't ideal.  We should fixup the payload expression to display
the same output as the input, i.e. adjust payload->len and offset as per
mask and discard the mask instead.

This will be done in a followup patch.

Fixes: 50ca788ca4d0 ("netlink: decode payload statment")
Reported-by: Sunny73Cr <Sunny73Cr@protonmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agoscanner: better error reporting for CRLF line terminators
Pablo Neira Ayuso [Mon, 6 Jan 2025 23:00:50 +0000 (00:00 +0100)] 
scanner: better error reporting for CRLF line terminators

commit 8c35615297983227cac1437edbe0cdedf4c2227b upstream.

Provide a hint to users that file is coming with CRLF line terminators,
maybe from a non-Linux OS.

Extend scanner.l to provide hint on CRLF in files:

 # file test.nft
 test.nft: ASCII text, with CRLF, LF line terminators
 # nft -f test.nft
 test.nft:1:13-14: Error: syntax error, unexpected CRLF line terminators
 table ip x {
             ^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agorule: make cmd_free(NULL) valid
Florian Westphal [Wed, 8 Jan 2025 11:30:15 +0000 (12:30 +0100)] 
rule: make cmd_free(NULL) valid

commit 581e051ae26b503484b7634b8799a9b9b531e95d upstream.

bison uses cmd_free($$) as destructor, but base_cmd can
set it to NULL, e.g.

  |       ELEMENT         set_spec        set_block_expr
  {
    if (nft_cmd_collapse_elems(CMD_ADD, state->cmds, &$2, $3)) {
       handle_free(&$2);
       expr_free($3);
       $$ = NULL;   // cmd set to NULL
       break;
    }
    $$ = cmd_alloc(CMD_ADD, CMD_OBJ_ELEMENTS, &$2, &@$, $3);

expr_free(NULL) is legal, cmd_free() causes crash.  So just allow
this to avoid cluttering parser_bison.y with "if ($$)".

Also add the afl-generated bogon input to the test files.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agointervals: set internal element location with the deletion trigger
Pablo Neira Ayuso [Wed, 4 Dec 2024 22:36:05 +0000 (23:36 +0100)] 
intervals: set internal element location with the deletion trigger

commit 93077e35accccd8cc056b67f70bfb3182c819fd4 upstream.

set location of internal elements (already in the kernel) to the one
that partial or fully deletes it.

Otherwise, error reporting refers to internal location.

Before this patch:

 # nft delete element x y { 1.1.1.3 }
 Error: Could not process rule: Too many open files in system
 delete element x y { 1.1.1.3 }
                      ^^^^^^^

After this patch:

 # nft delete element x y { 1.1.1.3 }
 Error: Could not process rule: Too many open files in system
 delete element x y { 1.1.1.3 }
                      ^^^^^^^

This occurs after splitting an existing interval in two:

 remove: [1010100-10101ff]
 add: [1010100-1010102]
 add: [1010104-10101ff]

which results in two additions after removing the existing interval
that is split.

Fixes: 81e36530fcac ("src: replace interval segment tree overlap and automerge")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoevaluate: reset statement length context before evaluating statement
Pablo Neira Ayuso [Wed, 6 Dec 2023 17:48:29 +0000 (18:48 +0100)] 
evaluate: reset statement length context before evaluating statement

commit 8d3de823b622136e1d05a6fed11ff2dc0e804f8a upstream.

This patch consolidates ctx->stmt_len reset in stmt_evaluate() to avoid
this problem. Note that stmt_evaluate_meta() and stmt_evaluate_ct()
already reset it after the statement evaluation.

Moreover, statement dependency can be generated while evaluating a meta
and ct statement. Payload statement dependency already manually stashes
this before calling stmt_evaluate(). Add a new stmt_dependency_evaluate()
function to stash statement length context when evaluating a new statement
dependency and use it for all of the existing statement dependencies.

Florian also says:

'meta mark set vlan id map { 1 : 0x00000001, 4095 : 0x00004095 }' will
crash. Reason is that the l2 dependency generated here is errounously
expanded to a 32bit-one, so the evaluation path won't recognize this
as a L2 dependency.  Therefore, pctx->stacked_ll_count is 0 and
__expr_evaluate_payload() crashes with a null deref when
dereferencing pctx->stacked_ll[0].

nft-test.py gains a fugly hack to tolerate '!map typeof vlan id : meta mark'.
For more generic support we should find something more acceptable, e.g.

!map typeof( everything here is a key or data ) timeout ...

tests/py update and assert(pctx->stacked_ll_count) by Florian Westphal.

Fixes: edecd58755a8 ("evaluate: support shifts larger than the width of the left operand")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 months agooptimize: compare expression length
Pablo Neira Ayuso [Mon, 18 Nov 2024 11:44:06 +0000 (12:44 +0100)] 
optimize: compare expression length

commit bc0311378285d41850e3508df905d75959ba4239 upstream.

do not merge raw payload expressions with different length.

Other expression rely on key comparison which is assumed to have the
same length already.

Fixes: 60dcc01d6351 ("optimize: add __expr_cmp()")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agosrc: allow to map key to nfqueue number
Florian Westphal [Fri, 25 Oct 2024 07:47:25 +0000 (09:47 +0200)] 
src: allow to map key to nfqueue number

commit 058246016188c8418cae1b3db70b16b935b1fe7c upstream.

Allow to specify a numeric queue id as part of a map.
The parser side is easy, but the reverse direction (listing) is not.

'queue' is a statement, it doesn't have an expression.

Add a generic 'queue_type' datatype as a shim to the real basetype with
constant expressions, this is used only for udata build/parse, it stores
the "key" (the parser token, here "queue") as udata in kernel and can
then restore the original key.

Add a dumpfile to validate parser & output.

JSON support is missing because JSON allow typeof only since quite
recently.

Joint work with Pablo.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1455
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agolibnftables-json: fix raw payload expression documentation
Eric Long [Thu, 17 Oct 2024 15:33:17 +0000 (23:33 +0800)] 
libnftables-json: fix raw payload expression documentation

commit 570320ab9a0752c7749a6c9cc85b34a5e7ab91b5 upstream.

Raw payload expression accesses payload data in bits, not bytes.

Fixes: 872f373dc50f7 ("doc: Add JSON schema documentation")
Signed-off-by: Eric Long <i@hack3r.moe>
Signed-off-by: Phil Sutter <phil@nwl.cc>
6 months agojson: Support typeof in set and map types
Phil Sutter [Fri, 27 Sep 2024 22:55:34 +0000 (00:55 +0200)] 
json: Support typeof in set and map types

commit bb6312484af93a83a9ec8716f3887a43566a775a upstream.

Implement this as a special "type" property value which is an object
with sole property "typeof". The latter's value is the JSON
representation of the expression in set->key, so for concatenated
typeofs it is a concat expression.

All this is a bit clumsy right now but it works and it should be
possible to tear it down a bit for more user-friendliness in a
compatible way by either replacing the concat expression by the array it
contains or even the whole "typeof" object - the parser would just
assume any object (or objects in an array) in the "type" property value
are expressions to extract a type from.

Signed-off-by: Phil Sutter <phil@nwl.cc>
6 months agocache: initialize filter when fetching implicit chains
Pablo Neira Ayuso [Tue, 17 Sep 2024 17:18:09 +0000 (19:18 +0200)] 
cache: initialize filter when fetching implicit chains

commit e3d2a5e852ceea587bfff5878e6e5c569f15116a upstream.

ASAN reports:

  src/cache.c:734:25: runtime error: load of value 189, which is not a valid value for type '_Bool'

because filter->reset.rule remains uninitialized.

Initialize filter and replace existing construct to initialize table and
chain which leaves remaining fields uninitialized.

Fixes: dbff26bfba83 ("cache: consolidate reset command")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoproto: use NFT_PAYLOAD_L4CSUM_PSEUDOHDR flag to mangle UDP checksum
Pablo Neira Ayuso [Mon, 9 Sep 2024 10:48:33 +0000 (12:48 +0200)] 
proto: use NFT_PAYLOAD_L4CSUM_PSEUDOHDR flag to mangle UDP checksum

commit f89abfb4068d31f7279cae298abf25e0c077d2d3 upstream.

There are two mechanisms to update the UDP checksum field:

 1) _CSUM_TYPE and _CSUM_OFFSET which specify the type of checksum
    (e.g. inet) and offset where it is located.
 2) use NFT_PAYLOAD_L4CSUM_PSEUDOHDR flag to use layer 4 kernel
    protocol parser.

The problem with 1) is that it is inconditional, that is, csum_type and
csum_offset cannot deal with zero UDP checksum.

Use NFT_PAYLOAD_L4CSUM_PSEUDOHDR flag instead since it relies on the
layer 4 kernel parser which skips updating zero UDP checksum.

Extend test coverage for the UDP mangling with and without zero
checksum.

Fixes: e6c9174e13b2 ("proto: add checksum key information to struct proto_desc")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agolibnftables: Zero ctx->vars after freeing it
Phil Sutter [Tue, 3 Sep 2024 15:43:19 +0000 (17:43 +0200)] 
libnftables: Zero ctx->vars after freeing it

commit d361be1f8734461e27117f6c569acf2189fcf81e upstream.

Leaving the invalid pointer value in place will cause a double-free when
users call nft_ctx_clear_vars() first, then nft_ctx_free(). Moreover,
nft_ctx_add_var() passes the pointer to mrealloc() and thus assumes it
to be either NULL or valid.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1772
Fixes: 9edaa6a51eab4 ("src: add --define key=value")
Signed-off-by: Phil Sutter <phil@nwl.cc>
6 months agocache: position does not require full cache
Pablo Neira Ayuso [Thu, 29 Aug 2024 10:42:17 +0000 (12:42 +0200)] 
cache: position does not require full cache

commit d414f756af9d638fe0c0002b2df31c8c17a15002 upstream.

position refers to the rule handle, it has similar cache requirements as
replace rule command, relax cache requirements.

Commit e5382c0d08e3 ("src: Support intra-transaction rule references")
uses position.id for index support which requires a full cache, but
only in such case.

Fixes: 01e5c6f0ed03 ("src: add cache level flags")
Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: relax requirement for replace rule command
Pablo Neira Ayuso [Thu, 29 Aug 2024 10:42:14 +0000 (12:42 +0200)] 
cache: relax requirement for replace rule command

commit 4984da8cc427974ea63796fa60a791b714a71440 upstream.

No need for full cache, this command relies on the rule handle which is
not validated from userspace. Cache requirements are similar to those
of add/create/delete rule commands.

This speeds up incremental updates with large rulesets.

Extend tests/coverage for rule replacement.

Fixes: 01e5c6f0ed03 ("src: add cache level flags")
Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: remove full cache requirement when echo flag is set on
Pablo Neira Ayuso [Thu, 29 Aug 2024 10:42:12 +0000 (12:42 +0200)] 
cache: remove full cache requirement when echo flag is set on

commit 53a503ad4a1abfa0374b3d12e884b69dc6df4b4f upstream.

The echo flag does not use the cache infrastructure yet, it relies on
the monitor cache which follows the netlink_echo_callback() path.

Fixes: 01e5c6f0ed03 ("src: add cache level flags")
Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: clean up evaluate_cache_del()
Pablo Neira Ayuso [Thu, 29 Aug 2024 10:42:10 +0000 (12:42 +0200)] 
cache: clean up evaluate_cache_del()

commit 19702ae3d5da18fef64248f95df471c6664dd08e upstream.

Move NFT_CACHE_TABLE flag to default case to disentangle this.

Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: assert filter when calling nft_cache_evaluate()
Pablo Neira Ayuso [Thu, 29 Aug 2024 10:42:08 +0000 (12:42 +0200)] 
cache: assert filter when calling nft_cache_evaluate()

commit 4dd20f3bbd606eed4869ebe449debee8b2ac7900 upstream.

nft_cache_evaluate() always takes a non-null filter, remove superfluous
checks when calculating cache requirements via flags.

Note that filter is still option from netlink dump path, since this can
be called from error path to provide hints.

Fixes: 08725a9dc14c ("cache: filter out rules by chain")
Fixes: b3ed8fd8c9f3 ("cache: missing family in cache filtering")
Fixes: 635ee1cad8aa ("cache: filter out sets and maps that are not requested")
Fixes: 3f1d3912c3a6 ("cache: filter out tables that are not requested")
Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: only dump rules for the given table
Pablo Neira Ayuso [Sun, 25 Aug 2024 22:41:40 +0000 (00:41 +0200)] 
cache: only dump rules for the given table

commit ebd06f85a3257c294572005d0fa6b8ab0f213486 upstream.

Only family is set on in the dump request, set on table and chain
otherwise, rules for the given family are fetched for each existing
table.

Fixes: afbd102211dc ("src: do not use the nft_cache_filter object from mnl.c")
Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: accumulate flags in batch
Pablo Neira Ayuso [Mon, 26 Aug 2024 08:19:39 +0000 (10:19 +0200)] 
cache: accumulate flags in batch

commit 68c8fb5f7c988a38a694c77c65e789e0cb8dfd8a upstream.

Recent updates are relaxing cache requirements:

  babc6ee8773c ("cache: populate chains on demand from error path")

Flags describe cache requirements for a given batch, accumulate flags
that are inferred from commands in this batch.

Fixes: 7df42800cf89 ("src: single cache_update() call to build cache before evaluation")
Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: reset filter for each command
Pablo Neira Ayuso [Mon, 26 Aug 2024 08:18:34 +0000 (10:18 +0200)] 
cache: reset filter for each command

commit 29cb49d0ca92b840938823dec697d8c5488d7253 upstream.

Inconditionally reset filter for each command in the batch, this is safer.

Fixes: 3f1d3912c3a6 ("cache: filter out tables that are not requested")
Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoparser_json: fix crash in json_parse_set_stmt_list
Sebastian Walz (sivizius) [Mon, 19 Aug 2024 22:09:26 +0000 (00:09 +0200)] 
parser_json: fix crash in json_parse_set_stmt_list

commit 26d9cbefb10e6bc3765df7e9e7a4fc3b951a80f3 upstream.

Due to missing `NULL`-check, there will be a segfault for invalid statements.

Fixes: 07958ec53830 ("json: add set statement list support")
Signed-off-by: Sebastian Walz (sivizius) <sebastian.walz@secunet.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoparser_json: fix handle memleak from error path
Pablo Neira Ayuso [Mon, 19 Aug 2024 19:34:49 +0000 (21:34 +0200)] 
parser_json: fix handle memleak from error path

commit 47e18c0eba51a538e1110322d1a9248b0501d7c8 upstream.

Based on patch from Sebastian Walz.

Fixes: 586ad210368b ("libnftables: Implement JSON parser")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoparser_json: fix several expression memleaks from error path
Sebastian Walz (sivizius) [Mon, 19 Aug 2024 18:11:44 +0000 (20:11 +0200)] 
parser_json: fix several expression memleaks from error path

commit bae7b4d283826efbeb28c21aecd7b355e86da170 upstream.

Fixes: 586ad210368b ("libnftables: Implement JSON parser")
Signed-off-by: Sebastian Walz (sivizius) <sebastian.walz@secunet.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoparser_json: release buffer returned by json_dumps
Sebastian Walz (sivizius) [Mon, 19 Aug 2024 17:58:14 +0000 (19:58 +0200)] 
parser_json: release buffer returned by json_dumps

commit 46700fbdbbbaab0d7db716fce3a438334c58ac9e upstream.

The signature of `json_dumps` is:

`char *json_dumps(const json_t *json, size_t flags)`:

It will return a pointer to an owned string, the caller must free it.
However, `json_error` just borrows the string to format it as `%s`, but
after printing the formatted error message, the pointer to the string is
lost and thus never freed.

Fixes: 586ad210368b ("libnftables: Implement JSON parser")
Signed-off-by: Sebastian Walz (sivizius) <sebastian.walz@secunet.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agodoc: update outdated route and pkttype info
谢致邦 (XIE Zhibang) [Tue, 20 Aug 2024 09:15:03 +0000 (09:15 +0000)] 
doc: update outdated route and pkttype info

commit 5089d0f46676aa13ab679f6e4820a08957f2e7e6 upstream.

inet family supports route type.
unicast pkttype changed to host pkttype.

Signed-off-by: 谢致邦 (XIE Zhibang) <Yeking@Red54.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 months agoparser_bison: allow 0 burst in limit rate byte mode
Pablo Neira Ayuso [Thu, 15 Aug 2024 11:56:21 +0000 (13:56 +0200)] 
parser_bison: allow 0 burst in limit rate byte mode

commit cea05ae5bdc50949d4c734796d6db5717187055a upstream.

Unbreak restoring elements in set with rate limit that fail with:

> /dev/stdin:3618:61-61: Error: limit burst must be > 0
>                  elements = { 1.2.3.4 limit rate over 1000 kbytes/second timeout 1s,

no need for burst != 0 for limit rate byte mode.

Add tests/shell too.

Fixes: 702eff5b5b74 ("src: allow burst 0 for byte ratelimit and use it as default")
Fixes: 285baccfea46 ("src: disallow burst 0 in ratelimits")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: do not fetch set inconditionally on delete
Pablo Neira Ayuso [Thu, 15 Aug 2024 10:47:54 +0000 (12:47 +0200)] 
cache: do not fetch set inconditionally on delete

commit ba13acf4be081129d5c943db9f607a13954be5f6 upstream.

This is only required to remove elements, relax cache requirements for
anything else.

Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: populate flowtables on demand from error path
Pablo Neira Ayuso [Thu, 15 Aug 2024 10:34:17 +0000 (12:34 +0200)] 
cache: populate flowtables on demand from error path

commit 52d99078521f0ae245ad0145348bebdba9f665ab upstream.

Flowtables are only required for error reporting hints if kernel reports
ENOENT. Populate the cache from this error path only.

Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: populate objects on demand from error path
Pablo Neira Ayuso [Thu, 15 Aug 2024 10:34:13 +0000 (12:34 +0200)] 
cache: populate objects on demand from error path

commit aab2fe87a665c0cba2676096b49b5c8ea21910f8 upstream.

Objects are only required for error reporting hints if kernel reports
ENOENT. Populate the cache from this error path only.

Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: populate chains on demand from error path
Pablo Neira Ayuso [Thu, 15 Aug 2024 10:34:11 +0000 (12:34 +0200)] 
cache: populate chains on demand from error path

commit babc6ee8773cfeed78167f78827b35e3141e04c6 upstream.

Updates on verdict maps that require many non-base chains are slowed
down due to fetching existing non-base chains into the cache.

Chains are only required for error reporting hints if kernel reports
ENOENT. Populate the cache from this error path only.

Similar approach already exists from rule ENOENT error path since:

  deb7c5927fad ("cmd: add misspelling suggestions for rule commands")

however, NFT_CACHE_CHAIN was toggled inconditionally for rule
commands, rendering this on-demand cache population useless.

before this patch, running Neels' nft_slew benchmark (peak values):

  created idx 4992 in 52587950 ns   (128 in 7122 ms)
  ...
  deleted idx  128 in 43542500 ns   (127 in 6187 ms)

after this patch:

  created idx 4992 in 11361299 ns   (128 in 1612 ms)
  ...
  deleted idx 1664 in  5239633 ns   (128 in 733 ms)

Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocmd: provide better hint if chain is already declared with different type/hook/priority
Pablo Neira Ayuso [Mon, 10 Jun 2024 17:36:21 +0000 (19:36 +0200)] 
cmd: provide better hint if chain is already declared with different type/hook/priority

commit 1f321f86c45fce88a5bcd6f8eafa0157248c8b38 upstream.

Display the following error in such case:

  ruleset.nft:7:9-52: Error: Chain "input" already exists in table ip 'filter' with different declaration
          type filter hook postrouting priority filter;
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

instead of reporting a misleading unsupported chain type when updating
an existing chain with different type/hook/priority.

Fixes: 573788e05363 ("src: improve error reporting for unsupported chain type")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agometa: stash context statement length when generating payload/meta dependency
Pablo Neira Ayuso [Tue, 18 Jul 2023 21:10:01 +0000 (23:10 +0200)] 
meta: stash context statement length when generating payload/meta dependency

commit 5f1676ac9f1aeb36d7695c3c354dade013a1e4f3 upstream.

... meta mark set ip dscp

generates an implicit dependency from the inet family to match on meta
nfproto ip.

The length of this implicit expression is incorrectly adjusted to the
statement length, ie. relational to compare meta nfproto takes 4 bytes
instead of 1 byte. The evaluation of 'ip dscp' under the meta mark
statement triggers this implicit dependency which should not consider
the context statement length since it is added before the statement
itself.

This problem shows when listing the ruleset, since netlink_parse_cmp()
where left->len < right->len, hence handling the implicit dependency as
a concatenation, but it is actually a bug in the evaluation step that
leads to incorrect bytecode.

Fixes: 3c64ea7995cb ("evaluate: honor statement length in integer evaluation")
Fixes: edecd58755a8 ("evaluate: support shifts larger than the width of the left operand")
Tested-by: Brian Davidson <davidson.brian@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agonetlink_linearize: use div_round_up in byteorder length
Pablo Neira Ayuso [Thu, 6 Jul 2023 08:26:39 +0000 (10:26 +0200)] 
netlink_linearize: use div_round_up in byteorder length

commit 25e7b99cc450490c38becb03d8bddd0199cfd3f9 upstream.

Use div_round_up() to calculate the byteorder length, otherwise fields
that take % BITS_PER_BYTE != 0 are not considered by the byteorder
expression.

Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoevaluate: honor statement length in integer evaluation
Pablo Neira Ayuso [Thu, 23 Mar 2023 11:52:39 +0000 (12:52 +0100)] 
evaluate: honor statement length in integer evaluation

commit 3c64ea7995cbbc4f1d9d7707f907667325eb62b9 upstream.

Otherwise, bogus error is reported:

 # nft --debug=netlink add rule ip x y 'ct mark set ip dscp & 0x0f << 1 | 0xff000000'
 Error: Value 4278190080 exceeds valid range 0-63
 add rule ip x y ct mark set ip dscp & 0x0f << 1 | 0xff000000
                                                   ^^^^^^^^^^

Use the statement length as the maximum value in the mark statement
expression.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agosrc: improve error reporting for unsupported chain type
Pablo Neira Ayuso [Fri, 10 Mar 2023 18:20:50 +0000 (19:20 +0100)] 
src: improve error reporting for unsupported chain type

commit 573788e053631a5c069f887caed7c62d521b022d upstream.

8c75d3a16960 ("Reject invalid chain priority values in user space")
provides error reporting from the evaluation phase. Instead, this patch
infers the error after the kernel reports EOPNOTSUPP.

test.nft:3:28-40: Error: Chains of type "nat" must have a priority value above -200
                type nat hook prerouting priority -300;
                                         ^^^^^^^^^^^^^

This patch also adds another common issue for users compiling their own
kernels if they forget to enable CONFIG_NFT_NAT in their .config file.

Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agocache: rule by index requires full cache
Pablo Neira Ayuso [Thu, 15 Aug 2024 10:34:08 +0000 (12:34 +0200)] 
cache: rule by index requires full cache

commit 161beaeacd2e5218d66febc3db825bf6a27119c5 upstream.

In preparation for on-demand cache population with errors, set on
NFT_CACHE_FULL if rule index is used since this requires a full cache
with rules.

This is not a fix, index is already fetching a full cache before this
patch.

But follow up patches relax cache requirements, so add this patch in
first place to make sure index does not break.

Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agodatatype: improve error reporting when time unit is not correct
Pablo Neira Ayuso [Wed, 14 Aug 2024 11:05:54 +0000 (13:05 +0200)] 
datatype: improve error reporting when time unit is not correct

commit 6bcaef6a1ea6dc60250ed6124f3b49a8cd29434c upstream.

Display:

  Wrong unit format, expecting bytes or kbytes or mbytes

instead of:

  Wrong rate format

Fixes: 6615676d825e ("src: add per-bytes limit")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agodatatype: reject rate in quota statement
Pablo Neira Ayuso [Wed, 14 Aug 2024 11:02:02 +0000 (13:02 +0200)] 
datatype: reject rate in quota statement

commit 8ed6fa6d66b2df50d118423c1cb0e98cdd45cdbd upstream.

Bail out if rate are used:

 ruleset.nft:5:77-106: Error: Wrong rate format, expecting bytes or kbytes or mbytes
 add rule netdev firewall PROTECTED_IPS update @quota_temp_before { ip daddr quota over 45000 mbytes/second } add @quota_trigger { ip daddr }
                                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

improve error reporting while at this.

Fixes: 6615676d825e ("src: add per-bytes limit")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agooptimize: skip variables in nat statements
Pablo Neira Ayuso [Thu, 18 Jul 2024 16:06:22 +0000 (18:06 +0200)] 
optimize: skip variables in nat statements

commit bc1f910f502701f1a1d28c7bd723e4be3bac1d8c upstream.

Do not hit assert():

  nft: optimize.c:486: rule_build_stmt_matrix_stmts: Assertion `k >= 0' failed.

variables are not supported by -o/--optimize at this stage.

Fixes: 9be404a153bc ("optimize: ignore existing nat mapping")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agolibnftables: skip useable checks for /dev/stdin
Pablo Neira Ayuso [Tue, 9 Jul 2024 14:59:53 +0000 (16:59 +0200)] 
libnftables: skip useable checks for /dev/stdin

commit 477fd8218777b75bdfa3a5643f692adae4f002fe upstream.

/dev/stdin is a placeholder, read() from STDIN_FILENO is used to fetch
the standard input into a buffer.

Since 5c2b2b0a2ba7 ("src: error reporting with -f and read from stdin")
stdin is stored in a buffer to fix error reporting.

This patch requires: ("parser_json: use stdin buffer if available")

Fixes: 149b1c95d129 ("libnftables: refuse to open onput files other than named pipes or regular files")
Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoparser_json: use stdin buffer if available
Pablo Neira Ayuso [Tue, 9 Jul 2024 14:59:52 +0000 (16:59 +0200)] 
parser_json: use stdin buffer if available

commit e48f32701ff65d522c2f29f34bf4f3ce8e562057 upstream.

Since 5c2b2b0a2ba7 ("src: error reporting with -f and read from stdin")
stdin is stored in a buffer, update json support to use it instead of
reading from /dev/stdin.

Some systems do not provide /dev/stdin symlink to /proc/self/fd/0
according to reporter (that mentions Yocto Linux as example).

Fixes: 935f82e7dd49 ("Support 'nft -f -' to read from stdin")
Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agooptimize: clone counter before insertion into set element
Pablo Neira Ayuso [Fri, 5 Jul 2024 12:03:33 +0000 (14:03 +0200)] 
optimize: clone counter before insertion into set element

commit ac77f3805c71f14c51730a9c5cb726ee67f14159 upstream.

The counter statement that is zapped from the rule needs to be cloned
before inserting it into each set element.

Fixes: 686ab8b6996e ("optimize: do not remove counter in verdict maps")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agoparser_bison: recursive table declaration in deprecated meter statement
Pablo Neira Ayuso [Tue, 2 Jul 2024 22:08:01 +0000 (00:08 +0200)] 
parser_bison: recursive table declaration in deprecated meter statement

commit a70a217079ef83482fc093d8549f8cdeaeaa3cae upstream.

This is allowing for recursive table NAME declarations such as:

 ... table xyz1 table xyz2 { ... }

remove it.

Fixes: 3ed5e31f4a32 ("src: add flow statement")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 months agointervals: fix element deletions with maps
Pablo Neira Ayuso [Wed, 3 Jul 2024 14:29:26 +0000 (16:29 +0200)] 
intervals: fix element deletions with maps

commit 551a4ad68b922fa6c942f5e79ac59f723a12e233 upstream.

Set element deletion in maps (including catchall elements) does not work.

 # nft delete element ip x m { \* }
 BUG: invalid range expression type catch-all set element
 nft: src/expression.c:1472: range_expr_value_low: Assertion `0' failed.
 Aborted

Call interval_expr_key() to fetch expr->left in the mapping but use the
expression that represents the mapping because it provides access to the
EXPR_F_REMOVE flags.

Moreover, assume maximum value for catchall expression by means of the
expr->len to reuse the existing code to check if the element to be
deleted really exists.

Fixes: 3e8d934e4f72 ("intervals: support to partial deletion with automerge")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>