Skip to contentSkip to conversation

strscpy: reject buffer sizes larger than INT_MAX

PATCH New Kees Cook 1 participant 1 message Jul 26, 2019

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 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.

---
 lib/string.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/string.c +1 -1
index 461fb620f85f..913cb945a82a 100644
@@ -182,7 +182,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
Signature · 5 lines
2.17.1


-- 
Kees Cook