diff mbox series

[2/2] mm, cma: use literal printf format string

Message ID 20250224141120.1240534-2-arnd@kernel.org (mailing list archive)
State New
Headers show
Series [1/2] mm, cma: fix 32-bit warning | expand

Commit Message

Arnd Bergmann Feb. 24, 2025, 2:07 p.m. UTC
From: Arnd Bergmann <arnd@arndb.de>

Using a variable string as a printf format can be a security issue
that clang warns about when extra warnings are enabled:

mm/cma.c:239:37: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
  239 |                 snprintf(cma->name, CMA_MAX_NAME, name);
      |                                                   ^~~~

This one does not appear to be a security issue since the string is
not user controlled, but it's better to avoid the warning.
Use "%s" as the format instead and just pass the name as the argument.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Zi Yan Feb. 24, 2025, 2:27 p.m. UTC | #1
On 24 Feb 2025, at 9:07, Arnd Bergmann wrote:

> From: Arnd Bergmann <arnd@arndb.de>
>
> Using a variable string as a printf format can be a security issue
> that clang warns about when extra warnings are enabled:
>
> mm/cma.c:239:37: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
>   239 |                 snprintf(cma->name, CMA_MAX_NAME, name);
>       |                                                   ^~~~
>
> This one does not appear to be a security issue since the string is
> not user controlled, but it's better to avoid the warning.
> Use "%s" as the format instead and just pass the name as the argument.
>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  mm/cma.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
LGTM. Reviewed-by: Zi Yan <ziy@nvidia.com>

Best Regards,
Yan, Zi
David Hildenbrand Feb. 24, 2025, 2:37 p.m. UTC | #2
On 24.02.25 15:07, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> Using a variable string as a printf format can be a security issue
> that clang warns about when extra warnings are enabled:
> 
> mm/cma.c:239:37: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
>    239 |                 snprintf(cma->name, CMA_MAX_NAME, name);
>        |                                                   ^~~~
> 
> This one does not appear to be a security issue since the string is
> not user controlled, but it's better to avoid the warning.
> Use "%s" as the format instead and just pass the name as the argument.
> 
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>   mm/cma.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/cma.c b/mm/cma.c
> index ef0206c0f16d..09322b8284bd 100644
> --- a/mm/cma.c
> +++ b/mm/cma.c
> @@ -236,7 +236,7 @@ static int __init cma_new_area(const char *name, phys_addr_t size,
>   	cma_area_count++;
>   
>   	if (name)
> -		snprintf(cma->name, CMA_MAX_NAME, name);
> +		snprintf(cma->name, CMA_MAX_NAME, "%s", name);
>   	else
>   		snprintf(cma->name, CMA_MAX_NAME,  "cma%d\n", cma_area_count);
>   

Acked-by: David Hildenbrand <david@redhat.com>
Frank van der Linden Feb. 24, 2025, 5:02 p.m. UTC | #3
On Mon, Feb 24, 2025 at 6:11 AM Arnd Bergmann <arnd@kernel.org> wrote:
>
> From: Arnd Bergmann <arnd@arndb.de>
>
> Using a variable string as a printf format can be a security issue
> that clang warns about when extra warnings are enabled:
>
> mm/cma.c:239:37: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
>   239 |                 snprintf(cma->name, CMA_MAX_NAME, name);
>       |                                                   ^~~~
>
> This one does not appear to be a security issue since the string is
> not user controlled, but it's better to avoid the warning.
> Use "%s" as the format instead and just pass the name as the argument.
>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  mm/cma.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/cma.c b/mm/cma.c
> index ef0206c0f16d..09322b8284bd 100644
> --- a/mm/cma.c
> +++ b/mm/cma.c
> @@ -236,7 +236,7 @@ static int __init cma_new_area(const char *name, phys_addr_t size,
>         cma_area_count++;
>
>         if (name)
> -               snprintf(cma->name, CMA_MAX_NAME, name);
> +               snprintf(cma->name, CMA_MAX_NAME, "%s", name);
>         else
>                 snprintf(cma->name, CMA_MAX_NAME,  "cma%d\n", cma_area_count);
>
> --
> 2.39.5
>

Yes, thanks - not sure why I didn't use "%s" there.

Reviewed-by: Frank van der Linden <fvdl@google.com>
diff mbox series

Patch

diff --git a/mm/cma.c b/mm/cma.c
index ef0206c0f16d..09322b8284bd 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -236,7 +236,7 @@  static int __init cma_new_area(const char *name, phys_addr_t size,
 	cma_area_count++;
 
 	if (name)
-		snprintf(cma->name, CMA_MAX_NAME, name);
+		snprintf(cma->name, CMA_MAX_NAME, "%s", name);
 	else
 		snprintf(cma->name, CMA_MAX_NAME,  "cma%d\n", cma_area_count);