From patchwork Sat Apr 7 18:38:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 10327991 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 5B09560365 for ; Sat, 7 Apr 2018 18:39:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 49C20294D5 for ; Sat, 7 Apr 2018 18:39:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3C065294E1; Sat, 7 Apr 2018 18:39:30 +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=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 597DF294D5 for ; Sat, 7 Apr 2018 18:39:29 +0000 (UTC) Received: (qmail 11954 invoked by uid 550); 7 Apr 2018 18:38:52 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 11860 invoked from network); 7 Apr 2018 18:38:50 -0000 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=oZJHlz74leLbB95syiNmkrgCZhugYvoNKD0PdVRJfUI=; b=LTYSPFeE9cfuZVF/qoPy1gom1rdJoEKa/tT6EtM2Joe1UHuXd/h/t5WNDnOTeIVoNr ZYPC41zW33909bAjJVLGUYlBHe8TNsAqoPRUz5wUNg6G3BWBTE0Pr5f6moWjIxp2hXe5 8hy6ToCDtw7fR/tPXePT5mFVaTv+hr6YEZa3w7oEMvNSEKZ0PCJ2u3/qfm6XW6fIqVyG 34hCsHhtFltcJMdme75W6ir6HnOdjUqQaIV+wZXrwpBzL0ABae9cZznRA1np1cyT5pt7 RlVKn90I6S6WZlv9gQ/h1ZNDmkOA6bVsw13IfuraqO75oEdHAm5MuTm2oIx+Kfv/GdT0 CO8g== 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=oZJHlz74leLbB95syiNmkrgCZhugYvoNKD0PdVRJfUI=; b=aLlDZe+BIAbHH9+fnRdZO77SlL3v6nm/kWzIzF2POJrgYXKxH7QPIVYcrNHT03xRuk QpPe4dy5wCzlD1zmF8l6tI5p2aG5hfUnOwew742uzPtT9QzR6gTd9XX/m1Zx2GCUwuZQ 0SJVk8K8N7Zxcvu7aCwlslZqroITh2ku2sYstY8b/lJ8nsouKh9BT6MtvYMR3i2XMAFe odOTFB0F7LTmLb6K1gKZxsgVzDPWaCj9csFY3K0QFGoh+hmMXcmf9yyUU9BfUnNiGVb6 V1DQaHjgcYaJZQZo2yb104nkrcJvzQWb384XFsYQUsfxDidPzt1iFZ/fqEeP4H2Lgin2 gnvA== X-Gm-Message-State: ALQs6tAF6mGK/+/GhMpzgFyI9VnTuxOQuPWFEg/sOLxZpp0KhRB8Ecn4 Pv6rj/1UHSoJWSNjqKiVJvAv8/dFETU= X-Google-Smtp-Source: AIpwx4+Ix3jjyJjo5vfGenKLsfZQtp2JdibQ8QMdlTUGUCkd56w0Z0HRaqT22Wd1VoIc2LPfyKqE0w== X-Received: by 10.28.234.207 with SMTP id g76mr17809015wmi.149.1523126319350; Sat, 07 Apr 2018 11:38:39 -0700 (PDT) From: Salvatore Mesoraca To: linux-kernel@vger.kernel.org Cc: kernel-hardening@lists.openwall.com, linux-crypto@vger.kernel.org, "David S. Miller" , Herbert Xu , Kees Cook , Salvatore Mesoraca , Eric Biggers , Laura Abbott Subject: [PATCH 2/6] crypto: ctr - avoid VLA use Date: Sat, 7 Apr 2018 20:38:19 +0200 Message-Id: <1523126303-23205-3-git-send-email-s.mesoraca16@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1523126303-23205-1-git-send-email-s.mesoraca16@gmail.com> References: <1523126303-23205-1-git-send-email-s.mesoraca16@gmail.com> X-Virus-Scanned: ClamAV using ClamSMTP We avoid 2 VLAs[1] by always allocating MAX_BLOCKSIZE + MAX_ALIGNMASK bytes. We also check the selected cipher at instance creation time, if it doesn't comply with these limits, the creation will fail. [1] http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Salvatore Mesoraca --- crypto/ctr.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crypto/ctr.c b/crypto/ctr.c index 854d924..ce62552 100644 --- a/crypto/ctr.c +++ b/crypto/ctr.c @@ -20,6 +20,7 @@ #include #include #include +#include "internal.h" struct crypto_ctr_ctx { struct crypto_cipher *child; @@ -58,7 +59,7 @@ static void crypto_ctr_crypt_final(struct blkcipher_walk *walk, unsigned int bsize = crypto_cipher_blocksize(tfm); unsigned long alignmask = crypto_cipher_alignmask(tfm); u8 *ctrblk = walk->iv; - u8 tmp[bsize + alignmask]; + u8 tmp[MAX_BLOCKSIZE + MAX_ALIGNMASK]; u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1); u8 *src = walk->src.virt.addr; u8 *dst = walk->dst.virt.addr; @@ -106,7 +107,7 @@ static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk, unsigned int nbytes = walk->nbytes; u8 *ctrblk = walk->iv; u8 *src = walk->src.virt.addr; - u8 tmp[bsize + alignmask]; + u8 tmp[MAX_BLOCKSIZE + MAX_ALIGNMASK]; u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1); do { @@ -206,6 +207,14 @@ static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb) if (alg->cra_blocksize < 4) goto out_put_alg; + /* Block size must be <= MAX_BLOCKSIZE. */ + if (alg->cra_blocksize > MAX_BLOCKSIZE) + goto out_put_alg; + + /* Alignmask must be <= MAX_ALIGNMASK. */ + if (alg->cra_alignmask > MAX_ALIGNMASK) + goto out_put_alg; + /* If this is false we'd fail the alignment of crypto_inc. */ if (alg->cra_blocksize % 4) goto out_put_alg;