lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <299a3c124f4e0ece36f2a04db2d4d2143e66ebca.1764272407.git.chris@chrisdown.name>
Date: Fri, 28 Nov 2025 03:44:01 +0800
From: Chris Down <chris@...isdown.name>
To: Petr Mladek <pmladek@...e.com>
Cc: linux-kernel@...r.kernel.org,
	Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
	Sergey Senozhatsky <senozhatsky@...omium.org>,
	Steven Rostedt <rostedt@...dmis.org>,
	John Ogness <john.ogness@...utronix.de>,
	Geert Uytterhoeven <geert@...ux-m68k.org>,
	Tony Lindgren <tony.lindgren@...ux.intel.com>, kernel-team@...com
Subject: [PATCH v8 13/21] printk: Toggle ignore_per_console_loglevel via
 syslog

The syslog() system call provides SYSLOG_ACTION_CONSOLE_OFF and
SYSLOG_ACTION_CONSOLE_ON to disable and re-enable console logging.
These actions save and restore the global console_loglevel.

With per-console loglevels, simply setting console_loglevel to
minimum_console_loglevel during CONSOLE_OFF is insufficient - consoles
with explicit per-console loglevels would continue printing based on
their local settings.

Augment these actions to also save and restore the state of
ignore_per_console_loglevel. When console logging is disabled via
syslog(), ignore_per_console_loglevel is set to true so that the
minimum_console_loglevel applies to all consoles. When logging is
re-enabled, the previous ignore_per_console_loglevel state is restored.

A forced flag tracks whether this code path actually set
ignore_per_console_loglevel. This prevents blindly restoring stale
state if other contexts (sysrq, module parameter writes) legitimately
modified the flag in parallel.

SYSLOG_ACTION_CONSOLE_LEVEL now emits a one-time warning when
per-console loglevels are active, informing administrators that their
level change only affects consoles using the global loglevel. The
level change also properly restores ignore_per_console_loglevel state
if it was forced by a previous CONSOLE_OFF.

These changes allow userspace tools like dmesg to properly control
console output even in the presence of per-console loglevels.

Reviewed-by: Petr Mladek <pmladek@...e.com>
Signed-off-by: Chris Down <chris@...isdown.name>
---
 kernel/printk/printk.c | 53 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 8 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index e5b6a926135e..1d28887e7218 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1869,6 +1869,8 @@ int do_syslog(int type, char __user *buf, int len, int source)
 	struct printk_info info;
 	bool clear = false;
 	static int saved_console_loglevel = LOGLEVEL_DEFAULT;
+	static bool saved_ignore_per_console_loglevel;
+	static bool saved_ignore_per_console_loglevel_forced;
 	int error;
 
 	error = check_syslog_permissions(type, source);
@@ -1910,9 +1912,23 @@ int do_syslog(int type, char __user *buf, int len, int source)
 	/* Disable logging to console */
 	case SYSLOG_ACTION_CONSOLE_OFF:
 		mutex_lock(&syslog_lock);
-		if (saved_console_loglevel == LOGLEVEL_DEFAULT)
+		if (saved_console_loglevel == LOGLEVEL_DEFAULT) {
 			saved_console_loglevel = console_loglevel;
+			saved_ignore_per_console_loglevel = ignore_per_console_loglevel;
+			saved_ignore_per_console_loglevel_forced = false;
+		}
 		console_loglevel = minimum_console_loglevel;
+		if (!ignore_per_console_loglevel) {
+			/*
+			 * Only remember the saved value if this path actually
+			 * forced the override. Other contexts (sysrq, module
+			 * parameter writes, etc.) can legitimately toggle the
+			 * flag in parallel, so blindly restoring it later would
+			 * resurrect stale state.
+			 */
+			ignore_per_console_loglevel = true;
+			saved_ignore_per_console_loglevel_forced = true;
+		}
 		mutex_unlock(&syslog_lock);
 		break;
 	/* Enable logging to console */
@@ -1920,20 +1936,41 @@ int do_syslog(int type, char __user *buf, int len, int source)
 		mutex_lock(&syslog_lock);
 		if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
 			console_loglevel = saved_console_loglevel;
+			if (saved_ignore_per_console_loglevel_forced)
+				ignore_per_console_loglevel = saved_ignore_per_console_loglevel;
 			saved_console_loglevel = LOGLEVEL_DEFAULT;
+			saved_ignore_per_console_loglevel_forced = false;
 		}
 		mutex_unlock(&syslog_lock);
 		break;
 	/* Set level of messages printed to console */
-	case SYSLOG_ACTION_CONSOLE_LEVEL:
-		if (len < 1 || len > 8)
+	case SYSLOG_ACTION_CONSOLE_LEVEL: {
+		int new_level = len;
+
+		if (!ignore_per_console_loglevel)
+			pr_warn_once(
+				"SYSLOG_ACTION_CONSOLE_LEVEL is ignored by consoles with an explicitly set per-console loglevel, see Documentation/admin-guide/per-console-loglevel.rst\n");
+		if (new_level < 1 || new_level > 8)
 			return -EINVAL;
-		if (len < minimum_console_loglevel)
-			len = minimum_console_loglevel;
-		console_loglevel = len;
-		/* Implicitly re-enable logging to console */
-		saved_console_loglevel = LOGLEVEL_DEFAULT;
+		if (new_level < minimum_console_loglevel)
+			new_level = minimum_console_loglevel;
+
+		mutex_lock(&syslog_lock);
+		console_loglevel = new_level;
+		/*
+		 * If SYSLOG_ACTION_CONSOLE_OFF forced ignore_per_console_loglevel,
+		 * restore the previous setting so per-console loglevels continue
+		 * to work after administrators re-enable logging via syslog().
+		 */
+		if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
+			if (saved_ignore_per_console_loglevel_forced)
+				ignore_per_console_loglevel = saved_ignore_per_console_loglevel;
+			saved_console_loglevel = LOGLEVEL_DEFAULT;
+			saved_ignore_per_console_loglevel_forced = false;
+		}
+		mutex_unlock(&syslog_lock);
 		break;
+	}
 	/* Number of chars in the log buffer */
 	case SYSLOG_ACTION_SIZE_UNREAD:
 		mutex_lock(&syslog_lock);
-- 
2.51.2


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ