Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1188510ybn; Wed, 2 Oct 2019 12:09:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqymzqSnIZhqhHagSnPfZevJ4AugmHaE4MBHSS3w9Z3Wiwr+I8CbiRt1x4KkWsYAkIUa0VLs X-Received: by 2002:a50:ac03:: with SMTP id v3mr5573149edc.113.1570043396040; Wed, 02 Oct 2019 12:09:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570043396; cv=none; d=google.com; s=arc-20160816; b=RT9czOyBOCxeMQQ+LuTlFWbKLNM2yZJTD/blvFGBBH31gqQQA/r5S1+XhnRrMSoTa7 FVSL+fKkZtc6WYfK6xOduP0lyukGVqt+pGTlgle9ISx0iwCO9kWwJL2p6JCnkS5S8phR WydCzXgOBYiNypgIDlHPaEiOa93Yz6WJd/LfOjYuVWTr3O3mA6HfWhPAXEjVeb4ISihs hz+pxObw7AOL40Ts4EowOkngvlzpwrK9CyRbNqOHwr+wJQmhLj5MW333cKfwTzuy0ODq KmRVdGKEwSvrs3qJUx10PCNhy48RA300nV0yD8diQEo8DFHKKfqJQ645zFNYN0M1fR3q Wn9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=1DnLeHx2W8jN5PhZe5lJL+JiKNmHBXszfCNIifWCf2M=; b=lIK9ouYzw10fYVdI6zjSsjK+4PdagN3k1lXgoRIDTDCB4qW3TPGBXKebRrpXGPUEQo aEH/DcKWbDaI6FnQoVjMA8GQmunz/TCoHT+Dxzd+YqKhIRahSixCwzidxdsSW8jC6+oP Nj1vdmgY0Lm7D5vqAbJnTx5MWJoKvmErtvPZbXJAg28lvSJgdjYkrX0+sxeNhp8Urz3Z 5w/qibz768QtjU1mmKzSH7iHeAHUd1JXURrInAQEALlqxtKTYmKCT6P2YfmeRX2OOXkT MiPm0F7CCjyaKZn8O8spnZdXQiV5VhqZlrQTL7qRj1WSGOj1NPGSZfuQ1LChixSFxbCg PFKQ== ARC-Authentication-Results: i=1; mx.google.com; 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 s21si16234ejm.122.2019.10.02.12.09.31; Wed, 02 Oct 2019 12:09:56 -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; 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 S1729486AbfJBTIc (ORCPT + 99 others); Wed, 2 Oct 2019 15:08:32 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:35836 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729307AbfJBTIQ (ORCPT ); Wed, 2 Oct 2019 15:08:16 -0400 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iFjyt-00035n-SX; Wed, 02 Oct 2019 20:08:12 +0100 Received: from ben by deadeye with local (Exim 4.92.1) (envelope-from ) id 1iFjyq-0003hB-SF; Wed, 02 Oct 2019 20:08:08 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, Denis Kirjanov , "Herbert Xu" , "Eric Biggers" , syzbot+f7baccc38dcc1e094e77@syzkaller.appspotmail.com Date: Wed, 02 Oct 2019 20:06:51 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 86/87] lib/mpi: Fix karactx leak in mpi_powm In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.75-rc1 review patch. If anyone has any objections, please let me know. ------------------ 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...") Signed-off-by: Herbert Xu Reviewed-by: Eric Biggers Signed-off-by: Herbert Xu Signed-off-by: Ben Hutchings --- 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 @@ -36,6 +36,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; @@ -293,8 +292,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) { @@ -311,6 +308,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)