Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7148958ybi; Mon, 8 Jul 2019 15:37:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqy3Q48Qt5ur0CumC3M6UVp08mCZY3qLz+becCcwlSTqT7s442rRKgdA5qFAz0gWlhdWnr2l X-Received: by 2002:a63:e53:: with SMTP id 19mr26279794pgo.137.1562625454485; Mon, 08 Jul 2019 15:37:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562625454; cv=none; d=google.com; s=arc-20160816; b=BbLw1sCMo6VMoJ97T6unc4qEthm4OPVofjUQ90bYdPcsm7yxIwSrS0Ek+W75MB9eis /HuMIaLVMZqpgdk8p+LA6P92fa3ZDCn1SySJwhq6PAsSe1u4wfAytms8IKrowhOmcpJP O5RaBQI2a6Ep0JAcwWoGNoPM4+LSDxV3B2wgfOy6C2ifVgUZ8o1LKT2YXVEVjZc90G0j 40/AwzbQalANmJGa78ZN4jV2RGt2YpkC5mt6gY6Lz/KQoaU9ixQ2aYPDs+QELZObXdjT 766IiUZ/ZrXPPDbNt3p0twK6/oxcViAh2yXbc2fZtaE7h9H2c/4Q0VeEs9HsDZZr0fZ7 kZ0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KGfSYi7afIcgbS3f1YrU/I8hUTnComeh0D2ayZAT8n8=; b=gil2eSwrmcxC3FeUjyBGSfKIGyLCE24Xwm/fxQ1OwxHljR8r6ggV8iBNM7ev4hhzrO R4OeWXZfaklqfwvFFsJr9EFE7CcrCeTCFb4mN9JHRKCjqnubyMRsTbLjpcKfIX+2td2k 2hcsbfwXzXkAawNOrlRhoJL1+7CbVWRz5/N0Wpeb1XbzXfGHr6UZOQoanmbpkglotx9w 9NyqbmBuUWnbWpYw7mcb3iDVPVYCpeJDTz3vICFjzUX4ClL6UkfPVJm+EQk2l9pYc191 Qg6eA59wQT/ppqImUr97GLFxTNym8LPbtIEOQtaemSajejTEtHy2S9WlFkMqCYeJw7fz AtBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VkSkVeoN; 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 b11si14158297plz.307.2019.07.08.15.37.19; Mon, 08 Jul 2019 15:37:34 -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=@kernel.org header.s=default header.b=VkSkVeoN; 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 S2388931AbfGHP0w (ORCPT + 99 others); Mon, 8 Jul 2019 11:26:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:52130 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729901AbfGHPYq (ORCPT ); Mon, 8 Jul 2019 11:24:46 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A0891204EC; Mon, 8 Jul 2019 15:24:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562599486; bh=XP/wjr61UkwKqFacjGA1z8OfOfSKKAteAPObbnI2bmo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VkSkVeoNFtPPMlojpSnRiOnh/+jJ634jJXQ3nWX1n1ruo0MVel0lOLg6qRlDawgqx sP4T4285vPk5ch5kfFXPh7TKTDUGs6vhU8HGSV2DVtY6Z4ymAbvsnGHlsiJBCXjjdi yX6PY+Eo+ALuNUpyB5OktnIB7olrNYhQk7otxhQo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+f7baccc38dcc1e094e77@syzkaller.appspotmail.com, Herbert Xu , Eric Biggers Subject: [PATCH 4.14 31/56] lib/mpi: Fix karactx leak in mpi_powm Date: Mon, 8 Jul 2019 17:13:23 +0200 Message-Id: <20190708150522.786651247@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190708150514.376317156@linuxfoundation.org> References: <20190708150514.376317156@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Herbert Xu commit c8ea9fce2baf7b643384f36f29e4194fa40d33a6 upstream. Sometimes mpi_powm will leak karactx because a memory allocation failure causes a bail-out that skips the freeing of karactx. This patch moves the freeing of karactx to the end of the function like everything else so that it can't be skipped. Reported-by: syzbot+f7baccc38dcc1e094e77@syzkaller.appspotmail.com Fixes: cdec9cb5167a ("crypto: GnuPG based MPI lib - source files...") Cc: Signed-off-by: Herbert Xu Reviewed-by: Eric Biggers Signed-off-by: Herbert Xu Signed-off-by: Greg Kroah-Hartman --- lib/mpi/mpi-pow.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) --- a/lib/mpi/mpi-pow.c +++ b/lib/mpi/mpi-pow.c @@ -37,6 +37,7 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod) { mpi_ptr_t mp_marker = NULL, bp_marker = NULL, ep_marker = NULL; + struct karatsuba_ctx karactx = {}; mpi_ptr_t xp_marker = NULL; mpi_ptr_t tspace = NULL; mpi_ptr_t rp, ep, mp, bp; @@ -164,13 +165,11 @@ int mpi_powm(MPI res, MPI base, MPI exp, int c; mpi_limb_t e; mpi_limb_t carry_limb; - struct karatsuba_ctx karactx; xp = xp_marker = mpi_alloc_limb_space(2 * (msize + 1)); if (!xp) goto enomem; - memset(&karactx, 0, sizeof karactx); negative_result = (ep[0] & 1) && base->sign; i = esize - 1; @@ -295,8 +294,6 @@ int mpi_powm(MPI res, MPI base, MPI exp, if (mod_shift_cnt) mpihelp_rshift(rp, rp, rsize, mod_shift_cnt); MPN_NORMALIZE(rp, rsize); - - mpihelp_release_karatsuba_ctx(&karactx); } if (negative_result && rsize) { @@ -313,6 +310,7 @@ int mpi_powm(MPI res, MPI base, MPI exp, leave: rc = 0; enomem: + mpihelp_release_karatsuba_ctx(&karactx); if (assign_rp) mpi_assign_limb_space(res, rp, size); if (mp_marker)