Skip to contentSkip to conversation

mm/page_alloc: __alloc_pages_bulk(): do bounds check before accessing array

PATCH Applied Rasmus Villemoes 3 participants 4 messages May 7, 2021 – Jun 23, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/page_alloc: __alloc_pages_bulk(): do bounds check before accessing array

In the event that somebody would call this with an already fully populated page_array, the last loop iteration would do an access beyond the end of page_array.

It's of course extremely unlikely that would ever be done, but this triggers my internal static analyzer. Also, if it really is not supposed to be invoked this way (i.e., with no NULL entries in page_array), the nr_populated<nr_pages check could simply be removed instead.

Fixes 0f87d9d30f21 (mm/page_alloc: add an array-based interface to the bulk page allocator)
---
 mm/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
index bcdc0c6f21f1..66785946eb28 100644
@@ -5053,7 +5053,7 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,
 	 * Skip populated array elements to determine if any pages need
 	 * to be allocated before disabling IRQs.
 	 */
-	while (page_array && page_array[nr_populated] && nr_populated < nr_pages)
+	while (page_array && nr_populated < nr_pages && page_array[nr_populated])
 		nr_populated++;
 
 	/* Use the single page allocator for one page. */
Signature · 1 line
2.29.2
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Rasmus Villemoes

In the event that somebody would call this with an already fully populated page_array, the last loop iteration would do an access beyond the end of page_array.

Quoted 8 lines

It's of course extremely unlikely that would ever be done, but this triggers my internal static analyzer. Also, if it really is not supposed to be invoked this way (i.e., with no NULL entries in page_array), the nr_populated<nr_pages check could simply be removed instead.

Fixes 0f87d9d30f21 (mm/page_alloc: add an array-based interface to the bulk page allocator)
Signature · 2 lines
Mel Gorman
SUSE Labs
#2 lore raw
Quoted 11 lines · Mel Gorman
Quoted 10 lines · Rasmus Villemoes

In the event that somebody would call this with an already fully populated page_array, the last loop iteration would do an access beyond the end of page_array.

It's of course extremely unlikely that would ever be done, but this triggers my internal static analyzer. Also, if it really is not supposed to be invoked this way (i.e., with no NULL entries in page_array), the nr_populated<nr_pages check could simply be removed instead.

Quoted 5 lines
Quoted 2 lines
Fixes 0f87d9d30f21 (mm/page_alloc: add an array-based interface to the bulk page allocator)

Andrew, will you get this to Linus before 5.13 is released? I got a mail on May 9 that it had been added to your queue, but I don't see it in master yet.

Rasmus

maintainer
#3 lore raw
Quoted 15 lines · On Mon, 21 Jun 2021 18:01:17 +0200 Rasmus Villemoes <[email protected]>…
Quoted 14 lines · Mel Gorman
Quoted 12 lines · Rasmus Villemoes

In the event that somebody would call this with an already fully populated page_array, the last loop iteration would do an access beyond the end of page_array.

It's of course extremely unlikely that would ever be done, but this triggers my internal static analyzer. Also, if it really is not supposed to be invoked this way (i.e., with no NULL entries in page_array), the nr_populated<nr_pages check could simply be removed instead.

Fixes 0f87d9d30f21 (mm/page_alloc: add an array-based interface to the bulk page allocator)
Quoted 6 lines
Quoted 2 lines

Andrew, will you get this to Linus before 5.13 is released? I got a mail on May 9 that it had been added to your queue, but I don't see it in master yet.

I had it queued for 5.14-rc1.

I'll move it into the 5.13 queue as it fixes a post-5.12 thing, but nothing in the changelog indicates that it's at all urgent? Was the changelog missing some important info?

: In the event that somebody would call this with an already fully populated : page_array, the last loop iteration would do an access beyond the end of : page_array.
:
: It's of course extremely unlikely that would ever be done, but this : triggers my internal static analyzer. Also, if it really is not supposed : to be invoked this way (i.e., with no NULL entries in page_array), the : nr_populated<nr_pages check could simply be removed instead.