linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] powerpc/pseries: Correct string length in pseries_of_derive_parent()
@ 2015-10-26 19:33 Nathan Fontenot
  2015-10-27  8:17 ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Fontenot @ 2015-10-26 19:33 UTC (permalink / raw)
  To: linuxppc-dev@lists.ozlabs.org; +Cc: andriy.shevchenko

Commit a030e1e4bbd085bbcfd0a23f8d355fcd41f39bed made a change to use
kstrndup() instead of kmalloc() + strlcpy() in pseries_of_derive_parent()
which introduces a subtle change in the parent path name generated.
The kstrndup() routine will copy n characters followed by a terminating null,
whereas strlcpy() will copy n-1 characters and add a terminating null.

This slight difference results in having a parent path that includes the
trailing '/' character, i.e. "/cpus/" vs. "/cpus". This then causes the
subsequent call to of_find_node_by_path() to fail, and in the case of
DLPAR add operations, the DLPAR request fails.

This patch reduces the total length of the string to copy in kstrndup by 1
so we no longer copy the trailing '/'.

Signed-off-by: Nathan Fontenot <nfont@linux.vnet.ibm.com>
---
 arch/powerpc/platforms/pseries/of_helpers.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/platforms/pseries/of_helpers.c b/arch/powerpc/platforms/pseries/of_helpers.c
index 4417afe..6d90378 100644
--- a/arch/powerpc/platforms/pseries/of_helpers.c
+++ b/arch/powerpc/platforms/pseries/of_helpers.c
@@ -24,7 +24,7 @@ struct device_node *pseries_of_derive_parent(const char *path)
 		return ERR_PTR(-EINVAL);
 
 	if (tail > path + 1) {
-		parent_path = kstrndup(path, tail - path, GFP_KERNEL);
+		parent_path = kstrndup(path, (tail - 1) - path, GFP_KERNEL);
 		if (!parent_path)
 			return ERR_PTR(-ENOMEM);
 	}

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

* Re: [PATCH] powerpc/pseries: Correct string length in pseries_of_derive_parent()
  2015-10-26 19:33 [PATCH] powerpc/pseries: Correct string length in pseries_of_derive_parent() Nathan Fontenot
@ 2015-10-27  8:17 ` Andy Shevchenko
  2015-10-27 13:51   ` Nathan Fontenot
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2015-10-27  8:17 UTC (permalink / raw)
  To: Nathan Fontenot, linuxppc-dev@lists.ozlabs.org

On Mon, 2015-10-26 at 14:33 -0500, Nathan Fontenot wrote:
> Commit a030e1e4bbd085bbcfd0a23f8d355fcd41f39bed made a change to use
> kstrndup() instead of kmalloc() + strlcpy() in
> pseries_of_derive_parent()
> which introduces a subtle change in the parent path name generated.
> The kstrndup() routine will copy n characters followed by a
> terminating null,
> whereas strlcpy() will copy n-1 characters and add a terminating
> null.

Nice catch!
One comment below.

> 
> This slight difference results in having a parent path that includes
> the
> trailing '/' character, i.e. "/cpus/" vs. "/cpus". This then causes
> the
> subsequent call to of_find_node_by_path() to fail, and in the case of
> DLPAR add operations, the DLPAR request fails.
> 
> This patch reduces the total length of the string to copy in kstrndup
> by 1
> so we no longer copy the trailing '/'.
> 
> Signed-off-by: Nathan Fontenot <nfont@linux.vnet.ibm.com>
> ---
>  arch/powerpc/platforms/pseries/of_helpers.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/powerpc/platforms/pseries/of_helpers.c
> b/arch/powerpc/platforms/pseries/of_helpers.c
> index 4417afe..6d90378 100644
> --- a/arch/powerpc/platforms/pseries/of_helpers.c
> +++ b/arch/powerpc/platforms/pseries/of_helpers.c
> @@ -24,7 +24,7 @@ struct device_node *pseries_of_derive_parent(const
> char *path)
>  		return ERR_PTR(-EINVAL);
>  
>  	if (tail > path + 1) {
> -		parent_path = kstrndup(path, tail - path,
> GFP_KERNEL);
> +		parent_path = kstrndup(path, (tail - 1) - path,
> GFP_KERNEL);

Since  previous line has (tail > path + 1) which is equivalent to (tail
- 1 > path), can we amend both?

For example (might be better, but first comes to my mind)

const char *tail = kbasename(path) - 1;

...

if (tail > path) {


>  		if (!parent_path)
>  			return ERR_PTR(-ENOMEM);
>  	}
> 

-- 
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Intel Finland Oy

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

* Re: [PATCH] powerpc/pseries: Correct string length in pseries_of_derive_parent()
  2015-10-27  8:17 ` Andy Shevchenko
@ 2015-10-27 13:51   ` Nathan Fontenot
  0 siblings, 0 replies; 3+ messages in thread
From: Nathan Fontenot @ 2015-10-27 13:51 UTC (permalink / raw)
  To: Andy Shevchenko, linuxppc-dev@lists.ozlabs.org

On 10/27/2015 03:17 AM, Andy Shevchenko wrote:
> On Mon, 2015-10-26 at 14:33 -0500, Nathan Fontenot wrote:
>> Commit a030e1e4bbd085bbcfd0a23f8d355fcd41f39bed made a change to use
>> kstrndup() instead of kmalloc() + strlcpy() in
>> pseries_of_derive_parent()
>> which introduces a subtle change in the parent path name generated.
>> The kstrndup() routine will copy n characters followed by a
>> terminating null,
>> whereas strlcpy() will copy n-1 characters and add a terminating
>> null.
> 
> Nice catch!
> One comment below.
> 
>>
>> This slight difference results in having a parent path that includes
>> the
>> trailing '/' character, i.e. "/cpus/" vs. "/cpus". This then causes
>> the
>> subsequent call to of_find_node_by_path() to fail, and in the case of
>> DLPAR add operations, the DLPAR request fails.
>>
>> This patch reduces the total length of the string to copy in kstrndup
>> by 1
>> so we no longer copy the trailing '/'.
>>
>> Signed-off-by: Nathan Fontenot <nfont@linux.vnet.ibm.com>
>> ---
>>  arch/powerpc/platforms/pseries/of_helpers.c |    2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/arch/powerpc/platforms/pseries/of_helpers.c
>> b/arch/powerpc/platforms/pseries/of_helpers.c
>> index 4417afe..6d90378 100644
>> --- a/arch/powerpc/platforms/pseries/of_helpers.c
>> +++ b/arch/powerpc/platforms/pseries/of_helpers.c
>> @@ -24,7 +24,7 @@ struct device_node *pseries_of_derive_parent(const
>> char *path)
>>  		return ERR_PTR(-EINVAL);
>>  
>>  	if (tail > path + 1) {
>> -		parent_path = kstrndup(path, tail - path,
>> GFP_KERNEL);
>> +		parent_path = kstrndup(path, (tail - 1) - path,
>> GFP_KERNEL);
> 
> Since  previous line has (tail > path + 1) which is equivalent to (tail
> - 1 > path), can we amend both?
> 
> For example (might be better, but first comes to my mind)
> 
> const char *tail = kbasename(path) - 1;

Agreed. I'll send out a v2 of the patch with this update and add a comment
to indicate we do not want to include the trailing '\' character.

-Nathan

> 
> ...
> 
> if (tail > path) {
> 
> 
>>  		if (!parent_path)
>>  			return ERR_PTR(-ENOMEM);
>>  	}
>>
> 

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

end of thread, other threads:[~2015-10-27 13:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-26 19:33 [PATCH] powerpc/pseries: Correct string length in pseries_of_derive_parent() Nathan Fontenot
2015-10-27  8:17 ` Andy Shevchenko
2015-10-27 13:51   ` Nathan Fontenot

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