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))