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: 10265381 X-Patchwork-Delegate: herbert@gondor.apana.org.au 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 78421602C8 for ; Wed, 7 Mar 2018 21:56:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 662C4295D0 for ; Wed, 7 Mar 2018 21:56:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5AAD7295D5; Wed, 7 Mar 2018 21:56:37 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 03EE9295D0 for ; Wed, 7 Mar 2018 21:56:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754812AbeCGV4V (ORCPT ); Wed, 7 Mar 2018 16:56:21 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:45182 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754810AbeCGV4S (ORCPT ); Wed, 7 Mar 2018 16:56:18 -0500 Received: by mail-pg0-f65.google.com with SMTP id i133so1407379pgc.12 for ; Wed, 07 Mar 2018 13:56:18 -0800 (PST) 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=PhaTOWTUnZO2S9jg3zqgws41PG4Fudc50l9v7ow6NFduZentTW22Bi3EINob96lOYI oNZIwwmPMjZ9bjhoBgcfvEYDqTA2QgQryP9Qv0N6cx3h+hSnTxgKsDIa/e0yqL90cesh 9NoTh5ZyYydeFMyfGpSKYkGU1jAVNV4/ZcMiJztwLHkBSAmf8+WoGQQc0eN8u1Vjy1kw GN5U3g6XQGM1dPYY12CFGfZxtz3Ym3Ir1K/ufy6kO+Q3DVnpnFWpkhTt9U8Qnh7xuh3s onq0TNKRGshD+/62I/pxxg622D+MM2m1rEO7kw+MkpHp86xrDGubgqaqj5d+Zel4PCmh uZiA== X-Gm-Message-State: APf1xPAzeN54Ipy21EEwWi3RjRHqRpg9vNKdh7S92ub++PCDcJtMW9aC SXuyKjZgNMIKIrmRFGdtLb7N9w== 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) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id f6sm30193238pgo.58.2018.03.07.13.56.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Mar 2018 13:56:16 -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 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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; }