From patchwork Tue Dec 20 07:24:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13077579 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBD27C10F1E for ; Tue, 20 Dec 2022 07:25:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233527AbiLTHZj (ORCPT ); Tue, 20 Dec 2022 02:25:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233557AbiLTHZT (ORCPT ); Tue, 20 Dec 2022 02:25:19 -0500 Received: from formenos.hmeau.com (helcar.hmeau.com [216.24.177.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0755315F04; Mon, 19 Dec 2022 23:25:12 -0800 (PST) Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.94.2 #2 (Debian)) id 1p7WzY-008mhE-TF; Tue, 20 Dec 2022 15:24:49 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Tue, 20 Dec 2022 15:24:48 +0800 Date: Tue, 20 Dec 2022 15:24:48 +0800 From: Herbert Xu To: Roberto Sassu Cc: Eric Biggers , dhowells@redhat.com, davem@davemloft.net, zohar@linux.ibm.com, dmitry.kasatkin@gmail.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Roberto Sassu , Tadeusz Struk Subject: [PATCH] lib/mpi: Fix buffer overrun when SG is too long Message-ID: References: <20221209150633.1033556-1-roberto.sassu@huaweicloud.com> <0f80852578436dbba7a0fce03d86c3fa2d38c571.camel@huaweicloud.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <0f80852578436dbba7a0fce03d86c3fa2d38c571.camel@huaweicloud.com> Precedence: bulk List-ID: X-Mailing-List: keyrings@vger.kernel.org On Mon, Dec 19, 2022 at 09:49:29AM +0100, Roberto Sassu wrote: > > do you have any news on this bug? Thanks for the reminder. Could you please try this patch? ---8<--- The helper mpi_read_raw_from_sgl ignores the second parameter nbytes when reading the SG list and may overrun its own buffer because it only allocates enough memory according to nbytes. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Reported-by: Roberto Sassu Signed-off-by: Herbert Xu diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 39c4c6731094..6bffc68c1a5a 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -494,17 +494,15 @@ MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) val->sign = 0; val->nlimbs = nlimbs; - if (nbytes == 0) - return val; - j = nlimbs - 1; a = 0; z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; z %= BYTES_PER_MPI_LIMB; - while (sg_miter_next(&miter)) { + while (nbytes && sg_miter_next(&miter)) { buff = miter.addr; - len = miter.length; + len = min_t(unsigned, miter.length, nbytes); + nbytes -= len; for (x = 0; x < len; x++) { a <<= 8;