Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10944825ybi; Thu, 25 Jul 2019 07:26:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqxVvO/9kQkUT0M9fKcViTgcqng9DnM/F0caTmcqXYBZBXYDLJsGBGYikkMNlSNO7sS/m0ov X-Received: by 2002:a17:90a:ab01:: with SMTP id m1mr6900337pjq.69.1564064776786; Thu, 25 Jul 2019 07:26:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564064776; cv=none; d=google.com; s=arc-20160816; b=yLj4Ap5cSeJ3EJsbGw0yV01b8Ks2Nq2OVSBxFdzeUyS2k5wW5gAoeLxkIPIAGrwtqY LUo5vZp4eE4YNv5beOTTxT0p8vcQyktyHZjhAXl7IxUzuoTYamhgakSXDDAKOuvyzfLL SxIYdg/3AxO0fOJo6NFS4Z7qBpbDVSyIhT56UwmdbXdXtxBuVVPTEySDNt8TVpHreNXi Shg9RewFKo7ZqavaWH1eIu7SgktkVp9QQajjjpJNMvB2o0Udz/x3agyC+7ugwUu6OWb6 KWL3niMVZGX2+DOUjOnMdfImt5opCby/7CZHEEcyZAy1UstreFYJQyrNnGECzAk9Svm+ dFNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=o6puhcrfEGv09ouJ4t5R+Sk/QBU3wAKVC/0rADG/AS8=; b=GWLGwwTOntmQmGl8SFhWb7+2ccz039LIww1bhFockrZyZIBwIMPgR5duoll2CFsRJs MpasXVVX64GD8mb9lfHJlsnY96oV7J5V8b9+CwnegeuyQ+3TQtjb6v+p07S86mmUEGuK E8zCRZbfo03kAL6Lid+jAcrbhogG6WJAmIxKP1H3xpsjMd4MUIIAiAViPHyRNCQ/gbDP KYewEvlHi9P2U8nfWQQQJQ+epx4GzGIAKjqjxHX66W+PDRusNp1bbjTdlCdxj1Kv088D y9AvZ2JJihKAhx4FFIJr9xC8O5OyeTJzIZm22bUKfJt5SMGOFkdfRBvaTdukRDP3IgZh JTjQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m32si18540538pld.438.2019.07.25.07.26.02; Thu, 25 Jul 2019 07:26:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404267AbfGYN6l (ORCPT + 99 others); Thu, 25 Jul 2019 09:58:41 -0400 Received: from inva021.nxp.com ([92.121.34.21]:58292 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404174AbfGYN6k (ORCPT ); Thu, 25 Jul 2019 09:58:40 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id AC4CF20071F; Thu, 25 Jul 2019 15:58:37 +0200 (CEST) Received: from inva024.eu-rdc02.nxp.com (inva024.eu-rdc02.nxp.com [134.27.226.22]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 9DCE520071D; Thu, 25 Jul 2019 15:58:37 +0200 (CEST) Received: from lorenz.ea.freescale.net (lorenz.ea.freescale.net [10.171.71.5]) by inva024.eu-rdc02.nxp.com (Postfix) with ESMTP id 41389205E8; Thu, 25 Jul 2019 15:58:37 +0200 (CEST) From: Iuliana Prodan To: Herbert Xu , Horia Geanta , Aymen Sghaier Cc: "David S. Miller" , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-imx Subject: [PATCH v3 03/14] crypto: caam - update IV only when crypto operation succeeds Date: Thu, 25 Jul 2019 16:58:15 +0300 Message-Id: <1564063106-9552-4-git-send-email-iuliana.prodan@nxp.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1564063106-9552-1-git-send-email-iuliana.prodan@nxp.com> References: <1564063106-9552-1-git-send-email-iuliana.prodan@nxp.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Horia Geantă skcipher encryption might fail and in some cases, like (invalid) input length smaller then block size, updating the IV would lead to panic due to copying from a negative offset (req->cryptlen - ivsize). Signed-off-by: Horia Geantă Signed-off-by: Iuliana Prodan --- drivers/crypto/caam/caamalg.c | 5 ++--- drivers/crypto/caam/caamalg_qi.c | 4 +++- drivers/crypto/caam/caamalg_qi2.c | 8 ++++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index 06b4f2d..28d55a0 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -990,10 +990,9 @@ static void skcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err, * ciphertext block (CBC mode) or last counter (CTR mode). * This is used e.g. by the CTS mode. */ - if (ivsize) { + if (ivsize && !ecode) { memcpy(req->iv, (u8 *)edesc->sec4_sg + edesc->sec4_sg_bytes, ivsize); - print_hex_dump_debug("dstiv @"__stringify(__LINE__)": ", DUMP_PREFIX_ADDRESS, 16, 4, req->iv, edesc->src_nents > 1 ? 100 : ivsize, 1); @@ -1030,7 +1029,7 @@ static void skcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err, * ciphertext block (CBC mode) or last counter (CTR mode). * This is used e.g. by the CTS mode. */ - if (ivsize) { + if (ivsize && !ecode) { memcpy(req->iv, (u8 *)edesc->sec4_sg + edesc->sec4_sg_bytes, ivsize); diff --git a/drivers/crypto/caam/caamalg_qi.c b/drivers/crypto/caam/caamalg_qi.c index ab263b1..66531d6 100644 --- a/drivers/crypto/caam/caamalg_qi.c +++ b/drivers/crypto/caam/caamalg_qi.c @@ -1201,7 +1201,9 @@ static void skcipher_done(struct caam_drv_req *drv_req, u32 status) * ciphertext block (CBC mode) or last counter (CTR mode). * This is used e.g. by the CTS mode. */ - memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes, ivsize); + if (!ecode) + memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes, + ivsize); qi_cache_free(edesc); skcipher_request_complete(req, ecode); diff --git a/drivers/crypto/caam/caamalg_qi2.c b/drivers/crypto/caam/caamalg_qi2.c index 2681581..bc370af 100644 --- a/drivers/crypto/caam/caamalg_qi2.c +++ b/drivers/crypto/caam/caamalg_qi2.c @@ -1358,7 +1358,9 @@ static void skcipher_encrypt_done(void *cbk_ctx, u32 status) * ciphertext block (CBC mode) or last counter (CTR mode). * This is used e.g. by the CTS mode. */ - memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes, ivsize); + if (!ecode) + memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes, + ivsize); qi_cache_free(edesc); skcipher_request_complete(req, ecode); @@ -1394,7 +1396,9 @@ static void skcipher_decrypt_done(void *cbk_ctx, u32 status) * ciphertext block (CBC mode) or last counter (CTR mode). * This is used e.g. by the CTS mode. */ - memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes, ivsize); + if (!ecode) + memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes, + ivsize); qi_cache_free(edesc); skcipher_request_complete(req, ecode); -- 2.1.0