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.
Unified · Split
| @@ -1361,6 +1361,19 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, · 1360 unmodified lines | |||
| 1361 | ret = -EINVAL; | 1361 | ret = -EINVAL; |
| 1362 | if (!vma_can_userfault(cur)) | 1362 | if (!vma_can_userfault(cur)) |
| 1363 | goto out_unlock; | 1363 | 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 | |||
| 1364 | /* | 1377 | /* |
| 1365 | * If this vma contains ending address, and huge pages | 1378 | * If this vma contains ending address, and huge pages |
| 1366 | * check alignment. | 1379 | * check alignment. |
| @@ -1406,6 +1419,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, · 39 unmodified lines | |||
| 1406 | BUG_ON(!vma_can_userfault(vma)); | 1419 | BUG_ON(!vma_can_userfault(vma)); |
| 1407 | BUG_ON(vma->vm_userfaultfd_ctx.ctx && | 1420 | BUG_ON(vma->vm_userfaultfd_ctx.ctx && |
| 1408 | vma->vm_userfaultfd_ctx.ctx != ctx); | 1421 | vma->vm_userfaultfd_ctx.ctx != ctx); |
| 1422 | WARN_ON(!(vma->vm_flags & VM_MAYWRITE)); | ||
| 1409 | 1423 | ||
| 1410 | /* | 1424 | /* |
| 1411 | * Nothing to do: this vma is already registered into this | 1425 | * 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 | |||
| 1552 | cond_resched(); | 1566 | cond_resched(); |
| 1553 | 1567 | ||
| 1554 | BUG_ON(!vma_can_userfault(vma)); | 1568 | BUG_ON(!vma_can_userfault(vma)); |
| 1569 | WARN_ON(!(vma->vm_flags & VM_MAYWRITE)); | ||
| 1555 | 1570 | ||
| 1556 | /* | 1571 | /* |
| 1557 | * Nothing to do: this vma is already registered into this | 1572 | * Nothing to do: this vma is already registered into this |
| @@ -205,8 +205,9 @@ retry: · 204 unmodified lines | |||
| 205 | if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) | 205 | if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) |
| 206 | goto out_unlock; | 206 | goto out_unlock; |
| 207 | /* | 207 | /* |
| 208 | * Only allow __mcopy_atomic_hugetlb on userfaultfd | 208 | * Check the vma is registered in uffd, this is |
| 209 | * registered ranges. | 209 | * required to enforce the VM_MAYWRITE check done at |
| 210 | * uffd registration time. | ||
| 210 | */ | 211 | */ |
| 211 | if (!dst_vma->vm_userfaultfd_ctx.ctx) | 212 | if (!dst_vma->vm_userfaultfd_ctx.ctx) |
| 212 | goto out_unlock; | 213 | goto out_unlock; |
| @@ -459,13 +460,9 @@ retry: · 246 unmodified lines | |||
| 459 | if (!dst_vma) | 460 | if (!dst_vma) |
| 460 | goto out_unlock; | 461 | goto out_unlock; |
| 461 | /* | 462 | /* |
| 462 | * Be strict and only allow __mcopy_atomic on userfaultfd | 463 | * Check the vma is registered in uffd, this is required to |
| 463 | * registered ranges to prevent userland errors going | 464 | * enforce the VM_MAYWRITE check done at uffd registration |
| 464 | * unnoticed. As far as the VM consistency is concerned, it | 465 | * time. |
| 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. | ||
| 469 | */ | 466 | */ |
| 470 | if (!dst_vma->vm_userfaultfd_ctx.ctx) | 467 | if (!dst_vma->vm_userfaultfd_ctx.ctx) |
| 471 | goto out_unlock; | 468 | goto out_unlock; |