From: Kees Cook Date: Wed, 25 Sep 2019 23:46:20 +0000 (-0700) Subject: strscpy: reject buffer sizes larger than INT_MAX X-Git-Tag: v5.4-rc1~46^2~67 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9a156466147b61504f4cbe97ea503e67c21e117a;p=thirdparty%2Fkernel%2Fstable.git strscpy: reject buffer sizes larger than INT_MAX As already done for snprintf(), add a check in strscpy() for giant (i.e. likely negative and/or miscalculated) copy sizes, WARN, and error out. Link: http://lkml.kernel.org/r/201907260928.23DE35406@keescook Signed-off-by: Kees Cook Cc: Joe Perches Cc: Rasmus Villemoes Cc: Yann Droneaud Cc: David Laight Cc: Jonathan Corbet Cc: Stephen Kitt Cc: Jann Horn Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- diff --git a/lib/string.c b/lib/string.c index f7bc10da4259f..cd7a10c192109 100644 --- a/lib/string.c +++ b/lib/string.c @@ -183,7 +183,7 @@ ssize_t strscpy(char *dest, const char *src, size_t count) size_t max = count; long res = 0; - if (count == 0) + if (count == 0 || WARN_ON_ONCE(count > INT_MAX)) return -E2BIG; #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS