Skip to contentSkip to conversation

powerpc/pseries: Correct string length in pseries_of_derive_parent()

PATCH Superseded Nathan Fontenot 2 participants 3 messages Oct 26, 2015 – Oct 27, 2015 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/pseries: Correct string length in pseries_of_derive_parent()

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 '/'.

---
 arch/powerpc/platforms/pseries/of_helpers.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/pseries/of_helpers.c +1 -1
index 4417afe..6d90378 100644
@@ -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);
 	}
#1 lore raw
Quoted 8 lines · Nathan Fontenot

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.

Quoted 30 lines

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 '/'.

---
 arch/powerpc/platforms/pseries/of_helpers.c |    2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/pseries/of_helpers.c +0 -0
index 4417afe..6d90378 100644
@@ -24,7 +24,7 @@ struct device_node *pseries_of_derive_parent(const
 char *path)
  		return ERR_PTR(-EINVAL);


  if (tail > path + 1) {

arch/powerpc/platforms/pseries/of_helpers.c +0 -1
-		parent_path = kstrndup(path, tail - path,

GFP_KERNEL);

arch/powerpc/platforms/pseries/of_helpers.c +1 -0
+		parent_path = kstrndup(path, (tail - 1) - path,
Quoted 1 line

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

Quoted 4 lines

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

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#2 lore raw
Quoted 48 lines · Andy Shevchenko
Quoted 8 lines · Nathan Fontenot

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.

Quoted 23 lines

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 '/'.

---
 arch/powerpc/platforms/pseries/of_helpers.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/pseries/of_helpers.c +0 -0
index 4417afe..6d90378 100644

arch/powerpc/platforms/pseries/of_helpers.c struct device_node *pseries_of_derive_parent(const · line 30

Quoted 8 lines
arch/powerpc/platforms/pseries/of_helpers.c +1 -1
@@ -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?

Quoted 3 lines

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

Quoted 11 lines

...

if (tail > path) {

Quoted 4 lines
arch/powerpc/platforms/pseries/of_helpers.c +0 -0
 		if (!parent_path)
 			return ERR_PTR(-ENOMEM);
 	}