Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4517051ybb; Tue, 7 Apr 2020 09:00:00 -0700 (PDT) X-Google-Smtp-Source: APiQypK/fqrtBY97u2zqYztP/G/sHcfwLR5NwrlMK/KqZsePSknztCfg+M5K0+QAJiLCaVS8KJOx X-Received: by 2002:a05:6830:8d:: with SMTP id a13mr2162311oto.321.1586275200314; Tue, 07 Apr 2020 09:00:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586275200; cv=none; d=google.com; s=arc-20160816; b=gOZE7d9IBBwQOZayhrTXdExmAEUm3VhhLtAuSS7kzudLOc+9Q1f1sWNWGzOS6IQAQb Eeqd0/NzLzC9RcH2xxALMTulBza6s5XAiPRb9ddGLTCaIP3BqdWwn3cd/0eWbYxpLjZg u11y9Rpp8jWIvBFwqaFbNGJ3mRuObitfmskjOR3ooycjFQ+K8GkHrENUL8L0RC9UZUje rG6jOVbRvB5m+u2Gf8GCa5HdDfgpFPNzpshehMrZGenNUMA8M/YiHtyk86mvMFBKsSO/ 6xwaZpbYnTDUupppbfazWGcyeICuKGsCISbHuZNVwm69+/xKfWpl6qVHcs177tki3tIL jCpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=b4hnos7PBNJhM4kdMbP36XBsiUVCHwl7pO4vWSFmySM=; b=zFFLobGpGYUsuR2bA7PGRJFquQQhf1qwQQDf9Sr3u9j1+H0uE2HzQSlQw4g72uty3R x7Dgk7E5y8IFqxu5DkBge42mOyqtlLOLnJPObTwJ/rNyMcYpDJ/AevyhQLsOy43yw6Cb lpQ4C8V5XtUujhRp6lPmlxHht+o27c/R2kBfhUd/CgP+Ax7mw9amCa56RDDSHhIty1Qp YQXX4fHf0J51iyhc90N6+ZGwTxXciLXoiM2EW4YmE6YsQW8U7N5TqVg5LaIFUDS9t8Gt 8zPA2fSx8j9bnpr3C4riRBu/qb/U04ENbldnzrdKs58OgOg6TFNhf810Iu4vIiqCZrA8 /FKA== 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 w203si795787oie.193.2020.04.07.08.59.38; Tue, 07 Apr 2020 09:00:00 -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 S1727924AbgDGP7C (ORCPT + 99 others); Tue, 7 Apr 2020 11:59:02 -0400 Received: from inva021.nxp.com ([92.121.34.21]:36532 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726937AbgDGP7C (ORCPT ); Tue, 7 Apr 2020 11:59:02 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 8ED10201192; Tue, 7 Apr 2020 17:58:59 +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 824E52003AC; Tue, 7 Apr 2020 17:58:59 +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 1C592204B1; Tue, 7 Apr 2020 17:58:59 +0200 (CEST) From: Iuliana Prodan To: Herbert Xu , Horia Geanta , Aymen Sghaier Cc: "David S. Miller" , Silvano Di Ninno , Franck Lenormand , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-imx , Iuliana Prodan Subject: [PATCH] crypto: caam - fix the address of the last entry of S/G Date: Tue, 7 Apr 2020 18:58:45 +0300 Message-Id: <1586275125-20571-1-git-send-email-iuliana.prodan@nxp.com> X-Mailer: git-send-email 2.1.0 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 For skcipher algorithms, the input, output HW S/G tables look like this: [IV, src][dst, IV] Now, we can have 2 conditions here: - there is no IV; - src and dst are equal (in-place encryption) and scattered and the error is an "off-by-one" in the HW S/G table. This issue was seen with KASAN: BUG: KASAN: slab-out-of-bounds in skcipher_edesc_alloc+0x95c/0x1018 Read of size 4 at addr ffff000022a02958 by task cryptomgr_test/321 CPU: 2 PID: 321 Comm: cryptomgr_test Not tainted 5.6.0-rc1-00165-ge4ef8383-dirty #4 Hardware name: LS1046A RDB Board (DT) Call trace: dump_backtrace+0x0/0x260 show_stack+0x14/0x20 dump_stack+0xe8/0x144 print_address_description.isra.11+0x64/0x348 __kasan_report+0x11c/0x230 kasan_report+0xc/0x18 __asan_load4+0x90/0xb0 skcipher_edesc_alloc+0x95c/0x1018 skcipher_encrypt+0x84/0x150 crypto_skcipher_encrypt+0x50/0x68 test_skcipher_vec_cfg+0x4d4/0xc10 test_skcipher_vec+0x178/0x1d8 alg_test_skcipher+0xec/0x230 alg_test.part.44+0x114/0x4a0 alg_test+0x1c/0x60 cryptomgr_test+0x34/0x58 kthread+0x1b8/0x1c0 ret_from_fork+0x10/0x18 Allocated by task 321: save_stack+0x24/0xb0 __kasan_kmalloc.isra.10+0xc4/0xe0 kasan_kmalloc+0xc/0x18 __kmalloc+0x178/0x2b8 skcipher_edesc_alloc+0x21c/0x1018 skcipher_encrypt+0x84/0x150 crypto_skcipher_encrypt+0x50/0x68 test_skcipher_vec_cfg+0x4d4/0xc10 test_skcipher_vec+0x178/0x1d8 alg_test_skcipher+0xec/0x230 alg_test.part.44+0x114/0x4a0 alg_test+0x1c/0x60 cryptomgr_test+0x34/0x58 kthread+0x1b8/0x1c0 ret_from_fork+0x10/0x18 Freed by task 0: (stack is not available) The buggy address belongs to the object at ffff000022a02800 which belongs to the cache dma-kmalloc-512 of size 512 The buggy address is located 344 bytes inside of 512-byte region [ffff000022a02800, ffff000022a02a00) The buggy address belongs to the page: page:fffffe00006a8000 refcount:1 mapcount:0 mapping:ffff00093200c400 index:0x0 compound_mapcount: 0 flags: 0xffff00000010200(slab|head) raw: 0ffff00000010200 dead000000000100 dead000000000122 ffff00093200c400 raw: 0000000000000000 0000000080100010 00000001ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff000022a02800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ffff000022a02880: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 >ffff000022a02900: 00 00 00 00 00 00 00 00 00 00 fc fc fc fc fc fc ^ ffff000022a02980: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ffff000022a02a00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc Fixes: 334d37c9e263 ("crypto: caam - update IV using HW support") Cc: # v5.3+ Signed-off-by: Iuliana Prodan --- drivers/crypto/caam/caamalg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index b7bb7c3..eed41da 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -1711,7 +1711,7 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req, if (ivsize || mapped_dst_nents > 1) sg_to_sec4_set_last(edesc->sec4_sg + dst_sg_idx + - mapped_dst_nents); + mapped_dst_nents - 1 + !!ivsize); if (sec4_sg_bytes) { edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, -- 2.1.0