netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel Gröber" <dxld@darkboxed.org>
To: netfilter-devel@vger.kernel.org
Subject: [libnf_ct PATCH v2 8/9] Fix buffer overflow in protocol related snprintf functions
Date: Wed, 24 Jun 2020 15:30:04 +0200	[thread overview]
Message-ID: <20200624133005.22046-8-dxld@darkboxed.org> (raw)
In-Reply-To: <20200624133005.22046-1-dxld@darkboxed.org>

Signed-off-by: Daniel Gröber <dxld@darkboxed.org>
---
 src/conntrack/snprintf_default.c | 14 ++++++--------
 src/conntrack/snprintf_xml.c     | 20 ++++++++++++++++++--
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/conntrack/snprintf_default.c b/src/conntrack/snprintf_default.c
index d00c5cb..d18d2f2 100644
--- a/src/conntrack/snprintf_default.c
+++ b/src/conntrack/snprintf_default.c
@@ -13,20 +13,18 @@ static int __snprintf_l3protocol(char *buf,
 				 unsigned int len,
 				 const struct nf_conntrack *ct)
 {
-	return (snprintf(buf, len, "%-8s %u ", 
-		l3proto2str[ct->head.orig.l3protonum] == NULL ?
-		"unknown" : l3proto2str[ct->head.orig.l3protonum], 
-		 ct->head.orig.l3protonum));
+	uint8_t num = ct->head.orig.l3protonum;
+
+	return snprintf(buf, len, "%-8s %u ", __l3proto2str(num), num);
 }
 
 int __snprintf_protocol(char *buf,
 			unsigned int len,
 			const struct nf_conntrack *ct)
 {
-	return (snprintf(buf, len, "%-8s %u ", 
-		proto2str[ct->head.orig.protonum] == NULL ?
-		"unknown" : proto2str[ct->head.orig.protonum], 
-		 ct->head.orig.protonum));
+	uint8_t num = ct->head.orig.protonum;
+
+	return snprintf(buf, len, "%-8s %u ", __proto2str(num), num);
 }
 
 static int __snprintf_timeout(char *buf,
diff --git a/src/conntrack/snprintf_xml.c b/src/conntrack/snprintf_xml.c
index c3a836a..e557df2 100644
--- a/src/conntrack/snprintf_xml.c
+++ b/src/conntrack/snprintf_xml.c
@@ -55,12 +55,28 @@
 
 const char *__proto2str(uint8_t protonum)
 {
-	return proto2str[protonum] ? proto2str[protonum] : "unknown";
+	const char *str = NULL;
+
+	if (protonum < ARRAY_SIZE(proto2str))
+		str = proto2str[protonum];
+
+	if (str == NULL)
+		str = "unknown";
+
+	return str;
 }
 
 const char *__l3proto2str(uint8_t protonum)
 {
-	return l3proto2str[protonum] ? l3proto2str[protonum] : "unknown";
+	const char *str = NULL;
+
+	if (protonum < ARRAY_SIZE(l3proto2str))
+		str = l3proto2str[protonum];
+
+	if (str == NULL)
+		str = "unknown";
+
+	return str;
 }
 
 static int __snprintf_ipv4_xml(char *buf,
-- 
2.20.1


  parent reply	other threads:[~2020-06-24 13:30 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-24 13:29 [libnf_ct PATCH v2 1/9] Handle negative snprintf return values properly Daniel Gröber
2020-06-24 13:29 ` [libnf_ct PATCH v2 2/9] Fix nfexp_snprintf return value docs Daniel Gröber
2020-06-24 13:29 ` [libnf_ct PATCH v2 3/9] Replace strncpy with snprintf to improve null byte handling Daniel Gröber
2020-06-24 13:30 ` [libnf_ct PATCH v2 4/9] Fix incorrect snprintf size calculation Daniel Gröber
2020-06-24 13:30 ` [libnf_ct PATCH v2 5/9] Add ARRAY_SIZE() macro Daniel Gröber
2020-06-24 13:30 ` [libnf_ct PATCH v2 6/9] Fix buffer overflow on invalid icmp type in setters Daniel Gröber
2020-06-24 13:30 ` [libnf_ct PATCH v2 7/9] Move icmp request>reply type mapping to common file Daniel Gröber
2020-06-24 13:30 ` Daniel Gröber [this message]
2020-06-24 13:30 ` [libnf_ct PATCH v2 9/9] Fix buffer overflows in __snprintf_protoinfo* like in *2str fns Daniel Gröber
2020-07-01 11:09 ` [libnf_ct PATCH v2 1/9] Handle negative snprintf return values properly Pablo Neira Ayuso
2020-07-01 13:46   ` Daniel Gröber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200624133005.22046-8-dxld@darkboxed.org \
    --to=dxld@darkboxed.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).