Commit cb424ffe authored by Kay Sievers's avatar Kay Sievers Committed by Greg Kroah-Hartman

kmsg: properly handle concurrent non-blocking read() from /proc/kmsg

The /proc/kmsg read() interface is internally simply wired up to a sequence
of syslog() syscalls, which might are racy between their checks and actions,
regarding concurrency.

In the (very uncommon) case of concurrent readers of /dev/kmsg, relying on
usual O_NONBLOCK behavior, the recently introduced mutex might block an
O_NONBLOCK reader in read(), when poll() returns for it, but another process
has already read the data in the meantime. We've seen that while running
artificial test setups and tools that "fight" about /proc/kmsg data.

This restores the original /proc/kmsg behavior, where in case of concurrent
read()s, poll() might wake up but the read() syscall will just return 0 to
the caller, while another process has "stolen" the data.

This is in the general case not the expected behavior, but it is the exact
same one, that can easily be triggered with a 3.4 kernel, and some tools
might just rely on it.

The mutex is not needed, the original integrity issue which introduced it,
is in the meantime covered by:
  "fill buffer with more than a single message for SYSLOG_ACTION_READ"
  116e90b2

Cc: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Acked-by: default avatarJan Beulich <jbeulich@suse.com>
Signed-off-by: default avatarKay Sievers <kay@vrfy.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 43a73a50
...@@ -1021,7 +1021,6 @@ int do_syslog(int type, char __user *buf, int len, bool from_file) ...@@ -1021,7 +1021,6 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
{ {
bool clear = false; bool clear = false;
static int saved_console_loglevel = -1; static int saved_console_loglevel = -1;
static DEFINE_MUTEX(syslog_mutex);
int error; int error;
error = check_syslog_permissions(type, from_file); error = check_syslog_permissions(type, from_file);
...@@ -1048,17 +1047,11 @@ int do_syslog(int type, char __user *buf, int len, bool from_file) ...@@ -1048,17 +1047,11 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
error = -EFAULT; error = -EFAULT;
goto out; goto out;
} }
error = mutex_lock_interruptible(&syslog_mutex);
if (error)
goto out;
error = wait_event_interruptible(log_wait, error = wait_event_interruptible(log_wait,
syslog_seq != log_next_seq); syslog_seq != log_next_seq);
if (error) { if (error)
mutex_unlock(&syslog_mutex);
goto out; goto out;
}
error = syslog_print(buf, len); error = syslog_print(buf, len);
mutex_unlock(&syslog_mutex);
break; break;
/* Read/clear last kernel messages */ /* Read/clear last kernel messages */
case SYSLOG_ACTION_READ_CLEAR: case SYSLOG_ACTION_READ_CLEAR:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment