Skip to contentSkip to conversation

fork: extend clone3() to support CLONE_SET_TID

PATCH Superseded Adrian Reber 4 participants 13 messages Aug 6, 2019 – Aug 7, 2019

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 fork: extend clone3() to support CLONE_SET_TID

The main motivation to add set_tid to clone3() is CRIU.

To restore a process with the same PID/TID CRIU currently uses /proc/sys/kernel/ns_last_pid. It writes the desired (PID - 1) to ns_last_pid and then (quickly) does a clone(). This works most of the time, but it is racy. It is also slow as it requires multiple syscalls.

Extending clone3() to support set_tid makes it possible restore a process using CRIU without accessing /proc/sys/kernel/ns_last_pid and race free (as long as the desired PID/TID is available).

This clone3() extension places the same restrictions (CAP_SYS_ADMIN) on clone3() with set_tid as they are currently in place for ns_last_pid.

---
v2:
- Removed (size < sizeof(struct clone_args)) as discussed with Christian and Dmitry
- Added comment to ((set_tid != 1) && idr_get_cursor() <= 1) (Oleg)
- Use idr_alloc() instead of idr_alloc_cyclic() (Oleg)

v3:
- Return EEXIST if PID is already in use (Christian)
- Drop CLONE_SET_TID (Christian and Oleg)
- Use idr_is_empty() instead of idr_get_cursor() (Oleg)
- Handle different struct clone_args sizes (Dmitry)

---
 include/linux/pid.h        |  2 +-
 include/linux/sched/task.h |  1 +
 include/uapi/linux/sched.h |  1 +
 kernel/fork.c              | 18 ++++++++++++++++--
 kernel/pid.c               | 37 ++++++++++++++++++++++++++++++-------
 5 files changed, 49 insertions(+), 10 deletions(-)
include/linux/pid.h +1 -1
index 2a83e434db9d..052000db0ced 100644
@@ -116,7 +116,7 @@ extern struct pid *find_vpid(int nr);
 extern struct pid *find_get_pid(int nr);
 extern struct pid *find_ge_pid(int nr, struct pid_namespace *);
 
-extern struct pid *alloc_pid(struct pid_namespace *ns);
+extern struct pid *alloc_pid(struct pid_namespace *ns, pid_t set_tid);
 extern void free_pid(struct pid *pid);
 extern void disable_pid_allocation(struct pid_namespace *ns);
 
include/linux/sched/task.h +1 -0
index 0497091e40c1..4f2a80564332 100644
@@ -26,6 +26,7 @@ struct kernel_clone_args {
 	unsigned long stack;
 	unsigned long stack_size;
 	unsigned long tls;
+	pid_t set_tid;
 };
 
 /*
include/uapi/linux/sched.h +1 -0
index b3105ac1381a..e1ce103a2c47 100644
@@ -45,6 +45,7 @@ struct clone_args {
 	__aligned_u64 stack;
 	__aligned_u64 stack_size;
 	__aligned_u64 tls;
+	__aligned_u64 set_tid;
 };
 
 /*
kernel/fork.c +16 -2
index 2852d0e76ea3..4bb3972ebb99 100644
@@ -2031,7 +2031,7 @@ static __latent_entropy struct task_struct *copy_process(
 	stackleak_task_init(p);
 
 	if (pid != &init_struct_pid) {
-		pid = alloc_pid(p->nsproxy->pid_ns_for_children);
+		pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid);
 		if (IS_ERR(pid)) {
 			retval = PTR_ERR(pid);
 			goto bad_fork_cleanup_thread;
@@ -2530,12 +2530,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 					      struct clone_args __user *uargs,
 					      size_t size)
 {
+	struct pid_namespace *pid_ns = task_active_pid_ns(current);
 	struct clone_args args;
 
 	if (unlikely(size > PAGE_SIZE))
 		return -E2BIG;
 
-	if (unlikely(size < sizeof(struct clone_args)))
+	/* The struct needs to be at least the size of the original struct. */
+	if (size < (sizeof(struct clone_args) - sizeof(__aligned_u64)))
 		return -EINVAL;
 
 	if (unlikely(!access_ok(uargs, size)))
@@ -2573,6 +2575,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 		.tls		= args.tls,
 	};
 
+	if (size == sizeof(struct clone_args)) {
+		/* Only check permissions if set_tid is actually set. */
+		if (args.set_tid &&
+			!ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
+			return -EPERM;
+		kargs->set_tid = args.set_tid;
+	}
+
 	return 0;
 }
 
@@ -2585,6 +2595,10 @@ static bool clone3_args_valid(const struct kernel_clone_args *kargs)
 	if (kargs->flags & ~CLONE_LEGACY_FLAGS)
 		return false;
 
+	/* Fail if set_tid is invalid */
+	if (kargs->set_tid < 0)
+		return false;
+
 	/*
 	 * - make the CLONE_DETACHED bit reuseable for clone3
 	 * - make the CSIGNAL bits reuseable for clone3
kernel/pid.c +30 -7
index 0a9f2e437217..520b75c17790 100644
@@ -157,7 +157,7 @@ void free_pid(struct pid *pid)
 	call_rcu(&pid->rcu, delayed_put_pid);
 }
 
-struct pid *alloc_pid(struct pid_namespace *ns)
+struct pid *alloc_pid(struct pid_namespace *ns, int set_tid)
 {
 	struct pid *pid;
 	enum pid_type type;
@@ -186,12 +186,35 @@ struct pid *alloc_pid(struct pid_namespace *ns)
 		if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
 			pid_min = RESERVED_PIDS;
 
-		/*
-		 * Store a null pointer so find_pid_ns does not find
-		 * a partially initialized PID (see below).
-		 */
-		nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
-				      pid_max, GFP_ATOMIC);
+		if (set_tid) {
+			/*
+			 * Also fail if a PID != 1 is requested
+			 * and no PID 1 exists.
+			 */
+			if ((set_tid >= pid_max) || ((set_tid != 1) &&
+				(idr_is_empty(&tmp->idr)))) {
+				spin_unlock_irq(&pidmap_lock);
+				retval = -EINVAL;
+				goto out_free;
+			}
+			nr = idr_alloc(&tmp->idr, NULL, set_tid,
+				       set_tid + 1, GFP_ATOMIC);
+			/*
+			 * If ENOSPC is returned it means that the PID is
+			 * alreay in use. Return EEXIST in that case.
+			 */
+			if (nr == -ENOSPC)
+				nr = -EEXIST;
+			/* Only use set_tid for one PID namespace. */
+			set_tid = 0;
+		} else {
+			/*
+			 * Store a null pointer so find_pid_ns does not find
+			 * a partially initialized PID (see below).
+			 */
+			nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
+					      pid_max, GFP_ATOMIC);
+		}
 		spin_unlock_irq(&pidmap_lock);
 		idr_preload_end();
 
Signature · 1 line
2.21.0
#1 lore raw

PATCH v3 2/2 selftests: add tests for clone3()

This tests clone3() with and without set_tid to see if all desired PIDs are working as expected. The test tries to clone3() with a set_tid of -1, 1, pid_max, a PID which is already in use and an unused PID. The same tests are also running in PID namespace.

---
 tools/testing/selftests/clone3/.gitignore     |   2 +
 tools/testing/selftests/clone3/Makefile       |  11 ++
 tools/testing/selftests/clone3/clone3.c       | 141 +++++++++++++++
 .../testing/selftests/clone3/clone3_set_tid.c | 161 ++++++++++++++++++
 4 files changed, 315 insertions(+)
 create mode 100644 tools/testing/selftests/clone3/.gitignore
 create mode 100644 tools/testing/selftests/clone3/Makefile
 create mode 100644 tools/testing/selftests/clone3/clone3.c
 create mode 100644 tools/testing/selftests/clone3/clone3_set_tid.c
tools/testing/selftests/clone3/.gitignore new file +2 -0
new file mode 100644
index 000000000000..c63c64a78ddf
@@ -0,0 +1,2 @@
+clone3_set_tid
+clone3
tools/testing/selftests/clone3/Makefile new file +11 -0
new file mode 100644
index 000000000000..4efcf45b995b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0
+uname_M := $(shell uname -m 2>/dev/null || echo not)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/i386/)
+
+CFLAGS += -I../../../../usr/include/
+
+ifeq ($(ARCH),x86_64)
+	TEST_GEN_PROGS := clone3 clone3_set_tid
+endif
+
+include ../lib.mk
tools/testing/selftests/clone3/clone3.c new file +141 -0

141 lines not on this page · raw message

tools/testing/selftests/clone3/clone3_set_tid.c new file +161 -0

161 lines not on this page · raw message

Signature · 1 line
2.21.0
#2 lore raw
In reply to Adrian Reber
Quoted 12 lines · Adrian Reber
kernel/pid.c +1 -3
+struct pid *alloc_pid(struct pid_namespace *ns, int set_tid)
 {
 	struct pid *pid;
 	enum pid_type type;
@@ -186,12 +186,35 @@ struct pid *alloc_pid(struct pid_namespace *ns)
 		if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
 			pid_min = RESERVED_PIDS;
 
-		/*
-		 * Store a null pointer so find_pid_ns does not find
-		 * a partially initialized PID (see below).

kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 195

Quoted 10 lines
kernel/pid.c +7 -3
-		 */
-		nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
-				      pid_max, GFP_ATOMIC);
+		if (set_tid) {
+			/*
+			 * Also fail if a PID != 1 is requested
+			 * and no PID 1 exists.
+			 */
+			if ((set_tid >= pid_max) || ((set_tid != 1) &&
+				(idr_is_empty(&tmp->idr)))) {

too many parentheses ;) this is purely cosmetic, up to you, but to me

			if (set_tid >= pid_max ||
			   (set_tid != 1 && idr_is_empty(&tmp->idr))) {

looks a bit more readable.

kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 198

Quoted 3 lines
kernel/pid.c +3 -0
+				spin_unlock_irq(&pidmap_lock);
+				retval = -EINVAL;
+				goto out_free;

This doesn't look right, you need idr_preload_end() before goto out_free.

But I'd suggest to simply do

			nr = -EINVAL;
			if (set_tid < pid_max &&
			   (set_tid != 1 || idr_is_empty(&tmp->idr)))
				nr = idr_alloc(&tmp->idr, NULL, set_tid,
						set_tid + 1, GFP_ATOMIC);

			...

this is more robust.

Oleg.

#3 lore raw
Quoted 48 lines · Oleg Nesterov
Quoted 12 lines · Adrian Reber
kernel/pid.c +1 -3
+struct pid *alloc_pid(struct pid_namespace *ns, int set_tid)
 {
 	struct pid *pid;
 	enum pid_type type;
@@ -186,12 +186,35 @@ struct pid *alloc_pid(struct pid_namespace *ns)
 		if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
 			pid_min = RESERVED_PIDS;
 
-		/*
-		 * Store a null pointer so find_pid_ns does not find
-		 * a partially initialized PID (see below).

kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 195

Quoted 10 lines
kernel/pid.c +7 -3
-		 */
-		nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
-				      pid_max, GFP_ATOMIC);
+		if (set_tid) {
+			/*
+			 * Also fail if a PID != 1 is requested
+			 * and no PID 1 exists.
+			 */
+			if ((set_tid >= pid_max) || ((set_tid != 1) &&
+				(idr_is_empty(&tmp->idr)))) {

too many parentheses ;) this is purely cosmetic, up to you, but to me

			if (set_tid >= pid_max ||
			   (set_tid != 1 && idr_is_empty(&tmp->idr))) {

looks a bit more readable.

kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 198

Quoted 3 lines
kernel/pid.c +3 -0
+				spin_unlock_irq(&pidmap_lock);
+				retval = -EINVAL;
+				goto out_free;

This doesn't look right, you need idr_preload_end() before goto out_free.

But I'd suggest to simply do

			nr = -EINVAL;
			if (set_tid < pid_max &&
			   (set_tid != 1 || idr_is_empty(&tmp->idr)))
				nr = idr_alloc(&tmp->idr, NULL, set_tid,
						set_tid + 1, GFP_ATOMIC);

			...
Quoted 1 line

this is more robust.

That all sounds reasonable to me.

#4 lore raw
In reply to Adrian Reber
Quoted 5 lines · Adrian Reber
kernel/fork.c +0 -0
@@ -2530,12 +2530,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 					      struct clone_args __user *uargs,
 					      size_t size)
 {

kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2541

Quoted 10 lines
kernel/fork.c +3 -1
+	struct pid_namespace *pid_ns = task_active_pid_ns(current);
 	struct clone_args args;
 
 	if (unlikely(size > PAGE_SIZE))
 		return -E2BIG;
 
-	if (unlikely(size < sizeof(struct clone_args)))
+	/* The struct needs to be at least the size of the original struct. */
+	if (size < (sizeof(struct clone_args) - sizeof(__aligned_u64)))
 		return -EINVAL;

slightly off-topic, but with or without this patch I do not understand -EINVAL. Can't we replace this check with

	if (size < sizeof(struct clone_args))
		memset((void*)&args + size, sizeof(struct clone_args) - size, 0);

?

this way we can new members at the end of clone_args and this matches the "if (size > sizeof(struct clone_args))" block below which promises that whatever we add into clone_args a zero value should work.

And if we do this

kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2584

Quoted 7 lines
kernel/fork.c +7 -0
+	if (size == sizeof(struct clone_args)) {
+		/* Only check permissions if set_tid is actually set. */
+		if (args.set_tid &&
+			!ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
+			return -EPERM;
+		kargs->set_tid = args.set_tid;
+	}

we can move this check into clone3_args_valid() or even copy_process()

	if (kargs->set_tid) {
		if (!ns_capable(...))
			return -EPERM;
	}

Either way,

kernel/fork.c static bool clone3_args_valid(const struct kernel_clone_args *kargs) · line 2600

Quoted 7 lines
kernel/fork.c +3 -0
@@ -2585,6 +2595,10 @@ static bool clone3_args_valid(const struct kernel_clone_args *kargs)
 	if (kargs->flags & ~CLONE_LEGACY_FLAGS)
 		return false;
 
+	/* Fail if set_tid is invalid */
+	if (kargs->set_tid < 0)
+		return false;

I think it would be more clean to do this along with ns_capable() check, or along with the "set_tid >= pid_max" check in alloc_pid().

I won't insist, but I do not really like the fact we check set_tid 3 times in copy_clone_args_from_user(), clone3_args_valid(), and alloc_pid().

Oleg.

#5 lore raw
Quoted 20 lines · Oleg Nesterov
Quoted 5 lines · Adrian Reber
kernel/fork.c +0 -0
@@ -2530,12 +2530,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 					      struct clone_args __user *uargs,
 					      size_t size)
 {

kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2541

Quoted 10 lines
kernel/fork.c +3 -1
+	struct pid_namespace *pid_ns = task_active_pid_ns(current);
 	struct clone_args args;
 
 	if (unlikely(size > PAGE_SIZE))
 		return -E2BIG;
 
-	if (unlikely(size < sizeof(struct clone_args)))
+	/* The struct needs to be at least the size of the original struct. */
+	if (size < (sizeof(struct clone_args) - sizeof(__aligned_u64)))
 		return -EINVAL;

slightly off-topic, but with or without this patch I do not understand -EINVAL. Can't we replace this check with

Quoted 8 lines
	if (size < sizeof(struct clone_args))
		memset((void*)&args + size, sizeof(struct clone_args) - size, 0);

?

this way we can new members at the end of clone_args and this matches the "if (size > sizeof(struct clone_args))" block below which promises that whatever we add into clone_args a zero value should work.

What if the size is lesser than offsetof(struct clone_args, stack_size)? Probably, there should be still a check that it's not lesser than what's the required minimum..

Also note, that (kargs) and (args) are a bit different beasts in this context..
kargs lies on the stack and might want to be with zero-initializer
: struct kernel_clone_args kargs = {};

Signature · 1 line
          Dmitry
#6 lore raw
Quoted 27 lines · Dmitry Safonov
Quoted 25 lines · Oleg Nesterov
Quoted 5 lines · Adrian Reber
kernel/fork.c +0 -0
@@ -2530,12 +2530,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 					      struct clone_args __user *uargs,
 					      size_t size)
 {

kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2541

Quoted 10 lines
kernel/fork.c +3 -1
+	struct pid_namespace *pid_ns = task_active_pid_ns(current);
 	struct clone_args args;
 
 	if (unlikely(size > PAGE_SIZE))
 		return -E2BIG;
 
-	if (unlikely(size < sizeof(struct clone_args)))
+	/* The struct needs to be at least the size of the original struct. */
+	if (size < (sizeof(struct clone_args) - sizeof(__aligned_u64)))
 		return -EINVAL;

slightly off-topic, but with or without this patch I do not understand -EINVAL. Can't we replace this check with

	if (size < sizeof(struct clone_args))
		memset((void*)&args + size, sizeof(struct clone_args) - size, 0);

?

Quoted 7 lines
Quoted 3 lines

this way we can new members at the end of clone_args and this matches the "if (size > sizeof(struct clone_args))" block below which promises that whatever we add into clone_args a zero value should work.

What if the size is lesser than offsetof(struct clone_args, stack_size)? Probably, there should be still a check that it's not lesser than what's the required minimum..

Not sure I understand... I mean, this doesn't differ from the case when size == sizeof(clone_args) but uargs->stack == NULL ?

Quoted 4 lines

Also note, that (kargs) and (args) are a bit different beasts in this context..
kargs lies on the stack and might want to be with zero-initializer
: struct kernel_clone_args kargs = {};

I don't think so. Lets consider this patch which adds the new set_tid into clone_args and kernel_clone_args. copy_clone_args_from_user() does

	*kargs = (struct kernel_clone_args){
		.flags		= args.flags,
		.pidfd		= u64_to_user_ptr(args.pidfd),
		.child_tid	= u64_to_user_ptr(args.child_tid),
		.parent_tid	= u64_to_user_ptr(args.parent_tid),
		.exit_signal	= args.exit_signal,
		.stack		= args.stack,
		.stack_size	= args.stack_size,
		.tls		= args.tls,
	};

so this patch should simply add

		.set_tid	= args.set_tid;

at the end. No?

Oleg.

#7 lore raw
Quoted 1 line · Oleg Nesterov

On 08/07, Dmitry Safonov wrote:

[..]

Quoted 6 lines
Quoted 3 lines

What if the size is lesser than offsetof(struct clone_args, stack_size)? Probably, there should be still a check that it's not lesser than what's the required minimum..

Not sure I understand... I mean, this doesn't differ from the case when size == sizeof(clone_args) but uargs->stack == NULL ?

I might be mistaken and I confess that I don't fully understand the code, but wouldn't it mystically fail in copy_thread_tls() with -ENOMEM instead of -EINVAL?
Maybe not a huge difference, but..

Quoted 19 lines
Quoted 4 lines

Also note, that (kargs) and (args) are a bit different beasts in this context..
kargs lies on the stack and might want to be with zero-initializer
: struct kernel_clone_args kargs = {};

I don't think so. Lets consider this patch which adds the new set_tid into clone_args and kernel_clone_args. copy_clone_args_from_user() does

	*kargs = (struct kernel_clone_args){
		.flags		= args.flags,
		.pidfd		= u64_to_user_ptr(args.pidfd),
		.child_tid	= u64_to_user_ptr(args.child_tid),
		.parent_tid	= u64_to_user_ptr(args.parent_tid),
		.exit_signal	= args.exit_signal,
		.stack		= args.stack,
		.stack_size	= args.stack_size,
		.tls		= args.tls,
	};
Quoted 5 lines

so this patch should simply add

		.set_tid	= args.set_tid;

at the end. No?

Agree, this may be better.

Signature · 1 line
          Dmitry
#8 lore raw
Quoted 7 lines · Dmitry Safonov
Quoted 1 line · Oleg Nesterov

On 08/07, Dmitry Safonov wrote:

[..]

Quoted 4 lines
Quoted 3 lines

What if the size is lesser than offsetof(struct clone_args, stack_size)? Probably, there should be still a check that it's not lesser than what's the required minimum..

Quoted 7 lines
Quoted 2 lines

Not sure I understand... I mean, this doesn't differ from the case when size == sizeof(clone_args) but uargs->stack == NULL ?

I might be mistaken and I confess that I don't fully understand the code, but wouldn't it mystically fail in copy_thread_tls() with -ENOMEM instead of -EINVAL?
Maybe not a huge difference, but..

Actually, not there. I've just tried clone3() with stack_size == 0, it sets it a proper size somewhere on the way..
So, apologies for the misinformation - it seems that we definitely could just memset() the missing fields.

Thanks,
Dmitry

#9 lore raw
In reply to Oleg Nesterov
Quoted 20 lines · Oleg Nesterov
Quoted 5 lines · Adrian Reber
kernel/fork.c +0 -0
@@ -2530,12 +2530,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 					      struct clone_args __user *uargs,
 					      size_t size)
 {

kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2541

Quoted 10 lines
kernel/fork.c +3 -1
+	struct pid_namespace *pid_ns = task_active_pid_ns(current);
 	struct clone_args args;
 
 	if (unlikely(size > PAGE_SIZE))
 		return -E2BIG;
 
-	if (unlikely(size < sizeof(struct clone_args)))
+	/* The struct needs to be at least the size of the original struct. */
+	if (size < (sizeof(struct clone_args) - sizeof(__aligned_u64)))
 		return -EINVAL;

slightly off-topic, but with or without this patch I do not understand -EINVAL. Can't we replace this check with

Quoted 8 lines
	if (size < sizeof(struct clone_args))
		memset((void*)&args + size, sizeof(struct clone_args) - size, 0);

?

this way we can new members at the end of clone_args and this matches the "if (size > sizeof(struct clone_args))" block below which promises that whatever we add into clone_args a zero value should work.

Hm, I actually think we should define:

#define CLONE3_ARGS_SIZE_V0 64
#define CLONE3_ARGS_SIZE_V1 ...
and then later on for future extensions
#define CLONE3_ARGS_SIZE_V2 ...

then do
if (size < CLONE3_ARGS_SIZE_V0)

	return -EINVAL;

then do what you suggested:

if (size < sizeof(struct clone_args))

	memset((void*)&args + size, sizeof(struct clone_args) - size, 0);
Quoted 33 lines

And if we do this

kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2584

Quoted 7 lines
kernel/fork.c +7 -0
+	if (size == sizeof(struct clone_args)) {
+		/* Only check permissions if set_tid is actually set. */
+		if (args.set_tid &&
+			!ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
+			return -EPERM;
+		kargs->set_tid = args.set_tid;
+	}

we can move this check into clone3_args_valid() or even copy_process()

	if (kargs->set_tid) {
		if (!ns_capable(...))
			return -EPERM;
	}

Either way,

kernel/fork.c static bool clone3_args_valid(const struct kernel_clone_args *kargs) · line 2600

Quoted 7 lines
kernel/fork.c +3 -0
@@ -2585,6 +2595,10 @@ static bool clone3_args_valid(const struct kernel_clone_args *kargs)
 	if (kargs->flags & ~CLONE_LEGACY_FLAGS)
 		return false;
 
+	/* Fail if set_tid is invalid */
+	if (kargs->set_tid < 0)
+		return false;

I think it would be more clean to do this along with ns_capable() check, or along with the "set_tid >= pid_max" check in alloc_pid().

Quoted 2 lines

I won't insist, but I do not really like the fact we check set_tid 3 times in copy_clone_args_from_user(), clone3_args_valid(), and alloc_pid().

Agreed on that part.

#10 lore raw
In reply to Adrian Reber

kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2581

Quoted 9 lines · Adrian Reber
kernel/fork.c +4 -0
@@ -2573,6 +2575,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 		.tls		= args.tls,
 	};
 
+	if (size == sizeof(struct clone_args)) {
+		/* Only check permissions if set_tid is actually set. */
+		if (args.set_tid &&
+			!ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))

and I just noticed this uses pid_ns = task_active_pid_ns() ...

is it correct?

I feel I am totally confused, but should we use the same p->nsproxy->pid_ns_for_children passed to alloc_pid?

Oleg.

#11 lore raw
Quoted 13 lines · Oleg Nesterov

kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2581

Quoted 9 lines · Adrian Reber
kernel/fork.c +4 -0
@@ -2573,6 +2575,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 		.tls		= args.tls,
 	};
 
+	if (size == sizeof(struct clone_args)) {
+		/* Only check permissions if set_tid is actually set. */
+		if (args.set_tid &&
+			!ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))

and I just noticed this uses pid_ns = task_active_pid_ns() ...

Quoted 4 lines

is it correct?

I feel I am totally confused, but should we use the same p->nsproxy->pid_ns_for_children passed to alloc_pid?

We need to have CAP_SYS_ADMIN in the owning user namespace of the target pidns for the pidns in which we spawn the new process. The value for pid_ns_for_children could've been altered by either passing CLONE_NEWPID or by having called unshare(CLONE_NEWPID) before. So yes, pid_ns_for_children is what we want.

Sorry again for the delay in my responses. On vacation atm.

Christian

#12 lore raw
In reply to Adrian Reber
Quoted 7 lines · Adrian Reber

The main motivation to add set_tid to clone3() is CRIU.

To restore a process with the same PID/TID CRIU currently uses /proc/sys/kernel/ns_last_pid. It writes the desired (PID - 1) to ns_last_pid and then (quickly) does a clone(). This works most of the time, but it is racy. It is also slow as it requires multiple syscalls.

Quoted 8 lines

Extending clone3() to support set_tid makes it possible restore a process using CRIU without accessing /proc/sys/kernel/ns_last_pid and race free (as long as the desired PID/TID is available).

This clone3() extension places the same restrictions (CAP_SYS_ADMIN) on clone3() with set_tid as they are currently in place for ns_last_pid.

I'm currently on vacation until 12 August so I'm going to be a little slow in responding. Sorry about that!

Christian