Skip to contentSkip to conversation

selftests/mm: fix unchecked ftruncate return value in soft-dirty test

PATCH Applied Anshuman 2 participants 3 messages Aug 18 – Aug 19

Subsystems (3)
lore
Share & tools
lore

Anshuman

author
#0 lore raw

PATCH 1/1 selftests/mm: fix unchecked ftruncate return value in soft-dirty test

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.

---
 tools/testing/selftests/mm/soft-dirty.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/soft-dirty.c +2 -1
index fb1864a68..a52ef79dd 100644
@@ -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)
Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · 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.

Quoted 6 lines

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.

---

Looks good to me.

Quoted 17 lines
 tools/testing/selftests/mm/soft-dirty.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/soft-dirty.c +2 -1
index fb1864a68..a52ef79dd 100644
@@ -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 lore raw

Thanks for the review , Sarthak - I apologize for delayed reply . Good news : Andrew picked it up into mm-unstable .

Anshuman

Quoted 43 lines · Sarthak Sharma
Quoted 8 lines · 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.

Quoted 6 lines

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.

---

Looks good to me.

Review

Quoted 4 lines
 tools/testing/selftests/mm/soft-dirty.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/tools/testing/selftests/mm/soft-dirty.c

tools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int

Quoted 4 lines

index fb1864a68..a52ef79dd 100644

tools/testing/selftests/mm/soft-dirty.c +0 -0
@@ -152,7 +152,8 @@ static void test_mprotect(int pagemap_fd, int

pagesize, bool anon)

Quoted 9 lines
tools/testing/selftests/mm/soft-dirty.c +2 -1
                      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)