From patchwork Tue Apr 24 01:03:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tycho Andersen X-Patchwork-Id: 10358447 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 671EB60225 for ; Tue, 24 Apr 2018 01:03:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 57C0128B7F for ; Tue, 24 Apr 2018 01:03:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4C9C128CB3; Tue, 24 Apr 2018 01:03:55 +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=ham 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 E43B028B7F for ; Tue, 24 Apr 2018 01:03:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932775AbeDXBDw (ORCPT ); Mon, 23 Apr 2018 21:03:52 -0400 Received: from mail-ot0-f193.google.com ([74.125.82.193]:41015 "EHLO mail-ot0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932635AbeDXBDf (ORCPT ); Mon, 23 Apr 2018 21:03:35 -0400 Received: by mail-ot0-f193.google.com with SMTP id m22-v6so19378221otf.8 for ; Mon, 23 Apr 2018 18:03:35 -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=Y1Srcwxgk8usNYONuhg9ZlvLu1E0ctymNb8QaBmNeDSdcjFipR89uppRkH3M8uwINg FCvyASQk+yAFuxXPsVL8L+RFZ7Yqb/Nhhz89EQFjbQSJUolwtKc3jNJMic5Qgg3VUnjl ipIg5IgiSrDPTGxw+NL6GG7Mro8Z0t3UaskWQ/vem90W2e9cDQsw2ZY4yziOScxMy9lU 5qa+mzUBJ9d/EjGpredJMeJKGMSRbyp6uLtYBk2CuVtihaHHGTYmH5XOY4YRUXXEL+xI 2BoaM/DATIQF1IxB5+TeyX2RrfgU2i74cpJg4XY3Tmjz+knWXVhAVJqT0cBkmmZlQPF+ 2Y2A== 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=MZLunwpXR/0f7yazeTFdyFzuojwgHL/uVljuDc4lh0mcM/pD5H9O/YZD7vZLt7wc+y d4CSDvoSIp1bopkmWoam/GUnGzWrlLoUvMhfSHDQP707djMSwdyc4ylo0ER5/oIGtbOn vx5VJ4MGfhbJ8ofyUsO5m5hM+6tV/A0osMILEbijctLbxzi4bAGRajjbO9yvrSFvDx18 tRslz+GeWcDJ6NiDd0aARHQFuAPu/Go2WkUX2W74W0QNxCepXAo5bq2e01buFaUpIqUP k08mOy3qO4dXTeyIohyEqU0ZMjapRBccuJDQ0EZ9IBsPPG7s5Fb1i4L40ep5hQEHmQzz 7ZzQ== X-Gm-Message-State: ALQs6tDWqdX9M29YRsHJCaeur9drcQeBwyCT3PYghqkULcruZqyo0Cxg otS87Cbz/0ibISoj+pQCVAVEwg== X-Google-Smtp-Source: AIpwx4/uq1uM6s1/PKFvxoodIjY1F4EqpCN0mNT4dK+rOJFSgGOOBUBqCulT6sBRQdEu6nogFvQWQQ== X-Received: by 2002:a9d:48b3:: with SMTP id d48-v6mr15978729otf.399.1524531814650; Mon, 23 Apr 2018 18:03:34 -0700 (PDT) Received: from cisco.lan ([8.24.24.129]) by smtp.gmail.com with ESMTPSA id n204-v6sm7410331oia.3.2018.04.23.18.03.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Apr 2018 18:03:33 -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 2/3] dh key: get rid of stack allocated array Date: Mon, 23 Apr 2018 19:03:20 -0600 Message-Id: <20180424010321.14739-2-tycho@tycho.ws> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180424010321.14739-1-tycho@tycho.ws> References: <20180424010321.14739-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;