Skip to contentSkip to conversation

Fix some covscan findings

PATCH In review Phil Sutter 1 participant 7 messages Jan 12, 2023 – Jan 13, 2023 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Fix some covscan findings

All these are rather minor issues, no big deal.

Phil Sutter (5):
optimize: Clarify chain_optimize() array allocations
optimize: Do not return garbage from stack
netlink: Fix for potential NULL-pointer deref
meta: parse_iso_date() returns boolean
mnl: dump_nf_hooks() leaks memory in error path

 src/meta.c     |  2 +-
 src/mnl.c      | 11 +++++++++--
 src/netlink.c  |  3 ++-
 src/optimize.c |  9 +++++----
 4 files changed, 17 insertions(+), 8 deletions(-)
Signature · 1 line
2.38.0
author reviewer
#1 lore raw

PATCH 1/5 optimize: Clarify chain_optimize() array allocations

Arguments passed to sizeof() where deemed suspicious by covscan due to the different type. Consistently specify size of an array 'a' using 'sizeof(*a) * nmemb'.

For the statement arrays in stmt_matrix, even use xzalloc_array() since the item count is fixed and therefore can't be zero.

Fixes fb298877ece27 ("src: add ruleset optimization infrastructure")
---
 src/optimize.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
src/optimize.c +4 -3
index 32aed866eb49f..12cae00da4ab4 100644
@@ -1113,10 +1113,11 @@ static int chain_optimize(struct nft_ctx *nft, struct list_head *rules)
 		ctx->num_rules++;
 	}
 
-	ctx->rule = xzalloc(sizeof(ctx->rule) * ctx->num_rules);
-	ctx->stmt_matrix = xzalloc(sizeof(struct stmt *) * ctx->num_rules);
+	ctx->rule = xzalloc(sizeof(*ctx->rule) * ctx->num_rules);
+	ctx->stmt_matrix = xzalloc(sizeof(*ctx->stmt_matrix) * ctx->num_rules);
 	for (i = 0; i < ctx->num_rules; i++)
-		ctx->stmt_matrix[i] = xzalloc(sizeof(struct stmt *) * MAX_STMTS);
+		ctx->stmt_matrix[i] = xzalloc_array(MAX_STMTS,
+						    sizeof(**ctx->stmt_matrix));
 
 	merge = xzalloc(sizeof(*merge) * ctx->num_rules);
 
Signature · 1 line
2.38.0
author reviewer
#2 lore raw

PATCH 2/5 optimize: Do not return garbage from stack

In reply to Phil Sutter

From: Phil Sutter <[email protected]>

If input does not contain a single 'add' command (unusual, but possible), 'ret' value was not initialized by nft_optimize() before returning its value.

Fixes fb298877ece27 ("src: add ruleset optimization infrastructure")
---
 src/optimize.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/optimize.c +1 -1
index 12cae00da4ab4..289c442dc915e 100644
@@ -1217,7 +1217,7 @@ static int cmd_optimize(struct nft_ctx *nft, struct cmd *cmd)
 int nft_optimize(struct nft_ctx *nft, struct list_head *cmds)
 {
 	struct cmd *cmd;
-	int ret;
+	int ret = 0;
 
 	list_for_each_entry(cmd, cmds, list) {
 		switch (cmd->op) {
Signature · 1 line
2.38.0
author reviewer
#3 lore raw

PATCH 3/5 netlink: Fix for potential NULL-pointer deref

In reply to Phil Sutter

If memory allocation fails, calloc() returns NULL which was not checked for. The code seems to expect zero array size though, so simply replacing this call by one of the x*calloc() ones won't work. So guard the call also by a check for 'len'.

Fixes db0697ce7f602 ("src: support for flowtable listing")
---
 src/netlink.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/netlink.c +2 -1
index 51de9c9c8edb2..efae125148b8c 100644
@@ -1790,7 +1790,8 @@ netlink_delinearize_flowtable(struct netlink_ctx *ctx,
 	while (dev_array[len])
 		len++;
 
-	flowtable->dev_array = calloc(1, len * sizeof(char *));
+	if (len)
+		flowtable->dev_array = xmalloc(len * sizeof(char *));
 	for (i = 0; i < len; i++)
 		flowtable->dev_array[i] = xstrdup(dev_array[i]);
 
Signature · 1 line
2.38.0
author reviewer
#4 lore raw

PATCH 4/5 meta: parse_iso_date() returns boolean

In reply to Phil Sutter

Returning ts if 'ts == (time_t) -1' signals success to caller despite failure.

Fixes 4460b839b945a ("meta: fix compiler warning in date_type_parse()")
---
 src/meta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/meta.c +1 -1
index bd8a41ba3a032..013e8cbaf38a5 100644
@@ -433,7 +433,7 @@ static bool parse_iso_date(uint64_t *tstamp, const char *sym)
 	cur_tm = localtime(&ts);
 
 	if (ts == (time_t) -1 || cur_tm == NULL)
-		return ts;
+		return false;
 
 	/* Substract tm_gmtoff to get the current time */
 	*tstamp = ts - cur_tm->tm_gmtoff;
Signature · 1 line
2.38.0
author reviewer
#5 lore raw

PATCH 5/5 mnl: dump_nf_hooks() leaks memory in error path

In reply to Phil Sutter

Have to free the basehook object before returning to caller.

Fixes 4694f7230195b ("src: add support for base hook dumping")
---
 src/mnl.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
src/mnl.c +9 -2
index 62b0b59c2da8a..46d86f0fd9502 100644
@@ -2217,16 +2217,23 @@ static int dump_nf_hooks(const struct nlmsghdr *nlh, void *_data)
 		struct nlattr *nested[NFNLA_HOOK_INFO_MAX + 1] = {};
 		uint32_t type;
 
-		if (mnl_attr_parse_nested(tb[NFNLA_HOOK_CHAIN_INFO], dump_nf_chain_info_cb, nested) < 0)
+		if (mnl_attr_parse_nested(tb[NFNLA_HOOK_CHAIN_INFO],
+					  dump_nf_chain_info_cb, nested) < 0) {
+			basehook_free(hook);
 			return -1;
+		}
 
 		type = ntohl(mnl_attr_get_u32(nested[NFNLA_HOOK_INFO_TYPE]));
 		if (type == NFNL_HOOK_TYPE_NFTABLES) {
 			struct nlattr *info[NFNLA_CHAIN_MAX + 1] = {};
 			const char *tablename, *chainname;
 
-			if (mnl_attr_parse_nested(nested[NFNLA_HOOK_INFO_DESC], dump_nf_attr_chain_cb, info) < 0)
+			if (mnl_attr_parse_nested(nested[NFNLA_HOOK_INFO_DESC],
+						  dump_nf_attr_chain_cb,
+						  info) < 0) {
+				basehook_free(hook);
 				return -1;
+			}
 
 			tablename = mnl_attr_get_str(info[NFNLA_CHAIN_TABLE]);
 			chainname = mnl_attr_get_str(info[NFNLA_CHAIN_NAME]);
Signature · 1 line
2.38.0
author reviewer
#6 lore raw
In reply to Phil Sutter
Quoted 1 line · Phil Sutter

All these are rather minor issues, no big deal.

Series applied after fixing up my email address in one of them.