History

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.

Andrea Arcangeli committed 7 years ago

Commit 29ec9061 parent 5b510722 files · +21 −9Browse files

Unified · Split

2 files
  1. fs/userfaultfd.c +15 −0
  2. mm/userfaultfd.c +6 −9
fs/userfaultfd.c+15−0
@@ -1361,6 +1361,19 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, · 1360 unmodified lines
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, · 39 unmodified lines
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, · 140 unmodified lines
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+6−9
@@ -205,8 +205,9 @@ retry: · 204 unmodified lines
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 @@ retry: · 246 unmodified lines
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;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t