From patchwork Tue Oct 8 22:37:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827101 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 8D15DCF042B for ; Tue, 8 Oct 2024 22:38:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A77936B00B2; Tue, 8 Oct 2024 18:38:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D8276B00B3; Tue, 8 Oct 2024 18:38:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 803EB6B00B4; Tue, 8 Oct 2024 18:38:41 -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 61FFB6B00B2 for ; Tue, 8 Oct 2024 18:38:41 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 838511A0C8D for ; Tue, 8 Oct 2024 22:38:38 +0000 (UTC) X-FDA: 82651900842.25.270E96D Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf16.hostedemail.com (Postfix) with ESMTP id 34F4A180008 for ; Tue, 8 Oct 2024 22:38:39 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RxGlstgC; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 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=1728426983; 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=H2HJsBemoUtn69Y0WdrOXv10G4III+URyauyH8vTu7Q=; b=JOhFNq6y5YBG9ybP4VPiVv7v+WHfV0oLdkR64G27Yl0j+/67JDlIqampFYdKhnm5ADaRB+ MDeRkgx3t0ieFV6RdG7NyTlOTQlQd5gVgyzy739t4fbj6dtvKrB7B8Q+UHLZhMupYvRLoY nuDqSlr9RgK0uKp+hl1vb/6NOivS3C4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426983; a=rsa-sha256; cv=none; b=MbkKHnXAiXbuGNezlljQbDANnAaObSUNdfQA74RAwBLsPhI35Oosxi58KfAJgr39/cVODb YTXp/kJdPE2Rlz6RTcfty3fy6lnUOF3lzJDZacCDKAklJOdhZUnodYn+7iOxbQgXxfge+N NlayqD/PYV5ublIvXG4IYZc5pIWs7VM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RxGlstgC; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-71dfc1124cdso223664b3a.1 for ; Tue, 08 Oct 2024 15:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427118; x=1729031918; 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=H2HJsBemoUtn69Y0WdrOXv10G4III+URyauyH8vTu7Q=; b=RxGlstgCBbc7eQyQRLs4M7YDcM7b6tL7b+ZxW6U9b1pM4YlAXMJ3FSsuYdtHhtQi1J 8/I00wh6nYrWnYwfm4dBz8/T69TYtylRDB5eVttlFkM+FxazMM1I1OHLTWILuqz4sOwO Tqp4BEqob9NvKF0oAoM/HS2RdoOx9b5sGvJOVqdGgwbZWOB/B8+DSevvy7fVCsJ/rJ5o MNxW9qTXAzGv7x8xmdA0TPRo5dldGqqSeDyfcLQqx8kMItr+KGa4aqQl9XhmQvEI3uwd 0X7coVboR+pMM5E1zwdrVnnK98bkE+sZbM0rZrkxkHUTUJXQVe7fSjHIKIKZ/DLKpnki w40w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427118; x=1729031918; 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=H2HJsBemoUtn69Y0WdrOXv10G4III+URyauyH8vTu7Q=; b=HXpIJf9P9e7rK3qQdS+Oatn3g1ZALuW1SpGbUk2ZYfUswRbP1ZfEV7nLk8gtVoJjUP w+5j9ctT3QlL2/9iYdTzMclcz3BCsOEmBW+Cnb8n1z1cQEH7MlHhy07vXsr6JWFU2+AH waU2sZ2pYj1uL3XIB6cGo5jB4ElMG/bKO7Th59/Y+LpKAr7Q7em64kFlWn5ut2nxVfS/ j/aFySYfU8WJD8kRpNjaAohcwS8fwPeCUKCYST7577qW7n/IGv51YINSs77OvauBQZ4C frKAfZPQpOh+/TLUR8ywU1b/UEQ1p4K+oOB2GyG1wNGhulKls0NN+IpoQlTb4pvpJvfz tXTQ== X-Forwarded-Encrypted: i=1; AJvYcCXj4IYiIgGsyVZ4uRzQTzMoXxi1rhsEfs3BHbg4L+O4KKydwVbxSJ5Rax9mQASakH3Brevrc+HRpA==@kvack.org X-Gm-Message-State: AOJu0YwAo8yna1/Jv3sQD2Jncn4SIut59qLEaB5vRJ909OE9RgCPEoNt cKYKk0vyrGYjhDBewUsPhPDHKoDMyTaQtLQUT5YxJ/4UU+3eOAbQBhIuUUV2pAE= X-Google-Smtp-Source: AGHT+IG1wGSlqMBE3PBvpa6FjggvdA8TOV0s8RC2JgpdWWcLYtxJbO7UMJi9+bIzOEiti8s0uhAHTQ== X-Received: by 2002:a05:6a00:2e08:b0:71d:f459:6df3 with SMTP id d2e1a72fcca58-71e1d6a47f2mr972694b3a.10.1728427117956; Tue, 08 Oct 2024 15:38:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:37 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:00 -0700 Subject: [PATCH v6 18/33] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-18-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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-Stat-Signature: qxcj7bmxe81o1j888kxkp4f8atxcennm X-Rspamd-Queue-Id: 34F4A180008 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728427119-421936 X-HE-Meta: U2FsdGVkX1+O8GiHIpEGgz1VgnjRWk4tdF6c5nlvMm5a9BsTg0XCSBvbSF+zFuTwvotEUjFB0LVDJHD0kElD66nImktJGeFDhdpRSLdXpsunL7vyfWimQCZuSEAwDpk2MEdL4I8tvuID7TumTr/jywv0lLC01CQcMCcj82eac/L2tTGrlY/cNd472R/EapRWogBUJq+sBfABglOIrqPTGP/vCCDDAgTSFgJz2V7iLhjfdQdJHjGQR4xhlgNDLxSVfO7xxATTjV77AmrbLGsyU/LXiceCeuIIGOk8Z4unC69soLURTDY1GPuGAueoJ/UGyRmkF+j9oA1MHILDlkgbXzT4o7NCM0aWwbMBfm5Qs89iyQtdkApvU7B3MkP/Hgf5SfLqSt8xxmKGgRrZqW4BBpuSDRKb4fgeOsgWuKFM6csw31GAmEzv2ROxokPmEVmWIZd2EoQ9lGLCP6OobGf0USLHMrieR1fBfhm1pEqPy7I6zt+zYlJtm4FmMtn4k3Bi1DO461jkr7jCBuBWzLCjwks8FgjJSftpxzPAcCotU5hLG91TsDYgIEgohkPhTdDgKFtHXF1XnL4nrKeXHigk/C4Qi5vVpBIlwGCXbIYRRKkKNeCD2SsbthkoDV9gPPdtg0cgGWm0JoGuqwzc4Jr7VHHQJtbPmfThf2qolrZI+NQzLYmwjKhV7fECYPVCmTa4UehI6Gp6OEQXhumsT6ASJhsHPbmw56ZfnbKMsvSwOxTyx5GEf2MKoArrx1wG3V4eNIzbHvEzkZrtSfjrVfZy6uAf8R3C+IfNwPzB7M0iJN+aO2zy1pPsCaI3Cuan84eNHxnubDl6J+6o1UzlAg0eXkB87PLL359zH7XmpzbDkK0sPkx5rvcpteerfSf6jAtecpkdDUXYRc7oP5dotC5xnfda/fOsPyqgrwmjwKrZYIKFsGx8G8OYGsMyG50fZjMI1FBdY/+WaeJswqBRb2k ZOCisI/z AYy9Z1hDuGRdtXO7476yG8/tQtQwvy5MtR3rQsHbUXumuYeomnOR5s1LNhS81V5h7i8clgTuYYFjmvEIiaEMdMAOFydkICKpA7LvxmweroEQqoZiNk6xlMIXiZWUuk5+o+UTsx9eq9gBZYPDca8JHWgTOQQV1NeUzTjj/le0AwLJaO8KzqH5/92K89pVs0yrO8hlD6JE6RKLa10lvwGhxmJYrOsiMHJkYd7MzKco8z+rPSbWJ9ixlW4Go8VdSxXglTOKKpCEhNUTRycELm3JZ3zRgCZZNVK8j6lLYL0WOmWTfvXuPW0EbYZ1fX+vR0T5rDZ1WhNr3R22cWXS6oqOthfQMFBGECRQEwPn0xmO29tZg6d6ZOv38jwidd4V+fIBhq7A8i5bpOXIcyqJAMgOHAGxPtF+M7jeeT+2TPCW2032twjPnZOpWEXKf6Kc7x/YrFheDbTM2GlbceXTno/mE8wUayqPguklLfRk9qn+ZMMwbQNsg4pfwXcOejlZ1Lwiqoln0Mi6wq+ENxrVTb8tFEk2EWV+2gPPfunEcd+Ww1CKC3XXJwM8tXUDXuzx4wd6pNRqkzsw33nJb7l8= 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 b8d7b6361754..41ffb53490a4 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -349,4 +349,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;