From patchwork Wed Aug 21 19:49:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 821105 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 BB6A0137745; Wed, 21 Aug 2024 19:50:31 +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=1724269831; cv=none; b=r20LdvzZ6z+ac7/7VMMvYQ/jIbNZo/z40PeUCkm9uUmRmXH7+KQ68RSJaMnUmVzSTbxjhh3ZylCc4BXfjXeG9O5oO+PU759KfoS9yjScszJJONnN+6diK5P5+O/ntCcikkoPDIs/AUJjQIF0Jqz7b4kFPpK2nMa23XcK8mxTtWI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724269831; c=relaxed/simple; bh=XqaXLGAQ/MwY54koFKC2rQRtWZp0KxmYdMIZOGKT0R8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dGrbjI+1iN3lCex3PyOIR/cEte5JWE5FHeUascZIGRO/8Nzj34u0NWoBoHH3rzxIiKByNDEfx9lXrxsM+ILDyEhm1dSOYZ4ogmFCuxwmUFFAYlmBPrANMg1MkjnaXT1mkYJGecFmk0kaUV4NKZWf0rowtoNMFA6ZvYmsPd4Rn5s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WLyRI3bO; 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="WLyRI3bO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43A13C4AF13; Wed, 21 Aug 2024 19:50:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724269831; bh=XqaXLGAQ/MwY54koFKC2rQRtWZp0KxmYdMIZOGKT0R8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WLyRI3bOPlN+noQ8+juXVDa0etYYSXg6Zz9bBTHAY4qvxJ48a3CSxpoOHeihFxu6g HsHu0f6joxUITmqgwweHwa471dfd1MiWY+Dc4TR3nYH7eTloUgHEHrqpl7mcgNjB/8 fcB9Hmm521oimjaXpHq5UgGQbVyx4/x2bP0V9zDc4T4irZrFsEne/zpUR80alGnG2y L20d5WBXlYLWRsOZ3fjBWWdXba7m7GP2AOzQd88vAa8zWcYslQsvho0ZhqyjQuzUgA DbjgJMP1YaLdI2m+A3ygBgTpSnowdGVG6wTQmLUVQY77PLOS6D7MOSMA0Y9nUkVjoG w/hxkd2v7AtZA== From: Mark Brown Date: Wed, 21 Aug 2024 20:49:47 +0100 Subject: [PATCH RFT v10 1/8] Documentation: userspace-api: Add shadow stack API documentation Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240821-clone3-shadow-stack-v10-1-06e8797b9445@kernel.org> References: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@kernel.org> In-Reply-To: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@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-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=3025; i=broonie@kernel.org; h=from:subject:message-id; bh=XqaXLGAQ/MwY54koFKC2rQRtWZp0KxmYdMIZOGKT0R8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmxkTzN8r7uAOtp9K3ugTi+sHgrab9M/rfCFd5DE9G jAMkX96JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsZE8wAKCRAk1otyXVSH0DYUB/ sEp2EphzksK2LC6zHqb4zhg9kZ6qJZA1N/HJovcXAeYxmUl6+DyHZn6WvxUqt34GnAPYV0j5F/h6rI Nyh9GjWy4KiJvYkbTAHLAzDEdu7KllpZlGgq4ESgzHXxu89cGS1vTGKO0TC+4R9MwrZUcQINB2JoXP +GyLCZnKVWUNWyUMPx2aqPQiq/oT8R84/lOdoZT3iaiiBofBO9YfyYOhTp67oEICW3/PfublaIQOC6 61f100EOCE6ZN1ZXUwDMq+FX3DLQFW3+42uiUXfeSu9JF0qzDSFsy+NVXDJNb9Gu3WMliIpa5Q+Lga rkGhIPS857aNxfrH1rmUaaH+C53e35 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB There are a number of architectures with shadow stack features which we are presenting to userspace with as consistent an API as we can (though there are some architecture specifics). Especially given that there are some important considerations for userspace code interacting directly with the feature let's provide some documentation covering the common aspects. Reviewed-by: Catalin Marinas Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/shadow_stack.rst | 41 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspace-api/index.rst index 274cc7546efc..c39709bfba2c 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -59,6 +59,7 @@ Everything else ELF netlink/index + shadow_stack sysfs-platform_profile vduse futex2 diff --git a/Documentation/userspace-api/shadow_stack.rst b/Documentation/userspace-api/shadow_stack.rst new file mode 100644 index 000000000000..c576ad3d7ec1 --- /dev/null +++ b/Documentation/userspace-api/shadow_stack.rst @@ -0,0 +1,41 @@ +============= +Shadow Stacks +============= + +Introduction +============ + +Several architectures have features which provide backward edge +control flow protection through a hardware maintained stack, only +writeable by userspace through very limited operations. This feature +is referred to as shadow stacks on Linux, on x86 it is part of Intel +Control Enforcement Technology (CET), on arm64 it is Guarded Control +Stacks feature (FEAT_GCS) and for RISC-V it is the Zicfiss extension. +It is expected that this feature will normally be managed by the +system dynamic linker and libc in ways broadly transparent to +application code, this document covers interfaces and considerations. + + +Enabling +======== + +Shadow stacks default to disabled when a userspace process is +executed, they can be enabled for the current thread with a syscall: + + - For x86 the ARCH_SHSTK_ENABLE arch_prctl() + +It is expected that this will normally be done by the dynamic linker. +Any new threads created by a thread with shadow stacks enabled will +themselves have shadow stacks enabled. + + +Enablement considerations +========================= + +- Returning from the function that enables shadow stacks without first + disabling them will cause a shadow stack exception. This includes + any syscall wrapper or other library functions, the syscall will need + to be inlined. +- A lock feature allows userspace to prevent disabling of shadow stacks. +- Those that change the stack context like longjmp() or use of ucontext + changes on signal return will need support from libc. From patchwork Wed Aug 21 19:49:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 821539 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 C542916F0DD; Wed, 21 Aug 2024 19:50:38 +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=1724269838; cv=none; b=bdoYS6EujNU0XQj6FnA1xlriXwYM7RbPOJv29m+Rlwet7wJIuvTQ4P1azbmL1ec0PUVRXi2jUV3ixzuCAOY4DY6o3y09XeMML/uic+CaZoX6wRt0M6EkXIX8CaU8/9zFooV2XXUPXwKxHVl6gaLqNVD3hTQGizOGjfvxHFGVw90= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724269838; c=relaxed/simple; bh=CV2+00DiKA+Ule1vDQG713YKkv7EqZ8uZ0vdFPGcS/o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Mc34pUEEfG7DwJ5e7BLAn/s6aL+/IdNZqdN9bo2QOUQcb7wdmzDWR6e7axU64wxHQ5LV5eyoyFDyeC58JD8/xJaWDzlPfF6Vr8AKhGlZN8l9vxvXZOPVqHJUJ1q6Lpc3B6JkvvF06HecqM4PyWqapXSghKEealSl3cRJVgPMGSs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ldLXIqEf; 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="ldLXIqEf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C37B7C32781; Wed, 21 Aug 2024 19:50:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724269838; bh=CV2+00DiKA+Ule1vDQG713YKkv7EqZ8uZ0vdFPGcS/o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ldLXIqEfiKw4EQx7ZdZC48wt1Ginlpxv8vijM39iyjOuTaGCOUHd14h8w4f7ulrzE 86LB2+gycMyfoNR56uOUDAB1gtNzjbobqz8XIi4TXJEBOs/NZU0ZMHTRqMpPLx4+1P jOEMQvIkq3TNZfckMG466biZJyCqZKX+kdU92q8EN8p/1EKpFCAADOehyngNDy475/ LN39u/jk7NbwAImgJYRzfQtyfia0vLtksAYplSrzq6qTD5GBJWE2LaUUWt0+jmM6/n LY/2Xe2fl4E5FZLHuFopvfim/D+QxV9FYJXX9/7ngO4gH1Zng7a2h8Z8LHrQj069m0 FQ7rU47hsmLtw== From: Mark Brown Date: Wed, 21 Aug 2024 20:49:48 +0100 Subject: [PATCH RFT v10 2/8] selftests: Provide helper header for shadow stack testing Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240821-clone3-shadow-stack-v10-2-06e8797b9445@kernel.org> References: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@kernel.org> In-Reply-To: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@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-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=2716; i=broonie@kernel.org; h=from:subject:message-id; bh=CV2+00DiKA+Ule1vDQG713YKkv7EqZ8uZ0vdFPGcS/o=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmxkT0pYB9su/KXi9puOsHeeGTRutBYq8powu/m0SH 3B9996SJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsZE9AAKCRAk1otyXVSH0LzrB/ 9xo8T67P5tfCTZ4/CrB7b8Otm4J98+sOlwDD9pQnVdymc8sPnMKSwft0M+axn8J5Z+rFSvJiI0Lixc MhFarbIgTCItA9SZHgcpAochhUGPpBQl2utbtw5nZlaL8aM4/t7fjiRcd1GtA/ihP0Xp49OozGzLPR /0r12IZ/Ip3zWA1fmCHXCamd3VpbFrdMQDXtcm999FGi3e6wAbOQauRplvlM2mmnBx95KyzhQmdc7W /k0SRHGb5Dw1bKjzh6FNwyy0sU5r0PKQQwaNzuJSUE8z6U5gxewftmmLbgm5TAFSNKmgsxC+C3oiK9 8EEGcMxYyfaJOf4eLN9KMqAkL9UhBz 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 | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tools/testing/selftests/ksft_shstk.h b/tools/testing/selftests/ksft_shstk.h new file mode 100644 index 000000000000..09f822b976e1 --- /dev/null +++ b/tools/testing/selftests/ksft_shstk.h @@ -0,0 +1,61 @@ +/* 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") = __NR_arch_prctl; \ + register long _arg1 asm("rdi") = (long)(arg1); \ + register long _arg2 asm("rsi") = (long)(arg2); \ + \ + asm volatile ( \ + "syscall\n" \ + : "=a"(_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 = ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK); + if (ret == 0) + shadow_stack_enabled = 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 From patchwork Wed Aug 21 19:49:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 821104 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 A60CC16EB7B; Wed, 21 Aug 2024 19:50:45 +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=1724269845; cv=none; b=MNrR+nAYG4tQiAd16/8xRbQkupeTmpbbEg7K+zrk/K7W7GBa7K2CNMHH9t6H4Jy+WbxpBHIBETq2tPHifVnPa0GLklH0oaiy17qOC4wU1ErSbj8zQ1x8BmTeHOud4moJFiMedof33ijFEh5ITVPr92W3hBeklCdFaV9u7mekq4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724269845; c=relaxed/simple; bh=aXGeSW3grtLdSnTdx5+gSzbAczg0duMe8mDGJOGSfWQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tX2y1JI/4mOHGHOXU1+dF/3TM05PCCXh0xIW4oRO7p1s4ZMCW0t/Z5bsPOWnafo/5j6mbPveSyUXoYibshE11QncRtilr7NNXIiOSx6NUSrcuNW4zq/g5SwbU5fP09qQvC4jBCCQxksvrH02wtR56sndqR+l/euPx8lyvqeFexw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nN2T3qhn; 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="nN2T3qhn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3818C4AF14; Wed, 21 Aug 2024 19:50:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724269845; bh=aXGeSW3grtLdSnTdx5+gSzbAczg0duMe8mDGJOGSfWQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=nN2T3qhnP3Fc7aDH2vnn5Wh55dpYvG/xNiLW6sXa6fi2g/B5EHTzeruwWoL8CJBpw zCoeqkd5o/NwJx28FWrZ8X8LNK8NOIVJNRIdkXRF/XIwqNswb9pPDntu3Kd3QIQcAi twi/S36nZ/ctECqqfi+K1aQdJYaCzEBeXykTDpXd5PFuyItRvphiULb4YQOZjHKwMF jus4zKdcw1p3RGYcAtigQoUU2z16lhkZxAsNc9pXeJeHKR+dhyMafLHulytf7DeHOv 9oj56TwmyRZpJ7cU/V9DALazcAyoiRhhlB4Avs53Jm0B70rflG2vcop0Gfigu48QAu mHi5+dHm3eejQ== From: Mark Brown Date: Wed, 21 Aug 2024 20:49:49 +0100 Subject: [PATCH RFT v10 3/8] mm: Introduce ARCH_HAS_USER_SHADOW_STACK Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240821-clone3-shadow-stack-v10-3-06e8797b9445@kernel.org> References: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@kernel.org> In-Reply-To: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@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 , David Hildenbrand , "Mike Rapoport (IBM)" , Kees Cook , Shuah Khan X-Mailer: b4 0.15-dev-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=2693; i=broonie@kernel.org; h=from:subject:message-id; bh=aXGeSW3grtLdSnTdx5+gSzbAczg0duMe8mDGJOGSfWQ=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmxkT1JN82YTNd5Xc0roP8UmiPdvmd5OWFRddLsNLv //dr4cGJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsZE9QAKCRAk1otyXVSH0FDzB/ 0ZfPd+KtGJCqx7/ZvTOUYTKzVXC0z0TBbi5IgAilS7zxjiigAyO0zvtpmXtwbcCylnVsuBBVd3sMtR rbbhP4kdkc1ZwbABsA3ZAgJ0l+fW7gQg06EA5b2jfZeKukw21l9Xcw+C7svCh0tOpTemv4NXsFU+QL jgiZmRpZGACM2P84WgM6WzQFjiJgNlQcz13anzTGERChPh4+NQGTBuVGIb8RgZbUTAPQjfvWNUabx3 3cvmLOWef5yrq9YlSTlS9mC4SstHLZ5IeJtNQ+iGjVpPwaVBIh7L+40rGdBTyAb1MBj6tTAUqS8OrQ m5Ii2cWg4xjAq9BFQkLFBe5bDDGvX7 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Since multiple architectures have support for shadow stacks and we need to select support for this feature in several places in the generic code provide a generic config option that the architectures can select. Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Reviewed-by: Deepak Gupta Reviewed-by: Rick Edgecombe Reviewed-by: Mike Rapoport (IBM) Reviewed-by: Catalin Marinas Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- arch/x86/Kconfig | 1 + fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 2 +- mm/Kconfig | 6 ++++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 007bab9f2a0e..320e1f411163 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1957,6 +1957,7 @@ config X86_USER_SHADOW_STACK depends on AS_WRUSS depends on X86_64 select ARCH_USES_HIGH_VMA_FLAGS + select ARCH_HAS_USER_SHADOW_STACK select X86_CET help Shadow stack protection is a hardware feature that detects function diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 5f171ad7b436..0ea49725f524 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -984,7 +984,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR [ilog2(VM_UFFD_MINOR)] = "ui", #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK [ilog2(VM_SHADOW_STACK)] = "ss", #endif #ifdef CONFIG_64BIT diff --git a/include/linux/mm.h b/include/linux/mm.h index c4b238a20b76..3357625c1db3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -342,7 +342,7 @@ extern unsigned int kobjsize(const void *objp); #endif #endif /* CONFIG_ARCH_HAS_PKEYS */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK /* * VM_SHADOW_STACK should not be set with VM_SHARED because of lack of * support core mm. diff --git a/mm/Kconfig b/mm/Kconfig index b72e7d040f78..3167be663bca 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1263,6 +1263,12 @@ config IOMMU_MM_DATA config EXECMEM bool +config ARCH_HAS_USER_SHADOW_STACK + bool + help + The architecture has hardware support for userspace shadow call + stacks (eg, x86 CET, arm64 GCS or RISC-V Zicfiss). + source "mm/damon/Kconfig" endmenu From patchwork Wed Aug 21 19:49:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 821538 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 0E2BD16EB7B; Wed, 21 Aug 2024 19:50:51 +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=1724269852; cv=none; b=bDqfx9z5fn2vzssqcKPFuS4K/+s08sj6DRT+5PTUyYPl0mdjWoT4SIbgaqqnQsi+NVTsaiWWW18/PRiCUDwGcrBCuCzaA2p8eujnsDeYFbWh+fn4HwWFbTGsq3QymqkyXDninVpFHL5Y46zubtApk1iRtMYE8lsfGPZnpYRP/1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724269852; c=relaxed/simple; bh=UZhCT1QuN7/aQ/L8FpjnptD+TWvV0XXThOZxLZ0LZiI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WwOblbARJYpazDF1h8/h5MX3K3xyFLqKXDX5fIb/4xRyqEFD3VmUPTz5ofmU+HMqEcrq/xeeK03E0Ipnop8ZpMeo5ElCUEfnuwo0GgbxwveZWIEKZWyAeiGwMWb/NJXeSfX7ZEwr1D0PmXa1t9OR8JEG/49hgpxiyiKm/0rjHJ4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ISjAzmK9; 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="ISjAzmK9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C02BC4AF0E; Wed, 21 Aug 2024 19:50:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724269851; bh=UZhCT1QuN7/aQ/L8FpjnptD+TWvV0XXThOZxLZ0LZiI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ISjAzmK9JJZEqetkn8+fFz3VcRixB9/BF4wZAhOtCba5aVIjYxzBf+pVGMn6lcrPW nQC6x7yGMuppfLfQCkjLejpyCW35wdmVtcH6r8vA5XjaNQ8Kbf4uRclT4T3sH6MXHQ JouJOLLgl90YFJrqZYovbYiwteqsNSvuNcUaPLY0GCm5gcsF0wWUZvApZWRDDJQaSf P0NxYo1Ft39aPczCMvr7dEeuDieW+TzRbxt6VWizQ/V7mDPOnvC9htx8B8QfAjbuCy S6Yt7mX4zAyBHNJMprSk/aVd+6U8dS+UAfsVLKjUYNYOkSVIQNbrHe3+wNb6Q78dHH 19IOF8HCBiJMA== From: Mark Brown Date: Wed, 21 Aug 2024 20:49:50 +0100 Subject: [PATCH RFT v10 4/8] fork: Add shadow stack support to clone3() Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240821-clone3-shadow-stack-v10-4-06e8797b9445@kernel.org> References: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@kernel.org> In-Reply-To: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@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 X-Mailer: b4 0.15-dev-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=16222; i=broonie@kernel.org; h=from:subject:message-id; bh=UZhCT1QuN7/aQ/L8FpjnptD+TWvV0XXThOZxLZ0LZiI=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmxkT2RbYr3jCtEynjYmQ/KD1KJ/YjQOAqnEgnTpXN LV2sPlSJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsZE9gAKCRAk1otyXVSH0P2HB/ wNMsROgqqsmZ4yTUNuAl3+UQJzxlsnxSFpyb+Zm0DFjYkpPZYx5ADy9pGiDnRcELvD51x0RdZST6zg d1JjEjGvjxDUNXi/x2oh2O+CRhjrEIw707Hdzu9jTGZPl1LJnPGpAyjNnPXDyMzwygrZKfB7w/COd+ r6Xcu/ov2cyY64vLcR1+mlHUqyT2IYrhzWzpHBLJUN+mdv8+GkL1hNHPXPbChUu0aRkj+7w4yb4f2p hQCTyAz11rUQ0Su44wd4wWGyIvn5dQE0nXFHpceWn4RDDVC/GW1nhd0i9IFB2K2wDaYrQBN9YW8rYf Ql9ZBbimBxz+wrJiDOSWycSnGEylqR X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Unlike with the normal stack there is no API for configuring the the shadow stack for a new thread, instead the kernel will dynamically allocate a new shadow stack with the same size as the normal stack. This appears to be due to the shadow stack series having been in development since before the more extensible clone3() was added rather than anything more deliberate. Add parameters to clone3() specifying the location and size of a shadow stack for the newly created process. If no shadow stack is specified then the existing implicit allocation behaviour is maintained. If a stack is specified then it is required to have an architecture defined token placed on the stack, this will be consumed by the new task. If the token is not provided then this will be reported as a segmentation fault with si_code SEGV_CPERR, as a runtime shadow stack protection error would be. This allows architectures to implement the validation of the token in the child process context. If the architecture does not support shadow stacks the shadow stack parameters must be zero, architectures that do support the feature are expected to enforce the same requirement on individual systems that lack shadow stack support. Update the existing x86 implementation to pay attention to the newly added arguments, in order to maintain compatibility we use the existing behaviour if no shadow stack is specified. Minimal validation is done of the supplied parameters, detailed enforcement is left to when the thread is executed. Since we are now using more fields from the kernel_clone_args we pass that into the shadow stack code rather than individual fields. Portions of the x86 architecture code were written by Rick Edgecombe. Signed-off-by: Mark Brown --- arch/x86/include/asm/shstk.h | 11 ++-- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/shstk.c | 96 ++++++++++++++++++++++++------- include/asm-generic/cacheflush.h | 11 ++++ include/linux/sched/task.h | 18 ++++++ include/uapi/linux/sched.h | 13 ++++- kernel/fork.c | 121 +++++++++++++++++++++++++++++++++++---- 7 files changed, 230 insertions(+), 42 deletions(-) diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 4cb77e004615..252feeda6999 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -6,6 +6,7 @@ #include struct task_struct; +struct kernel_clone_args; struct ksignal; #ifdef CONFIG_X86_USER_SHADOW_STACK @@ -16,8 +17,8 @@ struct thread_shstk { long shstk_prctl(struct task_struct *task, int option, unsigned long arg2); void reset_thread_features(void); -unsigned long shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, - unsigned long stack_size); +unsigned long shstk_alloc_thread_stack(struct task_struct *p, + const struct kernel_clone_args *args); void shstk_free(struct task_struct *p); int setup_signal_shadow_stack(struct ksignal *ksig); int restore_signal_shadow_stack(void); @@ -28,8 +29,10 @@ static inline long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) { return -EINVAL; } static inline void reset_thread_features(void) {} static inline unsigned long shstk_alloc_thread_stack(struct task_struct *p, - unsigned long clone_flags, - unsigned long stack_size) { return 0; } + const struct kernel_clone_args *args) +{ + return 0; +} static inline void shstk_free(struct task_struct *p) {} static inline int setup_signal_shadow_stack(struct ksignal *ksig) { return 0; } static inline int restore_signal_shadow_stack(void) { return 0; } diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index f63f8fd00a91..59456ab8d93f 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -207,7 +207,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) * is disabled, new_ssp will remain 0, and fpu_clone() will know not to * update it. */ - new_ssp = shstk_alloc_thread_stack(p, clone_flags, args->stack_size); + new_ssp = shstk_alloc_thread_stack(p, args); if (IS_ERR_VALUE(new_ssp)) return PTR_ERR((void *)new_ssp); diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 059685612362..ccfe562ea5d6 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -191,44 +191,96 @@ void reset_thread_features(void) current->thread.features_locked = 0; } -unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, - unsigned long stack_size) +int arch_shstk_validate_clone(struct task_struct *t, + struct vm_area_struct *vma, + struct page *page, + struct kernel_clone_args *args) +{ + /* + * SSP is aligned, so reserved bits and mode bit are a zero, just mark + * the token 64-bit. + */ + void *maddr = kmap_local_page(page); + int offset; + unsigned long addr, ssp; + u64 expected; + + if (!features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + ssp = args->shadow_stack + args->shadow_stack_size; + addr = ssp - SS_FRAME_SIZE; + expected = ssp | BIT(0); + offset = offset_in_page(addr); + + if (!cmpxchg_to_user_page(vma, page, addr, (unsigned long *)(maddr + offset), + expected, 0)) + return -EINVAL; + set_page_dirty_lock(page); + + return 0; +} + +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) { struct thread_shstk *shstk = &tsk->thread.shstk; + unsigned long clone_flags = args->flags; unsigned long addr, size; /* * If shadow stack is not enabled on the new thread, skip any - * switch to a new shadow stack. + * implicit switch to a new shadow stack and reject attempts to + * explicitly specify one. */ - if (!features_enabled(ARCH_SHSTK_SHSTK)) + if (!features_enabled(ARCH_SHSTK_SHSTK)) { + if (args->shadow_stack || args->shadow_stack_size) + return (unsigned long)ERR_PTR(-EINVAL); + return 0; + } /* - * For CLONE_VFORK the child will share the parents shadow stack. - * Make sure to clear the internal tracking of the thread shadow - * stack so the freeing logic run for child knows to leave it alone. + * If the user specified a shadow stack then do some basic + * validation and use it, otherwise fall back to a default + * shadow stack size if the clone_flags don't indicate an + * allocation is unneeded. */ - if (clone_flags & CLONE_VFORK) { + if (args->shadow_stack) { + addr = args->shadow_stack; + size = args->shadow_stack_size; shstk->base = 0; shstk->size = 0; - return 0; - } + } else { + /* + * For CLONE_VFORK the child will share the parents + * shadow stack. Make sure to clear the internal + * tracking of the thread shadow stack so the freeing + * logic run for child knows to leave it alone. + */ + if (clone_flags & CLONE_VFORK) { + shstk->base = 0; + shstk->size = 0; + return 0; + } - /* - * For !CLONE_VM the child will use a copy of the parents shadow - * stack. - */ - if (!(clone_flags & CLONE_VM)) - return 0; + /* + * For !CLONE_VM the child will use a copy of the + * parents shadow stack. + */ + if (!(clone_flags & CLONE_VM)) + return 0; - size = adjust_shstk_size(stack_size); - addr = alloc_shstk(0, size, 0, false); - if (IS_ERR_VALUE(addr)) - return addr; + size = args->stack_size; + size = adjust_shstk_size(size); + addr = alloc_shstk(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return addr; - shstk->base = addr; - shstk->size = size; + /* We allocated the shadow stack, we should deallocate it. */ + shstk->base = addr; + shstk->size = size; + } return addr + size; } diff --git a/include/asm-generic/cacheflush.h b/include/asm-generic/cacheflush.h index 7ee8a179d103..96cc0c7a5c90 100644 --- a/include/asm-generic/cacheflush.h +++ b/include/asm-generic/cacheflush.h @@ -124,4 +124,15 @@ static inline void flush_cache_vunmap(unsigned long start, unsigned long end) } while (0) #endif +#ifndef cmpxchg_to_user_page +#define cmpxchg_to_user_page(vma, page, vaddr, ptr, old, new) \ +({ \ + bool ret; \ + \ + ret = try_cmpxchg(ptr, &old, new); \ + flush_icache_user_page(vma, page, vaddr, sizeof(*ptr)); \ + ret; \ +}) +#endif + #endif /* _ASM_GENERIC_CACHEFLUSH_H */ diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index d362aacf9f89..c818efdd57af 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -16,6 +16,7 @@ struct task_struct; struct rusage; union thread_union; struct css_set; +struct vm_area_struct; /* All the bits taken by the old clone syscall. */ #define CLONE_LEGACY_FLAGS 0xffffffffULL @@ -43,6 +44,8 @@ struct kernel_clone_args { void *fn_arg; struct cgroup *cgrp; struct css_set *cset; + unsigned long shadow_stack; + unsigned long shadow_stack_size; }; /* @@ -230,4 +233,19 @@ static inline void task_unlock(struct task_struct *p) DEFINE_GUARD(task_lock, struct task_struct *, task_lock(_T), task_unlock(_T)) +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK +int arch_shstk_validate_clone(struct task_struct *p, + struct vm_area_struct *vma, + struct page *page, + struct kernel_clone_args *args); +#else +static inline int arch_shstk_validate_clone(struct task_struct *p, + struct vm_area_struct *vma, + struct page *page, + struct kernel_clone_args *args) +{ + return 0; +} +#endif + #endif /* _LINUX_SCHED_TASK_H */ diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h index 3bac0a8ceab2..8b7af52548fd 100644 --- a/include/uapi/linux/sched.h +++ b/include/uapi/linux/sched.h @@ -84,6 +84,10 @@ * kernel's limit of nested PID namespaces. * @cgroup: If CLONE_INTO_CGROUP is specified set this to * a file descriptor for the cgroup. + * @shadow_stack: Pointer to the memory allocated for the child + * shadow stack. + * @shadow_stack_size: Specify the size of the shadow stack for + * the child process. * * The structure is versioned by size and thus extensible. * New struct members must go at the end of the struct and @@ -101,12 +105,15 @@ struct clone_args { __aligned_u64 set_tid; __aligned_u64 set_tid_size; __aligned_u64 cgroup; + __aligned_u64 shadow_stack; + __aligned_u64 shadow_stack_size; }; #endif -#define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */ -#define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */ -#define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */ +#define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */ +#define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */ +#define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */ +#define CLONE_ARGS_SIZE_VER3 104 /* sizeof fourth published struct */ /* * Scheduling policies diff --git a/kernel/fork.c b/kernel/fork.c index cc760491f201..a53f2ccd431b 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -128,6 +128,11 @@ */ #define MAX_THREADS FUTEX_TID_MASK +/* + * Require that shadow stacks can store at least one element + */ +#define SHADOW_STACK_SIZE_MIN sizeof(void *) + /* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2107,6 +2112,57 @@ static void rv_task_fork(struct task_struct *p) #define rv_task_fork(p) do {} while (0) #endif +static int shstk_validate_clone(struct task_struct *p, + struct kernel_clone_args *args) +{ + struct mm_struct *mm; + struct vm_area_struct *vma; + struct page *page; + unsigned long addr; + int ret; + + if (!IS_ENABLED(CONFIG_ARCH_HAS_USER_SHADOW_STACK)) + return 0; + + if (!args->shadow_stack) + return 0; + + mm = get_task_mm(p); + if (!mm) + return -EFAULT; + + mmap_read_lock(mm); + + /* + * All current shadow stack architectures have tokens at the + * top of a downward growing shadow stack. + */ + addr = args->shadow_stack + args->shadow_stack_size - 1; + addr = untagged_addr_remote(mm, addr); + + page = get_user_page_vma_remote(mm, addr, FOLL_FORCE | FOLL_WRITE, + &vma); + if (IS_ERR(page)) { + ret = -EFAULT; + goto out; + } + + if (!(vma->vm_flags & VM_SHADOW_STACK) || + !(vma->vm_flags & VM_WRITE)) { + ret = -EFAULT; + goto out_page; + } + + ret = arch_shstk_validate_clone(p, vma, page, args); + +out_page: + put_page(page); +out: + mmap_read_unlock(mm); + mmput(mm); + return ret; +} + /* * This creates a new process as a copy of the old one, * but does not actually start it yet. @@ -2381,6 +2437,9 @@ __latent_entropy struct task_struct *copy_process( if (retval) goto bad_fork_cleanup_namespaces; retval = copy_thread(p, args); + if (retval) + goto bad_fork_cleanup_io; + retval = shstk_validate_clone(p, args); if (retval) goto bad_fork_cleanup_io; @@ -2939,7 +2998,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, CLONE_ARGS_SIZE_VER1); BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) != CLONE_ARGS_SIZE_VER2); - BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2); + BUILD_BUG_ON(offsetofend(struct clone_args, shadow_stack_size) != + CLONE_ARGS_SIZE_VER3); + BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER3); if (unlikely(usize > PAGE_SIZE)) return -E2BIG; @@ -2972,16 +3033,18 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, return -EINVAL; *kargs = (struct kernel_clone_args){ - .flags = args.flags, - .pidfd = u64_to_user_ptr(args.pidfd), - .child_tid = u64_to_user_ptr(args.child_tid), - .parent_tid = u64_to_user_ptr(args.parent_tid), - .exit_signal = args.exit_signal, - .stack = args.stack, - .stack_size = args.stack_size, - .tls = args.tls, - .set_tid_size = args.set_tid_size, - .cgroup = args.cgroup, + .flags = args.flags, + .pidfd = u64_to_user_ptr(args.pidfd), + .child_tid = u64_to_user_ptr(args.child_tid), + .parent_tid = u64_to_user_ptr(args.parent_tid), + .exit_signal = args.exit_signal, + .stack = args.stack, + .stack_size = args.stack_size, + .tls = args.tls, + .set_tid_size = args.set_tid_size, + .cgroup = args.cgroup, + .shadow_stack = args.shadow_stack, + .shadow_stack_size = args.shadow_stack_size, }; if (args.set_tid && @@ -3022,6 +3085,40 @@ static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) return true; } +/** + * clone3_shadow_stack_valid - check and prepare shadow stack + * @kargs: kernel clone args + * + * Verify that shadow stacks are only enabled if supported. + */ +static inline bool clone3_shadow_stack_valid(struct kernel_clone_args *kargs) +{ + if (kargs->shadow_stack) { + if (!kargs->shadow_stack_size) + return false; + + if (kargs->shadow_stack_size < SHADOW_STACK_SIZE_MIN) + return false; + + if (kargs->shadow_stack_size > rlimit(RLIMIT_STACK)) + return false; + + if (!IS_ALIGNED(kargs->shadow_stack, sizeof(void *))) + return false; + + if (!IS_ALIGNED(kargs->shadow_stack_size, sizeof(void *))) + return false; + + /* + * The architecture must check support on the specific + * machine. + */ + return IS_ENABLED(CONFIG_ARCH_HAS_USER_SHADOW_STACK); + } else { + return !kargs->shadow_stack_size; + } +} + static bool clone3_args_valid(struct kernel_clone_args *kargs) { /* Verify that no unknown flags are passed along. */ @@ -3044,7 +3141,7 @@ static bool clone3_args_valid(struct kernel_clone_args *kargs) kargs->exit_signal) return false; - if (!clone3_stack_valid(kargs)) + if (!clone3_stack_valid(kargs) || !clone3_shadow_stack_valid(kargs)) return false; return true; From patchwork Wed Aug 21 19:49:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 821103 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 73EE5172BD5; Wed, 21 Aug 2024 19:50:58 +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=1724269858; cv=none; b=Ay5a+slU+2mSBpteJVRkX2kHttBDhW6BxMhp7mslEy4SRB+BOQsNS2fSmFrngUByTOWKMG8fUSXjto9FU3lqMgU3NLafR7B6z0jwBjf09dnMhwIsHkRH8pzlSGgZBKaqP/Xw80wZlaOpwN8y8MhRm7LnF3wlgwohJB8FNEhIp0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724269858; c=relaxed/simple; bh=j9ob7ZYoLp4hP0MnrgcOUFxUwkxx358xO3rY/g4W29c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oAR62htgGZvI0thLZglrjkrc3Dp3GVYSWXf6I4b8q8u4Qg8CReWeqmS1BMUtmaQeuURxHcA/B8NfqQ5/qIwl1qKqbu5vtdIumUrct1JGHH7R/8yxKdYWM3O5aH6MUAcihaoF+OPV62lvzXHQzD3LIwY+24OUk9iBftcMgyOt0to= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MoKw2yZ9; 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="MoKw2yZ9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AB3FC4AF14; Wed, 21 Aug 2024 19:50:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724269858; bh=j9ob7ZYoLp4hP0MnrgcOUFxUwkxx358xO3rY/g4W29c=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=MoKw2yZ9KV1p5r3J7S1CSQ9O3+nVD91Sy68k+/7/QcHB37D9IhNcXSVRWbKtcDHsX OxoFtjah3jsvRu8UjbDY03FMKrKNOCwm9X6w/DmGmP+VHI/hVvTTPm0MvfexxVet+T rUOui2+ERQe7j/kM5ZVXmKy10VCkL/N/7HTPU8DCy1a657SANGkKOCXfvbF+/pWUth 1zOiDf8+U55NXq64FVxMzXK0djXbWiKYJGR6MuAlIKBp65e5C8cKiaLcPm27eIS3M+ Jf4HdIdGVb5QoMyw9ABba4s9znFTwTw1jDFNcUw42h9CEs7Zq6utF9unmw8tfmze6T S3BLHjKvpF8lQ== From: Mark Brown Date: Wed, 21 Aug 2024 20:49:51 +0100 Subject: [PATCH RFT v10 5/8] selftests/clone3: Remove redundant flushes of output streams Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240821-clone3-shadow-stack-v10-5-06e8797b9445@kernel.org> References: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@kernel.org> In-Reply-To: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@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-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=1058; i=broonie@kernel.org; h=from:subject:message-id; bh=j9ob7ZYoLp4hP0MnrgcOUFxUwkxx358xO3rY/g4W29c=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmxkT3+D5LXm84pnj9roznmK7UUj20NrCOZrrJXoNZ VIHaj3OJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsZE9wAKCRAk1otyXVSH0BdzB/ 47DHhwUw0m/H9CKUCRzIjZF+ciL+wgACMoWQW/F7u4q5VayUMv7J3qXcrdjTFWQCK6TOpAFAJRWlpr QPkvP1ZaBxDuKB5iPNoTEcAoHrZvpLk8PcjOCbVPuDniEV8JWth6RbgAKZYJ15X2RCyS4gaT+sWuRu 2m5z2kXUmn0t+FcA/oGvaanhYjNVMDaJRlhYyCMh2XesRSYzlCuqv9Qs1Zmelgo1Qf7SUahZpGYhdw j5l4QWFq9LTK7h8tpPJU/8isW1jM/ovcDTLENLg8Wi0xJ8YuemUOofObkJ7zmwB75Viw3QJK9UxfyI V4ZP8h3498/md4jcU363yxLjDmVuLM X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Since there were widespread issues with output not being flushed the kselftest framework was modified to explicitly set the output streams unbuffered in commit 58e2847ad2e6 ("selftests: line buffer test program's stdout") so there is no need to explicitly flush in the clone3 tests. Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3_selftests.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/testing/selftests/clone3/clone3_selftests.h b/tools/testing/selftests/clone3/clone3_selftests.h index 3d2663fe50ba..39b5dcba663c 100644 --- a/tools/testing/selftests/clone3/clone3_selftests.h +++ b/tools/testing/selftests/clone3/clone3_selftests.h @@ -35,8 +35,6 @@ struct __clone_args { static pid_t sys_clone3(struct __clone_args *args, size_t size) { - fflush(stdout); - fflush(stderr); return syscall(__NR_clone3, args, size); } From patchwork Wed Aug 21 19:49:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 821537 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 C1BDC170A00; Wed, 21 Aug 2024 19:51:04 +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=1724269864; cv=none; b=pP06gVSUqIoUQVqOtJRGvg4Y2Bls896fCAalohZssKHOLDYkffXXx78HAzKFUuh1HNaUvrOb4m7MTDaZKuzHH9Xc0ZR2x4gEvgLcWL/Y+4d/SmTxWXzOCsz/FJ+nwZcyaibDUW2UQoyg/WHy5vVHoXoWvDDpuBgNPsbGcdn7aNg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724269864; c=relaxed/simple; bh=XIYOtX/wTMGB1zAIVkW11px29AlArl5B/1MgdpVsr7Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=keD0GBpMWoUy5HhSX01s0nnmszW8qERLM/hpvfyoDWDCkFhRMCcdujFWYMzajTj0KnadNrQ06dQJ+dxCIVttA2DfcoU4wvG45bri+tORv1DlGai7MlJvCVjoUDFq9vwPVf6aUvW4nYuhCJfKMuO++Zl1vTDJFZ878yiGmcy9cyw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MJkGCy8A; 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="MJkGCy8A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72FA6C32781; Wed, 21 Aug 2024 19:50:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724269864; bh=XIYOtX/wTMGB1zAIVkW11px29AlArl5B/1MgdpVsr7Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=MJkGCy8ApV/ys0URSN6hH8Yy1KNhHmo2xsoT5kBmK0eIaRLb5WDVR4ky76ZvLNlr3 bNtFWkYzuonEX1vVgK3YE8B0JqZDY+JPU+RFOAaG0Wa4XwRja3eAl9TwlLESA1tXHT cYaTDaiWpJFlQ6SoFCX9fJ1qd54HPj5wFr0EyVPnaS0EM2DvZ600D3BGvV8qepzUIx K7SHkrjeb+jjsOsrCPqbdf9gI3EXXYuB6YjDtp5dgkbiiS9SfLM1oC7Ey6VIEaywjK KxkGE4yAdSgJHKd6jOtD2CPqDtctWOaotABJXZLxaAvZAuFYB+YJh9IDjuwGJfgKjJ 192ckqk/Yj0+g== From: Mark Brown Date: Wed, 21 Aug 2024 20:49:52 +0100 Subject: [PATCH RFT v10 6/8] selftests/clone3: Factor more of main loop into test_clone3() Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240821-clone3-shadow-stack-v10-6-06e8797b9445@kernel.org> References: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@kernel.org> In-Reply-To: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@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-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=3875; i=broonie@kernel.org; h=from:subject:message-id; bh=XIYOtX/wTMGB1zAIVkW11px29AlArl5B/1MgdpVsr7Y=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmxkT3/kh+bLhfd2RVxC+EsB9zKKBJ9YgaCLhhrY05 ciAPt/SJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsZE9wAKCRAk1otyXVSH0KNqB/ 4/jfxK9edVHH0AUcfbvG2WMLLLq7yIfLNAYFwkXWgV355hHCTfnUD0ARLCTExHd6L+NztqfvT+i3XZ 4rKTlQ7SiYf3Z+RrCWr3cQsPkLWeAkmcoaMDCPjp7LJw39UNmzHs/B0d4N5iAb0TVa9OwW0U3MAobX G7u+15MQm9mUTbdB8Eu4IEctRLLMbHGMt20exFFXo0KC9fZ00cPvZ31W6sQMMRRfaD+PBZjPjeSjdL uSAUkn33ehaiiQaPBACAtugprpVp/l1L4h/UwWqZHhgNs/HimTplzbO9c7ZkS/Ta5uSxy7UVEslNgN Gq7NbucGYIzEAYJseJ46u9YaABZAvb X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB In order to make it easier to add more configuration for the tests and more support for runtime detection of when tests can be run pass the structure describing the tests into test_clone3() rather than picking the arguments out of it and have that function do all the per-test work. No functional change. Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 77 ++++++++++++++++----------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index e61f07973ce5..e066b201fa64 100644 --- a/tools/testing/selftests/clone3/clone3.c +++ b/tools/testing/selftests/clone3/clone3.c @@ -30,6 +30,19 @@ enum test_mode { CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG, }; +typedef bool (*filter_function)(void); +typedef size_t (*size_function)(void); + +struct test { + const char *name; + uint64_t flags; + size_t size; + size_function size_function; + int expected; + enum test_mode test_mode; + filter_function filter; +}; + static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) { struct __clone_args args = { @@ -109,30 +122,40 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) return 0; } -static bool test_clone3(uint64_t flags, size_t size, int expected, - enum test_mode test_mode) +static void test_clone3(const struct test *test) { + size_t size; int ret; + if (test->filter && test->filter()) { + ksft_test_result_skip("%s\n", test->name); + return; + } + + if (test->size_function) + size = test->size_function(); + else + size = test->size; + + ksft_print_msg("Running test '%s'\n", test->name); + ksft_print_msg( "[%d] Trying clone3() with flags %#" PRIx64 " (size %zu)\n", - getpid(), flags, size); - ret = call_clone3(flags, size, test_mode); + getpid(), test->flags, size); + ret = call_clone3(test->flags, size, test->test_mode); ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n", - getpid(), ret, expected); - if (ret != expected) { + getpid(), ret, test->expected); + if (ret != test->expected) { ksft_print_msg( "[%d] Result (%d) is different than expected (%d)\n", - getpid(), ret, expected); - return false; + getpid(), ret, test->expected); + ksft_test_result_fail("%s\n", test->name); + return; } - return true; + ksft_test_result_pass("%s\n", test->name); } -typedef bool (*filter_function)(void); -typedef size_t (*size_function)(void); - static bool not_root(void) { if (getuid() != 0) { @@ -160,16 +183,6 @@ static size_t page_size_plus_8(void) return getpagesize() + 8; } -struct test { - const char *name; - uint64_t flags; - size_t size; - size_function size_function; - int expected; - enum test_mode test_mode; - filter_function filter; -}; - static const struct test tests[] = { { .name = "simple clone3()", @@ -319,24 +332,8 @@ int main(int argc, char *argv[]) ksft_set_plan(ARRAY_SIZE(tests)); test_clone3_supported(); - for (i = 0; i < ARRAY_SIZE(tests); i++) { - if (tests[i].filter && tests[i].filter()) { - ksft_test_result_skip("%s\n", tests[i].name); - continue; - } - - if (tests[i].size_function) - size = tests[i].size_function(); - else - size = tests[i].size; - - ksft_print_msg("Running test '%s'\n", tests[i].name); - - ksft_test_result(test_clone3(tests[i].flags, size, - tests[i].expected, - tests[i].test_mode), - "%s\n", tests[i].name); - } + for (i = 0; i < ARRAY_SIZE(tests); i++) + test_clone3(&tests[i]); ksft_finished(); } From patchwork Wed Aug 21 19:49:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 821102 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 B8576170855; Wed, 21 Aug 2024 19:51:12 +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=1724269872; cv=none; b=cDHFZxSURQNkuASzdloGgnncAgyF3fWZ8HBV5PnTupLiepZfNaKRLVt1CJZOSNQLZXns86s2Toz0wyNTx4rHo5zSF1qnbHMh8ErYcPXgA3QrzlZ8uFj/mLjZlZH5BS4oxXfRWXxChD9sBGQ0N4gbW8aynp6PvMZZVm+7gCpQieY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724269872; c=relaxed/simple; bh=2JIySYLy5Zx4h3HBoI1rUprWAIQuQf3PAf28WNSsOzU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fKtbkp167q3+1uh+kRz4Iv3oALnVyiSG0S+IhRbINhMFRQF6Tn/zSDe6Ke+p3ZnqgeEKbzvMMp8KIlGb7QpFLyhze16l8G6MHLs1vQx78oA81zisi3XzCssoNZKvhGiJL5uhQ+hp8uYrimZVXhnr3T01D26/0VOp/vEVvP2BUf4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hRwPRqJn; 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="hRwPRqJn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DD058C4AF0E; Wed, 21 Aug 2024 19:51:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724269872; bh=2JIySYLy5Zx4h3HBoI1rUprWAIQuQf3PAf28WNSsOzU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hRwPRqJnYBbPgMUhlyqAHi8FVMJjtGtrlmGixrMuBxwXsRqk3NUQqChuq3JpBHayW +Lymxrbp6NiGRdpnxg6nTI4OIU6x+2XEQ26OMgu18Vj8rZOv/r9E3sOhBGEptxoHji t7Y3wh3RDqSb+qb4ZO1w/Bh2Dd7jivf6h+17M6puOgqXyPo8RTDKDyFvm1r7Q0vM64 1c6IFeghp3u/yjFNkmzqXBhhFzq/pbbMkMHCMU9n1sTAR7ZyKmX2RadPfGD2IvHZOK lS0HwqdhK6jnZy8J31fzgPwOLQy8jr3zP6kYrw9EKMTk1yl+XVajTT+w3gbiIbX9J3 NKVgMJP0y3F3Q== From: Mark Brown Date: Wed, 21 Aug 2024 20:49:53 +0100 Subject: [PATCH RFT v10 7/8] selftests/clone3: Allow tests to flag if -E2BIG is a valid error code Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240821-clone3-shadow-stack-v10-7-06e8797b9445@kernel.org> References: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@kernel.org> In-Reply-To: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@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-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=2019; i=broonie@kernel.org; h=from:subject:message-id; bh=2JIySYLy5Zx4h3HBoI1rUprWAIQuQf3PAf28WNSsOzU=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmxkT40EpADXcYp8wA8JPtLKZznOrn0MPunDTJ0rVY 9K37166JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsZE+AAKCRAk1otyXVSH0FpIB/ 9zHnOLYBiS253oINoN3buiNPqjjiAdxdUjMHy23VQoqzAUEravIyKT7lzsLNKmkAcsAj+MKV9jLVdY Oxs04fpqNS7oLGrD7+uJ24G7vGu9s+clIp83Egw39hPvDuXGjXLmuAUfb4X6sRsA2N6GqN9zy3K6W0 sPXRgdE5SzwUJxii0uwP8Jk8f0YP1LE81xswiwau8rAv6Ks9jmfUNlO4baR6ZGWgprLJRkuHYekoKb S4vy8UYcs1BkvgRi5y3WMtzzaO4YG8pF7zFcvMgxkPpkj683clk1Bi8GSjVvST9nuS1cnhlvWSeOIm LZ7uN47i7j+k0WhYCzSVATLZC6InzV X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB The clone_args structure is extensible, with the syscall passing in the length of the structure. Inside the kernel we use copy_struct_from_user() to read the struct but this has the unfortunate side effect of silently accepting some overrun in the structure size providing the extra data is all zeros. This means that we can't discover the clone3() features that the running kernel supports by simply probing with various struct sizes. We need to check this for the benefit of test systems which run newer kselftests on old kernels. Add a flag which can be set on a test to indicate that clone3() may return -E2BIG due to the use of newer struct versions. Currently no tests need this but it will become an issue for testing clone3() support for shadow stacks, the support for shadow stacks is already present on x86. Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index e066b201fa64..5b8b7d640e70 100644 --- a/tools/testing/selftests/clone3/clone3.c +++ b/tools/testing/selftests/clone3/clone3.c @@ -39,6 +39,7 @@ struct test { size_t size; size_function size_function; int expected; + bool e2big_valid; enum test_mode test_mode; filter_function filter; }; @@ -146,6 +147,11 @@ static void test_clone3(const struct test *test) ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n", getpid(), ret, test->expected); if (ret != test->expected) { + if (test->e2big_valid && ret == -E2BIG) { + ksft_print_msg("Test reported -E2BIG\n"); + ksft_test_result_skip("%s\n", test->name); + return; + } ksft_print_msg( "[%d] Result (%d) is different than expected (%d)\n", getpid(), ret, test->expected); From patchwork Wed Aug 21 19:49:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 821536 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 CDBF516F907; Wed, 21 Aug 2024 19:51:20 +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=1724269880; cv=none; b=ahZ3e+MU1VKxQoGemzWBDxdgHrH+ZuPXxbz2FUqMSRmwndtoUGLiV6AVOQm2+fuqfCJqWcbMKNBzwqeAps3RjUK+cWqOgqCa4cj6w4Umgu/7OovbljLuqWVmVizGEKC8Co3D6ims8sTtesYddPdGXrDEWJa8pj4UqDHE9tClT5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724269880; c=relaxed/simple; bh=5yH/d7BOwvCKepANKQGaHVNcRaNRmrS4M7rZQdZpQ9I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JT6vv77vH6EdBKAqiqUFQ9waQR6iv2lzc7RpRcYNqIS6WWbHD3EnqhkKVWwo+RBu6YmyWiG/a14DSYqQIRdTB0NNy3CKZDvSYKaG6+5+UfNlrz84em/bHlPUaSYjtVrSz7eU9z4W+gSkLsSRGJ7BxzYdgwQSZG4wVor/PyeSpK4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bO76IR4x; 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="bO76IR4x" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BFDFFC32786; Wed, 21 Aug 2024 19:51:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724269880; bh=5yH/d7BOwvCKepANKQGaHVNcRaNRmrS4M7rZQdZpQ9I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bO76IR4xkVDTdbV58zfmkn/RnbX2mE0qFHCuzUS8FIi0wgKomSjKmjRcetqZzltxr UxM56Rh94VQsu+dd1+PMSn49PykuqqUrhaKLQdGe2dz3ns+GVGLtMn4bOw7IPZTaTd YuXb4brXIT9HvIhngvRbxhbsPVDf28bq94x0klM1pH0RQlKMnbtQWYTQTmGbvLggup o56nIHy08FO/rZeUb5qtQrnTPct8mhSWDETQwiCGzwWzKxOaN8Vjo6V/6EC4+ZKtyu ACEeWFSWUwaGgnS4REB3Yc1I83kt21uDe/GhGu6M6sAIdFkAGmVbEEvw+15KeJKn2c /zukYuwgHYxEg== From: Mark Brown Date: Wed, 21 Aug 2024 20:49:54 +0100 Subject: [PATCH RFT v10 8/8] selftests/clone3: Test shadow stack support Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240821-clone3-shadow-stack-v10-8-06e8797b9445@kernel.org> References: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@kernel.org> In-Reply-To: <20240821-clone3-shadow-stack-v10-0-06e8797b9445@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 , Shuah Khan X-Mailer: b4 0.15-dev-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=10217; i=broonie@kernel.org; h=from:subject:message-id; bh=5yH/d7BOwvCKepANKQGaHVNcRaNRmrS4M7rZQdZpQ9I=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmxkT5lEPDkHO+kI0AYWEGJW68V/Z2+99uCn37h2iU 7NF7e5uJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsZE+QAKCRAk1otyXVSH0DGhB/ 41437f4Z9w91qjmY7Y6wP8BqyTB5hZ/18M52syUYRGlKumZk8JmxoyUjN03v1PuJ4B4VxiBkjd7j0G nf8w8zDADmwgDFS32jTeQ+ETBewEE9vbe1cIMnIvl+Wz3mFtSddqVvDw0rJU3brE3+47LiHyWrCzsE Rq1ukwpb9FFT0k1KlTb7VPd0cGabyPj6v7EcGzi6rHDOPFYdLi2vhGAo1at33OtP+4a3iLo7CYxX/V fMyrGsXwYcmGq4RfcWMsHAeMsYU20mD6btNmwjqOdth40hktVFIQyrGAEbiIz96/Y5/8iYeDHJ0Kqf YCgcZJtl0jM7F1J2xJYwMqWoEThmD5 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Add basic test coverage for specifying the shadow stack for a newly created thread via clone3(), including coverage of the newly extended argument structure. We check that a user specified shadow stack can be provided, and that invalid combinations of parameters are rejected. In order to facilitate testing on systems without userspace shadow stack support we manually enable shadow stacks on startup, this is architecture specific due to the use of an arch_prctl() on x86. Due to interactions with potential userspace locking of features we actually detect support for shadow stacks on the running system by attempting to allocate a shadow stack page during initialisation using map_shadow_stack(), warning if this succeeds when the enable failed. In order to allow testing of user configured shadow stacks on architectures with that feature we need to ensure that we do not return from the function where the clone3() syscall is called in the child process, doing so would trigger a shadow stack underflow. To do this we use inline assembly rather than the standard syscall wrapper to call clone3(). In order to avoid surprises we also use a syscall rather than the libc exit() function., this should be overly cautious. Acked-by: Shuah Khan Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 171 +++++++++++++++++++++- tools/testing/selftests/clone3/clone3_selftests.h | 38 +++++ 2 files changed, 208 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index 5b8b7d640e70..534a9ea98cb1 100644 --- a/tools/testing/selftests/clone3/clone3.c +++ b/tools/testing/selftests/clone3/clone3.c @@ -3,6 +3,7 @@ /* Based on Christian Brauner's clone3() example */ #define _GNU_SOURCE +#include #include #include #include @@ -11,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -19,8 +21,12 @@ #include #include "../kselftest.h" +#include "../ksft_shstk.h" #include "clone3_selftests.h" +static bool shadow_stack_supported; +static size_t max_supported_args_size; + enum test_mode { CLONE3_ARGS_NO_TEST, CLONE3_ARGS_ALL_0, @@ -28,6 +34,13 @@ enum test_mode { CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG, CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG, + CLONE3_ARGS_SHADOW_STACK, + CLONE3_ARGS_SHADOW_STACK_BAD_BASE, + CLONE3_ARGS_SHADOW_STACK_BAD_SIZE, + CLONE3_ARGS_SHADOW_STACK_NO_SIZE, + CLONE3_ARGS_SHADOW_STACK_NO_POINTER, + CLONE3_ARGS_SHADOW_STACK_NO_TOKEN, + CLONE3_ARGS_SHADOW_STACK_NORMAL_MEMORY, }; typedef bool (*filter_function)(void); @@ -44,6 +57,44 @@ struct test { filter_function filter; }; + +/* + * We check for shadow stack support by attempting to use + * map_shadow_stack() since features may have been locked by the + * dynamic linker resulting in spurious errors when we attempt to + * enable on startup. We warn if the enable failed. + */ +static void test_shadow_stack_supported(void) +{ + long ret; + + ret = syscall(__NR_map_shadow_stack, 0, getpagesize(), 0); + if (ret == -1) { + ksft_print_msg("map_shadow_stack() not supported\n"); + } else if ((void *)ret == MAP_FAILED) { + ksft_print_msg("Failed to map shadow stack\n"); + } else { + ksft_print_msg("Shadow stack supportd\n"); + shadow_stack_supported = true; + + if (!shadow_stack_enabled) + ksft_print_msg("Mapped but did not enable shadow stack\n"); + } +} + +static unsigned long long get_shadow_stack_page(unsigned long flags) +{ + unsigned long long page; + + page = syscall(__NR_map_shadow_stack, 0, getpagesize(), flags); + if ((void *)page == MAP_FAILED) { + ksft_print_msg("map_shadow_stack() failed: %d\n", errno); + return 0; + } + + return page; +} + static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) { struct __clone_args args = { @@ -89,6 +140,32 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG: args.exit_signal = 0x00000000000000f0ULL; break; + case CLONE3_ARGS_SHADOW_STACK: + args.shadow_stack = get_shadow_stack_page(SHADOW_STACK_SET_TOKEN); + args.shadow_stack_size = getpagesize(); + break; + case CLONE3_ARGS_SHADOW_STACK_BAD_BASE: + args.shadow_stack = get_shadow_stack_page(SHADOW_STACK_SET_TOKEN) + 1; + args.shadow_stack_size = getpagesize(); + break; + case CLONE3_ARGS_SHADOW_STACK_BAD_SIZE: + args.shadow_stack = get_shadow_stack_page(SHADOW_STACK_SET_TOKEN); + args.shadow_stack_size = getpagesize() - 1; + break; + case CLONE3_ARGS_SHADOW_STACK_NO_POINTER: + args.shadow_stack_size = getpagesize(); + break; + case CLONE3_ARGS_SHADOW_STACK_NO_SIZE: + args.shadow_stack = get_shadow_stack_page(SHADOW_STACK_SET_TOKEN); + break; + case CLONE3_ARGS_SHADOW_STACK_NORMAL_MEMORY: + args.shadow_stack = (unsigned long long)malloc(getpagesize()); + args.shadow_stack_size = getpagesize(); + break; + case CLONE3_ARGS_SHADOW_STACK_NO_TOKEN: + args.shadow_stack = get_shadow_stack_page(0); + args.shadow_stack_size = getpagesize(); + break; } memcpy(&args_ext.args, &args, sizeof(struct __clone_args)); @@ -102,7 +179,12 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) if (pid == 0) { ksft_print_msg("I am the child, my PID is %d\n", getpid()); - _exit(EXIT_SUCCESS); + /* + * Use a raw syscall to ensure we don't get issues + * with manually specified shadow stack and exit handlers. + */ + syscall(__NR_exit, EXIT_SUCCESS); + ksft_print_msg("CHILD FAILED TO EXIT PID is %d\n", getpid()); } ksft_print_msg("I am the parent (%d). My child's pid is %d\n", @@ -184,6 +266,26 @@ static bool no_timenamespace(void) return true; } +static bool have_shadow_stack(void) +{ + if (shadow_stack_supported) { + ksft_print_msg("Shadow stack supported\n"); + return true; + } + + return false; +} + +static bool no_shadow_stack(void) +{ + if (!shadow_stack_supported) { + ksft_print_msg("Shadow stack not supported\n"); + return true; + } + + return false; +} + static size_t page_size_plus_8(void) { return getpagesize() + 8; @@ -327,6 +429,70 @@ static const struct test tests[] = { .expected = -EINVAL, .test_mode = CLONE3_ARGS_NO_TEST, }, + { + .name = "Shadow stack on system with shadow stack", + .size = 0, + .expected = 0, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK, + .filter = no_shadow_stack, + }, + { + .name = "Shadow stack with misaligned base address", + .size = 0, + .expected = -EINVAL, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK_BAD_BASE, + }, + { + .name = "Shadow stack with misaligned size", + .size = 0, + .expected = -EINVAL, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK_BAD_SIZE, + }, + { + .name = "Shadow stack with no pointer", + .size = 0, + .expected = -EINVAL, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK_NO_POINTER, + }, + { + .name = "Shadow stack with no size", + .size = 0, + .expected = -EINVAL, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK_NO_SIZE, + .filter = no_shadow_stack, + }, + { + .name = "Shadow stack with no token", + .flags = CLONE_VM, + .size = 0, + .expected = -EINVAL, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK_NO_TOKEN, + .filter = no_shadow_stack, + }, + { + .name = "Shadow stack with normal memory", + .flags = CLONE_VM, + .size = 0, + .expected = -EFAULT, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK_NORMAL_MEMORY, + .filter = no_shadow_stack, + }, + { + .name = "Shadow stack on system without shadow stack", + .flags = CLONE_VM, + .size = 0, + .expected = -EINVAL, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK, + .filter = have_shadow_stack, + }, }; int main(int argc, char *argv[]) @@ -334,9 +500,12 @@ int main(int argc, char *argv[]) size_t size; int i; + enable_shadow_stack(); + ksft_print_header(); ksft_set_plan(ARRAY_SIZE(tests)); test_clone3_supported(); + test_shadow_stack_supported(); for (i = 0; i < ARRAY_SIZE(tests); i++) test_clone3(&tests[i]); diff --git a/tools/testing/selftests/clone3/clone3_selftests.h b/tools/testing/selftests/clone3/clone3_selftests.h index 39b5dcba663c..38d82934668a 100644 --- a/tools/testing/selftests/clone3/clone3_selftests.h +++ b/tools/testing/selftests/clone3/clone3_selftests.h @@ -31,12 +31,50 @@ struct __clone_args { __aligned_u64 set_tid; __aligned_u64 set_tid_size; __aligned_u64 cgroup; +#ifndef CLONE_ARGS_SIZE_VER2 +#define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */ +#endif + __aligned_u64 shadow_stack; + __aligned_u64 shadow_stack_size; +#ifndef CLONE_ARGS_SIZE_VER3 +#define CLONE_ARGS_SIZE_VER3 104 /* sizeof fourth published struct */ +#endif }; +/* + * For architectures with shadow stack support we need to be + * absolutely sure that the clone3() syscall will be inline and not a + * function call so we open code. + */ +#ifdef __x86_64__ +static pid_t __always_inline sys_clone3(struct __clone_args *args, size_t size) +{ + long ret; + register long _num __asm__ ("rax") = __NR_clone3; + register long _args __asm__ ("rdi") = (long)(args); + register long _size __asm__ ("rsi") = (long)(size); + + __asm__ volatile ( + "syscall\n" + : "=a"(ret) + : "r"(_args), "r"(_size), + "0"(_num) + : "rcx", "r11", "memory", "cc" + ); + + if (ret < 0) { + errno = -ret; + return -1; + } + + return ret; +} +#else static pid_t sys_clone3(struct __clone_args *args, size_t size) { return syscall(__NR_clone3, args, size); } +#endif static inline void test_clone3_supported(void) {