Skip to contentSkip to conversation

checkpatch: Added warnings in favor of strscpy().

PATCH In review Nitin Gote 2 participants 2 messages Jul 3, 2019

Subsystems (3)
lore
Share & tools
lore

Nitin Gote

author
#0 lore raw

PATCH v2 1/1 checkpatch: Added warnings in favor of strscpy().

Added warnings in checkpatch.pl script to :

1. Deprecate strcpy() in favor of strscpy().
2. Deprecate strlcpy() in favor of strscpy().
3. Deprecate strncpy() in favor of strscpy() or strscpy_pad().

Updated strncpy() section in Documentation/process/deprecated.rst to cover strscpy_pad() case.

---
 Documentation/process/deprecated.rst | 6 +++---
 scripts/checkpatch.pl                | 4 ++++
 2 files changed, 7 insertions(+), 3 deletions(-)
Documentation/process/deprecated.rst +3 -3
index 49e0f64..f564de3 100644
@@ -93,9 +93,9 @@ will be NUL terminated. This can lead to various linear read overflows
 and other misbehavior due to the missing termination. It also NUL-pads the
 destination buffer if the source contents are shorter than the destination
 buffer size, which may be a needless performance penalty for callers using
-only NUL-terminated strings. The safe replacement is :c:func:`strscpy`.
-(Users of :c:func:`strscpy` still needing NUL-padding will need an
-explicit :c:func:`memset` added.)
+only NUL-terminated strings. In this case, the safe replacement is
+:c:func:`strscpy`. If, however, the destination buffer still needs
+NUL-padding, the safe replacement is :c:func:`strscpy_pad`.
 
 If a caller is using non-NUL-terminated strings, :c:func:`strncpy()` can
 still be used, but destinations should be marked with the `__nonstring
scripts/checkpatch.pl +0 -0
index 342c7c7..2ce2340 100755
@@ -595,6 +595,10 @@ our %deprecated_apis = (

"rcu_barrier_sched" => "rcu_barrier",
"get_state_synchronize_sched" => "get_state_synchronize_rcu",
"cond_synchronize_sched" => "cond_synchronize_rcu",
+ "strcpy" => "strscpy",
+ "strlcpy" => "strscpy",
+ "strncpy" => "strscpy, strscpy_pad or for non-NUL-terminated strings,
+ strncpy() can still be used, but destinations should be marked with the __nonstring",
);

#Create a search pattern for all these strings to speed up a loop below
--
2.7.4

Acked-by on v2 1/1
#1 lore raw
Quoted 10 lines · Nitin Gote

Added warnings in checkpatch.pl script to :

1. Deprecate strcpy() in favor of strscpy().
2. Deprecate strlcpy() in favor of strscpy().
3. Deprecate strncpy() in favor of strscpy() or strscpy_pad().

Updated strncpy() section in Documentation/process/deprecated.rst to cover strscpy_pad() case.

This looks good. I think you're ready for sending this upstream. Please see:
https://www.kernel.org/doc/html/latest/process/submitting-patches.html

The To: should likely be:
Andrew Morton <[email protected]>
and I recommend CC to:
Jonathan Corbet <[email protected]>
Andy Whitcroft <[email protected]>
Joe Perches <[email protected]>
[email protected]
[email protected]
[email protected]

Thanks!

-Kees

Quoted 39 lines
---
 Documentation/process/deprecated.rst | 6 +++---
 scripts/checkpatch.pl                | 4 ++++
 2 files changed, 7 insertions(+), 3 deletions(-)
Documentation/process/deprecated.rst +3 -3
index 49e0f64..f564de3 100644
@@ -93,9 +93,9 @@ will be NUL terminated. This can lead to various linear read overflows
 and other misbehavior due to the missing termination. It also NUL-pads the
 destination buffer if the source contents are shorter than the destination
 buffer size, which may be a needless performance penalty for callers using
-only NUL-terminated strings. The safe replacement is :c:func:`strscpy`.
-(Users of :c:func:`strscpy` still needing NUL-padding will need an
-explicit :c:func:`memset` added.)
+only NUL-terminated strings. In this case, the safe replacement is
+:c:func:`strscpy`. If, however, the destination buffer still needs
+NUL-padding, the safe replacement is :c:func:`strscpy_pad`.
 
 If a caller is using non-NUL-terminated strings, :c:func:`strncpy()` can
 still be used, but destinations should be marked with the `__nonstring
scripts/checkpatch.pl +4 -1
index 342c7c7..2ce2340 100755
@@ -595,6 +595,10 @@ our %deprecated_apis = (
 	"rcu_barrier_sched"			=> "rcu_barrier",
 	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
 	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
+	"strcpy"				=> "strscpy",
+	"strlcpy"				=> "strscpy",
+	"strncpy"				=> "strscpy, strscpy_pad or for non-NUL-terminated strings,
+	 strncpy() can still be used, but destinations should be marked with the __nonstring",
 );
 
 #Create a search pattern for all these strings to speed up a loop below
--

2.7.4

Signature · 1 line
Kees Cook