git: 4abc3b482e0d - main - libpfctl: fix Coverity issues

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Tue, 24 Oct 2023 09:34:03 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=4abc3b482e0d246cd3518622223795c8de102130

commit 4abc3b482e0d246cd3518622223795c8de102130
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2023-10-23 11:46:11 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2023-10-24 07:50:47 +0000

    libpfctl: fix Coverity issues
    
     - handle snl_finalize_msg() returning NULL
     - insert the correct data into the states list
     - add missing nvlist_destroy()
     - incorrect order for array bounds
    
    Coverity:       1522929, 1522925, 1522923, 1522921, 1522780, 1522770, 1522764, 1487785, 1471250
    Reviewed by:    emaste
    MFC after:      1 week
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D42330
---
 lib/libpfctl/libpfctl.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c
index 22b0471e2912..335aeb8e2c8c 100644
--- a/lib/libpfctl/libpfctl.c
+++ b/lib/libpfctl/libpfctl.c
@@ -196,6 +196,8 @@ pfctl_startstop(int start)
 	    start ? PFNL_CMD_START : PFNL_CMD_STOP);
 
 	hdr = snl_finalize_msg(&nw);
+	if (hdr == NULL)
+		return (ENOMEM);
 	seq_id = hdr->nlmsg_seq;
 
 	snl_send_message(&ss, hdr);
@@ -730,6 +732,8 @@ pfctl_get_eth_ruleset(int dev, const char *path, int nr,
 	strlcpy(ri->name, nvlist_get_string(nvl, "name"),
 	    PF_ANCHOR_NAME_SIZE);
 
+	nvlist_destroy(nvl);
+
 	return (0);
 }
 
@@ -828,8 +832,8 @@ pfctl_add_eth_rule(int dev, const struct pfctl_eth_rule *r, const char *anchor,
 	pfctl_nv_add_rule_addr(nvl, "ipdst", &r->ipdst);
 
 	labelcount = 0;
-	while (r->label[labelcount][0] != 0 &&
-	    labelcount < PF_RULE_MAX_LABEL_COUNT) {
+	while (labelcount < PF_RULE_MAX_LABEL_COUNT &&
+	    r->label[labelcount][0] != 0) {
 		nvlist_append_string_array(nvl, "labels",
 		    r->label[labelcount]);
 		labelcount++;
@@ -1208,6 +1212,8 @@ pfctl_get_creators_nl(struct snl_state *ss, uint32_t *creators, size_t *len)
 	hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GETCREATORS);
 	hdr->nlmsg_flags |= NLM_F_DUMP;
 	hdr = snl_finalize_msg(&nw);
+	if (hdr == NULL)
+		return (ENOMEM);
 	uint32_t seq_id = hdr->nlmsg_seq;
 
 	snl_send_message(ss, hdr);
@@ -1362,6 +1368,8 @@ pfctl_get_states_nl(struct pfctl_state_filter *filter, struct snl_state *ss, pfc
 	snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_MASK, &filter->mask.v6);
 
 	hdr = snl_finalize_msg(&nw);
+	if (hdr == NULL)
+		return (ENOMEM);
 
 	uint32_t seq_id = hdr->nlmsg_seq;
 
@@ -1417,7 +1425,7 @@ pfctl_append_states(struct pfctl_state *s, void *arg)
 
 	memcpy(new, s, sizeof(*s));
 
-	TAILQ_INSERT_TAIL(&states->states, s, entry);
+	TAILQ_INSERT_TAIL(&states->states, new, entry);
 
 	return (0);
 }