libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric
PATCH 1/1 libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric
Frequently when addr/mask is printed xtables_ipaddr_to_numeric and xtables_ipmask_to_numeric are called together in one printf call but xtables_ipmask_to_numeric internally calls xtables_ipaddr_to_numeric which prints into the same static buffer causing buffer to be overwritten and addr/mask incorrectly printed in such call scenarios.
Make xtables_ipaddr_to_numeric to use two static buffers rotating their use. This simplistic approach will leave ABI not changed and cover all such use cases.
Interestingly, testing stumbled over this only on non-x86 architectures. Error message:
extensions/libebt_arp.t: ERROR: line 11 (cannot find: ebtables -I INPUT -p ARP --arp-ip-src ! 1.2.3.4/255.0.255.255)
--- libxtables/xtables.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
libxtables/xtables.c +3 -1
@@ -1505,7 +1505,9 @@ void xtables_param_act(unsigned int status, const char *p1, ...)const char *xtables_ipaddr_to_numeric (const struct in_addr *addrp) { -static char buf[16 ]; +static char abuf[2 ][16 ]; +static int bufnum =0 ; +char *buf = abuf[++bufnum &1 ];const unsigned char *bytep = (const void *)&addrp->s_addr;sprintf (buf,"%u.%u.%u.%u" , bytep[0 ], bytep[1 ], bytep[2 ], bytep[3 ]);
Signature · 1 line
2.42.1
Quoted 9 lines · Vitaly Chikunov
Frequently when addr/mask is printed
xtables_ipaddr_to_numericandxtables_ipmask_to_numericare called together in one printf call butxtables_ipmask_to_numericinternally callsxtables_ipaddr_to_numericwhich prints into the same static buffer causing buffer to be overwritten and addr/mask incorrectly printed in such call scenarios.Make
xtables_ipaddr_to_numericto use two static buffers rotating their use. This simplistic approach will leave ABI not changed and cover all such use cases.
Additional thoughts are: the call order of these two function is important in will it override buf or not. Order of evaluation of function arguments is unspecified[1] and compile time dependent. It seems that on non-x86 architectures, sometime it could be different from left-to-right. That's why this bug was unnoticed for so much time.
[1] https://en.cppreference.com/w/c/language/eval_order
Thanks,
Quoted 30 lines
Interestingly, testing stumbled over this only on non-x86 architectures. Error message:
extensions/libebt_arp.t: ERROR: line 11 (cannot find: ebtables -I INPUT -p ARP --arp-ip-src ! 1.2.3.4/255.0.255.255)Signed-off-by Vitaly Chikunov <[email protected]>Cc Jan Engelhardt <[email protected]>Cc Gleb Fotengauer-Malinovskiy <[email protected]>--- libxtables/xtables.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)libxtables/xtables.c +3
-1@@ -1505,7 +1505,9 @@ void xtables_param_act(unsigned int status, const char *p1, ...)const char *xtables_ipaddr_to_numeric (const struct in_addr *addrp) { -static char buf[16 ]; +static char abuf[2 ][16 ]; +static int bufnum =0 ; +char *buf = abuf[++bufnum &1 ];const unsigned char *bytep = (const void *)&addrp->s_addr;sprintf (buf,"%u.%u.%u.%u" , bytep[0 ], bytep[1 ], bytep[2 ], bytep[3 ]);Signature · 1 line
2.42.1
Phil Sutter
reviewerQuoted 9 lines · Vitaly Chikunov
Frequently when addr/mask is printed
xtables_ipaddr_to_numericandxtables_ipmask_to_numericare called together in one printf call butxtables_ipmask_to_numericinternally callsxtables_ipaddr_to_numericwhich prints into the same static buffer causing buffer to be overwritten and addr/mask incorrectly printed in such call scenarios.Make
xtables_ipaddr_to_numericto use two static buffers rotating their use. This simplistic approach will leave ABI not changed and cover all such use cases.
I don't quite like the cat'n'mouse game this opens, although it's unlikely someone calls it a third time before copying the buffer.
What do you think about the attached solution?
Thanks, Phil
libxtables/xtables.c +11 -3
@@ -1511,12 +1511,19 @@ void xtables_param_act(unsigned int status, const char *p1, ...)va_end (args); } +static void +__xtables_ipaddr_to_numeric (const struct in_addr *addrp,char *bufp) +{ +const unsigned char *bytep = (const void *)&addrp->s_addr; + +sprintf (bufp,"%u.%u.%u.%u" , bytep[0 ], bytep[1 ], bytep[2 ], bytep[3 ]); +} +const char *xtables_ipaddr_to_numeric (const struct in_addr *addrp) {static char buf[16 ]; -const unsigned char *bytep = (const void *)&addrp->s_addr; -sprintf (buf,"%u.%u.%u.%u" , bytep[0 ], bytep[1 ], bytep[2 ], bytep[3 ]); +__xtables_ipaddr_to_numeric (addrp, buf);return buf; }
@@ -1583,7 +1590,8 @@ const char *xtables_ipmask_to_numeric(const struct in_addr *mask) cidr =xtables_ipmask_to_cidr (mask);if (cidr == (unsigned int )-1 ) {/* mask was not a decent combination of 1's and 0's */ -sprintf (buf,"/%s" ,xtables_ipaddr_to_numeric (mask)); + buf[0 ] ='/' ; +__xtables_ipaddr_to_numeric (mask, buf +1 );return buf; }else if (cidr ==32 ) {/* we don't want to see "/32" */
Quoted 7 lines · Phil Sutter
Quoted 6 lines · Vitaly Chikunov
Frequently when addr/mask is printed
xtables_ipaddr_to_numericandxtables_ipmask_to_numericare called together in one printf call butxtables_ipmask_to_numericinternally callsxtables_ipaddr_to_numericwhich prints into the same static buffer causing buffer to be overwritten and addr/mask incorrectly printed in such call scenarios.
Quoted 8 lines
Quoted 3 lines
Make
xtables_ipaddr_to_numericto use two static buffers rotating their use. This simplistic approach will leave ABI not changed and cover all such use cases.I don't quite like the cat'n'mouse game this opens, although it's unlikely someone calls it a third time before copying the buffer.
What do you think about the attached solution?
Your approach is indeed much better. But why double underscore prefix to a function name, this sounds like reserved identifiers.
https://en.cppreference.com/w/c/language/identifier
Thanks,
Quoted 39 lines
Thanks, Phil
libxtables/xtables.c +11
-3@@ -1511,12 +1511,19 @@ void xtables_param_act(unsigned int status, const char *p1, ...)va_end (args); } +static void +__xtables_ipaddr_to_numeric (const struct in_addr *addrp,char *bufp) +{ +const unsigned char *bytep = (const void *)&addrp->s_addr; + +sprintf (bufp,"%u.%u.%u.%u" , bytep[0 ], bytep[1 ], bytep[2 ], bytep[3 ]); +} +const char *xtables_ipaddr_to_numeric (const struct in_addr *addrp) {static char buf[16 ]; -const unsigned char *bytep = (const void *)&addrp->s_addr; -sprintf (buf,"%u.%u.%u.%u" , bytep[0 ], bytep[1 ], bytep[2 ], bytep[3 ]); +__xtables_ipaddr_to_numeric (addrp, buf);return buf; }@@ -1583,7 +1590,8 @@ const char *xtables_ipmask_to_numeric(const struct in_addr *mask) cidr =xtables_ipmask_to_cidr (mask);if (cidr == (unsigned int )-1 ) {/* mask was not a decent combination of 1's and 0's */ -sprintf (buf,"/%s" ,xtables_ipaddr_to_numeric (mask)); + buf[0 ] ='/' ; +__xtables_ipaddr_to_numeric (mask, buf +1 );return buf; }else if (cidr ==32 ) {/* we don't want to see "/32" */
Phil Sutter
reviewerQuoted 12 lines · Vitaly Chikunov
Quoted 11 lines · Phil Sutter
Quoted 9 lines · Vitaly Chikunov
Frequently when addr/mask is printed
xtables_ipaddr_to_numericandxtables_ipmask_to_numericare called together in one printf call butxtables_ipmask_to_numericinternally callsxtables_ipaddr_to_numericwhich prints into the same static buffer causing buffer to be overwritten and addr/mask incorrectly printed in such call scenarios.Make
xtables_ipaddr_to_numericto use two static buffers rotating their use. This simplistic approach will leave ABI not changed and cover all such use cases.
Quoted 7 lines
Quoted 4 lines
I don't quite like the cat'n'mouse game this opens, although it's unlikely someone calls it a third time before copying the buffer.
What do you think about the attached solution?
Your approach is indeed much better. But why double underscore prefix to a function name, this sounds like reserved identifiers.
Well, for once it was just a quick sketch. Also, when refactoring into an inner function it is not uncommon to prefix it this way, at least if it's an internal-only function.
Another option I could think of is _r suffix, typically used for reentrant variants.
Cheers, Phil