Skip to content

Commit

Permalink
clone: fix CLONE_PIDFD support
Browse files Browse the repository at this point in the history
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.

Link: https://strace.io/logs/strace/2019-07-12
Fixes: 7f192e3 ("fork: add clone3")
Bisected-and-tested-by: Anatoly Pugachev <matorola@gmail.com>
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Link: https://lore.kernel.org/r/20190714162047.GB10389@altlinux.org
Signed-off-by: Christian Brauner <christian@brauner.io>
  • Loading branch information
ldv-alt authored and brauner committed Jul 14, 2019
1 parent 964a4ea commit 028b6e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions arch/x86/ia32/sys_ia32.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,16 @@ 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),
.stack = newsp,
.tls = tls_val,
};

if (!legacy_clone_args_valid(&args))
return -EINVAL;

return _do_fork(&args);
}
1 change: 1 addition & 0 deletions include/linux/sched/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions kernel/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -2417,13 +2427,17 @@ 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),
.stack = stack_start,
.stack_size = stack_size,
};

if (!legacy_clone_args_valid(&args))
return -EINVAL;

return _do_fork(&args);
}
#endif
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 028b6e8

Please sign in to comment.