add lock proctect to __handle_sysrq in write_sysrq_trigger
PATCH 1/1 add lock proctect to __handle_sysrq in write_sysrq_trigger
From: Feilong Lin <[email protected]>
Add lock protect to __handle_sysrq to avoid race condition. __handle_sysrq will change console_loglevel without lock protect which can lead to console_loglevel to be set as an unexpected value.
Problem may occur when "echo t > /proc/sysrq-trigger" is called on multiple cpus concurrently.
In this case in __handle_sysrq, console_loglevel is set to 7 to print some head info to the console then restore it. But without lock protect in parallel execution situation, restoring may go wrong. The new loglevel may be taken as the previous loglevel incorrectly. Console_loglevel can be 7 at last, which causes the terminal to output info in most log levels.
This bug was found on linux 4.19
--- drivers/tty/sysrq.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/tty/sysrq.c +4 -0
@@ -1087,6 +1087,8 @@ EXPORT_SYMBOL(unregister_sysrq_key);/* * writing 'C' to /proc/sysrq-trigger is like sysrq-C */ +static DEFINE_MUTEX (sysrq_mutex); +static ssize_t write_sysrq_trigger (struct file *file,const char __user *buf,size_t count,loff_t *ppos) {
@@ -1095,7 +1097,9 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,if (get_user (c, buf))return -EFAULT ; +mutex_lock (&sysrq_mutex);__handle_sysrq (c, false); +mutex_unlock (&sysrq_mutex); }return count;
Signature · 1 line
2.6.4.windows.1
Greg KH
maintainerQuoted 38 lines · Shen Kai
From: Feilong Lin <[email protected]>
Add lock protect to
__handle_sysrqto avoid race condition.__handle_sysrqwill changeconsole_loglevelwithout lock protect which can lead toconsole_loglevelto be set as an unexpected value.Problem may occur when "echo t >
/proc/sysrq-trigger" is called on multiple cpus concurrently.In this case in
__handle_sysrq,console_loglevelis set to 7 to print some head info to the console then restore it. But without lock protect in parallel execution situation, restoring may go wrong. The new loglevel may be taken as the previous loglevel incorrectly.Console_loglevelcan be 7 at last, which causes the terminal to output info in most log levels.This bug was found on linux 4.19
Signed-off-by Feilong Lin <[email protected]>Reported-by Kai Shen <[email protected]>--- drivers/tty/sysrq.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/tty/sysrq.c +2
-0@@ -1087,6 +1087,8 @@ EXPORT_SYMBOL(unregister_sysrq_key);/* * writing 'C' to /proc/sysrq-trigger is like sysrq-C */ +static DEFINE_MUTEX (sysrq_mutex); +static ssize_t write_sysrq_trigger (struct file *file,const char __user *buf,size_t count,loff_t *ppos) {
drivers/tty/sysrq.c static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, · line 1102
Quoted 6 lines
drivers/tty/sysrq.c +2
-0@@ -1095,7 +1097,9 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,if (get_user (c, buf))return -EFAULT ; +mutex_lock (&sysrq_mutex);__handle_sysrq (c, false); +mutex_unlock (&sysrq_mutex);
What exactly are you protecting here? What other task is doing this at the same exact time?
You mention different tasks hitting this sysrq-trigger at the same time, but really, "just do not do that" should be the real answer, as even with this lock, you don't know what the end result will be as the "last" one in will have the last word, right?
thanks,
greg k-h
Quoted 54 lines · Greg KH
Quoted 38 lines · Shen Kai
From: Feilong Lin <[email protected]>
Add lock protect to
__handle_sysrqto avoid race condition.__handle_sysrqwill changeconsole_loglevelwithout lock protect which can lead toconsole_loglevelto be set as an unexpected value.Problem may occur when "echo t >
/proc/sysrq-trigger" is called on multiple cpus concurrently.In this case in
__handle_sysrq,console_loglevelis set to 7 to print some head info to the console then restore it. But without lock protect in parallel execution situation, restoring may go wrong. The new loglevel may be taken as the previous loglevel incorrectly.Console_loglevelcan be 7 at last, which causes the terminal to output info in most log levels.This bug was found on linux 4.19
Signed-off-by Feilong Lin <[email protected]>Reported-by Kai Shen <[email protected]>--- drivers/tty/sysrq.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/tty/sysrq.c +2
-0@@ -1087,6 +1087,8 @@ EXPORT_SYMBOL(unregister_sysrq_key);/* * writing 'C' to /proc/sysrq-trigger is like sysrq-C */ +static DEFINE_MUTEX (sysrq_mutex); +static ssize_t write_sysrq_trigger (struct file *file,const char __user *buf,size_t count,loff_t *ppos) {drivers/tty/sysrq.c static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, · line 1102
Quoted 6 lines
drivers/tty/sysrq.c +2
-0@@ -1095,7 +1097,9 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,if (get_user (c, buf))return -EFAULT ; +mutex_lock (&sysrq_mutex);__handle_sysrq (c, false); +mutex_unlock (&sysrq_mutex);What exactly are you protecting here? What other task is doing this at the same exact time?
You mention different tasks hitting this sysrq-trigger at the same time, but really, "just do not do that" should be the real answer, as even with this lock, you don't know what the end result will be as the "last" one in will have the last word, right?
Quoted 6 lines
thanks,
greg k-h
.
Here we want to protect the global variable console_loglevel (console_printk[0]).
Problem may occur when run shell programs like:
echo t > /proc/sysrq-trigger &
echo t > /proc/sysrq-trigger &
echo t > /proc/sysrq-trigger &
..
After above operations are done, console_loglevel may be 7 instead of the original log level. I doubt this is what we expect though those operations may not be meaningful.
In this case, much info may be output to the terminal for stack info of all threads is a lot to print which may cause soft lockup on a non-preempt kernel.
Best regards
Kai
Greg KH
maintainerQuoted 57 lines · shenkai
Quoted 55 lines · Greg KH
Quoted 38 lines · Shen Kai
From: Feilong Lin <[email protected]>
Add lock protect to
__handle_sysrqto avoid race condition.__handle_sysrqwill changeconsole_loglevelwithout lock protect which can lead toconsole_loglevelto be set as an unexpected value.Problem may occur when "echo t >
/proc/sysrq-trigger" is called on multiple cpus concurrently.In this case in
__handle_sysrq,console_loglevelis set to 7 to print some head info to the console then restore it. But without lock protect in parallel execution situation, restoring may go wrong. The new loglevel may be taken as the previous loglevel incorrectly.Console_loglevelcan be 7 at last, which causes the terminal to output info in most log levels.This bug was found on linux 4.19
Signed-off-by Feilong Lin <[email protected]>Reported-by Kai Shen <[email protected]>--- drivers/tty/sysrq.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/tty/sysrq.c +2
-0@@ -1087,6 +1087,8 @@ EXPORT_SYMBOL(unregister_sysrq_key);/* * writing 'C' to /proc/sysrq-trigger is like sysrq-C */ +static DEFINE_MUTEX (sysrq_mutex); +static ssize_t write_sysrq_trigger (struct file *file,const char __user *buf,size_t count,loff_t *ppos) {drivers/tty/sysrq.c static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, · line 1101
Quoted 5 lines
drivers/tty/sysrq.c +2
-0@@ -1095,7 +1097,9 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,if (get_user (c, buf))return -EFAULT ; +mutex_lock (&sysrq_mutex);__handle_sysrq (c, false); +mutex_unlock (&sysrq_mutex);What exactly are you protecting here? What other task is doing this at the same exact time?
You mention different tasks hitting this sysrq-trigger at the same time, but really, "just do not do that" should be the real answer, as even with this lock, you don't know what the end result will be as the "last" one in will have the last word, right?
thanks,
Quoted 7 lines
Quoted 4 lines
greg k-h
.
Here we want to protect the global variable
console_loglevel(console_printk[0]).
But how is this single lock protecting it?
Quoted 6 lines
Problem may occur when run shell programs like:
echo t >
/proc/sysrq-trigger&
echo t >/proc/sysrq-trigger&
echo t >/proc/sysrq-trigger&
..
Don't do that :)
Quoted 7 lines
After above operations are done,
console_loglevelmay be 7 instead of the original log level. I doubt this is what we expect though those operations may not be meaningful.In this case, much info may be output to the terminal for stack info of all threads is a lot to print which may cause soft lockup on a non-preempt kernel.
Dumping loads of stuff to the console is what you asked the above things to do. And why would you run non-preempt?
Anyway, this feels like you are not addressing the real issue and instead papering over it by just trying to serialize the sysrq trigger, which is not something that we always need to do.
thanks,
greg k-h