From patchwork Tue Apr 24 20:26:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tycho Andersen X-Patchwork-Id: 10360925 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 BEE1B601BE for ; Tue, 24 Apr 2018 20:28:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AE36C28A30 for ; Tue, 24 Apr 2018 20:28:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A267028BDA; Tue, 24 Apr 2018 20:28:24 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 0C3FE28A30 for ; Tue, 24 Apr 2018 20:28:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751251AbeDXU14 (ORCPT ); Tue, 24 Apr 2018 16:27:56 -0400 Received: from mail-pg0-f67.google.com ([74.125.83.67]:36790 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751037AbeDXU1q (ORCPT ); Tue, 24 Apr 2018 16:27:46 -0400 Received: by mail-pg0-f67.google.com with SMTP id i6so11688251pgv.3 for ; Tue, 24 Apr 2018 13:27:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tycho-ws.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SJErzKT9nbvMFCOiOIf+uqZIZJ+5ZpCdx6U/gxgdK3g=; b=sLEWD1OqsL1gF6QVVWFvm0yrT4CNaOwzCrMjJxPXfoq+ZIUQw4b/fxK8a5ndB2F2Kn jGNLw+IwiQuFcjuTY7gS+nVahLXfCgXax9IzMPhF2A3NqA5XqaxnWhBFVlwWYnfRgGiB MN0q1M4f5FU/pzSTikrUYFZdAjKXtZ1+0FwSkY++dSDvpdcjYqfxpFi+NsZgkU4PLP45 4XL7o4WcR50RSJscU6Zz0yR4fOqfsB6Iysg9bZIwsDrzULkKcg3pyZk/IR/VPwSt3AeF WtAKRDSqLXqLCJNQCy0X1h/nGaGPWMlyv4ZI6ul0kEumQgPrKNtHFRt6a8mfx2tN6icO zNQw== 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=SJErzKT9nbvMFCOiOIf+uqZIZJ+5ZpCdx6U/gxgdK3g=; b=f7YiGisL3z55mKIt0RVcGhV7En/UIRf9q3HwXDvO9rW9CZexbDgAhaL81r8v7tQvQ7 Zvy1kn661G/2SAD5l29XGp6rULPmciC8QWuWON43/ZjRYHrkh3O2FujIdn2yjzOoOHBA MLXdYjR/XiJQK3KZt9wrbyuhgUvYB1WMI5Ezi55A5oXtMzA+18pUjcGzN1FkdD9VQ8lW qOYbf1amZJUYyaKsjifonR3yORuYpil0S/H79UE5yj8gua+sNfOvwPv5eENsom8KEq5e C8M8FJPzsNwI+Q8asDys45VtSwAF13qVDda0HC56QxPBlv5XOE5u8cExtgiFD9DQ8LyZ DeOA== X-Gm-Message-State: ALQs6tCIciXVUpOrwBwbYPEFiVBMjsRcSg8+VqvE2ZSrptp3hZb9+WYT 79kIgdScbO7aUvZvBwrLf2JYvw== X-Google-Smtp-Source: AIpwx49NIe17bua9CFltCGGmt7OXUuOwq8MWk9zQboV3ora4lt7Lx6R93KLK4i4r3ZEJ73SAHL1eqA== X-Received: by 2002:a17:902:42a5:: with SMTP id h34-v6mr26177974pld.65.1524601665815; Tue, 24 Apr 2018 13:27:45 -0700 (PDT) Received: from localhost.localdomain ([128.107.241.171]) by smtp.gmail.com with ESMTPSA id r82sm49943847pfk.187.2018.04.24.13.27.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 13:27:44 -0700 (PDT) From: Tycho Andersen To: David Howells Cc: keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, Tycho Andersen , James Morris , "Serge E. Hallyn" , Eric Biggers Subject: [PATCH v3 2/3] dh key: get rid of stack allocated array Date: Tue, 24 Apr 2018 14:26:38 -0600 Message-Id: <20180424202639.19830-2-tycho@tycho.ws> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180424202639.19830-1-tycho@tycho.ws> References: <20180424202639.19830-1-tycho@tycho.ws> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP We're interested in getting rid of all of the stack allocated arrays in the kernel: https://lkml.org/lkml/2018/3/7/621 This particular vla is used as a temporary output buffer in case there is too much hash output for the destination buffer. Instead, let's just allocate a buffer that's big enough initially, but only copy back to userspace the amount that was originally asked for. v2: allocate enough in the original output buffer vs creating a temporary output buffer Signed-off-by: Tycho Andersen CC: David Howells CC: James Morris CC: "Serge E. Hallyn" CC: Eric Biggers --- security/keys/dh.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/security/keys/dh.c b/security/keys/dh.c index d1ea9f325f94..9fecaea6c298 100644 --- a/security/keys/dh.c +++ b/security/keys/dh.c @@ -183,24 +183,13 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen, goto err; } - if (dlen < h) { - u8 tmpbuffer[h]; - - err = crypto_shash_final(desc, tmpbuffer); - if (err) - goto err; - memcpy(dst, tmpbuffer, dlen); - memzero_explicit(tmpbuffer, h); - return 0; - } else { - err = crypto_shash_final(desc, dst); - if (err) - goto err; + err = crypto_shash_final(desc, dst); + if (err) + goto err; - dlen -= h; - dst += h; - counter = cpu_to_be32(be32_to_cpu(counter) + 1); - } + dlen -= h; + dst += h; + counter = cpu_to_be32(be32_to_cpu(counter) + 1); } return 0; @@ -216,14 +205,16 @@ static int keyctl_dh_compute_kdf(struct kdf_sdesc *sdesc, { uint8_t *outbuf = NULL; int ret; + size_t outbuf_len = round_up(buflen, + crypto_shash_digestsize(sdesc->shash.tfm)); - outbuf = kmalloc(buflen, GFP_KERNEL); + outbuf = kmalloc(outbuf_len, GFP_KERNEL); if (!outbuf) { ret = -ENOMEM; goto err; } - ret = kdf_ctr(sdesc, kbuf, kbuflen, outbuf, buflen, lzero); + ret = kdf_ctr(sdesc, kbuf, kbuflen, outbuf, outbuf_len, lzero); if (ret) goto err;