netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Richard Weinberger <richard@nod.at>
To: linux-hardening@vger.kernel.org
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	keescook@chromium.org, "Richard Weinberger" <richard@nod.at>,
	"Petr Mladek" <pmladek@suse.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Sergey Senozhatsky" <senozhatsky@chromium.org>,
	"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Jesper Dangaard Brouer" <hawk@kernel.org>,
	"John Fastabend" <john.fastabend@gmail.com>
Subject: [RFC PATCH 1/1] vsprintf: Warn on integer scanning overflows
Date: Thu,  8 Jun 2023 00:37:55 +0200	[thread overview]
Message-ID: <20230607223755.1610-2-richard@nod.at> (raw)
In-Reply-To: <20230607223755.1610-1-richard@nod.at>

The scanf function family has no way to indicate overflows
while scanning. As consequence users of these function have to make
sure their input cannot cause an overflow.
Since this is not always the case add WARN_ON_ONCE() guards to
trigger a warning upon an overflow.

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 lib/vsprintf.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 40f560959b169..3d8d751306cdc 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -70,6 +70,7 @@ static noinline unsigned long long simple_strntoull(const char *startp, size_t m
 	prefix_chars = cp - startp;
 	if (prefix_chars < max_chars) {
 		rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars);
+		WARN_ON_ONCE(rv & KSTRTOX_OVERFLOW);
 		/* FIXME */
 		cp += (rv & ~KSTRTOX_OVERFLOW);
 	} else {
@@ -3657,22 +3658,34 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
 
 		switch (qualifier) {
 		case 'H':	/* that's 'hh' in format */
-			if (is_sign)
+			if (is_sign) {
+				WARN_ON_ONCE(val.s > 127);
+				WARN_ON_ONCE(val.s < -128);
 				*va_arg(args, signed char *) = val.s;
-			else
+			} else {
+				WARN_ON_ONCE(val.u > 255);
 				*va_arg(args, unsigned char *) = val.u;
+			}
 			break;
 		case 'h':
-			if (is_sign)
+			if (is_sign) {
+				WARN_ON_ONCE(val.s > SHRT_MAX);
+				WARN_ON_ONCE(val.s < SHRT_MIN);
 				*va_arg(args, short *) = val.s;
-			else
+			} else {
+				WARN_ON_ONCE(val.u > USHRT_MAX);
 				*va_arg(args, unsigned short *) = val.u;
+			}
 			break;
 		case 'l':
-			if (is_sign)
+			if (is_sign) {
+				WARN_ON_ONCE(val.s > LONG_MAX);
+				WARN_ON_ONCE(val.s < LONG_MIN);
 				*va_arg(args, long *) = val.s;
-			else
+			} else {
+				WARN_ON_ONCE(val.u > ULONG_MAX);
 				*va_arg(args, unsigned long *) = val.u;
+			}
 			break;
 		case 'L':
 			if (is_sign)
@@ -3684,10 +3697,14 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
 			*va_arg(args, size_t *) = val.u;
 			break;
 		default:
-			if (is_sign)
+			if (is_sign) {
+				WARN_ON_ONCE(val.s > INT_MAX);
+				WARN_ON_ONCE(val.s < INT_MIN);
 				*va_arg(args, int *) = val.s;
-			else
+			} else {
+				WARN_ON_ONCE(val.u > UINT_MAX);
 				*va_arg(args, unsigned int *) = val.u;
+			}
 			break;
 		}
 		num++;
-- 
2.35.3


  reply	other threads:[~2023-06-07 22:38 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-07 22:37 [RFC PATCH 0/1] Integer overflows while scanning for integers Richard Weinberger
2023-06-07 22:37 ` Richard Weinberger [this message]
2023-06-08 14:27   ` [RFC PATCH 1/1] vsprintf: Warn on integer scanning overflows Andy Shevchenko
2023-06-08 16:14     ` Richard Weinberger
2023-06-08 16:23       ` Andy Shevchenko
2023-06-12  6:16   ` kernel test robot
2023-06-07 23:36 ` [RFC PATCH 0/1] Integer overflows while scanning for integers Kees Cook
2023-06-08 15:27   ` Petr Mladek
2023-06-08 16:12     ` Richard Weinberger
2023-06-08 16:19     ` Greg KH
2023-06-08 16:23       ` Richard Weinberger
2023-06-09 10:10     ` Rasmus Villemoes
2023-06-09 17:02       ` Petr Mladek
2023-06-09 17:46         ` Linus Torvalds
2023-06-10 19:31       ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230607223755.1610-2-richard@nod.at \
    --to=richard@nod.at \
    --cc=alex.gaynor@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=ast@kernel.org \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=gary@garyguo.net \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=keescook@chromium.org \
    --cc=kuba@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=netdev@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).