linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] mach-omap2: fixing wrong strcat for Non-NULL terminated string
@ 2016-12-08  4:10 Maninder Singh
  2016-12-27 17:40 ` Tony Lindgren
  0 siblings, 1 reply; 4+ messages in thread
From: Maninder Singh @ 2016-12-08  4:10 UTC (permalink / raw)
  To: bcousson, paul, tony
  Cc: linux, linux-omap, linux-arm-kernel, linux-kernel, pankaj.m,
	ajeet.y, Maninder Singh, Vaneet Narang

Issue caught with static analysis tool:
"Dangerous usage of 'name' (strncpy doesn't always 0-terminate it)"

Use strlcpy _includes_ the NUL terminator, and  strlcat() which ensures 
that it won't overflow the buffer.

Reported-by: Maninder Singh <maninder1.s@samsung.com>
Signed-off-by: Vaneet Narang <v.narang@samsung.com>
CC: Russell King <linux@armlinux.org.uk>
---
v1 -> v2: changed strncpy to strlcpy
v2 -> v3: use of strlcat as suggested by Russell to
	  make change clearer and simpler.

 arch/arm/mach-omap2/omap_hwmod.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/arm/mach-omap2/omap_hwmod.c b/arch/arm/mach-omap2/omap_hwmod.c
index 759e1d4..e8b9887 100644
--- a/arch/arm/mach-omap2/omap_hwmod.c
+++ b/arch/arm/mach-omap2/omap_hwmod.c
@@ -741,14 +741,14 @@ static int _init_main_clk(struct omap_hwmod *oh)
 	int ret = 0;
 	char name[MOD_CLK_MAX_NAME_LEN];
 	struct clk *clk;
+	static const char modck[] = "_mod_ck";
 
-	/* +7 magic comes from '_mod_ck' suffix */
-	if (strlen(oh->name) + 7 > MOD_CLK_MAX_NAME_LEN)
+	if (strlen(oh->name) >= MOD_CLK_MAX_NAME_LEN - strlen(modck))
 		pr_warn("%s: warning: cropping name for %s\n", __func__,
 			oh->name);
 
-	strncpy(name, oh->name, MOD_CLK_MAX_NAME_LEN - 7);
-	strcat(name, "_mod_ck");
+	strlcpy(name, oh->name, MOD_CLK_MAX_NAME_LEN - strlen(modck));
+	strlcat(name, modck, MOD_CLK_MAX_NAME_LEN);
 
 	clk = clk_get(NULL, name);
 	if (!IS_ERR(clk)) {
-- 
1.9.1

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

* Re: [PATCH v3] mach-omap2: fixing wrong strcat for Non-NULL terminated string
  2016-12-08  4:10 [PATCH v3] mach-omap2: fixing wrong strcat for Non-NULL terminated string Maninder Singh
@ 2016-12-27 17:40 ` Tony Lindgren
  0 siblings, 0 replies; 4+ messages in thread
From: Tony Lindgren @ 2016-12-27 17:40 UTC (permalink / raw)
  To: Maninder Singh
  Cc: bcousson, paul, linux, linux-omap, linux-arm-kernel,
	linux-kernel, pankaj.m, ajeet.y, Vaneet Narang

* Maninder Singh <maninder1.s@samsung.com> [161207 20:16]:
> Issue caught with static analysis tool:
> "Dangerous usage of 'name' (strncpy doesn't always 0-terminate it)"
> 
> Use strlcpy _includes_ the NUL terminator, and  strlcat() which ensures 
> that it won't overflow the buffer.

Applying into omap-for-v4.10/fixes thanks.

Tony

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

* Re: [PATCH v3] mach-omap2: fixing wrong strcat for Non-NULL terminated string
  2016-12-05  5:27 Maninder Singh
@ 2016-12-06 17:08 ` Tony Lindgren
  0 siblings, 0 replies; 4+ messages in thread
From: Tony Lindgren @ 2016-12-06 17:08 UTC (permalink / raw)
  To: Maninder Singh
  Cc: bcousson, paul, linux, linux-omap, linux-arm-kernel,
	linux-kernel, pankaj.m, ajeet.y, Vaneet Narang

* Maninder Singh <maninder1.s@samsung.com> [161204 21:32]:
> Issue caught with static analysis tool:
> "Dangerous usage of 'name' (strncpy doesn't always 0-terminate it)"
> 
> Use strlcpy _includes_ the NUL terminator, and  strlcat() which ensures 
> that it won't overflow the buffer.
> 
> Reported-by: Maninder Singh <maninder1.s@samsung.com>
> Signed-off-by: Vaneet Narang <v.narang@samsung.com>
> Signed-off-by: Russell King <linux@armlinux.org.uk>

I think the above should have been just:

Cc: Russell King <linux@armlinux.org.uk>

Can you please check and resend with Russell in Cc?

Regards,

Tony

> ---
> v1 -> v2: changed strncpy to strlcpy
> v2 -> v3: use of strlcat as suggested by Russell to
> 	  make change clearer and simpler.
> 
>  arch/arm/mach-omap2/omap_hwmod.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/arm/mach-omap2/omap_hwmod.c b/arch/arm/mach-omap2/omap_hwmod.c
> index 759e1d4..e8b9887 100644
> --- a/arch/arm/mach-omap2/omap_hwmod.c
> +++ b/arch/arm/mach-omap2/omap_hwmod.c
> @@ -741,14 +741,14 @@ static int _init_main_clk(struct omap_hwmod *oh)
>  	int ret = 0;
>  	char name[MOD_CLK_MAX_NAME_LEN];
>  	struct clk *clk;
> +	static const char modck[] = "_mod_ck";
>  
> -	/* +7 magic comes from '_mod_ck' suffix */
> -	if (strlen(oh->name) + 7 > MOD_CLK_MAX_NAME_LEN)
> +	if (strlen(oh->name) >= MOD_CLK_MAX_NAME_LEN - strlen(modck))
>  		pr_warn("%s: warning: cropping name for %s\n", __func__,
>  			oh->name);
>  
> -	strncpy(name, oh->name, MOD_CLK_MAX_NAME_LEN - 7);
> -	strcat(name, "_mod_ck");
> +	strlcpy(name, oh->name, MOD_CLK_MAX_NAME_LEN - strlen(modck));
> +	strlcat(name, modck, MOD_CLK_MAX_NAME_LEN);
>  
>  	clk = clk_get(NULL, name);
>  	if (!IS_ERR(clk)) {
> -- 
> 1.9.1
> 

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

* [PATCH v3] mach-omap2: fixing wrong strcat for Non-NULL terminated string
@ 2016-12-05  5:27 Maninder Singh
  2016-12-06 17:08 ` Tony Lindgren
  0 siblings, 1 reply; 4+ messages in thread
From: Maninder Singh @ 2016-12-05  5:27 UTC (permalink / raw)
  To: bcousson, paul, tony, linux
  Cc: linux-omap, linux-arm-kernel, linux-kernel, pankaj.m, ajeet.y,
	Maninder Singh, Vaneet Narang

Issue caught with static analysis tool:
"Dangerous usage of 'name' (strncpy doesn't always 0-terminate it)"

Use strlcpy _includes_ the NUL terminator, and  strlcat() which ensures 
that it won't overflow the buffer.

Reported-by: Maninder Singh <maninder1.s@samsung.com>
Signed-off-by: Vaneet Narang <v.narang@samsung.com>
Signed-off-by: Russell King <linux@armlinux.org.uk>
---
v1 -> v2: changed strncpy to strlcpy
v2 -> v3: use of strlcat as suggested by Russell to
	  make change clearer and simpler.

 arch/arm/mach-omap2/omap_hwmod.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/arm/mach-omap2/omap_hwmod.c b/arch/arm/mach-omap2/omap_hwmod.c
index 759e1d4..e8b9887 100644
--- a/arch/arm/mach-omap2/omap_hwmod.c
+++ b/arch/arm/mach-omap2/omap_hwmod.c
@@ -741,14 +741,14 @@ static int _init_main_clk(struct omap_hwmod *oh)
 	int ret = 0;
 	char name[MOD_CLK_MAX_NAME_LEN];
 	struct clk *clk;
+	static const char modck[] = "_mod_ck";
 
-	/* +7 magic comes from '_mod_ck' suffix */
-	if (strlen(oh->name) + 7 > MOD_CLK_MAX_NAME_LEN)
+	if (strlen(oh->name) >= MOD_CLK_MAX_NAME_LEN - strlen(modck))
 		pr_warn("%s: warning: cropping name for %s\n", __func__,
 			oh->name);
 
-	strncpy(name, oh->name, MOD_CLK_MAX_NAME_LEN - 7);
-	strcat(name, "_mod_ck");
+	strlcpy(name, oh->name, MOD_CLK_MAX_NAME_LEN - strlen(modck));
+	strlcat(name, modck, MOD_CLK_MAX_NAME_LEN);
 
 	clk = clk_get(NULL, name);
 	if (!IS_ERR(clk)) {
-- 
1.9.1

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

end of thread, other threads:[~2016-12-27 17:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-08  4:10 [PATCH v3] mach-omap2: fixing wrong strcat for Non-NULL terminated string Maninder Singh
2016-12-27 17:40 ` Tony Lindgren
  -- strict thread matches above, loose matches on Subject: below --
2016-12-05  5:27 Maninder Singh
2016-12-06 17:08 ` Tony Lindgren

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