linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tty: n_gsm: replace deprecated strncpy with strscpy
@ 2024-03-18 23:02 ` Justin Stitt
  2024-03-19  9:10   ` Maarten Brock
  2024-03-19 21:44   ` Kees Cook
  0 siblings, 2 replies; 4+ messages in thread
From: Justin Stitt @ 2024-03-18 23:02 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby
  Cc: linux-kernel, linux-serial, linux-hardening, Justin Stitt

strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

We expect nc->if_name to be NUL-terminated based on existing manual
NUL-byte assignments and checks:
|	nc.if_name[IFNAMSIZ-1] = '\0';
...
| 	if (nc->if_name[0] != '\0')

Let's use the new 2-argument strscpy() since it guarantees
NUL-termination on the destination buffer while correctly using the
destination buffers size to bound the operation.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
 drivers/tty/n_gsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 4036566febcb..f5b0d91d32a7 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -4010,7 +4010,7 @@ static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
 	mux_net = netdev_priv(net);
 	mux_net->dlci = dlci;
 	kref_init(&mux_net->ref);
-	strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
+	strscpy(nc->if_name, net->name); /* return net name */
 
 	/* reconfigure dlci for network */
 	dlci->prev_adaption = dlci->adaption;

---
base-commit: bf3a69c6861ff4dc7892d895c87074af7bc1c400
change-id: 20240318-strncpy-drivers-tty-n_gsm-c-ab1336e0e196

Best regards,
--
Justin Stitt <justinstitt@google.com>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* RE: [PATCH] tty: n_gsm: replace deprecated strncpy with strscpy
  2024-03-18 23:02 ` [PATCH] tty: n_gsm: replace deprecated strncpy with strscpy Justin Stitt
@ 2024-03-19  9:10   ` Maarten Brock
  2024-03-19 21:00     ` Justin Stitt
  2024-03-19 21:44   ` Kees Cook
  1 sibling, 1 reply; 4+ messages in thread
From: Maarten Brock @ 2024-03-19  9:10 UTC (permalink / raw)
  To: Justin Stitt, Greg Kroah-Hartman, Jiri Slaby
  Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org,
	linux-hardening@vger.kernel.org

Hi Justin,

> ---
> Note: build-tested only.

Really? Without warnings?

> --- a/drivers/tty/n_gsm.c
> +++ b/drivers/tty/n_gsm.c
> @@ -4010,7 +4010,7 @@ static int gsm_create_network(struct gsm_dlci *dlci,
> struct gsm_netconfig *nc)
>  	mux_net = netdev_priv(net);
>  	mux_net->dlci = dlci;
>  	kref_init(&mux_net->ref);
> -	strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
> +	strscpy(nc->if_name, net->name); /* return net name */

Where did IFNAMSIZ go?

Kind regards,
Maarten Brock


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tty: n_gsm: replace deprecated strncpy with strscpy
  2024-03-19  9:10   ` Maarten Brock
@ 2024-03-19 21:00     ` Justin Stitt
  0 siblings, 0 replies; 4+ messages in thread
From: Justin Stitt @ 2024-03-19 21:00 UTC (permalink / raw)
  To: Maarten Brock
  Cc: Greg Kroah-Hartman, Jiri Slaby, linux-kernel@vger.kernel.org,
	linux-serial@vger.kernel.org, linux-hardening@vger.kernel.org

Hi,

On Tue, Mar 19, 2024 at 2:11 AM Maarten Brock <Maarten.Brock@sttls.nl> wrote:
>
> Hi Justin,
>
> > ---
> > Note: build-tested only.
>
> Really? Without warnings?
>
> > --- a/drivers/tty/n_gsm.c
> > +++ b/drivers/tty/n_gsm.c
> > @@ -4010,7 +4010,7 @@ static int gsm_create_network(struct gsm_dlci *dlci,
> > struct gsm_netconfig *nc)
> >       mux_net = netdev_priv(net);
> >       mux_net->dlci = dlci;
> >       kref_init(&mux_net->ref);
> > -     strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
> > +     strscpy(nc->if_name, net->name); /* return net name */
>
> Where did IFNAMSIZ go?

There's a new 2-argument strscpy introduced in Commit e6584c3964f2f
("string: Allow 2-argument strscpy()"). Since the compiler can find
nc->if_name's size (which is == IFNAMSIZ) it should be A-OK to swap to
this new form.

>
> Kind regards,
> Maarten Brock
>

Thanks
Justin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tty: n_gsm: replace deprecated strncpy with strscpy
  2024-03-18 23:02 ` [PATCH] tty: n_gsm: replace deprecated strncpy with strscpy Justin Stitt
  2024-03-19  9:10   ` Maarten Brock
@ 2024-03-19 21:44   ` Kees Cook
  1 sibling, 0 replies; 4+ messages in thread
From: Kees Cook @ 2024-03-19 21:44 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Greg Kroah-Hartman, Jiri Slaby, linux-kernel, linux-serial,
	linux-hardening

On Mon, Mar 18, 2024 at 11:02:12PM +0000, Justin Stitt wrote:
> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.
> 
> We expect nc->if_name to be NUL-terminated based on existing manual
> NUL-byte assignments and checks:
> |	nc.if_name[IFNAMSIZ-1] = '\0';
> ...
> | 	if (nc->if_name[0] != '\0')
> 
> Let's use the new 2-argument strscpy() since it guarantees
> NUL-termination on the destination buffer while correctly using the
> destination buffers size to bound the operation.

We may need for -rc1 (or -rc2), depending on when subsystem tree re-open
for landing patches to use the 2-arg versio, but, regardless, it looks
right:

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Note: build-tested only.
> 
> Found with: $ rg "strncpy\("
> ---
>  drivers/tty/n_gsm.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
> index 4036566febcb..f5b0d91d32a7 100644
> --- a/drivers/tty/n_gsm.c
> +++ b/drivers/tty/n_gsm.c
> @@ -4010,7 +4010,7 @@ static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
>  	mux_net = netdev_priv(net);
>  	mux_net->dlci = dlci;
>  	kref_init(&mux_net->ref);
> -	strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
> +	strscpy(nc->if_name, net->name); /* return net name */
>  
>  	/* reconfigure dlci for network */
>  	dlci->prev_adaption = dlci->adaption;
> 
> ---
> base-commit: bf3a69c6861ff4dc7892d895c87074af7bc1c400
> change-id: 20240318-strncpy-drivers-tty-n_gsm-c-ab1336e0e196
> 
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
> 
> 

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-19 21:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <9bba38df-5881-43f7-b1b3-f77e7b57bbe7.6c628ed6-0c2a-48f7-ba62-bcb23589b41f.60196f95-e445-4678-a450-875badf86498@emailsignatures365.codetwo.com>
2024-03-18 23:02 ` [PATCH] tty: n_gsm: replace deprecated strncpy with strscpy Justin Stitt
2024-03-19  9:10   ` Maarten Brock
2024-03-19 21:00     ` Justin Stitt
2024-03-19 21:44   ` Kees Cook

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 NNTP newsgroup(s).