Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp868669lqh; Thu, 28 Mar 2024 21:49:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUW1qF+xpFv+qykhaxKyr6xEia8aZbE5QO8dAKfN+TZXDUj2+8pgpR5312lknTdEb1RhpfCbgbEjPbXMfuA9l/yO7k7uo2zZ7dkuDqOOQ== X-Google-Smtp-Source: AGHT+IE5mmt4Fn5aJp+ROe4y/1H8OfyKUE3IePHlun4m12MT5GMUU6z1y6QPXjYVmyWZQruoxyDz X-Received: by 2002:a17:906:2793:b0:a46:a8ba:60b3 with SMTP id j19-20020a170906279300b00a46a8ba60b3mr732538ejc.37.1711687796803; Thu, 28 Mar 2024 21:49:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711687796; cv=pass; d=google.com; s=arc-20160816; b=W28G2xwdf6ayinFQy0JxuznR6t7YP/xMAnr/eS4q8c+qIB6p3MMHH1j9ajAtY41af/ T6M1x6mRH3XPbtZSJ3oDnjEE743nghK0Ahcg/TA/4zRqs2YI/peL5YUHFuw4C87NBE69 A3V8X1d+5yUwNrx88G4PMWwQQxxyNDBK+WFVkSqkStojG0IXFg3KLipQe3dqA+XGnSrl NWT6FPb+1MsUoFtijFuPDvcWbwaXZctmPFShto6qkD4PKBjSXjFKBCItV6n6J1IJdSBk JOO6HeBxhz6UR76/ywz34qVzcYFcQUt/vEaksrFkU8iRWYAaXt4m6jGKp6+2r2IG23sH 5kiQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=jOjKUP7R2Pn5KHy+bcQHndffhGuedIhGkkiUJ74Ko2E=; fh=M7qpTXi1x7lYz7pZayPhtRqFsFbAf8r7MrDzonm8iQI=; b=bf9wJGPpfQslHtVFl3RE45YwmkmtWkfKD8AD6fVgArja8NRPuvWJMEE2vYlJpb41zd d19mQld0DDYIQuTgzJrhYa0NfYmXyQXENddjzJpmjy7wVo4LtEy8b8k8DTupsddhqOj6 bWlcVR9w8Aab1gWlHH1LDUIPnLGI0/uTM7xeOMosHV1cpmPP/WvLHxj+0Oz49Q88lyX0 wLjr6bc5U/0JVlcuoyk5w3H13eJr7Cn9cMRUCF95PQAANnEkdQkN3PXfYDK0iSWtfay1 Bfy1leWscZYYs/jNefIJ7+KeBTjClqrtg78WUu59YNuNwxh6EOc4E5IJaxkFTT3WPkfK /CMw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jnYDFceL; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-124133-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124133-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id cm19-20020a170906f59300b00a4720ded97dsi1478961ejd.185.2024.03.28.21.49.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:49:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124133-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jnYDFceL; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-124133-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124133-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5C4AC1F22272 for ; Fri, 29 Mar 2024 04:49:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 633A55102D; Fri, 29 Mar 2024 04:46:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="jnYDFceL" Received: from mail-oa1-f53.google.com (mail-oa1-f53.google.com [209.85.160.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 334424EB32 for ; Fri, 29 Mar 2024 04:46:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687600; cv=none; b=Plb4Bgf0SMyiyaNR73WZ1wEUAGWlJa8BgD/HrbCAjwuP+o0w/fPn5jAX0jCuJLvTbgPuFsPFmEDe9Ws7ayPJItbOPgKd9rdhoMPOou/vnjCc7fUHsVeqU22CuhlSxcusfO/ZBsctIx7jPPrWVSsBqmk/iCr40vS+CGvLvqaefDw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687600; c=relaxed/simple; bh=WZoX9OMaTgZ5W8TxdmEgour/LDEOHemNGGvTExrcBSI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qwJSGCTYK2q595M9YEsNXDOQCHcfQAWCWqQj4fyO3iy7K+tH6bWlEpEfQSTFZ2Dwo8z22oKyndwub6HZHlGckxQbhYtQA15SrzysbJjoaMGDAjZ5X8h8SEKXytfKe8dzKhDdDL3FTq3VKaa30Ttx+nQezy9O7dNvrwyFzB5gRew= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=jnYDFceL; arc=none smtp.client-ip=209.85.160.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-oa1-f53.google.com with SMTP id 586e51a60fabf-2282597eff5so1116792fac.3 for ; Thu, 28 Mar 2024 21:46:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687597; x=1712292397; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jOjKUP7R2Pn5KHy+bcQHndffhGuedIhGkkiUJ74Ko2E=; b=jnYDFceLOMiF4Lkr3b/7DoM2gfBm63VgJ2aeU5oqHWQ3iKMFVHE9xRGmMIePNNKXFu WOXY0dIqgK6YzeNWCPPFwlEP/15NqU20rqGhlwMHdhUl6Ly1XYPP4gnannd6nCqOha7r JMZOzCo1Ww5u+OR0Uj/AgoWzujDIJjMPznzR93nuDRshkCqSfOh65G+TVLHLJ8xXmx4U zC9WHvujAm/y2K98Y2n5Q87rg6M+ilR5ItyyNo4kl07oH5cHUx0zucQynn8DmIOa6WWU jWePgXUksnnpMW3yrwDSAwQ8pSrBpPNe9PDIHN+aYUiZ0Um1aOK9mjwQD8N04TL9gzKf lCdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687597; x=1712292397; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jOjKUP7R2Pn5KHy+bcQHndffhGuedIhGkkiUJ74Ko2E=; b=SvUoTCUWbDsEsQIXC23Lg2SgxQt013Tii5WukljvE51AlqfTNn1kCLLyVlkgiiMxj4 tf85X+Rv5kpVuIpfozGZczPl+m2zIw9hfBnrMEBy3U0zqFkMKQlxHMk2gtX3/qPyxzgr hRdWNgVleapMOkYiNtr8znXZKbxQTXAEqVu+CLqWPxKgwgMaBRaTNB+ChRkmxI6wzGzX gmXKLcEqUXIL4rCFY7S0E2J5vIXnOaeTRNb3c/sMx5VbK42QWQdBvb5Mhicn7YW4ImVH U4nY7am+URgW8sSct5Mvz0Abi/1QaoqOAhZ76xakFMgk7TwnQF5+yp7OZTFnOBGgBs2Z x5Xg== X-Forwarded-Encrypted: i=1; AJvYcCVr9R/P4zXwfJHV55R7VXE0EkWMjEUIAwNK+K6qh+GyCBAelsDEplUW6YrdUr3cVLBY3e5gA8P9QJcWHwR28G5gaqw4pzuAGtLdorCl X-Gm-Message-State: AOJu0YzO0NM5eSLzFU0xLk6uCpZ4wn1CgHhV035nGNc+9DhnfaGSF4Cb CgPZ+GACgtOVDt6qXG3XT53UXgW/vTll7yIEJYnPI2WMiv1cYaqaolF7Pu8swMw= X-Received: by 2002:a05:6870:b698:b0:229:e636:921f with SMTP id cy24-20020a056870b69800b00229e636921fmr1250647oab.49.1711687597188; Thu, 28 Mar 2024 21:46:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:36 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 16/27] prctl: arch-agnostic prtcl for indirect branch tracking Date: Thu, 28 Mar 2024 21:44:48 -0700 Message-Id: <20240329044459.3990638-17-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- include/uapi/linux/prctl.h | 27 +++++++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 3c66ed8f46d8..b7a8212a068e 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -328,4 +328,31 @@ struct prctl_mm_map { */ #define PR_LOCK_SHADOW_STACK_STATUS 73 +/* + * 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 74 + +/* + * 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 75 +# 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 76 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 242e9f147791..c770060c3f06 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2330,6 +2330,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 __user *status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2787,6 +2802,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, (unsigned long __user *) arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_indir_br_lp_status(me, (unsigned long __user *) arg2); + break; default: error = -EINVAL; break; -- 2.43.2