Skip to content

Commit 3807567

Browse files
heatdakpm00
authored andcommitted
mm/mremap: replace can_modify_mm with can_modify_vma
Delegate all can_modify checks to the proper places. Unmap checks are done in do_unmap (et al). The source VMA check is done purposefully before unmapping, to keep the original mseal semantics. Link: https://lkml.kernel.org/r/20240817-mseal-depessimize-v3-4-d8d2e037df30@gmail.com Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com> Reviewed-by: Liam R. Howlett <Liam.Howlett@Oracle.com> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Jeff Xu <jeffxu@chromium.org> Cc: Kees Cook <kees@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Shuah Khan <shuah@kernel.org> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 4a2dd02 commit 3807567

File tree

1 file changed

+6
-26
lines changed

1 file changed

+6
-26
lines changed

mm/mremap.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -902,19 +902,6 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
902902
if ((mm->map_count + 2) >= sysctl_max_map_count - 3)
903903
return -ENOMEM;
904904

905-
/*
906-
* In mremap_to().
907-
* Move a VMA to another location, check if src addr is sealed.
908-
*
909-
* Place can_modify_mm here because mremap_to()
910-
* does its own checking for address range, and we only
911-
* check the sealing after passing those checks.
912-
*
913-
* can_modify_mm assumes we have acquired the lock on MM.
914-
*/
915-
if (unlikely(!can_modify_mm(mm, addr, addr + old_len)))
916-
return -EPERM;
917-
918905
if (flags & MREMAP_FIXED) {
919906
/*
920907
* In mremap_to().
@@ -1052,6 +1039,12 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
10521039
goto out;
10531040
}
10541041

1042+
/* Don't allow remapping vmas when they have already been sealed */
1043+
if (!can_modify_vma(vma)) {
1044+
ret = -EPERM;
1045+
goto out;
1046+
}
1047+
10551048
if (is_vm_hugetlb_page(vma)) {
10561049
struct hstate *h __maybe_unused = hstate_vma(vma);
10571050

@@ -1079,19 +1072,6 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
10791072
goto out;
10801073
}
10811074

1082-
/*
1083-
* Below is shrink/expand case (not mremap_to())
1084-
* Check if src address is sealed, if so, reject.
1085-
* In other words, prevent shrinking or expanding a sealed VMA.
1086-
*
1087-
* Place can_modify_mm here so we can keep the logic related to
1088-
* shrink/expand together.
1089-
*/
1090-
if (unlikely(!can_modify_mm(mm, addr, addr + old_len))) {
1091-
ret = -EPERM;
1092-
goto out;
1093-
}
1094-
10951075
/*
10961076
* Always allow a shrinking remap: that just unmaps
10971077
* the unnecessary pages..

0 commit comments

Comments
 (0)