Skip to content

Commit 12c641a

Browse files
committed
unshare: Unsharing a thread does not require unsharing a vm
In the logic in the initial commit of unshare made creating a new thread group for a process, contingent upon creating a new memory address space for that process. That is wrong. Two separate processes in different thread groups can share a memory address space and clone allows creation of such proceses. This is significant because it was observed that mm_users > 1 does not mean that a process is multi-threaded, as reading /proc/PID/maps temporarily increments mm_users, which allows other processes to (accidentally) interfere with unshare() calls. Correct the check in check_unshare_flags() to test for !thread_group_empty() for CLONE_THREAD, CLONE_SIGHAND, and CLONE_VM. For sighand->count > 1 for CLONE_SIGHAND and CLONE_VM. For !current_is_single_threaded instead of mm_users > 1 for CLONE_VM. By using the correct checks in unshare this removes the possibility of an accidental denial of service attack. Additionally using the correct checks in unshare ensures that only an explicit unshare(CLONE_VM) can possibly trigger the slow path of current_is_single_threaded(). As an explict unshare(CLONE_VM) is pointless it is not expected there are many applications that make that call. Cc: [email protected] Fixes: b2e0d98 userns: Implement unshare of the user namespace Reported-by: Ricky Zhou <[email protected]> Reported-by: Kees Cook <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent 75509fd commit 12c641a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

kernel/fork.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,13 +1866,21 @@ static int check_unshare_flags(unsigned long unshare_flags)
18661866
CLONE_NEWUSER|CLONE_NEWPID))
18671867
return -EINVAL;
18681868
/*
1869-
* Not implemented, but pretend it works if there is nothing to
1870-
* unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND
1871-
* needs to unshare vm.
1869+
* Not implemented, but pretend it works if there is nothing
1870+
* to unshare. Note that unsharing the address space or the
1871+
* signal handlers also need to unshare the signal queues (aka
1872+
* CLONE_THREAD).
18721873
*/
18731874
if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
1874-
/* FIXME: get_task_mm() increments ->mm_users */
1875-
if (atomic_read(&current->mm->mm_users) > 1)
1875+
if (!thread_group_empty(current))
1876+
return -EINVAL;
1877+
}
1878+
if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {
1879+
if (atomic_read(&current->sighand->count) > 1)
1880+
return -EINVAL;
1881+
}
1882+
if (unshare_flags & CLONE_VM) {
1883+
if (!current_is_single_threaded())
18761884
return -EINVAL;
18771885
}
18781886

@@ -1940,16 +1948,16 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
19401948
*/
19411949
if (unshare_flags & CLONE_NEWUSER)
19421950
unshare_flags |= CLONE_THREAD | CLONE_FS;
1943-
/*
1944-
* If unsharing a thread from a thread group, must also unshare vm.
1945-
*/
1946-
if (unshare_flags & CLONE_THREAD)
1947-
unshare_flags |= CLONE_VM;
19481951
/*
19491952
* If unsharing vm, must also unshare signal handlers.
19501953
*/
19511954
if (unshare_flags & CLONE_VM)
19521955
unshare_flags |= CLONE_SIGHAND;
1956+
/*
1957+
* If unsharing a signal handlers, must also unshare the signal queues.
1958+
*/
1959+
if (unshare_flags & CLONE_SIGHAND)
1960+
unshare_flags |= CLONE_THREAD;
19531961
/*
19541962
* If unsharing namespace, must also unshare filesystem information.
19551963
*/

0 commit comments

Comments
 (0)