Skip to contentSkip to conversation

examples/flow_filtering: fix null derefs and memory leaks

PATCH Applied Shani Peretz 2 participants 2 messages Mar 31 – Jul 10

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 examples/flow_filtering: fix null derefs and memory leaks

Add missing return statements after failed calloc NULL checks to prevent null pointer dereferences.

Free previously allocated memory on error paths to prevent leaked storage when a subsequent allocation fails.

Fixes 3ebb8789136a ("examples/flow_filtering: add more snippets")
---
 .../snippets/snippet_match_gre.c              | 13 +++++++---
 .../snippets/snippet_match_integrity_flags.c  | 26 +++++++++++++++----
 .../snippets/snippet_match_ipv4.c             | 13 +++++++---
 .../snippets/snippet_match_mpls.c             |  8 ++++--
 .../snippets/snippet_match_nsh.c              | 13 +++++++---
 .../snippets/snippet_match_nvgre.c            | 26 +++++++++++++++----
 .../snippets/snippet_match_packet_type.c      | 13 +++++++---
 .../snippets/snippet_match_port_affinity.c    |  1 +
 .../snippets/snippet_match_roce_ib_bth.c      |  8 ++++--
 .../snippets/snippet_match_vxlan_gbp.c        | 13 +++++++---
 .../snippets/snippet_match_vxlan_gpe.c        |  8 ++++--
 .../snippets/snippet_modify_ecn.c             |  9 +++++--
 .../flow_filtering/snippets/snippet_nat64.c   | 10 +++++++
 .../snippets/snippet_random_match.c           |  8 ++++--
 .../snippets/snippet_switch_granularity.c     |  4 ++-
 15 files changed, 137 insertions(+), 36 deletions(-)
examples/flow_filtering/snippets/snippet_match_gre.c +10 -3
index 477ec59451..f1638f38c5 100644
@@ -21,8 +21,10 @@ snippet_match_gre_create_actions(__rte_unused uint16_t port_id, struct rte_flow_
 {
 	/* Create one action that moves the packet to the selected queue. */
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 
 	/* Set the selected queue. */
 	queue->index = 1;
@@ -40,12 +42,17 @@ snippet_match_gre_create_patterns(struct rte_flow_item *pattern)
 	struct rte_flow_item_gre_opt *gre_opt_spec;
 
 	gre_spec = calloc(1, sizeof(struct rte_flow_item_gre));
-	if (gre_spec == NULL)
+	if (gre_spec == NULL) {
 		fprintf(stderr, "Failed to allocate memory for gre_spec\n");
+		return;
+	}
 
 	gre_opt_spec = calloc(1, sizeof(struct rte_flow_item_gre_opt));
-	if (gre_opt_spec == NULL)
+	if (gre_opt_spec == NULL) {
 		fprintf(stderr, "Failed to allocate memory for gre_opt_spec\n");
+		free(gre_spec);
+		return;
+	}
 
 	/* Set the Checksum GRE option. */
 	gre_spec->c_rsvd0_ver = RTE_BE16(0x8000);
examples/flow_filtering/snippets/snippet_match_integrity_flags.c +21 -5
index 0f05c902b9..3d37b5f2a5 100644
@@ -20,8 +20,10 @@ snippet_match_integrity_flags_create_actions(__rte_unused uint16_t port_id,
 {
 	/* Create one action that moves the packet to the selected queue. */
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 
 	queue->index = 1;
 	action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
@@ -38,16 +40,21 @@ snippet_match_integrity_flags_create_patterns(struct rte_flow_item *pattern)
 	struct rte_flow_item_integrity *integrity_mask;
 
 	integrity_spec = calloc(1, sizeof(struct rte_flow_item_integrity));
-	if (integrity_spec == NULL)
+	if (integrity_spec == NULL) {
 		fprintf(stderr, "Failed to allocate memory for integrity_spec\n");
+		return;
+	}
 
 	integrity_spec->level = 0;
 	integrity_spec->l3_ok = 1;
 	integrity_spec->ipv4_csum_ok = 1;
 
 	integrity_mask = calloc(1, sizeof(struct rte_flow_item_integrity));
-	if (integrity_mask == NULL)
+	if (integrity_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for integrity_mask\n");
+		free(integrity_spec);
+		return;
+	}
 
 	integrity_mask->level = 0;
 	integrity_mask->l3_ok = 1;
@@ -55,12 +62,21 @@ snippet_match_integrity_flags_create_patterns(struct rte_flow_item *pattern)
 	integrity_mask->ipv4_csum_ok = 1;
 
 	ip_spec = calloc(1, sizeof(struct rte_flow_item_ipv4));
-	if (ip_spec == NULL)
+	if (ip_spec == NULL) {
 		fprintf(stderr, "Failed to allocate memory for ip_spec\n");
+		free(integrity_spec);
+		free(integrity_mask);
+		return;
+	}
 
 	ip_mask = calloc(1, sizeof(struct rte_flow_item_ipv4));
-	if (ip_mask == NULL)
+	if (ip_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for ip_mask\n");
+		free(integrity_spec);
+		free(integrity_mask);
+		free(ip_spec);
+		return;
+	}
 
 	ip_spec->hdr.dst_addr = htonl(((192<<24) + (168<<16) + (1<<8) + 1));
 	ip_mask->hdr.dst_addr = 0xffffffff;
examples/flow_filtering/snippets/snippet_match_ipv4.c +10 -3
index 7f8aba66bb..6fcaacdc42 100644
@@ -24,8 +24,10 @@ snippet_ipv4_flow_create_actions(__rte_unused uint16_t port_id, struct rte_flow_
 	 * one action only, move packet to queue
 	 */
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 	queue->index = 1; /* The selected target queue.*/
 	action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
 	action[0].conf = queue;
@@ -53,12 +55,17 @@ snippet_ipv4_flow_create_patterns(struct rte_flow_item *patterns)
 	patterns[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
 
 	ip_spec = calloc(1, sizeof(struct rte_flow_item_ipv4));
-	if (ip_spec == NULL)
+	if (ip_spec == NULL) {
 		fprintf(stderr, "Failed to allocate memory for ip_spec\n");
+		return;
+	}
 
 	ip_mask = calloc(1, sizeof(struct rte_flow_item_ipv4));
-	if (ip_mask == NULL)
+	if (ip_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for ip_mask\n");
+		free(ip_spec);
+		return;
+	}
 
 	/* Match destination IP 192.168.1.1 with full mask */
 	ip_spec->hdr.dst_addr = htonl(((192<<24) + (168<<16) + (1<<8) + 1));
examples/flow_filtering/snippets/snippet_match_mpls.c +6 -2
index 494a2d873d..0a54586c98 100644
@@ -25,8 +25,10 @@ snippet_mpls_create_actions(__rte_unused uint16_t port_id, struct rte_flow_actio
 	struct rte_flow_action_queue *queue;
 
 	queue = calloc(1, sizeof(struct rte_flow_item_ipv4));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 
 	/* Set the selected queue. */
 	queue->index = UINT16_MAX;
@@ -43,8 +45,10 @@ snippet_mpls_create_patterns(struct rte_flow_item *pattern)
 	struct rte_flow_item_mpls *mpls_item;
 
 	mpls_item = calloc(1, sizeof(struct rte_flow_item_ipv4));
-	if (mpls_item == NULL)
+	if (mpls_item == NULL) {
 		fprintf(stderr, "Failed to allocate memory for mpls_item\n");
+		return;
+	}
 
 	memcpy(mpls_item->label_tc_s, "\xab\xcd\xe1", sizeof(mpls_item->label_tc_s));
 
examples/flow_filtering/snippets/snippet_match_nsh.c +10 -3
index 262d0c8d81..6a8ac5b942 100644
@@ -25,8 +25,10 @@ snippet_match_nsh_create_actions(uint16_t port_id, struct rte_flow_action *actio
 	create_jump_flow(port_id, 1, &error);
 
 	struct rte_flow_action_port_id *portid = calloc(1, sizeof(struct rte_flow_action_port_id));
-	if (portid == NULL)
+	if (portid == NULL) {
 		fprintf(stderr, "Failed to allocate memory for port_id\n");
+		return;
+	}
 
 	/* To match on NSH to port_id 1. */
 	portid->id = 1;
@@ -43,12 +45,17 @@ snippet_match_nsh_create_patterns(struct rte_flow_item *pattern)
 	struct rte_flow_item_udp *mask;
 
 	spec = calloc(1, sizeof(struct rte_flow_item_udp));
-	if (spec == NULL)
+	if (spec == NULL) {
 		fprintf(stderr, "Failed to allocate memory for spec\n");
+		return;
+	}
 
 	mask = calloc(1, sizeof(struct rte_flow_item_udp));
-	if (mask == NULL)
+	if (mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for mask\n");
+		free(spec);
+		return;
+	}
 
 	/* Set the patterns. */
 	pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
examples/flow_filtering/snippets/snippet_match_nvgre.c +21 -5
index 4ae06c5f1a..3ed62db4a2 100644
@@ -23,8 +23,10 @@ snippet_match_nvgre_create_actions(uint16_t port_id, struct rte_flow_action *act
 	create_jump_flow(port_id, 1, &error);
 
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 	queue->index = 1;
 
 	action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
@@ -36,20 +38,34 @@ void
 snippet_match_nvgre_create_patterns(struct rte_flow_item *pattern)
 {
 	struct rte_flow_item_nvgre *nvgre = calloc(1, sizeof(struct rte_flow_item_nvgre));
-	if (nvgre == NULL)
+	if (nvgre == NULL) {
 		fprintf(stderr, "Failed to allocate memory for nvgre\n");
+		return;
+	}
 
 	struct rte_flow_item_udp *udp = calloc(1, sizeof(struct rte_flow_item_udp));
-	if (udp == NULL)
+	if (udp == NULL) {
 		fprintf(stderr, "Failed to allocate memory for udp\n");
+		free(nvgre);
+		return;
+	}
 
 	struct rte_flow_item_nvgre *nvgre_mask = calloc(1, sizeof(struct rte_flow_item_nvgre));
-	if (nvgre_mask == NULL)
+	if (nvgre_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for nvgre_mask\n");
+		free(nvgre);
+		free(udp);
+		return;
+	}
 
 	struct rte_flow_item_udp *udp_mask = calloc(1, sizeof(struct rte_flow_item_udp));
-	if (udp_mask == NULL)
+	if (udp_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for udp_mask\n");
+		free(nvgre);
+		free(udp);
+		free(nvgre_mask);
+		return;
+	}
 
 	/* build rule to match specific NVGRE:
 	 * tni = 0x12346, flow_id = 0x78, inner_udp_src = 0x1234
examples/flow_filtering/snippets/snippet_match_packet_type.c +10 -3
index 3b16d4b0ab..97c0a88f3d 100644
@@ -20,8 +20,10 @@ snippet_match_packet_type_create_actions(__rte_unused uint16_t port_id,
 {
 	/* Create one action that moves the packet to the selected queue. */
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 
 	/*
 	 * create the action sequence.
@@ -38,15 +40,20 @@ snippet_match_packet_type_create_patterns(struct rte_flow_item *pattern)
 {
 	struct rte_flow_item_ptype *ptype_spec;
 	ptype_spec = calloc(1, sizeof(struct rte_flow_item_ptype));
-	if (ptype_spec == NULL)
+	if (ptype_spec == NULL) {
 		fprintf(stderr, "Failed to allocate memory for ptype_spec\n");
+		return;
+	}
 
 	ptype_spec->packet_type = RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_TCP;
 
 	struct rte_flow_item_ptype *ptype_mask;
 	ptype_mask = calloc(1, sizeof(struct rte_flow_item_ptype));
-	if (ptype_mask == NULL)
+	if (ptype_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for ptype_mask\n");
+		free(ptype_spec);
+		return;
+	}
 
 	ptype_mask->packet_type = RTE_PTYPE_L3_MASK | RTE_PTYPE_L4_MASK;
 
examples/flow_filtering/snippets/snippet_match_port_affinity.c +1 -0
index 3709121173..a7fb8a49b4 100644
@@ -86,6 +86,7 @@ snippet_match_port_affinity_create_patterns(struct rte_flow_item *pattern)
 		calloc(1, sizeof(struct rte_flow_item_aggr_affinity));
 	if (affinity_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for affinity_mask\n");
+		free(affinity_spec);
 		return;
 	}
 
examples/flow_filtering/snippets/snippet_match_roce_ib_bth.c +6 -2
index f3c7e3eb70..6714fa734e 100644
@@ -26,8 +26,10 @@ snippet_match_roce_ib_bth_create_actions(uint16_t port_id, struct rte_flow_actio
 
 	/* Create one action that moves the packet to the selected queue. */
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 
 	/* Set the selected queue. */
 	queue->index = 1;
@@ -44,8 +46,10 @@ snippet_match_roce_ib_bth_create_patterns(struct rte_flow_item *pattern)
 	struct rte_flow_item_ib_bth *bth;
 
 	bth = calloc(1, sizeof(struct rte_flow_item_ib_bth));
-	if (bth == NULL)
+	if (bth == NULL) {
 		fprintf(stderr, "Failed to allocate memory for bth\n");
+		return;
+	}
 
 	bth->hdr.opcode = 0x81;
 	bth->hdr.dst_qp[0] = 0x0;
examples/flow_filtering/snippets/snippet_match_vxlan_gbp.c +10 -3
index 521e9aae94..ccd66f8d44 100644
@@ -21,8 +21,10 @@ snippet_match_vxlan_gbp_create_actions(__rte_unused uint16_t port_id,
 					struct rte_flow_action *action)
 {
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 
 	queue->index = 1;
 
@@ -35,12 +37,17 @@ void
 snippet_match_vxlan_gbp_create_patterns(struct rte_flow_item *pattern)
 {
 	struct rte_flow_item_vxlan *vxlan_gbp = calloc(1, sizeof(struct rte_flow_item_vxlan));
-	if (vxlan_gbp == NULL)
+	if (vxlan_gbp == NULL) {
 		fprintf(stderr, "Failed to allocate memory for vxlan_gbp\n");
+		return;
+	}
 
 	struct rte_flow_item_vxlan *vxlan_gbp_mask = calloc(1, sizeof(struct rte_flow_item_vxlan));
-	if (vxlan_gbp_mask == NULL)
+	if (vxlan_gbp_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for vxlan_gbp_mask\n");
+		free(vxlan_gbp);
+		return;
+	}
 
 	uint8_t vni[] = {0x00, 0x00, 0x00};
 	uint16_t group_policy_id = 0x200;
examples/flow_filtering/snippets/snippet_match_vxlan_gpe.c +6 -2
index 08c71a58e0..62b70ba5c9 100644
@@ -23,8 +23,10 @@ snippet_match_vxlan_gpe_create_actions(uint16_t port_id, struct rte_flow_action
 	create_jump_flow(port_id, 1, &error);
 
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 	queue->index = 1;
 
 	action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
@@ -37,8 +39,10 @@ snippet_match_vxlan_gpe_create_patterns(struct rte_flow_item *pattern)
 {
 	struct rte_flow_item_vxlan_gpe *vxlan_gpe_mask = calloc(1,
 		sizeof(struct rte_flow_item_vxlan_gpe));
-	if (vxlan_gpe_mask == NULL)
+	if (vxlan_gpe_mask == NULL) {
 		fprintf(stderr, "Failed to allocate memory for vxlan_gpe_mask\n");
+		return;
+	}
 	memset(vxlan_gpe_mask->hdr.vni, 0xff, 3);
 
 	pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
examples/flow_filtering/snippets/snippet_modify_ecn.c +7 -2
index 74bc708036..563a394dc6 100644
@@ -22,13 +22,18 @@ snippet_match_modify_ecn_create_actions(__rte_unused uint16_t port_id,
 {
 	/* Create one action that moves the packet to the selected queue. */
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 
 	struct rte_flow_action_modify_field *modify_field =
 					calloc(1, sizeof(struct rte_flow_action_modify_field));
-	if (modify_field == NULL)
+	if (modify_field == NULL) {
 		fprintf(stderr, "Failed to allocate memory for modify_field\n");
+		free(queue);
+		return;
+	}
 
 	queue->index = 1;
 	modify_field->operation = RTE_FLOW_MODIFY_SET;
examples/flow_filtering/snippets/snippet_nat64.c +10 -0
index 5c87abb3be..2190a625fd 100644
@@ -23,7 +23,17 @@ snippet_match_nat64_create_actions(uint16_t port_id, struct rte_flow_action *act
 	create_jump_flow(port_id, 1, &error);
 
 	struct rte_flow_action_nat64 *nat64_v = calloc(1, sizeof(struct rte_flow_action_nat64));
+	if (nat64_v == NULL) {
+		fprintf(stderr, "Failed to allocate memory for nat64_v\n");
+		return;
+	}
+
 	struct rte_flow_action_jump *jump_v = calloc(1, sizeof(struct rte_flow_action_jump));
+	if (jump_v == NULL) {
+		fprintf(stderr, "Failed to allocate memory for jump_v\n");
+		free(nat64_v);
+		return;
+	}
 
 	nat64_v->type = RTE_FLOW_NAT64_4TO6;
 	jump_v->group = 2;
examples/flow_filtering/snippets/snippet_random_match.c +6 -2
index 60e252a6ca..36adfdfa6b 100644
@@ -20,8 +20,10 @@ snippet_match_random_value_create_actions(__rte_unused uint16_t port_id,
 					struct rte_flow_action *action)
 {
 	struct rte_flow_action_queue *queue = calloc(1, sizeof(struct rte_flow_action_queue));
-	if (queue == NULL)
+	if (queue == NULL) {
 		fprintf(stderr, "Failed to allocate memory for queue\n");
+		return;
+	}
 	queue->index = UINT16_MAX; /* The selected target queue.*/
 	action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
 	action[0].conf = queue;
@@ -33,8 +35,10 @@ snippet_match_random_value_create_patterns(struct rte_flow_item *pattern)
 {
 	struct rte_flow_item_random *random_item;
 	random_item = calloc(1, sizeof(struct rte_flow_item_random));
-	if (random_item == NULL)
+	if (random_item == NULL) {
 		fprintf(stderr, "Failed to allocate memory for port_representor_spec\n");
+		return;
+	}
 
 	random_item->value = 0;
 
examples/flow_filtering/snippets/snippet_switch_granularity.c +3 -1
index 414870bf89..a6b40e8d90 100644
@@ -27,8 +27,10 @@ snippet_match_switch_granularity_create_actions(uint16_t port_id, struct rte_flo
 
 	struct rte_flow_action_ethdev *represented_port = calloc(1,
 		sizeof(struct rte_flow_action_ethdev));
-	if (represented_port == NULL)
+	if (represented_port == NULL) {
 		fprintf(stderr, "Failed to allocate memory for represented_port\n");
+		return;
+	}
 
 	represented_port->port_id = 0;
 	action[0].type = RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT;
Signature · 1 line
2.43.0
#1 lore raw

31/03/2026 14:53, Shani Peretz:

Quoted 9 lines

Add missing return statements after failed calloc NULL checks to prevent null pointer dereferences.

Free previously allocated memory on error paths to prevent leaked storage when a subsequent allocation fails.

Fixes 3ebb8789136a ("examples/flow_filtering: add more snippets")

Applied, thanks.