summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorTobin C. Harding <tobin@kernel.org>2019-04-05 12:58:58 +1100
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2021-02-13 13:51:15 +0100
commit2a3687034e47c51cd4f01ce82ed16f1964008b9e (patch)
tree675886c2eab4da0fa55fbe27e21303f2b8665234 /include
parent9848962bfb28515c476bcf34a8ec9655ccee08e4 (diff)
lib/string: Add strscpy_pad() function
[ Upstream commit 458a3bf82df4fe1f951d0f52b1e0c1e9d5a88a3b ] We have a function to copy strings safely and we have a function to copy strings and zero the tail of the destination (if source string is shorter than destination buffer) but we do not have a function to do both at once. This means developers must write this themselves if they desire this functionality. This is a chore, and also leaves us open to off by one errors unnecessarily. Add a function that calls strscpy() then memset()s the tail to zero if the source string is shorter than the destination buffer. Acked-by: Kees Cook <keescook@chromium.org> Signed-off-by: Tobin C. Harding <tobin@kernel.org> Signed-off-by: Shuah Khan <shuah@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'include')
-rw-r--r--include/linux/string.h4
1 files changed, 4 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h
index 4db285b83f44..1e0c442b941e 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -31,6 +31,10 @@ size_t strlcpy(char *, const char *, size_t);
#ifndef __HAVE_ARCH_STRSCPY
ssize_t strscpy(char *, const char *, size_t);
#endif
+
+/* Wraps calls to strscpy()/memset(), no arch specific code required */
+ssize_t strscpy_pad(char *dest, const char *src, size_t count);
+
#ifndef __HAVE_ARCH_STRCAT
extern char * strcat(char *, const char *);
#endif