Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp440329pxb; Thu, 14 Jan 2021 09:28:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJwyfIqTQt8EPBu2NLVKd2VyXUDz9+xwcpQHZkBUTcwu7pa8fzrn2DpDuA1lItLdqo9zW+Xc X-Received: by 2002:a05:6402:34c5:: with SMTP id w5mr6561857edc.65.1610645287638; Thu, 14 Jan 2021 09:28:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610645287; cv=none; d=google.com; s=arc-20160816; b=o+5iAs6QJNd/eknBvMe0XeNRZuxt4n2uAK3lWXOK2QxPOl8JoC62oBqfp/VY8OI3MX wfjkYOBxt34Gi8Z4Hvog8V4mNd/j6pJz5vzTVdtvJO3jHt2zPOV8otuFkaayRE85BPR/ wecRGuWh4lN8gaf7EVqj2cz1TcG2S/kLwTvogvEvwQB3Lwj/f4M7dp6MsNaeMYxObe+5 i2tBcdyjv+LlbSQueOIRKb1IOTuRkaQmJRtfUtw+C2JfAs9hPbKl8ZEH6HtCEYKdcpCi mMqJXAoZSAGqONGVbFNS5J4Xw9xjQCdWf1+RqCe7VXW+VnRLRdMiKWT1QWJGr0gVCEQ9 tZMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=XOmV9rOXqPPVqcegQZNJofTEyx38uhN2nVbmi9afywU=; b=G+aMpqkrFMHwmPdPFwgCp18dhA3Zkz9wI2F65ASwP9tczd3Pvj159EgNqNJa9FT8pX JdNX1iPrfIBpCXM/t7crmN7KLf1eK3D5RUKEMGHwH2y69EUIv9RIhDkmVBu040PAoaWG WhmSyDGsFnccp7B4WUCigeX0llK/ui+56dT/9PG8ZUGG9ufviaehr4dzDGPV4MvSr/Cj AhBmbrZU4N18eYylo2HCFDybUj2fu98MMOzMiPWJBlANjRVKisWJxp4LqDdzGZYRPXiC x5twnzYgGtDjfWNLTHnBalSj9Pr70AO0Tx/X790XKBWkB2pq1f9Exlhofu7EBaRR4r7u m7bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=wW2e4FyZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k20si2732098edo.493.2021.01.14.09.27.43; Thu, 14 Jan 2021 09:28:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=wW2e4FyZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728783AbhANRZm (ORCPT + 99 others); Thu, 14 Jan 2021 12:25:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725935AbhANRZk (ORCPT ); Thu, 14 Jan 2021 12:25:40 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4B9DC0613D6 for ; Thu, 14 Jan 2021 09:24:59 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id n11so2894578wro.7 for ; Thu, 14 Jan 2021 09:24:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=XOmV9rOXqPPVqcegQZNJofTEyx38uhN2nVbmi9afywU=; b=wW2e4FyZwiW4L+MD2pK71yjkUENfnlHKSdcjj5VQOEWjRQ/VMENAXofc/SBW/q3NqS As+WKNmk0v1a694hte5GMuu6L+ztFtEvy75X7u0teBJIkZrfu6w32KTxJvFl1zSXiiSs uh1waDR/zSMV8uINsy01PGok3/BSAn2BV/wG/RZPY9vbH5/bbWboCppBOonmCqz3a+IX 20gSuBJCAHT7XR8fGEnqlywuv05Z0b0k9Quho4eon4sbGa9nTUyGJhZzWp7FgN4E2wsz oNpYilKZdIn+H7Zmr4oEKebRiDVH0ovn0+TMHilpMqs053xwXZ6NuWFhU+Z3UaPs5seU KH6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=XOmV9rOXqPPVqcegQZNJofTEyx38uhN2nVbmi9afywU=; b=uZu7XYayKBkSg0reSf2u7yZG1DTPXeSsBHa4v93p0waTp92wAm9d1XHsCm5rAzI+9c bV1ABf7R7tuOs8yh+HWNd1CgNv57Ad8FDtOv51s0xJ3XgBgMcLPhxMT3JaE2q/rbdsfk BBaSwDTTMUz/xvGbkYii3nUH9fCfMwCz3HimSifoLmCGIn0I2vOHZ1kpYaSFpmUQT9VW Y5aev6JQhTk42xQMkoeIAN8eizWpo49lopKEl4J02ycLH4SyGJBAnVgE0KlpMG6rBoIO r7D5bCY3wEJdWBbbgGg4dodEzg5EfIFuUXXTTE84AlIkjoyo1lUG6ddxgJVoV9a2Ms+U Tlgw== X-Gm-Message-State: AOAM532G+KjiBT0xN0+QfDPNleRYSsfge47EQjYiybow8XrkbH/+RcY8 /Mf6xJNPsoFobc/DRk1Znfyq/IfKjCX2 Sender: "lenaptr via sendgmr" X-Received: from beef.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1091]) (user=lenaptr job=sendgmr) by 2002:a1c:c204:: with SMTP id s4mr4820767wmf.73.1610645098345; Thu, 14 Jan 2021 09:24:58 -0800 (PST) Date: Thu, 14 Jan 2021 17:23:37 +0000 In-Reply-To: <20210114172338.2798389-1-lenaptr@google.com> Message-Id: <20210114172338.2798389-8-lenaptr@google.com> Mime-Version: 1.0 References: <20210114172338.2798389-1-lenaptr@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 7/9] KVM: arm64: Detect type mismatch undefined behaviour from hyp/nVHE code From: Elena Petrova To: kvmarm@lists.cs.columbia.edu Cc: Elena Petrova , linux-arm-kernel@lists.infradead.org, open list , Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose , George Popescu , George Popescu Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: George Popescu Type mismatch undefiend behaviour handler provides two handlers with two data structures type_mismatch_data and type_mismatch_data_v1. Both can be stored inside a common data structure: type_mismatch_data_common, which differs of type_mismatch_data only by keeping a pointer to a struct source_location. In this way, the buffer keeps the data encapsulated inside of a struct type_mismatch_data, because pointers from nVHE can not be passed to the kernel. Inside the kernel call the __ubsan_handle_type_mismatch_data with the data from the buffer. Signed-off-by: George Popescu Signed-off-by: Elena Petrova --- arch/arm64/include/asm/kvm_ubsan.h | 6 ++++- arch/arm64/kvm/hyp/nvhe/ubsan.c | 41 ++++++++++++++++++++++++++++-- arch/arm64/kvm/kvm_ubsan_buffer.c | 5 +++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/kvm_ubsan.h b/arch/arm64/include/asm/kvm_ubsan.h index 95ac6728ffd1..93c1b695097a 100644 --- a/arch/arm64/include/asm/kvm_ubsan.h +++ b/arch/arm64/include/asm/kvm_ubsan.h @@ -21,13 +21,15 @@ struct kvm_ubsan_info { UBSAN_OUT_OF_BOUNDS, UBSAN_UNREACHABLE_DATA, UBSAN_SHIFT_OUT_OF_BOUNDS, - UBSAN_INVALID_DATA + UBSAN_INVALID_DATA, + UBSAN_TYPE_MISMATCH } type; union { struct out_of_bounds_data out_of_bounds_data; struct unreachable_data unreachable_data; struct shift_out_of_bounds_data shift_out_of_bounds_data; struct invalid_value_data invalid_value_data; + struct type_mismatch_data type_mismatch_data; }; union { struct ubsan_values u_val; @@ -38,3 +40,5 @@ void __ubsan_handle_out_of_bounds(void *_data, void *index); void __ubsan_handle_builtin_unreachable(void *_data); void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs); void __ubsan_handle_load_invalid_value(void *_data, void *val); +void __ubsan_handle_type_mismatch(struct type_mismatch_data *_data, void *ptr); + diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan.c b/arch/arm64/kvm/hyp/nvhe/ubsan.c index 3143f7722be2..a9f72d4bcab7 100644 --- a/arch/arm64/kvm/hyp/nvhe/ubsan.c +++ b/arch/arm64/kvm/hyp/nvhe/ubsan.c @@ -29,6 +29,24 @@ static inline struct kvm_ubsan_info *kvm_ubsan_buffer_next_slot(void) return res; } +static void write_type_mismatch_data(struct type_mismatch_data_common *data, void *lval) +{ + struct kvm_ubsan_info *slot; + struct type_mismatch_data *aux_cont; + + slot = kvm_ubsan_buffer_next_slot(); + if (slot) { + slot->type = UBSAN_TYPE_MISMATCH; + aux_cont = &slot->type_mismatch_data; + aux_cont->location.file_name = data->location->file_name; + aux_cont->location.reported = data->location->reported; + aux_cont->type = data->type; + aux_cont->alignment = data->alignment; + aux_cont->type_check_kind = data->type_check_kind; + slot->u_val.lval = lval; + } +} + void __ubsan_handle_add_overflow(void *_data, void *lhs, void *rhs) {} void __ubsan_handle_sub_overflow(void *_data, void *lhs, void *rhs) {} @@ -39,9 +57,28 @@ void __ubsan_handle_negate_overflow(void *_data, void *old_val) {} void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs) {} -void __ubsan_handle_type_mismatch(struct type_mismatch_data *data, void *ptr) {} +void __ubsan_handle_type_mismatch(struct type_mismatch_data *data, void *ptr) +{ + struct type_mismatch_data_common common_data = { + .location = &data->location, + .type = data->type, + .alignment = data->alignment, + .type_check_kind = data->type_check_kind + }; + write_type_mismatch_data(&common_data, ptr); +} -void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr) {} +void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr) +{ + struct type_mismatch_data_v1 *data = _data; + struct type_mismatch_data_common common_data = { + .location = &data->location, + .type = data->type, + .alignment = 1UL << data->log_alignment, + .type_check_kind = data->type_check_kind + }; + write_type_mismatch_data(&common_data, ptr); +} void __ubsan_handle_out_of_bounds(void *_data, void *index) { diff --git a/arch/arm64/kvm/kvm_ubsan_buffer.c b/arch/arm64/kvm/kvm_ubsan_buffer.c index 5439f7a91636..b7823dedf8b1 100644 --- a/arch/arm64/kvm/kvm_ubsan_buffer.c +++ b/arch/arm64/kvm/kvm_ubsan_buffer.c @@ -35,6 +35,10 @@ void __kvm_check_ubsan_data(struct kvm_ubsan_info *slot) __ubsan_handle_load_invalid_value(&slot->invalid_value_data, slot->u_val.lval); break; + case UBSAN_TYPE_MISMATCH: + __ubsan_handle_type_mismatch(&slot->type_mismatch_data, + slot->u_val.lval); + break; } } @@ -58,4 +62,3 @@ void __kvm_check_ubsan_buffer(void) write_ind = (unsigned long *) this_cpu_ptr_nvhe_sym(kvm_ubsan_buff_wr_ind); iterate_kvm_ubsan_buffer(0, *write_ind); } - -- 2.30.0.284.gd98b1dd5eaa7-goog