* [PATCH iptables] libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric
@ 2024-03-23 3:06 Vitaly Chikunov
2024-03-23 9:17 ` Vitaly Chikunov
2024-03-23 13:56 ` Phil Sutter
0 siblings, 2 replies; 5+ messages in thread
From: Vitaly Chikunov @ 2024-03-23 3:06 UTC (permalink / raw)
To: Pablo Neira Ayuso, Florian Westphal, netfilter-devel
Cc: Vitaly Chikunov, Jan Engelhardt, Gleb Fotengauer-Malinovskiy
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)
Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
Cc: Jan Engelhardt <jengelh@inai.de>
Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
---
libxtables/xtables.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index 748a50fc..16a0640d 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -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]);
--
2.42.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH iptables] libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric
2024-03-23 3:06 [PATCH iptables] libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric Vitaly Chikunov
@ 2024-03-23 9:17 ` Vitaly Chikunov
2024-03-23 13:56 ` Phil Sutter
1 sibling, 0 replies; 5+ messages in thread
From: Vitaly Chikunov @ 2024-03-23 9:17 UTC (permalink / raw)
To: Pablo Neira Ayuso, Florian Westphal, netfilter-devel
Cc: Jan Engelhardt, Gleb Fotengauer-Malinovskiy
On Sat, Mar 23, 2024 at 06:06:41AM +0300, Vitaly Chikunov wrote:
> 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.
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,
>
> 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 <vt@altlinux.org>
> Cc: Jan Engelhardt <jengelh@inai.de>
> Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
> ---
> libxtables/xtables.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/libxtables/xtables.c b/libxtables/xtables.c
> index 748a50fc..16a0640d 100644
> --- a/libxtables/xtables.c
> +++ b/libxtables/xtables.c
> @@ -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]);
> --
> 2.42.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH iptables] libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric
2024-03-23 3:06 [PATCH iptables] libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric Vitaly Chikunov
2024-03-23 9:17 ` Vitaly Chikunov
@ 2024-03-23 13:56 ` Phil Sutter
2024-03-23 21:37 ` Vitaly Chikunov
1 sibling, 1 reply; 5+ messages in thread
From: Phil Sutter @ 2024-03-23 13:56 UTC (permalink / raw)
To: Vitaly Chikunov
Cc: Pablo Neira Ayuso, Florian Westphal, netfilter-devel,
Jan Engelhardt, Gleb Fotengauer-Malinovskiy
[-- Attachment #1: Type: text/plain, Size: 775 bytes --]
On Sat, Mar 23, 2024 at 06:06:41AM +0300, Vitaly Chikunov wrote:
> 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.
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
[-- Attachment #2: __xtables_ipaddr_to_numeric.diff --]
[-- Type: text/plain, Size: 1197 bytes --]
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index f2fcc5c22fb61..54df1bc9336dd 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -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" */
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH iptables] libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric
2024-03-23 13:56 ` Phil Sutter
@ 2024-03-23 21:37 ` Vitaly Chikunov
2024-03-24 13:50 ` Phil Sutter
0 siblings, 1 reply; 5+ messages in thread
From: Vitaly Chikunov @ 2024-03-23 21:37 UTC (permalink / raw)
To: Phil Sutter, Pablo Neira Ayuso, Florian Westphal, netfilter-devel,
Jan Engelhardt, Gleb Fotengauer-Malinovskiy
On Sat, Mar 23, 2024 at 02:56:43PM +0100, Phil Sutter wrote:
> On Sat, Mar 23, 2024 at 06:06:41AM +0300, Vitaly Chikunov wrote:
> > 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.
>
> 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,
>
> Thanks, Phil
> diff --git a/libxtables/xtables.c b/libxtables/xtables.c
> index f2fcc5c22fb61..54df1bc9336dd 100644
> --- a/libxtables/xtables.c
> +++ b/libxtables/xtables.c
> @@ -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" */
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH iptables] libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric
2024-03-23 21:37 ` Vitaly Chikunov
@ 2024-03-24 13:50 ` Phil Sutter
0 siblings, 0 replies; 5+ messages in thread
From: Phil Sutter @ 2024-03-24 13:50 UTC (permalink / raw)
To: Vitaly Chikunov
Cc: Pablo Neira Ayuso, Florian Westphal, netfilter-devel,
Jan Engelhardt, Gleb Fotengauer-Malinovskiy
On Sun, Mar 24, 2024 at 12:37:53AM +0300, Vitaly Chikunov wrote:
> On Sat, Mar 23, 2024 at 02:56:43PM +0100, Phil Sutter wrote:
> > On Sat, Mar 23, 2024 at 06:06:41AM +0300, Vitaly Chikunov wrote:
> > > 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.
> >
> > 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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-03-24 13:51 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-23 3:06 [PATCH iptables] libxtables: Fix xtables_ipaddr_to_numeric calls with xtables_ipmask_to_numeric Vitaly Chikunov
2024-03-23 9:17 ` Vitaly Chikunov
2024-03-23 13:56 ` Phil Sutter
2024-03-23 21:37 ` Vitaly Chikunov
2024-03-24 13:50 ` Phil Sutter
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.