Skip to contentSkip to conversation

fix sys_unshare()+SEM_UNDO: add support for CLONE_SYSVSEM

PATCH In review Manfred Spraul 3 participants 8 messages Apr 13, 2008 – Apr 14, 2008

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

PATCH 1/2 fix sys_unshare()+SEM_UNDO: add support for CLONE_SYSVSEM

sys_unshare(CLONE_NEWIPC) doesn't handle the undo lists properly, this can cause a kernel memory corruption. CLONE_NEWIPC must detach from the existing undo lists.
Fix, part 1: add support for sys_unshare(CLONE_SYSVSEM)

I'm not sure who's the right maintainer, Andrew, could you merge it?

---
 ipc/sem.c     |    1 +
 kernel/fork.c |   18 ++++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
ipc/sem.c +1 -0
index 0b45a4d..35841bd 100644
@@ -1298,6 +1298,7 @@ void exit_sem(struct task_struct *tsk)
 	undo_list = tsk->sysvsem.undo_list;
 	if (!undo_list)
 		return;
+	tsk->sysvsem.undo_list = NULL;
 
 	if (!atomic_dec_and_test(&undo_list->refcnt))
 		return;
kernel/fork.c +14 -4
index 9c042f9..7f242b0 100644
@@ -1675,13 +1675,17 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp
 }
 
 /*
- * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
- * supported yet
+ * Unsharing of semundo for tasks created with CLONE_SYSVSEM doesn't require
+ * any allocations: it means that the task leaves the existing undo lists,
+ * just like sys_exit(). The new undo lists are allocated on demand in the
+ * ipc syscalls.
+ * new_ulistp is set to a non-NULL value, the caller expects that on success.
  */
 static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp)
 {
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
+	if (unshare_flags & CLONE_SYSVSEM) {
+		*new_ulistp = (void*)1;
+	}
 
 	return 0;
 }
@@ -1731,6 +1735,12 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 		goto bad_unshare_cleanup_semundo;
 
 	if (new_fs ||  new_mm || new_fd || new_ulist || new_nsproxy) {
+		if (unshare_flags & CLONE_SYSVSEM) {
+			/*
+			 * CLONE_SYSVSEM is equivalent to sys_exit().
+			 */
+			exit_sem(current);
+		}
 
 		if (new_nsproxy) {
 			switch_task_namespaces(current, new_nsproxy);
Signature · 1 line
1.5.4.1
maintainer
#1 lore raw
Quoted 5 lines · On Sun, 13 Apr 2008 10:04:17 +0200 Manfred Spraul <[email protected]>…

sys_unshare(CLONE_NEWIPC) doesn't handle the undo lists properly, this can cause a kernel memory corruption. CLONE_NEWIPC must detach from the existing undo lists.
Fix, part 1: add support for sys_unshare(CLONE_SYSVSEM)

Is this a non-back-compatible change?

Quoted 32 lines
---
 ipc/sem.c     |    1 +
 kernel/fork.c |   18 ++++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
ipc/sem.c +1 -0
index 0b45a4d..35841bd 100644
@@ -1298,6 +1298,7 @@ void exit_sem(struct task_struct *tsk)
 	undo_list = tsk->sysvsem.undo_list;
 	if (!undo_list)
 		return;
+	tsk->sysvsem.undo_list = NULL;
 
 	if (!atomic_dec_and_test(&undo_list->refcnt))
 		return;
kernel/fork.c +3 -2
index 9c042f9..7f242b0 100644
@@ -1675,13 +1675,17 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp
 }
 
 /*
- * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
- * supported yet
+ * Unsharing of semundo for tasks created with CLONE_SYSVSEM doesn't require
+ * any allocations: it means that the task leaves the existing undo lists,
+ * just like sys_exit(). The new undo lists are allocated on demand in the

kernel/fork.c static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp · line 1688

Quoted 10 lines
kernel/fork.c +5 -2
+ * ipc syscalls.
+ * new_ulistp is set to a non-NULL value, the caller expects that on success.
  */
 static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp)
 {
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
+	if (unshare_flags & CLONE_SYSVSEM) {
+		*new_ulistp = (void*)1;
+	}

And can we do anything nicer than this?

Quoted 17 lines
kernel/fork.c +6 -0
 	return 0;
 }
@@ -1731,6 +1735,12 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 		goto bad_unshare_cleanup_semundo;
 
 	if (new_fs ||  new_mm || new_fd || new_ulist || new_nsproxy) {
+		if (unshare_flags & CLONE_SYSVSEM) {
+			/*
+			 * CLONE_SYSVSEM is equivalent to sys_exit().
+			 */
+			exit_sem(current);
+		}
 
 		if (new_nsproxy) {
 			switch_task_namespaces(current, new_nsproxy);
Signature · 1 line
1.5.4.1
#2 lore raw
Quoted 13 lines · Andrew Morton
Quoted 6 lines · On Sun, 13 Apr 2008 10:04:17 +0200 Manfred Spraul <[email protected]>…

sys_unshare(CLONE_NEWIPC) doesn't handle the undo lists properly, this can cause a kernel memory corruption. CLONE_NEWIPC must detach from the existing undo lists.
Fix, part 1: add support for sys_unshare(CLONE_SYSVSEM)

Is this a non-back-compatible change?

It adds a new feature - previously sys_unshare(CLONE_SYSVSEM) returned -EINVAL.

Quoted 36 lines
Quoted 36 lines
---
 ipc/sem.c     |    1 +
 kernel/fork.c |   18 ++++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
ipc/sem.c +1 -0
index 0b45a4d..35841bd 100644
@@ -1298,6 +1298,7 @@ void exit_sem(struct task_struct *tsk)
 	undo_list = tsk->sysvsem.undo_list;
 	if (!undo_list)
 		return;
+	tsk->sysvsem.undo_list = NULL;
 
 	if (!atomic_dec_and_test(&undo_list->refcnt))
 		return;
kernel/fork.c +5 -2
index 9c042f9..7f242b0 100644
@@ -1675,13 +1675,17 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp
 }
 
 /*
- * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
- * supported yet
+ * Unsharing of semundo for tasks created with CLONE_SYSVSEM doesn't require
+ * any allocations: it means that the task leaves the existing undo lists,
+ * just like sys_exit(). The new undo lists are allocated on demand in the
+ * ipc syscalls.
+ * new_ulistp is set to a non-NULL value, the caller expects that on success.
  */
 static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp)
 {
Quoted 10 lines

kernel/fork.c static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp · line 1689

Quoted 6 lines
kernel/fork.c +3 -2
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
+	if (unshare_flags & CLONE_SYSVSEM) {
+		*new_ulistp = (void*)1;
+	}
 

And can we do anything nicer than this?

Attached is an alternative. If you prefer it, I'll send another patch set.

--
Manfred

ipc/sem.c +1 -0
index 0b45a4d..35841bd 100644
@@ -1298,6 +1298,7 @@ void exit_sem(struct task_struct *tsk)
 	undo_list = tsk->sysvsem.undo_list;
 	if (!undo_list)
 		return;
+	tsk->sysvsem.undo_list = NULL;
 
 	if (!atomic_dec_and_test(&undo_list->refcnt))
 		return;
kernel/fork.c +8 -18
index 9c042f9..535aa92 100644
@@ -1675,18 +1675,6 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp
 }
 
 /*
- * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
- * supported yet
- */
-static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp)
-{
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
-
-	return 0;
-}
-
-/*
  * unshare allows a process to 'unshare' part of the process
  * context which was originally shared using clone.  copy_*
  * functions used by do_fork() cannot be used here directly
@@ -1701,7 +1689,6 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 	struct sighand_struct *new_sigh = NULL;
 	struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
 	struct files_struct *fd, *new_fd = NULL;
-	struct sem_undo_list *new_ulist = NULL;
 	struct nsproxy *new_nsproxy = NULL;
 
 	check_unshare_flags(&unshare_flags);
@@ -1724,13 +1711,17 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 		goto bad_unshare_cleanup_sigh;
 	if ((err = unshare_fd(unshare_flags, &new_fd)))
 		goto bad_unshare_cleanup_vm;
-	if ((err = unshare_semundo(unshare_flags, &new_ulist)))
-		goto bad_unshare_cleanup_fd;
 	if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
 			new_fs)))
-		goto bad_unshare_cleanup_semundo;
+		goto bad_unshare_cleanup_fd;
 
-	if (new_fs ||  new_mm || new_fd || new_ulist || new_nsproxy) {
+	if (new_fs ||  new_mm || new_fd || (unshare_flags & CLONE_SYSVSEM) || new_nsproxy) {
+		if (unshare_flags & CLONE_SYSVSEM) {
+			/*
+			 * CLONE_SYSVSEM is equivalent to sys_exit().
+			 */
+			exit_sem(current);
+		}
 
 		if (new_nsproxy) {
 			switch_task_namespaces(current, new_nsproxy);
@@ -1766,7 +1757,6 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 	if (new_nsproxy)
 		put_nsproxy(new_nsproxy);
 
-bad_unshare_cleanup_semundo:
 bad_unshare_cleanup_fd:
 	if (new_fd)
 		put_files_struct(new_fd);
maintainer
#3 lore raw
Quoted 9 lines · On Sun, 13 Apr 2008 13:36:24 +0200 Manfred Spraul <[email protected]>…
Quoted 8 lines

kernel/fork.c asmlinkage long sys_unshare(unsigned long unshare_flags) · line 1724

Quoted 4 lines
kernel/fork.c +3 -0
+	if (unshare_flags & CLONE_SYSVSEM) {
+		*new_ulistp = (void*)1;
+	}

And can we do anything nicer than this?

Attached is an alternative. If you prefer it, I'll send another patch set.

Well I suppose we can fiddle with this sort of thing later. Right now the major concern is the testedness and reviewedness of these changes?

#4 lore raw
In reply to Manfred Spraul
Quoted 70 lines · Manfred Spraul
Quoted 13 lines · Andrew Morton
Quoted 8 lines · On Sun, 13 Apr 2008 10:04:17 +0200 Manfred Spraul <[email protected]>…

sys_unshare(CLONE_NEWIPC) doesn't handle the undo lists properly, this can
cause a kernel memory corruption. CLONE_NEWIPC must detach from the existing
undo lists.
Fix, part 1: add support for sys_unshare(CLONE_SYSVSEM)

Quoted 3 lines

Is this a non-back-compatible change?

It adds a new feature - previously sys_unshare(CLONE_SYSVSEM) returned -EINVAL.

Quoted 40 lines
Quoted 40 lines
---
 ipc/sem.c     |    1 +
 kernel/fork.c |   18 ++++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
ipc/sem.c +1 -0
index 0b45a4d..35841bd 100644
@@ -1298,6 +1298,7 @@ void exit_sem(struct task_struct *tsk)
 	undo_list = tsk->sysvsem.undo_list;
 	if (!undo_list)
 		return;
+	tsk->sysvsem.undo_list = NULL;
  	if (!atomic_dec_and_test(&undo_list->refcnt))
 		return;
kernel/fork.c +0 -0
index 9c042f9..7f242b0 100644
@@ -1675,13 +1675,17 @@ static int unshare_fd(unsigned long 

unshare_flags, struct files_struct **new_fdp
}
/*
- * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
- * supported yet
+ * Unsharing of semundo for tasks created with CLONE_SYSVSEM doesn't require
+ * any allocations: it means that the task leaves the existing undo lists,
+ * just like sys_exit(). The new undo lists are allocated on demand in the
+ * ipc syscalls.
+ * new_ulistp is set to a non-NULL value, the caller expects that on success.
*/
static int unshare_semundo(unsigned long unshare_flags, struct
sem_undo_list **new_ulistp)
{

Quoted 10 lines

kernel/fork.c

Quoted 6 lines
kernel/fork.c +3 -2
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
+	if (unshare_flags & CLONE_SYSVSEM) {
+		*new_ulistp = (void*)1;
+	}

And can we do anything nicer than this?

Quoted 1 line

Attached is an alternative. If you prefer it, I'll send another patch set.

FWIW I definately far far prefer this version :)

As for 'maintainers', in the end wrt this code I'd defer to any two of Pavel, Nadia, and Pierre, each of who I've seen do a great deal of digging around this code.

(I think I saw some set of these go into -mm so I guess I'll just grab mmotm and test with that a bit later today)

thanks,
-serge

Quoted 76 lines

--
Manfred

ipc/sem.c +1 -0
index 0b45a4d..35841bd 100644
@@ -1298,6 +1298,7 @@ void exit_sem(struct task_struct *tsk)
 	undo_list = tsk->sysvsem.undo_list;
 	if (!undo_list)
 		return;
+	tsk->sysvsem.undo_list = NULL;
 
 	if (!atomic_dec_and_test(&undo_list->refcnt))
 		return;
kernel/fork.c +8 -18
index 9c042f9..535aa92 100644
@@ -1675,18 +1675,6 @@ static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp
 }
 
 /*
- * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
- * supported yet
- */
-static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp)
-{
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
-
-	return 0;
-}
-
-/*
  * unshare allows a process to 'unshare' part of the process
  * context which was originally shared using clone.  copy_*
  * functions used by do_fork() cannot be used here directly
@@ -1701,7 +1689,6 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 	struct sighand_struct *new_sigh = NULL;
 	struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
 	struct files_struct *fd, *new_fd = NULL;
-	struct sem_undo_list *new_ulist = NULL;
 	struct nsproxy *new_nsproxy = NULL;
 
 	check_unshare_flags(&unshare_flags);
@@ -1724,13 +1711,17 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 		goto bad_unshare_cleanup_sigh;
 	if ((err = unshare_fd(unshare_flags, &new_fd)))
 		goto bad_unshare_cleanup_vm;
-	if ((err = unshare_semundo(unshare_flags, &new_ulist)))
-		goto bad_unshare_cleanup_fd;
 	if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
 			new_fs)))
-		goto bad_unshare_cleanup_semundo;
+		goto bad_unshare_cleanup_fd;
 
-	if (new_fs ||  new_mm || new_fd || new_ulist || new_nsproxy) {
+	if (new_fs ||  new_mm || new_fd || (unshare_flags & CLONE_SYSVSEM) || new_nsproxy) {
+		if (unshare_flags & CLONE_SYSVSEM) {
+			/*
+			 * CLONE_SYSVSEM is equivalent to sys_exit().
+			 */
+			exit_sem(current);
+		}
 
 		if (new_nsproxy) {
 			switch_task_namespaces(current, new_nsproxy);
@@ -1766,7 +1757,6 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 	if (new_nsproxy)
 		put_nsproxy(new_nsproxy);
 
-bad_unshare_cleanup_semundo:
 bad_unshare_cleanup_fd:
 	if (new_fd)
 		put_files_struct(new_fd);
maintainer
#5 lore raw
Quoted 17 lines · On Mon, 14 Apr 2008 09:58:40 -0500 "Serge E. Hallyn" <[email protected]> wrote:
Quoted 16 lines · Manfred Spraul
Quoted 15 lines · Andrew Morton

Review

Quoted 8 lines · On Sun, 13 Apr 2008 10:04:17 +0200 Manfred Spraul <[email protected]>…

sem_undo_list **new_ulistp)
{

(quoted diff) +3 -2
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
+	if (unshare_flags & CLONE_SYSVSEM) {
+		*new_ulistp = (void*)1;
+	}

And can we do anything nicer than this?

Quoted 3 lines
Quoted 1 line

Attached is an alternative. If you prefer it, I'll send another patch set.

FWIW I definately far far prefer this version :)

Oh, OK.

I guess I'll drop what I have. Manfred, can we please have a new patchset?

We might end up slipping this back to 2.6.25.1. Would that be a bad thing?

#6 lore raw
Quoted 22 lines · Andrew Morton
Quoted 19 lines · On Mon, 14 Apr 2008 09:58:40 -0500 "Serge E. Hallyn" <[email protected]> wrote:
Quoted 17 lines · Manfred Spraul
Quoted 15 lines · Andrew Morton

Review

Quoted 8 lines · On Sun, 13 Apr 2008 10:04:17 +0200 Manfred Spraul <[email protected]>…

sem_undo_list **new_ulistp)
{

(quoted diff) +3 -2
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
+	if (unshare_flags & CLONE_SYSVSEM) {
+		*new_ulistp = (void*)1;
+	}

And can we do anything nicer than this?

Attached is an alternative. If you prefer it, I'll send another patch set.

Quoted 7 lines
Quoted 1 line

FWIW I definately far far prefer this version :)

Oh, OK.

I guess I'll drop what I have. Manfred, can we please have a new patchset?

We might end up slipping this back to 2.6.25.1. Would that be a bad thing?

It'd be unfortunate, but as the existing patch doesn't completely fix the problem it's probably best to wait anyway.

Manfred, I don't want to step on your toes, but please do let me know if you don't have time to do the next version. If you do have time, then thank you again for spotting+fixing the problem.

thanks,
-serge

#7 lore raw
In reply to Serge E. Hallyn
Quoted 79 lines · Serge E. Hallyn
Quoted 70 lines · Manfred Spraul
Quoted 13 lines · Andrew Morton
Quoted 8 lines · On Sun, 13 Apr 2008 10:04:17 +0200 Manfred Spraul <[email protected]>…

sys_unshare(CLONE_NEWIPC) doesn't handle the undo lists properly, this can
cause a kernel memory corruption. CLONE_NEWIPC must detach from the existing
undo lists.
Fix, part 1: add support for sys_unshare(CLONE_SYSVSEM)

Quoted 3 lines

Is this a non-back-compatible change?

It adds a new feature - previously sys_unshare(CLONE_SYSVSEM) returned -EINVAL.

Quoted 40 lines
Quoted 40 lines
---
 ipc/sem.c     |    1 +
 kernel/fork.c |   18 ++++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
ipc/sem.c +1 -0
index 0b45a4d..35841bd 100644
@@ -1298,6 +1298,7 @@ void exit_sem(struct task_struct *tsk)
 	undo_list = tsk->sysvsem.undo_list;
 	if (!undo_list)
 		return;
+	tsk->sysvsem.undo_list = NULL;
  	if (!atomic_dec_and_test(&undo_list->refcnt))
 		return;
kernel/fork.c +0 -0
index 9c042f9..7f242b0 100644
@@ -1675,13 +1675,17 @@ static int unshare_fd(unsigned long 

unshare_flags, struct files_struct **new_fdp
}
/*
- * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
- * supported yet
+ * Unsharing of semundo for tasks created with CLONE_SYSVSEM doesn't require
+ * any allocations: it means that the task leaves the existing undo lists,
+ * just like sys_exit(). The new undo lists are allocated on demand in the
+ * ipc syscalls.
+ * new_ulistp is set to a non-NULL value, the caller expects that on success.
*/
static int unshare_semundo(unsigned long unshare_flags, struct
sem_undo_list **new_ulistp)
{

Quoted 10 lines

kernel/fork.c

Quoted 6 lines
kernel/fork.c +3 -2
-	if (unshare_flags & CLONE_SYSVSEM)
-		return -EINVAL;
+	if (unshare_flags & CLONE_SYSVSEM) {
+		*new_ulistp = (void*)1;
+	}

And can we do anything nicer than this?

Quoted 1 line

Attached is an alternative. If you prefer it, I'll send another patch set.

FWIW I definately far far prefer this version :)

As for 'maintainers', in the end wrt this code I'd defer to any two of Pavel, Nadia, and Pierre, each of who I've seen do a great deal of digging around this code.

Quoted 5 lines

(I think I saw some set of these go into -mm so I guess I'll just grab mmotm and test with that a bit later today)

thanks,
-serge

Of course if we do go this route, then the unshare(2) manpage will need an update (so Michael Kerrisk cc:d).

thanks,
-serge