From patchwork Tue Nov 14 20:05:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 16581 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:a59:b0:164:83eb:24d7 with SMTP id 25csp2172637rwb; Tue, 14 Nov 2023 12:07:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHquAf7e/HlFHW8ENf33XvvmrsYDsTKN1tMjKn6noZgAqe2gK4m1G0sVrdSg/TGKRMEQYt8 X-Received: by 2002:a17:902:e80d:b0:1cc:1106:cf5b with SMTP id u13-20020a170902e80d00b001cc1106cf5bmr4387236plg.19.1699992430883; Tue, 14 Nov 2023 12:07:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699992430; cv=none; d=google.com; s=arc-20160816; b=Mi8Z2r9mmA63D0LwbIidJ0MC3v8zmPVoBzQTa52LhztvJM87HqfxpVDTBJWGwoz9oo p66AtLIln/oftPr09/tZF44DwXUaf1NTZYLveixtYaY2lPuDU0jgwfN6hHRnAoIZLH7m 8EAHopf+A21XzLBcWuuGO4+azIWvovR9U9eAOHmzGmyWZgs/1ovMfPSb/uZi56MShN+L jN5tqO1gfXzbACPjgsFEBwmpgb1iwUaMOuX2d51f1BkyaAkuex4durN8YIObZRj42+FS 4yPPYQ1k5OLA3/gtHrOv+j/4miKU+a2v9FXM3qtaEywrinW0upczWEbAlXBhWFte6+hP 4JNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:content-transfer-encoding:mime-version :message-id:date:subject:from:dkim-signature; bh=WP/q1+rgPnFEPxxW5y0vU6rgTeNx85icbruND3OPQhc=; fh=QD8DltxdNEo4LPmCb1b9DaVqpdSmjfJB45XzRnalxtw=; b=WsaDy6/mYjxvAvDKzPzNIOXPfNoQXOtRHHxR9V13mASnShr+twpI6AznzlxTeXdkcb 4sJZPe9oEuyZrEnJoo1RP3F/VJ8oAzR2OTrPqSDs3gJxvRi7dVM3AvvHOnQjVPIUSerV S2iW3q/o43Rf8RW2iD2ya2+bY4DRW9HoRIt0L87CBpx8c0CLDPUsny8/FoHIi/vnYTAq tAWJUqQ2PFQDobSt6bj+ZahMxuOyGUE8f1DiHdQRdyRA/iOJHewZPy3jVY2GHbbE3ld6 HsXtfON9kQ6YvS2JVe3OmxsN3lx/2P3avEb4Z3ms+DPZvTYkaKKlVNn4Bw1h28WQTuD5 Fxtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hn2kwbk3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id i5-20020a170902cf0500b001c72b13a1cbsi9398210plg.352.2023.11.14.12.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 12:07:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hn2kwbk3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A241E802F442; Tue, 14 Nov 2023 12:06:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233849AbjKNUG0 (ORCPT + 29 others); Tue, 14 Nov 2023 15:06:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233554AbjKNUGW (ORCPT ); Tue, 14 Nov 2023 15:06:22 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC7DC120 for ; Tue, 14 Nov 2023 12:06:17 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBC6AC433C8; Tue, 14 Nov 2023 20:06:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699992377; bh=MfWlzzX7L8lz94mh8rv9WnWcKbtpvpSrDtFPJuz1FkY=; h=From:Subject:Date:To:Cc:From; b=Hn2kwbk3nSu+0e4wDUNVRZDYSQr6g5KPAu0StmRkF/ho8ubAx7AiDvl9lIl9HSnd7 KauF2TvGOydl4drevyqTvhy7o/31se2eUoWYhScFiMFDS0uE2N1qFfJHxLgNxMEk8g M5pWO26n2uvPWsZA0aEi0Ri7oMjb3dnrMXpJ2F9KnTPW0IB+ypGQ7ZkK8CzuKaCNTT TkKDoAs4F2oFXyNZCTsPzgZiZv4Bf1yyAaYlxZUAJzctjNwlovMdYIeaMJU2f6d/xG alU2SE+4mXfc+8M7LdartfnITPOJGtSmEQl5q6SVoEmIbtX3s+2U5DSaZSj/sLnL3C PML75dQMwgM1w== From: Mark Brown Subject: [PATCH RFC RFT v2 0/5] fork: Support shadow stacks in clone3() Date: Tue, 14 Nov 2023 20:05:53 +0000 Message-Id: <20231114-clone3-shadow-stack-v2-0-b613f8681155@kernel.org> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIACHTU2UC/22NzQ6CMBCEX4Xs2TX9Efw5mZjwAMSb4QDsIg2kN S1BDeHdrXj1NJnJzDczBPaGA5ySGTxPJhhno1GbBJqusndGQ9GDEkpLIY/YDM6yxtBV5J4Yxqr pUaa0E6TqNtUZxOXDc2teK/UGRX75ZkV+hTJqZ8Lo/Hs9nOTa+LGV/sueJAqkQ7YnUccbEueev eVh6/wdymVZPuOoiCTDAAAA 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-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=4195; i=broonie@kernel.org; h=from:subject:message-id; bh=MfWlzzX7L8lz94mh8rv9WnWcKbtpvpSrDtFPJuz1FkY=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlU9Ms07rh6zd2XIxqOwQmLb7yl+tzUKaiLOM8+zVg H8bjGLOJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZVPTLAAKCRAk1otyXVSH0BIWB/ 9w04C2iBH4mhSydpLa4tmVedxFN519c2gnTs4utY3o6cWf+gX6HHShvAUkMGrQPuyi1ClPakmiTEui keXqyemNKH7NUKPKb3jGayT9Z+f/KL/fdEvIXajnxIHLfn68NHWRDoJ8MrI3R6wzAufQuRaauaD4DB rVRKjPZ/6BAccI1Ht72CMK9uNZYHCZetQyBCTWddHIPx9hOPk5TOY+Y/sE15gMxO0OtdWtJUyMvyXO 9FI7h7LglUacOdP1AVMtm8wKKdpjLotAqAFGbU6bAowIbU8NffnGoOH8rbPps7rGedKbiKHolocAxi 5bf/RXLkR0ucr5VTMMk1FMY9PvmeFm X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 14 Nov 2023 12:06:52 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782571262906456401 X-GMAIL-MSGID: 1782571262906456401 The kernel has recently added support for shadow stacks, currently x86 only using their CET feature but both arm64 and RISC-V have equivalent features (GCS and Zisslpcfi respectively), I am actively working on GCS[1]. With shadow stacks the hardware maintains an additional stack containing only the return addresses for branch instructions which is not generally writeable by userspace and ensures that any returns are to the recorded addresses. This provides some protection against ROP attacks and making it easier to collect call stacks. These shadow stacks are allocated in the address space of the userspace process. Our API for shadow stacks does not currently offer userspace any flexiblity for managing the allocation of shadow stacks for newly created threads, instead the kernel allocates a new shadow stack with the same size as the normal stack whenever a thread is created with the feature enabled. The stacks allocated in this way are freed by the kernel when the thread exits or shadow stacks are disabled for the thread. This lack of flexibility and control isn't ideal, in the vast majority of cases the shadow stack will be over allocated and the implicit allocation and deallocation is not consistent with other interfaces. As far as I can tell the interface is done in this manner mainly because the shadow stack patches were in development since before clone3() was implemented. Since clone3() is readily extensible let's add support for specifying a shadow stack when creating a new thread or process in a similar manner to how the normal stack is specified, keeping the current implicit allocation behaviour if one is not specified either with clone3() or through the use of clone(). Unlike normal stacks only the shadow stack size is specified, similar issues to those that lead to the creation of map_shadow_stack() apply. Please note that the x86 portions of this code are build tested only, I don't appear to have a system that can run CET avaible to me, I have done testing with an integration into my pending work for GCS. There is some possibility that the arm64 implementation may require the use of clone3() and explicit userspace allocation of shadow stacks, this is still under discussion. A new architecture feature Kconfig option for shadow stacks is added as here, this was suggested as part of the review comments for the arm64 GCS series and since we need to detect if shadow stacks are supported it seemed sensible to roll it in here. [1] https://lore.kernel.org/r/20231009-arm64-gcs-v6-0-78e55deaa4dd@kernel.org/ Signed-off-by: Mark Brown --- Changes in v2: - Rebase onto v6.7-rc1. - Remove ability to provide preallocated shadow stack, just specify the desired size. - Link to v1: https://lore.kernel.org/r/20231023-clone3-shadow-stack-v1-0-d867d0b5d4d0@kernel.org --- Mark Brown (5): mm: Introduce ARCH_HAS_USER_SHADOW_STACK fork: Add shadow stack support to clone3() selftests/clone3: Factor more of main loop into test_clone3() selftests/clone3: Allow tests to flag if -E2BIG is a valid error code kselftest/clone3: Test shadow stack support arch/x86/Kconfig | 1 + arch/x86/include/asm/shstk.h | 11 +- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/shstk.c | 30 ++++- fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 2 +- include/linux/sched/task.h | 2 + include/uapi/linux/sched.h | 4 + kernel/fork.c | 24 +++- mm/Kconfig | 6 + tools/testing/selftests/clone3/clone3.c | 151 ++++++++++++++++------ tools/testing/selftests/clone3/clone3_selftests.h | 7 + 12 files changed, 188 insertions(+), 54 deletions(-) --- base-commit: b85ea95d086471afb4ad062012a4d73cd328fa86 change-id: 20231019-clone3-shadow-stack-15d40d2bf536 Best regards,