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 327CAC369AB for ; Thu, 24 Apr 2025 07:21:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5996D6B0088; Thu, 24 Apr 2025 03:21:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 520C86B0089; Thu, 24 Apr 2025 03:21:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34C3B6B008A; Thu, 24 Apr 2025 03:21:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0A0C66B0088 for ; Thu, 24 Apr 2025 03:21:17 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id ACC311A0B32 for ; Thu, 24 Apr 2025 07:21:18 +0000 (UTC) X-FDA: 83368091436.22.786CE45 Received: from mail-ua1-f51.google.com (mail-ua1-f51.google.com [209.85.222.51]) by imf21.hostedemail.com (Postfix) with ESMTP id BE94E1C0007 for ; Thu, 24 Apr 2025 07:21:16 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=UtwyRJIK; dmarc=none; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.222.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745479276; a=rsa-sha256; cv=none; b=MIJfcy9e6N6o2cJM9K78i7Je3cmxRed//qJavagF8Z6GByFElOIJ1opF3pubJsvPNBMSG4 yUNbU3zOTmRRoUFtIxGrCKsIKeDKeu+RKrRNfN+IaPy2PibqXr0e6+/9LjZMLimKdk26T5 lCNXsPD71vUrgjdz0G5OrfuONEtlJug= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=UtwyRJIK; dmarc=none; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.222.51 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=1745479276; 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=uTOlWSgIfNg/m3Oqt+BmGM6XFRmcXruZeuBu1MNVaGQ=; b=IbNLH6a91/I151e2121T3YG6ptLzlA58EDeOkeFc8/NIKMbdQUITkUecP1+5lGjDo3OiPH v99GJN7zWwE2UqdQzIb5XNUY2roRtU5/JLe25gP0nBMFz+e7FWWfeK+nKDd4rZJ9axlDPk xBZaGVz+blGMUnGFpp5Ai7OTLBemIGg= Received: by mail-ua1-f51.google.com with SMTP id a1e0cc1a2514c-875b8807011so787036241.0 for ; Thu, 24 Apr 2025 00:21:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1745479276; x=1746084076; 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=uTOlWSgIfNg/m3Oqt+BmGM6XFRmcXruZeuBu1MNVaGQ=; b=UtwyRJIKjmVQ7fKsC0toyinbbTbVpeNUfIh9QTVJ8VHoDDR1z37rR8A1+kHuHX0H1L AB+ICTPf4DKOuWe1Y3m2KarQVaJ38XIP+PGooxK8haQoM3I9w0aeXOaC5lg+rE2VKMJV 20x/nJ2OM4K6tYSHvC7dJAjo1zp639HdGwdpDphhLst+gIXd+FziURtDwc8nejIe7CYq WJpyAA/g4/2/KEMjX8s2iIsHx4W8ntBNgEGa+1fLs9APYR+9lnJwvUL5UrKGtZEQFXf3 E5H2GRAcNEQMLPzzdk+WafyFDQt/uW1UJ+gcDxxuxuuqy7apIrheKrnWegAiUCpSqTFB S76Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745479276; x=1746084076; 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=uTOlWSgIfNg/m3Oqt+BmGM6XFRmcXruZeuBu1MNVaGQ=; b=eWg+FXATs4mO+qWPWj57WDLHbCdL5+3WzLKXO7/hFxvJyn58RY4XRmClP4htKMQ6H7 cNQmY2wLHdQXCsROToITWsSnWzR27pAjEPWs5oLnD/ovR/iEHZD/OTXbfZmb2ejEuTVe rQa8/fdnINJuWUm2PN5TXNfg8ieJTOZnsHoBI2eKfaHRrOp/jDFFF47pKnLgPOcGCt2r v3wHelAYeaBn2muy76xIuF4M/o1gNexyPh/3/oL4AIG0keyNjV8ZG9yHcURsKoAElBbC uUxK42PQQn1jm3c4+8cWWNEcKfWEnUz2831oREyg1CP2tIZFfA9pIpaKjndd2IoQHd4U Y7hA== X-Forwarded-Encrypted: i=1; AJvYcCVIOA1wgCTiggNQFzFivdfOyvVzyD1BoYmVDe/9txyuCn6jAWYm3WWmWaq2KDYjCortg9qyP2m2pQ==@kvack.org X-Gm-Message-State: AOJu0Yx1QE0yV5I4Skr3dCwJNUxMDr39U4kTpxSSdcy97COaIFcAoZaI l8RK4lUGUWY7Zg+HMWOcbaQrkfUYGYCNjRw71RoMfE8y3q2BU+q1dSMZsXL7qdc= X-Gm-Gg: ASbGnctBqgaDEne23xWlJr25Q3Jom9eqRHp20EeBnOYKtAhCTs25Ibt3LqRjMIY6PdN 3MDE+hjgFSpC7NKw3uMJL4keR3ZOZE5cNCGQPOGBOZLQAXJ0B4YkW6D0Q/xltMG5wI8gQIRB94p zvHLoV0V43YhqnIIAH8MyKaLzyqP3g2tzKShaoZgFD6InIgzK7OicHstXp3nyywTX2iebe96WJU 4yR1oSxbTWQcMjE1ZVdv2nCa5r30P1v78cQ0a6sOBdqtvQdq2EHq3hVux+aTlU7pjJQyCUNSeyz a2/TpPSYN/U9XQ7jDap9sqtbtDRJiyQl6szZL2reCycdYPiM3+E= X-Google-Smtp-Source: AGHT+IG6rfYKWcrpqCiMIv7u+v5lHTdXwUfRT7tn0rH3sRxk3D1hWLROMKGb0/WJjtbUj6Z//dN74Q== X-Received: by 2002:a17:903:2290:b0:215:ba2b:cd55 with SMTP id d9443c01a7336-22db47ad1c7mr19626865ad.2.1745479264519; Thu, 24 Apr 2025 00:21:04 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db52163d6sm6240765ad.214.2025.04.24.00.21.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Apr 2025 00:21:04 -0700 (PDT) From: Deepak Gupta Date: Thu, 24 Apr 2025 00:20:28 -0700 Subject: [PATCH v13 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: <20250424-v5_user_cfi_series-v13-13-971437de586a@rivosinc.com> References: <20250424-v5_user_cfi_series-v13-0-971437de586a@rivosinc.com> In-Reply-To: <20250424-v5_user_cfi_series-v13-0-971437de586a@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-Queue-Id: BE94E1C0007 X-Rspamd-Server: rspam04 X-Stat-Signature: tsxryiswiyoqpssx4qkswg31izmo5jsb X-HE-Tag: 1745479276-709605 X-HE-Meta: U2FsdGVkX196Tw14W9Bo61k3HiyI0Jo8n6BPbn55KZuTHcem8fv3V4c+ZjoISJFmmhVA8cjvtXzMkkMdF1rAdDW465oQK40m6lkYs3A61k2SlIiFqJu3haQpp4m2SOBchdrWdXBQHMSV9QZ5xObzv9QOFMcqJvRy0JHmhOm1ois0kjBzYr6ZKhoq3EobC1NM/2mzxWXXuwozM+JBBz45HWB09e4z3948kUtKSQRK8XEfSiMdH7r4ED+x18vfonh5vPSeCRxCCApzx4dsrhFOYut+PaLTeF2Cs3ZTMRKD/LOJsAHUW9ShDPwqB8Pd690vpEFEN15lgFXxVRdAj2bDkgSfth3Bzx7Dz6VRkloUY4i+NO6MDegZrV1TIDW3L6cvO1Yzt/WUqhKBe86ho1oY0+LZ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ntcWGNJ4eLV0+Hmiuyf0UwB3GqH7XFJ3ond0kZ06iY/kPHdTfQea 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 6a0a8f1c7c90..fb0c394430c6 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 5c6080680cb2..6cd90460cbad 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -353,4 +353,31 @@ struct prctl_mm_map { */ #define PR_LOCK_SHADOW_STACK_STATUS 76 +/* + * 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 77 + +/* + * 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 78 +# 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 79 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index cb366ff8703a..f347f3518d0b 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2336,6 +2336,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 @@ -2811,6 +2826,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, return -EINVAL; error = arch_lock_shadow_stack_status(me, 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