From patchwork Fri Mar 30 08:53:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 10317421 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 8D63C602A7 for ; Fri, 30 Mar 2018 08:53:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7FEE42A566 for ; Fri, 30 Mar 2018 08:53:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 74A5E2A56A; Fri, 30 Mar 2018 08:53:54 +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=-4.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, 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 922F92A566 for ; Fri, 30 Mar 2018 08:53:53 +0000 (UTC) Received: (qmail 23911 invoked by uid 550); 30 Mar 2018 08:53:51 -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 23870 invoked from network); 30 Mar 2018 08:53:51 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=pDoNOVwK7qAVEtx0ZUWXAmQXO3jwjzFr/1BD4vPGBZ4=; b=QxF86thLcNULHIQBL4RQiqYBY26HJwI3en2sB+/PUrSkuhZ45oafKI0mA9we8K7vdk eYLcGzcCMEkCwg687tlIusyntiUR6YPW97DGvBV5nWm0CWRIA+AbW12qdXrg8WcIHFH0 tJzeHd0pugXgFIbZfoPAdN2oIjLY07Mz8C8zUNF/1BQ+H8Dx7zLJkAt+J+EyuyjLnzBL HlG1dM0MlfEkyrC2kkMOch+UYb2pEa/0ez5kCyYst3O6pL3lpq80Tz0lxd52OaEDvPq1 7erBLnJBPTYe3lM5UFFtly3XuJ5QWuSd+Ts+ZKa5MDCM1e7yfYqzlfvTHmSWwpb7c3xR 9PEA== 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; bh=pDoNOVwK7qAVEtx0ZUWXAmQXO3jwjzFr/1BD4vPGBZ4=; b=uXe89NDe+7m70oz6YeRSi03/pTO0EO+iVb/DJB50k7K9RzyetuH5zknu12/v0nS5dl f0LxTKsYQERMJsT3GLYQA9n67SW3+2lm25xM7YfVrZHBP7S0P/0OFSK7+DzPe78WgWIE DFqQfgqnPcQ98HGoJwy9jzIlOzKlTg0RhkFh6VMOBfbBKtSLI2dUD9VJaOVsUH4VZA8s 5vNuGtZdGASHeUvsKambBI+ZebmU0o5Jjd9qrA/0XBYRJa1KG5f1n8jQ5+4tpyyxh6yg xIWaziV8Z0twaVhZuIWSyftZZtzIPaVnOuARhAIRdx/BUw3qFvzlTWm4xtaIFjnvOpxC K/Hw== X-Gm-Message-State: AElRT7Gh+Vck90LluPnJAL+d7JayUd/9E5N+PTGzKrb+IZG0aG9SwtzC Ih6ZiEdzVpdKmDkWjb+xDpvgNUYr5DWrFA== X-Google-Smtp-Source: AIpwx48gNmOcawSeF8RPaP6i9aYL2TWmpU4xLD9QTJB+HdLGOBBKxgj0TQTj/1EX+XiByQ8Lh77dgg== X-Received: by 10.223.179.195 with SMTP id x3mr9239762wrd.94.1522400019924; Fri, 30 Mar 2018 01:53: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 Subject: [v3] crypto: ctr - avoid VLA use Date: Fri, 30 Mar 2018 10:53:26 +0200 Message-Id: <1522400006-8859-1-git-send-email-s.mesoraca16@gmail.com> X-Mailer: git-send-email 1.9.1 X-Virus-Scanned: ClamAV using ClamSMTP All ciphers implemented in Linux have a block size less than or equal to 16 bytes and the most demanding hw require 16 bytes alignment for the block buffer. We avoid 2 VLAs[1] by always allocating 16 bytes with 16 bytes alignment, unless the architecture supports efficient unaligned accesses. We also check the selected cipher at instance creation time, if it doesn't comply with these limits, we fail the creation. [1] https://lkml.org/lkml/2018/3/7/621 Signed-off-by: Salvatore Mesoraca --- crypto/ctr.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crypto/ctr.c b/crypto/ctr.c index 854d924..49c469d 100644 --- a/crypto/ctr.c +++ b/crypto/ctr.c @@ -21,6 +21,9 @@ #include #include +#define MAX_BLOCKSIZE 16 +#define MAX_ALIGNMASK 15 + struct crypto_ctr_ctx { struct crypto_cipher *child; }; @@ -58,7 +61,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 +109,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 +209,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;