From patchwork Mon Apr 9 13:54:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 10331373 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 DB71A60236 for ; Mon, 9 Apr 2018 13:55:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CE7DE28B57 for ; Mon, 9 Apr 2018 13:55:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C364328B59; Mon, 9 Apr 2018 13:55:22 +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 00FF228B57 for ; Mon, 9 Apr 2018 13:55:21 +0000 (UTC) Received: (qmail 15966 invoked by uid 550); 9 Apr 2018 13:54:31 -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 15661 invoked from network); 9 Apr 2018 13:54:28 -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=vGFjuAKLCs5aa850o77prnhfLelO6kB0mGF/ox3w017c8OxAlXKlCyJzzgrOcKTriS iwAG/KY+X6Z4QtCmOjSYRcY+FF0VxTR36xGbBU0lE+IFFBssHSpneHKuz4Tcushy4eN8 OlS4G8nhFm4S8xhFqH95dxSaf/7V9to5vzqpV+YfBrWKVmiv1OcuGBsHCBv/bS0jsNjQ FDsOgq0DHI6ZrDyWGQfqYkYq1fSHppV4pag6+5WVIXj6aPfn5JmKDcE+1P5ihtgDqQdN JPxQ/F+RGN2gK4DYZqZ9sMuOCNgWoOA5oen60bbLaytuMnqQ+jVMGjcD0NRXF8TV7A4Y pbdA== 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=d4hW8DCfTu9pSaUyu/DDhiafxAAz3ZZGfhw5gT1Avg8+iBTkv9RtXKCF5Lj7RfvAjw 6SyAs2vZEt10DAauSNa0orhhrXy+dpXOmamWPINDfdUHFSdJh0NigJrcENyAh0EslNtS Op4TuI59RC8gCAkqpPT+wjLi07+ay2iVOb7dzUx7WCoQoultLq2VgxjNHrQmeqfH1o09 acYonJRPH/99odQTsbK4+8lO01QJPC54sPtdIIrIpG22HoE6xqfWkKykpCqrSoYdp+Et QP8z6gChRoVRjf5UzR3l9mTRis5ZRL4TNkd0eXXQJWTEo58a1hh+vnJ9sDDVElEnWJRX dasw== X-Gm-Message-State: ALQs6tB624aaDIkg2rIxfyZy7XKqnuDUSmhF1GkM62JU83W4ZW5pCfsf ZYM6i84P99J0DKxtsTDDJbU= X-Google-Smtp-Source: AIpwx4+wJ+v5F6JstQAdGHhel61VhscGSU1+aOpsU+X7Kp7U438SFXrQxCk+StkNp/WL9+yBvmXpgQ== X-Received: by 10.28.236.152 with SMTP id h24mr75964wmi.92.1523282057043; Mon, 09 Apr 2018 06:54:17 -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: Mon, 9 Apr 2018 15:54:00 +0200 Message-Id: <1523282044-22075-3-git-send-email-s.mesoraca16@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1523282044-22075-1-git-send-email-s.mesoraca16@gmail.com> References: <1523282044-22075-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;