From patchwork Mon Sep 14 17:27:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774583 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 27BE46CA for ; Mon, 14 Sep 2020 17:34:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 086D2217BA for ; Mon, 14 Sep 2020 17:34:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ccbq3bhS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726258AbgINReT (ORCPT ); Mon, 14 Sep 2020 13:34:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726162AbgINR2F (ORCPT ); Mon, 14 Sep 2020 13:28:05 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8498FC06178A for ; Mon, 14 Sep 2020 10:28:04 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id t10so546020wrv.1 for ; Mon, 14 Sep 2020 10:28:04 -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=067z7eOkoIjswCFUZaQiAlz7Yh/C6SQlkfm83TpWTDI=; b=Ccbq3bhSWLvuICZcTk6r5XIHbRKedih0ZWTIzz80wN/dhQskbOMUHfhHes6S8NjK03 MdZiUUQ1RqECF0LV++t36OhqhWKQin1j4MGs2VLOVHTXD7mAlHdM1hMqdM0sm1D53f9d KkXWyEOSr43mtUVMecgZ3sBGstXl2R6MoLAtF48EkQ5IpPN2RtScfjiA74Iqd502EOpw 5LwHPOjozU2dfOg4wiCnsQJHx/5ydgKvDDJLVEeAbUJ1AomfjISseAhY1fyJhK+2EfWk JFJTokplO4+5jUEMY9SWa8pFoguuVKTfKKsEyOf0gPldU07FI17N8pzfweAg5M7GAaBI x3xg== 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=067z7eOkoIjswCFUZaQiAlz7Yh/C6SQlkfm83TpWTDI=; b=gIse+39P+j6xScTnPUbeIFhM3IIQbMWieCzD0PJGwOGaHjkWTPX5vYXw4VObIkwdz7 6StqbPbsa2zsxOQGzb8dYLPzSSKACLdPLS1l8gSJ6AXokhtDwlsXrA56Ja1HAw8D/42x s0L1hZeJCIRKte5TYhhSzFlrtOPmp7VVqp2USA6S4U2Ck+/h7hzQNnF8otSMT7JISZ8v /TOijTHJNG77jHXg8OvJVNgbdrXAVEl+RDl4aR2rdA80nPjPjFNj8DHltvYqGBZoTxrW N2U585PCkNF6odG/WfLsmn8eWZk3LwHikqXVjCy7pXgeq3ulTO3ey9u/UQ3mfI7LQX04 7gkg== X-Gm-Message-State: AOAM533WLW+NeI/HNhCxTtW7suBlWGqBQQJTygHfpehoyG2UevMzNCU9 ULx0n3VHzQSoU2RRESl87C+qGw== X-Google-Smtp-Source: ABdhPJxPTT5/y2bkI5dyBo11sTtACjcXqzQfAIFfMByvB17sbPDS6CpmIX1YCL1LK2SPPsa/YejUaQ== X-Received: by 2002:a5d:5281:: with SMTP id c1mr16890910wrv.184.1600104483118; Mon, 14 Sep 2020 10:28:03 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id y1sm19586977wma.36.2020.09.14.10.28.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:02 -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 01/14] KVM: arm64: Enable UBSan instrumentation in nVHE hyp code Date: Mon, 14 Sep 2020 17:27:37 +0000 Message-Id: <20200914172750.852684-2-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu Implement UBSan handlers inside nVHe hyp code, as empty functions for the moment, so the undefined behaviours, that are triggered there, will be linked to them, not to the ones defined in kernel-proper lib/ubsan.c. In this way, enabling UBSAN_MISC won't cause a link error. Signed-off-by: George Popescu --- arch/arm64/kvm/hyp/nvhe/Makefile | 4 +++- arch/arm64/kvm/hyp/nvhe/ubsan.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kvm/hyp/nvhe/ubsan.c diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile index aef76487edc2..cc082e516353 100644 --- a/arch/arm64/kvm/hyp/nvhe/Makefile +++ b/arch/arm64/kvm/hyp/nvhe/Makefile @@ -10,6 +10,9 @@ obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o +CFLAGS_ubsan.hyp.tmp.o += -I $(srctree)/lib/ +obj-$(CONFIG_UBSAN) += ubsan.o + obj-y := $(patsubst %.o,%.hyp.o,$(obj-y)) extra-y := $(patsubst %.hyp.o,%.hyp.tmp.o,$(obj-y)) @@ -54,7 +57,6 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG # cause crashes. Just disable it. GCOV_PROFILE := n KASAN_SANITIZE := n -UBSAN_SANITIZE := n KCOV_INSTRUMENT := n # Skip objtool checking for this directory because nVHE code is compiled with diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan.c b/arch/arm64/kvm/hyp/nvhe/ubsan.c new file mode 100644 index 000000000000..a5db6b61ceb2 --- /dev/null +++ b/arch/arm64/kvm/hyp/nvhe/ubsan.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2020 Google LLC + * Author: George Popescu + */ +#include +#include +#include + +void __ubsan_handle_add_overflow(void *_data, void *lhs, void *rhs) {} + +void __ubsan_handle_sub_overflow(void *_data, void *lhs, void *rhs) {} + +void __ubsan_handle_mul_overflow(void *_data, void *lhs, void *rhs) {} + +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_v1(void *_data, void *ptr) {} + +void __ubsan_handle_out_of_bounds(void *_data, void *index) {} + +void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs) {} + +void __ubsan_handle_builtin_unreachable(void *_data) {} + +void __ubsan_handle_load_invalid_value(void *_data, void *val) {} From patchwork Mon Sep 14 17:27:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774585 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D9CFE112E for ; Mon, 14 Sep 2020 17:34:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B492121BE5 for ; Mon, 14 Sep 2020 17:34:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uI/iQaAn" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726137AbgINReT (ORCPT ); Mon, 14 Sep 2020 13:34:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726269AbgINR2H (ORCPT ); Mon, 14 Sep 2020 13:28:07 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F23B8C06178C for ; Mon, 14 Sep 2020 10:28:06 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id w2so821225wmi.1 for ; Mon, 14 Sep 2020 10:28:06 -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=giQoIm7ww9o7WbiRNjhgzGzCrffBGGule76BQnYzoVI=; b=uI/iQaAnhz3Rb1rxk2fe04dFKCupBX6ghPL7T1uUwqbl92dUULrnTdyKTCk5O3kQ5A JRjuhoIY2N1UVpWIl9QtCEy/iGMZnNG41xPQCXLPEJpOF2Tpd6sp0KALC2AD8lgXTu2j bUhIOKUvEicaIOZsb2+fYEOfBzUZ1nKGsTGcJ7O25/87/uOAalz5qnul5IPGNfg+fEqg xwQ/11H9wYnyUwG7mCMtj8+Uj1pYNYZXNe9fanGcdlFzOTGFDaPukkC2wT/8/DrREZR2 5cSu5T12OjmTPxWAQR/2dka541vyP0ossBIVmncwMUbW/+IU+sD/AgHfEeqZLjuhn9/c PkEw== 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=giQoIm7ww9o7WbiRNjhgzGzCrffBGGule76BQnYzoVI=; b=IazLe2uLZtsY5q1Z0M8XQchWlQkoXkZP9c7Uf46a6KtclZDjrDv2ah5eYnNR8KSsXM b+3XXLClFcjGq9XHdrRYCsYea++p/oSP8PA4Pifn5JCWTGYBNuXCXoG2rhqa3gwTJAjr JcX++JrbCINNqyQzZHCcApLeNxd9tvI+9eS6ocEzvBnCHKHnWWwm5fg+CVFwg4MGK+nS Xz+wWzZcaaQDHLHZGpTVVVpta4YyKfBS6uNKhGQ5K7d6m/1l1nP48yyKC9FCS+BceYXA /Kmw10WbJtI2ZVGjGEDXPNhwwHoHwJOhnPIFloj0p2cWj4peyS9DNZAN2z3R0dJuuifJ OVaQ== X-Gm-Message-State: AOAM531zHVVNihL/e2m7ZDjgswC0LsiKV4HTM0/cvfPSeVi111H+BsHn up4WZRD4PiARB6Su62a1zToR0A== X-Google-Smtp-Source: ABdhPJzIM8L5QBOZ1G45T6rFbKm9SssKcSzPM7wAmTlp7uD3PTPktsRiE4T2L0aO6lTWC6jHotleiw== X-Received: by 2002:a1c:f608:: with SMTP id w8mr432260wmc.161.1600104485530; Mon, 14 Sep 2020 10:28:05 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id o9sm21315846wrw.58.2020.09.14.10.28.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:04 -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 02/14] KVM: arm64: Define a macro for storing a value inside a per_cpu variable Date: Mon, 14 Sep 2020 17:27:38 +0000 Message-Id: <20200914172750.852684-3-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu Use the hyp_str_this_cpu assembly macro to store a value in a per_cpu variable. This macro is designed to be used inside of the hyp code. Signed-off-by: George Popescu --- arch/arm64/include/asm/kvm_asm.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h index 6f98fbd0ac81..200bb8d0a720 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -211,6 +211,11 @@ extern char __smccc_workaround_1_smc[__SMCCC_WORKAROUND_1_SMC_SZ]; ldr \reg, [\reg, \tmp] .endm +.macro hyp_str_this_cpu sym, reg, tmp1, tmp2 + hyp_adr_this_cpu \tmp1, \sym, \tmp2 + str \reg, [\tmp1] +.endm + .macro get_host_ctxt reg, tmp hyp_adr_this_cpu \reg, kvm_host_data, \tmp add \reg, \reg, #HOST_DATA_CONTEXT From patchwork Mon Sep 14 17:27:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774579 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8471D112E for ; Mon, 14 Sep 2020 17:34:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 61823217BA for ; Mon, 14 Sep 2020 17:34:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FSOe1Y/p" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726254AbgINReE (ORCPT ); Mon, 14 Sep 2020 13:34:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726022AbgINR2J (ORCPT ); Mon, 14 Sep 2020 13:28:09 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7E2FC061351 for ; Mon, 14 Sep 2020 10:28:08 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id m6so555924wrn.0 for ; Mon, 14 Sep 2020 10:28:08 -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=I6yqR/klZ/NDqjAIsRW35d99E4ctp6zjUJ0+p6Qp1zk=; b=FSOe1Y/pA7hRXZNY72c3hBMe/ljMhIxE66K1pTUUVfwNFBvaQYXA0ufNm+qIClk/O+ vSZsvcN95MRwsi4JST1sCCLEHKfM6IxEY+MgDFP6z9uKuUgcIvWt5hwCegR+9a1Jcv+m gH8n0l5ZthmU1P5z+mETgJcgbXbDYJRY1i+0IQl2VaY5ngsnCmqitgyxHOxZkLnS/B7t UXFp+ntUx01PcgEYtHtyrccldppKaZ9Ry1m3p4Dc7kdsdGGPgDEoyTew8juouV20fTWo Q1wv6gsGClBEW0C3ySzRBVme5tXhbZYou1yowCKvdKf4SFhFjQehcpiWYtQM5a8kPB4j BwGg== 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=I6yqR/klZ/NDqjAIsRW35d99E4ctp6zjUJ0+p6Qp1zk=; b=WeG+VpSq5KFoTuOiDZmJzhi+RhwYyj0N8ygrn3DrcLw75pJ268oXe7ARKCFkkmkSBJ 1LK4hdzsgCRpBVRDn+TIkLVxt5UtJbAhX2Q1aLir2vRfksc5clFdVSb9Xen2ieGcSXlm ztiIHI5JGjP/WInRF2C2q7ODSziXSurKo2GA8R1OZVtp8lOvAZgakW+tzFy6ELojJEWC xO7B1iomHYuOcP/3Rs2wunqSEYLv+WNlluRN5MBWgCSmlHmgYKsqNXa1yS7p/QjeDzVx aDw9QoZbPcJvQVuZ7ucVyO/5LLNc8aM10knf+07iwRC/76TEgcz7v07mAfUFkf9VggtF jBOA== X-Gm-Message-State: AOAM533E8FPxAh4v4DnPSfD/SJLw6AlEF8f5Xo95fwJS/TV+U6X0QNaQ V0bv1T6FchMxjPRlCYuwQfHpOQ== X-Google-Smtp-Source: ABdhPJwcqjA/RfbM7RQw4khYb1A0QPdinAlS/u4JKeOCntEaITKsGnzpJGBE/zpzmvvaUWtI9yB3Ig== X-Received: by 2002:adf:f586:: with SMTP id f6mr16953275wro.299.1600104487527; Mon, 14 Sep 2020 10:28:07 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id h2sm22418218wrp.69.2020.09.14.10.28.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:06 -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 03/14] KVM: arm64: Add support for creating and checking a logging buffer inside hyp/nVHE Date: Mon, 14 Sep 2020 17:27:39 +0000 Message-Id: <20200914172750.852684-4-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu Share a buffer between the kernel and the hyp/nVHE code by using the macros from kvm_debug_buffer.h. The buffer is composed of a writing index and a statically allocated array. The writing index counts how many elements have been written inside the buffer and should be set to zero whenever the code goes back to EL2 with the clear_kvm_debug_buffer macro. To avoid consistency problems the buffer is defined per_cpu and is designed to be read-only from the kernel perspective. Check if there is any logging data from hyp/nVHE code. Every time when the state returns back to the kernel after an hvc call, the __kvm_arm_check_debug_buffer macro checks if there is any data inside one of the predefined buffers. Signed-off-by: George Popescu --- arch/arm64/include/asm/kvm_debug_buffer.h | 34 +++++++++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 6 ++++ arch/arm64/kvm/hyp/hyp-entry.S | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_debug_buffer.h diff --git a/arch/arm64/include/asm/kvm_debug_buffer.h b/arch/arm64/include/asm/kvm_debug_buffer.h new file mode 100644 index 000000000000..30c9b0b1a7bf --- /dev/null +++ b/arch/arm64/include/asm/kvm_debug_buffer.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright 2020 Google LLC + * Author: George Popescu + */ +#ifndef __ASSEMBLY__ + +#include +#include + +#ifdef __KVM_NVHE_HYPERVISOR__ +#define DEFINE_KVM_DEBUG_BUFFER(type_name, buff_name, size) \ + DEFINE_PER_CPU(type_name, buff_name)[(size)]; \ + DEFINE_PER_CPU(unsigned long, buff_name##_wr_ind) = 0 + +#define DECLARE_KVM_DEBUG_BUFFER(type_name, buff_name, size) \ + DECLARE_PER_CPU(type_name, buff_name)[(size)]; \ + DECLARE_PER_CPU(unsigned long, buff_name##_wr_ind) + +#else + +#define DECLARE_KVM_DEBUG_BUFFER(type_name, buff_name, size) \ + DECLARE_PER_CPU(type_name, kvm_nvhe_sym(buff_name))[(size)]; \ + DECLARE_PER_CPU(unsigned long, kvm_nvhe_sym(buff_name##_wr_ind)) +#endif //__KVM_NVHE_HYPERVISOR__ + +#else + +.macro clear_kvm_debug_buffer sym tmp1, tmp2, tmp3 + mov \tmp1, 0 + hyp_str_this_cpu \sym, \tmp1, \tmp2, \tmp3 +.endm + +#endif // __ASSEMBLY__ diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 905c2b87e05a..adc8957e9321 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -494,6 +494,10 @@ u64 __kvm_call_hyp(void *hypfn, ...); __kvm_call_hyp(kvm_ksym_ref_nvhe(f), ##__VA_ARGS__); \ }) +#define __kvm_arm_check_debug_buffer() \ +{ \ +} + /* * The couple of isb() below are there to guarantee the same behaviour * on VHE as on !VHE, where the eret to EL1 acts as a context @@ -506,6 +510,7 @@ u64 __kvm_call_hyp(void *hypfn, ...); isb(); \ } else { \ kvm_call_hyp_nvhe(f, ##__VA_ARGS__); \ + __kvm_arm_check_debug_buffer(); \ } \ } while(0) @@ -518,6 +523,7 @@ u64 __kvm_call_hyp(void *hypfn, ...); isb(); \ } else { \ ret = kvm_call_hyp_nvhe_ret(f, ##__VA_ARGS__); \ + __kvm_arm_check_debug_buffer(); \ } \ \ ret; \ diff --git a/arch/arm64/kvm/hyp/hyp-entry.S b/arch/arm64/kvm/hyp/hyp-entry.S index 46b4dab933d0..8df0082b9ccf 100644 --- a/arch/arm64/kvm/hyp/hyp-entry.S +++ b/arch/arm64/kvm/hyp/hyp-entry.S @@ -68,7 +68,7 @@ el1_sync: // Guest trapped into EL2 cbnz x1, el1_hvc_guest // called HVC /* Here, we're pretty sure the host called HVC. */ - ldp x0, x1, [sp], #16 + ldp x0, x1, [sp], #16 /* Check for a stub HVC call */ cmp x0, #HVC_STUB_HCALL_NR From patchwork Mon Sep 14 17:27:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774537 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C9E89112E for ; Mon, 14 Sep 2020 17:29:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A79F320E65 for ; Mon, 14 Sep 2020 17:29:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QYAJeh3Q" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726056AbgINR2x (ORCPT ); Mon, 14 Sep 2020 13:28:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726171AbgINR2Q (ORCPT ); Mon, 14 Sep 2020 13:28:16 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 116CDC06174A for ; Mon, 14 Sep 2020 10:28:11 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id z9so866848wmk.1 for ; Mon, 14 Sep 2020 10:28:10 -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=f1Yic7ZFZnWbBrZg7HASMUx7n5dssR2OZo/o20looTY=; b=QYAJeh3Q+9jHTWY5pAYKVdbC19xxu8ncb0TEqMua4SAPTHthmWkX9+Px4bz0H26kWs 6I/8bZ4ODyxi7WH5k6f6b+s9kmbHCeTl+tfRlX0bnULiJo010jodXWbFD8SVsMqwup4I ranJQ2a5yOZvCCFDvOUspxSVCwVf8U0EaBG4mpE+5Gn9jVn3kJa4JLb9x4guXwj5i4rb GvPiDjVjO/NaUXS7cSrcp1NQTKBiN7a0tz4VWC74xeF0p8I/9vSbS067MRKNUduneB+J zohifeHywe3Gjddluj1vmPUNN9NQBRDr1Bb2lSSSuwWAXQ9XfUh5b/vXVJKRsaf4p2Kc ZvMw== 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=f1Yic7ZFZnWbBrZg7HASMUx7n5dssR2OZo/o20looTY=; b=pvoNoKYrEAgArNp1HdCtCtQOc8NbA0JFJwtlh+XGYe9P+EA+3Fn/0gsK3vfK7Mt6eN 1xhij0J4UUYcWwIOUeKbuDSAqjfnAV4Unbrvj4yitkwuMdYIopN7f5/pfP569TPQJd30 G3EovgOiO3ejs1bIj67y0NDc7UOX+XUU1tkiUzfg8jlHMuK3R2YTa6Qdw9ObRiJgaIbY yxUDPdq+/3xV2GFRnqmNyVLaKW6RQLNNs21tkhD/VgDXWVJIRFsf8i3vZapEbli3YaZf uu4vzIDurQkfg0R8o97rwdVDHNkv6WOTGTLU3qutZ+MuLNE/qxrEuGt8UI5K1h9fVaZH Z+Eg== X-Gm-Message-State: AOAM533+sI2RO9FoVJsTPiicJRYjkiZvHSzsohYNXoVDwBROjPH0F0jq pIgyHrLvlyLj5m+FbvOxk1o5xQ== X-Google-Smtp-Source: ABdhPJwVPwGHg4dpWPy9oWTZbgeAo0d2o5bEHs5LmN11i0q+vogMdLJSPpEQ4lXgcBg5Y9aKVbtpdg== X-Received: by 2002:a7b:c3da:: with SMTP id t26mr422196wmj.23.1600104489670; Mon, 14 Sep 2020 10:28:09 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id u126sm23949690wmu.9.2020.09.14.10.28.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:08 -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 04/14] KVM: arm64: Add support for buffer usage Date: Mon, 14 Sep 2020 17:27:40 +0000 Message-Id: <20200914172750.852684-5-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu Use init_kvm_debug_buffer macro to init a pointer to the kvm_debug_buffer and a pointer to the write_index. It is needed a hyp/nVHE version and a kernel version because there are required different functions to extract the per_cpu data. Iterate through the buffer using the for_each_kvm_debug_buffer_slot. The parameters are the buffer's name, the buffer's type, a pointer of the type of the buffer, which is used to iterate through it, an (unsigned long *) to compute the write index and an unsigned long iterator. Get the buffer's next empty slot using the kvm_debug_buffer_next_slot function, the required parameters are a pointer to the buffer start, a pointer to the writing index, the stored type size and the allocated size of the buffer. This function has a meaning only inside hyp/nVHE, because it shouldn't be possible to write inside the buffer from the kernel. Signed-off-by: George Popescu --- arch/arm64/include/asm/kvm_debug_buffer.h | 31 +++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_debug_buffer.h b/arch/arm64/include/asm/kvm_debug_buffer.h index 30c9b0b1a7bf..e451c11a77a7 100644 --- a/arch/arm64/include/asm/kvm_debug_buffer.h +++ b/arch/arm64/include/asm/kvm_debug_buffer.h @@ -17,10 +17,37 @@ DECLARE_PER_CPU(type_name, buff_name)[(size)]; \ DECLARE_PER_CPU(unsigned long, buff_name##_wr_ind) +static inline void *kvm_debug_buffer_next_slot(void *buff, unsigned long *buff_ind, + unsigned int struct_size, unsigned long buff_size) +{ + void *res = NULL; + + if (*buff_ind < buff_size) { + res = buff + (*buff_ind * struct_size); + *buff_ind = *buff_ind + 1; + } + return res; +} + +#define init_kvm_debug_buffer(buff_name, buff_type, buff_pointer, write_ind) \ + do { \ + buff = (buff_type *) __hyp_this_cpu_ptr(buff_name); \ + buff_ind = (unsigned long *) __hyp_this_cpu_ptr(buff_name##_wr_ind); \ + } while (0) + #else -#define DECLARE_KVM_DEBUG_BUFFER(type_name, buff_name, size) \ - DECLARE_PER_CPU(type_name, kvm_nvhe_sym(buff_name))[(size)]; \ +#define init_kvm_debug_buffer(buff_name, buff_type, buff_pointer, write_ind) \ + do { \ + buff_pointer = (buff_type *) this_cpu_ptr_nvhe(buff_name); \ + write_ind = (unsigned long *) this_cpu_ptr_nvhe(buff_name##_wr_ind); \ + } while (0) + +#define for_each_kvm_debug_buffer_slot(slot, write_ind, it) \ + for ((it) = 0; (it) < *(write_ind); ++(it), ++(slot)) + +#define DECLARE_KVM_DEBUG_BUFFER(type_name, buff_name, size) \ + DECLARE_PER_CPU(type_name, kvm_nvhe_sym(buff_name))[(size)]; \ DECLARE_PER_CPU(unsigned long, kvm_nvhe_sym(buff_name##_wr_ind)) #endif //__KVM_NVHE_HYPERVISOR__ From patchwork Mon Sep 14 17:27:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774533 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1361A112E for ; Mon, 14 Sep 2020 17:28:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DE9CA217BA for ; Mon, 14 Sep 2020 17:28:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="amPh1yVy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726260AbgINR2n (ORCPT ); Mon, 14 Sep 2020 13:28:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726273AbgINR2Y (ORCPT ); Mon, 14 Sep 2020 13:28:24 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B95BC061355 for ; Mon, 14 Sep 2020 10:28:13 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id s12so466099wrw.11 for ; Mon, 14 Sep 2020 10:28:13 -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=/4VwXgJoMbk9POxFRHy1vRuuoE0F0rYUfbOkZd2TWH0=; b=amPh1yVyk5hsom+N7frn7ZaaN2fdVT09fNxiF+g8sBUUwDxE8gwytpL5RaaqpAKPXL K1P6UwD+bgpwaOVGkDSH8HhKaO6zCvViC3tpU9Zpuwn4OcPoiiGXGBoW2R0EoC8v4rKy EGoQGj3Dg1w2+f36a7/lnrpY/1iFHmPlqbhDSmo0O+MGsGmZGM6pqptzDnasinwhANLj 4Fc/V4MVAloP8rQ590HmNMl95HI36WRgEGjmpAubcTv73WDPz2iZNfd5xPgaOkDzx8TR RterKOwezbCeHnyORfnJPbeyY0EZq9QAv2mPqglRAYdyEvVDulzE9OeK3oSzuwgvg5TQ 6agA== 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=/4VwXgJoMbk9POxFRHy1vRuuoE0F0rYUfbOkZd2TWH0=; b=tTDb8NZFijTafORfCnaophDeux/j11Sehv3fBHn+Z19I7V65k10OI5mNcltOpvMlnJ bMBLZNNkWbgOENHyjwXpJGAT78Za6UNpGatM/DAxncZGULX9bFJBcWTpbi+YWg6ice2E 5uvnPbhAR9DhePGHWNkI9QuPoO/47UTC/blO5G4f7f1A/WJO2qzZeKhQzRFCcCqWqy4I mMvTxpWDUHyHgkE1vdXWH1DVYSe2yACjVo3++x6IOupp3TH3njZn2WFWttI8rmacdnIX 2Dg7pirVMSwGutq3sH4doFurT+jxbnLlkLGG7FI/oboZqd/lJrh6sqSZnBqNAr1G2qKM xjJg== X-Gm-Message-State: AOAM530HHcEQiUIlankhu6ev4pXpi7ld7g1RJogs/oZkbQyrAE12rKQd AP1sHM1QwP8ZmwRJr8jlFGslug== X-Google-Smtp-Source: ABdhPJzjIpl24eBuXRk5Xw2CVqvs2BB+r9DeJ7mZjJ1acd5LET7e/D2xbtzn/nEHsYfgyEIUS5e5Mg== X-Received: by 2002:adf:8b48:: with SMTP id v8mr17059052wra.21.1600104491563; Mon, 14 Sep 2020 10:28:11 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id h16sm22935608wre.87.2020.09.14.10.28.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:10 -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 05/14] KVM: arm64: Define a buffer that can pass UBSan data from hyp/nVHE to kernel Date: Mon, 14 Sep 2020 17:27:41 +0000 Message-Id: <20200914172750.852684-6-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu Store data, which is collected from UBSan handlers that lives inside hyp/nVHE, into the kvm_ubsan_buffer. This buffer is designed to store only UBSan data because it should not be preoccupied by other mechanisms data structures and functionalities. Map the buffer and the write index before switching the control to hyp/nVHE. Map the kernel .data region to read the compile time generated UBSan struct's data from hyp/nVHE. Signed-off-by: George Popescu --- arch/arm64/include/asm/kvm_asm.h | 3 +++ arch/arm64/include/asm/kvm_host.h | 6 +++++ arch/arm64/include/asm/kvm_ubsan.h | 17 +++++++++++++ arch/arm64/kvm/Makefile | 4 ++++ arch/arm64/kvm/arm.c | 38 +++++++++++++++++++++++++++++- arch/arm64/kvm/hyp/hyp-entry.S | 4 ++++ arch/arm64/kvm/hyp/nvhe/ubsan.c | 24 ++++++++++++++++++- arch/arm64/kvm/kvm_ubsan_buffer.c | 32 +++++++++++++++++++++++++ 8 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 arch/arm64/include/asm/kvm_ubsan.h create mode 100644 arch/arm64/kvm/kvm_ubsan_buffer.c diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h index 200bb8d0a720..9d4a77f08ffd 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -63,6 +63,9 @@ #define CHOOSE_VHE_SYM(sym) sym #define CHOOSE_NVHE_SYM(sym) kvm_nvhe_sym(sym) +#define this_cpu_ptr_nvhe(sym) this_cpu_ptr(&kvm_nvhe_sym(sym)) +#define per_cpu_ptr_nvhe(sym, cpu) per_cpu_ptr(&kvm_nvhe_sym(sym), cpu) + #ifndef __KVM_NVHE_HYPERVISOR__ /* * BIG FAT WARNINGS: diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index adc8957e9321..337fd2d0f976 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -494,8 +494,14 @@ u64 __kvm_call_hyp(void *hypfn, ...); __kvm_call_hyp(kvm_ksym_ref_nvhe(f), ##__VA_ARGS__); \ }) +#ifdef CONFIG_UBSAN +extern void __kvm_check_ubsan_buffer(void); +#endif + #define __kvm_arm_check_debug_buffer() \ { \ + if (IS_ENABLED(CONFIG_UBSAN)) \ + __kvm_check_ubsan_buffer(); \ } /* diff --git a/arch/arm64/include/asm/kvm_ubsan.h b/arch/arm64/include/asm/kvm_ubsan.h new file mode 100644 index 000000000000..af607a796376 --- /dev/null +++ b/arch/arm64/include/asm/kvm_ubsan.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright 2020 Google LLC + * Author: George Popescu + */ + +#ifdef CONFIG_UBSAN +#include + + +#define UBSAN_MAX_TYPE 6 +#define KVM_UBSAN_BUFFER_SIZE 1000 + +struct kvm_ubsan_info { + int type; +}; +#endif diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index 99977c1972cc..92f06cb5b3df 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -24,4 +24,8 @@ kvm-y := $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ vgic/vgic-its.o vgic/vgic-debug.o +CFLAGS_kvm_ubsan_buffer.o += -I $(srctree)/lib/ +CFLAGS_arm.o += -I $(srctree)/lib + +kvm-$(CONFIG_UBSAN) += kvm_ubsan_buffer.o kvm-$(CONFIG_KVM_ARM_PMU) += pmu-emul.o diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index b588c3b5c2f0..eff57069e103 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -42,10 +42,17 @@ #include #include +#include +#include + #ifdef REQUIRES_VIRT __asm__(".arch_extension virt"); #endif +#ifdef CONFIG_UBSAN +DECLARE_KVM_DEBUG_BUFFER(struct kvm_ubsan_info, kvm_ubsan_buff, KVM_UBSAN_BUFFER_SIZE); +#endif + DEFINE_PER_CPU(kvm_host_data_t, kvm_host_data); static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); @@ -1519,7 +1526,15 @@ static int init_hyp_mode(void) kvm_err("Cannot map bss section\n"); goto out_err; } - +#ifdef CONFIG_UBSAN + /* required by ubsan to access the handlers structures fields */ + err = create_hyp_mappings(kvm_ksym_ref(_data), + kvm_ksym_ref(__end_once), PAGE_HYP_RO); + if (err) { + kvm_err("Cannot map data section\n"); + goto out_err; + } +#endif err = kvm_map_vectors(); if (err) { kvm_err("Cannot map vectors\n"); @@ -1552,6 +1567,27 @@ static int init_hyp_mode(void) } } +#ifdef CONFIG_UBSAN + for_each_possible_cpu(cpu) { + /* map the write index */ + struct kvm_ubsan_info *buff; + unsigned long *wr_ind; + + wr_ind = per_cpu_ptr_nvhe(kvm_ubsan_buff_wr_ind, cpu); + err = create_hyp_mappings(wr_ind, wr_ind + 1, PAGE_HYP); + if (err) { + kvm_err("Cannot map the busan buffer write index: %d\n", err); + goto out_err; + } + buff = per_cpu_ptr(kvm_nvhe_sym(kvm_ubsan_buff), cpu); + err = create_hyp_mappings(buff, buff + KVM_UBSAN_BUFFER_SIZE, PAGE_HYP); + if (err) { + kvm_err("Cannot map the ubsan buffer: %d\n", err); + goto out_err; + } + } +#endif + err = hyp_map_aux_data(); if (err) kvm_err("Cannot map host auxiliary data: %d\n", err); diff --git a/arch/arm64/kvm/hyp/hyp-entry.S b/arch/arm64/kvm/hyp/hyp-entry.S index 8df0082b9ccf..bcdbab4d2e43 100644 --- a/arch/arm64/kvm/hyp/hyp-entry.S +++ b/arch/arm64/kvm/hyp/hyp-entry.S @@ -14,6 +14,7 @@ #include #include #include +#include .macro save_caller_saved_regs_vect /* x0 and x1 were saved in the vector entry */ @@ -74,6 +75,9 @@ el1_sync: // Guest trapped into EL2 cmp x0, #HVC_STUB_HCALL_NR b.hs 1f +#ifdef CONFIG_UBSAN + clear_kvm_debug_buffer kvm_ubsan_buff_wr_ind, x4, x5, x6 +#endif /* * Compute the idmap address of __kvm_handle_stub_hvc and * jump there. Since we use kimage_voffset, do not use the diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan.c b/arch/arm64/kvm/hyp/nvhe/ubsan.c index a5db6b61ceb2..a43c9646e1e8 100644 --- a/arch/arm64/kvm/hyp/nvhe/ubsan.c +++ b/arch/arm64/kvm/hyp/nvhe/ubsan.c @@ -3,9 +3,31 @@ * Copyright 2020 Google LLC * Author: George Popescu */ +#include #include #include -#include +#include +#include +#include +#include +#include +#include +#include + +DEFINE_KVM_DEBUG_BUFFER(struct kvm_ubsan_info, kvm_ubsan_buff, KVM_UBSAN_BUFFER_SIZE); + +static inline struct kvm_ubsan_info *kvm_ubsan_buffer_next_slot(void) +{ + struct kvm_ubsan_info *res; + struct kvm_ubsan_info *buff; + unsigned long *buff_ind; + unsigned long buff_size = KVM_UBSAN_BUFFER_SIZE; + unsigned int struct_size = sizeof(struct kvm_ubsan_info); + + init_kvm_debug_buffer(kvm_ubsan_buff, struct kvm_ubsan_info, buff, buff_ind); + res = kvm_debug_buffer_next_slot(buff, buff_ind, struct_size, buff_size); + return res; +} void __ubsan_handle_add_overflow(void *_data, void *lhs, void *rhs) {} diff --git a/arch/arm64/kvm/kvm_ubsan_buffer.c b/arch/arm64/kvm/kvm_ubsan_buffer.c new file mode 100644 index 000000000000..28dcf19b5706 --- /dev/null +++ b/arch/arm64/kvm/kvm_ubsan_buffer.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2020 Google LLC + * Author: George Popescu + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +DECLARE_KVM_DEBUG_BUFFER(struct kvm_ubsan_info, kvm_ubsan_buff, KVM_UBSAN_BUFFER_SIZE); + + +void __kvm_check_ubsan_buffer(void) +{ + unsigned long *write_ind; + unsigned long it; + struct kvm_ubsan_info *slot; + + 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 */ + slot->type = 0; + } +} + From patchwork Mon Sep 14 17:27:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774581 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 366B26CA for ; Mon, 14 Sep 2020 17:34:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1AA1520E65 for ; Mon, 14 Sep 2020 17:34:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ihcd/I42" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726011AbgINReB (ORCPT ); Mon, 14 Sep 2020 13:34:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbgINR2Z (ORCPT ); Mon, 14 Sep 2020 13:28:25 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDCABC0612F2 for ; Mon, 14 Sep 2020 10:28:15 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id d4so794712wmd.5 for ; Mon, 14 Sep 2020 10:28:15 -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=Ev8PHeG2wBS2zSKo7R1VQ4GKo6oUzjZvdtqCLwJ3pDs=; b=Ihcd/I42/8bqTmauvPs6wDsvnxOAZx+wYOevPiQvp+xJchPV5GVJkIShVJHhNU2FjO Zf1FpuxhmFlq6/3mUCs6IS98Ps2pku3dV/3fh5ECJ4gDZPm9+Ct13ppA30R3IhTtGzSd KyjRpOxXeBwr5vS+ttAyiOJy1txvnpeI7w+7wvzy7JfFhvY/m6Hyiw0ksxJxvBH1TKmw CpSNjsUVu0fP6g5CeuWF6cI5+zgTBD8gSJA/WEILjUZoiAXavXc7DnpojnXNHupGQsvG s7eZ41WyrXa0IMZ4lhH1LzLbNKcQgdbq0M8s5dYCdEOag6uxg2zPjBYciaEkq0N6363O Svhw== 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=Ev8PHeG2wBS2zSKo7R1VQ4GKo6oUzjZvdtqCLwJ3pDs=; b=HCsXrHsAGSC02/H8Y5qHfoxhB93tbXoTqVL8e6w47eE1etBIDuIXj5jb/k1hE50Zi3 D1aBlX8lBdTVTJ9+gVC2huA2zpP3SBiAxd4BEYxgd/VGQ0vDosxNyEz/6d1iACxfL5RW R6yeykwPs2wm8R90XSnuqAxwt2zS+QaI23ZCWs9WQBP+YsJSqya8bJVXDRIMaPjsB7MV TCW8N2Lt0mMw+ZjsGwBg59tLPxo8gmAIReVc04IbSCEdEJLF8ux5SeUYzt9tIBxmjSF0 7PLQXdb8sp5Ls6yhDn0xIH19TLbyPNNGBbuySYMOc2Xej3M3Z1OKvs7HJX1OzI3jJcZ3 8HlA== X-Gm-Message-State: AOAM532l3tYdFFNshRrWG57gylXaluYN3bOvjHMrXmZGGcZHvLdoezHo hyTMsXkyRFZVZxrO8xLcqZY8Yw== X-Google-Smtp-Source: ABdhPJwtapIR59wfa+PjunnI9DEEbaEmNaoIsHmxwFfZkq+7/rS32R8lqozJpnsFIN4Urr9dofZ6Kw== X-Received: by 2002:a1c:f619:: with SMTP id w25mr459880wmc.62.1600104493518; Mon, 14 Sep 2020 10:28:13 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id s12sm13377783wmd.20.2020.09.14.10.28.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:12 -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 06/14] Fix CFLAGS for UBSAN_BOUNDS on Clang Date: Mon, 14 Sep 2020 17:27:42 +0000 Message-Id: <20200914172750.852684-7-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu When the kernel is compiled with Clang, UBSAN_BOUNDS inserts a brk after the handler call, preventing it from printing any information processed inside the buffer. For Clang -fsanitize=bounds expands to -fsanitize=array-bounds and -fsanitize=local-bounds, and the latter adds a brk after the handler call Signed-off-by: George Popescu --- scripts/Makefile.ubsan | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/Makefile.ubsan b/scripts/Makefile.ubsan index 27348029b2b8..3d15ac346c97 100644 --- a/scripts/Makefile.ubsan +++ b/scripts/Makefile.ubsan @@ -4,7 +4,14 @@ ifdef CONFIG_UBSAN_ALIGNMENT endif ifdef CONFIG_UBSAN_BOUNDS - CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds) + # For Clang -fsanitize=bounds translates to -fsanitize=array-bounds and + # -fsanitize=local-bounds; the latter adds a brk right after the + # handler is called. + ifdef CONFIG_CC_IS_CLANG + CFLAGS_UBSAN += $(call cc-option, -fsanitize=array-bounds) + else + CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds) + endif endif ifdef CONFIG_UBSAN_MISC From patchwork Mon Sep 14 17:27:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774577 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 99A2C112E for ; Mon, 14 Sep 2020 17:34:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 76FF220E65 for ; Mon, 14 Sep 2020 17:34:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tSsBMUWs" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726044AbgINRd4 (ORCPT ); Mon, 14 Sep 2020 13:33:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726170AbgINR22 (ORCPT ); Mon, 14 Sep 2020 13:28:28 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0C49C06121C for ; Mon, 14 Sep 2020 10:28:16 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id g4so515884wrs.5 for ; Mon, 14 Sep 2020 10:28:16 -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=VE4tiaYIO2WH178ygWtKyTutOHC4+8KlDDbyhHM4F14=; b=tSsBMUWsMvg8+rPbGLC3iYKX0M9xTuJCgz8t9VzFY/lPy2b722XAFrOELq7cSNpIxk N9S5vrz2VcmU/j3WqAfHtIo7s+KuY8ak7B/tAU5NAvf54fH1rh0GHPJ6ElKhZGk3Mdw2 ZyUng2UvYJ18NYurvGMI2vrPFIihzVaGyJOS8jOH2ePwigt8T84XvtwVOp5ByrQLnaXd SxgwfYEnrlmxDAAm/fq1xJgaWOPKHUMNCKDRSG5G7oYbjiJGUMoAY65TE+vuPGm9bag9 Lh14EdLD9IpUB/n0nxNhI+Mj+Hx5DMLpZMQ3zlrmushDlXv2bFwUMuUlbzXDmKDYSRNl l23w== 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=VE4tiaYIO2WH178ygWtKyTutOHC4+8KlDDbyhHM4F14=; b=YhMJm/jv37ZI93ciBMBtVlRFp5a1wjbEkeeJ5nPAgmW88JoQw/rjjZ2yupX0mUKVzI czw3eX3WRbg19PtMv5UB+7QJsaIz8B+FvD+MgL14TMJTHCxFP523qAVexH5FvGsM9Njp yFYmz/dzStTZGBe+nKatIRrGLLWXzLV37N0O18OK8nIboUWA4N2+VhklDyUMEjZwmpwr DP/z3Zk5N8NG2eQuiSnPbJicDMMPmP9ZeuxBvATLEmEjX2QWre1tZadKAWgKG/1tre2e U3OaJgeaokkOsH2bWdw+sX5D5E2/lfWhJao3LiDyMhX3L29kFCHm9y3lfVueS/o8NXhp T1fw== X-Gm-Message-State: AOAM5329vx70hYSVnVqqCYBc4Nl4r+FY/SP0mmeUdmROtBUysqxM2uWX Ba2lWe8avgCqtcOzD6jYs3wnQw== X-Google-Smtp-Source: ABdhPJwDp0qDmVhRrvPNjBrZkX56Whr339QzJq0eZ5FLOChOIB1ToFOdRxzoHsQ+yPW+W6ciFtGbiw== X-Received: by 2002:a5d:50c3:: with SMTP id f3mr16765517wrt.125.1600104495500; Mon, 14 Sep 2020 10:28:15 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id e1sm15404359wrp.49.2020.09.14.10.28.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:14 -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 07/14] KVM: arm64: Enable UBSAN_BOUNDS for the both the kernel and hyp/nVHE Date: Mon, 14 Sep 2020 17:27:43 +0000 Message-Id: <20200914172750.852684-8-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu 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 --- 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; } } From patchwork Mon Sep 14 17:27:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774575 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E0D776CA for ; Mon, 14 Sep 2020 17:34:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BA8B1217BA for ; Mon, 14 Sep 2020 17:34:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LDOJW73/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726034AbgINRdz (ORCPT ); Mon, 14 Sep 2020 13:33:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726254AbgINR22 (ORCPT ); Mon, 14 Sep 2020 13:28:28 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2E38C06121D for ; Mon, 14 Sep 2020 10:28:18 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id q9so815336wmj.2 for ; Mon, 14 Sep 2020 10:28:18 -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=/EjNih3r7ygkjYASNBWp7YtEX1KLL1OovUvtOR4yQ8E=; b=LDOJW73/Nx4NRSF62nukRv0fkeOiLGfPXVP1aJhDKCMUKmk3HmHAf6NWlyYj1Fc30H 4ctb86ijJJ+PMUx7pA1+2eEIf+G0cmjOvbtE9oBe55C3Cvn1qkgI3USj0yAzKEsPU9q0 Kg6zWbX13Nvf35fM5FUm0vCKYp5AV89AlTGoxiq/9SNtkV9H5JeJZSjohjU7p9UCPTCc AFzF0SLwziqh80xl5uTlhqNQPcFqdMho3+ObPMEgdgw7XZugj3HlUg9gMeuZ9Wl5vn3G sEIZ2338YOSdEnkx35Dj1zujfiD6uqhHIIRpeBrFBUJeHIahNLU31ZLtfIcNVZWFj6tG ir0Q== 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=/EjNih3r7ygkjYASNBWp7YtEX1KLL1OovUvtOR4yQ8E=; b=F4aaHuhPxDe4XoGadbv4HrAzY2KfRCR2/KyLkLPjxbrH2oVMMMA9MhgE5xL6qA3J/0 KvSb/F7GZxvoLpjZzvsXPgTuxWyjJs3DvXfad+KW2RzHGhzAEfWL8qC/YAxZUfX6z6u5 zOjPbB8ppDh5rkm4nImEvrI3VITnWk5EeCTZTo3HlqpIkxy3EFNnzaG7S7+B52d3D6hJ iRCrBUt8mjtbKPCBL4akV152BhObkrtcWypwHgycR7tGWy+8Sf4rwGZgqWZvEYPzxxYq tQFBfu6ZLJAeT2uVz90ekiwwAQSz2+Zsp8261bpCbAgphy1zuaNC0MEADc8dKgcsIiws nzTg== X-Gm-Message-State: AOAM5334TkBYUKEmz1uGStADTUKJeF4ctyPzaPxj5HlRT4mTEEmsEKLo uInkK2XU/uWRAU0DKok3hJwH2Q== X-Google-Smtp-Source: ABdhPJz7n/8ZzKopjoksEbonJHe8/t/B9RylxHy4LiUHqu75FJ9ZhVdcDVjP7yVogd6IknutagEtqw== X-Received: by 2002:a1c:a551:: with SMTP id o78mr486952wme.4.1600104497464; Mon, 14 Sep 2020 10:28:17 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id a17sm22898300wra.24.2020.09.14.10.28.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:16 -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 08/14] KVM: arm64: Enable UBsan check for unreachable code inside hyp/nVHE code Date: Mon, 14 Sep 2020 17:27:44 +0000 Message-Id: <20200914172750.852684-9-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu The data from __ubsan_handle_builtin_unreachable is passed to the buffer and printed inside the kernel by its symmetric handler. Signed-off-by: George Popescu --- arch/arm64/include/asm/kvm_ubsan.h | 5 ++++- arch/arm64/kvm/hyp/nvhe/ubsan.c | 12 +++++++++++- arch/arm64/kvm/kvm_ubsan_buffer.c | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_ubsan.h b/arch/arm64/include/asm/kvm_ubsan.h index 575881e0bd5f..7fd0d0dfbd82 100644 --- a/arch/arm64/include/asm/kvm_ubsan.h +++ b/arch/arm64/include/asm/kvm_ubsan.h @@ -20,10 +20,12 @@ struct ubsan_values { struct kvm_ubsan_info { enum { UBSAN_NONE, - UBSAN_OUT_OF_BOUNDS + UBSAN_OUT_OF_BOUNDS, + UBSAN_UNREACHABLE_DATA } type; union { struct out_of_bounds_data out_of_bounds_data; + struct unreachable_data unreachable_data; }; union { struct ubsan_values u_val; @@ -32,3 +34,4 @@ struct kvm_ubsan_info { #endif void __ubsan_handle_out_of_bounds(void *_data, void *index); +void __ubsan_handle_builtin_unreachable(void *_data); diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan.c b/arch/arm64/kvm/hyp/nvhe/ubsan.c index b2d3404f6215..9497e7f7f397 100644 --- a/arch/arm64/kvm/hyp/nvhe/ubsan.c +++ b/arch/arm64/kvm/hyp/nvhe/ubsan.c @@ -58,6 +58,16 @@ void __ubsan_handle_out_of_bounds(void *_data, void *index) void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs) {} -void __ubsan_handle_builtin_unreachable(void *_data) {} +void __ubsan_handle_builtin_unreachable(void *_data) +{ + struct kvm_ubsan_info *slot; + struct unreachable_data *data = _data; + + slot = kvm_ubsan_buffer_next_slot(); + if (slot) { + slot->type = UBSAN_UNREACHABLE_DATA; + slot->unreachable_data = *data; + } +} void __ubsan_handle_load_invalid_value(void *_data, void *val) {} diff --git a/arch/arm64/kvm/kvm_ubsan_buffer.c b/arch/arm64/kvm/kvm_ubsan_buffer.c index ce796bdd027e..f66cc5f7878e 100644 --- a/arch/arm64/kvm/kvm_ubsan_buffer.c +++ b/arch/arm64/kvm/kvm_ubsan_buffer.c @@ -25,6 +25,9 @@ void __kvm_check_ubsan_data(struct kvm_ubsan_info *slot) __ubsan_handle_out_of_bounds(&slot->out_of_bounds_data, slot->u_val.lval); break; + case UBSAN_UNREACHABLE_DATA: + __ubsan_handle_builtin_unreachable(&slot->unreachable_data); + break; } } From patchwork Mon Sep 14 17:27:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774573 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3BBBB6CA for ; Mon, 14 Sep 2020 17:33:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1B5F0217BA for ; Mon, 14 Sep 2020 17:33:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EYgWAcl9" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726171AbgINRdv (ORCPT ); Mon, 14 Sep 2020 13:33:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726291AbgINR2c (ORCPT ); Mon, 14 Sep 2020 13:28:32 -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 CA199C06121F for ; Mon, 14 Sep 2020 10:28:20 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id a17so506671wrn.6 for ; Mon, 14 Sep 2020 10:28:20 -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=KftJRcollRLFMVov7TaPj7WtBHwyxRPUsiHY/UZT1fw=; b=EYgWAcl9cL9AeiTblL7CMC+AqQx78U6/I1W7VW7Ja5I15I0jt+My8KPdhD1SAVpEHD AJXt5LuKrD0WR0bw/B1exIDwe7tkOX43hf5qS4uwiYCJAEU/FA1aTxXb7U22+RLK0Gw5 IEqZhFqCrBcnVwsvqPgPtUtdGHrW6CU24YKBfkwIy52kBZc31aRqkrNTC7FpmXQy/eQ5 dVCJRFM3SOoQVUXjSfMIKlDXY7aS2d0IgN2BzjI6mWTiBBzhjwhcJRJy/vKgdde8y6Km iNWk73Vy+veU+2hjy0D/PR5vfHhuUmQkzEOLnrIPC2sx7KQNQCejEB2d78t3JnwXXpZV WvtQ== 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=KftJRcollRLFMVov7TaPj7WtBHwyxRPUsiHY/UZT1fw=; b=FWgNXA4K7RtFr0iC6rbKfvdA172k/z1ltwqnsz8TINHuObezbhS6PLllVJoniL4fSo 7PI1XHiS49szpLBMr+I957lJyFNOx0OmXM/a35eDyQVCxDqpFwRCGcfg4vSiy5+77FQK 8QbhY1AcFmDV6LsokqqD+ZQ2eLVd1w2hjlv2620oam6RRFK7VlfPcbnDaJnts8UmxXpp DSg637c+oRdUpM5qv4NF4A2wEzFpTx3nunf2YTSyT+SMwe6AEKlyyPHht3tRhTtyW+ME Yo7dTkk9I22YDkXHdVFO0GnFWfooaU4bMnX3Txne1TLGQTHBiAj+Ci0O59TTfy5nvOXO BSZA== X-Gm-Message-State: AOAM532uKPGE5EjfpiyGobDu9ZgV7YjMGV2nF9zgjOH18AyaDpkKUJBB jdWXgLlKrD7nL54BEyrcIkMYDw== X-Google-Smtp-Source: ABdhPJyWV+uC0DmNRAdK7vcbjg9wndOENA5KYq6/erRd565McYdWx62TZeUsQp7Eg/H+vCrNtXp7tA== X-Received: by 2002:adf:f10a:: with SMTP id r10mr16416565wro.86.1600104499411; Mon, 14 Sep 2020 10:28:19 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id l19sm19870946wmi.8.2020.09.14.10.28.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:18 -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 09/14] KVM: arm64: Enable shift out of bounds undefined behaviour check for hyp/nVHE Date: Mon, 14 Sep 2020 17:27:45 +0000 Message-Id: <20200914172750.852684-10-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu __ubsan_handle_shift_out_of_bounds data is passed to the buffer inside hyp/nVHE. This data is passed to the original handler from kernel. The values of the operands of the shift expression are stored as the lhs and rhs pointers, so there is no need to dereference them. Signed-off-by: George Popescu --- arch/arm64/include/asm/kvm_ubsan.h | 5 ++++- arch/arm64/kvm/hyp/nvhe/ubsan.c | 14 +++++++++++++- arch/arm64/kvm/kvm_ubsan_buffer.c | 4 ++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_ubsan.h b/arch/arm64/include/asm/kvm_ubsan.h index 7fd0d0dfbd82..3130a80cd8b2 100644 --- a/arch/arm64/include/asm/kvm_ubsan.h +++ b/arch/arm64/include/asm/kvm_ubsan.h @@ -21,11 +21,13 @@ struct kvm_ubsan_info { enum { UBSAN_NONE, UBSAN_OUT_OF_BOUNDS, - UBSAN_UNREACHABLE_DATA + UBSAN_UNREACHABLE_DATA, + UBSAN_SHIFT_OUT_OF_BOUNDS } 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; }; union { struct ubsan_values u_val; @@ -35,3 +37,4 @@ struct kvm_ubsan_info { 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); diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan.c b/arch/arm64/kvm/hyp/nvhe/ubsan.c index 9497e7f7f397..40b82143e57f 100644 --- a/arch/arm64/kvm/hyp/nvhe/ubsan.c +++ b/arch/arm64/kvm/hyp/nvhe/ubsan.c @@ -56,7 +56,19 @@ void __ubsan_handle_out_of_bounds(void *_data, void *index) } } -void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs) {} +void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs) +{ + struct kvm_ubsan_info *slot; + struct shift_out_of_bounds_data *data = _data; + + slot = kvm_ubsan_buffer_next_slot(); + if (slot) { + slot->type = UBSAN_SHIFT_OUT_OF_BOUNDS; + slot->shift_out_of_bounds_data = *data; + slot->u_val.lval = lhs; + slot->u_val.rval = rhs; + } +} void __ubsan_handle_builtin_unreachable(void *_data) { diff --git a/arch/arm64/kvm/kvm_ubsan_buffer.c b/arch/arm64/kvm/kvm_ubsan_buffer.c index f66cc5f7878e..b4a282bec91d 100644 --- a/arch/arm64/kvm/kvm_ubsan_buffer.c +++ b/arch/arm64/kvm/kvm_ubsan_buffer.c @@ -28,6 +28,10 @@ void __kvm_check_ubsan_data(struct kvm_ubsan_info *slot) case UBSAN_UNREACHABLE_DATA: __ubsan_handle_builtin_unreachable(&slot->unreachable_data); break; + case UBSAN_SHIFT_OUT_OF_BOUNDS: + __ubsan_handle_shift_out_of_bounds(&slot->shift_out_of_bounds_data, + slot->u_val.lval, slot->u_val.rval); + break; } } From patchwork Mon Sep 14 17:27:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774563 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 542EC112E for ; Mon, 14 Sep 2020 17:31:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2E99A20E65 for ; Mon, 14 Sep 2020 17:31:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iEFv1oxO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725984AbgINRbq (ORCPT ); Mon, 14 Sep 2020 13:31:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726249AbgINR2j (ORCPT ); Mon, 14 Sep 2020 13:28:39 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9606C061221 for ; Mon, 14 Sep 2020 10:28:22 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id e17so828670wme.0 for ; Mon, 14 Sep 2020 10:28:22 -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=oxR4q2exhcBC90vTFTNwz9/4eynAbLxOPQRAqHe9Mw8=; b=iEFv1oxODcKOHWza+NvTccTL3/wQordMg3U1eoRfqFg8wT2ZdgAAKIR5TqyBMDSz7j p6Two6b4VjgeiaRHbB/+ioQrmadcmqQvnY2N2R59qUqGWhKvSFcPBbecWZMmsRSOU+b8 KasiVBhlOh+RhMOjN/LwusWdi6h9O0xedIEU1Db+QSOrCrPoo9U0qsW40Nrzrbp0EX3n +kIWWdPuoVXtvUJpO3w/+q3szInGmWzwUlyfDpMM1RV0gzMMrgepqVoqPV5Re2h4Id55 72djWWoXs5+9bVxRyrIlmei/6++TYwru7YoKIBE4OfzDSAe/C622EhPn8E3Mn02ok9ma srOA== 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=oxR4q2exhcBC90vTFTNwz9/4eynAbLxOPQRAqHe9Mw8=; b=V6vJfypJ+cP0Hv6Ei2lOaY1slEXmbY/Xr6b0B/sbLTbyqxuc95k/OTiR4xyyKXYOCg C7+f/OxQFe5D2fiypZeQOASjumEzOLOfm7rrH4d/qHwzmhCkXLUaagLjiVHHtIIIqmfz nZ9Of7MEbT3tqkLIuhSzsv4h4PzGA3UVmYglVp9ylGYdkC9dM7jg9MjJqdeum+Ftx2NZ W2xwIbd3oZba9FrKcAdyAezsORnwYxIv3Myq2HlCkJ8sP9a+X/CilrWGyVI8x2lTrJZv +R2Cxj9ogdr3lSbNAK0ro3aUmTdSbPN7P/T34QkKE6j9P6cKx+JERNPJgI6gEsVhgXnv mOHw== X-Gm-Message-State: AOAM530lwwDcnJnFjhLhEAjtC6GZRCeN0YvHifyQ7rf32mOJ+O9fVMlq De16WsRq/JcDMX4elH+BXqJ8mQ== X-Google-Smtp-Source: ABdhPJziHq0hb99caNbhZ6ZpDc46fHJPvgXN25hJZzyvPSDfDyF5i1/XYuIsmSM1xvZxBc9ptSKANw== X-Received: by 2002:a1c:6145:: with SMTP id v66mr426746wmb.171.1600104501282; Mon, 14 Sep 2020 10:28:21 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id m1sm20204287wmc.28.2020.09.14.10.28.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:20 -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 10/14] KVM: arm64: __ubsan_handle_load_invalid_value hyp/nVHE implementation. Date: Mon, 14 Sep 2020 17:27:46 +0000 Message-Id: <20200914172750.852684-11-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu The handler for the load invalid value undefined behaviour is implemented for hyp/nVHE. The handler's parameters are stored inside the buffer. They are used by the symmetric handler from the kernel. Signed-off-by: George Popescu --- arch/arm64/include/asm/kvm_ubsan.h | 5 ++++- arch/arm64/kvm/hyp/nvhe/ubsan.c | 14 +++++++++++++- arch/arm64/kvm/kvm_ubsan_buffer.c | 4 ++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_ubsan.h b/arch/arm64/include/asm/kvm_ubsan.h index 3130a80cd8b2..b643ac9a4090 100644 --- a/arch/arm64/include/asm/kvm_ubsan.h +++ b/arch/arm64/include/asm/kvm_ubsan.h @@ -22,12 +22,14 @@ struct kvm_ubsan_info { UBSAN_NONE, UBSAN_OUT_OF_BOUNDS, UBSAN_UNREACHABLE_DATA, - UBSAN_SHIFT_OUT_OF_BOUNDS + UBSAN_SHIFT_OUT_OF_BOUNDS, + UBSAN_INVALID_DATA } 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; }; union { struct ubsan_values u_val; @@ -38,3 +40,4 @@ struct kvm_ubsan_info { 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); diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan.c b/arch/arm64/kvm/hyp/nvhe/ubsan.c index 40b82143e57f..1888a1f51724 100644 --- a/arch/arm64/kvm/hyp/nvhe/ubsan.c +++ b/arch/arm64/kvm/hyp/nvhe/ubsan.c @@ -82,4 +82,16 @@ void __ubsan_handle_builtin_unreachable(void *_data) } } -void __ubsan_handle_load_invalid_value(void *_data, void *val) {} +void __ubsan_handle_load_invalid_value(void *_data, void *val) +{ + struct kvm_ubsan_info *slot; + struct invalid_value_data *data = _data; + + slot = kvm_ubsan_buffer_next_slot(); + if (slot) { + slot->type = UBSAN_INVALID_DATA; + slot->invalid_value_data = *data; + slot->u_val.lval = val; + } + +} diff --git a/arch/arm64/kvm/kvm_ubsan_buffer.c b/arch/arm64/kvm/kvm_ubsan_buffer.c index b4a282bec91d..01bf2171af9e 100644 --- a/arch/arm64/kvm/kvm_ubsan_buffer.c +++ b/arch/arm64/kvm/kvm_ubsan_buffer.c @@ -32,6 +32,10 @@ void __kvm_check_ubsan_data(struct kvm_ubsan_info *slot) __ubsan_handle_shift_out_of_bounds(&slot->shift_out_of_bounds_data, slot->u_val.lval, slot->u_val.rval); break; + case UBSAN_INVALID_DATA: + __ubsan_handle_load_invalid_value(&slot->invalid_value_data, + slot->u_val.lval); + break; } } From patchwork Mon Sep 14 17:27:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774561 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1A8926CA for ; Mon, 14 Sep 2020 17:31:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E0ED320771 for ; Mon, 14 Sep 2020 17:31:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MRgm2+rK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726174AbgINRbk (ORCPT ); Mon, 14 Sep 2020 13:31:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726322AbgINR2j (ORCPT ); Mon, 14 Sep 2020 13:28:39 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E5F3C06178A for ; Mon, 14 Sep 2020 10:28:24 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id j2so499429wrx.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=lzqCHBvQAUYYfGslf7ytSo4YKu1IPGaapcQ9k/I6MdvqlGDjRvhXkSyO4BBVK/7IFT K0emQvvw84P9g7AsBjENpgdOnGsDONAHo6c75399+68609OkcHhnVTWWsGBlJ7+rm+vh s8yz8HRZo1HGk+rgyLbon4ceNJ+r5fVCBsSJJYit/HVxEu9iCI4SomOXlwcjgNfPRrhD cXDpapB5NZazY/mKhPg/o5uxQluqLgU38h3AthbgDUgcjZ1VAXAoGyP4qwLwSwUsDzs3 VMqgqgHMNvt2SycoE5zfEHnlbqQo8U7b6708MeY4YgNbY0Xw2Z28UpzJf+BnPb9TdI0m a1Jw== X-Gm-Message-State: AOAM530I2frscGxVKJvr9ezD+gdrkFEhg4uHOMkknFwGPCrKNOd7v4sQ 09hGWC+jNDMe9zdZNiZZeo7Fbw== X-Google-Smtp-Source: ABdhPJx5klzNqO5C+D8N9yK1WGPOUSEyiPciNY6KedkODWKmogjwhf4A1yZOnxEUD7w3yfpl+BSg4A== 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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@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; } } - From patchwork Mon Sep 14 17:27:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774565 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8AC68112E for ; Mon, 14 Sep 2020 17:31:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6120921BE5 for ; Mon, 14 Sep 2020 17:31:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tHkFPa0p" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726160AbgINRbk (ORCPT ); Mon, 14 Sep 2020 13:31:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726341AbgINR2j (ORCPT ); Mon, 14 Sep 2020 13:28:39 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65F7FC06178C for ; Mon, 14 Sep 2020 10:28:26 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id c18so480880wrm.9 for ; Mon, 14 Sep 2020 10:28:26 -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=iB4T9kLFB3iIR2Z+tST4GCiUmTN46viq6Nw4qtcThCM=; b=tHkFPa0pnHNp4OB1KzhooQ3KuORDrIJ7EGWt10+yQOe8MMQgNYmjvEghP0/sEQoSOw NrHpSaLPFtOEbdIH9+QT2zFtdAdtVSJMZUXbmMtmY3dpj/pbWw+n7CeJaMiwconli2u0 Aq9SPNQjoF5YaSNdG2pKHO6K6yXVQYc/hqdvdq04SyAuqS5OMA9RdpqN6bK/vjgH1P2E nEuQNt3dBI+O7bKCdwYE9d4NGSUX4o8hKfj56xPAkb3QSVu8+VbwpCHLiLB43E6Mmk0x T2fPnr0nxh+nP7t5TwBiUIkBB9Mp91pQ3RSH/8TvQJaByVAfUODo3KhhmSbUoNInEg47 59PA== 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=iB4T9kLFB3iIR2Z+tST4GCiUmTN46viq6Nw4qtcThCM=; b=ReKLoVhGp+So4kgB5soeGWd7KKL0TRxWtdGcz/LZYJ7KFYCt6UQiTR2xpTasDpvKpg ebNgeV0DE4HaWx3wI9nIsSaXdMgkuV6q+c6oncQkCj+nI85khqTDloJIT379vH7GRIZf usnvF2bGpTaCF+Mhz5JkMEhJjgUUjD+zOWrHrCMFn49Dsz83o3mwkzc1ooVsytJ9nVSt avfzJPKl6Wz4rzpuxfz7cy4fus6KOk7Y6nw12Cy9Cx5+3i82mtAXEXgFE1iYwvVDlumB QYeLVaNctOIJBtXmb4EMfIBf7SHI041cy/BUqfnhGUDTVEOzxc1VL3+g/VS+GFa6aQ+E sw1g== X-Gm-Message-State: AOAM533Zcl/vfRJ3qyyeR0Kru0cjzip11ElApD8dukgdOgsfZDzsnImL cJEponXNRTkY5GpxUX8gdzyb4g== X-Google-Smtp-Source: ABdhPJxsgUHbqteuZSLV5s7MMDzWKj4bHGqYYGxCbtmcUOFgjTdJjebJk3JjekSIihnZSCM+KQlc3w== X-Received: by 2002:adf:cf0b:: with SMTP id o11mr16218454wrj.94.1600104504963; Mon, 14 Sep 2020 10:28:24 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id l126sm20422503wmf.39.2020.09.14.10.28.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:24 -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 12/14] KVM: arm64: Detect arithmetic overflow is inside hyp/nVHE. Date: Mon, 14 Sep 2020 17:27:48 +0000 Message-Id: <20200914172750.852684-13-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu Whenever an arithmetic overflow: addition, subtraction, multiplication, division or negating happens inside the hyp/nVHE code, an __ubsan_handle_*_overflow is called. All the overflow handlers are sharing the same structure called overflow_data and they use the write_overflow_data(*) function to store the data to the buffer. When decapsulating the data inside the kernel, the right handler is called by checking the "op" field, which stores the arithmetic opperator. Signed-off-by: George Popescu --- arch/arm64/include/asm/kvm_ubsan.h | 10 ++++++-- arch/arm64/kvm/hyp/nvhe/ubsan.c | 40 ++++++++++++++++++++++++++---- arch/arm64/kvm/kvm_ubsan_buffer.c | 18 ++++++++++++++ 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/kvm_ubsan.h b/arch/arm64/include/asm/kvm_ubsan.h index a9f499f4ef6d..4abdbff38f79 100644 --- a/arch/arm64/include/asm/kvm_ubsan.h +++ b/arch/arm64/include/asm/kvm_ubsan.h @@ -24,7 +24,8 @@ struct kvm_ubsan_info { UBSAN_UNREACHABLE_DATA, UBSAN_SHIFT_OUT_OF_BOUNDS, UBSAN_INVALID_DATA, - UBSAN_TYPE_MISMATCH + UBSAN_TYPE_MISMATCH, + UBSAN_OVERFLOW_DATA } type; union { struct out_of_bounds_data out_of_bounds_data; @@ -32,6 +33,7 @@ struct kvm_ubsan_info { 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; + struct overflow_data overflow_data; }; union { struct ubsan_values u_val; @@ -44,4 +46,8 @@ 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); - +void __ubsan_handle_add_overflow(void *data, void *lhs, void *rhs); +void __ubsan_handle_sub_overflow(void *data, void *lhs, void *rhs); +void __ubsan_handle_mul_overflow(void *data, void *lhs, void *rhs); +void __ubsan_handle_negate_overflow(void *_data, void *old_val); +void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs); diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan.c b/arch/arm64/kvm/hyp/nvhe/ubsan.c index c99d919105aa..dd2dae60864f 100644 --- a/arch/arm64/kvm/hyp/nvhe/ubsan.c +++ b/arch/arm64/kvm/hyp/nvhe/ubsan.c @@ -47,15 +47,45 @@ static void write_type_mismatch_data(struct type_mismatch_data_common *data, voi } } -void __ubsan_handle_add_overflow(void *_data, void *lhs, void *rhs) {} +static void write_overflow_data(struct overflow_data *data, void *lval, void *rval, char op) +{ + struct kvm_ubsan_info *slot = kvm_ubsan_buffer_next_slot(); + + if (slot) { + slot->type = UBSAN_OVERFLOW_DATA; + slot->overflow_data = *data; + slot->u_val.op = op; + slot->u_val.lval = lval; + if (op != '!') + slot->u_val.rval = rval; + } +} + +void __ubsan_handle_add_overflow(void *_data, void *lhs, void *rhs) +{ + write_overflow_data(_data, lhs, rhs, '+'); +} -void __ubsan_handle_sub_overflow(void *_data, void *lhs, void *rhs) {} +void __ubsan_handle_sub_overflow(void *_data, void *lhs, void *rhs) +{ + write_overflow_data(_data, lhs, rhs, '-'); +} -void __ubsan_handle_mul_overflow(void *_data, void *lhs, void *rhs) {} +void __ubsan_handle_mul_overflow(void *_data, void *lhs, void *rhs) +{ + write_overflow_data(_data, lhs, rhs, '*'); +} -void __ubsan_handle_negate_overflow(void *_data, void *old_val) {} +void __ubsan_handle_negate_overflow(void *_data, void *old_val) +{ + write_overflow_data(_data, old_val, NULL, '!'); +} + +void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs) +{ + write_overflow_data(_data, lhs, rhs, '/'); +} -void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs) {} void __ubsan_handle_type_mismatch(struct type_mismatch_data *data, void *ptr) { diff --git a/arch/arm64/kvm/kvm_ubsan_buffer.c b/arch/arm64/kvm/kvm_ubsan_buffer.c index 21c242c92f0a..bd847ac1321e 100644 --- a/arch/arm64/kvm/kvm_ubsan_buffer.c +++ b/arch/arm64/kvm/kvm_ubsan_buffer.c @@ -40,6 +40,24 @@ void __kvm_check_ubsan_data(struct kvm_ubsan_info *slot) __ubsan_handle_type_mismatch(&slot->type_mismatch_data, slot->u_val.lval); break; + case UBSAN_OVERFLOW_DATA: + if (slot->u_val.op == '/') { + __ubsan_handle_divrem_overflow(&slot->overflow_data, + slot->u_val.lval, slot->u_val.rval); + } else if (slot->u_val.op == '!') { + __ubsan_handle_negate_overflow(&slot->overflow_data, + slot->u_val.lval); + } else if (slot->u_val.op == '+') { + __ubsan_handle_add_overflow(&slot->overflow_data, + slot->u_val.lval, slot->u_val.rval); + } else if (slot->u_val.op == '-') { + __ubsan_handle_sub_overflow(&slot->overflow_data, + slot->u_val.lval, slot->u_val.rval); + } else if (slot->u_val.op == '*') { + __ubsan_handle_mul_overflow(&slot->overflow_data, + slot->u_val.lval, slot->u_val.rval); + } + break; } } From patchwork Mon Sep 14 17:27:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774547 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A6CFC6CA for ; Mon, 14 Sep 2020 17:30:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 77B4A20771 for ; Mon, 14 Sep 2020 17:30:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jW8otPoo" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726285AbgINRaI (ORCPT ); Mon, 14 Sep 2020 13:30:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726342AbgINR2k (ORCPT ); Mon, 14 Sep 2020 13:28:40 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECF27C061797 for ; Mon, 14 Sep 2020 10:28:28 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id b79so846320wmb.4 for ; Mon, 14 Sep 2020 10:28:28 -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=80sUfzO1ZftIFVscZ9nv5OunuS9GCgF/C+i0bRP7nsw=; b=jW8otPoov1Khx2LUvHQVCmuvY73d3lv+staZhypFM7PaTDqnYWOwAxWZuVOecCVq7S vu4asBNBaOc1VPCOdQdDJKYaUb+JX4H7d19I0+68AyC6TGKhvH2ipYYCW29UT7odHYq5 itib7ImtZcY8zZGn47F0Jqev/Ht2LRLB/x1+veym+c4b+ELSLExiHtjRERUckezBaaFy 80dcQuOiy52I+XEVxxnnXlNcdLqcHQrnPc8neBdQCGT15fQ2TvwmlDkNWCqX8JPC71Wo RnZvt2JYdae4/TYaC71W/VmbpSRqHKkPRlaS/Fzs2WZTAqGbI9vwZx6nDpohVFlSFy05 T69Q== 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=80sUfzO1ZftIFVscZ9nv5OunuS9GCgF/C+i0bRP7nsw=; b=B8FxIy6NY43vf7j6QbIfv9Pi/dR4m9Cv76byZVL0ZYdAN7nfGEJPk/nekYToT0YidW 9csO+ja1/P1e7i62E1ix3c2J2O1QIh0Y5S37mSVlkdPT7LLR44I3gcfHl1nEC/ugKya/ Cw7qFQDl522k2dF0S9mBoC3lMG3MY44L6/ablaUZQBo0ZfSe7LzUj64Sj4FHUt1ov6os Occ1MlsgjXNxFVf/yLhcddE3mLNzPUfV0Hu3CxN3wU0NS/SlrkIfpd0BaWJwcIeYoeVq zO3epg/7chN/OINuG50mW0Yv5Pbb5v/v//Pqz6vZPBxAdUfNRMm1cobq+02YDzH+jVds JrkA== X-Gm-Message-State: AOAM5303mugUoMTyCrnkTrnkb9/CJCmhWTPLJUXCZXwfK4kfNMS2Zdbf zYHhxFrTthE1Eoi1H8v4l1C0Rg== X-Google-Smtp-Source: ABdhPJzbmBwXiCE6I0xucSZp+HgdOu9Gy5GeiJW6u2E8Bzpwxsz/xK+n4Xs62K/mJ7h3weUdTVHnyg== X-Received: by 2002:a1c:6487:: with SMTP id y129mr490641wmb.90.1600104506813; Mon, 14 Sep 2020 10:28:26 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id b11sm22135485wrt.38.2020.09.14.10.28.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:26 -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 13/14] KVM: arm64: Enable the CONFIG_TEST UBSan for PKVM. Date: Mon, 14 Sep 2020 17:27:49 +0000 Message-Id: <20200914172750.852684-14-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu Test the UBsan functionality for the hyp/nVHE code. Because modules are not supported inside of hyp/nVHE code, the default testing module for UBSan can not be used. For this purpose new functions are defined inside of hyp/nVHE. Test UBSan only when the hypervisor is initialized to prevent spamming the boot messages. Signed-off-by: George Popescu --- arch/arm64/kvm/Kconfig | 3 + arch/arm64/kvm/arm.c | 8 ++ arch/arm64/kvm/hyp/nvhe/Makefile | 1 + arch/arm64/kvm/hyp/nvhe/ubsan_test.c | 115 +++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 arch/arm64/kvm/hyp/nvhe/ubsan_test.c diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 318c8f2df245..b6581f2512fb 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -60,6 +60,9 @@ config KVM_ARM_PMU config KVM_INDIRECT_VECTORS def_bool HARDEN_BRANCH_PREDICTOR || RANDOMIZE_BASE +config NVHE_KVM_TEST_UBSAN + def_bool (TEST_UBSAN != n) + endif # KVM endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index eff57069e103..5468fa5599cf 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1297,6 +1297,14 @@ static void cpu_init_hyp_mode(void) BUG_ON(!system_capabilities_finalized()); __kvm_call_hyp((void *)pgd_ptr, hyp_stack_ptr, vector_ptr, tpidr_el2); +#ifdef CONFIG_NVHE_KVM_TEST_UBSAN + static bool test_ubsan_run; + + if (!test_ubsan_run && (smp_processor_id() == 0)) { + test_ubsan_run = true; + kvm_call_hyp_nvhe(__kvm_test_ubsan); + } +#endif /* * Disabling SSBD on a non-VHE system requires us to enable SSBS * at EL2. diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile index cc082e516353..2b495fe41f2b 100644 --- a/arch/arm64/kvm/hyp/nvhe/Makefile +++ b/arch/arm64/kvm/hyp/nvhe/Makefile @@ -12,6 +12,7 @@ obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ CFLAGS_ubsan.hyp.tmp.o += -I $(srctree)/lib/ obj-$(CONFIG_UBSAN) += ubsan.o +obj-$(CONFIG_NVHE_KVM_TEST_UBSAN) += ubsan_test.o obj-y := $(patsubst %.o,%.hyp.o,$(obj-y)) extra-y := $(patsubst %.hyp.o,%.hyp.tmp.o,$(obj-y)) diff --git a/arch/arm64/kvm/hyp/nvhe/ubsan_test.c b/arch/arm64/kvm/hyp/nvhe/ubsan_test.c new file mode 100644 index 000000000000..f4e7b3ed3cf5 --- /dev/null +++ b/arch/arm64/kvm/hyp/nvhe/ubsan_test.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include + +typedef void(*test_ubsan_fp)(void); + +static void test_ubsan_add_overflow(void) +{ + volatile int val = INT_MAX; + + val += 2; +} + +static void test_ubsan_sub_overflow(void) +{ + volatile int val = INT_MIN; + volatile int val2 = 2; + + val -= val2; +} + +static void test_ubsan_mul_overflow(void) +{ + volatile int val = INT_MAX / 2; + + val *= 3; +} + +static void test_ubsan_negate_overflow(void) +{ + volatile int val = INT_MIN; + + val = -val; +} + +static void test_ubsan_divrem_overflow(void) +{ + volatile int val = 16; + volatile int val2 = 0; + + val /= val2; +} + +static void test_ubsan_shift_out_of_bounds(void) +{ + volatile int val = -1; + int val2 = 10; + + val2 <<= val; +} + +static void test_ubsan_out_of_bounds(void) +{ + volatile int i = 4, j = 5; + volatile int arr[4]; + + arr[j] = i; +} + +static void test_ubsan_load_invalid_value(void) +{ + volatile char *dst, *src; + bool val, val2, *ptr; + char c = 4; + + dst = (char *)&val; + src = &c; + *dst = *src; + + ptr = &val2; + val2 = val; +} + +static void test_ubsan_misaligned_access(void) +{ + volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5}; + volatile int *ptr, val = 6; + + ptr = (int *)(arr + 1); + *ptr = val; +} + +static void test_ubsan_object_size_mismatch(void) +{ + /* "((aligned(8)))" helps this not into be misaligned for ptr-access. */ + volatile int val __aligned(8) = 4; + volatile long long *ptr, val2; + + ptr = (long long *)&val; + val2 = *ptr; +} + +static const test_ubsan_fp test_ubsan_array[] = { + test_ubsan_out_of_bounds, + test_ubsan_add_overflow, + test_ubsan_sub_overflow, + test_ubsan_mul_overflow, + test_ubsan_negate_overflow, + test_ubsan_divrem_overflow, + test_ubsan_shift_out_of_bounds, + test_ubsan_load_invalid_value, + test_ubsan_misaligned_access, + test_ubsan_object_size_mismatch, +}; + +void __kvm_test_ubsan(void) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++) + test_ubsan_array[i](); +} From patchwork Mon Sep 14 17:27:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Popescu X-Patchwork-Id: 11774551 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 284596CA for ; Mon, 14 Sep 2020 17:30:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0724420759 for ; Mon, 14 Sep 2020 17:30:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="L3Rqn405" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726031AbgINRaP (ORCPT ); Mon, 14 Sep 2020 13:30:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726106AbgINR3I (ORCPT ); Mon, 14 Sep 2020 13:29:08 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26269C061226 for ; Mon, 14 Sep 2020 10:28:30 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id j2so499742wrx.7 for ; Mon, 14 Sep 2020 10:28:30 -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=g9asLkQ3OEMA9W3/GEGNRUW1iZejtpJzTvLvCBaNFqA=; b=L3Rqn405GYhI0a/LIy0yLk69eNTGI09w8kK3GusLAgD2VDqrXLtU0QNtSaL3E2jnJl aS9V/1Ro1pV9zT8KpI+i624XrTJBd2gg/r8uQFwVfpXG+MpZa0bIA2MWLSvZK3m7cUlb HzbBUvIql160nVCPNlIZEkaeXxZAoh9ib5Wq8qddUg5N8BFHAy3BANe7Ewvg3428P7P5 0iUqBEhudOH/vLy73PmHEYdX5AmRKrjarZzGyKMseTtOquPD/o6H1j9mgPUQryGi9J6M AqxmR2pfAlDQpRKkGpdaofGPTEPO/SXB7XcE2rogZG7gtfQNXkNYutC/7Ds7WU6ZOgU5 70ZA== 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=g9asLkQ3OEMA9W3/GEGNRUW1iZejtpJzTvLvCBaNFqA=; b=PTt8WX+QLmrUaIJtNw6JEvuEaSAX1GI9ZSO7yNSZ/1JaXjsEIwO5h9POKQnK0pCPJC LvNwhNqYdP04SZzmBLiqFe3K4KkQFaHupIxVuZ1iXnZrv0r/snfOLzpgm9n3IUUYf2pS BJeNngtZcI7poVLVDLlz8LULXn02g2DnuNz8SMqfVl62UqXLfAB7kyYZPnG6qWOaNyLm Akmux8VJkRWC+E7Qwgs9gLYn2rpEyhAJ+JhzeTpGmJTeslHIlApK9jbs7zoNNPkBs2TR CYnuR9fZBNrYQ2yFiyfKKr9gqHMUn1AZBSYYsR5JW7YAwcSQGm9AfKa+O9OQid5nV0gj m+YA== X-Gm-Message-State: AOAM530t4zRLIYWqswiT33V6x5yYBWTzrTQDx2ZrOx2Ea4wLm7EXVBdD y5kLwA4NhNiZMHmklzXwok/mJw== X-Google-Smtp-Source: ABdhPJyuPd0Fs24xYDGYgLUyicl3WMGGQSI7fVgvHIQWca5SpyYpUi5fPphE0FqVU4DtkZnfl8xKwA== X-Received: by 2002:adf:fec7:: with SMTP id q7mr17052167wrs.293.1600104508663; Mon, 14 Sep 2020 10:28:28 -0700 (PDT) Received: from localhost (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id o6sm22778704wrm.76.2020.09.14.10.28.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 10:28:27 -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 14/14] DO NOT MERGE: Enable configs to test the patch series Date: Mon, 14 Sep 2020 17:27:50 +0000 Message-Id: <20200914172750.852684-15-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 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org From: George Popescu Enable configs from Kconfig.ubsan to test the buffer and the ubsan_handlers. Signed-off-by: George Popescu --- lib/Kconfig.ubsan | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Kconfig.ubsan b/lib/Kconfig.ubsan index 774315de555a..f72b8a564a8c 100644 --- a/lib/Kconfig.ubsan +++ b/lib/Kconfig.ubsan @@ -1,9 +1,11 @@ # SPDX-License-Identifier: GPL-2.0-only config ARCH_HAS_UBSAN_SANITIZE_ALL bool + default y menuconfig UBSAN bool "Undefined behaviour sanity checker" + default y help This option enables the Undefined Behaviour sanity checker. Compile-time instrumentation is used to detect various undefined @@ -82,7 +84,8 @@ config UBSAN_ALIGNMENT config TEST_UBSAN tristate "Module for testing for undefined behavior detection" - depends on m + depends on UBSAN + default m help This is a test module for UBSAN. It triggers various undefined behavior, and detect it.