From: Sean Christopherson <seanjc@google.com>
To: Kees Cook <keescook@chromium.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jim Mattson <jmattson@google.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
kvm@vger.kernel.org, Joerg Roedel <joro@8bytes.org>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] KVM: x86/emulator: Bounds check reg nr against reg array size
Date: Fri, 20 May 2022 18:48:41 +0000 [thread overview]
Message-ID: <YofiiZZu4/ja3C5R@google.com> (raw)
In-Reply-To: <20220520165705.2140042-1-keescook@chromium.org>
On Fri, May 20, 2022, Kees Cook wrote:
> GCC 12 sees that it might be possible for "nr" to be outside the _regs
> array. Add explicit bounds checking.
>
> In function 'reg_read',
> inlined from 'reg_rmw' at ../arch/x86/kvm/emulate.c:266:2:
> ../arch/x86/kvm/emulate.c:254:27: warning: array subscript 32 is above array bounds of 'long unsigned int[17]' [-Warray-bounds]
> 254 | return ctxt->_regs[nr];
> | ~~~~~~~~~~~^~~~
> In file included from ../arch/x86/kvm/emulate.c:23:
> ../arch/x86/kvm/kvm_emulate.h: In function 'reg_rmw':
> ../arch/x86/kvm/kvm_emulate.h:366:23: note: while referencing '_regs'
> 366 | unsigned long _regs[NR_VCPU_REGS];
> | ^~~~~
>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
> Cc: Wanpeng Li <wanpengli@tencent.com>
> Cc: Jim Mattson <jmattson@google.com>
> Cc: x86@kernel.org
> Cc: "H. Peter Anvin" <hpa@zytor.com>
> Cc: kvm@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> arch/x86/kvm/emulate.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
> index 89b11e7dca8a..fbcbc012a3ae 100644
> --- a/arch/x86/kvm/emulate.c
> +++ b/arch/x86/kvm/emulate.c
> @@ -247,6 +247,8 @@ enum x86_transfer_type {
>
> static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
> {
> + if (WARN_ON(nr >= ARRAY_SIZE(ctxt->_regs)))
> + return 0;
> if (!(ctxt->regs_valid & (1 << nr))) {
> ctxt->regs_valid |= 1 << nr;
> ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
> @@ -256,6 +258,8 @@ static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
>
> static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
> {
> + if (WARN_ON(nr >= ARRAY_SIZE(ctxt->_regs)))
> + return 0;
This is wrong, reg_write() confusingly returns a pointer the register to be written,
it doesn't actually do the write. So if we want to guard against array overflow,
it would be better to cap @nr and continue on, i.e. assume some higher bit was
spuriously set.
The other oddity here is that VCPU_REGS_RIP should never be read, the RIP relative
code reads _eip directly. I.e. _regs[] should really be VCPU_REGS_R15+1. And
adding a #define for that would clean up this bit of code in writeback_registers()
that hardcodes 16 (rax - r15) GPRs:
for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
Lastly, casting regs_dirty to an unsigned long pointer is all kinds of gross, e.g.
if it were moved to the end of struct x86_emulate_ctxt then the above could trigger
an out-of-bounds read.
I'll whip up a small series to clean this code up and add WARNs similar to above.
prev parent reply other threads:[~2022-05-20 18:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-20 16:57 [PATCH] KVM: x86/emulator: Bounds check reg nr against reg array size Kees Cook
2022-05-20 17:32 ` Sean Christopherson
2022-05-20 18:19 ` Kees Cook
2022-05-20 18:48 ` Sean Christopherson [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=YofiiZZu4/ja3C5R@google.com \
--to=seanjc@google.com \
--cc=hpa@zytor.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=keescook@chromium.org \
--cc=kvm@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
--cc=x86@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox