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]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D5CF6CCF9E3 for ; Thu, 23 Oct 2025 13:26:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 08EAC8E002A; Thu, 23 Oct 2025 09:26:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0669C8E0002; Thu, 23 Oct 2025 09:26:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EBF038E002A; Thu, 23 Oct 2025 09:26:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D809E8E0002 for ; Thu, 23 Oct 2025 09:26:09 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A4F49BCF5B for ; Thu, 23 Oct 2025 13:26:09 +0000 (UTC) X-FDA: 84029452458.02.CA8D51D Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf06.hostedemail.com (Postfix) with ESMTP id A6DB1180003 for ; Thu, 23 Oct 2025 13:26:07 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc.com header.s=google header.b=W3W113FU; dmarc=pass (policy=none) header.from=rivosinc.com; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1761225967; a=rsa-sha256; cv=none; b=GCy3gCB8ZewP63YOwuanTdwX6R4vYl+CXvdVuzzh8BYMW/lUMKVxnNoib/GwsIZOahEupg BgHeie5AfGVVhwmxEmwuJKlnhEtwwBfFnU6goQGqPajN+4xKk1rG7V2oH3S5VdE+1XZ01n zhEsHf9Hi5ffAyBjfXD31Rf2Q0D3Lno= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc.com header.s=google header.b=W3W113FU; dmarc=pass (policy=none) header.from=rivosinc.com; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.178 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=1761225967; 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=ujHqL1CkFocnxfBg/TilHgsDv7qqfxACnquN+j6SpiE=; b=I8c7ACGGwEaPfGB1uNkoiiIt9KzMXSl7LOeFCfEX0KMlaaUsmzd/Zxf1KiqOz+gvkWAygz T0pV9MJz1TQlBUo617BmJQlAAZUcxt01HROFU/CNJ2NXl11P2x8+C6oUwGEWEOeP25xVvo hiOPG6xKL7WgA/jc5SKCm5bCNWjkg9U= Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-b6a225b7e9eso597841a12.0 for ; Thu, 23 Oct 2025 06:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc.com; s=google; t=1761225966; x=1761830766; 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=ujHqL1CkFocnxfBg/TilHgsDv7qqfxACnquN+j6SpiE=; b=W3W113FUzvsgIopw/XWFrjm5L75ddHZl0cxTtimhJglMRsHBhoVW0mol6mHCOgLjZH YjsgzcsYWcUlIVIe1ugY96EwVyfF7xBdToDw2HnWbXMxtiIuuzDARBLT+BNULQ8nn4i5 orvTym/nKiXkD+e2t7n3RdFKn6HlvkX8GlEDB2dzSuO/Jq65UYLype7OX1jenJqSmXPw zvGT+FsPavqQdeQLSg+QhvAdx2PFnTq15kZfgjLVOlVxx3c/FZF9heTl2Kx7kdngHzm6 Pff+ALNNqmf7zWM1jh6ZkEeZiNWCY2hJXauzkmSmiYbl6I6JOkYHUjr6xYmn3xWB66cj N/Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761225966; x=1761830766; 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=ujHqL1CkFocnxfBg/TilHgsDv7qqfxACnquN+j6SpiE=; b=bcRwJ6dyht3FNDF/ZVUzhtZzv4lKdSdk2JFuXlRYetrnsbAkDFEboe50HdgDED7xPi kSirQx2OOulUvNreQCbKITit8NxY+oyu6G1JB0fvOGl9qb3SxXkNpk/MTsbqUPzcgNP6 Oy5lW0gfasiWX+dOiV6wAt2UMj81njzUXQ3C4HSW5h6xkxS9DX4fJZfqgYzknGTlVHvV bFo2sppuJeBCvN9X0CL3axDUTSdmOpdfVDvANZZsyqGNRV7YS2IdW8TxAV6tQCl1HsHy N52eWA0/FeAt0O1+0LobBdlsDaWj7yK1Ij+VIZmNxOg2MWlzxs4JfEbdMG39Y3rsr+9K fNGQ== X-Forwarded-Encrypted: i=1; AJvYcCUmtjIgA97WpjJ37aJtZEw3tOU3Y8in4L7oUF/EpUsgW24bor8YLb5YrNIOo8XkJgl+lIAfAMMgbg==@kvack.org X-Gm-Message-State: AOJu0Yy2xFXyLMFwbgnXn2bvr6XD5r/t6ThcuXFcPIPBeCtgBrECN2Zu 3pzUOfY1rWgYECTLmnnw1OJbND/6HVHoJnYld2juteN+GXvk6XVCTgJrNbYIj7fOJDU= X-Gm-Gg: ASbGncvtiAjMCrL7pSzp1Czzb4UUyW97InfAcqgaAPaQG5vYETgMLk4ud9jPlJX7uiQ XuOw4lmp8scMu0M5NPLF41/qtAkkfVK5EWcZqsMURreQV1xN19mbfI0fqDCdZ5i2vUunLqnbEeQ vy3Rt4vZ5exCfzoDdE2JpiuMTKH0ctvaL6QZhMgHrYwoa5RZ557qX7rVfbVvp/88kFrApict2ez or14QIN/SgEgwmkFqTPOyoGbpbv70A+ZT51xUFOh+uwUwtOoXCaxXe2uRIQuv8ETVbpFgeBpCly UOKma6ku35YMPvNzBd5I353uj6NwTQeCL5I6qu71jHSjlhz0qLk/NRUS8bBavuclWvcOfGU4pm1 t0MvTtLgQLFNaT7nsyPRrbVPZfYznGrDwp42qry3dLHr7+SnsEw6SkT09QVZUIfJACIuh570NA8 Z3iT15LEKDWzCg2hgMi3C0 X-Google-Smtp-Source: AGHT+IH7wZQAxr0YOme4DHQ+xBYehhpRlEEEBIFgBLPknKrf0JZUFDL/k+KqLwmt33/SrcSR7qDNWg== X-Received: by 2002:a17:903:22c3:b0:290:c317:a34e with SMTP id d9443c01a7336-290c9cd4b8fmr334317665ad.25.1761225966369; Thu, 23 Oct 2025 06:26:06 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2946e23e4b3sm23432035ad.103.2025.10.23.06.26.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Oct 2025 06:26:05 -0700 (PDT) From: Deepak Gupta Date: Thu, 23 Oct 2025 06:25:42 -0700 Subject: [PATCH v22 13/28] 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: <20251023-v5_user_cfi_series-v22-13-1d53ce35d8fd@rivosinc.com> References: <20251023-v5_user_cfi_series-v22-0-1d53ce35d8fd@rivosinc.com> In-Reply-To: <20251023-v5_user_cfi_series-v22-0-1d53ce35d8fd@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?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Benno Lossin 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: rspam04 X-Rspamd-Queue-Id: A6DB1180003 X-Stat-Signature: 6xft9p7nxhxi7k1xe1nojqz4dkk7d471 X-HE-Tag: 1761225967-629944 X-HE-Meta: U2FsdGVkX1/IBnIbskn2CUZJIsHIYmNgj7AAGIk7ksF4WAMs+iKMDRkYd57RkS326Hy9XpeXNQ3WTSrzyVd41g8WhQdsdwY7XWpGdpu2Nu5qgcrbW/S8SbNtCZn+lz8ikClTqwQ0ZMVGNwD1E0YedQcwiYrHiA8ki/pvD8fJy61Qalt1LeTQJISejyPg1ymwkL4B4xpoCXQG/nr/K9F+N+rNfOXJaWKidbnZ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NLpijRktT419F6dd4hnT3Tp/nz58geCnmm2FdZNFpj9VXTMqcYPMtKGwEBPcqZj/wzNEv519RcyQWv9gZyL2KDno9EOGsMvTJ/Wh+Q4fvfs8lc4zTh/EbajBPFtL8C/XfaEYU3k13sEWCO0/zw/AcWmj50eqk1HNMJyz5uWwOJkjEobx3snGf8mXwOTqvs1wyMhDLMtPd3g== 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 487b3bf2e1ea..8239cd95a005 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -229,4 +229,8 @@ static inline bool cpu_attack_vector_mitigated(enum cpu_attack_vectors v) #define smt_mitigations SMT_MITIGATIONS_OFF #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 51c4e8c82b1e..9b4afdc85099 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -386,4 +386,31 @@ struct prctl_mm_map { # define PR_FUTEX_HASH_SET_SLOTS 1 # define PR_FUTEX_HASH_GET_SLOTS 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 79 + +/* + * 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 80 +# 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 81 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 8b58eece4e58..9071422c1609 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2388,6 +2388,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) static int prctl_set_vma(unsigned long opt, unsigned long addr, @@ -2868,6 +2883,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_FUTEX_HASH: error = futex_hash_prctl(arg2, arg3, arg4); 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