From patchwork Tue Mar 13 04:29:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tycho Andersen X-Patchwork-Id: 10277767 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 631E46038F for ; Tue, 13 Mar 2018 04:30:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D05028C47 for ; Tue, 13 Mar 2018 04:30:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 619D228D17; Tue, 13 Mar 2018 04:30:14 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, 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 D728028C47 for ; Tue, 13 Mar 2018 04:30:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750860AbeCMEaM (ORCPT ); Tue, 13 Mar 2018 00:30:12 -0400 Received: from mail-it0-f65.google.com ([209.85.214.65]:55257 "EHLO mail-it0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751483AbeCME3g (ORCPT ); Tue, 13 Mar 2018 00:29:36 -0400 Received: by mail-it0-f65.google.com with SMTP id c11so14031690ith.4 for ; Mon, 12 Mar 2018 21:29: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=7elmkvTp7D2Y1qk9MSRAVsX+pNnrR9P0PXvPX04BVgc=; b=EHR72MOvvWBjSPt+OJVJLDCRVjLwDMAHF8tTAM7Y6/yPECxdMLqyPQqFSeVOHm2LjE Igvor20zc2PuLAl/68p90n+aAdCwW4OuPzmW03g5rmpirUpncJvEk/C/rPZI1znZoz6c Y705AmFlXz9g+ogqxO4BKp32KCPNCxnlgJIyUxR0b+LlrGbo+QL52cZlrUk+rxKH3WTa b8ypwvrDJ6mBSKk4F6JbE8XFpyr/gZ5Sp/Dn+rY3Ccts5kAoTZ4yHXfljyXKIVi7qc8W IBmowcXKfR9Jrkou4bAiM3xd2jaH8JIgVc+V98Mn/L260hD3IcI/ACf9EZlvag8+aak+ 40JQ== 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=7elmkvTp7D2Y1qk9MSRAVsX+pNnrR9P0PXvPX04BVgc=; b=PJ4mhwjbR+xcRVY2c6OSUoMXtpU2Qdp+oWehwVttZbiU7sc5vcnnYz97TS/G7hfCwl cBs3gtXKUekHWqPRZ+q2YN+tvjbWVLjVBmUp/NufYYMOWS31dmMbaf9vDOLkxQsKjbpS 2ADY8knr/qiKoFapY6onaUqewaH+exGJ6x5qU1RYs4zMsMEt6R72de863rQbQ0MKbX2p lqCs1he/92oC40nP2emdk9iD9hITlyuPW3jM7zJ4R3C4AcyC2g4fgPkGrovavptiA7yV hboDIUqZ8PIlHO9HctpSM++ulFOcHP+LlDAmL+89m64kxok7v7PlIWRoIEwo6b+RzogA ZKiA== X-Gm-Message-State: AElRT7FwsIobHG8aXMDnPLNr5D3xNjoYrcMYWspHevwaDKh+mLfzlJdp FLaGGxR6qiOPkaG3I0BzLCI15w== X-Google-Smtp-Source: AG47ELtJZ9B/T0yan2qWhBrzSYdzyId91nvl3fmB+A6lXOlMzyzXhXbR7omrv1Gdk1FO95DQNab2IA== X-Received: by 10.36.170.6 with SMTP id b6mr11150178itf.148.1520915375204; Mon, 12 Mar 2018 21:29:35 -0700 (PDT) Received: from localhost.localdomain ([8.24.24.129]) by smtp.gmail.com with ESMTPSA id z67sm6279356ioz.37.2018.03.12.21.29.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Mar 2018 21:29: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" Subject: [PATCH 2/2] dh key: get rid of stack array allocation Date: Mon, 12 Mar 2018 22:29:07 -0600 Message-Id: <20180313042907.29598-2-tycho@tycho.ws> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180313042907.29598-1-tycho@tycho.ws> References: <20180313042907.29598-1-tycho@tycho.ws> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Similarly to the previous patch, we would like to get rid of stack allocated arrays: https://lkml.org/lkml/2018/3/7/621 In this case, we can also use a malloc style approach to free the temporary buffer, being careful to also use kzfree to free them (indeed, at least one of these has a memzero_explicit, but it seems like maybe they both should?). Signed-off-by: Tycho Andersen CC: David Howells CC: James Morris CC: "Serge E. Hallyn" Acked-by: Serge Hallyn --- security/keys/dh.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/security/keys/dh.c b/security/keys/dh.c index d1ea9f325f94..f02261b24759 100644 --- a/security/keys/dh.c +++ b/security/keys/dh.c @@ -162,19 +162,27 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen, goto err; if (zlen && h) { - u8 tmpbuffer[h]; + u8 *tmpbuffer; size_t chunk = min_t(size_t, zlen, h); - memset(tmpbuffer, 0, chunk); + + err = -ENOMEM; + tmpbuffer = kzalloc(chunk, GFP_KERNEL); + if (!tmpbuffer) + goto err; do { err = crypto_shash_update(desc, tmpbuffer, chunk); - if (err) + if (err) { + kzfree(tmpbuffer); goto err; + } zlen -= chunk; chunk = min_t(size_t, zlen, h); } while (zlen); + + kzfree(tmpbuffer); } if (src && slen) { @@ -184,13 +192,20 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen, } if (dlen < h) { - u8 tmpbuffer[h]; + u8 *tmpbuffer; + + err = -ENOMEM; + tmpbuffer = kzalloc(h, GFP_KERNEL); + if (!tmpbuffer) + goto err; err = crypto_shash_final(desc, tmpbuffer); - if (err) + if (err) { + kzfree(tmpbuffer); goto err; + } memcpy(dst, tmpbuffer, dlen); - memzero_explicit(tmpbuffer, h); + kzfree(tmpbuffer); return 0; } else { err = crypto_shash_final(desc, dst);