From patchwork Mon Nov 11 20:53:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871351 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 4EA52D3ABF4 for ; Mon, 11 Nov 2024 20:54:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 296386B00C7; Mon, 11 Nov 2024 15:54:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21B2E6B00C8; Mon, 11 Nov 2024 15:54:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEB2D6B00C9; Mon, 11 Nov 2024 15:54:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CF8646B00C7 for ; Mon, 11 Nov 2024 15:54:35 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 96FF51A06D0 for ; Mon, 11 Nov 2024 20:54:35 +0000 (UTC) X-FDA: 82775017332.29.479CECC Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by imf07.hostedemail.com (Postfix) with ESMTP id 152A440017 for ; Mon, 11 Nov 2024 20:53:35 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GIgMcsdY; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.45 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358411; a=rsa-sha256; cv=none; b=fK6OqQZn9PH/KfI65yTkTNMVND+7M6hrhrTwr97HXV5QclSpSVLPBErQol6sus/8s34Bk8 Tj1yHuRcDXlnBlevx66T1+EhZ1UAsIz+CxV6giIoZHKh2orBkmGMNntugH7fCk9LWfmRBM cuRgmUPbw4GHpYjKtJ5WtcET3m9Et/0= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GIgMcsdY; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.45 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=1731358411; 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=tXX3HktdfdUDYxlxepoMs9/t12X7ubBzoX+ktyscBn0=; b=kKWDFrbZBxxBJCvpl23/GdKvU+k3mfspkH9a0cB0zexkZWXrhtJAhDMdcgNZLdIB7lCV9W 5KG215hJYAmXOEEGTSHjDuTYPvYp5+u7zg1EE98eDm6QoPNEM7TgJvRXY4wAi8krlntQzP 5SOEToLLH5wyisE4uSr9Z/BOMBGhHFU= Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2e31af47681so3979518a91.2 for ; Mon, 11 Nov 2024 12:54:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358472; x=1731963272; 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=tXX3HktdfdUDYxlxepoMs9/t12X7ubBzoX+ktyscBn0=; b=GIgMcsdYm+rhuct+zOzoHDZqOQKoXjg7Hsa4sY+FQkYqkiHiiOOy33NJo7S+QVYZev tsKkEkx4L/QgOv4sVv+jbYZZqkLk4b5oIIRQgwEro7hQ+RTJ1hI4Hq005CR9+6jPTdgQ suFbcZEQVs29uwgW7SNGro6xeIlEXsrJmKzSBq9f0Q22R/ENB6UgSk/sEJ7soH7pQaUZ wookIBBPXcd6uKI52AkKESx9rFVDRdrL2qWU8Wz0Qvk9sxQq5gftJcpVlUdSOJbNXMEi bX17bxC6Ygjw4qXsNgkWd3rsuFvcI4svi2hDRft+bCmf6JXLHcvXvrdM1GViqq1kOn96 lCTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358472; x=1731963272; 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=tXX3HktdfdUDYxlxepoMs9/t12X7ubBzoX+ktyscBn0=; b=L8bwql4T3chE91Lvext8CkbTNebVMqHkE8hZYi/tqT/LzT5AHOyGn5Amy4R+UlDtIy ei8APLuB70z2Ikk0+rNZr0aJ1Rpq9ZiYBmMw8jhtBc4MGwIX//BVFWT7FSyOwC1ZhEn1 t379axf79fKMYZlKNl+XguxDOEyNoLCYYWdDDEyh9Zior+W4TT+Hbox4M585JC8DLvSz afkvyWzWpjdcTzAklfsNgobzFhwI7r2AFSfMcyuDwj7iwkdEb1GCwXGuBCiqUOtFJtVc nzgNzaRFWEidciTAFT6ViYcs/mZ7Y6KL8uDHga/LTipn0/sBFmYzG0RKFMtsMPl8QaDe NBgg== X-Forwarded-Encrypted: i=1; AJvYcCXnflNeAyxXqqcgonrtfPzeBLgmPpRTLgFjntK3LYOE4bs4G6B9pw5ZYZSySvH/nKDoKcoCIBrVUw==@kvack.org X-Gm-Message-State: AOJu0Yxt9AS8SfzPws49OGfonbbiithqRgqhTqNXvPTBzG8lISkzKNJH P6L0Itr4UGd6U5WRNRhBe//P1nTE7ejKcQWB6CwFtt5ZczbkB0a1Zc/soB8VlIQ= X-Google-Smtp-Source: AGHT+IHq5Z7Xi/84uV65vfYB2LFlDXOy+r9mzEGPAb0M8IJd9siANoKs1hUvfB42yyVVA4FysR6vOw== X-Received: by 2002:a17:90b:250f:b0:2e2:c421:894b with SMTP id 98e67ed59e1d1-2e9b1682496mr17398074a91.14.1731358472327; Mon, 11 Nov 2024 12:54:32 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:32 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:59 -0800 Subject: [PATCH v8 14/29] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-14-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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 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, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Rspamd-Queue-Id: 152A440017 X-Rspamd-Server: rspam01 X-Stat-Signature: yua7qiko4tigmyxuoskztysi7re3tetw X-HE-Tag: 1731358415-750819 X-HE-Meta: U2FsdGVkX18HTahYMzbDMFaCQOaUiOpHW8NXg6/DT0tJxDnKmfJ1L6S/8GlUw6u9Ji418I7uT/Vugo0IldCAA77MDKy1tavO53ozXSOoy6si9M7G/4LCMsEoC3NcO+8vgKSrMM8SBlW2kqTP7Zx2TK3moHtJsJVjg6mfFkwHdz+f9YEzgfKsTbo5EmYg5+u0nueYVqN3aUGqyy3jZHy94J7eAAsyDGolBq6zxVbfzkygwk2mVOhYqixWUuLIFPpfwVLc6AS2ioJq3VHPCaYleH4e+vzjyqEj6OLFawXsumDyfGytnMrNb0BBkIfJo3zF1Oo4cNdj5ZoLRl0IlhqQijHyVlMCouQTTcTQC3WBDmlYbAU3uhz969PPDe/y+5bXaEiMb2QMwcmJySRCEO/twxRAyoLRhw6oujszXifNcfsrWOIfwuAfFkUsMQ4BBFXhZ39gIB3KDASKYk/xYlWj8R8of8Wh3V3SIVMD6Viu/JY/7B4C7Hd9P0h0q1Xo5Pm52BM+GefQbnSkuV5PCfYbFKAXybfI1UIyIi8zdyBJ5qmMnT7PlW+7PJVkcJ37Xf2vEOftuCspnzs4ei7YtqFxx7CK5iFlp+/LjZlQ3W9dVDpIinmC5LLoVBWvF3XcBEBmn6uPr8l8L5zM68nSWQ3jLLoQlqzfI9qYo4L8RaIYB0O4fuYJULvZzi2UWSl3qmt3JSzIdrvy6uNLU4UFjXZG/IvSUscqlNuJRGd8u9Z930zjYLuiphrWdIfX6vMdkXx3G8qkdOgk6plvwtML48XT3dkPUfEe9i4DduZfontFOJMzODV/BorQX+I3KRymdZDJSzUP0qJMcS0OwmcAN9y6CjoyBvbIeu74FJbgl4CroXhbYsbrjVC83R+j+m502KdQgC8290E2RUCGJRmLH8Sx0QEVy2kr+HjsB0yIqGoAeKncz6pVKM7tSECcR/xC3p0LZcBJIvWL+Olsi7Qp2mb +R3v6Vm5 6rwidcgga1LfGKUiGZ/keI49aP7EXjbjq8nw5qWItgEePn++uKnZKZVUYtLLeE/pUZVvbFtTrjcwy47lB3m8Evs86D2kjzBn1f6CAg5RH28URaQVhmtA5vsw/PUCJoR7lEzeEgLxjIQDwpYwb31+7Ai/KnYCqDiSBg9aF9ea7slS/qI9O+aZ8EclWqmQ8Ujwj8Dv2CgJ+3gu9d/AhMJPecq9aeDnJthah9uT7h/KZa/fQE3oJQWnCPRvBN93cJe7ElAko2ZuoJSQoSmrV9jCzjGuWt4IaeO1mcYmeNqtOKg5hpiyLKByT/12S6cJKrpRr9vBAy+N3fJxI8PEdDa0YdLt8hMIfDsCyoHKVdLFmbC1jOEA6KkPKcgRZ/h0Do74015Pa1nI44g7A+O8WljStPMVBLLMFWtQlZHKItJJDQkp88pAA0lURZs4bZGThSPP9sFgaR0eay4LA+uULCMP62U+9igzVXvOKSV52qYQSeIuo/9A= 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` aarch64 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. Signed-off-by: Deepak Gupta Reviewed-by: Mark Brown --- 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 bdcec1732445..eff56aae05d7 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -203,4 +203,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 c42ed44e86bc..8afd5cb70237 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -352,4 +352,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 3d38a9c7c5c9..dafa31485584 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2339,6 +2339,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 @@ -2814,6 +2829,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: error = -EINVAL; break;