diff options
| author | longlong yan <yanlonglong@kylinos.cn> | 2026-07-21 14:36:11 +0800 |
|---|---|---|
| committer | Andrew Morton <akpm@linux-foundation.org> | 2026-07-25 21:50:58 -0700 |
| commit | 48c0fa50e797f936f361e84e509d66e518ad7faf (patch) | |
| tree | 36c05ea737ea64c8a1149ede9a75e262cfb10d7b /tools | |
| parent | 58fd58b98d1a33a50e1826cc328ea244916dbc8e (diff) | |
| download | linux-next-48c0fa50e797f936f361e84e509d66e518ad7faf.tar.gz linux-next-48c0fa50e797f936f361e84e509d66e518ad7faf.zip | |
selftests/mm/pagemap_ioctl: fix missing NULL checks after calloc()
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.
Link: https://lore.kernel.org/20260721063611.342-1-yanlonglong@kylinos.cn
Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: SJ Park <sj@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/testing/selftests/mm/pagemap_ioctl.c | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/tools/testing/selftests/mm/pagemap_ioctl.c b/tools/testing/selftests/mm/pagemap_ioctl.c index f6ab626a90b4..1b2dffcc999b 100644 --- a/tools/testing/selftests/mm/pagemap_ioctl.c +++ b/tools/testing/selftests/mm/pagemap_ioctl.c @@ -213,6 +213,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); @@ -700,6 +702,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, @@ -1001,6 +1005,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, @@ -1163,7 +1169,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); |
