[PATCH 07/14] KVM: arm64: Enable UBSAN_BOUNDS for the both the kernel and hyp/nVHE

George-Aurelian Popescu
2020-09-14T17:34:10Z

From: George Popescu <georgepope@google.com>

If an out of bounds happens inside the hyp/nVHE code, the
ubsan_out_of_bounds handler stores the logging data inside the
kvm_ubsan_buffer. The one responsible for printing is the kernel
ubsan_out_of_bounds handler. The process of decapsulating the data happens
in kvm_ubsan_buffer.c.

The struct kvm_ubsan_info contains three main components:
-enum type, which is used to identify which handler to call from the
kernel.
-struct ubsan_values, which stores the operands involved during the
undefined behaviours, which can be one, two or zero, depending on what
undefiend behaviour is reported. As an example for: out_of_bounds there
is only one operand (the index).

Accessing a slot with no type should do nothing. Each slot is marked
with the UBSAN_NONE tag after it's first usage.

Signed-off-by: George Popescu <georgepope@google.com>
---
arch/arm64/include/asm/kvm_ubsan.h | 19 ++++++++++++++++++-
arch/arm64/kvm/hyp/nvhe/ubsan.c | 13 ++++++++++++-
arch/arm64/kvm/kvm_ubsan_buffer.c | 13 ++++++++++++-
3 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_ubsan.h b/arch/arm64/include/asm/kvm_ubsan.h
index af607a796376..575881e0bd5f 100644
--- a/arch/arm64/include/asm/kvm_ubsan.h
+++ b/arch/arm64/include/asm/kvm_ubsan.h
@@ -11,7 +11,24 @@
#define UBSAN_MAX_TYPE 6
#define KVM_UBSAN_BUFFER_SIZE 1000

+struct ubsan_values {
+ void *lval;
+ void *rval;
+ char op;
+};
+
struct kvm_ubsan_info {
- int type;
+ enum {
+ UBSAN_NONE,
+ UBSAN_OUT_OF_BOUNDS
+ } type;
+ union {
+ struct out_of_bounds_data out_of_bounds_data;
+ };
+ union {
+ struct ubsan_values u_val;
+ };
};
#endif
+
+void __ubsan_handle_out_of_bounds(void *_data, void *index);
diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan.c b/arch/arm64/kvm/hyp/nvhe/ubsan.c
index a43c9646e1e8..b2d3404f6215 100644
--- a/arch/arm64/kvm/hyp/nvhe/ubsan.c
+++ b/arch/arm64/kvm/hyp/nvhe/ubsan.c
@@ -43,7 +43,18 @@ void __ubsan_handle_type_mismatch(struct type_mismatch_data *data, void *ptr) {}

void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr) {}

-void __ubsan_handle_out_of_bounds(void *_data, void *index) {}
+void __ubsan_handle_out_of_bounds(void *_data, void *index)
+{
+ struct kvm_ubsan_info *slot = NULL;
+ struct out_of_bounds_data *data = _data;
+
+ slot = kvm_ubsan_buffer_next_slot();
+ if (slot) {
+ slot->type = UBSAN_OUT_OF_BOUNDS;
+ slot->out_of_bounds_data = *data;
+ slot->u_val.lval = index;
+ }
+}

void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs) {}

diff --git a/arch/arm64/kvm/kvm_ubsan_buffer.c b/arch/arm64/kvm/kvm_ubsan_buffer.c
index 28dcf19b5706..ce796bdd027e 100644
--- a/arch/arm64/kvm/kvm_ubsan_buffer.c
+++ b/arch/arm64/kvm/kvm_ubsan_buffer.c
@@ -16,6 +16,17 @@

DECLARE_KVM_DEBUG_BUFFER(struct kvm_ubsan_info, kvm_ubsan_buff, KVM_UBSAN_BUFFER_SIZE);

+void __kvm_check_ubsan_data(struct kvm_ubsan_info *slot)
+{
+ switch (slot->type) {
+ case UBSAN_NONE:
+ break;
+ case UBSAN_OUT_OF_BOUNDS:
+ __ubsan_handle_out_of_bounds(&slot->out_of_bounds_data,
+ slot->u_val.lval);
+ break;
+ }
+}

void __kvm_check_ubsan_buffer(void)
{
@@ -25,7 +36,7 @@ void __kvm_check_ubsan_buffer(void)

init_kvm_debug_buffer(kvm_ubsan_buff, struct kvm_ubsan_info, slot, write_ind);
for_each_kvm_debug_buffer_slot(slot, write_ind, it) {
- /* check ubsan data */
+ __kvm_check_ubsan_data(slot);
slot->type = 0;
}
}
--
2.28.0.618.gf4bc123cb7-goog