From nobody Thu Sep 11 11:38:15 2025 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 01BB7C05027 for ; Mon, 20 Feb 2023 03:03:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229901AbjBTDDi (ORCPT ); Sun, 19 Feb 2023 22:03:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229809AbjBTDDg (ORCPT ); Sun, 19 Feb 2023 22:03:36 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABF79E075; Sun, 19 Feb 2023 19:03:27 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.183.169]) by gnuweeb.org (Postfix) with ESMTPSA id 6C37E8313A; Mon, 20 Feb 2023 03:03:20 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1676862207; bh=Qk7N/yCYGwUwnYVbbMe3gs22DUs6Sr0x8uxQAHDneuQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IuKsoVXYGbDJ72YXbhVTj1d+HJIpBuWIrQ7xLt2XkM+O/N1Q4YDXAb6OI817+zejz DcEWH6gc1VmG/Cb9aEyOM1D950g1tHOhmJsB6BWYUb7EgTjQhj2RpdRYa+40tLVvo9 VeZZuWpgXD0cZtJvfSJXu0YjVEhE4cj9U7izkzZ7VwpIFBECtXGuxP9mdsWUK6byG4 cFmz3gVfmJKmwJ2q7kGWXWO7kChpAM/bKxOWWHBdparqtBywvZpNYmz/qHBgO2Hy8B HwMmNNuwvt+1q+hpTlqeuPXTz64XnQt/IwZseDbkS27kTf0ma1ZgL1ozSVO89bPNYZ btB3J8z+bLTxw== From: Ammar Faizi To: Ingo Molnar , "H. Peter Anvin" , Dave Hansen , Xin Li Cc: Ammar Faizi , Andrew Cooper , Andy Lutomirski , Borislav Petkov , Thomas Gleixner , Dave Hansen , Peter Zijlstra , "Kirill A. Shutemov" , x86 Mailing List , Linux x86-64 Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List , GNU/Weeb Mailing List Subject: [RFC PATCH v8 1/3] selftests/x86: sysret_rip: Handle syscall on the Intel FRED architecture Date: Mon, 20 Feb 2023 10:02:42 +0700 Message-Id: <20230220030244.115808-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230220030244.115808-1-ammarfaizi2@gnuweeb.org> References: <20230220030244.115808-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The current selftest asserts %r11 =3D=3D %rflags after the 'syscall' returns to userspace. However, such an assertion doesn't apply to the Intel FRED system because, in that system, the 'syscall' instruction does not set %r11=3D%rflags and %rcx=3D%rip. Handle the FRED system case. Now, test that: A) 'syscall' in a FRED system preserves %rcx and %r11. B) 'syscall' in a non-FRED system sets %rcx=3D%rip and %r11=3D%rflags. Note for the '__raise()' helper: Those tests must manipulate registers before the 'syscall' instruction is invoked. However, the current 'raise()' function from libc cannot accomplish it. Therefore, create a syscall wrapper in inline Assembly to control them. Fixes: 660602140103 ("selftests/x86: Add a selftest for SYSRET to noncanoni= cal addresses") Link: https://lore.kernel.org/lkml/25b96960-a07e-a952-5c23-786b55054126@zyt= or.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 | 122 +++++++++++++++++++++-- 1 file changed, 115 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selft= ests/x86/sysret_rip.c index 84d74be1d90207ab..300104900192d396 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -39,6 +39,112 @@ asm ( extern const char test_page[]; static void const *current_test_page_addr =3D test_page; =20 +/* + * Arbitrary values. + */ +static const unsigned long r11_sentinel =3D 0xfeedfacedeadbeef; +static const unsigned long rcx_sentinel =3D 0x5ca1ab1e0b57ac1e; + +/* + * An arbitrary *valid* RFLAGS value. + */ +static const unsigned long rflags_sentinel =3D 0x200a93; + +enum regs_ok { + REGS_UNDEFINED =3D -1, + REGS_SAVED =3D 0, /* Registers properly preserved (Intel FRED). */ + REGS_SYSRET =3D 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 =3D REGS_UNDEFINED; + enum regs_ok ret; + + if (r11 =3D=3D r11_sentinel && rcx =3D=3D rcx_sentinel) { + ret =3D REGS_SAVED; + } else if (r11 =3D=3D rflags_sentinel && rcx =3D=3D rbx) { + ret =3D REGS_SYSRET; + } else { + printf("[FAIL] check_regs_result\n"); + printf(" r11_sentinel =3D %#lx; %%r11 =3D %#lx;\n", r11_sentinel,= r11); + printf(" rcx_sentinel =3D %#lx; %%rcx =3D %#lx;\n", rcx_sentinel,= rcx); + printf(" rflags_sentinel =3D %#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 =3D=3D REGS_UNDEFINED) { + /* + * First time calling check_regs_result(). + */ + regs_ok_state =3D ret; + } else { + assert(regs_ok_state =3D=3D ret); + } +} + +/* + * There are two cases: + * + * A) 'syscall' in a FRED system preserves %rcx and %r11. + * B) 'syscall' in a non-FRED system sets %rcx=3D%rip and %r11=3D%rflags. + * + * When the do_syscall() function is called for the first time, + * check_regs_result() will memorize the behavior, either (A) or (B). + * Then, the next do_syscall() call will verify that the 'syscall' + * behavior is the same. + * + * This function needs to be called at least twice to assert. + */ +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) +{ + unsigned long rbx; + unsigned long rcx =3D rcx_sentinel; + register unsigned long r11 __asm__("%r11") =3D r11_sentinel; + register unsigned long r10 __asm__("%r10") =3D arg4; + register unsigned long r8 __asm__("%r8") =3D arg5; + register unsigned long r9 __asm__("%r9") =3D arg6; + + __asm__ volatile ( + "movq -8(%%rsp), %%r12\n\t" // Do not clobber the red zone. + "pushq %[rflags_sentinel]\n\t" + "popfq\n\t" + "movq %%r12, -8(%%rsp)\n\t" + "leaq 1f(%%rip), %[rbx]\n\t" + "syscall\n" + "1:" + + : "+a" (nr_syscall), + "+r" (r11), + "+c" (rcx), + [rbx] "=3Db" (rbx) + + : [rflags_sentinel] "g" (rflags_sentinel), + "D" (arg1), /* %rdi */ + "S" (arg2), /* %rsi */ + "d" (arg3), /* %rdx */ + "r" (r10), + "r" (r8), + "r" (r9) + + : "r12", "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 +194,26 @@ static void sigusr1(int sig, siginfo_t *info, void *ct= x_void) =20 memcpy(&initial_regs, &ctx->uc_mcontext.gregs, sizeof(gregset_t)); =20 + 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] =3D rip; ctx->uc_mcontext.gregs[REG_RCX] =3D rip; - - /* R11 and EFLAGS should already match. */ - assert(ctx->uc_mcontext.gregs[REG_EFL] =3D=3D - ctx->uc_mcontext.gregs[REG_R11]); - sethandler(SIGSEGV, sigsegv_for_sigreturn_test, SA_RESETHAND); +} =20 - return; +static void __raise(int sig) +{ + do_syscall(__NR_kill, getpid(), sig, 0, 0, 0, 0); } =20 static void test_sigreturn_to(unsigned long ip) { rip =3D ip; printf("[RUN]\tsigreturn to 0x%lx\n", ip); - raise(SIGUSR1); + __raise(SIGUSR1); } =20 static jmp_buf jmpbuf; --=20 Ammar Faizi From nobody Thu Sep 11 11:38:15 2025 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 94089C64EC4 for ; Mon, 20 Feb 2023 03:03:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229984AbjBTDDq (ORCPT ); Sun, 19 Feb 2023 22:03:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229997AbjBTDDn (ORCPT ); Sun, 19 Feb 2023 22:03:43 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1317ABDEB; Sun, 19 Feb 2023 19:03:35 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.183.169]) by gnuweeb.org (Postfix) with ESMTPSA id E8A3583130; Mon, 20 Feb 2023 03:03:27 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1676862214; bh=Hke7N4a3CN0c5boF6cakkDs3zb+KxALxVpws4FxKUhQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HZLrDE/2AZQbdHvSmD8pEes8Vh7voDhgqtC0DzC6HiRiw11GaF62rZWZ3CAZMKDOk 7P8adfTgqrfGksaa/jT/4Rf9QzSzL2XeEglEtoeMrArF9Qa40+pXkDFI2EJBSvt3k1 yXKt0b1sIERSVr04IH7huzS232xTMaEMA7f3qMoVjVgOkrvlIDKPphQ/EP5BBv1c8Q NwCTaHHhh25uVPxjKGlT83SESgmGbTy/TEsyj70+bPhbxpfJqg526dYSsXcv6LhFYw 5JK2bbXmdRewCJ6hasUjhstFl8Y7uzKpqoQyFeCwjkGWgjr49c7WyMN7gfvAhEJtq/ P6A+ZEcyclQeQ== From: Ammar Faizi To: Ingo Molnar , "H. Peter Anvin" , Dave Hansen , Xin Li Cc: Ammar Faizi , Andrew Cooper , Andy Lutomirski , Borislav Petkov , Thomas Gleixner , Dave Hansen , Peter Zijlstra , "Kirill A. Shutemov" , x86 Mailing List , Linux x86-64 Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List , GNU/Weeb Mailing List Subject: [RFC PATCH v8 2/3] selftests/x86: sysret_rip: Add more tests to verify the 'syscall' behavior Date: Mon, 20 Feb 2023 10:02:43 +0700 Message-Id: <20230220030244.115808-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230220030244.115808-1-ammarfaizi2@gnuweeb.org> References: <20230220030244.115808-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" There are two cases: A) 'syscall' in a FRED system preserves %rcx and %r11. B) 'syscall' in a non-FRED system sets %rcx=3D%rip and %r11=3D%rflags. When the do_syscall() function is called for the first time, it will memorize the behavior, either (A) or (B). Then, the next do_syscall() call will verify that the 'syscall' behavior is the same. Test them with trivial system calls like __NR_getppid and friends, which are highly likely to return with SYSRET on an IDT system. The purposes of this test are: - Ensure that the syscall behavior is consistent. It must always be (A) or always be (B). Not a mix of them. - Ensure that the kernel doesn't leak its internal data when returning to userspace. Cc: Xin Li Link: https://lore.kernel.org/lkml/25b96960-a07e-a952-5c23-786b55054126@zyt= or.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 | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selft= ests/x86/sysret_rip.c index 300104900192d396..1531593b50d02150 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -266,8 +266,21 @@ static void test_syscall_fallthrough_to(unsigned long = ip) printf("[OK]\tWe survived\n"); } =20 +/* See the comment in do_syscall(). */ +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 =3D 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 @@ -275,7 +288,7 @@ int main() * it'll crash on Intel CPUs. */ sethandler(SIGUSR1, sigusr1, 0); - for (int i =3D 47; i < 64; i++) + for (i =3D 47; i < 64; i++) test_sigreturn_to(1UL< 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 27361C61DA4 for ; Mon, 20 Feb 2023 03:03:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230001AbjBTDD6 (ORCPT ); Sun, 19 Feb 2023 22:03:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229981AbjBTDDy (ORCPT ); Sun, 19 Feb 2023 22:03:54 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B0E1E3AA; Sun, 19 Feb 2023 19:03:42 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.183.169]) by gnuweeb.org (Postfix) with ESMTPSA id 738E683133; Mon, 20 Feb 2023 03:03:35 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1676862222; bh=8+ewo69UeU0rHb78uAAPEiHOOweqFJBmdi5nkVAJieo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fQb5MRbcGzKs+xDyXwlIuAgQUmuMECY9c8DDzj3txjNOnHftopsZgnGd91RKcuon6 BtjA39ED7RNJuUYINZOUaFjbckKzCMjzZq9c7WUPK6Llqe0hvCE3Cfaitxfa+hnaKE /n2akSAu0xyDw/UUuKBpo17/eBMQgY5/Of10RRh3WI0/ITYauIviRI28b64iMM2yZl x/81Aoc1yzeQ/6rxnnK6//2RhTPS4TZZX1xDclsvztmk33tYTsX7Nqy5sJXjKT7RF0 vUKXlQKwvsaNBNYVPnen9DlLn8fSDRV3riPlA7xLXiN3SwNufu6TQwsvTt4meG5Ob8 GmH5D6E6lAFRw== From: Ammar Faizi To: Ingo Molnar , "H. Peter Anvin" , Dave Hansen , Xin Li Cc: Ammar Faizi , Andrew Cooper , Andy Lutomirski , Borislav Petkov , Thomas Gleixner , Dave Hansen , Peter Zijlstra , "Kirill A. Shutemov" , x86 Mailing List , Linux x86-64 Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List , GNU/Weeb Mailing List Subject: [RFC PATCH v8 3/3] selftests/x86: sysret_rip: Test SYSRET with a signal handler Date: Mon, 20 Feb 2023 10:02:44 +0700 Message-Id: <20230220030244.115808-4-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230220030244.115808-1-ammarfaizi2@gnuweeb.org> References: <20230220030244.115808-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The current test_sigreturn_to() goes to the slow-path syscall with IRET due to non-canonical addresses. It uses the SIGUSR1 signal to perform the test. Add a similar test that goes to the SYSRET path instead of IRET using the SIGUSR2 signal. There are two cases: A) 'syscall' in a FRED system preserves %rcx and %r11. B) 'syscall' in a non-FRED system sets %rcx=3D%rip and %r11=3D%rflags. The __raise(SIGUSR2) call verifies the 'syscall' behavior consistency when dealing with a signal handler. It must always be (A) or always be (B). Not a mix of them. Cc: Xin Li Link: https://lore.kernel.org/lkml/8770815f-0f23-d0c5-e56a-d401827842c9@zyt= or.com Suggested-by: H. Peter Anvin (Intel) Signed-off-by: Ammar Faizi --- tools/testing/selftests/x86/sysret_rip.c | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selft= ests/x86/sysret_rip.c index 1531593b50d02150..746801675fe77e9c 100644 --- a/tools/testing/selftests/x86/sysret_rip.c +++ b/tools/testing/selftests/x86/sysret_rip.c @@ -274,6 +274,28 @@ static void test_syscall_rcx_r11_consistent(void) do_syscall(__NR_getppid, 0, 0, 0, 0, 0, 0); } =20 +static unsigned long usr2_rcx; +static unsigned long usr2_r11; + +static void sigusr2(int sig, siginfo_t *info, void *ctx_void) +{ + ucontext_t *ctx =3D (ucontext_t*)ctx_void; + + usr2_r11 =3D ctx->uc_mcontext.gregs[REG_R11]; + usr2_rcx =3D 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 =3D %#lx; R11 =3D %#lx\n", usr2_rcx, usr2_r11); +} + int main() { int i; @@ -291,6 +313,14 @@ int main() for (i =3D 47; i < 64; i++) test_sigreturn_to(1UL<