Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues: - [High] Premature execution of vma_close() before unmap_region() during validation failure creates a Use-After-Free race window. - [High] Restoring vma->vm_start before calling unmap_region() on the validation error path leaves orphaned PTEs, causing permanent UAF. Pre-existing issues: - [High] mmap_validate() fails to check if a driver erroneously sets VMA_WRITE_BIT on a mapping without VMA_MAYWRITE_BIT, allowing read-only files to be mapped writable. - [High] mmap_validate() fails to check if a driver alters vma->vm_end, which silently corrupts the maple tree. -- commit d07b85544000595b057fc1ff43289af0359a7b6c Author: Lorenzo Stoakes (ARM) <[email protected]> mm: consistently validate VMA state after mmap[_prepare] hooks This patch generalizes VMA state validation checks for both mmap_prepare and legacy mmap hooks. It aims to ensure that drivers do not violate invariants, such as altering the VMA start address or creating writable mappings on read-only files, and adds validations for insert_vm_struct(). > diff --git a/mm/internal.h b/mm/internal.h > index fe576d468af45..1ed11eaf276a7 100644 > --- a/mm/internal.h > +++ b/mm/internal.h [ ... ] > @@ -239,26 +260,16 @@ static inline int mmap_file(struct file *file, struct > vm_area_struct *vma) > */ > if (unlikely(err || !vma->vm_ops)) > vma->vm_ops = &vma_dummy_vm_ops; > + if (unlikely(err)) > + return err; > > - return err; > -} > - > -/* > - * If the VMA has a close hook then close it, and since closing it might > leave > - * it in an inconsistent state which makes the use of any hooks suspect, > clear > - * them down by installing dummy empty hooks. > - */ > -static inline void vma_close(struct vm_area_struct *vma) > -{ > - if (vma->vm_ops && vma->vm_ops->close) { > - vma->vm_ops->close(vma); > - > - /* > - * The mapping is in an inconsistent state, and no further hooks > - * may be invoked upon it. > - */ > - vma->vm_ops = &vma_dummy_vm_ops; > + err = mmap_hook_validate(prev_start, &prev_flags, vma); > + if (unlikely(err)) { > + vma->vm_start = prev_start; [Severity: High] If a driver's legacy mmap hook erroneously decreases vma->vm_start (expanding the VMA downwards) and populates PTEs in that expanded range, mmap_hook_validate() catches the violation. By forcing vma->vm_start back to prev_start here before __mmap_new_file_vma() invokes unmap_region(), doesn't that initialize the unmap operation with the smaller, restored address range? This would cause unmap_region() to skip the expanded PTEs, leaving them orphaned and causing a permanent use-after-free since the backing memory gets freed. Should the unmap occur before restoring the start address? > + vma_close(vma); [Severity: High] Is it safe to invoke vma_close() here immediately on validation failure? If a driver successfully populates page tables (for example, via remap_pfn_range) but fails the mmap_hook_validate() checks, calling vma_close() frees driver memory prematurely. Since unmap_region() is not called until mmap_file() returns to __mmap_new_file_vma(), hardware PTEs remain populated while the backing resources are freed, creating a use-after-free race window. > } > + > + return err; > } > > /* unmap_vmas is in mm/memory.c */ > diff --git a/mm/vma.c b/mm/vma.c > index dc74b3d721cc5..626a18c08a269 100644 > --- a/mm/vma.c > +++ b/mm/vma.c [ ... ] > @@ -2777,6 +2762,76 @@ static void __mmap_complete(struct mmap_state *map, > struct vm_area_struct *vma) > vma_set_page_prot(vma); > } > > +/* Check to ensure that the VMA flags of a newly mapped VMA are sane. */ > +static int mmap_validate_vma_flags(const vma_flags_t *flags) > +{ [ ... ] > +/* Check to ensure a driver hasn't done something crazy. */ > +static int mmap_validate(unsigned long prev_start, > + unsigned long curr_start, > + const vma_flags_t *prev_flags, > + const vma_flags_t *curr_flags) > +{ > + bool was_maywrite, is_maywrite; > + > + /* Drivers cannot alter the address of the VMA. */ > + if (WARN_ON_ONCE(prev_start != curr_start)) > + return -EINVAL; [Severity: High] This isn't a bug introduced by this patch, but since the patch aims to implement comprehensive VMA state validation after mmap hooks, shouldn't it also verify that the end address (vma->vm_end) remains unmodified? If a buggy driver alters vm_end, the VMA object's bounds will mismatch the maple tree node's pre-allocated bounds. This structural inconsistency corrupts the maple tree and can lead to kernel panics later. > + > + was_maywrite = vma_flags_test(prev_flags, VMA_MAYWRITE_BIT); > + is_maywrite = vma_flags_test(curr_flags, VMA_MAYWRITE_BIT); > + > + /* A driver may not make a previously unwritable mapping writable. */ > + if (WARN_ON_ONCE(!was_maywrite && is_maywrite)) > + return -EINVAL; [Severity: High] This is a pre-existing issue, but this check appears to bypass a critical condition. It only evaluates VMA_MAYWRITE_BIT. If a malicious or buggy driver bitwise-ORs VMA_WRITE_BIT into vma->flags in its mmap hook without setting VMA_MAYWRITE_BIT, wouldn't it bypass this check completely? This could allow a read-only file mapping to be committed with VM_WRITE set, granting hardware write permissions and allowing an unprivileged user to overwrite read-only files. > + > + return mmap_validate_vma_flags(curr_flags); > +} -- Sashiko AI review ยท https://sashiko.dev/#/patchset/20260914-b4-mmap-prepare-vma-flag-sanify-v2-0-7d9781ed5...@kernel.org?part=4
