usb: dwc3: st: Add of_node_put() before return in probe function
Subsystems (3)
PATCH 1/2 usb: dwc3: st: Add of_node_put() before return in probe function
The local variable child in the function st_dwc3_probe takes the return value of of_get_child_by_name, which gets a node and does not put it. If the function returns without releasing child, this could cause a memory error. Hence put child as soon as there is no more use for it. Also create a new label, err_node_put, just before label undo_softreset; so that err_node_put puts child. In between initialisation of child and its first put, modify all statements that go to undo_softreset to now go to err_node_put instead, from where they can fall through to undo_softreset.
Issue found with Coccinelle.
--- drivers/usb/dwc3/dwc3-st.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/dwc3/dwc3-st.c +6 -3
@@ -255,24 +255,25 @@ static int st_dwc3_probe(struct platform_device *pdev)if (!child) {dev_err (&pdev->dev,"failed to find dwc3 core node\n" ); ret = -ENODEV ; - goto undo_softreset; + goto err_node_put; }/* Allocate and initialize the core */ ret =of_platform_populate (node,NULL ,NULL , dev);if (ret) {dev_err (dev,"failed to add dwc3 core\n" ); - goto undo_softreset; + goto err_node_put; } child_pdev =of_find_device_by_node (child);if (!child_pdev) {dev_err (dev,"failed to find dwc3 core device\n" ); ret = -ENODEV ; - goto undo_softreset; + goto err_node_put; } dwc3_data->dr_mode =usb_get_dr_mode (&child_pdev->dev); +of_node_put (child);/* * Configure the USB port as device or host according to the static
@@ -292,6 +293,8 @@ static int st_dwc3_probe(struct platform_device *pdev)platform_set_drvdata (pdev, dwc3_data);return 0 ; +err_node_put: +of_node_put (child); undo_softreset:reset_control_assert (dwc3_data->rstc_rst); undo_powerdown:
Signature · 1 line
2.19.1
PATCH 2/2 usb: dwc3: st: Add of_dev_put() in probe function
In function st_dwc3_probe, variable child_pdev takes the value returned by of_find_device_by_node, which gets a device pointer but does not put it. If child_pdev is not put before the probe function returns, it may cause a reference leak. Hence put child_pdev after its last usage. Issue found with Coccinelle.
--- drivers/usb/dwc3/dwc3-st.c | 1 + 1 file changed, 1 insertion(+)
drivers/usb/dwc3/dwc3-st.c +1 -0
@@ -274,6 +274,7 @@ static int st_dwc3_probe(struct platform_device *pdev) dwc3_data->dr_mode =usb_get_dr_mode (&child_pdev->dev);of_node_put (child); +of_dev_put (child_pdev);/* * Configure the USB port as device or host according to the static
Signature · 1 line
2.19.1
Re: [PATCH 2/2] usb: dwc3: st: Add of_dev_put() in probe function
Quoted 16 lines · Nishka Dasgupta
In function
st_dwc3_probe, variablechild_pdevtakes the value returned byof_find_device_by_node, which gets a device pointer but does not put it. Ifchild_pdevis not put before the probe function returns, it may cause a reference leak. Hence putchild_pdevafter its last usage. Issue found with Coccinelle.Signed-off-by Nishka Dasgupta <[email protected]>--- drivers/usb/dwc3/dwc3-st.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/dwc3/dwc3-st.c +0
-0
drivers/usb/dwc3/dwc3-st.c static int st_dwc3_probe(struct platform_device *pdev) · line 280
Quoted 7 lines
drivers/usb/dwc3/dwc3-st.c +1
-0@@ -274,6 +274,7 @@ static int st_dwc3_probe(struct platform_device *pdev) dwc3_data->dr_mode =usb_get_dr_mode (&child_pdev->dev);of_node_put (child); +of_dev_put (child_pdev);/* * Configure the USB port as device or host according to the static
Thanks
Hi Nishka
Quoted 50 lines · Nishka Dasgupta
The local variable child in the function
st_dwc3_probetakes the return value ofof_get_child_by_name, which gets a node and does not put it. If the function returns without releasing child, this could cause a memory error. Hence put child as soon as there is no more use for it. Also create a new label,err_node_put, just before labelundo_softreset; so thaterr_node_putputs child. In between initialisation of child and its first put, modify all statements that go toundo_softresetto now go toerr_node_putinstead, from where they can fall through toundo_softreset.
Issue found with Coccinelle.Signed-off-by Nishka Dasgupta <[email protected]>--- drivers/usb/dwc3/dwc3-st.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)drivers/usb/dwc3/dwc3-st.c +4
-3@@ -255,24 +255,25 @@ static int st_dwc3_probe(struct platform_device *pdev)if (!child) {dev_err (&pdev->dev,"failed to find dwc3 core node\n" ); ret = -ENODEV ; - goto undo_softreset; + goto err_node_put; }/* Allocate and initialize the core */ ret =of_platform_populate (node,NULL ,NULL , dev);if (ret) {dev_err (dev,"failed to add dwc3 core\n" ); - goto undo_softreset; + goto err_node_put; } child_pdev =of_find_device_by_node (child);if (!child_pdev) {dev_err (dev,"failed to find dwc3 core device\n" ); ret = -ENODEV ; - goto undo_softreset; + goto err_node_put; } dwc3_data->dr_mode =usb_get_dr_mode (&child_pdev->dev); +of_node_put (child);/* * Configure the USB port as device or host according to the static
drivers/usb/dwc3/dwc3-st.c static int st_dwc3_probe(struct platform_device *pdev) · line 300
Quoted 8 lines
drivers/usb/dwc3/dwc3-st.c +2
-0@@ -292,6 +293,8 @@ static int st_dwc3_probe(struct platform_device *pdev)platform_set_drvdata (pdev, dwc3_data);return 0 ; +err_node_put: +of_node_put (child); undo_softreset:reset_control_assert (dwc3_data->rstc_rst); undo_powerdown:
Thanks