Skip to contentSkip to conversation

Fix for shell testsuite '-V' option

PATCH In review Phil Sutter 1 participant 11 messages Nov 30, 2022 – Dec 2, 2022 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Fix for shell testsuite '-V' option

When running the testsuite in "valgrind mode", numerous cases of lost memory and still reachable at program exit are reported. Address them all plus libiptc's access of uninitialized memory.

Phil Sutter (9):
tests: shell: Fix valgrind mode for 0008-unprivileged_0
iptables-restore: Free handle with --test also
iptables-xml: Free allocated chain strings
nft: Plug memleak in nft_rule_zero_counters()
iptables: Plug memleaks in print_firewall()
xtables: Introduce xtables_clear_iptables_command_state()
iptables: Properly clear iptables_command_state object
xshared: Free data after printing help
libiptc: Eliminate garbage access

 iptables/ip6tables.c              |  9 +++++++--
 iptables/iptables-restore.c       |  4 ++--
 iptables/iptables-xml.c           | 10 +++++-----
 iptables/iptables.c               |  9 +++++++--
 iptables/nft-arp.c                |  4 ++--
 iptables/nft-ipv4.c               |  4 ++--
 iptables/nft-ipv6.c               |  4 ++--
 iptables/nft-shared.c             | 14 --------------
 iptables/nft-shared.h             |  1 -
 iptables/nft.c                    |  5 +++--
 iptables/tests/shell/run-tests.sh |  3 ++-
 iptables/xshared.c                | 20 ++++++++++++++++++++
 iptables/xshared.h                |  2 ++
 iptables/xtables-translate.c      |  2 +-
 iptables/xtables.c                |  2 +-
 libiptc/libiptc.c                 |  4 ++--
 16 files changed, 58 insertions(+), 39 deletions(-)
Signature · 1 line
2.38.0
author reviewer
#1 lore raw

PATCH 1/9 tests: shell: Fix valgrind mode for 0008-unprivileged_0

Valgrind is run as user nobody, let everyone write into the temporary directory.

---
 iptables/tests/shell/run-tests.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
iptables/tests/shell/run-tests.sh +2 -1
index 7878760fdcc4d..7a80af3432285 100755
@@ -122,7 +122,8 @@ EOF
 if [ "$VALGRIND" == "y" ]; then
 	tmpd=$(mktemp -d)
 	msg_info "writing valgrind logs to $tmpd"
-	chmod a+rx $tmpd
+	# let nobody write logs, too (././testcases/iptables/0008-unprivileged_0)
+	chmod 777 $tmpd
 	printscript "$XTABLES_NFT_MULTI" "$tmpd" >${tmpd}/xtables-nft-multi
 	printscript "$XTABLES_LEGACY_MULTI" "$tmpd" >${tmpd}/xtables-legacy-multi
 	trap "rm ${tmpd}/xtables-*-multi" EXIT
Signature · 1 line
2.38.0
author reviewer
#2 lore raw

PATCH 2/9 iptables-restore: Free handle with --test also

In reply to Phil Sutter

When running 'iptables-restore -t', valgrind reports:

1,496 (160 direct, 1,336 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
at 0x48417E5: malloc (vg_replace_malloc.c:381)
by 0x4857A46: alloc_handle (libiptc.c:1279)
by 0x4857A46: iptc_init (libiptc.c:1342)
by 0x1167CE: create_handle (iptables-restore.c:72)
by 0x1167CE: ip46tables_restore_main (iptables-restore.c:229)
by 0x116DAE: iptables_restore_main (iptables-restore.c:388)
by 0x49A2349: (below main) (in /lib64/libc.so.6)

Free the handle pointer before parsing the next table.

Fixes 1c9015b2cb483 ("libiptc: remove indirections")
---
 iptables/iptables-restore.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
iptables/iptables-restore.c +2 -2
index 05661bf6ceee3..6f7ddf93b01bb 100644
@@ -185,12 +185,12 @@ ip46tables_restore_main(const struct iptables_restore_cb *cb,
 			if (!testing) {
 				DEBUGP("Calling commit\n");
 				ret = cb->ops->commit(handle);
-				cb->ops->free(handle);
-				handle = NULL;
 			} else {
 				DEBUGP("Not calling commit, testing\n");
 				ret = 1;
 			}
+			cb->ops->free(handle);
+			handle = NULL;
 
 			/* Done with the current table, release the lock. */
 			if (lock >= 0) {
Signature · 1 line
2.38.0
author reviewer
#3 lore raw

PATCH 3/9 iptables-xml: Free allocated chain strings

In reply to Phil Sutter

Freeing only if 'created' is non-zero is wrong - the data was still allocated. In fact, the field is supposed to prevent only the call to openChain().

Fixes 8d3eccb19a9c6 ("Add iptables-xml tool (Amin Azez <[email protected]>)")
---
 iptables/iptables-xml.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
iptables/iptables-xml.c +5 -5
index d28cf7481b55d..396c0a123ea08 100644
@@ -225,13 +225,13 @@ finishChains(void)
 {
 	int c;
 
-	for (c = 0; c < nextChain; c++)
-		if (!chains[c].created) {
+	for (c = 0; c < nextChain; c++) {
+		if (!chains[c].created)
 			openChain(chains[c].chain, chains[c].policy,
 				  &(chains[c].count), '/');
-			free(chains[c].chain);
-			free(chains[c].policy);
-		}
+		free(chains[c].chain);
+		free(chains[c].policy);
+	}
 	nextChain = 0;
 }
 
Signature · 1 line
2.38.0
author reviewer
#4 lore raw

PATCH 4/9 nft: Plug memleak in nft_rule_zero_counters()

In reply to Phil Sutter

When zeroing a specific rule, valgrind reports:

40 bytes in 1 blocks are definitely lost in loss record 1 of 1
at 0x484659F: calloc (vg_replace_malloc.c:1328)
by 0x48DE128: xtables_calloc (xtables.c:434)
by 0x11C7C6: nft_parse_immediate (nft-shared.c:1071)
by 0x11C7C6: nft_rule_to_iptables_command_state (nft-shared.c:1236) by 0x119AF5: nft_rule_zero_counters (nft.c:2877)
by 0x11A3CA: nft_prepare (nft.c:3445)
by 0x11A7A8: nft_commit (nft.c:3479)
by 0x114258: xtables_main.isra.0 (xtables-standalone.c:94) by 0x1142D9: xtables_ip6_main (xtables-standalone.c:118) by 0x49F2349: (below main) (in /lib64/libc.so.6)

Have to free the matches/target in populated iptables_command_state object again. While being at it, call the proper family_ops callbacks since this is family-agnostic code.

Fixes a69cc575295ee ("xtables: allow to reset the counters of an existing rule")
---
 iptables/nft.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
iptables/nft.c +3 -2
index 4af55341a210f..5def01ad6bfbc 100644
@@ -2873,10 +2873,11 @@ int nft_rule_zero_counters(struct nft_handle *h, const char *chain,
 		goto error;
 	}
 
-	nft_rule_to_iptables_command_state(h, r, &cs);
-
+	h->ops->rule_to_cs(h, r, &cs);
 	cs.counters.pcnt = cs.counters.bcnt = 0;
 	new_rule = nft_rule_new(h, chain, table, &cs);
+	h->ops->clear_cs(&cs);
+
 	if (!new_rule)
 		return 1;
 
Signature · 1 line
2.38.0
author reviewer
#5 lore raw

PATCH 5/9 iptables: Plug memleaks in print_firewall()

In reply to Phil Sutter

When adding a rule in verbose mode, valgrind prints:

192 bytes in 1 blocks are definitely lost in loss record 1 of 2
at 0x48417E5: malloc (vg_replace_malloc.c:381)
by 0x486B158: xtables_malloc (xtables.c:446)
by 0x486C1F6: xtables_find_match (xtables.c:826) by 0x10E684: print_match (iptables.c:115)
by 0x10E684: print_firewall (iptables.c:169)
by 0x10FC0C: print_firewall_line (iptables.c:196) by 0x10FC0C: append_entry (iptables.c:221)
by 0x10FC0C: do_command4 (iptables.c:776)
by 0x10E45B: iptables_main (iptables-standalone.c:59) by 0x49A2349: (below main) (in /lib64/libc.so.6)

200 bytes in 1 blocks are definitely lost in loss record 2 of 2
at 0x48417E5: malloc (vg_replace_malloc.c:381)
by 0x486B158: xtables_malloc (xtables.c:446)
by 0x486BBD6: xtables_find_target (xtables.c:956) by 0x10E579: print_firewall (iptables.c:145)
by 0x10FC0C: print_firewall_line (iptables.c:196) by 0x10FC0C: append_entry (iptables.c:221)
by 0x10FC0C: do_command4 (iptables.c:776)
by 0x10E45B: iptables_main (iptables-standalone.c:59) by 0x49A2349: (below main) (in /lib64/libc.so.6)

If the match/target was cloned, it needs to be freed. Basically a bug since day 1.

---
 iptables/ip6tables.c | 6 ++++++
 iptables/iptables.c  | 6 ++++++
 2 files changed, 12 insertions(+)
iptables/ip6tables.c +6 -0
index 062b2b152d554..1d2326570a71d 100644
@@ -122,6 +122,9 @@ print_match(const struct xt_entry_match *m,
 			printf("%s%s ", match->name, unsupported_rev);
 		else
 			printf("%s ", match->name);
+
+		if (match->next == match)
+			free(match);
 	} else {
 		if (name[0])
 			printf("UNKNOWN match `%s' ", name);
@@ -179,6 +182,9 @@ print_firewall(const struct ip6t_entry *fw,
 			tg->print(&fw->ipv6, t, format & FMT_NUMERIC);
 		else if (target->print)
 			printf(" %s%s", target->name, unsupported_rev);
+
+		if (target->next == target)
+			free(target);
 	} else if (t->u.target_size != sizeof(*t))
 		printf("[%u bytes of unknown target data] ",
 		       (unsigned int)(t->u.target_size - sizeof(*t)));
iptables/iptables.c +6 -0
index 0351b39fb7e3e..d246198f49d27 100644
@@ -122,6 +122,9 @@ print_match(const struct xt_entry_match *m,
 			printf("%s%s ", match->name, unsupported_rev);
 		else
 			printf("%s ", match->name);
+
+		if (match->next == match)
+			free(match);
 	} else {
 		if (name[0])
 			printf("UNKNOWN match `%s' ", name);
@@ -178,6 +181,9 @@ print_firewall(const struct ipt_entry *fw,
 			tg->print(&fw->ip, t, format & FMT_NUMERIC);
 		else if (target->print)
 			printf(" %s%s", target->name, unsupported_rev);
+
+		if (target->next == target)
+			free(target);
 	} else if (t->u.target_size != sizeof(*t))
 		printf("[%u bytes of unknown target data] ",
 		       (unsigned int)(t->u.target_size - sizeof(*t)));
Signature · 1 line
2.38.0
author reviewer
#6 lore raw

PATCH 6/9 xtables: Introduce xtables_clear_iptables_command_state()

In reply to Phil Sutter

This is nft_clear_iptables_command_state() but in a location reachable by legacy iptables, too.

Changes callers in non-family-specific code to use clear_cs callback instead of directly calling it - ebtables still has a custom variant.

---
 iptables/nft-arp.c           |  4 ++--
 iptables/nft-ipv4.c          |  4 ++--
 iptables/nft-ipv6.c          |  4 ++--
 iptables/nft-shared.c        | 14 --------------
 iptables/nft-shared.h        |  1 -
 iptables/xshared.c           | 17 +++++++++++++++++
 iptables/xshared.h           |  2 ++
 iptables/xtables-translate.c |  2 +-
 iptables/xtables.c           |  2 +-
 9 files changed, 27 insertions(+), 23 deletions(-)
iptables/nft-arp.c +2 -2
index 59f100af2a6b9..d670cbe629fe4 100644
@@ -490,7 +490,7 @@ nft_arp_print_rule(struct nft_handle *h, struct nftnl_rule *r,
 	if (!(format & FMT_NONEWLINE))
 		fputc('\n', stdout);
 
-	nft_clear_iptables_command_state(&cs);
+	xtables_clear_iptables_command_state(&cs);
 }
 
 static bool nft_arp_is_same(const struct iptables_command_state *cs_a,
@@ -787,7 +787,7 @@ struct nft_family_ops nft_family_ops_arp = {
 	},
 	.rule_to_cs		= nft_rule_to_iptables_command_state,
 	.init_cs		= nft_arp_init_cs,
-	.clear_cs		= nft_clear_iptables_command_state,
+	.clear_cs		= xtables_clear_iptables_command_state,
 	.parse_target		= nft_ipv46_parse_target,
 	.add_entry		= nft_arp_add_entry,
 	.delete_entry		= nft_arp_delete_entry,
iptables/nft-ipv4.c +2 -2
index 6c62dd46dddac..42167351710e6 100644
@@ -247,7 +247,7 @@ static void nft_ipv4_print_rule(struct nft_handle *h, struct nftnl_rule *r,
 	if (!(format & FMT_NONEWLINE))
 		fputc('\n', stdout);
 
-	nft_clear_iptables_command_state(&cs);
+	xtables_clear_iptables_command_state(&cs);
 }
 
 static void nft_ipv4_save_rule(const struct iptables_command_state *cs,
@@ -454,7 +454,7 @@ struct nft_family_ops nft_family_ops_ipv4 = {
 	},
 	.parse_target		= nft_ipv46_parse_target,
 	.rule_to_cs		= nft_rule_to_iptables_command_state,
-	.clear_cs		= nft_clear_iptables_command_state,
+	.clear_cs		= xtables_clear_iptables_command_state,
 	.xlate			= nft_ipv4_xlate,
 	.add_entry		= nft_ipv4_add_entry,
 	.delete_entry		= nft_ipv4_delete_entry,
iptables/nft-ipv6.c +2 -2
index 98c35afa67ad9..3a373b7eb2cfe 100644
@@ -211,7 +211,7 @@ static void nft_ipv6_print_rule(struct nft_handle *h, struct nftnl_rule *r,
 	if (!(format & FMT_NONEWLINE))
 		fputc('\n', stdout);
 
-	nft_clear_iptables_command_state(&cs);
+	xtables_clear_iptables_command_state(&cs);
 }
 
 static void nft_ipv6_save_rule(const struct iptables_command_state *cs,
@@ -423,7 +423,7 @@ struct nft_family_ops nft_family_ops_ipv6 = {
 	},
 	.parse_target		= nft_ipv46_parse_target,
 	.rule_to_cs		= nft_rule_to_iptables_command_state,
-	.clear_cs		= nft_clear_iptables_command_state,
+	.clear_cs		= xtables_clear_iptables_command_state,
 	.xlate			= nft_ipv6_xlate,
 	.add_entry		= nft_ipv6_add_entry,
 	.delete_entry		= nft_ipv6_delete_entry,
iptables/nft-shared.c +0 -14
index 63d251986f65b..f1503b6ce0cbc 100644
@@ -1293,20 +1293,6 @@ bool nft_rule_to_iptables_command_state(struct nft_handle *h,
 	return ret;
 }
 
-void nft_clear_iptables_command_state(struct iptables_command_state *cs)
-{
-	xtables_rule_matches_free(&cs->matches);
-	if (cs->target) {
-		free(cs->target->t);
-		cs->target->t = NULL;
-
-		if (cs->target == cs->target->next) {
-			free(cs->target);
-			cs->target = NULL;
-		}
-	}
-}
-
 void nft_ipv46_save_chain(const struct nftnl_chain *c, const char *policy)
 {
 	const char *chain = nftnl_chain_get_str(c, NFTNL_CHAIN_NAME);
iptables/nft-shared.h +0 -1
index e2c3ac7b0cc5c..07d39131cb0d6 100644
@@ -216,7 +216,6 @@ void get_cmp_data(struct nftnl_expr *e, void *data, size_t dlen, bool *inv);
 bool nft_rule_to_iptables_command_state(struct nft_handle *h,
 					const struct nftnl_rule *r,
 					struct iptables_command_state *cs);
-void nft_clear_iptables_command_state(struct iptables_command_state *cs);
 void print_matches_and_target(struct iptables_command_state *cs,
 			      unsigned int format);
 void nft_ipv46_save_chain(const struct nftnl_chain *c, const char *policy);
iptables/xshared.c +17 -0
index d400dc595ea99..2a894c19a011d 100644
@@ -1356,6 +1356,23 @@ static const char *optstring_lookup(int family)
 	return "";
 }
 
+void xtables_clear_iptables_command_state(struct iptables_command_state *cs)
+{
+	xtables_rule_matches_free(&cs->matches);
+	if (cs->target) {
+		free(cs->target->t);
+		cs->target->t = NULL;
+
+		free(cs->target->udata);
+		cs->target->udata = NULL;
+
+		if (cs->target == cs->target->next) {
+			free(cs->target);
+			cs->target = NULL;
+		}
+	}
+}
+
 void do_parse(int argc, char *argv[],
 	      struct xt_cmd_parse *p, struct iptables_command_state *cs,
 	      struct xtables_args *args)
iptables/xshared.h +2 -0
index bfae4b4e1b5d3..0ed9f3c29c600 100644
@@ -130,6 +130,8 @@ struct iptables_command_state {
 	bool restore;
 };
 
+void xtables_clear_iptables_command_state(struct iptables_command_state *cs);
+
 typedef int (*mainfunc_t)(int, char **);
 
 struct subcommand {
iptables/xtables-translate.c +1 -1
index 4e8db4bedff88..e8d251b3162d9 100644
@@ -336,7 +336,7 @@ static int do_command_xlate(struct nft_handle *h, int argc, char *argv[],
 		exit(1);
 	}
 
-	nft_clear_iptables_command_state(&cs);
+	h->ops->clear_cs(&cs);
 
 	if (h->family == AF_INET) {
 		free(args.s.addr.v4);
iptables/xtables.c +1 -1
index 70924176df8c1..22d6ea58376fc 100644
@@ -262,7 +262,7 @@ int do_commandx(struct nft_handle *h, int argc, char *argv[], char **table,
 
 	*table = p.table;
 
-	nft_clear_iptables_command_state(&cs);
+	h->ops->clear_cs(&cs);
 
 	free(args.s.addr.ptr);
 	free(args.s.mask.ptr);
Signature · 1 line
2.38.0
author reviewer
#7 lore raw

PATCH 7/9 iptables: Properly clear iptables_command_state object

In reply to Phil Sutter

When adding a rule with a target which defines a udata_size, valgrind prints:

8 bytes in 1 blocks are definitely lost in loss record 1 of 1
at 0x484659F: calloc (vg_replace_malloc.c:1328) by 0x486B128: xtables_calloc (xtables.c:434)
by 0x1128B4: xs_init_target (xshared.c:238)
by 0x113CD3: command_jump (xshared.c:877)
by 0x114969: do_parse (xshared.c:1644)
by 0x10EEB9: do_command4 (iptables.c:691)
by 0x10E45B: iptables_main (iptables-standalone.c:59) by 0x49A2349: (below main) (in /lib64/libc.so.6)

It is not sufficient to free cs.target->t, so call xtables_clear_iptables_command_state() which takes care of all the details.

Fixes 2dba676b68ef8 ("extensions: support for per-extension instance "global" variable space")
---
 iptables/ip6tables.c | 3 +--
 iptables/iptables.c  | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
iptables/ip6tables.c +1 -2
index 1d2326570a71d..345af4519bfe7 100644
@@ -778,7 +778,6 @@ int do_command6(int argc, char *argv[], char **table,
 			xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED);
 		} else {
 			e = generate_entry(&cs.fw6, cs.matches, cs.target->t);
-			free(cs.target->t);
 		}
 	}
 
@@ -880,7 +879,7 @@ int do_command6(int argc, char *argv[], char **table,
 	if (verbose > 1)
 		dump_entries6(*handle);
 
-	xtables_rule_matches_free(&cs.matches);
+	xtables_clear_iptables_command_state(&cs);
 
 	if (e != NULL) {
 		free(e);
iptables/iptables.c +1 -2
index d246198f49d27..6f7b34762ea40 100644
@@ -773,7 +773,6 @@ int do_command4(int argc, char *argv[], char **table,
 			xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED);
 		} else {
 			e = generate_entry(&cs.fw, cs.matches, cs.target->t);
-			free(cs.target->t);
 		}
 	}
 
@@ -875,7 +874,7 @@ int do_command4(int argc, char *argv[], char **table,
 	if (verbose > 1)
 		dump_entries(*handle);
 
-	xtables_rule_matches_free(&cs.matches);
+	xtables_clear_iptables_command_state(&cs);
 
 	if (e != NULL) {
 		free(e);
Signature · 1 line
2.38.0
author reviewer
#8 lore raw

PATCH 8/9 xshared: Free data after printing help

In reply to Phil Sutter

This is merely to make valgrind happy, but less noise means less real issues missed.

---
 iptables/xshared.c | 3 +++
 1 file changed, 3 insertions(+)
iptables/xshared.c +3 -0
index 2a894c19a011d..f93529b11a319 100644
@@ -1543,6 +1543,9 @@ void do_parse(int argc, char *argv[],
 					XTF_TRY_LOAD, &cs->matches);
 
 			xtables_printhelp(cs->matches);
+			xtables_clear_iptables_command_state(cs);
+			xtables_free_opts(1);
+			xtables_fini();
 			exit(0);
 
 			/*
Signature · 1 line
2.38.0
author reviewer
#9 lore raw

PATCH 9/9 libiptc: Eliminate garbage access

In reply to Phil Sutter

When adding a rule, valgrind prints:

Syscall param socketcall.setsockopt(optval) points to uninitialised byte(s)
at 0x4A8165A: setsockopt (in /lib64/libc.so.6)
by 0x4857A48: iptc_commit (libiptc.c:2676)
by 0x10E4BB: iptables_main (iptables-standalone.c:61)
by 0x49A3349: (below main) (in /lib64/libc.so.6)
Address 0x4b63788 is 40 bytes inside a block of size 1,448 alloc'd
at 0x484659F: calloc (vg_replace_malloc.c:1328)
by 0x4857654: iptc_commit (libiptc.c:2564)
by 0x10E4BB: iptables_main (iptables-standalone.c:61)
by 0x49A3349: (below main) (in /lib64/libc.so.6)

This is because repl->counters is not initialized upon allocation. Since the field is an array, make use of calloc() which implicitly does the initialization.

Fixes e37c0dc100c51 ("Revert the recent addition of memset()'s to TC_COMMIT. One of them is bogus and the other one needs more investigation to why valgrind is complaining.")
---
 libiptc/libiptc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libiptc/libiptc.c +2 -2
index 97823f935d1ee..f9b7779efdba5 100644
@@ -2554,8 +2554,8 @@ TC_COMMIT(struct xtc_handle *handle)
 			+ sizeof(STRUCT_COUNTERS) * new_number;
 
 	/* These are the old counters we will get from kernel */
-	repl->counters = malloc(sizeof(STRUCT_COUNTERS)
-				* handle->info.num_entries);
+	repl->counters = calloc(handle->info.num_entries,
+				sizeof(STRUCT_COUNTERS));
 	if (!repl->counters) {
 		errno = ENOMEM;
 		goto out_free_repl;
Signature · 1 line
2.38.0
author reviewer
#10 lore raw
In reply to Phil Sutter
Quoted 4 lines · Phil Sutter

When running the testsuite in "valgrind mode", numerous cases of lost memory and still reachable at program exit are reported. Address them all plus libiptc's access of uninitialized memory.

Quoted 10 lines

Phil Sutter (9):
tests: shell: Fix valgrind mode for 0008-unprivileged_0
iptables-restore: Free handle with --test also
iptables-xml: Free allocated chain strings
nft: Plug memleak in nft_rule_zero_counters()
iptables: Plug memleaks in print_firewall()
xtables: Introduce xtables_clear_iptables_command_state()
iptables: Properly clear iptables_command_state object
xshared: Free data after printing help
libiptc: Eliminate garbage access

Series applied.