Skip to content

Commit 0fb4a7a

Browse files
lorenzo-stoakesakpm00
authored andcommitted
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: deb0f65 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") Signed-off-by: Lorenzo Stoakes <[email protected]> Reported-by: Jann Horn <[email protected]> Reviewed-by: Liam R. Howlett <[email protected]> Reviewed-by: Vlastimil Babka <[email protected]> Reviewed-by: Jann Horn <[email protected]> Cc: Andreas Larsson <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: David S. Miller <[email protected]> Cc: Helge Deller <[email protected]> Cc: James E.J. Bottomley <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Mark Brown <[email protected]> Cc: Peter Xu <[email protected]> Cc: Will Deacon <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 4080ef1 commit 0fb4a7a

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

include/linux/mman.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,31 @@ static inline bool arch_memory_deny_write_exec_supported(void)
188188
*
189189
* d) mmap(PROT_READ | PROT_EXEC)
190190
* 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.
191199
*/
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)
193201
{
202+
/* If MDWE is disabled, we have nothing to deny. */
194203
if (!test_bit(MMF_HAS_MDWE, &current->mm->flags))
195204
return false;
196205

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)
198212
return true;
199213

200-
if (!(vma->vm_flags & VM_EXEC) && (vm_flags & VM_EXEC))
214+
/* ...nor previously non-executable VMAs becoming executable. */
215+
if (!(old & VM_EXEC))
201216
return true;
202217

203218
return false;

mm/mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
15051505
vma_set_anonymous(vma);
15061506
}
15071507

1508-
if (map_deny_write_exec(vma, vma->vm_flags)) {
1508+
if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) {
15091509
error = -EACCES;
15101510
goto close_and_free_vma;
15111511
}

mm/mprotect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
810810
break;
811811
}
812812

813-
if (map_deny_write_exec(vma, newflags)) {
813+
if (map_deny_write_exec(vma->vm_flags, newflags)) {
814814
error = -EACCES;
815815
break;
816816
}

mm/vma.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct vma_munmap_struct {
4242
int vma_count; /* Number of vmas that will be removed */
4343
bool unlock; /* Unlock after the munmap */
4444
bool clear_ptes; /* If there are outstanding PTE to be cleared */
45-
/* 1 byte hole */
45+
/* 2 byte hole */
4646
unsigned long nr_pages; /* Number of pages being removed */
4747
unsigned long locked_vm; /* Number of locked pages */
4848
unsigned long nr_accounted; /* Number of VM_ACCOUNT pages */

0 commit comments

Comments
 (0)