1 // SPDX-License-Identifier: GPL-2.0
3 * Implement mseal() syscall.
5 * Copyright (c) 2023,2024 Google, Inc.
10 #include <linux/mempolicy.h>
11 #include <linux/mman.h>
13 #include <linux/mm_inline.h>
14 #include <linux/mmu_context.h>
15 #include <linux/syscalls.h>
16 #include <linux/sched.h>
19 static inline void set_vma_sealed(struct vm_area_struct *vma)
21 vm_flags_set(vma, VM_SEALED);
24 static bool is_madv_discard(int behavior)
29 case MADV_DONTNEED_LOCKED:
39 static bool is_ro_anon(struct vm_area_struct *vma)
41 /* check anonymous mapping. */
42 if (vma->vm_file || vma->vm_flags & VM_SHARED)
46 * check for non-writable:
47 * PROT=RO or PKRU is not writeable.
49 if (!(vma->vm_flags & VM_WRITE) ||
50 !arch_vma_access_permitted(vma, true, false, false))
57 * Check if a vma is allowed to be modified by madvise.
59 bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior)
61 if (!is_madv_discard(behavior))
64 if (unlikely(!can_modify_vma(vma) && is_ro_anon(vma)))
67 /* Allow by default. */
71 static int mseal_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
72 struct vm_area_struct **prev, unsigned long start,
73 unsigned long end, vm_flags_t newflags)
76 vm_flags_t oldflags = vma->vm_flags;
78 if (newflags == oldflags)
81 vma = vma_modify_flags(vmi, *prev, vma, start, end, newflags);
95 * 1> start is part of a valid vma.
96 * 2> end is part of a valid vma.
97 * 3> No gap (unallocated address) between start and end.
100 static int check_mm_seal(unsigned long start, unsigned long end)
102 struct vm_area_struct *vma;
103 unsigned long nstart = start;
105 VMA_ITERATOR(vmi, current->mm, start);
107 /* going through each vma to check. */
108 for_each_vma_range(vmi, vma, end) {
109 if (vma->vm_start > nstart)
110 /* unallocated memory found. */
113 if (vma->vm_end >= end)
116 nstart = vma->vm_end;
125 static int apply_mm_seal(unsigned long start, unsigned long end)
127 unsigned long nstart;
128 struct vm_area_struct *vma, *prev;
130 VMA_ITERATOR(vmi, current->mm, start);
132 vma = vma_iter_load(&vmi);
134 * Note: check_mm_seal should already checked ENOMEM case.
135 * so vma should not be null, same for the other ENOMEM cases.
137 prev = vma_prev(&vmi);
138 if (start > vma->vm_start)
142 for_each_vma_range(vmi, vma, end) {
147 newflags = vma->vm_flags | VM_SEALED;
151 error = mseal_fixup(&vmi, vma, &prev, nstart, tmp, newflags);
154 nstart = vma_iter_end(&vmi);
161 * mseal(2) seals the VM's meta data from
164 * addr/len: VM address range.
166 * The address range by addr/len must meet:
167 * start (addr) must be in a valid VMA.
168 * end (addr + len) must be in a valid VMA.
169 * no gap (unallocated memory) between start and end.
170 * start (addr) must be page aligned.
172 * len: len will be page aligned implicitly.
174 * Below VMA operations are blocked after sealing.
175 * 1> Unmapping, moving to another location, and shrinking
176 * the size, via munmap() and mremap(), can leave an empty
177 * space, therefore can be replaced with a VMA with a new
179 * 2> Moving or expanding a different vma into the current location,
181 * 3> Modifying a VMA via mmap(MAP_FIXED).
182 * 4> Size expansion, via mremap(), does not appear to pose any
183 * specific risks to sealed VMAs. It is included anyway because
184 * the use case is unclear. In any case, users can rely on
185 * merging to expand a sealed VMA.
186 * 5> mprotect and pkey_mprotect.
187 * 6> Some destructive madvice() behavior (e.g. MADV_DONTNEED)
188 * for anonymous memory, when users don't have write permission to the
189 * memory. Those behaviors can alter region contents by discarding pages,
190 * effectively a memset(0) for anonymous memory.
197 * invalid input flags.
198 * start address is not page aligned.
199 * Address arange (start + len) overflow.
201 * addr is not a valid address (not allocated).
202 * end (start + len) is not a valid address.
203 * a gap (unallocated memory) between start and end.
205 * - In 32 bit architecture, sealing is not supported.
207 * user can call mseal(2) multiple times, adding a seal on an
208 * already sealed memory is a no-action (no error).
210 * unseal() is not supported.
212 int do_mseal(unsigned long start, size_t len_in, unsigned long flags)
217 struct mm_struct *mm = current->mm;
219 ret = can_do_mseal(flags);
223 start = untagged_addr(start);
224 if (!PAGE_ALIGNED(start))
227 len = PAGE_ALIGN(len_in);
228 /* Check to see whether len was rounded up from small -ve to zero. */
239 if (mmap_write_lock_killable(mm))
243 * First pass, this helps to avoid
244 * partial sealing in case of error in input address range,
247 ret = check_mm_seal(start, end);
252 * Second pass, this should success, unless there are errors
253 * from vma_modify_flags, e.g. merge/split error, or process
254 * reaching the max supported VMAs, however, those cases shall
257 ret = apply_mm_seal(start, end);
260 mmap_write_unlock(current->mm);
264 SYSCALL_DEFINE3(mseal, unsigned long, start, size_t, len, unsigned long,
267 return do_mseal(start, len, flags);