Skip to contentSkip to conversation

powerpc/pseries: Correct string length in pseries_of_derive_parent()

PATCH Applied Nathan Fontenot 3 participants 3 messages Oct 27, 2015 – Nov 2, 2015 PowerPC

lore
Share & tools
lore
#0 lore raw

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

Commit a030e1e4bbd085bbcfd0a23f8d355fcd41f39bed make a change to use kstrndup() instead of kmalloc() + strlcpy() in the pseries_of_derive_parent() routine that 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 tailing '/' character, "/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 decrements the pointer returned from kbasename() to point to the '/' character before the base name instead of the base name. This then adjusts the string length calculations to not include the trailing '/' in the parent path name.

---

Updates for v2: Decrement the pointer returned from kbasename() instead of subtracting 1 when calculating the string length.

 arch/powerpc/platforms/pseries/of_helpers.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/powerpc/platforms/pseries/of_helpers.c +5 -2
index 4417afe..2798933 100644
@@ -17,13 +17,16 @@ struct device_node *pseries_of_derive_parent(const char *path)
 {
 	struct device_node *parent;
 	char *parent_path = "/";
-	const char *tail = kbasename(path);
+	const char *tail;
+
+	/* We do not want the trailing '/' character */
+	tail = kbasename(path) - 1;
 
 	/* reject if path is "/" */
 	if (!strcmp(path, "/"))
 		return ERR_PTR(-EINVAL);
 
-	if (tail > path + 1) {
+	if (tail > path) {
 		parent_path = kstrndup(path, tail - path, GFP_KERNEL);
 		if (!parent_path)
 			return ERR_PTR(-ENOMEM);
Reviewed-by on v2 1/1
#1 lore raw
Quoted 25 lines · Nathan Fontenot

Commit a030e1e4bbd085bbcfd0a23f8d355fcd41f39bed make a change to use kstrndup() instead of kmalloc() + strlcpy() in the pseries_of_derive_parent()
routine that 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
tailing '/' character, "/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 decrements the pointer returned from kbasename() to point to the
'/' character before the base name instead of the base name. This then
adjusts the string length calculations to not include the trailing '/'
in the parent path name.

Quoted 1 line
Quoted 36 lines

---

Updates for v2: Decrement the pointer returned from kbasename() instead
of subtracting 1 when calculating the string length.

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

 1 file changed, 5 insertions(+), 2 deletions(-)
arch/powerpc/platforms/pseries/of_helpers.c +4 -1
index 4417afe..2798933 100644
@@ -17,13 +17,16 @@ struct device_node
 *pseries_of_derive_parent(const char *path)
  {
  	struct device_node *parent;
  	char *parent_path = "/";
-	const char *tail = kbasename(path);
+	const char *tail;
+
+	/* We do not want the trailing '/' character */
+	tail = kbasename(path) - 1;


  /* reject if path is "/" */
  if (!strcmp(path, "/"))
  return ERR_PTR(-EINVAL);

arch/powerpc/platforms/pseries/of_helpers.c +1 -1
-	if (tail > path + 1) {
+	if (tail > path) {

  parent_path = kstrndup(path, tail - path, GFP_KERNEL);
  if (!parent_path)
  return ERR_PTR(-ENOMEM);

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
reviewer Applied
#2 lore raw
In reply to Nathan Fontenot
Quoted 11 lines · Nathan Fontenot

Commit a030e1e4bbd085bbcfd0a23f8d355fcd41f39bed make a change to use kstrndup() instead of kmalloc() + strlcpy() in the pseries_of_derive_parent() routine that 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 tailing '/' character, "/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.

Quoted 7 lines

This patch decrements the pointer returned from kbasename() to point to the '/' character before the base name instead of the base name. This then adjusts the string length calculations to not include the trailing '/' in the parent path name.

Applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/f755ecfb8cec7b19dff84295

cheers