Fix for shell testsuite '-V' option
Phil Sutter
reviewercover 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
Phil Sutter
reviewerPATCH 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
@@ -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
Phil Sutter
reviewerPATCH 2/9 iptables-restore: Free handle with --test also
In reply to Phil SutterWhen 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.
--- iptables/iptables-restore.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
iptables/iptables-restore.c +2 -2
@@ -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
Phil Sutter
reviewerPATCH 3/9 iptables-xml: Free allocated chain strings
In reply to Phil SutterFreeing 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().
--- iptables/iptables-xml.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
iptables/iptables-xml.c +5 -5
@@ -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
Phil Sutter
reviewerPATCH 4/9 nft: Plug memleak in nft_rule_zero_counters()
In reply to Phil SutterWhen 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.
--- iptables/nft.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
iptables/nft.c +3 -2
@@ -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
Phil Sutter
reviewerPATCH 5/9 iptables: Plug memleaks in print_firewall()
In reply to Phil SutterWhen 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
@@ -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->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
@@ -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->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
Phil Sutter
reviewerPATCH 6/9 xtables: Introduce xtables_clear_iptables_command_state()
In reply to Phil SutterThis 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Phil Sutter
reviewerPATCH 7/9 iptables: Properly clear iptables_command_state object
In reply to Phil SutterWhen 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.
--- iptables/ip6tables.c | 3 +-- iptables/iptables.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
iptables/ip6tables.c +1 -2
@@ -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
@@ -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
Phil Sutter
reviewerPATCH 8/9 xshared: Free data after printing help
In reply to Phil SutterThis 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
@@ -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
Phil Sutter
reviewerPATCH 9/9 libiptc: Eliminate garbage access
In reply to Phil SutterWhen 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.
--- libiptc/libiptc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libiptc/libiptc.c +2 -2
@@ -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
Phil Sutter
reviewerQuoted 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 innft_rule_zero_counters()
iptables: Plug memleaks inprint_firewall()
xtables: Introducextables_clear_iptables_command_state()
iptables: Properly cleariptables_command_stateobject
xshared: Free data after printing help
libiptc: Eliminate garbage access
Series applied.