Received: by 10.192.165.148 with SMTP id m20csp4050090imm; Mon, 23 Apr 2018 18:05:32 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+5B5EZG3CLbdGtLM85HyxZGBy9fxiihBQoOlG1HxFgGW9Ja+0uCKnFB2u+ki4j1kOJNJDC X-Received: by 2002:a17:902:887:: with SMTP id 7-v6mr23351115pll.319.1524531932185; Mon, 23 Apr 2018 18:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524531932; cv=none; d=google.com; s=arc-20160816; b=ewYnbfyDW3FYh4dhJecJkmxtdwdHceC9yWP+Z+meIp29sYMA7QBLho4MU9QSP7U/xd VHULvmhvHnm94rzaKp16iLufh+cTjl5KqfnbqBykpTvHRr7G4gtWJgV2shbhJCTzuCjj m/j+gv4lhW7382kdWrcPshYbNTfM6iCseOYIUx9+DwXJ61KfH8XZ3Phl+UYi8kp597vZ +1OYHFs4fwghjWvThgAVRYnnCLo9Zxy/uhyLQpY3Ncz/p+/y9O9/U4IeXRfHwMMnTaJM OnIif5yWXidELSIaHk2eLmAyzDV6kUDMy+QRB4SzAtKxmxPyCkiFunYaK4sRLuHOhawd /TwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=SJErzKT9nbvMFCOiOIf+uqZIZJ+5ZpCdx6U/gxgdK3g=; b=tqIPbvxxxCNnr3q44z6afkSWmwVdpADcF0aqCRDovZA8zbvKDqD4ZWJIWdCW6ZKLfe 7RBcEbbgF6GhRjLJu0e3ZcWO7ogILrSF/Mxfmwr2eOL0qJ8e0GCkLQlSK77WRyvEUjES hZRKDTyRbQuFU7LeVz2Bz+c0G3OY7/l8ks0YdW3ciFeEuH+YZLUKbiP956YYs1tPgOyE EwMAgI0+uDjOvOdYxF6kgLXWdQ+99ZKPIO2UvIof1l2iY5iiR9t6PsITn/cB8hplnTMF Ht/FwQraw5YF5un3iNIzhq9Fbe3lPwU6ibcXqg2Q4sT5DXnSyhSL1ghj+Jj6SPimmvIa u5Og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho-ws.20150623.gappssmtp.com header.s=20150623 header.b=Y1Srcwxg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o61-v6si588991pld.71.2018.04.23.18.05.18; Mon, 23 Apr 2018 18:05:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@tycho-ws.20150623.gappssmtp.com header.s=20150623 header.b=Y1Srcwxg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932783AbeDXBDx (ORCPT + 99 others); Mon, 23 Apr 2018 21:03:53 -0400 Received: from mail-ot0-f196.google.com ([74.125.82.196]:33968 "EHLO mail-ot0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932716AbeDXBDf (ORCPT ); Mon, 23 Apr 2018 21:03:35 -0400 Received: by mail-ot0-f196.google.com with SMTP id i5-v6so19421978oth.1 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=O4fRE2MucinZvN8chcDYB/2JVSKq30cp73JUXyv+yffk1RM5O3f19c29/EZ0I3x2BV qKRY+l4k2ooi+6Ur7SJnza/uvKJzHY/Af7wkRC/aPYXuwsiUwHc5/Vvo+zmRKnLkyOyi FbpirQLEfL+yzRZ9kp/P3G5SdVJjRqmLzWvTeKeRRlxjZCcbWDHcz5hf/qQhTckhRTIA VxiYKDHsbSitlUcF6sl0KMayDoxAGw7LeSu2SvxuNQtnca3uEm/zkKKjj2i1DxDkZoDg 75e8xZA5G95/AODASDjxKjgI8psL2SdSP7EezogbLhLTLwsP3xvkGr+arrlYDkDnKxVP rcZw== X-Gm-Message-State: ALQs6tCRVOk7hvDn+smLQPLuXV4NBBGRCUy9umtCMKAOxrpy73pi10L6 r9j066VDXp1OEy8TMBSu4kU6fOG5 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: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.17.0