All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Shen Kai <shenkai8@huawei.com>
Cc: jslaby@suse.com, linux-kernel@vger.kernel.org,
	hushiyuan@huawei.com, linfeilong@huawei.com
Subject: Re: [PATCH] add lock proctect to __handle_sysrq in write_sysrq_trigger
Date: Fri, 7 Feb 2020 09:10:06 +0100	[thread overview]
Message-ID: <20200207081006.GB309560@kroah.com> (raw)
In-Reply-To: <1581062166-27284-1-git-send-email-shenkai8@huawei.com>

On Fri, Feb 07, 2020 at 07:56:06AM +0000, Shen Kai wrote:
> From: Feilong Lin <linfeilong@huawei.com>
> 
> 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
> 
> Signed-off-by: Feilong Lin <linfeilong@huawei.com>
> Reported-by: Kai Shen <shenkai8@huawei.com>
> ---
>  drivers/tty/sysrq.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
> index f724962..cbb48a9 100644
> --- a/drivers/tty/sysrq.c
> +++ b/drivers/tty/sysrq.c
> @@ -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);

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

  reply	other threads:[~2020-02-07  8:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-07  7:56 [PATCH] add lock proctect to __handle_sysrq in write_sysrq_trigger Shen Kai
2020-02-07  8:10 ` Greg KH [this message]
2020-02-07  9:13   ` shenkai
2020-02-07  9:53     ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200207081006.GB309560@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=hushiyuan@huawei.com \
    --cc=jslaby@suse.com \
    --cc=linfeilong@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shenkai8@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.