From patchwork Sat Feb 3 00:04:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 196113 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp771789dyc; Fri, 2 Feb 2024 16:17:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IG584H/a3QbkJQNX9wbNDEDUStdoDjzexLxSfWt4zwP0x7JR0E9vk7ZSCxSX/UidSM/7ZeF X-Received: by 2002:aa7:d494:0:b0:560:463:6f35 with SMTP id b20-20020aa7d494000000b0056004636f35mr674971edr.10.1706919426619; Fri, 02 Feb 2024 16:17:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919426; cv=pass; d=google.com; s=arc-20160816; b=KvXTKeobf/oB5eYKVuV0Upz7FTmLQrvpbU28VJlKHjfYtGUarxTx1YOlnYRkTEg3k7 fQxKkdrPud1z/xpIxx5U/LAwXl85Wrwhoi6U6JrdCdrY5XvTvpCWhk2lNHixZfGfU3hc RzkVnaL/gcnAnmryvqBillpya/Wm+WRDTeoNGWYVpFApR+wu5mK/fqeu7/FqNsbV4mLC vzXxvSFxbxn8aOcQ2Y1hyqvgDz+X51O6UVDxx0+6ptR+dffzlJCafdgWzJD6OwxknPXx H5Ym5whV/hfoiZKDj9TGgHkn2d+Swsl8Px9YpDbxb3Y8zAuKKFePuiWAlw/p7q5MJKE5 aGFw== 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=ZUGGnLCx0CrON6JfEIDV0WoI0FeeuEVvFoTR/gd4J/M=; fh=x1G6e7CN+fq516B+/h87Vpoz0TDLRe1LD0gNZ1qLQgA=; b=JstsJ810EYrX4WCTOCMRT8l9NbB/tU441aH09t/582FI542TEeh8zo8W6CWanOpI0O cji2v6tiI7lEN7GbItDYKKfc0N5rIDXYH8Zst9Y4Eq6/p93VOI9vrh8gDBZXOpWJ29Wm 77Ng3U4dZ0x0O2G0aLs6/u8lOeHRBFGuIZ9YjnmEOAIph+zNcDd4zsqxChQhJE/tMhyq ZCmrAKlRNQqeXZD0xHKTVZ54KZRlolcpuCpDTfYCo6NMAZpkMB7EHSkMFFhCrcCgAusI wZX2AkXSTnxVnxXOERZrSdK3uYhEbA1ezn1Z/V7bMooEiOvHbqMHXhx0321TEvkKdXhR UI7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rc8YHIgG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCVkmZB+zPFgtEaC848w2izktYPHrcfzRu9OFLBvlhE6Gi7j/wrfNe3gcAYw5xNGX++c7bP62X+BogrBcz5Hc0YNIiHzTQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a12-20020aa7d74c000000b0055fec51fafdsi1136917eds.646.2024.02.02.16.17.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:17:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rc8YHIgG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50740-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 1E6CA1F2AE3F for ; Sat, 3 Feb 2024 00:06:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8FB55680; Sat, 3 Feb 2024 00:05:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rc8YHIgG" 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 14460538D; Sat, 3 Feb 2024 00:05:36 +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=1706918737; cv=none; b=Yt0PBukC9WFGvLRKsS2v6oZIXFtI3iEulvNBdELlIJD9/jgcLh4pyzeei3bc5XgXWtKJcsFZOHi6RL9D0odjLIYdbk2KT1xaoPoviyiOTuqtnaLZob0knH+0yh8a4zKB/6PzHXAyrKwz3sd+BTQjEwj8J7iRkaRZWX4LsKZwvK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918737; c=relaxed/simple; bh=CucuMui4D2UywYMkY4QvFT3PXHDtuX4p82gJW7uHGUw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZcRzjMYJgri78UX5H8nq9I6rsYg84KvadjdvVbU2It8MWfpc+VMBZdl8I36rb9KlpjhwC13r+DdlMT2zXQhfoW1aDQjg2xWThdiUdNXoDe3d+yeH8VlXyUgnrDfkXckvILY0zMfecSfVDZiLRBkjArG7tTP/vwU+PBA9lA5kynQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rc8YHIgG; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4717C433B2; Sat, 3 Feb 2024 00:05:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706918736; bh=CucuMui4D2UywYMkY4QvFT3PXHDtuX4p82gJW7uHGUw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rc8YHIgGmt3wOb/Joq6gRhlKp+Wmv3UhiJJKB6CMYX6xon6hoPtksJ6gHmhJIF3v/ 0i+yP2irL0QWfD/KhjdpFfyzQ/AZRR3UOLMLN3t5ranMSazDmRnHK7ecYEZpLXSU6M R3AuGfNWkMw1Bguiwbuz/QYzGlmoFLYVdVp9MCAiODwLPKcRz9465tiZv3pLDIiDf4 zAOEkQSmIdfM6Kd/kIkt6t9WEdbnEZ5Pco3Q8/CWLoAH0w+ZlO5rrtIftA1RRtC8Pg kJJnw0c04imrYu8ZyabBUAnox+vJJMJqZJohXy8JmjHbhNuUFicAqOMSzXlUcD2otB 83tegOSPLZMPQ== From: Mark Brown Date: Sat, 03 Feb 2024 00:04:58 +0000 Subject: [PATCH RFT v5 2/7] selftests: Provide helper header for shadow stack testing Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240203-clone3-shadow-stack-v5-2-322c69598e4b@kernel.org> References: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> In-Reply-To: <20240203-clone3-shadow-stack-v5-0-322c69598e4b@kernel.org> 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 X-Mailer: b4 0.13-dev-a684c X-Developer-Signature: v=1; a=openpgp-sha256; l=2532; i=broonie@kernel.org; h=from:subject:message-id; bh=CucuMui4D2UywYMkY4QvFT3PXHDtuX4p82gJW7uHGUw=; b=owGbwMvMwMWocq27KDak/QLjabUkhtS9zZYPtCb2voqeGvNkGePn0nauE0o6L+o6O+Z9teqMZJGa t9Gtk9GYhYGRi0FWTJFl7bOMVenhElvnP5r/CmYQKxPIFAYuTgGYyPML7P9UbzaHdov8EHj+u7o3sE KttXrrdV6LNKOuGKOiuVEvVlQWeV5f7O22g7/jDfeKG39YFK82ZC2K8n1leTCvfbvWw+DJeuzyMxlT QrQ0DQ9VnjebujWixHVmmDw3v3rC0vcuEws+701IPNR+/97TcC9l1gn14pcDpMwnX9a9OKe0PJ+h4o WjtcZCmQqnQ3E/twc5fX9VEPrUSH7KJ5n5PmarVnNaBfy991ze4uPi5W4WrArSr7csEUjODzywSDyU e0JHTs6m59PL2otCVbhNlD/tTPxza3aj6qHnyyeYe3UV3ztWWF+TsZtD0bP3Rvm5tBP+IjP4hNZ562 lOVbddahG2sCvgQqmLoecnu5+WAA== X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834744633935717 X-GMAIL-MSGID: 1789834744633935717 While almost all users of shadow stacks should be relying on the dynamic linker and libc to enable the feature there are several low level test programs where it is useful to enable without any libc support, allowing testing without full system enablement. This low level testing is helpful during bringup of the support itself, and also in enabling coverage by automated testing without needing all system components in the target root filesystems to have enablement. Provide a header with helpers for this purpose, intended for use only by test programs directly exercising shadow stack interfaces. Signed-off-by: Mark Brown Reviewed-by: Rick Edgecombe --- tools/testing/selftests/ksft_shstk.h | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tools/testing/selftests/ksft_shstk.h b/tools/testing/selftests/ksft_shstk.h new file mode 100644 index 000000000000..85d0747c1802 --- /dev/null +++ b/tools/testing/selftests/ksft_shstk.h @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Helpers for shadow stack enablement, this is intended to only be + * used by low level test programs directly exercising interfaces for + * working with shadow stacks. + * + * Copyright (C) 2024 ARM Ltd. + */ + +#ifndef __KSFT_SHSTK_H +#define __KSFT_SHSTK_H + +#include + +/* This is currently only defined for x86 */ +#ifndef SHADOW_STACK_SET_TOKEN +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) +#endif + +static bool shadow_stack_enabled; + +#ifdef __x86_64__ +#define ARCH_SHSTK_ENABLE 0x5001 +#define ARCH_SHSTK_SHSTK (1ULL << 0) + +#define ARCH_PRCTL(arg1, arg2) \ +({ \ + long _ret; \ + register long _num asm("eax") = __NR_arch_prctl; \ + register long _arg1 asm("rdi") = (long)(arg1); \ + register long _arg2 asm("rsi") = (long)(arg2); \ + \ + asm volatile ( \ + "syscall\n" \ + : "=a"(_ret) \ + : "r"(_arg1), "r"(_arg2), \ + "0"(_num) \ + : "rcx", "r11", "memory", "cc" \ + ); \ + _ret; \ +}) + +#define ENABLE_SHADOW_STACK +static inline __attribute__((always_inline)) void enable_shadow_stack(void) +{ + int ret = ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK); + if (ret == 0) + shadow_stack_enabled = true; +} + +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#ifndef ENABLE_SHADOW_STACK +static inline void enable_shadow_stack(void) { } +#endif + +#endif + +