linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] printk: Convert a use of sprintf to snprintf in console_unlock
@ 2020-01-30 22:16 Nathan Chancellor
  2020-01-31  7:02 ` Sergey Senozhatsky
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Chancellor @ 2020-01-30 22:16 UTC (permalink / raw)
  To: Petr Mladek, Sergey Senozhatsky
  Cc: Steven Rostedt, linux-kernel, clang-built-linux, Nathan Chancellor

When CONFIG_PRINTK is disabled (e.g. when building allnoconfig), clang
warns:

../kernel/printk/printk.c:2416:10: warning: 'sprintf' will always
overflow; destination buffer has size 0, but format string expands to at
least 33 [-Wfortify-source]
                        len = sprintf(text,
                              ^
1 warning generated.

It is not wrong; text has a zero size when CONFIG_PRINTK is disabled
because LOG_LINE_MAX and PREFIX_MAX are both zero. Change to snprintf so
that this case is explicitly handled without any risk of overflow.

Link: https://github.com/ClangBuiltLinux/linux/issues/846
Link: https://github.com/llvm/llvm-project/commit/6d485ff455ea2b37fef9e06e426dae6c1241b231
Link: https://lore.kernel.org/lkml/20200130051711.GF115889@google.com/
Suggested-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index fada22dc4ab6..a44094727a5c 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2413,9 +2413,9 @@ void console_unlock(void)
 		printk_safe_enter_irqsave(flags);
 		raw_spin_lock(&logbuf_lock);
 		if (console_seq < log_first_seq) {
-			len = sprintf(text,
-				      "** %llu printk messages dropped **\n",
-				      log_first_seq - console_seq);
+			len = snprintf(text, sizeof(text),
+				       "** %llu printk messages dropped **\n",
+				       log_first_seq - console_seq);
 
 			/* messages are gone, move to first one */
 			console_seq = log_first_seq;
-- 
2.25.0


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

* Re: [PATCH] printk: Convert a use of sprintf to snprintf in console_unlock
  2020-01-30 22:16 [PATCH] printk: Convert a use of sprintf to snprintf in console_unlock Nathan Chancellor
@ 2020-01-31  7:02 ` Sergey Senozhatsky
  2020-02-11 13:05   ` Petr Mladek
  0 siblings, 1 reply; 3+ messages in thread
From: Sergey Senozhatsky @ 2020-01-31  7:02 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, linux-kernel,
	clang-built-linux

On (20/01/30 15:16), Nathan Chancellor wrote:
> When CONFIG_PRINTK is disabled (e.g. when building allnoconfig), clang
> warns:
> 
> ../kernel/printk/printk.c:2416:10: warning: 'sprintf' will always
> overflow; destination buffer has size 0, but format string expands to at
> least 33 [-Wfortify-source]
>                         len = sprintf(text,
>                               ^
> 1 warning generated.
> 
> It is not wrong; text has a zero size when CONFIG_PRINTK is disabled
> because LOG_LINE_MAX and PREFIX_MAX are both zero. Change to snprintf so
> that this case is explicitly handled without any risk of overflow.

We probably can add a note here that for !CONFIG_PRINTK builds
logbuf overflow is very unlikely.

Otherwise,
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>

	-ss

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

* Re: [PATCH] printk: Convert a use of sprintf to snprintf in console_unlock
  2020-01-31  7:02 ` Sergey Senozhatsky
@ 2020-02-11 13:05   ` Petr Mladek
  0 siblings, 0 replies; 3+ messages in thread
From: Petr Mladek @ 2020-02-11 13:05 UTC (permalink / raw)
  To: Sergey Senozhatsky
  Cc: Nathan Chancellor, Sergey Senozhatsky, Steven Rostedt,
	linux-kernel, clang-built-linux

On Fri 2020-01-31 16:02:37, Sergey Senozhatsky wrote:
> On (20/01/30 15:16), Nathan Chancellor wrote:
> > When CONFIG_PRINTK is disabled (e.g. when building allnoconfig), clang
> > warns:
> > 
> > ../kernel/printk/printk.c:2416:10: warning: 'sprintf' will always
> > overflow; destination buffer has size 0, but format string expands to at
> > least 33 [-Wfortify-source]
> >                         len = sprintf(text,
> >                               ^
> > 1 warning generated.
> > 
> > It is not wrong; text has a zero size when CONFIG_PRINTK is disabled
> > because LOG_LINE_MAX and PREFIX_MAX are both zero. Change to snprintf so
> > that this case is explicitly handled without any risk of overflow.
> 
> We probably can add a note here that for !CONFIG_PRINTK builds
> logbuf overflow is very unlikely.

Good point. Well, the sprintf() was used for a well defined string:
"** %llu printk messages dropped **\n" ""

It could overflow only when anyone modified LOG_LINE_MAX to
something really small. It was not the case upstream, definitely.

> Otherwise,
> Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>

The patch has been committed into printk.git, branch for-5.7.

I did not add any extra comment to keep it simple. I hope
that it is ok.

Best Regards,
Petr

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

end of thread, other threads:[~2020-02-11 13:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-30 22:16 [PATCH] printk: Convert a use of sprintf to snprintf in console_unlock Nathan Chancellor
2020-01-31  7:02 ` Sergey Senozhatsky
2020-02-11 13:05   ` Petr Mladek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).