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 60BF9C369DC for ; Wed, 30 Apr 2025 00:26:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D3AB6B008A; Tue, 29 Apr 2025 20:26:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 880C76B0092; Tue, 29 Apr 2025 20:26:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D4566B0096; Tue, 29 Apr 2025 20:26:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 438446B008A for ; Tue, 29 Apr 2025 20:26:28 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 601885F8EA for ; Wed, 30 Apr 2025 00:17:10 +0000 (UTC) X-FDA: 83388795420.28.3344086 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf04.hostedemail.com (Postfix) with ESMTP id 7737D40006 for ; Wed, 30 Apr 2025 00:17:08 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=wPnBUrDB; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 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=1745972228; 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=Wqa+EnEmqD/WcxsFXdACBzGsJGhuUJWdKCVGNJwyc08=; b=wZ7mN+jL/drxU5EUVoD/hBMNBLZ/+wwvch1RDLcp41NrA8PESvlHUvgKAl3jr3F75d472V cgMt5VtIuDGu9a/99/O9lqgmvpXsKNnXShU9N3HXPUHbXLczCavvfaTMDz0xWmyXfJ2v3S kc4lH2p0mZiyoEA+RGp8xS9Enl1WbOY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=wPnBUrDB; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745972228; a=rsa-sha256; cv=none; b=IwHvLzrl/MlxQn/IV8O9pRCN41sTrX8FPVGKs0aAHymT9m77y7ocfcbqK8PgbbzctHmrz0 bn06P8eqF0dolYHr2d709N6yJ1/5BG4GvHqWoBuXzo6XfBndQ+HsUmhLGxXtbSs5PSdtQr j7/y1fIkkKLCVxBYQX1bEZHRtfSZR2w= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-22438c356c8so73203825ad.1 for ; Tue, 29 Apr 2025 17:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1745972227; x=1746577027; 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=Wqa+EnEmqD/WcxsFXdACBzGsJGhuUJWdKCVGNJwyc08=; b=wPnBUrDB1kTAtRpnByxXAdWJds36CWJfxQ95gVjb2psGNvBE+Medv8pUqorgbO/oKv LUOEQyF0jdJxdVFXAk1dglqbafrdZcrBNf9LrCWIh12G5M8XdUz7gFSkWYhq5/elVY1f PPyk8sh877KDW7x7v+gwjFPOl6NVH86aEaQc3eCoZO0n5hKm7trcbU3MZxhAhfqaCOvo Xd//27Yarbn9aWuwsRipKivumtdycDzrLHc5r6OHmbkwH6D8MKiEkOPdGqGYcbEAOyi6 7AP+Ulhd/uTmm51X5lX9S+EqgkGB6Hxt3/HsMm7bAh1hGZ4+JSLhFDNzozQWnz/y7CSy +aUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745972227; x=1746577027; 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=Wqa+EnEmqD/WcxsFXdACBzGsJGhuUJWdKCVGNJwyc08=; b=X+MIrUFalsWEo6TFoCkcOlZ/Y08Xl5CzboFEOPXx76BeIB9AsFqO02yauiVuV/zPFx db1oZ1z+DJZRi1vQR1cc/4ELZOsxnB1DItg+nq28v5ylO4lU7NGF+EkFVGBEfSLRHQQr uz8G/vNPWzxu/DxAqrQrD3Gl2mdgiQXeFEepXWMIT5NKv1YmX2KYCKdHflDuwdTMhWny hPr0XJyIkbBC0A7TsnfInu6SkqEFmn17s7wBq18uh8kXTX8DcEqkk/Hl8FcOTveUUyye J/H8QNGgOk1sCABMTAaLnBhZ2i1ugQCRF/CWk5Tr/6WwNNvvQGSwWlwQo4yWPFDFDdfe y+zA== X-Forwarded-Encrypted: i=1; AJvYcCX+xJxMRtn1f0oDxZcpIMbl2xCtLuDvGGLZD+mk+e988tohcBkPt4AIrRbRB/+aqU0pQ/JevSuFSA==@kvack.org X-Gm-Message-State: AOJu0Yz7hgOBQpyla11SpjSxde2spbEwRGq414OQkcUEBCVYf61cp6Lg tc4abtV9Nzpg7FpOLoFd6qu0yV3s1+WnKQMrYBsrmCaNxpk/+Ed8brZTvsw2TfM= X-Gm-Gg: ASbGncuLAQPLYCoPGbo1JxoymFlljXAl8eDM67eRz2TLrUaiBqjja1wMu86E/ig2Rdi /DwBU/mgyZU2B7kxXP9509UnpwTi21T4tApyNlJvUCe1k85KHPFvm1vMxURohNubTSSTgIscLzw jzhivSU9QAt4S+0JaPZatffsX0jrm6dXeo72NjHiKm+jBW82bBZFpiaVQIHD0+UFTHjoWquNUs5 P2/JhmuOZZu1IBjARxDyLi1FMltmcQCMGHClKNw6xRRh5QQTpgQnL1epRFSRUNnzjM/9V6ExGVh 8/kGiAOo9h0JDcFIoUexilwq24spygVw6zEO+D+APbFGdXYy+VI= X-Google-Smtp-Source: AGHT+IHQiHW6ihtjUTT0DTqmkuWcAmVo/rllEq5v8YlabaxyEntdh+cJYjoV0mkSuSPow8vsrYIpLg== X-Received: by 2002:a17:902:d4d1:b0:223:669f:ca2d with SMTP id d9443c01a7336-22df356d961mr20640625ad.35.1745972227361; Tue, 29 Apr 2025 17:17:07 -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.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 17:17:07 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Apr 2025 17:16:30 -0700 Subject: [PATCH v14 13/27] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250429-v5_user_cfi_series-v14-13-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, Zong Li , Deepak Gupta X-Mailer: b4 0.13.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7737D40006 X-Stat-Signature: 8sjpgqha1yun9wwbwsnmpje649kdaj39 X-HE-Tag: 1745972228-579586 X-HE-Meta: U2FsdGVkX1+gObvvqiDYT/fTKt/NjYN0XSaZUmto+knwh34iyPFMKWm5cyXBtX1zwc1Sk4K5J68Z7rUwJlBhV46YXpBygm1rZoolQrbT/KlLya7k/pPPKvQFXzxEHEk/SAwyWVdKxU/yiYOgUePTGieYJu29iQ7O29O+RBJz8iCohMd5ly7fXUw8P47HU1e6iSe86a5MgwqxIaNR5ZR+sZJSeWbqsDRqP0nKA3JFLtIguo5B2z3mhmDzeeJdopJX+X7owXFjAywu7xlJbEqFybGZ+vVtRChwiQhZl2ixUz50vXBpkSUZbSVhLk9qCSOMAWLxqyI+/l3TXnvGmzOftYtbswzU5JIMT4Z2UiQMIIM1FJxCeICDZSHJrT2MNTmOB42T4OccuC5a2vsd2P02OAYlha8HQYfODuLbpp59rlhUN7Xt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PEYuODqMZ+upOT6iXoUff5JjrbQj+k 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: Three architectures (x86, aarch64, riscv) have support for indirect branch tracking feature in a very similar fashion. On a very high level, indirect branch tracking is a CPU feature where CPU tracks branches which uses memory operand to perform control transfer in program. As part of this tracking on indirect branches, CPU goes in a state where it expects a landing pad instr on target and if not found then CPU raises some fault (architecture dependent) x86 landing pad instr - `ENDBRANCH` arch64 landing pad instr - `BTI` riscv landing instr - `lpad` Given that three major arches have support for indirect branch tracking, This patch makes `prctl` for indirect branch tracking arch agnostic. To allow userspace to enable this feature for itself, following prtcls are defined: - PR_GET_INDIR_BR_LP_STATUS: Gets current configured status for indirect branch tracking. - PR_SET_INDIR_BR_LP_STATUS: Sets a configuration for indirect branch tracking. Following status options are allowed - PR_INDIR_BR_LP_ENABLE: Enables indirect branch tracking on user thread. - PR_INDIR_BR_LP_DISABLE; Disables indirect branch tracking on user thread. - PR_LOCK_INDIR_BR_LP_STATUS: Locks configured status for indirect branch tracking for user thread. Reviewed-by: Mark Brown Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- include/linux/cpu.h | 4 ++++ include/uapi/linux/prctl.h | 27 +++++++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/include/linux/cpu.h b/include/linux/cpu.h index e3049543008b..ea8b351b5bc5 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -204,4 +204,8 @@ static inline bool cpu_mitigations_auto_nosmt(void) } #endif +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status); +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status); +int arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status); + #endif /* _LINUX_CPU_H_ */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 15c18ef4eb11..2e09b19317a3 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -364,4 +364,31 @@ struct prctl_mm_map { # define PR_TIMER_CREATE_RESTORE_IDS_ON 1 # define PR_TIMER_CREATE_RESTORE_IDS_GET 2 +/* + * Get the current indirect branch tracking configuration for the current + * thread, this will be the value configured via PR_SET_INDIR_BR_LP_STATUS. + */ +#define PR_GET_INDIR_BR_LP_STATUS 78 + +/* + * Set the indirect branch tracking configuration. PR_INDIR_BR_LP_ENABLE will + * enable cpu feature for user thread, to track all indirect branches and ensure + * they land on arch defined landing pad instruction. + * x86 - If enabled, an indirect branch must land on `ENDBRANCH` instruction. + * arch64 - If enabled, an indirect branch must land on `BTI` instruction. + * riscv - If enabled, an indirect branch must land on `lpad` instruction. + * PR_INDIR_BR_LP_DISABLE will disable feature for user thread and indirect + * branches will no more be tracked by cpu to land on arch defined landing pad + * instruction. + */ +#define PR_SET_INDIR_BR_LP_STATUS 79 +# define PR_INDIR_BR_LP_ENABLE (1UL << 0) + +/* + * Prevent further changes to the specified indirect branch tracking + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_INDIR_BR_LP_STATUS 80 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index c434968e9f5d..91a1dc093c2a 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2340,6 +2340,21 @@ int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long st return -EINVAL; } +int __weak arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2820,6 +2835,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, return -EINVAL; error = posixtimer_create_prctl(arg2); break; + case PR_GET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_indir_br_lp_status(me, (unsigned long __user *)arg2); + break; + case PR_SET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_indir_br_lp_status(me, arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_indir_br_lp_status(me, arg2); + break; default: trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5); error = -EINVAL; -- 2.43.0