]> git.ipfire.org Git - people/ms/linux.git/blob - mm/mremap.c
grsecurity: Make grsec compile on aarch64
[people/ms/linux.git] / mm / mremap.c
1 /*
2 * mm/mremap.c
3 *
4 * (C) Copyright 1996 Linus Torvalds
5 *
6 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
7 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
8 */
9
10 #include <linux/mm.h>
11 #include <linux/hugetlb.h>
12 #include <linux/shm.h>
13 #include <linux/ksm.h>
14 #include <linux/mman.h>
15 #include <linux/swap.h>
16 #include <linux/capability.h>
17 #include <linux/fs.h>
18 #include <linux/swapops.h>
19 #include <linux/highmem.h>
20 #include <linux/security.h>
21 #include <linux/syscalls.h>
22 #include <linux/mmu_notifier.h>
23 #include <linux/sched/sysctl.h>
24 #include <linux/uaccess.h>
25
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
28
29 #include "internal.h"
30
31 static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
32 {
33 pgd_t *pgd;
34 pud_t *pud;
35 pmd_t *pmd;
36
37 pgd = pgd_offset(mm, addr);
38 if (pgd_none_or_clear_bad(pgd))
39 return NULL;
40
41 pud = pud_offset(pgd, addr);
42 if (pud_none_or_clear_bad(pud))
43 return NULL;
44
45 pmd = pmd_offset(pud, addr);
46 if (pmd_none(*pmd))
47 return NULL;
48
49 return pmd;
50 }
51
52 static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
53 unsigned long addr)
54 {
55 pgd_t *pgd;
56 pud_t *pud;
57 pmd_t *pmd;
58
59 pgd = pgd_offset(mm, addr);
60 pud = pud_alloc(mm, pgd, addr);
61 if (!pud)
62 return NULL;
63
64 pmd = pmd_alloc(mm, pud, addr);
65 if (!pmd)
66 return NULL;
67
68 VM_BUG_ON(pmd_trans_huge(*pmd));
69
70 return pmd;
71 }
72
73 static pte_t move_soft_dirty_pte(pte_t pte)
74 {
75 /*
76 * Set soft dirty bit so we can notice
77 * in userspace the ptes were moved.
78 */
79 #ifdef CONFIG_MEM_SOFT_DIRTY
80 if (pte_present(pte))
81 pte = pte_mksoft_dirty(pte);
82 else if (is_swap_pte(pte))
83 pte = pte_swp_mksoft_dirty(pte);
84 else if (pte_file(pte))
85 pte = pte_file_mksoft_dirty(pte);
86 #endif
87 return pte;
88 }
89
90 static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
91 unsigned long old_addr, unsigned long old_end,
92 struct vm_area_struct *new_vma, pmd_t *new_pmd,
93 unsigned long new_addr, bool need_rmap_locks)
94 {
95 struct address_space *mapping = NULL;
96 struct anon_vma *anon_vma = NULL;
97 struct mm_struct *mm = vma->vm_mm;
98 pte_t *old_pte, *new_pte, pte;
99 spinlock_t *old_ptl, *new_ptl;
100
101 /*
102 * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
103 * locks to ensure that rmap will always observe either the old or the
104 * new ptes. This is the easiest way to avoid races with
105 * truncate_pagecache(), page migration, etc...
106 *
107 * When need_rmap_locks is false, we use other ways to avoid
108 * such races:
109 *
110 * - During exec() shift_arg_pages(), we use a specially tagged vma
111 * which rmap call sites look for using is_vma_temporary_stack().
112 *
113 * - During mremap(), new_vma is often known to be placed after vma
114 * in rmap traversal order. This ensures rmap will always observe
115 * either the old pte, or the new pte, or both (the page table locks
116 * serialize access to individual ptes, but only rmap traversal
117 * order guarantees that we won't miss both the old and new ptes).
118 */
119 if (need_rmap_locks) {
120 if (vma->vm_file) {
121 mapping = vma->vm_file->f_mapping;
122 i_mmap_lock_write(mapping);
123 }
124 if (vma->anon_vma) {
125 anon_vma = vma->anon_vma;
126 anon_vma_lock_write(anon_vma);
127 }
128 }
129
130 /*
131 * We don't have to worry about the ordering of src and dst
132 * pte locks because exclusive mmap_sem prevents deadlock.
133 */
134 old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
135 new_pte = pte_offset_map(new_pmd, new_addr);
136 new_ptl = pte_lockptr(mm, new_pmd);
137 if (new_ptl != old_ptl)
138 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
139 arch_enter_lazy_mmu_mode();
140
141 for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
142 new_pte++, new_addr += PAGE_SIZE) {
143 if (pte_none(*old_pte))
144 continue;
145 pte = ptep_get_and_clear(mm, old_addr, old_pte);
146 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
147
148 #ifdef CONFIG_ARCH_TRACK_EXEC_LIMIT
149 if (!(__supported_pte_mask & _PAGE_NX) && pte_present(pte) && (new_vma->vm_flags & (VM_PAGEEXEC | VM_EXEC)) == VM_PAGEEXEC)
150 pte = pte_exprotect(pte);
151 #endif
152
153 pte = move_soft_dirty_pte(pte);
154 set_pte_at(mm, new_addr, new_pte, pte);
155 }
156
157 arch_leave_lazy_mmu_mode();
158 if (new_ptl != old_ptl)
159 spin_unlock(new_ptl);
160 pte_unmap(new_pte - 1);
161 pte_unmap_unlock(old_pte - 1, old_ptl);
162 if (anon_vma)
163 anon_vma_unlock_write(anon_vma);
164 if (mapping)
165 i_mmap_unlock_write(mapping);
166 }
167
168 #define LATENCY_LIMIT (64 * PAGE_SIZE)
169
170 unsigned long move_page_tables(struct vm_area_struct *vma,
171 unsigned long old_addr, struct vm_area_struct *new_vma,
172 unsigned long new_addr, unsigned long len,
173 bool need_rmap_locks)
174 {
175 unsigned long extent, next, old_end;
176 pmd_t *old_pmd, *new_pmd;
177 bool need_flush = false;
178 unsigned long mmun_start; /* For mmu_notifiers */
179 unsigned long mmun_end; /* For mmu_notifiers */
180
181 old_end = old_addr + len;
182 flush_cache_range(vma, old_addr, old_end);
183
184 mmun_start = old_addr;
185 mmun_end = old_end;
186 mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
187
188 for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
189 cond_resched();
190 next = (old_addr + PMD_SIZE) & PMD_MASK;
191 /* even if next overflowed, extent below will be ok */
192 extent = next - old_addr;
193 if (extent > old_end - old_addr)
194 extent = old_end - old_addr;
195 old_pmd = get_old_pmd(vma->vm_mm, old_addr);
196 if (!old_pmd)
197 continue;
198 new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
199 if (!new_pmd)
200 break;
201 if (pmd_trans_huge(*old_pmd)) {
202 int err = 0;
203 if (extent == HPAGE_PMD_SIZE) {
204 VM_BUG_ON_VMA(vma->vm_file || !vma->anon_vma,
205 vma);
206 /* See comment in move_ptes() */
207 if (need_rmap_locks)
208 anon_vma_lock_write(vma->anon_vma);
209 err = move_huge_pmd(vma, new_vma, old_addr,
210 new_addr, old_end,
211 old_pmd, new_pmd);
212 if (need_rmap_locks)
213 anon_vma_unlock_write(vma->anon_vma);
214 }
215 if (err > 0) {
216 need_flush = true;
217 continue;
218 } else if (!err) {
219 split_huge_page_pmd(vma, old_addr, old_pmd);
220 }
221 VM_BUG_ON(pmd_trans_huge(*old_pmd));
222 }
223 if (pmd_none(*new_pmd) && __pte_alloc(new_vma->vm_mm, new_vma,
224 new_pmd, new_addr))
225 break;
226 next = (new_addr + PMD_SIZE) & PMD_MASK;
227 if (extent > next - new_addr)
228 extent = next - new_addr;
229 if (extent > LATENCY_LIMIT)
230 extent = LATENCY_LIMIT;
231 move_ptes(vma, old_pmd, old_addr, old_addr + extent,
232 new_vma, new_pmd, new_addr, need_rmap_locks);
233 need_flush = true;
234 }
235 if (likely(need_flush))
236 flush_tlb_range(vma, old_end-len, old_addr);
237
238 mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
239
240 return len + old_addr - old_end; /* how much done */
241 }
242
243 static unsigned long move_vma(struct vm_area_struct *vma,
244 unsigned long old_addr, unsigned long old_len,
245 unsigned long new_len, unsigned long new_addr, bool *locked)
246 {
247 struct mm_struct *mm = vma->vm_mm;
248 struct vm_area_struct *new_vma;
249 unsigned long vm_flags = vma->vm_flags;
250 unsigned long new_pgoff;
251 unsigned long moved_len;
252 unsigned long excess = 0;
253 unsigned long hiwater_vm;
254 int split = 0;
255 int err;
256 bool need_rmap_locks;
257
258 /*
259 * We'd prefer to avoid failure later on in do_munmap:
260 * which may split one vma into three before unmapping.
261 */
262 if (mm->map_count >= sysctl_max_map_count - 3)
263 return -ENOMEM;
264
265 /*
266 * Advise KSM to break any KSM pages in the area to be moved:
267 * it would be confusing if they were to turn up at the new
268 * location, where they happen to coincide with different KSM
269 * pages recently unmapped. But leave vma->vm_flags as it was,
270 * so KSM can come around to merge on vma and new_vma afterwards.
271 */
272 err = ksm_madvise(vma, old_addr, old_addr + old_len,
273 MADV_UNMERGEABLE, &vm_flags);
274 if (err)
275 return err;
276
277 new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
278 new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
279 &need_rmap_locks);
280 if (!new_vma)
281 return -ENOMEM;
282
283 moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
284 need_rmap_locks);
285 if (moved_len < old_len) {
286 /*
287 * On error, move entries back from new area to old,
288 * which will succeed since page tables still there,
289 * and then proceed to unmap new area instead of old.
290 */
291 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
292 true);
293 vma = new_vma;
294 old_len = new_len;
295 old_addr = new_addr;
296 new_addr = -ENOMEM;
297 } else if (vma->vm_file && vma->vm_file->f_op->mremap)
298 vma->vm_file->f_op->mremap(vma->vm_file, new_vma);
299
300 /* Conceal VM_ACCOUNT so old reservation is not undone */
301 if (vm_flags & VM_ACCOUNT) {
302 vma->vm_flags &= ~VM_ACCOUNT;
303 excess = vma->vm_end - vma->vm_start - old_len;
304 if (old_addr > vma->vm_start &&
305 old_addr + old_len < vma->vm_end)
306 split = 1;
307 }
308
309 /*
310 * If we failed to move page tables we still do total_vm increment
311 * since do_munmap() will decrement it by old_len == new_len.
312 *
313 * Since total_vm is about to be raised artificially high for a
314 * moment, we need to restore high watermark afterwards: if stats
315 * are taken meanwhile, total_vm and hiwater_vm appear too high.
316 * If this were a serious issue, we'd add a flag to do_munmap().
317 */
318 hiwater_vm = mm->hiwater_vm;
319 vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
320
321 if (do_munmap(mm, old_addr, old_len) < 0) {
322 /* OOM: unable to split vma, just get accounts right */
323 vm_unacct_memory(excess >> PAGE_SHIFT);
324 excess = 0;
325 }
326 mm->hiwater_vm = hiwater_vm;
327
328 /* Restore VM_ACCOUNT if one or two pieces of vma left */
329 if (excess) {
330 vma->vm_flags |= VM_ACCOUNT;
331 if (split)
332 vma->vm_next->vm_flags |= VM_ACCOUNT;
333 }
334
335 if (vm_flags & VM_LOCKED) {
336 mm->locked_vm += new_len >> PAGE_SHIFT;
337 *locked = true;
338 }
339
340 return new_addr;
341 }
342
343 static struct vm_area_struct *vma_to_resize(unsigned long addr,
344 unsigned long old_len, unsigned long new_len, unsigned long *p)
345 {
346 struct mm_struct *mm = current->mm;
347 struct vm_area_struct *vma = find_vma(mm, addr);
348
349 if (!vma || vma->vm_start > addr)
350 goto Efault;
351
352 if (is_vm_hugetlb_page(vma))
353 goto Einval;
354
355 #ifdef CONFIG_PAX_SEGMEXEC
356 if (pax_find_mirror_vma(vma))
357 goto Einval;
358 #endif
359
360 /* We can't remap across vm area boundaries */
361 if (old_len > vma->vm_end - addr)
362 goto Efault;
363
364 /* Need to be careful about a growing mapping */
365 if (new_len > old_len) {
366 unsigned long pgoff;
367
368 if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
369 goto Efault;
370 pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
371 pgoff += vma->vm_pgoff;
372 if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
373 goto Einval;
374 }
375
376 if (vma->vm_flags & VM_LOCKED) {
377 unsigned long locked, lock_limit;
378 locked = mm->locked_vm << PAGE_SHIFT;
379 lock_limit = rlimit(RLIMIT_MEMLOCK);
380 locked += new_len - old_len;
381 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
382 goto Eagain;
383 }
384
385 if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT))
386 goto Enomem;
387
388 if (vma->vm_flags & VM_ACCOUNT) {
389 unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
390 if (security_vm_enough_memory_mm(mm, charged))
391 goto Efault;
392 *p = charged;
393 }
394
395 return vma;
396
397 Efault: /* very odd choice for most of the cases, but... */
398 return ERR_PTR(-EFAULT);
399 Einval:
400 return ERR_PTR(-EINVAL);
401 Enomem:
402 return ERR_PTR(-ENOMEM);
403 Eagain:
404 return ERR_PTR(-EAGAIN);
405 }
406
407 static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
408 unsigned long new_addr, unsigned long new_len, bool *locked)
409 {
410 struct mm_struct *mm = current->mm;
411 struct vm_area_struct *vma;
412 unsigned long ret = -EINVAL;
413 unsigned long charged = 0;
414 unsigned long map_flags;
415 unsigned long pax_task_size = TASK_SIZE;
416
417 if (new_addr & ~PAGE_MASK)
418 goto out;
419
420 #ifdef CONFIG_PAX_SEGMEXEC
421 if (mm->pax_flags & MF_PAX_SEGMEXEC)
422 pax_task_size = SEGMEXEC_TASK_SIZE;
423 #endif
424
425 pax_task_size -= PAGE_SIZE;
426
427 if (new_len > TASK_SIZE || new_addr > pax_task_size - new_len)
428 goto out;
429
430 /* Check if the location we're moving into overlaps the
431 * old location at all, and fail if it does.
432 */
433 if (addr + old_len > new_addr && new_addr + new_len > addr)
434 goto out;
435
436 ret = do_munmap(mm, new_addr, new_len);
437 if (ret)
438 goto out;
439
440 if (old_len >= new_len) {
441 ret = do_munmap(mm, addr+new_len, old_len - new_len);
442 if (ret && old_len != new_len)
443 goto out;
444 old_len = new_len;
445 }
446
447 vma = vma_to_resize(addr, old_len, new_len, &charged);
448 if (IS_ERR(vma)) {
449 ret = PTR_ERR(vma);
450 goto out;
451 }
452
453 map_flags = MAP_FIXED;
454 if (vma->vm_flags & VM_MAYSHARE)
455 map_flags |= MAP_SHARED;
456
457 ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
458 ((addr - vma->vm_start) >> PAGE_SHIFT),
459 map_flags);
460 if (ret & ~PAGE_MASK)
461 goto out1;
462
463 ret = move_vma(vma, addr, old_len, new_len, new_addr, locked);
464 if (!(ret & ~PAGE_MASK))
465 goto out;
466 out1:
467 vm_unacct_memory(charged);
468
469 out:
470 return ret;
471 }
472
473 static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
474 {
475 unsigned long end = vma->vm_end + delta;
476 if (end < vma->vm_end) /* overflow */
477 return 0;
478 if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
479 return 0;
480 if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
481 0, MAP_FIXED) & ~PAGE_MASK)
482 return 0;
483 return 1;
484 }
485
486 /*
487 * Expand (or shrink) an existing mapping, potentially moving it at the
488 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
489 *
490 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
491 * This option implies MREMAP_MAYMOVE.
492 */
493 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
494 unsigned long, new_len, unsigned long, flags,
495 unsigned long, new_addr)
496 {
497 struct mm_struct *mm = current->mm;
498 struct vm_area_struct *vma;
499 unsigned long ret = -EINVAL;
500 unsigned long charged = 0;
501 bool locked = false;
502 unsigned long pax_task_size = TASK_SIZE;
503
504 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
505 return ret;
506
507 if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
508 return ret;
509
510 if (addr & ~PAGE_MASK)
511 return ret;
512
513 old_len = PAGE_ALIGN(old_len);
514 new_len = PAGE_ALIGN(new_len);
515
516 /*
517 * We allow a zero old-len as a special case
518 * for DOS-emu "duplicate shm area" thing. But
519 * a zero new-len is nonsensical.
520 */
521 if (!new_len)
522 return ret;
523
524 #ifdef CONFIG_PAX_SEGMEXEC
525 if (mm->pax_flags & MF_PAX_SEGMEXEC)
526 pax_task_size = SEGMEXEC_TASK_SIZE;
527 #endif
528
529 pax_task_size -= PAGE_SIZE;
530
531 if (new_len > pax_task_size || addr > pax_task_size-new_len ||
532 old_len > pax_task_size || addr > pax_task_size-old_len)
533 return ret;
534
535 down_write(&current->mm->mmap_sem);
536
537 if (flags & MREMAP_FIXED) {
538 ret = mremap_to(addr, old_len, new_addr, new_len,
539 &locked);
540 goto out;
541 }
542
543 /*
544 * Always allow a shrinking remap: that just unmaps
545 * the unnecessary pages..
546 * do_munmap does all the needed commit accounting
547 */
548 if (old_len >= new_len) {
549 ret = do_munmap(mm, addr+new_len, old_len - new_len);
550 if (ret && old_len != new_len)
551 goto out;
552 ret = addr;
553 goto out;
554 }
555
556 /*
557 * Ok, we need to grow..
558 */
559 vma = vma_to_resize(addr, old_len, new_len, &charged);
560 if (IS_ERR(vma)) {
561 ret = PTR_ERR(vma);
562 goto out;
563 }
564
565 /* old_len exactly to the end of the area..
566 */
567 if (old_len == vma->vm_end - addr) {
568 /* can we just expand the current mapping? */
569 if (vma_expandable(vma, new_len - old_len)) {
570 int pages = (new_len - old_len) >> PAGE_SHIFT;
571
572 if (vma_adjust(vma, vma->vm_start, addr + new_len,
573 vma->vm_pgoff, NULL)) {
574 ret = -ENOMEM;
575 goto out;
576 }
577
578 vm_stat_account(mm, vma->vm_flags, vma->vm_file, pages);
579 if (vma->vm_flags & VM_LOCKED) {
580 mm->locked_vm += pages;
581 locked = true;
582 new_addr = addr;
583 }
584 ret = addr;
585 track_exec_limit(vma->vm_mm, vma->vm_start, addr + new_len, vma->vm_flags);
586 goto out;
587 }
588 }
589
590 /*
591 * We weren't able to just expand or shrink the area,
592 * we need to create a new one and move it..
593 */
594 ret = -ENOMEM;
595 if (flags & MREMAP_MAYMOVE) {
596 unsigned long map_flags = 0;
597 if (vma->vm_flags & VM_MAYSHARE)
598 map_flags |= MAP_SHARED;
599
600 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
601 vma->vm_pgoff +
602 ((addr - vma->vm_start) >> PAGE_SHIFT),
603 map_flags);
604 if (new_addr & ~PAGE_MASK) {
605 ret = new_addr;
606 goto out;
607 }
608
609 map_flags = vma->vm_flags;
610 ret = move_vma(vma, addr, old_len, new_len, new_addr, &locked);
611 if (!(ret & ~PAGE_MASK)) {
612 track_exec_limit(current->mm, addr, addr + old_len, 0UL);
613 track_exec_limit(current->mm, new_addr, new_addr + new_len, map_flags);
614 }
615 }
616 out:
617 if (ret & ~PAGE_MASK)
618 vm_unacct_memory(charged);
619 up_write(&current->mm->mmap_sem);
620 if (locked && new_len > old_len)
621 mm_populate(new_addr + old_len, new_len - old_len);
622 return ret;
623 }