From patchwork Thu Aug 1 12:07:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13750417 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 8514AC3DA64 for ; Thu, 1 Aug 2024 13:01:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A1D76B00D7; Thu, 1 Aug 2024 09:01:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32AC36B00D9; Thu, 1 Aug 2024 09:01:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A3556B00DA; Thu, 1 Aug 2024 09:01:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E49136B00D7 for ; Thu, 1 Aug 2024 09:01:45 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3A5AE140D4C for ; Thu, 1 Aug 2024 13:01:45 +0000 (UTC) X-FDA: 82403688570.06.18E488D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id DAE3E14003D for ; Thu, 1 Aug 2024 13:01:41 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QeLlBHsh; spf=pass (imf23.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722517257; 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=gBkkGTg9iwkURdLLuf+UpkR7ejhULQidz4/W46P4K/c=; b=SsitSb+ynu77Kvk7yMq1Dm/8G2xfaS2M3p0XxQ9OnjQXw4xo/iw0Z9N/bh1Y9zsrpFsU+E zGeE58exomim0z60dMy+toUsXsFapNZX0AQGqrxjy9V8/iUr1wfh1kQKtH/u7vHTNkO8nA lMyo+k/ARVJMLVsaEVTXDziP0NujbLk= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QeLlBHsh; spf=pass (imf23.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722517257; a=rsa-sha256; cv=none; b=SRNU+UlmQRpkqt1s+Rw5cT5NCMk664E252PiDj03yBZytl1y3LRdDTusPcj10JfKcbbyv6 2kaPuIUgShAwCnLlKo1TGAxkjrEbrJx4vEpSxiJlo65JjuvbenC1wrbxqRgKv/5ZEMgtnA v51c/LokMn1BgR0/JAiV9vLarlO9hZM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D709262864; Thu, 1 Aug 2024 13:01:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28AD9C4AF0F; Thu, 1 Aug 2024 13:01:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722517300; bh=6kf8vUeE2/3/9q62BhJqQa6tQ8qTDalv3nPeOQkuUQk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=QeLlBHshq5P3qUUyfk8CRsRNpyqn0q57xNSDHPTQ6q1hGzJWNs7qGlaOJIGObFmE+ +K7CJRAveGuipAwXMwq5q9D8OTdH+cPigG3T08ObXRZTQed2/245kPDwVGxjlfU372 U6lz/IxwXQuNFMD41f8cTskuzUxtBh4ekcSGDr8Jlns1YvB+6SFED8gkTLAiZcnTrn LuJQ9+urmKyxyHj5P3x+s/TaASKq6bDfBdnLZzBjGtM9XuHa2bhbrUEN9xmS8k3GKd oOV40cTBc967x5JL8iSK1RpHh3zhNfJksh6haNjSxsR3JLWpGbQkXVyzSTeJKcJdtY pqLjCVcN/gdgA== From: Mark Brown Date: Thu, 01 Aug 2024 13:07:04 +0100 Subject: [PATCH v10 37/40] kselftest/arm64: Add GCS signal tests MIME-Version: 1.0 Message-Id: <20240801-arm64-gcs-v10-37-699e2bd2190b@kernel.org> References: <20240801-arm64-gcs-v10-0-699e2bd2190b@kernel.org> In-Reply-To: <20240801-arm64-gcs-v10-0-699e2bd2190b@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 , Shuah Khan , "Rick P. Edgecombe" , Deepak Gupta , Ard Biesheuvel , Szabolcs Nagy , Kees Cook Cc: "H.J. Lu" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Florian Weimer , Christian Brauner , Thiago Jung Bauermann , Ross Burton , 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.15-dev-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=7642; i=broonie@kernel.org; h=from:subject:message-id; bh=6kf8vUeE2/3/9q62BhJqQa6tQ8qTDalv3nPeOQkuUQk=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmq4YiilzosoycWemp86oXN2aAuvoGrtbZAHaiddpG iIMJiUGJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZquGIgAKCRAk1otyXVSH0DZUB/ 0TviaWwxSqh8BYp1zmU3cW61qEV3ideNIDwUSV+cLGZ+YR5Qzcp9E26Z7asPuhrUPHthXHL707fs8D VjVJzMku6aMeNJYxh360l8WTipPLqNFZ+UJ0cU9fbdPhbo8IyITKMmqn+EZIuFfLEMgfVjraFT5ggf fGE/19A+C75FGjCKT66LLzTU9WERRUohuNmyY95fidM8uuYcZdlP8IebAYK63KxpNM0gUmi8/MVQuZ YVKh30mJt7L+oZQA1xXtHAez0wx8f3oBLc9GWjftEtdDAayngF6ZffIKlEiNHKxXQQevQqnBwDBZ6x sLbAlttxAm9Ek0MvtSwXEYqHV01QSq X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: axih8e6rk7uuzeexertims5c97ct5mbk X-Rspam-User: X-Rspamd-Queue-Id: DAE3E14003D X-Rspamd-Server: rspam02 X-HE-Tag: 1722517301-85727 X-HE-Meta: U2FsdGVkX18CwB+ojyi/ZHUH4XeUxPIT85DA1Wjq3apM4py1bctc2E7SLIrQADv4935/koXDu7Sb4X2Qa/HuO+OFEV1iu+9hz7m8IhA31Ic4t9TUtNbFqj16wUR+fxfr9P7LSpyafJTutqXxPQUbpJxQXyagWT+Dkl+/7uoJyNUgH3eusDlD/hBz2yg1KM5vDoRXWSg8EAgco2lEGwoAJ36x9uC05lygtw1zI73NG691+8BqM0mJRkBqgOEOqoHVJHayaenXd/byeAo8QG0tfroCvD1/UwwYAkMYYW5KTzICSBsHFivxVsHXsYnaw763shY787GgDZnWQjuO7krM/ByruhAxDtl7M05FfFfPIYUiUwBLGbCEtQKYFp40tsw+0IMXLnKdNucvUPscx9MUltl4r3A3wR7KI0I4ij1BH+X+y56jFPIYEa6tbuEDN+ScEDk9mmk8u+HGrCKMje+PyhfVADA15UcBWedSQRuDub+Gf4+5stmelZko1fab2UK1M8Uo9U1O8S4bAKiK+3g2Fczu4rjykAEhUVWxVMTPRgozC+dfwh7qpSuum4q7NhiwWJMC6UBb2BfCpkyZAVf0mPvwG1ZXbf1aAHYO2+/EvP2WhMHpzrOl+bj+jp2i1UKJoggzKWXx+j4pXjxnkzVk69QoeRS2Qun+s7NlbtlsWcuzVxKShArt+a7nI2XWYzxA/afZeXW0siwpPjDy5kIesY8qp4MhVDNUfbW2LqCq+SaYS8R73VD0Fsg9BsyxYWbew1OMhrLYyrz77a2V5nT5Ckwdtg9FCODmz6NcJTq2g+V/IgwgNH0mql6kTRZax7F24mdUgbw6aPmVSM5Rqv0i9oUYfMryTctFIt7IcNso4+cQVHrb+sBCq7e7cte4pUCTvEWCvH/r0/34uSKmtJdMzlWwmM7kXamvohuRBSt2gCLZqjvyFSe1WxvpXxopQj7ubi9ygdpiuo+M75A/nJd ro5rdICb ZqbCGIX/Tzk3xrr5RS+ePHHJYSJVNxo7/WZ7Bsmpk+SSA8GD+7zeHR5KVVDHL9+fUhf7rf2BVFnv2Wg4F4fKMlFdoJTJVtm6k2T3Omc98VULlC4ZTTv4znnnZ+fU7kBFWSLT+2JVeAMmfHMzSofK1zBvrtfDm0GDtBcvcAovJsm8q2xWouKV2YPI+CoffK/Q8BuL3jl1Eycg+JYC0epFU764DYSXb3r27pHBgDeWrOdP4k+ai7XNlGcDp80ORS1+dSML4rCjx+P4b4kAQmtxFqP96DigdbnzXyT4GVCGXpVbENfjqOFxynB8KPA7t+kFCZBNzZp5uBZxHXNK+UVZHQmB9FwdMZ6SEpwW8SUR6fZ8r004YzBRevI5PATFyJh1ZMt0Q 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: List-Subscribe: List-Unsubscribe: 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. Reviewed-by: Thiago Jung Bauermann 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 | 62 +++++++++++++++ .../selftests/arm64/signal/testcases/gcs_frame.c | 88 ++++++++++++++++++++++ .../arm64/signal/testcases/gcs_write_fault.c | 67 ++++++++++++++++ 5 files changed, 228 insertions(+) diff --git a/tools/testing/selftests/arm64/signal/.gitignore b/tools/testing/selftests/arm64/signal/.gitignore index 1ce5b5eac386..75d691c13207 100644 --- a/tools/testing/selftests/arm64/signal/.gitignore +++ b/tools/testing/selftests/arm64/signal/.gitignore @@ -2,6 +2,7 @@ mangle_* fake_sigreturn_* fpmr_* +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..6228448b2ae7 --- /dev/null +++ b/tools/testing/selftests/arm64/signal/testcases/gcs_exception_fault.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 ARM Limited + */ + +#include +#include +#include + +#include +#include + +#include "test_signals_utils.h" +#include "testcases.h" + +/* + * We should get this from asm/siginfo.h but the testsuite is being + * clever with redefining siginfo_t. + */ +#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..b405d82321da --- /dev/null +++ b/tools/testing/selftests/arm64/signal/testcases/gcs_frame.c @@ -0,0 +1,88 @@ +// 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; + uint64_t *u64_val; + 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; + } + + /* Ensure that the signal restore token was consumed */ + u64_val = (uint64_t *)get_gcspr_el0() + 1; + if (*u64_val) { + fprintf(stderr, "GCS value at %p is %lx not 0\n", + u64_val, *u64_val); + 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..faeabb18c4b2 --- /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 453 +#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, +};