Skip to contentSkip to conversation

printk: Convert a use of sprintf to snprintf in console_unlock

PATCH In review Nathan Chancellor 3 participants 3 messages Jan 30, 2020 – Feb 11, 2020 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 printk: Convert a use of sprintf to snprintf in console_unlock

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.

---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/printk/printk.c +3 -3
index fada22dc4ab6..a44094727a5c 100644
@@ -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;
Signature · 1 line
2.25.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · 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.

Quoted 3 lines

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,

	-ss
maintainer
#2 lore raw
Quoted 11 lines · Sergey Senozhatsky
Quoted 10 lines · 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.

Quoted 6 lines
Quoted 3 lines

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.

Quoted 2 lines

Otherwise,

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