Fix faulty logic in the case of recursive printk
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
@@ -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
Quoted 21 lines · Patrick Palka
We shouldn't set
text_lenin the code path that detects printk recursion becausetext_lencorresponds to the length of the string inside textbuf. A few lines down from the linetext_len = strlen(recursion_msg);is the line
text_len += vscnprintf(text + text_len, ...);So if printk detects recursion, it sets
text_lento 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 separateprintk_schedbuffers and use printk buf instead") which turnedtext_len = vscnprintf(text + text_len, ...);
Quoted 7 lines
into
text_len += vscnprintf(text + text_len, ...);To fix this, this patch avoids setting
text_lenwhen logging the printk recursion error. This patch also performs a couple of local micro-optimizations (useunlikely()andARRAY_SIZE()).
Thanks for spotting this! Just one nit below:
Quoted 15 lines
Signed-off-by Patrick Palka <[email protected]>--- kernel/printk/printk.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)kernel/printk/printk.c +1
-1@@ -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
-2static 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
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
@@ -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
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_lenin the code path that detects printk recursion becausetext_lencorresponds to the length of the string inside textbuf. A few lines down from the linetext_len = strlen(recursion_msg);is the line
text_len += vscnprintf(text + text_len, ...);So if printk detects recursion, it sets
text_lento 29 (the length ofrecursion_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 separateprintk_schedbuffers and use printk buf instead") which turned the linetext_len = vscnprintf(text, ...);into
Quoted 7 lines
text_len += vscnprintf(text + text_len, ...);To fix this, this patch avoids setting
text_lenwhen logging the printk recursion error. This patch also marksunlikely()the branch leading up to this code.Signed-off-by Patrick Palka <[email protected]>
The patch looks good. You can add:
Plus you could also add tags:
Honza
Quoted 30 lines
--- kernel/printk/printk.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)kernel/printk/printk.c +3
-3@@ -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
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_lenin the code path that detects printk recursion becausetext_lencorresponds to the length of the string inside textbuf. A few lines down from the linetext_len = strlen(recursion_msg);is the line
text_len += vscnprintf(text + text_len, ...);So if printk detects recursion, it sets
text_lento 29 (the length ofrecursion_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 separateprintk_schedbuffers and use printk buf instead") which turned the linetext_len = vscnprintf(text, ...);into
Quoted 7 lines
text_len += vscnprintf(text + text_len, ...);To fix this, this patch avoids setting
text_lenwhen logging the printk recursion error. This patch also marksunlikely()the branch leading up to this code.Signed-off-by Patrick Palka <[email protected]>
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@@ -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
Quoted 21 lines · Patrick Palka
We shouldn't set
text_lenin the code path that detects printk recursion becausetext_lencorresponds to the length of the string inside textbuf. A few lines down from the linetext_len = strlen(recursion_msg);is the line
text_len += vscnprintf(text + text_len, ...);So if printk detects recursion, it sets
text_lento 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 separateprintk_schedbuffers and use printk buf instead") which turnedtext_len = vscnprintf(text + text_len, ...);
Quoted 7 lines
into
text_len += vscnprintf(text + text_len, ...);To fix this, this patch avoids setting
text_lenwhen logging the printk recursion error. This patch also performs a couple of local micro-optimizations (useunlikely()andARRAY_SIZE()).
Great catch! And I agree with Jack about that strlen.
Quoted 31 lines
Signed-off-by Patrick Palka <[email protected]>--- kernel/printk/printk.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)kernel/printk/printk.c +3
-3@@ -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
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_lenin the code path that detects printk recursion becausetext_lencorresponds to the length of the string inside textbuf. A few lines down from the linetext_len = strlen(recursion_msg);is the line
text_len += vscnprintf(text + text_len, ...);So if printk detects recursion, it sets
text_lento 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 separateprintk_schedbuffers and use printk buf instead") which turnedtext_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_lenwhen logging the printk recursion error. This patch also performs a couple of local micro-optimizations (useunlikely()andARRAY_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
Signed-off-by Patrick Palka <[email protected]>--- kernel/printk/printk.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)kernel/printk/printk.c +3
-3@@ -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