Skip to contentSkip to conversation

unsorted fixes

PATCH New Pablo Neira Ayuso 1 participant 9 messages Aug 18, 2015 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover unsorted fixes

The following patchset contains a set of fixes for conntrackd as reported by Paul Wouters.

Let me know if you have any concern with it, thank you.

Pablo Neira Ayuso (8):
conntrackd: fix sanitization of expection attribute in the wire format
conntrackd: NTA_MAX is also an invalid attribute
conntrackd: fix leak in fork_process_new()
conntrackd: fix descriptor leak in do_local_request()
conntrackd: fix error handling in nfq_queue_cb()
conntrackd: simplify branch in tcp_accept()
conntrackd: use strncpy to set up the cache name
conntrackd: missing break in expectation message parser function

 src/cache.c    |    5 +++--
 src/cthelper.c |   29 +++++++++++++++--------------
 src/local.c    |    9 ++++++---
 src/parse.c    |    8 +++++---
 src/process.c  |    2 ++
 src/tcp.c      |   12 +++++-------
 6 files changed, 36 insertions(+), 29 deletions(-)
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

PATCH 1/8 conntrackd: fix sanitization of expection attribute in the wire format

The maximum number of attribute is NTA_EXP_MAX for expectation sync messages.

---
 src/parse.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/parse.c +1 -1
index f3ec6ac..878e354 100644
@@ -510,7 +510,7 @@ int msg2exp(struct nf_expect *exp, struct nethdr *net, size_t remain)
 		ATTR_NETWORK2HOST(attr);
 		if (attr->nta_len > len)
 			goto err;
-		if (attr->nta_attr > NTA_MAX)
+		if (attr->nta_attr >= NTA_EXP_MAX)
 			goto err;
 		if (attr->nta_len < NTA_LENGTH(0))
 			goto err;
Signature · 1 line
1.7.10.4
author maintainer
#2 lore raw

PATCH 2/8 conntrackd: NTA_MAX is also an invalid attribute

In reply to Pablo Neira Ayuso

Otherwise this can result in an off-by-one array access.

---
 src/parse.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/parse.c +1 -1
index 878e354..3ac4092 100644
@@ -297,7 +297,7 @@ int msg2ct(struct nf_conntrack *ct, struct nethdr *net, size_t remain)
 			return -1;
 		if (attr->nta_len < NTA_LENGTH(0))
 			return -1;
-		if (attr->nta_attr > NTA_MAX)
+		if (attr->nta_attr >= NTA_MAX)
 			return -1;
 		if (h[attr->nta_attr].size &&
 		    attr->nta_len != h[attr->nta_attr].size)
Signature · 1 line
1.7.10.4
author maintainer
#3 lore raw

PATCH 3/8 conntrackd: fix leak in fork_process_new()

In reply to Pablo Neira Ayuso

Release the child_process structure in case that fork() fails.

---
 src/process.c |    2 ++
 1 file changed, 2 insertions(+)
src/process.c +2 -0
index 7f0a395..3ddad5f 100644
@@ -48,6 +48,8 @@ int fork_process_new(int type, int flags, void (*cb)(void *data), void *data)
 
 	if (c->pid > 0)
 		list_add(&c->head, &process_list);
+	else
+		free(c);
 
 	return pid;
 }
Signature · 1 line
1.7.10.4
author maintainer
#4 lore raw

PATCH 4/8 conntrackd: fix descriptor leak in do_local_request()

In reply to Pablo Neira Ayuso
---
 src/local.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
src/local.c +5 -2
index 453799a..85e5180 100644
@@ -147,11 +147,14 @@ int do_local_request(int request,
 
 	ret = send(fd, &request, sizeof(int), 0);
 	if (ret == -1)
-		return -1;
+		goto err1;
 
 	do_local_client_step(fd, step);
 
 	local_client_destroy(fd);
-	
+
 	return 0;
+err1:
+	local_client_destroy(fd);
+	return -1;
 }
Signature · 1 line
1.7.10.4
author maintainer
#5 lore raw

PATCH 5/8 conntrackd: fix error handling in nfq_queue_cb()

In reply to Pablo Neira Ayuso

Make sure we have a clean exit on error, everything needs to be properly released.

---
 src/cthelper.c |   29 +++++++++++++++--------------
 src/local.c    |    2 +-
 2 files changed, 16 insertions(+), 15 deletions(-)
src/cthelper.c +15 -14
index 6537515..54eb830 100644
@@ -277,11 +277,11 @@ static int nfq_queue_cb(const struct nlmsghdr *nlh, void *data)
 
 	if (!attr[NFQA_PAYLOAD]) {
 		dlog(LOG_ERR, "packet with no payload");
-		goto err;
+		goto err1;
 	}
 	if (!attr[NFQA_CT] || !attr[NFQA_CT_INFO]) {
 		dlog(LOG_ERR, "no CT attached to this packet");
-		goto err;
+		goto err1;
 	}
 
 	pkt = mnl_attr_get_payload(attr[NFQA_PAYLOAD]);
@@ -292,22 +292,22 @@ static int nfq_queue_cb(const struct nlmsghdr *nlh, void *data)
 	queue_num = ntohs(nfg->res_id);
 
 	if (pkt_get(pkt, pktlen, ntohs(ph->hw_protocol), &protoff))
-		goto err;
+		goto err1;
 
 	ct = nfct_new();
 	if (ct == NULL)
-		goto err;
+		goto err1;
 
 	if (nfct_payload_parse(mnl_attr_get_payload(attr[NFQA_CT]),
 			       mnl_attr_get_payload_len(attr[NFQA_CT]),
 			       l3num, ct) < 0) {
 		dlog(LOG_ERR, "cannot convert message to CT");
-		goto err;
+		goto err2;
 	}
 
 	myct = calloc(1, sizeof(struct myct));
 	if (myct == NULL)
-		goto err;
+		goto err2;
 
 	myct->ct = ct;
 	ctinfo = ntohl(mnl_attr_get_u32(attr[NFQA_CT_INFO]));
@@ -315,15 +315,15 @@ static int nfq_queue_cb(const struct nlmsghdr *nlh, void *data)
 	/* XXX: 256 bytes enough for possible NAT mangling in helpers? */
 	pktb = pktb_alloc(AF_INET, pkt, pktlen, 256);
 	if (pktb == NULL)
-		goto err;
+		goto err3;
 
 	/* Misconfiguration: if no helper found, accept the packet. */
 	helper = helper_run(pktb, protoff, myct, ctinfo, queue_num, &verdict);
 	if (!helper)
-		goto err_pktb;
+		goto err4;
 
 	if (pkt_verdict_issue(helper, myct, queue_num, id, verdict, pktb) < 0)
-		goto err_pktb;
+		goto err4;
 
 	nfct_destroy(ct);
 	if (myct->exp != NULL)
@@ -333,18 +333,19 @@ static int nfq_queue_cb(const struct nlmsghdr *nlh, void *data)
 	free(myct);
 
 	return MNL_CB_OK;
-err_pktb:
+err4:
 	pktb_free(pktb);
-err:
+err3:
+	free(myct);
+err2:
+	nfct_destroy(ct);
+err1:
 	/* In case of error, we don't want to disrupt traffic. We accept all.
 	 * This is connection tracking after all. The policy is not to drop
 	 * packet unless we enter some inconsistent state.
 	 */
 	pkt_verdict_error(queue_num, id);
 
-	if (ct != NULL)
-		nfct_destroy(ct);
-
 	return MNL_CB_OK;
 }
 
src/local.c +1 -1
index 85e5180..3395b4c 100644
@@ -77,7 +77,7 @@ int do_local_server_step(struct local_server *server, void *data,
 	int rfd;
 	struct sockaddr_un local;
 	socklen_t sin_size = sizeof(struct sockaddr_un);
-	
+
 	rfd = accept(server->fd, (struct sockaddr *) &local, &sin_size);
 	if (rfd == -1)
 		return -1;
Signature · 1 line
1.7.10.4
author maintainer
#6 lore raw

PATCH 6/8 conntrackd: simplify branch in tcp_accept()

In reply to Pablo Neira Ayuso

The same code is executed regardless the reason why accept() has failed.

---
 src/tcp.c |   12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
src/tcp.c +5 -7
index af27c46..c8f2544 100644
@@ -247,13 +247,11 @@ int tcp_accept(struct tcp_sock *m)
 	/* the other peer wants to connect ... */
 	ret = accept(m->fd, NULL, NULL);
 	if (ret == -1) {
-		if (errno != EAGAIN) {
-			/* unexpected error. Give us another try. */
-			m->state = TCP_SERVER_ACCEPTING;
-		} else {
-			/* waiting for new connections. */
-			m->state = TCP_SERVER_ACCEPTING;
-		}
+		/* unexpected error: Give us another try. Or we have hit
+ 		 * -EAGAIN, in that case we remain in the accepting connections
+		 * state.
+		 */
+		m->state = TCP_SERVER_ACCEPTING;
 	} else {
 		/* the peer finally got connected. */
 		if (fcntl(ret, F_SETFL, O_NONBLOCK) == -1) {
Signature · 1 line
1.7.10.4
author maintainer
#7 lore raw

PATCH 7/8 conntrackd: use strncpy to set up the cache name

In reply to Pablo Neira Ayuso

This is not exposed, but use the strncpy() variant to calm down static code validators.

---
 src/cache.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/cache.c +3 -2
index 7c41e54..79a024f 100644
@@ -34,7 +34,7 @@ struct cache_feature *cache_feature[CACHE_MAX_FEATURE] = {
 };
 
 struct cache *cache_create(const char *name, enum cache_type type,
-			   unsigned int features, 
+			   unsigned int features,
 			   struct cache_extra *extra,
 			   struct cache_ops *ops)
 {
@@ -53,7 +53,8 @@ struct cache *cache_create(const char *name, enum cache_type type,
 		return NULL;
 	memset(c, 0, sizeof(struct cache));
 
-	strcpy(c->name, name);
+	strncpy(c->name, name, CACHE_MAX_NAMELEN);
+	c->name[CACHE_MAX_NAMELEN - 1] = '\0';
 	c->type = type;
 
 	for (i = 0; i < CACHE_MAX_FEATURE; i++) {
Signature · 1 line
1.7.10.4
author maintainer
#8 lore raw

PATCH 8/8 conntrackd: missing break in expectation message parser function

In reply to Pablo Neira Ayuso

Fortunately, the TLVs come in order in the message, however, if the order is changed we'll incorrectly set up the expectation.

---
 src/parse.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/parse.c +3 -1
index 3ac4092..919d36c 100644
@@ -524,13 +524,15 @@ int msg2exp(struct nf_expect *exp, struct nethdr *net, size_t remain)
 			attr = NTA_NEXT(attr, len);
 			continue;
 		}
-		switch(exp_h[attr->nta_attr].exp_attr) {
+		switch (exp_h[attr->nta_attr].exp_attr) {
 		case ATTR_EXP_MASTER:
 			exp_h[attr->nta_attr].parse(master, attr->nta_attr,
 						    NTA_DATA(attr));
+			break;
 		case ATTR_EXP_EXPECTED:
 			exp_h[attr->nta_attr].parse(expected, attr->nta_attr,
 						    NTA_DATA(attr));
+			break;
 		case ATTR_EXP_MASK:
 			exp_h[attr->nta_attr].parse(mask, attr->nta_attr,
 						    NTA_DATA(attr));
Signature · 1 line
1.7.10.4