From patchwork Mon Oct 9 12:09:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13413537 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BEFEE95A91 for ; Mon, 9 Oct 2023 12:14:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 11F8E8D0070; Mon, 9 Oct 2023 08:14:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A74B8D0031; Mon, 9 Oct 2023 08:14:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E3BE48D0070; Mon, 9 Oct 2023 08:14:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CF74E8D0031 for ; Mon, 9 Oct 2023 08:14:50 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9E9501CA6C7 for ; Mon, 9 Oct 2023 12:14:50 +0000 (UTC) X-FDA: 81325816740.09.D32EBA5 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf17.hostedemail.com (Postfix) with ESMTP id 9E1F44001B for ; Mon, 9 Oct 2023 12:14:48 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=n5UQQ1AT; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf17.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696853688; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=a4sdhvetqGV+nGZwp3/CWKNpIVlxBXbcidOEijLs/Qk=; b=ETdzcbPVQGrj9n6P1mjormcu1YCS+NUfZN/IeV4rVx3cXxWSHo0CukuCDIrdXmKKwXh0SN K6BbEqcTaOlwmkLYWlKTRADOxYBuK2ujihukTb78qzWU2fH5fDtNzX8KxgAcmK3dP4z6q2 EipWBbtFurdtDsDY95jrYirAH0xq91k= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=n5UQQ1AT; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf17.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696853688; a=rsa-sha256; cv=none; b=Qrcxu2U5OUIlCR7GXH/+qh7+qypj0rx7UdGzIxEb0t8Nl3MbtgaW+9T+uyTIaOceo4/naq rCdUFgfIZviBGpnq0zUJ0gSs52fGqCIZReF9xgBXGnozeS1ClSwvIly//fSgm8m1PScNRl iyI9wDRYM93yWZAwX2xZDlQDbIpxA60= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 577B3B81151; Mon, 9 Oct 2023 12:14:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87C14C433C8; Mon, 9 Oct 2023 12:14:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696853686; bh=n0brHaZlsdiu+vZdVacJ8xeue5gjh150PYMG3rRf1kA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=n5UQQ1AT5I82mU7Nx0+Y9k7xNEgSigXUYhr5lH2zAhdzjpcwZ8tovFxITNmUjsyzB lgWEwU/CIX2nK95j1QKfUTqy0dVyqg7ab/35dbMkWUEBFAzXyYkdzNxLeOEEO+Fbso nLY+fU7CxvXItc8JKRQgE6SH+um7qVxQZuEoMM8oELlDrauEECyhoRvdyo0SiroXCa EmYSuyUki5zAvADxN6aIvmHDtXnQAyZjEE/apK221patQzIMUW6FRzPZJr+gCFOTv1 pQAtS4zl+B8/9rPsm3VpVMIpN5cBdf1Soigrmu4XTQeb5edI/V7i8RapIBuF8JFcA7 6a3j+oCTK34wA== From: Mark Brown Date: Mon, 09 Oct 2023 13:09:10 +0100 Subject: [PATCH v6 36/38] selftests/arm64: Add GCS signal tests MIME-Version: 1.0 Message-Id: <20231009-arm64-gcs-v6-36-78e55deaa4dd@kernel.org> References: <20231009-arm64-gcs-v6-0-78e55deaa4dd@kernel.org> In-Reply-To: <20231009-arm64-gcs-v6-0-78e55deaa4dd@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 , Florian Weimer , Christian Brauner , 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-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=7315; i=broonie@kernel.org; h=from:subject:message-id; bh=n0brHaZlsdiu+vZdVacJ8xeue5gjh150PYMG3rRf1kA=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlI+2tveb6LcwYU7gt5D9M1Yzq33Su1fslnEnmUQoy SjnyNBGJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZSPtrQAKCRAk1otyXVSH0EV1B/ 9focZ0RCun/nSyD/YCRi05xh3aUeAY3eqz5BL3aQGBPQgmMG3yHfYyM6vm9FrA2pXjlP99T2v3wy+1 kVSVboJJMmImQFHJJn16MuZQzmFX8w4XVHQurQLPuKpWh3PjT7OjDwcblT15zbd87OB//9A9aLUs/v uEJFElHcaq+Ug126uPaYzJQ+fc1ISuIxC3t1jZP/6LjjxGJgeSz0nu4MK5g6tx7Cw+zY7NHlPeHGkl ZQH+jGU/3G9WZtRyDcA3garOiz/TAtphRjFPKloSfcvdRvlYt/ecDyi+IkkNXH3wD46lJnZWzOJ2L4 AhEUBi+nK7qZI8QOb8EVMOXU6wg/2M X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 9E1F44001B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: h64tmpyktekjp4ygy9apm7drgy6n6bt3 X-HE-Tag: 1696853688-196953 X-HE-Meta: U2FsdGVkX19RR6QpEZWAlpgBWR3VeiE2jWOTkZ4FNt9zUh7QTO8B7JC4jUg1LZB8ps+Qn9VStMvON49M7GJxV0NV/cXtNtUop1fMJlzaDUVCbiL6NVPZTlj9j3M6O4qOz3QKW282QM/Zmdx6L3HpHWvahqhI5UbqNHKx+s7VJrrbK7LBuOZ8pgvw+cGhooR1Iq1bOoM+QhuLT013jU1HD96SOIvcYdKaMmzeBOQZCNxWTVHJG+LtdVBn6iKCfeBhAKxwJmzKAlbgwHVwKl2WULCethQhqSCLJuSbyDXwKOoB+QwA7O+Wh5HgnHYoI9rPRV0g6CNIPOjHtNUXkpWkZ3S2WHzmidP0jC/eON5CBwHEjZ4mBACgGvFULX4CQsiGmXEp99SrnC6NVtu14AJV23rx8FlNnnWtMLxbMd1nB4f9eyOEb4TgbVIkx9U/Iaflz5rzWVUqgsLnuSngIetzLA1r3W8PoOiNqaNUc8WNMTTfHeBkfopoPjidj3K+BheMarH/P0J80/ijyPt4vYa+p9Rxn+suIgU1G+KuP+T/gVL8mmCUzSlUDnzN4xZPtJZ/G+koPxEAnoyStf8yNGSMiBx6Sgl8NJflhn7Bv9q0O3P8mFhMXdoyhJU4k6ag4L4XXFAhZ6BMbfh2psYHVQYCFtyPkVl1C5fiuAu7ge43olpybJVBhXODAy+r0DPwF9/5A5MeAM3dVE7CFKGHJdd+cbcRJ8XKB3pcKRXzIbPFJ2TJVjvOPdr+J9DgHwrRzbAScdzhfxcyEGvvR5tb5i/D0QaN/w9dGLnha+qCfBiCgKTQOhWFB/Uz0SJAcA5l2Vn2zacKUPIALgsZuMwkVGJ2+AFj34Ex5OIK1thxHeCsDFummq1IYGgk4u6VHVkp5eurx/uVSOKlQC9PMvtoAcbqbmFXB/F7bgI+OAi2vQ6DHlatQUQHNvcIpBkBpx2ImippA40RCtsm0Iub7ZUTWm8 eKNsIrLh XzrJIemYnZsXxzrYkGI/VD2zaF9uHUADS+ITVryxkOvsfvsiBI1NNAiWyXGdUuR/oHbdiWT4NVIEpRQRgH4K6AzBns1WLdFmSIbly2HkBwzTOeJzR3RQYstYqC9F0Yh7qfuTZPZIueM8apEIohqjBAZWv9n3ZYRJlqcnB4KqexCpM2jN6cph4jRxwBTatd+CJPyaPE5cavZ4JHY+eiQXukT1ljhzdyGYQEvI1oo9OUXTa+lTVkcfTPjNIddcfkMikPJbI3mdbplGJ/uxw3ZyZT/Zh5tBVm2av0zNwarVxAA68ERZ5gUMg2tDceRN3tZvhsSpj9mAoVGyKbfPVi6yYhvcpykzJPqqF9HcIO5P0Ga65Y85DXthVTZMURQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Do some testing of the signal handling for GCS, checking that a GCS frame has the expected information in it and that the expected signals are delivered with invalid operations. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/signal/.gitignore | 1 + .../selftests/arm64/signal/test_signals_utils.h | 10 +++ .../arm64/signal/testcases/gcs_exception_fault.c | 59 ++++++++++++++++ .../selftests/arm64/signal/testcases/gcs_frame.c | 78 ++++++++++++++++++++++ .../arm64/signal/testcases/gcs_write_fault.c | 67 +++++++++++++++++++ 5 files changed, 215 insertions(+) diff --git a/tools/testing/selftests/arm64/signal/.gitignore b/tools/testing/selftests/arm64/signal/.gitignore index 839e3a252629..26de12918890 100644 --- a/tools/testing/selftests/arm64/signal/.gitignore +++ b/tools/testing/selftests/arm64/signal/.gitignore @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only mangle_* fake_sigreturn_* +gcs_* sme_* ssve_* sve_* diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.h b/tools/testing/selftests/arm64/signal/test_signals_utils.h index 1e80808ee105..36fc12b3cd60 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.h +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.h @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -47,6 +48,15 @@ void test_result(struct tdescr *td); _arg1; \ }) +static inline __attribute__((always_inline)) uint64_t get_gcspr_el0(void) +{ + uint64_t val; + + asm volatile("mrs %0, S3_3_C2_C5_1" : "=r" (val)); + + return val; +} + static inline bool feats_ok(struct tdescr *td) { if (td->feats_incompatible & td->feats_supported) diff --git a/tools/testing/selftests/arm64/signal/testcases/gcs_exception_fault.c b/tools/testing/selftests/arm64/signal/testcases/gcs_exception_fault.c new file mode 100644 index 000000000000..532d533592a1 --- /dev/null +++ b/tools/testing/selftests/arm64/signal/testcases/gcs_exception_fault.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 ARM Limited + */ + +#include +#include +#include + +#include +#include + +#include "test_signals_utils.h" +#include "testcases.h" + +/* This should be includable from some standard header, but which? */ +#ifndef SEGV_CPERR +#define SEGV_CPERR 10 +#endif + +static inline void gcsss1(uint64_t Xt) +{ + asm volatile ( + "sys #3, C7, C7, #2, %0\n" + : + : "rZ" (Xt) + : "memory"); +} + +static int gcs_op_fault_trigger(struct tdescr *td) +{ + /* + * The slot below our current GCS should be in a valid GCS but + * must not have a valid cap in it. + */ + gcsss1(get_gcspr_el0() - 8); + + return 0; +} + +static int gcs_op_fault_signal(struct tdescr *td, siginfo_t *si, + ucontext_t *uc) +{ + ASSERT_GOOD_CONTEXT(uc); + + return 1; +} + +struct tdescr tde = { + .name = "Invalid GCS operation", + .descr = "An invalid GCS operation generates the expected signal", + .feats_required = FEAT_GCS, + .timeout = 3, + .sig_ok = SIGSEGV, + .sig_ok_code = SEGV_CPERR, + .sanity_disabled = true, + .trigger = gcs_op_fault_trigger, + .run = gcs_op_fault_signal, +}; diff --git a/tools/testing/selftests/arm64/signal/testcases/gcs_frame.c b/tools/testing/selftests/arm64/signal/testcases/gcs_frame.c new file mode 100644 index 000000000000..d67cb26195a6 --- /dev/null +++ b/tools/testing/selftests/arm64/signal/testcases/gcs_frame.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 ARM Limited + */ + +#include +#include +#include + +#include "test_signals_utils.h" +#include "testcases.h" + +static union { + ucontext_t uc; + char buf[1024 * 64]; +} context; + +static int gcs_regs(struct tdescr *td, siginfo_t *si, ucontext_t *uc) +{ + size_t offset; + struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context); + struct gcs_context *gcs; + unsigned long expected, gcspr; + int ret; + + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &expected, 0, 0, 0); + if (ret != 0) { + fprintf(stderr, "Unable to query GCS status\n"); + return 1; + } + + /* We expect a cap to be added to the GCS in the signal frame */ + gcspr = get_gcspr_el0(); + gcspr -= 8; + fprintf(stderr, "Expecting GCSPR_EL0 %lx\n", gcspr); + + if (!get_current_context(td, &context.uc, sizeof(context))) { + fprintf(stderr, "Failed getting context\n"); + return 1; + } + fprintf(stderr, "Got context\n"); + + head = get_header(head, GCS_MAGIC, GET_BUF_RESV_SIZE(context), + &offset); + if (!head) { + fprintf(stderr, "No GCS context\n"); + return 1; + } + + gcs = (struct gcs_context *)head; + + /* Basic size validation is done in get_current_context() */ + + if (gcs->features_enabled != expected) { + fprintf(stderr, "Features enabled %llx but expected %lx\n", + gcs->features_enabled, expected); + return 1; + } + + if (gcs->gcspr != gcspr) { + fprintf(stderr, "Got GCSPR %llx but expected %lx\n", + gcs->gcspr, gcspr); + return 1; + } + + fprintf(stderr, "GCS context validated\n"); + td->pass = 1; + + return 0; +} + +struct tdescr tde = { + .name = "GCS basics", + .descr = "Validate a GCS signal context", + .feats_required = FEAT_GCS, + .timeout = 3, + .run = gcs_regs, +}; diff --git a/tools/testing/selftests/arm64/signal/testcases/gcs_write_fault.c b/tools/testing/selftests/arm64/signal/testcases/gcs_write_fault.c new file mode 100644 index 000000000000..126b1a294a29 --- /dev/null +++ b/tools/testing/selftests/arm64/signal/testcases/gcs_write_fault.c @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 ARM Limited + */ + +#include +#include +#include + +#include +#include + +#include "test_signals_utils.h" +#include "testcases.h" + +static uint64_t *gcs_page; + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 452 +#endif + +static bool alloc_gcs(struct tdescr *td) +{ + long page_size = sysconf(_SC_PAGE_SIZE); + + gcs_page = (void *)syscall(__NR_map_shadow_stack, 0, + page_size, 0); + if (gcs_page == MAP_FAILED) { + fprintf(stderr, "Failed to map %ld byte GCS: %d\n", + page_size, errno); + return false; + } + + return true; +} + +static int gcs_write_fault_trigger(struct tdescr *td) +{ + /* Verify that the page is readable (ie, not completely unmapped) */ + fprintf(stderr, "Read value 0x%lx\n", gcs_page[0]); + + /* A regular write should trigger a fault */ + gcs_page[0] = EINVAL; + + return 0; +} + +static int gcs_write_fault_signal(struct tdescr *td, siginfo_t *si, + ucontext_t *uc) +{ + ASSERT_GOOD_CONTEXT(uc); + + return 1; +} + + +struct tdescr tde = { + .name = "GCS write fault", + .descr = "Normal writes to a GCS segfault", + .feats_required = FEAT_GCS, + .timeout = 3, + .sig_ok = SIGSEGV, + .sanity_disabled = true, + .init = alloc_gcs, + .trigger = gcs_write_fault_trigger, + .run = gcs_write_fault_signal, +};