Skip to contentSkip to conversation

misc improvements and code cleaning

PATCH In review Eric Leblond 3 participants 15 messages Sep 22, 2010 – Oct 3, 2010 netfilter

lore
Share & tools
lore
#0 lore raw

cover misc improvements and code cleaning

Hi,

This patchset contains some light improvement and some code cleaning.

It starts with some build related fixes. The basic idea is to be able to compile ulogd on a system without libnetfilter_conntrack and/or libnetfilter_log installed:
- build: nfct and nflog can be disabled via configure option
- ulogd.conf: Comment library dependant plugins
Last build related patch adds a debug configure option to be able to activate some debug related features (like being able to use gdb):
- build: add debug configure flag

Next significative patches are two code cleaning patches. The only thing to note is that the second one could have some security implication:
- filter_MARK: code factorization.
- ulogd: use snprintf instead of sprintf in printpkt

If no one has problem with these patches, I will push them to the git repository.

BR,
--
Eric Leblond <[email protected]>

#1 lore raw

PATCH 1/6 build: nfct and nflog can be disabled via configure option

This patch modifies the build system to be able to disable the compilation of NFCT and NFLOG input plugin. They are dependant of external libraries which can not be available on the system.
Default is to compile these input plugin which make compilation behaviour conservative.

---
 configure.ac              |   19 ++++++++++++++++---
 input/flow/Makefile.am    |    2 ++
 input/packet/Makefile.am  |    5 +++++
 output/ulogd_output_XML.c |   12 ++++++++++--
 4 files changed, 33 insertions(+), 5 deletions(-)
configure.ac +16 -3
index e5a64fe..af14f36 100644
@@ -38,11 +38,21 @@ LIBNFNETLINK_REQUIRED=0.0.39
 LIBNETFILTER_CONNTRACK_REQUIRED=0.0.95
 LIBNETFILTER_LOG_REQUIRED=1.0.0
 
-PKG_CHECK_MODULES(LIBNFNETLINK, libnfnetlink >= $LIBNFNETLINK_REQUIRED,, AC_MSG_ERROR(Cannot find libnfnetlink >= $LIBNFNETLINK_REQUIRED))
+AC_ARG_ENABLE(nfct,   [AC_HELP_STRING(--disable-nfct, Do not build NFCT input plugin)],build_nfct=$enableval, build_nfct="yes")
+AC_ARG_ENABLE(nflog,   [AC_HELP_STRING(--disable-nflog, Do not build NFLOG input plugin)],build_nflog=$enableval, build_nflog="yes")
+if test "${build_nfct}" = "yes" || test "${build_nflog}" = "yes" ; then
+	PKG_CHECK_MODULES(LIBNFNETLINK, libnfnetlink >= $LIBNFNETLINK_REQUIRED,, AC_MSG_ERROR(Cannot find libnfnetlink >= $LIBNFNETLINK_REQUIRED))
+fi
 
-PKG_CHECK_MODULES(LIBNETFILTER_CONNTRACK, libnetfilter_conntrack >= $LIBNETFILTER_CONNTRACK_REQUIRED,, AC_MSG_ERROR(Cannot find libnetfilter_conntrack >= $LIBNETFILTER_CONNTRACK_REQUIRED))
+if test "${build_nfct}" = "yes"; then
+	PKG_CHECK_MODULES(LIBNETFILTER_CONNTRACK, libnetfilter_conntrack >= $LIBNETFILTER_CONNTRACK_REQUIRED,, AC_MSG_ERROR(Cannot find libnetfilter_conntrack >= $LIBNETFILTER_CONNTRACK_REQUIRED))
+	AC_DEFINE_UNQUOTED([HAVE_LIBNETFILTER_CONNTRACK],[1],[libnetfilter_conntrack is available])
+fi
 
-PKG_CHECK_MODULES(LIBNETFILTER_LOG, libnetfilter_log >= $LIBNETFILTER_LOG_REQUIRED,, AC_MSG_ERROR(Cannot find libnetfilter_log >= $LIBNETFILTER_LOG_REQUIRED))
+if test "${build_nflog}" = "yes"; then
+	PKG_CHECK_MODULES(LIBNETFILTER_LOG, libnetfilter_log >= $LIBNETFILTER_LOG_REQUIRED,, AC_MSG_ERROR(Cannot find libnetfilter_log >= $LIBNETFILTER_LOG_REQUIRED))
+	AC_DEFINE_UNQUOTED([HAVE_LIBNETFILTER_LOG],[1],[libnetfilter_log is available])
+fi
 
 
 CT_CHECK_POSTGRES_DB()
@@ -60,6 +70,9 @@ AM_CONDITIONAL(HAVE_DBI, test "x$DBI_LIB" != "x")
 CT_CHECK_PCAP()
 AM_CONDITIONAL(HAVE_PCAP, test "x$PCAP_LIB" != "x")
 
+AM_CONDITIONAL(HAVE_NFCT, test "x$build_nfct" != "xno")
+AM_CONDITIONAL(HAVE_NFLOG, test "x$build_nflog" != "xno")
+
 
 dnl AC_SUBST(DATABASE_DIR)
 dnl AC_SUBST(DATABASE_LIB)
input/flow/Makefile.am +2 -0
index 11bf217..5d7bdc4 100644
@@ -2,11 +2,13 @@
 AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
 AM_CFLAGS=-fPIC -Wall
 
+if HAVE_NFCT
 pkglib_LTLIBRARIES = ulogd_inpflow_NFCT.la # ulogd_inpflow_IPFIX.la
 
 ulogd_inpflow_NFCT_la_SOURCES = ulogd_inpflow_NFCT.c
 ulogd_inpflow_NFCT_la_LDFLAGS = -avoid-version -module $(LIBNETFILTER_CONNTRACK_LIBS)
 ulogd_inpflow_NFCT_la_CFLAGS = $(AM_CFLAGS) $(LIBNETFILTER_CONNTRACK_CFLAGS)
+endif
 
 #ulogd_inpflow_IPFIX_la_SOURCES = ulogd_inpflow_IPFIX.c
 #ulogd_inpflow_IPFIX_la_LDFLAGS = -avoid-version -module
input/packet/Makefile.am +5 -0
index e90e46e..44b7442 100644
@@ -3,12 +3,17 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
 AM_CFLAGS=-fPIC -Wall
 LIBS=
 
+if HAVE_NFLOG
 pkglib_LTLIBRARIES = ulogd_inppkt_NFLOG.la ulogd_inppkt_ULOG.la
 
 ulogd_inppkt_NFLOG_la_SOURCES = ulogd_inppkt_NFLOG.c
 ulogd_inppkt_NFLOG_la_LDFLAGS = -avoid-version -module $(LIBNETFILTER_LOG_LIBS)
 ulogd_inppkt_NFLOG_la_CFLAGS = $(AM_CFLAGS) $(LIBNETFILTER_LOG_CFLAGS)
 
+else
+pkglib_LTLIBRARIES = ulogd_inppkt_ULOG.la
+endif
+
 ulogd_inppkt_ULOG_la_SOURCES = ulogd_inppkt_ULOG.c
 ulogd_inppkt_ULOG_la_LDFLAGS = -avoid-version -module
 ulogd_inppkt_ULOG_la_LIBADD = ../../libipulog/libipulog.la
output/ulogd_output_XML.c +10 -2
index 1ec9d8c..55ccd5a 100644
@@ -18,8 +18,11 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <config.h>
 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
+#ifdef HAVE_LIBNETFILTER_LOG
 #include <libnetfilter_log/libnetfilter_log.h>
+#endif
 #include <ulogd/ulogd.h>
 #include <sys/param.h>
 #include <time.h>
@@ -95,6 +98,7 @@ xml_output_flow(struct ulogd_key *inp, char *buf, ssize_t size)
 	return 0;
 }
 
+#ifdef HAVE_LIBNETFILTER_LOG
 static int
 xml_output_packet(struct ulogd_key *inp, char *buf, ssize_t size)
 {
@@ -107,6 +111,7 @@ xml_output_packet(struct ulogd_key *inp, char *buf, ssize_t size)
 
 	return 0;
 }
+#endif
 
 static int xml_output(struct ulogd_pluginstance *upi)
 {
@@ -117,8 +122,11 @@ static int xml_output(struct ulogd_pluginstance *upi)
 
 	if (pp_is_valid(inp, KEY_CT))
 		ret = xml_output_flow(inp, buf, sizeof(buf));
-	else if (pp_is_valid(inp, KEY_PCKT))
-		ret = xml_output_packet(inp, buf, sizeof(buf));
+	else
+#ifdef HAVE_LIBNETFILTER_LOG
+		if (pp_is_valid(inp, KEY_PCKT))
+			ret = xml_output_packet(inp, buf, sizeof(buf));
+#endif
 
 	if (ret < 0)
 		return ULOGD_IRET_ERR;
Signature · 6 lines
1.6.1

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

Re: [PATCH 1/6] build: nfct and nflog can be disabled via configure option

Quoted 5 lines · Eric Leblond

This patch modifies the build system to be able to disable the compilation of NFCT and NFLOG input plugin. They are dependant of external libraries which can not be available on the system.
Default is to compile these input plugin which make compilation behaviour conservative.

OK, if you need this go ahead.

#3 lore raw

PATCH 2/6 ulogd.conf: Comment library dependant plugins

In reply to Eric Leblond

Some plugins depend of external library. Thus loading of ulogd2 can fail if they have not been compiled and are not present on the system.

---
 ulogd.conf.in |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
ulogd.conf.in +3 -3
index cda5bb9..de5bc11 100644
@@ -25,9 +25,9 @@ loglevel=1
 # 2. options for each plugin in seperate section below
 
 
-plugin="@libdir@/ulogd/ulogd_inppkt_NFLOG.so"
+#plugin="@libdir@/ulogd/ulogd_inppkt_NFLOG.so"
 #plugin="@libdir@/ulogd/ulogd_inppkt_ULOG.so"
-plugin="@libdir@/ulogd/ulogd_inpflow_NFCT.so"
+#plugin="@libdir@/ulogd/ulogd_inpflow_NFCT.so"
 plugin="@libdir@/ulogd/ulogd_filter_IFINDEX.so"
 plugin="@libdir@/ulogd/ulogd_filter_IP2STR.so"
 plugin="@libdir@/ulogd/ulogd_filter_IP2BIN.so"
@@ -37,7 +37,7 @@ plugin="@libdir@/ulogd/ulogd_filter_PRINTFLOW.so"
 #plugin="@libdir@/ulogd/ulogd_filter_MARK.so"
 plugin="@libdir@/ulogd/ulogd_output_LOGEMU.so"
 plugin="@libdir@/ulogd/ulogd_output_SYSLOG.so"
-plugin="@libdir@/ulogd/ulogd_output_XML.so"
+#plugin="@libdir@/ulogd/ulogd_output_XML.so"
 #plugin="@libdir@/ulogd/ulogd_output_OPRINT.so"
 #plugin="@libdir@/ulogd/ulogd_output_NACCT.so"
 #plugin="@libdir@/ulogd/ulogd_output_PCAP.so"
Signature · 1 line
1.6.1
#4 lore raw

Re: [PATCH 2/6] ulogd.conf: Comment library dependant plugins

ulogd.conf.in loglevel=1 · line 30

Quoted 13 lines · Eric Leblond
ulogd.conf.in +2 -2
index cda5bb9..de5bc11 100644
@@ -25,9 +25,9 @@ loglevel=1
 # 2. options for each plugin in seperate section below
 
 
-plugin="@libdir@/ulogd/ulogd_inppkt_NFLOG.so"
+#plugin="@libdir@/ulogd/ulogd_inppkt_NFLOG.so"
 #plugin="@libdir@/ulogd/ulogd_inppkt_ULOG.so"
-plugin="@libdir@/ulogd/ulogd_inpflow_NFCT.so"
+#plugin="@libdir@/ulogd/ulogd_inpflow_NFCT.so"

I prefer leaving this there. I know that you are doing this because of the compile time new option that you have inserted. But these are the main plugins!

#5 lore raw

Re: [PATCH 2/6] ulogd.conf: Comment library dependant plugins

Hi,

Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines · Eric Leblond
ulogd.conf.in +0 -0
index cda5bb9..de5bc11 100644
@@ -25,9 +25,9 @@ loglevel=1
 # 2. options for each plugin in seperate section below
 
 
Quoted 9 lines

ulogd.conf.in loglevel=1 · line 30

Quoted 5 lines
ulogd.conf.in +2 -2
-plugin="@libdir@/ulogd/ulogd_inppkt_NFLOG.so"
+#plugin="@libdir@/ulogd/ulogd_inppkt_NFLOG.so"
 #plugin="@libdir@/ulogd/ulogd_inppkt_ULOG.so"
-plugin="@libdir@/ulogd/ulogd_inpflow_NFCT.so"
+#plugin="@libdir@/ulogd/ulogd_inpflow_NFCT.so"

I prefer leaving this there. I know that you are doing this because of the compile time new option that you have inserted. But these are the main plugins!

I'm ok with that. It may cause more difficulty than solve issue.

BR,

Signature · 2 lines
Eric Leblond <[email protected]>
NuFW: http://www.nufw.org/
#6 lore raw

PATCH 3/6 build: add debug configure flag

In reply to Eric Leblond

This patch adds a debug option for configure which do some set compile option useful for debugging.

---
 configure.ac    |   11 +++++++++--
 src/Makefile.am |    4 ++++
 2 files changed, 13 insertions(+), 2 deletions(-)
configure.ac +9 -2
index af14f36..0819a17 100644
@@ -31,8 +31,6 @@ dnl Checks for library functions.
 AC_FUNC_VPRINTF
 AC_CHECK_FUNCS(socket strerror)
 
-CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused-parameter"
-
 dnl Check for the right nfnetlink version
 LIBNFNETLINK_REQUIRED=0.0.39
 LIBNETFILTER_CONNTRACK_REQUIRED=0.0.95
@@ -55,6 +53,8 @@ if test "${build_nflog}" = "yes"; then
 fi
 
 
+AC_ARG_ENABLE(debug,   [AC_HELP_STRING(--enable-debug, Do build ulogd in debug mode)],build_debug=$enableval, build_debug="no")
+
 CT_CHECK_POSTGRES_DB()
 AM_CONDITIONAL(HAVE_PGSQL, test "x$PQLIBPATH" != "x")
 
@@ -73,6 +73,13 @@ AM_CONDITIONAL(HAVE_PCAP, test "x$PCAP_LIB" != "x")
 AM_CONDITIONAL(HAVE_NFCT, test "x$build_nfct" != "xno")
 AM_CONDITIONAL(HAVE_NFLOG, test "x$build_nflog" != "xno")
 
+AM_CONDITIONAL(HAVE_DEBUG, test "x$build_debug" != "xno")
+
+if test "${build_debug}" = "yes" ; then
+	CFLAGS="$CFLAGS -O0 -Wall -Wextra -Wno-unused-parameter"
+else
+	CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused-parameter"
+fi
 
 dnl AC_SUBST(DATABASE_DIR)
 dnl AC_SUBST(DATABASE_LIB)
src/Makefile.am +4 -0
index aa9a3fa..26e0e53 100644
@@ -6,4 +6,8 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \
 sbin_PROGRAMS = ulogd
 
 ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c
+if HAVE_DEBUG
+ulogd_LDFLAGS = -lpthread -export-dynamic
+else
 ulogd_LDFLAGS = -export-dynamic
+endif
Signature · 1 line
1.6.1
#7 lore raw

Re: [PATCH 3/6] build: add debug configure flag

src/Makefile.am AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \ · line 10

Quoted 8 lines · Eric Leblond
src/Makefile.am +2 -0
@@ -6,4 +6,8 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \
 sbin_PROGRAMS = ulogd
 
 ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c
+if HAVE_DEBUG
+ulogd_LDFLAGS = -lpthread -export-dynamic

Do we still have the gdb pthread issue?

#8 lore raw

Re: [PATCH 3/6] build: add debug configure flag

Hi,

Quoted 11 lines · Pablo Neira Ayuso

src/Makefile.am AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \ · line 10

Quoted 8 lines · Eric Leblond
src/Makefile.am +2 -0
@@ -6,4 +6,8 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \
 sbin_PROGRAMS = ulogd
 
 ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c
+if HAVE_DEBUG
+ulogd_LDFLAGS = -lpthread -export-dynamic

Do we still have the gdb pthread issue?

I've got it on my debian "sid" and it seems someone has got it on bugzilla.

BR,

Signature · 2 lines
Eric Leblond <[email protected]>
NuFW: http://www.nufw.org/
#9 lore raw

Re: [PATCH 3/6] build: add debug configure flag

Quoted 9 lines · Eric Leblond

Hi,

Quoted 6 lines · Pablo Neira Ayuso
Quoted 5 lines · Eric Leblond
src/Makefile.am +0 -0
@@ -6,4 +6,8 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \
 sbin_PROGRAMS = ulogd
 
Quoted 8 lines
Quoted 5 lines

src/Makefile.am AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \ · line 10

Quoted 3 lines
src/Makefile.am +2 -0
 ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c
+if HAVE_DEBUG
+ulogd_LDFLAGS = -lpthread -export-dynamic

Do we still have the gdb pthread issue?

I've got it on my debian "sid" and it seems someone has got it on bugzilla.

When exactly does it happen? Do you have a testcase?
(I tried grep -ri pthr . in ulogd2 but that showed no use of pthr at all.)
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw

Re: [PATCH 3/6] build: add debug configure flag

Quoted 17 lines · Jan Engelhardt
Quoted 15 lines · Eric Leblond

Hi,

Quoted 11 lines · Pablo Neira Ayuso

src/Makefile.am AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \ · line 10

Quoted 8 lines · Eric Leblond
src/Makefile.am +2 -0
@@ -6,4 +6,8 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include \
 sbin_PROGRAMS = ulogd
 
 ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c
+if HAVE_DEBUG
+ulogd_LDFLAGS = -lpthread -export-dynamic

Do we still have the gdb pthread issue?

Quoted 6 lines
Quoted 2 lines

I've got it on my debian "sid" and it seems someone has got it on bugzilla.

When exactly does it happen? Do you have a testcase?
(I tried grep -ri pthr . in ulogd2 but that showed no use of pthr at all.)

The problem is gdb in debian, which doesn't seem to work if ulogd2 is not compiled with pthread.

If this is a gdb bug, it should be fixed there, I know that it's annoying for debugging ulogd2. However, I think this may confuse people since ulogd2 require no pthreads at all.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw

PATCH 4/6 ulogd.conf: add a complex but useful stack example

In reply to Eric Leblond

This patch adds a complex but useful stack example. It was undocumented that multiple output plugin can be used in the same stack.

This patch change a variable name to fix some example stacks.

---
 ulogd.conf.in |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
ulogd.conf.in +5 -2
index de5bc11..75b4880 100644
@@ -58,6 +58,9 @@ plugin="@libdir@/ulogd/ulogd_raw2packet_BASE.so"
 # this is a stack for packet-based logging via LOGEMU with filtering on MARK
 #stack=log2:NFLOG,mark1:MARK,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu1:LOGEMU
 
+# this is a stack for packet-based logging via SYSLOG and logging to LOGEMU after filtering on MARK
+#stack=log2:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,sys1:SYSLOG,mark1:MARK,emu1:LOGEMU
+
 # this is a stack for flow-based logging via LOGEMU
 #stack=ct1:NFCT,ip2str1:IP2STR,print1:PRINTFLOW,emu1:LOGEMU
 
@@ -209,8 +212,8 @@ table="ulog"
 pass="ulog2"
 procedure="INSERT_PACKET_FULL"
 
-[sys2]
-facility=LOG_LOCAL2
+[sys1]
+facility=LOG_LOCAL1
 
 [nacct1]
 sync = 1
Signature · 1 line
1.6.1
#12 lore raw

PATCH 5/6 filter_MARK: code factorization.

In reply to Eric Leblond

This patch brings a light factorization in the decision making process.

---
 filter/ulogd_filter_MARK.c |   24 +++++++++++-------------
 1 files changed, 11 insertions(+), 13 deletions(-)
filter/ulogd_filter_MARK.c +11 -13
index 1a7c2fd..85c146c 100644
@@ -71,20 +71,18 @@ static struct ulogd_key mark_inp[] = {
 static int interp_mark(struct ulogd_pluginstance *pi)
 {
 	struct ulogd_key *inp = pi->input.keys;
-	if (pp_is_valid(inp, KEY_CT_MARK)) {
-		if ((ikey_get_u32(&inp[KEY_CT_MARK]) &
-			pi->config_kset->ces[MARK_MASK].u.value) !=
+	int index = 0;
+	if (pp_is_valid(inp, KEY_CT_MARK))
+		index = KEY_CT_MARK;
+	else if (pp_is_valid(inp, KEY_OOB_MARK))
+		index = KEY_OOB_MARK;
+	else
+		return ULOGD_IRET_OK;
+	if ((ikey_get_u32(&inp[index]) &
+				pi->config_kset->ces[MARK_MASK].u.value) !=
 			(u_int32_t) pi->config_kset->ces[MARK_MARK].u.value
-		   ) {
-			return ULOGD_IRET_STOP;
-		}
-	} else if (pp_is_valid(inp, KEY_OOB_MARK)) {
-		if ((ikey_get_u32(&inp[KEY_OOB_MARK]) &
-			pi->config_kset->ces[MARK_MASK].u.value) !=
-			(u_int32_t) pi->config_kset->ces[MARK_MARK].u.value
-		   ) {
-			return ULOGD_IRET_STOP;
-		}
+	   ) {
+		return ULOGD_IRET_STOP;
 	}
 	return ULOGD_IRET_OK;	
 }
Signature · 1 line
1.6.1
#13 lore raw

PATCH 6/6 ulogd: use snprintf instead of sprintf in printpkt

In reply to Eric Leblond

This patch modifies printpkt to use snprintf instead of sprintf. As the buffer size is arbitrary fixed and as printed part include some user entry it is safer to use snprintf.

---
 filter/ulogd_filter_PRINTPKT.c |    4 +-
 include/ulogd/printpkt.h       |    3 +-
 util/printpkt.c                |  166 +++++++++++++++++++++-------------------
 3 files changed, 91 insertions(+), 82 deletions(-)
filter/ulogd_filter_PRINTPKT.c +2 -2
index 3313194..16a4741 100644
@@ -34,9 +34,9 @@ static int printpkt_interp(struct ulogd_pluginstance *upi)
 {
 	struct ulogd_key *inp = upi->input.keys;
 	struct ulogd_key *ret = upi->output.keys;
-	static char buf[4096];
+	static char buf[PRINTPKT_BUF_SIZE];
 
-	printpkt_print(inp, buf);
+	printpkt_print(inp, buf, PRINTPKT_BUF_SIZE);
 	okey_set_ptr(&ret[0], buf);
 	return ULOGD_IRET_OK;
 }
include/ulogd/printpkt.h +2 -1
index a11ce4a..882dbf5 100644
@@ -67,8 +67,9 @@ enum pkt_keys {
 };
 #define PRINTPKT_KEYS (__PRINTPKT_KEYS)
 
+#define PRINTPKT_BUF_SIZE 4096
 extern struct ulogd_key printpkt_keys[PRINTPKT_KEYS];
 
-int printpkt_print(struct ulogd_key *res, char *buf);
+int printpkt_print(struct ulogd_key *res, char *buf, size_t bufsiz);
 
 #endif
util/printpkt.c +87 -79

445 lines not on this page · raw message

Signature · 1 line
1.6.1
#14 lore raw

Re: [PATCH 6/6] ulogd: use snprintf instead of sprintf in printpkt

Hi Eric,

Quoted 9 lines · Eric Leblond
util/printpkt.c +0 -1
index 5250792..a800555 100644
@@ -106,91 +106,93 @@ struct ulogd_key printpkt_keys[] = {
 	[KEY_SCTP_DPORT]	= { .name = "sctp.dport", },
 };
 
-static int printpkt_proto(struct ulogd_key *res, char *buf, int protocol)

util/printpkt.c struct ulogd_key printpkt_keys[] = { · line 117

Quoted 10 lines
util/printpkt.c +4 -1
+static int printpkt_proto(struct ulogd_key *res, char *buf, size_t bufsiz, int protocol)
 {
 	char *buf_cur = buf;
+	char *end_buf = buf + bufsiz;
+
 
 	switch (protocol) {
 	case IPPROTO_TCP:
-		buf_cur += sprintf(buf_cur, "PROTO=TCP ");
+		buf_cur += snprintf(buf_cur, bufsiz, "PROTO=TCP ");

If you have to fix this. snprintf can return -1 in case of error. Moreover, it returns the number of bytes that would have been written if there's space in the buffer. So you have to check if the return value is higher that the remaining space in the buffer.

Have a look at the use of snprintf in libnetfilter_queue and libnetfilter_log in nf*snprintf_xml() function for instance.