All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Hou Wenlong" <houwenlong.hwl@antgroup.com>
To: kvm@vger.kernel.org
Subject: [PATCH 0/2] KVM: x86/emulator: Fix wrong checks when loading code segment in emulator
Date: Thu, 20 Jan 2022 17:33:28 +0800	[thread overview]
Message-ID: <cover.1642669684.git.houwenlong.hwl@antgroup.com> (raw)

Per Intel's SDM on "Instruction Set Reference", code segment
can be loaded by far jmp/call/ret, iret and int. For all those
instructions, not-present segment check should be after type and
privilege checks. But the emulator checks it first, so #NP is
triggered instead of #GP if privilege check fails and the segment
is not present.

When loading code segment above realmode, RPL/CPL/DPL should be
checked, but the privilege checks are different between those
instructions. Since iret and int are only implemented for realmode
in emulator, no checks ared needed.

The current implement only checks if DPL > CPL for conforming
code or (RPL > CPL or DPL != CPL) for non-conforming code. Since
far call/jump to call gate, task gate and task state segment are
not implemented for in emulator, the current checks are enough.

As for far return, outer level return is not implemented above
virtual-8086 mode in emulator, so RPL <= CPL. Per Intel's SDM,
if RPL < CPL, it should trigger #GP, but it is missing in
emulator. Other checks are satisfied in current implementation.

When vmexit for task switch, code segment would also be loaded
from tss. Since segment selector is loaded before segment descriptor
when load state from tss, it implies that RPL = CPL, the checks
are satisfied too.

I add some tests in kvm-unit-tests[*] for the wrong checks in
emulator. Enable kvm.force_enable_emulation to test them on emulator.

[*] https://lore.kernel.org/kvm/cover.1642669912.git.houwenlong.hwl@antgroup.com

Hou Wenlong (2):
  KVM: x86/emulator: Defer not-present segment check in
    __load_segment_descriptor()
  KVM: x86: Fix wrong privilege check for code segment in
    __load_segment_descriptor()

 arch/x86/kvm/emulate.c | 44 +++++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 13 deletions(-)

--
2.31.1


             reply	other threads:[~2022-01-20  9:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-20  9:33 Hou Wenlong [this message]
2022-01-20  9:33 ` [PATCH 1/2] KVM: x86/emulator: Defer not-present segment check in __load_segment_descriptor() Hou Wenlong
2022-02-07 19:21   ` Sean Christopherson
2022-01-20  9:33 ` [PATCH 2/2] KVM: x86: Fix wrong privilege check for code segment " Hou Wenlong
2022-02-07 19:51   ` Sean Christopherson
2022-02-08  9:34 ` [PATCH v2 0/3] KVM: x86/emulator: Fix wrong checks when loading code segment in emulator Hou Wenlong
2022-02-08  9:34   ` [PATCH v2 1/3] KVM: x86/emulator: Defer not-present segment check in __load_segment_descriptor() Hou Wenlong
2022-02-08  9:34   ` [PATCH v2 2/3] KVM: x86/emulator: Fix wrong privilege check for code segment " Hou Wenlong
2022-02-08  9:34   ` [PATCH v2 3/3] KVM: x86/emulator: Move the unhandled outer privilege level logic of far return into __load_segment_descriptor() Hou Wenlong
2022-02-25 15:07     ` 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=cover.1642669684.git.houwenlong.hwl@antgroup.com \
    --to=houwenlong.hwl@antgroup.com \
    --cc=kvm@vger.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 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.