fork: extend clone3() to support setting a PID
Subsystems (5)
PATCH v6 1/2 fork: extend clone3() to support setting a PID
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)
v5:
- make set_tid checks earlier in alloc_pid() (Christian)
- remove unnecessary comment and struct size check (Christian)
v6:
- remove CLONE_SET_TID from description (Christian)
- add clone3() tests for different clone_args sizes (Christian)
- move more set_tid checks to alloc_pid() (Oleg)
- make all set_tid checks lockless (Oleg)
--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 14 ++++++++++++-- kernel/pid.c | 37 ++++++++++++++++++++++++++++++------- 5 files changed, 45 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 +12 -2
@@ -117,6 +117,11 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* For different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* * Protected counters by write_lock_irq(&tasklist_lock) */
@@ -2031,7 +2036,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;
@@ -2535,9 +2540,13 @@ 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 ))) +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 ;
@@ -2571,6 +2580,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 ;
kernel/pid.c +30 -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;
@@ -166,6 +166,16 @@ struct pid *alloc_pid(struct pid_namespace *ns)struct upid *upid;int retval = -ENOMEM ; +if (set_tid) { +if (set_tid <0 || set_tid >= pid_max) +return ERR_PTR (-EINVAL ); +/* Also fail if a PID != 1 is requested and no PID 1 exists */ +if (set_tid !=1 && !ns->child_reaper) +return ERR_PTR (-EINVAL ); +if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return ERR_PTR (-EPERM ); + } + pid =kmem_cache_alloc (ns->pid_cachep,GFP_KERNEL );if (!pid)return ERR_PTR (retval);
@@ -186,12 +196,25 @@ 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) { + 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 v6 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.
In addition the clone3 test (without set_tid) tries to call clone3() with different sizes of clone_args.
--- tools/testing/selftests/clone3/.gitignore | 2 + tools/testing/selftests/clone3/Makefile | 11 + tools/testing/selftests/clone3/clone3.c | 231 ++++++++++++++++++ .../testing/selftests/clone3/clone3_set_tid.c | 161 ++++++++++++ 4 files changed, 405 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 +231 -0
231 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
Re: [PATCH v6 2/2] selftests: add tests for clone3()
Quoted 9 lines · Adrian Reber
This tests
clone3()with and withoutset_tidto see if all desired PIDs are working as expected. The test tries toclone3()with aset_tidof -1, 1,pid_max, a PID which is already in use and an unused PID. The same tests are also running in PID namespace.In addition the clone3 test (without
set_tid) tries to callclone3()with different sizes ofclone_args.Signed-off-by Adrian Reber <[email protected]>
This is missing a .gitignore file for the new binaries. But no need to resend I'll just add that myself.
Re: [PATCH v6 2/2] selftests: add tests for clone3()
Quoted 6 lines · Christian Brauner
Quoted 5 lines · Adrian Reber
This tests
clone3()with and withoutset_tidto see if all desired PIDs are working as expected. The test tries toclone3()with aset_tidof -1, 1,pid_max, a PID which is already in use and an unused PID. The same tests are also running in PID namespace.
Quoted 7 lines
Quoted 4 lines
In addition the clone3 test (without
set_tid) tries to callclone3()with different sizes ofclone_args.Signed-off-by Adrian Reber <[email protected]>This is missing a .gitignore file for the new binaries. But no need to resend I'll just add that myself.
Oh, I see it now. :)
Quoted 151 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)v5:
- makeset_tidchecks earlier inalloc_pid()(Christian)
- remove unnecessary comment and struct size check (Christian)v6:
- removeCLONE_SET_TIDfrom description (Christian)
- addclone3()tests for differentclone_argssizes (Christian)
- move moreset_tidchecks toalloc_pid()(Oleg)
- make allset_tidchecks lockless (Oleg)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 14 ++++++++++++-- kernel/pid.c | 37 ++++++++++++++++++++++++++++++------- 5 files changed, 45 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 +12
-2@@ -117,6 +117,11 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* For different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2036,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;@@ -2535,9 +2540,13 @@ 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 ))) +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 ;@@ -2571,6 +2580,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 ;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;
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 174
Quoted 9 lines
kernel/pid.c +6
-0@@ -166,6 +166,16 @@ struct pid *alloc_pid(struct pid_namespace *ns)struct upid *upid;int retval = -ENOMEM ; +if (set_tid) { +if (set_tid <0 || set_tid >= pid_max) +return ERR_PTR (-EINVAL ); +/* Also fail if a PID != 1 is requested and no PID 1 exists */ +if (set_tid !=1 && !ns->child_reaper) +return ERR_PTR (-EINVAL );
What will happen if I will create a new pid namespace (unsahre(CLONE_NEWPID)) and then call clone3 with tid which isn't equal to 1?
Will I get the init process with the 5 pid?...
Quoted 42 lines
kernel/pid.c +23
-7+if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return ERR_PTR (-EPERM ); + } + pid =kmem_cache_alloc (ns->pid_cachep,GFP_KERNEL );if (!pid)return ERR_PTR (retval);@@ -186,12 +196,25 @@ 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) { + 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 (); --2.21.0
Quoted 166 lines · Andrei Vagin
Quoted 151 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)v5:
- makeset_tidchecks earlier inalloc_pid()(Christian)
- remove unnecessary comment and struct size check (Christian)v6:
- removeCLONE_SET_TIDfrom description (Christian)
- addclone3()tests for differentclone_argssizes (Christian)
- move moreset_tidchecks toalloc_pid()(Oleg)
- make allset_tidchecks lockless (Oleg)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 14 ++++++++++++-- kernel/pid.c | 37 ++++++++++++++++++++++++++++++------- 5 files changed, 45 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 +12
-2@@ -117,6 +117,11 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* For different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2036,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;@@ -2535,9 +2540,13 @@ 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 ))) +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 ;@@ -2571,6 +2580,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 ;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;kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 174
Quoted 9 lines
kernel/pid.c +6
-0@@ -166,6 +166,16 @@ struct pid *alloc_pid(struct pid_namespace *ns)struct upid *upid;int retval = -ENOMEM ; +if (set_tid) { +if (set_tid <0 || set_tid >= pid_max) +return ERR_PTR (-EINVAL ); +/* Also fail if a PID != 1 is requested and no PID 1 exists */ +if (set_tid !=1 && !ns->child_reaper) +return ERR_PTR (-EINVAL );What will happen if I will create a new pid namespace (
unsahre(CLONE_NEWPID)) and then call clone3 with tid which isn't equal to 1?
Quoted 1 line
Will I get the init process with the 5 pid?...
No, you will get -EINVAL. In the selftest exactly this is tested. A PID 1 is still required even with this patch.
Quoted 42 lines
Quoted 42 lines
kernel/pid.c +23
-7+if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return ERR_PTR (-EPERM ); + } + pid =kmem_cache_alloc (ns->pid_cachep,GFP_KERNEL );if (!pid)return ERR_PTR (retval);@@ -186,12 +196,25 @@ 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) { + 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 (); --2.21.0
Quoted 167 lines · Adrian Reber
Quoted 166 lines · Andrei Vagin
Quoted 151 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)v5:
- makeset_tidchecks earlier inalloc_pid()(Christian)
- remove unnecessary comment and struct size check (Christian)v6:
- removeCLONE_SET_TIDfrom description (Christian)
- addclone3()tests for differentclone_argssizes (Christian)
- move moreset_tidchecks toalloc_pid()(Oleg)
- make allset_tidchecks lockless (Oleg)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 14 ++++++++++++-- kernel/pid.c | 37 ++++++++++++++++++++++++++++++------- 5 files changed, 45 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 +12
-2@@ -117,6 +117,11 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* For different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2036,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;@@ -2535,9 +2540,13 @@ 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 ))) +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 ;@@ -2571,6 +2580,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 ;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;kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 174
Quoted 9 lines
kernel/pid.c +6
-0@@ -166,6 +166,16 @@ struct pid *alloc_pid(struct pid_namespace *ns)struct upid *upid;int retval = -ENOMEM ; +if (set_tid) { +if (set_tid <0 || set_tid >= pid_max) +return ERR_PTR (-EINVAL ); +/* Also fail if a PID != 1 is requested and no PID 1 exists */ +if (set_tid !=1 && !ns->child_reaper) +return ERR_PTR (-EINVAL );What will happen if I will create a new pid namespace (
unsahre(CLONE_NEWPID)) and then call clone3 with tid which isn't equal to 1?
Quoted 4 lines
Quoted 1 line
Will I get the init process with the 5 pid?...
No, you will get
-EINVAL. In the selftest exactly this is tested. A PID 1 is still required even with this patch.
Correct. unshare(CLONE_PIDNS) leaves ->child_reaper unset. ->child_reaper is set when pid 1 is created in the pid namespace. So you that check catches the scenarios afaict. Adrians test should also have caught that if it were possible. I hope he's running them. :)
Quoted 168 lines · Adrian Reber
Quoted 166 lines · Andrei Vagin
Quoted 151 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)v5:
- makeset_tidchecks earlier inalloc_pid()(Christian)
- remove unnecessary comment and struct size check (Christian)v6:
- removeCLONE_SET_TIDfrom description (Christian)
- addclone3()tests for differentclone_argssizes (Christian)
- move moreset_tidchecks toalloc_pid()(Oleg)
- make allset_tidchecks lockless (Oleg)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 14 ++++++++++++-- kernel/pid.c | 37 ++++++++++++++++++++++++++++++------- 5 files changed, 45 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 +12
-2@@ -117,6 +117,11 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* For different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2036,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;@@ -2535,9 +2540,13 @@ 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 ))) +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 ;@@ -2571,6 +2580,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 ;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;kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns) · line 174
Quoted 9 lines
kernel/pid.c +6
-0@@ -166,6 +166,16 @@ struct pid *alloc_pid(struct pid_namespace *ns)struct upid *upid;int retval = -ENOMEM ; +if (set_tid) { +if (set_tid <0 || set_tid >= pid_max) +return ERR_PTR (-EINVAL ); +/* Also fail if a PID != 1 is requested and no PID 1 exists */ +if (set_tid !=1 && !ns->child_reaper) +return ERR_PTR (-EINVAL );What will happen if I will create a new pid namespace (
unsahre(CLONE_NEWPID)) and then call clone3 with tid which isn't equal to 1?
Quoted 4 lines
Quoted 1 line
Will I get the init process with the 5 pid?...
No, you will get
-EINVAL. In the selftest exactly this is tested. A PID 1 is still required even with this patch.
Good. Thanks. I had to read the test.
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
kernel/pid.c +23
-7+if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return ERR_PTR (-EPERM ); + } + pid =kmem_cache_alloc (ns->pid_cachep,GFP_KERNEL );if (!pid)return ERR_PTR (retval);@@ -186,12 +196,25 @@ 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) { + 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 (); --2.21.0
Quoted 7 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.
Quoted 8 lines
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]>
I'm fine with this version. Oleg, how do you feel about it now? :)
Quoted 186 lines
---
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)v5:
- makeset_tidchecks earlier inalloc_pid()(Christian)
- remove unnecessary comment and struct size check (Christian)v6:
- removeCLONE_SET_TIDfrom description (Christian)
- addclone3()tests for differentclone_argssizes (Christian)
- move moreset_tidchecks toalloc_pid()(Oleg)
- make allset_tidchecks lockless (Oleg)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 14 ++++++++++++-- kernel/pid.c | 37 ++++++++++++++++++++++++++++++------- 5 files changed, 45 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 +12
-2@@ -117,6 +117,11 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* For different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2036,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;@@ -2535,9 +2540,13 @@ 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 ))) +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 ;@@ -2571,6 +2580,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 ;kernel/pid.c +30
-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;@@ -166,6 +166,16 @@ struct pid *alloc_pid(struct pid_namespace *ns)struct upid *upid;int retval = -ENOMEM ; +if (set_tid) { +if (set_tid <0 || set_tid >= pid_max) +return ERR_PTR (-EINVAL ); +/* Also fail if a PID != 1 is requested and no PID 1 exists */ +if (set_tid !=1 && !ns->child_reaper) +return ERR_PTR (-EINVAL ); +if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return ERR_PTR (-EPERM ); + } + pid =kmem_cache_alloc (ns->pid_cachep,GFP_KERNEL );if (!pid)return ERR_PTR (retval);@@ -186,12 +196,25 @@ 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) { + 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
Quoted 8 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.
Quoted 8 lines
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]>
Quoted 7 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.
Quoted 8 lines
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]>
Quoted 184 lines
---
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)v5:
- makeset_tidchecks earlier inalloc_pid()(Christian)
- remove unnecessary comment and struct size check (Christian)v6:
- removeCLONE_SET_TIDfrom description (Christian)
- addclone3()tests for differentclone_argssizes (Christian)
- move moreset_tidchecks toalloc_pid()(Oleg)
- make allset_tidchecks lockless (Oleg)--- include/linux/pid.h | 2 +- include/linux/sched/task.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 14 ++++++++++++-- kernel/pid.c | 37 ++++++++++++++++++++++++++++++------- 5 files changed, 45 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 +12
-2@@ -117,6 +117,11 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* For different sizes of struct clone_args +*/ +#define CLONE3_ARGS_SIZE_V0 64 +/* * Protected counters by write_lock_irq(&tasklist_lock) */ @@ -2031,7 +2036,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;@@ -2535,9 +2540,13 @@ 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 ))) +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 ;@@ -2571,6 +2580,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 ;kernel/pid.c +30
-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;@@ -166,6 +166,16 @@ struct pid *alloc_pid(struct pid_namespace *ns)struct upid *upid;int retval = -ENOMEM ; +if (set_tid) { +if (set_tid <0 || set_tid >= pid_max) +return ERR_PTR (-EINVAL ); +/* Also fail if a PID != 1 is requested and no PID 1 exists */ +if (set_tid !=1 && !ns->child_reaper) +return ERR_PTR (-EINVAL ); +if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return ERR_PTR (-EPERM ); + } + pid =kmem_cache_alloc (ns->pid_cachep,GFP_KERNEL );if (!pid)return ERR_PTR (retval);@@ -186,12 +196,25 @@ 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) { + 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 18 lines · Pavel Emelianov
Quoted 7 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.Quoted 8 lines
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]>Acked-by Pavel Emelyanov <[email protected]>
Quoted 13 lines · Oleg Nesterov
Quoted 12 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).
Quoted 7 lines
Quoted 4 lines
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]>Reviewed-by Oleg Nesterov <[email protected]>
Added-to:
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=pidfd
Merged-into:
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=for-next
Thanks!
Christian