io_uring/memmap: bound io_pin_pages() by page array byte size
PATCH 1/1 io_uring/memmap: bound io_pin_pages() by page array byte size
io_pin_pages() checks that nr_pages does not exceed INT_MAX, then allocates a struct page * array of nr_pages entries. kvmalloc() limits allocations to INT_MAX bytes, but the check counts pages, not bytes. On 64-bit each entry is 8 bytes, so the array hits the INT_MAX byte limit at INT_MAX / sizeof(struct page *) pages, well before the page count check fires.
Since commit b4e41050b212 ("io_uring/rsrc: raise registered buffer 1GB limit") raised the per-buffer cap to 1TB, a buffer near that cap maps ~2^28 pages, making the array allocation exceed INT_MAX bytes. This passes the page count check, reaches kvmalloc(), and triggers the WARN_ON_ONCE() for oversized allocations in __kvmalloc_node_noprof().
Check nr_pages against INT_MAX / sizeof(struct page *) so the buffer is rejected with -EOVERFLOW before the allocation is attempted.
--- io_uring/memmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/memmap.c +1 -1
@@ -53,7 +53,7 @@ struct page **io_pin_pages(unsigned long uaddr, unsigned long len, int *npages) nr_pages = end - start;if (WARN_ON_ONCE (!nr_pages))return ERR_PTR (-EINVAL ); -if (WARN_ON_ONCE (nr_pages >INT_MAX )) +if (nr_pages >INT_MAX /sizeof (struct page *))return ERR_PTR (-EOVERFLOW ); pages = kvmalloc_objs(struct page *, nr_pages, GFP_KERNEL_ACCOUNT);
Signature · 1 line
2.43.0
Quoted 13 lines · Deepanshu Kartikey
io_pin_pages()checks thatnr_pagesdoes not exceedINT_MAX, then allocates a struct page * array ofnr_pagesentries.kvmalloc()limits allocations toINT_MAXbytes, but the check counts pages, not bytes. On 64-bit each entry is 8 bytes, so the array hits theINT_MAXbyte limit atINT_MAX/sizeof(struct page *)pages, well before the page count check fires.Since commit
b4e41050b212("io_uring/rsrc: raise registered buffer 1GB limit") raised the per-buffer cap to 1TB, a buffer near that cap maps ~2^28 pages, making the array allocation exceedINT_MAXbytes. This passes the page count check, reacheskvmalloc(), and triggers theWARN_ON_ONCE()for oversized allocations in__kvmalloc_node_noprof().
Quoted 8 lines
Check
nr_pagesagainstINT_MAX/sizeof(struct page *)so the buffer is rejected with-EOVERFLOWbefore the allocation is attempted.Reported-by [email protected]Fixes b4e41050b212 ("io_uring/rsrc: raise registered buffer 1GB limit")Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>
Looks good, feel free to add:
Quoted 20 lines
--- io_uring/memmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)io_uring/memmap.c +1
-1@@ -53,7 +53,7 @@ struct page **io_pin_pages(unsigned long uaddr, unsigned long len, int *npages) nr_pages = end - start;if (WARN_ON_ONCE (!nr_pages))return ERR_PTR (-EINVAL ); -if (WARN_ON_ONCE (nr_pages >INT_MAX )) +if (nr_pages >INT_MAX /sizeof (struct page *))return ERR_PTR (-EOVERFLOW ); pages = kvmalloc_objs(struct page *, nr_pages, GFP_KERNEL_ACCOUNT);Signature · 1 line
2.43.0
Signature · 1 line
Gabriel Krisman Bertazi
Quoted 8 lines · Deepanshu Kartikey
io_pin_pages()checks thatnr_pagesdoes not exceedINT_MAX, then allocates a struct page * array ofnr_pagesentries.kvmalloc()limits allocations toINT_MAXbytes, but the check counts pages, not bytes. On 64-bit each entry is 8 bytes, so the array hits theINT_MAXbyte limit atINT_MAX/sizeof(struct page *)pages, well before the page count check fires.[...]
Applied, thanks!
[1/1] io_uring/memmap: bound io_pin_pages() by page array byte size
commit: 3996771b8f759729cba0a28007438c085f814d61
Best regards,
Signature · 1 line
Jens Axboe