Skip to contentSkip to conversation

VM introspection

RFC PATCH Superseded Adalbert Lazar 4 participants 7 messages Sep 11, 2017 – Dec 4, 2017

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover VM introspection

The following patch adds the documentation for an introspection subsystem for KVM (KVMI). It details the purpose and the use case that has shaped the proposed API/ABI, as well as the wire protocol.

The previous RFC can be read here: https://marc.info/?l=kvm&m=149943806203155

In this iteration we consolidated the feedback received from Paolo and others. More discussion is needed on the topic of pausing the VM. For now we settled on a vCPU pause function that should be used to implement the former. This requires a bit of finesse in userspace but keeps a potentially complicated logic out of the kernel.

One other topic that we would like to bring again into discussion is that of page sharing between guests (the VM on which the introspection tool runs and the VM being introspected, provided they are on the same host).

This iteration of the document also tries to integrate #VE and multiple EPT views (VMFUNC). It does so simply by copying the ideas that have been discussed on xen-devel (see KVMI_CONTROL_VE and KVMI_{GET,SET}_PAGE_ACCESS).

Please note that some slight adjustments might still appear as we progress with the qemu side of things.

We have not included anything about IOMMU groups as we feel we don't yet have all the details to begin working on an API that deals with DMA attacks.

Changes since v2:
- make small changes to the wire protocol (eg. use kvmi_error_code with every command reply, a few renames, etc.)
- removed '_x86' from x86 specific structure names. Architecture specific structures will have the same name.
- drop KVMI_GET_MTRR_TYPE and KVMI_GET_MTRRS (use KVMI_SET_REGISTERS)
- drop KVMI_EVENT_ACTION_SET_REGS (use KVMI_SET_REGISTERS)
- remove KVMI_MAP_PHYSICAL_PAGE_TO_GUEST and KVMI_UNMAP_PHYSICAL_PAGE_FROM_GUEST (to be replaced by a token+hypercall pair)
- extend KVMI_GET_VERSION with allowed commnd/event masks
- replace KVMI_PAUSE_GUEST/KVMI_UNPAUSE_GUEST with KVMI_PAUSE_VCPU
- replace KVMI_SHUTDOWN_GUEST with KVMI_EVENT_ACTION_CRASH
- replace KVMI_GET_XSAVE_INFO with KVMI_GET_CPUID
- merge KVMI_INJECT_PAGE_FAULT and KVMI_INJECT_BREAKPOINT
in KVMI_INJECT_EXCEPTION
- replace event reply flags with ALLOW/SKIP/RETRY/CRASH actions
- make KVMI_SET_REGISTERS work with vCPU events only
- add EPT view support in KVMI_GET_PAGE_ACCESS/KVMI_SET_PAGE_ACCESS
- add support for multiple pages in KVMI_GET_PAGE_ACCESS/KVMI_SET_PAGE_ACCESS
- add (back) KVMI_READ_PHYSICAL/KVMI_WRITE_PHYSICAL
- add KVMI_CONTROL_VE
- add cstar to KVMI_EVENT
- add new events: KVMI_EVENT_VCPU_PAUSED, KVMI_EVENT_CREATE_VCPU, KVMI_EVENT_DESCRIPTOR_ACCESS, KVMI_EVENT_SINGLESTEP
- add new sections: "Introspection capabilities", "Live migrations", "Guest snapshots with memory", "Memory access safety"
- document the hypercall used by the KVMI_EVENT_HYPERCALL command (was KVMI_EVENT_USER_CALL)

Changes since v1:
- add documentation and ABI [Paolo, Jan]
- drop all the other patches for now [Paolo]
- remove KVMI_GET_GUESTS, KVMI_EVENT_GUEST_ON, KVMI_EVENT_GUEST_OFF, and let libvirt/qemu handle this [Stefan, Paolo]
- change the license from LGPL to GPL [Jan]
- remove KVMI_READ_PHYSICAL and KVMI_WRITE_PHYSICAL (not used anymore)
- make the interface a little more consistent

Adalbert Lazar (1):
kvm: add documentation for the VM introspection subsystem

 Documentation/virtual/kvm/hypercalls.txt |   32 +
 Documentation/virtual/kvm/kvmi.rst       | 1275 ++++++++++++++++++++++++++++++
 2 files changed, 1307 insertions(+)
 create mode 100644 Documentation/virtual/kvm/kvmi.rst
#1 lore raw

RFC PATCH v3 1/1 kvm: add documentation for the VM introspection subsystem

---
 Documentation/virtual/kvm/hypercalls.txt |   32 +
 Documentation/virtual/kvm/kvmi.rst       | 1275 ++++++++++++++++++++++++++++++
 2 files changed, 1307 insertions(+)
 create mode 100644 Documentation/virtual/kvm/kvmi.rst
Documentation/virtual/kvm/hypercalls.txt +32 -0
index a890529c63ed..4d35a859634e 100644
@@ -121,3 +121,35 @@ compute the CLOCK_REALTIME for its clock, at the same instant.
 
 Returns KVM_EOPNOTSUPP if the host does not use TSC clocksource,
 or if clock type is different than KVM_CLOCK_PAIRING_WALLCLOCK.
+
+7. KVM_HC_XEN_HVM_OP
+--------------------
+
+Architecture: x86
+Status: active
+Purpose: To enable communication between a guest agent and a VMI application
+Usage:
+
+An event will be sent to the VMI application (see kvmi.rst) if the following
+registers, which differ between 32bit and 64bit, have the following values:
+
+       32bit       64bit     value
+       ---------------------------
+       ebx (a0)    rdi       KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT
+       ecx (a1)    rsi       0
+
+This specification copies Xen's { __HYPERVISOR_hvm_op,
+HVMOP_guest_request_vm_event } hypercall and can originate from kernel or
+userspace.
+
+It returns 0 if successful, or a negative POSIX.1 error code if it fails. The
+absence of an active VMI application is not signaled in any way.
+
+The following registers are clobbered:
+
+  * 32bit: edx, esi, edi, ebp
+  * 64bit: rdx, r10, r8, r9
+
+In particular, for KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT, the last two
+registers can be poisoned deliberately and cannot be used for passing
+information.
Documentation/virtual/kvm/kvmi.rst new file +1275 -0

1,275 lines not on this page · raw message

maintainer
#2 lore raw

Re: [RFC PATCH v3 1/1] kvm: add documentation for the VM introspection subsystem

Quoted 5 lines · Adalbert Lazar

+13. KVMI_MSR_CONTROL
+--------------------

+14. KVMI_CONTROL_VE
+-------------------

Decide on one. :) I prefer KVMI_CONTROL_*

About events:

- page faults:

Documentation/virtual/kvm/kvmi.rst line 1126

Quoted 13 lines
Documentation/virtual/kvm/kvmi.rst new file +13 -0
+
+* *KVMI_EVENT_ACTION_ALLOW* -  allow the page fault via emulation (with
+  custom input if ``ctx_size`` > 0). The use of custom input is to trick
+  the guest software into believing it has read certain data, in order
+  to hide the content of certain memory areas (eg. hide injected code
+  from integrity checkers). If ``trap_access`` is not zero, the REP
+  prefixed instruction should be emulated just once.
+* *KVMI_EVENT_ACTION_SKIP* - the introspection tool did the emulation
+  (incremented the program counter and changed the registers/memory
+  affected by the emulation). It is used to prevent unwanted changes to
+  memory.
+* *KVMI_EVENT_ACTION_RETRY* - re-enter the guest and let it re-trigger
+   the page fault

SKIP and RETRY are the same, aren't they? I would just use RETRY everywhere. SKIP implies that KVM takes care of modifying the instruction pointer for example, RETRY does not.

- singlestepping:

Documentation/virtual/kvm/kvmi.rst line 1221

Quoted 4 lines
Documentation/virtual/kvm/kvmi.rst new file +4 -0
+* *KVMI_EVENT_ACTION_ALLOW* - disable the single-stepping and continue normal
+  guest execution
+* *KVMI_EVENT_ACTION_RETRY* - continue guest execution, which will lead to
+  a new *KVMI_EVENT_SINGLESTEP* event

Because singlestep is a trap event (happens after execution of the instruction), RETRY has the same meaning as SKIP elsewhere: KVM has nothing to do. Is this correct? So we can use RETRY.

ALLOW here is not a very good name, but I have no better idea so I guess it's okay. Maybe rename ALLOW to CONTINUE everywhere, so it makes more sense here too. :)

- paused:

Documentation/virtual/kvm/kvmi.rst line 890

Quoted 10 lines
Documentation/virtual/kvm/kvmi.rst new file +9 -0
+	struct kvmi_event_reply
+
+This event is sent in response to a *KVMI_PAUSE_VCPU* command, unless it
+is canceled by another *KVMI_PAUSE_VCPU* command (with ``cancel`` set to 1).
+
+The introspector can respond with one of the following actions:
+
+* *KVMI_EVENT_ACTION_ALLOW* - resume
+* *KVMI_EVENT_ACTION_CRASH* - stop the guest

ALLOW here is just a "do nothing" action and the API might as well use RETRY.

Quoted 4 lines

+5. KVMI_EVENT_HYPERCALL
+-----------------------

+* *KVMI_EVENT_ACTION_ALLOW* - acknowledged

Should there be a return value in the reply, that is written into eg. RAX?

In general, I think actions other than INJECT should be documented generically:

- CONTINUE (formerly ALLOW) - complete the instruction that caused the event (i.e. execute it without triggering the event again, possibly using information coming from the reply)

- RETRY (formerly SKIP or RETRY) - re-enter the guest (the event may happen again)

- CRASH - self-documenting

RETRY and CRASH should always be allowed. The specific behavior of CONTINUE can be documented for each event that allows it.

Do you need events for reading CRn or MSRs?

Otherwise looks good. I guess you can send code patches on v4. ;)

Thanks,

Paolo

#3 lore raw

Re: [RFC PATCH v3 1/1] kvm: add documentation for the VM introspection subsystem

Quoted 8 lines · Paolo Bonzini
Quoted 4 lines · Adalbert Lazar

+13. KVMI_MSR_CONTROL
+--------------------
+14. KVMI_CONTROL_VE
+-------------------

Decide on one. :) I prefer KVMI_CONTROL_*

I'm sorry about that. An obvious consistency mistake that got past me.

KVMI_CONTROL_* it is.

Quoted 13 lines

About events:

- page faults:

Quoted 9 lines
Documentation/virtual/kvm/kvmi.rst new file +9 -0
+
+* *KVMI_EVENT_ACTION_ALLOW* -  allow the page fault via emulation (with
+  custom input if ``ctx_size`` > 0). The use of custom input is to trick
+  the guest software into believing it has read certain data, in order
+  to hide the content of certain memory areas (eg. hide injected code
+  from integrity checkers). If ``trap_access`` is not zero, the REP
+  prefixed instruction should be emulated just once.
+* *KVMI_EVENT_ACTION_SKIP* - the introspection tool did the emulation
+  (incremented the program counter and changed the registers/memory
Quoted 8 lines

Documentation/virtual/kvm/kvmi.rst line 1126

Quoted 4 lines
Documentation/virtual/kvm/kvmi.rst new file +4 -0
+  affected by the emulation). It is used to prevent unwanted changes to
+  memory.
+* *KVMI_EVENT_ACTION_RETRY* - re-enter the guest and let it re-trigger
+   the page fault

SKIP and RETRY are the same, aren't they? I would just use RETRY everywhere. SKIP implies that KVM takes care of modifying the instruction pointer for example, RETRY does not.

I've re-checked with my colleagues and it looks like I've misunderstood things. The conclusion is that SKIP and RETRY should be the same from KVM's point of view. The user space component, however, needs some extra logic to adjust the RIP accordingly (skip over an instruction or jump to an 'unrelated' address).

We will use RETRY and drop SKIP.

Quoted 10 lines

- singlestepping:

Documentation/virtual/kvm/kvmi.rst line 1221

Quoted 4 lines
Documentation/virtual/kvm/kvmi.rst new file +4 -0
+* *KVMI_EVENT_ACTION_ALLOW* - disable the single-stepping and continue normal
+  guest execution
+* *KVMI_EVENT_ACTION_RETRY* - continue guest execution, which will lead to
+  a new *KVMI_EVENT_SINGLESTEP* event

Because singlestep is a trap event (happens after execution of the instruction), RETRY has the same meaning as SKIP elsewhere: KVM has nothing to do. Is this correct? So we can use RETRY.

Correct.

Quoted 7 lines

ALLOW here is not a very good name, but I have no better idea so I guess it's okay. Maybe rename ALLOW to CONTINUE everywhere, so it makes more sense here too. :)

- paused:

Quoted 1 line
Quoted 12 lines

Documentation/virtual/kvm/kvmi.rst line 890

Quoted 9 lines
Documentation/virtual/kvm/kvmi.rst new file +9 -0
+	struct kvmi_event_reply
+
+This event is sent in response to a *KVMI_PAUSE_VCPU* command, unless it
+is canceled by another *KVMI_PAUSE_VCPU* command (with ``cancel`` set to 1).
+
+The introspector can respond with one of the following actions:
+
+* *KVMI_EVENT_ACTION_ALLOW* - resume
+* *KVMI_EVENT_ACTION_CRASH* - stop the guest

ALLOW here is just a "do nothing" action and the API might as well use RETRY.

OK.

Quoted 5 lines
Quoted 3 lines

+5. KVMI_EVENT_HYPERCALL
+-----------------------
+* *KVMI_EVENT_ACTION_ALLOW* - acknowledged

Should there be a return value in the reply, that is written into eg. RAX?

We can provide a value for RAX with the event response or assume the introspection tool called SET_REGISTERS with the appropriate RAX.

For now we took your suggestion. We'll revisit if needed.

Quoted 10 lines

In general, I think actions other than INJECT should be documented generically:

- CONTINUE (formerly ALLOW) - complete the instruction that caused the event (i.e. execute it without triggering the event again, possibly using information coming from the reply)

- RETRY (formerly SKIP or RETRY) - re-enter the guest (the event may happen again)

Quoted 6 lines

- CRASH - self-documenting

RETRY and CRASH should always be allowed. The specific behavior of CONTINUE can be documented for each event that allows it.

Do you need events for reading CRn or MSRs?

There is another VMI project, Nitro, that uses MSR read events to do a sort of strace on syscalls and we were thinking on porting that feature. However, users of that API with which we are in contact, expressed the intention of moving to a breakpoint based system rather than trapping for every MSR read. Since we don't have a use case for it either, we decided to hold off on that unless somebody jumps in and asks for it.

Quoted 1 line

Otherwise looks good. I guess you can send code patches on v4. ;)

Thank you,

Signature · 1 line
Mihai Donțu
maintainer
#4 lore raw

Re: [RFC PATCH v3 1/1] kvm: add documentation for the VM introspection subsystem

Quoted 9 lines · Mihai Donțu
Quoted 1 line

Do you need events for reading CRn or MSRs?

There is another VMI project, Nitro, that uses MSR read events to do a sort of strace on syscalls and we were thinking on porting that feature. However, users of that API with which we are in contact, expressed the intention of moving to a breakpoint based system rather than trapping for every MSR read. Since we don't have a use case for it either, we decided to hold off on that unless somebody jumps in and asks for it.

I was thinking of the case where introspection has to modify some MSRs or CRs, and would like the guest kernel to see a fake value.

Paolo

#5 lore raw
In reply to Adalbert Lazar
Quoted 64 lines · Adalbert Lazar

The following patch adds the documentation for an introspection subsystem for KVM (KVMI). It details the purpose and the use case that has shaped the proposed API/ABI, as well as the wire protocol.

The previous RFC can be read here: https://marc.info/?l=kvm&m=149943806203155

In this iteration we consolidated the feedback received from Paolo and others. More discussion is needed on the topic of pausing the VM. For now we settled on a vCPU pause function that should be used to implement the former. This requires a bit of finesse in userspace but keeps a potentially complicated logic out of the kernel.

One other topic that we would like to bring again into discussion is that of page sharing between guests (the VM on which the introspection tool runs and the VM being introspected, provided they are on the same host).

This iteration of the document also tries to integrate #VE and multiple EPT views (VMFUNC). It does so simply by copying the ideas that have been discussed on xen-devel (see KVMI_CONTROL_VE and KVMI_{GET,SET}_PAGE_ACCESS).

Please note that some slight adjustments might still appear as we progress with the qemu side of things.

We have not included anything about IOMMU groups as we feel we don't yet have all the details to begin working on an API that deals with DMA attacks.

Changes since v2:
- make small changes to the wire protocol (eg. use kvmi_error_code with every command reply, a few renames, etc.)
- removed '_x86' from x86 specific structure names. Architecture specific structures will have the same name.
- drop KVMI_GET_MTRR_TYPE and KVMI_GET_MTRRS (use KVMI_SET_REGISTERS)
- drop KVMI_EVENT_ACTION_SET_REGS (use KVMI_SET_REGISTERS)
- remove KVMI_MAP_PHYSICAL_PAGE_TO_GUEST and KVMI_UNMAP_PHYSICAL_PAGE_FROM_GUEST (to be replaced by a token+hypercall pair)
- extend KVMI_GET_VERSION with allowed commnd/event masks
- replace KVMI_PAUSE_GUEST/KVMI_UNPAUSE_GUEST with KVMI_PAUSE_VCPU
- replace KVMI_SHUTDOWN_GUEST with KVMI_EVENT_ACTION_CRASH
- replace KVMI_GET_XSAVE_INFO with KVMI_GET_CPUID
- merge KVMI_INJECT_PAGE_FAULT and KVMI_INJECT_BREAKPOINT
in KVMI_INJECT_EXCEPTION
- replace event reply flags with ALLOW/SKIP/RETRY/CRASH actions
- make KVMI_SET_REGISTERS work with vCPU events only
- add EPT view support in KVMI_GET_PAGE_ACCESS/KVMI_SET_PAGE_ACCESS
- add support for multiple pages in KVMI_GET_PAGE_ACCESS/KVMI_SET_PAGE_ACCESS
- add (back) KVMI_READ_PHYSICAL/KVMI_WRITE_PHYSICAL
- add KVMI_CONTROL_VE
- add cstar to KVMI_EVENT
- add new events: KVMI_EVENT_VCPU_PAUSED, KVMI_EVENT_CREATE_VCPU, KVMI_EVENT_DESCRIPTOR_ACCESS, KVMI_EVENT_SINGLESTEP
- add new sections: "Introspection capabilities", "Live migrations", "Guest snapshots with memory", "Memory access safety"
- document the hypercall used by the KVMI_EVENT_HYPERCALL command (was KVMI_EVENT_USER_CALL)

Changes since v1:
- add documentation and ABI [Paolo, Jan]
- drop all the other patches for now [Paolo]
- remove KVMI_GET_GUESTS, KVMI_EVENT_GUEST_ON, KVMI_EVENT_GUEST_OFF, and let libvirt/qemu handle this [Stefan, Paolo]
- change the license from LGPL to GPL [Jan]
- remove KVMI_READ_PHYSICAL and KVMI_WRITE_PHYSICAL (not used anymore)
- make the interface a little more consistent

Quoted 8 lines

Adalbert Lazar (1):
kvm: add documentation for the VM introspection subsystem

  Documentation/virtual/kvm/hypercalls.txt |   32 +
  Documentation/virtual/kvm/kvmi.rst       | 1275 ++++++++++++++++++++++++++++++
  2 files changed, 1307 insertions(+)

create mode 100644 Documentation/virtual/kvm/kvmi.rst

Hi all,

We're interested in seeing this functionality in KVM and I'm happy to help out however I can. I'm wondering what the current status is on this and what the best thing I can do to start working on this is.

Thanks,
Patrick

#6 lore raw
Quoted 6 lines · Patrick Colp
Quoted 4 lines · Adalbert Lazar

The following patch adds the documentation for an introspection subsystem for KVM (KVMI). It details the purpose and the use case that has shaped the proposed API/ABI, as well as the wire protocol.

Quoted 8 lines

Hi all,

We're interested in seeing this functionality in KVM and I'm happy to help out however I can. I'm wondering what the current status is on this and what the best thing I can do to start working on this is.

Thanks,
Patrick

Hi Patrick,

We are glad to know you want to work on this. We were thinking to make our working/testing repositories public, somehow. Seeing more interest in VM introspection, the move will be sooner than expected.

The code regarding page sharing (map/unmap) and page tracking need more eyes, the reviews are always needed and any help is welcomed. But, on what would you like to work will be much more clear in one week or two, when we will send the v4 series. It will be the "complete" patch set (not just the documentation), with changes covering most of what was suggested during v1-v3.

Thanks,
Adalbert