From patchwork Mon Apr 9 13:54:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 10331367 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 417A66037F for ; Mon, 9 Apr 2018 13:55:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 347E228B57 for ; Mon, 9 Apr 2018 13:55:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 291D728B59; Mon, 9 Apr 2018 13:55:06 +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 5411128B57 for ; Mon, 9 Apr 2018 13:55:05 +0000 (UTC) Received: (qmail 15928 invoked by uid 550); 9 Apr 2018 13:54:30 -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 15888 invoked from network); 9 Apr 2018 13:54:29 -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=YAlTGdnPdSsRn+04SbOD45FkZFw5nVfUTH5BnaTOpDI=; b=r8Z+8gzYN8QLtF2tqF4LoxrQcsHAgQ1r1Uz4kPxGUDF3qX0rB+MPG38Yi3cMfwyINe syhNu4LsewcH7g5tluP1qvZxuUBgLfpm1AuiyVB7FVq7eXjU08qoW9yjMv4GnWMArdKB xs5aKPr5GghVtZE8LD93zwhMJtiKstXdpn19Vs10MK/r1UCD/81zRNSbsvMfk0x/gW2G 36IaC0XXA9dN1Z0o0VLsi6CoY4pMMpZ/Qno+iQy0K232FobzArnFpXJKv1TRI3nxjC7p eM+lODWXjaSmPGtVqpDJhg34rGMQN+3ZFC55uDyyKut1yAipEmfhYOy3pTwKRmwAQUbD e4Mg== 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=YAlTGdnPdSsRn+04SbOD45FkZFw5nVfUTH5BnaTOpDI=; b=MCYzN83QRxuUIU+t45XBZ6q3CFvWPqIgHqYa7wCeAUviie7To23GjgcPwiDA8fS/SS FXqvNfWlruiwROeaOt3fqoMYqbm3qmWUdoBJiSoYO1fys9SIzU0QYLPUW7H7w7gkBmok NV0i8Cm88DyRWnkzfw1g0hMoKkOgykXhfA8dgtNqSVKj0ia7z5n8tY5YCWYcDHQAam3t 3EgkJcgYhjsMrVRiYjnz0hEXTWLkR3ratM4VNBkmoAwUfzyQn80019XW2HnxLvJh3sDn gMLhdRmvmneuK2PeNxlCWb2XCG/c9k2HBP/ri930nR+GhudtnTax2sKKHGMo1D0sl5r5 0ypw== X-Gm-Message-State: ALQs6tBdYr7f0I4HvYkrJWHyx+iuPC2EQTiCFoU9RWsU9hjlnfJ3Tavm YtC8D8ABa76pQ6pGwsGgVqBW9HN8Mwg= X-Google-Smtp-Source: AIpwx4/rAesFftdAf11LmEVdHDORra1kBAgey79Fb5i9bSwkx7LtGLviynBor/dChI4PjWO8zrJHuA== X-Received: by 10.28.87.73 with SMTP id l70mr46139wmb.123.1523282058160; Mon, 09 Apr 2018 06:54:18 -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 3/6] crypto: api - avoid VLA use Date: Mon, 9 Apr 2018 15:54:01 +0200 Message-Id: <1523282044-22075-4-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 a VLA[1] by always allocating MAX_BLOCKSIZE + MAX_ALIGNMASK bytes. We also check the selected cipher at initialization time, if it doesn't comply with these limits, the initialization will fail. [1] http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Salvatore Mesoraca --- crypto/cipher.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crypto/cipher.c b/crypto/cipher.c index 94fa355..9cedf23 100644 --- a/crypto/cipher.c +++ b/crypto/cipher.c @@ -67,7 +67,7 @@ static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *, { unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); unsigned int size = crypto_tfm_alg_blocksize(tfm); - u8 buffer[size + alignmask]; + u8 buffer[MAX_BLOCKSIZE + MAX_ALIGNMASK]; u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); memcpy(tmp, src, size); @@ -105,9 +105,14 @@ static void cipher_decrypt_unaligned(struct crypto_tfm *tfm, int crypto_init_cipher_ops(struct crypto_tfm *tfm) { + const unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); + const unsigned int size = crypto_tfm_alg_blocksize(tfm); struct cipher_tfm *ops = &tfm->crt_cipher; struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; + if (size > MAX_BLOCKSIZE || alignmask > MAX_ALIGNMASK) + return -EINVAL; + ops->cit_setkey = setkey; ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ? cipher_encrypt_unaligned : cipher->cia_encrypt;