Skip to content

Commit 29ec906

Browse files
aagittorvalds
authored andcommitted
userfaultfd: shmem/hugetlbfs: only allow to register VM_MAYWRITE vmas
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>
1 parent 5b51072 commit 29ec906

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

fs/userfaultfd.c

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

14101424
/*
14111425
* Nothing to do: this vma is already registered into this
@@ -1552,6 +1566,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
15521566
cond_resched();
15531567

15541568
BUG_ON(!vma_can_userfault(vma));
1569+
WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
15551570

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

mm/userfaultfd.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
205205
if (!dst_vma || !is_vm_hugetlb_page(dst_vma))
206206
goto out_unlock;
207207
/*
208-
* Only allow __mcopy_atomic_hugetlb on userfaultfd
209-
* registered ranges.
208+
* Check the vma is registered in uffd, this is
209+
* required to enforce the VM_MAYWRITE check done at
210+
* uffd registration time.
210211
*/
211212
if (!dst_vma->vm_userfaultfd_ctx.ctx)
212213
goto out_unlock;
@@ -459,13 +460,9 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
459460
if (!dst_vma)
460461
goto out_unlock;
461462
/*
462-
* Be strict and only allow __mcopy_atomic on userfaultfd
463-
* registered ranges to prevent userland errors going
464-
* unnoticed. As far as the VM consistency is concerned, it
465-
* would be perfectly safe to remove this check, but there's
466-
* no useful usage for __mcopy_atomic ouside of userfaultfd
467-
* registered ranges. This is after all why these are ioctls
468-
* belonging to the userfaultfd and not syscalls.
463+
* Check the vma is registered in uffd, this is required to
464+
* enforce the VM_MAYWRITE check done at uffd registration
465+
* time.
469466
*/
470467
if (!dst_vma->vm_userfaultfd_ctx.ctx)
471468
goto out_unlock;

0 commit comments

Comments
 (0)