Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/mm: fix unchecked ftruncate return value in soft-dirty test
@ 2026-08-18 13:32 Anshuman
  2026-08-19  4:04 ` Sarthak Sharma
  0 siblings, 1 reply; 3+ messages in thread
From: Anshuman @ 2026-08-18 13:32 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand
  Cc: Shuah Khan, linux-mm, linux-kselftest, linux-kernel, Anshuman

test_mprotect() calls ftruncate() to resize the backing file
before mmap()'ing it, but never checks the return value. If
ftruncate() fails, the file may remain shorter than the requested
mapping size. The subsequent mmap() with MAP_SHARED can still
succeed in this case, but the very next line writes directly into
the mapped memory (*map = 1), which can trigger SIGBUS if the
mapping extends beyond the actual file size.

Check the return value and fail cleanly with ksft_exit_fail_msg() if
ftruncate() fails, matching the error-handling style already used
for the mmap() call immediately below it.

Signed-off-by: Anshuman <anshumantewari123@gmail.com>
---
 tools/testing/selftests/mm/soft-dirty.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/soft-dirty.c b/tools/testing/selftests/mm/soft-dirty.c
index fb1864a68..a52ef79dd 100644
--- a/tools/testing/selftests/mm/soft-dirty.c
+++ b/tools/testing/selftests/mm/soft-dirty.c
@@ -152,7 +152,8 @@ static void test_mprotect(int pagemap_fd, int pagesize, bool anon)
 			return;
 		}
 		unlink(fname);
-		ftruncate(test_fd, pagesize);
+		if (ftruncate(test_fd, pagesize) != 0)
+			ksft_exit_fail_msg("ftruncate failed\n");
 		map = mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
 			   MAP_SHARED, test_fd, 0);
 		if (map == MAP_FAILED)
-- 
2.55.0



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

end of thread, other threads:[~2026-08-19 10:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-18 13:32 [PATCH] selftests/mm: fix unchecked ftruncate return value in soft-dirty test Anshuman
2026-08-19  4:04 ` Sarthak Sharma
2026-08-19 10:37   ` Anshuman Tewari

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox