Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp605474yba; Thu, 18 Apr 2019 06:43:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqzq8tCNRWEdP97PSnVqATjHf2NiMr3g/22K0nvNvcfEQEaLG0yYBF21I/sX1NL8WEnXQyej X-Received: by 2002:a63:121f:: with SMTP id h31mr4983631pgl.287.1555595020984; Thu, 18 Apr 2019 06:43:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555595020; cv=none; d=google.com; s=arc-20160816; b=lXz2tdW0R5AYrxXvozp7xIOAWjCJnINBZNvDm0uElZIcKoTtZg7fUrV5O+zzkyPB/+ XwbyjlBBRY65jMYZl8i/0U247DfGoI4IbEHbRkMXqzx/DNNDCgA8lNg6wcXxZnOxNMWE HNrNpllhHXd/O0OJ2zThj4wjRspzwFhPH7vjaMBWZHwOq+mfDgCrkqw/9Jgjknj4dZgn 9MgjGy1Y6iHpnY6Pxcd4APHhI7O/j1Fge0rVcqOG7n6s/MZ/MnuwQ3JsHLzHbrvD3+Ik xULbhlJCmuycjEh30JoKZLfIkNJ+gDY/x5vqLGSZU9sfjS3JwL+TvXYp8bZ53nSOcD+g QjMQ== 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=h76lRDZeFi4w6e3WlK2VAXA0MTLFJFuxTTMN/Mcelhg=; b=l6DwbixjDXmR2ISTr0y+qL3xYwl42UXFfcPFMnVCehynNSxIVJHuM4jc52e6fQv3bF C8WQvBPcq1VRYOA+xvTFh7ykzzSieNk4uG6Pegxt4ikY8XZ+YbAdE63jyud5sHQYtuH5 Lj6E16Bg/6ryaj6jxjKG4c0RcpT7Iobzz9f6ZTmx9x1Yh4sHJgbh/f+OsdV72E6O1raY hglplFV5P4tiAkJuse3Ewm/r3cPQung6XWZHs1JRpGmEinVwAXTJ5JcQ99icNdPjd6s8 nsrb6RYNZ6ra+lzI2+GnXiQ/MSLHUiIxplaH1l1rnEQ5uEHpoF3Qq705Ii1XrkjAi63o pigQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a13si2030170pgi.60.2019.04.18.06.43.25; Thu, 18 Apr 2019 06:43:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389448AbfDRNlo (ORCPT + 99 others); Thu, 18 Apr 2019 09:41:44 -0400 Received: from foss.arm.com ([217.140.101.70]:33710 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389216AbfDRNln (ORCPT ); Thu, 18 Apr 2019 09:41:43 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4B58680D; Thu, 18 Apr 2019 06:41:43 -0700 (PDT) Received: from e110176-lin.kfn.arm.com (e110176-lin.kfn.arm.com [10.50.4.178]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CF1ED3F5AF; Thu, 18 Apr 2019 06:41:41 -0700 (PDT) From: Gilad Ben-Yossef To: Herbert Xu , "David S. Miller" Cc: Ofir Drang , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 25/35] crypto: ccree: make AEAD sgl iterator well behaved Date: Thu, 18 Apr 2019 16:39:00 +0300 Message-Id: <20190418133913.9122-26-gilad@benyossef.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190418133913.9122-1-gilad@benyossef.com> References: <20190418133913.9122-1-gilad@benyossef.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix some scatter list interation code was not handling scatter lists being shorter than expected in a graceful manner. Signed-off-by: Gilad Ben-Yossef --- drivers/crypto/ccree/cc_buffer_mgr.c | 55 +++++++++------------------- 1 file changed, 17 insertions(+), 38 deletions(-) diff --git a/drivers/crypto/ccree/cc_buffer_mgr.c b/drivers/crypto/ccree/cc_buffer_mgr.c index 86e498bee0bb..fa625bdde3f9 100644 --- a/drivers/crypto/ccree/cc_buffer_mgr.c +++ b/drivers/crypto/ccree/cc_buffer_mgr.c @@ -659,11 +659,9 @@ static int cc_aead_chain_assoc(struct cc_drvdata *drvdata, { struct aead_req_ctx *areq_ctx = aead_request_ctx(req); int rc = 0; - u32 mapped_nents = 0; - struct scatterlist *current_sg = req->src; + int mapped_nents = 0; struct crypto_aead *tfm = crypto_aead_reqtfm(req); - unsigned int sg_index = 0; - u32 size_of_assoc = areq_ctx->assoclen; + unsigned int size_of_assoc = areq_ctx->assoclen; struct device *dev = drvdata_to_dev(drvdata); if (areq_ctx->is_gcm4543) @@ -684,26 +682,10 @@ static int cc_aead_chain_assoc(struct cc_drvdata *drvdata, goto chain_assoc_exit; } - //iterate over the sgl to see how many entries are for associated data - //it is assumed that if we reach here , the sgl is already mapped - sg_index = current_sg->length; - //the first entry in the scatter list contains all the associated data - if (sg_index > size_of_assoc) { - mapped_nents++; - } else { - while (sg_index <= size_of_assoc) { - current_sg = sg_next(current_sg); - /* if have reached the end of the sgl, then this is - * unexpected - */ - if (!current_sg) { - dev_err(dev, "reached end of sg list. unexpected\n"); - return -EINVAL; - } - sg_index += current_sg->length; - mapped_nents++; - } - } + mapped_nents = sg_nents_for_len(req->src, size_of_assoc); + if (mapped_nents < 0) + return mapped_nents; + if (mapped_nents > LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES) { dev_err(dev, "Too many fragments. current %d max %d\n", mapped_nents, LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES); @@ -898,6 +880,7 @@ static int cc_aead_chain_data(struct cc_drvdata *drvdata, u32 sg_index = 0; bool is_gcm4543 = areq_ctx->is_gcm4543; u32 size_to_skip = areq_ctx->assoclen; + struct scatterlist *sgl; if (is_gcm4543) size_to_skip += crypto_aead_ivsize(tfm); @@ -920,15 +903,13 @@ static int cc_aead_chain_data(struct cc_drvdata *drvdata, sg_index = areq_ctx->src_sgl->length; //check where the data starts while (sg_index <= size_to_skip) { + src_mapped_nents--; offset -= areq_ctx->src_sgl->length; - areq_ctx->src_sgl = sg_next(areq_ctx->src_sgl); - //if have reached the end of the sgl, then this is unexpected - if (!areq_ctx->src_sgl) { - dev_err(dev, "reached end of sg list. unexpected\n"); - return -EINVAL; - } + sgl = sg_next(areq_ctx->src_sgl); + if (!sgl) + break; + areq_ctx->src_sgl = sgl; sg_index += areq_ctx->src_sgl->length; - src_mapped_nents--; } if (src_mapped_nents > LLI_MAX_NUM_OF_DATA_ENTRIES) { dev_err(dev, "Too many fragments. current %d max %d\n", @@ -962,15 +943,13 @@ static int cc_aead_chain_data(struct cc_drvdata *drvdata, //check where the data starts while (sg_index <= size_to_skip) { + dst_mapped_nents--; offset -= areq_ctx->dst_sgl->length; - areq_ctx->dst_sgl = sg_next(areq_ctx->dst_sgl); - //if have reached the end of the sgl, then this is unexpected - if (!areq_ctx->dst_sgl) { - dev_err(dev, "reached end of sg list. unexpected\n"); - return -EINVAL; - } + sgl = sg_next(areq_ctx->dst_sgl); + if (!sgl) + break; + areq_ctx->dst_sgl = sgl; sg_index += areq_ctx->dst_sgl->length; - dst_mapped_nents--; } if (dst_mapped_nents > LLI_MAX_NUM_OF_DATA_ENTRIES) { dev_err(dev, "Too many fragments. current %d max %d\n", -- 2.21.0