* [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
* Re: [PATCH] selftests/mm: fix unchecked ftruncate return value in soft-dirty test
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
0 siblings, 1 reply; 3+ messages in thread
From: Sarthak Sharma @ 2026-08-19 4:04 UTC (permalink / raw)
To: Anshuman, Andrew Morton, David Hildenbrand
Cc: Shuah Khan, linux-mm, linux-kselftest, linux-kernel
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)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/mm: fix unchecked ftruncate return value in soft-dirty test
2026-08-19 4:04 ` Sarthak Sharma
@ 2026-08-19 10:37 ` Anshuman Tewari
0 siblings, 0 replies; 3+ messages in thread
From: Anshuman Tewari @ 2026-08-19 10:37 UTC (permalink / raw)
To: Sarthak Sharma
Cc: Andrew Morton, David Hildenbrand, Shuah Khan, linux-mm,
linux-kselftest, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 1909 bytes --]
Thanks for the review , Sarthak - I apologize for delayed reply . Good news
: Andrew picked it up into mm-unstable .
Anshuman
On Wed, 19 Aug 2026 at 09:35, Sarthak Sharma <sarthak.sharma@arm.com> wrote:
>
>
> 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)
>
>
[-- Attachment #2: Type: text/html, Size: 2670 bytes --]
^ permalink raw reply [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