aboutsummaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorYuanhan Liu <yuanhan.liu@linux.intel.com>2012-06-16 09:21:51 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-06-16 11:36:02 -0400
commit4a77a5a06ec66ed05199b301e7c25f42f979afdc (patch)
tree4b1897ff18501dae3a62c3f1618a761829eb64d7 /kernel
parente2ae715d66bf4becfb85eb84b7150e23cf27df30 (diff)
printk: use mutex lock to stop syslog_seq from going wild
Although syslog_seq and log_next_seq stuff are protected by logbuf_lock spin log, it's not enough. Say we have two processes A and B, and let syslog_seq = N, while log_next_seq = N + 1, and the two processes both come to syslog_print at almost the same time. And No matter which process get the spin lock first, it will increase syslog_seq by one, then release spin lock; thus later, another process increase syslog_seq by one again. In this case, syslog_seq is bigger than syslog_next_seq. And latter, it would make: wait_event_interruptiable(log_wait, syslog != log_next_seq) don't wait any more even there is no new write comes. Thus it introduce a infinite loop reading. I can easily see this kind of issue by the following steps: # cat /proc/kmsg # at meantime, I don't kill rsyslog # So they are the two processes. # xinit # I added drm.debug=6 in the kernel parameter line, # so that it will produce lots of message and let that # issue happen It's 100% reproducable on my side. And my disk will be filled up by /var/log/messages in a quite short time. So, introduce a mutex_lock to stop syslog_seq from going wild just like what devkmsg_read() does. It does fix this issue as expected. v2: use mutex_lock_interruptiable() instead (comments from Kay) Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com> Reviewed-by: Fengguang Wu <fengguang.wu@intel.com> Acked-By: Kay Sievers <kay@vrfy.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/printk.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/kernel/printk.c b/kernel/printk.c
index ceb4a2f775a1..572730bd8a5c 100644
--- a/kernel/printk.c
+++ b/kernel/printk.c
@@ -414,7 +414,9 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
414 if (!user) 414 if (!user)
415 return -EBADF; 415 return -EBADF;
416 416
417 mutex_lock(&user->lock); 417 ret = mutex_lock_interruptible(&user->lock);
418 if (ret)
419 return ret;
418 raw_spin_lock(&logbuf_lock); 420 raw_spin_lock(&logbuf_lock);
419 while (user->seq == log_next_seq) { 421 while (user->seq == log_next_seq) {
420 if (file->f_flags & O_NONBLOCK) { 422 if (file->f_flags & O_NONBLOCK) {
@@ -976,6 +978,7 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
976{ 978{
977 bool clear = false; 979 bool clear = false;
978 static int saved_console_loglevel = -1; 980 static int saved_console_loglevel = -1;
981 static DEFINE_MUTEX(syslog_mutex);
979 int error; 982 int error;
980 983
981 error = check_syslog_permissions(type, from_file); 984 error = check_syslog_permissions(type, from_file);
@@ -1002,11 +1005,17 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
1002 error = -EFAULT; 1005 error = -EFAULT;
1003 goto out; 1006 goto out;
1004 } 1007 }
1008 error = mutex_lock_interruptible(&syslog_mutex);
1009 if (error)
1010 goto out;
1005 error = wait_event_interruptible(log_wait, 1011 error = wait_event_interruptible(log_wait,
1006 syslog_seq != log_next_seq); 1012 syslog_seq != log_next_seq);
1007 if (error) 1013 if (error) {
1014 mutex_unlock(&syslog_mutex);
1008 goto out; 1015 goto out;
1016 }
1009 error = syslog_print(buf, len); 1017 error = syslog_print(buf, len);
1018 mutex_unlock(&syslog_mutex);
1010 break; 1019 break;
1011 /* Read/clear last kernel messages */ 1020 /* Read/clear last kernel messages */
1012 case SYSLOG_ACTION_READ_CLEAR: 1021 case SYSLOG_ACTION_READ_CLEAR: