Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp622946pxb; Tue, 1 Feb 2022 07:11:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJwKnwhPxqT60sIHPzdbMvkDZBgN8Iud7/beEpSPxkxHiWoEMdNO8McN8+uQp78Jf49zXj4m X-Received: by 2002:a17:907:6ea7:: with SMTP id sh39mr20880672ejc.380.1643728271026; Tue, 01 Feb 2022 07:11:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728271; cv=none; d=google.com; s=arc-20160816; b=o0zEpi1c72yMAmGPQNFdZNAJuBbpfIQ/3F8UnSsz4N9hoZcd7LQ4dVEORA5hEmuJof OFdXw7w1L125EKD8p0usVS14NadYygBtnEkIuGXwsRUJzMOtY0AfHfyr9Zc4QVN65mTN W/O0Lb/qQVN/u1kO2fdxHIYAj0RkAviLOiv+F/lBx8yrmgvzA/v2N9lYY1RxuD8MMsTV ZU1SpTDF2PuzqfZ3ajkU5M8TYPjdIoEITE33mXkD4/CnrwyS04St+1gMG2v4gGNnuxAI Q0MGw2ud4TJLURw41AkhdPeiDVkdHj9a8qA+GN4+zX7GciimWIprcIs4SsEg3gnB5CTl EB4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=uytgQZEdScoiy/AeiNVQg/B+wY+T5Wt292Tx+jLqtSQ=; b=bHgPcyGH6Uo1er6cAlwPJYwEzDynkAcGkPQWmyAisERUjTFwWBz7mHetbqX17VQ8wX RANZLwrqIuiE8QagT0g1BKh45vILVCocn6bmp1pEJR+sE1hhxNiXDW/hdoRGRSWxvWFX H1/QUw64Rc1PL5NSXD3fWuXB9W48xvKDCNIEoB4c1xU6lQfe5ftu4gm2mS+5ou7vT8zz 95/yRGadEE6NtTc/O6a5m9AzYhWxfngaqCre8KiI6CCn6E5wSmUD6pNGdsCEvHaO9/uG 51uR7VcYgzDU9aSph0zL7xPSdEoB3/9QkJdG3q7GMn3lItPyFyKSwbHgLefhWnHyGl3/ Ckcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@intel.com header.s=Intel header.b=ZOSIUoV2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f18si9424109ejl.181.2022.02.01.07.10.44; Tue, 01 Feb 2022 07:11:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@intel.com header.s=Intel header.b=ZOSIUoV2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356631AbiA3VZw (ORCPT + 99 others); Sun, 30 Jan 2022 16:25:52 -0500 Received: from mga07.intel.com ([134.134.136.100]:9047 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356486AbiA3VYz (ORCPT ); Sun, 30 Jan 2022 16:24:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577895; x=1675113895; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=G4fVgUiNuZ57ki/aV9t4t6lONJ9wFjRKKu+IxwPcJt0=; b=ZOSIUoV2It2xfNWtpmAFFWKirdDjHIYy1YSLPTBe2PUU4E8ugaDHJsnc X2hwgoorJwzXBaNk26KbSNHybSb/1HuxJ71O5b898sDESEUR398UYdMKa W9Z33AgJA0jd8u5b5gRf5suIaxa1Nbc1xdhGpYpTXPMx7upevrIjyz9wZ ukk54NTvY2/G/ShC5HHHszLQ+ybbeXbPgxQcClceaNoME4E0LOGAn8Kux 6Dwtu6nCyxEivqsfovNdhJy4r8QbR1SMRkFgLoig86paesvcFd+BpqgpS bZzRtzGcWLEHZoe0yVLqwVHmY9syKkMN4WPcfQb+owB0ZW46c7dCSRakI w==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="310685834" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="310685834" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:22:13 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536857008" Received: from avmallar-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.123.171]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:22:12 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Dave Martin , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: rick.p.edgecombe@intel.com, Yu@vger.kernel.org, Yu-cheng Subject: [PATCH 33/35] selftests/x86: Add map_shadow_stack syscall test Date: Sun, 30 Jan 2022 13:18:36 -0800 Message-Id: <20220130211838.8382-34-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220130211838.8382-1-rick.p.edgecombe@intel.com> References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a simple selftest for exercising the new map_shadow_stack syscall. Co-developed-by: Yu, Yu-cheng Signed-off-by: Yu, Yu-cheng Signed-off-by: Rick Edgecombe --- v1: - New patch. tools/testing/selftests/x86/Makefile | 9 ++- .../selftests/x86/test_map_shadow_stack.c | 75 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/test_map_shadow_stack.c diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile index 8a1f62ab3c8e..9114943336f9 100644 --- a/tools/testing/selftests/x86/Makefile +++ b/tools/testing/selftests/x86/Makefile @@ -9,11 +9,13 @@ UNAME_M := $(shell uname -m) CAN_BUILD_I386 := $(shell ./check_cc.sh $(CC) trivial_32bit_program.c -m32) CAN_BUILD_X86_64 := $(shell ./check_cc.sh $(CC) trivial_64bit_program.c) CAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh $(CC) trivial_program.c -no-pie) +CAN_BUILD_WITH_SHSTK := $(shell ./check_cc.sh $(CC) trivial_program.c -mshstk -fcf-protection) TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \ check_initial_reg_state sigreturn iopl ioperm \ test_vsyscall mov_ss_trap \ - syscall_arg_fault fsgsbase_restore sigaltstack + syscall_arg_fault fsgsbase_restore sigaltstack \ + test_map_shadow_stack TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer @@ -105,3 +107,8 @@ $(OUTPUT)/test_syscall_vdso_32: thunks_32.S # state. $(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static + +ifeq ($(CAN_BUILD_WITH_SHSTK),1) +$(OUTPUT)/test_map_shadow_stack_64: CFLAGS += -mshstk -fcf-protection +$(OUTPUT)/test_map_shadow_stack_32: CFLAGS += -mshstk -fcf-protection +endif \ No newline at end of file diff --git a/tools/testing/selftests/x86/test_map_shadow_stack.c b/tools/testing/selftests/x86/test_map_shadow_stack.c new file mode 100644 index 000000000000..dfd94ef0176d --- /dev/null +++ b/tools/testing/selftests/x86/test_map_shadow_stack.c @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SS_SIZE 0x200000 + +void *create_shstk(void) +{ + return (void *)syscall(__NR_map_shadow_stack, SS_SIZE, SHADOW_STACK_SET_TOKEN); +} + +#if (__GNUC__ < 8) || (__GNUC__ == 8 && __GNUC_MINOR__ < 5) +int main(int argc, char *argv[]) +{ + printf("SKIP: compiler does not support CET."); + return 0; +} +#else +void try_shstk(unsigned long new_ssp) +{ + unsigned long ssp0, ssp1; + + printf("pid=%d\n", getpid()); + printf("new_ssp = %lx, *new_ssp = %lx\n", + new_ssp, *((unsigned long *)new_ssp)); + + ssp0 = _get_ssp(); + printf("changing ssp from %lx to %lx\n", ssp0, new_ssp); + + /* Make sure is aligned to 8 bytes */ + if ((ssp0 & 0xf) != 0) + ssp0 &= -8; + + asm volatile("rstorssp (%0)\n":: "r" (new_ssp)); + asm volatile("saveprevssp"); + ssp1 = _get_ssp(); + printf("ssp is now %lx\n", ssp1); + + ssp0 -= 8; + asm volatile("rstorssp (%0)\n":: "r" (ssp0)); + asm volatile("saveprevssp"); +} + +int main(int argc, char *argv[]) +{ + void *shstk; + + if (!_get_ssp()) { + printf("SKIP: shadow stack disabled."); + return 0; + } + + shstk = create_shstk(); + if (shstk == MAP_FAILED) { + printf("FAIL: Error creating shadow stack: %d\n", errno); + return 1; + } + try_shstk((unsigned long)shstk + SS_SIZE - 8); + + printf("PASS.\n"); + return 0; +} +#endif -- 2.17.1