f846a2a
From ce10d1b72b4da3c98bbbcb1b945687d964c31923 Mon Sep 17 00:00:00 2001
c040d8c
From: Josh Boyer <jwboyer@redhat.com>
f846a2a
Date: Tue, 9 Apr 2013 11:08:13 -0400
c040d8c
Subject: [PATCH] kmsg: Honor dmesg_restrict sysctl on /dev/kmsg
c040d8c
f846a2a
The dmesg_restrict sysctl currently covers the syslog method for access
f846a2a
dmesg, however /dev/kmsg isn't covered by the same protections.  Most
f846a2a
people haven't noticed because util-linux dmesg(1) defaults to using the
f846a2a
syslog method for access in older versions.  With util-linux dmesg(1)
f846a2a
defaults to reading directly from /dev/kmsg.
c040d8c
f846a2a
Fix this by reworking all of the access methods to use the
f846a2a
check_syslog_permissions function and adding checks to devkmsg_open and
f846a2a
devkmsg_read.
c040d8c
c040d8c
This fixes https://bugzilla.redhat.com/show_bug.cgi?id=903192
c040d8c
c040d8c
Reported-by: Christian Kujau <lists@nerdbynature.de>
c040d8c
CC: stable@vger.kernel.org
f846a2a
Signed-off-by: Eric Paris <eparis@redhat.com>
c040d8c
Signed-off-by: Josh Boyer <jwboyer@redhat.com>
c040d8c
---
f846a2a
 kernel/printk.c | 91 +++++++++++++++++++++++++++++----------------------------
f846a2a
 1 file changed, 47 insertions(+), 44 deletions(-)
c040d8c
c040d8c
diff --git a/kernel/printk.c b/kernel/printk.c
f846a2a
index abbdd9e..5541095 100644
c040d8c
--- a/kernel/printk.c
c040d8c
+++ b/kernel/printk.c
f846a2a
@@ -368,6 +368,46 @@ static void log_store(int facility, int level,
f846a2a
 	log_next_seq++;
f846a2a
 }
c040d8c
 
f846a2a
+#ifdef CONFIG_SECURITY_DMESG_RESTRICT
f846a2a
+int dmesg_restrict = 1;
f846a2a
+#else
f846a2a
+int dmesg_restrict;
f846a2a
+#endif
f846a2a
+
f846a2a
+static int syslog_action_restricted(int type)
f846a2a
+{
f846a2a
+	if (dmesg_restrict)
f846a2a
+		return 1;
f846a2a
+	/* Unless restricted, we allow "read all" and "get buffer size" for everybody */
f846a2a
+	return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
f846a2a
+}
f846a2a
+
f846a2a
+static int check_syslog_permissions(int type, bool from_file)
f846a2a
+{
f846a2a
+	/*
f846a2a
+	 * If this is from /proc/kmsg and we've already opened it, then we've
f846a2a
+	 * already done the capabilities checks at open time.
f846a2a
+	 */
f846a2a
+	if (from_file && type != SYSLOG_ACTION_OPEN)
f846a2a
+		goto ok;
c040d8c
+
f846a2a
+	if (syslog_action_restricted(type)) {
f846a2a
+		if (capable(CAP_SYSLOG))
f846a2a
+			goto ok;
f846a2a
+		/* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
f846a2a
+		if (capable(CAP_SYS_ADMIN)) {
f846a2a
+			printk_once(KERN_WARNING "%s (%d): "
f846a2a
+				 "Attempt to access syslog with CAP_SYS_ADMIN "
f846a2a
+				 "but no CAP_SYSLOG (deprecated).\n",
f846a2a
+				 current->comm, task_pid_nr(current));
f846a2a
+			goto ok;
f846a2a
+		}
f846a2a
+		return -EPERM;
f846a2a
+	}
f846a2a
+ok:
f846a2a
+	return security_syslog(type);
f846a2a
+}
f846a2a
+
f846a2a
 /* /dev/kmsg - userspace message inject/listen interface */
f846a2a
 struct devkmsg_user {
f846a2a
 	u64 seq;
f846a2a
@@ -443,10 +483,16 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
f846a2a
 	char cont = '-';
f846a2a
 	size_t len;
f846a2a
 	ssize_t ret;
f846a2a
+	int err;
f846a2a
 
f846a2a
 	if (!user)
f846a2a
 		return -EBADF;
f846a2a
 
f846a2a
+	err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
f846a2a
+		SYSLOG_FROM_FILE);
f846a2a
+	if (err)
f846a2a
+		return err;
f846a2a
+
f846a2a
 	ret = mutex_lock_interruptible(&user->lock);
f846a2a
 	if (ret)
f846a2a
 		return ret;
f846a2a
@@ -624,7 +670,7 @@ static int devkmsg_open(struct inode *inode, struct file *file)
c040d8c
 	if ((file->f_flags & O_ACCMODE) == O_WRONLY)
c040d8c
 		return 0;
f846a2a
 
f846a2a
-	err = security_syslog(SYSLOG_ACTION_READ_ALL);
f846a2a
+	err = check_syslog_permissions(SYSLOG_ACTION_OPEN, SYSLOG_FROM_FILE);
f846a2a
 	if (err)
f846a2a
 		return err;
f846a2a
 
f846a2a
@@ -817,45 +863,6 @@ static inline void boot_delay_msec(int level)
f846a2a
 }
f846a2a
 #endif
f846a2a
 
f846a2a
-#ifdef CONFIG_SECURITY_DMESG_RESTRICT
f846a2a
-int dmesg_restrict = 1;
f846a2a
-#else
f846a2a
-int dmesg_restrict;
f846a2a
-#endif
f846a2a
-
f846a2a
-static int syslog_action_restricted(int type)
f846a2a
-{
f846a2a
-	if (dmesg_restrict)
f846a2a
-		return 1;
f846a2a
-	/* Unless restricted, we allow "read all" and "get buffer size" for everybody */
f846a2a
-	return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
f846a2a
-}
f846a2a
-
f846a2a
-static int check_syslog_permissions(int type, bool from_file)
f846a2a
-{
f846a2a
-	/*
f846a2a
-	 * If this is from /proc/kmsg and we've already opened it, then we've
f846a2a
-	 * already done the capabilities checks at open time.
f846a2a
-	 */
f846a2a
-	if (from_file && type != SYSLOG_ACTION_OPEN)
f846a2a
-		return 0;
f846a2a
-
f846a2a
-	if (syslog_action_restricted(type)) {
f846a2a
-		if (capable(CAP_SYSLOG))
f846a2a
-			return 0;
f846a2a
-		/* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
f846a2a
-		if (capable(CAP_SYS_ADMIN)) {
f846a2a
-			printk_once(KERN_WARNING "%s (%d): "
f846a2a
-				 "Attempt to access syslog with CAP_SYS_ADMIN "
f846a2a
-				 "but no CAP_SYSLOG (deprecated).\n",
f846a2a
-				 current->comm, task_pid_nr(current));
f846a2a
-			return 0;
f846a2a
-		}
f846a2a
-		return -EPERM;
f846a2a
-	}
f846a2a
-	return 0;
f846a2a
-}
f846a2a
-
f846a2a
 #if defined(CONFIG_PRINTK_TIME)
f846a2a
 static bool printk_time = 1;
f846a2a
 #else
f846a2a
@@ -1131,10 +1138,6 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
f846a2a
 	if (error)
f846a2a
 		goto out;
f846a2a
 
f846a2a
-	error = security_syslog(type);
f846a2a
-	if (error)
f846a2a
-		return error;
f846a2a
-
f846a2a
 	switch (type) {
f846a2a
 	case SYSLOG_ACTION_CLOSE:	/* Close log */
f846a2a
 		break;
c040d8c
-- 
f846a2a
1.8.1.4
c040d8c