All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/mm/pagemap_ioctl: Fix missing NULL checks after calloc()
@ 2026-07-21  6:36 longlong yan
  2026-07-21 14:55 ` David Hildenbrand (Arm)
  2026-07-22  0:18 ` SJ Park
  0 siblings, 2 replies; 3+ messages in thread
From: longlong yan @ 2026-07-21  6:36 UTC (permalink / raw)
  To: akpm, david, linux-kselftest, linux-kernel; +Cc: linux-mm, shuah, longlong yan

The pagemap_ioctl selftest allocates memory via calloc() in several
places but does not check the return values. If calloc() fails, the
subsequent code will dereference a NULL pointer and crash.

Additionally, in sanity_tests(), the calloc() failure check incorrectly
uses MAP_FAILED (the mmap() error constant) instead of NULL. Since
calloc() returns NULL on failure, the check never triggers and a
failed allocation goes undetected.

Add NULL checks after each calloc() call, and fix the wrong error
constant in sanity_tests(). Use ksft_exit_fail_msg() consistent with
the existing error handling pattern in the file.

Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
 tools/testing/selftests/mm/pagemap_ioctl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/pagemap_ioctl.c b/tools/testing/selftests/mm/pagemap_ioctl.c
index f9bcff8e78fa..f6629f44a13d 100644
--- a/tools/testing/selftests/mm/pagemap_ioctl.c
+++ b/tools/testing/selftests/mm/pagemap_ioctl.c
@@ -212,6 +212,8 @@ int userfaultfd_tests(void)
 
 	vec_size = mem_size/page_size;
 	vec = calloc(vec_size, sizeof(struct page_region));
+	if (!vec)
+		ksft_exit_fail_msg("error nomem\n");
 
 	written = pagemap_ioctl(mem, mem_size, vec, 1, PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC,
 				vec_size - 2, PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
@@ -699,6 +701,8 @@ int base_tests(char *prefix, char *mem, unsigned long long mem_size, int skip)
 	vec_size = mem_size/page_size;
 	vec = calloc(vec_size, sizeof(struct page_region));
 	vec2 = calloc(vec_size, sizeof(struct page_region));
+	if (!vec || !vec2)
+		ksft_exit_fail_msg("error nomem\n");
 
 	/* 1. all new pages must be not be written (dirty) */
 	written = pagemap_ioctl(mem, mem_size, vec, 1, PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC,
@@ -1000,6 +1004,8 @@ int unmapped_region_tests(void)
 	int written, len = 0x00040000;
 	long vec_size = len / page_size;
 	struct page_region *vec = calloc(vec_size, sizeof(struct page_region));
+	if (!vec)
+		ksft_exit_fail_msg("error nomem\n");
 
 	/* 1. Get written pages */
 	written = pagemap_ioctl(start, len, vec, vec_size, 0, 0,
@@ -1116,7 +1122,7 @@ int sanity_tests(void)
 
 	vec = calloc(vec_size, sizeof(struct page_region));
 	mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
-	if (mem == MAP_FAILED || vec == MAP_FAILED)
+	if (mem == MAP_FAILED || !vec)
 		ksft_exit_fail_msg("error nomem\n");
 
 	wp_init(mem, mem_size);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests/mm/pagemap_ioctl: Fix missing NULL checks after calloc()
  2026-07-21  6:36 [PATCH] selftests/mm/pagemap_ioctl: Fix missing NULL checks after calloc() longlong yan
@ 2026-07-21 14:55 ` David Hildenbrand (Arm)
  2026-07-22  0:18 ` SJ Park
  1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-21 14:55 UTC (permalink / raw)
  To: longlong yan, akpm, linux-kselftest, linux-kernel; +Cc: linux-mm, shuah

On 7/21/26 08:36, longlong yan wrote:
> The pagemap_ioctl selftest allocates memory via calloc() in several
> places but does not check the return values. If calloc() fails, the
> subsequent code will dereference a NULL pointer and crash.
> 
> Additionally, in sanity_tests(), the calloc() failure check incorrectly
> uses MAP_FAILED (the mmap() error constant) instead of NULL. Since
> calloc() returns NULL on failure, the check never triggers and a
> failed allocation goes undetected.
> 
> Add NULL checks after each calloc() call, and fix the wrong error
> constant in sanity_tests(). Use ksft_exit_fail_msg() consistent with
> the existing error handling pattern in the file.
> 
> Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
> ---
>  tools/testing/selftests/mm/pagemap_ioctl.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/mm/pagemap_ioctl.c b/tools/testing/selftests/mm/pagemap_ioctl.c
> index f9bcff8e78fa..f6629f44a13d 100644
> --- a/tools/testing/selftests/mm/pagemap_ioctl.c
> +++ b/tools/testing/selftests/mm/pagemap_ioctl.c
> @@ -212,6 +212,8 @@ int userfaultfd_tests(void)
>  
>  	vec_size = mem_size/page_size;
>  	vec = calloc(vec_size, sizeof(struct page_region));
> +	if (!vec)
> +		ksft_exit_fail_msg("error nomem\n");
>  
>  	written = pagemap_ioctl(mem, mem_size, vec, 1, PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC,
>  				vec_size - 2, PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
> @@ -699,6 +701,8 @@ int base_tests(char *prefix, char *mem, unsigned long long mem_size, int skip)
>  	vec_size = mem_size/page_size;
>  	vec = calloc(vec_size, sizeof(struct page_region));
>  	vec2 = calloc(vec_size, sizeof(struct page_region));
> +	if (!vec || !vec2)
> +		ksft_exit_fail_msg("error nomem\n");
>  
>  	/* 1. all new pages must be not be written (dirty) */
>  	written = pagemap_ioctl(mem, mem_size, vec, 1, PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC,
> @@ -1000,6 +1004,8 @@ int unmapped_region_tests(void)
>  	int written, len = 0x00040000;
>  	long vec_size = len / page_size;
>  	struct page_region *vec = calloc(vec_size, sizeof(struct page_region));
> +	if (!vec)
> +		ksft_exit_fail_msg("error nomem\n");
>  
>  	/* 1. Get written pages */
>  	written = pagemap_ioctl(start, len, vec, vec_size, 0, 0,
> @@ -1116,7 +1122,7 @@ int sanity_tests(void)
>  
>  	vec = calloc(vec_size, sizeof(struct page_region));
>  	mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
> -	if (mem == MAP_FAILED || vec == MAP_FAILED)
> +	if (mem == MAP_FAILED || !vec)
>  		ksft_exit_fail_msg("error nomem\n");
>  
>  	wp_init(mem, mem_size);

Acked-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests/mm/pagemap_ioctl: Fix missing NULL checks after calloc()
  2026-07-21  6:36 [PATCH] selftests/mm/pagemap_ioctl: Fix missing NULL checks after calloc() longlong yan
  2026-07-21 14:55 ` David Hildenbrand (Arm)
@ 2026-07-22  0:18 ` SJ Park
  1 sibling, 0 replies; 3+ messages in thread
From: SJ Park @ 2026-07-22  0:18 UTC (permalink / raw)
  To: longlong yan
  Cc: SJ Park, akpm, david, linux-kselftest, linux-kernel, linux-mm,
	shuah

Hello longlong,


I found get_maitnainer.pl is suggesting to Cc below recipients.

- Lorenzo Stoakes <ljs@kernel.org>
- "Liam R. Howlett" <liam@infradead.org>
- Vlastimil Babka <vbabka@kernel.org>
- Mike Rapoport <rppt@kernel.org>
- Suren Baghdasaryan <surenb@google.com>
- Michal Hocko <mhocko@suse.com>

On Tue, 21 Jul 2026 14:36:11 +0800 longlong yan <yanlonglong@kylinos.cn> wrote:

> The pagemap_ioctl selftest allocates memory via calloc() in several
> places but does not check the return values. If calloc() fails, the
> subsequent code will dereference a NULL pointer and crash.
> 
> Additionally, in sanity_tests(), the calloc() failure check incorrectly
> uses MAP_FAILED (the mmap() error constant) instead of NULL. Since
> calloc() returns NULL on failure, the check never triggers and a
> failed allocation goes undetected.
> 
> Add NULL checks after each calloc() call, and fix the wrong error
> constant in sanity_tests(). Use ksft_exit_fail_msg() consistent with
> the existing error handling pattern in the file.

Makes sense to me.

> 
> Signed-off-by: longlong yan <yanlonglong@kylinos.cn>

I have a trivial comment below.  Regardless of that,

Reviewed-by: SJ Park <sj@kernel.org>

> ---
>  tools/testing/selftests/mm/pagemap_ioctl.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
[...]
> @@ -1000,6 +1004,8 @@ int unmapped_region_tests(void)
>  	int written, len = 0x00040000;
>  	long vec_size = len / page_size;
>  	struct page_region *vec = calloc(vec_size, sizeof(struct page_region));
> +	if (!vec)
> +		ksft_exit_fail_msg("error nomem\n");

I'd suggest to separate the definitions and statements by putting an empty line
in the middle.


Thanks,
SJ

[...]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-22  0:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-21  6:36 [PATCH] selftests/mm/pagemap_ioctl: Fix missing NULL checks after calloc() longlong yan
2026-07-21 14:55 ` David Hildenbrand (Arm)
2026-07-22  0:18 ` SJ Park

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.