drm/bridge: imx8qxp-pxl2dpi: avoid of_node_put() on ERR_PTR()
Subsystems (2)
PATCH v2 1/1 drm/bridge: imx8qxp-pxl2dpi: avoid of_node_put() on ERR_PTR()
imx8qxp_pxl2dpi_get_available_ep_from_port() may return ERR_PTR(-ENODEV) or ERR_PTR(-EINVAL). imx8qxp_pxl2dpi_find_next_bridge() stores that value in a __free(device_node) variable and then immediately checks IS_ERR(ep).
On the error path, returning from the function triggers the cleanup handler for __free(device_node). Since the device_node cleanup helper only checks for NULL before calling of_node_put(), this results in of_node_put(ERR_PTR(...)), which may lead to an invalid kobject_put() dereference and crash the kernel.
Fix it by avoiding __free(device_node) for the endpoint pointer and releasing it explicitly after obtaining the remote port parent.
This issue was found by a custom static analysis tool.
---
v2:
- Fix DEFINE_FREE(device_node, ...) directly
include/linux/of.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/of.h +1 -1
@@ -135,7 +135,7 @@ static inline struct device_node *of_node_get(struct device_node *node) }static inline void of_node_put (struct device_node *node) { }#endif /* !CONFIG_OF_DYNAMIC */ -DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T)) +DEFINE_FREE(device_node, struct device_node *, if (_T && !IS_ERR(_T)) of_node_put(_T)) /* Pointer for first entry in chain of all nodes. */extern struct device_node *of_root;
Signature · 1 line
2.43.0
Quoted 2 lines · Guangshuo Li
imx8qxp_pxl2dpi_get_available_ep_from_port()may returnERR_PTR(-ENODEV)orERR_PTR(-EINVAL).imx8qxp_pxl2dpi_find_next_bridge()stores that
That's bug there.
Quoted 9 lines
value in a
__free(device_node)variable and then immediately checksIS_ERR(ep).On the error path, returning from the function triggers the cleanup handler for
__free(device_node). Since thedevice_nodecleanup helper only checks for NULL before callingof_node_put(), this results in of_node_put(ERR_PTR(...)), which may lead to an invalidkobject_put()dereference and crash the kernel.
Quoted 6 lines
Fix it by avoiding
__free(device_node)for the endpoint pointer and releasing it explicitly after obtaining the remote port parent.This issue was found by a custom static analysis tool.
Fixes ceea3f7806a10 ("drm/bridge: imx8qxp-pxl2dpi: simplify put of device_node pointers")
Nope. This is not a fix for buggy driver. Fix buggy driver.
Quoted 14 lines
Signed-off-by Guangshuo Li <[email protected]>---
v2:
- FixDEFINE_FREE(device_node, ...)directlyinclude/linux/of.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/of.h +0
-0
include/linux/of.h static inline struct device_node *of_node_get(struct device_node *node) · line 138
Quoted 5 lines
include/linux/of.h +1
-1@@ -135,7 +135,7 @@ static inline struct device_node *of_node_get(struct device_node *node) }static inline void of_node_put (struct device_node *node) { }#endif /* !CONFIG_OF_DYNAMIC */ -DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T)) +DEFINE_FREE(device_node, struct device_node *, if (_T && !IS_ERR(_T)) of_node_put(_T))
So you open coded IS_ERR_OR_NULL. No, wrong pattern. Fix buggy pattern in the driver.
Best regards,
Krzysztof