Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp143702pxb; Fri, 15 Jan 2021 09:23:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJylWUsFkf6DiUxZw/uFgAoKq1tdENqYqVRuyFlKjTpEJSOCmT7CCIZuHyBS0Lfnq9bkVinT X-Received: by 2002:a17:906:1288:: with SMTP id k8mr9370889ejb.206.1610731387210; Fri, 15 Jan 2021 09:23:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610731387; cv=none; d=google.com; s=arc-20160816; b=KAHTl8Miw+j8Tt4UiGO1DQ0Fehzj7LscVCw0o0qLC8R4UCnVfPlkP08FN1FQzuGDgd d15dUEBDNVpkMP34yCvnWCUxHctxo5nBYVrQEYGXOw1FAZntN3S4cjWLKoz9Z5Pz8Fjg MaAdDHTWpLI7OPpC7x//PONd7Y/YDkEe5tkA2OHaVuaINBnrR7ikoNTRsWuEIlwbyJ6/ jVCcKA53vpxmsCq6k/tYkPtDxTriU7j2RiXkJxNIQD6CqQ4uEOxg22gDr0ZOS2Cj/++F 5Cu3uCUo9F+0ZqrcbYS8gCxgi/cyFEsJJmH6PUd9CPWMldCQD6HR4fL9VsVp1Peyad1E irrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=dbukutgYzHT9MiWfvULK4mqHhSiWytSaif6tz/CiEZM=; b=XndBMpO4XxMWMuz+tkobN5RiFuHXeg8LqLMGRm728Vp0Lz/YOvtiau4HCwRM5YAkq2 g17IPi0CxlR3XAGGiD7WSnixsHFdIkDA3U62cAbmVV4hCLRgWRcR7PvCaIh/3RFw0mLa KY4OenWv7PNZylrI6bwNWS44ZF3TsCJkvXXvEXlqBmeAu3bhktOlixCm2nYRDC3Vek7m iX+cgdYl19FEHB9pOM1OeESSO4ScRh7d9L0FYroVbTDDbYJuaK5iwIzFu5QMCfiuZkEE 7NZPTmpVOUAJCQOGk6CJykVJpjLnDy1P8kTnA6ybvK+rskt1t4ammRmulrYGqxEenrqh 5cNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=o5x+TKeE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w13si3568545ejq.43.2021.01.15.09.22.42; Fri, 15 Jan 2021 09:23:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=o5x+TKeE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387415AbhAORUL (ORCPT + 99 others); Fri, 15 Jan 2021 12:20:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733220AbhAORUK (ORCPT ); Fri, 15 Jan 2021 12:20:10 -0500 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BFC4C061799 for ; Fri, 15 Jan 2021 09:19:14 -0800 (PST) Received: by mail-qv1-xf49.google.com with SMTP id j5so8255194qvu.22 for ; Fri, 15 Jan 2021 09:19:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=dbukutgYzHT9MiWfvULK4mqHhSiWytSaif6tz/CiEZM=; b=o5x+TKeEjPaVlxVW2WwM7smd2sabHdLTnC8LFV1VrZQ/GGd4Bgqar1ue6KnB83naGM MJT/HcEcZkF+D0O1RZQx8Y1GwomJ6XnuSWTYL0NEttffqFoWwA+fVJhe+Y2sP/jwHFXK LM3I0Iasp3fRNiQMEpuS/VH3E8cWKObdLfSP4yNdHw1DzrecpCrtArsNMvVchBJqI8SF mWo6vbOqPdhLmriUmNoOOUNjqLKR6SmKIirmIAnCV2/PvV+vpiAFpTYjRByIy3cSPSLK /d3ZiripFD1TXXle8YUHzFq0zkpAbs4aGPx+spj4dKEkZ8zz4Pp0TzP4ASQz1o5xy8XX 0fwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=dbukutgYzHT9MiWfvULK4mqHhSiWytSaif6tz/CiEZM=; b=LDbg1H12a9EKstPKKSYsMmVOubDMg909MVL52n/BkEYwvnaJXuDynxVGPgvfFYZQJe ZyVqB010siGysIGKVuSu0jo2DbLRVNhNl/T4hNFf/FB/rlQBLnkIIkNOyfRojYV3yiDf An7DyIPIyZ4SzN/2LlXPi2bx7hP3FffmD+9rqwUSwc3LXv9FwYrEBECnG5iMD7+IKLCw /TVpGGsX0YML9e54WTJQB7CqIfpl6Jhlu3E/hyczPpbC1V2YgxEgV5bH2F1MFXo/ShsH RFQjuTh2rCGGy37QDSbZhhyuw+pNIE2mnOSloO+qHtT0DZxswQcj1OzfCgukASwzZQN/ WPvA== X-Gm-Message-State: AOAM530jrcd0nfEYAdowMbbZj5hCVy6sTGlrftPpjcobtZCur4wrDSx/ UYel5wfKl5v+P9adSu/zvArGaCh8b4qP Sender: "lenaptr via sendgmr" X-Received: from beef.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1091]) (user=lenaptr job=sendgmr) by 2002:ad4:5901:: with SMTP id ez1mr13147103qvb.6.1610731153435; Fri, 15 Jan 2021 09:19:13 -0800 (PST) Date: Fri, 15 Jan 2021 17:18:29 +0000 In-Reply-To: <20210115171830.3602110-1-lenaptr@google.com> Message-Id: <20210115171830.3602110-9-lenaptr@google.com> Mime-Version: 1.0 References: <20210115171830.3602110-1-lenaptr@google.com> X-Mailer: git-send-email 2.30.0.296.g2bfb1c46d8-goog Subject: [PATCH v3 8/9] KVM: arm64: Detect arithmetic overflow is inside hyp/nVHE. From: Elena Petrova To: kvmarm@lists.cs.columbia.edu Cc: Elena Petrova , linux-arm-kernel@lists.infradead.org, open list , Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose , George Popescu , George Popescu Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: George Popescu Whenever an arithmetic overflow: addition, substraction, 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. Signed-off-by: George Popescu Signed-off-by: Elena Petrova --- arch/arm64/include/asm/kvm_ubsan.h | 10 ++++++-- arch/arm64/kvm/hyp/nvhe/ubsan.c | 40 ++++++++++++++++++++++++++---- arch/arm64/kvm/kvm_ubsan_buffer.c | 20 ++++++++++++++- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/kvm_ubsan.h b/arch/arm64/include/asm/kvm_ubsan.h index 93c1b695097a..da4a3b4e28e0 100644 --- a/arch/arm64/include/asm/kvm_ubsan.h +++ b/arch/arm64/include/asm/kvm_ubsan.h @@ -22,7 +22,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; @@ -30,6 +31,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; @@ -41,4 +43,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 a9f72d4bcab7..f16842ff7316 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 b7823dedf8b1..2c7060cbb48b 100644 --- a/arch/arm64/kvm/kvm_ubsan_buffer.c +++ b/arch/arm64/kvm/kvm_ubsan_buffer.c @@ -39,7 +39,25 @@ 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; + } } void iterate_kvm_ubsan_buffer(unsigned long left, unsigned long right) -- 2.30.0.296.g2bfb1c46d8-goog