[PATCH] checkpatch: add a new check for strcpy/strlcpy uses

From: Dwaipayan Ray
Date: Tue Jan 05 2021 - 03:24:38 EST


strcpy() performs no bounds checking on the destination buffer.
This could result in linear overflows beyond the end of the buffer.

strlcpy() reads the entire source buffer first. This read
may exceed the destination size limit. This can be both inefficient
and lead to linear read overflows.

The safe replacement to both of these is to use strscpy() instead.
Add a new checkpatch warning which alerts the user on finding usage of
strcpy() or strlcpy().

Signed-off-by: Dwaipayan Ray <dwaipayanray1@xxxxxxxxx>
---
scripts/checkpatch.pl | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d6a4d25b0972..0003fd9de62c 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6604,6 +6604,13 @@ sub process {
}
}

+# Check for strcpy/strlcpy uses
+ if (defined($stat) &&
+ $stat =~ /^\+(?:.*?)\b(str[l]?cpy)\s*\(/) {
+ WARN("PREFER_STRSCPY",
+ "Prefer strscpy() over $1()\n" . "$here\n$stat\n");
+ }
+
# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
# if ($perl_version_ok &&
# defined $stat &&
--
2.27.0