From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01C8AC433E2 for ; Mon, 14 Sep 2020 17:31:59 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 957EC20771 for ; Mon, 14 Sep 2020 17:31:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="ergcAA0w"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="tHkFPa0p" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 957EC20771 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=USsjlH3fP9mMlFu0+NLzY57/Yv5+ktxcaSU/MzqxjQc=; b=ergcAA0wM9WjWISk36v+TBtKe LTgSs+9ayiyT6VAAE8FdVmeZJqQOQHiEN0RDDFMmcam9wxLSy2EfJNlExYamZmTc7npkgkn1R01aO 8MRlfy5Idnyn3VT+mjzumMvp9SkSDae4fXvFSel8nN4ClR2q/vmcHx9JzSzwXmrGdYvrLxnx87j4k pWVSP0TXg+FIR4wuj+yEpXFy+6Xlz6KLZj3LxK2I2k4hEPuLNanDzjvmzw6xruHvpWEsKojr96htu wqRsHbh3L8EO0Fe2zwGpl7HLLWSvXrtDC7HCXA3ZnoKK01/uyGkrgg9MzoG+lNpv67xBFrxFVs0eR xes9ZeLsg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kHsIl-0001Pu-Qg; Mon, 14 Sep 2020 17:30:04 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kHsHC-0000eu-5C for linux-arm-kernel@lists.infradead.org; Mon, 14 Sep 2020 17:28:27 +0000 Received: by mail-wr1-x441.google.com with SMTP id o5so448185wrn.13 for ; Mon, 14 Sep 2020 10:28:25 -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=I9OsV3ds6vMd+Xjl2vewE2TFyqoL85O5LpEjnnt6EGrqcnvHDOTxjKBgx1A3gD/EgV MwErHmRllF6auox8PRp12XdGTr3h3D+rzIfgcZ2VU6iQdTZx3C6mvferMxV/6FJ91CsT mbPyr7EVvbOOGVaXMhhRs73RmccE4YaoDdgQ5cJ4gIpsNzy9q/pSFdsCwrmj0yKE9fJf dxoq0imJIz+AVy0smPecNawSk1WKaTH0ZQNTuU2sLay98uqpKWpc/2lS0w+fkci0oKpw ekTHcQxDeJg/G01yuULrkDOKugm3uc1WaDNnvEy4q280NId5T2NoMoL7x1B5dtA2fnvS J//w== X-Gm-Message-State: AOAM5305v2+aqUiimWqydGqnPqkXvlQSdaZAkgRQMVt2cIF4ic+2w16O qk1EyDXzUTr4dg7b4fy0cTpqsQ== 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200914_132826_348037_35914EF3 X-CRM114-Status: GOOD ( 17.77 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: arnd@arndb.de, elver@google.com, tglx@linutronix.de, keescook@chromium.org, maskray@google.com, linux-kbuild@vger.kernel.org, suzuki.poulose@arm.com, ndesaulniers@google.com, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, George Popescu , clang-built-linux@googlegroups.com, broonie@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, dvyukov@google.com, natechancellor@gmail.com, dbrazdil@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, ascull@google.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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; } } -- 2.28.0.618.gf4bc123cb7-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel