IPsec flow parsing leaves the pattern-match item allocated. Validation
has no callback to release the parsed flow item, and creation leaks it
when adding the security policy fails.

Free the pattern-match item after parsing, add a validation callback
to release the parsed flow item, and free it if policy creation fails.
Report a parsing error when no flow item was produced.

Fixes: 6bc987ecb860 ("net/iavf: support IPsec inline crypto")
Cc: [email protected]

Signed-off-by: Anatoly Burakov <[email protected]>
---
 drivers/net/intel/iavf/iavf_ipsec_crypto.c | 40 +++++++++++++++++-----
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/drivers/net/intel/iavf/iavf_ipsec_crypto.c 
b/drivers/net/intel/iavf/iavf_ipsec_crypto.c
index 9fcdc311377..cd13e14ae74 100644
--- a/drivers/net/intel/iavf/iavf_ipsec_crypto.c
+++ b/drivers/net/intel/iavf/iavf_ipsec_crypto.c
@@ -1893,6 +1893,7 @@ iavf_ipsec_flow_create(struct iavf_adapter *ad,
        }
 
        if (flow_id < 1) {
+               rte_free(ipsec_flow);
                rte_flow_error_set(error, EINVAL,
                                RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
                                "Failed to add SA.");
@@ -1923,6 +1924,16 @@ iavf_ipsec_flow_destroy(struct iavf_adapter *ad,
        return 0;
 }
 
+static int
+iavf_ipsec_flow_validation(__rte_unused struct iavf_adapter *ad,
+               __rte_unused struct rte_flow *flow,
+               void *meta,
+               __rte_unused struct rte_flow_error *error)
+{
+       rte_free(meta);
+       return 0;
+}
+
 static int
 iavf_ipsec_flow_parse(struct iavf_adapter *ad,
                      struct iavf_pattern_match_item *array,
@@ -1942,6 +1953,7 @@ iavf_ipsec_flow_parse(struct iavf_adapter *ad,
                .max_actions = 1,
        };
        struct iavf_pattern_match_item *item = NULL;
+       struct iavf_ipsec_flow_item *fi = NULL;
        int ret = -1;
 
        ret = ci_flow_check_attr(attr, NULL, error);
@@ -1953,17 +1965,28 @@ iavf_ipsec_flow_parse(struct iavf_adapter *ad,
                return ret;
 
        item = iavf_search_pattern_match_item(pattern, array, array_len, error);
-       if (item && item->meta) {
+       if (item == NULL)
+               return -rte_errno;
+
+       if (item->meta != NULL) {
                const struct rte_security_session *session = 
parsed_actions.actions[0]->conf;
                uint32_t type = (uint64_t)(item->meta);
-               struct iavf_ipsec_flow_item *fi =
-                               iavf_ipsec_flow_item_parse(ad->vf.eth_dev, 
pattern, session, type);
-               if (fi && meta) {
-                       *meta = fi;
-                       ret = 0;
-               }
+
+               fi = iavf_ipsec_flow_item_parse(ad->vf.eth_dev, pattern, 
session, type);
        }
-       return ret;
+       rte_free(item);
+
+       if (fi == NULL)
+               return rte_flow_error_set(error, EINVAL,
+                               RTE_FLOW_ERROR_TYPE_ACTION, NULL,
+                               "Invalid IPsec flow.");
+
+       if (meta != NULL)
+               *meta = fi;
+       else
+               rte_free(fi);
+
+       return 0;
 }
 
 /*
@@ -2060,6 +2083,7 @@ static struct iavf_flow_engine iavf_ipsec_flow_engine = {
        .uninit = iavf_ipsec_flow_uninit,
        .create = iavf_ipsec_flow_create,
        .destroy = iavf_ipsec_flow_destroy,
+       .validation = iavf_ipsec_flow_validation,
        .name = "ipsec_crypto",
        .type = IAVF_FLOW_ENGINE_IPSEC_CRYPTO,
        .rule_size = sizeof(struct iavf_ipsec_flow_item),
-- 
2.52.0

Reply via email to