From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 61C39A0352; Tue, 5 May 2020 23:41:33 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 163EB1D6E8; Tue, 5 May 2020 23:41:33 +0200 (CEST) Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) by dpdk.org (Postfix) with ESMTP id EBE2A1D6E5 for ; Tue, 5 May 2020 23:41:31 +0200 (CEST) Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20200505214130euoutp021788894c26f4eaa7ffaf8ba2b7c38d7f~MP8abdy2Z1709617096euoutp02j for ; Tue, 5 May 2020 21:41:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20200505214130euoutp021788894c26f4eaa7ffaf8ba2b7c38d7f~MP8abdy2Z1709617096euoutp02j DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1588714890; bh=ccRlXkgwTjGLqkKYmCSIWwv+M66GLBCGnPbuZvx6MY0=; h=From:To:Cc:Subject:Date:References:From; b=PcgUR52Jiv6uT2wrklvsBw3oWKuMGaWAmXCRjniZJgIRzAhf1b0fY2S2OJLToVT6G oKmZmZdU6iaHhKVoeSAnxdLmtHpGfDBpsdaMiMIYjw8xBnKWjhFzXqJSactGU3Ao7F XIP/pzdh6QRQFcTzmtKSw7MYYdfiu5E7SoeKaXJ8= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20200505214129eucas1p1dacbc661753c3bc74affd1e302f595d7~MP8ZgTPA50905209052eucas1p13; Tue, 5 May 2020 21:41:29 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id A9.15.60679.98DD1BE5; Tue, 5 May 2020 22:41:29 +0100 (BST) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20200505214128eucas1p26202807ca7da9e16c1628fd63cb2dfeb~MP8Yo_egi1857718577eucas1p2X; Tue, 5 May 2020 21:41:28 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20200505214128eusmtrp2bf6a1de390113a1f109a3d8ac4976aa2~MP8YodJrd0253702537eusmtrp2K; Tue, 5 May 2020 21:41:28 +0000 (GMT) X-AuditID: cbfec7f4-0cbff7000001ed07-7b-5eb1dd8927c2 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 75.E2.08375.88DD1BE5; Tue, 5 May 2020 22:41:28 +0100 (BST) Received: from localhost.localdomain (unknown [106.210.88.70]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20200505214128eusmtip1502a865d8997b128d68b4815dbe459e8~MP8YJxmsR1722617226eusmtip1J; Tue, 5 May 2020 21:41:28 +0000 (GMT) From: Lukasz Wojciechowski To: Akhil Goyal , Hemant Agrawal Cc: dev@dpdk.org, l.wojciechow@partner.samsung.com Date: Tue, 5 May 2020 23:41:04 +0200 Message-Id: <20200505214105.19465-1-l.wojciechow@partner.samsung.com> X-Mailer: git-send-email 2.17.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrCIsWRmVeSWpSXmKPExsWy7djPc7qddzfGGTRM1LdYf2Yeo8W7T9uZ LFY+3shm8axnHaMDi8evBUtZPTa+28HkcfDdHqYA5igum5TUnMyy1CJ9uwSujEeX0gqaDSo+ nVnG1sC4Xa2LkZNDQsBE4vzs2WxdjFwcQgIrGCXur7gL5XxhlOg8/hrK+cwocfH5F0aYlsWX FrOB2EICyxklLk8ugSua9WYDWBGbgK3EkZlfWUFsEYE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y4srcQ4wSHMxKIrzLfmyIE+JNSaysSi3Kjy8qzUktPsRoCrR6IrOUaHI+MNDzSuIN TQ3NLSwNzY3Njc0slMR5OwQOxggJpCeWpGanphakFsH0MXFwSjUwBq9e8s91u7iB4cwg6Xna LZm9HspX54ez8N/zP9EdkPfhhFi2gDdH0y3h5HlnfY92xKhwVplnyGXezZN6IJwmJiG2pN7R I581eOPFWaLB8z8omWxaWnhqWd96sTURcqd3P/hm9Utkv1Dx45bmnpAl0zt+c32YyNbGt7p2 z92MV7JrNl5lj+VTYinOSDTUYi4qTgQA8X/vBTwCAAA= X-CMS-MailID: 20200505214128eucas1p26202807ca7da9e16c1628fd63cb2dfeb X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20200505214128eucas1p26202807ca7da9e16c1628fd63cb2dfeb X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200505214128eucas1p26202807ca7da9e16c1628fd63cb2dfeb References: Subject: [dpdk-dev] [PATCH 1/2] crypto/dpaa_sec: improve memory freeing X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This patch fixes management of memory for authentication and encryption keys. There were two issues with former state of implementation: 1) Invalid access to dpaa_sec_session union members The dpaa_sec_session structure includes an anonymous union: union { struct {...} aead_key; struct { struct {...} cipher_key; struct {...} auth_key; }; }; Depending on the used algorithm a rte_zmalloc() function allocated memory that was kept in aead_key, cipher_key or auth_key. However every time the memory was released, rte_free() was called only on cipher and auth keys, even if pointer to allocated memory was stored in aead_key. The C language specification defines such behavior as undefined. As the cipher_key and aead_key are similar, have same sizes and alignment, it has worked, but it's directly against C specification. This patch fixes this, providing a free_session_data() function to free the keys data. It verifies which algorithm was used (aead or auth+cipher) and frees proper part of the union. 2) Some keys might have been freed multiple times In functions like: dpaa_sec_cipher_init(), dpaa_sec_auth_init(), dpaa_sec_chain_init(), dpaa_sec_aead_init() keys data were freed before returning due to some error conditions. However the pointers were not zeroed causing another calls to ret_free from higher layers of code. This causes an error log about invalid memory address to be printed. This patch fixes it by making only one layer responsible for freeing memory: * dpaa_sec_set_session_parameters() for allocations made in: - dpaa_sec_cipher_init() - dpaa_sec_auth_init() - dpaa_sec_chain_init() - dpaa_sec_aead_init() * dpaa_sec_set_ipsec_session() for allocations made in: - dpaa_sec_ipsec_aead_init() - dpaa_sec_ipsec_proto_init() * dpaa_sec_set_pdcp_session() for allocations made in: - dpaa_sec_set_pdcp_session() /*only one layer in case of pdcp*/ Signed-off-by: Lukasz Wojciechowski --- drivers/crypto/dpaa_sec/dpaa_sec.c | 45 ++++++++++++++++-------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/drivers/crypto/dpaa_sec/dpaa_sec.c b/drivers/crypto/dpaa_sec/dpaa_sec.c index a11b17bda..021a5639d 100644 --- a/drivers/crypto/dpaa_sec/dpaa_sec.c +++ b/drivers/crypto/dpaa_sec/dpaa_sec.c @@ -219,6 +219,13 @@ dpaa_sec_init_tx(struct qman_fq *fq) return ret; } +static inline int is_aead(dpaa_sec_session *ses) +{ + return ((ses->cipher_alg == 0) && + (ses->auth_alg == 0) && + (ses->aead_alg != 0)); +} + static inline int is_encode(dpaa_sec_session *ses) { return ses->dir == DIR_ENC; @@ -2040,7 +2047,6 @@ dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused, default: DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u", xform->cipher.algo); - rte_free(session->cipher_key.data); return -1; } session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ? @@ -2108,7 +2114,6 @@ dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused, default: DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u", xform->auth.algo); - rte_free(session->auth_key.data); return -1; } @@ -2151,7 +2156,6 @@ dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused, RTE_CACHE_LINE_SIZE); if (session->auth_key.data == NULL && auth_xform->key.length > 0) { DPAA_SEC_ERR("No Memory for auth key"); - rte_free(session->cipher_key.data); return -ENOMEM; } session->auth_key.length = auth_xform->key.length; @@ -2191,7 +2195,7 @@ dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused, default: DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u", auth_xform->algo); - goto error_out; + return -1; } session->cipher_alg = cipher_xform->algo; @@ -2212,16 +2216,11 @@ dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused, default: DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u", cipher_xform->algo); - goto error_out; + return -1; } session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ? DIR_ENC : DIR_DEC; return 0; - -error_out: - rte_free(session->cipher_key.data); - rte_free(session->auth_key.data); - return -1; } static int @@ -2253,7 +2252,6 @@ dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused, break; default: DPAA_SEC_ERR("unsupported AEAD alg %d", session->aead_alg); - rte_free(session->aead_key.data); return -ENOMEM; } @@ -2323,6 +2321,18 @@ dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess) return ret; } +static inline void +free_session_data(dpaa_sec_session *s) +{ + if (is_aead(s)) + rte_free(s->aead_key.data); + else { + rte_free(s->auth_key.data); + rte_free(s->cipher_key.data); + } + memset(s, 0, sizeof(dpaa_sec_session)); +} + static int dpaa_sec_set_session_parameters(struct rte_cryptodev *dev, struct rte_crypto_sym_xform *xform, void *sess) @@ -2415,10 +2425,7 @@ dpaa_sec_set_session_parameters(struct rte_cryptodev *dev, return 0; err1: - rte_free(session->cipher_key.data); - rte_free(session->auth_key.data); - memset(session, 0, sizeof(dpaa_sec_session)); - + free_session_data(session); return -EINVAL; } @@ -2467,9 +2474,7 @@ free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s) s->inq[i] = NULL; s->qp[i] = NULL; } - rte_free(s->cipher_key.data); - rte_free(s->auth_key.data); - memset(s, 0, sizeof(dpaa_sec_session)); + free_session_data(s); rte_mempool_put(sess_mp, (void *)s); } @@ -2836,9 +2841,7 @@ dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev, return 0; out: - rte_free(session->auth_key.data); - rte_free(session->cipher_key.data); - memset(session, 0, sizeof(dpaa_sec_session)); + free_session_data(session); return -1; } -- 2.17.1