Blame SOURCES/0015-xtables-Review-nft_init.patch

e8e25e
From 51f895d54af6e163e0290520e124e9413438ccf4 Mon Sep 17 00:00:00 2001
e8e25e
From: Phil Sutter <phil@nwl.cc>
e8e25e
Date: Fri, 21 Feb 2020 14:55:52 +0100
e8e25e
Subject: [PATCH] xtables: Review nft_init()
e8e25e
e8e25e
Move common code into nft_init(), such as:
e8e25e
e8e25e
* initial zeroing nft_handle fields
e8e25e
* family ops lookup and assignment to 'ops' field
e8e25e
* setting of 'family' field
e8e25e
e8e25e
This requires minor adjustments in xtables_restore_main() so extra field
e8e25e
initialization doesn't happen before nft_init() call.
e8e25e
e8e25e
As a side-effect, this fixes segfaulting xtables-monitor binary when
e8e25e
printing rules for trace event as in that code-path 'ops' field wasn't
e8e25e
initialized.
e8e25e
e8e25e
Signed-off-by: Phil Sutter <phil@nwl.cc>
e8e25e
(cherry picked from commit d0446ab11182f6ca2adc486a124895f09a220c6e)
e8e25e
Signed-off-by: Phil Sutter <psutter@redhat.com>
e8e25e
---
e8e25e
 iptables/nft.c                |  9 ++++++++-
e8e25e
 iptables/nft.h                |  2 +-
e8e25e
 iptables/xtables-arp.c        |  9 +--------
e8e25e
 iptables/xtables-eb.c         |  9 +--------
e8e25e
 iptables/xtables-monitor.c    |  2 +-
e8e25e
 iptables/xtables-restore.c    | 14 +++++++-------
e8e25e
 iptables/xtables-save.c       |  9 ++-------
e8e25e
 iptables/xtables-standalone.c |  6 ++----
e8e25e
 iptables/xtables-translate.c  |  2 +-
e8e25e
 iptables/xtables.c            |  4 ----
e8e25e
 10 files changed, 24 insertions(+), 42 deletions(-)
e8e25e
e8e25e
diff --git a/iptables/nft.c b/iptables/nft.c
e8e25e
index 3f2a62ae12c07..0287add3fb21f 100644
e8e25e
--- a/iptables/nft.c
e8e25e
+++ b/iptables/nft.c
e8e25e
@@ -789,8 +789,10 @@ int nft_restart(struct nft_handle *h)
e8e25e
 	return 0;
e8e25e
 }
e8e25e
 
e8e25e
-int nft_init(struct nft_handle *h, const struct builtin_table *t)
e8e25e
+int nft_init(struct nft_handle *h, int family, const struct builtin_table *t)
e8e25e
 {
e8e25e
+	memset(h, 0, sizeof(*h));
e8e25e
+
e8e25e
 	h->nl = mnl_socket_open(NETLINK_NETFILTER);
e8e25e
 	if (h->nl == NULL)
e8e25e
 		return -1;
e8e25e
@@ -800,9 +802,14 @@ int nft_init(struct nft_handle *h, const struct builtin_table *t)
e8e25e
 		return -1;
e8e25e
 	}
e8e25e
 
e8e25e
+	h->ops = nft_family_ops_lookup(family);
e8e25e
+	if (!h->ops)
e8e25e
+		xtables_error(PARAMETER_PROBLEM, "Unknown family");
e8e25e
+
e8e25e
 	h->portid = mnl_socket_get_portid(h->nl);
e8e25e
 	h->tables = t;
e8e25e
 	h->cache = &h->__cache[0];
e8e25e
+	h->family = family;
e8e25e
 
e8e25e
 	INIT_LIST_HEAD(&h->obj_list);
e8e25e
 	INIT_LIST_HEAD(&h->err_list);
e8e25e
diff --git a/iptables/nft.h b/iptables/nft.h
e8e25e
index 51b5660314c0c..5cf260a6d2cd3 100644
e8e25e
--- a/iptables/nft.h
e8e25e
+++ b/iptables/nft.h
e8e25e
@@ -80,7 +80,7 @@ extern const struct builtin_table xtables_bridge[NFT_TABLE_MAX];
e8e25e
 int mnl_talk(struct nft_handle *h, struct nlmsghdr *nlh,
e8e25e
 	     int (*cb)(const struct nlmsghdr *nlh, void *data),
e8e25e
 	     void *data);
e8e25e
-int nft_init(struct nft_handle *h, const struct builtin_table *t);
e8e25e
+int nft_init(struct nft_handle *h, int family, const struct builtin_table *t);
e8e25e
 void nft_fini(struct nft_handle *h);
e8e25e
 int nft_restart(struct nft_handle *h);
e8e25e
 
e8e25e
diff --git a/iptables/xtables-arp.c b/iptables/xtables-arp.c
e8e25e
index 9cfad76263d32..c8196f08baa59 100644
e8e25e
--- a/iptables/xtables-arp.c
e8e25e
+++ b/iptables/xtables-arp.c
e8e25e
@@ -500,17 +500,10 @@ int nft_init_arp(struct nft_handle *h, const char *pname)
e8e25e
 	init_extensionsa();
e8e25e
 #endif
e8e25e
 
e8e25e
-	memset(h, 0, sizeof(*h));
e8e25e
-	h->family = NFPROTO_ARP;
e8e25e
-
e8e25e
-	if (nft_init(h, xtables_arp) < 0)
e8e25e
+	if (nft_init(h, NFPROTO_ARP, xtables_arp) < 0)
e8e25e
 		xtables_error(OTHER_PROBLEM,
e8e25e
 			      "Could not initialize nftables layer.");
e8e25e
 
e8e25e
-	h->ops = nft_family_ops_lookup(h->family);
e8e25e
-	if (h->ops == NULL)
e8e25e
-		xtables_error(PARAMETER_PROBLEM, "Unknown family");
e8e25e
-
e8e25e
 	return 0;
e8e25e
 }
e8e25e
 
e8e25e
diff --git a/iptables/xtables-eb.c b/iptables/xtables-eb.c
e8e25e
index 15b971da3d425..c006bc95ac681 100644
e8e25e
--- a/iptables/xtables-eb.c
e8e25e
+++ b/iptables/xtables-eb.c
e8e25e
@@ -739,16 +739,9 @@ int nft_init_eb(struct nft_handle *h, const char *pname)
e8e25e
 	init_extensionsb();
e8e25e
 #endif
e8e25e
 
e8e25e
-	memset(h, 0, sizeof(*h));
e8e25e
-
e8e25e
-	h->family = NFPROTO_BRIDGE;
e8e25e
-
e8e25e
-	if (nft_init(h, xtables_bridge) < 0)
e8e25e
+	if (nft_init(h, NFPROTO_BRIDGE, xtables_bridge) < 0)
e8e25e
 		xtables_error(OTHER_PROBLEM,
e8e25e
 			      "Could not initialize nftables layer.");
e8e25e
-	h->ops = nft_family_ops_lookup(h->family);
e8e25e
-	if (!h->ops)
e8e25e
-		xtables_error(PARAMETER_PROBLEM, "Unknown family");
e8e25e
 
e8e25e
 	/* manually registering ebt matches, given the original ebtables parser
e8e25e
 	 * don't use '-m matchname' and the match can't be loaded dynamically when
e8e25e
diff --git a/iptables/xtables-monitor.c b/iptables/xtables-monitor.c
e8e25e
index a5245d1422af9..c2b31dbaa0795 100644
e8e25e
--- a/iptables/xtables-monitor.c
e8e25e
+++ b/iptables/xtables-monitor.c
e8e25e
@@ -615,7 +615,7 @@ int xtables_monitor_main(int argc, char *argv[])
e8e25e
 	init_extensions4();
e8e25e
 #endif
e8e25e
 
e8e25e
-	if (nft_init(&h, xtables_ipv4)) {
e8e25e
+	if (nft_init(&h, AF_INET, xtables_ipv4)) {
e8e25e
 		fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
e8e25e
 			xtables_globals.program_name,
e8e25e
 			xtables_globals.program_version,
e8e25e
diff --git a/iptables/xtables-restore.c b/iptables/xtables-restore.c
e8e25e
index fb2ac8b5c12a3..11834c0ea98c5 100644
e8e25e
--- a/iptables/xtables-restore.c
e8e25e
+++ b/iptables/xtables-restore.c
e8e25e
@@ -360,15 +360,13 @@ static int
e8e25e
 xtables_restore_main(int family, const char *progname, int argc, char *argv[])
e8e25e
 {
e8e25e
 	const struct builtin_table *tables;
e8e25e
-	struct nft_handle h = {
e8e25e
-		.family = family,
e8e25e
-		.restore = true,
e8e25e
-	};
e8e25e
-	int c;
e8e25e
 	struct nft_xt_restore_parse p = {
e8e25e
 		.commit = true,
e8e25e
 		.cb = &restore_cb,
e8e25e
 	};
e8e25e
+	bool noflush = false;
e8e25e
+	struct nft_handle h;
e8e25e
+	int c;
e8e25e
 
e8e25e
 	line = 0;
e8e25e
 
e8e25e
@@ -402,7 +400,7 @@ xtables_restore_main(int family, const char *progname, int argc, char *argv[])
e8e25e
 				print_usage(prog_name, PACKAGE_VERSION);
e8e25e
 				exit(0);
e8e25e
 			case 'n':
e8e25e
-				h.noflush = 1;
e8e25e
+				noflush = true;
e8e25e
 				break;
e8e25e
 			case 'M':
e8e25e
 				xtables_modprobe_program = optarg;
e8e25e
@@ -464,13 +462,15 @@ xtables_restore_main(int family, const char *progname, int argc, char *argv[])
e8e25e
 		return 1;
e8e25e
 	}
e8e25e
 
e8e25e
-	if (nft_init(&h, tables) < 0) {
e8e25e
+	if (nft_init(&h, family, tables) < 0) {
e8e25e
 		fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
e8e25e
 				xtables_globals.program_name,
e8e25e
 				xtables_globals.program_version,
e8e25e
 				strerror(errno));
e8e25e
 		exit(EXIT_FAILURE);
e8e25e
 	}
e8e25e
+	h.noflush = noflush;
e8e25e
+	h.restore = true;
e8e25e
 
e8e25e
 	xtables_restore_parse(&h, &p);
e8e25e
 
e8e25e
diff --git a/iptables/xtables-save.c b/iptables/xtables-save.c
e8e25e
index 3a52f8c3d8209..228282deaed07 100644
e8e25e
--- a/iptables/xtables-save.c
e8e25e
+++ b/iptables/xtables-save.c
e8e25e
@@ -139,10 +139,8 @@ xtables_save_main(int family, int argc, char *argv[],
e8e25e
 	struct do_output_data d = {
e8e25e
 		.format = FMT_NOCOUNTS,
e8e25e
 	};
e8e25e
+	struct nft_handle h;
e8e25e
 	bool dump = false;
e8e25e
-	struct nft_handle h = {
e8e25e
-		.family	= family,
e8e25e
-	};
e8e25e
 	FILE *file = NULL;
e8e25e
 	int ret, c;
e8e25e
 
e8e25e
@@ -242,16 +240,13 @@ xtables_save_main(int family, int argc, char *argv[],
e8e25e
 		return 1;
e8e25e
 	}
e8e25e
 
e8e25e
-	if (nft_init(&h, tables) < 0) {
e8e25e
+	if (nft_init(&h, family, tables) < 0) {
e8e25e
 		fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
e8e25e
 				xtables_globals.program_name,
e8e25e
 				xtables_globals.program_version,
e8e25e
 				strerror(errno));
e8e25e
 		exit(EXIT_FAILURE);
e8e25e
 	}
e8e25e
-	h.ops = nft_family_ops_lookup(h.family);
e8e25e
-	if (!h.ops)
e8e25e
-		xtables_error(PARAMETER_PROBLEM, "Unknown family");
e8e25e
 
e8e25e
 	ret = do_output(&h, tablename, &d);
e8e25e
 	nft_fini(&h);
e8e25e
diff --git a/iptables/xtables-standalone.c b/iptables/xtables-standalone.c
e8e25e
index 1a28c5480629f..022d5dd44abbf 100644
e8e25e
--- a/iptables/xtables-standalone.c
e8e25e
+++ b/iptables/xtables-standalone.c
e8e25e
@@ -44,9 +44,7 @@ xtables_main(int family, const char *progname, int argc, char *argv[])
e8e25e
 {
e8e25e
 	int ret;
e8e25e
 	char *table = "filter";
e8e25e
-	struct nft_handle h = {
e8e25e
-		.family = family,
e8e25e
-	};
e8e25e
+	struct nft_handle h;
e8e25e
 
e8e25e
 	xtables_globals.program_name = progname;
e8e25e
 	ret = xtables_init_all(&xtables_globals, family);
e8e25e
@@ -61,7 +59,7 @@ xtables_main(int family, const char *progname, int argc, char *argv[])
e8e25e
 	init_extensions4();
e8e25e
 #endif
e8e25e
 
e8e25e
-	if (nft_init(&h, xtables_ipv4) < 0) {
e8e25e
+	if (nft_init(&h, family, xtables_ipv4) < 0) {
e8e25e
 		fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
e8e25e
 				xtables_globals.program_name,
e8e25e
 				xtables_globals.program_version,
e8e25e
diff --git a/iptables/xtables-translate.c b/iptables/xtables-translate.c
e8e25e
index 0f95855b41aa4..76ad7eb69eca9 100644
e8e25e
--- a/iptables/xtables-translate.c
e8e25e
+++ b/iptables/xtables-translate.c
e8e25e
@@ -480,7 +480,7 @@ static int xtables_xlate_main_common(struct nft_handle *h,
e8e25e
 		return 1;
e8e25e
 	}
e8e25e
 
e8e25e
-	if (nft_init(h, tables) < 0) {
e8e25e
+	if (nft_init(h, family, tables) < 0) {
e8e25e
 		fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
e8e25e
 				xtables_globals.program_name,
e8e25e
 				xtables_globals.program_version,
e8e25e
diff --git a/iptables/xtables.c b/iptables/xtables.c
e8e25e
index 8f9dc628d0029..4b24d15c46295 100644
e8e25e
--- a/iptables/xtables.c
e8e25e
+++ b/iptables/xtables.c
e8e25e
@@ -571,10 +571,6 @@ void do_parse(struct nft_handle *h, int argc, char *argv[],
e8e25e
 	   demand-load a protocol. */
e8e25e
 	opterr = 0;
e8e25e
 
e8e25e
-	h->ops = nft_family_ops_lookup(h->family);
e8e25e
-	if (h->ops == NULL)
e8e25e
-		xtables_error(PARAMETER_PROBLEM, "Unknown family");
e8e25e
-
e8e25e
 	opts = xt_params->orig_opts;
e8e25e
 	while ((cs->c = getopt_long(argc, argv,
e8e25e
 	   "-:A:C:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvw::W::nt:m:xc:g:46",
e8e25e
-- 
e8e25e
2.26.2
e8e25e