From nobody Mon Nov 25 01:34:57 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9971F1CB339; Thu, 31 Oct 2024 19:25:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730402753; cv=none; b=FzJXZWXQkwlI8v4GtRBTFvzWGb1pgZl30IRgRbN4SvEVJY4hK7oOeqUyQVbW8CMODg/IMXNBQgzsuUw3XHd+WZ9TkC+LcLmb2aaKAOQ6U907gx9jvorQlzZ66QNXiNFJvTM6mwrjQYvq+l0hogj040wPDSGKIcaIf8B1Cna4YJY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730402753; c=relaxed/simple; bh=5R2x8naFdQiQL8xwTf6W/tsDu9xiZhpQtf9OK3u1Lgo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EWhpcyOq7vmRhazF65S8ZzX1a0+ApMsJd+xMLR/dPwJ/KcRcG0+9iPaMCMuIJAMnw/R8saYHrD4pnsTJG0fmvZutIdR3kpm2M3Ek7mVyXhwghnj1uB+9kX7Hl+Eyz2ydBYQGL3UALa2N9iv6h0SPYgd79x6EE81BrotOQ+0k4Zs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=K9vXctD7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="K9vXctD7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59A6CC4CED4; Thu, 31 Oct 2024 19:25:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730402753; bh=5R2x8naFdQiQL8xwTf6W/tsDu9xiZhpQtf9OK3u1Lgo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=K9vXctD7ReNpEiq72uPI7pm2fiFX2e262GfFd7My4PPeGzZOmN77zelVZZ6FAbyht jiCANmNLb6iqfCROlXDmGgqlu9xxpNFCw88G4pmYbb6xD7f/ex7x4txw7X2h5Dj3NS sNx3SmEQRUhIRvVm33nRDi0//5rNp8M4E2xc9w3k3MCOCcKoueUniBR57WY3whKJbq 8WqXOTpPEyHTDVsSWJaIbWOhZdOdtPbOHIbeM3PO6/gfgRX1JMN9UWMGj79Rf5i/G+ IDVqVKK7Dfhou+QuLyD5KhWib9F4ybcZyYxyRdm1ZlNxQ/bj/434nHZ3SAIcIJBbl9 fAxC06OI04zxQ== From: Mark Brown Date: Thu, 31 Oct 2024 19:25:04 +0000 Subject: [PATCH RFT v12 3/8] selftests: Provide helper header for shadow stack testing Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241031-clone3-shadow-stack-v12-3-7183eb8bee17@kernel.org> References: <20241031-clone3-shadow-stack-v12-0-7183eb8bee17@kernel.org> In-Reply-To: <20241031-clone3-shadow-stack-v12-0-7183eb8bee17@kernel.org> To: "Rick P. Edgecombe" , Deepak Gupta , Szabolcs Nagy , "H.J. Lu" , Florian Weimer , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook , Kees Cook , Shuah Khan X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=4486; i=broonie@kernel.org; h=from:subject:message-id; bh=5R2x8naFdQiQL8xwTf6W/tsDu9xiZhpQtf9OK3u1Lgo=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnI9mj3fEEo4TgyQgf5BXn3JpIJPSyNDqHqa96x1bi vFCAGpqJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZyPZowAKCRAk1otyXVSH0PZOB/ 0ZRLRGhiniOUUTFyVEdmettuRM6SXcFEYIkF8bUKgbkdY98Tlq1AZRgwBMykp7taAkZdcLsn4GD5JP 2O3mcfnWgD8NBlRx7ecMaidxaLCobLAbodkDoNIknnXEI3oqzLKdoaFjls8OaIIHPLwzl8vl8AMcSh AjOd1VR+r5GRTMNagchvcA71TUTY4LIzQVuz7oCs+EJLlnm04lsMZa88seMpxF90UORHv6Fi2UQ2mU QGUTryZcUVBXpUUPVU1fCC4uWl8HKvXzXeW5laCW8nobhXs8gm5wor/KAmHWNfCSsEcMKGoENkwxCa ACu5rcW9RMeAZ2elMhM6rdVZuo43x3 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB While almost all users of shadow stacks should be relying on the dynamic linker and libc to enable the feature there are several low level test programs where it is useful to enable without any libc support, allowing testing without full system enablement. This low level testing is helpful during bringup of the support itself, and also in enabling coverage by automated testing without needing all system components in the target root filesystems to have enablement. Provide a header with helpers for this purpose, intended for use only by test programs directly exercising shadow stack interfaces. Reviewed-by: Rick Edgecombe Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- tools/testing/selftests/ksft_shstk.h | 98 ++++++++++++++++++++++++++++++++= ++++ 1 file changed, 98 insertions(+) diff --git a/tools/testing/selftests/ksft_shstk.h b/tools/testing/selftests= /ksft_shstk.h new file mode 100644 index 0000000000000000000000000000000000000000..869ecea2bf3ea3d30cead9819d2= b3a75f5397754 --- /dev/null +++ b/tools/testing/selftests/ksft_shstk.h @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Helpers for shadow stack enablement, this is intended to only be + * used by low level test programs directly exercising interfaces for + * working with shadow stacks. + * + * Copyright (C) 2024 ARM Ltd. + */ + +#ifndef __KSFT_SHSTK_H +#define __KSFT_SHSTK_H + +#include + +/* This is currently only defined for x86 */ +#ifndef SHADOW_STACK_SET_TOKEN +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) +#endif + +static bool shadow_stack_enabled; + +#ifdef __x86_64__ +#define ARCH_SHSTK_ENABLE 0x5001 +#define ARCH_SHSTK_SHSTK (1ULL << 0) + +#define ARCH_PRCTL(arg1, arg2) \ +({ \ + long _ret; \ + register long _num asm("eax") =3D __NR_arch_prctl; \ + register long _arg1 asm("rdi") =3D (long)(arg1); \ + register long _arg2 asm("rsi") =3D (long)(arg2); \ + \ + asm volatile ( \ + "syscall\n" \ + : "=3Da"(_ret) \ + : "r"(_arg1), "r"(_arg2), \ + "0"(_num) \ + : "rcx", "r11", "memory", "cc" \ + ); \ + _ret; \ +}) + +#define ENABLE_SHADOW_STACK +static inline __attribute__((always_inline)) void enable_shadow_stack(void) +{ + int ret =3D ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK); + if (ret =3D=3D 0) + shadow_stack_enabled =3D true; +} + +#endif + +#ifdef __aarch64__ +#define PR_SET_SHADOW_STACK_STATUS 75 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) + +#define my_syscall2(num, arg1, arg2) = \ +({ = \ + register long _num __asm__ ("x8") =3D (num); \ + register long _arg1 __asm__ ("x0") =3D (long)(arg1); \ + register long _arg2 __asm__ ("x1") =3D (long)(arg2); \ + register long _arg3 __asm__ ("x2") =3D 0; \ + register long _arg4 __asm__ ("x3") =3D 0; \ + register long _arg5 __asm__ ("x4") =3D 0; \ + \ + __asm__ volatile ( \ + "svc #0\n" \ + : "=3Dr"(_arg1) \ + : "r"(_arg1), "r"(_arg2), \ + "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define ENABLE_SHADOW_STACK +static inline __attribute__((always_inline)) void enable_shadow_stack(voi= d) +{ + int ret; + + ret =3D my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + PR_SHADOW_STACK_ENABLE); + if (ret =3D=3D 0) + shadow_stack_enabled =3D true; +} + +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#ifndef ENABLE_SHADOW_STACK +static inline void enable_shadow_stack(void) { } +#endif + +#endif --=20 2.39.2