Skip to contentSkip to conversation

fork: fix incorrect fput of ->exe_file causing use-after-free

PATCH Applied Eric Biggers 4 participants 8 messages Aug 23, 2017 – Nov 16, 2017

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

PATCH 1/1 fork: fix incorrect fput of ->exe_file causing use-after-free

From: Eric Biggers <[email protected]>

Commit 7c051267931a ("mm, fork: make dup_mmap wait for mmap_sem for write killable") made it possible to kill a forking task while it is waiting to acquire its ->mmap_sem for write, in dup_mmap(). However, it was overlooked that this introduced an new error path before a reference is taken on the mm_struct's ->exe_file. Since the ->exe_file of the new mm_struct was already set to the old ->exe_file by the memcpy() in dup_mm(), it was possible for the mmput() in the error path of dup_mm() to drop a reference to ->exe_file which was never taken. This caused the struct file to later be freed prematurely.

Fix it by updating mm_init() to NULL out the ->exe_file, in the same place it clears other things like the list of mmaps.

This bug was found by syzkaller. It can be reproduced using the following C program:

    #define _GNU_SOURCE
    #include <pthread.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    #include <sys/syscall.h>
    #include <sys/wait.h>
    #include <unistd.h>

    static void *mmap_thread(void *_arg)
    {
        for (;;) {
            mmap(NULL, 0x1000000, PROT_READ,
                 MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
        }
    }

    static void *fork_thread(void *_arg)
    {
        usleep(rand() % 10000);
        fork();
    }

    int main(void)
    {
        fork();
        fork();
        fork();
        for (;;) {
            if (fork() == 0) {
                pthread_t t;

                pthread_create(&t, NULL, mmap_thread, NULL);
                pthread_create(&t, NULL, fork_thread, NULL);
                usleep(rand() % 10000);
                syscall(__NR_exit_group, 0);
            }
            wait(NULL);
        }
    }

No special kernel config options are needed. It usually causes a NULL pointer dereference in __remove_shared_vm_struct() during exit, or in dup_mmap() (which is usually inlined into copy_process()) during fork. Both are due to a vm_area_struct's ->vm_file being used after it's already been freed.

Fixes 7c051267931a ("mm, fork: make dup_mmap wait for mmap_sem for write killable")

Google-Bug-Id: 64772007

Cc Dmitry Vyukov <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vlastimil Babka <[email protected]>
---
 kernel/fork.c | 1 +
 1 file changed, 1 insertion(+)
kernel/fork.c +1 -0
index e075b7780421..cbbea277b3fb 100644
@@ -806,6 +806,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_cpumask(mm);
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
+	RCU_INIT_POINTER(mm->exe_file, NULL);
 	mmu_notifier_mm_init(mm);
 	init_tlb_flush_pending(mm);
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
Signature · 7 lines
2.14.1.342.g6490525c54-goog

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 8 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Commit 7c051267931a ("mm, fork: make dup_mmap wait for mmap_sem for write killable") made it possible to kill a forking task while it is waiting to acquire its ->mmap_sem for write, in dup_mmap(). However, it was overlooked that this introduced an new error path before a reference is taken on the mm_struct's ->exe_file.

Hmm. Unless I am totally confused, the same problem with mm->exol_area? I'll recheck....

kernel/fork.c static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, · line 809

Quoted 7 lines
kernel/fork.c +1 -0
@@ -806,6 +806,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_cpumask(mm);
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
+	RCU_INIT_POINTER(mm->exe_file, NULL);

Can't we simply move

	RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));

from dup_mmap() here? Afaics this doesn't need mmap_sem.

Good catch!

Oleg.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
In reply to Eric Biggers
Quoted 20 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Commit 7c051267931a ("mm, fork: make dup_mmap wait for mmap_sem for write killable") made it possible to kill a forking task while it is waiting to acquire its ->mmap_sem for write, in dup_mmap(). However, it was overlooked that this introduced an new error path before a reference is taken on the mm_struct's ->exe_file. Since the ->exe_file of the new mm_struct was already set to the old ->exe_file by the memcpy() in dup_mm(), it was possible for the mmput() in the error path of dup_mm() to drop a reference to ->exe_file which was never taken. This caused the struct file to later be freed prematurely.

Fix it by updating mm_init() to NULL out the ->exe_file, in the same place it clears other things like the list of mmaps.

kernel/fork.c +0 -0
index e075b7780421..cbbea277b3fb 100644

kernel/fork.c static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, · line 812

Quoted 7 lines
kernel/fork.c +1 -0
@@ -806,6 +806,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_cpumask(mm);
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
+	RCU_INIT_POINTER(mm->exe_file, NULL);
 	mmu_notifier_mm_init(mm);
 	init_tlb_flush_pending(mm);
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS

I've been seeing similar issues on arm64 with use-after-free of a file and other memory corruption [1].

This patch seems to fix that; a test that normally fired in a few minutes has been happily running for hours with this applied.

Thanks,
Mark.

[1] https://lkml.kernel.org/r/20170824113743.GA14737@leverpostej

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
In reply to Oleg Nesterov
Quoted 4 lines · Oleg Nesterov
Quoted 3 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Quoted 8 lines
Quoted 5 lines

Commit 7c051267931a ("mm, fork: make dup_mmap wait for mmap_sem for write killable") made it possible to kill a forking task while it is waiting to acquire its ->mmap_sem for write, in dup_mmap(). However, it was overlooked that this introduced an new error path before a reference is taken on the mm_struct's ->exe_file.

Hmm. Unless I am totally confused, the same problem with mm->exol_area? I'll recheck....

I'm not sure what you mean by ->exol_area.

Quoted 11 lines

kernel/fork.c static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, · line 809

Quoted 8 lines
kernel/fork.c +1 -0
@@ -806,6 +806,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_cpumask(mm);
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
+	RCU_INIT_POINTER(mm->exe_file, NULL);

Can't we simply move

Quoted 4 lines
	RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));

from dup_mmap() here? Afaics this doesn't need mmap_sem.

Two problems, even assuming that get_mm_exe_file() doesn't require mmap_sem:

- If mm_alloc_pgd() or init_new_context() in mm_init() fails, mm_init() doesn't do the full mmput(), so the file reference would not be dropped. So it would need to be changed to drop the file reference too.

- The file would also be set when called from mm_alloc() which is used when exec'ing a new task. *Maybe* it would be safe to do temporarily, but it's pointless because ->exe_file will be set later by flush_old_exec().

Eric

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Tested-by
#6 lore raw
In reply to Mark Rutland
Quoted 32 lines · Mark Rutland
Quoted 20 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Commit 7c051267931a ("mm, fork: make dup_mmap wait for mmap_sem for write killable") made it possible to kill a forking task while it is waiting to acquire its ->mmap_sem for write, in dup_mmap(). However, it was overlooked that this introduced an new error path before a reference is taken on the mm_struct's ->exe_file. Since the ->exe_file of the new mm_struct was already set to the old ->exe_file by the memcpy() in dup_mm(), it was possible for the mmput() in the error path of dup_mm() to drop a reference to ->exe_file which was never taken. This caused the struct file to later be freed prematurely.

Fix it by updating mm_init() to NULL out the ->exe_file, in the same place it clears other things like the list of mmaps.

kernel/fork.c +0 -0
index e075b7780421..cbbea277b3fb 100644

kernel/fork.c static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, · line 812

Quoted 7 lines
kernel/fork.c +1 -0
@@ -806,6 +806,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_cpumask(mm);
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
+	RCU_INIT_POINTER(mm->exe_file, NULL);
 	mmu_notifier_mm_init(mm);
 	init_tlb_flush_pending(mm);
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS

I've been seeing similar issues on arm64 with use-after-free of a file and other memory corruption [1].

Quoted 2 lines

This patch seems to fix that; a test that normally fired in a few minutes has been happily running for hours with this applied.

Those haven't triggered after 24 hours, and in 16+ hours of fuzzing with this applied, I haven't seen new issues. FWIW:

Thanks,
Mark.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
In reply to Eric Biggers
Quoted 12 lines · Eric Biggers
Quoted 10 lines · Oleg Nesterov
Quoted 8 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Commit 7c051267931a ("mm, fork: make dup_mmap wait for mmap_sem for write killable") made it possible to kill a forking task while it is waiting to acquire its ->mmap_sem for write, in dup_mmap(). However, it was overlooked that this introduced an new error path before a reference is taken on the mm_struct's ->exe_file.

Quoted 4 lines
Quoted 2 lines

Hmm. Unless I am totally confused, the same problem with mm->exol_area? I'll recheck....

I'm not sure what you mean by ->exol_area.

I meant mm->uprobes_state.xol_area, sorry

Quoted 12 lines
Quoted 12 lines

kernel/fork.c static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, · line 809

Quoted 7 lines
kernel/fork.c +1 -0
@@ -806,6 +806,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_cpumask(mm);
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
+	RCU_INIT_POINTER(mm->exe_file, NULL);

Can't we simply move

	RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
Quoted 8 lines
Quoted 2 lines

from dup_mmap() here? Afaics this doesn't need mmap_sem.

Two problems, even assuming that get_mm_exe_file() doesn't require mmap_sem:

- If mm_alloc_pgd() or init_new_context() in mm_init() fails, mm_init() doesn't do the full mmput(), so the file reference would not be dropped. So it would need to be changed to drop the file reference too.

Ah yes, I forgot that mm_init() can fail after that, thanks for correcting me.

Oleg.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 13 lines · Oleg Nesterov
Quoted 12 lines · Eric Biggers
Quoted 10 lines · Oleg Nesterov
Quoted 8 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Commit 7c051267931a ("mm, fork: make dup_mmap wait for mmap_sem for write killable") made it possible to kill a forking task while it is waiting to acquire its ->mmap_sem for write, in dup_mmap(). However, it was overlooked that this introduced an new error path before a reference is taken on the mm_struct's ->exe_file.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Hmm. Unless I am totally confused, the same problem with mm->exol_area? I'll recheck....

I'm not sure what you mean by ->exol_area.

I meant mm->uprobes_state.xol_area, sorry

Yep, that's a bug too. I was able to cause a use-after-free using the same reproducer program I gave in my commit message, after setting a uprobe tracepoint on the beginning of the fork_thread() function. I'll send a patch to fix it when I have a chance.

Eric

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw
In reply to Eric Biggers

Hey Eric,

This is a pretty late response to the thread, but in case you're curious, since Ubuntu forgot to backport this (I already emailed them about it), I actually experienced a set of boxes that hit this bug in the wild and were crashing every 2 weeks or so, when under highload. It's pretty amazing that syzkaller unearthed this, resulting in a nice test case, making it possible to debug and fix the error. Otherwise it'd be a real heisenbug.

So, thanks.

Jason

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>