From patchwork Wed Jan 25 21:17:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13116202 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 67A82C54E94 for ; Wed, 25 Jan 2023 21:17:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236505AbjAYVRm (ORCPT ); Wed, 25 Jan 2023 16:17:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236437AbjAYVRi (ORCPT ); Wed, 25 Jan 2023 16:17:38 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBF7630E80; Wed, 25 Jan 2023 13:17:31 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.152]) by gnuweeb.org (Postfix) with ESMTPSA id 80DB582EFE; Wed, 25 Jan 2023 21:17:25 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1674681451; bh=Btm3NcJ/RK9GewCcxsk7hj+u/YbVqlV/6oZ5bJ8S7bQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XmJSlGHS5yzcit7fTMvoYJkCI0BQ30UI8X70gG+W54279auoUWUF8xoeVjmQ23RO2 j9SyDhnveJ7JwSULem87RB8PL3XoxQfEbcMjlsBJcEjO5B5Wld/fElYkQvMK/14QKj S1d+OZpmaoGEBxAKbG7DFyZFlUJ29JUkJWNpdMydog+9sns768LW2Z8eso+97T8VoA VnZpK1Cf/5JWI9Xu0xlKla+XbJQpTvwKkmaW0wAOL5kzsEmGsq40S+CsZYFwzhjgen FBZQDt2uc7tOMHkE2roTk/wwlHdoVp+hYGyxhAy28azBJjGYEvvdUNS9PpYE7zCF4V WipOZ+3xmMNLg== 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 v6 1/3] selftests/x86: sysret_rip: Handle syscall in a FRED system Date: Thu, 26 Jan 2023 04:17:12 +0700 Message-Id: <20230125211714.838216-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230125211714.838216-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> <20230125211714.838216-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 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 | 120 +++++++++++++++++++++-- 1 file changed, 113 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selftests/x86/sysret_rip.c index 84d74be1d90207ab..100f55981d77a29b 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -39,6 +39,110 @@ 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 */ +}; + +/* + * REGS_SAVED = %rcx and %r11 preserved. + * REGS_SYSRET = %rcx and %r11 set to %rflags and %rip. + * REGS_ERROR = %rcx and/or %r11 set to any other values. + * + * @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; + + /* + * 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. + */ + 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 +192,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 21:17: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: 13116203 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 41D73C54E94 for ; Wed, 25 Jan 2023 21:17:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236437AbjAYVRq (ORCPT ); Wed, 25 Jan 2023 16:17:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236514AbjAYVRm (ORCPT ); Wed, 25 Jan 2023 16:17:42 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AA035A82A; Wed, 25 Jan 2023 13:17:38 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.152]) by gnuweeb.org (Postfix) with ESMTPSA id 3129A82F36; Wed, 25 Jan 2023 21:17:31 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1674681458; bh=X0endH8ae5ajbiCeXLcvMupSYZ6qEQlQpeGCxJkk+NM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iiJQQsHE2YBLdPkNoFcY3qYIp0Ha6qpZlgieAy4lHgXQn6FbxMxCDhmR0D41g8e9N R3N8IT2s0o8Y4nUQck1HGHciya4Nk2xnXYgPuXXZlq/f73cN0PS6bQvo5ihGxJ9KLO tpSWe4mv0iUN/SJsbh6/CwgrX+SDLrGGqMirJR1gM+8KZb7Xffw9MQ/6MgUvsJH/0r D+faAXfNJyUmSALCfoWLSxPKXjEkZW6gqkSLmDxRUseD1B5fUtseCr+HvAuKh2BneQ bbfjU6eHlYCQiulTy7eC9Chv2f6AioDEXQeibcGfoi4nf3GqXZ37NO9Stiomjl5Bdq kGcgqsEts6bxw== 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 v6 2/3] selftests/x86: sysret_rip: Add more syscall tests with respect to `%rcx` and `%r11` Date: Thu, 26 Jan 2023 04:17:13 +0700 Message-Id: <20230125211714.838216-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230125211714.838216-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> <20230125211714.838216-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 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 should 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 100f55981d77a29b..d45b7f0147cd25ad 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -264,8 +264,24 @@ static void test_syscall_fallthrough_to(unsigned long ip) printf("[OK]\tWe survived\n"); } +/* + * Ensure that various system calls are consistent. + * We should 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 @@ -273,7 +289,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: 13116204 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 AEFD2C27C76 for ; Wed, 25 Jan 2023 21:18:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236662AbjAYVSF (ORCPT ); Wed, 25 Jan 2023 16:18:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236474AbjAYVR5 (ORCPT ); Wed, 25 Jan 2023 16:17:57 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F1D07D89; Wed, 25 Jan 2023 13:17:45 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.152]) by gnuweeb.org (Postfix) with ESMTPSA id D6310824E0; Wed, 25 Jan 2023 21:17:38 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1674681464; bh=vBWFR+xb4VVQ1ESzMMkJK66vUOBLYOX/iFxSXT2+DEg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RtSY8u6I6XwmtvHyIZv8YZmG7mWHsus0383CNV+Jp7r3nXQJ9z1k3IJ3IjvOk3SRe 2DDMX8YQej538IKIthIcU7BAa9m9Zken/B6McJM4LVv+/4NVrJ3QqnGWzT7D/QhrEO Ayp0zzbiy4afnTWnaBFmwvZQbBbrR30BZgcHFwrBlumRSOWFadkvoMzg/fIjlqe/Ax whBZIJ3Sf03mwZtH9bsNH19AdLqyvO9hTnjgPAzRuRwo/cYcu4U9SW/f68kjoB/UD8 xOoQanlpZwDn16uQlRXBcp6nN9r6g5wykAHsQMBB9vPj6fDs+sZSyK5K5weyLzRiH4 4Rc0EZJ3lEPMQ== 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 v6 3/3] selftests/x86: sysret_rip: Test opportunistic SYSRET Date: Thu, 26 Jan 2023 04:17:14 +0700 Message-Id: <20230125211714.838216-4-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230125211714.838216-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> <20230125211714.838216-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org When run on a non-FRED system, test the opportunistic SYSRET fast-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 d45b7f0147cd25ad..a1e5ec6f08bcd523 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -275,6 +275,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; @@ -292,6 +314,21 @@ int main() for (i = 47; i < 64; i++) test_sigreturn_to(1UL<