Skip to contentSkip to conversation

net/softnic: fix memory illegal access

PATCH Superseded Jasvinder Singh 4 participants 6 messages Jul 20, 2018 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/softnic: fix memory illegal access

While deleting the elements from the linked list, TAILQ_FOREACH causes read from the freed pointer. Fixes the issue by using for loop instead of TAILQ_FOREACH.

Coverity issue: 302867

Fixes bef50bcb1c47 ("net/softnic: implement start and stop")
---
 drivers/net/softnic/rte_eth_softnic_swq.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/softnic/rte_eth_softnic_swq.c +4 -2
index 1944fbb..a1f1899 100644
@@ -36,9 +36,11 @@ softnic_swq_free(struct pmd_internals *p)
 void
 softnic_softnic_swq_free_keep_rxq_txq(struct pmd_internals *p)
 {
-	struct softnic_swq *swq;
+	struct softnic_swq *swq, *swq_next;
+
+	for (swq = TAILQ_FIRST(&p->swq_list); swq != NULL; swq = swq_next) {
+		swq_next = TAILQ_NEXT(swq, node);
 
-	TAILQ_FOREACH(swq, &p->swq_list, node) {
 		if ((strncmp(swq->name, "RXQ", strlen("RXQ")) == 0) ||
 			(strncmp(swq->name, "TXQ", strlen("TXQ")) == 0))
 			continue;
Signature · 1 line
2.9.3
#1 lore raw
Quoted 20 lines · Jasvinder Singh

While deleting the elements from the linked list, TAILQ_FOREACH causes read from the freed pointer. Fixes the issue by using for loop instead of TAILQ_FOREACH.

Coverity issue: 302867

Fixes bef50bcb1c47 ("net/softnic: implement start and stop")
---
 drivers/net/softnic/rte_eth_softnic_swq.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/softnic/rte_eth_softnic_swq.c +0 -0
index 1944fbb..a1f1899 100644
@@ -36,9 +36,11 @@ softnic_swq_free(struct pmd_internals *p)
 void
 softnic_softnic_swq_free_keep_rxq_txq(struct pmd_internals *p)

drivers/net/softnic/rte_eth_softnic_swq.c softnic_swq_free(struct pmd_internals *p) · line 45

Quoted 10 lines
drivers/net/softnic/rte_eth_softnic_swq.c +4 -2
 {
-	struct softnic_swq *swq;
+	struct softnic_swq *swq, *swq_next;
+
+	for (swq = TAILQ_FIRST(&p->swq_list); swq != NULL; swq = swq_next) {
+		swq_next = TAILQ_NEXT(swq, node);
 
-	TAILQ_FOREACH(swq, &p->swq_list, node) {
 		if ((strncmp(swq->name, "RXQ", strlen("RXQ")) == 0) ||
 			(strncmp(swq->name, "TXQ", strlen("TXQ")) == 0))

TAILQ_FOREACH_SAFE is probably what you want to use here.

Quoted 1 line

From man page:

The macros TAILQ_FOREACH, TAILQ_FOREACH_REVERSE, TAILQ_FOREACH_SAFE, and TAILQ_FOREACH_REVERSE_SAFE traverse the tail queue referenced by head in the forward or reverse direction direction, assigning each element in turn to var.

The SAFE versions use tmp to hold the next element, so var may be freed or removed from the list.

#2 lore raw
In reply to Jasvinder Singh
Quoted 29 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Jasvinder Singh
Sent: Friday, July 20, 2018 10:45 AM
To: [email protected]
Cc: Dumitrescu, Cristian <[email protected]>
Subject: [dpdk-dev] [PATCH] net/softnic: fix memory illegal access

While deleting the elements from the linked list, TAILQ_FOREACH causes read from the freed pointer. Fixes the issue by using for loop instead of TAILQ_FOREACH.

Coverity issue: 302867

Fixes bef50bcb1c47 ("net/softnic: implement start and stop")
---
 drivers/net/softnic/rte_eth_softnic_swq.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/softnic/rte_eth_softnic_swq.c +0 -0
index 1944fbb..a1f1899 100644
@@ -36,9 +36,11 @@ softnic_swq_free(struct pmd_internals *p)
 void
 softnic_softnic_swq_free_keep_rxq_txq(struct pmd_internals *p)
 {

drivers/net/softnic/rte_eth_softnic_swq.c softnic_swq_free(struct pmd_internals *p) · line 46

Quoted 10 lines
drivers/net/softnic/rte_eth_softnic_swq.c +4 -2
-	struct softnic_swq *swq;
+	struct softnic_swq *swq, *swq_next;
+
+	for (swq = TAILQ_FIRST(&p->swq_list); swq != NULL; swq = swq_next) {
+		swq_next = TAILQ_NEXT(swq, node);
 
-	TAILQ_FOREACH(swq, &p->swq_list, node) {
 		if ((strncmp(swq->name, "RXQ", strlen("RXQ")) == 0) ||
 			(strncmp(swq->name, "TXQ", strlen("TXQ")) == 0))
 			continue;

The TAILQ_FOREACH_SAFE() macro handles exactly this case. Although it is not in the linux TAILQ header, DPDK provides it in rte_tailq.h:

http://git.dpdk.org/dpdk/tree/lib/librte_eal/common/include/rte_tailq.h#n130

I think it is cleaner to use the MACRO instead of manually doing the loop, linked-list iter + delete is error prone enough already :)

#3 lore raw
Quoted 51 lines

-----Original Message-----
From: Van Haaren, Harry
Sent: Friday, July 20, 2018 11:32 AM
To: Singh, Jasvinder <[email protected]>; [email protected]
Cc: Dumitrescu, Cristian <[email protected]>
Subject: RE: [dpdk-dev] [PATCH] net/softnic: fix memory illegal access

Quoted 27 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Jasvinder Singh
Sent: Friday, July 20, 2018 10:45 AM
To: [email protected]
Cc: Dumitrescu, Cristian <[email protected]>
Subject: [dpdk-dev] [PATCH] net/softnic: fix memory illegal access

While deleting the elements from the linked list, TAILQ_FOREACH causes read from the freed pointer. Fixes the issue by using for loop instead of TAILQ_FOREACH.

Coverity issue: 302867

Fixes bef50bcb1c47 ("net/softnic: implement start and stop")
---
 drivers/net/softnic/rte_eth_softnic_swq.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/softnic/rte_eth_softnic_swq.c +0 -0
index 1944fbb..a1f1899 100644
@@ -36,9 +36,11 @@ softnic_swq_free(struct pmd_internals *p)  void
 softnic_softnic_swq_free_keep_rxq_txq(struct pmd_internals *p)  {

drivers/net/softnic/rte_eth_softnic_swq.c softnic_swq_free(struct pmd_internals *p) void · line 44

Quoted 10 lines
drivers/net/softnic/rte_eth_softnic_swq.c +4 -2
-	struct softnic_swq *swq;
+	struct softnic_swq *swq, *swq_next;
+
+	for (swq = TAILQ_FIRST(&p->swq_list); swq != NULL; swq = swq_next) {
+		swq_next = TAILQ_NEXT(swq, node);
 
-	TAILQ_FOREACH(swq, &p->swq_list, node) {
 		if ((strncmp(swq->name, "RXQ", strlen("RXQ")) == 0) ||
 			(strncmp(swq->name, "TXQ", strlen("TXQ")) == 0))
 			continue;

The TAILQ_FOREACH_SAFE() macro handles exactly this case. Although it is not in the linux TAILQ header, DPDK provides it in rte_tailq.h:

Quoted 5 lines

http://git.dpdk.org/dpdk/tree/lib/librte_eal/common/include/rte_tailq.h#n13
0

I think it is cleaner to use the MACRO instead of manually doing the loop, linked-list iter + delete is error prone enough already :)

Macro lessens the confusion as well :) thanks, Harry!

#4 lore raw

PATCH v2 1/1 net/softnic: fix memory illegal access

In reply to Jasvinder Singh

While deleting the elements from the linked list, TAILQ_FOREACH causes read from the freed pointer. Fixes the issue by using TAILQ_FOREACH_SAFE instead.

Coverity issue: 302867

Fixes bef50bcb1c47 ("net/softnic: implement start and stop")
---
 drivers/net/softnic/rte_eth_softnic_swq.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/softnic/rte_eth_softnic_swq.c +3 -2
index 1944fbb..2083d0a 100644
@@ -6,6 +6,7 @@
 #include <string.h>
 
 #include <rte_string_fns.h>
+#include <rte_tailq.h>
 
 #include "rte_eth_softnic_internals.h"
 
@@ -36,9 +37,9 @@ softnic_swq_free(struct pmd_internals *p)
 void
 softnic_softnic_swq_free_keep_rxq_txq(struct pmd_internals *p)
 {
-	struct softnic_swq *swq;
+	struct softnic_swq *swq, *tswq;
 
-	TAILQ_FOREACH(swq, &p->swq_list, node) {
+	TAILQ_FOREACH_SAFE(swq, &p->swq_list, node, tswq) {
 		if ((strncmp(swq->name, "RXQ", strlen("RXQ")) == 0) ||
 			(strncmp(swq->name, "TXQ", strlen("TXQ")) == 0))
 			continue;
Signature · 1 line
2.9.3