From patchwork Tue Jul 17 04:21:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10528077 X-Patchwork-Delegate: snitzer@redhat.com 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 4C8BB601D2 for ; Tue, 17 Jul 2018 04:22:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 424572522B for ; Tue, 17 Jul 2018 04:22:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 368B12621D; Tue, 17 Jul 2018 04:22:35 +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=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CF56A2223E for ; Tue, 17 Jul 2018 04:22:34 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 040908110F; Tue, 17 Jul 2018 04:22:34 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C8B803001A49; Tue, 17 Jul 2018 04:22:33 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 90A6718037F3; Tue, 17 Jul 2018 04:22:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6H4M5tt000727 for ; Tue, 17 Jul 2018 00:22:05 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5C9925D70C; Tue, 17 Jul 2018 04:22:05 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx16.extmail.prod.ext.phx2.redhat.com [10.5.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 514B85D6B5 for ; Tue, 17 Jul 2018 04:22:05 +0000 (UTC) Received: from mail-pf0-f195.google.com (mail-pf0-f195.google.com [209.85.192.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 538883082A26 for ; Tue, 17 Jul 2018 04:22:04 +0000 (UTC) Received: by mail-pf0-f195.google.com with SMTP id k21-v6so2480696pff.11 for ; Mon, 16 Jul 2018 21:22:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CrCDbQt0h3SDwmF/v9IF6YKRxfzI15nnHqkP77JIozM=; b=VIu6JHIPql6GKegNQ67lMUlfJMf+R4say1sJ6uTXDBx3SmKoGMPTiThbqVRCDjjDNC Pz4WoRC5pCTIJMC4PujEsP1RjSDW0KtfzBsQSOWVhEMQpLRklIvlnjrihsEmnBflz8cd /y2dDzos8A/rpgoM2IzBkaDpMm/dCZ41qNrPY= 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=CrCDbQt0h3SDwmF/v9IF6YKRxfzI15nnHqkP77JIozM=; b=sQrD+5cqITQL7eBUjPlfp7/QuOZ5KpTRpx3bcvzgpRIu1tDHvtmKPXe+bUgRw3Qawr nEcbnxlv2CsMRcoJzbshzHUBhRz0t+a3FERMcmpkLnYc0YH2EMzWPp7+II/yMCcMY9A3 G/ZyTd/KpMAo4KWsHvj7xQzqGGlyEbYPCmbvKPWPgVv+5cuW+h/fqk4JKBJwnzdcl+p1 iT8xI/GaZLzm/9l+W8ANoqHMf1OLanzrgmSp9VZZrApkEy9ztkJsaqsfxFCuypb/g/RU mGA5Q/4elrts3I5+sNEDreH9/NimE+N59pNYUOh1KslIBDLiWynjo7Y6OobCy7b1HQsC o2iA== X-Gm-Message-State: AOUpUlE/5dV4nO6f3e8DsY/vE3Bx82ovQ+Bte4y4kbVMONJ31lrOON+m uEbMbtbBwLqdweiM1Oj+7EKU2A== X-Google-Smtp-Source: AAOMgpfccYhXcA4RF9140EVHuPnm4MbMEzyCiRAD+qMqvKDqWssor+a4TgIqt0D2eGplvsV29PJCjg== X-Received: by 2002:a62:6b44:: with SMTP id g65-v6mr17570pfc.226.1531801323844; Mon, 16 Jul 2018 21:22:03 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id v6-v6sm96483872pfa.28.2018.07.16.21.21.56 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Jul 2018 21:21:58 -0700 (PDT) From: Kees Cook To: Herbert Xu Date: Mon, 16 Jul 2018 21:21:49 -0700 Message-Id: <20180717042150.37761-11-keescook@chromium.org> In-Reply-To: <20180717042150.37761-1-keescook@chromium.org> References: <20180717042150.37761-1-keescook@chromium.org> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Tue, 17 Jul 2018 04:22:04 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Tue, 17 Jul 2018 04:22:04 +0000 (UTC) for IP:'209.85.192.195' DOMAIN:'mail-pf0-f195.google.com' HELO:'mail-pf0-f195.google.com' FROM:'keescook@chromium.org' RCPT:'' X-RedHat-Spam-Score: -0.112 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS) 209.85.192.195 mail-pf0-f195.google.com 209.85.192.195 mail-pf0-f195.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.45 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: dm-devel@redhat.com Cc: Giovanni Cabiddu , Kees Cook , Arnd Bergmann , "Gustavo A. R. Silva" , Mike Snitzer , Eric Biggers , qat-linux@intel.com, linux-kernel@vger.kernel.org, dm-devel@redhat.com, linux-crypto@vger.kernel.org, Lars Persson , Tim Chen , Alasdair Kergon , Rabin Vincent Subject: [dm-devel] [PATCH v5 10/11] crypto: ahash: Remove VLA usage for AHASH_REQUEST_ON_STACK X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 17 Jul 2018 04:22:34 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP In the quest to remove all stack VLA usage from the kernel[1], this caps the ahash request size similar to the other limits and adds a sanity check at initialization. AHASH_REQUEST_ON_STACK is special, though: it is only ever used for shash-wrapped ahash, so its size is bounded only by non-async hashes. A manual inspection of this shows the largest to be: sizeof(struct shash_desc) + SHASH_MAX_DESCSIZE [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Kees Cook --- crypto/shash.c | 9 ++++++++- include/crypto/hash.h | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crypto/shash.c b/crypto/shash.c index 8d4746b14dd5..e344560458cb 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -355,6 +355,7 @@ int crypto_init_shash_ops_async(struct crypto_tfm *tfm) struct crypto_ahash *crt = __crypto_ahash_cast(tfm); struct crypto_shash **ctx = crypto_tfm_ctx(tfm); struct crypto_shash *shash; + size_t reqsize; if (!crypto_mod_get(calg)) return -EAGAIN; @@ -365,6 +366,12 @@ int crypto_init_shash_ops_async(struct crypto_tfm *tfm) return PTR_ERR(shash); } + reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); + if (WARN_ON(reqsize > AHASH_MAX_REQSIZE)) { + crypto_mod_put(calg); + return -EINVAL; + } + *ctx = shash; tfm->exit = crypto_exit_shash_ops_async; @@ -383,7 +390,7 @@ int crypto_init_shash_ops_async(struct crypto_tfm *tfm) if (alg->import) crt->import = shash_async_import; - crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); + crt->reqsize = reqsize; return 0; } diff --git a/include/crypto/hash.h b/include/crypto/hash.h index 4fcd0e2368cd..2181fb38eab9 100644 --- a/include/crypto/hash.h +++ b/include/crypto/hash.h @@ -67,9 +67,17 @@ struct ahash_request { #define AHASH_MAX_DIGESTSIZE 512 #define AHASH_MAX_STATESIZE 512 +/* + * HASH_REQUEST_ON_STACK must only be used when wrapping shash (i.e. + * allocated with a CRYPTO_ALG_ASYNC mask), and is generally discouraged + * (just use shash directly). This is really only ever needed when using + * scatter/gather input sources. + */ +#define AHASH_MAX_REQSIZE (sizeof(struct shash_desc) + \ + SHASH_MAX_DESCSIZE) #define AHASH_REQUEST_ON_STACK(name, ahash) \ char __##name##_desc[sizeof(struct ahash_request) + \ - crypto_ahash_reqsize(ahash)] CRYPTO_MINALIGN_ATTR; \ + AHASH_MAX_REQSIZE] CRYPTO_MINALIGN_ATTR; \ struct ahash_request *name = (void *)__##name##_desc /**