drm/tegra: rgb: Fix use-after-free and leak in tegra_dc_rgb_probe()
PATCH 1/1 drm/tegra: rgb: Fix use-after-free and leak in tegra_dc_rgb_probe()
Move the of_device_is_available() check before devm_add_action_or_reset() to avoid using np after it may have been freed (if the action registration fails). Also release np immediately when the device is not available to prevent a reference leak.
--- drivers/gpu/drm/tegra/rgb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/tegra/rgb.c +5 -3
@@ -215,13 +215,15 @@ int tegra_dc_rgb_probe(struct tegra_dc *dc)if (!np)return -ENODEV ; +if (!of_device_is_available (np)) { +of_node_put (np); +return -ENODEV ; + } + err =devm_add_action_or_reset (dc->dev, tegra_dc_of_node_put, np);if (err <0 )return err; -if (!of_device_is_available (np)) -return -ENODEV ; - rgb =devm_kzalloc (dc->dev,sizeof (*rgb),GFP_KERNEL );if (!rgb)return -ENOMEM ;
Signature · 1 line
2.34.1
Thierry Reding
maintainerQuoted 22 lines · Wentao Liang
Move the
of_device_is_available()check beforedevm_add_action_or_reset()to avoid using np after it may have been freed (if the action registration fails). Also release np immediately when the device is not available to prevent a reference leak.Fixes 3c3642335065 ("drm/tegra: rgb: Fix the unbound reference count")Signed-off-by Wentao Liang <[email protected]>--- drivers/gpu/drm/tegra/rgb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/tegra/rgb.c +2
-0@@ -215,13 +215,15 @@ int tegra_dc_rgb_probe(struct tegra_dc *dc)if (!np)return -ENODEV ; +if (!of_device_is_available (np)) { +of_node_put (np);
drivers/gpu/drm/tegra/rgb.c int tegra_dc_rgb_probe(struct tegra_dc *dc) · line 224
Quoted 10 lines
drivers/gpu/drm/tegra/rgb.c +3
-3+return -ENODEV ; + } + err =devm_add_action_or_reset (dc->dev, tegra_dc_of_node_put, np);if (err <0 )return err; -if (!of_device_is_available (np)) -return -ENODEV ; -
I don't see how this achieves anything. If the action registration fails, devm_add_action_or_reset() executes the action (that's the "or_reset" part), so that takes care of the leak. Similarly, when things fail, np is not going to be used anymore since we immediately return, so there's no use-after-free either.
Thierry