Skip to contentSkip to conversation

of: overlay: avoid potential null pointer exception

PATCH Applied Michael Riesch 3 participants 7 messages Sep 5, 2022 – Sep 21, 2022 Device tree

lore
Share & tools
lore
#0 lore raw

cover of: overlay: avoid potential null pointer exception

Hi all,

The function of_overlay_fix_path returns NULL in certain error cases but of_overlay_apply_symbols (which is the only caller) does not check the return value. For broken overlays this may result in a null pointer exception. Fix this by checking the return value and inform the user what exactly went wrong. To this end, improve the error handling in of_overlay_apply_tree.

The thread [0] gives a bit more context.

Best regards,
Michael

[0] https://lore.barebox.org/barebox/[email protected]/T/#u

Michael Riesch (2):
of: overlay: improve error handling in of_overlay_apply_tree
of: overlay: avoid potential null pointer exception

 drivers/of/overlay.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 of: overlay: improve error handling in of_overlay_apply_tree

Propagate any error from of_overlay_apply_symbols and let the user know if the provided overlay is not applicable.

---
 drivers/of/overlay.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/of/overlay.c +9 -5
index 20a43f5170..20686db511 100644
@@ -115,8 +115,8 @@ static char *of_overlay_fix_path(struct device_node *root,
 	return basprintf("%s%s", target->full_name, path_tail);
 }
 
-static void of_overlay_apply_symbols(struct device_node *root,
-				     struct device_node *overlay)
+static int of_overlay_apply_symbols(struct device_node *root,
+				    struct device_node *overlay)
 {
 	const char *old_path;
 	char *new_path;
@@ -129,12 +129,12 @@ static void of_overlay_apply_symbols(struct device_node *root,
 
 	if (!overlay_symbols) {
 		pr_debug("overlay doesn't have a __symbols__ node\n");
-		return;
+		return -EINVAL;
 	}
 
 	if (!root_symbols) {
 		pr_info("root doesn't have a __symbols__ node\n");
-		return;
+		return -EINVAL;
 	}
 
 	list_for_each_entry(prop, &overlay_symbols->properties, list) {
@@ -148,6 +148,8 @@ static void of_overlay_apply_symbols(struct device_node *root,
 			 prop->name, new_path);
 		of_property_write_string(root_symbols, prop->name, new_path);
 	}
+
+	return 0;
 }
 
 static int of_overlay_apply_fragment(struct device_node *root,
@@ -190,7 +192,9 @@ int of_overlay_apply_tree(struct device_node *root,
 		goto out_err;
 
 	/* Copy symbols from resolved overlay to base device tree */
-	of_overlay_apply_symbols(root, resolved);
+	err = of_overlay_apply_symbols(root, resolved);
+	if (err)
+		goto out_err;
 
 	/* Copy nodes and properties from resolved overlay to root */
 	for_each_child_of_node(resolved, fragment) {
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] of: overlay: improve error handling in of_overlay_apply_tree

Hi all,

Quoted 24 lines · Michael Riesch

Propagate any error from of_overlay_apply_symbols and let the user know if the provided overlay is not applicable.

---
 drivers/of/overlay.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/of/overlay.c +2 -2
index 20a43f5170..20686db511 100644
@@ -115,8 +115,8 @@ static char *of_overlay_fix_path(struct device_node *root,
 	return basprintf("%s%s", target->full_name, path_tail);
 }
 
-static void of_overlay_apply_symbols(struct device_node *root,
-				     struct device_node *overlay)
+static int of_overlay_apply_symbols(struct device_node *root,
+				    struct device_node *overlay)
 {
 	const char *old_path;
 	char *new_path;

drivers/of/overlay.c static void of_overlay_apply_symbols(struct device_node *root, · line 132

Quoted 5 lines
drivers/of/overlay.c +1 -1
@@ -129,12 +129,12 @@ static void of_overlay_apply_symbols(struct device_node *root,
 
 	if (!overlay_symbols) {
 		pr_debug("overlay doesn't have a __symbols__ node\n");
-		return;
+		return -EINVAL;

Come to think of it, do all overlays need to provide a __symbols__ node? If not, this check is overly strict.

drivers/of/overlay.c static void of_overlay_apply_symbols(struct device_node *root, · line 137

Quoted 6 lines
drivers/of/overlay.c +1 -1
 	}
 
 	if (!root_symbols) {
 		pr_info("root doesn't have a __symbols__ node\n");
-		return;
+		return -EINVAL;

Ditto for the root.

Quoted 13 lines
drivers/of/overlay.c +2 -0
 	}
 
 	list_for_each_entry(prop, &overlay_symbols->properties, list) {
@@ -148,6 +148,8 @@ static void of_overlay_apply_symbols(struct device_node *root,
 			 prop->name, new_path);
 		of_property_write_string(root_symbols, prop->name, new_path);
 	}
+
+	return 0;
 }
 
 static int of_overlay_apply_fragment(struct device_node *root,

drivers/of/overlay.c int of_overlay_apply_tree(struct device_node *root, · line 197

Quoted 7 lines
drivers/of/overlay.c +3 -1
@@ -190,7 +192,9 @@ int of_overlay_apply_tree(struct device_node *root,
 		goto out_err;
 
 	/* Copy symbols from resolved overlay to base device tree */
-	of_overlay_apply_symbols(root, resolved);
+	err = of_overlay_apply_symbols(root, resolved);
+	if (err)
+		goto out_err;

If both checks need to be relaxed, the complete patch should be reverted I guess :-/

Thanks in advance for your comments.

Best regards,
Michael

Quoted 3 lines
drivers/of/overlay.c +0 -0
 	/* Copy nodes and properties from resolved overlay to root */
 	for_each_child_of_node(resolved, fragment) {
#3 lore raw

Re: [PATCH 1/2] of: overlay: improve error handling in of_overlay_apply_tree

Quoted 26 lines · Michael Riesch
Quoted 25 lines · Michael Riesch

Propagate any error from of_overlay_apply_symbols and let the user know if the provided overlay is not applicable.

---
 drivers/of/overlay.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/of/overlay.c +2 -2
index 20a43f5170..20686db511 100644
@@ -115,8 +115,8 @@ static char *of_overlay_fix_path(struct device_node *root,
 	return basprintf("%s%s", target->full_name, path_tail);
 }
 
-static void of_overlay_apply_symbols(struct device_node *root,
-				     struct device_node *overlay)
+static int of_overlay_apply_symbols(struct device_node *root,
+				    struct device_node *overlay)
 {
 	const char *old_path;
 	char *new_path;
@@ -129,12 +129,12 @@ static void of_overlay_apply_symbols(struct device_node *root,
 
Quoted 7 lines

drivers/of/overlay.c static void of_overlay_apply_symbols(struct device_node *root, · line 132

Quoted 4 lines
drivers/of/overlay.c +1 -1
 	if (!overlay_symbols) {
 		pr_debug("overlay doesn't have a __symbols__ node\n");
-		return;
+		return -EINVAL;

Come to think of it, do all overlays need to provide a __symbols__ node? If not, this check is overly strict.

Overlays don't need a __symbols__ node. It would be only required, if overlays are stacked and the second overlay refers to nodes of the first overlay by labels. Having no __symbols__ in the overlay is a success path and the message is just a debug message.

Quoted 9 lines

drivers/of/overlay.c static void of_overlay_apply_symbols(struct device_node *root, · line 137

Quoted 7 lines
drivers/of/overlay.c +1 -1
 	}
 
 	if (!root_symbols) {
 		pr_info("root doesn't have a __symbols__ node\n");
-		return;
+		return -EINVAL;

Ditto for the root.

I'm not sure what should happen, if the root does not have __symbols__. Barebox wouldn't be able to copy the __symbols__ of the overlay, but this still wouldn't be a problem unless overlays are stacked. In the stacking case, only applying the second overlay should fail.

Maybe, we should add a new __symbols__ node, if the root doesn't have a __symbols__ node?

Quoted 16 lines
Quoted 16 lines
drivers/of/overlay.c +2 -0
 	}
 
 	list_for_each_entry(prop, &overlay_symbols->properties, list) {
@@ -148,6 +148,8 @@ static void of_overlay_apply_symbols(struct device_node *root,
 			 prop->name, new_path);
 		of_property_write_string(root_symbols, prop->name, new_path);
 	}
+
+	return 0;
 }
 
 static int of_overlay_apply_fragment(struct device_node *root,
@@ -190,7 +192,9 @@ int of_overlay_apply_tree(struct device_node *root,
 		goto out_err;
 
Quoted 8 lines

drivers/of/overlay.c int of_overlay_apply_tree(struct device_node *root, · line 197

Quoted 5 lines
drivers/of/overlay.c +3 -1
 	/* Copy symbols from resolved overlay to base device tree */
-	of_overlay_apply_symbols(root, resolved);
+	err = of_overlay_apply_symbols(root, resolved);
+	if (err)
+		goto out_err;

If both checks need to be relaxed, the complete patch should be reverted I guess :-/

What did you do to run into this error? What was your expectation?

Michael

#4 lore raw

Re: [PATCH 1/2] of: overlay: improve error handling in of_overlay_apply_tree

Hi Michael,

Quoted 35 lines · Michael Tretter
Quoted 26 lines · Michael Riesch
Quoted 25 lines · Michael Riesch

Propagate any error from of_overlay_apply_symbols and let the user know if the provided overlay is not applicable.

---
 drivers/of/overlay.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/of/overlay.c +2 -2
index 20a43f5170..20686db511 100644
@@ -115,8 +115,8 @@ static char *of_overlay_fix_path(struct device_node *root,
 	return basprintf("%s%s", target->full_name, path_tail);
 }
 
-static void of_overlay_apply_symbols(struct device_node *root,
-				     struct device_node *overlay)
+static int of_overlay_apply_symbols(struct device_node *root,
+				    struct device_node *overlay)
 {
 	const char *old_path;
 	char *new_path;
@@ -129,12 +129,12 @@ static void of_overlay_apply_symbols(struct device_node *root,
 
Quoted 7 lines

drivers/of/overlay.c static void of_overlay_apply_symbols(struct device_node *root, · line 132

Quoted 4 lines
drivers/of/overlay.c +1 -1
 	if (!overlay_symbols) {
 		pr_debug("overlay doesn't have a __symbols__ node\n");
-		return;
+		return -EINVAL;

Come to think of it, do all overlays need to provide a __symbols__ node? If not, this check is overly strict.

Quoted 4 lines

Overlays don't need a __symbols__ node. It would be only required, if overlays are stacked and the second overlay refers to nodes of the first overlay by labels. Having no __symbols__ in the overlay is a success path and the message is just a debug message.

Thanks for the clarification. We need to fix this one, then.

Seeing that the patch is in next: Am I supposed to send an incremental "fixup! ..." patch which can be squashed? Or should I send a proper patch with a Fixes: tag?

Quoted 9 lines
Quoted 8 lines

drivers/of/overlay.c static void of_overlay_apply_symbols(struct device_node *root, · line 137

Quoted 6 lines
drivers/of/overlay.c +1 -1
 	}
 
 	if (!root_symbols) {
 		pr_info("root doesn't have a __symbols__ node\n");
-		return;
+		return -EINVAL;

Ditto for the root.

Quoted 4 lines

I'm not sure what should happen, if the root does not have __symbols__. Barebox wouldn't be able to copy the __symbols__ of the overlay, but this still wouldn't be a problem unless overlays are stacked. In the stacking case, only applying the second overlay should fail.

I can reestablish the behavior before this patch, i.e., __symbols__ is optional in both root and overlay.

Quoted 2 lines

Maybe, we should add a new __symbols__ node, if the root doesn't have a __symbols__ node?

If this is desired, I can implement the change -- but is it desired?

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
drivers/of/overlay.c +2 -0
 	}
 
 	list_for_each_entry(prop, &overlay_symbols->properties, list) {
@@ -148,6 +148,8 @@ static void of_overlay_apply_symbols(struct device_node *root,
 			 prop->name, new_path);
 		of_property_write_string(root_symbols, prop->name, new_path);
 	}
+
+	return 0;
 }
 
 static int of_overlay_apply_fragment(struct device_node *root,
@@ -190,7 +192,9 @@ int of_overlay_apply_tree(struct device_node *root,
 		goto out_err;
 
Quoted 8 lines

drivers/of/overlay.c int of_overlay_apply_tree(struct device_node *root, · line 197

Quoted 5 lines
drivers/of/overlay.c +3 -1
 	/* Copy symbols from resolved overlay to base device tree */
-	of_overlay_apply_symbols(root, resolved);
+	err = of_overlay_apply_symbols(root, resolved);
+	if (err)
+		goto out_err;

If both checks need to be relaxed, the complete patch should be reverted I guess :-/

Quoted 1 line

What did you do to run into this error? What was your expectation?

Well I tried to apply an overlay without __symbols__ :-) (which did work before).

Best regards,
Michael

#5 lore raw

PATCH 2/2 of: overlay: avoid potential null pointer exception

In reply to Michael Riesch

The function of_overlay_fix_path returns NULL in certain error cases but of_overlay_apply_symbols (which is the only caller) does not check the return value. For broken overlays this may result in a null pointer exception. Fix this by checking the return value and inform the user what exactly went wrong.

---
 drivers/of/overlay.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/of/overlay.c +5 -1
index 20686db511..884cdf8928 100644
@@ -102,8 +102,10 @@ static char *of_overlay_fix_path(struct device_node *root,
 		if (of_get_child_by_name(fragment, "__overlay__"))
 			break;
 	}
-	if (!fragment)
+	if (!fragment) {
+		pr_info("could not find __overlay__ node\n");
 		return NULL;
+	}
 
 	target = find_target(root, fragment);
 	if (!target)
@@ -143,6 +145,8 @@ static int of_overlay_apply_symbols(struct device_node *root,
 
 		old_path = of_property_get_value(prop);
 		new_path = of_overlay_fix_path(root, overlay, old_path);
+		if (!new_path)
+			return -EINVAL;
 
 		pr_debug("add symbol %s with new path %s\n",
 			 prop->name, new_path);
Signature · 1 line
2.30.2
Applied
#6 lore raw
In reply to Michael Riesch
Quoted 11 lines · Michael Riesch

Hi all,

The function of_overlay_fix_path returns NULL in certain error cases but of_overlay_apply_symbols (which is the only caller) does not check the return value. For broken overlays this may result in a null pointer exception. Fix this by checking the return value and inform the user what exactly went wrong. To this end, improve the error handling in of_overlay_apply_tree.

The thread [0] gives a bit more context.

Quoted 8 lines

Best regards,
Michael

[0] https://lore.barebox.org/barebox/[email protected]/T/#u

Michael Riesch (2):
of: overlay: improve error handling in of_overlay_apply_tree
of: overlay: avoid potential null pointer exception

Applied, thanks

Sascha

Quoted 9 lines
 drivers/of/overlay.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
Signature · 1 line
2.30.2
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |