]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
7 weeks agosrc: BASECHAIN flag no longer implies presence of priority expression
Florian Westphal [Thu, 12 Jun 2025 18:17:15 +0000 (20:17 +0200)] 
src: BASECHAIN flag no longer implies presence of priority expression

commit 44ea1936463728475768861073ca4ba34a5c2f75 upstream.

The included bogon will crash nft because print side assumes that BASECHAIN
flag presence also means that priority expression is available.

Make the print side conditional.

Fixes: a66b5ad9540d ("src: allow for updating devices on existing netdev chain")
Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: restrict allowed subtypes of concatenations
Florian Westphal [Fri, 6 Jun 2025 12:12:37 +0000 (14:12 +0200)] 
evaluate: restrict allowed subtypes of concatenations

commit 3cec07327ea2b91ac8395e0c0ee2a635a5e9fcd5 upstream.

We need to restrict this, included bogon asserts with:
BUG: unknown expression type prefix
nft: src/netlink_linearize.c:940: netlink_gen_expr: Assertion `0' failed.

Prefix expressions are only allowed if the concatenation is used within
a set element, not when specifying the lookup key.

For the former, anything that represents a value is allowed.
For the latter, only what will generate data (fill a register) is
permitted.

At this time we do not have an annotation that tells if the expression
is on the left hand side (lookup key) or right hand side (set element).

Add a new list recursion counter for this. If its 0 then we're building
the lookup key, if its the latter the concatenation is the RHS part
of a relational expression and prefix, ranges and so on are allowed.

IOW, we don't really need a recursion counter, another type of annotation
that would tell if the expression is placed on the left or right hand side
of another expression would work too.

v2: explicitly list all 'illegal' expression types instead of
using a default label for them.

This will raise a compiler warning to remind us to adjust the case
labels in case a new expression type gets added in the future.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: rename recursion counter to recursion.binop
Florian Westphal [Fri, 6 Jun 2025 12:12:36 +0000 (14:12 +0200)] 
evaluate: rename recursion counter to recursion.binop

commit 10b44319a53a131ed943e2b6eeb62d197178bf4d upstream.

The existing recursion counter is used by the binop expression to detect
if we've completely followed all the binops.

We can only chain up to NFT_MAX_EXPR_RECURSION binops, but the evaluation
step can perform constant-folding, so we must recurse until we found the
rightmost (last) binop in the chain.

Then we can check the post-eval chain to see if it is something that can
be serialized later (i.e., if we are within the NFT_MAX_EXPR_RECURSION
after constant folding) or not.

Thus we can't reuse the existing ctx->recursion counter for other
expressions; entering the initial expr_evaluate_binop with
ctx->recursion > 0 would break things.

Therefore rename this to an embedded structure.
This allows us to add a new recursion counter in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agotest: shell: Don't use system nft binary
Yi Chen [Sun, 22 Jun 2025 12:55:51 +0000 (20:55 +0800)] 
test: shell: Don't use system nft binary

commit c73eadca05c781ebad631331a6864fa8c54a5024 upstream.

Use the defined $NFT variable instead of calling the system nft binary directly.
Add a nat_ftp.nodump file to avoid the following check-tree.sh error:
ERR: "tests/shell/testcases/packetpath/nat_ftp" has no "tests/shell/testcases/packetpath/dumps/nat_ftp.{nft,nodump}" file.

Signed-off-by: Yi Chen <yiche@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: don't BUG on unexpected base datatype
Florian Westphal [Fri, 13 Jun 2025 14:46:06 +0000 (16:46 +0200)] 
evaluate: don't BUG on unexpected base datatype

commit 845b8d7208077310e77560a64b698973fb047ef2 upstream.

Included bogon will cause a crash but this is the evaluation stage where
we can just emit an error instead.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agojson: Dump flowtable hook spec only if present
Phil Sutter [Wed, 11 Jun 2025 14:45:48 +0000 (16:45 +0200)] 
json: Dump flowtable hook spec only if present

commit 9fa3fbed73a36111e308c9856514a032f5444564 upstream.

If there is no priority.expr set, assume hook.num is bogus, too.

While this is fixing JSON output, it's hard to tell what commit this is
actually fixing: Before commit 627c451b23513 ("src: allow variables in
the chain priority specification"), there was no way to detect
flowtables missing hook specs (e.g. when printing flowtable delete
monitor event).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agomonitor: Recognize flowtable add/del events
Phil Sutter [Wed, 15 May 2024 14:01:20 +0000 (16:01 +0200)] 
monitor: Recognize flowtable add/del events

commit 73a8adfc2432ec8337288cc90e7c9f4509139846 upstream.

These were entirely ignored before, add the necessary code analogous to
e.g. objects.

Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agomnl: catch bogus expressions before crashing
Florian Westphal [Thu, 5 Jun 2025 22:20:28 +0000 (00:20 +0200)] 
mnl: catch bogus expressions before crashing

commit d3339f9e35ee4dddf290fcc3e9cc63dac8cb836a upstream.

We can't recover from errors here, but we can abort with a more
precise reason than 'segmentation fault', or stack corruptions
that get caught way later, or not at all.

expr->value is going to be read, we can't cope with other expression
types here.

We will copy to stack buffer of IFNAMSIZ size, abort if we would
overflow.

Check there is a NUL byte present too.
This is a preemptive patch, I've seen one crash in this area but
no reproducer yet.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agomnl: handle singleton element in netdevice set
Pablo Neira Ayuso [Tue, 25 Apr 2023 09:48:52 +0000 (11:48 +0200)] 
mnl: handle singleton element in netdevice set

commit 89b60bc18ca76e45b1682d8c56a0d01fffedebad upstream.

expr_evaluate_set() turns sets with singleton element into value,
nft_dev_add() expects a list of expression, so it crashes.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1676
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agomnl: flowtable support for extended netlink error reporting
Pablo Neira Ayuso [Wed, 19 Apr 2023 13:38:04 +0000 (15:38 +0200)] 
mnl: flowtable support for extended netlink error reporting

commit 99b56d4ee1442ccfa0aadde3cb3ecee74a4de815 upstream.

This patch extends existing flowtable support to improve error
reporting:

 # nft add flowtable inet x y '{ devices = { x } ; }'
 Error: Could not process rule: No such file or directory
 add flowtable inet x y { devices = { x } ; }
                                      ^
 # nft delete flowtable inet x y '{ devices = { x } ; }'
 Error: Could not process rule: No such file or directory
 delete flowtable inet x y { devices = { x } ; }
                                         ^
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agocache: Tolerate object deserialization failures
Phil Sutter [Fri, 16 May 2025 17:36:37 +0000 (19:36 +0200)] 
cache: Tolerate object deserialization failures

commit 610089f273c968c983d965f3cac42e4d9784cde8 upstream.

If netlink_delinearize_obj() fails, it will print an error message. Skip
this object and keep going.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agonetlink: Keep going after set element parsing failures
Phil Sutter [Fri, 16 May 2025 17:33:25 +0000 (19:33 +0200)] 
netlink: Keep going after set element parsing failures

commit 31732dea34a2a4c484df13c4003728eca396e573 upstream.

Print an error message and try to deserialize the remaining elements
instead of calling BUG().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agonetlink: Pass netlink_ctx to netlink_delinearize_setelem()
Phil Sutter [Fri, 16 May 2025 17:17:00 +0000 (19:17 +0200)] 
netlink: Pass netlink_ctx to netlink_delinearize_setelem()

commit 6296f09b8b8ecb6d46cfcd90fd955a39bd86c6c9 upstream.

Prepare for calling netlink_io_error() which needs the context pointer.
Trade this in for the cache pointer since no caller uses a special one.
No functional change intended.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agonetlink_delinearize: Replace some BUG()s by error messages
Phil Sutter [Fri, 16 May 2025 11:28:19 +0000 (13:28 +0200)] 
netlink_delinearize: Replace some BUG()s by error messages

commit 9f2a11aa15422333d1812501dd4f294348d6e0d5 upstream.

Netlink parser tries to keep going despite errors. Faced with an
incompatible ruleset, this is much more user-friendly than exiting the
program upon the first obstacle. This patch fixes three more spots to
support this.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agonetlink: Catch unknown types when deserializing objects
Phil Sutter [Fri, 16 May 2025 17:41:19 +0000 (19:41 +0200)] 
netlink: Catch unknown types when deserializing objects

commit 690f19eadde5cb607ec3d8d471c86d558c7229bd upstream.

Print an error message and discard the object instead of returning it to
the caller. At least when trying to print it, we would hit an assert()
in obj_type_name() anyway.

Fixes: 4756d92e517ae ("src: listing of stateful objects")
Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agonetlink: Avoid potential NULL-ptr deref parsing set elem expressions
Phil Sutter [Fri, 16 May 2025 18:08:05 +0000 (20:08 +0200)] 
netlink: Avoid potential NULL-ptr deref parsing set elem expressions

commit 89645d04106d45d5e23b6d5206777dad1fb3e6bf upstream.

Since netlink_parse_set_expr() may return NULL, the following deref must
be guarded.

Fixes: e6d1d0d611958 ("src: add set element multi-statement support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agodoc: Fix typo in nat statement 'prefix' description
Phil Sutter [Thu, 8 May 2025 14:35:47 +0000 (16:35 +0200)] 
doc: Fix typo in nat statement 'prefix' description

commit 1b3c33a7931967fb7afe4efdf68deb7781786489 upstream.

No point in repeating 'to map' here.

Fixes: 19d73ccdd39fa ("doc: add nat examples")
Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agodoc: add nat examples
Florian Westphal [Mon, 1 May 2023 10:10:09 +0000 (12:10 +0200)] 
doc: add nat examples

commit 19d73ccdd39fae49b44330097ecd2bc507e63d1b upstream.

nftables nat is much more capable than what the existing
documentation describes.

In particular, nftables can fully emulate iptables
NETMAP target and can perform n:m address mapping.

Add a new example section extracted from commit log
messages when those features got added.

Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agosrc: netlink: fix crash when ops doesn't support udata
Florian Westphal [Thu, 8 May 2025 14:29:04 +0000 (16:29 +0200)] 
src: netlink: fix crash when ops doesn't support udata

commit be737a1986bfee0ddea4bee7863dca0123a2bcbc upstream.

Whenever a new version adds udata support to an expression, then old
versions of nft will crash when trying to list such a ruleset generated
by a more recent version of nftables.

Fix this by falling back to 'type' format.

Fixes: 6e48df5329ea ('src: add "typeof" build/parse/print support')
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoexpression: cleanup expr_ops_by_type() and handle u32 input
Thomas Haller [Wed, 20 Sep 2023 14:26:08 +0000 (16:26 +0200)] 
expression: cleanup expr_ops_by_type() and handle u32 input

commit 3d0ce3c19d319a5aae806b617905cfa1ee7f87f4 upstream.

Make fewer assumptions about the underlying integer type of the enum.
Instead, be clear about where we have an untrusted uint32_t from netlink
and an enum. Rename expr_ops_by_type() to expr_ops_by_type_u32() to make
this clearer. Later we might make the enum as packed, when this starts
to matter more.

Also, only the code path expr_ops() wants strict validation and assert
against valid enum values. Move the assertion out of
__expr_ops_by_type(). Then expr_ops_by_type_u32() does not need to
duplicate the handling of EXPR_INVALID. We still need to duplicate the
check against EXPR_MAX, to ensure that the uint32_t value can be cast to
an enum value.

[ Remove cast on EXPR_MAX. --pablo ]

Signed-off-by: Thomas Haller <thaller@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agosrc: remove bogus empty file
Florian Westphal [Fri, 25 Apr 2025 06:20:19 +0000 (08:20 +0200)] 
src: remove bogus empty file

commit f7d932bff5c9550905659dd4a5df23ca46f84935 upstream.

No idea how this happened, remove it.

Reported-by: Sunny73Cr <Sunny73Cr@protonmail.com>
Fixes: 058246016188 ("src: allow to map key to nfqueue number")
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: bail out if ct saddr/daddr dependency cannot be inserted
Florian Westphal [Wed, 2 Apr 2025 23:09:22 +0000 (01:09 +0200)] 
evaluate: bail out if ct saddr/daddr dependency cannot be inserted

commit 7f60519e356833ca007b138c00b9f5de09f21b56 upstream.

If we have an incomplete rule like "ct original saddr" in inet
family, this function generates an error because it can't determine the required protocol
dependency, hinting at missing ip/ip6 keyword.

We should not go on in this case to avoid a redundant followup error:

nft add rule inet f c ct original saddr 1.2.3.4
Error: cannot determine ip protocol version, use "ip saddr" or "ip6 saddr" instead
add rule inet f c ct original saddr 1.2.3.4
                  ^^^^^^^^^^^^^^^^^
Error: Could not parse symbolic invalid expression
add rule inet f c ct original saddr 1.2.3.4

After this change only the first error is shown.

Fixes: 2b29ea5f3c3e ("src: ct: add eval part to inject dependencies for ct saddr/daddr")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_json: bail out on malformed statement in set
Pablo Neira Ayuso [Tue, 1 Apr 2025 07:57:59 +0000 (09:57 +0200)] 
parser_json: bail out on malformed statement in set

commit cc7a7d025a60dced5f5e894aa1688014c9deef3d upstream.

Propagate error to caller so it bails out on malformed set statements.

Fixes: 07958ec53830 ("json: add set statement list support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_json: allow statement stateful statement only in set elements
Pablo Neira Ayuso [Tue, 1 Apr 2025 07:49:48 +0000 (09:49 +0200)] 
parser_json: allow statement stateful statement only in set elements

commit 85b5b051f0e64f106cee078fc6d66d0872770e79 upstream.

Upfront reject of non stateful statements in set elements.

Fixes: 07958ec53830 ("json: add set statement list support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_json: reject empty jump/goto chain
Pablo Neira Ayuso [Mon, 31 Mar 2025 15:55:45 +0000 (17:55 +0200)] 
parser_json: reject empty jump/goto chain

commit 9cce81701a033c4ff5e804fbf7a1149acb9e115a upstream.

When parsing a verdict map json where element jumps to chain represented
as empty string.

internal:0:0-0: Error: Parsing list expression item at index 0 failed.
internal:0:0-0: Error: Invalid set elem at index 0.
internal:0:0-0: Error: Invalid set elem expression.
internal:0:0-0: Error: Parsing command array at index 2 failed.

Fixes: 586ad210368b ("libnftables: Implement JSON parser")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agojson: don't BUG when asked to list synproxies
Florian Westphal [Thu, 27 Mar 2025 16:32:00 +0000 (17:32 +0100)] 
json: don't BUG when asked to list synproxies

commit 40b0708ca6dee4829a9b6e1c2f4677ff2c206b43 upstream.

"-j list synproxys" triggers a BUG().

Rewrite this so that all enum values are handled so the compiler can alert
us to a missing value in case there are more commands in the future.

While at it, implement a few low-hanging fruites as well.

Not-yet-supported cases are simply ignored.

v2: return EOPNOTSUPP for unsupported commands (Pablo Neira Ayuso)

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoct expectation: fix 'list object x' vs. 'list objects in table' confusion
Florian Westphal [Fri, 28 Jul 2023 19:04:13 +0000 (21:04 +0200)] 
ct expectation: fix 'list object x' vs. 'list objects in table' confusion

commit a8ff324dc64fd76f7d218d3d94c5885250951258 upstream.

Just like "ct timeout", "ct expectation" is in need of the same fix,
we get segfault on "nft list ct expectation table t", if table t exists.

This is the exact same pattern as resolved for "ct timeout" in commit
1d2e22fc0521 ("ct timeout: fix 'list object x' vs. 'list objects in table' confusion").

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoct timeout: fix 'list object x' vs. 'list objects in table' confusion
Florian Westphal [Mon, 19 Jun 2023 20:43:06 +0000 (22:43 +0200)] 
ct timeout: fix 'list object x' vs. 'list objects in table' confusion

commit 1d2e22fc0521bcf73ee1f891c291dc1bde47a6bb upstream.

<empty ruleset>
$ nft list ct timeout table t
Error: No such file or directory
list ct timeout table t
                      ^
This is expected to list all 'ct timeout' objects.
The failure is correct, the table 't' does not exist.

But now lets add one:
$ nft add table t
$ nft list ct timeout  table t
Segmentation fault (core dumped)

... and thats not expected, nothing should be shown
and nft should exit normally.

Because of missing TIMEOUTS command enum, the backend thinks
it should do an object lookup, but as frontend asked for
'list of objects' rather than 'show this object',
handle.obj.name is NULL, which then results in this crash.

Update the command enums so that backend knows what the
frontend asked for.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agojson: return error if table does not exist
Florian Westphal [Thu, 27 Mar 2025 08:07:52 +0000 (09:07 +0100)] 
json: return error if table does not exist

commit 2200a105d4befc15b0cfa6cc24282e493888dc82 upstream.

Identical bug and thus same fix as
853d3a2d3cbd ("rule: return error if table does not exist"),
but this time for json.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: reject unsupported expressions in payload statement for bitfields
Pablo Neira Ayuso [Fri, 28 Feb 2025 14:57:18 +0000 (15:57 +0100)] 
evaluate: reject unsupported expressions in payload statement for bitfields

commit de447e248cce3ca3fd7040c386ac798c35721415 upstream.

The payload statement evaluation pretends that it can handle any
expression for bitfields, but the existing evaluation code only knows
how to handle value expression.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoevaluate: simplify payload statement evaluation for bitfields
Pablo Neira Ayuso [Fri, 28 Feb 2025 14:55:04 +0000 (15:55 +0100)] 
evaluate: simplify payload statement evaluation for bitfields

commit 6d9297f278290888e7e26762ee72ed6afa22a97e upstream.

Instead of allocating a lshift expression and relying on the binary
operation transfer propagate this to the mask value, lshift the mask
value immediately.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agotests: py: fix WARNING with JSON
Pablo Neira Ayuso [Tue, 12 Aug 2025 19:52:31 +0000 (21:52 +0200)] 
tests: py: fix WARNING with JSON

backport commit c4c740117f6fbf39dd67dd87635ea8b497718ad7 upstream.

To remove the following WARNING when running JSON tests:

WARNING: Recorded JSON output matches input for: meter acct_out size 4096 { meta iif . ip6 saddr timeout 600s counter }
WARNING: Recorded JSON output matches input for: meter acct_out size 12345 { ip6 saddr . meta iif timeout 600s counter }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoRevert "tests: py: fix json single-flag output for fib & synproxy"
Pablo Neira Ayuso [Tue, 12 Aug 2025 19:57:43 +0000 (21:57 +0200)] 
Revert "tests: py: fix json single-flag output for fib & synproxy"

This reverts commit 4725f4887da84b1b6bbfd0c138bc48a707301fa1.

This 1.0.6.y branch does not contain the follow upstream commits:

  38f99ee84fe6 ("json: Print single synproxy flags as non-array")
  dbe5c44f2b89 ("json: Print single fib flag as non-array")

Therefore, such commit above is not required.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: fix json single-flag output for fib & synproxy
Florian Westphal [Mon, 2 Jun 2025 12:12:16 +0000 (14:12 +0200)] 
tests: py: fix json single-flag output for fib & synproxy

commit c2ee1d5978bf6ea10e3b1a74125da5b5e8468f26 upstream.

Blamed commits change output format but did not adjust existing tests:
  inet/fib.t: WARNING: line 16: '{"nftables": ..

Fixes: 38f99ee84fe6 ("json: Print single synproxy flags as non-array")
Fixes: dbe5c44f2b89 ("json: Print single fib flag as non-array")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Phil Sutter <phil@nwl.cc>
2 months agonetlink_delinerize: add more restrictions on meta nfproto removal
Florian Westphal [Sun, 16 Mar 2025 13:10:26 +0000 (14:10 +0100)] 
netlink_delinerize: add more restrictions on meta nfproto removal

commit 7b3ee497040ff8efb131c566e1c6b466e16f45cc upstream.

We can't remove 'meta nfproto' dependencies for all cases.
Its removed for ip/ip6 families, this works fine.

But for others, e.g. inet, removal is not as simple.
For example

   meta nfproto ipv4 ct protocol tcp

is listed as 'ct protocol tcp', even when this is uses in the inet
table.

Meta L4PROTO removal checks were correct, but refactor this
into a helper function to split meta/ct checks from the common
calling function.

Ct check was lacking, we need to examine ct keys more closely
to figure out if they need to retain the network protocol depenency
or not.  Elide for NFT_CT_SRC/DST and its variants, as those imply
the network protocol to use, all others must keep it as-is.

Also extend test coverage for this.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1783
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agopayload: don't kill dependency for proto_th
Florian Westphal [Thu, 27 Feb 2025 10:47:02 +0000 (11:47 +0100)] 
payload: don't kill dependency for proto_th

commit c0843dec745433ac36aafb5726c580449e522d1d upstream.

proto_th carries no information about the proto number, we need to
preserve the L4 protocol expression unless we can be sure that

For example, if "meta l4proto 91 @th,0,16 0" is simplified to
"th sport 0", the information of protocol number is lost.

Based on initial patch from Xiao Liang.

Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Properly fix JSON equivalents for netdev/reject.t
Phil Sutter [Thu, 12 Jun 2025 10:59:29 +0000 (12:59 +0200)] 
tests: py: Properly fix JSON equivalents for netdev/reject.t

commit b7a11c691d459d06a801a28ae9a52bc564584a1a upstream.

Revert commit d1a7b9e19fe65 ("tests: py: update netdev reject test
file"), the stored JSON equivalents were correct in that they matched
the standard syntax input.

In fact, we missed a .json.output file recording the expected deviation
in JSON output.

Fixes: d1a7b9e19fe65 ("tests: py: update netdev reject test file")
Fixes: 7ca3368cd7575 ("reject: Unify inet, netdev and bridge delinearization")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: extend raw payload match tests
Florian Westphal [Thu, 30 Jan 2025 17:47:14 +0000 (18:47 +0100)] 
tests: py: extend raw payload match tests

commit 857904bdfaf7aa624d974ed6533fd4d4cd39babd upstream.

Add more test cases to exercise binop elimination for raw
payload matches.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Fix for storing payload into missing file
Phil Sutter [Wed, 2 Oct 2024 17:55:49 +0000 (19:55 +0200)] 
tests: py: Fix for storing payload into missing file

commit c1c0c54e237c880adaa8172b93d7450e6c617cfc upstream.

When running a test for which no corresponding *.payload file exists,
the *.payload.got file name was incorrectly constructed due to
'payload_path' variable not being set.

Fixes: 2cfab7a3e10fc ("tests/py: Write dissenting payload into the right file")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: py: fix up udp csum fixup output
Florian Westphal [Wed, 11 Sep 2024 12:23:01 +0000 (14:23 +0200)] 
tests: py: fix up udp csum fixup output

commit 036a1eb3304995e4e000c552b9dfd33b1073ddb6 upstream.

Preceeding commit switched udp to use the inkernel csum parser, so tests
warn:

WARNING: line 7: 'add rule ip test-ip4 input iif "lo" udp checksum set 0':
'[ payload write reg 1 => 2b @ transport header + 6 csum_type 1 csum_off 6 csum_flags 0x0 ]' mismatches
'[ payload write reg 1 => 2b @ transport header + 6 csum_type 0 csum_off 0 csum_flags 0x1 ]'

Fixes: f89abfb4068d ("proto: use NFT_PAYLOAD_L4CSUM_PSEUDOHDR flag to mangle UDP checksum")
Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: py: drop redundant JSON outputs
Pablo Neira Ayuso [Tue, 18 Jun 2024 15:31:46 +0000 (17:31 +0200)] 
tests: py: drop redundant JSON outputs

commit f13fd62ef1e2755ed5eef11adbe0e8a479528f72 upstream.

8abe71f862e6 ("tests: py: Warn if recorded JSON output matches the input")
adds a warning on duplicated JSON outputs.

Remove them when running tests with -j:

  WARNING: Recorded JSON output matches input for: icmp code { 2, 4, 54, 33, 56}

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: complete icmp and icmpv6 update
Pablo Neira Ayuso [Thu, 4 Apr 2024 11:56:39 +0000 (13:56 +0200)] 
tests: py: complete icmp and icmpv6 update

commit 1702bfd70b39fe955d5e2c4b93f7dc07acb79027 upstream.

Update json update and leftover payload update to complete
5fecd2a6ef61 ("src: disentangle ICMP code types").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Drop needless recorded JSON outputs
Phil Sutter [Fri, 22 Mar 2024 14:18:50 +0000 (15:18 +0100)] 
tests: py: Drop needless recorded JSON outputs

commit 07c9733a4b4ab5b6f6fb94f305d323528a058823 upstream.

These match the input already, no need to track them.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: py: Warn if recorded JSON output matches the input
Phil Sutter [Fri, 22 Mar 2024 14:04:40 +0000 (15:04 +0100)] 
tests: py: Warn if recorded JSON output matches the input

commit 8abe71f862e6248517365bec4bac31d85141cf69 upstream.

Actively support spring-cleaning by nagging callers.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: py: Fix some JSON equivalents
Phil Sutter [Fri, 22 Mar 2024 13:00:26 +0000 (14:00 +0100)] 
tests: py: Fix some JSON equivalents

commit 0c17b910b3e19c4eacf5567f90b6545ed1fd8aac upstream.

Make sure they match the standard syntax input as much as possible.

For some reason inet/tcp.t.json was using plain arrays in place of
binary OR expressions in many cases. These arrays are interpreted as
list expressions, which seems to be semantically identical but the goal
here is to present an accurate equivalent to the rule in standard
syntax.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: py: add payload merging test cases
Florian Westphal [Fri, 8 Mar 2024 13:40:12 +0000 (14:40 +0100)] 
tests: py: add payload merging test cases

commit 04fc306359a49bc9f314ae82520218f5dfebfef1 upstream.

Add a test case that would fail without preceeding fix.

[ NB. Precending fix is f35a0d78fe870737fa39d859bd2e3ac25bf1b12e ]

Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: py: add missing json.output data
Florian Westphal [Mon, 26 Feb 2024 08:45:43 +0000 (09:45 +0100)] 
tests: py: add missing json.output data

commit f58e72a2f5aa134653e7ee8b5432b23eb0674c3c upstream.

Fixes: bridge/vlan.t: WARNING: line 56: ...
Fixes: 8b9ae77598b4 ("tests: never merge across non-expression statements redux 2")
Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: py: remove huge-limit test cases
Florian Westphal [Thu, 18 Jan 2024 12:24:04 +0000 (13:24 +0100)] 
tests: py: remove huge-limit test cases

commit 1ee84db0360db0da336d5b1ee92807d29fcfbbe1 upstream.

These tests will fail once the kernel checks for overflow
in the internal token bucken counter, so drop them.

Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: py: missing json output in meta.t with vlan mapping
Pablo Neira Ayuso [Mon, 11 Dec 2023 11:54:35 +0000 (12:54 +0100)] 
tests: py: missing json output in meta.t with vlan mapping

commit 5fec559727ffd2c6c8958748beab782096385758 upstream.

Fix this warning due to missing coverage:

 tests/py/any/meta.t.json.got: WARNING: line 2: Wrote JSON equivalent for rule meta mark set vlan id map { 1 : 0x00000001, 4095 : 0x00004095 }
 ERROR: did not find JSON equivalent for rule 'meta mark set vlan id map @map1

Fixes: 8d3de823b622 ("evaluate: reset statement length context before evaluating statement")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: missing json output in never merge across non-expression statements
Pablo Neira Ayuso [Wed, 6 Dec 2023 18:30:44 +0000 (19:30 +0100)] 
tests: py: missing json output in never merge across non-expression statements

commit 94fd162ea4d25fe6b0b4d58dcb7ff66dc55f3247 upstream.

Add missing json output.

Fixes: 99ab1b8feb16 ("rule: never merge across non-expression statements")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: debloat frag.t.payload.netdev
Pablo Neira Ayuso [Tue, 29 Aug 2023 17:30:14 +0000 (19:30 +0200)] 
tests: py: debloat frag.t.payload.netdev

commit 998c09a2cb011735cbe96c8182ef34e32603b4fc upstream.

This bytecode output file contains many duplicated entries, remove them.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: extend ip frag-off coverage
Pablo Neira Ayuso [Tue, 29 Aug 2023 17:04:07 +0000 (19:04 +0200)] 
tests: py: extend ip frag-off coverage

commit 3e22505e470966635facc0395541ec80c587c063 upstream.

Cover matching on DF and MF bits and fragments.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Document JSON mode in README
Phil Sutter [Tue, 27 Jun 2023 15:50:07 +0000 (17:50 +0200)] 
tests: py: Document JSON mode in README

commit 50bebfaf9d558d539f06c0710781a49e8b58f25f upstream.

Mostly identify the various files that (may) appear or exist already and
how to deal with them.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agoAmend "tests: shell: Fix ifname_based_hooks feature check"
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 months 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 commit 156b5936b3b7a0b1ee590a02874beaba5235f758 upstream.

It fails for unknown reason to me.

2 months 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"

Partial revert of commit c4c740117f6fbf39dd67dd87635ea8b497718ad7 upstream.

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

2 months 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 upstream.

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

2 months 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

Partial revert of commit 1f3d0b9cf9cc823c1f747650a84f1b43817d460f upstream.

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 months 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 upstream commits:

 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 months 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 upstream.

Conversion to dynamic set is not available in 1.0.6.y

2 months 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 upstream.

2 months 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 upstream.

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

2 months 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 months 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 months 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 months 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 months 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 months 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 months 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 months 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 months 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 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
2 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>
3 months 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>