From: Geert Uytterhoeven <geert@linux-m68k.org>
To: Kees Cook <keescook@chromium.org>
Cc: Andy Shevchenko <andy@kernel.org>,
Justin Stitt <justinstitt@google.com>,
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 v2 2/4] string: Allow 2-argument strscpy()
Date: Mon, 5 Feb 2024 13:47:08 +0100 [thread overview]
Message-ID: <CAMuHMdU2c75WDCX+ptQgB7h0taHjG2pwL9db6gE3LKxv5Vz04Q@mail.gmail.com> (raw)
In-Reply-To: <20240205123525.1379299-2-keescook@chromium.org>
Hi Kees,
On Mon, Feb 5, 2024 at 1:37 PM Kees Cook <keescook@chromium.org> wrote:
> Using sizeof(dst) for the "size" argument in strscpy() is the
> overwhelmingly common case. Instead of requiring this everywhere, allow a
> 2-argument version to be used that will use the sizeof() internally. There
> are other functions in the kernel with optional arguments[1], so this
> isn't unprecedented, and improves readability. Update and relocate the
> kern-doc for strscpy() too.
>
> Adjust ARCH=um build to notice the changed export name, as it doesn't
> do full header includes for the string helpers.
>
> This could additionally let us save a few hundred lines of code:
> 1177 files changed, 2455 insertions(+), 3026 deletions(-)
> with a treewide cleanup using Coccinelle:
>
> @needless_arg@
> expression DST, SRC;
> @@
>
> strscpy(DST, SRC
> -, sizeof(DST)
> )
>
> Link: https://elixir.bootlin.com/linux/v6.7/source/include/linux/pci.h#L1517 [1]
> Reviewed-by: Justin Stitt <justinstitt@google.com>
> Cc: Andy Shevchenko <andy@kernel.org>
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>
Thanks for your patch!
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> +/*
> + * The 2 argument style can only be used when dst is an array with a
> + * known size.
> + */
> +#define __strscpy0(dst, src, ...) \
> + sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst))
> +#define __strscpy1(dst, src, size) sized_strscpy(dst, src, size)
(dst), (src), (size) etc.
> +
> +/**
> + * strscpy - Copy a C-string into a sized buffer
> + * @dst: Where to copy the string to
> + * @src: Where to copy the string from
> + * @...: Size of destination buffer (optional)
> + *
> + * Copy the source string @src, or as much of it as fits, into the
> + * destination @dst buffer. The behavior is undefined if the string
> + * buffers overlap. The destination @dst buffer is always NUL terminated,
> + * unless it's zero-sized.
> + *
> + * The size argument @... is only required when @dst is not an array, or
> + * when the copy needs to be smaller than sizeof(@dst).
> + *
> + * Preferred to strncpy() since it always returns a valid string, and
> + * doesn't unnecessarily force the tail of the destination buffer to be
> + * zero padded. If padding is desired please use strscpy_pad().
> + *
> + * Returns the number of characters copied in @dst (not including the
> + * trailing %NUL) or -E2BIG if @size is 0 or the copy from @src was
> + * truncated.
> + */
> +#define strscpy(dst, src, ...) \
> + CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
Likewise
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
next parent reply other threads:[~2024-02-05 12:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240205122916.it.909-kees@kernel.org>
[not found] ` <20240205123525.1379299-2-keescook@chromium.org>
2024-02-05 12:47 ` Geert Uytterhoeven [this message]
2024-02-05 12:58 ` [PATCH v2 2/4] string: Allow 2-argument strscpy() Andy Shevchenko
[not found] ` <202402050459.892907C59C@keescook>
2024-02-05 13:07 ` Geert Uytterhoeven
[not found] ` <20240205123525.1379299-3-keescook@chromium.org>
2024-02-05 12:48 ` [PATCH v2 3/4] string: Allow 2-argument strscpy_pad() Geert Uytterhoeven
2024-02-05 12:57 ` Andy Shevchenko
[not found] ` <20240205123525.1379299-4-keescook@chromium.org>
2024-02-05 12:50 ` [PATCH v2 4/4] um: Convert strscpy() usage to 2-argument style Geert Uytterhoeven
2024-02-05 12:57 ` Andy Shevchenko
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=CAMuHMdU2c75WDCX+ptQgB7h0taHjG2pwL9db6gE3LKxv5Vz04Q@mail.gmail.com \
--to=geert@linux-m68k.org \
--cc=andy@kernel.org \
--cc=anton.ivanov@cambridgegreys.com \
--cc=azeemshaikh38@gmail.com \
--cc=jasowang@redhat.com \
--cc=johannes@sipsolutions.net \
--cc=justinstitt@google.com \
--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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).