From: Shu-Chun Weng <scw@google.com>
To: Laurent Vivier <laurent@vivier.eu>
Cc: Riku Voipio <riku.voipio@iki.fi>, qemu-devel@nongnu.org
Subject: Re: [PATCH] Fix unsigned integer underflow in fd-trans.c
Date: Fri, 18 Oct 2019 11:27:11 -0700 [thread overview]
Message-ID: <CAF3nBxicPpCjTN1M581bbxqT6y7KupG4GW4-4m0v0JkiHR8Yww@mail.gmail.com> (raw)
In-Reply-To: <4b34aa24-8669-7713-84f3-07e9e8400734@vivier.eu>
[-- Attachment #1: Type: text/plain, Size: 3089 bytes --]
(Re-sending to the list because I forgot to turn off HTML before and
it was bounced.)
That does prevent the integer underflow, but it also changes the
behavior and I don't think the new behavior is desirable.
If the extra payload has a smaller alignment than the header, it makes
sense for the user program to generate a nlmsg_len that is not a
multiple of the alignment. When it's the last entry, the new condition
will it because NLMSG_ALIGN pushes the aligned length over `len`, yet
the single entry processing function won't actually read beyond the
buffer as long as it's bounded by nlmsg_len.
Shu-Chun
On Fri, Oct 18, 2019 at 12:26 AM Laurent Vivier <laurent@vivier.eu> wrote:
>
> Le 18/10/2019 à 02:19, Shu-Chun Weng a écrit :
> > In any of these `*_for_each_*` functions, the last entry in the buffer (so the
> > "remaining length in the buffer" `len` is equal to the length of the
> > entry `nlmsg_len`/`nla_len`/etc) has size that is not a multiple of the
> > alignment, the aligned lengths `*_ALIGN(*_len)` will be greater than `len`.
> > Since `len` is unsigned (`size_t`), it underflows and the loop will read
> > pass the buffer.
> >
> > This may manifest as random EINVAL or EOPNOTSUPP error on IO or network
> > system calls.
> >
> > Signed-off-by: Shu-Chun Weng <scw@google.com>
> > ---
> > linux-user/fd-trans.c | 51 +++++++++++++++++++++++++++++++++----------
> > 1 file changed, 40 insertions(+), 11 deletions(-)
> >
> > diff --git a/linux-user/fd-trans.c b/linux-user/fd-trans.c
> > index 60077ce531..9b92386abf 100644
> > --- a/linux-user/fd-trans.c
> > +++ b/linux-user/fd-trans.c
> > @@ -279,6 +279,7 @@ static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
> > (struct nlmsghdr *))
> > {
> > uint32_t nlmsg_len;
> > + uint32_t aligned_nlmsg_len;
> > abi_long ret;
> >
> > while (len > sizeof(struct nlmsghdr)) {
> > @@ -312,8 +313,13 @@ static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
> > break;
> > }
> > tswap_nlmsghdr(nlh);
> > - len -= NLMSG_ALIGN(nlmsg_len);
> > - nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len));
> > +
> > + aligned_nlmsg_len = NLMSG_ALIGN(nlmsg_len);
> > + if (aligned_nlmsg_len >= len) {
> > + break;
> > + }
> > + len -= aligned_nlmsg_len;
> > + nlh = (struct nlmsghdr *)(((char*)nlh) + aligned_nlmsg_len);
> > }
> > return 0;
> > }
>
> Nice catch.
>
> But the first "if" in the loop is already here for that, we only need to
> fix it with something like that in all the for_each functions:
>
> @@ -285,7 +285,7 @@ static abi_long host_to_target_for_each_nlmsg(struct
> nlmsghdr *nlh,
>
> nlmsg_len = nlh->nlmsg_len;
> if (nlmsg_len < sizeof(struct nlmsghdr) ||
> - nlmsg_len > len) {
> + NLMSG_ALIGN(nlmsg_len) > len) {
> break;
> }
>
> Thanks,
> Laurent
>
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4837 bytes --]
next prev parent reply other threads:[~2019-10-18 18:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-18 0:19 [PATCH] Fix unsigned integer underflow in fd-trans.c Shu-Chun Weng
2019-10-18 7:26 ` Laurent Vivier
2019-10-18 18:18 ` Shu-Chun Weng
2019-10-18 18:27 ` Shu-Chun Weng [this message]
2019-10-18 18:54 ` Laurent Vivier
2019-10-21 9:34 ` Laurent Vivier
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=CAF3nBxicPpCjTN1M581bbxqT6y7KupG4GW4-4m0v0JkiHR8Yww@mail.gmail.com \
--to=scw@google.com \
--cc=laurent@vivier.eu \
--cc=qemu-devel@nongnu.org \
--cc=riku.voipio@iki.fi \
/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).