selftests/mm: fix unchecked ftruncate return value in soft-dirty test
Subsystems (3)
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
@@ -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
Quoted 8 lines · Anshuman
test_mprotect()callsftruncate()to resize the backing file before mmap()'ing it, but never checks the return value. Ifftruncate()fails, the file may remain shorter than the requested mapping size. The subsequentmmap()withMAP_SHAREDcan 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()ifftruncate()fails, matching the error-handling style already used for themmap()call immediately below it.Signed-off-by Anshuman <[email protected]>---
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@@ -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 )
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()callsftruncate()to resize the backing file before mmap()'ing it, but never checks the return value. Ifftruncate()fails, the file may remain shorter than the requested mapping size. The subsequentmmap()withMAP_SHAREDcan 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()ifftruncate()fails, matching the error-handling style already used for themmap()call immediately below it.Signed-off-by Anshuman <[email protected]>---
Looks good to me.
Reviewed-by Sarthak Sharma <[email protected]>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.ctools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int
Quoted 4 lines
index
fb1864a68..a52ef79dd100644tools/testing/selftests/mm/soft-dirty.c +0
-0@@ -152,7 +152,8 @@ static void test_mprotect(int pagemap_fd, intpagesize, bool anon)
Quoted 9 lines
tools/testing/selftests/mm/soft-dirty.c +2
-1return ; }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 )