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: 10358459 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 3290F6038F for ; Tue, 24 Apr 2018 01:31:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2848428CCA for ; Tue, 24 Apr 2018 01:31:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1CDB328CCD; Tue, 24 Apr 2018 01:31:53 +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=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, MAILING_LIST_MULTI, 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 ED5BB28CCA for ; Tue, 24 Apr 2018 01:31:51 +0000 (UTC) Received: (qmail 9891 invoked by uid 550); 24 Apr 2018 01:31:49 -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 9871 invoked from network); 24 Apr 2018 01:31:49 -0000 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=CKacuGXaPGQDHW/4g7MUAtcVI5Y5fYw8LPtgZXOZdW82bPPh8i/z4+iggDYWKUZ/Rj yz1FkZU2y+ECloV5lW+jHtuJWeUAWqdu5jXRzXu3tl5RAkviqYXtTJxzUdXz5fh14/3w N1H/mCNeWZTBP7823U2yNzggauCBWrhQzjUFtObebDOHejqrUL8ImqtMmMk8T9r8BDQa Myy1K4jEUcMlJs4U0aVVQ2BIs4AOdo3pUl1toBDW/xXaCR1pKkysTCowi+WO28j73IxU tT0+Qc56EzzJR1/QJH1dF8vefMwzTsMj/Jywpuq9mH6h/Sk5xX7pAcLp0tg6Me1ogzNU JZ3w== X-Gm-Message-State: ALQs6tAohOGcSU05IMkk6Jnakz38PzBLLurmON5NMCYwOqZfNTMpQ4HD 2zk+/f7Za7LOHUtj3ZKnhNYlOA== 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) 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> 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;