Blame SOURCES/0005-nfct-remove-lazy-binding.patch

028103
From d18e2e7b13ce623da968e896c04813f9d3b8efbf Mon Sep 17 00:00:00 2001
028103
From: Pablo Neira Ayuso <pablo@netfilter.org>
028103
Date: Tue, 8 Mar 2022 23:05:39 +0100
028103
Subject: [PATCH] nfct: remove lazy binding
028103
028103
Since cd5135377ac4 ("conntrackd: cthelper: Set up userspace helpers when
028103
daemon starts"), userspace conntrack helpers do not depend on a previous
028103
invocation of nfct to set up the userspace helpers.
028103
028103
Move helper definitions to nfct-extensions/helper.c since existing
028103
deployments might still invoke nfct, even if not required anymore.
028103
028103
This patch was motivated by the removal of the lazy binding.
028103
028103
Phil Sutter says:
028103
028103
"For security purposes, distributions might want to pass -Wl,-z,now
028103
linker flags to all builds, thereby disabling lazy binding globally.
028103
028103
In the past, nfct relied upon lazy binding: It uses the helper objects'
028103
parsing functions without but doesn't provide all symbols the objects
028103
use."
028103
028103
Acked-by: Phil Sutter <phil@nwl.cc>
028103
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
028103
(cherry picked from commit dc454a657f57a5cf143fddc5c1dd87a510c1790a)
028103
(cherry picked from commit 4527e4fec140ff5480d4fbfb2916001d64a0f72a)
028103
---
028103
 configure.ac                 |   5 +-
028103
 include/Makefile.am          |   2 +-
028103
 include/helper.h             |   1 +
028103
 include/helpers/Makefile.am  |   1 +
028103
 include/helpers/ftp.h        |  14 +++
028103
 include/helpers/rpc.h        |  15 +++
028103
 include/helpers/sane.h       |  13 +++
028103
 include/helpers/tns.h        |   9 ++
028103
 src/Makefile.am              |   2 -
028103
 src/helpers.c                |   3 +-
028103
 src/helpers/Makefile.am      |   2 +-
028103
 src/helpers/ftp.c            |  12 +--
028103
 src/helpers/rpc.c            |  13 +--
028103
 src/helpers/sane.c           |  10 +-
028103
 src/helpers/tns.c            |   7 +-
028103
 src/nfct-extensions/helper.c | 184 ++++++++++++++++++++++++++++++++++-
028103
 16 files changed, 246 insertions(+), 47 deletions(-)
028103
 create mode 100644 include/helpers/Makefile.am
028103
 create mode 100644 include/helpers/ftp.h
028103
 create mode 100644 include/helpers/rpc.h
028103
 create mode 100644 include/helpers/sane.h
028103
 create mode 100644 include/helpers/tns.h
028103
028103
diff --git a/configure.ac b/configure.ac
028103
index 5388054e64a58..1e444508fdc3c 100644
028103
--- a/configure.ac
028103
+++ b/configure.ac
028103
@@ -78,15 +78,12 @@ AC_CHECK_HEADERS([linux/capability.h],, [AC_MSG_ERROR([Cannot find linux/capabib
028103
 AC_CHECK_HEADERS(arpa/inet.h)
028103
 AC_CHECK_FUNCS(inet_pton)
028103
 
028103
-# Let nfct use dlopen() on helper libraries without resolving all symbols.
028103
-AX_CHECK_LINK_FLAG([-Wl,-z,lazy], [AC_SUBST([LAZY_LDFLAGS], [-Wl,-z,lazy])])
028103
-
028103
 if test ! -z "$libdir"; then
028103
 	MODULE_DIR="\\\"$libdir/conntrack-tools/\\\""
028103
 	CFLAGS="$CFLAGS -DCONNTRACKD_LIB_DIR=$MODULE_DIR"
028103
 fi
028103
 
028103
-AC_CONFIG_FILES([Makefile src/Makefile include/Makefile include/linux/Makefile include/linux/netfilter/Makefile extensions/Makefile src/helpers/Makefile])
028103
+AC_CONFIG_FILES([Makefile src/Makefile include/Makefile include/helpers/Makefile include/linux/Makefile include/linux/netfilter/Makefile extensions/Makefile src/helpers/Makefile])
028103
 AC_OUTPUT
028103
 
028103
 echo "
028103
diff --git a/include/Makefile.am b/include/Makefile.am
028103
index 352054e9135bd..4741b50228eb9 100644
028103
--- a/include/Makefile.am
028103
+++ b/include/Makefile.am
028103
@@ -1,4 +1,4 @@
028103
-SUBDIRS = linux
028103
+SUBDIRS = linux helpers
028103
 
028103
 noinst_HEADERS = alarm.h jhash.h cache.h linux_list.h linux_rbtree.h \
028103
 		 sync.h conntrackd.h local.h udp.h tcp.h \
028103
diff --git a/include/helper.h b/include/helper.h
028103
index d15c1c62c0534..7353dfa9b2073 100644
028103
--- a/include/helper.h
028103
+++ b/include/helper.h
028103
@@ -56,6 +56,7 @@ extern int in4_pton(const char *src, int srclen, uint8_t *dst, int delim, const
028103
 extern int in6_pton(const char *src, int srclen, uint8_t *dst, int delim, const char **end);
028103
 
028103
 extern void helper_register(struct ctd_helper *helper);
028103
+struct ctd_helper *__helper_find(const char *helper_name, uint8_t l4proto);
028103
 struct ctd_helper *helper_find(const char *libdir_path, const char *name, uint8_t l4proto, int flags);
028103
 
028103
 #define min_t(type, x, y) ({			\
028103
diff --git a/include/helpers/Makefile.am b/include/helpers/Makefile.am
028103
new file mode 100644
028103
index 0000000000000..99a4257d2d061
028103
--- /dev/null
028103
+++ b/include/helpers/Makefile.am
028103
@@ -0,0 +1 @@
028103
+noinst_HEADERS = ftp.h  rpc.h  sane.h  tns.h
028103
diff --git a/include/helpers/ftp.h b/include/helpers/ftp.h
028103
new file mode 100644
028103
index 0000000000000..50e2d0c97946d
028103
--- /dev/null
028103
+++ b/include/helpers/ftp.h
028103
@@ -0,0 +1,14 @@
028103
+#ifndef _CTD_FTP_H
028103
+#define _CTD_FTP_H
028103
+
028103
+#define NUM_SEQ_TO_REMEMBER 2
028103
+
028103
+/* This structure exists only once per master */
028103
+struct ftp_info {
028103
+	/* Valid seq positions for cmd matching after newline */
028103
+	uint32_t seq_aft_nl[MYCT_DIR_MAX][NUM_SEQ_TO_REMEMBER];
028103
+	/* 0 means seq_match_aft_nl not set */
028103
+	int seq_aft_nl_num[MYCT_DIR_MAX];
028103
+};
028103
+
028103
+#endif
028103
diff --git a/include/helpers/rpc.h b/include/helpers/rpc.h
028103
new file mode 100644
028103
index 0000000000000..b0b8d176fb542
028103
--- /dev/null
028103
+++ b/include/helpers/rpc.h
028103
@@ -0,0 +1,15 @@
028103
+#ifndef _CTD_RPC_H
028103
+#define _CTD_RPC_H
028103
+
028103
+struct rpc_info {
028103
+	/* XID */
028103
+	uint32_t xid;
028103
+	/* program */
028103
+	uint32_t pm_prog;
028103
+	/* program version */
028103
+	uint32_t pm_vers;
028103
+	/* transport protocol: TCP|UDP */
028103
+	uint32_t pm_prot;
028103
+};
028103
+
028103
+#endif
028103
diff --git a/include/helpers/sane.h b/include/helpers/sane.h
028103
new file mode 100644
028103
index 0000000000000..1e70ff636d60d
028103
--- /dev/null
028103
+++ b/include/helpers/sane.h
028103
@@ -0,0 +1,13 @@
028103
+#ifndef _CTD_SANE_H
028103
+#define _CTD_SANE_H
028103
+
028103
+enum sane_state {
028103
+	SANE_STATE_NORMAL,
028103
+	SANE_STATE_START_REQUESTED,
028103
+};
028103
+
028103
+struct nf_ct_sane_master {
028103
+	enum sane_state state;
028103
+};
028103
+
028103
+#endif
028103
diff --git a/include/helpers/tns.h b/include/helpers/tns.h
028103
new file mode 100644
028103
index 0000000000000..60dcf253657fc
028103
--- /dev/null
028103
+++ b/include/helpers/tns.h
028103
@@ -0,0 +1,9 @@
028103
+#ifndef _CTD_TNS_H
028103
+#define _CTD_TNS_H
028103
+
028103
+struct tns_info {
028103
+	/* Scan next DATA|REDIRECT packet */
028103
+	bool parse;
028103
+};
028103
+
028103
+#endif
028103
diff --git a/src/Makefile.am b/src/Makefile.am
028103
index a5b918d951327..9e47d2278a0d5 100644
028103
--- a/src/Makefile.am
028103
+++ b/src/Makefile.am
028103
@@ -35,8 +35,6 @@ if HAVE_CTHELPER
028103
 nfct_LDADD += ${LIBNETFILTER_CTHELPER_LIBS}
028103
 endif
028103
 
028103
-nfct_LDFLAGS = -export-dynamic ${LAZY_LDFLAGS}
028103
-
028103
 conntrackd_SOURCES = alarm.c main.c run.c hash.c queue.c queue_tx.c rbtree.c \
028103
 		    local.c log.c mcast.c udp.c netlink.c vector.c \
028103
 		    filter.c fds.c event.c process.c origin.c date.c \
028103
diff --git a/src/helpers.c b/src/helpers.c
028103
index 3e4e6c8553b8a..8ca78dc113fb7 100644
028103
--- a/src/helpers.c
028103
+++ b/src/helpers.c
028103
@@ -26,8 +26,7 @@ void helper_register(struct ctd_helper *helper)
028103
 	list_add(&helper->head, &helper_list);
028103
 }
028103
 
028103
-static struct ctd_helper *
028103
-__helper_find(const char *helper_name, uint8_t l4proto)
028103
+struct ctd_helper *__helper_find(const char *helper_name, uint8_t l4proto)
028103
 {
028103
 	struct ctd_helper *cur, *helper = NULL;
028103
 
028103
diff --git a/src/helpers/Makefile.am b/src/helpers/Makefile.am
028103
index d851d313e6fea..8f9c4ec556b66 100644
028103
--- a/src/helpers/Makefile.am
028103
+++ b/src/helpers/Makefile.am
028103
@@ -10,7 +10,7 @@ pkglib_LTLIBRARIES = ct_helper_amanda.la \
028103
 		     ct_helper_sane.la	\
028103
 		     ct_helper_ssdp.la
028103
 
028103
-HELPER_LDFLAGS = -avoid-version -module $(LIBNETFILTER_CONNTRACK_LIBS) $(LAZY_LDFLAGS)
028103
+HELPER_LDFLAGS = -avoid-version -module $(LIBNETFILTER_CONNTRACK_LIBS)
028103
 HELPER_CFLAGS = $(AM_CFLAGS) $(LIBNETFILTER_CONNTRACK_CFLAGS)
028103
 
028103
 ct_helper_amanda_la_SOURCES = amanda.c
028103
diff --git a/src/helpers/ftp.c b/src/helpers/ftp.c
028103
index c3aa28485b0f3..bd3f11788cc24 100644
028103
--- a/src/helpers/ftp.c
028103
+++ b/src/helpers/ftp.c
028103
@@ -35,17 +35,9 @@
028103
 #include <libnetfilter_queue/pktbuff.h>
028103
 #include <linux/netfilter.h>
028103
 
028103
-static bool loose; /* XXX: export this as config option. */
028103
-
028103
-#define NUM_SEQ_TO_REMEMBER 2
028103
+#include "helpers/ftp.h"
028103
 
028103
-/* This structure exists only once per master */
028103
-struct ftp_info {
028103
-	/* Valid seq positions for cmd matching after newline */
028103
-	uint32_t seq_aft_nl[MYCT_DIR_MAX][NUM_SEQ_TO_REMEMBER];
028103
-	/* 0 means seq_match_aft_nl not set */
028103
-	int seq_aft_nl_num[MYCT_DIR_MAX];
028103
-};
028103
+static bool loose; /* XXX: export this as config option. */
028103
 
028103
 enum nf_ct_ftp_type {
028103
 	/* PORT command from client */
028103
diff --git a/src/helpers/rpc.c b/src/helpers/rpc.c
028103
index bd24dd3269c8e..83adf658521d4 100644
028103
--- a/src/helpers/rpc.c
028103
+++ b/src/helpers/rpc.c
028103
@@ -40,21 +40,12 @@
028103
 #include <libnetfilter_queue/pktbuff.h>
028103
 #include <linux/netfilter.h>
028103
 
028103
+#include "helpers/rpc.h"
028103
+
028103
 /* RFC 1050: RPC: Remote Procedure Call Protocol Specification Version 2 */
028103
 /* RFC 1014: XDR: External Data Representation Standard */
028103
 #define SUPPORTED_RPC_VERSION	2
028103
 
028103
-struct rpc_info {
028103
-	/* XID */
028103
-	uint32_t xid;
028103
-	/* program */
028103
-	uint32_t pm_prog;
028103
-	/* program version */
028103
-	uint32_t pm_vers;
028103
-	/* transport protocol: TCP|UDP */
028103
-	uint32_t pm_prot;
028103
-};
028103
-
028103
 /* So, this packet has hit the connection tracking matching code.
028103
    Mangle it, and change the expectation to match the new version. */
028103
 static unsigned int
028103
diff --git a/src/helpers/sane.c b/src/helpers/sane.c
028103
index c30f4ba18533e..5e02e4fc2c1c3 100644
028103
--- a/src/helpers/sane.c
028103
+++ b/src/helpers/sane.c
028103
@@ -38,11 +38,7 @@
028103
 #include <libnetfilter_queue/libnetfilter_queue_tcp.h>
028103
 #include <libnetfilter_queue/pktbuff.h>
028103
 #include <linux/netfilter.h>
028103
-
028103
-enum sane_state {
028103
-	SANE_STATE_NORMAL,
028103
-	SANE_STATE_START_REQUESTED,
028103
-};
028103
+#include "helpers/sane.h"
028103
 
028103
 struct sane_request {
028103
 	uint32_t RPC_code;
028103
@@ -60,10 +56,6 @@ struct sane_reply_net_start {
028103
 	/* other fields aren't interesting for conntrack */
028103
 };
028103
 
028103
-struct nf_ct_sane_master {
028103
-	enum sane_state state;
028103
-};
028103
-
028103
 static int
028103
 sane_helper_cb(struct pkt_buff *pkt, uint32_t protoff,
028103
 		 struct myct *myct, uint32_t ctinfo)
028103
diff --git a/src/helpers/tns.c b/src/helpers/tns.c
028103
index 2b4fed420afb0..d9c7ae693f3a7 100644
028103
--- a/src/helpers/tns.c
028103
+++ b/src/helpers/tns.c
028103
@@ -28,6 +28,8 @@
028103
 #include <libnetfilter_queue/pktbuff.h>
028103
 #include <linux/netfilter.h>
028103
 
028103
+#include "helpers/tns.h"
028103
+
028103
 /* TNS SQL*Net Version 2 */
028103
 enum tns_types {
028103
        TNS_TYPE_CONNECT        = 1,
028103
@@ -57,11 +59,6 @@ struct tns_redirect {
028103
        uint16_t data_len;
028103
 };
028103
 
028103
-struct tns_info {
028103
-       /* Scan next DATA|REDIRECT packet */
028103
-       bool parse;
028103
-};
028103
-
028103
 static int try_number(const char *data, size_t dlen, uint32_t array[],
028103
 		      int array_size, char sep, char term)
028103
 {
028103
diff --git a/src/nfct-extensions/helper.c b/src/nfct-extensions/helper.c
028103
index 0569827612f06..fdeb94c5e5172 100644
028103
--- a/src/nfct-extensions/helper.c
028103
+++ b/src/nfct-extensions/helper.c
028103
@@ -180,7 +180,7 @@ static int nfct_cmd_helper_add(struct mnl_socket *nl, int argc, char *argv[])
028103
 		return -1;
028103
 	}
028103
 
028103
-	helper = helper_find(CONNTRACKD_LIB_DIR, argv[3], l4proto, RTLD_LAZY);
028103
+	helper = __helper_find(argv[3], l4proto);
028103
 	if (helper == NULL) {
028103
 		nfct_perror("that helper is not supported");
028103
 		return -1;
028103
@@ -430,7 +430,7 @@ nfct_cmd_helper_disable(struct mnl_socket *nl, int argc, char *argv[])
028103
 		return -1;
028103
 	}
028103
 
028103
-	helper = helper_find(CONNTRACKD_LIB_DIR, argv[3], l4proto, RTLD_LAZY);
028103
+	helper = __helper_find(argv[3], l4proto);
028103
 	if (helper == NULL) {
028103
 		nfct_perror("that helper is not supported");
028103
 		return -1;
028103
@@ -468,7 +468,187 @@ static struct nfct_extension helper = {
028103
 	.parse_params	= nfct_helper_parse_params,
028103
 };
028103
 
028103
+/*
028103
+ * supported helpers: to set up helpers via nfct, the following definitions are
028103
+ * provided for backward compatibility reasons since conntrackd does not depend
028103
+ * on nfct anymore to set up the userspace helpers.
028103
+ */
028103
+
028103
+static struct ctd_helper amanda_helper = {
028103
+	.name		= "amanda",
028103
+	.l4proto	= IPPROTO_UDP,
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name			= "amanda",
028103
+			.expect_max		= 3,
028103
+			.expect_timeout		= 180,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+static struct ctd_helper dhcpv6_helper = {
028103
+	.name		= "dhcpv6",
028103
+	.l4proto	= IPPROTO_UDP,
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name			= "dhcpv6",
028103
+			.expect_max		= 1,
028103
+			.expect_timeout		= 300,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+#include "helpers/ftp.h"
028103
+
028103
+static struct ctd_helper ftp_helper = {
028103
+	.name		= "ftp",
028103
+	.l4proto	= IPPROTO_TCP,
028103
+	.priv_data_len	= sizeof(struct ftp_info),
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name			= "ftp",
028103
+			.expect_max		= 1,
028103
+			.expect_timeout		= 300,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+static struct ctd_helper mdns_helper = {
028103
+	.name		= "mdns",
028103
+	.l4proto	= IPPROTO_UDP,
028103
+	.priv_data_len	= 0,
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name		= "mdns",
028103
+			.expect_max	= 8,
028103
+			.expect_timeout = 30,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+#include "helpers/rpc.h"
028103
+
028103
+static struct ctd_helper rpc_helper_tcp = {
028103
+	.name		= "rpc",
028103
+	.l4proto	= IPPROTO_TCP,
028103
+	.priv_data_len	= sizeof(struct rpc_info),
028103
+	.policy		= {
028103
+		{
028103
+			.name			= "rpc",
028103
+			.expect_max		= 1,
028103
+			.expect_timeout		= 300,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+static struct ctd_helper rpc_helper_udp = {
028103
+	.name		= "rpc",
028103
+	.l4proto	= IPPROTO_UDP,
028103
+	.priv_data_len	= sizeof(struct rpc_info),
028103
+	.policy		= {
028103
+		{
028103
+			.name			= "rpc",
028103
+			.expect_max		= 1,
028103
+			.expect_timeout		= 300,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+#include "helpers/sane.h"
028103
+
028103
+static struct ctd_helper sane_helper = {
028103
+	.name		= "sane",
028103
+	.l4proto	= IPPROTO_TCP,
028103
+	.priv_data_len	= sizeof(struct nf_ct_sane_master),
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name			= "sane",
028103
+			.expect_max		= 1,
028103
+			.expect_timeout		= 5 * 60,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+static struct ctd_helper slp_helper = {
028103
+	.name		= "slp",
028103
+	.l4proto	= IPPROTO_UDP,
028103
+	.priv_data_len	= 0,
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name		= "slp",
028103
+			.expect_max	= 8,
028103
+			.expect_timeout = 16, /* default CONFIG_MC_MAX + 1 */
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+static struct ctd_helper ssdp_helper_udp = {
028103
+	.name		= "ssdp",
028103
+	.l4proto	= IPPROTO_UDP,
028103
+	.priv_data_len	= 0,
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name		= "ssdp",
028103
+			.expect_max	= 8,
028103
+			.expect_timeout = 5 * 60,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+static struct ctd_helper ssdp_helper_tcp = {
028103
+	.name		= "ssdp",
028103
+	.l4proto	= IPPROTO_TCP,
028103
+	.priv_data_len	= 0,
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name		= "ssdp",
028103
+			.expect_max	= 8,
028103
+			.expect_timeout = 5 * 60,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+static struct ctd_helper tftp_helper = {
028103
+	.name		= "tftp",
028103
+	.l4proto	= IPPROTO_UDP,
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name			= "tftp",
028103
+			.expect_max		= 1,
028103
+			.expect_timeout		= 5 * 60,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
+#include "helpers/tns.h"
028103
+
028103
+static struct ctd_helper tns_helper = {
028103
+	.name		= "tns",
028103
+	.l4proto	= IPPROTO_TCP,
028103
+	.priv_data_len	= sizeof(struct tns_info),
028103
+	.policy		= {
028103
+		[0] = {
028103
+			.name		= "tns",
028103
+			.expect_max	= 1,
028103
+			.expect_timeout = 300,
028103
+		},
028103
+	},
028103
+};
028103
+
028103
 static void __init helper_init(void)
028103
 {
028103
+	helper_register(&amanda_helper);
028103
+	helper_register(&dhcpv6_helper);
028103
+	helper_register(&ftp_helper);
028103
+	helper_register(&mdns_helper);
028103
+	helper_register(&rpc_helper_tcp);
028103
+	helper_register(&rpc_helper_udp);
028103
+	helper_register(&sane_helper);
028103
+	helper_register(&slp_helper);
028103
+	helper_register(&ssdp_helper_udp);
028103
+	helper_register(&ssdp_helper_tcp);
028103
+	helper_register(&tftp_helper);
028103
+	helper_register(&tns_helper);
028103
+
028103
 	nfct_extension_register(&helper);
028103
 }
028103
-- 
028103
2.34.1
028103