From patchwork Wed Jan 25 23:24:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13116398 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A868CC54E94 for ; Wed, 25 Jan 2023 23:24:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235298AbjAYXYf (ORCPT ); Wed, 25 Jan 2023 18:24:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235286AbjAYXYe (ORCPT ); Wed, 25 Jan 2023 18:24:34 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCF4023DAB; Wed, 25 Jan 2023 15:24:32 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.152]) by gnuweeb.org (Postfix) with ESMTPSA id 553FF82EFE; Wed, 25 Jan 2023 23:24:26 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1674689072; bh=esmX/7V1+r0INpHmeEptwhnVrmI7QSkQT7lZUUUYBRw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fbcSBj9L3oqnmMfDSGMSIBqLIOQO0KF6GoqV6YHamfqmO70ie+wmxloy4IWQuJxUG rPmtG68VMqJF+yh3HDlbQ/2eqUkHaySt7lJqOAARFEZ8BQWp6xQvzynInPqmcdm08U OmcWpxB7gcqajayHgFaeAjnjfEp1EERxT0zvZ/awqnYRDwH1QfFQ8w7aPVHbs0Sdr5 NBpMjxtyKD7/HlyCxiJJjVNYxvhk7X0MHc+BO9T+yxKu0wxH7nmwfXmYWxjnyzE/LN +faLRqpfHuCQT/s6rYNMvGSIMgYsTkqYG831F+aD8k7K9KTpcXyHjJRfGIv5lJaeCk nUiPY+19QAUyQ== From: Ammar Faizi To: "H. Peter Anvin" , Xin Li Cc: Dave Hansen , Dave Hansen , "Kirill A. Shutemov" , Thomas Gleixner , Andrew Cooper , Peter Zijlstra , Brian Gerst , Borislav Petkov , Shuah Khan , Ingo Molnar , Andy Lutomirski , x86 Mailing List , Linux Kselftest Mailing List , Linux Kernel Mailing List Subject: [RFC PATCH v7 1/3] selftests/x86: sysret_rip: Handle syscall in a FRED system Date: Thu, 26 Jan 2023 06:24:13 +0700 Message-Id: <20230125232415.860397-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230125232415.860397-1-ammarfaizi2@gnuweeb.org> References: <25b96960-a07e-a952-5c23-786b55054126@zytor.com> <6cd0db14-c9e2-3598-fd10-4b473d78c373@citrix.com> <5ecc383c-621b-57d9-7f6d-d63496fca3b3@zytor.com> <20230124022729.596997-1-ammarfaizi2@gnuweeb.org> <20230124022729.596997-3-ammarfaizi2@gnuweeb.org> <20230125232415.860397-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi The current selftest asserts (%r11 == %rflags) after the 'syscall' returns to user. Such an assertion doesn't apply to the FRED system because in that system the 'syscall' instruction does not set %r11=%rflags and %rcx=%rip. Handle the FRED case. Now, test that: - "syscall" in a FRED system doesn't clobber %rcx and %r11. - "syscall" in a non-FRED system sets %rcx=%rip and %r11=%rflags. The 'raise()' function from libc can't be used to control those registers. Therefore, create a syscall wrapper in inline Assembly to fully control them. Fixes: 660602140103 ("selftests/x86: Add a selftest for SYSRET to noncanonical addresses") Link: https://lore.kernel.org/lkml/25b96960-a07e-a952-5c23-786b55054126@zytor.com Reported-by: Xin Li Co-developed-by: H. Peter Anvin (Intel) Signed-off-by: H. Peter Anvin (Intel) Signed-off-by: Ammar Faizi --- tools/testing/selftests/x86/sysret_rip.c | 114 +++++++++++++++++++++-- 1 file changed, 107 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selftests/x86/sysret_rip.c index 84d74be1d90207ab..ef3f492d95f6f2a1 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -39,6 +39,104 @@ asm ( extern const char test_page[]; static void const *current_test_page_addr = test_page; +/* Arbitrary values */ +static const unsigned long r11_sentinel = 0xfeedfacedeadbeef; +static const unsigned long rcx_sentinel = 0x5ca1ab1e0b57ac1e; + +/* An arbitrary *valid* RFLAGS value */ +static const unsigned long rflags_sentinel = 0x200a93; + +enum regs_ok { + REGS_UNDEFINED = -1, /* For consistency checker init, never returned */ + REGS_SAVED = 0, /* Registers properly preserved */ + REGS_SYSRET = 1 /* Registers match syscall/sysret */ +}; + +/* + * @rbx should be set to the syscall return %rip. + */ +static void check_regs_result(unsigned long r11, unsigned long rcx, + unsigned long rbx) +{ + static enum regs_ok regs_ok_state = REGS_UNDEFINED; + enum regs_ok ret; + + /* + * REGS_SAVED = %rcx and %r11 preserved (Intel FRED). + * REGS_SYSRET = %rcx and %r11 set to %rflags and %rip. + */ + if (r11 == r11_sentinel && rcx == rcx_sentinel) { + ret = REGS_SAVED; + } else if (r11 == rflags_sentinel && rcx == rbx) { + ret = REGS_SYSRET; + } else { + printf("[FAIL] check_regs_result\n"); + printf(" r11_sentinel = %#lx; %%r11 = %#lx;\n", r11_sentinel, r11); + printf(" rcx_sentinel = %#lx; %%rcx = %#lx;\n", rcx_sentinel, rcx); + printf(" rflags_sentinel = %#lx\n", rflags_sentinel); + exit(1); + } + + + /* + * Test that we don't get a mix of REGS_SAVED and REGS_SYSRET. + * It needs at least calling check_regs_result() twice to assert. + */ + if (regs_ok_state == REGS_UNDEFINED) { + /* + * First time calling check_regs_result(). + */ + regs_ok_state = ret; + } else { + assert(regs_ok_state == ret); + } +} + +static long do_syscall(long nr_syscall, unsigned long arg1, unsigned long arg2, + unsigned long arg3, unsigned long arg4, + unsigned long arg5, unsigned long arg6) +{ + register unsigned long r11 asm("%r11"); + register unsigned long r10 asm("%r10"); + register unsigned long r8 asm("%r8"); + register unsigned long r9 asm("%r9"); + register void *rsp asm("%rsp"); + unsigned long rcx, rbx; + + r11 = r11_sentinel; + rcx = rcx_sentinel; + r10 = arg4; + r8 = arg5; + r9 = arg6; + + asm volatile ( + "pushq %[rflags_sentinel]\n\t" + "popf\n\t" + "leaq 1f(%%rip), %[rbx]\n\t" + "syscall\n" + "1:" + + : "+a" (nr_syscall), + "+r" (r11), + "+c" (rcx), + [rbx] "=b" (rbx), + "+r" (rsp) /* Clobber the redzone */ + + : [rflags_sentinel] "g" (rflags_sentinel), + "D" (arg1), /* %rdi */ + "S" (arg2), /* %rsi */ + "d" (arg3), /* %rdx */ + "r" (r10), + "r" (r8), + "r" (r9) + + : "memory" + ); + + check_regs_result(r11, rcx, rbx); + return nr_syscall; +} + static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), int flags) { @@ -88,24 +186,26 @@ static void sigusr1(int sig, siginfo_t *info, void *ctx_void) memcpy(&initial_regs, &ctx->uc_mcontext.gregs, sizeof(gregset_t)); + check_regs_result(ctx->uc_mcontext.gregs[REG_R11], + ctx->uc_mcontext.gregs[REG_RCX], + ctx->uc_mcontext.gregs[REG_RBX]); + /* Set IP and CX to match so that SYSRET can happen. */ ctx->uc_mcontext.gregs[REG_RIP] = rip; ctx->uc_mcontext.gregs[REG_RCX] = rip; - - /* R11 and EFLAGS should already match. */ - assert(ctx->uc_mcontext.gregs[REG_EFL] == - ctx->uc_mcontext.gregs[REG_R11]); - sethandler(SIGSEGV, sigsegv_for_sigreturn_test, SA_RESETHAND); +} - return; +static void __raise(int sig) +{ + do_syscall(__NR_kill, getpid(), sig, 0, 0, 0, 0); } static void test_sigreturn_to(unsigned long ip) { rip = ip; printf("[RUN]\tsigreturn to 0x%lx\n", ip); - raise(SIGUSR1); + __raise(SIGUSR1); } static jmp_buf jmpbuf; From patchwork Wed Jan 25 23:24:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13116399 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3898DC54E94 for ; Wed, 25 Jan 2023 23:24:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235341AbjAYXYp (ORCPT ); Wed, 25 Jan 2023 18:24:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235501AbjAYXYo (ORCPT ); Wed, 25 Jan 2023 18:24:44 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B85524ED39; Wed, 25 Jan 2023 15:24:39 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.152]) by gnuweeb.org (Postfix) with ESMTPSA id 416F1824E0; Wed, 25 Jan 2023 23:24:32 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1674689079; bh=hNQSW6B6+nk0aXTkdBZ0AWffDkkVWECsoP5oElAmxWw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GnxtqNDQaLjQBSDWSOzBqdNlHWx1Aa9D4EZfQODUxGXItAFRLnvGrcyD1I99Qm9w4 rkTFtv6+vDeJQzuQNwXCmN43fcAvlH/L1QVWUKjRYrQJ7fhwBs1JQRwgU0ZIZNfoQp eTWL9GFhmHDh83Pp58hpJAoDzsSjFUjIbpAnSYOrjGVc8iCUAECIh1poE+5ues0Uhd 5rDUqMd8Ug1yUsMQiT+y7i0a2X0luSpLifCEWZgYsfytfg0SNz5f5xJ7qD9/YVrire YngYC9PBJ00lHhDHy8GLQe6FAEZgvPxc3DyBkfFDSpq0DSIiQpe/kClfgUZcqTO7BU qy9bjL7/4ixTA== From: Ammar Faizi To: "H. Peter Anvin" , Xin Li Cc: Dave Hansen , Dave Hansen , "Kirill A. Shutemov" , Thomas Gleixner , Andrew Cooper , Peter Zijlstra , Brian Gerst , Borislav Petkov , Shuah Khan , Ingo Molnar , Andy Lutomirski , x86 Mailing List , Linux Kselftest Mailing List , Linux Kernel Mailing List Subject: [RFC PATCH v7 2/3] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Date: Thu, 26 Jan 2023 06:24:14 +0700 Message-Id: <20230125232415.860397-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230125232415.860397-1-ammarfaizi2@gnuweeb.org> References: <25b96960-a07e-a952-5c23-786b55054126@zytor.com> <6cd0db14-c9e2-3598-fd10-4b473d78c373@citrix.com> <5ecc383c-621b-57d9-7f6d-d63496fca3b3@zytor.com> <20230124022729.596997-1-ammarfaizi2@gnuweeb.org> <20230124022729.596997-3-ammarfaizi2@gnuweeb.org> <20230125232415.860397-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi Test that: - REGS_SAVED: "syscall" in a FRED system doesn't clobber %rcx and %r11. - REGS_SYSRET: "syscall" in a non-FRED system sets %rcx=%rip and %r11=%rflags. Test them out with trivial system calls like __NR_getppid and friends which are extremely likely to return with SYSRET on an IDT system. Goals of this test: - Ensure that the syscall behavior is consistent. It must be either always REGS_SAVED or always REGS_SYSRET. Not a mix of them. - The kernel doesn't leak its internal data when returning to userspace. Link: https://lore.kernel.org/lkml/25b96960-a07e-a952-5c23-786b55054126@zytor.com Co-developed-by: H. Peter Anvin (Intel) Signed-off-by: H. Peter Anvin (Intel) Signed-off-by: Ammar Faizi --- tools/testing/selftests/x86/sysret_rip.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selftests/x86/sysret_rip.c index ef3f492d95f6f2a1..d688cb9d5ac919eb 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -258,8 +258,24 @@ static void test_syscall_fallthrough_to(unsigned long ip) printf("[OK]\tWe survived\n"); } +/* + * Ensure that various system calls are consistent. + * We must not get a mix of REGS_SAVED and REGS_SYSRET. + */ +static void test_syscall_rcx_r11_consistent(void) +{ + do_syscall(__NR_getpid, 0, 0, 0, 0, 0, 0); + do_syscall(__NR_gettid, 0, 0, 0, 0, 0, 0); + do_syscall(__NR_getppid, 0, 0, 0, 0, 0, 0); +} + int main() { + int i; + + for (i = 0; i < 32; i++) + test_syscall_rcx_r11_consistent(); + /* * When the kernel returns from a slow-path syscall, it will * detect whether SYSRET is appropriate. If it incorrectly @@ -267,7 +283,7 @@ int main() * it'll crash on Intel CPUs. */ sethandler(SIGUSR1, sigusr1, 0); - for (int i = 47; i < 64; i++) + for (i = 47; i < 64; i++) test_sigreturn_to(1UL< X-Patchwork-Id: 13116400 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9DE1C54E94 for ; Wed, 25 Jan 2023 23:24:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235296AbjAYXYz (ORCPT ); Wed, 25 Jan 2023 18:24:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235506AbjAYXYx (ORCPT ); Wed, 25 Jan 2023 18:24:53 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA89CCA25; Wed, 25 Jan 2023 15:24:46 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.152]) by gnuweeb.org (Postfix) with ESMTPSA id 2DA5382EFE; Wed, 25 Jan 2023 23:24:39 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1674689086; bh=U5dSuQGYE25QjM+XfiVRl8ioAnk2uXKIVAwpxL+5pac=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aGfTjSHe3eYaTBDWbIpeYIL8oPnlqkJG2dmu3T7E85OVxOFZ504KO/38rgY/zKQ72 PMqpVdyNEcbVmR6TNNVyEbLKSW5klepI8XLRHj37agNXJUeZur2RxYIRpFpA2UJpsV dBbhSPZn+fLt292XHt6v15ZLcKe32J2g+qP1h2qn7+UGUyMftV6ymzmwh8VILeeaFj kOTm6oliLLbaW9IVdhkaMOqDsNEDhngS+sbA2px3M+F1OpveOd1SVspSlOaVNrNzZJ EwrRwmDCR+UWcWXmZBHTHv68C19kO9PQHezgt7N8v7oxr4oWtDR0D01sSQWMFgtqt5 ugBxcV7K/J1BA== From: Ammar Faizi To: "H. Peter Anvin" , Xin Li Cc: Dave Hansen , Dave Hansen , "Kirill A. Shutemov" , Thomas Gleixner , Andrew Cooper , Peter Zijlstra , Brian Gerst , Borislav Petkov , Shuah Khan , Ingo Molnar , Andy Lutomirski , x86 Mailing List , Linux Kselftest Mailing List , Linux Kernel Mailing List Subject: [RFC PATCH v7 3/3] selftests/x86: sysret_rip: Test SYSRET with a signal handler Date: Thu, 26 Jan 2023 06:24:15 +0700 Message-Id: <20230125232415.860397-4-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230125232415.860397-1-ammarfaizi2@gnuweeb.org> References: <25b96960-a07e-a952-5c23-786b55054126@zytor.com> <6cd0db14-c9e2-3598-fd10-4b473d78c373@citrix.com> <5ecc383c-621b-57d9-7f6d-d63496fca3b3@zytor.com> <20230124022729.596997-1-ammarfaizi2@gnuweeb.org> <20230124022729.596997-3-ammarfaizi2@gnuweeb.org> <20230125232415.860397-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi When run on a non-FRED system, test the SYSRET path. Make sure the %rcx/%r11 clobbering behavior is consistent. When run on a FRED system, test that %rcx/%r11 are preserved when invoking syscall. This is similar to what test_syscall_rcx_r11_consistent() is doing, but with addition it's done via the SIGUSR2 signal handler. Link: https://lore.kernel.org/lkml/8770815f-0f23-d0c5-e56a-d401827842c9@zytor.com Suggested-by: H. Peter Anvin (Intel) Signed-off-by: Ammar Faizi --- On Wed, 25 Jan 2023 00:39:26 -0800, "H. Peter Anvin" wrote: > > /* Set IP and CX to match so that SYSRET can happen. */ > > ctx->uc_mcontext.gregs[REG_RIP] = rip; > > ctx->uc_mcontext.gregs[REG_RCX] = rip; > > It would be interesting to have the syscall handler try both with and > without this (so it would end up doing both IRET and SYSCALL on legacy.) > Perhaps SIGUSR1 versus SIGUSR2... tools/testing/selftests/x86/sysret_rip.c | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selftests/x86/sysret_rip.c index d688cb9d5ac919eb..630ee261559c14b1 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -269,6 +269,28 @@ static void test_syscall_rcx_r11_consistent(void) do_syscall(__NR_getppid, 0, 0, 0, 0, 0, 0); } +static unsigned long usr2_rcx; +static unsigned long usr2_r11; + +static void sigusr2(int sig, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx = (ucontext_t*)ctx_void; + + usr2_r11 = ctx->uc_mcontext.gregs[REG_R11]; + usr2_rcx = ctx->uc_mcontext.gregs[REG_RCX]; + + check_regs_result(ctx->uc_mcontext.gregs[REG_R11], + ctx->uc_mcontext.gregs[REG_RCX], + ctx->uc_mcontext.gregs[REG_RBX]); +} + +static void test_sysret_consistent(void) +{ + printf("[RUN]\ttest_sysret_consistent\n"); + __raise(SIGUSR2); + printf("[OK]\tRCX = %#lx; R11 = %#lx\n", usr2_rcx, usr2_r11); +} + int main() { int i; @@ -286,6 +308,21 @@ int main() for (i = 47; i < 64; i++) test_sigreturn_to(1UL<