mm/gup_test: prevent overflow and report actual pinned bytes
Subsystems (2)
v3 cover mm/gup_test: prevent overflow and report actual pinned bytes
__gup_test_ioctl() accepts the number of pages per call from userspace. Its batch end calculation can overflow and wrap around, bypassing the existing check. This can cause GUP to write beyond the allocated pages array. This issue was reported by Sashiko.
Also, gup->size is calculated from address difference instead of the actual pages pinned. This can report partial pages pinned as covering the whole requested range.
Prevent overflow by clamping each batch before calculating next and report the actual bytes pinned.
Changes in v3:
- Add a new patch to prevent overflow in GUP batch calculation
Changes in v2:
- Fix a typo in the commit message
Previous versions:
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Sarthak Sharma (2):
mm/gup_test: prevent overflow in GUP batch calculation mm/gup_test: report actual pinned bytes
mm/gup_test.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
Signature · 1 line
2.53.0
PATCH v3 1/2 mm/gup_test: prevent overflow in GUP batch calculation
__gup_test_ioctl() calculates the end of a GUP batch using:
next = addr + nr * PAGE_SIZE;
If nr is too large, it can cause the next to overflow and wrap around. If it wraps, the next > end check is bypassed and a large value of nr is passed to the gup call, even though the pages array was allocated according to gup->size. This can lead to out of bounds writes.
Compare nr with the number of pages remaining before performing the multiplication. Clamp it to remaining range so that next does not overflow or exceed end.
--- mm/gup_test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
mm/gup_test.c +3 -2
@@ -139,10 +139,11 @@ static int __gup_test_ioctl(unsigned int cmd,if (nr != gup->nr_pages_per_call)break ; - next = addr + nr *PAGE_SIZE ; -if (next > end) { +if (nr > (end - addr) /PAGE_SIZE ) { next = end; nr = (next - addr) /PAGE_SIZE ; + }else { + next = addr + nr *PAGE_SIZE ; }switch (cmd) {
Signature · 1 line
2.53.0
Re: [PATCH v3 1/2] mm/gup_test: prevent overflow in GUP batch calculation
Quoted 27 lines · Sarthak Sharma
__gup_test_ioctl()calculates the end of a GUP batch using:next = addr + nr *
PAGE_SIZE;If nr is too large, it can cause the next to overflow and wrap around. If it wraps, the next > end check is bypassed and a large value of nr is passed to the gup call, even though the pages array was allocated according to
gup->size. This can lead to out of bounds writes.Compare nr with the number of pages remaining before performing the multiplication. Clamp it to remaining range so that next does not overflow or exceed end.
Fixes 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking")Signed-off-by Sarthak Sharma <[email protected]>--- mm/gup_test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)mm/gup_test.c +0
-0@@ -139,10 +139,11 @@ static int __gup_test_ioctl(unsigned int cmd,if (nr != gup->nr_pages_per_call)break ;
mm/gup_test.c static int __gup_test_ioctl(unsigned int cmd, · line 149
Quoted 10 lines
mm/gup_test.c +3
-2- next = addr + nr *PAGE_SIZE ; -if (next > end) { +if (nr > (end - addr) /PAGE_SIZE ) { next = end; nr = (next - addr) /PAGE_SIZE ; + }else { + next = addr + nr *PAGE_SIZE ; }switch (cmd) {
What about this:
nr = min(nr, (end - addr) / PAGE_SIZE);
next = addr + nr * PAGE_SIZE;Seems to be easier to follow, no?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH v3 1/2] mm/gup_test: prevent overflow in GUP batch calculation
Hi Kiryl!
Quoted 37 lines · Kiryl Shutsemau
Quoted 36 lines · Sarthak Sharma
__gup_test_ioctl()calculates the end of a GUP batch using:next = addr + nr *
PAGE_SIZE;If nr is too large, it can cause the next to overflow and wrap around. If it wraps, the next > end check is bypassed and a large value of nr is passed to the gup call, even though the pages array was allocated according to
gup->size. This can lead to out of bounds writes.Compare nr with the number of pages remaining before performing the multiplication. Clamp it to remaining range so that next does not overflow or exceed end.
Fixes 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking")Signed-off-by Sarthak Sharma <[email protected]>--- mm/gup_test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)mm/gup_test.c +3
-2@@ -139,10 +139,11 @@ static int __gup_test_ioctl(unsigned int cmd,if (nr != gup->nr_pages_per_call)break ; - next = addr + nr *PAGE_SIZE ; -if (next > end) { +if (nr > (end - addr) /PAGE_SIZE ) { next = end; nr = (next - addr) /PAGE_SIZE ; + }else { + next = addr + nr *PAGE_SIZE ; }
Quoted 8 lines
mm/gup_test.c static int __gup_test_ioctl(unsigned int cmd, · line 149
Quoted 1 line
mm/gup_test.c +0
-0switch (cmd) {What about this:
nr = min(nr, (end - addr) / PAGE_SIZE); next = addr + nr * PAGE_SIZE;Seems to be easier to follow, no?
Yup, this would be cleaner. Do you want me to respin with this changed?
Re: [PATCH v3 1/2] mm/gup_test: prevent overflow in GUP batch calculation
Quoted 6 lines · Sarthak Sharma
Hi Kiryl!
Quoted 3 lines · Kiryl Shutsemau
On Tue, Sep 01, 2026 at 02:04:51PM +0530, Sarthak Sharma wrote:
What about this:
Quoted 6 lines
Quoted 4 lines
nr = min(nr, (end - addr) / PAGE_SIZE); next = addr + nr * PAGE_SIZE;Seems to be easier to follow, no?
Yup, this would be cleaner. Do you want me to respin with this changed?
Let's wait a bit for more feedback.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
PATCH v3 2/2 mm/gup_test: report actual pinned bytes
In reply to Sarthak Sharma__gup_test_ioctl() advances addr to the end of the current batch before checking if GUP pinned the entire requested batch. If GUP pins more than 0 pages but less than the requested batch size, addr still advances by the requested batch size.
The next iteration detects the partial pinning and breaks out of the loop. Again gup->size is calculated using addr - gup->addr, so it also includes the unpinned pages of the requested batch.
Calculate gup->size using the actual number of pages pinned multiplied by PAGE_SIZE.
--- mm/gup_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/gup_test.c +1 -1
@@ -189,7 +189,7 @@ static int __gup_test_ioctl(unsigned int cmd, nr_pages = i; gup->get_delta_usec =ktime_us_delta (end_time, start_time); - gup->size = addr - gup->addr; + gup->size = nr_pages *PAGE_SIZE ;/* * Take an un-benchmark-timed moment to verify DMA pinned
Signature · 1 line
2.53.0
Re: [PATCH v3 2/2] mm/gup_test: report actual pinned bytes
Quoted 10 lines · Sarthak Sharma
__gup_test_ioctl()advances addr to the end of the current batch before checking if GUP pinned the entire requested batch. If GUP pins more than 0 pages but less than the requested batch size, addr still advances by the requested batch size.The next iteration detects the partial pinning and breaks out of the loop. Again
gup->sizeis calculated using addr -gup->addr, so it also includes the unpinned pages of the requested batch.[...]
Applied, thanks!
tree: mm/core.git
branch: gup-7.4.misc
Applied patches will appear soon in mm-next, followed by linux-next.
Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.
It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.
[2/2] mm/gup_test: report actual pinned bytes
https://git.kernel.org/mm/core/c/65a79b9b
Signature · 3 lines
Cheers, David