linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] leds: lp3952: replace deprecated strncpy with strscpy
@ 2023-09-22 15:27 Justin Stitt
  2023-09-24  3:43 ` Kees Cook
  2023-09-28 13:41 ` (subset) " Lee Jones
  0 siblings, 2 replies; 4+ messages in thread
From: Justin Stitt @ 2023-09-22 15:27 UTC (permalink / raw)
  To: Pavel Machek, Lee Jones
  Cc: linux-leds, linux-kernel, 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 `dest` to be NUL-terminated due to its use with dev_err.

lp3952_get_label()'s  dest argument is priv->leds[i].name:
|    acpi_ret = lp3952_get_label(&priv->client->dev, led_name_hdl[i],
|                                priv->leds[i].name);
... which is then assigned to:
|    priv->leds[i].cdev.name = priv->leds[i].name;
... which is used with a format string
|    dev_err(&priv->client->dev,
|            "couldn't register LED %s\n",
|            priv->leds[i].cdev.name);

There is no indication that NUL-padding is required but if it is let's
opt for strscpy_pad.

Considering the above, a suitable replacement is `strscpy` [2] due to
the fact that it guarantees NUL-termination on the destination buffer
without unnecessarily NUL-padding.

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.
---
 drivers/leds/leds-lp3952.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/leds/leds-lp3952.c b/drivers/leds/leds-lp3952.c
index 3bd55652a706..62ade3f05a87 100644
--- a/drivers/leds/leds-lp3952.c
+++ b/drivers/leds/leds-lp3952.c
@@ -101,7 +101,7 @@ static int lp3952_get_label(struct device *dev, const char *label, char *dest)
 	if (ret)
 		return ret;
 
-	strncpy(dest, str, LP3952_LABEL_MAX_LEN);
+	strscpy(dest, str, LP3952_LABEL_MAX_LEN);
 	return 0;
 }
 

---
base-commit: 2cf0f715623872823a72e451243bbf555d10d032
change-id: 20230922-strncpy-drivers-leds-leds-lp3952-c-666fcfabeebd

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


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

* Re: [PATCH] leds: lp3952: replace deprecated strncpy with strscpy
  2023-09-22 15:27 [PATCH] leds: lp3952: replace deprecated strncpy with strscpy Justin Stitt
@ 2023-09-24  3:43 ` Kees Cook
  2023-09-28 13:41   ` Lee Jones
  2023-09-28 13:41 ` (subset) " Lee Jones
  1 sibling, 1 reply; 4+ messages in thread
From: Kees Cook @ 2023-09-24  3:43 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Pavel Machek, Lee Jones, linux-leds, linux-kernel,
	linux-hardening

On Fri, Sep 22, 2023 at 03:27:17PM +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 `dest` to be NUL-terminated due to its use with dev_err.
> 
> lp3952_get_label()'s  dest argument is priv->leds[i].name:
> |    acpi_ret = lp3952_get_label(&priv->client->dev, led_name_hdl[i],
> |                                priv->leds[i].name);
> ... which is then assigned to:
> |    priv->leds[i].cdev.name = priv->leds[i].name;
> ... which is used with a format string
> |    dev_err(&priv->client->dev,
> |            "couldn't register LED %s\n",
> |            priv->leds[i].cdev.name);
> 
> There is no indication that NUL-padding is required but if it is let's
> opt for strscpy_pad.
> 
> Considering the above, a suitable replacement is `strscpy` [2] due to
> the fact that it guarantees NUL-termination on the destination buffer
> without unnecessarily NUL-padding.
> 
> 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.
> ---
>  drivers/leds/leds-lp3952.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/leds/leds-lp3952.c b/drivers/leds/leds-lp3952.c
> index 3bd55652a706..62ade3f05a87 100644
> --- a/drivers/leds/leds-lp3952.c
> +++ b/drivers/leds/leds-lp3952.c
> @@ -101,7 +101,7 @@ static int lp3952_get_label(struct device *dev, const char *label, char *dest)
>  	if (ret)
>  		return ret;
>  
> -	strncpy(dest, str, LP3952_LABEL_MAX_LEN);
> +	strscpy(dest, str, LP3952_LABEL_MAX_LEN);

Given my desire to use sizeof(dest) for these things, I wonder if it'd
be nicer to pass more context here for the compiler as the only user of
this function is the immediately next function. Instead of passing in
"char *dest", it could pass "struct lp3952_led_array *priv", and
suddenly sizeof() would be possible.

But, since it's technically correct as-is:

struct lp3952_ctrl_hdl {
        struct led_classdev cdev;
        char name[LP3952_LABEL_MAX_LEN];

There's no pressing need to actually do the priv refactor. It's just a
comment on the coding style of the original code. :)

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

-Kees

>  	return 0;
>  }
>  
> 
> ---
> base-commit: 2cf0f715623872823a72e451243bbf555d10d032
> change-id: 20230922-strncpy-drivers-leds-leds-lp3952-c-666fcfabeebd
> 
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
> 

-- 
Kees Cook

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

* Re: (subset) [PATCH] leds: lp3952: replace deprecated strncpy with strscpy
  2023-09-22 15:27 [PATCH] leds: lp3952: replace deprecated strncpy with strscpy Justin Stitt
  2023-09-24  3:43 ` Kees Cook
@ 2023-09-28 13:41 ` Lee Jones
  1 sibling, 0 replies; 4+ messages in thread
From: Lee Jones @ 2023-09-28 13:41 UTC (permalink / raw)
  To: Pavel Machek, Lee Jones, Justin Stitt
  Cc: linux-leds, linux-kernel, linux-hardening

On Fri, 22 Sep 2023 15:27:17 +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 `dest` to be NUL-terminated due to its use with dev_err.
> 
> lp3952_get_label()'s  dest argument is priv->leds[i].name:
> |    acpi_ret = lp3952_get_label(&priv->client->dev, led_name_hdl[i],
> |                                priv->leds[i].name);
> ... which is then assigned to:
> |    priv->leds[i].cdev.name = priv->leds[i].name;
> ... which is used with a format string
> |    dev_err(&priv->client->dev,
> |            "couldn't register LED %s\n",
> |            priv->leds[i].cdev.name);
> 
> [...]

Applied, thanks!

[1/1] leds: lp3952: replace deprecated strncpy with strscpy
      commit: 821d3ff4b4e2c689576a623348555114e3f2f1c2

--
Lee Jones [李琼斯]


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

* Re: [PATCH] leds: lp3952: replace deprecated strncpy with strscpy
  2023-09-24  3:43 ` Kees Cook
@ 2023-09-28 13:41   ` Lee Jones
  0 siblings, 0 replies; 4+ messages in thread
From: Lee Jones @ 2023-09-28 13:41 UTC (permalink / raw)
  To: Kees Cook
  Cc: Justin Stitt, Pavel Machek, linux-leds, linux-kernel,
	linux-hardening

On Sat, 23 Sep 2023, Kees Cook wrote:

> On Fri, Sep 22, 2023 at 03:27:17PM +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 `dest` to be NUL-terminated due to its use with dev_err.
> > 
> > lp3952_get_label()'s  dest argument is priv->leds[i].name:
> > |    acpi_ret = lp3952_get_label(&priv->client->dev, led_name_hdl[i],
> > |                                priv->leds[i].name);
> > ... which is then assigned to:
> > |    priv->leds[i].cdev.name = priv->leds[i].name;
> > ... which is used with a format string
> > |    dev_err(&priv->client->dev,
> > |            "couldn't register LED %s\n",
> > |            priv->leds[i].cdev.name);
> > 
> > There is no indication that NUL-padding is required but if it is let's
> > opt for strscpy_pad.
> > 
> > Considering the above, a suitable replacement is `strscpy` [2] due to
> > the fact that it guarantees NUL-termination on the destination buffer
> > without unnecessarily NUL-padding.
> > 
> > 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.
> > ---
> >  drivers/leds/leds-lp3952.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/leds/leds-lp3952.c b/drivers/leds/leds-lp3952.c
> > index 3bd55652a706..62ade3f05a87 100644
> > --- a/drivers/leds/leds-lp3952.c
> > +++ b/drivers/leds/leds-lp3952.c
> > @@ -101,7 +101,7 @@ static int lp3952_get_label(struct device *dev, const char *label, char *dest)
> >  	if (ret)
> >  		return ret;
> >  
> > -	strncpy(dest, str, LP3952_LABEL_MAX_LEN);
> > +	strscpy(dest, str, LP3952_LABEL_MAX_LEN);
> 
> Given my desire to use sizeof(dest) for these things, I wonder if it'd
> be nicer to pass more context here for the compiler as the only user of
> this function is the immediately next function. Instead of passing in
> "char *dest", it could pass "struct lp3952_led_array *priv", and
> suddenly sizeof() would be possible.
> 
> But, since it's technically correct as-is:
> 
> struct lp3952_ctrl_hdl {
>         struct led_classdev cdev;
>         char name[LP3952_LABEL_MAX_LEN];
> 
> There's no pressing need to actually do the priv refactor. It's just a
> comment on the coding style of the original code. :)
> 
> Reviewed-by: Kees Cook <keescook@chromium.org>

I've applied this as-is, but please consider Kees' proposal.

-- 
Lee Jones [李琼斯]

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

end of thread, other threads:[~2023-09-28 13:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-22 15:27 [PATCH] leds: lp3952: replace deprecated strncpy with strscpy Justin Stitt
2023-09-24  3:43 ` Kees Cook
2023-09-28 13:41   ` Lee Jones
2023-09-28 13:41 ` (subset) " Lee Jones

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).