From patchwork Wed Mar 7 21:56:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10265383 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 53DAA60211 for ; Wed, 7 Mar 2018 21:56:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 41972295D0 for ; Wed, 7 Mar 2018 21:56:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3627A295D5; Wed, 7 Mar 2018 21:56:57 +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 541CE295D0 for ; Wed, 7 Mar 2018 21:56:55 +0000 (UTC) Received: (qmail 30472 invoked by uid 550); 7 Mar 2018 21:56: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 30437 invoked from network); 7 Mar 2018 21:56:30 -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=ouZF0y5KLhRn9w9F0iNKpmOk6Nmmj31Co9EY3r/FL4c=; b=bmMIilWIo0L6Adca8Z5vMW8nYL3w32F1HJgBQYxaUzgiNxFDTgpV0D1i3ibYWXncRk dwQL1woaLK8tKFqONDLir4JWBrM6srOcU/ZGFog6CK3SDMYUmGZdzx2CNbn6qtBc0Z61 Hij8SFnClxQlAjVVlVyDPKJMDP/J3BGn+AZPI= 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=ouZF0y5KLhRn9w9F0iNKpmOk6Nmmj31Co9EY3r/FL4c=; b=hLKIe+IAkqYgraFN3EEAdNi8NTZNK2ya1hepkk+yMyeOSr2JDIj/9O9y+TfIQmUocs fdRXV5XbIr0fAm5Jfv/C1Tae4t4cyXoU8Q7KzGBWRc/6LDa0FRQa/zTjo9aUAfKH8YrO Js8M5woQ3AFBnc7Czvm2DlVUmfpIriw3hd1WCPFGuNUb59k6/Vp55vGa+KqxXYYMVKIs gCC+EL2VZLrw2LDqn2IQFuFbWjiWs38L6qBMFsbYwEXgM99+EHWSL2DNXFW9Cf8zC0cT aW6Tm0J2MHg6GxsZ/IcwVu3xCxzZvmcvktfN8yyZsfvttQMqePXgqqebsE9kIs9lGRPq Wtnw== X-Gm-Message-State: APf1xPDQl78LcssLRmV0M2zCINI3H3re/UTBMyoxBWqcn5ksNF7vXD/Q 2XPMmtK69rt3rqaMtc1HHM1n8Q== X-Google-Smtp-Source: AG47ELvG+hUbmKrmWKQjKo3HdoyiNy4K796+n7FUDlHN+Bvuh2UM2pVm2ptT10SPYHk2SGVB6LNzSA== X-Received: by 10.99.174.66 with SMTP id e2mr19325796pgp.269.1520459778506; Wed, 07 Mar 2018 13:56:18 -0800 (PST) Date: Wed, 7 Mar 2018 13:56:15 -0800 From: Kees Cook To: Herbert Xu Cc: linux-kernel@vger.kernel.org, "David S. Miller" , linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: [PATCH] crypto/ecc: Remove stack VLA usage Message-ID: <20180307215615.GA18928@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 --- crypto/ecc.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/crypto/ecc.c b/crypto/ecc.c index 18f32f2a5e1c..5bfa63603da0 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: + kfree(priv); + kfree(rand_z); out: return ret; }