fork: extend clone3() to support CLONE_SET_TID
Subsystems (5)
PATCH v2 1/2 fork: extend clone3() to support CLONE_SET_TID
The main motivation to add CLONE_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 CLONE_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)
--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 2 ++ kernel/fork.c | 25 ++++++++++++++++--------- kernel/pid.c | 30 +++++++++++++++++++++++------- 5 files changed, 43 insertions(+), 17 deletions(-)
include/linux/pid.h +1 -1
@@ -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
@@ -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 +2 -0
@@ -32,6 +32,7 @@#define CLONE_NEWPID 0x20000000/* New pid namespace */ #define CLONE_NEWNET 0x40000000/* New network namespace */ #define CLONE_IO 0x80000000/* Clone io context */ +#define CLONE_SET_TID 0x100000000ULL/* set if the desired TID is set in set_tid */ /* * Arguments for the clone3 syscall
@@ -45,6 +46,7 @@ struct clone_args {__aligned_u64 stack;__aligned_u64 stack_size;__aligned_u64 tls; +__aligned_u64 set_tid; };/*
kernel/fork.c +16 -9
@@ -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,14 +2530,12 @@ 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 ))) -return -EINVAL ; -if (unlikely (!access_ok (uargs, size)))return -EFAULT ;
@@ -2562,6 +2560,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,if (copy_from_user (&args, uargs, size))return -EFAULT ; +if ((args.flags &CLONE_SET_TID ) && !ns_capable (pid_ns->user_ns,CAP_SYS_ADMIN )) +return -EPERM ; + *kargs = (struct kernel_clone_args ){ .flags = args.flags, .pidfd =u64_to_user_ptr (args.pidfd),
@@ -2571,6 +2572,7 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .stack = args.stack, .stack_size = args.stack_size, .tls = args.tls, + .set_tid = args.set_tid, };return 0 ;
@@ -2578,11 +2580,16 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,static bool clone3_args_valid (const struct kernel_clone_args *kargs) { -/* -* All lower bits of the flag word are taken. -* Verify that no other unknown flags are passed along. -*/ -if (kargs->flags & ~CLONE_LEGACY_FLAGS ) +/* Verify that no other unknown flags are passed along. */ +if (kargs->flags & ~(CLONE_LEGACY_FLAGS |CLONE_SET_TID )) +return false; + +/* Fail if set_tid is set without CLONE_SET_TID */ +if (kargs->set_tid && !(kargs->flags &CLONE_SET_TID )) +return false; + +/* Also fail if set_tid is invalid */ +if ((kargs->set_tid <=0 ) && (kargs->flags &CLONE_SET_TID ))return false;/*
kernel/pid.c +23 -7
@@ -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,28 @@ 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_get_cursor (&tmp->idr) <=1 ))) { +spin_unlock_irq (&pidmap_lock); + retval = -EINVAL ; + goto out_free; + } + nr =idr_alloc (&tmp->idr,NULL , set_tid, + set_tid +1 ,GFP_ATOMIC ); + 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
PATCH v2 2/2 selftests: add test for clone3() with set_tid
This tests clone3() with 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 | 1 + tools/testing/selftests/clone3/Makefile | 11 ++ .../testing/selftests/clone3/clone3_set_tid.c | 148 ++++++++++++++++++ 3 files changed, 160 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_set_tid.c
tools/testing/selftests/clone3/.gitignore new file +1 -0
@@ -0,0 +1 @@ +clone3_set_tid
tools/testing/selftests/clone3/Makefile new file +11 -0
@@ -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_set_tid +endif + +include ../lib.mk
tools/testing/selftests/clone3/clone3_set_tid.c new file +148 -0
148 lines not on this page · raw message
Signature · 1 line
2.21.0
Hi Adrian,
On 7/31/19 5:12 PM, Adrian Reber wrote:
[..]
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2540
Quoted 13 lines
kernel/fork.c +1
-3@@ -2530,14 +2530,12 @@ 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 ))) -return -EINVAL ; -
It might be better to validate it still somehow, but I don't insist.
[..]
Quoted 10 lines
kernel/fork.c +1
-5@@ -2578,11 +2580,16 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,static bool clone3_args_valid (const struct kernel_clone_args *kargs) { -/* -* All lower bits of the flag word are taken. -* Verify that no other unknown flags are passed along. -*/ -if (kargs->flags & ~CLONE_LEGACY_FLAGS ) +/* Verify that no other unknown flags are passed along. */
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2593
Quoted 10 lines
kernel/fork.c +9
-0+if (kargs->flags & ~(CLONE_LEGACY_FLAGS |CLONE_SET_TID )) +return false; + +/* Fail if set_tid is set without CLONE_SET_TID */ +if (kargs->set_tid && !(kargs->flags &CLONE_SET_TID )) +return false; + +/* Also fail if set_tid is invalid */ +if ((kargs->set_tid <=0 ) && (kargs->flags &CLONE_SET_TID ))return false;
Sorry for not mentioning it on v1, but I've noticed it only now:
you check kargs->set_tid even with the legacy-sized kernel_clone_args, which is probably some random value on a task's stack?
[..]
Thanks much,
Dmitry
Quoted 37 lines · Dmitry Safonov
Hi Adrian,
On 7/31/19 5:12 PM, Adrian Reber wrote:
[..]kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2540
Quoted 13 lines
kernel/fork.c +1
-3@@ -2530,14 +2530,12 @@ 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 ))) -return -EINVAL ; -It might be better to validate it still somehow, but I don't insist.
[..]
Quoted 16 lines
kernel/fork.c +7
-5@@ -2578,11 +2580,16 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,static bool clone3_args_valid (const struct kernel_clone_args *kargs) { -/* -* All lower bits of the flag word are taken. -* Verify that no other unknown flags are passed along. -*/ -if (kargs->flags & ~CLONE_LEGACY_FLAGS ) +/* Verify that no other unknown flags are passed along. */ +if (kargs->flags & ~(CLONE_LEGACY_FLAGS |CLONE_SET_TID )) +return false; + +/* Fail if set_tid is set without CLONE_SET_TID */ +if (kargs->set_tid && !(kargs->flags &CLONE_SET_TID )) +return false;
Quoted 8 lines
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2593
Quoted 4 lines
kernel/fork.c +3
-0+ +/* Also fail if set_tid is invalid */ +if ((kargs->set_tid <=0 ) && (kargs->flags &CLONE_SET_TID ))return false;Sorry for not mentioning it on v1, but I've noticed it only now:
you checkkargs->set_tideven with the legacy-sizedkernel_clone_args, which is probably some random value on a task's stack?
Self-correction: On kernel stack in copy_clone_args_from_user(). Which may probably be considered as a security leak.. Sorry again for not spotting it in v1.
Thanks,
Dmitry
Quoted 4 lines · Adrian Reber
Extending
clone3()to supportCLONE_SET_TIDmakes it possible restore a process using CRIU without accessing/proc/sys/kernel/ns_last_pidand race free (as long as the desired PID/TID is available).
I personally like this... but please see the question below.
Quoted 11 lines
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,28 @@ 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_get_cursor (&tmp->idr) <=1 )))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Ah, I forgot to mention... this should work but only because RESERVED_PIDS > 0. How about idr_is_empty() ?
But the main question is how it can really help if ns->level > 0, unlikely CRIU will ever need to clone the process with the same pid_nr == set_tid in the ns->parent chain.
So may be kernel_clone_args->set_tid should be pid_t __user *set_tid_array? Or I missed something ?
Oleg.
Quoted 31 lines · Oleg Nesterov
Quoted 4 lines · Adrian Reber
Extending
clone3()to supportCLONE_SET_TIDmakes it possible restore a process using CRIU without accessing/proc/sys/kernel/ns_last_pidand race free (as long as the desired PID/TID is available).I personally like this... but please see the question below.
Quoted 11 lines
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,28 @@ 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_get_cursor (&tmp->idr) <=1 )))^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 7 lines
Ah, I forgot to mention... this should work but only because
RESERVED_PIDS> 0. How aboutidr_is_empty()?But the main question is how it can really help if
ns->level> 0, unlikely CRIU will ever need to clone the process with the samepid_nr==set_tidin thens->parentchain.
Not sure I understand what you mean. For CRIU only the PID in the PID namespace is relevant.
Quoted 2 lines
So may be
kernel_clone_args->set_tidshould bepid_t__user*set_tid_array? Or I missed something ?
Not sure why and how an array would be needed. Could you give me some more details why you think this is needed.
Adrian
Quoted 8 lines · Adrian Reber
Quoted 3 lines · Oleg Nesterov
But the main question is how it can really help if
ns->level> 0, unlikely CRIU will ever need to clone the process with the samepid_nr==set_tidin thens->parentchain.Not sure I understand what you mean. For CRIU only the PID in the PID namespace is relevant.
If it runs "inside" this namespace. But in this case alloc_pid() should use nr == set_tid only once in the main loop, when i == ns->level.
It doesn't need to have the same pid_nr in the parent pid namespace.
And in fact we should not allow criu (or anything else) to control the child's pid_nr in the parent(s) namespace.
Right?
Quoted 5 lines
Quoted 2 lines
So may be
kernel_clone_args->set_tidshould bepid_t__user*set_tid_array? Or I missed something ?Not sure why and how an array would be needed. Could you give me some more details why you think this is needed.
IIURC, criu can restore the process tree along with nested pid namespaces.
how can this patch help in this case?
But again, perhaps I missed something. I forgot everything about criu.
Oleg.
Christian Brauner
maintainerQuoted 10 lines · Oleg Nesterov
Quoted 8 lines · Adrian Reber
Quoted 3 lines · Oleg Nesterov
But the main question is how it can really help if
ns->level> 0, unlikely CRIU will ever need to clone the process with the samepid_nr==set_tidin thens->parentchain.Not sure I understand what you mean. For CRIU only the PID in the PID namespace is relevant.
Quoted 7 lines
If it runs "inside" this namespace. But in this case
alloc_pid()should use nr ==set_tidonly once in the main loop, when i ==ns->level.It doesn't need to have the same
pid_nrin the parent pid namespace.And in fact we should not allow criu (or anything else) to control the child's
pid_nrin the parent(s) namespace.
This should definitely not be possible!
Quoted 10 lines
Right?
Quoted 5 lines
Quoted 2 lines
So may be
kernel_clone_args->set_tidshould bepid_t__user*set_tid_array? Or I missed something ?Not sure why and how an array would be needed. Could you give me some more details why you think this is needed.
IIURC, criu can restore the process tree along with nested pid namespaces.
Hm, I'm not a fan of this array approach...
Quoted 7 lines
how can this patch help in this case?
But again, perhaps I missed something. I forgot everything about criu.
Oleg.
Quoted 8 lines · Oleg Nesterov
Quoted 6 lines · Adrian Reber
Quoted 3 lines · Oleg Nesterov
But the main question is how it can really help if
ns->level> 0, unlikely CRIU will ever need to clone the process with the samepid_nr==set_tidin thens->parentchain.
Quoted 5 lines
Quoted 2 lines
Not sure I understand what you mean. For CRIU only the PID in the PID namespace is relevant.
If it runs "inside" this namespace. But in this case
alloc_pid()should use nr ==set_tidonly once in the main loop, when i ==ns->level.
and I just noticed that your patch does exactly this, it clears set_tid after the 1st usage when i == ns->level.
Quoted 11 lines
Quoted 5 lines
Quoted 2 lines
So may be
kernel_clone_args->set_tidshould bepid_t__user*set_tid_array? Or I missed something ?Not sure why and how an array would be needed. Could you give me some more details why you think this is needed.
IIURC, criu can restore the process tree along with nested pid namespaces.
how can this patch help in this case?
But again, perhaps I missed something. I forgot everything about criu.
Yes... I guess in this case it can "safely" modify ns_last_pid in the child namespaces it needs to create.
So Adrian, sorry for confusion, I think your patch is fine.
Oleg.
Quoted 2 lines · Oleg Nesterov
So Adrian, sorry for confusion, I think your patch is fine.
Yes... but do we really need the new CLONE_SET_TID ?
set_tid == 0 has no effect, can't we simply check kargs->set_tid != 0 before ns_capable() ?
and to remind, I still think alloc_pid() should use idr_is_empty(), but I won't insist.
Oleg.
Christian Brauner
maintainerQuoted 8 lines · Oleg Nesterov
Quoted 2 lines · Oleg Nesterov
So Adrian, sorry for confusion, I think your patch is fine.
Yes... but do we really need the new
CLONE_SET_TID?
set_tid== 0 has no effect, can't we simply checkkargs->set_tid!= 0 beforens_capable()?
Yeah, I agree that sounds much better and aligns with exit_signal.
Christian
Quoted 4 lines · Christian Brauner
Quoted 3 lines · Oleg Nesterov
Quoted 2 lines · Oleg Nesterov
So Adrian, sorry for confusion, I think your patch is fine.
Good to know.
Quoted 6 lines
Quoted 4 lines
Yes... but do we really need the new
CLONE_SET_TID?
set_tid== 0 has no effect, can't we simply checkkargs->set_tid!= 0 beforens_capable()?Yeah, I agree that sounds much better and aligns with
exit_signal.
Let me remove CLONE_SET_TID from the patch and I will try out idr_is_empty().
I will also address Dmitry's comment about accessing smaller parameter structs.
Adrian
Christian Brauner
maintainerQuoted 6 lines · Adrian Reber
The main motivation to add
CLONE_SET_TIDtoclone3()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) tons_last_pidand then (quickly) does aclone(). This works most of the time, but it is racy. It is also slow as it requires multiple syscalls.
Can you elaborate how this is racy, please. Afaict, CRIU will always usually restore in a new pid namespace that it controls, right? What is the exact race?
Quoted 5 lines
Extending
clone3()to supportCLONE_SET_TIDmakes it possible restore a process using CRIU without accessing/proc/sys/kernel/ns_last_pidand race free (as long as the desired PID/TID is available).
Quoted 8 lines
This
clone3()extension places the same restrictions (CAP_SYS_ADMIN) onclone3()withset_tidas they are currently in place forns_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)
- Useidr_alloc()instead ofidr_alloc_cyclic()(Oleg)
Fwiw, the changelog should be placed after the "---" after your SOB, so rather :):
---
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)
---
Quoted 85 lines
Signed-off-by Adrian Reber <[email protected]>--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 2 ++ kernel/fork.c | 25 ++++++++++++++++--------- kernel/pid.c | 30 +++++++++++++++++++++++------- 5 files changed, 43 insertions(+), 17 deletions(-)include/linux/pid.h +1
-1@@ -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@@ -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 +2
-0@@ -32,6 +32,7 @@#define CLONE_NEWPID 0x20000000/* New pid namespace */ #define CLONE_NEWNET 0x40000000/* New network namespace */ #define CLONE_IO 0x80000000/* Clone io context */ +#define CLONE_SET_TID 0x100000000ULL/* set if the desired TID is set in set_tid */ /* * Arguments for the clone3 syscall @@ -45,6 +46,7 @@ struct clone_args {__aligned_u64 stack;__aligned_u64 stack_size;__aligned_u64 tls; +__aligned_u64 set_tid; };/* kernel/fork.c +2
-4@@ -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,14 +2530,12 @@ 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 ))) -return -EINVAL ; -if (unlikely (!access_ok (uargs, size)))return -EFAULT ;
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2564
Quoted 5 lines
kernel/fork.c +2
-0@@ -2562,6 +2560,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,if (copy_from_user (&args, uargs, size))return -EFAULT ; +if ((args.flags &CLONE_SET_TID ) && !ns_capable (pid_ns->user_ns,CAP_SYS_ADMIN )) +return -EPERM ;
Have you made sure that this makes sense with all flags, e.g. does this make sense when specified with CLONE_THREAD?
Quoted 60 lines
kernel/fork.c +12
-5+ *kargs = (struct kernel_clone_args ){ .flags = args.flags, .pidfd =u64_to_user_ptr (args.pidfd),@@ -2571,6 +2572,7 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .stack = args.stack, .stack_size = args.stack_size, .tls = args.tls, + .set_tid = args.set_tid, };return 0 ;@@ -2578,11 +2580,16 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,static bool clone3_args_valid (const struct kernel_clone_args *kargs) { -/* -* All lower bits of the flag word are taken. -* Verify that no other unknown flags are passed along. -*/ -if (kargs->flags & ~CLONE_LEGACY_FLAGS ) +/* Verify that no other unknown flags are passed along. */ +if (kargs->flags & ~(CLONE_LEGACY_FLAGS |CLONE_SET_TID )) +return false; + +/* Fail if set_tid is set without CLONE_SET_TID */ +if (kargs->set_tid && !(kargs->flags &CLONE_SET_TID )) +return false; + +/* Also fail if set_tid is invalid */ +if ((kargs->set_tid <=0 ) && (kargs->flags &CLONE_SET_TID ))return false;/* kernel/pid.c +4
-7@@ -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,28 @@ 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
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 201
Quoted 10 lines
kernel/pid.c +10
-0+* and no PID 1 exists. +*/ +if ((set_tid >= pid_max) || ((set_tid !=1 ) && + (idr_get_cursor (&tmp->idr) <=1 ))) { +spin_unlock_irq (&pidmap_lock); + retval = -EINVAL ; + goto out_free; + } + nr =idr_alloc (&tmp->idr,NULL , set_tid, + set_tid +1 ,GFP_ATOMIC );
Hm, feels to me that we should report a different error code than EAGAIN when the allocation fails for set_tid. Right now you get EAGAIN for both the non set_tid and the set_tid codepath.
But for set_tid the error that you likely should surface is EEXIST, i.e. that pid is already taken.
Quoted 15 lines
kernel/pid.c +9
-0+ 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
Quoted 11 lines · Christian Brauner
Quoted 6 lines · Adrian Reber
The main motivation to add
CLONE_SET_TIDtoclone3()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) tons_last_pidand then (quickly) does aclone(). This works most of the time, but it is racy. It is also slow as it requires multiple syscalls.Can you elaborate how this is racy, please. Afaict, CRIU will always usually restore in a new pid namespace that it controls, right?
Why? No. For example you can checkpoint (not sure this is correct word) a single process in your namespace, then (try to restore) it.
Quoted 2 lines
What is
the exact race?
something else in the same namespace can fork() right after criu writes the pid-for-restore into ns_last_pid.
Oleg.
Christian Brauner
maintainerQuoted 13 lines · Oleg Nesterov
Quoted 11 lines · Christian Brauner
Quoted 6 lines · Adrian Reber
The main motivation to add
CLONE_SET_TIDtoclone3()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) tons_last_pidand then (quickly) does aclone(). This works most of the time, but it is racy. It is also slow as it requires multiple syscalls.Can you elaborate how this is racy, please. Afaict, CRIU will always usually restore in a new pid namespace that it controls, right?
Quoted 8 lines
Why? No. For example you can checkpoint (not sure this is correct word) a single process in your namespace, then (try to restore) it.
Quoted 2 lines
What is
the exact race?something else in the same namespace can
fork()right after criu writes the pid-for-restore intons_last_pid.
Ok, that makes sense. :)
My CRIU userspace knowledge is sporadic, so I'm not sure how exactly it restores process trees in pid namespaces and what workloads this would especially help with.
Quoted 20 lines · Christian Brauner
Quoted 19 lines · Oleg Nesterov
Quoted 11 lines · Christian Brauner
Quoted 6 lines · Adrian Reber
The main motivation to add
CLONE_SET_TIDtoclone3()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) tons_last_pidand then (quickly) does aclone(). This works most of the time, but it is racy. It is also slow as it requires multiple syscalls.Can you elaborate how this is racy, please. Afaict, CRIU will always usually restore in a new pid namespace that it controls, right?
Why? No. For example you can checkpoint (not sure this is correct word) a single process in your namespace, then (try to restore) it.
Quoted 2 lines
What is
the exact race?
Quoted 7 lines
Quoted 2 lines
something else in the same namespace can
fork()right after criu writes the pid-for-restore intons_last_pid.Ok, that makes sense. :)
My CRIU userspace knowledge is sporadic, so I'm not sure how exactly it restores process trees in pid namespaces and what workloads this would especially help with.
Just what Oleg said. CRIU can restore processes in a new PID namespaces or in an existing. To restore a process into an existing PID namespace has the possibility of a PID collision, but if the PID is not yet in use there is no limitation from CRIU's side.
Restoring into an existing PID namespace which is used by other processes always has the possibility that between writing to /proc/sys/kernel/ns_last_pid and clone() something else has fork()'d and therefore it is racy.
Adrian