diff options
author | Petr Mladek <pmladek@suse.com> | 2019-04-17 13:53:50 +0200 |
---|---|---|
committer | Petr Mladek <pmladek@suse.com> | 2019-04-26 16:21:22 +0200 |
commit | c8c3b584343cb7522fc00322769a9f288305743f (patch) | |
tree | 4c0fb6e7eb3ddd39db25d45d0244cb1cc1632363 /lib/vsprintf.c | |
parent | 635720ac75a51092b456bed517ff170047883252 (diff) | |
download | linux-c8c3b584343cb7522fc00322769a9f288305743f.tar.bz2 |
vsprintf: Limit the length of inlined error messages
The inlined error messages must be used carefully because
they need to fit into the given buffer.
Handle them using a custom wrapper that makes people aware
of the problem. Also define a reasonable hard limit to
avoid a completely insane usage.
Suggested-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Link: http://lkml.kernel.org/r/20190417115350.20479-11-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/vsprintf.c')
-rw-r--r-- | lib/vsprintf.c | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 4e5666035b74..1f367f3a7e2b 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -612,6 +612,21 @@ static char *string_nocheck(char *buf, char *end, const char *s, return widen_string(buf, len, end, spec); } +/* Be careful: error messages must fit into the given buffer. */ +static char *error_string(char *buf, char *end, const char *s, + struct printf_spec spec) +{ + /* + * Hard limit to avoid a completely insane messages. It actually + * works pretty well because most error messages are in + * the many pointer format modifiers. + */ + if (spec.precision == -1) + spec.precision = 2 * sizeof(void *); + + return string_nocheck(buf, end, s, spec); +} + /* * This is not a fool-proof test. 99% of the time that this will fault is * due to a bad pointer, not one that crosses into bad memory. Just test @@ -638,7 +653,7 @@ static int check_pointer(char **buf, char *end, const void *ptr, err_msg = check_pointer_msg(ptr); if (err_msg) { - *buf = string_nocheck(*buf, end, err_msg, spec); + *buf = error_string(*buf, end, err_msg, spec); return -EFAULT; } @@ -741,7 +756,7 @@ static char *ptr_to_id(char *buf, char *end, const void *ptr, if (static_branch_unlikely(¬_filled_random_ptr_key)) { spec.field_width = 2 * sizeof(ptr); /* string length must be less than default_width */ - return string_nocheck(buf, end, str, spec); + return error_string(buf, end, str, spec); } #ifdef CONFIG_64BIT @@ -777,7 +792,7 @@ char *restricted_pointer(char *buf, char *end, const void *ptr, if (in_irq() || in_serving_softirq() || in_nmi()) { if (spec.field_width == -1) spec.field_width = 2 * sizeof(ptr); - return string_nocheck(buf, end, "pK-error", spec); + return error_string(buf, end, "pK-error", spec); } /* @@ -1511,12 +1526,12 @@ char *ip_addr_string(char *buf, char *end, const void *ptr, case AF_INET6: return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); default: - return string_nocheck(buf, end, "(einval)", spec); + return error_string(buf, end, "(einval)", spec); }} } err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; - return string_nocheck(buf, end, err_fmt_msg, spec); + return error_string(buf, end, err_fmt_msg, spec); } static noinline_for_stack @@ -1653,7 +1668,7 @@ char *netdev_bits(char *buf, char *end, const void *addr, size = sizeof(netdev_features_t); break; default: - return string_nocheck(buf, end, "(%pN?)", spec); + return error_string(buf, end, "(%pN?)", spec); } return special_hex_number(buf, end, num, size); @@ -1765,7 +1780,7 @@ char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, case 'R': return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); default: - return string_nocheck(buf, end, "(%ptR?)", spec); + return error_string(buf, end, "(%ptR?)", spec); } } @@ -1774,7 +1789,7 @@ char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, const char *fmt) { if (!IS_ENABLED(CONFIG_HAVE_CLK)) - return string_nocheck(buf, end, "(%pC?)", spec); + return error_string(buf, end, "(%pC?)", spec); if (check_pointer(&buf, end, clk, spec)) return buf; @@ -1785,7 +1800,7 @@ char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, #ifdef CONFIG_COMMON_CLK return string(buf, end, __clk_get_name(clk), spec); #else - return string_nocheck(buf, end, "(%pC?)", spec); + return error_string(buf, end, "(%pC?)", spec); #endif } } @@ -1843,7 +1858,7 @@ char *flags_string(char *buf, char *end, void *flags_ptr, names = gfpflag_names; break; default: - return string_nocheck(buf, end, "(%pG?)", spec); + return error_string(buf, end, "(%pG?)", spec); } return format_flags(buf, end, flags, names); @@ -1899,7 +1914,7 @@ char *device_node_string(char *buf, char *end, struct device_node *dn, str_spec.field_width = -1; if (!IS_ENABLED(CONFIG_OF)) - return string_nocheck(buf, end, "(%pOF?)", spec); + return error_string(buf, end, "(%pOF?)", spec); if (check_pointer(&buf, end, dn, spec)) return buf; @@ -1978,7 +1993,7 @@ static char *kobject_string(char *buf, char *end, void *ptr, return device_node_string(buf, end, ptr, spec, fmt + 1); } - return string_nocheck(buf, end, "(%pO?)", spec); + return error_string(buf, end, "(%pO?)", spec); } /* |