From patchwork Thu Sep 6 22:58:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10591275 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 C6F8D13AC for ; Thu, 6 Sep 2018 22:59:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B81E92B149 for ; Thu, 6 Sep 2018 22:59:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AC2B02B15E; Thu, 6 Sep 2018 22:59:07 +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,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 4E25A2B149 for ; Thu, 6 Sep 2018 22:59:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728144AbeIGDgu (ORCPT ); Thu, 6 Sep 2018 23:36:50 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:35135 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728086AbeIGDgt (ORCPT ); Thu, 6 Sep 2018 23:36:49 -0400 Received: by mail-pf1-f196.google.com with SMTP id p12-v6so6009461pfh.2 for ; Thu, 06 Sep 2018 15:59: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=3iq55GoU+tLApB726SD4i2dQYVt9A2ASC3TLsp01ZLo=; b=A3UJKfaDq/KIZ/jSL4CCz6gf6ItNwoNjslBKueg5aPG8kQGOubBHg7K7Il2pd/+Fo9 m9SRDwWCZ+BNIMCbF6OCTkmwG7F2DPAHdEEOIrDOW+HHIZKef8WL5PAXkdZPAGRszfOL tD8KjyfeBs2oEjZcNVZLl2jtOXXzlWyWNbjgs= 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=3iq55GoU+tLApB726SD4i2dQYVt9A2ASC3TLsp01ZLo=; b=KMXOl82CsByOuaw/A/iIawnVdvZZ6NhlRu3MfNFkRrjGWcK9K24y2MFBtnY+kjmQb2 pcuNYxPU8aUeRS5DiQdy0U0amQkhsDPn8z81MMfPbbWuCOb5KLoGgDz5oHFpePtYYyus SloQUxYZf4eEA+WkeAD6+OVZNOBVY9ek0I64drrO840kdxYfiED421fGyTD7vR0O/W+Q EEsbmID2cTzueD83Uw8GbeQXcC2fkc3uKtYvflwF7cNObCEprUrWpVp1GdnR19vjie6R 66YvmEC4Tv+p/NkEvzTWdX0Re0iTvlkJaBuExNVwyyQ2aLp0DvyWYXtEUUG3g8vey79z RsPA== X-Gm-Message-State: APzg51A41/66Pqrl2k3SMpGFqZZjOGqP2inZgwnuWiEJc2vl/oz1+9NU Xams+IAwJ391/7FFBgIvdcNT8A== X-Google-Smtp-Source: ANB0Vdb093qGdJ0xfu9GJ0+hy2cT35kwDytA4iv8Ph7ohgX2b1qNn+dNjq02qUNHOEwns4ge1kcbfg== X-Received: by 2002:a62:1bc2:: with SMTP id b185-v6mr5445400pfb.170.1536274743739; Thu, 06 Sep 2018 15:59: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 c1-v6sm6535693pgp.34.2018.09.06.15.59.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 06 Sep 2018 15:59:01 -0700 (PDT) From: Kees Cook To: Herbert Xu Cc: Kees Cook , Eric Biggers , Ard Biesheuvel , Gilad Ben-Yossef , Alexander Stein , Antoine Tenart , Boris Brezillon , Arnaud Ebalard , Corentin Labbe , Maxime Ripard , Chen-Yu Tsai , Christian Lamparter , Philippe Ombredanne , Jonathan Cameron , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 3/4] crypto: skcipher - Remove VLA usage for SKCIPHER_REQUEST_ON_STACK Date: Thu, 6 Sep 2018 15:58:53 -0700 Message-Id: <20180906225854.40989-4-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180906225854.40989-1-keescook@chromium.org> References: <20180906225854.40989-1-keescook@chromium.org> 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 In the quest to remove all stack VLA usage from the kernel[1], this caps the non-ASYNC skcipher request size similar to other limits and adds a sanity check at usage. After a review of all non-ASYNC algorithm callers of crypto_skcipher_set_reqsize(), the largest appears to be 384: 4 struct sun4i_cipher_req_ctx 96 struct crypto_rfc3686_req_ctx 375 cts: 160 crypto_skcipher_blocksize(cipher) (max) 152 struct crypto_cts_reqctx 63 align_mask (max) 384 struct rctx (lrw, xts) [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Kees Cook --- include/crypto/skcipher.h | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/include/crypto/skcipher.h b/include/crypto/skcipher.h index 3aabd5d098ed..cca216999bf1 100644 --- a/include/crypto/skcipher.h +++ b/include/crypto/skcipher.h @@ -144,9 +144,10 @@ struct skcipher_alg { /* * This must only ever be used with synchronous algorithms. */ +#define MAX_SYNC_SKCIPHER_REQSIZE 384 #define SKCIPHER_REQUEST_ON_STACK(name, tfm) \ char __##name##_desc[sizeof(struct skcipher_request) + \ - crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR = { 1 }; \ + MAX_SYNC_SKCIPHER_REQSIZE] CRYPTO_MINALIGN_ATTR = { 1 }; \ struct skcipher_request *name = (void *)__##name##_desc /** @@ -412,6 +413,17 @@ static inline unsigned int crypto_skcipher_default_keysize( return tfm->keysize; } +/** + * crypto_skcipher_reqsize() - obtain size of the request data structure + * @tfm: cipher handle + * + * Return: number of bytes + */ +static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm) +{ + return tfm->reqsize; +} + /** * crypto_skcipher_reqtfm() - obtain cipher handle from request * @req: skcipher_request out of which the cipher handle is to be obtained @@ -446,6 +458,9 @@ static inline struct crypto_skcipher *crypto_skcipher_reqtfm_check( if (WARN_ON(crypto_skcipher_alg(tfm)->base.cra_flags & CRYPTO_ALG_ASYNC)) return ERR_PTR(-EINVAL); + if (WARN_ON(crypto_skcipher_reqsize(tfm) > + MAX_SYNC_SKCIPHER_REQSIZE)) + return ERR_PTR(-ENOSPC); } if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) @@ -507,17 +522,6 @@ static inline int crypto_skcipher_decrypt(struct skcipher_request *req) * skcipher handle to the crypto_skcipher_* API calls. */ -/** - * crypto_skcipher_reqsize() - obtain size of the request data structure - * @tfm: cipher handle - * - * Return: number of bytes - */ -static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm) -{ - return tfm->reqsize; -} - /** * skcipher_request_set_tfm() - update cipher handle reference in request * @req: request handle to be modified