From: Eric Biggers <ebiggers@kernel.org>
To: Roberto Sassu <roberto.sassu@huaweicloud.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
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: Re: [PATCH] lib/mpi: Fix buffer overrun when SG is too long
Date: Tue, 20 Dec 2022 20:30:16 +0000 [thread overview]
Message-ID: <Y6IbWA5aZeBnn4n2@gmail.com> (raw)
In-Reply-To: <a04e6458-6814-97fc-f03a-617809e2e6ce@huaweicloud.com>
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
next prev parent reply other threads:[~2022-12-20 20:30 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-09 15:06 [PATCH v2] KEYS: asymmetric: Copy sig and digest in public_key_verify_signature() Roberto Sassu
2022-12-09 19:04 ` Eric Biggers
2022-12-12 9:07 ` Roberto Sassu
2022-12-12 9:15 ` Herbert Xu
2022-12-19 8:49 ` Roberto Sassu
2022-12-20 7:24 ` [PATCH] lib/mpi: Fix buffer overrun when SG is too long Herbert Xu
2022-12-20 10:36 ` Roberto Sassu
2022-12-20 20:30 ` Eric Biggers [this message]
2022-12-21 6:53 ` [v2 PATCH] " Herbert Xu
2022-12-21 20:53 ` Eric Biggers
2022-12-23 6:25 ` Herbert Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y6IbWA5aZeBnn4n2@gmail.com \
--to=ebiggers@kernel.org \
--cc=davem@davemloft.net \
--cc=dhowells@redhat.com \
--cc=dmitry.kasatkin@gmail.com \
--cc=herbert@gondor.apana.org.au \
--cc=jmorris@namei.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=paul@paul-moore.com \
--cc=roberto.sassu@huawei.com \
--cc=roberto.sassu@huaweicloud.com \
--cc=serge@hallyn.com \
--cc=tadeusz.struk@intel.com \
--cc=zohar@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).