Skip to content

Commit 58a039e

Browse files
kirylakpm00
authored andcommitted
mm: split critical region in remap_file_pages() and invoke LSMs in between
Commit ea7e2d5 ("mm: call the security_mmap_file() LSM hook in remap_file_pages()") fixed a security issue, it added an LSM check when trying to remap file pages, so that LSMs have the opportunity to evaluate such action like for other memory operations such as mmap() and mprotect(). However, that commit called security_mmap_file() inside the mmap_lock lock, while the other calls do it before taking the lock, after commit 8b3ec68 ("take security_mmap_file() outside of ->mmap_sem"). This caused lock inversion issue with IMA which was taking the mmap_lock and i_mutex lock in the opposite way when the remap_file_pages() system call was called. Solve the issue by splitting the critical region in remap_file_pages() in two regions: the first takes a read lock of mmap_lock, retrieves the VMA and the file descriptor associated, and calculates the 'prot' and 'flags' variables; the second takes a write lock on mmap_lock, checks that the VMA flags and the VMA file descriptor are the same as the ones obtained in the first critical region (otherwise the system call fails), and calls do_mmap(). In between, after releasing the read lock and before taking the write lock, call security_mmap_file(), and solve the lock inversion issue. Link: https://lkml.kernel.org/r/[email protected] Fixes: ea7e2d5 ("mm: call the security_mmap_file() LSM hook in remap_file_pages()") Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Roberto Sassu <[email protected]> Reported-by: [email protected] Closes: https://lore.kernel.org/linux-security-module/[email protected]/ Tested-by: Roberto Sassu <[email protected]> Reviewed-by: Roberto Sassu <[email protected]> Reviewed-by: Jann Horn <[email protected]> Reviewed-by: Lorenzo Stoakes <[email protected]> Reviewed-by: Liam R. Howlett <[email protected]> Reviewed-by: Paul Moore <[email protected]> Tested-by: [email protected] Cc: Jarkko Sakkinen <[email protected]> Cc: Dmitry Kasatkin <[email protected]> Cc: Eric Snowberg <[email protected]> Cc: James Morris <[email protected]> Cc: Mimi Zohar <[email protected]> Cc: "Serge E. Hallyn" <[email protected]> Cc: Shu Han <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent f2330b6 commit 58a039e

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

mm/mmap.c

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
16421642
unsigned long populate = 0;
16431643
unsigned long ret = -EINVAL;
16441644
struct file *file;
1645+
vm_flags_t vm_flags;
16451646

16461647
pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
16471648
current->comm, current->pid);
@@ -1658,12 +1659,60 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
16581659
if (pgoff + (size >> PAGE_SHIFT) < pgoff)
16591660
return ret;
16601661

1661-
if (mmap_write_lock_killable(mm))
1662+
if (mmap_read_lock_killable(mm))
16621663
return -EINTR;
16631664

1665+
/*
1666+
* Look up VMA under read lock first so we can perform the security
1667+
* without holding locks (which can be problematic). We reacquire a
1668+
* write lock later and check nothing changed underneath us.
1669+
*/
16641670
vma = vma_lookup(mm, start);
16651671

1666-
if (!vma || !(vma->vm_flags & VM_SHARED))
1672+
if (!vma || !(vma->vm_flags & VM_SHARED)) {
1673+
mmap_read_unlock(mm);
1674+
return -EINVAL;
1675+
}
1676+
1677+
prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
1678+
prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
1679+
prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
1680+
1681+
flags &= MAP_NONBLOCK;
1682+
flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
1683+
if (vma->vm_flags & VM_LOCKED)
1684+
flags |= MAP_LOCKED;
1685+
1686+
/* Save vm_flags used to calculate prot and flags, and recheck later. */
1687+
vm_flags = vma->vm_flags;
1688+
file = get_file(vma->vm_file);
1689+
1690+
mmap_read_unlock(mm);
1691+
1692+
/* Call outside mmap_lock to be consistent with other callers. */
1693+
ret = security_mmap_file(file, prot, flags);
1694+
if (ret) {
1695+
fput(file);
1696+
return ret;
1697+
}
1698+
1699+
ret = -EINVAL;
1700+
1701+
/* OK security check passed, take write lock + let it rip. */
1702+
if (mmap_write_lock_killable(mm)) {
1703+
fput(file);
1704+
return -EINTR;
1705+
}
1706+
1707+
vma = vma_lookup(mm, start);
1708+
1709+
if (!vma)
1710+
goto out;
1711+
1712+
/* Make sure things didn't change under us. */
1713+
if (vma->vm_flags != vm_flags)
1714+
goto out;
1715+
if (vma->vm_file != file)
16671716
goto out;
16681717

16691718
if (start + size > vma->vm_end) {
@@ -1691,25 +1740,11 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
16911740
goto out;
16921741
}
16931742

1694-
prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
1695-
prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
1696-
prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
1697-
1698-
flags &= MAP_NONBLOCK;
1699-
flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
1700-
if (vma->vm_flags & VM_LOCKED)
1701-
flags |= MAP_LOCKED;
1702-
1703-
file = get_file(vma->vm_file);
1704-
ret = security_mmap_file(vma->vm_file, prot, flags);
1705-
if (ret)
1706-
goto out_fput;
17071743
ret = do_mmap(vma->vm_file, start, size,
17081744
prot, flags, 0, pgoff, &populate, NULL);
1709-
out_fput:
1710-
fput(file);
17111745
out:
17121746
mmap_write_unlock(mm);
1747+
fput(file);
17131748
if (populate)
17141749
mm_populate(ret, populate);
17151750
if (!IS_ERR_VALUE(ret))

0 commit comments

Comments
 (0)