* [PATCH] selftests/mm: compaction_test: Fix off by one in check_compaction()
@ 2024-08-09 12:32 Dan Carpenter
2024-08-09 17:20 ` Shuah Khan
0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2024-08-09 12:32 UTC (permalink / raw)
To: Dev Jain
Cc: Andrew Morton, Shuah Khan, linux-mm, linux-kselftest,
linux-kernel, kernel-janitors
The "initial_nr_hugepages" variable is unsigned long so it takes up to
20 characters to print, plus 1 more character for the NUL terminator.
Unfortunately, this buffer is not quite large enough for the terminator
to fit. Also use snprintf() for a belt and suspenders approach.
Fixes: fb9293b6b015 ("selftests/mm: compaction_test: fix bogus test success and reduce probability of OOM-killer invocation")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
tools/testing/selftests/mm/compaction_test.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/mm/compaction_test.c b/tools/testing/selftests/mm/compaction_test.c
index e140558e6f53..2c3a0eb6b22d 100644
--- a/tools/testing/selftests/mm/compaction_test.c
+++ b/tools/testing/selftests/mm/compaction_test.c
@@ -89,9 +89,10 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size,
int fd, ret = -1;
int compaction_index = 0;
char nr_hugepages[20] = {0};
- char init_nr_hugepages[20] = {0};
+ char init_nr_hugepages[24] = {0};
- sprintf(init_nr_hugepages, "%lu", initial_nr_hugepages);
+ snprintf(init_nr_hugepages, sizeof(init_nr_hugepages),
+ "%lu", initial_nr_hugepages);
/* We want to test with 80% of available memory. Else, OOM killer comes
in to play */
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] selftests/mm: compaction_test: Fix off by one in check_compaction()
2024-08-09 12:32 [PATCH] selftests/mm: compaction_test: Fix off by one in check_compaction() Dan Carpenter
@ 2024-08-09 17:20 ` Shuah Khan
2024-08-09 20:01 ` Dan Carpenter
0 siblings, 1 reply; 3+ messages in thread
From: Shuah Khan @ 2024-08-09 17:20 UTC (permalink / raw)
To: Dan Carpenter, Dev Jain
Cc: Andrew Morton, Shuah Khan, linux-mm, linux-kselftest,
linux-kernel, kernel-janitors, Shuah Khan
On 8/9/24 06:32, Dan Carpenter wrote:
> The "initial_nr_hugepages" variable is unsigned long so it takes up to
> 20 characters to print, plus 1 more character for the NUL terminator.
> Unfortunately, this buffer is not quite large enough for the terminator
> to fit. Also use snprintf() for a belt and suspenders approach.
>
> Fixes: fb9293b6b015 ("selftests/mm: compaction_test: fix bogus test success and reduce probability of OOM-killer invocation")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
> tools/testing/selftests/mm/compaction_test.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/compaction_test.c b/tools/testing/selftests/mm/compaction_test.c
> index e140558e6f53..2c3a0eb6b22d 100644
> --- a/tools/testing/selftests/mm/compaction_test.c
> +++ b/tools/testing/selftests/mm/compaction_test.c
> @@ -89,9 +89,10 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size,
> int fd, ret = -1;
> int compaction_index = 0;
> char nr_hugepages[20] = {0};
> - char init_nr_hugepages[20] = {0};
> + char init_nr_hugepages[24] = {0};
Can we exceed this limit too? Can you make this a define?
>
> - sprintf(init_nr_hugepages, "%lu", initial_nr_hugepages);
> + snprintf(init_nr_hugepages, sizeof(init_nr_hugepages),
> + "%lu", initial_nr_hugepages);
>
> /* We want to test with 80% of available memory. Else, OOM killer comes
> in to play */
With that change:
Reviewed-by: Shuah Khan <skhan@linuxfoundation.org>
thanks,
-- Shuah
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] selftests/mm: compaction_test: Fix off by one in check_compaction()
2024-08-09 17:20 ` Shuah Khan
@ 2024-08-09 20:01 ` Dan Carpenter
0 siblings, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2024-08-09 20:01 UTC (permalink / raw)
To: Shuah Khan
Cc: Dev Jain, Andrew Morton, Shuah Khan, linux-mm, linux-kselftest,
linux-kernel, kernel-janitors
On Fri, Aug 09, 2024 at 11:20:48AM -0600, Shuah Khan wrote:
> On 8/9/24 06:32, Dan Carpenter wrote:
> > The "initial_nr_hugepages" variable is unsigned long so it takes up to
> > 20 characters to print, plus 1 more character for the NUL terminator.
> > Unfortunately, this buffer is not quite large enough for the terminator
> > to fit. Also use snprintf() for a belt and suspenders approach.
> >
> > Fixes: fb9293b6b015 ("selftests/mm: compaction_test: fix bogus test success and reduce probability of OOM-killer invocation")
> > Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> > ---
> > tools/testing/selftests/mm/compaction_test.c | 5 +++--
> > 1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/testing/selftests/mm/compaction_test.c b/tools/testing/selftests/mm/compaction_test.c
> > index e140558e6f53..2c3a0eb6b22d 100644
> > --- a/tools/testing/selftests/mm/compaction_test.c
> > +++ b/tools/testing/selftests/mm/compaction_test.c
> > @@ -89,9 +89,10 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size,
> > int fd, ret = -1;
> > int compaction_index = 0;
> > char nr_hugepages[20] = {0};
> > - char init_nr_hugepages[20] = {0};
> > + char init_nr_hugepages[24] = {0};
>
> Can we exceed this limit too? Can you make this a define?
>
It's based on counting the digits in U64_MAX.
18446744073709551615X
123456789012345678901
We don't have any defines for that kind of thing. It's not a bad idea.
#define STRLEN_U64_MAX 20
char init_nr_hugepages[STRLEN_U64_MAX + 1];
But it should be done as part of a kernel wide clean up and not part of this
buffer overflow fix. The line above it could be changed as well, for example.
Let me create a KTODO and hope the internet will take care of it.
KTODO: create defines for STRLEN_[SU]8/16/32/64_MIN/MAX.
Btw, I rounded up to 24 just because I like buffer sizes that are divisible
by 4 but the compiler is probably going to do that automatically either way.
regards,
dan carpenter
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-08-09 20:01 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-09 12:32 [PATCH] selftests/mm: compaction_test: Fix off by one in check_compaction() Dan Carpenter
2024-08-09 17:20 ` Shuah Khan
2024-08-09 20:01 ` Dan Carpenter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox