Skip to contentSkip to conversation

of: property: Fix of_fwnode_get_reference_args() with negative index

PATCH Applied Alban Bedel 6 participants 11 messages Jun 18 – Jun 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 of: property: Fix of_fwnode_get_reference_args() with negative index

fwnode_property_get_reference_args() should return -ENOENT when an out of bound index is passed. An issue arised with the OF backend because the OF API use signed indexes while the fwnode API use unsigned ones. When an index value greater the INT_MAX was passed to the OF backend it got casted to a negative value and it returned -EINVAL instead of -ENOENT. This patch add a check to of_fwnode_get_reference_args() to catch negative index before they are passed to the OF API and return -ENOENT right away.

This issue appeared when the following pattern was used in the LED subsystem:

index = fwnode_property_match_string(fwnode, "led-names", name) led_node = fwnode_find_reference(fwnode, "leds", index);

Unlike the same pattern with the OF API, this pattern implicitly cast the signed return value of fwnode_property_match_string() to an unsigned index leading to the above issue with the OF backend. It can be argued that the return value of fwnode_property_match_string() should be checked separately, but I think there is value in supporting such simple and straight to the point patterns.

---
v2: Fixed comment block to follow linux coding style.

---
 drivers/of/property.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/of/property.c +8 -0
index 136946f8b746f..a4e9ffe1a4121 100644
@@ -1157,6 +1157,14 @@ of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
 	unsigned int i;
 	int ret;
 
+	/*
+	 * This function should return -ENOENT for out of bound indexes,
+	 * but the OF API uses signed indexes and consider negative indexes
+	 * as invalid. Catch them here to correctly implement the fwnode API.
+	 */
+	if ((int)index < 0)
+		return -ENOENT;
+
 	if (nargs_prop)
 		ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
 						 nargs_prop, index, &of_args);
Signature · 1 line
2.39.5
#1 lore raw

PATCH v2 2/2 software node: Fix software_node_get_reference_args() with index -1

The bounds check for the index passed to software_node_get_reference_args() was failing when passed UINT_MAX, this in turn would lead to an out of bound access in the property array. Fix the bound check to also cover the UINT_MAX case.

Fixes 31e4e12e0e960 ("software node: Correct a OOB check in software_node_get_reference_args()")

--
v2: No changes. Only submit this patch along with the patch that
triggered the Sashiko report, to hopefully avoid another useless report.

---
 drivers/base/swnode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/base/swnode.c +1 -1
index 869228a65cb36..2bc76f01eb77d 100644
@@ -537,7 +537,7 @@ software_node_get_reference_args(const struct fwnode_handle *fwnode,
 	if (prop->is_inline)
 		return -EINVAL;
 
-	if ((index + 1) * sizeof(*ref) > prop->length)
+	if (index >= prop->length / sizeof(*ref))
 		return -ENOENT;
 
 	ref_array = prop->pointer;
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH v2 2/2] software node: Fix software_node_get_reference_args() with index -1

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Use-after-free vulnerability in software node reference resolution.
- [High] Data race and linked list corruption in software node children management. --

commit 3b564802f7102505002d84b52abe9e6a26646a5d
Author: Alban Bedel <[email protected]>

software node: Fix software_node_get_reference_args() with index -1

This patch corrects an out-of-bounds access check in software_node_get_reference_args() by ensuring the bounds check correctly handles UINT_MAX as an index.

drivers/base/swnode.c software_node_get_reference_args(const struct fwnode_handle *fwnode, · line 535

Quoted 13 lines
drivers/base/swnode.c +1 -1
index a19f8f722bc8e..e76f7c36f19e6 100644
@@ -529,7 +529,7 @@ software_node_get_reference_args(const struct fwnode_handle *fwnode,
 	if (prop->is_inline)
 		return -EINVAL;
 
-	if ((index + 1) * sizeof(*ref) > prop->length)
+	if (index >= prop->length / sizeof(*ref))
 		return -ENOENT;
 
 	ref_array = prop->pointer;

[Severity: High]
This isn't a bug introduced by this patch, but it looks like there's a pre-existing use-after-free risk later in this function.

When we resolve the reference here:

	if (ref->swnode)
		refnode = software_node_fwnode(ref->swnode);
	...
	args->fwnode = fwnode_handle_get(refnode);

software_node_fwnode() calls software_node_to_swnode(), which drops the swnode_kset->list_lock and returns the swnode pointer without elevating its reference count via kobject_get():

drivers/base/swnode.c:software_node_to_swnode() {
    ...
    list_for_each_entry(k, &swnode_kset->list, entry) {
        swnode = kobj_to_swnode(k);
        if (swnode->node == node)
            break;
        swnode = NULL;
    }
    spin_unlock(&swnode_kset->list_lock);
    return swnode;
}

Since the pointer is unreferenced, could it be concurrently unregistered and freed (e.g., by software_node_unregister()) before we call fwnode_handle_get() on it?

[Severity: High]
This is also a pre-existing issue, but I noticed a potential data race and linked list corruption in the software node children management in drivers/base/swnode.c.

In swnode_register(), child nodes are added to the parent's list without a lock:

drivers/base/swnode.c:swnode_register() {
    ...
    if (parent)
        list_add_tail(&swnode->entry, &parent->children);
    ...
}

And in software_node_release(), they are removed without synchronization:

drivers/base/swnode.c:software_node_release() {
    ...
    if (swnode->parent) {
        ida_free(&swnode->parent->child_ids, swnode->id);
        list_del(&swnode->entry);
    }
    ...
}

If multiple child nodes are concurrently registered or unregistered under the same parent (e.g., dynamically by drivers calling fwnode_create_software_node()), could this corrupt the doubly linked list and crash the kernel?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#3 lore raw

Re: [PATCH v2 2/2] software node: Fix software_node_get_reference_args() with index -1

In reply to Alban Bedel
Quoted 4 lines · Alban Bedel

The bounds check for the index passed to software_node_get_reference_args() was failing when passed UINT_MAX, this in turn would lead to an out of bound access in the property array. Fix the bound check to also cover the UINT_MAX case.

...

drivers/base/swnode.c software_node_get_reference_args(const struct fwnode_handle *fwnode, · line 540

Quoted 2 lines
drivers/base/swnode.c +1 -1
-	if ((index + 1) * sizeof(*ref) > prop->length)
+	if (index >= prop->length / sizeof(*ref))

It trades multiplication for division (which might be not always power-of-two).

Quoted 1 line

return -ENOENT;

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH v2 2/2] software node: Fix software_node_get_reference_args() with index -1

Quoted 6 lines · On Thu, 18 Jun 2026 20:35:59 +0200 Andy Shevchenko…
Quoted 4 lines · Alban Bedel

The bounds check for the index passed to software_node_get_reference_args() was failing when passed UINT_MAX, this in turn would lead to an out of bound access in the property array. Fix the bound check to also cover the UINT_MAX case.

Quoted 7 lines

...

drivers/base/swnode.c software_node_get_reference_args(const struct fwnode_handle *fwnode, · line 540

Quoted 2 lines
drivers/base/swnode.c +1 -1
-	if ((index + 1) * sizeof(*ref) > prop->length)
+	if (index >= prop->length / sizeof(*ref))  

It trades multiplication for division (which might be not always power-of-two).

This code is not really performance relevant and using a non trivial expression lead to the currently buggy code. I find that easy to understand and obviously correct code is better suited here.

What alternative would you suggest?

Alban

#5 lore raw

Re: [PATCH v2 2/2] software node: Fix software_node_get_reference_args() with index -1

In reply to Alban Bedel
Quoted 6 lines · Alban Bedel

The bounds check for the index passed to software_node_get_reference_args() was failing when passed UINT_MAX, this in turn would lead to an out of bound access in the property array. Fix the bound check to also cover the UINT_MAX case.

Fixes 31e4e12e0e960 ("software node: Correct a OOB check in software_node_get_reference_args()")

i think the fix tag may not be right.

for original express before the fix tag: if (index * sizeof(*ref) > prop->length)

for UINT_MAX, multiplication overflow?

Quoted 16 lines

--
v2: No changes. Only submit this patch along with the patch that
triggered the Sashiko report, to hopefully avoid another useless report.

---
 drivers/base/swnode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/base/swnode.c +0 -0
index 869228a65cb36..2bc76f01eb77d 100644

drivers/base/swnode.c software_node_get_reference_args(const struct fwnode_handle *fwnode, · line 542

Quoted 7 lines
drivers/base/swnode.c +1 -1
@@ -537,7 +537,7 @@ software_node_get_reference_args(const struct fwnode_handle *fwnode,
 	if (prop->is_inline)
 		return -EINVAL;
 
-	if ((index + 1) * sizeof(*ref) > prop->length)
+	if (index >= prop->length / sizeof(*ref))
 		return -ENOENT;
 

who will use UINT_MAX ?

This function is a interface function. the best fix should check input parameter @index and return -EINVAL if it is not expected?

#6 lore raw

Re: [PATCH v2 2/2] software node: Fix software_node_get_reference_args() with index -1

Quoted 6 lines · Zijun Hu
Quoted 5 lines · Alban Bedel

The bounds check for the index passed to software_node_get_reference_args() was failing when passed UINT_MAX, this in turn would lead to an out of bound access in the property array. Fix the bound check to also cover the UINT_MAX case.

Quoted 7 lines
Quoted 1 line
Fixes 31e4e12e0e960 ("software node: Correct a OOB check in software_node_get_reference_args()")

i think the fix tag may not be right.

for original express before the fix tag: if (index * sizeof(*ref) > prop->length)

for UINT_MAX, multiplication overflow?

I agree that Fixes tag may be not the best chosen.

...

Quoted 12 lines

drivers/base/swnode.c software_node_get_reference_args(const struct fwnode_handle *fwnode, · line 541

Quoted 9 lines

software_node_get_reference_args(const struct fwnode_handle *fwnode,

drivers/base/swnode.c +1 -1
 	if (prop->is_inline)
 		return -EINVAL;
 
-	if ((index + 1) * sizeof(*ref) > prop->length)
+	if (index >= prop->length / sizeof(*ref))
 		return -ENOENT;

who will use UINT_MAX ?

Quoted 2 lines

This function is a interface function. the best fix should check input parameter @index and return -EINVAL if it is not expected?

And how do you know which index is allowed without doing that division? Basically this patch does what you asked for: check input parameter for the allowed range (which is dynamic, depending on the property length and size of the reference structure.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v2 2/2] software node: Fix software_node_get_reference_args() with index -1

Quoted 8 lines · Andy Shevchenko
Quoted 4 lines

who will use UINT_MAX ?

This function is a interface function. the best fix should check input parameter @index and return -EINVAL if it is not expected?

And how do you know which index is allowed without doing that division? Basically this patch does what you asked for: check input parameter for the allowed range (which is dynamic, depending on the property length and size of the reference structure.

Introduce a common macro to limit the max reference handle count and put it in include/linux/fwnode.h with the following macro.
#define NR_FWNODE_REFERENCE_ARGS 16

OR introduce a macro for software node only.

#8 lore raw

Re: [PATCH v2 2/2] software node: Fix software_node_get_reference_args() with index -1

Quoted 1 line · Zijun Hu

On 6/29/26 15:30, Andy Shevchenko wrote:

...

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

who will use UINT_MAX ?

This function is a interface function. the best fix should check input parameter @index and return -EINVAL if it is not expected?

And how do you know which index is allowed without doing that division? Basically this patch does what you asked for: check input parameter for the allowed range (which is dynamic, depending on the property length and size of the reference structure.

Quoted 5 lines

Introduce a common macro to limit the max reference handle count and put it in include/linux/fwnode.h with the following macro.
#define NR_FWNODE_REFERENCE_ARGS 16

OR introduce a macro for software node only.

The property can be still smaller that capacity. I agree that division is not the best approach performance-wise, but in my eyes it makes less checks needed to be done. Let's see what others think about this?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH v2 2/2] software node: Fix software_node_get_reference_args() with index -1

In reply to Zijun Hu
Quoted 11 lines · On Sat, 27 Jun 2026 07:50:12 +0800 Zijun Hu <[email protected]> wrote:
Quoted 6 lines · Alban Bedel

The bounds check for the index passed to software_node_get_reference_args() was failing when passed UINT_MAX, this in turn would lead to an out of bound access in the property array. Fix the bound check to also cover the UINT_MAX case.

Fixes 31e4e12e0e960 ("software node: Correct a OOB check in software_node_get_reference_args()")

i think the fix tag may not be right.
for original express before the fix tag: if (index * sizeof(*ref) > prop->length) for UINT_MAX, multiplication overflow?

You are right both the orignal code and the version introduced in 31e4e12e0e960 are subject to multiplication overflow issues. I added the fix tag because 31e4e12e0e960 claim to fix an issue but fall short. If someone backport this commit it might be good if they can find that it has an issue.

But if that's not an appropriate use of the fix tag I can remove it.

Quoted 19 lines
Quoted 19 lines

--
v2: No changes. Only submit this patch along with the patch that
triggered the Sashiko report, to hopefully avoid another useless report.

---
 drivers/base/swnode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/base/swnode.c +0 -0
index 869228a65cb36..2bc76f01eb77d 100644
@@ -537,7 +537,7 @@ software_node_get_reference_args(const struct fwnode_handle *fwnode,
 	if (prop->is_inline)
 		return -EINVAL;
 
Quoted 6 lines

drivers/base/swnode.c software_node_get_reference_args(const struct fwnode_handle *fwnode, · line 542

Quoted 4 lines
drivers/base/swnode.c +1 -1
-	if ((index + 1) * sizeof(*ref) > prop->length)
+	if (index >= prop->length / sizeof(*ref))
 		return -ENOENT;
 

who will use UINT_MAX ?

A likely scenario is if a caller pass a negative errno, for example the return value of fwnode_property_match_string(). But as you pointed out there is also the matter of the multiplication overflow, so various values smaller than UINT_MAX can also lead to a failure.

Quoted 2 lines

This function is a interface function. the best fix should check input parameter @index and return -EINVAL if it is not expected?

The index is unsigned, so there is no invalid values, just out of bounds. The documentation of fwnode_property_get_reference_args() says:

* Return: %0 on success
* %-ENOENT when the index is out of bounds, the index has an empty
* reference or the property was not found
* %-EINVAL on parse error
* %-ENOTCONN when the remote firmware node exists but has not been
* registered yet

So this should return -ENOENT.

Alban

maintainer Applied
#10 lore raw
In reply to Alban Bedel
Quoted 26 lines · Alban Bedel

fwnode_property_get_reference_args() should return -ENOENT when an out of bound index is passed. An issue arised with the OF backend because the OF API use signed indexes while the fwnode API use unsigned ones. When an index value greater the INT_MAX was passed to the OF backend it got casted to a negative value and it returned -EINVAL instead of -ENOENT. This patch add a check to of_fwnode_get_reference_args() to catch negative index before they are passed to the OF API and return -ENOENT right away.

This issue appeared when the following pattern was used in the LED subsystem:

index = fwnode_property_match_string(fwnode, "led-names", name) led_node = fwnode_find_reference(fwnode, "leds", index);

Unlike the same pattern with the OF API, this pattern implicitly cast the signed return value of fwnode_property_match_string() to an unsigned index leading to the above issue with the OF backend. It can be argued that the return value of fwnode_property_match_string() should be checked separately, but I think there is value in supporting such simple and straight to the point patterns.

Quoted 6 lines

---
v2: Fixed comment block to follow linux coding style.

---
 drivers/of/property.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Applied, thanks!