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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94E92C369AB for ; Thu, 24 Apr 2025 07:21:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0A7496B007B; Thu, 24 Apr 2025 03:21:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0303F6B0082; Thu, 24 Apr 2025 03:21:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC44C6B0083; Thu, 24 Apr 2025 03:21:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BB9D36B007B for ; Thu, 24 Apr 2025 03:21:12 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6768D1A0B08 for ; Thu, 24 Apr 2025 07:21:14 +0000 (UTC) X-FDA: 83368091268.27.1AEE786 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf29.hostedemail.com (Postfix) with ESMTP id 859A0120012 for ; Thu, 24 Apr 2025 07:21:12 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=e9Sk2hme; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745479272; a=rsa-sha256; cv=none; b=pigd8mZlGItwcEwL84qqXtiUIi0eynQE/WcLdJLsVurFZwnlH3HJ0G2HXDjaHKFXQGcCrj 3M6YFhHRtK/tHKv7SNI/aiDwNiihMeFeBbhh0d6uEdsiFa/nFe64XxGy9jn52PEfhqu8dr qkk1fT/BQMVgRSaw8WukKAqHo77pmX0= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=e9Sk2hme; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745479272; 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=vNyUtqhy49c/aONmOB6LtdzAVyuHVQdM0m5j+dRROOM=; b=KxajJ0KHUiySUNx+eh27iTSfcTHfqpJAkZJuePFe38rp8An8TdtfKc/SwFb2c7SoH0qVN4 Ed1Z02jrcj6J+a/DuCi6VLr8oxrPNjX3TumagkfzYHyo+ibcc8aYtkTtgUJb83eAYJKyj6 xO+YDJQUl9MsBAJlheI9FMjo3Om1RfE= Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-22c33e4fdb8so6640655ad.2 for ; Thu, 24 Apr 2025 00:21:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1745479271; x=1746084071; 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=vNyUtqhy49c/aONmOB6LtdzAVyuHVQdM0m5j+dRROOM=; b=e9Sk2hmeTetkyGQQ300gFFwMFNNEyoSpcdh8wunKQqujjHzpuJ8safA4sKqduWtNPw nfeL3Rw3EEuX7/OZIjKc3XvsU8x2Z4V9d6l+utsRBYlQQoAW2ucIJaOao7W8oPffrwJc eFqpNZEYbxDPaAZhxq/UEnAoaFW2UG+Ah0EtBR3AlfLo5qvun75VMG9BnYIV7Gf3PGdt b/RbxsrxWtFlNtdPTUJ5sXZYst5/FEyl8lOQXJGHQSQuv+SBCMhsIA4E9WpFBt/0iTw6 bPI2Qzb+oQ7+wu5/xZ5nIVx32vfLb1J6QKk+KhM0A69EmrVOE5f78clWOnpjbOuFLkjB e4mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745479271; x=1746084071; 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=vNyUtqhy49c/aONmOB6LtdzAVyuHVQdM0m5j+dRROOM=; b=ao4a44OsJvM8pgVDwbP/uMDuuI54eSS0GwvVT7cw+BNhp63b6h+5n9rujkTYhnFdyV h82R8Qw4VLp0uNi/SNNm/XmXPgr0ViXw6NsZguuISDxd6v+8syck/esh0eeXf6KwfDtA gpRRDeyzKKvRk2Vy3MUGQn/GsKH5/whHjcqpd0Mymq9daTr2/vzOSBN78zydPP8zWyqk PqG4siypUMMdggJIMtOoXR28ptrLFTEoAkj3vr/2GgRB8HM7YP9XCc/t5aA+XZXsWmat jz+dvS8md82BN/bZTmiJ0nZExhzkRll/xwLvmkOxwRckInfn06VVP6pBJ7116WoyHxAl 71mA== X-Forwarded-Encrypted: i=1; AJvYcCXojKgt+ixpdj5aAwq0lP1fng/Q9MBJwPCDzC10DnbQOMxLGIGfhbk+cRHCLRrzDYfp57IYyzkzKA==@kvack.org X-Gm-Message-State: AOJu0YxcbZV6tPKQs1J2nuMpJebET4ybXTVF8kNgm1FV4ZawJibodRdB yDPQYsP2rB49DxH+8zhiav1+2S6fCPQj1ziHK0OvZofEbR0srbKMr/JSxRbq3MA= X-Gm-Gg: ASbGncu9O2WIpZThNTe45LrKZTmCcHWZ7/L5hlxKGFZTQ9uRbFOTwi0nc9avoaYdHcm eGHpvSpuG5raOzE27YSwgPuzdpETKTjqPPqJeiXh77AfS12rUNpDHP/drJgw6YF+SJyfKhFxoTC 4qb5+FiXIn6TGtZf6I3kWDYGpOcLE8P2I3jjCeaqcJ1JeTvKlMamN1zTiTcpmOeR5umV5+P/Dve h5fytitwhXk6+PQqqDVIGQlQt9En1hbB9z8Z+jVVngpBGCY4fVwU9McVhqk2qL39zGK7rHhXzMH 1SbQnLcQVnNvUuict5SbOA6SfMZCHXL4hVTQBSpa3eTWmE+cYWEvVJsIapJbIw== X-Google-Smtp-Source: AGHT+IHadtNNYWZkDCzTiGYeWe9oA4Vsp8z4ELowNDAwjuf1JPHJyEoD3ZWaMaE6YM+bEnLB54Vo5A== X-Received: by 2002:a17:902:ced1:b0:224:1005:7280 with SMTP id d9443c01a7336-22db3db8797mr24350205ad.38.1745479271372; Thu, 24 Apr 2025 00:21:11 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db52163d6sm6240765ad.214.2025.04.24.00.21.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Apr 2025 00:21:10 -0700 (PDT) From: Deepak Gupta Date: Thu, 24 Apr 2025 00:20:30 -0700 Subject: [PATCH v13 15/28] riscv/traps: Introduce software check exception MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250424-v5_user_cfi_series-v13-15-971437de586a@rivosinc.com> References: <20250424-v5_user_cfi_series-v13-0-971437de586a@rivosinc.com> In-Reply-To: <20250424-v5_user_cfi_series-v13-0-971437de586a@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 , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross 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, rust-for-linux@vger.kernel.org, Zong Li , Deepak Gupta X-Mailer: b4 0.13.0 X-Rspam-User: X-Rspamd-Queue-Id: 859A0120012 X-Rspamd-Server: rspam04 X-Stat-Signature: dwao3ydrrrumtbmh3ss9zi99wk7f39wq X-HE-Tag: 1745479272-316304 X-HE-Meta: U2FsdGVkX1/XFwPwomDZbfiOl7P/K2W/Bj/vDfnTG6auMuQmVGEvo3KubXEtRV2r8/3mFbiB5bKQvrUQegf9+pTcaiU04IXZ6DtTBH26+pWazayQO0lfJcTB+bcYLqWxJ3Fni9WC8bgmYe0bd9Gb8t8SC4oKFd+6DPG2q+qnH4jeWWkuJTk3kELmUlyCGkqhjfj/nPQCzLxJifAP2B+TjseqDv4NzXcsW4pCkItlDZhOXH4OUVJ/zw2m8Yu9jFThXsvhDUUL1nQfzuNWsj20n38DvpxLCEo3L5b1ETR2XYqS2DeesWRoP6bFv+za/DjwoDtaTPA2TmfCyfn0NWCJiMZHLci+9mlUaalUYct3Tb6wlPawp1CMKukW1FA/Rgyt8kP8gI2Er02UJ3+5y4obP4HsR/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laAYSAgWcfl7veIZILnWemN2bKCejNWa4= 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. Reviewed-by: Zong Li 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 02442f8ad91a..bad75c32fe15 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -469,6 +469,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) { -- 2.43.0