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