Skip to contentSkip to conversation

Some covscan fixes

PATCH New Phil Sutter 1 participant 11 messages Jun 11, 2021 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Some covscan fixes

This series fixes a bunch of minor issues identified by Coverity tool.

Phil Sutter (10):
parser_bison: Fix for implicit declaration of isalnum
parser_json: Fix for memleak in tcp option error path
evaluate: Mark fall through case in str2hooknum()
json: Drop pointless assignment in exthdr_expr_json()
netlink: Avoid memleak in error path of netlink_delinearize_set()
netlink: Avoid memleak in error path of netlink_delinearize_chain()
netlink: Avoid memleak in error path of netlink_delinearize_table()
netlink: Avoid memleak in error path of netlink_delinearize_obj()
netlink_delinearize: Fix suspicious calloc() call
rule: Fix for potential off-by-one in cmd_add_loc()

 src/evaluate.c            | 1 +
 src/json.c                | 1 -
 src/netlink.c             | 7 +++++--
 src/netlink_delinearize.c | 5 ++---
 src/parser_bison.y        | 1 +
 src/parser_json.c         | 6 +++---
 src/rule.c                | 2 +-
 7 files changed, 13 insertions(+), 10 deletions(-)
Signature · 1 line
2.31.1
author reviewer
#1 lore raw

PATCH 1/10 parser_bison: Fix for implicit declaration of isalnum

Have to include ctype.h to make it known.

Fixes e76bb37940181 ("src: allow for variables in the log prefix string")
---
 src/parser_bison.y | 1 +
 1 file changed, 1 insertion(+)
src/parser_bison.y +1 -0
index 136ae105f5132..c0a9dc52963ea 100644
@@ -10,6 +10,7 @@
 
 %{
 
+#include <ctype.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <inttypes.h>
Signature · 1 line
2.31.1
author reviewer
#2 lore raw

PATCH 2/10 parser_json: Fix for memleak in tcp option error path

In reply to Phil Sutter

If 'kind' value is invalid, the function returned without freeing 'expr' first. Fix this by performing the check before allocation.

Fixes cb21869649208 ("json: tcp: add raw tcp option match support")
---
 src/parser_json.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/parser_json.c +3 -3
index e6a0233ab6ce3..bb0e4169b477d 100644
@@ -603,12 +603,12 @@ static struct expr *json_parse_tcp_option_expr(struct json_ctx *ctx,
 			"base", &kind, "offset", &offset, "len", &len)) {
 		uint32_t flag = 0;
 
-		expr = tcpopt_expr_alloc(int_loc, kind,
-					 TCPOPT_COMMON_KIND);
-
 		if (kind < 0 || kind > 255)
 			return NULL;
 
+		expr = tcpopt_expr_alloc(int_loc, kind,
+					 TCPOPT_COMMON_KIND);
+
 		if (offset == TCPOPT_COMMON_KIND && len == 8)
 			flag = NFT_EXTHDR_F_PRESENT;
 
Signature · 1 line
2.31.1
author reviewer
#3 lore raw

PATCH 3/10 evaluate: Mark fall through case in str2hooknum()

In reply to Phil Sutter

It is certainly intentional, so just mark it as such.

Fixes b4775dec9f80b ("src: ingress inet support")
---
 src/evaluate.c | 1 +
 1 file changed, 1 insertion(+)
src/evaluate.c +1 -0
index 43f1f8a3977b2..ee79f160f8ff3 100644
@@ -4145,6 +4145,7 @@ static uint32_t str2hooknum(uint32_t family, const char *hook)
 	case NFPROTO_INET:
 		if (!strcmp(hook, "ingress"))
 			return NF_INET_INGRESS;
+		/* fall through */
 	case NFPROTO_IPV4:
 	case NFPROTO_BRIDGE:
 	case NFPROTO_IPV6:
Signature · 1 line
2.31.1
author reviewer
#4 lore raw

PATCH 4/10 json: Drop pointless assignment in exthdr_expr_json()

In reply to Phil Sutter

The updated value of 'is_exists' is no longer read at this point.

Fixes cb21869649208 ("json: tcp: add raw tcp option match support")
---
 src/json.c | 1 -
 1 file changed, 1 deletion(-)
src/json.c +0 -1
index f648ea1b8c178..f111ad678f8a0 100644
@@ -708,7 +708,6 @@ json_t *exthdr_expr_json(const struct expr *expr, struct output_ctx *octx)
 					 "base", expr->exthdr.raw_type,
 					 "offset", expr->exthdr.offset,
 					 "len", expr->len);
-			is_exists = false;
 		}
 
 		return json_pack("{s:o}", "tcp option", root);
Signature · 1 line
2.31.1
author reviewer
#5 lore raw

PATCH 5/10 netlink: Avoid memleak in error path of netlink_delinearize_set()

In reply to Phil Sutter

Duplicate string 'comment' later when the function does not fail anymore.

Fixes 0864c2d49ee8a ("src: add comment support for set declarations")
---
 src/netlink.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/netlink.c +2 -2
index e91b06e3ea971..41cce3379ca50 100644
@@ -867,7 +867,7 @@ struct set *netlink_delinearize_set(struct netlink_ctx *ctx,
 		if (ud[NFTNL_UDATA_SET_DATA_TYPEOF])
 			typeof_expr_data = set_make_key(ud[NFTNL_UDATA_SET_DATA_TYPEOF]);
 		if (ud[NFTNL_UDATA_SET_COMMENT])
-			comment = xstrdup(nftnl_udata_get(ud[NFTNL_UDATA_SET_COMMENT]));
+			comment = nftnl_udata_get(ud[NFTNL_UDATA_SET_COMMENT]);
 	}
 
 	key = nftnl_set_get_u32(nls, NFTNL_SET_KEY_TYPE);
@@ -905,7 +905,7 @@ struct set *netlink_delinearize_set(struct netlink_ctx *ctx,
 	set->handle.set.name = xstrdup(nftnl_set_get_str(nls, NFTNL_SET_NAME));
 	set->automerge	   = automerge;
 	if (comment)
-		set->comment = comment;
+		set->comment = xstrdup(comment);
 
 	init_list_head(&set_parse_ctx.stmt_list);
 
Signature · 1 line
2.31.1
author reviewer
#6 lore raw

PATCH 6/10 netlink: Avoid memleak in error path of netlink_delinearize_chain()

In reply to Phil Sutter

If parsing udata fails, 'chain' has to be freed before returning to caller.

Fixes 702ac2b72c0e8 ("src: add comment support for chains")
---
 src/netlink.c | 1 +
 1 file changed, 1 insertion(+)
src/netlink.c +1 -0
index 41cce3379ca50..1bbdf98bd2ee2 100644
@@ -599,6 +599,7 @@ struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx,
 		udata = nftnl_chain_get_data(nlc, NFTNL_CHAIN_USERDATA, &ulen);
 		if (nftnl_udata_parse(udata, ulen, chain_parse_udata_cb, ud) < 0) {
 			netlink_io_error(ctx, NULL, "Cannot parse userdata");
+			chain_free(chain);
 			return NULL;
 		}
 		if (ud[NFTNL_UDATA_CHAIN_COMMENT])
Signature · 1 line
2.31.1
author reviewer
#7 lore raw

PATCH 7/10 netlink: Avoid memleak in error path of netlink_delinearize_table()

In reply to Phil Sutter

If parsing udata fails, 'table' has to be freed before returning to caller.

Fixes c156232a530b3 ("src: add comment support when adding tables")
---
 src/netlink.c | 1 +
 1 file changed, 1 insertion(+)
src/netlink.c +1 -0
index 1bbdf98bd2ee2..be98bfb7f5c12 100644
@@ -647,6 +647,7 @@ struct table *netlink_delinearize_table(struct netlink_ctx *ctx,
 		udata = nftnl_table_get_data(nlt, NFTNL_TABLE_USERDATA, &ulen);
 		if (nftnl_udata_parse(udata, ulen, table_parse_udata_cb, ud) < 0) {
 			netlink_io_error(ctx, NULL, "Cannot parse userdata");
+			table_free(table);
 			return NULL;
 		}
 		if (ud[NFTNL_UDATA_TABLE_COMMENT])
Signature · 1 line
2.31.1
author reviewer
#8 lore raw

PATCH 8/10 netlink: Avoid memleak in error path of netlink_delinearize_obj()

In reply to Phil Sutter

If parsing udata fails, 'obj' has to be freed before returning to caller.

Fixes 293c9b114faef ("src: add comment support for objects")
---
 src/netlink.c | 1 +
 1 file changed, 1 insertion(+)
src/netlink.c +1 -0
index be98bfb7f5c12..f2c1a4a15dee8 100644
@@ -1450,6 +1450,7 @@ struct obj *netlink_delinearize_obj(struct netlink_ctx *ctx,
 		udata = nftnl_obj_get_data(nlo, NFTNL_OBJ_USERDATA, &ulen);
 		if (nftnl_udata_parse(udata, ulen, obj_parse_udata_cb, ud) < 0) {
 			netlink_io_error(ctx, NULL, "Cannot parse userdata");
+			obj_free(obj);
 			return NULL;
 		}
 		if (ud[NFTNL_UDATA_OBJ_COMMENT])
Signature · 1 line
2.31.1
author reviewer
#9 lore raw

PATCH 9/10 netlink_delinearize: Fix suspicious calloc() call

In reply to Phil Sutter

Parameter passed to sizeof() was wrong. While being at it, replace the whole call with xmalloc_array() which takes care of error checking.

Fixes 913979f882d13 ("src: add expression handler hashtable")
---
 src/netlink_delinearize.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
src/netlink_delinearize.c +2 -3
index a71d06d7fe12f..9a1cf3c4f7d90 100644
@@ -1733,9 +1733,8 @@ void expr_handler_init(void)
 	unsigned int i;
 	uint32_t hash;
 
-	expr_handle_ht = calloc(NFT_EXPR_HSIZE, sizeof(expr_handle_ht));
-	if (!expr_handle_ht)
-		memory_allocation_error();
+	expr_handle_ht = xmalloc_array(NFT_EXPR_HSIZE,
+				       sizeof(expr_handle_ht[0]));
 
 	for (i = 0; i < array_size(netlink_parsers); i++) {
 		hash = djb_hash(netlink_parsers[i].name) % NFT_EXPR_HSIZE;
Signature · 1 line
2.31.1
author reviewer
#10 lore raw

PATCH 10/10 rule: Fix for potential off-by-one in cmd_add_loc()

In reply to Phil Sutter

Using num_attrs as index means it must be at max one less than the array's size at function start.

Fixes 27362a5bfa433 ("rule: larger number of error locations")
---
 src/rule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/rule.c +1 -1
index dbbe744eee0d8..92daf2f33b76b 100644
@@ -1275,7 +1275,7 @@ struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj,
 
 void cmd_add_loc(struct cmd *cmd, uint16_t offset, const struct location *loc)
 {
-	if (cmd->num_attrs > NFT_NLATTR_LOC_MAX)
+	if (cmd->num_attrs >= NFT_NLATTR_LOC_MAX)
 		return;
 
 	cmd->attr[cmd->num_attrs].offset = offset;
Signature · 1 line
2.31.1