From patchwork Sat Feb 3 00:04:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 196104 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769729dyc; Fri, 2 Feb 2024 16:11:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IEI8tJ50CxlkRoMhOVy7UHHB2fcGWL0oSP8B7YYGICc1OYwxnc7MZ5oOl8ynwGHyA3RgFVA X-Received: by 2002:a19:5f03:0:b0:511:29ee:83b8 with SMTP id t3-20020a195f03000000b0051129ee83b8mr4270379lfb.62.1706919089682; Fri, 02 Feb 2024 16:11:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919089; cv=pass; d=google.com; s=arc-20160816; b=XyttwHz6hf9RdVvwXXcRl4YkbSCwhzMwwkx9wY1C5Pw5jSzEAU7Va4vx4jN86e9n64 TPZay2IAL7ynEydP0kg96BCjJmc+WErkO7qYUkIy71aupQSl/gqIfc5Wh85Vvqmu0Ml1 M58J9WrKsisOG4bmrey8K3xsGa8uvxzuJfDZUOzGs5HEwr580WmIrwK7JzykzpvC4TIO 0JXwcpjfQqK/t9tluwIyK9+wqjPe2CSiiMtBaHAR+N7GwJKGAkuyMLnzDR5MzW6yG99W Bpjb1Nuw4UyQrBy49W1tjG//eQ0xjhpB/u+wJdwUfEhci5SGlanbHsVzCY1JFsGoH59n PxXg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=JYU7zNZEc0jNQqCYpKbZ1WdssjBATpThCfL0cMdaGDM=; fh=jtFUmLWqp60EP7g4eF4lrHD3qa1zuhKrSdT9An4K1lA=; b=BKRhRF6yQBJNDndZhJrBG6PfNxrwJqApjLq6J/Cf7q7Bz7yXthTh0psddHHv5aJt75 Szga02J5IZHkV8rjTk9buJKdLdB6Z5mMm+4s5Eh5Un/+QrsNNKJNI0p9MG1HC1GQ7Opd PClgPaMY0z284IrF9v4Zv4vkUMgFW1djiXD6Zb3Drb4+1Y3sSXUOxIhifYKSvpSh3Jgr c8kS9pWfkX/x+CddFsolKytLpfSBawH1pkoMfHp5euD0KrmbrCaBnkfIPl+H85g/4btS KuN/tZ8C3FT2PwclKFZu9WfOi7OxLPM+VyOnA0t6QoyUC/843dGOzImtksXiCAEAyYll mJDg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=quybCE13; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50739-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50739-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCWrLRv6Wg7dQ88KOyxURGAQg5wsi0PY5u9Vl+TH9Y16aSB0i5hK1AixFY3gTCLJ0fZ9uyPRdp546Ltcwx0o1s+Hio6TlQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id v23-20020a1709064e9700b00a374a05f1d2si72803eju.371.2024.02.02.16.11.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:11:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50739-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=quybCE13; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50739-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50739-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 50FC11F2AAAD for ; Sat, 3 Feb 2024 00:06:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EF08E33C9; Sat, 3 Feb 2024 00:05:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="quybCE13" 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 0D1A22C9A; Sat, 3 Feb 2024 00:05:30 +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=1706918731; cv=none; b=tJkx8rTqV4Bv37TmnTjpZOsYKxG4tJDwX1AWDysjxWqCeE1dhDv+ZPHCs/5Qm16T5YxapPApxLBFfj0XU7c53Qn4lIZIsx7Ea7TIlPn+rNytO7MYWtTX5ZjUBhUk6WcZPocSYGIL+BYaZu1VDGuqzSPJ1R08JbBVor/ySxwG35I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918731; c=relaxed/simple; bh=+CKBuAS1U0g6d+plSKonOkK5ibGpCzW6nF3Z/9khEN8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ChsKEgAqkxbYCcPdAHmDN0Ko4klJXFzZRsdqVY4Ydx0bWrhcoVcHe15ReH92bctL5eiq7T1anJYg3yGXVUZnf4UwitP59IV4vOHkPQJ7NeRT9J4rNL56fD29Ml6p35derTYVjlJIjqHG1cGQBKGh26amgfprAObsXmpcwJ/BeQ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=quybCE13; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8BD5C433F1; Sat, 3 Feb 2024 00:05:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706918730; bh=+CKBuAS1U0g6d+plSKonOkK5ibGpCzW6nF3Z/9khEN8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=quybCE139kulUgOUUZjY60sg/CxCkpadcp7ma3i/0fJ6wBtmhbyOzaZriSCPxLHn3 T1GSx0+JbDJon40BkHC0YAv3EydPOs9/Dt4jb/BSb+Svny03ArFWdWL+hexE6cwgdX UPGofI+FnjVB0/y9eZORoXham4OmvoqJiQ/Af6y8nsbWK/RQsN9g7LwSUUW0EgUKaG sddnyDiK7vLa6Jp2JWYIzUFMOqdnXEbE1N/70xKYIyxrz+EpOHhi2tTZpni9N++rA3 QOldOTbqRGmv5PQpUzVXIT3hDN/VYjbr4nS0k87kPUVzVno3c/3h5OgtiCURGHYV9y ebD53AvWiT1lg== From: Mark Brown Date: Sat, 03 Feb 2024 00:04:57 +0000 Subject: [PATCH RFT v5 1/7] Documentation: userspace-api: Add shadow stack API documentation Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-clone3-shadow-stack-v5-1-322c69598e4b@kernel.org> References: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> In-Reply-To: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@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 , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=2879; i=broonie@kernel.org; h=from:subject:message-id; bh=+CKBuAS1U0g6d+plSKonOkK5ibGpCzW6nF3Z/9khEN8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvYM4PfeqziRGhW4IOCIb95VJvN7t5rCF8sHAO12V PtiRT4uJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb2DOAAKCRAk1otyXVSH0OcVCA CC6svYp+IuzT1RotcWcIvb3o558FjYjpMZ9P/76i+BXyygGOGbC+MetefOydO4RvlDTVNOefQ+HtHj UO+s6V3pE0jwJIbLFR/4m9XvCAWcD/+jJdapcCWp/ruR8ynxHXDPYzh2xm09rfixLaRzeAOjcIAxBD xL0Rto4cD/372AyJxEMKu5YEKTfzo5z5Yaiptukfw3eVkZOzft2M0hoTl1N9SussHL9XEA+1d7WEnv /RvOdN1DT0plk1mCJVO41YdyMIu1//ZrsfYsP6UGsqcpCDtBFdU09Gq9DoY096D9bre26gkg6PdK/Z +1vpAgD5vIv6jerTf6qByqgxistU3S X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834391256437840 X-GMAIL-MSGID: 1789834391256437840 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. 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 09f61bd2ac2e..c142183d9c98 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -27,6 +27,7 @@ place where this information is gathered. iommufd media/index 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..c6e5ab795b60 --- /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 +themsleves 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. +- This that change the stack context like longjmp() or use of ucontext + changes on signal return will need support from libc. From patchwork Sat Feb 3 00:04:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 196113 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp771789dyc; Fri, 2 Feb 2024 16:17:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IG584H/a3QbkJQNX9wbNDEDUStdoDjzexLxSfWt4zwP0x7JR0E9vk7ZSCxSX/UidSM/7ZeF X-Received: by 2002:aa7:d494:0:b0:560:463:6f35 with SMTP id b20-20020aa7d494000000b0056004636f35mr674971edr.10.1706919426619; Fri, 02 Feb 2024 16:17:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919426; cv=pass; d=google.com; s=arc-20160816; b=KvXTKeobf/oB5eYKVuV0Upz7FTmLQrvpbU28VJlKHjfYtGUarxTx1YOlnYRkTEg3k7 fQxKkdrPud1z/xpIxx5U/LAwXl85Wrwhoi6U6JrdCdrY5XvTvpCWhk2lNHixZfGfU3hc RzkVnaL/gcnAnmryvqBillpya/Wm+WRDTeoNGWYVpFApR+wu5mK/fqeu7/FqNsbV4mLC vzXxvSFxbxn8aOcQ2Y1hyqvgDz+X51O6UVDxx0+6ptR+dffzlJCafdgWzJD6OwxknPXx H5Ym5whV/hfoiZKDj9TGgHkn2d+Swsl8Px9YpDbxb3Y8zAuKKFePuiWAlw/p7q5MJKE5 aGFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=ZUGGnLCx0CrON6JfEIDV0WoI0FeeuEVvFoTR/gd4J/M=; fh=x1G6e7CN+fq516B+/h87Vpoz0TDLRe1LD0gNZ1qLQgA=; b=JstsJ810EYrX4WCTOCMRT8l9NbB/tU441aH09t/582FI542TEeh8zo8W6CWanOpI0O cji2v6tiI7lEN7GbItDYKKfc0N5rIDXYH8Zst9Y4Eq6/p93VOI9vrh8gDBZXOpWJ29Wm 77Ng3U4dZ0x0O2G0aLs6/u8lOeHRBFGuIZ9YjnmEOAIph+zNcDd4zsqxChQhJE/tMhyq ZCmrAKlRNQqeXZD0xHKTVZ54KZRlolcpuCpDTfYCo6NMAZpkMB7EHSkMFFhCrcCgAusI wZX2AkXSTnxVnxXOERZrSdK3uYhEbA1ezn1Z/V7bMooEiOvHbqMHXhx0321TEvkKdXhR UI7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rc8YHIgG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCVkmZB+zPFgtEaC848w2izktYPHrcfzRu9OFLBvlhE6Gi7j/wrfNe3gcAYw5xNGX++c7bP62X+BogrBcz5Hc0YNIiHzTQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a12-20020aa7d74c000000b0055fec51fafdsi1136917eds.646.2024.02.02.16.17.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:17:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rc8YHIgG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 1E6CA1F2AE3F for ; Sat, 3 Feb 2024 00:06:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8FB55680; Sat, 3 Feb 2024 00:05:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rc8YHIgG" 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 14460538D; Sat, 3 Feb 2024 00:05:36 +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=1706918737; cv=none; b=Yt0PBukC9WFGvLRKsS2v6oZIXFtI3iEulvNBdELlIJD9/jgcLh4pyzeei3bc5XgXWtKJcsFZOHi6RL9D0odjLIYdbk2KT1xaoPoviyiOTuqtnaLZob0knH+0yh8a4zKB/6PzHXAyrKwz3sd+BTQjEwj8J7iRkaRZWX4LsKZwvK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918737; c=relaxed/simple; bh=CucuMui4D2UywYMkY4QvFT3PXHDtuX4p82gJW7uHGUw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZcRzjMYJgri78UX5H8nq9I6rsYg84KvadjdvVbU2It8MWfpc+VMBZdl8I36rb9KlpjhwC13r+DdlMT2zXQhfoW1aDQjg2xWThdiUdNXoDe3d+yeH8VlXyUgnrDfkXckvILY0zMfecSfVDZiLRBkjArG7tTP/vwU+PBA9lA5kynQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rc8YHIgG; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4717C433B2; Sat, 3 Feb 2024 00:05:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706918736; bh=CucuMui4D2UywYMkY4QvFT3PXHDtuX4p82gJW7uHGUw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rc8YHIgGmt3wOb/Joq6gRhlKp+Wmv3UhiJJKB6CMYX6xon6hoPtksJ6gHmhJIF3v/ 0i+yP2irL0QWfD/KhjdpFfyzQ/AZRR3UOLMLN3t5ranMSazDmRnHK7ecYEZpLXSU6M R3AuGfNWkMw1Bguiwbuz/QYzGlmoFLYVdVp9MCAiODwLPKcRz9465tiZv3pLDIiDf4 zAOEkQSmIdfM6Kd/kIkt6t9WEdbnEZ5Pco3Q8/CWLoAH0w+ZlO5rrtIftA1RRtC8Pg kJJnw0c04imrYu8ZyabBUAnox+vJJMJqZJohXy8JmjHbhNuUFicAqOMSzXlUcD2otB 83tegOSPLZMPQ== From: Mark Brown Date: Sat, 03 Feb 2024 00:04:58 +0000 Subject: [PATCH RFT v5 2/7] selftests: Provide helper header for shadow stack testing Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-clone3-shadow-stack-v5-2-322c69598e4b@kernel.org> References: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> In-Reply-To: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@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 , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=2532; i=broonie@kernel.org; h=from:subject:message-id; bh=CucuMui4D2UywYMkY4QvFT3PXHDtuX4p82gJW7uHGUw=; b=owGbwMvMwMWocq27KDak/QLjabUkhtS9zZYPtCb2voqeGvNkGePn0nauE0o6L+o6O+Z9teqMZJGa t9Gtk9GYhYGRi0FWTJFl7bOMVenhElvnP5r/CmYQKxPIFAYuTgGYyPML7P9UbzaHdov8EHj+u7o3sE KttXrrdV6LNKOuGKOiuVEvVlQWeV5f7O22g7/jDfeKG39YFK82ZC2K8n1leTCvfbvWw+DJeuzyMxlT QrQ0DQ9VnjebujWixHVmmDw3v3rC0vcuEws+701IPNR+/97TcC9l1gn14pcDpMwnX9a9OKe0PJ+h4o WjtcZCmQqnQ3E/twc5fX9VEPrUSH7KJ5n5PmarVnNaBfy991ze4uPi5W4WrArSr7csEUjODzywSDyU e0JHTs6m59PL2otCVbhNlD/tTPxza3aj6qHnyyeYe3UV3ztWWF+TsZtD0bP3Rvm5tBP+IjP4hNZ562 lOVbddahG2sCvgQqmLoecnu5+WAA== X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834744633935717 X-GMAIL-MSGID: 1789834744633935717 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. Signed-off-by: Mark Brown Reviewed-by: Rick Edgecombe --- 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 Sat Feb 3 00:04:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 196097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp767776dyc; Fri, 2 Feb 2024 16:06:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IGUtcuxLbFbcErlNkwvS3H0hcp0mLzduR2jfmx8j1dm26w+IoswbO3caVkdLJnxnBGrUCu0 X-Received: by 2002:a17:902:a387:b0:1d9:435b:a6f3 with SMTP id x7-20020a170902a38700b001d9435ba6f3mr168624pla.26.1706918802054; Fri, 02 Feb 2024 16:06:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706918802; cv=pass; d=google.com; s=arc-20160816; b=qUGUTa0C77eaBqTLKl70qPyPfjOQT8NXpWKBhfR4moI7XWu+sHa/7N3DrhrpaLBYIi kRyRPmMoHaAgysAbRwkDRnFWtNSVBuJ6KRl1880oNAjFU0FNffUpzBe/H6kCMs6cHxJU r4puvwmCtqA8e8G7eejX0OEArxEjAGzFezjcW1h25YMxFG8rbgLhRr9cmw9Hdb7etP/0 TpZS8KMAZjDLDrdrVq6HLnqD9iG2sQMIJP4NcwZsiIw5ThzidX4Ern6s95XJDpLx14uq 74t7PA6gfAjW584+Q7syVYVjK2oCEp0j8TEOkE/B+ssoode1uCxiUVw9zYFfDQ+iGV5i hAhQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=VIviiWLj30ZrQSwedCiUOrNlfExcTLxcukEkZwTYEWM=; fh=lPPIfIsiN/TVdwNhFXhfFCmEulRW43jLg46UfEFGbmA=; b=nHYJZhjOHNZAPp62sk3CHywC813ZQ+7f7EOpY13ZMF9Du8kGCqhDSUc2zm7KsAftgm Df87xrFARIhc75LXfzHuFebL1aCm0UBlgUDp8mydQpL1RMtHaVNh9H0Ul3jtY5VS4wyU RkxF0gjm9YxcaK/AUog/P8trExVUMyBp7mhfakNxM2IgPC1slF1tWLAZyaNdUT0feLPm u3RZ3nJlipqyL0/Z5VU7sMXUvfnHXKXNBkYys/cQ4RAlX34vzpZDxXy16tHfcl13z4kd uCpWrAGGT4d1scHCkXc/uRmdxE9n9Rasy3YBpE2fNSvpNFd0uAuseHJVE/lfqTJTDuCs KDQw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FvjfNJJC; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50741-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50741-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCVuMzD28Bs2ffISQct24ara0dU1VUL2uYpHIIMOt5rcPUJKU3SZn++DCrR/yABgwmC6xIzGtvBIYr01Lz+y704t08kSRQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d5-20020a170902b70500b001d930ef5c1csi2426184pls.47.2024.02.02.16.06.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:06:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50741-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FvjfNJJC; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50741-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50741-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id CE9AB28AF33 for ; Sat, 3 Feb 2024 00:06:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0E7CB6FBE; Sat, 3 Feb 2024 00:05:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FvjfNJJC" 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 526DE6FAD; Sat, 3 Feb 2024 00:05:43 +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=1706918743; cv=none; b=NnJIAnU7BQyPDvbfcAa6IJrHVOslv8BAbIBWezyOx9/m/ipSXdo6P4CD2tHZdnMrHW7MBxKtkJiR28p6sFRdAH22yILmb3siFzVPKfQOhkVFmhnsyE4sxMUNLYvlYQmRaqT7CnQ7kPbNobLV+LXYed4kZAY59vu73cZWbQ/shuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918743; c=relaxed/simple; bh=kiK765H9QFvcPD51NDMimYIoE+CAz43kont3MOS2Ik4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TWQtvxp5/XlatL5F8sAcqJa8mXAZtvepQ90x3BHdot4ZqPZM0nE/XrXRWdL804oKRDwM6djfCoe9n5k2qeZm9UawhZooUohXFL9solBBwdIyGPKOmxnUR/7rs5n6Acj2afzqQwhlVfadfJQcglZmdTuB8tHpwvxGaZdx6GeqqGs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FvjfNJJC; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0FC7BC43394; Sat, 3 Feb 2024 00:05:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706918742; bh=kiK765H9QFvcPD51NDMimYIoE+CAz43kont3MOS2Ik4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=FvjfNJJC6wgrEPZLZSwTVk0ET1J7SKuJvQvF5GDxDbmYKyChIkB8ymouFyiymLkI9 X/owQnCAjnUc4VHI/ma1JXnlVHAbxCLDStdlMLVKfctz0xvYgYXDXcxQriO+1jGrpj pu8wBs+Xva+81RL/HCsD3jrb6VgJQyFnorhJr2CwQn1TZHhjS480rhX9KQzqGfEa1e C8nC5PHeOsHm1AX/s1HYJ5J4RoiDgcFTeTlYCDO67rieMulRmvT5NQrojOLyUvyYE5 t1JB7ozzM0Ii09hFjUFclDNHRQ/3clwsjnvOWsUZkkSNvEqZsiUeTksns2dNqcoiuA 2onigevkxHXBQ== From: Mark Brown Date: Sat, 03 Feb 2024 00:04:59 +0000 Subject: [PATCH RFT v5 3/7] mm: Introduce ARCH_HAS_USER_SHADOW_STACK Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-clone3-shadow-stack-v5-3-322c69598e4b@kernel.org> References: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> In-Reply-To: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@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 , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , David Hildenbrand X-Mailer: b4 0.13-dev-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=2344; i=broonie@kernel.org; h=from:subject:message-id; bh=kiK765H9QFvcPD51NDMimYIoE+CAz43kont3MOS2Ik4=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvYM6UvdX1xy39P6Ycpi5xqxlbO/j3vEL7OV15CBs w656sDeJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb2DOgAKCRAk1otyXVSH0J7YB/ 9jR2meWiqMNmTde6tFYxtIM7l52JWZsu1IZdPUbTKAJQS0NnGcrqxkEQjDA8nn2vLD9h78yX3JAa+h po/CqlJ9GD6TXa4uOwbojPl/j3iSOelQFjJtlLw+zwDWRbR94YUpfNf68MLbfIKvcKbGOWIt/qd5cf WmfeeUTbbDFr1JL44Tk7MrDzG7XVFQKEi0HfQL9bW2wrmr1mKJ//a6XEevdWmGUk3YMJc0P8NS8l8K ceNjmQisd2pMYDjwBDlsHg9hgdCs8lZ8mzHzqArsSGz2g7sRNf51LaDbJWbag7xjYgu9UTC0KTICn1 s5vZjVItqQ8P/YOK3KRvImN5Dnr67B X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834089749609361 X-GMAIL-MSGID: 1789834089749609361 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 Signed-off-by: Mark Brown Reviewed-by: Rick Edgecombe Reviewed-by: Deepak Gupta X-Patchwork-Id: 196098 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp767888dyc; Fri, 2 Feb 2024 16:07:00 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXJTiOwgfklCVwdjs4xeZBcAzX4Gi/LMEUF3vZKdSVjg/uOo80jHzmMa+zSixY9W9GMBfyur5S7T1hm3qP76K/4c5vkqA== X-Google-Smtp-Source: AGHT+IGU4FE5tbLlW2uDMN8OahcyLex6Rr4VdIKv7GkiQRxI6Jdl+SkcQpKm4Np56SuwaftFZY55 X-Received: by 2002:a05:6a20:1824:b0:19c:a2aa:67f8 with SMTP id bk36-20020a056a20182400b0019ca2aa67f8mr9197796pzb.24.1706918819852; Fri, 02 Feb 2024 16:06:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706918819; cv=pass; d=google.com; s=arc-20160816; b=ehcPMB6TtMkEsMk67ncEjo/fYytV5Np6Jg5L+tcIzS+2U6Ua5xpy8ea4RDYr7KbbtV cXdCurK1K33jH9w4sEmkr27kGNnMWFwnJxB7APYW9CsYgvyZnvpeomO6AFbVmzQmcdcm Xs1xdt7SB/1zpt/43dRjLYwtVTJQkhETVho3V2VsFksUYfUZ+CglEOMf8dT9NCOXfBUw va3Edi5DUnfdynK+sbTUXlhjrVGd6tBMsXZTFlYxUOnoxW5oLb6L0DadMxg5yj7Srt5G jGdDTi4QBiYo97F8DLSFd28fKnBgE2UGVw0zEfbeJpNjh7i+gZMf2eyb8NGodPCO6kfO Dkxg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=CHNRvJxS4p7ZjtgZ3RE4y5i8hh5vP+qCqTKUT+f33TU=; fh=Fifb+GkLOif3L0jWku0kz7/FDe6/U4UPz3Rj916DHZI=; b=c3oe/BxgT9RTXaIZvREXFHW+EiKyplVxNeJZLQfU8ina104DkLbrEKCB0gNV0nBK2v AUDOc8k+LoBiQ32QYoM21DUdw9Wz1vGLBv7UXqjfcVHyFeCqt+yt+P1KfOmhWXOUnTRz LY05RQu75Zk0OBKLVdEFP7nO3cu0W1bxhX/Lh4v6fLivx0rWtQxhKHX63ZQq960DTNFt RcHwb/gPN/jCYRP0VxzQFmHnZgTqd7SCxo7LLJYUg66GKDJTuQKk/cPNhVYve/2L36P4 eWxHmnjCqm6KNUMCSFcBfirXKtpLuTTI86G/LC0Lgt9zeMQnJYPEJ5V0h97k7yRxZhR8 fFjg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qrBKQGhz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50742-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50742-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCXMpx+0ZA2TX+FTQY2YbClOyVese5tyDUIBobbzhSKCNI8qQc14PO0Vbw/EqpXsbUhjakKqKnUArV8T9+dDiGKn9LQIZQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id u27-20020a056a00099b00b006dbacac0f8esi2355595pfg.21.2024.02.02.16.06.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:06:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50742-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qrBKQGhz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50742-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50742-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 96B9028B23C for ; Sat, 3 Feb 2024 00:06:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0993E7460; Sat, 3 Feb 2024 00:05:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qrBKQGhz" 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 6D19F6FC9; Sat, 3 Feb 2024 00:05:49 +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=1706918749; cv=none; b=pSd5ZPDCRzLevrQqj5bzwJmlf5aNrGiU211skFrmLllalyTOw43K3DoIFviuApnkoOZh3eZ856yK7exwomLauuhdJojfbib9oE7rJGSM5FcsbxpCTj2ARzuQjMimApk82fb/k3Wh37JgDJUYmyvKlEo/0uAHnbGzc6L3qNWLZ9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918749; c=relaxed/simple; bh=/Uxgj2IAESh9gETndlWWvEFVRUx/ON9J1XPtcKlvNl0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tW2eyiC3WhhW4wFA69J3ZB36FRsB5eye31WULMhpoIKxsSOt++wUVYrqgEe6l4r0WmDma8wU5hQX/jxdH/zgkitCDG/WoYIe/rEfBASTOJb7xOY6fJQDv8anPu34YOYd1rUg5+uIevv8lUWfpAtLYTgVCXlUPRwX7wQzZYrVeEo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qrBKQGhz; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A5FFC433A6; Sat, 3 Feb 2024 00:05:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706918749; bh=/Uxgj2IAESh9gETndlWWvEFVRUx/ON9J1XPtcKlvNl0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qrBKQGhzDjbMD1hPRzg+FhkluePUyAG7yQ8lMrbhJ1PmJD7UPq1ZaWDhaZgtP/PrA c9KGSpzKKHAz2scvLevgcJ4Kwf62YpcbnlKrBKJl4KNcvuqB00rBUsK2kJatQSUYnw JasUrpsQ5Xsw4kp9jGwSHTkTCtQxcu7/alHbjUIzXpTJbEBABnAgb+jJHPM+T4cO35 I58FKBv5AB5xScMfFh4JxvxpluGMYo1dkdFK5go8wQyp896wkIpEbu6W3IgEhgovLp 9X0HMUJCZzNiEdmRyoeWk/as+ocoXUA5A2quzRuLUnDIhxbdKZiFNXYDWiw7zxeozO eX4rG7e7maBNQ== From: Mark Brown Date: Sat, 03 Feb 2024 00:05:00 +0000 Subject: [PATCH RFT v5 4/7] fork: Add shadow stack support to clone3() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-clone3-shadow-stack-v5-4-322c69598e4b@kernel.org> References: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> In-Reply-To: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@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 , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=12389; i=broonie@kernel.org; h=from:subject:message-id; bh=/Uxgj2IAESh9gETndlWWvEFVRUx/ON9J1XPtcKlvNl0=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvYM7qtPkzoKYQlZ1WsaXCFO/ZJrlwVY5ooj5EuDA bnva/GuJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb2DOwAKCRAk1otyXVSH0CjOB/ 4/gAq2McZAJMyKWRAGBo1K7766JCUyDJ53E3DElouyujc0tXFZt/nUbUkpyiopJ4b65tFgqpXPKN4K Kk7rhR3FI595uWxVo9RNsMQMaVkg9hGqSAv5wbz6XzyNWnHT4b53MmeFRymWH5wI/Guujv+mQaaUhW Dr1rt/53YE6IVWKdreITyDGMfmekm/yOaDbGp+0Xv0J9dl8Ornl/QCL6Ci0WK/hzRwJH0OY3yrRDJk b9oCxAUbSybPuJnxlU40AqvojbkH9FjRPIf3HJtiFIo18kcai/UeZk6ysjfL8t3EzzhgqkKtFitNVk v0PTEQLOxYbsm4WmjczO3W4DIQJ28V X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834108313707563 X-GMAIL-MSGID: 1789834108313707563 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 a parameter to clone3() specifying the 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 the architecture does not support shadow stacks the shadow stack size parameter 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 implemntation does not consume the shadow stack token atomically as would be desirable. Signed-off-by: Mark Brown --- arch/x86/include/asm/shstk.h | 11 ++++-- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/shstk.c | 91 +++++++++++++++++++++++++++++++++----------- include/linux/sched/task.h | 2 + include/uapi/linux/sched.h | 13 +++++-- kernel/fork.c | 61 +++++++++++++++++++++++------ 6 files changed, 137 insertions(+), 43 deletions(-) diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 42fee8959df7..8be7b0a909c3 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); @@ -26,8 +27,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 ab49ade31b0d..d2bfcd44de05 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 59e15dd8d0f8..24d0e9b825da 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -191,44 +191,89 @@ 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) +static bool shstk_consume_token(struct task_struct *tsk, + unsigned long addr) +{ + /* + * SSP is aligned, so reserved bits and mode bit are a zero, just mark + * the token 64-bit. + */ + u64 expected = (addr - SS_FRAME_SIZE) | BIT(0); + u64 val; + + /* This should really be an atomic cpmxchg. It is not. */ + __get_user(val, (__user u64 *)addr); + if (val != expected) + return false; + + if (write_user_shstk_64((u64 __user *)addr, 0)) + return false; + + return true; +} + +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 + * explciitly specify one. */ - if (!features_enabled(ARCH_SHSTK_SHSTK)) - return 0; + if (!features_enabled(ARCH_SHSTK_SHSTK)) { + if (args->shadow_stack || args->shadow_stack_size) + return (unsigned long)ERR_PTR(-EINVAL); - /* - * 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 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_VM)) - return 0; + if (args->shadow_stack) { + addr = args->shadow_stack; + size = args->shadow_stack_size; - size = adjust_shstk_size(stack_size); - addr = alloc_shstk(0, size, 0, false); - if (IS_ERR_VALUE(addr)) - return addr; + /* There should be a valid token at the top of the stack. */ + if (!shstk_consume_token(tsk, addr + size - sizeof(u64))) + return (unsigned long)ERR_PTR(-EINVAL); + } 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; + } - shstk->base = addr; - shstk->size = size; + /* + * For !CLONE_VM the child will use a copy of the + * parents shadow stack. + */ + if (!(clone_flags & CLONE_VM)) + return 0; + + size = args->stack_size; + size = adjust_shstk_size(size); + addr = alloc_shstk(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return addr; + + /* 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..dd577e8dc881 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -43,6 +43,8 @@ struct kernel_clone_args { void *fn_arg; struct cgroup *cgrp; struct css_set *cset; + unsigned long shadow_stack; + unsigned long shadow_stack_size; }; /* 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 0d944e92a43f..fca041cc2b8a 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -123,6 +123,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) */ @@ -3062,7 +3067,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; @@ -3095,16 +3102,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 && @@ -3145,6 +3154,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. */ @@ -3167,7 +3204,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 Sat Feb 3 00:05:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 196101 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769320dyc; Fri, 2 Feb 2024 16:10:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IHznM8qIp+xYHOalPk8qChptMe7yAAYIy3AJIZhtustHV1vRsD7kh2ZDEbdu4IzC0rNVAGa X-Received: by 2002:a17:903:264a:b0:1d9:44d4:706b with SMTP id je10-20020a170903264a00b001d944d4706bmr3771041plb.50.1706919028692; Fri, 02 Feb 2024 16:10:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919028; cv=pass; d=google.com; s=arc-20160816; b=scbDyyLdM3bRiHUMnPhxIY2LzlGvJkZA4CGtx/qybEmBboVV64O0LZ/YQ1Vkg5/0yX dblXQX/NammVjAYLq+BTTRb32wF6kAmDvl2gDMzrT7peI/LR1biUCTu0+2RMsq6wb7+f KqODjyCAiwMwH0is4SJD0Xnr149p6IZNrz4qFgEzpdIMC8X/ow2ctUVJvPscxswWcO7U rS24M8eftwAQWAcV6ZLUA1PdWCWmu+XAiZ6q3tv1sd84ABoo1dOv0M0Ppc70AwiODnvY Bo8m6rKz+vtxsI0dRPMLUUZp2227O+7gMPRqqM6PedP09B4AM3tnWIvd0Wais5F2VVLj w6JA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=vVZkVRUin1/QmjMnw/YaKi0zacs/6BQ0/2j1SkJTQ38=; fh=cQYB/P4bTh6EId60PvdRTG4gjYv2OMGfHGsAfIdbdas=; b=CDOQ0QgmcPqFcDuHUew0n7/vMeKwUQFX1yO5HhHVXDtjQBtyBSagjNSQWJS9K3jw6B 9Y4iDFLyg3j9RvnjLRK1lZVCeFipLlB1nNj+Wo3h21mVU4ueK/fiDD5X/ojNrDovRbRm 30zSwETktaaDvSz2enp+roXd1z1BxQo1T5igEOBSwY4Kr/tI/QF5gk73Tj/qw9wdG+B8 1E4+sxlFivuNhCS9HtPxdL0pu/r1q7YaoeD5EeFPGd5duXrIJlX6cyhRg4eAWfaOci5V ElwJX+kYhHBHsaYaGtI5PiKecSAX2m8L04L0wsu8TwhK1016hnmP4vAykd4V3KyCmtoR Ze3A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jLsFaBFH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50743-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50743-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCU90JeM77h/fP3oDt4wB1n6FORV5k1N+m9WcdvbQAT8P61gqQ4daaONGyAMyeezTLs6ri6dkn838o8OXBEd5Imo0VdnSA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 21-20020a170902e9d500b001d757b25122si2387497plk.235.2024.02.02.16.10.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:10:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50743-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jLsFaBFH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50743-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50743-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 2E944B286D8 for ; Sat, 3 Feb 2024 00:07:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B047779F9; Sat, 3 Feb 2024 00:05:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jLsFaBFH" 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 A951979C8; Sat, 3 Feb 2024 00:05:55 +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=1706918755; cv=none; b=fv3Kf6ycqe02P7uQ2ebqYx4yJ6Nww6ULyaFC6bS5jCuu8IySNcwTv3farEUfU9oCk74SPCRhmN08OPJB0EVYc62jkIGE5SN+hGwJHNR7fXvjop1nlrUQJRj9CWtJ1goetftoeV69S8hZ9/JT1q1KThwC0w4IVhcTAqBymHJZYYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918755; c=relaxed/simple; bh=bhar0tJdOqw54YK6iRyaAGbYhafIqhucm+rmgUHLm6s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pTb+VK7u0xunhpbZ7eXWs9LN1PbvDWCDVOwlcK4iFpqRK/Qj/U2oqA0ggoD+caTqkhUVaZ5oSU6fx6GZIMZFk+fzs9hXVCccSKmesl4BPOYrC/x9Rb0NIHXRHbl70KmI0SaU6ON+rB6yacSxB3QY9hmluPKLrBgNot5FADamEz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jLsFaBFH; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D673C433B2; Sat, 3 Feb 2024 00:05:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706918755; bh=bhar0tJdOqw54YK6iRyaAGbYhafIqhucm+rmgUHLm6s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jLsFaBFHCSBrixG4hcZYBOrB75ouuKUlnr438pHKofOeoOX0TyBaenr1drDbtiVOn fsSMxgGZLw6xzRMrZiZFtxYWRWXbtmyr25rKMdb9emXXXMUpkluCi2NRzoLhBU9xxL Hfyu4/PBkX9fILuTsLwBCgwYxHxc0Zrl/yH/pulfiFiertbPT/SBG+/ZAz3bfJh6Sw 7FilMJi8lmrz5uYlTn6anerHlGiGl4uk13g9wHFfC0DYK5phUkwOEGPedfIz+efABv 5EJrILPHLdCe077NB2jmQFC+w6zWvDSTRGcG/t+KYg2/WgQ+81SJYcI+GDme92svv5 vuaWJTTqnH1TQ== From: Mark Brown Date: Sat, 03 Feb 2024 00:05:01 +0000 Subject: [PATCH RFT v5 5/7] selftests/clone3: Factor more of main loop into test_clone3() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-clone3-shadow-stack-v5-5-322c69598e4b@kernel.org> References: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> In-Reply-To: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@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 , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=3743; i=broonie@kernel.org; h=from:subject:message-id; bh=bhar0tJdOqw54YK6iRyaAGbYhafIqhucm+rmgUHLm6s=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvYM7GLBbYi7dllKXEgV0RzBo5WGltkP1CeQGkqcm Wo26q8yJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb2DOwAKCRAk1otyXVSH0ASJB/ 9ktbBQUb2QeI7D2t4eFe8bPvy6+UZ6NzcbA0Nn3XBIyLkTyY8CeLqC6RTeM3xol6c2hX/1b58LObXo QZbXvri/s4lbDvZDP1pdFkiK4E/m2piEnIC0SJ4ScW9+b1ju9MofbVZR0FnylTQ1rhTUTh5ss8TD4+ p1FhU0J6YqCUJ1b3SvziccgEd9HrYPq/yINeKh3rabAF+jYcg45utYiCjh9pgVVK7k3mGlipq4lQ7I YTzKFhdosYBgeSHLsFeLpNWNGGBn1p7nOs4JnI3Y/adkaxKGawOB2xVFgZyQ2q46zGEW51w3oufksY TFxHYhHl5Z7UnnCc7C84G7pCkwETAV X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834327170092123 X-GMAIL-MSGID: 1789834327170092123 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. 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 3c9bf0cd82a8..1108bd8e36d6 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 = { @@ -104,30 +117,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) { @@ -155,16 +178,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()", @@ -314,24 +327,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 Sat Feb 3 00:05:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 196099 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp768563dyc; Fri, 2 Feb 2024 16:08:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IH6vfE1SgnDpoAKhK4oC5iOXYsJ+699tO28BxqH/duX2E01bX84g1FDBcJZ7CjRhjsVhRfM X-Received: by 2002:a17:903:2307:b0:1d8:d90d:c987 with SMTP id d7-20020a170903230700b001d8d90dc987mr9096220plh.16.1706918918162; Fri, 02 Feb 2024 16:08:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706918918; cv=pass; d=google.com; s=arc-20160816; b=mXiYcF85ywLSMLbtpV34sJLL8VTQo6w+so2dhAB+ed2GucEhMWnkBbaiGDVSYExztz b2lgHjkOgRr2oJW26HSRMAiEWTqO8pPEC+5rCwGV4PlqN/npCGFy49+oZ2YXGk1VMXOc GOU1TSplZO3PbKbqSFaZnKsoDevJRghI9aGdXfjzrbLUurLf3R0dPE4U09wT3N9r+Onr Q26tZlLHCAMUvSGaVqa/FSVYpdyiul9SyE4+rqYONYRRSLuefRzP3afNBhri4WYnD4+X TjsN0H7NMhNwCyID1jUe0BMnBtWEOvayFbyZe+CVt9+j3NQMRBJ9E5oB125yWsRmztVm D63w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=rnrAcFFJlTaTWrEDOlhOmzSEK/Ayvew57WsJeG2pZk8=; fh=985hIEoFai2qkX0HLhPy013RPZgiSQa9n0w3YzKu3ms=; b=qL9CtZWBBtywXk6XOYQItbqlWeoJxObAW4cO3T32y8bTg7JXVMJdasXo/jPFD57ECm QkhcnlQMK4R6DX5/SIKcTHAgedbiXeyhZWeM4n263XdpHsonnONPBH65oZYVr4OFOXIg j/kFqw3qzIDTKFPLXpVKBkyEJiQ5gjPZx+t7CrZ7+ghLP10YuZXd7pDNkKYcbpTC/Qut ifFuAZrDjn+EIyMLU/bmzBFuMIlNqtEQwQ3MlZZPyDCndprWy7/VJjPrgQ9XLZI46+Zv pqhG5MAEkT1BW9hmVtu9gxM/1dQ0MgQccGQaK263Ng+sGVqpYUC6qlekFOrOvKVWhsCR B//A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sC8XyJ2t; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50744-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50744-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCW08CM1bGpINMr+gmBWYx0BGvHHMQdU31n22E8LCdokz9ICD8qkBIYSf5aQ92D3nva4dfOXKEnuVGfscJhXl17aV6IrOg== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 23-20020a631657000000b005d24ebc7f36si2308604pgw.406.2024.02.02.16.08.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:08:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50744-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sC8XyJ2t; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50744-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50744-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 2250F2811FD for ; Sat, 3 Feb 2024 00:07:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77C82848D; Sat, 3 Feb 2024 00:06:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sC8XyJ2t" 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 BD2248472; Sat, 3 Feb 2024 00:06:01 +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=1706918761; cv=none; b=mLC7FmPdqxC2t+2o+8fnKcq9AHGA3QaTdtxrvdus3aFuWJsm9o4Kvsc31i+NWfrb3lEwDIlwSPR16tujgfBKB7GHdjMlAk58MLES9BCHFduRO+qAvo4k5jUzLduTI/8qdNolwHAbpKISwDGZjE2gRmE7cbh5/yH1X+9mxmUKbmg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918761; c=relaxed/simple; bh=ZmJrvmANp1kG9kAUuq4ZkamJ4qQyGfBTGbaFEhsSApg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=S54Yua9nkLBmktmZaIzyz3Y32n3S7lOOdP6MexXQVg6XcwkTJkBjg6USLnVsrVo7qugJwxQcPRHKqzAcquTwQLIOf+7UXA4GzK8jifk83b+OLxc49ttmu8vDpagmMr0sYGMIWYx8xOo7+9zdVoSpimmKoQZXOkdOuP/tO43zseY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sC8XyJ2t; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B329C433C7; Sat, 3 Feb 2024 00:05:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706918761; bh=ZmJrvmANp1kG9kAUuq4ZkamJ4qQyGfBTGbaFEhsSApg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=sC8XyJ2tl+Jv+nMg7kAlHTfGiiunwtOdzY+vfI6myXuzX17FY60/s+/g23rHbGyvG jTc4DgipAAux9rkk+UZog/myaTd9S6nIAmbFNNuIaaBthYVH7BKRWy+5WOGU9DqKwr NZrDPQzFph7iezj64I2aL1EP1oPqfa+PMP/IIfe8X6IPEM/ZMFbL75oUbnUMxRUHo2 urzWWHW/4NGel5+UD90WjjZdYDoOJfcLtpivixnVqVVuUq2nMd4zTafrF0krut2Xf8 3tGuVpMJuGSYFuN0rlLIcGfOt+nulzQivVfvQJ9R/QC0H1CMC8qYScaDIatY3Ni4xR /vq9YbEFIChHA== From: Mark Brown Date: Sat, 03 Feb 2024 00:05:02 +0000 Subject: [PATCH RFT v5 6/7] selftests/clone3: Allow tests to flag if -E2BIG is a valid error code Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-clone3-shadow-stack-v5-6-322c69598e4b@kernel.org> References: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> In-Reply-To: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@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 , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=1887; i=broonie@kernel.org; h=from:subject:message-id; bh=ZmJrvmANp1kG9kAUuq4ZkamJ4qQyGfBTGbaFEhsSApg=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvYM8du2iMR3YFLO+fn1uPqS0LIf1mYcOElHC1XUz znsriz2JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb2DPAAKCRAk1otyXVSH0JSSB/ 9YIPz1a2MqXlpuNlxfN51p6rKezpSkt4SG83/Wnn8gJWOrgIRIJno9fDwno9O5GIk3S1DscMBpDNSB hkyVSXw3CUoHdyDbhHNDsmPhFRa0zPMdVzBMnw27UDOz0Vrvf9h5wCebi/Mu5wU00fA3NWjnBCXkso bbk+u6pZczo4Iu4DvGybrgkUGlCb7HbLRZqSu5ptNGBHXTSN9oDoklorKd3xandZ4RlDek34HmuuCl eCiw+J2PkZ/Aqiz5TNosuM8KbBeChhq+/KBZ0G7gXah72pHKTFO35E7bnxXY1QBBVxsM/Le7QME6O8 sT6TWuSlqvEsWXnv6loxFoNKrmnZ6M X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834211797690595 X-GMAIL-MSGID: 1789834211797690595 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. 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 1108bd8e36d6..6adbfd14c841 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; }; @@ -141,6 +142,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 Sat Feb 3 00:05:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 196110 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp771641dyc; Fri, 2 Feb 2024 16:16:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IHfv1FrBGFN8eOLOR+X1hz38Po8T57uUATp1rJPC9XFJZNt8VX6DOizuSKfpSbqeTy5ygCL X-Received: by 2002:a17:906:e246:b0:a36:598b:e291 with SMTP id gq6-20020a170906e24600b00a36598be291mr6906951ejb.26.1706919406765; Fri, 02 Feb 2024 16:16:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919406; cv=pass; d=google.com; s=arc-20160816; b=WLtPM0Xwi2R4Ho7BmbnPjUez3CX6amfaB8tj+iuSrvAin2LU0AeiQ5yhQ1ZO95gp+n TH2B/re2jh0SDzyyFr8ewe3xClkaxuYEAn6CwAD8CRpOsfQ9pr3ZlpLlfNViM01Y91pr IelPThsLEuat2pUHQlN1ns2niSZy+f/xc5xD3bVcksp/GgInxY2QSr48K6TFai1/7W9Y tAEy9ubfcX59VWqf9p1+7CXwLs+KHaZ2Pc1DcnFak3RejH4etPO53e4PPXfJ8bVDdhFU 6WUfm7lIBmgYyxNryE0tI2JTgHIyEDsj3u5qHR1E+7UF6to8cVCImQ58mkX4EQZti06a Trwg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=EZBz0lv+yEe/QGVxpPxKvPtzEUoDGLGXVkFOEe6rv8g=; fh=uD4oPBMHI60+xbVRShavPomHXCCJ/O/iYo0pL/Ig+28=; b=Lzj1U+tMY5JNW4NR+4Cdk5ASkHGPSCbck840WP2ma6VgpadmPFkY/h1+7XW8mzGJpu IPF41yEsf33QKThhF5zd4AFQSqDY83Z4q0YUVBmP3YSFMHhN5svbWlRcElPv9RWr40Oj /XQuEnspE2R65MGE7LMItSXVB7DwRiRqDvhYw1tjDxK3hA+Yb0h2wKA9r5THEJOEejr9 0u1f+VjwjZznUKiC/QG0z6W638YUz9l/F+XgUX4Rwl14hDdyfpqKBz50R47wTVTwgQ/Y sb83uNKTLfWVRjYDTb7H0T+6P4ph0ywmUesNpTO/7lRM8O5n4wQzvjGs/5l3tjRFLzOa f82w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=h6hjWje2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50745-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50745-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCV6Ow86e5uVLv6g8ui3pUH2ikANODDDGZBd4jdCNmF5qHWixZmNoSV/4VOAXbVOYRxT6iRRVSobDZyq9t9UF6Ln26tSpw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id u2-20020a1709064ac200b00a365c4276efsi1283802ejt.9.2024.02.02.16.16.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:16:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50745-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=h6hjWje2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50745-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50745-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 75FF21F2C905 for ; Sat, 3 Feb 2024 00:07:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 86740C12F; Sat, 3 Feb 2024 00:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="h6hjWje2" 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 C2D27883D; Sat, 3 Feb 2024 00:06:08 +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=1706918768; cv=none; b=PMIvLJzbblLuZSUqpGyVwyMbp3KYszsnHzpYZc0z8mXZNKgjfRPWOCml4KzofgGPkmGeOX6Mahgb9bKfyZ7N0G+rkqyZXmJYj7cKYodSq6Wxd76qkrwz5/LoiPXUejiOYl40y+0YSo8pgjzOqXXOOy+La69Qhi4hesQSS4fj+j8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918768; c=relaxed/simple; bh=tLwTbiFrM4HHqU9nz4FbJLnTenWOwfYkzVrlMYdP4yc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gFv9Zq+aWvvpZCYbl3/nNCHUZ/AqU6ABN4Nbz2QaO9XmACiS9QFy7CobBPtLfKFoOerngEZLsQQF4qPmJ1oxPdV7ragLWL8se1D/Qe3GK9NHceJ0kqeTS+gcxln6B61OBg4po5MdefrQDkj9I3lh7ErbdLcwVqe0X55w+4H0S3c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h6hjWje2; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B596DC43390; Sat, 3 Feb 2024 00:06:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706918768; bh=tLwTbiFrM4HHqU9nz4FbJLnTenWOwfYkzVrlMYdP4yc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=h6hjWje2Jzr3dORUU62oOWti7/PsRPfDsI5xxwBJnfa/gvaY0Cd9ejPfSZ16qM05z 4q5kU4pfQdim/3iY1O2NVxBwmkh8dvR2FZ8FbRuB3VwU4iFDvJ3j8ryoOoa+lY/uS+ 8E53dhY7bS1T8bcwA/Vn9E34TGaJMlmN5sQv2GnY2bcM9NoP/jubW917TKm1XhyEz4 8NffObNR2RRY/l3+QFzWpzwwzkXDFQaG3aAhevbFzYwm1VV3gdoOFM29+tDOMsSyXR ecilIxpR6yTLEP+NjzepPJxXoJwjz1oucmjjyq7hp5RDW/47wvP2fAlUN2zaAg2xXI CEbkDxTGP3jmw== From: Mark Brown Date: Sat, 03 Feb 2024 00:05:03 +0000 Subject: [PATCH RFT v5 7/7] selftests/clone3: Test shadow stack support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-clone3-shadow-stack-v5-7-322c69598e4b@kernel.org> References: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> In-Reply-To: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@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 , Daniel Bristot de Oliveira , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, bsegall@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=7047; i=broonie@kernel.org; h=from:subject:message-id; bh=tLwTbiFrM4HHqU9nz4FbJLnTenWOwfYkzVrlMYdP4yc=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvYM9GU3402XETN5uhtlMqpBMbL/h7l8ItCThuT/D ssUeLFaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb2DPQAKCRAk1otyXVSH0L57B/ 47zDNvJKiHQAEn8Z/Z/aea4Zqk1sJzbkNpOUpfdDV+NejHcDlJSIo5yOuqKpCF0kBfi51t/wZUQoty 2G1w1bAPR6vHljsyHmSFvL1YlpGgQXDL+IXKISeaA5snKrt6P/4b80iLmvv7n8RZYAHnfU7kMPzWUe XyV2i6GF8g8EXV/xaucNSyeBQyCZRW9v5lcyg4/IyJ26+SPACcdJ8gZG0exYhhYyVaY2p9B0WrFCQG qRluJAiAP72RE6UhbHW/30Q7TD3VE/9mIPOBPInxM7I/diNQVTTC/q2Vnh50wp4HP1pI6gdnpYxhut gO1ZIknxByjcp0Wf5TPf8n6Ke7dXcS X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834724198403458 X-GMAIL-MSGID: 1789834724198403458 Add basic test coverage for specifying the shadow stack for a newly created thread via clone3(), including coverage of the newly extended argument structure. 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. Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 128 ++++++++++++++++++++++ tools/testing/selftests/clone3/clone3_selftests.h | 8 ++ 2 files changed, 136 insertions(+) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index 6adbfd14c841..c468d9b87bd5 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,10 @@ 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, }; typedef bool (*filter_function)(void); @@ -44,6 +54,43 @@ 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 +136,20 @@ 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_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)); @@ -179,6 +240,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; @@ -322,6 +403,50 @@ static const struct test tests[] = { .expected = -EINVAL, .test_mode = CLONE3_ARGS_NO_TEST, }, + { + .name = "Shadow stack on system with shadow stack", + .flags = CLONE_VM, + .size = 0, + .expected = 0, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK, + .filter = no_shadow_stack, + }, + { + .name = "Shadow stack with no pointer", + .flags = CLONE_VM, + .size = 0, + .expected = -EINVAL, + .e2big_valid = true, + .test_mode = CLONE3_ARGS_SHADOW_STACK_NO_POINTER, + }, + { + .name = "Shadow stack with no size", + .flags = CLONE_VM, + .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 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[]) @@ -329,9 +454,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 3d2663fe50ba..1011dae85098 100644 --- a/tools/testing/selftests/clone3/clone3_selftests.h +++ b/tools/testing/selftests/clone3/clone3_selftests.h @@ -31,6 +31,14 @@ 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 }; static pid_t sys_clone3(struct __clone_args *args, size_t size)