Skip to contentSkip to conversation

clone: fix CLONE_PIDFD support

PATCH Superseded Dmitry V. Levin 2 participants 7 messages Jul 14, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clone: fix CLONE_PIDFD support

The introduction of clone3 syscall accidentally broke CLONE_PIDFD support in traditional clone syscall on compat x86 and those architectures that use do_fork to implement clone syscall.

This bug was found by strace test suite.

Fixes 7f192e3cd316 ("fork: add clone3")
---
 arch/x86/ia32/sys_ia32.c | 1 +
 kernel/fork.c            | 1 +
 2 files changed, 2 insertions(+)
arch/x86/ia32/sys_ia32.c +1 -0
index 64a6c952091e..98754baf411a 100644
@@ -239,6 +239,7 @@ COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),
kernel/fork.c +1 -0
index 8f3e2d97d771..2c3cbad807b6 100644
@@ -2417,6 +2417,7 @@ long do_fork(unsigned long clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),
Signature · 1 line
ldv
#1 lore raw
Quoted 10 lines · Dmitry V. Levin

The introduction of clone3 syscall accidentally broke CLONE_PIDFD support in traditional clone syscall on compat x86 and those architectures that use do_fork to implement clone syscall.

This bug was found by strace test suite.

Fixes 7f192e3cd316 ("fork: add clone3")

Good catch! Thank you Dmitry.

One change request below.

Quoted 23 lines
---
 arch/x86/ia32/sys_ia32.c | 1 +
 kernel/fork.c            | 1 +
 2 files changed, 2 insertions(+)
arch/x86/ia32/sys_ia32.c +1 -0
index 64a6c952091e..98754baf411a 100644
@@ -239,6 +239,7 @@ COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),
kernel/fork.c +0 -0
index 8f3e2d97d771..2c3cbad807b6 100644

kernel/fork.c long do_fork(unsigned long clone_flags, · line 2423

Quoted 7 lines
kernel/fork.c +1 -0
@@ -2417,6 +2417,7 @@ long do_fork(unsigned long clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),

Both of these legacy clone helpers need to make CLONE_PIDFD and CLONE_PARENT_SETTID incompatible, i.e. could you please add a helper to kernel/fork.c:

bool legacy_clone_args_valid(const struct kernel_clone_args *kargs)

{
	/* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */
	if ((kargs->flags & CLONE_PIDFD) && (kargs->flags & CLONE_PARENT_SETTID))
		return false;
}

and export it and use it in ia32 too?

Then resend and I'll put it into my tree that already has a few other fixes about to be sent.

Christian

#2 lore raw
Quoted 51 lines · Christian Brauner
Quoted 10 lines · Dmitry V. Levin

The introduction of clone3 syscall accidentally broke CLONE_PIDFD support in traditional clone syscall on compat x86 and those architectures that use do_fork to implement clone syscall.

This bug was found by strace test suite.

Fixes 7f192e3cd316 ("fork: add clone3")

Good catch! Thank you Dmitry.

One change request below.

Quoted 23 lines
---
 arch/x86/ia32/sys_ia32.c | 1 +
 kernel/fork.c            | 1 +
 2 files changed, 2 insertions(+)
arch/x86/ia32/sys_ia32.c +1 -0
index 64a6c952091e..98754baf411a 100644
@@ -239,6 +239,7 @@ COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),
kernel/fork.c +0 -0
index 8f3e2d97d771..2c3cbad807b6 100644

kernel/fork.c long do_fork(unsigned long clone_flags, · line 2423

Quoted 7 lines
kernel/fork.c +1 -0
@@ -2417,6 +2417,7 @@ long do_fork(unsigned long clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),

Both of these legacy clone helpers need to make CLONE_PIDFD and CLONE_PARENT_SETTID incompatible, i.e. could you please add a helper to kernel/fork.c:

Quoted 8 lines

bool legacy_clone_args_valid(const struct kernel_clone_args *kargs)

{
	/* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */
	if ((kargs->flags & CLONE_PIDFD) && (kargs->flags & CLONE_PARENT_SETTID))
		return false;
}

and export it and use it in ia32 too?

copy_process already performs the check, isn't this enough? Also, the check in sys_clone looks redundant and I was going to suggest its removal.

Signature · 1 line
ldv
#3 lore raw
Quoted 59 lines · Dmitry V. Levin
Quoted 58 lines · Christian Brauner
Quoted 10 lines · Dmitry V. Levin

The introduction of clone3 syscall accidentally broke CLONE_PIDFD support in traditional clone syscall on compat x86 and those architectures that use do_fork to implement clone syscall.

This bug was found by strace test suite.

Fixes 7f192e3cd316 ("fork: add clone3")

Good catch! Thank you Dmitry.

One change request below.

Quoted 23 lines
---
 arch/x86/ia32/sys_ia32.c | 1 +
 kernel/fork.c            | 1 +
 2 files changed, 2 insertions(+)
arch/x86/ia32/sys_ia32.c +1 -0
index 64a6c952091e..98754baf411a 100644
@@ -239,6 +239,7 @@ COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),
kernel/fork.c +0 -0
index 8f3e2d97d771..2c3cbad807b6 100644

kernel/fork.c long do_fork(unsigned long clone_flags, · line 2423

Quoted 7 lines
kernel/fork.c +1 -0
@@ -2417,6 +2417,7 @@ long do_fork(unsigned long clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),

Both of these legacy clone helpers need to make CLONE_PIDFD and CLONE_PARENT_SETTID incompatible, i.e. could you please add a helper to kernel/fork.c:

bool legacy_clone_args_valid(const struct kernel_clone_args *kargs)

{
	/* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */
	if ((kargs->flags & CLONE_PIDFD) && (kargs->flags & CLONE_PARENT_SETTID))
		return false;
}
Quoted 3 lines
Quoted 1 line

and export it and use it in ia32 too?

copy_process already performs the check, isn't this enough?

No it doesn't anymore. clone3() allows CLONE_PIDFD + CLONE_PARENT_SETTID since struct clone_args has a dedicated pidfd return argument.

Quoted 2 lines

Also, the check in sys_clone looks redundant and I was going to suggest its removal.

See above.

#4 lore raw
Quoted 60 lines · Christian Brauner
Quoted 59 lines · Dmitry V. Levin
Quoted 58 lines · Christian Brauner
Quoted 10 lines · Dmitry V. Levin

The introduction of clone3 syscall accidentally broke CLONE_PIDFD support in traditional clone syscall on compat x86 and those architectures that use do_fork to implement clone syscall.

This bug was found by strace test suite.

Fixes 7f192e3cd316 ("fork: add clone3")

Good catch! Thank you Dmitry.

One change request below.

Quoted 23 lines
---
 arch/x86/ia32/sys_ia32.c | 1 +
 kernel/fork.c            | 1 +
 2 files changed, 2 insertions(+)
arch/x86/ia32/sys_ia32.c +1 -0
index 64a6c952091e..98754baf411a 100644
@@ -239,6 +239,7 @@ COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),
kernel/fork.c +0 -0
index 8f3e2d97d771..2c3cbad807b6 100644

kernel/fork.c long do_fork(unsigned long clone_flags, · line 2423

Quoted 7 lines
kernel/fork.c +1 -0
@@ -2417,6 +2417,7 @@ long do_fork(unsigned long clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),

Both of these legacy clone helpers need to make CLONE_PIDFD and CLONE_PARENT_SETTID incompatible, i.e. could you please add a helper to kernel/fork.c:

bool legacy_clone_args_valid(const struct kernel_clone_args *kargs)

{
	/* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */
	if ((kargs->flags & CLONE_PIDFD) && (kargs->flags & CLONE_PARENT_SETTID))
		return false;
}
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

and export it and use it in ia32 too?

copy_process already performs the check, isn't this enough?

No it doesn't anymore. clone3() allows CLONE_PIDFD + CLONE_PARENT_SETTID since struct clone_args has a dedicated pidfd return argument.

Indeed, I must've missed it, thanks.

OK, I'll send a new fix with legacy_clone_args_valid.

Signature · 1 line
ldv
#5 lore raw

PATCH v2 1/1 clone: fix CLONE_PIDFD support

The introduction of clone3 syscall accidentally broke CLONE_PIDFD support in traditional clone syscall on compat x86 and those architectures that use do_fork to implement clone syscall.

This bug was found by strace test suite.

Fixes 7f192e3cd316 ("fork: add clone3")
---
 arch/x86/ia32/sys_ia32.c   |  4 ++++
 include/linux/sched/task.h |  1 +
 kernel/fork.c              | 17 +++++++++++++++--
 3 files changed, 20 insertions(+), 2 deletions(-)
arch/x86/ia32/sys_ia32.c +4 -0
index 64a6c952091e..21790307121e 100644
@@ -239,6 +239,7 @@ COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),
@@ -246,5 +247,8 @@ COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
 		.tls		= tls_val,
 	};
 
+	if (!legacy_clone_args_valid(&args))
+		return -EINVAL;
+
 	return _do_fork(&args);
 }
include/linux/sched/task.h +1 -0
index 109a0df5af39..0497091e40c1 100644
@@ -89,6 +89,7 @@ extern void exit_files(struct task_struct *);
 extern void exit_itimers(struct signal_struct *);
 
 extern long _do_fork(struct kernel_clone_args *kargs);
+extern bool legacy_clone_args_valid(const struct kernel_clone_args *kargs);
 extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
 struct task_struct *fork_idle(int);
 struct mm_struct *copy_init_mm(void);
kernel/fork.c +15 -2
index 8f3e2d97d771..ef1e05a68827 100644
@@ -2406,6 +2406,16 @@ long _do_fork(struct kernel_clone_args *args)
 	return nr;
 }
 
+bool legacy_clone_args_valid(const struct kernel_clone_args *kargs)
+{
+	/* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */
+	if ((kargs->flags & CLONE_PIDFD) &&
+	    (kargs->flags & CLONE_PARENT_SETTID))
+		return false;
+
+	return true;
+}
+
 #ifndef CONFIG_HAVE_COPY_THREAD_TLS
 /* For compatibility with architectures that call do_fork directly rather than
  * using the syscall entry points below. */
@@ -2417,6 +2427,7 @@ long do_fork(unsigned long clone_flags,
 {
 	struct kernel_clone_args args = {
 		.flags		= (clone_flags & ~CSIGNAL),
+		.pidfd		= parent_tidptr,
 		.child_tid	= child_tidptr,
 		.parent_tid	= parent_tidptr,
 		.exit_signal	= (clone_flags & CSIGNAL),
@@ -2424,6 +2435,9 @@ long do_fork(unsigned long clone_flags,
 		.stack_size	= stack_size,
 	};
 
+	if (!legacy_clone_args_valid(&args))
+		return -EINVAL;
+
 	return _do_fork(&args);
 }
 #endif
@@ -2505,8 +2519,7 @@ SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
 		.tls		= tls,
 	};
 
-	/* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */
-	if ((clone_flags & CLONE_PIDFD) && (clone_flags & CLONE_PARENT_SETTID))
+	if (!legacy_clone_args_valid(&args))
 		return -EINVAL;
 
 	return _do_fork(&args);
Signature · 1 line
ldv
#6 lore raw
Quoted 10 lines · Dmitry V. Levin

The introduction of clone3 syscall accidentally broke CLONE_PIDFD support in traditional clone syscall on compat x86 and those architectures that use do_fork to implement clone syscall.

This bug was found by strace test suite.

Fixes 7f192e3cd316 ("fork: add clone3")

This looks good; moving this into my tree.

Thanks!
Christian