From patchwork Tue Oct 29 23:44:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855736 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 5B33DD7494E for ; Tue, 29 Oct 2024 23:45:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 268FC6B00B7; Tue, 29 Oct 2024 19:45:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F2166B00BA; Tue, 29 Oct 2024 19:45:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 047126B00BB; Tue, 29 Oct 2024 19:45:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D58616B00B7 for ; Tue, 29 Oct 2024 19:45:11 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9735F1607CF for ; Tue, 29 Oct 2024 23:45:11 +0000 (UTC) X-FDA: 82728272298.25.2641D11 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf07.hostedemail.com (Postfix) with ESMTP id A60F84001A for ; Tue, 29 Oct 2024 23:44:34 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LfzOa5a9; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 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=1730245454; 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=CTbsvOHRfAmS9fg/eDt9XRAZ9JnlumsLrbz7p3xsVRs=; b=NcyXTjgNPNkQD3ihsoJbOwuGIiaqRiyhnGYs19eSo1jd3g4qInU3NXB5vhbLLqjZgMLvDH 79S3eI7YBJs4sXWzZ8EtIiNCBTC25oaOswwBf2+R0HriN4CuNwrCIUyEnFa72SIJHN0ype Y/0cPf2h8F7oAfjuQZOUil1WgyEIKHk= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LfzOa5a9; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245454; a=rsa-sha256; cv=none; b=UVfvdXUSlutMaudKMtVWYaNr24Z61zyR3p+2AW9w04wDe4gaqBFjj4uiX5zERsEim6Jp23 JZYJjWMcbje9Oz6EGISNiwDhX/RBfYQyedKTyTrnTt77j5cNei2a4I0Cg9pjRTlkZtdIjV m9x+ZJ4Yd45homp6BljxsZXto2CEAUI= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-71e3fce4a60so4358273b3a.0 for ; Tue, 29 Oct 2024 16:45:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245508; x=1730850308; 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=CTbsvOHRfAmS9fg/eDt9XRAZ9JnlumsLrbz7p3xsVRs=; b=LfzOa5a9CBbOa66mxHwOb8OPQF/3Nog4iKsE3pt2rv8MphzbbZuI0yKaBeyDwext/t 3L1SK1ol6cNsB1ouZSXHKYn3gs6VDYfhps/RN84idhN8QQrgcVvQ5PpmVkue+b4+k1Cl L3s4NhnA0xljObN1kEKLZ31tUic1jVcJOmNyxePhYOmJ6OcCyts+hIen1XePcSjAHwlG ji6pLsLzj8fU7Jf9jVk3Bpz4+UkJPNrOa/5RYV1p3KNz7ckKzZmRZrHqD/VBsxRKT40M 0RqbEnrQXr5nH1qpj1UGqcCGpc7gHl5LRTpa/qGlvkXl3W7DYnMEPsCvuh0QTy9606u7 5fXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245508; x=1730850308; 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=CTbsvOHRfAmS9fg/eDt9XRAZ9JnlumsLrbz7p3xsVRs=; b=sBvy1DKD2kt7cAZLndDHmO4y4iQ/IwsaY15WadOsUTsEWKQ5mMoJIIlwwWnlg0JWv0 0q1aOs+5i0vaMNZZrQdvTCNXIJsqc9NHjD28fSeQYwFJkPcK5imNLTCv4Vewvqp12xj6 caaiilHbCdyDLKYwgfZoIbfESAgy1pPNpiMsViDW6eyEBfYAsV1pyKShY0zAdgJud0/V UL+NDf2NJQRcgDi3m0jwrxkJfDwDir/I9V0l2wuFX5l8pWfevaa7Nhne6N321hmc+VZU tZcWxsOcVnPxrmivaAwaipBrb8WjeuASMj7VMaIwsgmw4uUq1lKJOlFbuEIK/GfbWZHe raSA== X-Forwarded-Encrypted: i=1; AJvYcCVklszkbGDbJJv0V5iJkFZ5ad5SMJgSBAve2jYqPJ48vHtI3XAccuLOqGtRJ2+A+wNJUWHMFz5wrw==@kvack.org X-Gm-Message-State: AOJu0Yz4tR0wM3Stq+lzTobyigzh/cuCyvOLuIHWoGcku0X9tiUrdSf4 /F7cpDk+zgw3OHMqxcBilbZiLvCZwUs6L6YL4CxEecX5E4SQLFnbBkiTcKFLtHM= X-Google-Smtp-Source: AGHT+IGGDI9GHaayPj4Cs7Vha9dStUeFmWxlHEYmFUmhCBfJrJaPT/+XmpxPiVHSTz0RAhEYFmvh6w== X-Received: by 2002:a05:6a00:1892:b0:71e:4c20:75fc with SMTP id d2e1a72fcca58-72062f67d62mr19737734b3a.10.1730245508363; Tue, 29 Oct 2024 16:45:08 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:07 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:20 -0700 Subject: [PATCH v7 20/32] riscv/traps: Introduce software check exception MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-20-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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 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-Rspam-User: X-Stat-Signature: 6dthrpdfps4bmu39z9ti7jskpbf1b8e9 X-Rspamd-Queue-Id: A60F84001A X-Rspamd-Server: rspam11 X-HE-Tag: 1730245474-695444 X-HE-Meta: U2FsdGVkX1/XbxSZW8p/jfNCXW6tn3KTD6K0eXflmW7X6tyRcuD2TerHeSf1XpG4N8cxZr4H1ZxXza1aGklaHSkau8TakCjjQsnrZAO539nW1UN2a0+OtRu90L1Ulx3hJPQWu4eW/ASKkO9yTdqo7wSb4zwVVotYW6ODq2/uRe29NtNjN7CdDLSt9zafnWEpBtMoDyiAhLPCaygCmYQklLZX8NNPlxeEF68NImUJDHvPi2lh1CfSBKPOxA7xvtbHtJmtUhdfkRNwdvajWaQM2CAHSGMjs/T3kmFLa1X/v4vLzqoJcgPrYqb8owqjvCpUm0+yuMmUyM/0lXaixrtO/OlW9Q984rwS1cARzSpWdioUM5RZMK18/BlJ1s1KhvDdCVutmhB/cnOwdT+b/uBZCanj3RwwTv+dC0R45deMW+k5OmYL+jY2dPoeI0eRm/Ik9MCx62z5/S65OL28nkZvflyIEGjqHQKHhkpTHTGJd4hfx7PESzgbwXi0LRL9mIYQYBc1cEoyE+pdij36tOS3jqu2ke1OPIpS0Fs3BpFFazzD+DI0QQHkLp9pb7G4jIJwoHDlUa1gKcTKdvHCIoTolUtjK/QeJjOrdYzMdOaAjWxJJr+0GNiZf2PVqhSovoWjvhSQ5mfVTm8JxjiN7NtnTKhDbGzU2s5OLfS+aZ5lFe3mhJKP05VevVVTkiawFltcFfF/FsAtsJkmMkv/ChBeXtzFFlHbuUvZg1rB9ozK2EhHG8RWj2W+EozXeD1fRl8Y5zcJgMJGI+pPTRRPuiCWEFQ4hfZf3znZj1sVuCF/nGiVkBe8S6IEbz1bXJEI15bDhTiJrxzdZcngB7vLNYsIKdWpezhE6U0E3hf9CMWQv//0s3VPBOTyC5SFSlInDBNNncUb8FCw1xL2ZGu1iLJ5yFl6NDZnk8rJ61p1gBKpeBx2tO5oD0HJf19NJTYXMTYG+9yrt7kAY237SBJRuTE VdvX4x6T w3702r43AuJfKPudFhgRx7ilHaGs2XOmK9kNrANiJpsw9JIr6eRo3xZTchxsSoCioZuPj2qHbZyCj36DVd/XYKRbEeDxz7ezq1sVJzr4pp83D23/yI348sA6YcoyF9+gXQfboB0kgeK1lWIxRgqcpsefHfghSQxzYq9LvILD0zhVGFepCpzqI7HeZh8QP2rxNoL6hfYL9dxIufZvKh1OGPTifNUnv5HN36h8YXvZkRDhQ6G9NwjiCkwMjHHE/LqWqoxPy+763UARbKLMGQQ6oIV4IkAivJgtnh8rYS+QS9vNFgGBv3hzG936QVLRLyWonDTPQh9VSBpqHn2mILqpSKWPYANGGnXTaiIuVT0Ba0GaDUpRbrhwQqr7vlACCuf5zNM6tq2K6TQE19mTot6HX5UHzdKCXgDGenBe+zFdwOmRR5OxsnE9FhbXHzTDHn6221HvzIGq//gk93mENp147J1O6rw== 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 | 42 +++++++++++++++++++++++++++++++++ 4 files changed, 48 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 2293e535f865..4068c7e5452a 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -39,4 +39,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 a1f258fd7bbc..aaef4604d841 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -471,6 +471,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 51ebfd23e007..225b1d198ab6 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -354,6 +354,48 @@ 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) {