]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
of: property: Fix of_fwnode_get_reference_args() with negative index
authorAlban Bedel <alban.bedel@lht.dlh.de>
Thu, 18 Jun 2026 15:20:34 +0000 (17:20 +0200)
committerRob Herring (Arm) <robh@kernel.org>
Wed, 24 Jun 2026 12:56:54 +0000 (07:56 -0500)
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.

Link: https://lore.kernel.org/linux-leds/aimVRwJPhlGxsIUj@tom-desktop/T/#mc43cbf7e0599991b56dd0d9680714d28d145fbc8
Cc: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
Signed-off-by: Alban Bedel <alban.bedel@lht.dlh.de>
Link: https://patch.msgid.link/20260618152035.1600436-1-alban.bedel@lht.dlh.de
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
drivers/of/property.c

index b276d1de32222d2c33b96d9856d57e39dfcb2a2a..72cf12907de034e96724c4bcd920cde0340a7342 100644 (file)
@@ -1172,6 +1172,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);