Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2337015pxk; Mon, 14 Sep 2020 10:33:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz8VeiSWe0SmCmTj3hE4cLeGFMcNbcZqs5CRlyNI0xuFJh/r7cv2WH64SZQHOgY4XXGX8Rv X-Received: by 2002:a50:fc83:: with SMTP id f3mr18924746edq.102.1600104814263; Mon, 14 Sep 2020 10:33:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600104814; cv=none; d=google.com; s=arc-20160816; b=Y+wDLFNREtcH2lrnGuhZMO+KAcykicojDirN2RXO1UJ9lMz+2mVcalMjxdLjXAbgjP PPbXNTdbbpMI1Jt6SDCmY4FBiNraLybkOTWnVVazrOYixRi99BgH1VyIDEe6DMGlxx07 4e6eoqtTvK4JXXL2N2L0Ic8ewEprTk8bNDrGuO9RhYLWWeC8D1iQwmk+pRbq/R5JHZQn AsAWJkTWK6jiFGqnj458dR5QnayiflXRFsw/dZch82PA63C42XjD+WReFAWdrYjJ7kO1 XJGWHhQtyFL8PEx3jqX/cCcj/ii91MfqtaEwFMdCmf/Ou5zvnblUFb6/KVxSurM9DXop 5f7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CR8a6tMHdr0USdHEJnOukkMN6eSncqh9/9HspEdN4UY=; b=pbQcTnUkOpUugI/tBuolP/q5WLt7ZU/EiiOM7AUUoQsnSFC/A8T1RtGij0f7vrDTcz OZDQYqDyXtfVfpwU/ZwVOIeHiDk9P8nGH0HEa9rVK1PTdIHL1FibyGhuFCqkljnkPwnq 7tFQiHaMNO0aP6sVHB29iq9HjWbO0Yn+rWEfu+Of52QFflqqOwO2jaRDuMHy5ZXcPLEM Zp4gMIEO5bTS9edckn9/qeXy5W6D5nPy2WzH1OYUPU9HuTxJOtGGc18vXqHCfgoDFE5G MvunqRZflMqixAluk63mJ5/6zquCDC87tm8ROyIHBfjZ7UPOcbPxJlvEvaQU7/CoSpFG s7fA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MRgm2+rK; 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 y11si7976221edp.213.2020.09.14.10.33.11; Mon, 14 Sep 2020 10:33:34 -0700 (PDT) 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=MRgm2+rK; 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 S1726097AbgINRb7 (ORCPT + 99 others); Mon, 14 Sep 2020 13:31:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726331AbgINR2j (ORCPT ); Mon, 14 Sep 2020 13:28:39 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E73AC061222 for ; Mon, 14 Sep 2020 10:28:24 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id j2so499428wrx.7 for ; Mon, 14 Sep 2020 10:28:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CR8a6tMHdr0USdHEJnOukkMN6eSncqh9/9HspEdN4UY=; b=MRgm2+rKGyjkfvyujlRyBIOQT8NCbfm4KKDVDwHLRRHlD5WGV1auhwHouV2MeQI6dF zIyYkgBw2wejuJUELfkk6Ay18dTESFVPqqBPpI9zj/KvPzXv9tdlhflhOmYQUU62xw0W V29wHi2hnqK9PGB/ZYSyNBaXxVHJLBd+uo4Ec/+vUICZRFpSUr/9OFyJOQyudIL6V2uH hdv6qYn9LE3RZ6X6bsj8IXVKUViiL5MCpw0RmeZMgzefEZR96WeKni9QIlkgykEysnIF doq0McQXcdv+G9lExgusCVKbZmJoUIhrBmLSHwERahucZpyZ9a/UZ6bWgQocmkHC08s8 QhKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CR8a6tMHdr0USdHEJnOukkMN6eSncqh9/9HspEdN4UY=; b=iRvtqwYTIL4eNDScQTsWTEqrskF5FObq3Oj8XDftYBsS1wTYqsO90b/fAg0nFNMImG LO8cZpwgf58gViEmceFspEiyyFBVd/b5XUeVux6uuuczlq9nK68nmzIs9X+w3/FTHsp/ 37whP43+9bksOL75o8+WAyobCVKVOkSYX09iyqqu3KMYzV4+CrcnwgiZNTENoZoolaPB JipswsByCVo+qloaO0BR+RtJFn+j0+QL0e1/vYMkWv7/Z7eD+fe0yIPGgpbe78jyY5qf PCo9AWAlgDZZ4UnItpLWcV6a72j8P9yHA5GGq0rp7MEGBOuZyRdtC4B8EyIYzrNOqWN3 fLSw== X-Gm-Message-State: AOAM532olX1lxSyEbg4Yv95adwRjTttOS4J3LpiXlX91+zCjP0TO3f/L s56coWjRw6ZdFpoeUBvAqVSU9w== X-Received: by 2002:a5d:6283:: with SMTP id k3mr17620699wru.191.1600104503135; Mon, 14 Sep 2020 10:28:23 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id k8sm21852072wrl.42.2020.09.14.10.28.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:22 -0700 (PDT) From: George-Aurelian Popescu To: maz@kernel.org, catalin.marinas@arm.com, will@kernel.org, masahiroy@kernel.org, michal.lkml@markovi.net Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, clang-built-linux@googlegroups.com, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com, natechancellor@gmail.com, ndesaulniers@google.com, dbrazdil@google.com, broonie@kernel.org, maskray@google.com, ascull@google.com, keescook@chromium.org, akpm@linux-foundation.org, dvyukov@google.com, elver@google.com, tglx@linutronix.de, arnd@arndb.de, George Popescu Subject: [PATCH 11/14] KVM: arm64: Detect type mismatch undefined behaviour from hyp/nVHE code Date: Mon, 14 Sep 2020 17:27:47 +0000 Message-Id: <20200914172750.852684-12-georgepope@google.com> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog In-Reply-To: <20200914172750.852684-1-georgepope@google.com> References: <20200914172750.852684-1-georgepope@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: George Popescu Type mismatch undefined 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 instead of the value of the struct. 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 --- 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 b643ac9a4090..a9f499f4ef6d 100644 --- a/arch/arm64/include/asm/kvm_ubsan.h +++ b/arch/arm64/include/asm/kvm_ubsan.h @@ -23,13 +23,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; @@ -41,3 +43,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 1888a1f51724..c99d919105aa 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 01bf2171af9e..21c242c92f0a 100644 --- a/arch/arm64/kvm/kvm_ubsan_buffer.c +++ b/arch/arm64/kvm/kvm_ubsan_buffer.c @@ -36,6 +36,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; } } @@ -51,4 +55,3 @@ void __kvm_check_ubsan_buffer(void) slot->type = 0; } } - -- 2.28.0.618.gf4bc123cb7-goog