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: 10591289 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 0E4E9921 for ; Thu, 6 Sep 2018 23:01:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F14062B163 for ; Thu, 6 Sep 2018 23:01:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E56122B1BC; Thu, 6 Sep 2018 23:01:52 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6E4252B163 for ; Thu, 6 Sep 2018 23:01:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=aPEFx1sAcvZ+xi71fUal7fE4bpUyVl6Q/AMp+W6hUB8=; b=V+V1V0NgIewn9gfV2csNP7AXI7 +Cy8kQjZCc1fkWgw/xs0fC1KhYF+a72yQ8HJ7ay5HlKG92Ij5sIC/LcL1zHjwqfYEsCRFPBKX58HT aPHCClaf/6oz+bATOj7ivs1scHAJN9WSN9Z5eKfx6GIDywgbrRxcQsGcTiPjlAM/GnVQ+0oKD0jYe PMjNtNaSXen596C+rHyMiQUPSdjxaDznvuk/Ao+WldhNOFXnWFyLFZDQI2/sTiD8kgH2lwyC/0K9+ rq2DR62ridha3EY9QSxSvQwVY0K6CF6hYPo2aM9gK8Tu53xVyHKwc6udV8LdkVjIWg5fpXMA9ovzi FNwJ/GYw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fy3HQ-0007dZ-Kr; Thu, 06 Sep 2018 23:01:40 +0000 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fy3F4-0005UI-Ds for linux-arm-kernel@lists.infradead.org; Thu, 06 Sep 2018 22:59:27 +0000 Received: by mail-pg1-x542.google.com with SMTP id l63-v6so5958328pga.7 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=CNGDH/hmhbWMXPQJVDSjTr4UGkcXRy/U11ulLbFLiUMtwlFLTHjw6c4ifhRIVkDrHU 9Rnif5YVFB53Mk23VR8kNhPLPqV+25rTIrJFNOKv0QIQni/TJ1HV3RSrI9L4kiD/D/ij 7UG0v3mbUxiF6zHkbhel1LMBlM1DWs9yXZ3bn6rYgUiXLwxxM/I8OsH80WM1oRYUBOcm 7/RGDoA58KVzg3Pjq42i2NQ6D0PkNVgGb1ibIfvKIXXcXTq9sMVhRuZpOSQDQ55Tu17e B0wGO+QUtEZnKAPApzUYjk1Rrf9L/Ks8Ed1jCxZBVQWzqxTXh554o77AluE7EkwJ5U7M X3zQ== X-Gm-Message-State: APzg51A8Jd7k7h0k45EWl5y5b1SBYJCtsoaxcL1QdCSZbKZEzFtJ3rsE 6eo0oZzReUMmkfxQvYGli3z+gQ== 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 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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180906_155914_507662_D07EAC01 X-CRM114-Status: GOOD ( 15.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Maxime Ripard , Arnaud Ebalard , Kees Cook , Christian Lamparter , Ard Biesheuvel , Antoine Tenart , Boris Brezillon , Eric Biggers , linux-kernel@vger.kernel.org, Gilad Ben-Yossef , Chen-Yu Tsai , Corentin Labbe , linux-crypto@vger.kernel.org, Jonathan Cameron , Philippe Ombredanne , linux-arm-kernel@lists.infradead.org, Alexander Stein MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.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