linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/damon/dbgfs: fix bogus string length
@ 2024-02-02 12:43 Arnd Bergmann
  2024-02-02 17:14 ` SeongJae Park
  0 siblings, 1 reply; 2+ messages in thread
From: Arnd Bergmann @ 2024-02-02 12:43 UTC (permalink / raw)
  To: SeongJae Park, Andrew Morton; +Cc: Arnd Bergmann, damon, linux-mm, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

gcc correctly points out that using strnlen() on a fixed size array
is nonsense with an overlong limit:

mm/damon/dbgfs.c: In function 'damon_dbgfs_deprecated_read':
mm/damon/dbgfs.c:814:19: error: 'strnlen' specified bound 1024 exceeds source size 512 [-Werror=stringop-overread]
  814 |         int len = strnlen(kbuf, 1024);
      |                   ^~~~~~~~~~~~~~~~~~~
mm/damon/dbgfs.c:813:14: note: source object allocated here
  813 |         char kbuf[512] = DAMON_DBGFS_DEPRECATION_NOTICE;
      |              ^~~~

In fact, neither of the arbitrary limits are needed here: The first
one can just be a static const string and avoid wasting any more
space then necessary, and the strnlen() can be either strlen() or
sizeof(kbuf)-1, both of which the compiler turns into the same
constant here.

Fixes: adf9047adfff ("mm/damon/dbgfs: implement deprecation notice file")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 mm/damon/dbgfs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c
index f7abbc0633aa..4a189eb65d14 100644
--- a/mm/damon/dbgfs.c
+++ b/mm/damon/dbgfs.c
@@ -810,10 +810,9 @@ static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
 static ssize_t damon_dbgfs_deprecated_read(struct file *file,
 		char __user *buf, size_t count, loff_t *ppos)
 {
-	char kbuf[512] = DAMON_DBGFS_DEPRECATION_NOTICE;
-	int len = strnlen(kbuf, 1024);
+	static const char kbuf[] = DAMON_DBGFS_DEPRECATION_NOTICE;
 
-	return simple_read_from_buffer(buf, count, ppos, kbuf, len);
+	return simple_read_from_buffer(buf, count, ppos, kbuf, strlen(kbuf));
 }
 
 /*
-- 
2.39.2



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] mm/damon/dbgfs: fix bogus string length
  2024-02-02 12:43 [PATCH] mm/damon/dbgfs: fix bogus string length Arnd Bergmann
@ 2024-02-02 17:14 ` SeongJae Park
  0 siblings, 0 replies; 2+ messages in thread
From: SeongJae Park @ 2024-02-02 17:14 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: SeongJae Park, Andrew Morton, Arnd Bergmann, damon, linux-mm,
	linux-kernel

Hi Arnd,

On Fri,  2 Feb 2024 13:43:26 +0100 Arnd Bergmann <arnd@kernel.org> wrote:

> From: Arnd Bergmann <arnd@arndb.de>
> 
> gcc correctly points out that using strnlen() on a fixed size array
> is nonsense with an overlong limit:
> 
> mm/damon/dbgfs.c: In function 'damon_dbgfs_deprecated_read':
> mm/damon/dbgfs.c:814:19: error: 'strnlen' specified bound 1024 exceeds source size 512 [-Werror=stringop-overread]
>   814 |         int len = strnlen(kbuf, 1024);
>       |                   ^~~~~~~~~~~~~~~~~~~
> mm/damon/dbgfs.c:813:14: note: source object allocated here
>   813 |         char kbuf[512] = DAMON_DBGFS_DEPRECATION_NOTICE;
>       |              ^~~~
> 
> In fact, neither of the arbitrary limits are needed here: The first
> one can just be a static const string and avoid wasting any more
> space then necessary, and the strnlen() can be either strlen() or
> sizeof(kbuf)-1, both of which the compiler turns into the same
> constant here.

Thank you for this fix!

> 
> Fixes: adf9047adfff ("mm/damon/dbgfs: implement deprecation notice file")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

Reviewed-by: SeongJae Park <sj@kernel.org>


Thanks,
SJ

[...]


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-02 17:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-02 12:43 [PATCH] mm/damon/dbgfs: fix bogus string length Arnd Bergmann
2024-02-02 17:14 ` SeongJae Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox