Skip to contentSkip to conversation

Unbreak xtables-translate

PATCH In review Phil Sutter 3 participants 5 messages Nov 6, 2021 – Nov 8, 2021 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 Unbreak xtables-translate

Fixed commit broke xtables-translate which still relied upon do_parse() to properly initialize the passed iptables_command_state reference. To allow for callers to preset fields, this doesn't happen anymore so do_command_xlate() has to initialize itself. Otherwise garbage from stack is read leading to segfaults and program aborts.

Although init_cs callback is used by arptables only and arptables-translate has not been implemented, do call it if set just to avoid future issues.

Fixes cfdda18044d81 ("nft-shared: Introduce init_cs family ops callback")
---
 iptables/xtables-translate.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
iptables/xtables-translate.c +8 -1
index 086b85d2f9cef..e2948c5009dd6 100644
@@ -253,11 +253,18 @@ static int do_command_xlate(struct nft_handle *h, int argc, char *argv[],
 		.restore	= restore,
 		.xlate		= true,
 	};
-	struct iptables_command_state cs;
+	struct iptables_command_state cs = {
+		.jumpto = "",
+		.argv = argv,
+	};
+
 	struct xtables_args args = {
 		.family = h->family,
 	};
 
+	if (h->ops->init_cs)
+		h->ops->init_cs(&cs);
+
 	do_parse(h, argc, argv, &p, &cs, &args);
 
 	cs.restore = restore;
Signature · 1 line
2.33.0
#1 lore raw
Quoted 21 lines · Phil Sutter

Fixed commit broke xtables-translate which still relied upon do_parse() to properly initialize the passed iptables_command_state reference. To allow for callers to preset fields, this doesn't happen anymore so do_command_xlate() has to initialize itself. Otherwise garbage from stack is read leading to segfaults and program aborts.

Although init_cs callback is used by arptables only and arptables-translate has not been implemented, do call it if set just to avoid future issues.

Fixes cfdda18044d81 ("nft-shared: Introduce init_cs family ops callback")
---
 iptables/xtables-translate.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
iptables/xtables-translate.c +0 -0
index 086b85d2f9cef..e2948c5009dd6 100644

iptables/xtables-translate.c static int do_command_xlate(struct nft_handle *h, int argc, char *argv[], · line 259

Quoted 8 lines
iptables/xtables-translate.c +4 -1
@@ -253,11 +253,18 @@ static int do_command_xlate(struct nft_handle *h, int argc, char *argv[],
 		.restore	= restore,
 		.xlate		= true,
 	};
-	struct iptables_command_state cs;
+	struct iptables_command_state cs = {
+		.jumpto = "",
+		.argv = argv,
+	};

No need to initialize .jumpto explicitly: initializing .argv will zero-initialize all the other members.

Quoted 15 lines
iptables/xtables-translate.c +4 -1
+
 	struct xtables_args args = {
 		.family = h->family,
 	};
 
+	if (h->ops->init_cs)
+		h->ops->init_cs(&cs);
+
 	do_parse(h, argc, argv, &p, &cs, &args);
 
 	cs.restore = restore;
--

2.33.0

#2 lore raw
Quoted 18 lines · Jeremy Sowden
Quoted 17 lines · Phil Sutter

Fixed commit broke xtables-translate which still relied upon do_parse() to properly initialize the passed iptables_command_state reference. To allow for callers to preset fields, this doesn't happen anymore so do_command_xlate() has to initialize itself. Otherwise garbage from stack is read leading to segfaults and program aborts.

Although init_cs callback is used by arptables only and arptables-translate has not been implemented, do call it if set just to avoid future issues.

Fixes cfdda18044d81 ("nft-shared: Introduce init_cs family ops callback")
---
 iptables/xtables-translate.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Quoted 16 lines

iptables/xtables-translate.c static int do_command_xlate(struct nft_handle *h, int argc, char *argv[], · line 259

Quoted 13 lines
iptables/xtables-translate.c +4 -1
index 086b85d2f9cef..e2948c5009dd6 100644
@@ -253,11 +253,18 @@ static int do_command_xlate(struct nft_handle *h, int argc, char *argv[],
 		.restore	= restore,
 		.xlate		= true,
 	};
-	struct iptables_command_state cs;
+	struct iptables_command_state cs = {
+		.jumpto = "",
+		.argv = argv,
+	};

No need to initialize .jumpto explicitly: initializing .argv will zero-initialize all the other members.

Apologies, I'm talking nonsense: .jumpto is a pointer, not an array. Ignore me. :)

Quoted 15 lines
Quoted 15 lines
iptables/xtables-translate.c +4 -1
+
 	struct xtables_args args = {
 		.family = h->family,
 	};
 
+	if (h->ops->init_cs)
+		h->ops->init_cs(&cs);
+
 	do_parse(h, argc, argv, &p, &cs, &args);
 
 	cs.restore = restore;
--

2.33.0

author reviewer
#3 lore raw

Hey Jeremy,

On Sun, Nov 07, 2021 at 04:07:27PM +0000, Jeremy Sowden wrote:
[...]

Quoted 2 lines

Apologies, I'm talking nonsense: .jumpto is a pointer, not an array. Ignore me. :)

I wondered at first, but indeed assigning an empty string to an array is identical to setting all fields zero. :)

Thanks for the review!

Cheers, Phil

maintainer Tested-by on v1 1/1
#4 lore raw
In reply to Phil Sutter
Quoted 6 lines · Phil Sutter

Fixed commit broke xtables-translate which still relied upon do_parse() to properly initialize the passed iptables_command_state reference. To allow for callers to preset fields, this doesn't happen anymore so do_command_xlate() has to initialize itself. Otherwise garbage from stack is read leading to segfaults and program aborts.

Quoted 6 lines

Although init_cs callback is used by arptables only and arptables-translate has not been implemented, do call it if set just to avoid future issues.

Fixes cfdda18044d81 ("nft-shared: Introduce init_cs family ops callback")