libiptc memory leak - PATCH
Hi,
I've used libiptc for some utils and found a little problem with memory allocation, discused in January and may be earlier. Here is a small patch (r.1.2.8):
*** libiptc/libiptc.c.orig Mon Apr 21 23:54:48 2003
--- libiptc/libiptc.c Mon Apr 21 23:59:30 2003
***************
*** 504,511 ****
(*handle)->cache_chain_iteration++;
if ((*handle)->cache_chain_iteration - (*handle)->cache_chain_heads
! == (*handle)->cache_num_chains)
return NULL;
return (*handle)->cache_chain_iteration->name;
}
--- 504,513 ----
(*handle)->cache_chain_iteration++;
if ((*handle)->cache_chain_iteration - (*handle)->cache_chain_heads
! == (*handle)->cache_num_chains){
! free((*handle)->cache_chain_heads);
return NULL;+ }
return (*handle)->cache_chain_iteration->name;
}
T.v.L
Quoted 4 lines · Tomá? Lejdar
Hi,
I've used libiptc for some utils and found a little problem with memory allocation, discused in January and may be earlier. Here is a small patch (r.1.2.8):
thanks!
please send unified diffs (diff -u) in the future.
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
Quoted 10 lines · Tomá? Lejdar
(*handle)->cache_chain_iteration++; if ((*handle)->cache_chain_iteration - (*handle)->cache_chain_heads ! == (*handle)->cache_num_chains){ ! free((*handle)->cache_chain_heads); return NULL;+ }
return (*handle)->cache_chain_iteration->name;}
I cannot accept this patch It would basically mean that something like:
first_chain()next_chain()first_chain()
would no longer work.
we need an explicit free_handle that cleans up all allocated regions instead. Patches are welcome.
Quoted 1 line
T.v.L
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
Hi,
I thing there is no problem, the memory is freed only when the function next_chain returns NULL and run through the chains is on the end. In the next call of first_chain() is memory alocated again in populate_cache().
Is there something wrong in my idea ?
T.v.L
Harald Welte wrote:
I cannot accept this patch It would basically mean that something like:
Quoted 10 lines
first_chain()next_chain()first_chain()would no longer work.
we need an explicit
free_handlethat cleans up all allocated regions instead. Patches are welcome.
Quoted 5 lines · Tomá? Lejdar
Hi,
I thing there is no problem, the memory is freed only when the functionnext_chainreturns NULL and run through the chains is on the end. In the next call offirst_chain()is memory alocated again inpopulate_cache().
no, it's fine. It was a misunderstanding. I'll apply your patch now. Sorry for the fuzz.
Quoted 1 line
T.v.L
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
Quoted 9 lines · Harald Welte
Quoted 5 lines · Tomá? Lejdar
Hi,
I thing there is no problem, the memory is freed only when the functionnext_chainreturns NULL and run through the chains is on the end. In the next call offirst_chain()is memory alocated again inpopulate_cache().no, it's fine. It was a misunderstanding. I'll apply your patch now. Sorry for the fuzz.
This patch breaks {ip,ip6}tables-{save,restore} and it runs free() on the wrong memoryaddress (I think free() does the right thing anyway but the patch still breaks stuff).
-{save,restore} segfaulted with the patch.
I've attached a patch that reverts this patch and adds iptc_exit(iptc_handle_t h) that takes care of the freeing. It also contains some small cleanups.
The patch also fixes iptables, ip6tables and their -{save,restore} cousins to not leak memory.
I've tested iptables and iptables-{save,restore} and they appear to work just fine.
Please apply or at least revert the previous patch.
Signature · 1 line
/Martin
include/libiptc/libiptc.h +3 -0
@@ -34,6 +34,9 @@/* Take a snapshot of the rules. Returns NULL on error. */ iptc_handle_t iptc_init (const char *tablename); +/* Cleanup after iptc_init(). */ +void iptc_exit (iptc_handle_t h); +/* Iterator functions to run through the chains. Returns NULL at end. */ const char *iptc_first_chain (iptc_handle_t *handle);const char *iptc_next_chain (iptc_handle_t *handle);
ip6tables-restore.c +4 -1
@@ -102,7 +102,7 @@int main (int argc,char *argv[]) { -ip6tc_handle_t handle; +ip6tc_handle_t handle =NULL ;char buffer[10240 ];int c;char curtable[IP6T_TABLE_MAXNAMELEN +1 ];
@@ -183,6 +183,9 @@ }strncpy (curtable, table,IP6T_TABLE_MAXNAMELEN ); +if (handle) +ip6tc_exit (handle); + handle =create_handle (table, modprobe);if (noflush ==0 ) { DEBUGP("Cleaning all chains of table '%s'\n",
ip6tables-save.c +2 -0
@@ -305,6 +305,8 @@exit_error (OTHER_PROBLEM ,"Binary NYI\n" ); } +ip6tc_exit (h); +return 1 ; }
ip6tables.c +9 -1
@@ -1670,6 +1670,7 @@const char *modprobe =NULL ;int proto_used =0 ;char icmp6p[] ="icmpv6" ; +int no_handle =0 ;memset (&fw,0 ,sizeof (fw));
@@ -2147,8 +2148,10 @@ chain, IP6T_FUNCTION_MAXNAMELEN);/* only allocate handle if we weren't called with a handle */ -if (!*handle) +if (!*handle) { *handle =ip6tc_init (*table); + no_handle =1 ; + }if (!*handle) {/* try to insmod the module if iptc_init failed */
@@ -2293,5 +2296,10 @@if (verbose >1 )dump_entries6 (*handle); +if (no_handle) { +ip6tc_exit (*handle); + *handle =NULL ; + } +return ret; }
iptables-restore.c +4 -1
@@ -99,7 +99,7 @@int main (int argc,char *argv[]) { -iptc_handle_t handle; +iptc_handle_t handle =NULL ;char buffer[10240 ];int c;char curtable[IPT_TABLE_MAXNAMELEN +1 ];
@@ -180,6 +180,9 @@ }strncpy (curtable, table,IPT_TABLE_MAXNAMELEN ); +if (handle) +iptc_exit (handle); + handle =create_handle (table, modprobe);if (noflush ==0 ) { DEBUGP("Cleaning all chains of table '%s'\n",
iptables-save.c +2 -0
@@ -304,6 +304,8 @@exit_error (OTHER_PROBLEM ,"Binary NYI\n" ); } +iptc_exit (h); +return 1 ; }
iptables.c +9 -1
@@ -1668,6 +1668,7 @@char *protocol =NULL ;const char *modprobe =NULL ;int proto_used =0 ; +int no_handle =0 ;memset (&fw,0 ,sizeof (fw));
@@ -2148,8 +2149,10 @@ chain, IPT_FUNCTION_MAXNAMELEN);/* only allocate handle if we weren't called with a handle */ -if (!*handle) +if (!*handle) { *handle =iptc_init (*table); + no_handle =1 ; + }if (!*handle) {/* try to insmod the module if iptc_init failed */
@@ -2294,5 +2297,10 @@if (verbose >1 )dump_entries (*handle); +if (no_handle) { +iptc_exit (*handle); + *handle =NULL ; + } +return ret; }
libiptc/libip4tc.c +1 -0
@@ -91,6 +91,7 @@#define TC_SET_POLICY iptc_set_policy#define TC_GET_RAW_SOCKET iptc_get_raw_socket#define TC_INIT iptc_init +#define TC_EXIT iptc_exit#define TC_COMMIT iptc_commit#define TC_STRERROR iptc_strerror
libiptc/libip6tc.c +1 -0
@@ -86,6 +86,7 @@#define TC_SET_POLICY ip6tc_set_policy#define TC_GET_RAW_SOCKET ip6tc_get_raw_socket#define TC_INIT ip6tc_init +#define TC_EXIT ip6tc_exit#define TC_COMMIT ip6tc_commit#define TC_STRERROR ip6tc_strerror
libiptc/libiptc.c +23 -11
@@ -237,22 +237,26 @@if (sockfd !=-1 )close (sockfd); +if (strlen (tablename) >=TABLE_MAXNAMELEN ) { + errno =EINVAL ; +return NULL ; + } + sockfd =socket (TC_AF ,SOCK_RAW ,IPPROTO_RAW );if (sockfd <0 )return NULL ; s =sizeof (info); -if (strlen (tablename) >=TABLE_MAXNAMELEN ) { - errno =EINVAL ; -return NULL ; - } +strcpy (info.name, tablename);if (getsockopt (sockfd,TC_IPPROTO ,SO_GET_INFO , &info, &s) <0 )return NULL ;if ((h =alloc_handle (info.name, info.size, info.num_entries)) - ==NULL ) + ==NULL ) { +close (sockfd);return NULL ; + }/* Too hard --RR */ #if 0
@@ -284,6 +288,7 @@if (getsockopt (sockfd,TC_IPPROTO ,SO_GET_ENTRIES , &h->entries, &tmp) <0 ) { +close (sockfd);free (h);return NULL ; }
@@ -292,6 +297,13 @@return h; } +void +TC_EXIT (TC_HANDLE_T h) +{ +close (sockfd); +free (h); +} +static inline int print_match (const STRUCT_ENTRY_MATCH *m) {
@@ -504,10 +516,8 @@ (*handle)->cache_chain_iteration++;if ((*handle)->cache_chain_iteration - (*handle)->cache_chain_heads - == (*handle)->cache_num_chains) { -free ((*handle)->cache_chain_heads); + == (*handle)->cache_num_chains)return NULL ; - }return (*handle)->cache_chain_iteration->name; }
@@ -1584,11 +1594,13 @@STRUCT_REPLACE *repl;STRUCT_COUNTERS_INFO *newcounters;unsigned int i; -size_t counterlen - =sizeof (STRUCT_COUNTERS_INFO ) - +sizeof (STRUCT_COUNTERS ) * (*handle)->new_number; +size_t counterlen;CHECK (*handle); + + counterlen =sizeof (STRUCT_COUNTERS_INFO ) + +sizeof (STRUCT_COUNTERS ) * (*handle)->new_number; +#if 0 TC_DUMP_ENTRIES (*handle);#endif
Quoted 3 lines · Martin Josefsson
This patch breaks {ip,ip6}tables-{save,restore} and it runs
free()on the wrong memoryaddress (I thinkfree()does the right thing anyway but the patch still breaks stuff).
Ok, I was wrong, it did free on a correct memoryaddress, I'll fix up my patch and resend, sorry about that.
Signature · 1 line
/Martin
Quoted 8 lines · Martin Josefsson
Quoted 3 lines · Martin Josefsson
This patch breaks {ip,ip6}tables-{save,restore} and it runs
free()on the wrong memoryaddress (I thinkfree()does the right thing anyway but the patch still breaks stuff).Ok, I was wrong, it did free on a correct memoryaddress, I'll fix up my patch and resend, sorry about that.
Here's an updated patch that fixes the memory-leak in the previous one.
I've renamed {ip,ip6}tc_exit(iptc_handle_t h) into {ip,ip6}tc_free(iptc_handle_t *h)
This should fix it correctly (I hope), iptables, iptables-{save,restore} works fine.
Signature · 1 line
/Martin
include/libiptc/libiptc.h +3 -0
@@ -34,6 +34,9 @@/* Take a snapshot of the rules. Returns NULL on error. */ iptc_handle_t iptc_init (const char *tablename); +/* Cleanup after iptc_init(). */ +void iptc_free (iptc_handle_t *h); +/* Iterator functions to run through the chains. Returns NULL at end. */ const char *iptc_first_chain (iptc_handle_t *handle);const char *iptc_next_chain (iptc_handle_t *handle);
ip6tables-restore.c +4 -1
@@ -102,7 +102,7 @@int main (int argc,char *argv[]) { -ip6tc_handle_t handle; +ip6tc_handle_t handle =NULL ;char buffer[10240 ];int c;char curtable[IP6T_TABLE_MAXNAMELEN +1 ];
@@ -183,6 +183,9 @@ }strncpy (curtable, table,IP6T_TABLE_MAXNAMELEN ); +if (handle) +ip6tc_free (&handle); + handle =create_handle (table, modprobe);if (noflush ==0 ) { DEBUGP("Cleaning all chains of table '%s'\n",
ip6tables-save.c +2 -0
@@ -305,6 +305,8 @@exit_error (OTHER_PROBLEM ,"Binary NYI\n" ); } +ip6tc_free (&h); +return 1 ; }
ip6tables.c +7 -1
@@ -1670,6 +1670,7 @@const char *modprobe =NULL ;int proto_used =0 ;char icmp6p[] ="icmpv6" ; +int no_handle =0 ;memset (&fw,0 ,sizeof (fw));
@@ -2147,8 +2148,10 @@ chain, IP6T_FUNCTION_MAXNAMELEN);/* only allocate handle if we weren't called with a handle */ -if (!*handle) +if (!*handle) { *handle =ip6tc_init (*table); + no_handle =1 ; + }if (!*handle) {/* try to insmod the module if iptc_init failed */
@@ -2293,5 +2296,8 @@if (verbose >1 )dump_entries6 (*handle); +if (no_handle) +ip6tc_free (handle); +return ret; }
iptables-restore.c +4 -1
@@ -99,7 +99,7 @@int main (int argc,char *argv[]) { -iptc_handle_t handle; +iptc_handle_t handle =NULL ;char buffer[10240 ];int c;char curtable[IPT_TABLE_MAXNAMELEN +1 ];
@@ -180,6 +180,9 @@ }strncpy (curtable, table,IPT_TABLE_MAXNAMELEN ); +if (handle) +iptc_free (&handle); + handle =create_handle (table, modprobe);if (noflush ==0 ) { DEBUGP("Cleaning all chains of table '%s'\n",
iptables-save.c +2 -0
@@ -304,6 +304,8 @@exit_error (OTHER_PROBLEM ,"Binary NYI\n" ); } +iptc_free (&h); +return 1 ; }
iptables.c +7 -1
@@ -1668,6 +1668,7 @@char *protocol =NULL ;const char *modprobe =NULL ;int proto_used =0 ; +int no_handle =0 ;memset (&fw,0 ,sizeof (fw));
@@ -2148,8 +2149,10 @@ chain, IPT_FUNCTION_MAXNAMELEN);/* only allocate handle if we weren't called with a handle */ -if (!*handle) +if (!*handle) { *handle =iptc_init (*table); + no_handle =1 ; + }if (!*handle) {/* try to insmod the module if iptc_init failed */
@@ -2294,5 +2297,8 @@if (verbose >1 )dump_entries (*handle); +if (no_handle) +iptc_free (handle); +return ret; }
libiptc/libip4tc.c +1 -0
@@ -91,6 +91,7 @@#define TC_SET_POLICY iptc_set_policy#define TC_GET_RAW_SOCKET iptc_get_raw_socket#define TC_INIT iptc_init +#define TC_FREE iptc_free#define TC_COMMIT iptc_commit#define TC_STRERROR iptc_strerror
libiptc/libip6tc.c +1 -0
@@ -86,6 +86,7 @@#define TC_SET_POLICY ip6tc_set_policy#define TC_GET_RAW_SOCKET ip6tc_get_raw_socket#define TC_INIT ip6tc_init +#define TC_FREE ip6tc_free#define TC_COMMIT ip6tc_commit#define TC_STRERROR ip6tc_strerror
libiptc/libiptc.c +27 -15
92 lines not on this page · raw message
Quoted 8 lines · Martin Josefsson
Quoted 7 lines · Harald Welte
Quoted 5 lines · Tomá? Lejdar
Hi,
I thing there is no problem, the memory is freed only when the functionnext_chainreturns NULL and run through the chains is on the end. In the next call offirst_chain()is memory alocated again inpopulate_cache().
Quoted 6 lines
Quoted 2 lines
no, it's fine. It was a misunderstanding. I'll apply your patch now. Sorry for the fuzz.
This patch breaks {ip,ip6}tables-{save,restore} and it runs
free()on the wrong memoryaddress (I thinkfree()does the right thing anyway but the patch still breaks stuff).
*sigh*. I knew that I should have tested it before accepting.
Quoted 3 lines
I've attached a patch that reverts this patch and adds
iptc_exit(iptc_handle_t h)that takes care of the freeing. It also contains some small cleanups.
I'll apply your patch, thanks Martin.
Quoted 1 line
/Martin
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
Quoted 11 lines · Martin Josefsson
Quoted 8 lines · Martin Josefsson
Quoted 3 lines · Martin Josefsson
This patch breaks {ip,ip6}tables-{save,restore} and it runs
free()on the wrong memoryaddress (I thinkfree()does the right thing anyway but the patch still breaks stuff).Ok, I was wrong, it did free on a correct memoryaddress, I'll fix up my patch and resend, sorry about that.
Here's an updated patch that fixes the memory-leak in the previous one.
applied this one instead of the other.
Quoted 2 lines
This should fix it correctly (I hope), iptables, iptables-{save,restore} works fine.
we'll see. I think iptables-1.2.9 is in a quite distant future, so I hope we discover any potentially introduced bugs until then.
Quoted 1 line
/Martin
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
Quoted 5 lines · Harald Welte
Quoted 2 lines
This should fix it correctly (I hope), iptables, iptables-{save,restore} works fine.
we'll see. I think iptables-1.2.9 is in a quite distant future, so I hope we discover any potentially introduced bugs until then.
I broke it a little...
I've attached a fix...
it fixes iptables.c and ip6tables.c
Signature · 1 line
/Martin
iptables.c renamed from iptables.c.orig +1 -7
@@ -1668,7 +1668,6 @@char *protocol =NULL ;const char *modprobe =NULL ;int proto_used =0 ; -int no_handle =0 ;memset (&fw,0 ,sizeof (fw));
@@ -2149,10 +2148,8 @@ chain, IPT_FUNCTION_MAXNAMELEN);/* only allocate handle if we weren't called with a handle */ -if (!*handle) { +if (!*handle) *handle =iptc_init (*table); - no_handle =1 ; - }if (!*handle) {/* try to insmod the module if iptc_init failed */
@@ -2297,8 +2294,5 @@if (verbose >1 )dump_entries (*handle); -if (no_handle) -iptc_free (handle); -return ret; }
ip6tables.c renamed from ip6tables.c.orig +1 -7
@@ -1670,7 +1670,6 @@const char *modprobe =NULL ;int proto_used =0 ;char icmp6p[] ="icmpv6" ; -int no_handle =0 ;memset (&fw,0 ,sizeof (fw));
@@ -2148,10 +2147,8 @@ chain, IP6T_FUNCTION_MAXNAMELEN);/* only allocate handle if we weren't called with a handle */ -if (!*handle) { +if (!*handle) *handle =ip6tc_init (*table); - no_handle =1 ; - }if (!*handle) {/* try to insmod the module if iptc_init failed */
@@ -2296,8 +2293,5 @@if (verbose >1 )dump_entries6 (*handle); -if (no_handle) -ip6tc_free (handle); -return ret; }