From patchwork Mon Nov 11 20:53:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871343 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 7B2C2D3ABF4 for ; Mon, 11 Nov 2024 20:54:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1313F6B00BF; Mon, 11 Nov 2024 15:54:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BBC46B00C0; Mon, 11 Nov 2024 15:54:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4FC96B00C1; Mon, 11 Nov 2024 15:54:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C238C6B00BF for ; Mon, 11 Nov 2024 15:54:14 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F0468141A50 for ; Mon, 11 Nov 2024 20:54:13 +0000 (UTC) X-FDA: 82775014728.07.5B93A21 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf26.hostedemail.com (Postfix) with ESMTP id 0233D140010 for ; Mon, 11 Nov 2024 20:53:40 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hUYIRPNQ; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.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=1731358279; 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=/wlYhlTfRbz917f0tizIL6fD52nDOL2jxneRKObslEg=; b=W+U5lJU/E+v6RqOOxppNehxbUFpXSmSXGbkXNpJwphSQLw0HteMZksrPdFaZyXh5x28Xhs +6202j30pWyTNHB7qKWw/uBNDQpTQKVKfIJ+t+zrkT4LeTGhBz6slxqqRAi6VVrhja+oJP Jg4P/J9QABpSdyUwO9to61BEM+JX0AA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hUYIRPNQ; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358279; a=rsa-sha256; cv=none; b=0skPxIAjr1RLm1VRrv69ecihFr6JEUJSH9feV4WH4zRUDM/q5dp2N+5PmgAo4YfUBIyY1t xagHsKE/4qrbiL/m4zDwJaAEjBKZV05fmBpBT/Js9rUazva1CKhnryCen8/RaWhD2/f0fW zmhLuW5d506phulpem/dLLVPI7mIodQ= Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7ea9739647bso3401706a12.0 for ; Mon, 11 Nov 2024 12:54:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358451; x=1731963251; 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=/wlYhlTfRbz917f0tizIL6fD52nDOL2jxneRKObslEg=; b=hUYIRPNQcR/wrv938JAIs6vi9o4edjGuwXLYtliZTt/k334daE476YGH2yzgaxbBti ryvPQFtHEm7UFXBC9fy378PT/qGgmJ79sK9/0hDQxdOVw8fjTEX6uaKLsE5yf0AMErHz 99s6BeXkykNR/lZi42B6fLYNEGR4g3Atx/bjYTY33zQM/w8siVBg9BzxnsH/QbMPGIbs J9M7qy0y39hEYNjPwMZmQm6buBf27Yrrd12Dkwmkc1Q0Fe336m6lN630ZN/Wje6VxtRB zFaC6POVqGAh/A1n+oRNknW7iPNIr9oTaHd0VVohajWeXaClmhsB7KCHjVB5bxl4iqLm KIvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358451; x=1731963251; 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=/wlYhlTfRbz917f0tizIL6fD52nDOL2jxneRKObslEg=; b=KCEe553dlCDcV7O98arHFVB1gfitRdHi7YZ3WdcpticMrCX3bU17SDmiLsDIPRxjcl WKJ9ZfRbI3Gid1LsRR0pTkFk8Rzbh/vfKCdrsnHYz1SeKE2d/wa6N7maZE7XEZ0PCXRB toQYBhqie/occdb37hF5lYI/1XGDkTaQ5vSERo5s17CD8NA93MrcNHmRgDgfrNFvmjkJ ZGTCh1Hw/61GfdkalD7S8NIhgdJPkWqbGSsmcGTAtV2UaRXWZWK65LT2YIvsSYoQU17Q 59Q5/0gycuZnNgXdd75LYVxHu99TIEekxwmJj05j+vvsASCbbmxF/s2UpuFblaN7z34h QxPw== X-Forwarded-Encrypted: i=1; AJvYcCV8+BlmK8pFznTAlmK0AqqNnhopWv0qAk6FjXrKu/UuClW7iiTkM+H5uc9EA9yiICvhJaFZdUElnw==@kvack.org X-Gm-Message-State: AOJu0YzW3rZdqPpS9sU+J8nqZtxd6OIliXJX//4xGiBYxSKXT8r6k4ku C3z5+jZESB3gdLXZUoExOvnsnVQwDz64vAhPQYlqu85gYtAYjuG0iUxHD+6Os5g= X-Google-Smtp-Source: AGHT+IHCStdYWzF0a25kqcOGnG0Pu4JGvRjMiuZ8NNCn1MCLP5RJQJdVrm6DVqL2Hypx+qG3O/OjVw== X-Received: by 2002:a17:90b:2747:b0:2e2:cf5c:8ee3 with SMTP id 98e67ed59e1d1-2e9b17138bcmr21222036a91.10.1731358450607; Mon, 11 Nov 2024 12:54:10 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:10 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:51 -0800 Subject: [PATCH v8 06/29] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-6-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam09 X-Rspamd-Queue-Id: 0233D140010 X-Stat-Signature: 6b14r4wyup51sgwd8wzp8oiyph1yuoyn X-Rspam-User: X-HE-Tag: 1731358420-224526 X-HE-Meta: U2FsdGVkX19r+YSt+2v/KZkc44Ly8Guo36LQCMujqdABbzkgpHaIIaqCFm29zYkMPjIUFexBclyyoAhW8NkvBbVkGnMk3PcgkEx+G8+vktvbro9rYMpdGefcyakRZfwqRGY1hChbSyzsQsLV8kapEMWZw3w/4p27bnbOJEWqh2EOhYqrMW2cv0WUlLOXJrlW5d0zPHbU/C8F1w/8p+5ZvjIlRZ4RNKnW3/SUQhtabQCBlyRBlkMeGFfp78+0WT/Yka+eh7vGrxn5amQS7uHVI1TvWIs+aBSMRLZ4S+zOGcW4KiLhWi3HMP6kH/xiIUqVMAZ13Gnt8FMnl8LGxI6dblpKt3Vlddh7Ilmk3yVXTaCK9AGYmUpE7ekrgyLRDGmGXlsXuBgjmVF5xYtCYPV/tTsyt6P0QXvbg5SXCUjzyNcNlkpImLiH/4/tUUA5rcihdMRxrwM84OSkRfPcQlTlu/yVpWiDOmNXPZiIyLeQWq5bISnz2OmnljbNVf+vm6Gyp8PlglesUwrNLru8UEHt8P0LWZXJT01CGh+9eYPTFRljupkYWPYp05+X9oqIJi+2RdvdKC9u2ePdEf/TQAYWNx6ZYrQo5YRDdC9ee0ImSPtQWYNd4MiVOzDO/0Ng8epndqsO0uRQ9bsW5RKnQDRR3LqfKNPdnsOkpUc32ldZVxvgmShpxf2KQU4iEaFE2KIv4t354o9aq2UkP+K+AIPGiQ1USg93+cIF78ixx14h1QbHp6jkRs9XVXX/PVz3AvvJMGGHDfBe4FF3dglegVej6OLYIEFkuHytw4M2aCwHKUr5SN1LrgT07YHIP21J/lVYCaZ9BTrWUfyajHHehBwkO1ocEC2INIOecCa20vMm2/0eN27/3rU6oIyDn1PcV13XvZsBd+yn85fSbM4RMe+B/HeABbaXevk3DxloldaqRuLpQcgu/1rrMm3YMoE1EHXgubvM9ryHBVn7FAO3dPU vFirNeu2 qF5oQcWhtTIZ+ilNg4SJxItmyuqK8xrmu30NYzgIQMov8D1vCxAYAiqfMroegFnHzeGr/IMynrQtXphsPV3gQ/hC5StMTvN8EJjbQNtOyB4OvjgqiLOD4IFxDaQvzWY+NezcOo8gI0F/ddzZOtYs03qJDr7fVnwPTkyimrM/x1mJut+0y2P8+rpW9+OkYVAXrSXsRHa2ULn9ZldtiouG2Y54LnItV43I/p35gsGmR3gldtfyKRxbBIEC0N1n5bGMn0WhZJ8wzDCi7pZgGHImfeB5GmZIF+X7h5MKOESeEqa2y/Xlhd0qkuFls/nMkdUdiYNe5qdOsgDEVBrz1fYGz/QBbXeN49pfxCHWvOmcMADBgcl0HPcW/Kmo93QQ/0Cfg///eadSfJuDYpxBJ0a88deDBlrdsxY4Rl0PMnYjFYIgXAPib8PvqSqLXenpOc0mUX1g0mg0xHQGg2CC64YZvegKQhu4SjaCfz7rymAdXOxOLV6tuPEYYg1EC5qGfIraV+MUtCHC+coT88l/GQeIj+O3MC7I9uMMFQuRY2ghXmABKZI4= 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: Carves out space in arch specific thread struct for cfi status and shadow stack in usermode on riscv. This patch does following - defines a new structure cfi_status with status bit for cfi feature - defines shadow stack pointer, base and size in cfi_status structure - defines offsets to new member fields in thread in asm-offsets.c - Saves and restore shadow stack pointer on trap entry (U --> S) and exit (S --> U) Shadow stack save/restore is gated on feature availiblity and implemented using alternative. CSR can be context switched in `switch_to` as well but soon as kernel shadow stack support gets rolled in, shadow stack pointer will need to be switched at trap entry/exit point (much like `sp`). It can be argued that kernel using shadow stack deployment scenario may not be as prevalant as user mode using this feature. But even if there is some minimal deployment of kernel shadow stack, that means that it needs to be supported. And thus save/restore of shadow stack pointer in entry.S instead of in `switch_to.h`. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 24 ++++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/entry.S | 26 ++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index e3aba3336e63..d851bb5c6da0 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ #include #include +#include #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index 9c10fb180f43..bb20bb7d17c3 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -62,6 +62,9 @@ struct thread_info { long user_sp; /* User stack pointer */ int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ +#ifdef CONFIG_RISCV_USER_CFI + struct cfi_status user_cfi_state; +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h new file mode 100644 index 000000000000..4fa201b4fc4e --- /dev/null +++ b/arch/riscv/include/asm/usercfi.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ +#ifndef _ASM_RISCV_USERCFI_H +#define _ASM_RISCV_USERCFI_H + +#ifndef __ASSEMBLY__ +#include + +#ifdef CONFIG_RISCV_USER_CFI +struct cfi_status { + unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ + unsigned long rsvd : ((sizeof(unsigned long)*8) - 1); + unsigned long user_shdw_stk; /* Current user shadow stack pointer */ + unsigned long shdw_stk_base; /* Base address of shadow stack */ + unsigned long shdw_stk_size; /* size of shadow stack */ +}; + +#endif /* CONFIG_RISCV_USER_CFI */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_RISCV_USERCFI_H */ diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index e94180ba432f..766bd33f10cb 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -52,6 +52,10 @@ void asm_offsets(void) #endif OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu); +#ifdef CONFIG_RISCV_USER_CFI + OFFSET(TASK_TI_CFI_STATUS, task_struct, thread_info.user_cfi_state); + OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shdw_stk); +#endif OFFSET(TASK_THREAD_F0, task_struct, thread.fstate.f[0]); OFFSET(TASK_THREAD_F1, task_struct, thread.fstate.f[1]); OFFSET(TASK_THREAD_F2, task_struct, thread.fstate.f[2]); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index c200d329d4bd..8f7f477517e3 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -147,6 +147,20 @@ SYM_CODE_START(handle_exception) REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 + /* + * If previous mode was U, capture shadow stack pointer and save it away + * Zero CSR_SSP at the same time for sanitization. + */ + ALTERNATIVE("nop; nop; nop; nop", + __stringify( \ + andi s2, s1, SR_SPP; \ + bnez s2, skip_ssp_save; \ + csrrw s2, CSR_SSP, x0; \ + REG_S s2, TASK_TI_USER_SSP(tp); \ + skip_ssp_save:), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) csrr s2, CSR_EPC csrr s3, CSR_TVAL csrr s4, CSR_CAUSE @@ -236,6 +250,18 @@ SYM_CODE_START_NOALIGN(ret_from_exception) * structures again. */ csrw CSR_SCRATCH, tp + + /* + * Going back to U mode, restore shadow stack pointer + */ + ALTERNATIVE("nop; nop", + __stringify( \ + REG_L s3, TASK_TI_USER_SSP(tp); \ + csrw CSR_SSP, s3), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) + 1: #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE move a0, sp