vsprintf: Prevent crash when dereferencing invalid pointers for %pD
Subsystems (2)
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
@@ -869,6 +869,15 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spreturn 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
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
@@ -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
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
Signed-off-by Jia He <[email protected]>--- lib/test_printf.c | 7 +++++++ 1 file changed, 7 insertions(+)lib/test_printf.c +7
-1@@ -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
Petr Mladek
maintainerRe: [PATCH 2/2] lib/test_printf: add test of null/invalid pointer dereference for dentry
In reply to Jia HeQuoted 13 lines · Jia He
This add some additional test cases of null/invalid pointer dereference for dentry and file (%pd and %pD)
Signed-off-by Jia He <[email protected]>--- lib/test_printf.c | 7 +++++++ 1 file changed, 7 insertions(+)lib/test_printf.c +0
-0
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 );
Best Regards,
Petr
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 dentryQuoted 16 lines · Jia He
This add some additional test cases of null/invalid pointer dereference for dentry and file (%pd and %pD)
Signed-off-by Jia He <[email protected]>--- lib/test_printf.c | 7 +++++++ 1 file changed, 7 insertions(+)lib/test_printf.c +0
-0@@ -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=8ebea6ea1a7ed5d67ecbb2a493c716a2a89c0be2Best 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.
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 beforedentry_name.At least,
vma->filecan be NULL and be passed to printk %pD inprint_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
Signed-off-by Jia He <[email protected]>--- lib/vsprintf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)lib/vsprintf.c +10
-4@@ -869,6 +869,15 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spreturn 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
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 %pDQuoted 2 lines · Jia He
Commit
3e5903eb9cff("vsprintf: Prevent crash when dereferencinginvalid
Quoted 6 lines
pointers") prevents most crash except for %pD.
There is an additional pointer dereferencing beforedentry_name.At least,
vma->filecan be NULL and be passed to printk %pD inprint_bad_pte, which can cause crash.
Quoted 6 lines
Quoted 2 lines
This patch fixes it with introducing a new
file_dentry_name.Reviewed-by Andy Shevchenko <[email protected]>Perhaps you need to add a Fixes tag
Thanks, Andy
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
Signed-off-by Jia He <[email protected]>--- lib/vsprintf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)lib/vsprintf.c +0
-0@@ -869,6 +869,15 @@ char *dentry_name(char *buf, char *end, conststruct dentry *d, struct
printf_splib/vsprintf.c char *pointer(const char *fmt, char *buf, char
Quoted 16 lines
lib/vsprintf.c +9
-0return 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
-4case '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.
Petr Mladek
maintainerQuoted 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 %pDQuoted 2 lines · Jia He
Commit
3e5903eb9cff("vsprintf: Prevent crash when dereferencinginvalid
Quoted 6 lines
pointers") prevents most crash except for %pD.
There is an additional pointer dereferencing beforedentry_name.At least,
vma->filecan be NULL and be passed to printk %pD inprint_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.Reviewed-by Andy Shevchenko <[email protected]>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
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 beforedentry_name.At least,
vma->filecan be NULL and be passed to printk %pD inprint_bad_pte, which can cause crash.This patch fixes it with introducing a new
file_dentry_name.Signed-off-by Jia He <[email protected]>
The series looks OK to me.
FWIW,
-ss