From patchwork Sat Feb 3 12:26: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: 196284 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp996448dyc; Sat, 3 Feb 2024 04:43:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IFzkL5LRku2lyI6sY4oyk/Osg3hJpLa6rnaP3fr2zjD/4I0pHCmc5qd3BkjkdhLaK/S49Yl X-Received: by 2002:a05:622a:24f:b0:42c:c26:78f5 with SMTP id c15-20020a05622a024f00b0042c0c2678f5mr1317724qtx.46.1706964209048; Sat, 03 Feb 2024 04:43:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706964209; cv=pass; d=google.com; s=arc-20160816; b=neDEZ+QP1ctfaY4LeT+khbdvJGp8k6QPA7QigL9gXYOVGph17srUnLJ24kUm0w9mOQ abDcmNWOnDHpuicHl6SPoVKpfJzeMkLVirHyhGOcHOGkU5r3yRG4iCQz4q/dzx1jjN90 0W8astuGaIov7kvLQr8wPAwKBAc9DPc92eiXwZ/V616IMbvWi51rnKALPWd1VvQj12og +xZBo4u+Z68Fedas7ex4lvCoFfanwyKVsTRW+f6m241ogxYo1+idIHNF0HHHC209xlyT XaLi89f16A3BZlJQkZJo3mRX7rn62r1pGaqOTAtSrzM0VYx4lSJh1+qLSpaLCm0v6GKC d7XA== 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=Q63bLDXkh15yQCdV0K5VqiZFNax7wobma8vbysoaWMs=; fh=tWNH31s79K7TEBfiNPILNQPBuEoAIyi7WlmrE7B0xT4=; b=gxCf0PIs+uWMUC8mByVnaxbCB910clZu5rN9QlnbcSzA/s2kZJdsNeW6GMCRM3Pg29 HPML8CQZU9ddjGutpurwd1mtI7QvsYMFOm1opx/JVq7j6oFXAKYUu2v0zQONs+689VLt 9yIUlsjykIxkQwx5aqTVIeh0tkD7xKQ1fR2TtGjaXxViQDynm/yJTQ5HSzWz3VOTKMz2 nvEN6EFgjLjSYL4I5aIvDMx+IWKOQ9N+mm7izxicF7XK0X4GouZv6XZmhINaB6a/GGgl QJGk7OL0Xs8hIEBIpn0gy93hKefEeM2yZ5f7ZBZPZwXI+BYExlT+XUJurhQVhuCQIb0m LBDQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ou0+iCIH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-51077-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51077-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCVjDSluNYQkjyletb12yRsAMutnrssNE4+7mBL1CZi+OSYnp3tK7aOCMEBnfroXaiE3emcT5OJJ2HJjlK+xppmPQ53hYg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 11-20020ac84e8b000000b0042c17d04dddsi174883qtp.6.2024.02.03.04.43.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Feb 2024 04:43:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-51077-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ou0+iCIH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-51077-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51077-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id C66821C218EE for ; Sat, 3 Feb 2024 12:43:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1C8385FEF5; Sat, 3 Feb 2024 12:33:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ou0+iCIH" 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 61E537E78E; Sat, 3 Feb 2024 12:33:41 +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=1706963621; cv=none; b=T4OWlTCzBzYh0huRYy9P/oK53aWCa1LXy+ckchy1UeTZbr4X314ufbGxYKR6GrNnICKCwr7lVhRiW2XhpUgGn9RIpGh/ahwMs/VHGacQJhWqA4iib8O+gZaHqgqC+5QOQdvBa1PRi26g0w7QEwKjMFAbyrhuxKaq+sUzTc7QFTU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706963621; c=relaxed/simple; bh=29MX4bFmi9RjszKvoThz75zOXEvMrbFZDv9wXNIm3c0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kbcRdij6mgUf1taD1p7PZP5mFbMZkYE7/icPMV8BbTYubQ4lO/ZtwJ3OzxROyECue+8nwPYZeVkWvF4D70Nj3Jq9hlTXzzsyuF+kzwLBFyy5VdCTkb/gCcFnx1SD1EhxN65tpCylwk/wIsVUMwnyfQmTXGwQ7wqmbFSqVlM7CrY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ou0+iCIH; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E16E0C433F1; Sat, 3 Feb 2024 12:33:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706963621; bh=29MX4bFmi9RjszKvoThz75zOXEvMrbFZDv9wXNIm3c0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ou0+iCIHrfjXH8n3BCVtgt+iHJGGPftd3xtuxNQzXoESJCfCMvhUTf2x5E3rIuQXp Z7X1iVJst836kXKlagMfIlHB4EcQHl98pVo+QfyapUl9k537WYC6jKYvmBdZ8DuN4f swOLANkxDyBQO5rIaFMOPmeBkOOw6DHQ5/dWVmyJQJp3QN0dHZiqNYA1k3g+1/5HUb wikHa/mQQOEtYIMALYfqVSNvwK7Z7N4PoorqEccGi2S8fK3zwpvpkHwGYd7sUz7j0D IXCHps1tEZxuErKWv1XBKO/0kQVm6GNTaiMdxIaPagWkxXQwm+PO85dzIyxITFNfFD TgmaqF/7+K36g== From: Mark Brown Date: Sat, 03 Feb 2024 12:26:01 +0000 Subject: [PATCH v8 35/38] selftests/arm64: Add GCS signal tests Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-arm64-gcs-v8-35-c9fec77673ef@kernel.org> References: <20240203-arm64-gcs-v8-0-c9fec77673ef@kernel.org> In-Reply-To: <20240203-arm64-gcs-v8-0-c9fec77673ef@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 , Thiago Jung Bauermann , 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-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=7609; i=broonie@kernel.org; h=from:subject:message-id; bh=29MX4bFmi9RjszKvoThz75zOXEvMrbFZDv9wXNIm3c0=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlvjDsC+adCQ5LH7Db6WhjtpyGz4I1K5rQocla/DSr j0dGSSyJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZb4w7AAKCRAk1otyXVSH0Eh4B/ 9VPWk1n8iVrCOX/tNVqSxjuteZ6BZ4SFxLj1NSvlrss8g9uHuY3fK+z7N8fegHs6CpMUqAp5Ps4noG T4tDEHp1kDKvUgiKBiVS/th0nhCtsneRDl884ghHbL+4WQ3Y9i1VA+bGTN6+/AcTuHtst3o84jo4SX f9Yxd4zSCAFbTw6hOzwQKUbzkj32Xtj9V05+803T5/Tbq0j6cSrjLqzF10edbtBlDaHE7owmUzqKvm 6IhK3sBOJrT7uwbvUzBqfobQza7tQY9E2LBIzVe7WG1yqVOkKxRh0x6kji2WRdMCkL+evutA5AC/in +eWj3fFSAwg70jXfu4I5F02Wt9/uK7 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789881702298676039 X-GMAIL-MSGID: 1789881702298676039 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 | 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 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..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, +};