From patchwork Wed Nov 4 23:18:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11883087 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=-12.7 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 17695C388F9 for ; Thu, 5 Nov 2020 04:09:21 +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 8887320732 for ; Thu, 5 Nov 2020 04:09:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="B5/kp8mf"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="aihdbuVP"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="dmRgatD+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8887320732 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:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=xJYPQYdytiZHiZyZ2/Z+v3EYd42/c2cqpESAoeayrH8=; b=B5/kp8mfMf8Hv184PiE/UurHW 4JtRceSq7mM1npa3QmrqKxHQLNcEcaY/ZUvZcC9I17yqeQ6Cn63WS+Id9qaDDiVNiTGpNuHBEKmgI Oc8cB3+uFQvmATEB43cyRTSqA2EmoHKnvWPOTYP1CWg5mJuWA5XsQGKl/W2HkZ28aVho8EGVyZJbt lJXayLeLaQjitJfk3P8YhuOxJ/VGzns8SRCYWgLaQzBeRb1z0N5QqswcgNTOY/8f4tPTVSiOZsPz3 gv3D0WsFhbOEv4C+EjNAAK84rLZzpMO8iwUv65LVgotC/aV2XgmbOfbDUP5xJjHDMwRfPvEAvoFOy Gt+rdAKeg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaWZ6-0003ZR-TD; Thu, 05 Nov 2020 04:08:01 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaW59-00016k-Em for linux-arm-kernel@merlin.infradead.org; Thu, 05 Nov 2020 03:37:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=SHQTTAC6Hd6Fbj5acE+XjHaNfSSfd6dVM7XrjREAdfI=; b=aihdbuVPBlsmXXcaqIXDdV3tC8 n6o6k6tUKpkWlclu8no7/PyuFutHjlgOuhG5M+slf2PqlWjKVhnoyVU/z+n0eT5A0KioigLdPT8Rh 6rLl2xJ4hQZhKHVVo9Crg1Sukohjtv8zJbvYN2INZDAElDHZMWlLHSTPWNqrBbhGdHspc63F3P/JC fCvIyFSHwtar6lxs8dLxtk+u4VcvUtvomyR2cwd5JFZIjTWlwTZEtuHd07daYpZTksHdvqOUqyE+A bGCCAMv91dqPW/QEBJkcvWU232faJv6VUgvxA1p5CP92TE28qECRKNIMYt0osSj7LZC0cxOwRqXTP ephJzfvA==; Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by casper.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaS5f-00046j-T4 for linux-arm-kernel@lists.infradead.org; Wed, 04 Nov 2020 23:21:24 +0000 Received: by mail-wm1-x349.google.com with SMTP id y1so22604wma.5 for ; Wed, 04 Nov 2020 15:21:19 -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=SHQTTAC6Hd6Fbj5acE+XjHaNfSSfd6dVM7XrjREAdfI=; b=dmRgatD+4KwlD8vDRkWm1qwcz6X931KBLbs+0dVEVgERYTxh2uCRiAOiLREksLhWty YRoxEP9m369Nn/rrRzymYGAsT9/oPOHVG9kIgPCKtD3fd0eihFwVRLk2ev/OoaTDXwJL MlqVDuKwvRrbL3x7QLaxT9zqdgZE0o9Y3DW9RIYIgwsh6R4pFCoDFd0/vKRzWD0M27fP 2vJ3bNRgRqTGpXiS8tt6D9Lbk3h6JCh1Ph8fQKvWphd07cLOpioQ1pwILZK2nWQv/ybT unUUgmirdG/N4DlbEfkvKbyHUtoX/un96wFkDOnNchFCuDI2WCJWcipCH3m4Ll60cLh0 LqcQ== 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=SHQTTAC6Hd6Fbj5acE+XjHaNfSSfd6dVM7XrjREAdfI=; b=GNdrHwHacOPj+EV7QWG6Gm78z5aeKSRFP9hnuNsrQmzJVWaHvxb1oBiR2gUn/WOUmk IDa8YDTdTDa94pppQDcMpfKK0XeFT54V4CzDaVS3EyklnYM8Z/5GyypThf1gggxPRb1M zKYOQRrWQCuk9FlaS059pfaBQ5yo2zZAkMwgUAJiHgAvFGPWW4eAQZx0VBX8yyyHcYoq se8oBJcsK2UySCbJF/FPQN00e4YXLbFRHk2x6qxV3WHgVQfr+6ZSUE8qco5TvPnetQ3k PTOANTlL50EcIIx6rpx8xPehORrDfzDgmeeB9W8bo8PbcsQlDQWFJEJDGzXjzfnEVXkO Lt5g== X-Gm-Message-State: AOAM532pcMLLO4bNYM2GGlUy7BMZ5s8xCFXfMJIB5JZHTlZkmkvvQcSl Uf9BoBlLrkD7YVBGJs+5HRZBNh14mNNPf5LZ X-Google-Smtp-Source: ABdhPJyZlAFM15HsNj8Y27OfWjkPQ5brBiE/WnkgLcu2rjh7UqAl2lowj5bgDZbGEOg2aUmBh32jCZ6uAnaTyF4Z X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:adf:ff82:: with SMTP id j2mr329460wrr.401.1604532016330; Wed, 04 Nov 2020 15:20:16 -0800 (PST) Date: Thu, 5 Nov 2020 00:18:44 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v8 29/43] arm64: mte: Add in-kernel tag fault handler From: Andrey Konovalov To: Catalin Marinas X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201104_232120_130363_52F25733 X-CRM114-Status: GOOD ( 20.52 ) 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: linux-arm-kernel@lists.infradead.org, Marco Elver , Andrey Konovalov , Kevin Brodsky , Will Deacon , Branislav Rankov , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexander Potapenko , Evgenii Stepanov , Andrey Ryabinin , Andrew Morton , Vincenzo Frascino , Dmitry Vyukov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Vincenzo Frascino Add the implementation of the in-kernel fault handler. When a tag fault happens on a kernel address: * MTE is disabled on the current CPU, * the execution continues. When a tag fault happens on a user address: * the kernel executes do_bad_area() and panics. The tag fault handler for kernel addresses is currently empty and will be filled in by a future commit. Signed-off-by: Vincenzo Frascino Co-developed-by: Andrey Konovalov Signed-off-by: Andrey Konovalov Reviewed-by: Catalin Marinas --- Change-Id: I9b8aa79567f7c45f4d6a1290efcf34567e620717 --- arch/arm64/include/asm/uaccess.h | 23 ++++++++++++++++ arch/arm64/mm/fault.c | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 991dd5f031e4..c7fff8daf2a7 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -200,13 +200,36 @@ do { \ CONFIG_ARM64_PAN)); \ } while (0) +/* + * The Tag Check Flag (TCF) mode for MTE is per EL, hence TCF0 + * affects EL0 and TCF affects EL1 irrespective of which TTBR is + * used. + * The kernel accesses TTBR0 usually with LDTR/STTR instructions + * when UAO is available, so these would act as EL0 accesses using + * TCF0. + * However futex.h code uses exclusives which would be executed as + * EL1, this can potentially cause a tag check fault even if the + * user disables TCF0. + * + * To address the problem we set the PSTATE.TCO bit in uaccess_enable() + * and reset it in uaccess_disable(). + * + * The Tag check override (TCO) bit disables temporarily the tag checking + * preventing the issue. + */ static inline void uaccess_disable(void) { + asm volatile(ALTERNATIVE("nop", SET_PSTATE_TCO(0), + ARM64_MTE, CONFIG_KASAN_HW_TAGS)); + __uaccess_disable(ARM64_HAS_PAN); } static inline void uaccess_enable(void) { + asm volatile(ALTERNATIVE("nop", SET_PSTATE_TCO(1), + ARM64_MTE, CONFIG_KASAN_HW_TAGS)); + __uaccess_enable(ARM64_HAS_PAN); } diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 1ee94002801f..fbceb14d93b1 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -296,6 +297,44 @@ static void die_kernel_fault(const char *msg, unsigned long addr, do_exit(SIGKILL); } +static void report_tag_fault(unsigned long addr, unsigned int esr, + struct pt_regs *regs) +{ +} + +static void do_tag_recovery(unsigned long addr, unsigned int esr, + struct pt_regs *regs) +{ + static bool reported = false; + + if (!READ_ONCE(reported)) { + report_tag_fault(addr, esr, regs); + WRITE_ONCE(reported, true); + } + + /* + * Disable MTE Tag Checking on the local CPU for the current EL. + * It will be done lazily on the other CPUs when they will hit a + * tag fault. + */ + sysreg_clear_set(sctlr_el1, SCTLR_ELx_TCF_MASK, SCTLR_ELx_TCF_NONE); + isb(); +} + +static bool is_el1_mte_sync_tag_check_fault(unsigned int esr) +{ + unsigned int ec = ESR_ELx_EC(esr); + unsigned int fsc = esr & ESR_ELx_FSC; + + if (ec != ESR_ELx_EC_DABT_CUR) + return false; + + if (fsc == ESR_ELx_FSC_MTE) + return true; + + return false; +} + static void __do_kernel_fault(unsigned long addr, unsigned int esr, struct pt_regs *regs) { @@ -312,6 +351,12 @@ static void __do_kernel_fault(unsigned long addr, unsigned int esr, "Ignoring spurious kernel translation fault at virtual address %016lx\n", addr)) return; + if (is_el1_mte_sync_tag_check_fault(esr)) { + do_tag_recovery(addr, esr, regs); + + return; + } + if (is_el1_permission_fault(addr, esr, regs)) { if (esr & ESR_ELx_WNR) msg = "write to read-only memory";