Skip to content

Commit 06fe9b1

Browse files
committed
io_uring: don't attempt to mmap larger than what the user asks for
If IORING_FEAT_SINGLE_MMAP is ignored, as can happen if an application uses an ancient liburing or does setup manually, then 3 mmap's are required to map the ring into userspace. The kernel will still have collapsed the mappings, however userspace may ask for mapping them individually. If so, then we should not use the full number of ring pages, as it may exceed the partial mapping. Doing so will yield an -EFAULT from vm_insert_pages(), as we pass in more pages than what the application asked for. Cap the number of pages to match what the application asked for, for the particular mapping operation. Reported-by: Lucas Mülling <[email protected]> Link: axboe/liburing#1157 Fixes: 3ab1db3 ("io_uring: get rid of remap_pfn_range() for mapping rings/sqes") Signed-off-by: Jens Axboe <[email protected]>
1 parent 1613e60 commit 06fe9b1

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

io_uring/memmap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
244244
struct io_ring_ctx *ctx = file->private_data;
245245
size_t sz = vma->vm_end - vma->vm_start;
246246
long offset = vma->vm_pgoff << PAGE_SHIFT;
247+
unsigned int npages;
247248
void *ptr;
248249

249250
ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
@@ -253,8 +254,8 @@ __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
253254
switch (offset & IORING_OFF_MMAP_MASK) {
254255
case IORING_OFF_SQ_RING:
255256
case IORING_OFF_CQ_RING:
256-
return io_uring_mmap_pages(ctx, vma, ctx->ring_pages,
257-
ctx->n_ring_pages);
257+
npages = min(ctx->n_ring_pages, (sz + PAGE_SIZE - 1) >> PAGE_SHIFT);
258+
return io_uring_mmap_pages(ctx, vma, ctx->ring_pages, npages);
258259
case IORING_OFF_SQES:
259260
return io_uring_mmap_pages(ctx, vma, ctx->sqe_pages,
260261
ctx->n_sqe_pages);

0 commit comments

Comments
 (0)