All of lore.kernel.org
 help / color / mirror / Atom feed
From: Klaus Jensen <its@irrelevant.dk>
To: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Cc: Qiuhao Li <Qiuhao.Li@outlook.com>,
	Mauro Matteo Cascella <mcascell@redhat.com>,
	qemu-block@nongnu.org, qemu-devel@nongnu.org,
	qemu-stable@nongnu.org, Keith Busch <kbusch@kernel.org>
Subject: Re: [PATCH-for-6.2] hw/nvme/ctrl: Fix buffer overrun (CVE-2021-3947)
Date: Thu, 11 Nov 2021 21:42:25 +0100	[thread overview]
Message-ID: <YY2AMcQ/KU0RrSqG@apples.localdomain> (raw)
In-Reply-To: <2d711ff2-2280-a283-7206-0b5bd1db4bad@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2621 bytes --]

On Nov 11 19:46, Philippe Mathieu-Daudé wrote:
> On 11/11/21 19:08, Klaus Jensen wrote:
> > On Nov 11 16:31, Philippe Mathieu-Daudé wrote:
> >> Both 'buf_len' and 'off' arguments are under guest control.
> >> Since nvme_c2h() doesn't check out of boundary access, the
> >> caller must check for eventual buffer overrun on 'trans_len'.
> >>
> >> Cc: qemu-stable@nongnu.org
> >> Reported-by: Qiuhao Li <Qiuhao.Li@outlook.com>
> >> Fixes: f432fdfa121 ("support changed namespace asynchronous event")
> >> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> >> ---
> >>  hw/nvme/ctrl.c | 22 ++++++++++++----------
> >>  1 file changed, 12 insertions(+), 10 deletions(-)
> >>
> >> diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
> >> index 6a571d18cfa..634b290e069 100644
> >> --- a/hw/nvme/ctrl.c
> >> +++ b/hw/nvme/ctrl.c
> >> @@ -4072,7 +4072,8 @@ static uint16_t nvme_smart_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
> >>      NvmeNamespace *ns;
> >>      time_t current_ms;
> >>  
> >> -    if (off >= sizeof(smart)) {
> >> +    trans_len = MIN(sizeof(smart) - off, buf_len);
> >> +    if (trans_len >= sizeof(smart)) {
> >>          return NVME_INVALID_FIELD | NVME_DNR;
> >>      }
> >>  
> >> @@ -4094,7 +4095,6 @@ static uint16_t nvme_smart_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
> >>          }
> >>      }
> >>  
> >> -    trans_len = MIN(sizeof(smart) - off, buf_len);
> >>      smart.critical_warning = n->smart_critical_warning;
> >>  
> >>      smart.data_units_read[0] = cpu_to_le64(DIV_ROUND_UP(stats.units_read,
> > 
> > Uhm. Hehe.
> > 
> > This "fix" breaks all log pages. Take smart_info as an example. Say the
> > offset is zero and the buffer length is 512. The transfer length
> > (trans_len) then becomes 512 and it ends up returning Invalid Field in
> > Command because trans_len equals sizeof(smart).
> > 
> > Worse, this "fix" actually *introduce* oob's all over the place if
> > 
> >     off > sizeof(smart) && buf_len < sizeof(smart)
> > 
> > 
> > Example
> > 
> >    sizeof(smart) = 512
> >    off = 516 (must be dword aligned to get to this spot)
> >    buf_len = 4 (same, but is always aligned)
> >    => trans_len = min(512 - 516, 4) = 4
> >    if (1 >= 512) => false
> > 
> > And we end up with
> > 
> >    nvme_c2h(n, &smart + 516, 4, req);
> 
> Doh, I missed the '+ off' part in the nvme_c2h() call...
> 

You wanna send a v2 just adding the check on offset like the other log
pages, or was there a particular reason you think it could use a
refactor?

I'm open to whatever can make it safer! :)

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

      reply	other threads:[~2021-11-11 20:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-11 15:31 [PATCH-for-6.2] hw/nvme/ctrl: Fix buffer overrun (CVE-2021-3947) Philippe Mathieu-Daudé
2021-11-11 18:08 ` Klaus Jensen
2021-11-11 18:46   ` Philippe Mathieu-Daudé
2021-11-11 20:42     ` Klaus Jensen [this message]

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=YY2AMcQ/KU0RrSqG@apples.localdomain \
    --to=its@irrelevant.dk \
    --cc=Qiuhao.Li@outlook.com \
    --cc=kbusch@kernel.org \
    --cc=mcascell@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.