From patchwork Thu Mar 8 21:57:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10269281 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 7C0C6602C8 for ; Thu, 8 Mar 2018 21:57:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A33E29AAC for ; Thu, 8 Mar 2018 21:57:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5EDB729AAF; Thu, 8 Mar 2018 21:57:19 +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, 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 6CD9C29AAC for ; Thu, 8 Mar 2018 21:57:18 +0000 (UTC) Received: (qmail 9937 invoked by uid 550); 8 Mar 2018 21:57:16 -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 9902 invoked from network); 8 Mar 2018 21:57:15 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=w0U19uYN3nSjPLiSqmXDBEuAMXFjl13Cwv1eIZMdKfw=; b=NszixnG1n5PuN2kUsDTc3ZJOZESPLh5ric/7jUQwozrxoliDEIiJ9b2wGqkz+Teddq AqILmc8sRpgPd6aNjR0Z6AWhGBofuzOOTQp+GAtWvFGLdsmEYCEGgizEOnbIlRpvpK+K BG+p1H3d/s1ANzmTQK+ZMZ5TTUv3f+0qW1s28= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=w0U19uYN3nSjPLiSqmXDBEuAMXFjl13Cwv1eIZMdKfw=; b=d4aaOl8JinMPO1y8YuEMEPM19Ldswff5ZJfBXvILKEGoJQyXzn8F+n59YndiQ9wUzc s3DzEjOiU8Uq6e4/00i7F7hNHrbi87Vv19oiVzlloZe6xGEqQtmhkQSSCEd7UDqDkf9D VJaF35WRxoWIdz+7RWQKpFrpUCjITD96wjCooZGkLvAcqFXcmxU8/IMMcK37cADLo6/H 0VyFY25rP2+yovvAIfNqTDvVA9I4W5R5BCOmF5ZBjGdW6LrwhsD2yzxh/9dJQf+SVnko Hce3RH5fxSoUf5fU+jcBs14oGm44Dbj0swCoT2yNM0fej9EAQt0uqhfdOQ+4PKUXw7Zp +nLw== X-Gm-Message-State: APf1xPB9FVqL1BqEjMiqAQyFouDVQZf4OZb1g703r9VJ7j6H/0pbK1id Me0TP3ZBV63y2Ph2OPa8yvMGoA== X-Google-Smtp-Source: AG47ELs576DaZbuZqmMVzprghQnPYbQI3q8Q0FS8vr/dulSEmswA89s+U9FVfljRGE4pkLPkOF7eGw== X-Received: by 2002:a17:902:e85:: with SMTP id 5-v6mr25180132plx.420.1520546224051; Thu, 08 Mar 2018 13:57:04 -0800 (PST) Date: Thu, 8 Mar 2018 13:57:02 -0800 From: Kees Cook To: Herbert Xu Cc: Tudor Ambarus , "David S. Miller" , linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-kernel@vger.kernel.org Subject: [PATCH v2] crypto/ecc: Remove stack VLA usage Message-ID: <20180308215702.GA9902@beast> MIME-Version: 1.0 Content-Disposition: inline X-Virus-Scanned: ClamAV using ClamSMTP On the quest to remove all VLAs from the kernel[1], this switches to a pair of kmalloc regions instead of using the stack. This also moves the get_random_bytes() after all allocations (and drops the needless "nbytes" variable). [1] https://lkml.org/lkml/2018/3/7/621 Signed-off-by: Kees Cook Reviewed-by: Tudor Ambarus --- crypto/ecc.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/crypto/ecc.c b/crypto/ecc.c index 18f32f2a5e1c..9c066b5ac12d 100644 --- a/crypto/ecc.c +++ b/crypto/ecc.c @@ -1025,9 +1025,7 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, { int ret = 0; struct ecc_point *product, *pk; - u64 priv[ndigits]; - u64 rand_z[ndigits]; - unsigned int nbytes; + u64 *priv, *rand_z; const struct ecc_curve *curve = ecc_get_curve(curve_id); if (!private_key || !public_key || !curve) { @@ -1035,14 +1033,22 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, goto out; } - nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT; + priv = kmalloc_array(ndigits, sizeof(*priv), GFP_KERNEL); + if (!priv) { + ret = -ENOMEM; + goto out; + } - get_random_bytes(rand_z, nbytes); + rand_z = kmalloc_array(ndigits, sizeof(*rand_z), GFP_KERNEL); + if (!rand_z) { + ret = -ENOMEM; + goto kfree_out; + } pk = ecc_alloc_point(ndigits); if (!pk) { ret = -ENOMEM; - goto out; + goto kfree_out; } product = ecc_alloc_point(ndigits); @@ -1051,6 +1057,8 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, goto err_alloc_product; } + get_random_bytes(rand_z, ndigits << ECC_DIGITS_TO_BYTES_SHIFT); + ecc_swap_digits(public_key, pk->x, ndigits); ecc_swap_digits(&public_key[ndigits], pk->y, ndigits); ecc_swap_digits(private_key, priv, ndigits); @@ -1065,6 +1073,9 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, ecc_free_point(product); err_alloc_product: ecc_free_point(pk); +kfree_out: + kzfree(priv); + kzfree(rand_z); out: return ret; }