Skip to contentSkip to conversation

printk: implement printk_buf overflow warning

PATCH In review Tejun Heo 2 participants 3 messages Feb 13, 2008 – Feb 14, 2008

lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

PATCH 1/1 printk: implement printk_buf overflow warning

printk silently truncates messages longer than 1024 - 1 bytes. Implement overflow detection and append "$PRINTK_BUF_OVERFLOW$\n" to truncated messages.

---
 kernel/printk.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)
kernel/printk.c +12 -3
index 074a3ea..419cd47 100644
@@ -42,6 +42,7 @@ void __attribute__((weak)) early_printk(const char *fmt, ...)
 {
 }
 
+#define PRINTK_BUF_LEN	1024
 #define __LOG_BUF_LEN	(1 << CONFIG_LOG_BUF_SHIFT)
 
 /* printk's without a loglevel use this.. */
@@ -619,9 +620,10 @@ asmlinkage int vprintk(const char *fmt, va_list args)
 	static volatile unsigned int printk_cpu = UINT_MAX;
 	static const char printk_recursion_bug_msg [] =
 		KERN_CRIT "BUG: recent printk recursion!\n";
+	static const char overflow_tag[] = "$PRINTK_BUF_OVERFLOW$\n";
 	static int printk_recursion_bug;
 	static int log_level_unknown = 1;
-	static char printk_buf[1024];
+	static char printk_buf[PRINTK_BUF_LEN + sizeof(overflow_tag) - 1];
 
 	unsigned long flags;
 	int printed_len = 0;
@@ -663,8 +665,15 @@ asmlinkage int vprintk(const char *fmt, va_list args)
 		printed_len = sizeof(printk_recursion_bug_msg);
 	}
 	/* Emit the output into the temporary buffer */
-	printed_len += vscnprintf(printk_buf + printed_len,
-				  sizeof(printk_buf) - printed_len, fmt, args);
+	printed_len += vsnprintf(printk_buf + printed_len,
+				 PRINTK_BUF_LEN - printed_len, fmt, args);
+
+	/* Check for overflow and tag the message if overflowed */
+	if (printed_len >= PRINTK_BUF_LEN) {
+		memcpy(printk_buf + PRINTK_BUF_LEN - 1, overflow_tag,
+		       sizeof(overflow_tag));
+		printed_len = sizeof(printk_buf) - 1;
+	}
 
 	/*
 	 * Copy the output into log_buf.  If the caller didn't provide
Signature · 1 line
1.5.2.4
#1 lore raw
Quoted 3 lines · On Wed, 13 Feb 2008 18:03:12 +0900 Tejun Heo <[email protected]> wrote:

printk silently truncates messages longer than 1024 - 1 bytes. Implement overflow detection and append "$PRINTK_BUF_OVERFLOW$\n" to truncated messages.

I don't think I've ever heard of anyone managing to overflow the printk buffer and it's hard to imagine how a sane person could manage to do this.

I assume this is happening because of changes in your other patchset, and that this change is hence associated with those changes and should probably be a part of the same patch series?

author
#2 lore raw
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Wed, 13 Feb 2008 18:03:12 +0900 Tejun Heo <[email protected]> wrote:

printk silently truncates messages longer than 1024 - 1 bytes. Implement overflow detection and append "$PRINTK_BUF_OVERFLOW$\n" to truncated messages.

Quoted 6 lines

I don't think I've ever heard of anyone managing to overflow the printk buffer and it's hard to imagine how a sane person could manage to do this.

I assume this is happening because of changes in your other patchset, and that this change is hence associated with those changes and should probably be a part of the same patch series?

I thought this went well with the recursion warning. I'll move it into the mprintk patchset.

Thanks.

Signature · 1 line
tejun