Skip to content

Commit

Permalink
Take mmap lock in cacheflush syscall
Browse files Browse the repository at this point in the history
[ Upstream commit c26958c ]

We need to take the mmap lock around find_vma() and subsequent use of the
VMA. Otherwise, we can race with concurrent operations like munmap(), which
can lead to use-after-free accesses to freed VMAs.

Fixes: 1000197 ("nios2: System calls handling")
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Ley Foon Tan <ley.foon.tan@intel.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
thejh authored and gregkh committed Mar 4, 2021
1 parent 5917fdc commit 9dfb367
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions arch/nios2/kernel/sys_nios2.c
Expand Up @@ -22,6 +22,7 @@ asmlinkage int sys_cacheflush(unsigned long addr, unsigned long len,
unsigned int op)
{
struct vm_area_struct *vma;
struct mm_struct *mm = current->mm;

if (len == 0)
return 0;
Expand All @@ -34,16 +35,22 @@ asmlinkage int sys_cacheflush(unsigned long addr, unsigned long len,
if (addr + len < addr)
return -EFAULT;

if (mmap_read_lock_killable(mm))
return -EINTR;

/*
* Verify that the specified address region actually belongs
* to this process.
*/
vma = find_vma(current->mm, addr);
if (vma == NULL || addr < vma->vm_start || addr + len > vma->vm_end)
vma = find_vma(mm, addr);
if (vma == NULL || addr < vma->vm_start || addr + len > vma->vm_end) {
mmap_read_unlock(mm);
return -EFAULT;
}

flush_cache_range(vma, addr, addr + len);

mmap_read_unlock(mm);
return 0;
}

Expand Down

0 comments on commit 9dfb367

Please sign in to comment.