Skip to contentSkip to conversation

Fixes for a recent Coverity tool run

PATCH In review Phil Sutter 2 participants 10 messages Mar 25, 2022 – Mar 28, 2022 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Fixes for a recent Coverity tool run

Phil Sutter (8):
hash: Flush tables when destroying
cache: Fix features array allocation
Fix potential buffer overrun in snprintf() calls
helpers: ftp: Avoid ugly casts
read_config_yy: Drop extra argument from dlog() call Don't call exit() from signal handler
Drop pointless assignments
connntrack: Fix for memleak when parsing -j arg

 src/cache.c          |  4 ++--
 src/conntrack.c      |  2 ++
 src/hash.c           |  1 +
 src/helpers/ftp.c    | 20 +++++++++-----------
 src/helpers/ssdp.c   |  1 -
 src/main.c           |  2 +-
 src/process.c        |  2 +-
 src/queue.c          |  4 ++--
 src/read_config_yy.y |  2 +-
 src/run.c            |  2 +-
 10 files changed, 20 insertions(+), 20 deletions(-)
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH 1/8 hash: Flush tables when destroying

This is cosmetics only, but stops valgrind from complaining about definitely lost memory.

---
 src/hash.c | 1 +
 1 file changed, 1 insertion(+)
src/hash.c +1 -0
index fe6a047fcebe0..a0f240c21fa82 100644
@@ -55,6 +55,7 @@ hashtable_create(int hashsize, int limit,
 
 void hashtable_destroy(struct hashtable *h)
 {
+	hashtable_flush(h);
 	free(h);
 }
 
Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH 2/8 cache: Fix features array allocation

In reply to Phil Sutter

struct cache::features is of type struct cache_feature **, allocate and populate accordingly.

Fixes ad31f852c3454 ("initial import of the conntrack daemon to Netfilter SVN")
---
 src/cache.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/cache.c +2 -2
index 79a024f8b6bb0..9bc8d0f5bf34a 100644
@@ -69,12 +69,12 @@ struct cache *cache_create(const char *name, enum cache_type type,
 
 	memcpy(c->feature_type, feature_type, sizeof(feature_type));
 
-	c->features = malloc(sizeof(struct cache_feature) * j);
+	c->features = malloc(sizeof(struct cache_feature *) * j);
 	if (!c->features) {
 		free(c);
 		return NULL;
 	}
-	memcpy(c->features, feature_array, sizeof(struct cache_feature) * j);
+	memcpy(c->features, feature_array, sizeof(struct cache_feature *) * j);
 	c->num_features = j;
 
 	c->extra_offset = size;
Signature · 1 line
2.34.1
author reviewer
#3 lore raw

PATCH 3/8 Fix potential buffer overrun in snprintf() calls

In reply to Phil Sutter

When consecutively printing into the same buffer at increasing offset, reduce buffer size passed to snprintf() to not defeat its size checking.

---
 src/process.c | 2 +-
 src/queue.c   | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
src/process.c +1 -1
index 3ddad5ffa7959..08598eeae84de 100644
@@ -84,7 +84,7 @@ void fork_process_dump(int fd)
 	int size = 0;
 
 	list_for_each_entry(this, &process_list, head) {
-		size += snprintf(buf+size, sizeof(buf),
+		size += snprintf(buf + size, sizeof(buf) - size,
 				 "PID=%u type=%s\n",
 				 this->pid,
 				 this->type < CTD_PROC_MAX ?
src/queue.c +2 -2
index 76425b18495b5..e94dc7c45d1fd 100644
@@ -69,12 +69,12 @@ void queue_stats_show(int fd)
 	int size = 0;
 	char buf[512];
 
-	size += snprintf(buf+size, sizeof(buf),
+	size += snprintf(buf + size, sizeof(buf) - size,
 			 "allocated queue nodes:\t\t%12u\n\n",
 			 qobjects_num);
 
 	list_for_each_entry(this, &queue_list, list) {
-		size += snprintf(buf+size, sizeof(buf),
+		size += snprintf(buf + size, sizeof(buf) - size,
 				 "queue %s:\n"
 				 "current elements:\t\t%12u\n"
 				 "maximum elements:\t\t%12u\n"
Signature · 1 line
2.34.1
author reviewer
#4 lore raw

PATCH 4/8 helpers: ftp: Avoid ugly casts

In reply to Phil Sutter

Coverity tool complains about accessing a local variable at non-zero offset. Avoid this by using a helper union. This should silence the checker, although the code is still probably not Big Endian-safe.

---
 src/helpers/ftp.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
src/helpers/ftp.c +9 -11
index 29ac55c236507..2b345340a70b9 100644
@@ -332,23 +332,21 @@ static int nf_nat_ftp_fmt_cmd(enum nf_ct_ftp_type type,
 			      char *buffer, size_t buflen,
 			      uint32_t addr, uint16_t port)
 {
+	union {
+		unsigned char c[4];
+		uint32_t d;
+	} tmp;
+
+	tmp.d = addr;
 	switch (type) {
 	case NF_CT_FTP_PORT:
 	case NF_CT_FTP_PASV:
 		return snprintf(buffer, buflen, "%u,%u,%u,%u,%u,%u",
-				((unsigned char *)&addr)[0],
-				((unsigned char *)&addr)[1],
-				((unsigned char *)&addr)[2],
-				((unsigned char *)&addr)[3],
-				port >> 8,
-				port & 0xFF);
+				tmp.c[0], tmp.c[1], tmp.c[2], tmp.c[3],
+				port >> 8, port & 0xFF);
 	case NF_CT_FTP_EPRT:
 		return snprintf(buffer, buflen, "|1|%u.%u.%u.%u|%u|",
-				((unsigned char *)&addr)[0],
-				((unsigned char *)&addr)[1],
-				((unsigned char *)&addr)[2],
-				((unsigned char *)&addr)[3],
-				port);
+				tmp.c[0], tmp.c[1], tmp.c[2], tmp.c[3], port);
 	case NF_CT_FTP_EPSV:
 		return snprintf(buffer, buflen, "|||%u|", port);
 	}
Signature · 1 line
2.34.1
author reviewer
#5 lore raw

PATCH 5/8 read_config_yy: Drop extra argument from dlog() call

In reply to Phil Sutter

False priority value was never printed.

Fixes dfb88dae65fbd ("conntrackd: change scheduler and priority via configuration file")
---
 src/read_config_yy.y | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/read_config_yy.y +1 -1
index 070b349c59498..5815d6ab464e8 100644
@@ -1052,7 +1052,7 @@ scheduler_line : T_PRIO T_NUMBER
 {
 	conf.sched.prio = $2;
 	if (conf.sched.prio < 0 || conf.sched.prio > 99) {
-		dlog(LOG_ERR, "`Priority' must be [0, 99]\n", $2);
+		dlog(LOG_ERR, "`Priority' must be [0, 99]\n");
 		exit(EXIT_FAILURE);
 	}
 };
Signature · 1 line
2.34.1
author reviewer
#6 lore raw

PATCH 6/8 Don't call exit() from signal handler

In reply to Phil Sutter

Coverity tool complains that exit() is not signal-safe and therefore should not be called from within a signal handler. Call _exit() instead.

---
 src/run.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/run.c +1 -1
index f11a5327fe5e6..37a0eb1c6b957 100644
@@ -67,7 +67,7 @@ void killer(int signo)
 	close_log();
 
 	sd_ct_stop();
-	exit(0);
+	_exit(0);
 }
 
 static void child(int foo)
Signature · 1 line
2.34.1
author reviewer
#7 lore raw

PATCH 7/8 Drop pointless assignments

In reply to Phil Sutter

These variables are not referred to after assigning within their scope (or until they're overwritten).

---
 src/helpers/ssdp.c | 1 -
 src/main.c         | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
src/helpers/ssdp.c +0 -1
index 0c6f563c592aa..527892cdabf8a 100644
@@ -256,7 +256,6 @@ static int find_hdr(const char *name, const uint8_t *data, int data_len,
 		data += i+2;
 	}
 
-	data_len -= name_len;
 	data += name_len;
 	if (pos)
 		*pos = data;
src/main.c +1 -1
index 31e0eed950b48..de4773df8a204 100644
@@ -319,7 +319,7 @@ int main(int argc, char *argv[])
 
 	umask(0177);
 
-	if ((ret = init_config(config_file)) == -1) {
+	if (init_config(config_file) == -1) {
 		dlog(LOG_ERR, "can't open config file `%s'", config_file);
 		exit(EXIT_FAILURE);
 	}
Signature · 1 line
2.34.1
author reviewer
#8 lore raw

PATCH 8/8 connntrack: Fix for memleak when parsing -j arg

In reply to Phil Sutter

Have to free the strings allocated by split_address_and_port().

Fixes 29b390a212214 ("conntrack: Support IPv6 NAT")
---
 src/conntrack.c | 2 ++
 1 file changed, 2 insertions(+)
src/conntrack.c +2 -0
index 679a1d27e250a..894bf3f6bf440 100644
@@ -3113,6 +3113,8 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 					nfct_set_nat_details(c, tmpl->ct, &ad,
 							     port_str, family);
 				}
+				free(port_str);
+				free(nat_address);
 			}
 			break;
 		case 'w':
Signature · 1 line
2.34.1
#9 lore raw
In reply to Phil Sutter

LGTM

Quoted 25 lines · Phil Sutter

Phil Sutter (8):
hash: Flush tables when destroying
cache: Fix features array allocation
Fix potential buffer overrun in snprintf() calls
helpers: ftp: Avoid ugly casts
read_config_yy: Drop extra argument from dlog() call Don't call exit() from signal handler
Drop pointless assignments
connntrack: Fix for memleak when parsing -j arg

 src/cache.c          |  4 ++--
 src/conntrack.c      |  2 ++
 src/hash.c           |  1 +
 src/helpers/ftp.c    | 20 +++++++++-----------
 src/helpers/ssdp.c   |  1 -
 src/main.c           |  2 +-
 src/process.c        |  2 +-
 src/queue.c          |  4 ++--
 src/read_config_yy.y |  2 +-
 src/run.c            |  2 +-
 10 files changed, 20 insertions(+), 20 deletions(-)
Signature · 1 line
2.34.1