These commands can be used by the introspection tool to check what introspection commands and events are supported (by KVMi) and allowed (by userspace/QEMU). The introspection tool will get one of the following error codes: * -KVM_EOPNOTSUPP (unsupported command/event) * -KVM_PERM (disallowed command/event) * -KVM_EINVAL (the padding space, used for future extensions, is not zero) * 0 (the command/event is supported and allowed) These commands can be seen as an alternative method to KVMI_GET_VERSION in checking if the introspection supports a specific command/event. As with the KVMI_GET_VERSION command, these commands can never be disallowed by userspace/QEMU. Signed-off-by: Adalbert Lazăr <alazar@xxxxxxxxxxxxxxx> --- Documentation/virtual/kvm/kvmi.rst | 60 ++++++++++++++++++++++++++++++ include/uapi/linux/kvmi.h | 12 ++++++ virt/kvm/kvmi.c | 8 +++- virt/kvm/kvmi_msg.c | 38 +++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-) diff --git a/Documentation/virtual/kvm/kvmi.rst b/Documentation/virtual/kvm/kvmi.rst index 82de474d512b..61cf69aa5d07 100644 --- a/Documentation/virtual/kvm/kvmi.rst +++ b/Documentation/virtual/kvm/kvmi.rst @@ -302,3 +302,63 @@ While the command reply is disabled: * the reply status is ignored for any unsupported/unknown or disallowed commands (and ``struct kvmi_error_code`` will be sent with -KVM_EOPNOTSUPP or -KVM_PERM). + +3. KVMI_CHECK_COMMAND +--------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_check_command { + __u16 id; + __u16 padding1; + __u32 padding2; + }; + +:Returns: + +:: + + struct kvmi_error_code; + +Checks if the command specified by ``id`` is allowed. + +This command is always allowed. + +:Errors: + +* -KVM_PERM - the command specified by ``id`` is disallowed +* -KVM_EINVAL - padding is not zero + +4. KVMI_CHECK_EVENT +------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_check_event { + __u16 id; + __u16 padding1; + __u32 padding2; + }; + +:Returns: + +:: + + struct kvmi_error_code; + +Checks if the event specified by ``id`` is allowed. + +This command is always allowed. + +:Errors: + +* -KVM_PERM - the event specified by ``id`` is disallowed +* -KVM_EINVAL - padding is not zero diff --git a/include/uapi/linux/kvmi.h b/include/uapi/linux/kvmi.h index a1ab39c5b8e0..7390303371c9 100644 --- a/include/uapi/linux/kvmi.h +++ b/include/uapi/linux/kvmi.h @@ -90,4 +90,16 @@ struct kvmi_control_cmd_response { __u32 padding2; }; +struct kvmi_check_command { + __u16 id; + __u16 padding1; + __u32 padding2; +}; + +struct kvmi_check_event { + __u16 id; + __u16 padding1; + __u32 padding2; +}; + #endif /* _UAPI__LINUX_KVMI_H */ diff --git a/virt/kvm/kvmi.c b/virt/kvm/kvmi.c index d5b6af21564e..dc1bb8326763 100644 --- a/virt/kvm/kvmi.c +++ b/virt/kvm/kvmi.c @@ -69,6 +69,8 @@ static bool alloc_kvmi(struct kvm *kvm, const struct kvm_introspection *qemu) return false; set_bit(KVMI_GET_VERSION, ikvm->cmd_allow_mask); + set_bit(KVMI_CHECK_COMMAND, ikvm->cmd_allow_mask); + set_bit(KVMI_CHECK_EVENT, ikvm->cmd_allow_mask); memcpy(&ikvm->uuid, &qemu->uuid, sizeof(ikvm->uuid)); @@ -295,10 +297,14 @@ int kvmi_ioctl_command(struct kvm *kvm, void __user *argp) if (!allow) { DECLARE_BITMAP(always_allowed, KVMI_NUM_COMMANDS); - if (id == KVMI_GET_VERSION) + if (id == KVMI_GET_VERSION + || id == KVMI_CHECK_COMMAND + || id == KVMI_CHECK_EVENT) return -EPERM; set_bit(KVMI_GET_VERSION, always_allowed); + set_bit(KVMI_CHECK_COMMAND, always_allowed); + set_bit(KVMI_CHECK_EVENT, always_allowed); bitmap_andnot(requested, requested, always_allowed, KVMI_NUM_COMMANDS); diff --git a/virt/kvm/kvmi_msg.c b/virt/kvm/kvmi_msg.c index 2237a6ed25f6..e24996611e3a 100644 --- a/virt/kvm/kvmi_msg.c +++ b/virt/kvm/kvmi_msg.c @@ -9,6 +9,8 @@ #include "kvmi_int.h" static const char *const msg_IDs[] = { + [KVMI_CHECK_COMMAND] = "KVMI_CHECK_COMMAND", + [KVMI_CHECK_EVENT] = "KVMI_CHECK_EVENT", [KVMI_CONTROL_CMD_RESPONSE] = "KVMI_CONTROL_CMD_RESPONSE", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", }; @@ -177,6 +179,40 @@ static bool is_command_allowed(struct kvmi *ikvm, int id) return test_bit(id, ikvm->cmd_allow_mask); } +static int handle_check_command(struct kvmi *ikvm, + const struct kvmi_msg_hdr *msg, + const void *_req) +{ + const struct kvmi_check_command *req = _req; + int ec = 0; + + if (req->padding1 || req->padding2) + ec = -KVM_EINVAL; + else if (!is_command_allowed(ikvm, req->id)) + ec = -KVM_EPERM; + + return kvmi_msg_vm_maybe_reply(ikvm, msg, ec, NULL, 0); +} + +static bool is_event_allowed(struct kvmi *ikvm, int id) +{ + return test_bit(id, ikvm->event_allow_mask); +} + +static int handle_check_event(struct kvmi *ikvm, + const struct kvmi_msg_hdr *msg, const void *_req) +{ + const struct kvmi_check_event *req = _req; + int ec = 0; + + if (req->padding1 || req->padding2) + ec = -KVM_EINVAL; + else if (!is_event_allowed(ikvm, req->id)) + ec = -KVM_EPERM; + + return kvmi_msg_vm_maybe_reply(ikvm, msg, ec, NULL, 0); +} + static int handle_control_cmd_response(struct kvmi *ikvm, const struct kvmi_msg_hdr *msg, const void *_req) @@ -207,6 +243,8 @@ static int handle_control_cmd_response(struct kvmi *ikvm, */ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, const void *) = { + [KVMI_CHECK_COMMAND] = handle_check_command, + [KVMI_CHECK_EVENT] = handle_check_event, [KVMI_CONTROL_CMD_RESPONSE] = handle_control_cmd_response, [KVMI_GET_VERSION] = handle_get_version, };