From patchwork Mon Nov 6 02:30:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10042537 X-Patchwork-Delegate: herbert@gondor.apana.org.au 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 95A6C601EB for ; Mon, 6 Nov 2017 02:31:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A3A2A28D7A for ; Mon, 6 Nov 2017 02:31:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9891128EF9; Mon, 6 Nov 2017 02:31:36 +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.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 3785528D7A for ; Mon, 6 Nov 2017 02:31:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751728AbdKFCbb (ORCPT ); Sun, 5 Nov 2017 21:31:31 -0500 Received: from mail-pf0-f193.google.com ([209.85.192.193]:44564 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751337AbdKFCbX (ORCPT ); Sun, 5 Nov 2017 21:31:23 -0500 Received: by mail-pf0-f193.google.com with SMTP id x7so6643971pfa.1; Sun, 05 Nov 2017 18:31:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bxxiWBdORTbB8TXDWgtk5EMNUEyiMLRZgvHgGFWKUEk=; b=mcl17fL1qNPNOwDwMHq4xNep0DUPxJxA9GAeGAMVCq732OaV+EVxjEZvl0J1puMZEP vQPmjuqr+1+ishb+SCOdMZgGH9Tm/Bk1nsrqaYdMO5s8qH2S9LULps3amxRIt15jhf7Q 3mn3Hirm5GdHE82D8Neu+NTa55EHwZ8PJsyBYFOzYSlJOTSpFV36IomKG1hlBSffLPVj x4lxWUAZ5rOBMfKQr/38+LKxenkAMJQU4y7fCd1uSxtElwbZnLY66U9LNUSlZ0hMZSGK d2XLFu7bamEI4gy1PZzNytyQ/DYs/mn0VBCh0kf8qlBRAYVGwPsw2UFo12S9Y9dx54PC 95MA== 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=bxxiWBdORTbB8TXDWgtk5EMNUEyiMLRZgvHgGFWKUEk=; b=aP4IDRoPf2U90KiqGNL8Wl8Egg+U1RKkVZPOEhm0Ug7rNdW22TzBxLDtZ4HcujOaas N4Kjw/LiknWBErODkNkLnP+qaSsLgSDOEafHAzLMBM9L2ezEaWs2PP51TepKGohLfve5 Kr1sNM2WctbQ9EcGItKog9oLksBV33y/v0bBNmUXfvP3mkEcNAKQY0Gh3HYzFTCgv8aX LuClFIwsGhKCmCFLTLlIu9MNUf7VuHjehRSwHixnTzJCsFTBx77hFowO7r1724IrlJLL Nja4oJaq/h2zHNWIjjlWEHc4AM7nD5ycDKFrlXK8RFQJzPasv9JYTI/9xtY5mNPmubCF feqQ== X-Gm-Message-State: AMCzsaUDBnqsVNeZ5jG/w0jEKlN3T7/HTVbdAU9P0S90F1Tm4S5YJTsv DHhusg9JqHQ9r9g7IAsfDS+TRdjr X-Google-Smtp-Source: ABhQp+R3dBQBMXt2ls72b1FQ3QTHXWA7DQ/PwrOqDTEwMQxdAvSIr5vVH7S1IVS/xwzeHpsPoyFAAw== X-Received: by 10.84.176.65 with SMTP id u59mr13516157plb.21.1509935482997; Sun, 05 Nov 2017 18:31:22 -0800 (PST) Received: from zzz.hsd1.wa.comcast.net (c-67-185-97-198.hsd1.wa.comcast.net. [67.185.97.198]) by smtp.gmail.com with ESMTPSA id e71sm23012520pfk.55.2017.11.05.18.31.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Nov 2017 18:31:22 -0800 (PST) From: Eric Biggers To: linux-crypto@vger.kernel.org, Herbert Xu Cc: Giovanni Cabiddu , Salvatore Benedetto , Tudor-Dan Ambarus , Mat Martineau , Stephan Mueller , qat-linux@intel.com, keyrings@vger.kernel.org, Eric Biggers , stable@vger.kernel.org Subject: [PATCH v2 3/5] crypto: dh - Don't permit 'key' or 'g' size longer than 'p' Date: Sun, 5 Nov 2017 18:30:46 -0800 Message-Id: <20171106023048.8067-4-ebiggers3@gmail.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171106023048.8067-1-ebiggers3@gmail.com> References: <20171106023048.8067-1-ebiggers3@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers The "qat-dh" DH implementation assumes that 'key' and 'g' can be copied into a buffer with size 'p_size'. However it was never checked that that was actually the case, which most likely allowed users to cause a buffer underflow via KEYCTL_DH_COMPUTE. Fix this by updating crypto_dh_decode_key() to verify this precondition for all DH implementations. Fixes: c9839143ebbf ("crypto: qat - Add DH support") Cc: # v4.8+ Signed-off-by: Eric Biggers Reviewed-by: Tudor Ambarus --- crypto/dh_helper.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crypto/dh_helper.c b/crypto/dh_helper.c index 708ae20d2d3c..7f00c771fe8d 100644 --- a/crypto/dh_helper.c +++ b/crypto/dh_helper.c @@ -83,6 +83,14 @@ int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params) if (secret.len != crypto_dh_key_len(params)) return -EINVAL; + /* + * Don't permit the buffer for 'key' or 'g' to be larger than 'p', since + * some drivers assume otherwise. + */ + if (params->key_size > params->p_size || + params->g_size > params->p_size) + return -EINVAL; + /* Don't allocate memory. Set pointers to data within * the given buffer */