FAILED: patch "[PATCH] entry: Fix seccomp bypass after ptrace with TSYNC" failed to apply to 6.1-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 4a3591287fb7f808e209b4974ed337f609a2006b
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026090331-spouse-sprawl-968e@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 4a3591287fb7f808e209b4974ed337f609a2006b Mon Sep 17 00:00:00 2001
From: Jinjie Ruan <[email protected]>
Date: Mon, 13 Jul 2026 10:57:12 +0800
Subject: [PATCH] entry: Fix seccomp bypass after ptrace with TSYNC
Sashiko review pointed out the following issue.
If a thread is stopped in syscall_trace_enter() for ptrace, another thread can install a seccomp filter with SECCOMP_FILTER_FLAG_TSYNC (e.g., via seccomp_attach_filter()). This will successfully set SYSCALL_WORK_SECCOMP on the stopped thread, but syscall_trace_enter() evaluates a cached 'work' variable sampled on entry. Consequently, the subsequent check for SYSCALL_WORK_SECCOMP misses the newly assigned flag, and the filter is silently bypassed.
This race condition could allow an unprivileged process to execute a prohibited system call (e.g., execve) that the newly installed filter was intended to block, especially since the tracer might have modified the system call number during the ptrace stop.
Fix this by re-reading the syscall_work flags after ptrace handling, so that any new SYSCALL_WORK_SECCOMP flag set by another thread via TSYNC during the ptrace stop is observed before the subsequent seccomp check.
include/linux/entry-common.h +3 -0
@@ -97,6 +97,9 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned lif (!arch_ptrace_report_syscall_permit_entry (regs) || (work &SYSCALL_WORK_SYSCALL_EMU ))return -1L ; + +/* ptrace might have changed work flags */ + work =READ_ONCE (current_thread_info ()->syscall_work); }/* Do seccomp after ptrace, to catch any tracer changes. */
Sasha Levin
maintainerPATCH 1/5 entry: Inline syscall_exit_to_user_mode()
From: Charlie Jenkins <[email protected]>
[ Upstream commit e43b8bb56e537bfc8d9076793091e7679020fc9c ]
Similar to commit 221a164035fd ("entry: Move syscall_enter_from_user_mode() to header file"), move syscall_exit_to_user_mode() to the header file as well.
Testing was done with the byte-unixbench syscall benchmark (which calls getpid) and QEMU. On riscv I measured a 7.09246% improvement, on x86 a 2.98843% improvement, on loongarch a 6.07954% improvement, and on s390 a 11.1328% improvement.
The Intel bot also reported "kernel test robot noticed a 1.9% improvement of stress-ng.seek.ops_per_sec".
Stable-dep-of: 4a3591287fb7 ("entry: Fix seccomp bypass after ptrace with TSYNC")
--- include/linux/entry-common.h | 131 ++++++++++++++++++++++++++++-- kernel/entry/common.c | 149 +++-------------------------------- 2 files changed, 136 insertions(+), 144 deletions(-)
include/linux/entry-common.h +126 -5
@@ -7,8 +7,14 @@#include <linux/syscalls.h> #include <linux/seccomp.h> #include <linux/sched.h> +#include <linux/context_tracking.h> +#include <linux/livepatch.h> +#include <linux/resume_user_mode.h> +#include <linux/tick.h> +#include <linux/kmsan.h> #include <asm/entry-common.h> +#include <asm/syscall.h> /* * Define dummy _TIF work flags if not defined by the architecture or for
@@ -98,7 +104,19 @@ static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs) {}* done between establishing state and enabling interrupts. The caller must * enable interrupts before invoking syscall_enter_from_user_mode_work(). */ -void enter_from_user_mode (struct pt_regs *regs); +static __always_inline void enter_from_user_mode(struct pt_regs *regs) +{ +arch_enter_from_user_mode (regs); +lockdep_hardirqs_off (CALLER_ADDR0 ); + +CT_WARN_ON (__ct_state () !=CONTEXT_USER ); +user_exit_irqoff (); + +instrumentation_begin (); +kmsan_unpoison_entry_regs (regs); +trace_hardirqs_off_finish (); +instrumentation_end (); +}/** * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts
@@ -117,6 +135,9 @@ void enter_from_user_mode(struct pt_regs *regs);*/ void syscall_enter_from_user_mode_prepare (struct pt_regs *regs); +long syscall_trace_enter (struct pt_regs *regs,long syscall, +unsigned long work); +/** * syscall_enter_from_user_mode_work - Check and handle work before invoking * a syscall
@@ -140,7 +161,15 @@ void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);* ptrace_report_syscall_entry(), __secure_computing(), trace_sys_enter() * 2) Invocation of audit_syscall_entry() */ -long syscall_enter_from_user_mode_work (struct pt_regs *regs,long syscall); +static __always_inline long syscall_enter_from_user_mode_work (struct pt_regs *regs,long syscall) +{ +unsigned long work =READ_ONCE (current_thread_info ()->syscall_work); + +if (work &SYSCALL_WORK_ENTER ) + syscall =syscall_trace_enter (regs, syscall, work); + +return syscall; +}/** * syscall_enter_from_user_mode - Establish state and check and handle work
@@ -159,7 +188,19 @@ long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall);* Returns: The original or a modified syscall number. See * syscall_enter_from_user_mode_work() for further explanation. */ -long syscall_enter_from_user_mode (struct pt_regs *regs,long syscall); +static __always_inline long syscall_enter_from_user_mode (struct pt_regs *regs,long syscall) +{ +long ret; + +enter_from_user_mode (regs); + +instrumentation_begin (); +local_irq_enable (); + ret =syscall_enter_from_user_mode_work (regs, syscall); +instrumentation_end (); + +return ret; +}/** * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
@@ -258,6 +299,44 @@ static __always_inline void arch_exit_to_user_mode(void) { }*/ void arch_do_signal_or_restart (struct pt_regs *regs); +/** +* exit_to_user_mode_loop - do any pending work before leaving to user space +*/ +unsigned long exit_to_user_mode_loop (struct pt_regs *regs, +unsigned long ti_work); + +/** +* exit_to_user_mode_prepare - call exit_to_user_mode_loop() if required +* @regs: Pointer to pt_regs on entry stack +* +* 1) check that interrupts are disabled +* 2) call tick_nohz_user_enter_prepare() +* 3) call exit_to_user_mode_loop() if any flags from +* EXIT_TO_USER_MODE_WORK are set +* 4) check that interrupts are still disabled +*/ +static __always_inline void exit_to_user_mode_prepare(struct pt_regs *regs) +{ +unsigned long ti_work; + +lockdep_assert_irqs_disabled (); + +/* Flush pending rcuog wakeup before the last need_resched() check */ +tick_nohz_user_enter_prepare (); + + ti_work =read_thread_flags (); +if (unlikely (ti_work &EXIT_TO_USER_MODE_WORK )) + ti_work =exit_to_user_mode_loop (regs, ti_work); + +arch_exit_to_user_mode_prepare (regs, ti_work); + +/* Ensure that the address limit is intact and no locks are held */ +addr_limit_user_check (); +kmap_assert_nomap (); +lockdep_assert_irqs_disabled (); +lockdep_sys_exit (); +} +/** * exit_to_user_mode - Fixup state when exiting to user mode *
@@ -276,7 +355,26 @@ void arch_do_signal_or_restart(struct pt_regs *regs);* non-instrumentable. * The caller has to invoke syscall_exit_to_user_mode_work() before this. */ -void exit_to_user_mode (void ); +static __always_inline void exit_to_user_mode(void) +{ +instrumentation_begin (); +trace_hardirqs_on_prepare (); +lockdep_hardirqs_on_prepare (); +instrumentation_end (); + +user_enter_irqoff (); +arch_exit_to_user_mode (); +lockdep_hardirqs_on (CALLER_ADDR0 ); +} + +/** +* syscall_exit_work - Handle work before returning to user mode +* @regs: Pointer to current pt_regs +* @work: Current thread syscall work +* +* Do one-time syscall specific work. +*/ +void syscall_exit_work (struct pt_regs *regs,unsigned long work);/** * syscall_exit_to_user_mode_work - Handle work before returning to user mode
@@ -291,7 +389,30 @@ void exit_to_user_mode(void);* make the final state transitions. Interrupts must stay disabled between * return from this function and the invocation of exit_to_user_mode(). */ -void syscall_exit_to_user_mode_work (struct pt_regs *regs); +static __always_inline void syscall_exit_to_user_mode_work(struct pt_regs *regs) +{ +unsigned long work =READ_ONCE (current_thread_info ()->syscall_work); +unsigned long nr =syscall_get_nr (current, regs); + +CT_WARN_ON (ct_state () !=CONTEXT_KERNEL ); + +if (IS_ENABLED (CONFIG_PROVE_LOCKING )) { +if (WARN (irqs_disabled (),"syscall %lu left IRQs disabled" , nr)) +local_irq_enable (); + } + +rseq_syscall (regs); + +/* +* Do one-time syscall specific work. If these work items are +* enabled, we want to run them exactly once per syscall exit with +* interrupts enabled. +*/ +if (unlikely (work &SYSCALL_WORK_EXIT )) +syscall_exit_work (regs, work); +local_irq_disable_exit_to_user (); +exit_to_user_mode_prepare (regs); +}/** * syscall_exit_to_user_mode - Handle work before returning to user mode
kernel/entry/common.c +10 -139
209 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/5 entry: Remove syscall_enter_from_user_mode_prepare()
From: Thomas Gleixner <[email protected]>
[ Upstream commit 54a5ab56242f96555999aaa41228f77b4a76e386 ]
Open code the only user in the x86 syscall code and reduce the zoo of functions.
[ Stable: Open-code the helper in arch/x86/entry/common.c, where the
32-bit syscall entry code still resides. ]
[ sashal: Reduced backport -- upstream 54a5ab56242f9 touches 3 file(s), this
backport carries 3. Not backported here:
arch/x86/entry/syscall_32.c
This note is generated from the file lists only; see the resolution record for the reasoning. ]
Stable-dep-of: 4a3591287fb7 ("entry: Fix seccomp bypass after ptrace with TSYNC")
--- arch/x86/entry/common.c | 3 ++- include/linux/entry-common.h | 26 +++++--------------------- kernel/entry/syscall-common.c | 8 -------- 3 files changed, 7 insertions(+), 30 deletions(-)
arch/x86/entry/common.c +2 -1
@@ -236,9 +236,10 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)* fetch EBP before invoking any of the syscall entry work * functions. */ -syscall_enter_from_user_mode_prepare (regs); +enter_from_user_mode (regs);instrumentation_begin (); +local_irq_enable ();/* Fetch EBP from where the vDSO stashed it. */ if (IS_ENABLED (CONFIG_X86_64 )) {/*
include/linux/entry-common.h +5 -21
@@ -88,23 +88,6 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, long syscareturn syscall_enter_audit (regs, syscall, work); } -/** -* syscall_enter_from_user_mode_prepare - Establish state and enable interrupts -* @regs: Pointer to currents pt_regs -* -* Invoked from architecture specific syscall entry code with interrupts -* disabled. The calling code has to be non-instrumentable. When the -* function returns all state is correct, interrupts are enabled and the -* subsequent functions can be instrumented. -* -* This handles lockdep, RCU (context tracking) and tracing state, i.e. -* the functionality provided by enter_from_user_mode(). -* -* This is invoked when there is extra architecture specific functionality -* to be done between establishing state and handling user mode entry work. -*/ -void syscall_enter_from_user_mode_prepare (struct pt_regs *regs); -/** * syscall_enter_from_user_mode_work - Check and handle work before invoking * a syscall
@@ -112,8 +95,8 @@ void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);* @syscall: The syscall number * * Invoked from architecture specific syscall entry code with interrupts -* enabled after invoking syscall_enter_from_user_mode_prepare() and extra -* architecture specific work. +* enabled after invoking enter_from_user_mode(), enabling interrupts and +* extra architecture specific work. * * Returns: The original or a modified syscall number *
@@ -149,8 +132,9 @@ static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *re* function returns all state is correct, interrupts are enabled and the * subsequent functions can be instrumented. * -* This is combination of syscall_enter_from_user_mode_prepare() and -* syscall_enter_from_user_mode_work(). +* This is the combination of enter_from_user_mode() and +* syscall_enter_from_user_mode_work() to be used when there is no +* architecture specific work to be done between the two. * * Returns: The original or a modified syscall number. See * syscall_enter_from_user_mode_work() for further explanation.
kernel/entry/syscall-common.c +0 -8
@@ -29,14 +29,6 @@ long syscall_enter_audit(struct pt_regs *regs, long syscall,return syscall; } -noinstr void syscall_enter_from_user_mode_prepare(struct pt_regs *regs) -{ -enter_from_user_mode (regs); -instrumentation_begin (); -local_irq_enable (); -instrumentation_end (); -} -/* * If SYSCALL_EMU is set, then the only reason to report is when * SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/5 entry: Split generic entry into generic exception and syscall entry
In reply to Sasha LevinFrom: Jinjie Ruan <[email protected]>
[ Upstream commit a70e9f647f501e36a6a092888b1ea7386b7c5664 ]
Currently CONFIG_GENERIC_ENTRY enables both the generic exception entry logic and the generic syscall entry logic, which are otherwise loosely coupled.
Introduce separate config options for these so that architectures can select the two independently. This will make it easier for architectures to migrate to generic entry code.
[Linus Walleij: rebase onto v6.16-rc1]
Stable-dep-of: 4a3591287fb7 ("entry: Fix seccomp bypass after ptrace with TSYNC")
--- MAINTAINERS | 1 + arch/Kconfig | 9 + include/linux/entry-common.h | 415 +++---------------------------- include/linux/irq-entry-common.h | 394 +++++++++++++++++++++++++++++ kernel/entry/Makefile | 3 +- kernel/entry/common.c | 121 +-------- kernel/entry/syscall-common.c | 86 +++++++ kernel/sched/core.c | 8 +- 8 files changed, 535 insertions(+), 502 deletions(-) create mode 100644 include/linux/irq-entry-common.h create mode 100644 kernel/entry/syscall-common.c
MAINTAINERS +1 -0
@@ -8562,6 +8562,7 @@ S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git core/entry F: include/linux/entry-common.h F: include/linux/entry-kvm.h +F: include/linux/irq-entry-common.h F: kernel/entry/ GENERIC GPIO I2C DRIVER
arch/Kconfig +9 -0
@@ -45,8 +45,17 @@ config HOTPLUG_SMT config SMT_NUM_THREADS_DYNAMIC bool +config GENERIC_IRQ_ENTRY + bool + +config GENERIC_SYSCALL + bool + depends on GENERIC_IRQ_ENTRY + config GENERIC_ENTRY bool + select GENERIC_IRQ_ENTRY + select GENERIC_SYSCALL config KPROBES bool "Kprobes"
include/linux/entry-common.h +38 -377
460 lines not on this page · raw message
include/linux/irq-entry-common.h new file +394 -0
394 lines not on this page · raw message
kernel/entry/Makefile +2 -1
@@ -9,5 +9,6 @@ KCOV_INSTRUMENT := n CFLAGS_REMOVE_common.o = -fstack-protector -fstack-protector-strong CFLAGS_common.o += -fno-stack-protector -obj-$(CONFIG_GENERIC_ENTRY) += common.o syscall_user_dispatch.o +obj-$(CONFIG_GENERIC_IRQ_ENTRY) += common.o +obj-$(CONFIG_GENERIC_SYSCALL) += syscall-common.o syscall_user_dispatch.o obj-$(CONFIG_KVM_XFER_TO_GUEST_WORK) += kvm.o
kernel/entry/common.c +1 -120
139 lines not on this page · raw message
kernel/entry/syscall-common.c new file +86 -0
86 lines not on this page · raw message
kernel/sched/core.c +4 -4
@@ -66,8 +66,8 @@#include <linux/workqueue_api.h> #ifdef CONFIG_PREEMPT_DYNAMIC -# ifdef CONFIG_GENERIC_ENTRY -# include <linux/entry-common.h> +# ifdef CONFIG_GENERIC_IRQ_ENTRY +# include <linux/irq-entry-common.h> # endif #endif
@@ -8499,8 +8499,8 @@ EXPORT_SYMBOL(__cond_resched_rwlock_write);#ifdef CONFIG_PREEMPT_DYNAMIC -#ifdef CONFIG_GENERIC_ENTRY -#include <linux/entry-common.h> +#ifdef CONFIG_GENERIC_IRQ_ENTRY +#include <linux/irq-entry-common.h> #endif /*
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/5 entry: Remove unused syscall argument from syscall_trace_enter()
In reply to Sasha LevinFrom: Jinjie Ruan <[email protected]>
[ Upstream commit 03150a9f84b328f5c724b8ed9ff8600c2d7e2d7b ]
The 'syscall' argument of syscall_trace_enter() is immediately overwritten before any real use and serves only as a local variable, so drop the parameter.
No functional change intended.
[ sashal: Reduced backport -- upstream 03150a9f84b32 touches 2 file(s), this
backport carries 1. Not backported here:
kernel/entry/syscall-common.c
This note is generated from the file lists only; see the resolution record for the reasoning. ]
Stable-dep-of: 4a3591287fb7 ("entry: Fix seccomp bypass after ptrace with TSYNC")
--- include/linux/entry-common.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/entry-common.h +3 -3
@@ -53,10 +53,10 @@ bool syscall_user_dispatch(struct pt_regs *regs);long syscall_enter_audit (struct pt_regs *regs,long syscall,unsigned long work); -static __always_inline long syscall_trace_enter (struct pt_regs *regs,long syscall, +static __always_inline long syscall_trace_enter (struct pt_regs *regs,unsigned long work) { -long ret =0 ; +long syscall, ret =0 ;/* * Handle Syscall User Dispatch. This must comes first, since
@@ -116,7 +116,7 @@ static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *reunsigned long work =READ_ONCE (current_thread_info ()->syscall_work);if (work &SYSCALL_WORK_ENTER ) - syscall =syscall_trace_enter (regs, syscall, work); + syscall =syscall_trace_enter (regs, work);return syscall; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/5 entry: Fix seccomp bypass after ptrace with TSYNC
In reply to Sasha LevinFrom: Jinjie Ruan <[email protected]>
[ Upstream commit 4a3591287fb7f808e209b4974ed337f609a2006b ]
Sashiko review pointed out the following issue.
If a thread is stopped in syscall_trace_enter() for ptrace, another thread can install a seccomp filter with SECCOMP_FILTER_FLAG_TSYNC (e.g., via seccomp_attach_filter()). This will successfully set SYSCALL_WORK_SECCOMP on the stopped thread, but syscall_trace_enter() evaluates a cached 'work' variable sampled on entry. Consequently, the subsequent check for SYSCALL_WORK_SECCOMP misses the newly assigned flag, and the filter is silently bypassed.
This race condition could allow an unprivileged process to execute a prohibited system call (e.g., execve) that the newly installed filter was intended to block, especially since the tracer might have modified the system call number during the ptrace stop.
Fix this by re-reading the syscall_work flags after ptrace handling, so that any new SYSCALL_WORK_SECCOMP flag set by another thread via TSYNC during the ptrace stop is observed before the subsequent seccomp check.
--- include/linux/entry-common.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/entry-common.h +3 -0
@@ -73,6 +73,9 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs,if (!arch_ptrace_report_syscall_permit_entry (regs) || (work &SYSCALL_WORK_SYSCALL_EMU ))return -1L ; + +/* ptrace might have changed work flags */ + work =READ_ONCE (current_thread_info ()->syscall_work); }/* Do seccomp after ptrace, to catch any tracer changes. */
Signature · 1 line
2.53.0