Skip to contentSkip to conversation

Fix faulty logic in the case of recursive printk

PATCH In review Patrick Palka 4 participants 7 messages Aug 24, 2014 – Aug 27, 2014 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix faulty logic in the case of recursive printk

We shouldn't set text_len in the code path that detects printk recursion because text_len corresponds to the length of the string inside textbuf. A few lines down from the line

    text_len = strlen(recursion_msg);

is the line

    text_len += vscnprintf(text + text_len, ...);

So if printk detects recursion, it sets text_len to 29 and logs an error. Then the message supplied by the caller of printk is stored inside textbuf but offset by 29 bytes. This means that the output of the recursive call to printk will contain 29 bytes of garbage in front of it.

This defect is caused by commit 458df9fd ("printk: remove separate printk_sched buffers and use printk buf instead") which turned

    text_len = vscnprintf(text + text_len, ...);

into

    text_len += vscnprintf(text + text_len, ...);

To fix this, this patch avoids setting text_len when logging the printk recursion error. This patch also performs a couple of local micro-optimizations (use unlikely() and ARRAY_SIZE()).

---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/printk/printk.c +3 -3
index e04c455..c101ec2 100644
@@ -1665,15 +1665,15 @@ asmlinkage int vprintk_emit(int facility, int level,
 	raw_spin_lock(&logbuf_lock);
 	logbuf_cpu = this_cpu;
 
-	if (recursion_bug) {
+	if (unlikely(recursion_bug)) {
 		static const char recursion_msg[] =
 			"BUG: recent printk recursion!";
 
 		recursion_bug = 0;
-		text_len = strlen(recursion_msg);
 		/* emit KERN_CRIT message */
 		printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
-					 NULL, 0, recursion_msg, text_len);
+					 NULL, 0, recursion_msg,
+					 ARRAY_SIZE(recursion_msg) - 1);
 	}
 
 	/*
Signature · 1 line
2.1.0
#1 lore raw
Quoted 21 lines · Patrick Palka

We shouldn't set text_len in the code path that detects printk recursion because text_len corresponds to the length of the string inside textbuf. A few lines down from the line

    text_len = strlen(recursion_msg);

is the line

    text_len += vscnprintf(text + text_len, ...);

So if printk detects recursion, it sets text_len to 29 and logs an error. Then the message supplied by the caller of printk is stored inside textbuf but offset by 29 bytes. This means that the output of the recursive call to printk will contain 29 bytes of garbage in front of it.

This defect is caused by commit 458df9fd ("printk: remove separate printk_sched buffers and use printk buf instead") which turned

    text_len = vscnprintf(text + text_len, ...);
Quoted 7 lines

into

    text_len += vscnprintf(text + text_len, ...);

To fix this, this patch avoids setting text_len when logging the printk recursion error. This patch also performs a couple of local micro-optimizations (use unlikely() and ARRAY_SIZE()).

Thanks for spotting this! Just one nit below:

Quoted 15 lines
---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/printk/printk.c +1 -1
index e04c455..c101ec2 100644
@@ -1665,15 +1665,15 @@ asmlinkage int vprintk_emit(int facility, int level,
 	raw_spin_lock(&logbuf_lock);
 	logbuf_cpu = this_cpu;
 
-	if (recursion_bug) {
+	if (unlikely(recursion_bug)) {

kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1676

Quoted 10 lines
kernel/printk/printk.c +2 -2
 		static const char recursion_msg[] =
 			"BUG: recent printk recursion!";
 
 		recursion_bug = 0;
-		text_len = strlen(recursion_msg);
 		/* emit KERN_CRIT message */
 		printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
-					 NULL, 0, recursion_msg, text_len);
+					 NULL, 0, recursion_msg,
+					 ARRAY_SIZE(recursion_msg) - 1);

I'd prefer to keep using strlen(recursion_msg). It is more obvious what
it does and is more futureproof.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw

PATCH 1/1 Fix faulty logic in the case of recursive printk

Thanks for reviewing! I have put back the call to strlen() and adjusted the commit message accordingly.

Patrick

-- >8 --

We shouldn't set text_len in the code path that detects printk recursion because text_len corresponds to the length of the string inside textbuf. A few lines down from the line

    text_len = strlen(recursion_msg);

is the line

    text_len += vscnprintf(text + text_len, ...);

So if printk detects recursion, it sets text_len to 29 (the length of recursion_msg) and logs an error. Then the message supplied by the caller of printk is stored inside textbuf but offset by 29 bytes. This means that the output of the recursive call to printk will contain 29 bytes of garbage in front of it.

This defect is caused by commit 458df9fd ("printk: remove separate printk_sched buffers and use printk buf instead") which turned the line

    text_len = vscnprintf(text, ...);

into

    text_len += vscnprintf(text + text_len, ...);

To fix this, this patch avoids setting text_len when logging the printk recursion error. This patch also marks unlikely() the branch leading up to this code.

---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/printk/printk.c +3 -3
index e04c455..1ce7706 100644
@@ -1665,15 +1665,15 @@ asmlinkage int vprintk_emit(int facility, int level,
 	raw_spin_lock(&logbuf_lock);
 	logbuf_cpu = this_cpu;
 
-	if (recursion_bug) {
+	if (unlikely(recursion_bug)) {
 		static const char recursion_msg[] =
 			"BUG: recent printk recursion!";
 
 		recursion_bug = 0;
-		text_len = strlen(recursion_msg);
 		/* emit KERN_CRIT message */
 		printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
-					 NULL, 0, recursion_msg, text_len);
+					 NULL, 0, recursion_msg,
+					 strlen(recursion_msg));
 	}
 
 	/*
Signature · 1 line
2.1.0
Reviewed-by on v1 1/1
#3 lore raw
Quoted 30 lines · Patrick Palka

Thanks for reviewing! I have put back the call to strlen() and adjusted the commit message accordingly.

Patrick

-- >8 --

We shouldn't set text_len in the code path that detects printk recursion because text_len corresponds to the length of the string inside textbuf. A few lines down from the line

    text_len = strlen(recursion_msg);

is the line

    text_len += vscnprintf(text + text_len, ...);

So if printk detects recursion, it sets text_len to 29 (the length of recursion_msg) and logs an error. Then the message supplied by the caller of printk is stored inside textbuf but offset by 29 bytes. This means that the output of the recursive call to printk will contain 29 bytes of garbage in front of it.

This defect is caused by commit 458df9fd ("printk: remove separate printk_sched buffers and use printk buf instead") which turned the line

    text_len = vscnprintf(text, ...);

into

Quoted 7 lines
    text_len += vscnprintf(text + text_len, ...);

To fix this, this patch avoids setting text_len when logging the printk recursion error. This patch also marks unlikely() the branch leading up to this code.

The patch looks good. You can add:

Plus you could also add tags:

Fixes 458df9fd4815b47809875d57f42e16401674b621

Honza

Quoted 30 lines
---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/printk/printk.c +3 -3
index e04c455..1ce7706 100644
@@ -1665,15 +1665,15 @@ asmlinkage int vprintk_emit(int facility, int level,
 	raw_spin_lock(&logbuf_lock);
 	logbuf_cpu = this_cpu;
 
-	if (recursion_bug) {
+	if (unlikely(recursion_bug)) {
 		static const char recursion_msg[] =
 			"BUG: recent printk recursion!";
 
 		recursion_bug = 0;
-		text_len = strlen(recursion_msg);
 		/* emit KERN_CRIT message */
 		printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
-					 NULL, 0, recursion_msg, text_len);
+					 NULL, 0, recursion_msg,
+					 strlen(recursion_msg));
 	}
 
 	/*
Signature · 1 line
2.1.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Reviewed-by on v1 1/1
#4 lore raw
In reply to Patrick Palka
Quoted 30 lines · Patrick Palka

Thanks for reviewing! I have put back the call to strlen() and adjusted the commit message accordingly.

Patrick

-- >8 --

We shouldn't set text_len in the code path that detects printk recursion because text_len corresponds to the length of the string inside textbuf. A few lines down from the line

    text_len = strlen(recursion_msg);

is the line

    text_len += vscnprintf(text + text_len, ...);

So if printk detects recursion, it sets text_len to 29 (the length of recursion_msg) and logs an error. Then the message supplied by the caller of printk is stored inside textbuf but offset by 29 bytes. This means that the output of the recursive call to printk will contain 29 bytes of garbage in front of it.

This defect is caused by commit 458df9fd ("printk: remove separate printk_sched buffers and use printk buf instead") which turned the line

    text_len = vscnprintf(text, ...);

into

Quoted 7 lines
    text_len += vscnprintf(text + text_len, ...);

To fix this, this patch avoids setting text_len when logging the printk recursion error. This patch also marks unlikely() the branch leading up to this code.

Looks fine to me.

Quoted 30 lines
---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/printk/printk.c +3 -3
index e04c455..1ce7706 100644
@@ -1665,15 +1665,15 @@ asmlinkage int vprintk_emit(int facility, int level,
 	raw_spin_lock(&logbuf_lock);
 	logbuf_cpu = this_cpu;
 
-	if (recursion_bug) {
+	if (unlikely(recursion_bug)) {
 		static const char recursion_msg[] =
 			"BUG: recent printk recursion!";
 
 		recursion_bug = 0;
-		text_len = strlen(recursion_msg);
 		/* emit KERN_CRIT message */
 		printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
-					 NULL, 0, recursion_msg, text_len);
+					 NULL, 0, recursion_msg,
+					 strlen(recursion_msg));
 	}
 
 	/*
Signature · 1 line
2.1.0
#5 lore raw
In reply to Patrick Palka
Quoted 21 lines · Patrick Palka

We shouldn't set text_len in the code path that detects printk recursion because text_len corresponds to the length of the string inside textbuf. A few lines down from the line

    text_len = strlen(recursion_msg);

is the line

    text_len += vscnprintf(text + text_len, ...);

So if printk detects recursion, it sets text_len to 29 and logs an error. Then the message supplied by the caller of printk is stored inside textbuf but offset by 29 bytes. This means that the output of the recursive call to printk will contain 29 bytes of garbage in front of it.

This defect is caused by commit 458df9fd ("printk: remove separate printk_sched buffers and use printk buf instead") which turned

    text_len = vscnprintf(text + text_len, ...);
Quoted 7 lines

into

    text_len += vscnprintf(text + text_len, ...);

To fix this, this patch avoids setting text_len when logging the printk recursion error. This patch also performs a couple of local micro-optimizations (use unlikely() and ARRAY_SIZE()).

Great catch! And I agree with Jack about that strlen.

Quoted 31 lines
---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/printk/printk.c +3 -3
index e04c455..c101ec2 100644
@@ -1665,15 +1665,15 @@ asmlinkage int vprintk_emit(int facility, int level,
 	raw_spin_lock(&logbuf_lock);
 	logbuf_cpu = this_cpu;
 
-	if (recursion_bug) {
+	if (unlikely(recursion_bug)) {
 		static const char recursion_msg[] =
 			"BUG: recent printk recursion!";
 
 		recursion_bug = 0;
-		text_len = strlen(recursion_msg);
 		/* emit KERN_CRIT message */
 		printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
-					 NULL, 0, recursion_msg, text_len);
+					 NULL, 0, recursion_msg,
+					 ARRAY_SIZE(recursion_msg) - 1);
 	}
 
 	/*
Signature · 1 line
2.1.0
reviewer Acked-by
#6 lore raw
Quoted 24 lines · On Tue, 26 Aug 2014 11:19:41 +0200 Petr Mládek <[email protected]> wrote:
Quoted 23 lines · Patrick Palka

We shouldn't set text_len in the code path that detects printk recursion because text_len corresponds to the length of the string inside textbuf. A few lines down from the line

    text_len = strlen(recursion_msg);

is the line

    text_len += vscnprintf(text + text_len, ...);

So if printk detects recursion, it sets text_len to 29 and logs an error. Then the message supplied by the caller of printk is stored inside textbuf but offset by 29 bytes. This means that the output of the recursive call to printk will contain 29 bytes of garbage in front of it.

This defect is caused by commit 458df9fd ("printk: remove separate printk_sched buffers and use printk buf instead") which turned

    text_len = vscnprintf(text + text_len, ...);

into

Quoted 7 lines
Quoted 5 lines
    text_len += vscnprintf(text + text_len, ...);

To fix this, this patch avoids setting text_len when logging the printk recursion error. This patch also performs a couple of local micro-optimizations (use unlikely() and ARRAY_SIZE()).

Great catch! And I agree with Jack about that strlen.

Although I will say that ARRAY_SIZE() is used elsewhere in the kernel like this.

But as this isn't a critical section, either is fine.

-- Steve

Quoted 32 lines
Quoted 32 lines
---
 kernel/printk/printk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/printk/printk.c +3 -3
index e04c455..c101ec2 100644
@@ -1665,15 +1665,15 @@ asmlinkage int vprintk_emit(int facility, int level,
 	raw_spin_lock(&logbuf_lock);
 	logbuf_cpu = this_cpu;
 
-	if (recursion_bug) {
+	if (unlikely(recursion_bug)) {
 		static const char recursion_msg[] =
 			"BUG: recent printk recursion!";
 
 		recursion_bug = 0;
-		text_len = strlen(recursion_msg);
 		/* emit KERN_CRIT message */
 		printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
-					 NULL, 0, recursion_msg, text_len);
+					 NULL, 0, recursion_msg,
+					 ARRAY_SIZE(recursion_msg) - 1);
 	}
 
 	/*
Signature · 1 line
2.1.0