2018-09-28 14:46:49

by He Zhe

[permalink] [raw]
Subject: [PATCH v3 1/2] printk: Fix panic caused by passing log_buf_len to command line

From: He Zhe <[email protected]>

log_buf_len_setup does not check input argument before passing it to
simple_strtoull. The argument would be a NULL pointer if "log_buf_len",
without its value, is set in command line and thus causes the following
panic.

PANIC: early exception 0xe3 IP 10:ffffffffaaeacd0d error 0 cr2 0x0
[ 0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 4.19.0-rc4-yocto-standard+ #1
[ 0.000000] RIP: 0010:_parse_integer_fixup_radix+0xd/0x70
...
[ 0.000000] Call Trace:
[ 0.000000] simple_strtoull+0x29/0x70
[ 0.000000] memparse+0x26/0x90
[ 0.000000] log_buf_len_setup+0x17/0x22
[ 0.000000] do_early_param+0x57/0x8e
[ 0.000000] parse_args+0x208/0x320
[ 0.000000] ? rdinit_setup+0x30/0x30
[ 0.000000] parse_early_options+0x29/0x2d
[ 0.000000] ? rdinit_setup+0x30/0x30
[ 0.000000] parse_early_param+0x36/0x4d
[ 0.000000] setup_arch+0x336/0x99e
[ 0.000000] start_kernel+0x6f/0x4ee
[ 0.000000] x86_64_start_reservations+0x24/0x26
[ 0.000000] x86_64_start_kernel+0x6f/0x72
[ 0.000000] secondary_startup_64+0xa4/0xb0

This patch adds a check to prevent the panic and a check to report if someone is
setting it over 4G.

Signed-off-by: He Zhe <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
v2:
Split out the addition of pr_fmt and the unsigned update
v3:
Remove error message for NULL pointer
Add check and error message for over 4G use

kernel/printk/printk.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 9bf5404..1c932b6 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1037,18 +1037,29 @@ void log_buf_vmcoreinfo_setup(void)
static unsigned long __initdata new_log_buf_len;

/* we practice scaling the ring buffer by powers of 2 */
-static void __init log_buf_len_update(unsigned size)
+static void __init log_buf_len_update(u64 size)
{
+ if (size > UINT_MAX) {
+ size = UINT_MAX;
+ pr_err("log_buf over 4G is not supported.\n");
+ }
+
if (size)
size = roundup_pow_of_two(size);
if (size > log_buf_len)
- new_log_buf_len = size;
+ new_log_buf_len = (unsigned long)size;
}

/* save requested log_buf_len since it's too early to process it */
static int __init log_buf_len_setup(char *str)
{
- unsigned size = memparse(str, &str);
+ u64 size;
+
+ if (!str) {
+ return -EINVAL;
+ }
+
+ size = memparse(str, &str);

log_buf_len_update(size);

--
2.7.4



2018-09-28 14:47:09

by He Zhe

[permalink] [raw]
Subject: [PATCH v2 2/2] printk: Add KBUILD_MODNAME and correct wrong casting

From: He Zhe <[email protected]>

Add KBUILD_MODNAME to make prints more clear and correct wrong casting that
might cut off the normal output.

Signed-off-by: He Zhe <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
v2:
Correct wrong cast in sprintf

kernel/printk/printk.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 1c932b6..12ab154 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -16,6 +16,8 @@
* 01Mar01 Andrew Morton
*/

+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/tty.h>
@@ -2362,8 +2364,9 @@ void console_unlock(void)
printk_safe_enter_irqsave(flags);
raw_spin_lock(&logbuf_lock);
if (console_seq < log_first_seq) {
- len = sprintf(text, "** %u printk messages dropped **\n",
- (unsigned)(log_first_seq - console_seq));
+ len = sprintf(text,
+ "** %llu printk messages dropped **\n",
+ log_first_seq - console_seq);

/* messages are gone, move to first one */
console_seq = log_first_seq;
--
2.7.4


2018-09-29 08:26:47

by Sergey Senozhatsky

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] printk: Fix panic caused by passing log_buf_len to command line

On (09/28/18 22:46), [email protected] wrote:
> This patch adds a check to prevent the panic and a check to report if someone is
> setting it over 4G.

OK, He Zhe, you are almost there.
Let's do the series the following way:

- four patches
- each change goes into a separate patch
- the first two patches Cc stable


patch #0 Cc stable

---
unsigned int size;

if (!str)
return -EINVAL;
size = memparse(str, &str);

---


patch #1 Cc stable

---

len = sprintf(text,
"** %llu printk messages dropped **\n",
log_first_seq - console_seq);

---


=====================================================================

patch #2

---

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

---


patch #3

---

if (size > UINT_MAX) {
size = UINT_MAX;
pr_err("log_buf over 4G is not supported.\n");
}

...
---

-ss