From: Paolo Bonzini <pbonzini@redhat.com>
To: "Radim Krčmář" <rkrcmar@redhat.com>,
"Nadav Amit" <namit@cs.technion.ac.il>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v2 2/5] KVM: x86: Emulator performs code segment checks on read access
Date: Sat, 11 Oct 2014 11:39:34 +0200 [thread overview]
Message-ID: <5438FAD6.3010805@redhat.com> (raw)
In-Reply-To: <20141010155455.GA17902@potion.brq.redhat.com>
Il 10/10/2014 17:54, Radim Krčmář ha scritto:
>> >
>> > One exception is the case of conforming code segment. The SDM says: "Use a
>> > code-segment override prefix (CS) to read a readable... [it is] valid because
>> > the DPL of the code segment selected by the CS register is the same as the
>> > CPL." This is misleading since CS.DPL may be lower (numerically) than CPL, and
>> > CS would still be accessible. The emulator should avoid privilage level checks
>> > for data reads using CS.
> Ah, after stripping faulty presumptions, I'm not sure this change is
> enough ... shouldn't we also skip the check on conforming code segments?
>
> Method 2 is always valid because the privilege level of a conforming
> code segment is effectively the same as the CPL, regardless of its DPL.
Radim is right; we need to skip the check on conforming code segments
and, once we do that, checking addr.seg is not necessary anymore. That
is because, for a CS override on a nonconforming code segment, at the
time we fetch the instruction we know that cpl == desc.dpl. The less
restrictive data segment check (cpl <= desc.dpl) thus always passes.
Let's put together this check and the readability check, too, since
we are adding another "if (fetch)".
Can you guys think of a way to simplify the following untested patch?
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 03954f7900f5..9f3e33551db9 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -638,9 +638,6 @@ static int __linearize(struct x86_emulate_ctxt *ctxt,
if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
|| !(desc.type & 2)) && write)
goto bad;
- /* unreadable code segment */
- if (!fetch && (desc.type & 8) && !(desc.type & 2))
- goto bad;
lim = desc_limit_scaled(&desc);
if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch &&
(ctxt->d & NoBigReal)) {
@@ -660,17 +657,40 @@ static int __linearize(struct x86_emulate_ctxt *ctxt,
goto bad;
}
cpl = ctxt->ops->cpl(ctxt);
- if (!(desc.type & 8)) {
- /* data segment */
+ if (fetch && (desc.type & 8)) {
+ if (!(desc.type & 4)) {
+ /* nonconforming code segment */
+ if (cpl != desc.dpl)
+ goto bad;
+ break;
+ } else {
+ /* conforming code segment */
+ if (cpl < desc.dpl)
+ goto bad;
+ break;
+ }
+ }
+
+ if (likely(!(desc.type & 8) || (desc.type & 6) == 2)) {
+ /*
+ * Data segment or readable, nonconforming code
+ * segment. The SDM mentions that access through
+ * a code-segment override prefix is always valid.
+ * This really only matters for conforming code
+ * segments (checked below, and always valid anyway):
+ * for nonconforming ones, cpl == desc.dpl was checked
+ * when fetching the instruction, meaning the following
+ * test will always pass too.
+ */
if (cpl > desc.dpl)
goto bad;
- } else if ((desc.type & 8) && !(desc.type & 4)) {
- /* nonconforming code segment */
- if (cpl != desc.dpl)
- goto bad;
- } else if ((desc.type & 8) && (desc.type & 4)) {
- /* conforming code segment */
- if (cpl < desc.dpl)
+ } else {
+ /*
+ * These are the (rare) cases that do not behave
+ * like data segments: nonreadable code segments (bad)
+ * and readable, conforming code segments (good).
+ */
+ if (!(desc.type & 2))
goto bad;
}
break;
next prev parent reply other threads:[~2014-10-11 9:39 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-02 22:10 [PATCH 0/5] KVM: x86: Various bug fixes Nadav Amit
2014-10-02 22:10 ` [PATCH 1/5] KVM: x86: Clear DR7.LE during task-switch Nadav Amit
2014-10-06 19:45 ` Radim Krčmář
2014-10-02 22:10 ` [PATCH 2/5] KVM: x86: Emulator performs code segment checks on read access Nadav Amit
2014-10-06 20:32 ` Radim Krčmář
2014-10-10 2:07 ` [PATCH v2 " Nadav Amit
2014-10-10 15:54 ` Radim Krčmář
2014-10-11 9:39 ` Paolo Bonzini [this message]
2014-10-12 6:57 ` Nadav Amit
2014-10-12 12:12 ` Paolo Bonzini
2014-10-12 23:15 ` Nadav Amit
2014-10-13 4:29 ` Paolo Bonzini
2014-10-13 11:31 ` Gleb Natapov
2014-10-19 16:07 ` Nadav Amit
2014-10-02 22:10 ` [PATCH 3/5] KVM: x86: Decoding guest instructions which cross page boundary may fail Nadav Amit
2014-10-06 20:50 ` Radim Krčmář
2014-10-07 9:15 ` Nadav Amit
2014-10-08 9:02 ` Paolo Bonzini
2014-10-02 22:10 ` [PATCH 4/5] KVM: vmx: Unavailable DR4/5 is checked before CPL Nadav Amit
2014-10-06 19:33 ` Radim Krčmář
2014-10-02 22:10 ` [PATCH 5/5] KVM: x86: Using TSC deadline may cause multiple interrupts by user writes Nadav Amit
2014-10-06 20:57 ` Radim Krčmář
2014-10-07 9:35 ` Nadav Amit
2014-10-08 10:06 ` Radim Krčmář
2014-10-08 10:07 ` Paolo Bonzini
2014-10-10 1:55 ` Nadav Amit
2014-10-10 9:45 ` Paolo Bonzini
2014-10-10 12:50 ` Radim Krčmář
2014-10-10 12:51 ` Nadav Amit
2014-10-10 13:55 ` Paolo Bonzini
2014-10-10 14:02 ` Paolo Bonzini
2014-10-08 9:29 ` Paolo Bonzini
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=5438FAD6.3010805@redhat.com \
--to=pbonzini@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=namit@cs.technion.ac.il \
--cc=rkrcmar@redhat.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).