All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rseq: Clear yield state when syscall entry aborts
@ 2026-12-02  8:50 Shawn Shao
  2026-12-04 13:10 ` Thomas Gleixner
  0 siblings, 1 reply; 4+ messages in thread
From: Shawn Shao @ 2026-12-02  8:50 UTC (permalink / raw)
  To: tglx, peterz, mathieu.desnoyers
  Cc: Andy Lutomirski, Paul E. McKenney, Boqun Feng, Dmitry Vyukov,
	linux-kernel

rseq_syscall_enter_work() can set the yield result before ptrace and seccomp
policy checks run. If either check prevents the syscall from being dispatched,
the syscall body does not consume that result. A later rseq_slice_yield() can
then return a stale success.

Clear the one-shot result when syscall entry rejects the call, and when ptrace
changes the syscall number so the yield body cannot consume the original
result. The normal rseq_slice_yield() path continues to consume its result in
the syscall body.

The time-slice extension is opt-in. This is a correctness issue in the return
value; the reproducer does not demonstrate memory corruption or a privilege
boundary bypass.

Fixes: dd0a04606937 ("rseq: Implement syscall entry work for time slice extensions")
Signed-off-by: Shawn Shao <shawnq1@163.com>

---
Testing:
- x86-64 bzImage build with CONFIG_RSEQ_SLICE_EXTENSION=y passed.
- The x86-64 syscall-entry object compiled with CONFIG_RSEQ_SLICE_EXTENSION
  disabled.
- On the unpatched kernel, three fresh QEMU TCG boots returned 1 from the
  allowed yield after the preceding seccomp-denied yield, with slice_ctrl=0.
- With the patch, two fresh boots returned 0 for that sequence.
- The patched no-grant control passed 10,000 denied/allowed pairs and returned
  0; the live-grant control retained the expected return value of 1.
- git apply --check and checkpatch.pl --strict passed; checkpatch reported
  0 errors and 0 warnings.

Reproducer and results:
https://gist.github.com/shaos26/525e502547c0fd673e93067aed7f6d88
console output: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/d7eb70ad659d30fc8a4ae012515270b9a8148ff3/console-output.txt
kernel config: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/2ef579c9651051d222d532a9d339d3fccb7a985a/kernel-config.txt
PoC: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/a2a99144687e987376c8b045b3f29eeb5098c88d/poc.c
initramfs init: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/97e031180a22a2c59c037d6b01f872a50dbd7895/init
initramfs builder: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/32d33092bc0c0920a6389bf98e0ddab8b5c6eeec/build-initramfs.sh
QEMU runner: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/5d44df3440bbfb04518763279006a9ad8144731b/run-qemu.sh
reproduction steps: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/d123a444579160125457b3c7e39a7e2cec5b8c01/reproduction.md
reproduction wrapper: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/40a5c2d5fe81024f4020b146240d010f575463dc/reproduce.sh
patch file: https://gist.githubusercontent.com/shaos26/525e502547c0fd673e93067aed7f6d88/raw/8a1710df3bfb7155cc0f13e25bb872c0134558df/fix.patch

 include/linux/entry-common.h | 14 +++++++++++++-
 include/linux/rseq.h         |  2 ++
 kernel/rseq.c                |  5 +++++
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h
index 15278416f8860..557d55cb8cd34 100644
--- a/include/linux/entry-common.h
+++ b/include/linux/entry-common.h
@@ -140,15 +140,27 @@ static __always_inline bool syscall_trace_enter(struct pt_regs *regs, unsigned l
 static __always_inline bool syscall_enter_from_user_mode_work(struct pt_regs *regs, long *syscall)
 {
 	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
+	long syscall_nr;
 
 	if (!(work & SYSCALL_WORK_ENTER))
 		return true;
 
-	if (unlikely(!syscall_trace_enter(regs, work, *syscall)))
+	syscall_nr = *syscall;
+
+	/*
+	 * rseq entry work can prepare a yield result before ptrace or seccomp
+	 * decides whether to invoke the syscall.
+	 */
+	if (unlikely(!syscall_trace_enter(regs, work, *syscall))) {
+		rseq_syscall_enter_clear_yielded();
 		return false;
+	}
 
 	/* Reread the syscall number as it might have been modified */
 	*syscall = syscall_get_nr(current, regs);
+	/* A changed syscall number cannot consume the original yield result. */
+	if (unlikely(*syscall != syscall_nr))
+		rseq_syscall_enter_clear_yielded();
 
 	return true;
 }
diff --git a/include/linux/rseq.h b/include/linux/rseq.h
index 7ef79b25e714b..0971ace23ee73 100644
--- a/include/linux/rseq.h
+++ b/include/linux/rseq.h
@@ -192,9 +192,11 @@ static inline void rseq_syscall(struct pt_regs *regs) { }
 
 #ifdef CONFIG_RSEQ_SLICE_EXTENSION
 void rseq_syscall_enter_work(long syscall);
+void rseq_syscall_enter_clear_yielded(void);
 int rseq_slice_extension_prctl(unsigned long arg2, unsigned long arg3);
 #else /* CONFIG_RSEQ_SLICE_EXTENSION */
 static inline void rseq_syscall_enter_work(long syscall) { }
+static inline void rseq_syscall_enter_clear_yielded(void) { }
 static inline int rseq_slice_extension_prctl(unsigned long arg2, unsigned long arg3)
 {
 	return -ENOTSUPP;
diff --git a/kernel/rseq.c b/kernel/rseq.c
index e75e3a5e312c8..90e9eddeeb140 100644
--- a/kernel/rseq.c
+++ b/kernel/rseq.c
@@ -743,6 +743,11 @@ void rseq_syscall_enter_work(long syscall)
 		force_sig(SIGSEGV);
 }
 
+void rseq_syscall_enter_clear_yielded(void)
+{
+	current->rseq.slice.yielded = 0;
+}
+
 int rseq_slice_extension_prctl(unsigned long arg2, unsigned long arg3)
 {
 	switch (arg2) {


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rseq: Clear yield state when syscall entry aborts
  2026-12-02  8:50 [PATCH] rseq: Clear yield state when syscall entry aborts Shawn Shao
@ 2026-12-04 13:10 ` Thomas Gleixner
  2026-12-05  3:39   ` [PATCH v2] rseq: Clear stale yield state on slice grant Shawn Shao
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Gleixner @ 2026-12-04 13:10 UTC (permalink / raw)
  To: Shawn Shao, peterz, mathieu.desnoyers
  Cc: Andy Lutomirski, Paul E. McKenney, Boqun Feng, Dmitry Vyukov,
	linux-kernel

On Wed, Dec 02 2026 at 08:50, Shawn Shao wrote:
> rseq_syscall_enter_work() can set the yield result before ptrace and seccomp
> policy checks run. If either check prevents the syscall from being dispatched,
> the syscall body does not consume that result. A later rseq_slice_yield() can
> then return a stale success.
>
> Clear the one-shot result when syscall entry rejects the call, and when ptrace
> changes the syscall number so the yield body cannot consume the original
> result. The normal rseq_slice_yield() path continues to consume its result in
> the syscall body.
>
> The time-slice extension is opt-in. This is a correctness issue in the return

I agree, but...

>  static __always_inline bool syscall_enter_from_user_mode_work(struct pt_regs *regs, long *syscall)
>  {
>  	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
> +	long syscall_nr;
>  
>  	if (!(work & SYSCALL_WORK_ENTER))
>  		return true;
>  
> -	if (unlikely(!syscall_trace_enter(regs, work, *syscall)))
> +	syscall_nr = *syscall;
> +
> +	/*
> +	 * rseq entry work can prepare a yield result before ptrace or seccomp
> +	 * decides whether to invoke the syscall.
> +	 */
> +	if (unlikely(!syscall_trace_enter(regs, work, *syscall))) {
> +		rseq_syscall_enter_clear_yielded();
>  		return false;
> +	}
>  
>  	/* Reread the syscall number as it might have been modified */
>  	*syscall = syscall_get_nr(current, regs);
> +	/* A changed syscall number cannot consume the original yield result. */
> +	if (unlikely(*syscall != syscall_nr))
> +		rseq_syscall_enter_clear_yielded();

... adding this unholy mess to cure that is not justified.

It's sufficient to do wipe out stale state when the next slice extension
is granted because it's arguable that rseq_sched_yield() only provides a
valid result when invoked in the related section if and only if the
granted bit is observed, i.e.:

start:
    rseq->slice_ctrl.request = 1;
    critical_section();
    rseq->slice_ctrl.request = 0;
    if (rseq->slice_ctrl.granted)
        rseq_slice_yield();
end:

Any invocation after #end is bogus to begin with. So in case that
rseq_slice_yield() is invoked and a tracer aborts the syscall after
recording yielded, then the only requirement is that the next sequence
which requests a slice extension and observes granted at the end get's
the correct information.

Something like the below plus an update to the documentation which
explains the validity rules of rseq_slice_yield().

No?

Thanks,

        tglx
---
--- a/include/linux/rseq_entry.h
+++ b/include/linux/rseq_entry.h
@@ -174,6 +174,9 @@ static __always_inline bool __rseq_grant
 	rseq_stat_inc(rseq_stats.s_granted);
 
 	curr->rseq.slice.state.granted = true;
+	/* Wipe out stale state caused by a tracer aborting rseq_slice_yield() */
+	curr->rseq.slice.yielded = false;
+
 	/* Store expiry time for arming the timer on the way out */
 	curr->rseq.slice.expires = data_race(rseq_slice_ext_nsecs) + ktime_get_mono_fast_ns();
 	/*



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] rseq: Clear stale yield state on slice grant
  2026-12-04 13:10 ` Thomas Gleixner
@ 2026-12-05  3:39   ` Shawn Shao
  2026-12-05 20:00     ` [tip: core/rseq] " tip-bot2 for Shawn Shao
  0 siblings, 1 reply; 4+ messages in thread
From: Shawn Shao @ 2026-12-05  3:39 UTC (permalink / raw)
  To: tglx, peterz, mathieu.desnoyers
  Cc: luto, paulmck, boqun, dvyukov, linux-kernel

rseq_syscall_enter_work() can set the yield result before ptrace or
seccomp decides whether to dispatch the syscall. If entry processing
aborts the call or changes the syscall number, the syscall body does
not consume the result.

The return value is valid only when rseq_slice_yield() follows a
grant observed by user space at the end of the corresponding critical
section. Clear the result on the next grant so an aborted call cannot
provide stale success if that grant is revoked before syscall entry work.

Document this userspace validity rule.

Tested on x86-64 QEMU TCG with
CONFIG_RSEQ_SLICE_EXTENSION=y. The regression returned 1 on the unpatched
kernel and 0 with this patch after a grant was observed and then revoked. A
live-grant control still returned 1.

Link: https://gist.github.com/shaos26/525e502547c0fd673e93067aed7f6d88

Fixes: dd0a04606937 ("rseq: Implement syscall entry work for time slice extensions")
Signed-off-by: Shawn Shao <shawnq1@163.com>
---
 Documentation/userspace-api/rseq.rst | 6 ++++++
 include/linux/rseq_entry.h           | 2 ++
 2 files changed, 8 insertions(+)

Changes in v2:
- Clear stale yield state on the next slice extension grant.
- Document when the rseq_slice_yield() return value is valid.

diff --git a/Documentation/userspace-api/rseq.rst b/Documentation/userspace-api/rseq.rst
index 8549a6c61531c..475fb023f4a7f 100644
--- a/Documentation/userspace-api/rseq.rst
+++ b/Documentation/userspace-api/rseq.rst
@@ -191,6 +191,12 @@ leaving the critical section to relinquish the CPU. The kernel enforces
 this by arming a timer to prevent misbehaving userspace from abusing this
 mechanism.
 
+The return value from ``rseq_slice_yield(2)`` is valid only when the syscall
+follows userspace observing the granted bit set at the end of the
+corresponding critical section. A call outside this sequence is invalid; its
+return value must not be used to determine the result of an earlier or
+unrelated time slice extension request.
+
 If both the request bit and the granted bit are false when leaving the
 critical section, then this indicates that a grant was revoked and no
 further action is required by userspace.
diff --git a/include/linux/rseq_entry.h b/include/linux/rseq_entry.h
index 31ce349ed42ce..7806fd8c51bde 100644
--- a/include/linux/rseq_entry.h
+++ b/include/linux/rseq_entry.h
@@ -174,6 +174,8 @@ static __always_inline bool __rseq_grant_slice_extension(bool work_pending)
 	rseq_stat_inc(rseq_stats.s_granted);
 
 	curr->rseq.slice.state.granted = true;
+	/* Clear stale state left by an aborted rseq_slice_yield() syscall. */
+	curr->rseq.slice.yielded = false;
 	/* Store expiry time for arming the timer on the way out */
 	curr->rseq.slice.expires = data_race(rseq_slice_ext_nsecs) + ktime_get_mono_fast_ns();
 	/*


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [tip: core/rseq] rseq: Clear stale yield state on slice grant
  2026-12-05  3:39   ` [PATCH v2] rseq: Clear stale yield state on slice grant Shawn Shao
@ 2026-12-05 20:00     ` tip-bot2 for Shawn Shao
  0 siblings, 0 replies; 4+ messages in thread
From: tip-bot2 for Shawn Shao @ 2026-12-05 20:00 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Shawn Shao, Thomas Gleixner, stable, x86, linux-kernel

The following commit has been merged into the core/rseq branch of tip:

Commit-ID:     6602eb187c8c083a170b89dab9e7abe509d598bf
Gitweb:        https://git.kernel.org/tip/6602eb187c8c083a170b89dab9e7abe509d598bf
Author:        Shawn Shao <shawnq1@163.com>
AuthorDate:    Sat, 05 Dec 2026 03:39:44 
Committer:     Thomas Gleixner <tglx@kernel.org>
CommitterDate: Sat, 05 Dec 2026 20:57:41 +01:00

rseq: Clear stale yield state on slice grant

rseq_syscall_enter_work() sets the yield result of a rseq_slice_yield()
syscall before ptrace or seccomp decides whether to dispatch the
syscall. If entry processing aborts the call or changes the syscall number,
the syscall body does not consume the result.

Due to the stale result a subsequent rseq_slice_yield() will return
'yielded == true' unconditionally. That's wrong as the subsequent
rseq_slice_yield() invocation is related to a subsequent time slice
request.

There is no point to handle this situation in the entry code after
rseq_syscall_enter_work() because the aborted syscall leaves the critical
section scope.

Fix this by clearing a potentially stale result when the next time slice is
granted.

Also clarify the undocumented validity of the rseq_slice_yield() return
value:

  The return value is only valid when rseq_slice_yield() follows a grant
  observed by user space at the end of the corresponding critical
  section. Any invocation outside of that condition and scope returns a
  undefined value.

[ tglx: Massage change log ]

Fixes: dd0a04606937 ("rseq: Implement syscall entry work for time slice extensions")
Signed-off-by: Shawn Shao <shawnq1@163.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Cc: stable@vger.kernel.org
Link: https://gist.github.com/shaos26/525e502547c0fd673e93067aed7f6d88
Link: https://patch.msgid.link/20261205033944.2871328-1-shawnq1@163.com
---
 Documentation/userspace-api/rseq.rst | 6 ++++++
 include/linux/rseq_entry.h           | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/Documentation/userspace-api/rseq.rst b/Documentation/userspace-api/rseq.rst
index 8549a6c..42b6e3a 100644
--- a/Documentation/userspace-api/rseq.rst
+++ b/Documentation/userspace-api/rseq.rst
@@ -191,6 +191,12 @@ leaving the critical section to relinquish the CPU. The kernel enforces
 this by arming a timer to prevent misbehaving userspace from abusing this
 mechanism.
 
+The return value from ``rseq_slice_yield(2)`` is only valid when the
+syscall follows userspace observing the granted bit set at the end of the
+corresponding critical section. A call outside this sequence is invalid;
+its return value is undefined and must not be used to determine the result
+of an earlier or unrelated time slice extension request.
+
 If both the request bit and the granted bit are false when leaving the
 critical section, then this indicates that a grant was revoked and no
 further action is required by userspace.
diff --git a/include/linux/rseq_entry.h b/include/linux/rseq_entry.h
index 31ce349..7806fd8 100644
--- a/include/linux/rseq_entry.h
+++ b/include/linux/rseq_entry.h
@@ -174,6 +174,8 @@ static __always_inline bool __rseq_grant_slice_extension(bool work_pending)
 	rseq_stat_inc(rseq_stats.s_granted);
 
 	curr->rseq.slice.state.granted = true;
+	/* Clear stale state left by an aborted rseq_slice_yield() syscall. */
+	curr->rseq.slice.yielded = false;
 	/* Store expiry time for arming the timer on the way out */
 	curr->rseq.slice.expires = data_race(rseq_slice_ext_nsecs) + ktime_get_mono_fast_ns();
 	/*

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-05 20:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  8:50 [PATCH] rseq: Clear yield state when syscall entry aborts Shawn Shao
2026-12-04 13:10 ` Thomas Gleixner
2026-12-05  3:39   ` [PATCH v2] rseq: Clear stale yield state on slice grant Shawn Shao
2026-12-05 20:00     ` [tip: core/rseq] " tip-bot2 for Shawn Shao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.