From patchwork Wed Oct 18 13:32:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Romain Izard X-Patchwork-Id: 10014709 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0DDD960215 for ; Wed, 18 Oct 2017 13:33:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F25C128B4B for ; Wed, 18 Oct 2017 13:33:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E61ED28B53; Wed, 18 Oct 2017 13:33:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4CE8328B4B for ; Wed, 18 Oct 2017 13:33:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From: MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=aPIuBxYQxMSzNy21eySsmbbs7P7cSYSvn7pnNkn1Z4M=; b=tYQr/ElDL3Rr/k 2qbslWmD21HTZeOVwFGfZtrOhXFoheDJ9LJPu4pcChh8MvzS4tzZFYKOlzaQguur/hicO348YCP36 cAsXPD32HI48fFW00+lft7Isj6rsjcDgyoh7F2Bp1Lf/zlIRuzXTAK29626L57IrfP+P/N0q6/Ii9 14PV008eCPoObA3r7XAddZLbuAM0KVrSHUawxM5hwFTiWPdWl/s3lY7ATH9FF7coUiG5YqGkg17Uk QBMrR9Y8rXunoGY4ToNy9eKmOTCstby7Mq0C5hLz82yqu8ifkKhEtAtCCQ5GAh7JzmFiETl8teSUo oFKi8YVuH2CrN3w6dc/w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e4oTg-00028O-MC; Wed, 18 Oct 2017 13:33:44 +0000 Received: from mail-qk0-x243.google.com ([2607:f8b0:400d:c09::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e4oTb-0001Vb-D2 for linux-arm-kernel@lists.infradead.org; Wed, 18 Oct 2017 13:33:42 +0000 Received: by mail-qk0-x243.google.com with SMTP id d67so6214543qkg.5 for ; Wed, 18 Oct 2017 06:33:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mobile-devices.fr; s=google; h=mime-version:sender:from:date:message-id:subject:to:cc; bh=86IZteak2Bwd3InybgoWf6aNxdCz0g7OYLVx71FTopg=; b=Jb31kBWXnK8R//byA6cgJFbcNTxZOd/Koyq+PaN/Yv3X3VHdM1cfwN4PrHq9MMlPEx 7dDOLuQLeyTQF0adzpmoD+x1xdUj/TzaR52heGELt2TCLxcHkdBmangEuCya0xTE5mQu yJM5zrVnBxjlpQMA8wXZooH7q+p2lPS1ORQ2E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:from:date:message-id:subject:to:cc; bh=86IZteak2Bwd3InybgoWf6aNxdCz0g7OYLVx71FTopg=; b=U4ug8g+CCQHth4Xxb3P7+CXA8kzkeJX56MgclAofSb+AXqG+4qOkCUUpwp/QoyiA5Q AXK8HqQckqsLHS6bVH0DRYL68XfAivG38nhJqA6g0P5nf+HG09ijJgkvtQh211RlGwmP lTbhDmzl/uKsfWvvPGQ8H7T9zvSmhWz/jVqKBGycdwLq03aGBtfE1ufja9A54m+S6yhz r78uT104Sqm/TARSFyhnSWle13Uqw5lWK00pVZrlndp19nuDEPAqzJ6VcPwccMMg+OHo vvOFXYtz0KPLgROpq4vOes7Qp0+lxOgdozHgVgA89ayntnorkzc8gRL2iV33Ml68cQCQ PpYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:from:date:message-id:subject :to:cc; bh=86IZteak2Bwd3InybgoWf6aNxdCz0g7OYLVx71FTopg=; b=iWjc8NxE5gI+1MopddHCJnya7tR9/sHGqf/ob6nu7Cf+eiFe2N2lsL+jOojsC+B1tP ATXrMHSB+Gpok+Kphs6sXIbKz9kTFxnu+bWjvf/MJsySlTAOf4Z8kgjn3HDU5JDnTx42 kP3ML15e53Tk+Ff6FFlNbMbhp6lGB8cK7r4Fh4VjaJbqN1CW1ulKSambQ6NNiyaE8+aR Rxk5N9QysZ6deuHqkjybmNQenjTpcI29pmPCSkCkjlzmA6/ep9d/Lt8uto4hP5aK8vW5 VPwuEnPtrnGp4Ud14ezepgat27zbndC5wG8fLBYMaE15CidTmT1PDeMQz2lwBk2KHZpW kEcw== X-Gm-Message-State: AMCzsaU59FRLqLOwz7apiicaQnyXsGXwYLQh4EEWix/zVegSwZrjIywx bOocJ7+jgOe6vNsIFEfOfW1hXol/VWSwzF5e67qRAQ== X-Google-Smtp-Source: ABhQp+Q20PNOEpAuyU+OZbLgR1urLa0cIBgilTTxJUk2Ouhm4dCxwzmMJT4wYaLsxuXTVu01Ty9Bw0W/NbqKBsxUzdY= X-Received: by 10.55.43.227 with SMTP id r96mr2554932qkr.216.1508333597668; Wed, 18 Oct 2017 06:33:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.200.23.61 with HTTP; Wed, 18 Oct 2017 06:32:57 -0700 (PDT) From: Romain Izard Date: Wed, 18 Oct 2017 15:32:57 +0200 X-Google-Sender-Auth: hIY_jsLmiIsV8Faekw16gEoDsa8 Message-ID: Subject: Kernel panic when using ccm(aes) with the Atmel AES HW accelerator To: linux-crypto@vger.kernel.org, linux-arm-kernel X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171018_063339_776963_9DBD1A8B X-CRM114-Status: GOOD ( 25.91 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Cyrille Pitchen , Herbert Xu , "David S. Miller" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Hello, For some time I have been trying to fix an issue with the Atmel AES hardware accelerator available on SAMA5D2 chips. The ciphertext stealing mode did not work, and this led to problems when using the cts(cbc(aes)) crypto engine for fscrypt with Linux 4.13. (see also I have updated the driver in atmel-aes.c to fix this issue, taking care of the corner case where the source and destination are the same in decryption mode by saving the last 16 bytes of ciphertext in memory, and restoring them into the 'info' member of the encryption request. This made it possible to pass the cts(cbc(aes)) test in tcrypt.ko, which was failing before. Here are my modifications: 8<---------------------------------------------------------------------- sg_set_buf(pctx->src, tag, 16); @@ -301,10 +292,10 @@ static int crypto_ccm_encrypt(struct aead_request *req) struct scatterlist *dst; unsigned int cryptlen = req->cryptlen; u8 *odata = pctx->odata; - u8 *iv = req->iv; + u8 *iv = pctx->iv; int err; - err = crypto_ccm_init_crypt(req, odata); + err = crypto_ccm_init_crypt(req, odata, iv); if (err) return err; @@ -363,12 +354,12 @@ static int crypto_ccm_decrypt(struct aead_request *req) unsigned int cryptlen = req->cryptlen; u8 *authtag = pctx->auth_tag; u8 *odata = pctx->odata; - u8 *iv = req->iv; + u8 *iv = pctx->iv; int err; cryptlen -= authsize; - err = crypto_ccm_init_crypt(req, authtag); + err = crypto_ccm_init_crypt(req, authtag, iv); if (err) return err; 8<---------------------------------------------------------------------- My problem is that I do not understand why it works. It ensures that in both encryption and decryption cases, the IV buffer is available and 16 bytes wide. But normally the IV buffer provided by the crypto request is already 16 bytes wide, as the algorithm is registered with ivsize=16. As I am not very familiar with the crypto subsystem, I fear that I missed something. I would gladly appreciate the feedback of more experienced developers regarding this issue. Best regards, diff --git a/drivers/crypto/atmel-aes.c b/drivers/crypto/atmel-aes.c index 29e20c37f3a6..f3eabe1f1490 100644 --- a/drivers/crypto/atmel-aes.c +++ b/drivers/crypto/atmel-aes.c @@ -80,6 +80,7 @@ #define AES_FLAGS_BUSY BIT(3) #define AES_FLAGS_DUMP_REG BIT(4) #define AES_FLAGS_OWN_SHA BIT(5) +#define AES_FLAGS_CIPHERTAIL BIT(6) #define AES_FLAGS_PERSISTENT (AES_FLAGS_INIT | AES_FLAGS_BUSY) @@ -156,6 +157,7 @@ struct atmel_aes_authenc_ctx { struct atmel_aes_reqctx { unsigned long mode; + u32 ciphertail[AES_BLOCK_SIZE / sizeof(u32)]; }; #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC @@ -496,6 +498,11 @@ static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err); static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err) { + struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); + struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req); + struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req); + int ivsize = crypto_ablkcipher_ivsize(ablkcipher); + #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC atmel_aes_authenc_complete(dd, err); #endif @@ -503,6 +510,17 @@ static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err) clk_disable(dd->iclk); dd->flags &= ~AES_FLAGS_BUSY; + if (rctx->mode & AES_FLAGS_CIPHERTAIL) { + if (rctx->mode & AES_FLAGS_ENCRYPT) { + scatterwalk_map_and_copy(req->info, req->dst, + req->nbytes - ivsize, + ivsize, 0); + } else { + memcpy(req->info, rctx->ciphertail, ivsize); + memset(rctx->ciphertail, 0, ivsize); + } + } + if (dd->is_async) dd->areq->complete(dd->areq, err); @@ -1071,11 +1089,13 @@ static int atmel_aes_ctr_start(struct atmel_aes_dev *dd) static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode) { + struct crypto_ablkcipher *ablkcipher; struct atmel_aes_base_ctx *ctx; struct atmel_aes_reqctx *rctx; struct atmel_aes_dev *dd; - ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req)); + ablkcipher = crypto_ablkcipher_reqtfm(req); + ctx = crypto_ablkcipher_ctx(ablkcipher); switch (mode & AES_FLAGS_OPMODE_MASK) { case AES_FLAGS_CFB8: ctx->block_size = CFB8_BLOCK_SIZE; @@ -1103,7 +1123,15 @@ static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode) return -ENODEV; rctx = ablkcipher_request_ctx(req); - rctx->mode = mode; + + if (!(mode & AES_FLAGS_ENCRYPT)) { + int ivsize = crypto_ablkcipher_ivsize(ablkcipher); + + scatterwalk_map_and_copy(rctx->ciphertail, req->src, + (req->nbytes - ivsize), ivsize, 0); + } + rctx->mode = mode | AES_FLAGS_CIPHERTAIL; + return atmel_aes_handle_queue(dd, &req->base); } 8<---------------------------------------------------------------------- But as I wanted to test my code more thoroughly, I also ran the kcapi test suite on my test platform. Some tests cases were now passing, some others did not pass both before and after my fix, but my fix also led to a systematic oops when running the ccm(aes) test case. Here is an example of the kernel logs observed: # kcapi -x 2 -c 'ccm(aes)' -i 0100000003020100a0a1a2a3a4a50000 -k c0c1c2c3c4c5c6 c7c8c9cacbcccdcecf -q 588c979a61c663d2f066d0c2c0f989806d5f6b61dac38417e8d12cfdf9 26e0 -t 0001020304050607 [ 92.340000] atmel_aes f002c000.aes: saving ciphertext tail (16b) [ 92.350000] atmel_aes f002c000.aes: copy ciphertext tail to IV EBADMSG[ 92.360000] Unable to handle kernel NULL pointer dereference at virtual address 00000020 [ 92.370000] pgd = deebc000 [ 92.370000] [00000020] *pgd=3ec40831, *pte=00000000, *ppte=00000000 [ 92.370000] Internal error: Oops: 17 [#1] ARM [ 92.370000] Modules linked in: [ 92.370000] CPU: 0 PID: 839 Comm: kcapi Not tainted 4.13.4+ #38 [ 92.370000] Hardware name: Atmel SAMA5 [ 92.370000] task: dec0f580 task.stack: df730000 [ 92.370000] PC is at aead_sock_destruct+0x28/0x7c [ 92.370000] LR is at __sk_destruct+0x30/0x168 [ 92.370000] pc : [] lr : [] psr: 600b0013 [ 92.370000] sp : df731e78 ip : df731e98 fp : df731e94 [ 92.370000] r10: 00000008 r9 : df241220 r8 : 00000000 [ 92.370000] r7 : df427710 r6 : df221908 r5 : df6a9800 r4 : dee6e400 [ 92.370000] r3 : 00000000 r2 : 00000000 r1 : 00000026 r0 : dee6e400 [ 92.370000] Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none [ 92.370000] Control: 10c53c7d Table: 3eebc059 DAC: 00000051 [ 92.370000] Process kcapi (pid: 839, stack limit = 0xdf730208) [ 92.370000] Stack: (0xdf731e78 to 0xdf732000) [ 92.370000] 1e60: dee6e5c0 dee6e400 [ 92.370000] 1e80: df221908 df427710 df731eac df731e98 c063df5c c0388e50 dee6e400 00000000 [ 92.370000] 1ea0: df731ebc df731eb0 c063fd5c c063df38 df731ed4 df731ec0 c063fdcc c063fd40 [ 92.370000] 1ec0: df241200 00000000 df731ee4 df731ed8 c063fe78 c063fd7c df731ef4 df731ee8 [ 92.370000] 1ee0: c0385a5c c063fe48 df731f0c df731ef8 c063a1fc c0385a18 dee7fb40 df241220 [ 92.370000] 1f00: df731f1c df731f10 c063a294 c063a1d8 df731f5c df731f20 c01f10d8 c063a284 [ 92.370000] 1f20: 00000000 00000000 dee7fb40 dee7fb48 df731f54 dec0f8bc dec0f580 c0d8a22c [ 92.370000] 1f40: 00000000 c0108ea4 df730000 00000000 df731f6c df731f60 c01f1284 c01f1050 [ 92.370000] 1f60: df731f8c df731f70 c0134cec c01f1278 df730000 c0108ea4 df731fb0 00000006 [ 92.370000] 1f80: df731fac df731f90 c010c378 c0134c78 0004a070 0004a130 00000000 00000006 [ 92.370000] 1fa0: 00000000 df731fb0 c0108d34 c010c2f0 00000000 00000000 00000001 00000000 [ 92.370000] 1fc0: 0004a070 0004a130 00000000 00000006 00000027 ffffffb6 00000002 0004a130 [ 92.370000] 1fe0: 00000000 bef01844 b6f43a1c b6ec5040 400b0010 00000006 00000000 00000000 [ 92.370000] [] (aead_sock_destruct) from [] (__sk_destruct+0x30/0x168) [ 92.370000] [] (__sk_destruct) from [] (sk_destruct+0x28/0x3c) [ 92.370000] [] (sk_destruct) from [] (__sk_free+0x5c/0xcc) [ 92.370000] [] (__sk_free) from [] (sk_free+0x3c/0x40) [ 92.370000] [] (sk_free) from [] (af_alg_release+0x50/0x58) [ 92.370000] [] (af_alg_release) from [] (sock_release+0x30/0xac) [ 92.370000] [] (sock_release) from [] (sock_close+0x1c/0x24) [ 92.370000] [] (sock_close) from [] (__fput+0x94/0x1d8) [ 92.370000] [] (__fput) from [] (____fput+0x18/0x1c) [ 92.370000] [] (____fput) from [] (task_work_run+0x80/0xa4) [ 92.370000] [] (task_work_run) from [] (do_work_pending+0x94/0xbc) [ 92.370000] [] (do_work_pending) from [] (slow_work_pending+0xc/0x20) [ 92.370000] Code: e5902064 e1a04000 e59532d0 e3520000 (e5933020) [ 92.660000] ---[ end trace cce00d600b8ad985 ]--- Segmentation fault As the changes in the hardware driver were related to the IV buffer, the symptoms were coherent with an out-of-bounds memory access when updating the IV. I applied the following patch, and this was enough to avoid the panic: 8<---------------------------------------------------------------------- diff --git a/crypto/ccm.c b/crypto/ccm.c index 1ce37ae0ce56..e7c2121a3ab2 100644 --- a/crypto/ccm.c +++ b/crypto/ccm.c @@ -47,6 +47,7 @@ struct crypto_ccm_req_priv_ctx { u8 odata[16]; u8 idata[16]; u8 auth_tag[16]; + u8 iv[16]; u32 flags; struct scatterlist src[3]; struct scatterlist dst[3]; @@ -248,32 +249,22 @@ static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err) aead_request_complete(req, err); } -static inline int crypto_ccm_check_iv(const u8 *iv) -{ - /* 2 <= L <= 8, so 1 <= L' <= 7. */ - if (1 > iv[0] || iv[0] > 7) - return -EINVAL; - - return 0; -} - -static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag) +static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag, u8* iv) { struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req); struct scatterlist *sg; - u8 *iv = req->iv; - int err; + u8 L = req->iv[0] + 1; - err = crypto_ccm_check_iv(iv); - if (err) - return err; - - pctx->flags = aead_request_flags(req); + if (2 > L || L > 8) + return -EINVAL; /* Note: rfc 3610 and NIST 800-38C require counter of * zero to encrypt auth tag. */ - memset(iv + 15 - iv[0], 0, iv[0] + 1); + memcpy(iv, req->iv, 16 - L); + memset(iv + 16 - L, 0, L); + + pctx->flags = aead_request_flags(req); sg_init_table(pctx->src, 3);