From patchwork Mon Aug 19 19:24:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13768885 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 048EB4438B; Mon, 19 Aug 2024 19:26:14 +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=1724095574; cv=none; b=Vjun7CrYiSB23bwtgNXnjrmlaKAdxCN+yEiwbM8iI8NxDg+hlYtQHebNz+ZU2FwBptFHkK2r/PeMGAecjEAzWZ4YhANOY5goTHyq2iKAJol77uR/xSUpc1t7eK2hz6zBpz3kyF1abS6/IWIjpgQEmducACdgkd3KStHrSKwxEi8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724095574; c=relaxed/simple; bh=XqaXLGAQ/MwY54koFKC2rQRtWZp0KxmYdMIZOGKT0R8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JrxdLokgRk+0SjADgQ4ZzEAdwVGtVbGhbLgckdzjTgi8OHLnQm9baqS4/BWvxQQUOK9HFpeF8RFMrcbrB07j0olxNdr5yG2HibNehZqDniJxxUxsW54lqPF0N4Ee3r5Y6vFXapDapxRAtFud0WLOB8AW5xJW/UWw9LLva5/9+84= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ob5kjtRv; 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="Ob5kjtRv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11D5BC4AF0F; Mon, 19 Aug 2024 19:26:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724095573; bh=XqaXLGAQ/MwY54koFKC2rQRtWZp0KxmYdMIZOGKT0R8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Ob5kjtRvMZr9p18LrWF7FYWFSgcWs7j0et9E4BUf5lYjIzKBhQDZWXKoaknEKU+4k +I4ZAgY11BW0D4D7iU4ZLdV84anqTe1LmPp6SVwgnPILqRgsrOmSlkh6wtzhWx4CNj f4OiHd8jPl+WBXXRr0hADi6zXVoaKcJwZkIL862p9ZVd+1jfp2QlyUng2uVG3yEayL 8vYJkDlHEsL+1eVOkBT8iZJ9TAA9GFyQ3P9ZhVErlVFjUsOVLnKfzElhYPls4Hxlu6 +cUjR37lbSmOV88FnyewuT1IjEGepxl3A2ESa0o/T+hRbWRy4ZjMbG+4/WMQ9CdrG9 EUSQobM/qHDHA== From: Mark Brown Date: Mon, 19 Aug 2024 20:24:22 +0100 Subject: [PATCH RFT v9 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: <20240819-clone3-shadow-stack-v9-1-962d74f99464@kernel.org> References: <20240819-clone3-shadow-stack-v9-0-962d74f99464@kernel.org> In-Reply-To: <20240819-clone3-shadow-stack-v9-0-962d74f99464@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/pANAwAKASTWi3JdVIfQAcsmYgBmw5xC0y0bUFtqGjv6GwHm2mrytwY5XdTuWUvKZVjW bivxP+iJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsOcQgAKCRAk1otyXVSH0I41B/ 966o0WSrMUG5NtJOY/5I2Y1yYchhXGt1VpICIRl+qxiXRLo3MqBu/IdXPcwB/4gzh62RxKP9F3MT9X o5VXrmwpo1Clr8JfxvWfMQ6GJWJYWsJOMSAh6KNmnBxIzIl37yx4Fs6DK+ZuNgFPEpmvhtySzIbUL/ TJpsCpgZz/4OHFa9A7PzEz2Tq814hlNUsOTmZQkQx325N2ae/vvITBLY23uJO+aahDKcQVA07rZ+eY ang7iLckmiq9U7DyZoqRnPenQWk2FJDd7J4C1S3FlSemckFsAGHh9NMNjfKMvoumsXGlllPRjnpLGW 83izKsV3BUDke49LWSAWVH1z0LajVQ 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 Mon Aug 19 19:24:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13768886 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 933541DC460; Mon, 19 Aug 2024 19:26: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=1724095580; cv=none; b=Deo5XHeB1u3p4Gw5Jzy9nmJKaukAKKqpAig1SHeqZsM5zfdd3EZnZ1xhiUyVuVuQqU65IAueJAH1lwJMlDVLs5b6gy3uvqo+uHgu2fn2ZSSUP69MzbCRL59vLKeENDt7G5LmfoerRgVvqn1lQGFdVM8TexJqTojLioIqM4B2Yhc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724095580; c=relaxed/simple; bh=Lqc0aeXK2Sje7x0iWLcH10niGKrB2vEXzwnzDeVxcmI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=K2IpSlZzeuOSEc8gC7+tQI3VWmb8HZldmfnYhvM59Ct01Kow3RvLcLlZYPKDGkPpLvqqgWYSRFPtTxm2Akcttcgftx+1GKpbVA9gUINNCvi6+TjuH4EK6vZqMFklbdr4UWwWmkD5msE8yPurFL39qc93O07ralMK3R6afo6Pw6w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=obVteLeR; 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="obVteLeR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4ED8DC4AF11; Mon, 19 Aug 2024 19:26:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724095580; bh=Lqc0aeXK2Sje7x0iWLcH10niGKrB2vEXzwnzDeVxcmI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=obVteLeR82Cbh93xRjPXJ/xQ7Pyy3moJcsPKo34kRZMYbC7llmgU2M2oHW0a4Nscj moWrXHTn2xyQhPtDlW+zvu4p36aIhR15MGoMsXahVBU9RXkn8QlUE9juZoKygZoGkL Yq13FSMFIuYj4le+EYleCkB7r1plNaZvc1VSYmgKXxcDfVJnc+u26gyN98fH4OCtPF n0Krt3sWt9DUui9IbnYQtduTscWJNW48RKqZK9ljU863RVcuWdfUjHoxhOuxuF8kpW dbkXcUBiKCoH9WSa337mOpsvYyhM1MtMARmNwmbzQrdd/H5lcWQB0XJtQunsqfQHCA x9iA2/pXor6uA== From: Mark Brown Date: Mon, 19 Aug 2024 20:24:23 +0100 Subject: [PATCH RFT v9 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: <20240819-clone3-shadow-stack-v9-2-962d74f99464@kernel.org> References: <20240819-clone3-shadow-stack-v9-0-962d74f99464@kernel.org> In-Reply-To: <20240819-clone3-shadow-stack-v9-0-962d74f99464@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=2722; i=broonie@kernel.org; h=from:subject:message-id; bh=Lqc0aeXK2Sje7x0iWLcH10niGKrB2vEXzwnzDeVxcmI=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmw5xDXYN5rGkvlse/fXHMaeqJyBQjQ2OzR+hrZDva j9xAwvaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsOcQwAKCRAk1otyXVSH0FW5B/ sEdb+/3/C6mVHojOK22Umc+jVhiURJrDocmcsxvafRk+YK7TJ6ZbUzPsibtL+/ToD8Swc+fRF15aXS 4Ku2BQn/eeMVF7YBVGA3ce4tTCNXx+meX0wTE/FrlIhk0mF3XpglJFsNUyhPnbNW5F2Vi29b+/NWpK t0DT07vlmlSc5qjM9UX0uVHTrwCom62NmeWK1ajzKUgiEjgwzh3E9IYri5ZM9NBh7xjrytCxyDrgeC iR3e+JX4W+cTpzK4M+L20dA1hSrPxV2M7CWAhQXD4UmPx/djYMSx3wCNlvoaCsLjpGjBfuy4PQSSiK VzgXFCVqlwpg6Ju1bWINMc42Z/Me29 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 | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tools/testing/selftests/ksft_shstk.h b/tools/testing/selftests/ksft_shstk.h new file mode 100644 index 000000000000..85d0747c1802 --- /dev/null +++ b/tools/testing/selftests/ksft_shstk.h @@ -0,0 +1,63 @@ +/* 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 Mon Aug 19 19:24:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13768887 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 3266D1DC472; Mon, 19 Aug 2024 19:26:26 +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=1724095587; cv=none; b=JfvZHdWLzU+Y+M5XgHSgm8I2dyv1hu4hHJrxnQg7ZQQk0N2N+MWZxoH3fC34kkxA+djRPk2h1l94c+KRcKyNyb4r+1Sh7WBhmyTy5Meg8adrZ5s5IfGrsSQtN1GXZQHhSd2FOPf2buVwUv0+iSSnBHpdRvJZ/uviXDmLtKhnupk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724095587; c=relaxed/simple; bh=aXGeSW3grtLdSnTdx5+gSzbAczg0duMe8mDGJOGSfWQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Dpv5SCgg9/HYDiTv8AnvsB/3YNFFp3nC8spMclruHXiKlYV7QovkAETU9jrXagbD6uNL9ySMOmRdxmppB5MtQ9qbKnGfFbxe3kEi1r/DaSubnbJKtVFjsewS8xqxq2xLUT/bVsjc7fa8jbh3G9d7yX4Uc5Mgl7FSXPgLbZjCSRo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Rd4C6R14; 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="Rd4C6R14" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7DE48C32782; Mon, 19 Aug 2024 19:26:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724095586; bh=aXGeSW3grtLdSnTdx5+gSzbAczg0duMe8mDGJOGSfWQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Rd4C6R14skBr2RVjD+O1OmP4EBeK7xQUQ7zeyPiSllKC5WvXA3aRpdfcBA6gqzo2W wNwP5P0ay2oJ/8wDLZXMj+jQ5pr0gpUllyLzVkVdWAa8yT9ySHeuS+twdi53D0Q/ro YYocL2lRTrvFZWechjbXEP3aNwVuVMuzBkfowu+SQnUUJK1sfqm1cL4CZxR9EYRfS2 shTGNqfGeTaMLYNRspkzI9nymPoGwiRHjlsSPWKAB+T+TbYzwWCg2lJohgqjt5Dcta 16ToXfGKn5lVUg1qut6O0GNRnWhbVSUePNvrKFIc/MmQpj5MBQCqW63RxNmIicSLFO dNU6Pj7NmXfVw== From: Mark Brown Date: Mon, 19 Aug 2024 20:24:24 +0100 Subject: [PATCH RFT v9 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: <20240819-clone3-shadow-stack-v9-3-962d74f99464@kernel.org> References: <20240819-clone3-shadow-stack-v9-0-962d74f99464@kernel.org> In-Reply-To: <20240819-clone3-shadow-stack-v9-0-962d74f99464@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/pANAwAKASTWi3JdVIfQAcsmYgBmw5xErfFb8uONK8Tu4hC9CX4D4YDjcDcyOx9eluD1 i0BbB/+JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsOcRAAKCRAk1otyXVSH0CtNB/ 0caOlpo1xwqfHAFBQsdbD1OvYyCrdARg02XBPV65LoK6zEPBCmDOIKyXtu04AjNqtaqDYgOKP/7/MA I6FHFbMnTWZBqH7da12ttE+GnDcLOXuMBFzKxmBBHB5TD4/PIFSeJnhcpvTQU6+Orppqqbi+ZMa3av YCyqWSCaA20CzQXRopsezRH0hZCIXP2Jx+Rj006BuHl637WU3S5uB4hO8JQHGExvl6GDZYBwgxOHYt asBkwv8mkPTZIyKxfKNZMey5vVjrtGb4gqxbPx/it62qKxEsL6IHdOxqWVjjrIjIPyopittdmN5Nd7 8x9eAONMsPj8vpYt5Lj35R6HAsXcLK 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 Mon Aug 19 19:24:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13768888 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 2E56E1DC484; Mon, 19 Aug 2024 19:26:32 +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=1724095593; cv=none; b=trObMTnZXN4T++F8XD6lcB93fsROH/iOcn0BnOJ+sFkbL2PIlRYOwcMZ0ukbNGJlzAfQpxTQbJOAheqy0xOWzZFLj2R80uA5vcKZ6iIQrzs0CSDAO8M1tpzgD+hbYzRPvbreOj/IGSFUyXJJKodk2ejpge1IvqUzUMm8e9mSsko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724095593; c=relaxed/simple; bh=Ivhu6148TvgC23QrzNVW4IRdU6BsT/SWPb/mP7ICcGc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dFl1p67zgT5tKWE8vconNxJWzgWrtMRd4hhsypTJwLdEqU5wN8eVQfCpazkV0OJ4a44i60XyV7JGQkw1nm/fdoHGllfjbD4VP+GxA8euPYUf9s1I1KAFs0UtBFszFmKPc8pC1rlVcq4iUAZloiEMuH+lny1DXLQCe7isPFbhbTU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Bq96IITQ; 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="Bq96IITQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0CF3CC4AF0F; Mon, 19 Aug 2024 19:26:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724095592; bh=Ivhu6148TvgC23QrzNVW4IRdU6BsT/SWPb/mP7ICcGc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Bq96IITQE3yioxJPmU3JIkz2OpwqV7DM8ltHOhtcwdVioTMF72w4iCPLv3HP2TSKZ 1n0qYvo59dzCo44HF3/+QuBbiIX7+S6eirocRx9BotrhN8Y0HHOJETyLSOEIesXWKR EdOaCCMbOr5tHZHzoUUV3tDwcafcqT3U0oCAN2dCruomoM9WkfiupSn/1TElg/13j2 ET/wfby7mGX7cSpWB4l03gnp0x56zGkt0GqP5iYEZkeYDD9DP4PNL909GAn35S7Bp8 i1m73f3BxgXL3YpslGIXaCJAYOMFhJGoV8ZZ3+qgIiKFyxkGpO7dOmcuob/tEd4Jr8 4734zkixeVy/A== From: Mark Brown Date: Mon, 19 Aug 2024 20:24:25 +0100 Subject: [PATCH RFT v9 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: <20240819-clone3-shadow-stack-v9-4-962d74f99464@kernel.org> References: <20240819-clone3-shadow-stack-v9-0-962d74f99464@kernel.org> In-Reply-To: <20240819-clone3-shadow-stack-v9-0-962d74f99464@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=15541; i=broonie@kernel.org; h=from:subject:message-id; bh=Ivhu6148TvgC23QrzNVW4IRdU6BsT/SWPb/mP7ICcGc=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmw5xF7PQRGird/YKIl8XJFVLDpGOnto3cqauIOlye 222zt1iJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsOcRQAKCRAk1otyXVSH0FbuB/ 99zBXFDzMAV7NNEiZnzWrU0euKbRZ3YEMldcnjqlcy4qxfpHV4BJjH5ad1KX/kgnFMH1+Gua8g8pwa jWI/JM8vgOEcavsbx67bYxUm91yP8jifP1idJ3lu6XMbJbecONk+C1ckee7ipKKDszpffBDosAsrOJ sZcrFYlrb3VyBbOs6nGJDNCKAkEpPVk4cwTxtbRi3s5zbkby1jA85tBRcXvn0IpRUoxOcFIwSeB4Jl dM95I8TWkTisTxXRmpOCOWFhxwXf2bzr4WUijVXdblw1+NHzITfsDiqATUtnvQGsL/z/jh+ncdLP/Q 99+bqCm7cv3tfIgwmJt7xxlz4uRL9Z 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. At present this implementation does not consume the shadow stack token atomically as would be desirable, it uses a separate read and write. Signed-off-by: Mark Brown --- arch/x86/include/asm/shstk.h | 11 +++-- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/shstk.c | 103 +++++++++++++++++++++++++++++--------- include/linux/sched/task.h | 18 +++++++ include/uapi/linux/sched.h | 13 +++-- kernel/fork.c | 114 ++++++++++++++++++++++++++++++++++++++----- 6 files changed, 219 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..42b2b18de20d 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -191,44 +191,103 @@ 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; + u64 val; + + 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(ssp); + + /* This should really be an atomic cmpxchg. It is not. */ + copy_from_user_page(vma, page, addr, &val, maddr + offset, + sizeof(val)); + + if (val != expected) + return false; + val = 0; + + copy_to_user_page(vma, page, addr, maddr + offset, &val, sizeof(val)); + 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/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..275d8cf3f66b 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,56 @@ 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)) { + 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 +2436,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 +2997,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 +3032,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 +3084,34 @@ 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; + + /* + * 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 +3134,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 Mon Aug 19 19:24:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13768889 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 1133D1DC48E; Mon, 19 Aug 2024 19:26:39 +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=1724095599; cv=none; b=SfV+q0uKh+MSZkiyCab2LQrpormgP/sIeLYOFoOF/qcpAmhg8HGfG6GcWCIXW9Eyz0vO1wGBJHHM3SB/ZHqSwDohvR9KvkOZX4GVMJGAYL2nUJ10uaVt10fQEoDAMSrVDAsBghsro9gGkrimy2NYuSk9IsJn/OsgbDqbWaBJomA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724095599; c=relaxed/simple; bh=j9ob7ZYoLp4hP0MnrgcOUFxUwkxx358xO3rY/g4W29c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qglM7Q1ftRB0FsClRoUgFWWixPMRWQYhl4UAVDX3om6d/3myJSm6B8GNx4BoCOU7xTAHXuggdhmzSF6DmqjXwxXpql0W9UoJj6DPFut3OIXwvilZUbrTfHJpXHh0DLQg/P63jLvMuDs37Yxeljx4hTgXqEKmzzM3WklUcs1fnwk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ct2gCKg6; 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="Ct2gCKg6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1ECC8C4AF14; Mon, 19 Aug 2024 19:26:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724095598; bh=j9ob7ZYoLp4hP0MnrgcOUFxUwkxx358xO3rY/g4W29c=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Ct2gCKg69IGce57qBJiGSp+85i7msM6BmHmvMaTzQ09NrIxuigsS0FrV5QSl8Ne6r YdU+n7mGrkTDcEBEfNr7e3fLqtTX9JPxILoMjGnvfjaPJ2LI0ByhcotqZuCVDfLh+D 8Rjo0N+s7mFIHDpV+PFtD+NZKzwH7ZH3p0Sdj1lP4uAnkzJLcCQb0lbndoeLYEIM1k fnJv8GlQraEZkBobUOBaXI5z8W7yL9sZtUoeew6KwaVUPnB2BJLfPvtfckisuVnsCE /HK1w/H5QL8+ANiBj8CmAF+0toYfhZq/6qe5VWblRnlPXCQfA9z6JQTneHkyfKC/Qg V5e5wltDaBHJw== From: Mark Brown Date: Mon, 19 Aug 2024 20:24:26 +0100 Subject: [PATCH RFT v9 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: <20240819-clone3-shadow-stack-v9-5-962d74f99464@kernel.org> References: <20240819-clone3-shadow-stack-v9-0-962d74f99464@kernel.org> In-Reply-To: <20240819-clone3-shadow-stack-v9-0-962d74f99464@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/pANAwAKASTWi3JdVIfQAcsmYgBmw5xGTQsGFnnsXkzj9iyPYSkbO7zgaru1OyjNN0ln fdOTBH+JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsOcRgAKCRAk1otyXVSH0KSVB/ 91+3bGW9gkcum5wjok8hv+EPZDZx0MgWGLJcZEcI1Y85ek4duInhVz+alA4WqQPQPKwdd7c5gwGrKi eBnOOppXivd5IHS5G7vxTpHUQrCeUXJBNbpycQ/oop6yHb6nshnKjbgcBCVfbxSQmCKKH/o3Ql3zaW WmEeNuYbV1DuocsU8yDxcYniiC6CIMIySJXfP4ERdo46s1S2l1Kmw4eMYUi7dH8dIwtjbi0vbSaag6 ryN/kuS3Ncg/pAMclIjITW5XqA524+zzCWqmkjivdvy1TTF/4RKvPDgw7IvI3HGhqw4oq7uFEw7a5o Jiu928fQSbnzYx6lT653nnHCpHhgh8 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 Mon Aug 19 19:24:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13768890 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 A55A41DC495; Mon, 19 Aug 2024 19:26: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=1724095605; cv=none; b=tq9/6SsEciB0VkMMifgOyVt4gkmvOBkLrKQFANhLKr59bTfctnfPCexRd3+ejltpKIWzm60rjt6rgYsciCwdbwM80+LagvH9IGFqAjFaNMViwPvxsrxE8IvTuMEQhjfX9q8lg1zAN/EC8aNmAJ9a5hP04NZa9bqOEMhTbA0Jcj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724095605; c=relaxed/simple; bh=XIYOtX/wTMGB1zAIVkW11px29AlArl5B/1MgdpVsr7Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=czl9tHdYh2TVfyJCZA6jZZC7WxopDgnk5kp+OmNaBOXxGt62HmQPKaTmv5HwNh0inLOufqEF9x2ArkAxXUTvYv6zOsJp2fXzKEiyIxrNCThreszvsa4FRKF/i6jDrwLajuMqgg54FZs8s4kGISQB1bg5zNmhoUwki4uxmvYNSNc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=vL4iRbuY; 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="vL4iRbuY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54234C4AF09; Mon, 19 Aug 2024 19:26:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724095605; bh=XIYOtX/wTMGB1zAIVkW11px29AlArl5B/1MgdpVsr7Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=vL4iRbuYuTdai+JcgUOXr6gRTtOEFncirWf8KHi39lgA7firFVuHp6kT+NoGZ9l/W OwHefl6dHOmGa+c1eLkghyiSO0elIVfwicKHf9tkcD43leP5rha9q29LA1quDpau1o dVFUYvZh2K3QToX/24vqJ2v6Bg0+XqB2M/gotCqsSzbBiBC2KsTzoxLhOks9wgmaG3 7ID6j1S1mGmgHrBaAH/7w86T8Ruarmqmp3STOF6l5fxPT/QtMbl+eu673sAaWqSG6r xXsz16ZMTYDiK2+QadPxjNdY+IKVmUWXwLjpJ1Epeu2KvCBmc+WiY22/06KX1gC5dw zzjENZgefouPQ== From: Mark Brown Date: Mon, 19 Aug 2024 20:24:27 +0100 Subject: [PATCH RFT v9 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: <20240819-clone3-shadow-stack-v9-6-962d74f99464@kernel.org> References: <20240819-clone3-shadow-stack-v9-0-962d74f99464@kernel.org> In-Reply-To: <20240819-clone3-shadow-stack-v9-0-962d74f99464@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/pANAwAKASTWi3JdVIfQAcsmYgBmw5xGxt8Plx6+f1eSDN/4zPRXIUOSUPGpRnadoI6o oy2SbnWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsOcRgAKCRAk1otyXVSH0CIgB/ wLMRyQX+O2b5yRn8Df19/74ahLQqXTz8ay0N9kh/B8hFRrZ5d4MlUQn4ItKr/wGunwl/TKspR5RrIm 0dW6Z7iKlAu60ti9D7LJywgAGNeJ69V935r9KhJeP2XHe9TUMMi/C1m1rDuAnKbUQh4OSdiOkBOpUo rw8u4FFbEDyaPFcWVMW7sVRLHYAfn7SDIp9Ugvlkma1KlQkrjZy2tAeHG9CrAdv/xADFY40h8js7uO PqO0BJ3f4y00OF361fWILHwL/x1Uz8H9UkXJwAJmsK4eY4OCApPMyg6McVP9dTBRnJ9Woabp9b81i3 9Fu0mXb3nxfWL5TeHcv4GTZnPQaMj1 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 Mon Aug 19 19:24:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13768891 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 C67B11DC495; Mon, 19 Aug 2024 19:26: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=1724095611; cv=none; b=SY0MZ4nYy1LIRpyMKcHmbZUWf4ljycDid0EnpBde31Xa0dtyGHjAlbGciycjZ0RKZvnovTkHYqGLdr+eDbhzJt3TCAtsTIHTxpCltoQaC3QMABrB6BYwgPHkAI1HqfXkwSHICH5+q9uSkZK0+wF1dxX5tdUbiGkt2ZSfcWQNK6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724095611; c=relaxed/simple; bh=2JIySYLy5Zx4h3HBoI1rUprWAIQuQf3PAf28WNSsOzU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Xn4EoZIgk9BmXqxMw3sgW8+kEa2jZ2a7zaxnvzGm1UAYxZeqQ778cOAO3vm8kw81qjoZRwznKja/5TwGJ57F0qYbyzIN5Sr3uxm2yPoKVAqQzs37QY3v/0uJDYOttsij+WSyFYBdFyUHxhbV1pf2YJwUiGvu0u/L9Sbf4Mvc5oI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UmQEAi85; 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="UmQEAi85" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D69CC32782; Mon, 19 Aug 2024 19:26:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724095611; bh=2JIySYLy5Zx4h3HBoI1rUprWAIQuQf3PAf28WNSsOzU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=UmQEAi85HOG8xPxY1ajE0Eh+r5Q2W9Bybb5uF8yIbJRlPriOVS0cqkZ/dA9qZFImt IH/cM9rtxNsQd+faaIz/oBu/7ugy7fAYxJwSilhKH+hTZD2qbTqqjTaLvK4mfaAmoJ oqBXn5/WDW87xVthbfs0pnhb+70FFcTTN9QyZ4WwWDL3slPcJm4J1SQFHFPpjYDJ4/ hkVy/RODl5UxqWA3EVuLf60m4G70KhNZPaZDoZz31TPrbOcWaOeWZkNXeRUIImkzei X4JtXwxhAiHfSW6CFSJEC67pkC0trRQGxs/Z4yLM2sVV36dDQGEqOAynhY5fUmij1c HieeUc4Rb/CWg== From: Mark Brown Date: Mon, 19 Aug 2024 20:24:28 +0100 Subject: [PATCH RFT v9 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: <20240819-clone3-shadow-stack-v9-7-962d74f99464@kernel.org> References: <20240819-clone3-shadow-stack-v9-0-962d74f99464@kernel.org> In-Reply-To: <20240819-clone3-shadow-stack-v9-0-962d74f99464@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/pANAwAKASTWi3JdVIfQAcsmYgBmw5xH6zvJ3/IIuidECJVeszRpi1MgxvvTIvjEbc4j AWfNUTCJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsOcRwAKCRAk1otyXVSH0G+AB/ 4ro9bm570C7JGnCmzFAWBy4xxRRUoMPzf0Xl+xZlRZ0eUz5T/NB3iKDPaioEVkh6E7Y3SjN+6m57Lh t41CgRp96n/2Ok9Z/4oUwOSd8ve1cQ6uidYZWFIKtb6yRbTeOh7y9VS7NHMjAJSsSChQiCUSWhiA8/ DgShaEmv7PWiJUlIIk0650c5l3wxbZXKZc2seGyuc36qC9m64iP7EiG7VBAcE/gP/aoF4l1yZq5Fla m4eIKbtr4CSuI+vgRHxBwfXX+l+ECTMFYwD5n3Pym3eoaTbiwIAM4FSBb+H+jy4nR02217Q8rx26iF b3lvv8nRX7RvOw1Tm2XTPZ7ISESUsq 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 Mon Aug 19 19:24:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13768892 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 A694E1DC478; Mon, 19 Aug 2024 19:26:57 +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=1724095617; cv=none; b=S/L2rPPMvAcMYIobU+Bi1gumvCc7Mb/omvYniVfGwP+FudWL6G/W2YyumX4KbOL9u2oxty0YCqdHX4JxSuZgoeQkfzGIfKHzDBCSxBHPRTjMBr8xL86aYIDtUxRSQrBff1dSKU4/3dmiSfBZ3Fbn0oaZHrRAsSbucSGvHxe4OsA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724095617; c=relaxed/simple; bh=Mle6ZIN9OAB76YXNoQ8OFM3hxrRxvGOd7qYVVyxbwl4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=E4i22ThXybAbPss6rnqhOlFcZeDSNwqSbxsiDUI8Vz0mPOmuostdQynbGbN0+pV/TTMSgmJfp9IKsrXi70B/DAwXvwOOddD52JcWaLinYRK2blmokDoyEyzZC8mfclvjdQIOw0w2peuYQcUIIQiVVGLq7SNPGKkcAjIFJC5N+is= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bV+Qsm9z; 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="bV+Qsm9z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BD22EC4AF0E; Mon, 19 Aug 2024 19:26:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724095617; bh=Mle6ZIN9OAB76YXNoQ8OFM3hxrRxvGOd7qYVVyxbwl4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bV+Qsm9zdtrWHAosWgNNFxNqiEU6ZeC+xjozizud74Cm4NhIfmLBqkHsM83+g8AvS 5xGctu3LWcC5SjP3zeRRu2znthNf1WyBWtEgAT3dyuDMSRfbKiLfS4q4rNa6pgfT9q 6rF5O358btGhgLJ1D407uw+9g0tva/1ztJ0mQ1x36cWyq4Q0MKUnm4ShbYxN9N0Vbd zaXv21uhByHpy511XDIb/k6vuBRb/rLHheX/Uf0EL+XAAmvyEZAlO3jXf/j63eBCyB 37/Ne8VSZudTVfZknxmKPeJAUGcsf+ivoCrRwZH5oN7cp5gAx9sUM1rg5sC0myJSYi 6HWOx1+TQ9sUA== From: Mark Brown Date: Mon, 19 Aug 2024 20:24:29 +0100 Subject: [PATCH RFT v9 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: <20240819-clone3-shadow-stack-v9-8-962d74f99464@kernel.org> References: <20240819-clone3-shadow-stack-v9-0-962d74f99464@kernel.org> In-Reply-To: <20240819-clone3-shadow-stack-v9-0-962d74f99464@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=9434; i=broonie@kernel.org; h=from:subject:message-id; bh=Mle6ZIN9OAB76YXNoQ8OFM3hxrRxvGOd7qYVVyxbwl4=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmw5xI2QW+aUIYQ0W3jU4jZglrqZmZHYoHKndQvApo Q1NO/KaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZsOcSAAKCRAk1otyXVSH0E3IB/ sG7NZiQ22NFTe8b1fNsEPkeXrWjY30QyUwpre+XQ/udi9rFzn1UW2J+7tWZYln7ldcyUXov3G6FiTq xkgaRu+7zuabauxM0JrAlPICKNKyXgSl8IZ26NczmV6exkk/VpGlTFghpIG9X+/8T10XEKrUNOu4Lb PQ4RwaolxHHJc11Hz+I9YRdsceIyj/g31DQfGGtLCGvq96A716ceLSZbzs/l9AUlZkufPfqFmQImJa k8+rkRN8wePAu7YPj3D3fhUqI50wDskaSEH2mkHFmBp9MuguKprLbhjic1p8dHBWTbRHgcKCvQlnv9 CPRxE/p4Ai1udVWFvvs1Ids3Wxb0u7 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 | 147 +++++++++++++++++++++- tools/testing/selftests/clone3/clone3_selftests.h | 38 ++++++ 2 files changed, 184 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index 5b8b7d640e70..f64fe5cd701d 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,11 @@ 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_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 +55,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 +138,24 @@ 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_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 +169,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 +256,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 +419,56 @@ 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 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 +476,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) {