fork: extend clone3() to support CLONE_SET_TID
Subsystems (5)
PATCH v4 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)
v4:
- Rework struct size check with defines (Christian)
- Reduce number of set_tid checks (Oleg)
- Less parentheses and more robust code (Oleg)
- Do ns_capable() on correct user_ns (Oleg, Christian)
--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 25 +++++++++++++++++++++++-- kernel/pid.c | 34 +++++++++++++++++++++++++++------- 5 files changed, 53 insertions(+), 10 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 +1 -0
@@ -45,6 +45,7 @@ struct clone_args {__aligned_u64 stack;__aligned_u64 stack_size;__aligned_u64 tls; +__aligned_u64 set_tid; };/*
kernel/fork.c +23 -2
@@ -117,6 +117,13 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* Different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* V1 includes set_tid */ +#define CLONE3_ARGS_SIZE_V1 72 +/* * Protected counters by write_lock_irq(&tasklist_lock) */
@@ -2031,7 +2038,13 @@ 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); +if (args->set_tid && !ns_capable ( + p->nsproxy->pid_ns_for_children->user_ns, +CAP_SYS_ADMIN )) { + retval = -EPERM ; + goto bad_fork_cleanup_thread; + } + 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;
@@ -2535,9 +2548,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,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 (unlikely (size <CLONE3_ARGS_SIZE_V0 ))return -EINVAL ; +if (size <sizeof (struct clone_args )) +memset ((void *)&args + size,0 , +sizeof (struct clone_args ) - size); +if (unlikely (!access_ok (uargs, size)))return -EFAULT ;
@@ -2573,6 +2591,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .tls = args.tls, }; +if (size >=CLONE3_ARGS_SIZE_V1 ) + kargs->set_tid = args.set_tid; +return 0 ; }
kernel/pid.c +27 -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,32 @@ 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. +*/ + nr = -EINVAL ; +if (set_tid < pid_max && set_tid >0 && + (set_tid ==1 || !idr_is_empty (&tmp->idr))) + 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
PATCH v4 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
@@ -0,0 +1,2 @@ +clone3_set_tid +clone3
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 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
Quoted 112 lines · Adrian Reber
The main motivation to add
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.Extending
clone3()to supportset_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).This
clone3()extension places the same restrictions (CAP_SYS_ADMIN) onclone3()withset_tidas they are currently in place forns_last_pid.Signed-off-by Adrian Reber <[email protected]>---
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)v3:
- Return EEXIST if PID is already in use (Christian)
- DropCLONE_SET_TID(Christian and Oleg)
- Useidr_is_empty()instead ofidr_get_cursor()(Oleg)
- Handle differentstruct clone_argssizes (Dmitry)v4:
- Rework struct size check with defines (Christian)
- Reduce number ofset_tidchecks (Oleg)
- Less parentheses and more robust code (Oleg)
- Dons_capable()on correctuser_ns(Oleg, Christian)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 25 +++++++++++++++++++++++-- kernel/pid.c | 34 +++++++++++++++++++++++++++------- 5 files changed, 53 insertions(+), 10 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 +1
-0@@ -45,6 +45,7 @@ struct clone_args {__aligned_u64 stack;__aligned_u64 stack_size;__aligned_u64 tls; +__aligned_u64 set_tid; };/* kernel/fork.c +14
-1@@ -117,6 +117,13 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* Different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* V1 includes set_tid */ +#define CLONE3_ARGS_SIZE_V1 72 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2038,13 @@ 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); +if (args->set_tid && !ns_capable ( + p->nsproxy->pid_ns_for_children->user_ns, +CAP_SYS_ADMIN )) { + retval = -EPERM ; + goto bad_fork_cleanup_thread; + } + 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;
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2551
Quoted 5 lines
kernel/fork.c +1
-1@@ -2535,9 +2548,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,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. */
I don't think you need that comment. I think the macro is pretty self-explanatory. If you want it to be even clearer you could even make it CLONE3_ARGS_SIZE_MIN but V0 is good enough. :)
Quoted 11 lines
kernel/fork.c +5
-0+if (unlikely (size <CLONE3_ARGS_SIZE_V0 ))return -EINVAL ; +if (size <sizeof (struct clone_args )) +memset ((void *)&args + size,0 , +sizeof (struct clone_args ) - size); +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 2595
Quoted 5 lines
kernel/fork.c +2
-0@@ -2573,6 +2591,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .tls = args.tls, }; +if (size >=CLONE3_ARGS_SIZE_V1 ) + kargs->set_tid = args.set_tid;
Hm, the if-condition is not needed though, right? At this point we will have already copied from struct clone_args __user *uargs into struct clone_args args. If we hit that codepath that means the kernel definitely has a field for set_tid in its struct clone_args. :) So this could probably just be:
.tls = args.tls,
.set_tid = args.set_tid, } ?
Quoted 24 lines
kernel/fork.c +1
-0+return 0 ; }kernel/pid.c +1
-4@@ -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,32 @@ 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. +*/ + nr = -EINVAL ; + if (set_tid < pid_max && set_tid > 0 &&
Hm, you're already in the if-branch hat verified if (set_tid) so the set_tid > 0 conjunct seems redundant. :)
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 198
Quoted 3 lines
kernel/pid.c +3
-0+ (set_tid == 1 || !idr_is_empty(&tmp->idr))) + nr =idr_alloc (&tmp->idr,NULL , set_tid, + set_tid +1 ,GFP_ATOMIC );
I'm confused, shouldn't this be
if (set_tid < pid_max || (set_tid == 1 && !idr_is_emtpy(&tmp->idf)))
Quoted 22 lines
kernel/pid.c +16
-0+/* +* 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
Quoted 116 lines · Christian Brauner
Quoted 115 lines · Adrian Reber
The main motivation to add
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.Extending
clone3()to supportset_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).This
clone3()extension places the same restrictions (CAP_SYS_ADMIN) onclone3()withset_tidas they are currently in place forns_last_pid.Signed-off-by Adrian Reber <[email protected]>---
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)v3:
- Return EEXIST if PID is already in use (Christian)
- DropCLONE_SET_TID(Christian and Oleg)
- Useidr_is_empty()instead ofidr_get_cursor()(Oleg)
- Handle differentstruct clone_argssizes (Dmitry)v4:
- Rework struct size check with defines (Christian)
- Reduce number ofset_tidchecks (Oleg)
- Less parentheses and more robust code (Oleg)
- Dons_capable()on correctuser_ns(Oleg, Christian)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 25 +++++++++++++++++++++++-- kernel/pid.c | 34 +++++++++++++++++++++++++++------- 5 files changed, 53 insertions(+), 10 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 +1
-0@@ -45,6 +45,7 @@ struct clone_args {__aligned_u64 stack;__aligned_u64 stack_size;__aligned_u64 tls; +__aligned_u64 set_tid; };/* kernel/fork.c +14
-1@@ -117,6 +117,13 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* Different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* V1 includes set_tid */ +#define CLONE3_ARGS_SIZE_V1 72 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2038,13 @@ 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); +if (args->set_tid && !ns_capable ( + p->nsproxy->pid_ns_for_children->user_ns, +CAP_SYS_ADMIN )) { + retval = -EPERM ; + goto bad_fork_cleanup_thread; + } + 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;@@ -2535,9 +2548,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,if (unlikely (size >PAGE_SIZE ))return -E2BIG ;
Quoted 6 lines
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2551
Quoted 2 lines
kernel/fork.c +1
-1-if (unlikely (size <sizeof (struct clone_args ))) +/* The struct needs to be at least the size of the original struct. */ I don't think you need that comment. I think the macro is pretty self-explanatory. If you want it to be even clearer you could even make it
CLONE3_ARGS_SIZE_MINbut V0 is good enough. :)
Will remove the comment.
Quoted 23 lines
Quoted 11 lines
kernel/fork.c +5
-0+if (unlikely (size <CLONE3_ARGS_SIZE_V0 ))return -EINVAL ; +if (size <sizeof (struct clone_args )) +memset ((void *)&args + size,0 , +sizeof (struct clone_args ) - size); +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 2595
Quoted 5 lines
kernel/fork.c +2
-0@@ -2573,6 +2591,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .tls = args.tls, }; +if (size >=CLONE3_ARGS_SIZE_V1 ) + kargs->set_tid = args.set_tid;Hm, the if-condition is not needed though, right? At this point we will have already copied from struct
clone_args__user*uargs into structclone_argsargs. If we hit that codepath that means the kernel definitely has a field forset_tidin its structclone_args. :) So this could probably just be:
Quoted 5 lines
.tls = args.tls, .set_tid = args.set_tid, } ?
Right.
Quoted 21 lines
Quoted 21 lines
kernel/fork.c +1
-0+return 0 ; }kernel/pid.c +1
-1@@ -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,32 @@ struct pid *alloc_pid(struct pid_namespace *ns)if (idr_get_cursor (&tmp->idr) >RESERVED_PIDS ) pid_min =RESERVED_PIDS ;
Quoted 16 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 195
Quoted 13 lines
kernel/pid.c +7
-6-/* -* 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. +*/ + nr = -EINVAL ; + if (set_tid < pid_max && set_tid > 0 &&Hm, you're already in the if-branch hat verified if (
set_tid) so theset_tid> 0 conjunct seems redundant. :)
Yes, but I dropped all checks to see if set_tid is negative as suggested by Oleg and moved it here.
Quoted 7 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 198
Quoted 3 lines
kernel/pid.c +3
-0+ (set_tid == 1 || !idr_is_empty(&tmp->idr))) + nr =idr_alloc (&tmp->idr,NULL , set_tid, + set_tid +1 ,GFP_ATOMIC );I'm confused, shouldn't this be
if (set_tid < pid_max || (set_tid == 1 && !idr_is_emtpy(&tmp->idf)))
Now I am also confused ;). This does not work. This will always return true if set_tid is less than pid_max. So pid_max needs to be something like 1 for the check after || to make sense, right? But you really got me confused here right now. Right now I still think what I did is correct.
Quoted 22 lines
Quoted 22 lines
kernel/pid.c +16
-0+/* +* 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
Quoted 204 lines · Adrian Reber
Quoted 116 lines · Christian Brauner
Quoted 115 lines · Adrian Reber
The main motivation to add
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.Extending
clone3()to supportset_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).This
clone3()extension places the same restrictions (CAP_SYS_ADMIN) onclone3()withset_tidas they are currently in place forns_last_pid.Signed-off-by Adrian Reber <[email protected]>---
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)v3:
- Return EEXIST if PID is already in use (Christian)
- DropCLONE_SET_TID(Christian and Oleg)
- Useidr_is_empty()instead ofidr_get_cursor()(Oleg)
- Handle differentstruct clone_argssizes (Dmitry)v4:
- Rework struct size check with defines (Christian)
- Reduce number ofset_tidchecks (Oleg)
- Less parentheses and more robust code (Oleg)
- Dons_capable()on correctuser_ns(Oleg, Christian)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 25 +++++++++++++++++++++++-- kernel/pid.c | 34 +++++++++++++++++++++++++++------- 5 files changed, 53 insertions(+), 10 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 +1
-0@@ -45,6 +45,7 @@ struct clone_args {__aligned_u64 stack;__aligned_u64 stack_size;__aligned_u64 tls; +__aligned_u64 set_tid; };/* kernel/fork.c +14
-1@@ -117,6 +117,13 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* Different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* V1 includes set_tid */ +#define CLONE3_ARGS_SIZE_V1 72 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2038,13 @@ 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); +if (args->set_tid && !ns_capable ( + p->nsproxy->pid_ns_for_children->user_ns, +CAP_SYS_ADMIN )) { + retval = -EPERM ; + goto bad_fork_cleanup_thread; + } + 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;@@ -2535,9 +2548,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,if (unlikely (size >PAGE_SIZE ))return -E2BIG ;Quoted 6 lines
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2551
Quoted 2 lines
kernel/fork.c +1
-1-if (unlikely (size <sizeof (struct clone_args ))) +/* The struct needs to be at least the size of the original struct. */ I don't think you need that comment. I think the macro is pretty self-explanatory. If you want it to be even clearer you could even make it
CLONE3_ARGS_SIZE_MINbut V0 is good enough. :)Will remove the comment.
Quoted 23 lines
Quoted 11 lines
kernel/fork.c +5
-0+if (unlikely (size <CLONE3_ARGS_SIZE_V0 ))return -EINVAL ; +if (size <sizeof (struct clone_args )) +memset ((void *)&args + size,0 , +sizeof (struct clone_args ) - size); +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 2595
Quoted 5 lines
kernel/fork.c +2
-0@@ -2573,6 +2591,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .tls = args.tls, }; +if (size >=CLONE3_ARGS_SIZE_V1 ) + kargs->set_tid = args.set_tid;Hm, the if-condition is not needed though, right? At this point we will have already copied from struct
clone_args__user*uargs into structclone_argsargs. If we hit that codepath that means the kernel definitely has a field forset_tidin its structclone_args. :) So this could probably just be:Quoted 5 lines
.tls = args.tls, .set_tid = args.set_tid, } ?Right.
Quoted 21 lines
Quoted 21 lines
kernel/fork.c +1
-0+return 0 ; }kernel/pid.c +1
-1@@ -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,32 @@ struct pid *alloc_pid(struct pid_namespace *ns)if (idr_get_cursor (&tmp->idr) >RESERVED_PIDS ) pid_min =RESERVED_PIDS ;Quoted 16 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 195
Quoted 13 lines
kernel/pid.c +7
-6-/* -* 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. +*/ + nr = -EINVAL ; + if (set_tid < pid_max && set_tid > 0 &&Hm, you're already in the if-branch hat verified if (
set_tid) so theset_tid> 0 conjunct seems redundant. :)Yes, but I dropped all checks to see if
set_tidis negative as suggested by Oleg and moved it here.Quoted 6 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 198
Quoted 3 lines
kernel/pid.c +3
-0+ (set_tid == 1 || !idr_is_empty(&tmp->idr))) + nr =idr_alloc (&tmp->idr,NULL , set_tid, + set_tid +1 ,GFP_ATOMIC );I'm confused, shouldn't this be
Quoted 7 lines
Quoted 1 line
if (set_tid < pid_max || (set_tid == 1 && !idr_is_emtpy(&tmp->idf)))Now I am also confused ;). This does not work. This will always return true if
set_tidis less thanpid_max. Sopid_maxneeds to be something like 1 for the check after || to make sense, right? But you really got me confused here right now. Right now I still think what I did is correct.
I missed the part where you reset set_tid to 0 below and mis-parsed the rightmost conjunct in the if statement.
One thing I dislike is that you do
if (set_tid) {
if ([...] && set_tid > 0 && [...])
}For consistency this should rather be:
if (set_tid) {
if ([...] && set_tid && [...])
}Overall I'd rather write the second if as:
if (set_tid && set_tid < pid_max && (set_tid == 1 || !idr_is_empty(&tmp->idr)))
to place the most basic condition (i.e. set_tid is non-zero) leftmost.
The right-most conjunct (set_tid == 1 || !idr_is_empty(&tmp->idr)) is non-trivial to parse. Someone not familiar with that code would need time to figure out why and how exactly 1 is special-cased.
I think overall this is begging for alloc_pid() to be documented now if you don't mind. :)
Christian
Quoted 213 lines · Christian Brauner
Quoted 204 lines · Adrian Reber
Quoted 116 lines · Christian Brauner
Quoted 115 lines · Adrian Reber
The main motivation to add
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.Extending
clone3()to supportset_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).This
clone3()extension places the same restrictions (CAP_SYS_ADMIN) onclone3()withset_tidas they are currently in place forns_last_pid.Signed-off-by Adrian Reber <[email protected]>---
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)v3:
- Return EEXIST if PID is already in use (Christian)
- DropCLONE_SET_TID(Christian and Oleg)
- Useidr_is_empty()instead ofidr_get_cursor()(Oleg)
- Handle differentstruct clone_argssizes (Dmitry)v4:
- Rework struct size check with defines (Christian)
- Reduce number ofset_tidchecks (Oleg)
- Less parentheses and more robust code (Oleg)
- Dons_capable()on correctuser_ns(Oleg, Christian)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 25 +++++++++++++++++++++++-- kernel/pid.c | 34 +++++++++++++++++++++++++++------- 5 files changed, 53 insertions(+), 10 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 +1
-0@@ -45,6 +45,7 @@ struct clone_args {__aligned_u64 stack;__aligned_u64 stack_size;__aligned_u64 tls; +__aligned_u64 set_tid; };/* kernel/fork.c +14
-1@@ -117,6 +117,13 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* Different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* V1 includes set_tid */ +#define CLONE3_ARGS_SIZE_V1 72 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2038,13 @@ 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); +if (args->set_tid && !ns_capable ( + p->nsproxy->pid_ns_for_children->user_ns, +CAP_SYS_ADMIN )) { + retval = -EPERM ; + goto bad_fork_cleanup_thread; + } + 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;@@ -2535,9 +2548,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,if (unlikely (size >PAGE_SIZE ))return -E2BIG ;Quoted 6 lines
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2551
Quoted 2 lines
kernel/fork.c +1
-1-if (unlikely (size <sizeof (struct clone_args ))) +/* The struct needs to be at least the size of the original struct. */ I don't think you need that comment. I think the macro is pretty self-explanatory. If you want it to be even clearer you could even make it
CLONE3_ARGS_SIZE_MINbut V0 is good enough. :)Will remove the comment.
Quoted 23 lines
Quoted 11 lines
kernel/fork.c +5
-0+if (unlikely (size <CLONE3_ARGS_SIZE_V0 ))return -EINVAL ; +if (size <sizeof (struct clone_args )) +memset ((void *)&args + size,0 , +sizeof (struct clone_args ) - size); +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 2595
Quoted 5 lines
kernel/fork.c +2
-0@@ -2573,6 +2591,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .tls = args.tls, }; +if (size >=CLONE3_ARGS_SIZE_V1 ) + kargs->set_tid = args.set_tid;Hm, the if-condition is not needed though, right? At this point we will have already copied from struct
clone_args__user*uargs into structclone_argsargs. If we hit that codepath that means the kernel definitely has a field forset_tidin its structclone_args. :) So this could probably just be:Quoted 5 lines
.tls = args.tls, .set_tid = args.set_tid, } ?Right.
Quoted 21 lines
Quoted 21 lines
kernel/fork.c +1
-0+return 0 ; }kernel/pid.c +1
-1@@ -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,32 @@ struct pid *alloc_pid(struct pid_namespace *ns)if (idr_get_cursor (&tmp->idr) >RESERVED_PIDS ) pid_min =RESERVED_PIDS ;Quoted 16 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 195
Quoted 13 lines
kernel/pid.c +7
-6-/* -* 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. +*/ + nr = -EINVAL ; + if (set_tid < pid_max && set_tid > 0 &&Hm, you're already in the if-branch hat verified if (
set_tid) so theset_tid> 0 conjunct seems redundant. :)Yes, but I dropped all checks to see if
set_tidis negative as suggested by Oleg and moved it here.Quoted 6 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 198
Quoted 3 lines
kernel/pid.c +3
-0+ (set_tid == 1 || !idr_is_empty(&tmp->idr))) + nr =idr_alloc (&tmp->idr,NULL , set_tid, + set_tid +1 ,GFP_ATOMIC );I'm confused, shouldn't this be
Quoted 7 lines
Quoted 1 line
if (set_tid < pid_max || (set_tid == 1 && !idr_is_emtpy(&tmp->idf)))Now I am also confused ;). This does not work. This will always return true if
set_tidis less thanpid_max. Sopid_maxneeds to be something like 1 for the check after || to make sense, right? But you really got me confused here right now. Right now I still think what I did is correct.
Quoted 8 lines
I missed the part where you reset
set_tidto 0 below and mis-parsed the rightmost conjunct in the if statement.One thing I dislike is that you do
if (set_tid) { if ([...] && set_tid > 0 && [...]) }
Thinking about this a bit more you probably did the explicit set_tid > 0 to catch the case where it is negative? But also, I don't understand why we do any work at all before having verified that set_tid is sensible, i.e. why do we call kmem_cache_calloc(), do idr_preload(), and take a spinlock, before even verifying that our parameters are sane? If there's no specific reason for this I suggest to patch alloc_pid() like this:
-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;
@@ -166,6 +166,9 @@ struct pid *alloc_pid(struct pid_namespace *ns)
struct upid *upid;
int retval = -ENOMEM;+ if (set_tid < 0 || set_tid >= pid_max)
+ return ERR_PTR(-EINVAL);
+
pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
if (!pid)
return ERR_PTR(retval);
@@ -186,12 +189,31 @@ 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. + */
+ nr = -EINVAL;
+ if (set_tid == 1 || !idr_is_empty(&tmp->idr))
+ 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()This makes things a lot more clearer in my opinion. First, verify that the pre-conditions are met. Second, verify that the conditions are met which depend on the state of the pid namespace, i.e. there's either already a pid 1 or pid 1 is requested.
We should also do this since alloc_pid() is exported in a header file and so we can't and shouldn't rely on the fact that all callers will pass in something sensible for set_tid.
Quoted 20 lines
For consistency this should rather be:
if (set_tid) { if ([...] && set_tid && [...]) }Overall I'd rather write the second if as:
if (
set_tid&&set_tid<pid_max&& (set_tid== 1 || !idr_is_empty(&tmp->idr)))to place the most basic condition (i.e.
set_tidis non-zero) leftmost.The right-most conjunct (
set_tid== 1 || !idr_is_empty(&tmp->idr)) is non-trivial to parse. Someone not familiar with that code would need time to figure out why and how exactly 1 is special-cased.
I think overall this is begging foralloc_pid()to be documented now if you don't mind. :)Christian
Quoted 283 lines · Christian Brauner
Quoted 213 lines · Christian Brauner
Quoted 204 lines · Adrian Reber
Quoted 116 lines · Christian Brauner
Quoted 115 lines · Adrian Reber
The main motivation to add
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.Extending
clone3()to supportset_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).This
clone3()extension places the same restrictions (CAP_SYS_ADMIN) onclone3()withset_tidas they are currently in place forns_last_pid.Signed-off-by Adrian Reber <[email protected]>---
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)v3:
- Return EEXIST if PID is already in use (Christian)
- DropCLONE_SET_TID(Christian and Oleg)
- Useidr_is_empty()instead ofidr_get_cursor()(Oleg)
- Handle differentstruct clone_argssizes (Dmitry)v4:
- Rework struct size check with defines (Christian)
- Reduce number ofset_tidchecks (Oleg)
- Less parentheses and more robust code (Oleg)
- Dons_capable()on correctuser_ns(Oleg, Christian)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 25 +++++++++++++++++++++++-- kernel/pid.c | 34 +++++++++++++++++++++++++++------- 5 files changed, 53 insertions(+), 10 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 +1
-0@@ -45,6 +45,7 @@ struct clone_args {__aligned_u64 stack;__aligned_u64 stack_size;__aligned_u64 tls; +__aligned_u64 set_tid; };/* kernel/fork.c +14
-1@@ -117,6 +117,13 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* Different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* V1 includes set_tid */ +#define CLONE3_ARGS_SIZE_V1 72 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2038,13 @@ 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); +if (args->set_tid && !ns_capable ( + p->nsproxy->pid_ns_for_children->user_ns, +CAP_SYS_ADMIN )) { + retval = -EPERM ; + goto bad_fork_cleanup_thread; + } + 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;@@ -2535,9 +2548,14 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,if (unlikely (size >PAGE_SIZE ))return -E2BIG ;Quoted 6 lines
kernel/fork.c noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, · line 2551
Quoted 2 lines
kernel/fork.c +1
-1-if (unlikely (size <sizeof (struct clone_args ))) +/* The struct needs to be at least the size of the original struct. */ I don't think you need that comment. I think the macro is pretty self-explanatory. If you want it to be even clearer you could even make it
CLONE3_ARGS_SIZE_MINbut V0 is good enough. :)Will remove the comment.
Quoted 23 lines
Quoted 11 lines
kernel/fork.c +5
-0+if (unlikely (size <CLONE3_ARGS_SIZE_V0 ))return -EINVAL ; +if (size <sizeof (struct clone_args )) +memset ((void *)&args + size,0 , +sizeof (struct clone_args ) - size); +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 2595
Quoted 5 lines
kernel/fork.c +2
-0@@ -2573,6 +2591,9 @@ noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, .tls = args.tls, }; +if (size >=CLONE3_ARGS_SIZE_V1 ) + kargs->set_tid = args.set_tid;Hm, the if-condition is not needed though, right? At this point we will have already copied from struct
clone_args__user*uargs into structclone_argsargs. If we hit that codepath that means the kernel definitely has a field forset_tidin its structclone_args. :) So this could probably just be:Quoted 5 lines
.tls = args.tls, .set_tid = args.set_tid, } ?Right.
Quoted 21 lines
Quoted 21 lines
kernel/fork.c +1
-0+return 0 ; }kernel/pid.c +1
-1@@ -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,32 @@ struct pid *alloc_pid(struct pid_namespace *ns)if (idr_get_cursor (&tmp->idr) >RESERVED_PIDS ) pid_min =RESERVED_PIDS ;Quoted 16 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 195
Quoted 13 lines
kernel/pid.c +7
-6-/* -* 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. +*/ + nr = -EINVAL ; + if (set_tid < pid_max && set_tid > 0 &&Hm, you're already in the if-branch hat verified if (
set_tid) so theset_tid> 0 conjunct seems redundant. :)Yes, but I dropped all checks to see if
set_tidis negative as suggested by Oleg and moved it here.Quoted 6 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 198
Quoted 3 lines
kernel/pid.c +3
-0+ (set_tid == 1 || !idr_is_empty(&tmp->idr))) + nr =idr_alloc (&tmp->idr,NULL , set_tid, + set_tid +1 ,GFP_ATOMIC );I'm confused, shouldn't this be
Quoted 7 lines
Quoted 1 line
if (set_tid < pid_max || (set_tid == 1 && !idr_is_emtpy(&tmp->idf)))Now I am also confused ;). This does not work. This will always return true if
set_tidis less thanpid_max. Sopid_maxneeds to be something like 1 for the check after || to make sense, right? But you really got me confused here right now. Right now I still think what I did is correct.Quoted 8 lines
I missed the part where you reset
set_tidto 0 below and mis-parsed the rightmost conjunct in the if statement.One thing I dislike is that you do
if (set_tid) { if ([...] && set_tid > 0 && [...]) }Thinking about this a bit more you probably did the explicit
set_tid> 0 to catch the case where it is negative? But also, I don't understand why we do any work at all before having verified thatset_tidis sensible, i.e. why do we callkmem_cache_calloc(), doidr_preload(), and take a spinlock, before even verifying that our parameters are sane? If there's no specific reason for this I suggest to patchalloc_pid()like this:kernel/pid.c +29
-7-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;@@ -166,6 +166,9 @@ struct pid *alloc_pid(struct pid_namespace *ns)struct upid *upid;int retval = -ENOMEM ; +if (set_tid <0 || set_tid >= pid_max) +return ERR_PTR (-EINVAL ); + pid =kmem_cache_alloc (ns->pid_cachep,GFP_KERNEL );if (!pid)return ERR_PTR (retval);@@ -186,12 +189,31 @@ 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. +*/ + nr = -EINVAL ; +if (set_tid ==1 || !idr_is_empty (&tmp->idr)) + 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 ()
Quoted 7 lines
This makes things a lot more clearer in my opinion. First, verify that the pre-conditions are met. Second, verify that the conditions are met which depend on the state of the pid namespace, i.e. there's either already a pid 1 or pid 1 is requested.
We should also do this sincealloc_pid()is exported in a header file and so we can't and shouldn't rely on the fact that all callers will pass in something sensible forset_tid.
I like that. I will update the patch and send it out to match this suggestion.
Adrian