From patchwork Mon Jul 23 17:54:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10540703 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 36BA89093 for ; Mon, 23 Jul 2018 17:57:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 21C2A26256 for ; Mon, 23 Jul 2018 17:57:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 16062262AE; Mon, 23 Jul 2018 17:57:26 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9D33626256 for ; Mon, 23 Jul 2018 17:57:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388134AbeGWS7q (ORCPT ); Mon, 23 Jul 2018 14:59:46 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:45672 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388084AbeGWS7q (ORCPT ); Mon, 23 Jul 2018 14:59:46 -0400 Received: by mail-pg1-f194.google.com with SMTP id f1-v6so903503pgq.12 for ; Mon, 23 Jul 2018 10:57:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1TbrJwt0D5EoUHUvMr5YnMDYOm+xOPL4r6wc3ZRCfqk=; b=s/8gzMYrVEGT6LHyWUyKH8/w8GNVyE8TzHUvN8SIaLed/xtm/jnNhFdex0K50q9rZw yq58Bjs0fnPF09G/lqlWVF021gPcpqJa3CLa3eidB8NuoSJoVGncJeBdtXCIYZUKadX1 AtJqgCA0ULleR5fXd4LNU211+ht1V69ecgG0ThUxE17PSKsOL/UKMuM6dZbZ0Hx5VByU SspCzXR+pyxdpxBGg1DgZbu0Sn1HR3Vd70fbtcxLHCfY17s2oJ+NwWJgUvTivmqrnyXe hbp7qCmoHGUp4H672Lpm0rWdtemUw4VWkTPPPzdiRRjx4rOAESssQl1PG5wdi5kzmFPh oEYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1TbrJwt0D5EoUHUvMr5YnMDYOm+xOPL4r6wc3ZRCfqk=; b=fKrQvOh+Yl2RlbojX6WPee0jU1XebhxbXpDpBtWBVkyjUvq6zVD3hd04Qxeb3cyGns 2nsPnPV+Ggg7GGFS+nDBxWjHMaronNIEAeyBcR8L4e1aiEsH3xEU3U9XIGNjFBM6KyJ4 2PWq93NmOWeEoG0M6ivPEXNNJZWR0qXKh+riNfMp6Cxe0gIEJD6zSK+IUQ7YhX2OBAc1 fgL/2WL6cfKXWljhRauPKszYWNE72mFi3BO/LLICFpeXrJU1FuobhV5FhFpUHtlzTvEr Y7Kz8xjzsHOR/3+rYLvQNGwpb+E0lWvxIxdVHN3/B3/W+b8FSyEcrx+OKoycM5GYzn0L zu8w== X-Gm-Message-State: AOUpUlH7juBjECj2Sop+x9Cfaijib/BwRrxmeYzaS4rT6PaTTylRUg/R HSLNmofNzYjplZL8xvsRUGuAev0p X-Google-Smtp-Source: AAOMgpfD1AlOyG1uNTjMZH5S+QbnMbRZYfPUemQU18QDNExsXqEmcWazAzcygDr6YQ8+8qE65lRsDQ== X-Received: by 2002:a63:5922:: with SMTP id n34-v6mr13197892pgb.113.1532368643881; Mon, 23 Jul 2018 10:57:23 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([2620:15c:17:3:dc28:5c82:b905:e8a8]) by smtp.gmail.com with ESMTPSA id p64-v6sm15575578pfa.47.2018.07.23.10.57.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jul 2018 10:57:23 -0700 (PDT) From: Eric Biggers To: linux-crypto@vger.kernel.org, Herbert Xu Cc: Liu Chao , Luo Xinqiang , Gao Kui , Eric Biggers Subject: [PATCH 1/3] crypto: skcipher - fix crash flushing dcache in error path Date: Mon, 23 Jul 2018 10:54:56 -0700 Message-Id: <20180723175458.108900-2-ebiggers3@gmail.com> X-Mailer: git-send-email 2.18.0.233.g985f88cf7e-goog In-Reply-To: <20180723175458.108900-1-ebiggers3@gmail.com> References: <20180723175458.108900-1-ebiggers3@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers scatterwalk_done() is only meant to be called after a nonzero number of bytes have been processed, since scatterwalk_pagedone() will flush the dcache of the *previous* page. But in the error case of skcipher_walk_done(), e.g. if the input wasn't an integer number of blocks, scatterwalk_done() was actually called after advancing 0 bytes. This caused a crash ("BUG: unable to handle kernel paging request") during '!PageSlab(page)' on architectures like arm and arm64 that define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE, provided that the input was page-aligned as in that case walk->offset == 0. Fix it by reorganizing skcipher_walk_done() to skip the scatterwalk_advance() and scatterwalk_done() if an error has occurred. This bug was found by syzkaller fuzzing. Reproducer, assuming ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE: #include #include #include int main() { struct sockaddr_alg addr = { .salg_type = "skcipher", .salg_name = "cbc(aes-generic)", }; char buffer[4096] __attribute__((aligned(4096))) = { 0 }; int fd; fd = socket(AF_ALG, SOCK_SEQPACKET, 0); bind(fd, (void *)&addr, sizeof(addr)); setsockopt(fd, SOL_ALG, ALG_SET_KEY, buffer, 16); fd = accept(fd, NULL, NULL); write(fd, buffer, 15); read(fd, buffer, 15); } Reported-by: Liu Chao Fixes: b286d8b1a690 ("crypto: skcipher - Add skcipher walk interface") Cc: # v4.10+ Signed-off-by: Eric Biggers --- crypto/skcipher.c | 53 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/crypto/skcipher.c b/crypto/skcipher.c index 7d6a49fe3047..5f7017b36d75 100644 --- a/crypto/skcipher.c +++ b/crypto/skcipher.c @@ -95,7 +95,7 @@ static inline u8 *skcipher_get_spot(u8 *start, unsigned int len) return max(start, end_page); } -static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize) +static void skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize) { u8 *addr; @@ -103,23 +103,24 @@ static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize) addr = skcipher_get_spot(addr, bsize); scatterwalk_copychunks(addr, &walk->out, bsize, (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1); - return 0; } int skcipher_walk_done(struct skcipher_walk *walk, int err) { - unsigned int n = walk->nbytes - err; - unsigned int nbytes; - - nbytes = walk->total - n; - - if (unlikely(err < 0)) { - nbytes = 0; - n = 0; - } else if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS | - SKCIPHER_WALK_SLOW | - SKCIPHER_WALK_COPY | - SKCIPHER_WALK_DIFF)))) { + unsigned int n; /* bytes processed */ + bool more; + + if (unlikely(err < 0)) + goto finish; + + n = walk->nbytes - err; + walk->total -= n; + more = (walk->total != 0); + + if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS | + SKCIPHER_WALK_SLOW | + SKCIPHER_WALK_COPY | + SKCIPHER_WALK_DIFF)))) { unmap_src: skcipher_unmap_src(walk); } else if (walk->flags & SKCIPHER_WALK_DIFF) { @@ -131,28 +132,28 @@ int skcipher_walk_done(struct skcipher_walk *walk, int err) skcipher_unmap_dst(walk); } else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) { if (WARN_ON(err)) { + /* unexpected case; didn't process all bytes */ err = -EINVAL; - nbytes = 0; - } else - n = skcipher_done_slow(walk, n); + goto finish; + } + skcipher_done_slow(walk, n); + goto already_advanced; } - if (err > 0) - err = 0; - - walk->total = nbytes; - walk->nbytes = nbytes; - scatterwalk_advance(&walk->in, n); scatterwalk_advance(&walk->out, n); - scatterwalk_done(&walk->in, 0, nbytes); - scatterwalk_done(&walk->out, 1, nbytes); +already_advanced: + scatterwalk_done(&walk->in, 0, more); + scatterwalk_done(&walk->out, 1, more); - if (nbytes) { + if (more) { crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ? CRYPTO_TFM_REQ_MAY_SLEEP : 0); return skcipher_walk_next(walk); } + err = 0; +finish: + walk->nbytes = 0; /* Short-circuit for the common/fast path. */ if (!((unsigned long)walk->buffer | (unsigned long)walk->page)) From patchwork Mon Jul 23 17:54:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10540705 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4F1F29093 for ; Mon, 23 Jul 2018 17:57:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3B1BA26256 for ; Mon, 23 Jul 2018 17:57:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2F607262AE; Mon, 23 Jul 2018 17:57:27 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B13E826256 for ; Mon, 23 Jul 2018 17:57:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388198AbeGWS7r (ORCPT ); Mon, 23 Jul 2018 14:59:47 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:45681 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388084AbeGWS7r (ORCPT ); Mon, 23 Jul 2018 14:59:47 -0400 Received: by mail-pg1-f196.google.com with SMTP id f1-v6so903555pgq.12 for ; Mon, 23 Jul 2018 10:57:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CuTVeoT5MOS6RbaMjfFH3DaMg28Lvhhfil8nSNqMSGI=; b=kxcZNVFOi/flCn9+esrhKM2iTsMUjqvj3m0QwF7h7uAMUXPkos9t015Rb5u9Jcpq/D yZnA5O5TQEdK6HLfXHcBKajEK7YAHI7BkUTAlKv2rFO/oMEyjXbf9xdvNscrL2bu/Agk y5dvQ94BiTKbR00ajGqxhyZlOFLdJuHXEigiP/y3Z2Q29oBiNc0bk1hBekYPBKhTrPLH PmwbJE7PADUM9gwXddvpAL5kHeRhEKsvXRka7YZJugoJ8E6UPJjW7GbI2y86FNy14QyJ 0QCbecOnqRDDlZgkmIwrhx02LtnAbleEIZO+yBbw/gPbUp4zoa+Ybu9lkKnyhObeVWqL 6cRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CuTVeoT5MOS6RbaMjfFH3DaMg28Lvhhfil8nSNqMSGI=; b=PSNhWjCE4r5txm4QT3ewxBcmbhXM6UeKxXtbEXBbjLkrOM3GnJI+mWri5HHRBJrdKe NVeAzRJaoKw5OxN3GpuJj7/pPzBJz/O2NJ7q4tAMPmdL0sZoC8dA0+AiMD30yDsCwOsD Lpttu7io/GGbFEZNlIOw72bU94NS9K/ENiVDPjFF6enr934eNaNO94Xf58JIJ3yoZn2n g3ZNm6qbPI1Dm3nirIat96JZP/2iCF2Kg9VKFCzno+u6tYWCbbMO4/JZhSSa6y5zuCZc 1njDFeY8z1xesOXZ1BsmyI6oiKeSXhYqH0i87VQeM4mjCBXmMkW+Ix8ayhqmcfHcVgO1 YBew== X-Gm-Message-State: AOUpUlHMI9qWSW7lN9D3e/leiUKcR3VoDtFDSwH1f6YfmnDf+xAwtwSq FcmkNarIS5thiVqedsk1TzecMxY1 X-Google-Smtp-Source: AAOMgpc5+U9oiKy6qcxKkDl3Q4WkwqoN4zzS/YqsmTuqqgHpspxbqrgBifrPhtj8MhhSbZuQ4lF+vg== X-Received: by 2002:a63:460d:: with SMTP id t13-v6mr12972244pga.201.1532368644878; Mon, 23 Jul 2018 10:57:24 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([2620:15c:17:3:dc28:5c82:b905:e8a8]) by smtp.gmail.com with ESMTPSA id p64-v6sm15575578pfa.47.2018.07.23.10.57.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jul 2018 10:57:24 -0700 (PDT) From: Eric Biggers To: linux-crypto@vger.kernel.org, Herbert Xu Cc: Liu Chao , Luo Xinqiang , Gao Kui , Eric Biggers Subject: [PATCH 2/3] crypto: blkcipher - fix crash flushing dcache in error path Date: Mon, 23 Jul 2018 10:54:57 -0700 Message-Id: <20180723175458.108900-3-ebiggers3@gmail.com> X-Mailer: git-send-email 2.18.0.233.g985f88cf7e-goog In-Reply-To: <20180723175458.108900-1-ebiggers3@gmail.com> References: <20180723175458.108900-1-ebiggers3@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers Like the skcipher_walk case: scatterwalk_done() is only meant to be called after a nonzero number of bytes have been processed, since scatterwalk_pagedone() will flush the dcache of the *previous* page. But in the error case of blkcipher_walk_done(), e.g. if the input wasn't an integer number of blocks, scatterwalk_done() was actually called after advancing 0 bytes. This caused a crash ("BUG: unable to handle kernel paging request") during '!PageSlab(page)' on architectures like arm and arm64 that define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE, provided that the input was page-aligned as in that case walk->offset == 0. Fix it by reorganizing blkcipher_walk_done() to skip the scatterwalk_advance() and scatterwalk_done() if an error has occurred. This bug was found by syzkaller fuzzing. Reproducer, assuming ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE: #include #include #include int main() { struct sockaddr_alg addr = { .salg_type = "skcipher", .salg_name = "ecb(aes-generic)", }; char buffer[4096] __attribute__((aligned(4096))) = { 0 }; int fd; fd = socket(AF_ALG, SOCK_SEQPACKET, 0); bind(fd, (void *)&addr, sizeof(addr)); setsockopt(fd, SOL_ALG, ALG_SET_KEY, buffer, 16); fd = accept(fd, NULL, NULL); write(fd, buffer, 15); read(fd, buffer, 15); } Reported-by: Liu Chao Fixes: 5cde0af2a982 ("[CRYPTO] cipher: Added block cipher type") Cc: # v2.6.19+ Signed-off-by: Eric Biggers --- crypto/blkcipher.c | 54 ++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/crypto/blkcipher.c b/crypto/blkcipher.c index dd4dcab3766a..f93abf13b5d4 100644 --- a/crypto/blkcipher.c +++ b/crypto/blkcipher.c @@ -70,19 +70,18 @@ static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len) return max(start, end_page); } -static inline unsigned int blkcipher_done_slow(struct blkcipher_walk *walk, - unsigned int bsize) +static inline void blkcipher_done_slow(struct blkcipher_walk *walk, + unsigned int bsize) { u8 *addr; addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1); addr = blkcipher_get_spot(addr, bsize); scatterwalk_copychunks(addr, &walk->out, bsize, 1); - return bsize; } -static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk, - unsigned int n) +static inline void blkcipher_done_fast(struct blkcipher_walk *walk, + unsigned int n) { if (walk->flags & BLKCIPHER_WALK_COPY) { blkcipher_map_dst(walk); @@ -96,49 +95,48 @@ static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk, scatterwalk_advance(&walk->in, n); scatterwalk_advance(&walk->out, n); - - return n; } int blkcipher_walk_done(struct blkcipher_desc *desc, struct blkcipher_walk *walk, int err) { - unsigned int nbytes = 0; + unsigned int n; /* bytes processed */ + bool more; - if (likely(err >= 0)) { - unsigned int n = walk->nbytes - err; + if (unlikely(err < 0)) + goto finish; - if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW))) - n = blkcipher_done_fast(walk, n); - else if (WARN_ON(err)) { - err = -EINVAL; - goto err; - } else - n = blkcipher_done_slow(walk, n); + n = walk->nbytes - err; + walk->total -= n; + more = (walk->total != 0); - nbytes = walk->total - n; - err = 0; + if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW))) { + blkcipher_done_fast(walk, n); + } else { + if (WARN_ON(err)) { + /* unexpected case; didn't process all bytes */ + err = -EINVAL; + goto finish; + } + blkcipher_done_slow(walk, n); } - scatterwalk_done(&walk->in, 0, nbytes); - scatterwalk_done(&walk->out, 1, nbytes); + scatterwalk_done(&walk->in, 0, more); + scatterwalk_done(&walk->out, 1, more); -err: - walk->total = nbytes; - walk->nbytes = nbytes; - - if (nbytes) { + if (more) { crypto_yield(desc->flags); return blkcipher_walk_next(desc, walk); } - + err = 0; +finish: + walk->nbytes = 0; if (walk->iv != desc->info) memcpy(desc->info, walk->iv, walk->ivsize); if (walk->buffer != walk->page) kfree(walk->buffer); if (walk->page) free_page((unsigned long)walk->page); - return err; } EXPORT_SYMBOL_GPL(blkcipher_walk_done); From patchwork Mon Jul 23 17:54:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10540707 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 09AB717FD for ; Mon, 23 Jul 2018 17:57:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E8B2526256 for ; Mon, 23 Jul 2018 17:57:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DD341262AE; Mon, 23 Jul 2018 17:57:27 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7268B26256 for ; Mon, 23 Jul 2018 17:57:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388203AbeGWS7s (ORCPT ); Mon, 23 Jul 2018 14:59:48 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:33780 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388175AbeGWS7s (ORCPT ); Mon, 23 Jul 2018 14:59:48 -0400 Received: by mail-pg1-f196.google.com with SMTP id r5-v6so921893pgv.0 for ; Mon, 23 Jul 2018 10:57:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4y+RDajoew1iudBSxdpV53ojdIQDNG+LQrDWIf7ZvJ0=; b=uJpIM7fgnrKMsGBSz7AIdjtrkdEmF8Lg7vBV9WkOodSX3uV5d7+FXE5eLAc0Wi11Mo ezXGpzGJIuSX3Jr0yP0/SYn2vG3foleWdroq5Wg/I6gza9LMudTGUoAOFU61g3fKNkwC LbTHodyAHmjsz/Y3DhaTY1QgctQs/6EtEapb5z/dzRm0Nu0xAOmatfHCeSZ7eRwX6pVH nmzzTn3R7v2DzS1vJ12BvVBJv5RZq2BH9hx2YaR3qmCCTpGEnWa0Uac002H+We83SPkG WJKCVa1k/yMpmwl7P8awnTJPHqAr+eGEGtCo1bVGhU7aVCS/jG/PcSNbezjWII0dU6RZ RDiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4y+RDajoew1iudBSxdpV53ojdIQDNG+LQrDWIf7ZvJ0=; b=jogK0IGdk5AxDiqc9rxQDZf5xvoRmh7CmBwliOv1Mr56zTriw3qx7/mErQH1xRgVfz dAYZcgJrXfFoKfVTKohi4seI3fubLUR2zyNszjBJSHnpr31RSUGyqitaPZdgnugzhoUk qNGBLBa2VSZ6CC2XlLYFHXiXhjmzTVB0GBlYtoCrJbaNMwcEhV62wdib9eVBDvchw0I/ tJTl95/d/9h+hK851YkJyYfzQDJ957f3xcBkXCw3vKiyiQr03unEKmnkrxoNQ/83jEVw l7QkaTs/KHg1NtNfcnqlmJU8VzPrbDkHGKO0tIfgJy4VS9BLlk8SPg4ctWslY8XAqQ4H oESQ== X-Gm-Message-State: AOUpUlG4w896hetEFob4RUAcr21XxMTN3SDir3vscoUg6RxmJUcZK9do HibsSDpoPcK4c0Ku1Yk+ScKySpfj X-Google-Smtp-Source: AAOMgpex9PpflzGo9RlB2xUR2tQynAhvdh5yyv1hJrhLXWrrmZz77hgI4z2UhrmN6ajbwuV/0ZRtig== X-Received: by 2002:a63:e116:: with SMTP id z22-v6mr7969118pgh.89.1532368645764; Mon, 23 Jul 2018 10:57:25 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([2620:15c:17:3:dc28:5c82:b905:e8a8]) by smtp.gmail.com with ESMTPSA id p64-v6sm15575578pfa.47.2018.07.23.10.57.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jul 2018 10:57:25 -0700 (PDT) From: Eric Biggers To: linux-crypto@vger.kernel.org, Herbert Xu Cc: Liu Chao , Luo Xinqiang , Gao Kui , Eric Biggers Subject: [PATCH 3/3] crypto: ablkcipher - fix crash flushing dcache in error path Date: Mon, 23 Jul 2018 10:54:58 -0700 Message-Id: <20180723175458.108900-4-ebiggers3@gmail.com> X-Mailer: git-send-email 2.18.0.233.g985f88cf7e-goog In-Reply-To: <20180723175458.108900-1-ebiggers3@gmail.com> References: <20180723175458.108900-1-ebiggers3@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers Like the skcipher_walk and blkcipher_walk cases: scatterwalk_done() is only meant to be called after a nonzero number of bytes have been processed, since scatterwalk_pagedone() will flush the dcache of the *previous* page. But in the error case of ablkcipher_walk_done(), e.g. if the input wasn't an integer number of blocks, scatterwalk_done() was actually called after advancing 0 bytes. This caused a crash ("BUG: unable to handle kernel paging request") during '!PageSlab(page)' on architectures like arm and arm64 that define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE, provided that the input was page-aligned as in that case walk->offset == 0. Fix it by reorganizing ablkcipher_walk_done() to skip the scatterwalk_advance() and scatterwalk_done() if an error has occurred. Reported-by: Liu Chao Fixes: bf06099db18a ("crypto: skcipher - Add ablkcipher_walk interfaces") Cc: # v2.6.35+ Signed-off-by: Eric Biggers --- crypto/ablkcipher.c | 57 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/crypto/ablkcipher.c b/crypto/ablkcipher.c index 1edb5000d783..8882e90e868e 100644 --- a/crypto/ablkcipher.c +++ b/crypto/ablkcipher.c @@ -71,11 +71,9 @@ static inline u8 *ablkcipher_get_spot(u8 *start, unsigned int len) return max(start, end_page); } -static inline unsigned int ablkcipher_done_slow(struct ablkcipher_walk *walk, - unsigned int bsize) +static inline void ablkcipher_done_slow(struct ablkcipher_walk *walk, + unsigned int n) { - unsigned int n = bsize; - for (;;) { unsigned int len_this_page = scatterwalk_pagelen(&walk->out); @@ -87,17 +85,13 @@ static inline unsigned int ablkcipher_done_slow(struct ablkcipher_walk *walk, n -= len_this_page; scatterwalk_start(&walk->out, sg_next(walk->out.sg)); } - - return bsize; } -static inline unsigned int ablkcipher_done_fast(struct ablkcipher_walk *walk, - unsigned int n) +static inline void ablkcipher_done_fast(struct ablkcipher_walk *walk, + unsigned int n) { scatterwalk_advance(&walk->in, n); scatterwalk_advance(&walk->out, n); - - return n; } static int ablkcipher_walk_next(struct ablkcipher_request *req, @@ -107,39 +101,40 @@ int ablkcipher_walk_done(struct ablkcipher_request *req, struct ablkcipher_walk *walk, int err) { struct crypto_tfm *tfm = req->base.tfm; - unsigned int nbytes = 0; + unsigned int n; /* bytes processed */ + bool more; - if (likely(err >= 0)) { - unsigned int n = walk->nbytes - err; + if (unlikely(err < 0)) + goto finish; - if (likely(!(walk->flags & ABLKCIPHER_WALK_SLOW))) - n = ablkcipher_done_fast(walk, n); - else if (WARN_ON(err)) { - err = -EINVAL; - goto err; - } else - n = ablkcipher_done_slow(walk, n); + n = walk->nbytes - err; + walk->total -= n; + more = (walk->total != 0); - nbytes = walk->total - n; - err = 0; + if (likely(!(walk->flags & ABLKCIPHER_WALK_SLOW))) { + ablkcipher_done_fast(walk, n); + } else { + if (WARN_ON(err)) { + /* unexpected case; didn't process all bytes */ + err = -EINVAL; + goto finish; + } + ablkcipher_done_slow(walk, n); } - scatterwalk_done(&walk->in, 0, nbytes); - scatterwalk_done(&walk->out, 1, nbytes); - -err: - walk->total = nbytes; - walk->nbytes = nbytes; + scatterwalk_done(&walk->in, 0, more); + scatterwalk_done(&walk->out, 1, more); - if (nbytes) { + if (more) { crypto_yield(req->base.flags); return ablkcipher_walk_next(req, walk); } - + err = 0; +finish: + walk->nbytes = 0; if (walk->iv != req->info) memcpy(req->info, walk->iv, tfm->crt_ablkcipher.ivsize); kfree(walk->iv_buffer); - return err; } EXPORT_SYMBOL_GPL(ablkcipher_walk_done);