From patchwork Wed Feb 3 11:28:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniele Alessandrelli X-Patchwork-Id: 375537 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7D8FC433DB for ; Wed, 3 Feb 2021 11:32:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 80A8864F74 for ; Wed, 3 Feb 2021 11:32:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234339AbhBCLcJ (ORCPT ); Wed, 3 Feb 2021 06:32:09 -0500 Received: from mga05.intel.com ([192.55.52.43]:12058 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234120AbhBCLb5 (ORCPT ); Wed, 3 Feb 2021 06:31:57 -0500 IronPort-SDR: C4FTAmDKvOX3EIUiVCucro9Qx+Z9O4ZD4H/ue48SgypCL92ZwdtkJmpp4uOflcUHjZ8eqguYkn mIqhnI+3ONQA== X-IronPort-AV: E=McAfee;i="6000,8403,9883"; a="265858360" X-IronPort-AV: E=Sophos;i="5.79,398,1602572400"; d="scan'208";a="265858360" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 03:28:54 -0800 IronPort-SDR: x/1kVMMYJ/8NzLI3z6smp8outjpzQzfJ0+r1BucZkfqpTZYDQrTfVrdGb4ywWH5j5V7URucPt3 RHJaErMOR1Bg== X-IronPort-AV: E=Sophos;i="5.79,398,1602572400"; d="scan'208";a="371404556" Received: from dmarkey-mobl1.ger.corp.intel.com (HELO dalessan-mobl1.ir.intel.com) ([10.252.21.31]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 03:28:53 -0800 From: Daniele Alessandrelli To: herbert@gondor.apana.org.au Cc: daniele.alessandrelli@intel.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] crypto: ecdh_helper - Ensure 'len >= secret.len' in decode_key() Date: Wed, 3 Feb 2021 11:28:37 +0000 Message-Id: <20210203112837.203732-1-daniele.alessandrelli@linux.intel.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Daniele Alessandrelli The length ('len' parameter) passed to crypto_ecdh_decode_key() is never checked against the length encoded in the passed buffer ('buf' parameter). This could lead to an out-of-bounds access when the passed length is less than the encoded length. Add a check to prevent that. Signed-off-by: Daniele Alessandrelli --- crypto/ecdh_helper.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crypto/ecdh_helper.c b/crypto/ecdh_helper.c index 66fcb2ea8154..fca63b559f65 100644 --- a/crypto/ecdh_helper.c +++ b/crypto/ecdh_helper.c @@ -67,6 +67,9 @@ int crypto_ecdh_decode_key(const char *buf, unsigned int len, if (secret.type != CRYPTO_KPP_SECRET_TYPE_ECDH) return -EINVAL; + if (unlikely(len < secret.len)) + return -EINVAL; + ptr = ecdh_unpack_data(¶ms->curve_id, ptr, sizeof(params->curve_id)); ptr = ecdh_unpack_data(¶ms->key_size, ptr, sizeof(params->key_size)); if (secret.len != crypto_ecdh_key_len(params))