linux-um.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Anton Ivanov <anton.ivanov@cambridgegreys.com>
To: Justin Stitt <justinstitt@google.com>,
	Richard Weinberger <richard@nod.at>,
	Johannes Berg <johannes@sipsolutions.net>
Cc: linux-um@lists.infradead.org, linux-kernel@vger.kernel.org,
	Kees Cook <keescook@chromium.org>,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH] um: vector: refactor deprecated strncpy
Date: Tue, 8 Aug 2023 07:04:29 +0100	[thread overview]
Message-ID: <0bdda692-25a3-227c-69ef-341f037e1dd7@cambridgegreys.com> (raw)
In-Reply-To: <20230807-arch-um-drivers-v1-1-10d602c5577a@google.com>



On 07/08/2023 19:22, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
> 
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on its destination buffer argument which is
> _not_ the case for `strncpy`!
> 
> In this case, we are able to drop the now superfluous `... - 1`
> instances because `strscpy` will automatically truncate the last byte by
> setting it to a NUL byte if the source size exceeds the destination size
> or if the source string is not NUL-terminated.
> 
> I've also opted to remove the seemingly useless char* casts. I'm not
> sure why they're present at all since (after expanding the `ifr_name`
> macro) `ifr.ifr_ifrn.ifrn_name` is a char* already.
> 
> All in all, `strscpy` is a more robust and less ambiguous interface
> while also letting us remove some `... -1`'s which cleans things up a
> bit.
> 
> [1]: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> [2]: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
> 
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
>   arch/um/drivers/vector_user.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/um/drivers/vector_user.c b/arch/um/drivers/vector_user.c
> index c650e428432b..c719e1ec4645 100644
> --- a/arch/um/drivers/vector_user.c
> +++ b/arch/um/drivers/vector_user.c
> @@ -141,7 +141,7 @@ static int create_tap_fd(char *iface)
>   	}
>   	memset(&ifr, 0, sizeof(ifr));
>   	ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
> -	strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
> +	strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
>   
>   	err = ioctl(fd, TUNSETIFF, (void *) &ifr);
>   	if (err != 0) {
> @@ -171,7 +171,7 @@ static int create_raw_fd(char *iface, int flags, int proto)
>   		goto raw_fd_cleanup;
>   	}
>   	memset(&ifr, 0, sizeof(ifr));
> -	strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
> +	strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
>   	if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) {
>   		err = -errno;
>   		goto raw_fd_cleanup;
> 
> ---
> base-commit: c1a515d3c0270628df8ae5f5118ba859b85464a2
> change-id: 20230807-arch-um-drivers-ad44050885d0
> 
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
> 
> 
> _______________________________________________
> linux-um mailing list
> linux-um@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-um
> 
Acked-by: Anton Ivanov <anton.ivanov@cambridgegreys.com>

-- 
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
https://www.cambridgegreys.com/

_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um

      reply	other threads:[~2023-08-08  6:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-07 18:22 [PATCH] um: vector: refactor deprecated strncpy Justin Stitt
2023-08-08  6:04 ` Anton Ivanov [this message]

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=0bdda692-25a3-227c-69ef-341f037e1dd7@cambridgegreys.com \
    --to=anton.ivanov@cambridgegreys.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=richard@nod.at \
    /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).