aboutsummaryrefslogtreecommitdiffstats
path: root/lib/test_printf.c
diff options
context:
space:
mode:
authorPetr Mladek <pmladek@suse.com>2019-04-17 07:53:48 -0400
committerPetr Mladek <pmladek@suse.com>2019-04-26 10:20:43 -0400
commit3e5903eb9cff707301712498aed9e34b3e2ee883 (patch)
tree1f2e896d9b3a1c9e064ba8defe626b25bd7c0e76 /lib/test_printf.c
parent0b74d4d763fd4ee9daa53889324300587c015338 (diff)
vsprintf: Prevent crash when dereferencing invalid pointers
We already prevent crash when dereferencing some obviously broken pointers. But the handling is not consistent. Sometimes we print "(null)" only for pure NULL pointer, sometimes for pointers in the first page and sometimes also for pointers in the last page (error codes). Note that printk() call this code under logbuf_lock. Any recursive printks are redirected to the printk_safe implementation and the messages are stored into per-CPU buffers. These buffers might be eventually flushed in printk_safe_flush_on_panic() but it is not guaranteed. This patch adds a check using probe_kernel_read(). It is not a full-proof test. But it should help to see the error message in 99% situations where the kernel would silently crash otherwise. Also it makes the error handling unified for "%s" and the many %p* specifiers that need to read the data from a given address. We print: + (null) when accessing data on pure pure NULL address + (efault) when accessing data on an invalid address It does not affect the %p* specifiers that just print the given address in some form, namely %pF, %pf, %pS, %ps, %pB, %pK, %px, and plain %p. Note that we print (efault) from security reasons. In fact, the real address can be seen only by %px or eventually %pK. Link: http://lkml.kernel.org/r/20190417115350.20479-9-pmladek@suse.com To: Rasmus Villemoes <linux@rasmusvillemoes.dk> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: "Tobin C . Harding" <me@tobin.cc> Cc: Joe Perches <joe@perches.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Michal Hocko <mhocko@suse.cz> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> Cc: linux-kernel@vger.kernel.org Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Petr Mladek <pmladek@suse.com>
Diffstat (limited to 'lib/test_printf.c')
-rw-r--r--lib/test_printf.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/lib/test_printf.c b/lib/test_printf.c
index 250ee864b8b8..359ae4fb1ece 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -239,6 +239,7 @@ plain_format(void)
239#define PTR ((void *)0x456789ab) 239#define PTR ((void *)0x456789ab)
240#define PTR_STR "456789ab" 240#define PTR_STR "456789ab"
241#define PTR_VAL_NO_CRNG "(ptrval)" 241#define PTR_VAL_NO_CRNG "(ptrval)"
242#define ZEROS ""
242 243
243static int __init 244static int __init
244plain_format(void) 245plain_format(void)
@@ -268,7 +269,6 @@ plain_hash_to_buffer(const void *p, char *buf, size_t len)
268 return 0; 269 return 0;
269} 270}
270 271
271
272static int __init 272static int __init
273plain_hash(void) 273plain_hash(void)
274{ 274{
@@ -326,6 +326,24 @@ test_hashed(const char *fmt, const void *p)
326} 326}
327 327
328static void __init 328static void __init
329null_pointer(void)
330{
331 test_hashed("%p", NULL);
332 test(ZEROS "00000000", "%px", NULL);
333 test("(null)", "%pE", NULL);
334}
335
336#define PTR_INVALID ((void *)0x000000ab)
337
338static void __init
339invalid_pointer(void)
340{
341 test_hashed("%p", PTR_INVALID);
342 test(ZEROS "000000ab", "%px", PTR_INVALID);
343 test("(efault)", "%pE", PTR_INVALID);
344}
345
346static void __init
329symbol_ptr(void) 347symbol_ptr(void)
330{ 348{
331} 349}
@@ -571,6 +589,8 @@ static void __init
571test_pointer(void) 589test_pointer(void)
572{ 590{
573 plain(); 591 plain();
592 null_pointer();
593 invalid_pointer();
574 symbol_ptr(); 594 symbol_ptr();
575 kernel_ptr(); 595 kernel_ptr();
576 struct_resource(); 596 struct_resource();