Skip to contentSkip to conversation

nf_tables: follow-up on audit fix, add selftest

PATCH In review Phil Sutter 3 participants 6 messages Sep 13, 2023

Subsystems (4)
lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

v3 cover nf_tables: follow-up on audit fix, add selftest

Patch f1 fixes/improves Pablo's fix for the bug I built into nf_tables' audit support code.

Patch 2 adds a selftest for the audit notifications in nf_tables. I consider it mature enough to submit it as non-RFC now.

Larger changes in both patches, details in each patch.

Phil Sutter (2):
netfilter: nf_tables: Fix entries val in rule reset audit log
selftests: netfilter: Test nf_tables audit logging

 net/netfilter/nf_tables_api.c                 |  16 +-
 tools/testing/selftests/netfilter/.gitignore  |   1 +
 tools/testing/selftests/netfilter/Makefile    |   4 +-
 .../selftests/netfilter/audit_logread.c       | 165 ++++++++++++++++++
 tools/testing/selftests/netfilter/config      |   1 +
 .../testing/selftests/netfilter/nft_audit.sh  | 108 ++++++++++++
 6 files changed, 287 insertions(+), 8 deletions(-)
 create mode 100644 tools/testing/selftests/netfilter/audit_logread.c
 create mode 100755 tools/testing/selftests/netfilter/nft_audit.sh
Signature · 1 line
2.41.0
author reviewer
#1 lore raw

PATCH v3 1/2 netfilter: nf_tables: Fix entries val in rule reset audit log

The value in idx and the number of rules handled in that particular __nf_tables_dump_rules() call is not identical. The former is a cursor to pick up from if multiple netlink messages are needed, so its value is ever increasing. Fixing this is not just a matter of subtracting s_idx from it, though: When resetting rules in multiple chains, __nf_tables_dump_rules() is called for each and cb->args[0] is not adjusted in between. Introduce a dedicated counter to record the number of rules reset in this call in a less confusing way.

While being at it, prevent the direct return upon buffer exhaustion: Any rules previously dumped into that skb would evade audit logging otherwise.

Fixes 9b5ba5c9c5109 ("netfilter: nf_tables: Unbreak audit log reset")

---
Changes since v2:
- Restore per-chain logging as requested.

Changes since v1:
- Use max_t() to eliminate the kernel warning

---
 net/netfilter/nf_tables_api.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
net/netfilter/nf_tables_api.c +10 -6
index e429ebba74b3d..446e1882428e6 100644
@@ -3449,6 +3449,8 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,
 	struct net *net = sock_net(skb->sk);
 	const struct nft_rule *rule, *prule;
 	unsigned int s_idx = cb->args[0];
+	unsigned int entries = 0;
+	int ret = 0;
 	u64 handle;
 
 	prule = NULL;
@@ -3471,9 +3473,11 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,
 					NFT_MSG_NEWRULE,
 					NLM_F_MULTI | NLM_F_APPEND,
 					table->family,
-					table, chain, rule, handle, reset) < 0)
-			return 1;
-
+					table, chain, rule, handle, reset) < 0) {
+			ret = 1;
+			break;
+		}
+		entries++;
 		nl_dump_check_consistent(cb, nlmsg_hdr(skb));
 cont:
 		prule = rule;
@@ -3481,10 +3485,10 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,
 		(*idx)++;
 	}
 
-	if (reset && *idx)
-		audit_log_rule_reset(table, cb->seq, *idx);
+	if (reset && entries)
+		audit_log_rule_reset(table, cb->seq, entries);
 
-	return 0;
+	return ret;
 }
 
 static int nf_tables_dump_rules(struct sk_buff *skb,
Signature · 1 line
2.41.0
maintainer Reviewed-by on v3 1/2
#2 lore raw

Re: [nf PATCH v3 1/2] netfilter: nf_tables: Fix entries val in rule reset audit log

Quoted 9 lines · Phil Sutter

The value in idx and the number of rules handled in that particular __nf_tables_dump_rules() call is not identical. The former is a cursor to pick up from if multiple netlink messages are needed, so its value is ever increasing. Fixing this is not just a matter of subtracting s_idx from it, though: When resetting rules in multiple chains, __nf_tables_dump_rules() is called for each and cb->args[0] is not adjusted in between. Introduce a dedicated counter to record the number of rules reset in this call in a less confusing way.

Quoted 3 lines

While being at it, prevent the direct return upon buffer exhaustion: Any rules previously dumped into that skb would evade audit logging otherwise.

We can investigate ways to compress/coalesce (read: make this more complicated) in case somebody complains about too many audit messages.

But I would not go ahead and keep it simple for now.

author reviewer
#3 lore raw

Re: [nf PATCH v3 1/2] netfilter: nf_tables: Fix entries val in rule reset audit log

Quoted 10 lines · Florian Westphal
Quoted 9 lines · Phil Sutter

The value in idx and the number of rules handled in that particular __nf_tables_dump_rules() call is not identical. The former is a cursor to pick up from if multiple netlink messages are needed, so its value is ever increasing. Fixing this is not just a matter of subtracting s_idx from it, though: When resetting rules in multiple chains, __nf_tables_dump_rules() is called for each and cb->args[0] is not adjusted in between. Introduce a dedicated counter to record the number of rules reset in this call in a less confusing way.

Quoted 8 lines
Quoted 3 lines

While being at it, prevent the direct return upon buffer exhaustion: Any rules previously dumped into that skb would evade audit logging otherwise.

We can investigate ways to compress/coalesce (read: make this more complicated) in case somebody complains about too many audit messages.

It is only about reset command. Anything following the transaction path is coalesced already (on a per-table basis, so there's more work needed if consistent per-chain logging is desired).

Quoted 1 line

But I would not go ahead and keep it simple for now.

I just want to avoid a second rhbz#2001815[1]. As we both know, OpenShift likes to have both excessively big chains and excessively many small ones. %)

Cheers, Phil

[1] https://bugzilla.redhat.com/show_bug.cgi?id=2001815

author reviewer
#4 lore raw

PATCH v3 2/2 selftests: netfilter: Test nf_tables audit logging

In reply to Phil Sutter

Compare NETFILTER_CFG type audit logs emitted from kernel upon ruleset modifications against expected output.

---
Changes since v1:
- Implement a custom log reader which turns audit logging on/off as needed, drop auditd dependency.
- Record required CONFIG_AUDIT in 'config' file.
- Add SPDX license headers.
- Open readonly fd to logfile for continuous reading, shell's redirect doesn't like file truncating to clear previous output.
- Use a single 'diff' call for status and error reporting.
- Use ruleset setup commands for audit log testing, too.
- Extend many rules reset test by two more commands.

---
 tools/testing/selftests/netfilter/.gitignore  |   1 +
 tools/testing/selftests/netfilter/Makefile    |   4 +-
 .../selftests/netfilter/audit_logread.c       | 165 ++++++++++++++++++
 tools/testing/selftests/netfilter/config      |   1 +
 .../testing/selftests/netfilter/nft_audit.sh  | 108 ++++++++++++
 5 files changed, 277 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/netfilter/audit_logread.c
 create mode 100755 tools/testing/selftests/netfilter/nft_audit.sh
tools/testing/selftests/netfilter/.gitignore +1 -0
index 4cb887b574138..4b2928e1c19d8 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 nf-queue
 connect_close
+audit_logread
tools/testing/selftests/netfilter/Makefile +2 -2
index 3686bfa6c58d7..321db8850da00 100644
@@ -6,13 +6,13 @@ TEST_PROGS := nft_trans_stress.sh nft_fib.sh nft_nat.sh bridge_brouter.sh \
 	nft_concat_range.sh nft_conntrack_helper.sh \
 	nft_queue.sh nft_meta.sh nf_nat_edemux.sh \
 	ipip-conntrack-mtu.sh conntrack_tcp_unreplied.sh \
-	conntrack_vrf.sh nft_synproxy.sh rpath.sh
+	conntrack_vrf.sh nft_synproxy.sh rpath.sh nft_audit.sh
 
 HOSTPKG_CONFIG := pkg-config
 
 CFLAGS += $(shell $(HOSTPKG_CONFIG) --cflags libmnl 2>/dev/null)
 LDLIBS += $(shell $(HOSTPKG_CONFIG) --libs libmnl 2>/dev/null || echo -lmnl)
 
-TEST_GEN_FILES =  nf-queue connect_close
+TEST_GEN_FILES =  nf-queue connect_close audit_logread
 
 include ../lib.mk
tools/testing/selftests/netfilter/audit_logread.c new file +165 -0

165 lines not on this page · raw message

tools/testing/selftests/netfilter/config +1 -0
index 4faf2ce021d90..7c42b1b2c69b4 100644
@@ -6,3 +6,4 @@ CONFIG_NFT_REDIR=m
 CONFIG_NFT_MASQ=m
 CONFIG_NFT_FLOW_OFFLOAD=m
 CONFIG_NF_CT_NETLINK=m
+CONFIG_AUDIT=y
tools/testing/selftests/netfilter/nft_audit.sh new file +108 -0

108 lines not on this page · raw message

Signature · 1 line
2.41.0
#5 lore raw
In reply to Phil Sutter
Quoted 7 lines · Phil Sutter

Patch f1 fixes/improves Pablo's fix for the bug I built into nf_tables' audit support code.

Patch 2 adds a selftest for the audit notifications in nf_tables. I consider it mature enough to submit it as non-RFC now.

Larger changes in both patches, details in each patch.

Series applied, thanks