public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] um,ethertap: refactor deprecated strncpy
@ 2023-09-11 17:52 Justin Stitt
  2023-09-12  7:36 ` Geert Uytterhoeven
  2023-09-29 18:32 ` Kees Cook
  0 siblings, 2 replies; 5+ messages in thread
From: Justin Stitt @ 2023-09-11 17:52 UTC (permalink / raw)
  To: Willem de Bruijn, Jason Wang, Richard Weinberger, Anton Ivanov,
	Johannes Berg
  Cc: linux-um, linux-kernel, linux-hardening, Kees Cook, Justin Stitt

`strncpy` is deprecated for use on NUL-terminated destination strings [1].

`gate_buf` should always be NUL-terminated and does not require
NUL-padding. It is used as a string arg inside an argv array given to
`run_helper()`. Due to this, let's use `strscpy` as it guarantees
NUL-terminated on the destination buffer preventing potential buffer
overreads [2].

This exact invocation was changed from `strcpy` to `strncpy` in commit
7879b1d94badb ("um,ethertap: use strncpy") back in 2015. Let's continue
hardening our `str*cpy` apis and use the newer and safer `strscpy`!

Link: 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
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
 arch/um/os-Linux/drivers/ethertap_user.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/um/os-Linux/drivers/ethertap_user.c b/arch/um/os-Linux/drivers/ethertap_user.c
index 9483021d86dd..3363851a4ae8 100644
--- a/arch/um/os-Linux/drivers/ethertap_user.c
+++ b/arch/um/os-Linux/drivers/ethertap_user.c
@@ -105,7 +105,7 @@ static int etap_tramp(char *dev, char *gate, int control_me,
 	sprintf(data_fd_buf, "%d", data_remote);
 	sprintf(version_buf, "%d", UML_NET_VERSION);
 	if (gate != NULL) {
-		strncpy(gate_buf, gate, 15);
+		strscpy(gate_buf, gate, sizeof(gate_buf));
 		args = setup_args;
 	}
 	else args = nosetup_args;

---
base-commit: 2dde18cd1d8fac735875f2e4987f11817cc0bc2c
change-id: 20230911-strncpy-arch-um-os-linux-drivers-ethertap_user-c-859160d13f59

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


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

* Re: [PATCH] um,ethertap: refactor deprecated strncpy
  2023-09-11 17:52 [PATCH] um,ethertap: refactor deprecated strncpy Justin Stitt
@ 2023-09-12  7:36 ` Geert Uytterhoeven
  2023-09-12 20:12   ` Justin Stitt
  2023-09-29 18:32 ` Kees Cook
  1 sibling, 1 reply; 5+ messages in thread
From: Geert Uytterhoeven @ 2023-09-12  7:36 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Willem de Bruijn, Jason Wang, Richard Weinberger, Anton Ivanov,
	Johannes Berg, linux-um, linux-kernel, linux-hardening, Kees Cook

Hi Justin,

Thanks for your patch!

On Mon, Sep 11, 2023 at 7:53 PM Justin Stitt <justinstitt@google.com> wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> `gate_buf` should always be NUL-terminated and does not require
> NUL-padding. It is used as a string arg inside an argv array given to

Can you please explain why it does not require NUL-padding?
It looks like this buffer is passed eventually to a user space
application, thus possibly leaking uninitialized stack data.

> `run_helper()`. Due to this, let's use `strscpy` as it guarantees
> NUL-terminated on the destination buffer preventing potential buffer
> overreads [2].
>
> This exact invocation was changed from `strcpy` to `strncpy` in commit
> 7879b1d94badb ("um,ethertap: use strncpy") back in 2015. Let's continue
> hardening our `str*cpy` apis and use the newer and safer `strscpy`!
>
> Link: 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
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
>  arch/um/os-Linux/drivers/ethertap_user.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/um/os-Linux/drivers/ethertap_user.c b/arch/um/os-Linux/drivers/ethertap_user.c
> index 9483021d86dd..3363851a4ae8 100644
> --- a/arch/um/os-Linux/drivers/ethertap_user.c
> +++ b/arch/um/os-Linux/drivers/ethertap_user.c
> @@ -105,7 +105,7 @@ static int etap_tramp(char *dev, char *gate, int control_me,
>         sprintf(data_fd_buf, "%d", data_remote);
>         sprintf(version_buf, "%d", UML_NET_VERSION);
>         if (gate != NULL) {
> -               strncpy(gate_buf, gate, 15);
> +               strscpy(gate_buf, gate, sizeof(gate_buf));
>                 args = setup_args;
>         }
>         else args = nosetup_args;
>
> ---
> base-commit: 2dde18cd1d8fac735875f2e4987f11817cc0bc2c
> change-id: 20230911-strncpy-arch-um-os-linux-drivers-ethertap_user-c-859160d13f59

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

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

* Re: [PATCH] um,ethertap: refactor deprecated strncpy
  2023-09-12  7:36 ` Geert Uytterhoeven
@ 2023-09-12 20:12   ` Justin Stitt
  2023-09-15  3:04     ` Kees Cook
  0 siblings, 1 reply; 5+ messages in thread
From: Justin Stitt @ 2023-09-12 20:12 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Willem de Bruijn, Jason Wang, Richard Weinberger, Anton Ivanov,
	Johannes Berg, linux-um, linux-kernel, linux-hardening, Kees Cook

On Tue, Sep 12, 2023 at 12:36 AM Geert Uytterhoeven
<geert@linux-m68k.org> wrote:
>
> Hi Justin,
>
> Thanks for your patch!
>
> On Mon, Sep 11, 2023 at 7:53 PM Justin Stitt <justinstitt@google.com> wrote:
> > `strncpy` is deprecated for use on NUL-terminated destination strings [1].
> >
> > `gate_buf` should always be NUL-terminated and does not require
> > NUL-padding. It is used as a string arg inside an argv array given to
>
> Can you please explain why it does not require NUL-padding?
> It looks like this buffer is passed eventually to a user space
> application, thus possibly leaking uninitialized stack data.

It looks like it's being passed as a list of command-line arguments in
`run_helper()`. Should this be NUL-padded due to its eventual use in
user space? If we think yes I can send a v2. Thanks for pointing this
out.


>
> > `run_helper()`. Due to this, let's use `strscpy` as it guarantees
> > NUL-terminated on the destination buffer preventing potential buffer
> > overreads [2].
> >
> > This exact invocation was changed from `strcpy` to `strncpy` in commit
> > 7879b1d94badb ("um,ethertap: use strncpy") back in 2015. Let's continue
> > hardening our `str*cpy` apis and use the newer and safer `strscpy`!
> >
> > Link: 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
> > Cc: Kees Cook <keescook@chromium.org>
> > Signed-off-by: Justin Stitt <justinstitt@google.com>
> > ---
> >  arch/um/os-Linux/drivers/ethertap_user.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/arch/um/os-Linux/drivers/ethertap_user.c b/arch/um/os-Linux/drivers/ethertap_user.c
> > index 9483021d86dd..3363851a4ae8 100644
> > --- a/arch/um/os-Linux/drivers/ethertap_user.c
> > +++ b/arch/um/os-Linux/drivers/ethertap_user.c
> > @@ -105,7 +105,7 @@ static int etap_tramp(char *dev, char *gate, int control_me,
> >         sprintf(data_fd_buf, "%d", data_remote);
> >         sprintf(version_buf, "%d", UML_NET_VERSION);
> >         if (gate != NULL) {
> > -               strncpy(gate_buf, gate, 15);
> > +               strscpy(gate_buf, gate, sizeof(gate_buf));
> >                 args = setup_args;
> >         }
> >         else args = nosetup_args;
> >
> > ---
> > base-commit: 2dde18cd1d8fac735875f2e4987f11817cc0bc2c
> > change-id: 20230911-strncpy-arch-um-os-linux-drivers-ethertap_user-c-859160d13f59
>
> 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

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

* Re: [PATCH] um,ethertap: refactor deprecated strncpy
  2023-09-12 20:12   ` Justin Stitt
@ 2023-09-15  3:04     ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2023-09-15  3:04 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Geert Uytterhoeven, Willem de Bruijn, Jason Wang,
	Richard Weinberger, Anton Ivanov, Johannes Berg, linux-um,
	linux-kernel, linux-hardening

On Tue, Sep 12, 2023 at 01:12:35PM -0700, Justin Stitt wrote:
> On Tue, Sep 12, 2023 at 12:36 AM Geert Uytterhoeven
> <geert@linux-m68k.org> wrote:
> >
> > Hi Justin,
> >
> > Thanks for your patch!
> >
> > On Mon, Sep 11, 2023 at 7:53 PM Justin Stitt <justinstitt@google.com> wrote:
> > > `strncpy` is deprecated for use on NUL-terminated destination strings [1].
> > >
> > > `gate_buf` should always be NUL-terminated and does not require
> > > NUL-padding. It is used as a string arg inside an argv array given to
> >
> > Can you please explain why it does not require NUL-padding?
> > It looks like this buffer is passed eventually to a user space
> > application, thus possibly leaking uninitialized stack data.
> 
> It looks like it's being passed as a list of command-line arguments in
> `run_helper()`. Should this be NUL-padded due to its eventual use in
> user space? If we think yes I can send a v2. Thanks for pointing this
> out.

No, it's passed as a pointer to a string, and the clone call will
ultimately make a copy-until-%NUL when building the new process. This
doesn't need padding.

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

-Kees

-- 
Kees Cook

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

* Re: [PATCH] um,ethertap: refactor deprecated strncpy
  2023-09-11 17:52 [PATCH] um,ethertap: refactor deprecated strncpy Justin Stitt
  2023-09-12  7:36 ` Geert Uytterhoeven
@ 2023-09-29 18:32 ` Kees Cook
  1 sibling, 0 replies; 5+ messages in thread
From: Kees Cook @ 2023-09-29 18:32 UTC (permalink / raw)
  To: Willem de Bruijn, Jason Wang, Richard Weinberger, Anton Ivanov,
	Johannes Berg, Justin Stitt
  Cc: Kees Cook, linux-um, linux-kernel, linux-hardening

On Mon, 11 Sep 2023 17:52:44 +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
> 
> `gate_buf` should always be NUL-terminated and does not require
> NUL-padding. It is used as a string arg inside an argv array given to
> `run_helper()`. Due to this, let's use `strscpy` as it guarantees
> NUL-terminated on the destination buffer preventing potential buffer
> overreads [2].
> 
> [...]

Applied to for-next/hardening, thanks!

[1/1] um,ethertap: refactor deprecated strncpy
      https://git.kernel.org/kees/c/d4e178fe19c9

Take care,

-- 
Kees Cook


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

end of thread, other threads:[~2023-09-29 18:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-11 17:52 [PATCH] um,ethertap: refactor deprecated strncpy Justin Stitt
2023-09-12  7:36 ` Geert Uytterhoeven
2023-09-12 20:12   ` Justin Stitt
2023-09-15  3:04     ` Kees Cook
2023-09-29 18:32 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox