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