Skip to contentSkip to conversation

fork.c: Move check of clone NEWIPC and SYSVSEM to copy_process

PATCH In review Marcos Paulo de Souza 2 participants 4 messages Nov 26, 2017 – Dec 1, 2017

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

PATCH 1/1 fork.c: Move check of clone NEWIPC and SYSVSEM to copy_process

Currently this check for CLONE_NEWIPC with CLONE_SYSVSEM is done inside copy_namespaces, resulting in a handful of error paths being executed if these flags were used together. So, move this check to the beginning of copy_process, exiting earlier if the condition is true.

This move is safe because copy_namespaces is called just from copy_process function.

---
 kernel/fork.c    | 11 +++++++++++
 kernel/nsproxy.c | 11 -----------
 2 files changed, 11 insertions(+), 11 deletions(-)
kernel/fork.c +11 -0
index 2113e252cb9d..691f9ba135fc 100644
@@ -1600,6 +1600,17 @@ static __latent_entropy struct task_struct *copy_process(
 		return ERR_PTR(-EINVAL);
 
 	/*
+	 * CLONE_NEWIPC must detach from the undolist: after switching
+	 * to a new ipc namespace, the semaphore arrays from the old
+	 * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
+	 * means share undolist with parent, so we must forbid using
+	 * it along with CLONE_NEWIPC.
+	 */
+	if ((clone_flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
+		(CLONE_NEWIPC | CLONE_SYSVSEM))
+		return ERR_PTR(-EINVAL);
+
+	/*
 	 * Thread groups must share signals as well, and detached threads
 	 * can only be started up within the thread group.
 	 */
kernel/nsproxy.c +0 -11
index f6c5d330059a..30882727dff5 100644
@@ -151,17 +151,6 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
 	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
 		return -EPERM;
 
-	/*
-	 * CLONE_NEWIPC must detach from the undolist: after switching
-	 * to a new ipc namespace, the semaphore arrays from the old
-	 * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
-	 * means share undolist with parent, so we must forbid using
-	 * it along with CLONE_NEWIPC.
-	 */
-	if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
-		(CLONE_NEWIPC | CLONE_SYSVSEM)) 
-		return -EINVAL;
-
 	new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
 	if (IS_ERR(new_ns))
 		return  PTR_ERR(new_ns);
Signature · 1 line
2.13.6
#1 lore raw

CC Eric

Quoted 7 lines · Marcos Paulo de Souza

Currently this check for CLONE_NEWIPC with CLONE_SYSVSEM is done inside copy_namespaces, resulting in a handful of error paths being executed if these flags were used together. So, move this check to the beginning of copy_process, exiting earlier if the condition is true.

This move is safe because copy_namespaces is called just from copy_process function.

I am not familiar with the code all that much but the justification is not clear to me. Thesea re namespace related flags so why should we pull them out of copy_namespaces. I do not see any simplifications in the error code paths or something like that.

Quoted 53 lines
---
 kernel/fork.c    | 11 +++++++++++
 kernel/nsproxy.c | 11 -----------
 2 files changed, 11 insertions(+), 11 deletions(-)
kernel/fork.c +11 -0
index 2113e252cb9d..691f9ba135fc 100644
@@ -1600,6 +1600,17 @@ static __latent_entropy struct task_struct *copy_process(
 		return ERR_PTR(-EINVAL);
 
 	/*
+	 * CLONE_NEWIPC must detach from the undolist: after switching
+	 * to a new ipc namespace, the semaphore arrays from the old
+	 * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
+	 * means share undolist with parent, so we must forbid using
+	 * it along with CLONE_NEWIPC.
+	 */
+	if ((clone_flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
+		(CLONE_NEWIPC | CLONE_SYSVSEM))
+		return ERR_PTR(-EINVAL);
+
+	/*
 	 * Thread groups must share signals as well, and detached threads
 	 * can only be started up within the thread group.
 	 */
kernel/nsproxy.c +0 -11
index f6c5d330059a..30882727dff5 100644
@@ -151,17 +151,6 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
 	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
 		return -EPERM;
 
-	/*
-	 * CLONE_NEWIPC must detach from the undolist: after switching
-	 * to a new ipc namespace, the semaphore arrays from the old
-	 * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
-	 * means share undolist with parent, so we must forbid using
-	 * it along with CLONE_NEWIPC.
-	 */
-	if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
-		(CLONE_NEWIPC | CLONE_SYSVSEM)) 
-		return -EINVAL;
-
 	new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
 	if (IS_ERR(new_ns))
 		return  PTR_ERR(new_ns);
Signature · 1 line
2.13.6
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 10 lines · Michal Hocko

CC Eric

Quoted 7 lines · Marcos Paulo de Souza

Currently this check for CLONE_NEWIPC with CLONE_SYSVSEM is done inside copy_namespaces, resulting in a handful of error paths being executed if these flags were used together. So, move this check to the beginning of copy_process, exiting earlier if the condition is true.

This move is safe because copy_namespaces is called just from copy_process function.

This change is introduced right below the point where clone_flags is already checking for inconsistencies in namespace flags[1], and returns EINVAL when conflicting flags are informed together.

In this case, it's easier to return early when conflicting flags are informed at the beginning, so moving a namespace check to where namespaces are already being sanitized makes sense. If the code stays where it is now, and a user calls clone syscalls informing CLONE_NEWIPC | CLONE_SYSVSEM, the code will need to undo a lot of work before returning the same EINVAL[2].

[1] https://elixir.free-electrons.com/linux/latest/source/kernel/fork.c#L1552
[2] https://elixir.free-electrons.com/linux/latest/source/kernel/fork.c#L1953

Quoted 63 lines

I am not familiar with the code all that much but the justification is not clear to me. Thesea re namespace related flags so why should we pull them out of copy_namespaces. I do not see any simplifications in the error code paths or something like that.

Quoted 53 lines
---
 kernel/fork.c    | 11 +++++++++++
 kernel/nsproxy.c | 11 -----------
 2 files changed, 11 insertions(+), 11 deletions(-)
kernel/fork.c +11 -0
index 2113e252cb9d..691f9ba135fc 100644
@@ -1600,6 +1600,17 @@ static __latent_entropy struct task_struct *copy_process(
 		return ERR_PTR(-EINVAL);
 
 	/*
+	 * CLONE_NEWIPC must detach from the undolist: after switching
+	 * to a new ipc namespace, the semaphore arrays from the old
+	 * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
+	 * means share undolist with parent, so we must forbid using
+	 * it along with CLONE_NEWIPC.
+	 */
+	if ((clone_flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
+		(CLONE_NEWIPC | CLONE_SYSVSEM))
+		return ERR_PTR(-EINVAL);
+
+	/*
 	 * Thread groups must share signals as well, and detached threads
 	 * can only be started up within the thread group.
 	 */
kernel/nsproxy.c +0 -11
index f6c5d330059a..30882727dff5 100644
@@ -151,17 +151,6 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
 	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
 		return -EPERM;
 
-	/*
-	 * CLONE_NEWIPC must detach from the undolist: after switching
-	 * to a new ipc namespace, the semaphore arrays from the old
-	 * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
-	 * means share undolist with parent, so we must forbid using
-	 * it along with CLONE_NEWIPC.
-	 */
-	if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
-		(CLONE_NEWIPC | CLONE_SYSVSEM)) 
-		return -EINVAL;
-
 	new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
 	if (IS_ERR(new_ns))
 		return  PTR_ERR(new_ns);
Signature · 1 line
2.13.6
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Thanks,
	Marcos
#3 lore raw
Quoted 12 lines · Marcos Paulo de Souza
Quoted 10 lines · Michal Hocko

CC Eric

Quoted 7 lines · Marcos Paulo de Souza

Currently this check for CLONE_NEWIPC with CLONE_SYSVSEM is done inside copy_namespaces, resulting in a handful of error paths being executed if these flags were used together. So, move this check to the beginning of copy_process, exiting earlier if the condition is true.

This move is safe because copy_namespaces is called just from copy_process function.

Quoted 7 lines

This change is introduced right below the point where clone_flags is already checking for inconsistencies in namespace flags[1], and returns EINVAL when conflicting flags are informed together.

In this case, it's easier to return early when conflicting flags are informed at the beginning, so moving a namespace check to where namespaces are already being sanitized makes sense.

What doe easier mean in this context?

Quoted 3 lines

If the code stays where it is now, and a user calls clone syscalls informing CLONE_NEWIPC | CLONE_SYSVSEM, the code will need to undo a lot of work before returning the same EINVAL[2].

And why do we care about that error path? Does it trigger that often?

Do not take me wrong. I am not saying the patch is incorrect or wrong. The changelog lacks the justification and explanation _why_ do we care at all. A rarely failing path reorganization doesn't sound too interesting to me. I cannot judge the cleanup aspect of this though but from a very naive diffstat POV it doesn't look like a huge win to me.

Quoted 70 lines

[1] https://elixir.free-electrons.com/linux/latest/source/kernel/fork.c#L1552
[2] https://elixir.free-electrons.com/linux/latest/source/kernel/fork.c#L1953

Quoted 63 lines

I am not familiar with the code all that much but the justification is not clear to me. Thesea re namespace related flags so why should we pull them out of copy_namespaces. I do not see any simplifications in the error code paths or something like that.

Quoted 53 lines
---
 kernel/fork.c    | 11 +++++++++++
 kernel/nsproxy.c | 11 -----------
 2 files changed, 11 insertions(+), 11 deletions(-)
kernel/fork.c +11 -0
index 2113e252cb9d..691f9ba135fc 100644
@@ -1600,6 +1600,17 @@ static __latent_entropy struct task_struct *copy_process(
 		return ERR_PTR(-EINVAL);
 
 	/*
+	 * CLONE_NEWIPC must detach from the undolist: after switching
+	 * to a new ipc namespace, the semaphore arrays from the old
+	 * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
+	 * means share undolist with parent, so we must forbid using
+	 * it along with CLONE_NEWIPC.
+	 */
+	if ((clone_flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
+		(CLONE_NEWIPC | CLONE_SYSVSEM))
+		return ERR_PTR(-EINVAL);
+
+	/*
 	 * Thread groups must share signals as well, and detached threads
 	 * can only be started up within the thread group.
 	 */
kernel/nsproxy.c +0 -11
index f6c5d330059a..30882727dff5 100644
@@ -151,17 +151,6 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
 	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
 		return -EPERM;
 
-	/*
-	 * CLONE_NEWIPC must detach from the undolist: after switching
-	 * to a new ipc namespace, the semaphore arrays from the old
-	 * namespace are unreachable.  In clone parlance, CLONE_SYSVSEM
-	 * means share undolist with parent, so we must forbid using
-	 * it along with CLONE_NEWIPC.
-	 */
-	if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
-		(CLONE_NEWIPC | CLONE_SYSVSEM)) 
-		return -EINVAL;
-
 	new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
 	if (IS_ERR(new_ns))
 		return  PTR_ERR(new_ns);
Signature · 1 line
2.13.6
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Thanks,
	Marcos
Signature · 2 lines
Michal Hocko
SUSE Labs