2023-09-14 19:20:22

by Justin Stitt

[permalink] [raw]
Subject: [PATCH] drm/etnaviv: refactor deprecated strncpy

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

We should prefer more robust and less ambiguous string interfaces.

A suitable replacement is `strscpy` [2] due to the fact that it guarantees
NUL-termination on the destination buffer.

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: [email protected]
Cc: Bo YU <[email protected]>
Signed-off-by: Justin Stitt <[email protected]>
---
Similar to [1] which was never picked up. Let's prefer strscpy to strlcpy, though

[1]: https://lore.kernel.org/all/[email protected]/
---
drivers/gpu/drm/etnaviv/etnaviv_perfmon.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/etnaviv/etnaviv_perfmon.c b/drivers/gpu/drm/etnaviv/etnaviv_perfmon.c
index bafdfe49c1d8..9e7bebcf24eb 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_perfmon.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_perfmon.c
@@ -511,7 +511,7 @@ int etnaviv_pm_query_dom(struct etnaviv_gpu *gpu,

domain->id = domain->iter;
domain->nr_signals = dom->nr_signals;
- strncpy(domain->name, dom->name, sizeof(domain->name));
+ strscpy(domain->name, dom->name, sizeof(domain->name));

domain->iter++;
if (domain->iter == nr_domains)
@@ -540,7 +540,7 @@ int etnaviv_pm_query_sig(struct etnaviv_gpu *gpu,
sig = &dom->signal[signal->iter];

signal->id = signal->iter;
- strncpy(signal->name, sig->name, sizeof(signal->name));
+ strscpy(signal->name, sig->name, sizeof(signal->name));

signal->iter++;
if (signal->iter == dom->nr_signals)

---
base-commit: 3669558bdf354cd352be955ef2764cde6a9bf5ec
change-id: 20230914-strncpy-drivers-gpu-drm-etnaviv-etnaviv_perfmon-c-dd095491dfde

Best regards,
--
Justin Stitt <[email protected]>


2023-09-16 05:43:34

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] drm/etnaviv: refactor deprecated strncpy

On Thu, Sep 14, 2023 at 06:20:01PM +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> We should prefer more robust and less ambiguous string interfaces.
>
> A suitable replacement is `strscpy` [2] due to the fact that it guarantees
> NUL-termination on the destination buffer.
>
> 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: [email protected]
> Cc: Bo YU <[email protected]>
> Signed-off-by: Justin Stitt <[email protected]>
> ---
> Similar to [1] which was never picked up. Let's prefer strscpy to strlcpy, though
>
> [1]: https://lore.kernel.org/all/[email protected]/
> ---
> drivers/gpu/drm/etnaviv/etnaviv_perfmon.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/etnaviv/etnaviv_perfmon.c b/drivers/gpu/drm/etnaviv/etnaviv_perfmon.c
> index bafdfe49c1d8..9e7bebcf24eb 100644
> --- a/drivers/gpu/drm/etnaviv/etnaviv_perfmon.c
> +++ b/drivers/gpu/drm/etnaviv/etnaviv_perfmon.c
> @@ -511,7 +511,7 @@ int etnaviv_pm_query_dom(struct etnaviv_gpu *gpu,
>
> domain->id = domain->iter;
> domain->nr_signals = dom->nr_signals;
> - strncpy(domain->name, dom->name, sizeof(domain->name));
> + strscpy(domain->name, dom->name, sizeof(domain->name));

It looks like this is passing through an ioctl(). Let's use
strscpy_pad(0 instead...

-Kees

>
> domain->iter++;
> if (domain->iter == nr_domains)
> @@ -540,7 +540,7 @@ int etnaviv_pm_query_sig(struct etnaviv_gpu *gpu,
> sig = &dom->signal[signal->iter];
>
> signal->id = signal->iter;
> - strncpy(signal->name, sig->name, sizeof(signal->name));
> + strscpy(signal->name, sig->name, sizeof(signal->name));
>
> signal->iter++;
> if (signal->iter == dom->nr_signals)
>
> ---
> base-commit: 3669558bdf354cd352be955ef2764cde6a9bf5ec
> change-id: 20230914-strncpy-drivers-gpu-drm-etnaviv-etnaviv_perfmon-c-dd095491dfde
>
> Best regards,
> --
> Justin Stitt <[email protected]>
>

--
Kees Cook