Message ID | Y6FjQPZiJYTEG1zI@gondor.apana.org.au |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2821709wrn; Mon, 19 Dec 2022 23:28:04 -0800 (PST) X-Google-Smtp-Source: AMrXdXscSCDd+QLh9j9sMav7ILBBTkVHKJ2XX8rSahkKbw9NR0ID9Oc/5bIp7UWfAG7nbXjZPXME X-Received: by 2002:a17:906:414c:b0:7c0:d606:c570 with SMTP id l12-20020a170906414c00b007c0d606c570mr1132418ejk.61.1671521284211; Mon, 19 Dec 2022 23:28:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671521284; cv=none; d=google.com; s=arc-20160816; b=bfpo25uAltirZnNibZi+pjk+ngRMOAkskUxAg9Arh6G8i0teQ5AQftUgUDINfe2ZKM 9YRX81ZevR2NZMp+WWK3rYhcJ/FKp1baUT8rRvVt8zMFCmFyr8Sqfhn0tUKd/PJl+Fj4 MyzhVdajN9oA8tEiR0z2C83dmWHIR5QBxjs1pEO6m2KtkiXWwzCL68nv5PqbWTX7CR7U xG9j+TL2k6/Qloj3dtEzaFX/UgN5SdXOzQnMnvjGhR5zhio9ca2r2hhLoh47phnXsBbO achwlSB9nrqYrvfsRKBp6WKEVqa1DHQDaTzgLw4OgrhcNV1EwoUnKnGLz/COKK6jyWmL NgMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=XF5IRexiJ0Y3vQEtvPZA7DY/GeEg5PqLPBbmZeZ3cmw=; b=KxgYqv/sDxXmCokuGKDE4UzcRAJzYML51uHljH9OLEcIYb/wfo2srskOO4rXnt1d7k YG/HVQY947jb54qRaJAVGagLskcGopPeg6bADwKPAgROyb3jZQMavDdYi8d8ZQ3+IV20 AEEaoo0YZeolhl104TGTyzJMpwH2bSVzL8eUfdYXJU/iQky8teSQKTo4ziXKis3fVxng h/SE+K7DOCgYkeMFgZroP55UHr62jCbXd8Le27ILjMxv2vmNccRSg5eAaIgx7bUW9Bxi jIIz/0wZsQnKX+61c/b3ZTZAf0AcEWz7Yh1AYlo+YLnEqZsCowpL7VoFk4yLEtOa753L MhPA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qf39-20020a1709077f2700b007edf5234461si9733601ejc.645.2022.12.19.23.27.41; Mon, 19 Dec 2022 23:28:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232897AbiLTHZl (ORCPT <rfc822;abdi.embedded@gmail.com> + 99 others); Tue, 20 Dec 2022 02:25:41 -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 <rfc822;linux-kernel@vger.kernel.org>); 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 <herbert@gondor.apana.org.au> To: Roberto Sassu <roberto.sassu@huaweicloud.com> Cc: Eric Biggers <ebiggers@kernel.org>, 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 <roberto.sassu@huawei.com>, Tadeusz Struk <tadeusz.struk@intel.com> Subject: [PATCH] lib/mpi: Fix buffer overrun when SG is too long Message-ID: <Y6FjQPZiJYTEG1zI@gondor.apana.org.au> References: <20221209150633.1033556-1-roberto.sassu@huaweicloud.com> <Y5OGr59A9wo86rYY@sol.localdomain> <fa8a307541735ec9258353d8ccb75c20bb22aafe.camel@huaweicloud.com> <Y5bxJ5UZNPzxwtoy@gondor.apana.org.au> <0f80852578436dbba7a0fce03d86c3fa2d38c571.camel@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0f80852578436dbba7a0fce03d86c3fa2d38c571.camel@huaweicloud.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752717102517925974?= X-GMAIL-MSGID: =?utf-8?q?1752717102517925974?= |
Series |
lib/mpi: Fix buffer overrun when SG is too long
|
|
Commit Message
Herbert Xu
Dec. 20, 2022, 7:24 a.m. UTC
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 <roberto.sassu@huaweicloud.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Comments
On 12/20/2022 8:24 AM, Herbert Xu wrote: > 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? Tried, could not boot the UML kernel. After looking, it seems we have to call sg_miter_stop(). Or alternatively, we could let sg_miter_next() be called but not writing anything inside the loop. With either of those fixes, the tests pass (using one scatterlist). Roberto > ---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 <roberto.sassu@huaweicloud.com> > Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> > > 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;
On Tue, Dec 20, 2022 at 11:36:50AM +0100, Roberto Sassu wrote: > On 12/20/2022 8:24 AM, Herbert Xu wrote: > > 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? > > Tried, could not boot the UML kernel. > > After looking, it seems we have to call sg_miter_stop(). Or alternatively, > we could let sg_miter_next() be called but not writing anything inside the > loop. > > With either of those fixes, the tests pass (using one scatterlist). > > Roberto > > > ---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 <roberto.sassu@huaweicloud.com> > > Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> > > > > 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; I think it should look like: while (nbytes) { sg_miter_next(&miter); ... } sg_miter_stop(&miter); - Eric
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;