Petr Mladek 39bffb
From: Petr Mladek <pmladek@suse.com>
Petr Mladek 39bffb
Date: Fri, 14 Jul 2017 14:51:13 +0200
Petr Mladek 39bffb
Subject: [PATCH] printk/console: Enhance the check for consoles using init
Petr Mladek 39bffb
 memory
Petr Mladek 39bffb
Git-commit: 5a814231ae3d4f248a8ecb668a072a1da471c656
Petr Mladek 39bffb
References: bsc#1063026
Petr Mladek 39bffb
Patch-Mainline: v4.14-rc1
Petr Mladek 39bffb
Petr Mladek 39bffb
printk_late_init() is responsible for disabling boot consoles that
Petr Mladek 39bffb
use init memory. It checks the address of struct console for this.
Petr Mladek 39bffb
Petr Mladek 39bffb
But this is not enough. For example, there are several early
Petr Mladek 39bffb
consoles that have write() method in the init section and
Petr Mladek 39bffb
struct console in the normal section. They are not disabled
Petr Mladek 39bffb
and could cause fancy and hard to debug system states.
Petr Mladek 39bffb
Petr Mladek 39bffb
It is even more complicated by the macros EARLYCON_DECLARE() and
Petr Mladek 39bffb
OF_EARLYCON_DECLARE() where various struct members are set at
Petr Mladek 39bffb
runtime by the provided setup() function.
Petr Mladek 39bffb
Petr Mladek 39bffb
I have tried to reproduce this problem and forced the classic uart
Petr Mladek 39bffb
early console to stay using keep_bootcon parameter. In particular
Petr Mladek 39bffb
I used earlycon=uart,io,0x3f8 keep_bootcon console=ttyS0,115200.
Petr Mladek 39bffb
The system did not boot:
Petr Mladek 39bffb
Petr Mladek 39bffb
[    1.570496] PM: Image not found (code -22)
Petr Mladek 39bffb
[    1.570496] PM: Image not found (code -22)
Petr Mladek 39bffb
[    1.571886] PM: Hibernation image not present or could not be loaded.
Petr Mladek 39bffb
[    1.571886] PM: Hibernation image not present or could not be loaded.
Petr Mladek 39bffb
[    1.576407] Freeing unused kernel memory: 2528K
Petr Mladek 39bffb
[    1.577244] kernel tried to execute NX-protected page - exploit attempt? (uid: 0)
Petr Mladek 39bffb
Petr Mladek 39bffb
The double lines are caused by having both early uart console and
Petr Mladek 39bffb
ttyS0 console enabled at the same time. The early console stopped
Petr Mladek 39bffb
working when the init memory was freed. Fortunately, the invalid
Petr Mladek 39bffb
call was caught by the NX-protexted page check and did not cause
Petr Mladek 39bffb
any silent fancy problems.
Petr Mladek 39bffb
Petr Mladek 39bffb
This patch adds a check for many other addresses stored in
Petr Mladek 39bffb
struct console. It omits setup() and match() that are used
Petr Mladek 39bffb
only when the console is registered. Therefore they have
Petr Mladek 39bffb
already been used at this point and there is no reason
Petr Mladek 39bffb
to use them again.
Petr Mladek 39bffb
Petr Mladek 39bffb
Link: http://lkml.kernel.org/r/1500036673-7122-3-git-send-email-pmladek@suse.com
Petr Mladek 39bffb
Cc: Steven Rostedt <rostedt@goodmis.org>
Petr Mladek 39bffb
Cc: Andrew Morton <akpm@linux-foundation.org>
Petr Mladek 39bffb
Cc: Peter Zijlstra <peterz@infradead.org>
Petr Mladek 39bffb
Cc: Matt Redfearn <matt.redfearn@imgtec.com>
Petr Mladek 39bffb
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Petr Mladek 39bffb
Cc: Jiri Slaby <jslaby@suse.com>
Petr Mladek 39bffb
Cc: "David S. Miller" <davem@davemloft.net>
Petr Mladek 39bffb
Cc: Alan Cox <gnomes@lxorguk.ukuu.org.uk>
Petr Mladek 39bffb
Cc: "Fabio M. Di Nitto" <fdinitto@redhat.com>
Petr Mladek 39bffb
Cc: linux-serial@vger.kernel.org
Petr Mladek 39bffb
Cc: linux-kernel@vger.kernel.org
Petr Mladek 39bffb
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Petr Mladek 39bffb
Signed-off-by: Petr Mladek <pmladek@suse.com>
Petr Mladek 39bffb
---
Petr Mladek 39bffb
 kernel/printk/printk.c | 12 ++++++++++--
Petr Mladek 39bffb
 1 file changed, 10 insertions(+), 2 deletions(-)
Petr Mladek 39bffb
Petr Mladek 39bffb
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
Petr Mladek 39bffb
index 76985ee3dfff..87f1a8f4e0f9 100644
Petr Mladek 39bffb
--- a/kernel/printk/printk.c
Petr Mladek 39bffb
+++ b/kernel/printk/printk.c
Petr Mladek 39bffb
@@ -2659,8 +2659,16 @@ static int __init printk_late_init(void)
Petr Mladek 39bffb
 	int ret;
Petr Mladek 39bffb
 
Petr Mladek 39bffb
 	for_each_console(con) {
Petr Mladek 39bffb
-		if ((con->flags & CON_BOOT) &&
Petr Mladek 39bffb
-		    init_section_intersects(con, sizeof(*con))) {
Petr Mladek 39bffb
+		if (!(con->flags & CON_BOOT))
Petr Mladek 39bffb
+			continue;
Petr Mladek 39bffb
+
Petr Mladek 39bffb
+		/* Check addresses that might be used for enabled consoles. */
Petr Mladek 39bffb
+		if (init_section_intersects(con, sizeof(*con)) ||
Petr Mladek 39bffb
+		    init_section_contains(con->write, 0) ||
Petr Mladek 39bffb
+		    init_section_contains(con->read, 0) ||
Petr Mladek 39bffb
+		    init_section_contains(con->device, 0) ||
Petr Mladek 39bffb
+		    init_section_contains(con->unblank, 0) ||
Petr Mladek 39bffb
+		    init_section_contains(con->data, 0)) {
Petr Mladek 39bffb
 			/*
Petr Mladek 39bffb
 			 * Please, consider moving the reported consoles out
Petr Mladek 39bffb
 			 * of the init section.
Petr Mladek 39bffb
-- 
Petr Mladek 39bffb
1.8.5.6
Petr Mladek 39bffb