summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobin C. Harding <tobin@kernel.org>2019-04-05 12:58:58 +1100
committerShuah Khan <shuah@kernel.org>2019-04-08 16:44:21 -0600
commit458a3bf82df4fe1f951d0f52b1e0c1e9d5a88a3b (patch)
tree4d9df5df245f8f068f6624bbf6cf308784c90a94
parentlib: Use new kselftest header (diff)
downloadlinux-458a3bf82df4fe1f951d0f52b1e0c1e9d5a88a3b.tar.gz
linux-458a3bf82df4fe1f951d0f52b1e0c1e9d5a88a3b.zip
lib/string: Add strscpy_pad() function
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>
-rw-r--r--include/linux/string.h4
-rw-r--r--lib/string.c47
2 files changed, 44 insertions, 7 deletions
diff --git a/include/linux/string.h b/include/linux/string.h
index 6ab0a6fa512e..4deb11f7976b 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
diff --git a/lib/string.c b/lib/string.c
index 3ab861c1a857..6016eb3ac73d 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -159,11 +159,9 @@ EXPORT_SYMBOL(strlcpy);
* @src: Where to copy the string from
* @count: Size of destination buffer
*
- * Copy the string, or as much of it as fits, into the dest buffer.
- * The routine returns the number of characters copied (not including
- * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough.
- * The behavior is undefined if the string buffers overlap.
- * The destination buffer is always NUL terminated, unless it's zero-sized.
+ * Copy the string, or as much of it as fits, into the dest buffer. The
+ * behavior is undefined if the string buffers overlap. The destination
+ * buffer is always NUL terminated, unless it's zero-sized.
*
* Preferred to strlcpy() since the API doesn't require reading memory
* from the src string beyond the specified "count" bytes, and since
@@ -173,8 +171,10 @@ EXPORT_SYMBOL(strlcpy);
*
* Preferred to strncpy() since it always returns a valid string, and
* doesn't unnecessarily force the tail of the destination buffer to be
- * zeroed. If the zeroing is desired, it's likely cleaner to use strscpy()
- * with an overflow test, then just memset() the tail of the dest buffer.
+ * zeroed. If zeroing is desired please use strscpy_pad().
+ *
+ * Return: The number of characters copied (not including the trailing
+ * %NUL) or -E2BIG if the destination buffer wasn't big enough.
*/
ssize_t strscpy(char *dest, const char *src, size_t count)
{
@@ -237,6 +237,39 @@ ssize_t strscpy(char *dest, const char *src, size_t count)
EXPORT_SYMBOL(strscpy);
#endif
+/**
+ * strscpy_pad() - Copy a C-string into a sized buffer
+ * @dest: Where to copy the string to
+ * @src: Where to copy the string from
+ * @count: Size of destination buffer
+ *
+ * Copy the string, or as much of it as fits, into the dest buffer. The
+ * behavior is undefined if the string buffers overlap. The destination
+ * buffer is always %NUL terminated, unless it's zero-sized.
+ *
+ * If the source string is shorter than the destination buffer, zeros
+ * the tail of the destination buffer.
+ *
+ * For full explanation of why you may want to consider using the
+ * 'strscpy' functions please see the function docstring for strscpy().
+ *
+ * Return: The number of characters copied (not including the trailing
+ * %NUL) or -E2BIG if the destination buffer wasn't big enough.
+ */
+ssize_t strscpy_pad(char *dest, const char *src, size_t count)
+{
+ ssize_t written;
+
+ written = strscpy(dest, src, count);
+ if (written < 0 || written == count - 1)
+ return written;
+
+ memset(dest + written + 1, 0, count - written - 1);
+
+ return written;
+}
+EXPORT_SYMBOL(strscpy_pad);
+
#ifndef __HAVE_ARCH_STRCAT
/**
* strcat - Append one %NUL-terminated string to another
9/+47 2023-10-25perf callchain: Minor layout changes to callchain_listIan Rogers1-8/+8 2023-10-25perf callchain: Make brtype_stat in callchain_list optionalIan Rogers2-9/+34 2023-10-25perf callchain: Make display use of branch_type_stat constIan Rogers3-7/+7 2023-10-25perf offcpu: Add missed btf_freeIan Rogers1-4/+6 2023-10-25perf threads: Remove unused dead thread listIan Rogers2-2/+0 2023-10-25perf hist: Add missing puts to hist__account_cyclesIan Rogers1-3/+7 2023-10-25libperf rc_check: Add RC_CHK_EQUALIan Rogers9-15/+21 2023-10-25libperf rc_check: Make implicit enabling work for GCCIan Rogers1-1/+5 2023-10-25perf machine: Avoid out of bounds LBR memory readIan Rogers1-10/+12 2023-10-25perf rwsem: Add debug mode that uses a mutexIan Rogers2-0/+45 2023-10-25perf build: Address stray '\' before # that is warned about since grep 3.8Arnaldo Carvalho de Melo1-1/+1 2023-10-25perf report: Fix hierarchy mode on pipe inputNamhyung Kim1-0/+15 2023-10-25perf lock contention: Use per-cpu array map for spinlocksNamhyung Kim1-17/+72 2023-10-25perf lock contention: Check race in tstamp elem creationNamhyung Kim1-1/+5 2023-10-25perf lock contention: Clear lock addr after useNamhyung Kim1-0/+4 2023-10-25perf evsel: Rename evsel__increase_rlimit to rlimit__increase_nofileYang Jihong5-34/+42 2023-10-25perf bench sched pipe: Add -G/--cgroups optionNamhyung Kim2-4/+147 2023-10-25perf test: Skip CoreSight tests if cs_etm// event is not availableMichael Petlan1-0/+2 2023-10-19perf data: Increase RLIMIT_NOFILE limit when open too many files in perf_data...Yang Jihong1-0/+12 2023-10-19perf vendor events: Update PMC used in PM_RUN_INST_CMPL event for power10 pla...Kajol Jain1-1/+1 2023-10-19perf trace: Use the right bpf_probe_read(_str) variant for reading user dataThomas Richter1-8/+8