diff mbox series

[v5,23/38] kmsan: printk: treat the result of vscnprintf() as initialized

Message ID 20200325161249.55095-24-glider@google.com (mailing list archive)
State New, archived
Headers show
Series Add KernelMemorySanitizer infrastructure | expand

Commit Message

Alexander Potapenko March 25, 2020, 4:12 p.m. UTC
In vprintk_store(), vscnprintf() may return an uninitialized text_len
value if any of its arguments are uninitialized. In that case KMSAN will
report one or more errors in vscnprintf() itself, but it doesn't make
much sense to track that value further, as it may trigger more errors in
printk. Instead, we explicitly mark it as initialized.

Signed-off-by: Alexander Potapenko <glider@google.com>
To: Alexander Potapenko <glider@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Wolfram Sang <wsa@the-dreams.de>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Vegard Nossum <vegard.nossum@oracle.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Andrey Konovalov <andreyknvl@google.com>
Cc: linux-mm@kvack.org
Acked-by: Petr Mladek <pmladek@suse.com>

---

This patch was split from "kmsan: call KMSAN hooks where needed", as
requested by Andrey Konovalov. Petr Mladek has previously acked the
printk part of that patch, hence the Acked-by above.

v4:
 - split this patch away

Change-Id: Ibed60b0bdd25f8ae91acee5800b5328e78e0735a
---
 kernel/printk/printk.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Andrey Konovalov April 14, 2020, 2:37 p.m. UTC | #1
On Wed, Mar 25, 2020 at 5:14 PM <glider@google.com> wrote:
>
> In vprintk_store(), vscnprintf() may return an uninitialized text_len
> value if any of its arguments are uninitialized. In that case KMSAN will
> report one or more errors in vscnprintf() itself, but it doesn't make
> much sense to track that value further, as it may trigger more errors in
> printk. Instead, we explicitly mark it as initialized.
>
> Signed-off-by: Alexander Potapenko <glider@google.com>
> To: Alexander Potapenko <glider@google.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Eric Dumazet <edumazet@google.com>
> Cc: Wolfram Sang <wsa@the-dreams.de>
> Cc: Petr Mladek <pmladek@suse.com>
> Cc: Vegard Nossum <vegard.nossum@oracle.com>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Cc: Marco Elver <elver@google.com>
> Cc: Andrey Konovalov <andreyknvl@google.com>
> Cc: linux-mm@kvack.org
> Acked-by: Petr Mladek <pmladek@suse.com>

Reviewed-by: Andrey Konovalov <andreyknvl@google.com>

>
> ---
>
> This patch was split from "kmsan: call KMSAN hooks where needed", as
> requested by Andrey Konovalov. Petr Mladek has previously acked the
> printk part of that patch, hence the Acked-by above.
>
> v4:
>  - split this patch away
>
> Change-Id: Ibed60b0bdd25f8ae91acee5800b5328e78e0735a
> ---
>  kernel/printk/printk.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index ad46062345452..4cadba3c1e68d 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -1913,6 +1913,12 @@ int vprintk_store(int facility, int level,
>          * prefix which might be passed-in as a parameter.
>          */
>         text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
> +       /*
> +        * If any of vscnprintf() arguments is uninitialized, KMSAN will report
> +        * one or more errors and also probably mark text_len as uninitialized.
> +        * Initialize |text_len| to prevent the errors from spreading further.
> +        */
> +       text_len = KMSAN_INIT_VALUE(text_len);
>
>         /* mark and strip a trailing newline */
>         if (text_len && text[text_len-1] == '\n') {
> --
> 2.25.1.696.g5e7596f4ac-goog
>
diff mbox series

Patch

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index ad46062345452..4cadba3c1e68d 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1913,6 +1913,12 @@  int vprintk_store(int facility, int level,
 	 * prefix which might be passed-in as a parameter.
 	 */
 	text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
+	/*
+	 * If any of vscnprintf() arguments is uninitialized, KMSAN will report
+	 * one or more errors and also probably mark text_len as uninitialized.
+	 * Initialize |text_len| to prevent the errors from spreading further.
+	 */
+	text_len = KMSAN_INIT_VALUE(text_len);
 
 	/* mark and strip a trailing newline */
 	if (text_len && text[text_len-1] == '\n') {