Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp81938imm; Tue, 7 Aug 2018 14:21:25 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxzjAYjS6BqgVmezW5dDcC9coDcRACugUAylqGKksjmARJBgw1K+slN46G30vIWYkOp7Vwk X-Received: by 2002:a17:902:a9:: with SMTP id a38-v6mr89087pla.102.1533676885241; Tue, 07 Aug 2018 14:21:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533676885; cv=none; d=google.com; s=arc-20160816; b=f7tZzLDFvicxCozcc4kR/Es5jeZcHHCRiGVRakWqsRoRMB75yx2uDR/P2Eez6ZUACe pyRkYwfNjRcQhM9rnSZVUsc9ir5yg+qxbwQvv/t92BscFXSrBnvw8Hsu+f1OZwA2gOn8 aD0ubVFBrxCxiyAKHzET/zF4UgTmfXxi+bfeEH2BFsfv+6ObpyW5b+vlpum5MXjPWwBM yb54NfEce22TQ+GPN54/MqOufUe042PV1m2WVbFpveSM+zwEWipkutgU9n4QEEsWYZhw AVvQlWckV2KU9eXQNmoudRs+VrtwZ3fekGl904ROWhOIev2hvFQ+04qltW0ggaiY2/k6 4vWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=f6HZhbULuwp1ONQvXFzzLM5M+BCFj7xnBsodxU5HF/8=; b=b6jdsqldHXhGiimajP6LXQspbb2fYTdzdSUXOPdEL7EBQCP6l4H5LLjCPVT/t2D+QR 1qrkoTaFPJ3EMtGi/n9zPIoPBbfrmJ8vltaZ5T4FvWQ8Gw/ZlvpFckOJaudtXYT380YH Hur2SLbgLoivUdtTnwAYz7SBNd62D4RQOPh2AtgYdMTGcnH2uJ2Ng7NHGsWbITrcV7YN xfaLz1HEHc6ECAt47PmKOXMQ8kBJqdlfMC4utXFtuWhbqEmGydCONDeEuBBRwMM/xgNn PjWIOi/VsRE59YTcHLhRYssZ3/x4FkD1QcDVEf8+M9WBkSbeQAh/kErXiY9xmAfRPzyD I5cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AQHeD2Em; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x16-v6si1706010pln.165.2018.08.07.14.21.10; Tue, 07 Aug 2018 14:21:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AQHeD2Em; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727475AbeHGXfv (ORCPT + 99 others); Tue, 7 Aug 2018 19:35:51 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:37416 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726986AbeHGXfI (ORCPT ); Tue, 7 Aug 2018 19:35:08 -0400 Received: by mail-pf1-f193.google.com with SMTP id a26-v6so42956pfo.4 for ; Tue, 07 Aug 2018 14:18:49 -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=f6HZhbULuwp1ONQvXFzzLM5M+BCFj7xnBsodxU5HF/8=; b=AQHeD2EmOI4vzY7tE412tf3d4wZqk04hm56bZ/zFfnsovVcaoBMwGdDWkXviKDWyxJ ucqcMKo8jU+qU611qS/iWtCST1ndKdgUlDJ8W5SNkkumFu5u+Fdm0H5+jkUtByWcOIFw 3wmThKCMx52Qi3kXdqdwF55mm1JDmlVlvitqk= 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=f6HZhbULuwp1ONQvXFzzLM5M+BCFj7xnBsodxU5HF/8=; b=ZivJjGw1ARBW7pWmfaEQb0DMiI9j0rW4LBpHBwGv9QSKon2DvIlg+S0wKnxMMsr68N DP2ITOqQpP49HqpK1vGdNb9PUIA0NNJoc3lwymXsEa4+UKil9FGWkxIVy4Jvg2qOnGZo 4L26dEpUAnbqlJq/BlIfxfZ0aOYmHP87y6IKzXtuz8RiTuBsKkejo71ycdNSIcBb3sXQ nOSf3bf0iXWwemC9neZn++lBWauenk1jnpWHq16TLsu5sJeK926rALXRyRkCi+TBjXzA n+XSH+k5EMt55sq+bgC72ITijmQqRwtD02LeF49iAyxJMkupJNdXw8Q6T6rsgLZe5v6c v6Yg== X-Gm-Message-State: AOUpUlE29a5zauT71t31Q5xv9j9W/cErIjCUPz8bFfc3mguFKXlbK0Hy ZXmZKVpZ1u+WDC4mg7CEyhFyMA== X-Received: by 2002:a63:c50c:: with SMTP id f12-v6mr87199pgd.88.1533676729111; Tue, 07 Aug 2018 14:18:49 -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 v4-v6sm2382904pgr.36.2018.08.07.14.18.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 07 Aug 2018 14:18:46 -0700 (PDT) From: Kees Cook To: Herbert Xu Cc: Kees Cook , Eric Biggers , Ard Biesheuvel , Giovanni Cabiddu , Alasdair Kergon , Mike Snitzer , Tudor-Dan Ambarus , Andrew Morton , Thomas Gleixner , Geert Uytterhoeven , Arnd Bergmann , Will Deacon , Rasmus Villemoes , David Woodhouse , Matthew Wilcox , "David S. Miller" , "Gustavo A. R. Silva" , linux-crypto@vger.kernel.org, dm-devel@redhat.com, qat-linux@intel.com, linux-kernel@vger.kernel.org Subject: [PATCH v8 4/9] crypto: hash: Remove VLA usage Date: Tue, 7 Aug 2018 14:18:38 -0700 Message-Id: <20180807211843.47586-5-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180807211843.47586-1-keescook@chromium.org> References: <20180807211843.47586-1-keescook@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the quest to remove all stack VLA usage from the kernel[1], this removes the VLAs in SHASH_DESC_ON_STACK (via crypto_shash_descsize()) by using the maximum allowable size (which is now more clearly captured in a macro), along with a few other cases. Similar limits are turned into macros as well. A review of existing sizes shows that SHA512_DIGEST_SIZE (64) is the largest digest size and that sizeof(struct sha3_state) (360) is the largest descriptor size. The corresponding maximums are reduced. [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Kees Cook --- crypto/ahash.c | 4 ++-- crypto/algif_hash.c | 2 +- crypto/shash.c | 6 +++--- include/crypto/hash.h | 6 +++++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/crypto/ahash.c b/crypto/ahash.c index a64c143165b1..78aaf2158c43 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -550,8 +550,8 @@ static int ahash_prepare_alg(struct ahash_alg *alg) { struct crypto_alg *base = &alg->halg.base; - if (alg->halg.digestsize > PAGE_SIZE / 8 || - alg->halg.statesize > PAGE_SIZE / 8 || + if (alg->halg.digestsize > HASH_MAX_DIGESTSIZE || + alg->halg.statesize > HASH_MAX_STATESIZE || alg->halg.statesize == 0) return -EINVAL; diff --git a/crypto/algif_hash.c b/crypto/algif_hash.c index bfcf595fd8f9..d0cde541beb6 100644 --- a/crypto/algif_hash.c +++ b/crypto/algif_hash.c @@ -239,7 +239,7 @@ static int hash_accept(struct socket *sock, struct socket *newsock, int flags, struct alg_sock *ask = alg_sk(sk); struct hash_ctx *ctx = ask->private; struct ahash_request *req = &ctx->req; - char state[crypto_ahash_statesize(crypto_ahash_reqtfm(req)) ? : 1]; + char state[HASH_MAX_STATESIZE]; struct sock *sk2; struct alg_sock *ask2; struct hash_ctx *ctx2; diff --git a/crypto/shash.c b/crypto/shash.c index 5d732c6bb4b2..86d76b5c626c 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -458,9 +458,9 @@ static int shash_prepare_alg(struct shash_alg *alg) { struct crypto_alg *base = &alg->base; - if (alg->digestsize > PAGE_SIZE / 8 || - alg->descsize > PAGE_SIZE / 8 || - alg->statesize > PAGE_SIZE / 8) + if (alg->digestsize > HASH_MAX_DIGESTSIZE || + alg->descsize > HASH_MAX_DESCSIZE || + alg->statesize > HASH_MAX_STATESIZE) return -EINVAL; base->cra_type = &crypto_shash_type; diff --git a/include/crypto/hash.h b/include/crypto/hash.h index 76e432cab75d..21587011ab0f 100644 --- a/include/crypto/hash.h +++ b/include/crypto/hash.h @@ -151,9 +151,13 @@ struct shash_desc { void *__ctx[] CRYPTO_MINALIGN_ATTR; }; +#define HASH_MAX_DIGESTSIZE 64 +#define HASH_MAX_DESCSIZE 360 +#define HASH_MAX_STATESIZE 512 + #define SHASH_DESC_ON_STACK(shash, ctx) \ char __##shash##_desc[sizeof(struct shash_desc) + \ - crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \ + HASH_MAX_DESCSIZE] CRYPTO_MINALIGN_ATTR; \ struct shash_desc *shash = (struct shash_desc *)__##shash##_desc /** -- 2.17.1