Skip to contentSkip to conversation

clone: Introduce the CLONE_CHILD_USEPID functionality

PATCH New Cyrill Gorcunov 1 participant 1 message Oct 13, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clone: Introduce the CLONE_CHILD_USEPID functionality

At previous review session seems no final conclusion was made so this is a second attempt to step forward.

---
From: Pavel Emelyanov <[email protected]>
Subject: [PATCH] clone: Introduce the CLONE_CHILD_USEPID functionality

When restoring a task (or a set of tasks) we need to recreate them with exactly the same pid as they had before. Thus we need the ability to create a task with specified pid.

The proposal is to reuse the already free CLONE_STOPPED clone flag.

About the security implication - this can create some problems with pids wraparound and similar, so this approach can be restricted with the "don't allow for CLONE_CHILD_USEPID when the current pid namespace has ever done real pid allocation". This will work perfectly for checkpoint-restore and will not give anyone chances for screwing pids up on a living system.

---
 include/linux/pid.h   |    2 -
 include/linux/sched.h |    1 
 kernel/fork.c         |   10 ++++++-
 kernel/pid.c          |   70 ++++++++++++++++++++++++++++++++++++--------------
 4 files changed, 62 insertions(+), 21 deletions(-)
include/linux/pid.h +1 -1
Index: linux-2.6.git/include/linux/pid.h
===================================================================
@@ -119,7 +119,7 @@ extern struct pid *find_get_pid(int nr);
 extern struct pid *find_ge_pid(int nr, struct pid_namespace *);
 int next_pidmap(struct pid_namespace *pid_ns, unsigned int last);
 
-extern struct pid *alloc_pid(struct pid_namespace *ns);
+extern struct pid *alloc_pid(struct pid_namespace *ns, int pid);
 extern void free_pid(struct pid *pid);
 
 /*
include/linux/sched.h +1 -0
Index: linux-2.6.git/include/linux/sched.h
===================================================================
@@ -23,6 +23,7 @@
 #define CLONE_CHILD_SETTID	0x01000000	/* set the TID in the child */
 /* 0x02000000 was previously the unused CLONE_STOPPED (Start in stopped state)
    and is now available for re-use. */
+#define CLONE_CHILD_USEPID	0x02000000	/* use the given pid */
 #define CLONE_NEWUTS		0x04000000	/* New utsname group? */
 #define CLONE_NEWIPC		0x08000000	/* New ipcs */
 #define CLONE_NEWUSER		0x10000000	/* New user namespace */
kernel/fork.c +9 -1
Index: linux-2.6.git/kernel/fork.c
===================================================================
@@ -1239,8 +1239,16 @@ static struct task_struct *copy_process(
 		goto bad_fork_cleanup_io;
 
 	if (pid != &init_struct_pid) {
+		int want_pid = 0;
+
+		if (clone_flags & CLONE_CHILD_USEPID) {
+			retval = get_user(want_pid, child_tidptr);
+			if (retval)
+				goto bad_fork_cleanup_io;
+		}
+
 		retval = -ENOMEM;
-		pid = alloc_pid(p->nsproxy->pid_ns);
+		pid = alloc_pid(p->nsproxy->pid_ns, want_pid);
 		if (!pid)
 			goto bad_fork_cleanup_io;
 	}
kernel/pid.c +51 -19

104 lines not on this page · raw message