From: Justin Stitt <justinstitt@google.com>
To: Kees Cook <keescook@chromium.org>
Cc: Andy Shevchenko <andy@kernel.org>,
linux-hardening@vger.kernel.org,
Richard Weinberger <richard@nod.at>,
Anton Ivanov <anton.ivanov@cambridgegreys.com>,
Johannes Berg <johannes@sipsolutions.net>,
Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
Jason Wang <jasowang@redhat.com>,
kernel test robot <lkp@intel.com>,
Nathan Chancellor <nathan@kernel.org>,
Azeem Shaikh <azeemshaikh38@gmail.com>,
linux-kernel@vger.kernel.org, linux-um@lists.infradead.org
Subject: Re: [PATCH v3 1/4] string: Redefine strscpy_pad() as a macro
Date: Wed, 7 Feb 2024 00:32:24 +0000 [thread overview]
Message-ID: <20240207003224.gzatchm47gy3aihv@google.com> (raw)
In-Reply-To: <20240206142221.2208763-1-keescook@chromium.org>
Hi,
On Tue, Feb 06, 2024 at 06:22:16AM -0800, Kees Cook wrote:
> In preparation for making strscpy_pad()'s 3rd argument optional, redefine
> it as a macro. This also has the benefit of allowing greater FORITFY
> introspection, as it couldn't see into the strscpy() nor the memset()
> within strscpy_pad().
>
> Cc: Andy Shevchenko <andy@kernel.org>
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> include/linux/string.h | 33 +++++++++++++++++++++++++++++++--
> lib/string_helpers.c | 34 ----------------------------------
> 2 files changed, 31 insertions(+), 36 deletions(-)
>
> diff --git a/include/linux/string.h b/include/linux/string.h
> index ab148d8dbfc1..03f59cf7fe72 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -70,8 +70,37 @@ extern char * strncpy(char *,const char *, __kernel_size_t);
> 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);
> +/**
> + * 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().
> + *
> + * Returns:
> + * * The number of characters copied (not including the trailing %NULs)
> + * * -E2BIG if count is 0 or @src was truncated.
> + */
> +#define strscpy_pad(dest, src, count) ({ \
> + char *__dst = (dest); \
> + const char *__src = (src); \
> + const size_t __count = (count); \
> + ssize_t __wrote; \
> + \
> + __wrote = strscpy(__dst, __src, __count); \
> + if (__wrote >= 0 && __wrote < __count) \
> + memset(__dst + __wrote + 1, 0, __count - __wrote - 1); \
> + __wrote; \
> +})
>
> #ifndef __HAVE_ARCH_STRCAT
> extern char * strcat(char *, const char *);
> diff --git a/lib/string_helpers.c b/lib/string_helpers.c
> index 7713f73e66b0..606c3099013f 100644
> --- a/lib/string_helpers.c
> +++ b/lib/string_helpers.c
> @@ -825,40 +825,6 @@ char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n)
> }
> EXPORT_SYMBOL_GPL(devm_kasprintf_strarray);
>
> -/**
> - * 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().
> - *
> - * Returns:
> - * * The number of characters copied (not including the trailing %NUL)
> - * * -E2BIG if count is 0 or @src was truncated.
> - */
> -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);
Yep, looks good. This is reminiscent of strtomem and strtomem_pad.
Reviewed-by: Justin Stitt <justinstitt@google.com>
> -
> /**
> * skip_spaces - Removes leading whitespace from @str.
> * @str: The string to be stripped.
> --
> 2.34.1
>
Thanks
Justin
next prev parent reply other threads:[~2024-02-07 0:32 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-06 14:22 [PATCH v3 0/4] string: Allow 2-argument strscpy() Kees Cook
2024-02-06 14:22 ` [PATCH v3 1/4] string: Redefine strscpy_pad() as a macro Kees Cook
2024-02-07 0:32 ` Justin Stitt [this message]
2024-02-06 14:22 ` [PATCH v3 2/4] string: Allow 2-argument strscpy() Kees Cook
2024-02-06 14:22 ` [PATCH v3 3/4] string: Allow 2-argument strscpy_pad() Kees Cook
2024-02-07 0:51 ` Justin Stitt
2024-02-07 9:18 ` Kees Cook
2024-02-10 12:34 ` David Laight
2024-02-06 14:22 ` [PATCH v3 4/4] um: Convert strscpy() usage to 2-argument style Kees Cook
2024-02-06 15:02 ` Andy Shevchenko
2024-02-07 10:42 ` Removing more str APIs (was Re: [PATCH v3 4/4] um: Convert strscpy() usage to 2-argument style) Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240207003224.gzatchm47gy3aihv@google.com \
--to=justinstitt@google.com \
--cc=andy@kernel.org \
--cc=anton.ivanov@cambridgegreys.com \
--cc=azeemshaikh38@gmail.com \
--cc=jasowang@redhat.com \
--cc=johannes@sipsolutions.net \
--cc=keescook@chromium.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-um@lists.infradead.org \
--cc=lkp@intel.com \
--cc=nathan@kernel.org \
--cc=richard@nod.at \
--cc=willemdebruijn.kernel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox