Skip to content

Commit 33aa58c

Browse files
kirylopsiff
authored andcommitted
mm: split critical region in remap_file_pages() and invoke LSMs in between
commit 58a039e upstream. 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]> Signed-off-by: Jianqi Ren <[email protected]> Signed-off-by: He Zhe <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 86d97d4)
1 parent b0b574b commit 33aa58c

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
@@ -2981,6 +2981,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
29812981
unsigned long populate = 0;
29822982
unsigned long ret = -EINVAL;
29832983
struct file *file;
2984+
vm_flags_t vm_flags;
29842985

29852986
pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
29862987
current->comm, current->pid);
@@ -2997,12 +2998,60 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
29972998
if (pgoff + (size >> PAGE_SHIFT) < pgoff)
29982999
return ret;
29993000

3000-
if (mmap_write_lock_killable(mm))
3001+
if (mmap_read_lock_killable(mm))
3002+
return -EINTR;
3003+
3004+
/*
3005+
* Look up VMA under read lock first so we can perform the security
3006+
* without holding locks (which can be problematic). We reacquire a
3007+
* write lock later and check nothing changed underneath us.
3008+
*/
3009+
vma = vma_lookup(mm, start);
3010+
3011+
if (!vma || !(vma->vm_flags & VM_SHARED)) {
3012+
mmap_read_unlock(mm);
3013+
return -EINVAL;
3014+
}
3015+
3016+
prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
3017+
prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
3018+
prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
3019+
3020+
flags &= MAP_NONBLOCK;
3021+
flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
3022+
if (vma->vm_flags & VM_LOCKED)
3023+
flags |= MAP_LOCKED;
3024+
3025+
/* Save vm_flags used to calculate prot and flags, and recheck later. */
3026+
vm_flags = vma->vm_flags;
3027+
file = get_file(vma->vm_file);
3028+
3029+
mmap_read_unlock(mm);
3030+
3031+
/* Call outside mmap_lock to be consistent with other callers. */
3032+
ret = security_mmap_file(file, prot, flags);
3033+
if (ret) {
3034+
fput(file);
3035+
return ret;
3036+
}
3037+
3038+
ret = -EINVAL;
3039+
3040+
/* OK security check passed, take write lock + let it rip. */
3041+
if (mmap_write_lock_killable(mm)) {
3042+
fput(file);
30013043
return -EINTR;
3044+
}
30023045

30033046
vma = vma_lookup(mm, start);
30043047

3005-
if (!vma || !(vma->vm_flags & VM_SHARED))
3048+
if (!vma)
3049+
goto out;
3050+
3051+
/* Make sure things didn't change under us. */
3052+
if (vma->vm_flags != vm_flags)
3053+
goto out;
3054+
if (vma->vm_file != file)
30063055
goto out;
30073056

30083057
if (start + size > vma->vm_end) {
@@ -3030,25 +3079,11 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
30303079
goto out;
30313080
}
30323081

3033-
prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
3034-
prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
3035-
prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
3036-
3037-
flags &= MAP_NONBLOCK;
3038-
flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
3039-
if (vma->vm_flags & VM_LOCKED)
3040-
flags |= MAP_LOCKED;
3041-
3042-
file = get_file(vma->vm_file);
3043-
ret = security_mmap_file(vma->vm_file, prot, flags);
3044-
if (ret)
3045-
goto out_fput;
30463082
ret = do_mmap(vma->vm_file, start, size,
30473083
prot, flags, 0, pgoff, &populate, NULL);
3048-
out_fput:
3049-
fput(file);
30503084
out:
30513085
mmap_write_unlock(mm);
3086+
fput(file);
30523087
if (populate)
30533088
mm_populate(ret, populate);
30543089
if (!IS_ERR_VALUE(ret))

0 commit comments

Comments
 (0)