Skip to content

Commit 05a9d43

Browse files
kirylpaniakin-aws
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: Pratyush Yadav <[email protected]>
1 parent 86ab7f0 commit 05a9d43

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

mm/mmap.c

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,6 +3003,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
30033003
unsigned long populate = 0;
30043004
unsigned long ret = -EINVAL;
30053005
struct file *file;
3006+
vm_flags_t vm_flags;
30063007

30073008
pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
30083009
current->comm, current->pid);
@@ -3019,37 +3020,19 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
30193020
if (pgoff + (size >> PAGE_SHIFT) < pgoff)
30203021
return ret;
30213022

3022-
if (mmap_write_lock_killable(mm))
3023+
if (mmap_read_lock_killable(mm))
30233024
return -EINTR;
30243025

3026+
/*
3027+
* Look up VMA under read lock first so we can perform the security
3028+
* without holding locks (which can be problematic). We reacquire a
3029+
* write lock later and check nothing changed underneath us.
3030+
*/
30253031
vma = find_vma(mm, start);
30263032

3027-
if (!vma || !(vma->vm_flags & VM_SHARED))
3028-
goto out;
3029-
3030-
if (start < vma->vm_start)
3031-
goto out;
3032-
3033-
if (start + size > vma->vm_end) {
3034-
struct vm_area_struct *next;
3035-
3036-
for (next = vma->vm_next; next; next = next->vm_next) {
3037-
/* hole between vmas ? */
3038-
if (next->vm_start != next->vm_prev->vm_end)
3039-
goto out;
3040-
3041-
if (next->vm_file != vma->vm_file)
3042-
goto out;
3043-
3044-
if (next->vm_flags != vma->vm_flags)
3045-
goto out;
3046-
3047-
if (start + size <= next->vm_end)
3048-
break;
3049-
}
3050-
3051-
if (!next)
3052-
goto out;
3033+
if (!vma || !(vma->vm_flags & VM_SHARED)) {
3034+
mmap_read_unlock(mm);
3035+
return -EINVAL;
30533036
}
30543037

30553038
prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
@@ -3077,16 +3060,68 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
30773060
}
30783061
}
30793062

3063+
/* Save vm_flags used to calculate prot and flags, and recheck later. */
3064+
vm_flags = vma->vm_flags;
30803065
file = get_file(vma->vm_file);
3081-
ret = security_mmap_file(vma->vm_file, prot, flags);
3082-
if (ret)
3083-
goto out_fput;
3066+
3067+
mmap_read_unlock(mm);
3068+
3069+
/* Call outside mmap_lock to be consistent with other callers. */
3070+
ret = security_mmap_file(file, prot, flags);
3071+
if (ret) {
3072+
fput(file);
3073+
return ret;
3074+
}
3075+
3076+
ret = -EINVAL;
3077+
3078+
/* OK security check passed, take write lock + let it rip. */
3079+
if (mmap_write_lock_killable(mm)) {
3080+
fput(file);
3081+
return -EINTR;
3082+
}
3083+
3084+
vma = find_vma(mm, start);
3085+
3086+
if (!vma)
3087+
goto out;
3088+
3089+
/* Make sure things didn't change under us. */
3090+
if (vma->vm_flags != vm_flags)
3091+
goto out;
3092+
if (vma->vm_file != file)
3093+
goto out;
3094+
3095+
if (start < vma->vm_start)
3096+
goto out;
3097+
3098+
if (start + size > vma->vm_end) {
3099+
struct vm_area_struct *next;
3100+
3101+
for (next = vma->vm_next; next; next = next->vm_next) {
3102+
/* hole between vmas ? */
3103+
if (next->vm_start != next->vm_prev->vm_end)
3104+
goto out;
3105+
3106+
if (next->vm_file != vma->vm_file)
3107+
goto out;
3108+
3109+
if (next->vm_flags != vma->vm_flags)
3110+
goto out;
3111+
3112+
if (start + size <= next->vm_end)
3113+
break;
3114+
}
3115+
3116+
if (!next)
3117+
goto out;
3118+
}
3119+
30843120
ret = do_mmap(vma->vm_file, start, size,
30853121
prot, flags, pgoff, &populate, NULL);
3086-
out_fput:
3087-
fput(file);
30883122
out:
30893123
mmap_write_unlock(mm);
3124+
fput(file);
30903125
if (populate)
30913126
mm_populate(ret, populate);
30923127
if (!IS_ERR_VALUE(ret))

0 commit comments

Comments
 (0)