Skip to contentSkip to conversation

vsprintf: Prevent crash when dereferencing invalid pointers for %pD

PATCH In review Jia He 4 participants 9 messages Aug 9, 2019 – Aug 16, 2019

Subsystems (2)
lore
Share & tools
lore

Jia He

author
#0 lore raw

PATCH 1/2 vsprintf: Prevent crash when dereferencing invalid pointers for %pD

Commit 3e5903eb9cff ("vsprintf: Prevent crash when dereferencing invalid pointers") prevents most crash except for %pD.
There is an additional pointer dereferencing before dentry_name.

At least, vma->file can be NULL and be passed to printk %pD in print_bad_pte, which can cause crash.

This patch fixes it with introducing a new file_dentry_name.

---
 lib/vsprintf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
lib/vsprintf.c +10 -3
index 63937044c57d..b4a119176fdb 100644
@@ -869,6 +869,15 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp
 	return widen_string(buf, n, end, spec);
 }
 
+static noinline_for_stack
+char *file_dentry_name(char *buf, char *end, const struct file *f,
+			struct printf_spec spec, const char *fmt)
+{
+	if (check_pointer(&buf, end, f, spec))
+		return buf;
+
+	return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
+}
 #ifdef CONFIG_BLOCK
 static noinline_for_stack
 char *bdev_name(char *buf, char *end, struct block_device *bdev,
@@ -2166,9 +2175,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 	case 'C':
 		return clock(buf, end, ptr, spec, fmt);
 	case 'D':
-		return dentry_name(buf, end,
-				   ((const struct file *)ptr)->f_path.dentry,
-				   spec, fmt);
+		return file_dentry_name(buf, end, ptr, spec, fmt);
 #ifdef CONFIG_BLOCK
 	case 'g':
 		return bdev_name(buf, end, ptr, spec, fmt);
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 lib/test_printf: add test of null/invalid pointer dereference for dentry

This add some additional test cases of null/invalid pointer dereference for dentry and file (%pd and %pD)

---
 lib/test_printf.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/test_printf.c +7 -0
index 944eb50f3862..befedffeb476 100644
@@ -455,6 +455,13 @@ dentry(void)
 	test("foo", "%pd", &test_dentry[0]);
 	test("foo", "%pd2", &test_dentry[0]);
 
+	/* test the null/invalid pointer case for dentry */
+	test("(null)", "%pd", NULL);
+	test("(efault)", "%pd", PTR_INVALID);
+	/* test the null/invalid pointer case for file */
+	test("(null)", "%pD", NULL);
+	test("(efault)", "%pD", PTR_INVALID);
+
 	test("romeo", "%pd", &test_dentry[3]);
 	test("alfa/romeo", "%pd2", &test_dentry[3]);
 	test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] lib/test_printf: add test of null/invalid pointer dereference for dentry

Quoted 4 lines · Jia He

This add some additional test cases of null/invalid pointer dereference for dentry and file (%pd and %pD)

Quoted 26 lines
---
 lib/test_printf.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/test_printf.c +7 -1
index 944eb50f3862..befedffeb476 100644
@@ -455,6 +455,13 @@ dentry(void)
        test("foo", "%pd", &test_dentry[0]);
        test("foo", "%pd2", &test_dentry[0]);
 
+       /* test the null/invalid pointer case for dentry */
+       test("(null)", "%pd", NULL);
+       test("(efault)", "%pd", PTR_INVALID);
+       /* test the null/invalid pointer case for file */
+       test("(null)", "%pD", NULL);
+       test("(efault)", "%pD", PTR_INVALID);
+
        test("romeo", "%pd", &test_dentry[3]);
        test("alfa/romeo", "%pd2", &test_dentry[3]);
        test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
--

2.17.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw

Re: [PATCH 2/2] lib/test_printf: add test of null/invalid pointer dereference for dentry

In reply to Jia He
Quoted 13 lines · Jia He

This add some additional test cases of null/invalid pointer dereference for dentry and file (%pd and %pD)

---
 lib/test_printf.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/test_printf.c +0 -0
index 944eb50f3862..befedffeb476 100644

lib/test_printf.c dentry(void) · line 461

Quoted 7 lines
lib/test_printf.c +4 -0
@@ -455,6 +455,13 @@ dentry(void)
 	test("foo", "%pd", &test_dentry[0]);
 	test("foo", "%pd2", &test_dentry[0]);
 
+	/* test the null/invalid pointer case for dentry */
+	test("(null)", "%pd", NULL);
+	test("(efault)", "%pd", PTR_INVALID);
+	/* test the null/invalid pointer case for file */

The two comments mention something that is obvious from the code.

I have pushed the patch as is and removed the comments in a follow up patch [1]. Both are in printk.git, branch for-5.4.

lib/test_printf.c dentry(void) · line 463

Quoted 2 lines
lib/test_printf.c +2 -0
+	test("(null)", "%pD", NULL);
+	test("(efault)", "%pD", PTR_INVALID);

Reference:
[1] https://git.kernel.org/pub/scm/linux/kernel/git/pmladek/printk.git/commit/?h=for-5.4&id=8ebea6ea1a7ed5d67ecbb2a493c716a2a89c0be2

Best Regards,
Petr

#4 lore raw

RE: [PATCH 2/2] lib/test_printf: add test of null/invalid pointer dereference for dentry

Hi Petr

Quoted 30 lines

-----Original Message-----
From: Petr Mladek <[email protected]>
Sent: 2019年8月16日 16:27
To: Justin He (Arm Technology China) <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>; Sergey Senozhatsky <[email protected]>; Thomas Gleixner <[email protected]>; Andy Shevchenko <[email protected]>; linux- [email protected]; Kees Cook <[email protected]>; Steven Rostedt (VMware) <[email protected]>; Shuah Khan <[email protected]>; Tobin C. Harding <[email protected]>
Subject: Re: [PATCH 2/2] lib/test_printf: add test of null/invalid pointer dereference for dentry

Quoted 16 lines · Jia He

This add some additional test cases of null/invalid pointer dereference for dentry and file (%pd and %pD)

---
 lib/test_printf.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/test_printf.c +0 -0
index 944eb50f3862..befedffeb476 100644
@@ -455,6 +455,13 @@ dentry(void)
    test("foo", "%pd", &test_dentry[0]);
    test("foo", "%pd2", &test_dentry[0]);
 
Quoted 7 lines

lib/test_printf.c dentry(void) · line 461

Quoted 4 lines
lib/test_printf.c +4 -0
+   /* test the null/invalid pointer case for dentry */
+   test("(null)", "%pd", NULL);
+   test("(efault)", "%pd", PTR_INVALID);
+   /* test the null/invalid pointer case for file */

The two comments mention something that is obvious from the code.

No problem, ok with me 😊

--
Cheers,
Justin (Jia He)

Quoted 6 lines

I have pushed the patch as is and removed the comments in a follow up patch [1]. Both are in printk.git, branch for-5.4.

lib/test_printf.c dentry(void) · line 463

Quoted 2 lines
lib/test_printf.c +2 -0
+   test("(null)", "%pD", NULL);
+   test("(efault)", "%pD", PTR_INVALID);
Quoted 7 lines

Reference:
[1]
https://git.kernel.org/pub/scm/linux/kernel/git/pmladek/printk.git/commi
t/?h=for-5.4&id=8ebea6ea1a7ed5d67ecbb2a493c716a2a89c0be2

Best Regards,
Petr

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Reviewed-by on v1 1/2
#5 lore raw
In reply to Jia He
Quoted 10 lines · Jia He

Commit 3e5903eb9cff ("vsprintf: Prevent crash when dereferencing invalid pointers") prevents most crash except for %pD.
There is an additional pointer dereferencing before dentry_name.

At least, vma->file can be NULL and be passed to printk %pD in print_bad_pte, which can cause crash.

This patch fixes it with introducing a new file_dentry_name.

Perhaps you need to add a Fixes tag

Quoted 39 lines
---
 lib/vsprintf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
lib/vsprintf.c +10 -4
index 63937044c57d..b4a119176fdb 100644
@@ -869,6 +869,15 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp
        return widen_string(buf, n, end, spec);
 }
 
+static noinline_for_stack
+char *file_dentry_name(char *buf, char *end, const struct file *f,
+                       struct printf_spec spec, const char *fmt)
+{
+       if (check_pointer(&buf, end, f, spec))
+               return buf;
+
+       return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
+}
 #ifdef CONFIG_BLOCK
 static noinline_for_stack
 char *bdev_name(char *buf, char *end, struct block_device *bdev,
@@ -2166,9 +2175,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
        case 'C':
                return clock(buf, end, ptr, spec, fmt);
        case 'D':
-               return dentry_name(buf, end,
-                                  ((const struct file *)ptr)->f_path.dentry,
-                                  spec, fmt);
+               return file_dentry_name(buf, end, ptr, spec, fmt);
 #ifdef CONFIG_BLOCK
        case 'g':
                return bdev_name(buf, end, ptr, spec, fmt);
--

2.17.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 27 lines

-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: 2019年8月9日 18:52
To: Justin He (Arm Technology China) <[email protected]>
Cc: Petr Mladek <[email protected]>; Andy Shevchenko <[email protected]>; Sergey Senozhatsky <[email protected]>; Geert Uytterhoeven <[email protected]>; Linux Kernel Mailing List <linux- [email protected]>; Thomas Gleixner <[email protected]>; Steven Rostedt (VMware) <[email protected]>; Kees Cook <[email protected]>; Shuah Khan <[email protected]>; Tobin C. Harding <[email protected]>
Subject: Re: [PATCH 1/2] vsprintf: Prevent crash when dereferencing invalid pointers for %pD

Quoted 2 lines · Jia He

Commit 3e5903eb9cff ("vsprintf: Prevent crash when dereferencing

invalid

Quoted 6 lines

pointers") prevents most crash except for %pD.
There is an additional pointer dereferencing before dentry_name.

At least, vma->file can be NULL and be passed to printk %pD in print_bad_pte, which can cause crash.

Quoted 6 lines
Quoted 2 lines

This patch fixes it with introducing a new file_dentry_name.

Perhaps you need to add a Fixes tag

Thanks, Andy

Fixes 3e5903eb9cff ("vsprintf: Prevent crash when dereferencing invalid pointers")

Need I reposted v2?

--
Cheers,
Justin (Jia He)

Quoted 44 lines

lib/vsprintf.c char *dentry_name(char *buf, char *end, const

Quoted 11 lines
---
 lib/vsprintf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
lib/vsprintf.c +0 -0
index 63937044c57d..b4a119176fdb 100644
@@ -869,6 +869,15 @@ char *dentry_name(char *buf, char *end, const

struct dentry *d, struct printf_sp

lib/vsprintf.c char *pointer(const char *fmt, char *buf, char

Quoted 16 lines
lib/vsprintf.c +9 -0
        return widen_string(buf, n, end, spec);
 }
 
+static noinline_for_stack
+char *file_dentry_name(char *buf, char *end, const struct file *f,
+                       struct printf_spec spec, const char *fmt)
+{
+       if (check_pointer(&buf, end, f, spec))
+               return buf;
+
+       return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
+}
 #ifdef CONFIG_BLOCK
 static noinline_for_stack
 char *bdev_name(char *buf, char *end, struct block_device *bdev,
@@ -2166,9 +2175,7 @@ char *pointer(const char *fmt, char *buf, char

*end, void *ptr,

Quoted 11 lines
lib/vsprintf.c +1 -4
        case 'C':
                return clock(buf, end, ptr, spec, fmt);
        case 'D':
-               return dentry_name(buf, end,
-                                  ((const struct file *)ptr)->f_path.dentry,
-                                  spec, fmt);
+               return file_dentry_name(buf, end, ptr, spec, fmt);
 #ifdef CONFIG_BLOCK
        case 'g':
                return bdev_name(buf, end, ptr, spec, fmt);
--
Quoted 2 lines

2.17.1

Quoted 3 lines

--
With Best Regards,
Andy Shevchenko

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

maintainer
#7 lore raw
Quoted 27 lines · Justin He (Arm Technology China)
Quoted 27 lines

-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: 2019年8月9日 18:52
To: Justin He (Arm Technology China) <[email protected]>
Cc: Petr Mladek <[email protected]>; Andy Shevchenko <[email protected]>; Sergey Senozhatsky <[email protected]>; Geert Uytterhoeven <[email protected]>; Linux Kernel Mailing List <linux- [email protected]>; Thomas Gleixner <[email protected]>; Steven Rostedt (VMware) <[email protected]>; Kees Cook <[email protected]>; Shuah Khan <[email protected]>; Tobin C. Harding <[email protected]>
Subject: Re: [PATCH 1/2] vsprintf: Prevent crash when dereferencing invalid pointers for %pD

Quoted 2 lines · Jia He

Commit 3e5903eb9cff ("vsprintf: Prevent crash when dereferencing

invalid

Quoted 6 lines

pointers") prevents most crash except for %pD.
There is an additional pointer dereferencing before dentry_name.

At least, vma->file can be NULL and be passed to printk %pD in print_bad_pte, which can cause crash.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

This patch fixes it with introducing a new file_dentry_name.

Perhaps you need to add a Fixes tag

Thanks, Andy

Fixes 3e5903eb9cff ("vsprintf: Prevent crash when dereferencing invalid pointers")

I have added the Fixes tag and pushed the patch into printk.git, branch for-5.4.

Thanks for the fix.

Best Regards,
Petr

Reviewed-by on v1 1/2
#8 lore raw
In reply to Jia He
Quoted 10 lines · Jia He

Commit 3e5903eb9cff ("vsprintf: Prevent crash when dereferencing invalid pointers") prevents most crash except for %pD.
There is an additional pointer dereferencing before dentry_name.

At least, vma->file can be NULL and be passed to printk %pD in print_bad_pte, which can cause crash.

This patch fixes it with introducing a new file_dentry_name.

The series looks OK to me.

FWIW,

	-ss