From patchwork Sat Apr 7 18:38:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 10327993 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 3907960365 for ; Sat, 7 Apr 2018 18:39:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A07E294D5 for ; Sat, 7 Apr 2018 18:39:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1DBDE294E1; Sat, 7 Apr 2018 18:39:39 +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 4CFBB294D5 for ; Sat, 7 Apr 2018 18:39:38 +0000 (UTC) Received: (qmail 12001 invoked by uid 550); 7 Apr 2018 18:38:55 -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 11933 invoked from network); 7 Apr 2018 18:38: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:in-reply-to:references; bh=YAlTGdnPdSsRn+04SbOD45FkZFw5nVfUTH5BnaTOpDI=; b=fQbxwYCQRwguXt0Nu40/mZ5/B67a2rWGwzf3U9rFLld0sJH1wO/aywLd910gm7v4DF ExjNqLVoklVkkqDfHshlft6EKkAK1wtzkF1LXPGgEe2P+EsQ0+oEXvpGm43abJ7hAN8H oMCBUdJVNKeqz+2PHoIAM/U2b8slQ/ZsrXgvYAXTAEhoCVDBTWN8kyosUwaPJDia7oki upO0WOq+CiVKMMz/B1fgtDWz2oHjYAY58uU4SQUn0VCNIjhEglSVERPElKo7cQiIqwZC WfjDGZPA120unW7ENN6a66czCj1LE2zgXCtKIyLLC0G+ByORC5SMoFNqVJmHxVARsJ1j wDFw== 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=mNl3kLiIvWKeBoDFDwaVWAqHpGM6EZ8U7n1HOxSh9VUGAfrto7NuE/sXBmmkQL96gc QADL51lQ4D3F/+X2Cw0/MswkV2n0vUY1Tk48/wmTNlF+WwIMYg20rmdwsdiPUqMaAIsz TsXCE4dlEYA4XaVS7wCHE1JjQO40MJnWhLyIr2J0y2QQO1Os65IbW69DMtPkCxr4dopJ M7SmbByEe6MTCV0NyS3HcfliNyVTmGcSGNs1Ruh3yjz3sNjFi6vh9DDMxXjIcGslvkq+ ghqQjDBSGKdTG7amkKtdYCJIp7NdbU0zASeMRICWFGQObiAxqexEmHHCKRqrbn7MaGIC Tgkg== X-Gm-Message-State: ALQs6tDJCq61E8Y4AZbEQqsf7nsDRhnQ7d27XCSafzDdAhPAitw4mBdz pkDH7rtenILIGbPIWxRnYcg= X-Google-Smtp-Source: AIpwx49Z64EOPvvkM0YfCg+Fmzo6IQFUgYlKoPIuIpANWRVNsL7dWi4r3Uc2yKDTay0PNEa2eHE7bw== X-Received: by 10.28.21.84 with SMTP id 81mr740331wmv.36.1523126320413; Sat, 07 Apr 2018 11:38:40 -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: Sat, 7 Apr 2018 20:38:20 +0200 Message-Id: <1523126303-23205-4-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 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;