ptp: ocp: fix resource freeing order
Vadim Fedorenko
maintainerPATCH 1/1 ptp: ocp: fix resource freeing order
Commit a60fc3294a37 ("ptp: rework ptp_clock_unregister() to disable events") added a call to ptp_disable_all_events() which changes the configuration of pins if they support EXTTS events. In ptp_ocp_detach() pins resources are freed before ptp_clock_unregister() and it leads to use-after-free during driver removal. Fix it by changing the order of free/unregister calls.
--- drivers/ptp/ptp_ocp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/ptp/ptp_ocp.c +3 -3
@@ -4558,6 +4558,9 @@ ptp_ocp_detach(struct ptp_ocp *bp)ptp_ocp_detach_sysfs (bp);ptp_ocp_attr_group_del (bp);timer_delete_sync (&bp->watchdog); +if (bp->ptp) +ptp_clock_unregister (bp->ptp); +kfree (bp->ptp_info.pin_config);ptp_ocp_unregister_ext (bp->ts0);ptp_ocp_unregister_ext (bp->ts1);ptp_ocp_unregister_ext (bp->ts2);
@@ -4575,9 +4578,6 @@ ptp_ocp_detach(struct ptp_ocp *bp)clk_hw_unregister_fixed_rate (bp->i2c_clk);if (bp->n_irqs)pci_free_irq_vectors (bp->pdev); -if (bp->ptp) -ptp_clock_unregister (bp->ptp); -kfree (bp->ptp_info.pin_config);device_unregister (&bp->dev); }
Signature · 1 line
2.47.3
Quoted 40 lines · Vadim Fedorenko
Commit
a60fc3294a37("ptp: reworkptp_clock_unregister()to disable events") added a call toptp_disable_all_events()which changes the configuration of pins if they support EXTTS events. Inptp_ocp_detach()pins resources are freed beforeptp_clock_unregister()and it leads to use-after-free during driver removal. Fix it by changing the order of free/unregister calls.Fixes a60fc3294a37 ("ptp: rework ptp_clock_unregister() to disable events")Signed-off-by Vadim Fedorenko <[email protected]>--- drivers/ptp/ptp_ocp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/ptp/ptp_ocp.c +3
-3@@ -4558,6 +4558,9 @@ ptp_ocp_detach(struct ptp_ocp *bp)ptp_ocp_detach_sysfs (bp);ptp_ocp_attr_group_del (bp);timer_delete_sync (&bp->watchdog); +if (bp->ptp) +ptp_clock_unregister (bp->ptp); +kfree (bp->ptp_info.pin_config);ptp_ocp_unregister_ext (bp->ts0);ptp_ocp_unregister_ext (bp->ts1);ptp_ocp_unregister_ext (bp->ts2);@@ -4575,9 +4578,6 @@ ptp_ocp_detach(struct ptp_ocp *bp)clk_hw_unregister_fixed_rate (bp->i2c_clk);if (bp->n_irqs)pci_free_irq_vectors (bp->pdev); -if (bp->ptp) -ptp_clock_unregister (bp->ptp); -kfree (bp->ptp_info.pin_config);device_unregister (&bp->dev); }Signature · 1 line
2.47.3
The code wasn't safe even before the patch, since user space could have emitted a PTP ioctl that passed into the driver in between the ptp_ocp_unregister_ext() calls and the subsequent ptp_clock_unregister() call. It's just that the patch you blame made it more obvious.
Quoted 38 lines · Vladimir Oltean
Quoted 37 lines · Vadim Fedorenko
Commit
a60fc3294a37("ptp: reworkptp_clock_unregister()to disable events") added a call toptp_disable_all_events()which changes the configuration of pins if they support EXTTS events. Inptp_ocp_detach()pins resources are freed beforeptp_clock_unregister()and it leads to use-after-free during driver removal. Fix it by changing the order of free/unregister calls.Fixes a60fc3294a37 ("ptp: rework ptp_clock_unregister() to disable events")Signed-off-by Vadim Fedorenko <[email protected]>--- drivers/ptp/ptp_ocp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/ptp/ptp_ocp.c +3
-3@@ -4558,6 +4558,9 @@ ptp_ocp_detach(struct ptp_ocp *bp)ptp_ocp_detach_sysfs (bp);ptp_ocp_attr_group_del (bp);timer_delete_sync (&bp->watchdog); +if (bp->ptp) +ptp_clock_unregister (bp->ptp); +kfree (bp->ptp_info.pin_config);ptp_ocp_unregister_ext (bp->ts0);ptp_ocp_unregister_ext (bp->ts1);ptp_ocp_unregister_ext (bp->ts2);@@ -4575,9 +4578,6 @@ ptp_ocp_detach(struct ptp_ocp *bp)clk_hw_unregister_fixed_rate (bp->i2c_clk);if (bp->n_irqs)pci_free_irq_vectors (bp->pdev); -if (bp->ptp) -ptp_clock_unregister (bp->ptp); -kfree (bp->ptp_info.pin_config);device_unregister (&bp->dev); }
Quoted 8 lines
Quoted 3 lines
Signature · 1 line
2.47.3The code wasn't safe even before the patch, since user space could have emitted a PTP ioctl that passed into the driver in between the
ptp_ocp_unregister_ext()calls and the subsequentptp_clock_unregister()call. It's just that the patch you blame made it more obvious.
Indeed.
We've been talking about "setup, then publish interfaces, unpublish interfaces, tear down" for decades. I remember it coming up at the Ottawa Linux Symposiums in the early 2000s when the driver model was in its infancy. Yet, here we are in 2026, and the same problem persists. :(
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!