From patchwork Mon Jul 24 12:46:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 124924 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1781703vqg; Mon, 24 Jul 2023 06:01:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlHkmZB5JkQKZ3vj2McEOx1YbLO+pvTS/XM/cDe4mUU16/+qCzFIh5OubwSyboHNMzIrjCWb X-Received: by 2002:a17:90a:7442:b0:262:fa59:2908 with SMTP id o2-20020a17090a744200b00262fa592908mr14329338pjk.1.1690203670635; Mon, 24 Jul 2023 06:01:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690203670; cv=none; d=google.com; s=arc-20160816; b=YJ/6XKBBNrDz+8BVvNEKPFZOyNJYBh9asuMw7d3i1IZs5OZfl+y0IRmJH1wQ1jxIrx 219BHfzAzdPsT5x6JSjbxwBC0qMI9GuPPPmZpOftajDdp4FsOFGbLDE4OELUPsD/3C85 U4xUKH4SAFAB3TT+LTHIOUTFvyYbuJjlqLDlmvkvv63EeE5LNqZnbDyqCwfO9UmlUxjd iPbjU16ufUZ/83vr913aMgelgzEox/Ze9MU3EIHhihZWevSHEMsGUTlr8KbLYzaa/wtM MOsbreZBcQ4kSNocRJTsKMEfdruTOhiJxHPPiOsHQBZFK0ShNJHtKUtc7dLxCEqKa9La 60Vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=nDVR3fn2xd0MNSzIh34zJUX6KbyaSvUYB+1ILixlbyQ=; fh=EdAFSmIgUzZTC6WCy4Jg1wBZ0/m6m2d/OjiRUJ3BsTI=; b=c/pXTWp0aAToWA/VsMlZr3HkacVpkoDZzQgRJJGqjg9iSVxZ9/3zopZmdardgB/5tP GLY49/9WCi8Hw/Ww+DxEfGIoLH20n+LCzAFTJmb3AqlDd/JFKEMNytOVjXcHJPhBZxmf HNNVYYtZhI1UgWoOGZuUZcEUU0W7Xv9IydD7okHmF2xRgKQ6BoZo+B8BxT3BkSVgjoQC 99KKegdtUtVu+dPk+XY8nB92qfyypcD3VmNpfzbL0PKxYMe6aTXUbWLtp8R0coldaz16 3v31sxzralHIalI3j3T8JyC5exOZVhSySwri63OawKOtDyX65v2Ul7UzNRp+RHBKBRZJ WOYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uAvnRHpr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id in8-20020a17090b438800b0024e4ace1a41si8867784pjb.183.2023.07.24.06.00.54; Mon, 24 Jul 2023 06:01:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uAvnRHpr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231608AbjGXMzX (ORCPT + 99 others); Mon, 24 Jul 2023 08:55:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231558AbjGXMyi (ORCPT ); Mon, 24 Jul 2023 08:54:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8B4649E9; Mon, 24 Jul 2023 05:51:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8F21D61161; Mon, 24 Jul 2023 12:50:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99FACC43391; Mon, 24 Jul 2023 12:50:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690203011; bh=Nuasx9Ylf/ySuoGV/J7jU7muHvsTQeng/1l4pki2YVk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=uAvnRHprExm4rHtwFtNZqCW2nNBMYLh6iJY1+IZfHvOwHzMsr5Im673ImNRnxclXp KkD1zAeNnpM+Qko7Dpk+qMYTpceQ3rTt/gP8MXX/MVHhRWrq4+O7LGcOzIJzuB3jL6 TXym3xrMX03wrjHPfM9JN2g+DXk3H/6hRlTgKnx0Ze7iQAIvDuHPw+aA2PyzhnwxPy OvxMPY//pg5m4QnRgw9y6zYMO8X7B8r4CBOkPUqkwDdjgOs1JEOEBDcomGC3L2ts/y D4MCYMPPhM1SOhi25AE11qQoMDdAAd3dSIoePSoXLsKZCLFwV+2we6aWVQFe2Ic9u0 nui/HMjfMEZMQ== From: Mark Brown Date: Mon, 24 Jul 2023 13:46:17 +0100 Subject: [PATCH v2 30/35] kselftest/arm64: Always run signals tests with GCS enabled MIME-Version: 1.0 Message-Id: <20230724-arm64-gcs-v2-30-dc2c1d44c2eb@kernel.org> References: <20230724-arm64-gcs-v2-0-dc2c1d44c2eb@kernel.org> In-Reply-To: <20230724-arm64-gcs-v2-0-dc2c1d44c2eb@kernel.org> To: Catalin Marinas , Will Deacon , Jonathan Corbet , Andrew Morton , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Arnd Bergmann , Oleg Nesterov , Eric Biederman , Kees Cook , Shuah Khan , "Rick P. Edgecombe" , Deepak Gupta , Ard Biesheuvel , Szabolcs Nagy Cc: "H.J. Lu" , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, kvmarm@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Brown X-Mailer: b4 0.13-dev-099c9 X-Developer-Signature: v=1; a=openpgp-sha256; l=3590; i=broonie@kernel.org; h=from:subject:message-id; bh=Nuasx9Ylf/ySuoGV/J7jU7muHvsTQeng/1l4pki2YVk=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBkvnK45+pzq1hMYh7vNBEddQXBP/Zib2wAsrLN39VS 4uKPDWaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZL5yuAAKCRAk1otyXVSH0ONQB/ 9hD5i8L4J/RyK5GDvEKXhoT30mkQIEm2V1R8pAGD+HB+o7qJo2L/7vKRam8n914cShaH/Ki+POidad gYBZdQyFp4eOLGY85E9KNi/4x5F6kvV0jBD1EM4KErzrZE4r+HWDJ4QFiXL2wJNCR38NAvMIPJjpwC dSAecumgA73NDEeH/9kmTuh9bVOwwyVzSEudn2VEA59fcXevqmHhpUod5aisb/rAMNwg18z+ueXAgT JIO3d0PznDq3/xOBAdUusU/2mnBky3EiseJS6QHoUORpCSjnryyuS9f7QuZQUQ5QWALD/PRthFZHja 7MyRuPdnX9R2cOElJYdbu3NANETYWh X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772307004539170357 X-GMAIL-MSGID: 1772307004539170357 Since it is not possible to return from the function that enabled GCS without disabling GCS it is very inconvenient to use the signal handling tests to cover GCS when GCS is not enabled by the toolchain and runtime, something that no current distribution does. Since none of the testcases do anything with stacks that would cause problems with GCS we can sidestep this issue by unconditionally enabling GCS on startup and exiting with a call to exit() rather than a return from main(). Signed-off-by: Mark Brown --- .../testing/selftests/arm64/signal/test_signals.c | 17 ++++++++++++- .../selftests/arm64/signal/test_signals_utils.h | 29 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/arm64/signal/test_signals.c b/tools/testing/selftests/arm64/signal/test_signals.c index 00051b40d71e..30e95f50db19 100644 --- a/tools/testing/selftests/arm64/signal/test_signals.c +++ b/tools/testing/selftests/arm64/signal/test_signals.c @@ -7,6 +7,10 @@ * Each test provides its own tde struct tdescr descriptor to link with * this wrapper. Framework provides common helpers. */ + +#include +#include + #include #include "test_signals.h" @@ -16,6 +20,16 @@ struct tdescr *current = &tde; int main(int argc, char *argv[]) { + /* + * Ensure GCS is at least enabled throughout the tests if + * supported, otherwise the inability to return from the + * function that enabled GCS makes it very inconvenient to set + * up test cases. The prctl() may fail if GCS was locked by + * libc setup code. + */ + if (getauxval(AT_HWCAP2) & HWCAP2_GCS) + gcs_set_state(PR_SHADOW_STACK_ENABLE); + ksft_print_msg("%s :: %s\n", current->name, current->descr); if (test_setup(current) && test_init(current)) { test_run(current); @@ -23,5 +37,6 @@ int main(int argc, char *argv[]) } test_result(current); - return current->result; + /* Do not return in case GCS was enabled */ + exit(current->result); } diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.h b/tools/testing/selftests/arm64/signal/test_signals_utils.h index 222093f51b67..1cea64986baa 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.h +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.h @@ -16,6 +16,35 @@ void test_cleanup(struct tdescr *td); int test_run(struct tdescr *td); void test_result(struct tdescr *td); +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +/* + * The prctl takes 1 argument but we need to ensure that the other + * values passed in registers to the syscall are zero since the kernel + * validates them. + */ +#define gcs_set_state(state) \ + ({ \ + register long _num __asm__ ("x8") = __NR_prctl; \ + register long _arg1 __asm__ ("x0") = PR_SET_SHADOW_STACK_STATUS; \ + register long _arg2 __asm__ ("x1") = (long)(state); \ + register long _arg3 __asm__ ("x2") = 0; \ + register long _arg4 __asm__ ("x3") = 0; \ + register long _arg5 __asm__ ("x4") = 0; \ + \ + __asm__ volatile ( \ + "svc #0\n" \ + : "=r"(_arg1) \ + : "r"(_arg1), "r"(_arg2), \ + "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ + }) + static inline bool feats_ok(struct tdescr *td) { if (td->feats_incompatible & td->feats_supported)