Skip to content
This repository was archived by the owner on Oct 30, 2021. It is now read-only.

Commit 705a281

Browse files
aagitgregkh
authored andcommitted
userfaultfd: shmem/hugetlbfs: only allow to register VM_MAYWRITE vmas
commit 29ec90660d68bbdd69507c1c8b4e33aa299278b1 upstream. After the VMA to register the uffd onto is found, check that it has VM_MAYWRITE set before allowing registration. This way we inherit all common code checks before allowing to fill file holes in shmem and hugetlbfs with UFFDIO_COPY. The userfaultfd memory model is not applicable for readonly files unless it's a MAP_PRIVATE. Link: http://lkml.kernel.org/r/20181126173452.26955-4-aarcange@redhat.com Fixes: ff62a34 ("hugetlb: implement memfd sealing") Signed-off-by: Andrea Arcangeli <aarcange@redhat.com> Reviewed-by: Mike Rapoport <rppt@linux.ibm.com> Reviewed-by: Hugh Dickins <hughd@google.com> Reported-by: Jann Horn <jannh@google.com> Fixes: 4c27fe4 ("userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support") Cc: <stable@vger.kernel.org> Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Peter Xu <peterx@redhat.com> Cc: stable@vger.kernel.org Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6ea54af commit 705a281

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

fs/userfaultfd.c

+15
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,19 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
13621362
ret = -EINVAL;
13631363
if (!vma_can_userfault(cur))
13641364
goto out_unlock;
1365+
1366+
/*
1367+
* UFFDIO_COPY will fill file holes even without
1368+
* PROT_WRITE. This check enforces that if this is a
1369+
* MAP_SHARED, the process has write permission to the backing
1370+
* file. If VM_MAYWRITE is set it also enforces that on a
1371+
* MAP_SHARED vma: there is no F_WRITE_SEAL and no further
1372+
* F_WRITE_SEAL can be taken until the vma is destroyed.
1373+
*/
1374+
ret = -EPERM;
1375+
if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
1376+
goto out_unlock;
1377+
13651378
/*
13661379
* If this vma contains ending address, and huge pages
13671380
* check alignment.
@@ -1407,6 +1420,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
14071420
BUG_ON(!vma_can_userfault(vma));
14081421
BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
14091422
vma->vm_userfaultfd_ctx.ctx != ctx);
1423+
WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
14101424

14111425
/*
14121426
* Nothing to do: this vma is already registered into this
@@ -1553,6 +1567,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
15531567
cond_resched();
15541568

15551569
BUG_ON(!vma_can_userfault(vma));
1570+
WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
15561571

15571572
/*
15581573
* Nothing to do: this vma is already registered into this

mm/userfaultfd.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
228228
if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
229229
goto out_unlock;
230230
/*
231-
* Only allow __mcopy_atomic_hugetlb on userfaultfd
232-
* registered ranges.
231+
* Check the vma is registered in uffd, this is
232+
* required to enforce the VM_MAYWRITE check done at
233+
* uffd registration time.
233234
*/
234235
if (!dst_vma->vm_userfaultfd_ctx.ctx)
235236
goto out_unlock;
@@ -472,13 +473,9 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
472473
if (!dst_vma)
473474
goto out_unlock;
474475
/*
475-
* Be strict and only allow __mcopy_atomic on userfaultfd
476-
* registered ranges to prevent userland errors going
477-
* unnoticed. As far as the VM consistency is concerned, it
478-
* would be perfectly safe to remove this check, but there's
479-
* no useful usage for __mcopy_atomic ouside of userfaultfd
480-
* registered ranges. This is after all why these are ioctls
481-
* belonging to the userfaultfd and not syscalls.
476+
* Check the vma is registered in uffd, this is required to
477+
* enforce the VM_MAYWRITE check done at uffd registration
478+
* time.
482479
*/
483480
if (!dst_vma->vm_userfaultfd_ctx.ctx)
484481
goto out_unlock;

0 commit comments

Comments
 (0)