Commit 1c1a902a authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Khalid Elmously

userfaultfd_release: always remove uffd flags and clear vm_userfaultfd_ctx

BugLink: https://bugs.launchpad.net/bugs/1845036

commit 46d0b24c upstream.

userfaultfd_release() should clear vm_flags/vm_userfaultfd_ctx even if
mm->core_state != NULL.

Otherwise a page fault can see userfaultfd_missing() == T and use an
already freed userfaultfd_ctx.

Link: http://lkml.kernel.org/r/20190820160237.GB4983@redhat.com
Fixes: 04f5866e ("coredump: fix race condition between mmget_not_zero()/get_task_mm() and core dumping")
Signed-off-by: default avatarOleg Nesterov <oleg@redhat.com>
Reported-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
Reviewed-by: default avatarAndrea Arcangeli <aarcange@redhat.com>
Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Mike Rapoport <rppt@linux.ibm.com>
Cc: Jann Horn <jannh@google.com>
Cc: Jason Gunthorpe <jgg@mellanox.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarConnor Kuehl <connor.kuehl@canonical.com>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
parent 52fac741
...@@ -431,6 +431,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file) ...@@ -431,6 +431,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
/* len == 0 means wake all */ /* len == 0 means wake all */
struct userfaultfd_wake_range range = { .len = 0, }; struct userfaultfd_wake_range range = { .len = 0, };
unsigned long new_flags; unsigned long new_flags;
bool still_valid;
ACCESS_ONCE(ctx->released) = true; ACCESS_ONCE(ctx->released) = true;
...@@ -446,8 +447,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file) ...@@ -446,8 +447,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
* taking the mmap_sem for writing. * taking the mmap_sem for writing.
*/ */
down_write(&mm->mmap_sem); down_write(&mm->mmap_sem);
if (!mmget_still_valid(mm)) still_valid = mmget_still_valid(mm);
goto skip_mm;
prev = NULL; prev = NULL;
for (vma = mm->mmap; vma; vma = vma->vm_next) { for (vma = mm->mmap; vma; vma = vma->vm_next) {
cond_resched(); cond_resched();
...@@ -458,6 +458,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file) ...@@ -458,6 +458,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
continue; continue;
} }
new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP); new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
if (still_valid) {
prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end, prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
new_flags, vma->anon_vma, new_flags, vma->anon_vma,
vma->vm_file, vma->vm_pgoff, vma->vm_file, vma->vm_pgoff,
...@@ -467,10 +468,10 @@ static int userfaultfd_release(struct inode *inode, struct file *file) ...@@ -467,10 +468,10 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
vma = prev; vma = prev;
else else
prev = vma; prev = vma;
}
vma->vm_flags = new_flags; vma->vm_flags = new_flags;
vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
} }
skip_mm:
up_write(&mm->mmap_sem); up_write(&mm->mmap_sem);
mmput(mm); mmput(mm);
wakeup: wakeup:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment