DPDK patches and discussions
 help / color / mirror / Atom feed
From: Tejasree Kondoj <ktejasree@marvell.com>
To: Akhil Goyal <gakhil@marvell.com>
Cc: Nithinsen Kaithakadan <nkaithakadan@marvell.com>,
	Anoob Joseph <anoobj@marvell.com>,
	Aakash Sasidharan <asasidharan@marvell.com>,
	"Rupesh Chiluka" <rchiluka@marvell.com>,
	Sucharitha Sarananaga <ssarananaga@marvell.com>,
	Vidya Sagar Velumuri <vvelumuri@marvell.com>, <dev@dpdk.org>,
	<stable@dpdk.org>
Subject: [PATCH v3 10/10] crypto/cnxk: fix tls mbuf sanity failures
Date: Mon, 29 Sep 2025 15:13:53 +0530	[thread overview]
Message-ID: <20250929094353.1027744-11-ktejasree@marvell.com> (raw)
In-Reply-To: <20250929094353.1027744-1-ktejasree@marvell.com>

From: Nithinsen Kaithakadan <nkaithakadan@marvell.com>

Fix mbuf sanity check failure by zeroing data_len of each
segment from the target len onward and adjusting pkt_len
in the head mbuf. Hence by avoiding call to free mbuf from
intermediate node.

Fixes: 9a126e7cf088 ("crypto/cnxk: support TLS padding verification")
Fixes: c05eb27d55d8 ("crypto/cnxk: add CN20K TLS post-process")
Cc: stable@dpdk.org

Signed-off-by: Nithinsen Kaithakadan <nkaithakadan@marvell.com>
---
 drivers/crypto/cnxk/cn10k_cryptodev_ops.c | 40 ++++++++---------------
 drivers/crypto/cnxk/cn20k_cryptodev_ops.c | 39 +++++++---------------
 drivers/crypto/cnxk/cnxk_cryptodev_ops.h  | 26 +++++++++++++++
 3 files changed, 51 insertions(+), 54 deletions(-)

diff --git a/drivers/crypto/cnxk/cn10k_cryptodev_ops.c b/drivers/crypto/cnxk/cn10k_cryptodev_ops.c
index d6b95a14aa..870e65c049 100644
--- a/drivers/crypto/cnxk/cn10k_cryptodev_ops.c
+++ b/drivers/crypto/cnxk/cn10k_cryptodev_ops.c
@@ -931,13 +931,13 @@ cn10k_cpt_ipsec_post_process(struct rte_crypto_op *cop, struct cpt_cn10k_res_s *
 static inline void
 cn10k_cpt_tls12_trim_mac(struct rte_crypto_op *cop, struct cpt_cn10k_res_s *res, uint8_t mac_len)
 {
-	struct rte_mbuf *mac_prev_seg = NULL, *mac_seg = NULL, *seg;
 	uint32_t pad_len, trim_len, mac_offset, pad_offset;
 	struct rte_mbuf *mbuf = cop->sym->m_src;
-	uint16_t m_len = res->rlen;
-	uint32_t i, nb_segs = 1;
+	uint16_t m_len = res->rlen, len_to_trim;
+	struct rte_mbuf *seg;
 	uint8_t pad_res = 0;
 	uint8_t pad_val;
+	uint32_t i;
 
 	pad_val = ((res->spi >> 16) & 0xff);
 	pad_len = pad_val + 1;
@@ -966,11 +966,8 @@ cn10k_cpt_tls12_trim_mac(struct rte_crypto_op *cop, struct cpt_cn10k_res_s *res,
 	seg = mbuf;
 	while (mac_offset >= seg->data_len) {
 		mac_offset -= seg->data_len;
-		mac_prev_seg = seg;
 		seg = seg->next;
-		nb_segs++;
 	}
-	mac_seg = seg;
 
 	pad_offset = mac_offset + mac_len;
 	while (pad_offset >= seg->data_len) {
@@ -995,17 +992,9 @@ cn10k_cpt_tls12_trim_mac(struct rte_crypto_op *cop, struct cpt_cn10k_res_s *res,
 		cop->aux_flags = res->uc_compcode;
 	}
 
-	mbuf->pkt_len = m_len - trim_len;
-	if (mac_offset) {
-		rte_pktmbuf_free(mac_seg->next);
-		mac_seg->next = NULL;
-		mac_seg->data_len = mac_offset;
-		mbuf->nb_segs = nb_segs;
-	} else {
-		rte_pktmbuf_free(mac_seg);
-		mac_prev_seg->next = NULL;
-		mbuf->nb_segs = nb_segs - 1;
-	}
+	len_to_trim = mbuf->pkt_len - (m_len - trim_len);
+
+	pktmbuf_trim_chain(mbuf, len_to_trim);
 }
 
 /* TLS-1.3:
@@ -1016,11 +1005,11 @@ cn10k_cpt_tls12_trim_mac(struct rte_crypto_op *cop, struct cpt_cn10k_res_s *res,
 static inline void
 cn10k_cpt_tls13_trim_mac(struct rte_crypto_op *cop, struct cpt_cn10k_res_s *res)
 {
+	uint16_t m_len = res->rlen, len_to_trim;
 	struct rte_mbuf *mbuf = cop->sym->m_src;
 	struct rte_mbuf *seg = mbuf;
-	uint16_t m_len = res->rlen;
 	uint8_t *ptr, type = 0x0;
-	int len, i, nb_segs = 1;
+	int len, i;
 
 	while (m_len && !type) {
 		len = m_len;
@@ -1030,7 +1019,6 @@ cn10k_cpt_tls13_trim_mac(struct rte_crypto_op *cop, struct cpt_cn10k_res_s *res)
 		while (len > seg->data_len) {
 			len -= seg->data_len;
 			seg = seg->next;
-			nb_segs++;
 		}
 
 		/* walkthrough from last until a non zero value is found */
@@ -1043,16 +1031,14 @@ cn10k_cpt_tls13_trim_mac(struct rte_crypto_op *cop, struct cpt_cn10k_res_s *res)
 		m_len -= len;
 	}
 
+	len_to_trim = mbuf->pkt_len - i;
+
 	if (type) {
+		pktmbuf_trim_chain(mbuf, len_to_trim);
 		cop->param1.tls_record.content_type = type;
-		mbuf->pkt_len = m_len + i;
-		mbuf->nb_segs = nb_segs;
-		seg->data_len = i;
-		rte_pktmbuf_free(seg->next);
-		seg->next = NULL;
-	} else {
+
+	} else
 		cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
-	}
 }
 
 static inline void
diff --git a/drivers/crypto/cnxk/cn20k_cryptodev_ops.c b/drivers/crypto/cnxk/cn20k_cryptodev_ops.c
index b696c28081..1803e4ba5a 100644
--- a/drivers/crypto/cnxk/cn20k_cryptodev_ops.c
+++ b/drivers/crypto/cnxk/cn20k_cryptodev_ops.c
@@ -896,13 +896,13 @@ cn20k_cpt_ipsec_post_process(struct rte_crypto_op *cop, struct cpt_cn20k_res_s *
 static inline void
 cn20k_cpt_tls12_trim_mac(struct rte_crypto_op *cop, struct cpt_cn20k_res_s *res, uint8_t mac_len)
 {
-	struct rte_mbuf *mac_prev_seg = NULL, *mac_seg = NULL, *seg;
 	uint32_t pad_len, trim_len, mac_offset, pad_offset;
 	struct rte_mbuf *mbuf = cop->sym->m_src;
-	uint16_t m_len = res->rlen;
-	uint32_t i, nb_segs = 1;
+	uint16_t m_len = res->rlen, len_to_trim;
+	struct rte_mbuf *seg;
 	uint8_t pad_res = 0;
 	uint8_t pad_val;
+	uint32_t i;
 
 	pad_val = ((res->spi >> 16) & 0xff);
 	pad_len = pad_val + 1;
@@ -931,11 +931,8 @@ cn20k_cpt_tls12_trim_mac(struct rte_crypto_op *cop, struct cpt_cn20k_res_s *res,
 	seg = mbuf;
 	while (mac_offset >= seg->data_len) {
 		mac_offset -= seg->data_len;
-		mac_prev_seg = seg;
 		seg = seg->next;
-		nb_segs++;
 	}
-	mac_seg = seg;
 
 	pad_offset = mac_offset + mac_len;
 	while (pad_offset >= seg->data_len) {
@@ -960,17 +957,9 @@ cn20k_cpt_tls12_trim_mac(struct rte_crypto_op *cop, struct cpt_cn20k_res_s *res,
 		cop->aux_flags = res->uc_compcode;
 	}
 
-	mbuf->pkt_len = m_len - trim_len;
-	if (mac_offset) {
-		rte_pktmbuf_free(mac_seg->next);
-		mac_seg->next = NULL;
-		mac_seg->data_len = mac_offset;
-		mbuf->nb_segs = nb_segs;
-	} else {
-		rte_pktmbuf_free(mac_seg);
-		mac_prev_seg->next = NULL;
-		mbuf->nb_segs = nb_segs - 1;
-	}
+	len_to_trim = mbuf->pkt_len - (m_len - trim_len);
+
+	pktmbuf_trim_chain(mbuf, len_to_trim);
 }
 
 /* TLS-1.3:
@@ -981,11 +970,11 @@ cn20k_cpt_tls12_trim_mac(struct rte_crypto_op *cop, struct cpt_cn20k_res_s *res,
 static inline void
 cn20k_cpt_tls13_trim_mac(struct rte_crypto_op *cop, struct cpt_cn20k_res_s *res)
 {
+	uint16_t m_len = res->rlen, len_to_trim;
 	struct rte_mbuf *mbuf = cop->sym->m_src;
 	struct rte_mbuf *seg = mbuf;
-	uint16_t m_len = res->rlen;
 	uint8_t *ptr, type = 0x0;
-	int len, i, nb_segs = 1;
+	int len, i;
 
 	while (m_len && !type) {
 		len = m_len;
@@ -995,7 +984,6 @@ cn20k_cpt_tls13_trim_mac(struct rte_crypto_op *cop, struct cpt_cn20k_res_s *res)
 		while (len > seg->data_len) {
 			len -= seg->data_len;
 			seg = seg->next;
-			nb_segs++;
 		}
 
 		/* walkthrough from last until a non zero value is found */
@@ -1008,16 +996,13 @@ cn20k_cpt_tls13_trim_mac(struct rte_crypto_op *cop, struct cpt_cn20k_res_s *res)
 		m_len -= len;
 	}
 
+	len_to_trim = mbuf->pkt_len - i;
+
 	if (type) {
+		pktmbuf_trim_chain(mbuf, len_to_trim);
 		cop->param1.tls_record.content_type = type;
-		mbuf->pkt_len = m_len + i;
-		mbuf->nb_segs = nb_segs;
-		seg->data_len = i;
-		rte_pktmbuf_free(seg->next);
-		seg->next = NULL;
-	} else {
+	} else
 		cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
-	}
 }
 
 static inline void
diff --git a/drivers/crypto/cnxk/cnxk_cryptodev_ops.h b/drivers/crypto/cnxk/cnxk_cryptodev_ops.h
index 17d39aa34f..02223fbf3a 100644
--- a/drivers/crypto/cnxk/cnxk_cryptodev_ops.h
+++ b/drivers/crypto/cnxk/cnxk_cryptodev_ops.h
@@ -239,4 +239,30 @@ cnxk_cpt_sec_inst_w7_get(struct roc_cpt *roc_cpt, void *cptr)
 
 	return w7.u64;
 }
+
+static inline void
+pktmbuf_trim_chain(struct rte_mbuf *m, uint16_t len)
+{
+	uint16_t len_so_far = 0, left_over = 0, new_mlen;
+	struct rte_mbuf *cur = m;
+
+	new_mlen = m->pkt_len - len;
+
+	while (len_so_far < new_mlen) {
+		left_over = new_mlen - len_so_far;
+		if (left_over < cur->data_len)
+			break;
+		len_so_far += cur->data_len;
+		cur = cur->next;
+	}
+
+	cur->data_len = left_over;
+	cur = cur->next;
+	while (cur) {
+		cur->data_len = 0;
+		cur = cur->next;
+	}
+
+	m->pkt_len = new_mlen;
+}
 #endif /* _CNXK_CRYPTODEV_OPS_H_ */
-- 
2.25.1


      parent reply	other threads:[~2025-09-29  9:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-29  9:43 [PATCH v3 00/10] fixes and improvements to cnxk crypto PMD Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 01/10] common/cnxk: get context ilen as devarg Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 02/10] crypto/cnxk: fix compilation error and warnings Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 03/10] crypto/cnxk: add new API to get fpm tbl address Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 04/10] crypto/cnxk: add new API to get EC grp " Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 05/10] crypto/cnxk: align cptr to 256B in cn20k Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 06/10] crypto/cnxk: refactor rsa verification Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 07/10] crypto/cnxk: align PDCP API with latest firmware Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 08/10] crypto/cnxk: support custom metadata with CN20K Tejasree Kondoj
2025-09-29  9:43 ` [PATCH v3 09/10] common/cnxk: optimize ROC layer Tejasree Kondoj
2025-09-29  9:43 ` Tejasree Kondoj [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250929094353.1027744-11-ktejasree@marvell.com \
    --to=ktejasree@marvell.com \
    --cc=anoobj@marvell.com \
    --cc=asasidharan@marvell.com \
    --cc=dev@dpdk.org \
    --cc=gakhil@marvell.com \
    --cc=nkaithakadan@marvell.com \
    --cc=rchiluka@marvell.com \
    --cc=ssarananaga@marvell.com \
    --cc=stable@dpdk.org \
    --cc=vvelumuri@marvell.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).