From patchwork Tue Sep 12 09:35:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 112257 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp4927147qgf; Tue, 12 Sep 2017 02:36:50 -0700 (PDT) X-Google-Smtp-Source: ADKCNb64s5bN31IhiCmEoWeNlvScuRsJma0X6IYB91tIyIxrGKfKhXJWXAhq5OeBogdlaxUOrRf+ X-Received: by 10.98.147.219 with SMTP id r88mr14335541pfk.0.1505209010596; Tue, 12 Sep 2017 02:36:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1505209010; cv=none; d=google.com; s=arc-20160816; b=wf0z2huRfX6eo4E99KHp9eteQoklMF3d0xHM9Aqm46m18+GB4185l4RBUan2k8Ttq0 JMO3fPZIMFNDEOZ8TLbff/wIwvp2zZMdcF77Y3ooPBep0gs5hKxf+yirlnhAynX3Cmw2 hpiAoWtNzokbSBqdlIwtSN3lM8KSizyBa3+6NRn8NaXekN1h46PDVF0rzB+DNummRNyw AiX65Sq1pSCbltiLs5sLzPypMqy6Vvi+y82zGTDtyK7adZUewPEai4JirBhjK15ADkP8 TYE7pavelU8JDsAiWmkBApZoWbx4Eb7IoDZWaBaKlQ/ppiDJGJJYiG9bNpZA/MlpfVRS Egdg== 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 :arc-authentication-results; bh=TI9uJh/1elKmSqlY5VYQzCJapTf7PHXZYOfExf2Ns3U=; b=hjCKTsWgGylKwFS/zOmq95koqL5HeZ1Tmov4MayEoyl9mI2YThPa3r3SXI6yFuYF9w nSqIz4jPKc4WTQHfoM5mDdenBJ/vwG3jurGkt3R0KNW6UHoMoWhaO3HrE/MFrlZ2SwvX EY7b0H4QtQYMcsesY2NEN+fjcg6FeLjJy1cne6U+rBORxsaj24pXlE68S9ZG2vpsMSVe /+wzAN8kT1xQFww0k+5VKys6tQjaUFycGIIfa65H8Iaf7rQXgg/7PuXgNWqTfxIduHgJ Q7vDZBd7QHvuVPp5hg3INMq6nu4OBdAMcMrD8HRXMrmQ37xoD9wBX2YtoXONoPhy+qu0 D57g== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m21si7349947pgn.782.2017.09.12.02.36.50; Tue, 12 Sep 2017 02:36:50 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751292AbdILJgt (ORCPT + 1 other); Tue, 12 Sep 2017 05:36:49 -0400 Received: from mout.kundenserver.de ([217.72.192.73]:63893 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751214AbdILJgs (ORCPT ); Tue, 12 Sep 2017 05:36:48 -0400 Received: from wuerfel.lan ([95.208.190.237]) by mrelayeu.kundenserver.de (mreue101 [212.227.15.145]) with ESMTPA (Nemesis) id 0LnjcV-1dEFnX3UkR-00hsps; Tue, 12 Sep 2017 11:36:05 +0200 From: Arnd Bergmann To: Lionel Debieve Cc: Arnd Bergmann , Herbert Xu , "David S. Miller" , Maxime Coquelin , Alexandre Torgue , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] crypto: stm32 - Fix uninitialized data usage Date: Tue, 12 Sep 2017 11:35:38 +0200 Message-Id: <20170912093553.2580986-1-arnd@arndb.de> X-Mailer: git-send-email 2.9.0 X-Provags-ID: V03:K0:pq236YfxbVNwqj355JlDKkTRo6fzQdnPRprypRxSAhAXZDs/HSc PzTueg773DKir5HcHp29L0K5buZ9E7KIheuWixMVb5TalKHzrHzBigWpkd+Pe2XLRFufmPK E3UO/lsUO3Add5d5O1gOBck0nfBKtMJGZda3g73DtCTs37c0phD+Stq4GG+RixKXf/Ibadd fsyqmFLix8XMOzxqoI3Gw== X-UI-Out-Filterresults: notjunk:1; V01:K0:RppDc2c8SGg=:j+4EbY9dofdT2lEgGa8Tj4 lGmgntTfTEmCLv8q1LMnBX83Lcd6pIWgyoB18uv6MQ7A45ej+ywB8tEr2J2gyZUYXBMEbmlYI 1aa9WvOTLoGqDIZZSXfqMYL0z2cG0Yr5FLjyzuEzwgKg91ZbhaCQxup6OwdOGlw4gZHfbV+lP WdLJ1uOFlwR1ePDanNC/IUk9AsFSdIwsYaG+4a1uZ+c7oMcKKld7GFW8cmRzHNDUQV6yIQNYg ZuZ/81KWxbsV3f/f10BDZY8AiPvpknjJxuigmgNa+qrDVNrrrQfKo8cQZA4DajNwTPEhrL5xC EiB78bTx5Bd+myGlflqcS/QiZ/sVDLMeROu733eJeJoE/GpEfSRNnYj/7ByLvWrb/htPu/p4h IHeZr83mNjtnSsS3x9YArAyv3JCgLaErKiWyK4X7DGwMQBMu6ApaGVnArVpfqlIpGvB3IBkyv DybaZokscVq4YJGCPtWtBBm4HretrVNEGsSewbITCkTzuuRzxSOah3sKPqP+BcKvNtl4pcZ6C 1j6GMy9U9EoyrR/UwB0aI6XGujgRxvRsF08yWdzJoVpENcDbndPRY2YBHpja9sJyXwAGwRxaM VtST/JFIDBkGh7z9PM/BZES3reIhpaUqHk1tX+t0o0GolDTOsoOxhLuy4Ny6AClG7LXVXNT8y Ip3sCYJmeai/zFkbMphigwi3qO9QrBMeSoQ6i2/ulyBMXv32YAGY9rH0jn+LhFDO11oske4Rt bLmxr7ulccDr6Kh8wvkj0GuCGvaBxWaC2NCcJA== Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The error handling in stm32_hash_irq_thread passes uninitialized data into stm32_hash_finish_req, as gcc points out: drivers/crypto/stm32/stm32-hash.c: In function 'stm32_hash_irq_thread': drivers/crypto/stm32/stm32-hash.c:1088:2: error: 'err' may be used uninitialized in this function [-Werror=maybe-uninitialized] I could not tell what data should be passed there instead, so this changes the code to always pass zero, making it well-defined, though possibly still wrong. Please check. Signed-off-by: Arnd Bergmann --- drivers/crypto/stm32/stm32-hash.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) -- 2.9.0 diff --git a/drivers/crypto/stm32/stm32-hash.c b/drivers/crypto/stm32/stm32-hash.c index b585ce54a802..3c23a23e9ee5 100644 --- a/drivers/crypto/stm32/stm32-hash.c +++ b/drivers/crypto/stm32/stm32-hash.c @@ -1067,7 +1067,6 @@ static int stm32_hash_cra_sha256_init(struct crypto_tfm *tfm) static irqreturn_t stm32_hash_irq_thread(int irq, void *dev_id) { struct stm32_hash_dev *hdev = dev_id; - int err; if (HASH_FLAGS_CPU & hdev->flags) { if (HASH_FLAGS_OUTPUT_READY & hdev->flags) { @@ -1085,7 +1084,7 @@ static irqreturn_t stm32_hash_irq_thread(int irq, void *dev_id) finish: /*Finish current request */ - stm32_hash_finish_req(hdev->req, err); + stm32_hash_finish_req(hdev->req, 0); return IRQ_HANDLED; } From patchwork Tue Sep 12 09:35:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 112258 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp4927361qgf; Tue, 12 Sep 2017 02:37:10 -0700 (PDT) X-Google-Smtp-Source: ADKCNb69Ozann3wxTf9/5PK0Ad33TRKeSCIgaDyYyqWLEZPzsqSYPJ69ZOUDt7dHkGp9wsk66wd7 X-Received: by 10.101.73.66 with SMTP id q2mr14377290pgs.146.1505209030743; Tue, 12 Sep 2017 02:37:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1505209030; cv=none; d=google.com; s=arc-20160816; b=0DVj+CODrz3ZE4T29Q8QUPIY4yumFA24DkHRzVIed4oO+QXm6f+Elni6Qnf1KjO+26 64wXz1esjr9GQAXNu9LHiybb5TAEVyH/n6XbechnsRKiRk5xj0kpL5fCSFwFsBCLT/va N3DgzBt6Vwc5HhuOMWc9GiYEoD1VeGU0pw6xKA4zwCeR9n+v0ywXkwsUs5DnhlGMGfys mULjwnWbYTKZlyYv5Vp52h8lfIKZeItfhI8rfTsFGYDUT0kZpWL8hkXJL/fsTazzjPPa 7yXf+bcZtEcXrI8Sm6R5o+2ar1VunRgUgrxnK16p73gksTlJu9ZcDZl6iHNfsx80MPFo G0Ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=CrwhWknO8/haKwX1KK2a1Nulw7MaTgfT4xgapYWON5s=; b=zAdj2Dusoi7IkFgsuGrs2clYoQnDHk55zoj975IAWX5hCbWVHA0HS1tfY+Rybe4Xgh xFeVxQmUpxZIZie131QEazwbQqQmXzzpWCVXytzsxO5pAcP7mjc7dkj8RXRHAKfyvnhU awFT0kMRpvjkvvDzK5jnEKgTYryKaMkOhigBXIA8GiXPABAuMeAU3OArxjJrJ/TG0D/e DFvF3PoQQUCJMK3w6AQa6hihfjHzzfy2hTR6zVzbB6XBxll/mED5Mr9BRdyU74kapYgt kVRGH7Htur5pTsn7eZpSX6NbOwvJVqkyJSNND5vc30qcmm1CtqnKlPs25SUxJWIBHMzD hQHw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o24si7371728pgd.251.2017.09.12.02.37.10; Tue, 12 Sep 2017 02:37:10 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751214AbdILJhI (ORCPT + 1 other); Tue, 12 Sep 2017 05:37:08 -0400 Received: from mout.kundenserver.de ([217.72.192.75]:61513 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751373AbdILJhH (ORCPT ); Tue, 12 Sep 2017 05:37:07 -0400 Received: from wuerfel.lan ([95.208.190.237]) by mrelayeu.kundenserver.de (mreue101 [212.227.15.145]) with ESMTPA (Nemesis) id 0MdLhj-1e8XoG2x9v-00IVK8; Tue, 12 Sep 2017 11:36:19 +0200 From: Arnd Bergmann To: Lionel Debieve Cc: Arnd Bergmann , Herbert Xu , "David S. Miller" , Maxime Coquelin , Alexandre Torgue , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] crypto: stm32 - Try to fix hash padding Date: Tue, 12 Sep 2017 11:35:39 +0200 Message-Id: <20170912093553.2580986-2-arnd@arndb.de> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20170912093553.2580986-1-arnd@arndb.de> References: <20170912093553.2580986-1-arnd@arndb.de> X-Provags-ID: V03:K0:AJkvj2x9HxjC7RGwXlK5bQ5Tjcu3QZRmWOuiPiIX5kPdYxi312W PHa2lxYpPGv9fSh9gF506a3Kxt7/YssSDBj4oxB3kC9VwQ+FDVEoAGlIZeB2T1tNQ3B9vxG MYS6JKvRIQGW297HNs5yeWLUueqVYxC6c6PaHyk+wXTZfa+jwN4iiFVNqkGtHSYI3DBDoT/ W8MFTQ5HdYZy9G1ybHWvQ== X-UI-Out-Filterresults: notjunk:1; V01:K0:9Au2K2g0QHQ=:tm11RMAdi8rsEIylyuv2Ct ze+IbsZUj856PEv0HeArX9PPH44gFKTqlyhThBIeiIBKTXM0XOsMu00tAxc9I2lmI8rKpL+fv Efr0pWz7R+v+oowXj8yaD5hua50FRatXpiEd2/xv7AZdi0YeYje+iOcJ4gR34jeXPc8xlb5Mb ivOTxuYDRlgquRE61f1US8BdKjdnTXf+Lx03/ubkRjNGp+35bYHjB8vxvFP0v3dMnzucqg3Ze /Ej2vMnfYiZVMNUWAikI17M1TO4plRIooO0Q9r1AN36B4el+l7VxaeGMtA8JQQurpd2rRf5HC MhSNQVig5Um/XcQrDr73dqh+feBAOxchiUbOF255DmVLZTrkDJdauZs+WEtlD5NZJb9ErESQ8 rNrinBdu4Knjr4cPHkMNPhDGRJPIJTlAo9eSr7LlJ89GmMUlwH71ufliOhS1yTfQ/2mtNbCn6 OLpyQXTDg9VEzwsZCDq/52s6pr4VY533Gloi7UQ02DWXsIl7QlWkRaw/PBJtTgzAqLkoR5bJk Ic0JgitJE5ZTuNfl6r9oV5eUMuIbYN6zPk0eCoV2L80yrkd1ng6n+7Eb6h5ETgD1AobZHM3Wm 2/1woIAQ4MNstRdy7+xXLzlaW3UOhXHFL4HB+vLncjj44V6hfmlRcu97ZTG6jS1iBTSI1H4T0 Ca9hwe8tVBDCkQWMuG+ul+XiEa2Ei44EJ317keMI/XLZL3MtDvAQMMng33KQeEir+wihvICHg ULN1gDtgWjqCrMkKL60fBs7fPd58r7G5W0c/6w== Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org gcc warns that the length for the extra unaligned data in the hash function may be used unaligned. In theory this could happen if we pass a zero-length sg_list, or if sg_is_last() was never true: In file included from drivers/crypto/stm32/stm32-hash.c:23: drivers/crypto/stm32/stm32-hash.c: In function 'stm32_hash_one_request': include/uapi/linux/kernel.h:12:49: error: 'ncp' may be used uninitialized in this function [-Werror=maybe-uninitialized] #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) Neither of these can happen in practice, so the warning is harmless. However while trying to suppress the warning, I noticed multiple problems with that code: - On big-endian kernels, we byte-swap the data like we do for register accesses, however this is a data stream and almost certainly needs to use a single writesl() instead of series of writel() to give the correct hash. - If the length is not a multiple of four bytes, we skip the last word entirely, since we write the truncated length using stm32_hash_set_nblw(). - If we change the code to round the length up rather than down, the last bytes contain stale data, so it needs some form of padding. This tries to address all four problems, by correctly initializing the length to zero, using endian-safe copy functions, adding zero-padding and passing the padded length. I have done no testing on this patch, so please review carefully and if possible test with an unaligned length and big-endian kernel builds. Fixes: 8a1012d3f2ab ("crypto: stm32 - Support for STM32 HASH module") Signed-off-by: Arnd Bergmann --- drivers/crypto/stm32/stm32-hash.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) -- 2.9.0 diff --git a/drivers/crypto/stm32/stm32-hash.c b/drivers/crypto/stm32/stm32-hash.c index 3c23a23e9ee5..b2ed2bccdb66 100644 --- a/drivers/crypto/stm32/stm32-hash.c +++ b/drivers/crypto/stm32/stm32-hash.c @@ -553,9 +553,9 @@ static int stm32_hash_dma_send(struct stm32_hash_dev *hdev) { struct stm32_hash_request_ctx *rctx = ahash_request_ctx(hdev->req); struct scatterlist sg[1], *tsg; - int err = 0, len = 0, reg, ncp; + int err = 0, len = 0, reg, ncp = 0; unsigned int i; - const u32 *buffer = (const u32 *)rctx->buffer; + u32 *buffer = (void *)rctx->buffer; rctx->sg = hdev->req->src; rctx->total = hdev->req->nbytes; @@ -620,10 +620,13 @@ static int stm32_hash_dma_send(struct stm32_hash_dev *hdev) reg |= HASH_CR_DMAA; stm32_hash_write(hdev, HASH_CR, reg); - for (i = 0; i < DIV_ROUND_UP(ncp, sizeof(u32)); i++) - stm32_hash_write(hdev, HASH_DIN, buffer[i]); - - stm32_hash_set_nblw(hdev, ncp); + if (ncp) { + memset(buffer + ncp, 0, + DIV_ROUND_UP(ncp, sizeof(u32)) - ncp); + writesl(hdev->io_base + HASH_DIN, buffer, + DIV_ROUND_UP(ncp, sizeof(u32))); + } + stm32_hash_set_nblw(hdev, DIV_ROUND_UP(ncp, sizeof(u32))); reg = stm32_hash_read(hdev, HASH_STR); reg |= HASH_STR_DCAL; stm32_hash_write(hdev, HASH_STR, reg);