Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm: refactor map_deny_write_exec()

Refactor the map_deny_write_exec() to not unnecessarily require a VMA
parameter but rather to accept VMA flags parameters, which allows us to
use this function early in mmap_region() in a subsequent commit.

While we're here, we refactor the function to be more readable and add
some additional documentation.

Link: https://lkml.kernel.org/r/6be8bb59cd7c68006ebb006eb9d8dc27104b1f70.1730224667.git.lorenzo.stoakes@oracle.com
Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reported-by: Jann Horn <jannh@google.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Jann Horn <jannh@google.com>
Cc: Andreas Larsson <andreas@gaisler.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Helge Deller <deller@gmx.de>
Cc: James E.J. Bottomley <James.Bottomley@HansenPartnership.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes and committed by
Andrew Morton
0fb4a7ad 4080ef15

+21 -6
+18 -3
include/linux/mman.h
··· 188 188 * 189 189 * d) mmap(PROT_READ | PROT_EXEC) 190 190 * mmap(PROT_READ | PROT_EXEC | PROT_BTI) 191 + * 192 + * This is only applicable if the user has set the Memory-Deny-Write-Execute 193 + * (MDWE) protection mask for the current process. 194 + * 195 + * @old specifies the VMA flags the VMA originally possessed, and @new the ones 196 + * we propose to set. 197 + * 198 + * Return: false if proposed change is OK, true if not ok and should be denied. 191 199 */ 192 - static inline bool map_deny_write_exec(struct vm_area_struct *vma, unsigned long vm_flags) 200 + static inline bool map_deny_write_exec(unsigned long old, unsigned long new) 193 201 { 202 + /* If MDWE is disabled, we have nothing to deny. */ 194 203 if (!test_bit(MMF_HAS_MDWE, &current->mm->flags)) 195 204 return false; 196 205 197 - if ((vm_flags & VM_EXEC) && (vm_flags & VM_WRITE)) 206 + /* If the new VMA is not executable, we have nothing to deny. */ 207 + if (!(new & VM_EXEC)) 208 + return false; 209 + 210 + /* Under MDWE we do not accept newly writably executable VMAs... */ 211 + if (new & VM_WRITE) 198 212 return true; 199 213 200 - if (!(vma->vm_flags & VM_EXEC) && (vm_flags & VM_EXEC)) 214 + /* ...nor previously non-executable VMAs becoming executable. */ 215 + if (!(old & VM_EXEC)) 201 216 return true; 202 217 203 218 return false;
+1 -1
mm/mmap.c
··· 1505 1505 vma_set_anonymous(vma); 1506 1506 } 1507 1507 1508 - if (map_deny_write_exec(vma, vma->vm_flags)) { 1508 + if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) { 1509 1509 error = -EACCES; 1510 1510 goto close_and_free_vma; 1511 1511 }
+1 -1
mm/mprotect.c
··· 810 810 break; 811 811 } 812 812 813 - if (map_deny_write_exec(vma, newflags)) { 813 + if (map_deny_write_exec(vma->vm_flags, newflags)) { 814 814 error = -EACCES; 815 815 break; 816 816 }
+1 -1
mm/vma.h
··· 42 42 int vma_count; /* Number of vmas that will be removed */ 43 43 bool unlock; /* Unlock after the munmap */ 44 44 bool clear_ptes; /* If there are outstanding PTE to be cleared */ 45 - /* 1 byte hole */ 45 + /* 2 byte hole */ 46 46 unsigned long nr_pages; /* Number of pages being removed */ 47 47 unsigned long locked_vm; /* Number of locked pages */ 48 48 unsigned long nr_accounted; /* Number of VM_ACCOUNT pages */