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 D2A91C3ABA5 for ; Wed, 30 Apr 2025 00:25:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04F856B00BD; Tue, 29 Apr 2025 20:25:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F19D86B00BC; Tue, 29 Apr 2025 20:25:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D441B6B00BD; Tue, 29 Apr 2025 20:25:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id AD1406B00BA for ; Tue, 29 Apr 2025 20:25:14 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 112CC12154D for ; Wed, 30 Apr 2025 00:17:36 +0000 (UTC) X-FDA: 83388796512.18.01761D6 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf28.hostedemail.com (Postfix) with ESMTP id 12907C0008 for ; Wed, 30 Apr 2025 00:17:33 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LyIREvi+; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.176 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=1745972254; 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=aI596RAmm7Lv4XL+SxRun/ZHSsQZoWKcT7qQq9RSlTc=; b=H3tLyIHojQJASnqNPkUlPMwrSPc43Y1a3ADNxRLbBJ25jDvjIHRm/ZoNW7gwNFIR9JoCBv giE0paMKX0iX+YbSDgyKKaA08y8hIOmHRUmX2w2oBEmBwYjtDTSA/oAE6JCNJrB3YGDTc7 wFgEz1M33d6cux/npTIgVxvTUJTo2hE= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LyIREvi+; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745972254; a=rsa-sha256; cv=none; b=oPQZjipwfySHaMu/iTBj+0RqFtHpxmzE5nzVDxyiw64jK+mAkDS7GP8n8d0+qmMeN9+9Br JqlbAcN+7ddlPcr2Xu3C/oQTmhOxZ5kMRbiruViQ/JjFTuzRcCiKeSWx26v188z8rpWBhF 0l2e/M58hA/tZvFfHJXGEzZVj8BfzTw= Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-227d6b530d8so73988025ad.3 for ; Tue, 29 Apr 2025 17:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1745972253; x=1746577053; 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=aI596RAmm7Lv4XL+SxRun/ZHSsQZoWKcT7qQq9RSlTc=; b=LyIREvi+I9648Oj1C7Kampq85gXyRC0qf6PNHWBx5qtEiCme7gt9IPZbTot3tqLh7z NT4R8T6rDAro9vw2r/dJtC0V++jNqUe0luPQmGCHjzzFCM9QrfZcIHtDQBEvJlQxybG2 +Ly9wR3wvqKeiGcBP49D8PBoP7ZLyRcieRSRptBpVkbTUbtjyFWz9ojd3FceHWqBcAFp pCAIXMhN0esoV9hk49Cw10698uE5a5SSV2fn5OiE7LFR+e72l11TsWdj/Bil7CQDm7lY ftkxaKE0eilzJ/bKfJsp6Ps97dybq5r3cTUHZX3RaHBNNy5apiSLklBviISENmz0HjYa Tb6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745972253; x=1746577053; 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=aI596RAmm7Lv4XL+SxRun/ZHSsQZoWKcT7qQq9RSlTc=; b=TCUFZrNsoyDPiIUZcAa6/d/G1KpPtaPDfWFDHKoJSl7RWy641qwJfbJhmS6qbeidea 312P3JY2Vvb4TempTsK0Hslt5oTJAnOtHlfBK/08kqeT3ucrjnMET8wemtoq3F+D/nn+ jOD+kJVl9HtBe7dymsDwZC4BJX281vrt2jB0iBc6PFFgTGVn0a1LyYUwnoxTLMbh0Zha DReieiNFad3QE/WFQIoMg7OVvTd3KU4kEOYfN/DaU5H25QlmYS2Y5SP5r6l/S/7pHb2n pvf2RBy/3g7HpBikOK2xgKB0MHLtuE07+5ubzmvYDWRdToPxzXyoF03ZZLrbKTxESweS behA== X-Forwarded-Encrypted: i=1; AJvYcCUNOCrLkI0qPJUwnqaJwsl4m0k6q2MXoEs7MvLoGnRZWTvVe8sbIGZgNgGaXyx6+Vey4oA/PZeS7g==@kvack.org X-Gm-Message-State: AOJu0Yw0aeTxvezvFfrDaVsuc/RE18Md4qp/4j9Dd9i/cz1lHD5tg7r3 cBP66IYyfRIHNIUjsjxLURiHccevoHodHk2lcCOi7U9zQDEuqa5pHbA3Iy9b9Q4= X-Gm-Gg: ASbGncs8z1qh06IR5jJyT1nBVjRub+QNQ9GcQTXuf/AwPwu2/atgHKWStvXn4vIn/mh LkNvzi7RLLJNX2MHxdxhw9XNAy+Om3wX5OkW6oA7QVsvvsHpmg22WvcH/HZK5D15vh8/zaLTcmS m6OWQ/IXNqZHGG8+S/UO5gtzqenkrrX0whcA/R6s14lHYytpdNFI3N2VZRsxil3Uqfi2pVeKZGm l8ZG2WmJc5AALcTRcH1nYEd2Rf8mGytYU6IFn45mMiec9I4qPnNNy0a/tLGINpyInbk/+KrsGD4 gNvhmjrGx5yP4IkvgihHjr+e3XhqqVl+7enfEy7/PQR7SjzTGowp0GChOmhQ2w== X-Google-Smtp-Source: AGHT+IE1ImDWArA4vAOMZ3tPvYABZbcFyPq43rxtUjG5ptU/GyltCdFbvr8IQ/OkfIGhaCQBtfBIRg== X-Received: by 2002:a17:902:d2cb:b0:224:fa0:36d2 with SMTP id d9443c01a7336-22df350917emr17573875ad.26.1745972252926; Tue, 29 Apr 2025 17:17:32 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db4d770d6sm109386035ad.17.2025.04.29.17.17.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 17:17:32 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Apr 2025 17:16:38 -0700 Subject: [PATCH v14 21/27] riscv: kernel command line option to opt out of user cfi MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250429-v5_user_cfi_series-v14-21-5239410d012a@rivosinc.com> References: <20250429-v5_user_cfi_series-v14-0-5239410d012a@rivosinc.com> In-Reply-To: <20250429-v5_user_cfi_series-v14-0-5239410d012a@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, Deepak Gupta X-Mailer: b4 0.13.0 X-Stat-Signature: gdry5imwnin1oje683mhk4uqsqfisqcd X-Rspamd-Queue-Id: 12907C0008 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1745972253-404281 X-HE-Meta: U2FsdGVkX18SRqoFTu9ybVSQeM7+jWuwUzJ9Z/PlpjfKxymcrZiB/GKpLiZV3nL7Q1N86cQ0yn0d6QyqhOwTIqT/X6HYaxcBZWBYG7vx81RENvPfcGsC20bCQZFv+jil16DeoLnJX0yBh2zl59Bx5fmXW3NJ7Asf0aymWDmsn4RhDNMhVWdx55DtW82BXO1GmGkCAIO9o0Ra/LmFgg9WM9E6mDiDbZay+El+De4/HzyvC+mMVWlLDAMLo3mnuN0lvq2UTQP1hCkulKn8h+dLxD2X31P7JFpy1WKwRQRwHJHdFKfgE4qyo/vl+Ql4ydH1KsikiAOVFMIEiNjC8xaTbXgrRcZyb50kUxifJiDWbqAyEMowZri0e3F8znwpNRGplRArpfDtG86ea4NQgKWa04w8t0x1ZBl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linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This commit adds a kernel command line option using which user cfi can be disabled. User backward cfi and forward cfi can be enabled independently. Kernel command line parameter "riscv_nousercfi" can take below values: - "all" : Disable forward and backward cfi both. - "bcfi" : Disable backward cfi. - "fcfi" : Disable forward cfi Signed-off-by: Deepak Gupta --- Documentation/admin-guide/kernel-parameters.txt | 8 ++++ arch/riscv/include/asm/usercfi.h | 7 +++ arch/riscv/kernel/usercfi.c | 59 ++++++++++++++++++++----- 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 76e538c77e31..f75d50420a56 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -6237,6 +6237,14 @@ replacement properties are not found. See the Kconfig entry for RISCV_ISA_FALLBACK. + riscv_nousercfi= + all Disable user cfi ABI to userspace even if cpu extension + are available. + bcfi Disable user backward cfi ABI to userspace even if + shadow stack extension is available. + fcfi Disable user forward cfi ABI to userspace even if landing + pad extension is available. + ro [KNL] Mount root device read-only on boot rodata= [KNL,EARLY] diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 68da5b7b79fd..6867ba6bd5a5 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -5,6 +5,10 @@ #ifndef _ASM_RISCV_USERCFI_H #define _ASM_RISCV_USERCFI_H +#define CMDLINE_DISABLE_RISCV_USERCFI_FCFI 1 +#define CMDLINE_DISABLE_RISCV_USERCFI_BCFI 2 +#define CMDLINE_DISABLE_RISCV_USERCFI 3 + #ifndef __ASSEMBLY__ #include #include @@ -83,6 +87,9 @@ void set_indir_lp_lock(struct task_struct *task); #endif /* CONFIG_RISCV_USER_CFI */ +bool is_user_shstk_enabled(void); +bool is_user_lpad_enabled(void); + #endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_USERCFI_H */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 8bc3e1e3f712..5ef357f43ad7 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -17,6 +17,8 @@ #include #include +unsigned int riscv_nousercfi; + #define SHSTK_ENTRY_SIZE sizeof(void *) bool is_shstk_enabled(struct task_struct *task) @@ -59,7 +61,7 @@ unsigned long get_active_shstk(struct task_struct *task) void set_shstk_status(struct task_struct *task, bool enable) { - if (!cpu_supports_shadow_stack()) + if (!is_user_shstk_enabled()) return; task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0; @@ -89,7 +91,7 @@ bool is_indir_lp_locked(struct task_struct *task) void set_indir_lp_status(struct task_struct *task, bool enable) { - if (!cpu_supports_indirect_br_lp_instr()) + if (!is_user_lpad_enabled()) return; task->thread_info.user_cfi_state.ufcfi_en = enable ? 1 : 0; @@ -259,7 +261,7 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi bool set_tok = flags & SHADOW_STACK_SET_TOKEN; unsigned long aligned_size = 0; - if (!cpu_supports_shadow_stack()) + if (!is_user_shstk_enabled()) return -EOPNOTSUPP; /* Anything other than set token should result in invalid param */ @@ -306,7 +308,7 @@ unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long addr, size; /* If shadow stack is not supported, return 0 */ - if (!cpu_supports_shadow_stack()) + if (!is_user_shstk_enabled()) return 0; /* @@ -352,7 +354,7 @@ void shstk_release(struct task_struct *tsk) { unsigned long base = 0, size = 0; /* If shadow stack is not supported or not enabled, nothing to release */ - if (!cpu_supports_shadow_stack() || !is_shstk_enabled(tsk)) + if (!is_user_shstk_enabled() || !is_shstk_enabled(tsk)) return; /* @@ -381,7 +383,7 @@ int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *st { unsigned long bcfi_status = 0; - if (!cpu_supports_shadow_stack()) + if (!is_user_shstk_enabled()) return -EINVAL; /* this means shadow stack is enabled on the task */ @@ -395,7 +397,7 @@ int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) unsigned long size = 0, addr = 0; bool enable_shstk = false; - if (!cpu_supports_shadow_stack()) + if (!is_user_shstk_enabled()) return -EINVAL; /* Reject unknown flags */ @@ -448,7 +450,7 @@ int arch_lock_shadow_stack_status(struct task_struct *task, unsigned long arg) { /* If shtstk not supported or not enabled on task, nothing to lock here */ - if (!cpu_supports_shadow_stack() || + if (!is_user_shstk_enabled() || !is_shstk_enabled(task) || arg != 0) return -EINVAL; @@ -461,7 +463,7 @@ int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *sta { unsigned long fcfi_status = 0; - if (!cpu_supports_indirect_br_lp_instr()) + if (!is_user_lpad_enabled()) return -EINVAL; /* indirect branch tracking is enabled on the task or not */ @@ -474,7 +476,7 @@ int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) { bool enable_indir_lp = false; - if (!cpu_supports_indirect_br_lp_instr()) + if (!is_user_lpad_enabled()) return -EINVAL; /* indirect branch tracking is locked and further can't be modified by user */ @@ -498,7 +500,7 @@ int arch_lock_indir_br_lp_status(struct task_struct *task, * If indirect branch tracking is not supported or not enabled on task, * nothing to lock here */ - if (!cpu_supports_indirect_br_lp_instr() || + if (!is_user_lpad_enabled() || !is_indir_lp_enabled(task) || arg != 0) return -EINVAL; @@ -506,3 +508,38 @@ int arch_lock_indir_br_lp_status(struct task_struct *task, return 0; } + +bool is_user_shstk_enabled(void) +{ + return (cpu_supports_shadow_stack() && + !(riscv_nousercfi & CMDLINE_DISABLE_RISCV_USERCFI_BCFI)); +} + +bool is_user_lpad_enabled(void) +{ + return (cpu_supports_indirect_br_lp_instr() && + !(riscv_nousercfi & CMDLINE_DISABLE_RISCV_USERCFI_FCFI)); +} + +static int __init setup_global_riscv_enable(char *str) +{ + if (strcmp(str, "all") == 0) + riscv_nousercfi = CMDLINE_DISABLE_RISCV_USERCFI; + + if (strcmp(str, "fcfi") == 0) + riscv_nousercfi |= CMDLINE_DISABLE_RISCV_USERCFI_FCFI; + + if (strcmp(str, "bcfi") == 0) + riscv_nousercfi |= CMDLINE_DISABLE_RISCV_USERCFI_BCFI; + + if (riscv_nousercfi) + pr_info("riscv user cfi disabled via cmdline" + "shadow stack status : %s, landing pad status : %s\n", + (riscv_nousercfi & CMDLINE_DISABLE_RISCV_USERCFI_BCFI) ? "disabled" : + "enabled", (riscv_nousercfi & CMDLINE_DISABLE_RISCV_USERCFI_FCFI) ? + "disabled" : "enabled"); + + return 1; +} + +__setup("riscv_nousercfi=", setup_global_riscv_enable); -- 2.43.0