Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7150062ybi; Mon, 8 Jul 2019 15:39:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqy/KgsKTHNIiJewrMh80h9MtiFv8bqSsltpeejTbeaUWV7bWJ5ltruUNQwptsMixsolJZmA X-Received: by 2002:a63:e610:: with SMTP id g16mr15335141pgh.392.1562625544828; Mon, 08 Jul 2019 15:39:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562625544; cv=none; d=google.com; s=arc-20160816; b=XTSZzUd/9J7+g26TBolEjt2pWJLa/g5TvJuzTlUU3QYcEZNRde5UnF10qaLHm9y3Tb MzuWuwSnWkHndaQg5aSB8TJ3pXR+7DaA1rfpV0nOA8dCHVjvSo2t8D0+and1zdPX6TDk MwTrK70NGmEhcdZlPvNAHWV7XNyozOsLZvW4v03l0WnEX9B1ll0neXY+lpO8E9oP7vHi mLjqfxKuvrUA6ajKk0JyOi9BR1VCJlyGSSX7kCe3+gkULElsHtlt8kDWhJxo+Xu06TaX vp5dTf7vAIQEpADBj2sHhoAtMNcQa/A8mo028GX7Nj/ljk1d8e8pAGzV5cR1Fluvn+ix 2wCw== 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=MULbq1XVBKzCMJjNou6SmByaGRCzgzK94SepppWnR7A=; b=BJY2en4aD0CYeMEm+mJ/3FWp3Pp4vhFmsY5Mkft+5Rn18rsotwyGl7aOUFa/OKyg/d M0B/v2F2BuRKxOLXx/7IQlPg9E4yaCjJrVMHPXO793pGaLehljlpOwwaVEASSA+9QxMr 6jJG6Vm9pt7G5E+BUBzqtzjDq3kJ2AqEK5iGF5hjZ6W8NRHS79nJbhT37FuSnHQa8uss MjjgADVN110lLcjYPiCIkSCdTRo6ODfrLmfHvCF74wv358ZHUnNErNKxL861oCvBs3CP C2gTZ4Jt7ojh0Xz+5fPlrF1qYkDWIZVXGP2+/88b5OuGRwv0JLo2OxjS02RDKpF4EVrP uEsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=C5qixreJ; 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 a16si20388011pgi.120.2019.07.08.15.38.50; Mon, 08 Jul 2019 15:39:04 -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=C5qixreJ; 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 S2389293AbfGHP21 (ORCPT + 99 others); Mon, 8 Jul 2019 11:28:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:56906 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389277AbfGHP2Y (ORCPT ); Mon, 8 Jul 2019 11:28:24 -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 5533A21537; Mon, 8 Jul 2019 15:28:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562599703; bh=II54N9oyz9p4+uMfs8XurpWIeyvXpBd2PwxshOEByrs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C5qixreJixuIZ6SD0u+EUOYlla5g70gB//JeAgvFv8E0UGKCDVOHHCnpDZ95QfdQJ ivADcdXrj5PX4njU66MtvFqP3e+/2Gqi0avFkST/r1gRw3xMvAdZRUc4WKQM1GOhr8 CD9NzHpi5QaSJaggEzE9HqsqnUXBlsSs/LwpvbV4= 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.19 50/90] lib/mpi: Fix karactx leak in mpi_powm Date: Mon, 8 Jul 2019 17:13:17 +0200 Message-Id: <20190708150525.072211436@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190708150521.829733162@linuxfoundation.org> References: <20190708150521.829733162@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; @@ -163,13 +164,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; @@ -294,8 +293,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) { @@ -312,6 +309,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)