From patchwork Mon Feb 10 20:26:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13968610 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9DCCC02198 for ; Mon, 10 Feb 2025 20:27:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AF26280021; Mon, 10 Feb 2025 15:27:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E78C28001E; Mon, 10 Feb 2025 15:27:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 739F7280021; Mon, 10 Feb 2025 15:27:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 52D5E28001E for ; Mon, 10 Feb 2025 15:27:20 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 14E5B466EA for ; Mon, 10 Feb 2025 20:27:20 +0000 (UTC) X-FDA: 83105169840.17.1B98C94 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf04.hostedemail.com (Postfix) with ESMTP id 2541640002 for ; Mon, 10 Feb 2025 20:27:17 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Gd3UGzFw; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739219238; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=M49HWf8gzr6wWBf1Qtr4VOivQAEEQjfyuWgORGOVTSw=; b=5+lRHCIBpCs3WN6hdHKJrnl9KMJMs7AwXQ79AhszCD+lNaWLplaM0tk53apunQ3LJ11s7K PD72EkNEkvkbVJLqc4RJCAvf449BXcRXQMluKDsD+xc5mIZwPumE5QRzMBocTwsXiKE6CE 0kAgHRbXvPi974q2MtOJb4Z2uYlsEgw= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Gd3UGzFw; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739219238; a=rsa-sha256; cv=none; b=vN+qveubuqLhWKy/dPHg+7pna3VAkGBeK9sxcus9rHYAo934gAyynK1gJLmp/hBx5Z3yb6 LsNHIgV5BdXHbdinbEWJqAVtcTzlXSsBmzdpGhf56UaBIoTmgSjP61y9VP6Oq9jsM2U6nF ii3wyxUp2OyyVro8OwT/9xxyXPqhKbA= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-21fa56e1583so18606965ad.3 for ; Mon, 10 Feb 2025 12:27:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219237; x=1739824037; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=M49HWf8gzr6wWBf1Qtr4VOivQAEEQjfyuWgORGOVTSw=; b=Gd3UGzFwUukUag3nnMpcKY0cYPh6v18gQlqbRwNyXtC/ReDxTIbE3+FFmw7tPUS+CR RetUPKHT4F5KJEiexPPPznd8beq9HmhiIY4FEkrBMWS/bqfBnovXhJZyPI1zMk00NiUw FZfCaldxkYVsS7S8Mmn7MPwlkO3fNsgWEnfkIA2k+gsb3C5vJR6RR2+du7A6aUyZcIQN TEhpElsijuzRoeu1my8CAQkoTw77ZrQp1c7eOTalmN2yQVs0/71BPsB3SFxLZxIbaV32 mrbkkcdwpkpPNIubFdYrSDp5GEQhswd+RQZYc6ePKZ17nhWuad0bx71bE36taRXM5dej 7+bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219237; x=1739824037; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M49HWf8gzr6wWBf1Qtr4VOivQAEEQjfyuWgORGOVTSw=; b=ZJ2/pih1F1lx83/U8xdoTPuJZMI9PR3fHstnddRuZGdutNFNM6wCLP69nx92Yvpg7w Jp4T0zrdjHlPoE6+TXH/hW27XePNmOEVrqZPCDAMbWQ428NOR1up5fpUpRZ7u29V8tSx pY4rch5X5AcFn5hP7LJnjuyv8me/mBMBhdfwLxVuLNnGeOaIbfYnwPi9Y0T8OUGlpv7c jz8AzlG7dBazucMbA32pxDTQctbeeCtN0Ock3ldHxVq3P4VEMiPSVSIBXTjnykAom+fU P+ne11cOIrIZGOhgFLo6qOrrcSkAAJTdjpYH6K+iIJcRDjo1GIYXcgYkVdfqYnMTKG9T LKOw== X-Forwarded-Encrypted: i=1; AJvYcCWCjMwf5sU1wludRElutJaKc7x6LGsvsumiefnq4btjAvPbXBoKigl9z+iTcDY4i9sBgBSvTDaG5g==@kvack.org X-Gm-Message-State: AOJu0YwO/gw45Lb+hBNIgQImjPW8I37zbYFtojQUg6myL5IkBQCFHk6l T11hPPsfGw8bd0sImRSEZZ0D7fOyk5iHDiXAaiqO5THFleLHUoyHwL8YjYQLBN0= X-Gm-Gg: ASbGncv6W4XXK6jKTIH4HPHJ2rdWVpdRiBBImOTyiF4eD76sRYmJ0hN8VDB9Y1SO4Cz sgh4uI4uy3yDoKPuI9/SCiDKb845muRjiSOwIZkCII5umsKd7Ge/oLRgxyDMIYKvF1XraNJz6NT XMyHyJZym9MmTKh8a/VoFbsrB1wj7AgUBvOxxomi1BUhE921hDkd5gidO7Pt7oQwSwvWN4zD5s2 CGNIlGzqIwOnJiYCeOhUVu4S91oGrTRz9I8Lx96A1alJelGxMdWKkq4MnBRL/P8mzLDtWOaLjXZ qVbGcpYECEJOpIIQBn2UNDEAPw== X-Google-Smtp-Source: AGHT+IHdHEBMhep7yU/mVgZUfZFs7fvP4+JZwXsHPWxQJF2WCxVaTQUYKv93Rn4bqO7ShpSAaGLGsg== X-Received: by 2002:a17:903:46c6:b0:216:393b:23d4 with SMTP id d9443c01a7336-21f4e1cb92fmr222614515ad.11.1739219237018; Mon, 10 Feb 2025 12:27:17 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:16 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:47 -0800 Subject: [PATCH v10 14/27] riscv/traps: Introduce software check exception MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-14-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Queue-Id: 2541640002 X-Stat-Signature: 86ktt6kbjx3wgyb7sj7u58wupshuarhe X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1739219237-516379 X-HE-Meta: U2FsdGVkX18ustUm196oLw5HMeAzG8dBOlZU192M5Xr7MnITXuZuuhJjTaBz81mH6wlcQaDCEQXv0ZVC4Jz74EEYHHFON+DfBozF4BqJjLeO9EUOTzLFfiotAQ963sBFKa/d0eJp5OoZ4GqGsGKba1CMbiPHKVg+BMuhxeCLoeFZDUo1SZ4KKdx5POXEQdrZKgDaeVT41rC97M+954hxbfZ1bcFA857emApWaTMWl0N4J55dRev9BMKgw+hi+upvbinDsrnRTcQuTthyw+RCVELziRj+6gZn8meIaH/1Z04BLu/oZnxmJZoklOj7UqA6TEndZvxSpV4hsuvTrqIp3ksC60xUEQbJkzkGqzxjKLJIfEJBObmiLpC79pOs9GT/UyopcUXsLiUX8IrPBJnSV1vmPRKKllVtz4z7R0ZtkIY0oZyPOLDLMAx0NkHVR47sIjME2ClHNm3/01Qu/4D4Pr9x662qsXjb5ICsTZf5NSF4TobC1PdSPLcGbhBQRs0/EMSD5Vw680lz5oMCxflCLT/4qfEubSpXZs/xZPHllV5LtSnXSlfeE/rr27p65ke2UVFgzKLdGjbduvYdhOExmuBUvMooXt3ewCSDk2X2SGRAJgGwA8IVdhY14ZqWCSkN6Vxalu4/ij8bD6r+uRvl/ilmH+KJzz16sMQMfZqSG0CCu0hPxuiBRNpfQokq2L0c/xlzryC52/i47UMcLethXwV/KDgRkrS/2MDGNMjMDLqaLof1C83RMFHswtWC9+K094BWOb9w23umKW7Xqm05fCKcu3mmp084jg+oP6rW1KshwRMQ+9j5uljfRz/evG4TH/pH4dZOAOP9gGDDZuEpHy2c5OG4U8Hf9wXAHU/Nk6xhrFTMO+r8oMV7yZQgaY/yzbUuchpspSW94rTYzmqd6ijbyb66neJ9OINokBOGEy2WbjatPJfPqwNi/7tKFWpb+Q7QEW8QRXk0FiJekha UNiSsne5 9kZTvk6wfaMr+pQYNb4BNRhPpZHP1DjE9UMMZGIJWVKx9tOw5hWMKTir2KVd67Rtd9vpmKZY3ezeSvFzeWprrvt3j6S90/eupYEqEfh4S+y6LUwqYCz3gt937LJZtkqZLZve2SQosr7Bs6ET1uDf769nbx7AHFSE7SJ53gosYG6JAsAWL428iD/GPBIaIjN4bd2LYWYTloez1txyV80CkxzuKVxp2iTCAOEib1XTN1VivSLXdS9AT5G3PcF0hD+oyExJiMPLS3qAKy6MphjUV3A916uoKXWn0PnhSCk28UcXccaEOHek0YuzASvelsa4mqZmV/Iixk32eLhfIENaqgOjxhhLgkDjEQU9JP13XUjx0LlI7tevM1P6NFh8CTkBdbZjP17F0wf2TewPeakH3TKLMO1wkBjsaK488i0/O/zd4TKoADL4EzpzDrmYKbl9cjhlJF0X5GVZ2fxg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: zicfiss / zicfilp introduces a new exception to priv isa `software check exception` with cause code = 18. This patch implements software check exception. Additionally it implements a cfi violation handler which checks for code in xtval. If xtval=2, it means that sw check exception happened because of an indirect branch not landing on 4 byte aligned PC or not landing on `lpad` instruction or label value embedded in `lpad` not matching label value setup in `x7`. If xtval=3, it means that sw check exception happened because of mismatch between link register (x1 or x5) and top of shadow stack (on execution of `sspopchk`). In case of cfi violation, SIGSEGV is raised with code=SEGV_CPERR. SEGV_CPERR was introduced by x86 shadow stack patches. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/asm-prototypes.h | 1 + arch/riscv/include/asm/entry-common.h | 2 ++ arch/riscv/kernel/entry.S | 3 +++ arch/riscv/kernel/traps.c | 43 +++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/asm/asm-prototypes.h index cd627ec289f1..5a27cefd7805 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -51,6 +51,7 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_u); DECLARE_DO_ERROR_INFO(do_trap_ecall_s); DECLARE_DO_ERROR_INFO(do_trap_ecall_m); DECLARE_DO_ERROR_INFO(do_trap_break); +DECLARE_DO_ERROR_INFO(do_trap_software_check); asmlinkage void handle_bad_stack(struct pt_regs *regs); asmlinkage void do_page_fault(struct pt_regs *regs); diff --git a/arch/riscv/include/asm/entry-common.h b/arch/riscv/include/asm/entry-common.h index b28ccc6cdeea..34ed149af5d1 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -40,4 +40,6 @@ static inline int handle_misaligned_store(struct pt_regs *regs) } #endif +bool handle_user_cfi_violation(struct pt_regs *regs); + #endif /* _ASM_RISCV_ENTRY_COMMON_H */ diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 00494b54ff4a..9c00cac3f6f2 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -472,6 +472,9 @@ SYM_DATA_START_LOCAL(excp_vect_table) RISCV_PTR do_page_fault /* load page fault */ RISCV_PTR do_trap_unknown RISCV_PTR do_page_fault /* store page fault */ + RISCV_PTR do_trap_unknown /* cause=16 */ + RISCV_PTR do_trap_unknown /* cause=17 */ + RISCV_PTR do_trap_software_check /* cause=18 is sw check exception */ SYM_DATA_END_LABEL(excp_vect_table, SYM_L_LOCAL, excp_vect_table_end) #ifndef CONFIG_MMU diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 8ff8e8b36524..3f7709f4595a 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -354,6 +354,49 @@ void do_trap_ecall_u(struct pt_regs *regs) } +#define CFI_TVAL_FCFI_CODE 2 +#define CFI_TVAL_BCFI_CODE 3 +/* handle cfi violations */ +bool handle_user_cfi_violation(struct pt_regs *regs) +{ + bool ret = false; + unsigned long tval = csr_read(CSR_TVAL); + + if ((tval == CFI_TVAL_FCFI_CODE && cpu_supports_indirect_br_lp_instr()) || + (tval == CFI_TVAL_BCFI_CODE && cpu_supports_shadow_stack())) { + do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc, + "Oops - control flow violation"); + ret = true; + } + + return ret; +} + +/* + * software check exception is defined with risc-v cfi spec. Software check + * exception is raised when:- + * a) An indirect branch doesn't land on 4 byte aligned PC or `lpad` + * instruction or `label` value programmed in `lpad` instr doesn't + * match with value setup in `x7`. reported code in `xtval` is 2. + * b) `sspopchk` instruction finds a mismatch between top of shadow stack (ssp) + * and x1/x5. reported code in `xtval` is 3. + */ +asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *regs) +{ + if (user_mode(regs)) { + irqentry_enter_from_user_mode(regs); + + /* not a cfi violation, then merge into flow of unknown trap handler */ + if (!handle_user_cfi_violation(regs)) + do_trap_unknown(regs); + + irqentry_exit_to_user_mode(regs); + } else { + /* sw check exception coming from kernel is a bug in kernel */ + die(regs, "Kernel BUG"); + } +} + #ifdef CONFIG_MMU asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs) {