Skip to contentSkip to conversation

A series of covscan-indicated fixes

PATCH In review Phil Sutter 2 participants 8 messages Dec 6, 2019 – Dec 7, 2019 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover A series of covscan-indicated fixes

I had to dig through a huge coverity tool report, these are the things I found worth fixing.

Phil Sutter (6):
xtables-restore: Avoid access of uninitialized data
extensions: time: Avoid undefined shift
extensions: cluster: Avoid undefined shift
libxtables: Avoid buffer overrun in xtables_compatible_revision()
xtables-translate: Guard strcpy() call in xlate_ifname()
extensions: among: Check call to fstat()

 extensions/libebt_among.c    | 6 +++++-
 extensions/libxt_cluster.c   | 2 +-
 extensions/libxt_time.c      | 2 +-
 iptables/xtables-restore.c   | 2 +-
 iptables/xtables-translate.c | 5 ++---
 libxtables/xtables.c         | 3 ++-
 6 files changed, 12 insertions(+), 8 deletions(-)
Signature · 1 line
2.24.0
author reviewer
#1 lore raw

PATCH 1/6 xtables-restore: Avoid access of uninitialized data

When flushing, 'buffer' is not written to prior to checking its first byte's value. Therefore it needs to be initialized upon declaration.

Fixes 09cb517949e69 ("xtables-restore: Improve performance of --noflush operation")
---
 iptables/xtables-restore.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
iptables/xtables-restore.c +1 -1
index dd907e0b8ddd5..63cc15cee9621 100644
@@ -281,7 +281,7 @@ void xtables_restore_parse(struct nft_handle *h,
 			   const struct nft_xt_restore_parse *p)
 {
 	struct nft_xt_restore_state state = {};
-	char preload_buffer[PREBUFSIZ] = {}, buffer[10240], *ptr;
+	char preload_buffer[PREBUFSIZ] = {}, buffer[10240] = {}, *ptr;
 
 	if (!h->noflush) {
 		nft_fake_cache(h);
Signature · 1 line
2.24.0
author reviewer
#2 lore raw

PATCH 2/6 extensions: time: Avoid undefined shift

In reply to Phil Sutter

Value 1 is signed 32-bit by default and left-shifting that by 31 is undefined. Fix this by marking the value as unsigned.

Fixes ad326ef9f734a ("Add the libxt_time iptables match")
---
 extensions/libxt_time.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
extensions/libxt_time.c +1 -1
index 5a8cc5de13031..d001f5b7f448f 100644
@@ -330,7 +330,7 @@ static void time_print_monthdays(uint32_t mask, bool human_readable)
 
 	printf(" ");
 	for (i = 1; i <= 31; ++i)
-		if (mask & (1 << i)) {
+		if (mask & (1u << i)) {
 			if (nbdays++ > 0)
 				printf(",");
 			printf("%u", i);
Signature · 1 line
2.24.0
author reviewer
#3 lore raw

PATCH 3/6 extensions: cluster: Avoid undefined shift

In reply to Phil Sutter

Value 1 is signed 32-bit by default and left-shifting that by 31 is undefined. Fix this by marking the value as unsigned.

Fixes 64a0e09894e52 ("extensions: libxt_cluster: Add translation to nft")
---
 extensions/libxt_cluster.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
extensions/libxt_cluster.c +1 -1
index c9c35ee22e3df..d164bf6960166 100644
@@ -156,7 +156,7 @@ static int cluster_xlate(struct xt_xlate *xl,
 		xt_xlate_add(xl, "%s %u seed 0x%08x ", jhash_st,
 				info->total_nodes, info->hash_seed);
 		for (node = 0; node < 32; node++) {
-			if (info->node_mask & (1 << node)) {
+			if (info->node_mask & (1u << node)) {
 				if (needs_set == 0) {
 					xt_xlate_add(xl, "{ ");
 					needs_set = 1;
Signature · 1 line
2.24.0
author reviewer
#4 lore raw

PATCH 4/6 libxtables: Avoid buffer overrun in xtables_compatible_revision()

In reply to Phil Sutter

The function is exported and accepts arbitrary strings as input. Calling strcpy() without length checks is not OK.

---
 libxtables/xtables.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libxtables/xtables.c +2 -1
index 895f6988eaf57..777c2b08e9896 100644
@@ -856,7 +856,8 @@ int xtables_compatible_revision(const char *name, uint8_t revision, int opt)
 
 	xtables_load_ko(xtables_modprobe_program, true);
 
-	strcpy(rev.name, name);
+	strncpy(rev.name, name, XT_EXTENSION_MAXNAMELEN - 1);
+	rev.name[XT_EXTENSION_MAXNAMELEN - 1] = '\0';
 	rev.revision = revision;
 
 	max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Signature · 1 line
2.24.0
author reviewer
#5 lore raw

PATCH 5/6 xtables-translate: Guard strcpy() call in xlate_ifname()

In reply to Phil Sutter

The function potentially fed overlong strings to strcpy(). Given that everything needed to avoid this is there, reorder code a bit to prevent those inputs, too.

Fixes 0ddd663e9c167 ("iptables-translate: add in/out ifname wildcard match translation to nft")
---
 iptables/xtables-translate.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
iptables/xtables-translate.c +2 -3
index a42c60a3b64c6..77a186b905d73 100644
@@ -32,14 +32,13 @@
 void xlate_ifname(struct xt_xlate *xl, const char *nftmeta, const char *ifname,
 		  bool invert)
 {
+	int ifaclen = strlen(ifname);
 	char iface[IFNAMSIZ];
-	int ifaclen;
 
-	if (ifname[0] == '\0')
+	if (ifaclen < 1 || ifaclen >= IFNAMSIZ)
 		return;
 
 	strcpy(iface, ifname);
-	ifaclen = strlen(iface);
 	if (iface[ifaclen - 1] == '+')
 		iface[ifaclen - 1] = '*';
 
Signature · 1 line
2.24.0
author reviewer
#6 lore raw

PATCH 6/6 extensions: among: Check call to fstat()

In reply to Phil Sutter

If this fails, a bogus length value may be passed to mmap().

Fixes 26753888720d8 ("nft: bridge: Rudimental among extension support")
---
 extensions/libebt_among.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
extensions/libebt_among.c +5 -1
index 2e87db3bc06fa..715d559f432c2 100644
@@ -6,6 +6,7 @@
  * August, 2003
  */
 
+#include <errno.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <getopt.h>
@@ -137,7 +138,10 @@ static int bramong_parse(int c, char **argv, int invert,
 		if ((fd = open(optarg, O_RDONLY)) == -1)
 			xtables_error(PARAMETER_PROBLEM,
 				      "Couldn't open file '%s'", optarg);
-		fstat(fd, &stats);
+		if (fstat(fd, &stats) < 0)
+			xtables_error(PARAMETER_PROBLEM,
+				      "fstat(%s) failed: '%s'",
+				      optarg, strerror(errno));
 		flen = stats.st_size;
 		/* use mmap because the file will probably be big */
 		optarg = mmap(0, flen, PROT_READ | PROT_WRITE,
Signature · 1 line
2.24.0