tools/testing/selftests/mm/soft-dirty.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
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
On 8/18/26 7:02 PM, Anshuman wrote:
> 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>
> ---
Looks good to me.
Reviewed-by: Sarthak Sharma <sarthak.sharma@arm.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)
© 2016 - 2026 Red Hat, Inc.