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 25BEFCF042A for ; Tue, 8 Oct 2024 22:39:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F0B436B00B7; Tue, 8 Oct 2024 18:38:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB9066B00B8; Tue, 8 Oct 2024 18:38:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0DE16B00B9; Tue, 8 Oct 2024 18:38:49 -0400 (EDT) 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 ACC986B00B7 for ; Tue, 8 Oct 2024 18:38:49 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 196C2140D4F for ; Tue, 8 Oct 2024 22:38:48 +0000 (UTC) X-FDA: 82651901178.06.3A1DCEE Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf27.hostedemail.com (Postfix) with ESMTP id 87A8C40002 for ; Tue, 8 Oct 2024 22:38:47 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Wp6MzIct; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 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=1728427059; 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=j4AiFsRLeV62lBIMG1PY0Kuggf3Da9jB0k3/CgFujB4=; b=aerWvlClZgeDy9ExmdpZN09dL3228ivBvlLeyE3jxdxuAYSXvwdWCmo1NamTSoetzUceOu lth5OjyEIy5cXNbartpCJESbEFdX9ORjsqh+evljAhPSXuxVlXynthQ5Dz0HEGbaJ7gTQw q4SFDTSRzJnFy+oQ7AMpnsc0v3QHvUM= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Wp6MzIct; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427059; a=rsa-sha256; cv=none; b=v4cey4OO6h9scqpGrU8pBAWahTiph7RTX9HEvMF+1S0UAey9mgI4cGXylncFWCypMQX63o PFI7xICpMDvBznNhSUmN3eWX8R8z39SydJBKFKNQt+MeF7uXrmvuvHkXHEphPiukmXcMpV Cq29SIFwK4+27pYqpdDIRR/13NDxB3I= Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-71dec1cf48fso3808902b3a.0 for ; Tue, 08 Oct 2024 15:38:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427126; x=1729031926; 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=j4AiFsRLeV62lBIMG1PY0Kuggf3Da9jB0k3/CgFujB4=; b=Wp6MzIctOZRVJt0p+To5Q4wH0zm/8w5Og5Es9TXkN6xeqBwbqUqOvwflZyeVUhwrII gIhDvVutg4hUA6DXpYEIpwKkXQr5F/FSEuRDdd8d1b5fd/30sRJgOwX61VmoZzebS2Xw IgTZoHOFgiF49az/oFP6VVOxFWXqDXnkr/Ei0/ix7jjtI4FRB128mq2PEQdQhk+QPJRO kHtEy15dDtQpvcW9Yb/k3TBTnMhmajWX5urdRsGGkmgGTAVLvNTKAHjQTSZ153FTF4tw sJPtXMg8acefA4kd+9SKEdLOKmjF6gp9zqxPzhVPkjwXP+uT6Yj+4LgqjytvaMzwYey5 ySqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427126; x=1729031926; 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=j4AiFsRLeV62lBIMG1PY0Kuggf3Da9jB0k3/CgFujB4=; b=tE0WVeEPoZ2RkiyGUifBJ7gvQlip1ITlDJHQdadaaqz5u0Qjcw7ARpFNEAR+Z19Uzf I3ImmNDgpAbz1rYUQjUBQ6Wo5ZgWyr5JGs85r9Jsbj0FEKElILvWoa1ugHPtnDS+4/rs Mh4vthJBzTpthFwe7haWqd/H+tToUR66qAdQVfUO5skLdrQk0HHXTKm3imtC3A8cOBI7 Wp0erzgWiyQIyu4x4QfDq0tyH6BXzlXT1nqZma0rrdXEuzbgN8P0Oru7/NbnTMj//kbU EtjlPLHx6Dv9plGprjgz5x9SlcxwolGv1JXQdl4y7pqCdeawH1n4xbVy+IMvYakCqA64 RNnQ== X-Forwarded-Encrypted: i=1; AJvYcCX4c7DNEY2D/0et1kmrlfB7CjH2wb20AuTTnrKtgI8czrjIlse/QUJOfIak3CeMMJlMh9Zjtvr0lQ==@kvack.org X-Gm-Message-State: AOJu0YzAViXI0a0y+kBDgqDZcwrDs/A8scITn338udo9s1SxvWx2plDf 9Mq09VfP2lgZs9/nSprdGzWHRTQbLrIboZwWxuI7+hfhHgZ+/JllJtFkosy2YGA= X-Google-Smtp-Source: AGHT+IHj+wxxQcaXZGibjg+ho7ihWvomHSyh8+LLrL/jLeKQmqCGUMNbfsMhMaogGrs0QJ7WN9Lawg== X-Received: by 2002:a05:6a00:198b:b0:71e:135a:ae5d with SMTP id d2e1a72fcca58-71e1db7555emr586407b3a.10.1728427126363; Tue, 08 Oct 2024 15:38:46 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:46 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:03 -0700 Subject: [PATCH v6 21/33] riscv/traps: Introduce software check exception MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20241008-v5_user_cfi_series-v6-21-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 87A8C40002 X-Stat-Signature: yq3yjhz7imzwoj7k4s6gi7168b3zk3rm X-HE-Tag: 1728427127-607724 X-HE-Meta: U2FsdGVkX1+B/ttZRwZEu+PNn+JadzQjxTmo084U5SM/QusjN7RCUG/fbx7R0r9uqdf+4ZaEyFGX70CSdOSpYUo4p+U70rj+zK4es2Dx23mP5ak/Lr9owj3kgqqnXva5UEoytFn6KUi4cii6RfajF2EmB2XRUi9ld+XbmXV4zQxFtpbVGbYFJsUGOMTSL+YYTT+vAtn66MeMIX2vrVgp84IHWvCg2WJlIcYJZCZWAoDnunlCd/dHhPFBd1L83WrLi6Hu8oqD0HddbE2oFP/FpbowSR9D5/6yKVzsNHznn41zSpopB6NrkQO17AB7jrRJF1HrEh+xAbu8Eby2fpEE7zY6aXbVt8/9tKe10dp7noAZ6sVCkD4QKT4Wcwooiz94uZ8cipm/p/e0rujcuntBbv0slPh8bIi0o5NraeJjEBakZM2IhzQ03tUWXrtinJB/EBIEdD42rqU836Pa3ai06xv8CimAOdvMyZONM7/g0sQVz+lOMZvBNaqIngNg0i052vk5zv9ImJHyvK1WSwF5UQPYExF9WsfFnVhMFi6nKIrY+miIBy5ctgH6QssaP/BmXx1y42mCW9mpsQU1aREKYo7HP6Mmp5cBW0j5SCaUpS0ufgqIzrWm02vY9I8BX42TLdGHxtMRMHCdbKQ7vkpxQwUHiVc3/UUaHnk2qFt0mtkusKD2FAR/iUzlCZpR/eq/YMw9EqsyBowaST9xbaYA/ccMynzNiNtSt3NqtI06GT+eqncatH9K3qFDgNumsH3cfz4LSTFk6+K8QlSLXwx/U6HD69O50nMxdtHqN9ohLeBSpXXGg7K06b5Qh9havDxiBplKOQ6RiK1uu3IPzilUXTuyfa9M8UTDvug1Ml6nY0aGYpvfk2oY5M1gYSr0xvcFo7G/K9j5wOnxO3qflV7Kuda0vn1WfOgMFrh3dyHGekSjpAUEXt903lxIykbvkjR8RMwerQFn3ptFwbEixkg 2RIolt6t bSqWVMUSRYjZX/1TqiiZLCw8kABE6Q67eHwyC1QwB1qpMy+JhxMaduUlqRgHWOH3tJLel0BxYx6DGSTObwTuHtKVMaYV5gSzzBPf0AEueAAFqsb1SogLidA8iE6GQZv/2OhHi7g6fkVvf/Yfkftb5D4uK1TKdQH2nmTvYCEHZFScr6VreYwgO1xkCd6dVnFx5gud+JKtw/wZI3EezfMUNXwu9yvkIkkxHSLR8IG7yo70UpgBSuoHMn/567NoSBu7dQxhKNoFnmfkBF9hrSMBZqGCdRpT9kB6t0e/U4zJ30mUehrsppIFQRby6PvF0IntDrbWCSzXwbX1SAnIkr6K//OLiOQjpKoizY8kfSeDXpnBBOTmW5FKAUhYjMiopK662X9vfnLnLoQ16cbDdXxgJElBa5rbYU+OXNBDqJQ9+nBIY4rpJa2U66XCn2sIz8NT3x6eQ9N4I9fYHywqecdsNz26YFP146fZHRni9ziRUbaZiYTiJIFOGqJo1t2R/09L/Z8vPSrBK9AU1kXld3Ov8b9OAgR0o9rQamjlR73GNNg5HN3FM7yxhrL4pjvm+vlF0A5DVR3JyQVjcPls= 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) { -- 2.45.0