bnxt_free_filter_mem() freed each filter and then passed the freed
pointer to STAILQ_REMOVE, which walks the list to unlink it.
STAILQ_FOREACH also read the next pointer out of the freed element.

The loop empties the list, so take each entry off the head and free it
after it is unlinked. STAILQ_REMOVE_HEAD also avoids the list walk that
STAILQ_REMOVE does to find the element.

Found while moving the list macros into a DPDK header, where the
compiler could see them and report -Wuse-after-free. The fix does
not depend on that work.

Fixes: f11fd694a84a ("net/bnxt: free memory allocated for VF filters")
Cc: [email protected]

Signed-off-by: Stephen Hemminger <[email protected]>
---
 drivers/net/bnxt/bnxt_filter.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/net/bnxt/bnxt_filter.c b/drivers/net/bnxt/bnxt_filter.c
index 7b90ba651f..541b329e81 100644
--- a/drivers/net/bnxt/bnxt_filter.c
+++ b/drivers/net/bnxt/bnxt_filter.c
@@ -151,10 +151,9 @@ void bnxt_free_filter_mem(struct bnxt *bp)
        bp->filter_info = NULL;
 
        for (i = 0; i < bp->pf->max_vfs; i++) {
-               STAILQ_FOREACH(filter, &bp->pf->vf_info[i].filter, next) {
+               while ((filter = STAILQ_FIRST(&bp->pf->vf_info[i].filter))) {
+                       STAILQ_REMOVE_HEAD(&bp->pf->vf_info[i].filter, next);
                        rte_free(filter);
-                       STAILQ_REMOVE(&bp->pf->vf_info[i].filter, filter,
-                                     bnxt_filter_info, next);
                }
        }
 }
-- 
2.53.0

Reply via email to