From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B88FB1A7264 for ; Fri, 16 May 2025 15:16:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747408576; cv=none; b=pCBiFbR1zNdMG94eEVKdul/ji29PiW2yYy87bCSNP56WVE0d+vF4DZAY8wY23aq8zC72E4XdvoSElmI0qFjm40p+2pXKLlA0cXjqDAOrimA1m+fkd0Qr7AfF/CNzpchux7w+4U5pxFQ/cdHJ+tF9SrOcRS8BY+wVlJuA1Kh3ZIc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747408576; c=relaxed/simple; bh=t+2Gst9xAi5hlIwYx/cxza1UHimWoYdHPusTdQrrccY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iiyM7GXMrJXzJXJS0kRcyTZ6fA6BmyD5ubXRzEe4LUU6xsNvOEZYJwQiEw6yb424LNsH3zrlg+0Q5TdWs7PWgedI3F26PPX3KBxbZWx1zKFJsY1F1n0GEbtpTprQH6e5olBBzkm5F/B2bnRehHnnFy9UT7LO/GRchSfgB9Zox4Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=GQfsSeGy; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="GQfsSeGy" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-22e033a3a07so23783785ad.0 for ; Fri, 16 May 2025 08:16:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747408574; x=1748013374; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=0GNirFBXfZksDmcLkIx7giCST3II4UpjpZdHFvaex5s=; b=GQfsSeGyAB7JAoCVcMbV98hs3mI84XNwIJHFtrgZ90GXZKudh64JoDvi+O3SUidgzX wiBkIPz47Q97T6sfusCfCzh0Dk5lLz3omyUJigHsvb/MvSSSToZxSPeDs55kTmN+Dje6 Ki0sQL/oLrJp6rWPCpfIOTDzB9OS/ZWkGpE4op+b3+LdLJw9FuUA61krpV1YlMV3tJPq sjiDQ/w1EIWBYEb6Q1/yCANfG3KI4kwTvTy0TolvE/EhUPFv+EAYz+RSaAjDs5FwXb24 QgtLTuDiBW3kzQCZJyeDIGKyfXtcikj3A4LntFRTfkmy/zOIk4iF1Nso18z8PWPcJ3sj 3wIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747408574; x=1748013374; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0GNirFBXfZksDmcLkIx7giCST3II4UpjpZdHFvaex5s=; b=oa3UMjvQFA602hgiYtN9C/kOrOCuMiHH3kFxjtCRpmOmRPkeoPtMB0gtoYnXF2irmY rxCbe/d5T5Fiw/qbzskQGZNE2XRJcn8wA6P06m+pCBd9+w11ZxG6CTNxH01kyLIz7/Io BtmHWiak9arelthdOJKf9UhgMd4UBjyppbxgHk4LglfzoRgn1HQUIJxjvLqS8TsVhWAr 4T8ExoYDzV6AvaSZ+rghMKmyFr7A4Z+93q4rPPwUnQf3S8aehUtFG9QbLAvwOIcvnwIL dxZJVt9ReFZmsjXI/tveXEJviS58YG8Wfzd2CYvEz2WxCqKQ1NF9nJwWvUvK8xvzjBGV gF3Q== X-Forwarded-Encrypted: i=1; AJvYcCX3sVyRVmBV0CI1Jt43Q/Le1AbtxiZnqX4Ijl4DyjQ7K7Zyysv8f2+LK64PHYluJ/yaiZ4rLt/Scpc=@vger.kernel.org X-Gm-Message-State: AOJu0YyWYptSf5XDIvmTThvBpH7a5sbGfDha6Acj7C706vaF3nsAJ1e0 6XnWwbfuVBikKLdBwOttTtGKhJcX8ucjPQ+B+ImdgpiFgxNYPZaZo440Z2rA/o+mN9o= X-Gm-Gg: ASbGncsJmEP3s2DHrB8kNxtXm90D1iCfYV3Dv7YB2wzSGgWZqbsrV77jQzIi8IXnPXv 4DAp+K6+meuuD+LFSp8d67HwOQpEphK7VKKubjyNhBgt2RF8EX5GyeR2bvWaj4pDCEMVk/77dix gb9SJGUCDqDn7j/E0ZzRMZzLWBNoKEZdLrea8vZWctQIEhmdBS8UrHKDNOjBPVZZ4wfZzTbOsDf KwX3r43xVOceTQt56UKasrg6Q+BxuKbnNnyTlk3MKqHmSq23M4jdgVVePbbhXVHeXRZG73pN2Y2 GxUnopNoVvFcdSu1kyGGgUz9nbgMcYagm6xaWZr5x1LISSJ4LWLm+acobPrcAA== X-Google-Smtp-Source: AGHT+IHSSRQv6UaVUp1uY7YxcQ7Fp0Nkh/iVwU/C9R2BEes+S1JYlxj3SIRdl94BDLGOoJJBYixrLQ== X-Received: by 2002:a17:903:faf:b0:224:c46:d167 with SMTP id d9443c01a7336-231d44e7049mr42573705ad.16.1747408573939; Fri, 16 May 2025 08:16:13 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4ebb0d4sm15446025ad.195.2025.05.16.08.16.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 May 2025 08:16:13 -0700 (PDT) Date: Fri, 16 May 2025 08:16:09 -0700 From: Deepak Gupta To: Alexandre Ghiti Cc: 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 , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , 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 Subject: Re: [PATCH v15 22/27] riscv: enable kernel access to shadow stack memory via FWFT sbi call Message-ID: References: <20250502-v5_user_cfi_series-v15-0-914966471885@rivosinc.com> <20250502-v5_user_cfi_series-v15-22-914966471885@rivosinc.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Thu, May 15, 2025 at 09:10:08AM +0200, Alexandre Ghiti wrote: >Hi Deepak, > >On 03/05/2025 01:30, Deepak Gupta wrote: >>Kernel will have to perform shadow stack operations on user shadow stack. >>Like during signal delivery and sigreturn, shadow stack token must be >>created and validated respectively. Thus shadow stack access for kernel >>must be enabled. >> >>In future when kernel shadow stacks are enabled for linux kernel, it must >>be enabled as early as possible for better coverage and prevent imbalance >>between regular stack and shadow stack. After `relocate_enable_mmu` has >>been done, this is as early as possible it can enabled. >> >>Reviewed-by: Zong Li >>Signed-off-by: Deepak Gupta >>--- >> arch/riscv/kernel/asm-offsets.c | 4 ++++ >> arch/riscv/kernel/head.S | 27 +++++++++++++++++++++++++++ >> 2 files changed, 31 insertions(+) >> >>diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c >>index f33945432f8f..7ab41f01aa17 100644 >>--- a/arch/riscv/kernel/asm-offsets.c >>+++ b/arch/riscv/kernel/asm-offsets.c >>@@ -514,4 +514,8 @@ void asm_offsets(void) >> DEFINE(FREGS_A6, offsetof(struct __arch_ftrace_regs, a6)); >> DEFINE(FREGS_A7, offsetof(struct __arch_ftrace_regs, a7)); >> #endif >>+ DEFINE(SBI_EXT_FWFT, SBI_EXT_FWFT); >>+ DEFINE(SBI_EXT_FWFT_SET, SBI_EXT_FWFT_SET); >>+ DEFINE(SBI_FWFT_SHADOW_STACK, SBI_FWFT_SHADOW_STACK); >>+ DEFINE(SBI_FWFT_SET_FLAG_LOCK, SBI_FWFT_SET_FLAG_LOCK); > > >kernel test robot reported errors when !RV64 and !SBI, the following >diff fixes it: > >diff --git a/arch/riscv/kernel/asm-offsets.c >b/arch/riscv/kernel/asm-offsets.c >index 7fc085d27ca79..3aa5f56a84e9a 100644 >--- a/arch/riscv/kernel/asm-offsets.c >+++ b/arch/riscv/kernel/asm-offsets.c >@@ -532,8 +532,10 @@ void asm_offsets(void) >        DEFINE(FREGS_A6,            offsetof(struct >__arch_ftrace_regs, a6)); >        DEFINE(FREGS_A7,            offsetof(struct >__arch_ftrace_regs, a7)); > #endif >+#ifdef CONFIG_RISCV_SBI >        DEFINE(SBI_EXT_FWFT, SBI_EXT_FWFT); >        DEFINE(SBI_EXT_FWFT_SET, SBI_EXT_FWFT_SET); >        DEFINE(SBI_FWFT_SHADOW_STACK, SBI_FWFT_SHADOW_STACK); >        DEFINE(SBI_FWFT_SET_FLAG_LOCK, SBI_FWFT_SET_FLAG_LOCK); >+#endif > } > >No need to resend the whole series, I'll squash it. Thanks. > >Thanks, > >Alex > > >> } >>diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S >>index 356d5397b2a2..7eae9a172351 100644 >>--- a/arch/riscv/kernel/head.S >>+++ b/arch/riscv/kernel/head.S >>@@ -15,6 +15,7 @@ >> #include >> #include >> #include >>+#include >> #include "efi-header.S" >> __HEAD >>@@ -164,6 +165,19 @@ secondary_start_sbi: >> call relocate_enable_mmu >> #endif >> call .Lsetup_trap_vector >>+#if defined(CONFIG_RISCV_SBI) && defined(CONFIG_RISCV_USER_CFI) >>+ li a7, SBI_EXT_FWFT >>+ li a6, SBI_EXT_FWFT_SET >>+ li a0, SBI_FWFT_SHADOW_STACK >>+ li a1, 1 /* enable supervisor to access shadow stack access */ >>+ li a2, SBI_FWFT_SET_FLAG_LOCK >>+ ecall >>+ beqz a0, 1f >>+ la a1, riscv_nousercfi >>+ li a0, CMDLINE_DISABLE_RISCV_USERCFI_BCFI >>+ REG_S a0, (a1) >>+1: >>+#endif >> scs_load_current >> call smp_callin >> #endif /* CONFIG_SMP */ >>@@ -320,6 +334,19 @@ SYM_CODE_START(_start_kernel) >> la tp, init_task >> la sp, init_thread_union + THREAD_SIZE >> addi sp, sp, -PT_SIZE_ON_STACK >>+#if defined(CONFIG_RISCV_SBI) && defined(CONFIG_RISCV_USER_CFI) >>+ li a7, SBI_EXT_FWFT >>+ li a6, SBI_EXT_FWFT_SET >>+ li a0, SBI_FWFT_SHADOW_STACK >>+ li a1, 1 /* enable supervisor to access shadow stack access */ >>+ li a2, SBI_FWFT_SET_FLAG_LOCK >>+ ecall >>+ beqz a0, 1f >>+ la a1, riscv_nousercfi >>+ li a0, CMDLINE_DISABLE_RISCV_USERCFI_BCFI >>+ REG_S a0, (a1) >>+1: >>+#endif >> scs_load_current >> #ifdef CONFIG_KASAN >>