Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp1011932rdf; Wed, 22 Nov 2023 03:20:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IFRJ8RV6Ga28csjfTy3HWK76ARHL4cDhee/YlLZYDvZHcVjxdOVcBNPYifGNBtg6cKtS8bh X-Received: by 2002:a05:6a00:1881:b0:693:3c11:4293 with SMTP id x1-20020a056a00188100b006933c114293mr2373670pfh.14.1700652019372; Wed, 22 Nov 2023 03:20:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700652019; cv=none; d=google.com; s=arc-20160816; b=wrTg6vly2gWOJV6TJ3FKTwqYn6oAg+5///eGSsgHQOlNQbgqTeLmfQJCVMPMjKwOlW p7LgHahzv8gOa71mPC1idJyJDUKzUmh2pJHOhD24sZfxIay6FvSCLqMsW6XvCSUhWDtV 3K9JIGG5/ypkRR3TGM0rJHGlBdW6rBdk32bJwSljQSbbEPiVVPuviWnshKtBHdn0lhhc JYUjOsFbItaZk6Mq+LQwSqgCaDW/tyugaS95ABriGagc86RcFjNjJ26JsC5kMmuVR3GK sQX8FRCfiir7ydEv7sPHuroh6k5aZLtmIsBKXyqefyY122U48YOy0GdrJ/HqnvNSFLxN 994A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=b3kjvu0VloZP4KQiHhVadM49un1kZn5Ae0fw9syYhgw=; fh=3paNYYQk5xJEGFEjUGROphGxVtRK5oYV+hcyZ1hy/J4=; b=nG8JboH9xtPBMchDbkzNGa5wbLA+GXQWeUCwcCFNAWe0GnaZIAC5YLGIozmxu8rHbo wNrNw4FoipwXdwS3my2lkjVwZHMyurF1Z10aKNmz6idhaESyKJjrFOQSH0peGBtlzNEC FRdMm/yc/CqnLOPhJH00dQnoR0iVr6fl6QYtRykCw//uclal3CdCMhfjFLZ2hX8yW8AE bodD/MNfTt6gZiNkKQXnikrHj3KNQ//VOoSmckCp2GbH8a1PKCHneXYozDWNPy2uvoYV QVTbwgAURT9gl9y7zZNVckqRCYx2k9gkl8km0bWhW2bmmIs1EadRIYODloQCaFeWyGyL wW1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=b9hBk4aI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id g1-20020aa796a1000000b006cb5009c1e1si9598041pfk.365.2023.11.22.03.20.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 03:20:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=b9hBk4aI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id C27F4804C562; Wed, 22 Nov 2023 03:20:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229806AbjKVLUB (ORCPT + 99 others); Wed, 22 Nov 2023 06:20:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229714AbjKVLT7 (ORCPT ); Wed, 22 Nov 2023 06:19:59 -0500 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1947D8 for ; Wed, 22 Nov 2023 03:19:53 -0800 (PST) Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-507be298d2aso8679000e87.1 for ; Wed, 22 Nov 2023 03:19:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1700651992; x=1701256792; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=b3kjvu0VloZP4KQiHhVadM49un1kZn5Ae0fw9syYhgw=; b=b9hBk4aI07U7BHGkvvcEj++6n+o412jYbFGek2hsPU2ARW23/a6t9wGJ7+/FBtV8b3 RuCcEUa6XcWCpe7L0q5AnVmEUJPY9OJvqSRXKu3Tt59F6v7fOUjhSO75gPVwcfRpg4aa ZUJOdNWQrUitW0pVEUSAinyZNUoKUsXq+ZC6rMF9e7UOEKYAWeRYHaYaAVNDHXRI9Syz fmQY41OaliII61zBkxhVKwoQYMbqoNCG4juKPWIJ3GSRIH0uGNZN/Es+6dYkV2hqoYsA eWpdKzfaAjPSKGhQR4XIc6TxhUQyK43rHssz8H5vFXveEEM5fKroOD/yxFomIuydZtTh XEAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700651992; x=1701256792; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=b3kjvu0VloZP4KQiHhVadM49un1kZn5Ae0fw9syYhgw=; b=IV0zv/jWJTVM5I0jmCtsjeSskZj/xxCg2PqKAliwL1N655/VUZ2c2w8Nzk4oXiFu/o z1oUgB/QNjuZ6hWoJzD2I3y81tPxq7kivbQYWC8GoKguK233Y/igGU2ufE/O+gHYug1q zvdjY24I32XF/2HQnIPgMiAY95mBW0Ib/NokfNmrghmwyi1Zf1F9psvjtNIJuKIUddsy wVLZbQAGt4TzOpHdC7h+dibNu42OnXMObtW5z9liDDtj0mEHLPdyJk0UogHi/v/f2BKF z/XnLFG5QGOkY9f3q0Em57L3Ag6p0lg+AlL6G6xzc44VVBnwfB9eplM+erhhIH9fXFxc qqYA== X-Gm-Message-State: AOJu0YzIXkU5eZvvPw39ryDfCzz9SxcUrkGzyzdhjrw6vjRstzHYU70f XJnBP5CvYWYOv0Oa6uQZQnWk3Q== X-Received: by 2002:a05:6512:544:b0:509:4530:e7fb with SMTP id h4-20020a056512054400b005094530e7fbmr1229715lfl.32.1700651991933; Wed, 22 Nov 2023 03:19:51 -0800 (PST) Received: from mutt (c-9b0ee555.07-21-73746f28.bbcust.telenor.se. [85.229.14.155]) by smtp.gmail.com with ESMTPSA id f19-20020a0565123b1300b0050aaaa37f16sm1205123lfv.208.2023.11.22.03.19.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 03:19:51 -0800 (PST) Date: Wed, 22 Nov 2023 12:19:49 +0100 From: Anders Roxell To: Mark Brown Cc: "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 , linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Kees Cook , jannh@google.com, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org Subject: Re: [PATCH RFT v3 5/5] kselftest/clone3: Test shadow stack support Message-ID: <20231122111949.GB364395@mutt> References: <20231120-clone3-shadow-stack-v3-0-a7b8ed3e2acc@kernel.org> <20231120-clone3-shadow-stack-v3-5-a7b8ed3e2acc@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20231120-clone3-shadow-stack-v3-5-a7b8ed3e2acc@kernel.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 22 Nov 2023 03:20:16 -0800 (PST) On 2023-11-20 23:54, Mark Brown wrote: > Add basic test coverage for specifying the shadow stack for a newly > created thread via clone3(), including coverage of the newly extended > argument structure. > > In order to facilitate testing on systems without userspace shadow stack > support we manually enable shadow stacks on startup, this is architecture > specific due to the use of an arch_prctl() on x86. Due to interactions with > potential userspace locking of features we actually detect support for > shadow stacks on the running system by attempting to allocate a shadow > stack page during initialisation using map_shadow_stack(), warning if this > succeeds when the enable failed. > > Signed-off-by: Mark Brown > --- > tools/testing/selftests/clone3/clone3.c | 117 ++++++++++++++++++++++ > tools/testing/selftests/clone3/clone3_selftests.h | 7 ++ > 2 files changed, 124 insertions(+) > > diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c > index 6adbfd14c841..0f9f99dc5aac 100644 > --- a/tools/testing/selftests/clone3/clone3.c > +++ b/tools/testing/selftests/clone3/clone3.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -21,6 +22,10 @@ > #include "../kselftest.h" > #include "clone3_selftests.h" > > +static bool shadow_stack_enabled; > +static bool shadow_stack_supported; > +static size_t max_supported_args_size; > + > enum test_mode { > CLONE3_ARGS_NO_TEST, > CLONE3_ARGS_ALL_0, > @@ -28,6 +33,7 @@ enum test_mode { > CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG, > CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG, > CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG, > + CLONE3_ARGS_SHADOW_STACK, > }; > > typedef bool (*filter_function)(void); > @@ -44,6 +50,36 @@ struct test { > filter_function filter; > }; > > +#ifndef __NR_map_shadow_stack > +#define __NR_map_shadow_stack 453 > +#endif > + > +/* > + * We check for shadow stack support by attempting to use > + * map_shadow_stack() since features may have been locked by the > + * dynamic linker resulting in spurious errors when we attempt to > + * enable on startup. We warn if the enable failed. > + */ > +static void test_shadow_stack_supported(void) > +{ > + long shadow_stack; > + > + shadow_stack = syscall(__NR_map_shadow_stack, 0, getpagesize(), 0); > + if (shadow_stack == -1) { > + ksft_print_msg("map_shadow_stack() not supported\n"); > + } else if ((void *)shadow_stack == MAP_FAILED) { > + ksft_print_msg("Failed to map shadow stack\n"); > + } else { > + ksft_print_msg("Shadow stack supportd\n"); > + shadow_stack_supported = true; > + > + if (!shadow_stack_enabled) > + ksft_print_msg("Mapped but did not enable shadow stack\n"); > + > + munmap((void *)shadow_stack, getpagesize()); > + } > +} > + > static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) > { > struct __clone_args args = { > @@ -89,6 +125,9 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode) > case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG: > args.exit_signal = 0x00000000000000f0ULL; > break; > + case CLONE3_ARGS_SHADOW_STACK: > + args.shadow_stack_size = getpagesize(); > + break; > } > > memcpy(&args_ext.args, &args, sizeof(struct __clone_args)); > @@ -179,6 +218,26 @@ static bool no_timenamespace(void) > return true; > } > > +static bool have_shadow_stack(void) > +{ > + if (shadow_stack_supported) { > + ksft_print_msg("Shadow stack supported\n"); > + return true; > + } > + > + return false; > +} > + > +static bool no_shadow_stack(void) > +{ > + if (!shadow_stack_supported) { > + ksft_print_msg("Shadow stack not supported\n"); > + return true; > + } > + > + return false; > +} > + > static size_t page_size_plus_8(void) > { > return getpagesize() + 8; > @@ -322,16 +381,74 @@ static const struct test tests[] = { > .expected = -EINVAL, > .test_mode = CLONE3_ARGS_NO_TEST, > }, > + { > + .name = "Shadow stack on system with shadow stack", > + .flags = CLONE_VM, > + .size = 0, > + .expected = 0, > + .e2big_valid = true, > + .test_mode = CLONE3_ARGS_SHADOW_STACK, > + .filter = no_shadow_stack, > + }, > + { > + .name = "Shadow stack on system without shadow stack", > + .flags = CLONE_VM, > + .size = 0, > + .expected = -EINVAL, > + .e2big_valid = true, > + .test_mode = CLONE3_ARGS_SHADOW_STACK, > + .filter = have_shadow_stack, > + }, > }; > > +#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 ENABLED_SHADOW_STACK > +static 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 ENABLE_SHADOW_STACK Should this be ENABLED_SHADOW_STACK ? Built this patchset for x86 gave me this build error: make[4]: Entering directory '/home/anders/src/kernel/linux/tools/testing/selftests/clone3' x86_64-linux-gnu-gcc -g -std=gnu99 -isystem /home/anders/.cache/tuxmake/builds/513/build/usr/include clone3.c -lcap -o /home/anders/.cache/tuxmake/builds/513/build/kselftest/clone3/clone 3 clone3.c:436:13: error: redefinition of 'enable_shadow_stack' 436 | static void enable_shadow_stack(void) | ^~~~~~~~~~~~~~~~~~~ clone3.c:426:20: note: previous definition of 'enable_shadow_stack' with type 'void(void)' 426 | static inline void enable_shadow_stack(void) | ^~~~~~~~~~~~~~~~~~~ make[4]: Leaving directory '/home/anders/src/kernel/linux/tools/testing/selftests/clone3' make[4]: *** [../lib.mk:181: /home/anders/.cache/tuxmake/builds/513/build/kselftest/clone3/clone3] Error 1 make[3]: *** [Makefile:178: all] Error 2 make[3]: Target 'install' not remade because of errors. make[2]: *** [/home/anders/src/kernel/linux/Makefile:1362: kselftest-install] Error 2 Cheers, Anders