checkpatch: Added warnings in favor of strscpy().
Subsystems (4)
PATCH 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.
---
This patch is already reviewed by mailing list [email protected]. Refer below link
<https://www.openwall.com/lists/kernel-hardening/2019/07/03/4>
Documentation/process/deprecated.rst | 6 +++--- scripts/checkpatch.pl | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-)
Documentation/process/deprecated.rst +3 -3
@@ -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 +5 -0
@@ -595,6 +595,11 @@ 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
Signature · 1 line
2.7.4
Joe Perches
maintainerQuoted 10 lines · Nitin Gote
Added warnings in checkpatch.pl script to :
1. Deprecate
strcpy()in favor ofstrscpy().
2. Deprecatestrlcpy()in favor ofstrscpy().
3. Deprecatestrncpy()in favor ofstrscpy()orstrscpy_pad().Updated
strncpy()section inDocumentation/process/deprecated.rstto coverstrscpy_pad()case.Signed-off-by Nitin Gote <[email protected]>
OK, for whatever reason, this when into a spam folder.
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
scripts/checkpatch.pl our %deprecated_apis = ( · line 603
Quoted 10 lines
scripts/checkpatch.pl +5
-0@@ -595,6 +595,11 @@ 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", );
$ git grep -w strcpy | wc -l 2239 $ git grep -w strlcpy | wc -l 1760 $ git grep -w strncpy | wc -l 839
These functions are _really_ commonly used in the kernel.
This should probably be a different %deprecated_string_api and these should probably not be emitted at WARN level when using command line option -f/--file but at CHECK level so that novice script users just don't send bad patches.
Also, perhaps there could be some macro for the relatively commonly used
strscpy(foo, bar, sizeof(foo))
and
strlcpy(foo, bar, sizeof(foo))
so argument 1 doesn't have to be repeated in the sizeof()
Something like:
#define stracpy(to, from) \
({ \
size_t size = ARRAY_SIZE(to); \
BUILD_BUG_ON(!__same_type(typeof(*to), char)); \
\
strscpy(to, from, size); \
})Joe Perches
maintainerRFC PATCH 1/1 string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().)
Quoted 9 lines · Joe Perches
Quoted 8 lines · Nitin Gote
Added warnings in checkpatch.pl script to :
1. Deprecate
strcpy()in favor ofstrscpy().
2. Deprecatestrlcpy()in favor ofstrscpy().
3. Deprecatestrncpy()in favor ofstrscpy()orstrscpy_pad().Updated
strncpy()section inDocumentation/process/deprecated.rstto coverstrscpy_pad()case.
[]
I sent a patch series for some strscpy/strlcpy misuses.
How about adding a macro helper to avoid the misuses like:
--- include/linux/string.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
include/linux/string.h +16 -0
@@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t);/* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count); +#define stracpy (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy(to, from, size); \ +}) + +#define stracpy_pad (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy_pad(to, from, size); \ +}) +#ifndef __HAVE_ARCH_STRCATextern char *strcat (char *,const char *);#endif
Re: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().)
Quoted 37 lines · Joe Perches
Quoted 9 lines · Joe Perches
Quoted 8 lines · Nitin Gote
Added warnings in checkpatch.pl script to :
1. Deprecate
strcpy()in favor ofstrscpy().
2. Deprecatestrlcpy()in favor ofstrscpy().
3. Deprecatestrncpy()in favor ofstrscpy()orstrscpy_pad().Updated
strncpy()section inDocumentation/process/deprecated.rstto coverstrscpy_pad()case.[]
I sent a patch series for some strscpy/strlcpy misuses.
How about adding a macro helper to avoid the misuses like:
--- include/linux/string.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/string.h +9
-0@@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t);/* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count); +#define stracpy (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy(to, from, size); \ +}) + +#define stracpy_pad (to, from) \
include/linux/string.h ssize_t strscpy(char *, const char *, size_t); · line 56
Quoted 10 lines
include/linux/string.h +7
-0+({ \ +size_t size =ARRAY_SIZE (to); \ +BUILD_BUG_ON (!__same_type (typeof (*to), char)); \ + \ +strscpy_pad (to, from, size); \ +}) +#ifndef __HAVE_ARCH_STRCATextern char *strcat (char *,const char *);#endif
This seems like a reasonable addition, yes. I think Coccinelle might actually be able to find all the existing strscpy(dst, src, sizeof(dst)) cases to jump-start this conversion.
Devil's advocate: this adds yet more string handling functions... will this cause even more confusion?
Signature · 1 line
Kees Cook
Joe Perches
maintainerRe: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().)
Quoted 44 lines · Kees Cook
Quoted 43 lines · Joe Perches
Quoted 9 lines · Joe Perches
Quoted 8 lines · Nitin Gote
Added warnings in checkpatch.pl script to :
1. Deprecate
strcpy()in favor ofstrscpy().
2. Deprecatestrlcpy()in favor ofstrscpy().
3. Deprecatestrncpy()in favor ofstrscpy()orstrscpy_pad().Updated
strncpy()section inDocumentation/process/deprecated.rstto coverstrscpy_pad()case.[]
I sent a patch series for some strscpy/strlcpy misuses.
How about adding a macro helper to avoid the misuses like:
--- include/linux/string.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/string.h +15
-0@@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t);/* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count); +#define stracpy (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy(to, from, size); \ +}) + +#define stracpy_pad (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy_pad(to, from, size); \ +})
Quoted 8 lines
include/linux/string.h ssize_t strscpy(char *, const char *, size_t); · line 56
Quoted 4 lines
include/linux/string.h +1
-0+#ifndef __HAVE_ARCH_STRCATextern char *strcat (char *,const char *);#endif This seems like a reasonable addition, yes. I think Coccinelle might actually be able to find all the existing
strscpy(dst, src, sizeof(dst))cases to jump-start this conversion.
I did that. It works. It's a lot of conversions.
$ cat str.cpy.cocci @@ expression e1; expression e2; @@
- strscpy(e1, e2, sizeof(e1))
+ stracpy(e1, e2)
@@
expression e1;
expression e2;
@@
- strlcpy(e1, e2, sizeof(e1))
+ stracpy(e1, e2)
Quoted 2 lines
Devil's advocate: this adds yet more string handling functions... will this cause even more confusion?
Documentation is good.
Actual in-kernel use and examples better.
Joe Perches
maintainerRe: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().)
Quoted 70 lines · Joe Perches
Quoted 44 lines · Kees Cook
Quoted 43 lines · Joe Perches
Quoted 9 lines · Joe Perches
Quoted 8 lines · Nitin Gote
Added warnings in checkpatch.pl script to :
1. Deprecate
strcpy()in favor ofstrscpy().
2. Deprecatestrlcpy()in favor ofstrscpy().
3. Deprecatestrncpy()in favor ofstrscpy()orstrscpy_pad().Updated
strncpy()section inDocumentation/process/deprecated.rstto coverstrscpy_pad()case.[]
I sent a patch series for some strscpy/strlcpy misuses.
How about adding a macro helper to avoid the misuses like:
--- include/linux/string.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/string.h +15
-0@@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t);/* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count); +#define stracpy (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy(to, from, size); \ +}) + +#define stracpy_pad (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy_pad(to, from, size); \ +})Quoted 8 lines
include/linux/string.h ssize_t strscpy(char *, const char *, size_t); · line 56
Quoted 4 lines
include/linux/string.h +1
-0+#ifndef __HAVE_ARCH_STRCATextern char *strcat (char *,const char *);#endif This seems like a reasonable addition, yes. I think Coccinelle might actually be able to find all the existing
strscpy(dst, src, sizeof(dst))cases to jump-start this conversion.I did that. It works. It's a lot of conversions.
$ cat str.cpy.cocci @@ expression e1; expression e2; @@-
strscpy(e1, e2, sizeof(e1))
+stracpy(e1, e2)@@
expression e1;
expression e2;
@@
Quoted 8 lines
-
strlcpy(e1, e2, sizeof(e1))
+stracpy(e1, e2)Quoted 2 lines
Devil's advocate: this adds yet more string handling functions... will this cause even more confusion?
Documentation is good.
Actual in-kernel use and examples better.
btw: I just ran this again and it produces:
$ spatch --in-place -sp-file str.cpy.cocci . $ git checkout tools/ $ git diff --shortstat 958 files changed, 2179 insertions(+), 2655 deletions(-)
Re: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().)
Quoted 80 lines · Joe Perches
Quoted 70 lines · Joe Perches
Quoted 44 lines · Kees Cook
Quoted 43 lines · Joe Perches
Quoted 9 lines · Joe Perches
Quoted 8 lines · Nitin Gote
Added warnings in checkpatch.pl script to :
1. Deprecate
strcpy()in favor ofstrscpy().
2. Deprecatestrlcpy()in favor ofstrscpy().
3. Deprecatestrncpy()in favor ofstrscpy()orstrscpy_pad().Updated
strncpy()section inDocumentation/process/deprecated.rstto coverstrscpy_pad()case.[]
I sent a patch series for some strscpy/strlcpy misuses.
How about adding a macro helper to avoid the misuses like:
--- include/linux/string.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/string.h +15
-0@@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t);/* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count); +#define stracpy (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy(to, from, size); \ +}) + +#define stracpy_pad (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy_pad(to, from, size); \ +})Quoted 8 lines
include/linux/string.h ssize_t strscpy(char *, const char *, size_t); · line 56
Quoted 4 lines
include/linux/string.h +1
-0+#ifndef __HAVE_ARCH_STRCATextern char *strcat (char *,const char *);#endif This seems like a reasonable addition, yes. I think Coccinelle might actually be able to find all the existing
strscpy(dst, src, sizeof(dst))cases to jump-start this conversion.I did that. It works. It's a lot of conversions.
$ cat str.cpy.cocci @@ expression e1; expression e2; @@-
strscpy(e1, e2, sizeof(e1))
+stracpy(e1, e2)@@
expression e1;
expression e2;
@@Quoted 8 lines
-
strlcpy(e1, e2, sizeof(e1))
+stracpy(e1, e2)Quoted 2 lines
Devil's advocate: this adds yet more string handling functions... will this cause even more confusion?
Documentation is good.
Actual in-kernel use and examples better.
Quoted 6 lines
btw: I just ran this again and it produces:
$ spatch --in-place -sp-file str.cpy.cocci . $ git checkout tools/ $ git diff --shortstat 958 files changed, 2179 insertions(+), 2655 deletions(-)
Cool. Well, assuming no one hates this, let's do it. :) Can you send a more complete patch with docs, etc? Maybe Linus will take it for late in the next merge window, perhaps?
Signature · 1 line
Kees Cook
Re: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().)
In reply to Joe PerchesQuoted 31 lines · Joe Perches
Quoted 30 lines · Joe Perches
Quoted 29 lines · Kees Cook
Quoted 28 lines · Joe Perches
Quoted 9 lines · Joe Perches
Quoted 8 lines · Nitin Gote
Added warnings in checkpatch.pl script to :
1. Deprecate
strcpy()in favor ofstrscpy().
2. Deprecatestrlcpy()in favor ofstrscpy().
3. Deprecatestrncpy()in favor ofstrscpy()orstrscpy_pad().Updated
strncpy()section inDocumentation/process/deprecated.rstto coverstrscpy_pad()case.[]
I sent a patch series for some strscpy/strlcpy misuses.
How about adding a macro helper to avoid the misuses like:
--- include/linux/string.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/string.h +0
-0@@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t);/* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count);
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
include/linux/string.h ssize_t strscpy(char *, const char *, size_t); · line 44
Quoted 7 lines
include/linux/string.h +7
-0+#define stracpy (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy(to, from, size); \ +})
Where does the 'a' in 'stracpy' come from? Googling around finds other people using a function called stracpy, but it takes different arguments.
http://stracpy.blogspot.com/ takes a size argument, as does
https://docs.polserver.com/doxygen/html/d5/dce/stracpy_8cpp_source.html
The one in the 'Links' webbrowser (can't find a link to its source) seems like a strdup clone.
Joe Perches
maintainerRe: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().)
Quoted 5 lines · Matthew Wilcox
Quoted 4 lines · Joe Perches
Quoted 3 lines · Joe Perches
Quoted 2 lines · Kees Cook
Quoted 1 line · Joe Perches
On Thu, 2019-07-04 at 13:46 -0700, Joe Perches wrote:
[]
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
include/linux/string.h ssize_t strscpy(char *, const char *, size_t); · line 44
Quoted 7 lines
include/linux/string.h +7
-0+#define stracpy (to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy(to, from, size); \ +})Where does the 'a' in 'stracpy' come from?
No place in particular.
I used it because dst has to be an 'a'rray rather
than a pointer.