Skip to contentSkip to conversation

Restartable Sequences Fixes

PATCH Applied Mathieu Desnoyers 5 participants 12 messages Dec 11, 2019 – Jan 7, 2020

Subsystems (4)
lore
Share & tools
lore

Mathieu Desnoyers

author maintainer
#0 lore raw

cover Restartable Sequences Fixes

Hi,

Here is a repost of a small set of rseq fixes which was initially posted in September 2019. It now targets kernel 5.5. Those should be backported to stable kernels >= 4.18.

Thanks,

Mathieu

Mathieu Desnoyers (3):
rseq: Fix: Reject unknown flags on rseq unregister
rseq: Fix: Unregister rseq for clone CLONE_VM rseq/selftests: Fix: Namespace gettid() for compatibility with glibc

    2.30
 include/linux/sched.h                     |  4 ++--
 kernel/rseq.c                             |  2 ++
 tools/testing/selftests/rseq/param_test.c | 18 ++++++++++--------
 3 files changed, 14 insertions(+), 10 deletions(-)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH 1/3 rseq: Fix: Reject unknown flags on rseq unregister

It is preferrable to reject unknown flags within rseq unregistration rather than to ignore them. It is an oversight caused by the fact that the check for unknown flags is after the rseq unregister flag check.

Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Boqun Feng <[email protected]>
Cc "H . Peter Anvin" <[email protected]>
Cc Paul Turner <[email protected]>
Cc <[email protected]> # v4.18+
---
 kernel/rseq.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rseq.c +2 -0
index 27c48eb7de40..a4f86a9d6937 100644
@@ -310,6 +310,8 @@ SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
 	int ret;
 
 	if (flags & RSEQ_FLAG_UNREGISTER) {
+		if (flags & ~RSEQ_FLAG_UNREGISTER)
+			return -EINVAL;
 		/* Unregister rseq for current thread. */
 		if (current->rseq != rseq || !current->rseq)
 			return -EINVAL;
Signature · 1 line
2.17.1
#2 lore raw

[tip: core/urgent] rseq: Reject unknown flags on rseq unregister

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

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

Commit-ID:     66528a4575eee9f5a5270219894ab6178f146e84
Gitweb:        https://git.kernel.org/tip/66528a4575eee9f5a5270219894ab6178f146e84
Author:        Mathieu Desnoyers <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:17:11 -05:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:41:20 +01:00

rseq: Reject unknown flags on rseq unregister

It is preferrable to reject unknown flags within rseq unregistration rather than to ignore them. It is an oversight caused by the fact that the check for unknown flags is after the rseq unregister flag check.

---
 kernel/rseq.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rseq.c +2 -0
index 27c48eb..a4f86a9 100644
@@ -310,6 +310,8 @@ SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
 	int ret;
 
 	if (flags & RSEQ_FLAG_UNREGISTER) {
+		if (flags & ~RSEQ_FLAG_UNREGISTER)
+			return -EINVAL;
 		/* Unregister rseq for current thread. */
 		if (current->rseq != rseq || !current->rseq)
 			return -EINVAL;
#3 lore raw

Re: [tip: core/urgent] rseq: Reject unknown flags on rseq unregister

Quoted 31 lines · On Wed, Dec 25, 2019 at 10:38:53AM -0000, tip-bot2 for Mathieu Desnoyers wrote:

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

Commit-ID:     66528a4575eee9f5a5270219894ab6178f146e84
Gitweb:        https://git.kernel.org/tip/66528a4575eee9f5a5270219894ab6178f146e84
Author:        Mathieu Desnoyers <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:17:11 -05:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:41:20 +01:00

rseq: Reject unknown flags on rseq unregister

It is preferrable to reject unknown flags within rseq unregistration rather than to ignore them. It is an oversight caused by the fact that the check for unknown flags is after the rseq unregister flag check.

---
 kernel/rseq.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rseq.c +0 -0
index 27c48eb..a4f86a9 100644

kernel/rseq.c SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len, · line 317

Quoted 8 lines
kernel/rseq.c +2 -0
@@ -310,6 +310,8 @@ SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
 	int ret;
 
 	if (flags & RSEQ_FLAG_UNREGISTER) {
+		if (flags & ~RSEQ_FLAG_UNREGISTER)
+			return -EINVAL;
 		/* Unregister rseq for current thread. */
 		if (current->rseq != rseq || !current->rseq)
 			return -EINVAL;
Cc stable perhaps?
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#4 lore raw

Re: [tip: core/urgent] rseq: Reject unknown flags on rseq unregister

Quoted 37 lines · ----- On Dec 25, 2019, at 6:39 AM, Borislav Petkov [email protected] wrote:
Quoted 36 lines · On Wed, Dec 25, 2019 at 10:38:53AM -0000, tip-bot2 for Mathieu Desnoyers wrote:

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

Commit-ID: 66528a4575eee9f5a5270219894ab6178f146e84
Gitweb:
https://git.kernel.org/tip/66528a4575eee9f5a5270219894ab6178f146e84

Author:        Mathieu Desnoyers <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:17:11 -05:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:41:20 +01:00

rseq: Reject unknown flags on rseq unregister

It is preferrable to reject unknown flags within rseq unregistration rather than to ignore them. It is an oversight caused by the fact that the check for unknown flags is after the rseq unregister flag check.

Link:
https://lkml.kernel.org/r/[email protected]

---
 kernel/rseq.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rseq.c +0 -0
index 27c48eb..a4f86a9 100644
@@ -310,6 +310,8 @@ SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32,
 rseq_len,
 	int ret;
 
Quoted 8 lines
Quoted 6 lines
kernel/rseq.c +2 -0
 	if (flags & RSEQ_FLAG_UNREGISTER) {
+		if (flags & ~RSEQ_FLAG_UNREGISTER)
+			return -EINVAL;
 		/* Unregister rseq for current thread. */
 		if (current->rseq != rseq || !current->rseq)

return -EINVAL;

Cc stable perhaps?

This could indeed be a candidate for stable, even though it's just a stricter checking of unknown flags (returning an error rather than ignoring them).

Adding stable in CC here.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
author maintainer
#5 lore raw

Re: [tip: core/urgent] rseq: Reject unknown flags on rseq unregister

Quoted 48 lines · ----- On Dec 26, 2019, at 5:32 PM, Mathieu Desnoyers…
Quoted 37 lines · ----- On Dec 25, 2019, at 6:39 AM, Borislav Petkov [email protected] wrote:
Quoted 36 lines · On Wed, Dec 25, 2019 at 10:38:53AM -0000, tip-bot2 for Mathieu Desnoyers wrote:

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

Commit-ID: 66528a4575eee9f5a5270219894ab6178f146e84
Gitweb:
https://git.kernel.org/tip/66528a4575eee9f5a5270219894ab6178f146e84

Author:        Mathieu Desnoyers <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:17:11 -05:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:41:20 +01:00

rseq: Reject unknown flags on rseq unregister

It is preferrable to reject unknown flags within rseq unregistration rather than to ignore them. It is an oversight caused by the fact that the check for unknown flags is after the rseq unregister flag check.

Link:
https://lkml.kernel.org/r/[email protected]

---
 kernel/rseq.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rseq.c +0 -0
index 27c48eb..a4f86a9 100644
@@ -310,6 +310,8 @@ SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32,
 rseq_len,
 	int ret;
 
Quoted 8 lines
Quoted 6 lines
kernel/rseq.c +2 -0
 	if (flags & RSEQ_FLAG_UNREGISTER) {
+		if (flags & ~RSEQ_FLAG_UNREGISTER)
+			return -EINVAL;
 		/* Unregister rseq for current thread. */
 		if (current->rseq != rseq || !current->rseq)

return -EINVAL;

Cc stable perhaps?
Quoted 4 lines

This could indeed be a candidate for stable, even though it's just a stricter checking of unknown flags (returning an error rather than ignoring them).

Adding stable in CC here.

For the records, I had stable in CC in my original patch submission. The stable CC has been stripped when it was merged into the tip tree.

Thanks,

Mathieu

Quoted 10 lines

Thanks,

Mathieu

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
maintainer
#6 lore raw

Re: [tip: core/urgent] rseq: Reject unknown flags on rseq unregister

Quoted 2 lines · Mathieu Desnoyers

For the records, I had stable in CC in my original patch submission. The stable CC has been stripped when it was merged into the tip tree.

Argh, lemme go fix my scripts _again_..

I was recently made aware that we should not have spurious Cc: tags in commit messages, but obviously the stable thing is an exception there.

author maintainer
#7 lore raw

PATCH 2/3 rseq: Fix: Unregister rseq for clone CLONE_VM

In reply to Mathieu Desnoyers

It has been reported by Google that rseq is not behaving properly with respect to clone when CLONE_VM is used without CLONE_THREAD. It keeps the prior thread's rseq TLS registered when the TLS of the thread has moved, so the kernel can corrupt the TLS of the parent.

The approach of clearing the per task-struct rseq registration on clone with CLONE_THREAD flag is incomplete. It does not cover the use-case of clone with CLONE_VM set, but without CLONE_THREAD.

Here is the rationale for unregistering rseq on clone with CLONE_VM flag set:

1) CLONE_THREAD requires CLONE_SIGHAND, which requires CLONE_VM to be set. Therefore, just checking for CLONE_VM covers all CLONE_THREAD uses. There is no point in checking for both CLONE_THREAD and CLONE_VM,

2) There is the possibility of an unlikely scenario where CLONE_SETTLS is used without CLONE_VM. In order to be an issue, it would require that the rseq TLS is in a shared memory area.

I do not plan on adding CLONE_SETTLS to the set of clone flags which unregister RSEQ, because it would require that we also unregister RSEQ on set_thread_area(2) and arch_prctl(2) ARCH_SET_FS for completeness. So rather than doing a partial solution, it appears better to let user-space explicitly perform rseq unregistration across clone if needed in scenarios where CLONE_VM is not set.

Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Boqun Feng <[email protected]>
Cc "H . Peter Anvin" <[email protected]>
Cc Paul Turner <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Neel Natu <[email protected]>
Cc <[email protected]> # v4.18+
---
 include/linux/sched.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/sched.h +2 -2
index 467d26046416..716ad1d8d95e 100644
@@ -1929,11 +1929,11 @@ static inline void rseq_migrate(struct task_struct *t)
 
 /*
  * If parent process has a registered restartable sequences area, the
- * child inherits. Only applies when forking a process, not a thread.
+ * child inherits. Unregister rseq for a clone with CLONE_VM set.
  */
 static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
 {
-	if (clone_flags & CLONE_THREAD) {
+	if (clone_flags & CLONE_VM) {
 		t->rseq = NULL;
 		t->rseq_sig = 0;
 		t->rseq_event_mask = 0;
Signature · 1 line
2.17.1
#8 lore raw

[tip: core/urgent] rseq: Unregister rseq for clone CLONE_VM

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

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

Commit-ID:     463f550fb47bede3a5d7d5177f363a6c3b45d50b
Gitweb:        https://git.kernel.org/tip/463f550fb47bede3a5d7d5177f363a6c3b45d50b
Author:        Mathieu Desnoyers <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:17:12 -05:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:41:20 +01:00

rseq: Unregister rseq for clone CLONE_VM

It has been reported by Google that rseq is not behaving properly with respect to clone when CLONE_VM is used without CLONE_THREAD.

It keeps the prior thread's rseq TLS registered when the TLS of the thread has moved, so the kernel can corrupt the TLS of the parent.

The approach of clearing the per task-struct rseq registration on clone with CLONE_THREAD flag is incomplete. It does not cover the use-case of clone with CLONE_VM set, but without CLONE_THREAD.

Here is the rationale for unregistering rseq on clone with CLONE_VM flag set:

1) CLONE_THREAD requires CLONE_SIGHAND, which requires CLONE_VM to be set. Therefore, just checking for CLONE_VM covers all CLONE_THREAD uses. There is no point in checking for both CLONE_THREAD and CLONE_VM,

2) There is the possibility of an unlikely scenario where CLONE_SETTLS is used without CLONE_VM. In order to be an issue, it would require that the rseq TLS is in a shared memory area.

I do not plan on adding CLONE_SETTLS to the set of clone flags which unregister RSEQ, because it would require that we also unregister RSEQ on set_thread_area(2) and arch_prctl(2) ARCH_SET_FS for completeness. So rather than doing a partial solution, it appears better to let user-space explicitly perform rseq unregistration across clone if needed in scenarios where CLONE_VM is not set.

---
 include/linux/sched.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/sched.h +2 -2
index 467d260..716ad1d 100644
@@ -1929,11 +1929,11 @@ static inline void rseq_migrate(struct task_struct *t)
 
 /*
  * If parent process has a registered restartable sequences area, the
- * child inherits. Only applies when forking a process, not a thread.
+ * child inherits. Unregister rseq for a clone with CLONE_VM set.
  */
 static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
 {
-	if (clone_flags & CLONE_THREAD) {
+	if (clone_flags & CLONE_VM) {
 		t->rseq = NULL;
 		t->rseq_sig = 0;
 		t->rseq_event_mask = 0;
author maintainer
#9 lore raw

PATCH 3/3 rseq/selftests: Fix: Namespace gettid() for compatibility with glibc 2.30

In reply to Mathieu Desnoyers

glibc 2.30 introduces gettid() in public headers, which clashes with the internal static definition within rseq selftests.

Rename gettid() to rseq_gettid() to eliminate this symbol name clash.

Cc Shuah Khan <[email protected]>
Cc Tommi T. Rantala <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Boqun Feng <[email protected]>
Cc "H . Peter Anvin" <[email protected]>
Cc Paul Turner <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc <[email protected]> # v4.18+
---
 tools/testing/selftests/rseq/param_test.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
tools/testing/selftests/rseq/param_test.c +10 -8
index eec2663261f2..e8a657a5f48a 100644
@@ -15,7 +15,7 @@
 #include <errno.h>
 #include <stddef.h>
 
-static inline pid_t gettid(void)
+static inline pid_t rseq_gettid(void)
 {
 	return syscall(__NR_gettid);
 }
@@ -373,11 +373,12 @@ void *test_percpu_spinlock_thread(void *arg)
 		rseq_percpu_unlock(&data->lock, cpu);
 #ifndef BENCHMARK
 		if (i != 0 && !(i % (reps / 10)))
-			printf_verbose("tid %d: count %lld\n", (int) gettid(), i);
+			printf_verbose("tid %d: count %lld\n",
+				       (int) rseq_gettid(), i);
 #endif
 	}
 	printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
-		       (int) gettid(), nr_abort, signals_delivered);
+		       (int) rseq_gettid(), nr_abort, signals_delivered);
 	if (!opt_disable_rseq && thread_data->reg &&
 	    rseq_unregister_current_thread())
 		abort();
@@ -454,11 +455,12 @@ void *test_percpu_inc_thread(void *arg)
 		} while (rseq_unlikely(ret));
 #ifndef BENCHMARK
 		if (i != 0 && !(i % (reps / 10)))
-			printf_verbose("tid %d: count %lld\n", (int) gettid(), i);
+			printf_verbose("tid %d: count %lld\n",
+				       (int) rseq_gettid(), i);
 #endif
 	}
 	printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
-		       (int) gettid(), nr_abort, signals_delivered);
+		       (int) rseq_gettid(), nr_abort, signals_delivered);
 	if (!opt_disable_rseq && thread_data->reg &&
 	    rseq_unregister_current_thread())
 		abort();
@@ -605,7 +607,7 @@ void *test_percpu_list_thread(void *arg)
 	}
 
 	printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
-		       (int) gettid(), nr_abort, signals_delivered);
+		       (int) rseq_gettid(), nr_abort, signals_delivered);
 	if (!opt_disable_rseq && rseq_unregister_current_thread())
 		abort();
 
@@ -796,7 +798,7 @@ void *test_percpu_buffer_thread(void *arg)
 	}
 
 	printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
-		       (int) gettid(), nr_abort, signals_delivered);
+		       (int) rseq_gettid(), nr_abort, signals_delivered);
 	if (!opt_disable_rseq && rseq_unregister_current_thread())
 		abort();
 
@@ -1011,7 +1013,7 @@ void *test_percpu_memcpy_buffer_thread(void *arg)
 	}
 
 	printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
-		       (int) gettid(), nr_abort, signals_delivered);
+		       (int) rseq_gettid(), nr_abort, signals_delivered);
 	if (!opt_disable_rseq && rseq_unregister_current_thread())
 		abort();
 
Signature · 1 line
2.17.1
#10 lore raw

Re: [PATCH for 5.5 3/3] rseq/selftests: Fix: Namespace gettid() for compatibility with glibc 2.30

Quoted 17 lines · Mathieu Desnoyers

glibc 2.30 introduces gettid() in public headers, which clashes with the internal static definition within rseq selftests.

Rename gettid() to rseq_gettid() to eliminate this symbol name clash.

Cc Shuah Khan <[email protected]>
Cc Tommi T. Rantala <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Boqun Feng <[email protected]>
Cc "H . Peter Anvin" <[email protected]>
Cc Paul Turner <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc <[email protected]> # v4.18+
Quoted 2 lines
---
  tools/testing/selftests/rseq/param_test.c | 18 ++++++++++--------

I am pulling this patch in for Linux 5.5-rc4.

Let me know if you have any objections.

thanks,
-- Shuah

maintainer Applied
#11 lore raw
In reply to Mathieu Desnoyers
Quoted 8 lines · Mathieu Desnoyers

Hi,

Here is a repost of a small set of rseq fixes which was initially posted in September 2019. It now targets kernel 5.5. Those should be backported to stable kernels >= 4.18.

Thanks,

Quoted 7 lines

Mathieu

Mathieu Desnoyers (3):
rseq: Fix: Reject unknown flags on rseq unregister
rseq: Fix: Unregister rseq for clone CLONE_VM rseq/selftests: Fix: Namespace gettid() for compatibility with glibc

    2.30

I've picked up the first two patches, thanks!