phy: rcar-gen3-usb2: cancel OTG work on unbind to avoid use-after-free
Subsystems (4)
PATCH v2 1/1 phy: rcar-gen3-usb2: cancel OTG work on unbind to avoid use-after-free
From: Hyunjung Ko <[email protected]>
The OTG role/VBUS interrupt (rcar_gen3_phy_usb2_irq) and the role sysfs attribute schedule channel->work (rcar_gen3_phy_usb2_work), whose handler dereferences the channel (ch->extcon). The channel is devm-allocated, so it is freed at unbind, but nothing cancels channel->work:rcar_gen3_phy_usb2_remove() only drops the role sysfs file. A work scheduled just before unbind can run after the channel has been freed, a use-after-free.
Cancel the work via devm, registered before the interrupt is requested so that on unbind devm frees the interrupt first (no new work can be scheduled) and then runs the cancel action before the channel is freed.
---
Changes in v2 (thanks to the Sashiko AI review):
- correct the Fixes: tag to c14f8a4032ef, which introduced channel->work and rcar_gen3_phy_usb2_work(), rather than 2b38543c8db1 which only added synchronous extcon handling
drivers/phy/renesas/phy-rcar-gen3-usb2.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
drivers/phy/renesas/phy-rcar-gen3-usb2.c +18 -0
@@ -1171,6 +1171,13 @@ static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *chareturn rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable (channel, enable); } +static void rcar_gen3_phy_usb2_cancel_work (void *data) +{ +struct rcar_gen3_chan *channel = data; + +cancel_work_sync (&channel->work); +} +static int rcar_gen3_phy_usb2_probe (struct platform_device *pdev) {struct device *dev = &pdev->dev;
@@ -1277,6 +1284,17 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)return irq; }else if (irq >0 ) {INIT_WORK (&channel->work, rcar_gen3_phy_usb2_work); +/* +* Registered before the IRQ so that, on unbind, devm frees the +* IRQ first (no more work can be scheduled) and then cancels any +* work still pending before the channel is freed. +*/ + ret =devm_add_action_or_reset (dev, + rcar_gen3_phy_usb2_cancel_work, + channel); +if (ret <0 ) +return ret; + ret =devm_request_irq (dev, irq, rcar_gen3_phy_usb2_irq,IRQF_SHARED ,dev_name (dev), channel);if (ret <0 )
Signature · 1 line
2.43.0
Hi hyunjungg,
Thanks for the patch.
Quoted 48 lines
-----Original Message-----
From: hyunjungg <[email protected]>
Sent: 09 November 2026 11:31
Subject: [PATCH v2] phy: rcar-gen3-usb2: cancel OTG work on unbind to avoid use-after-freeFrom: Hyunjung Ko <[email protected]>
The OTG role/VBUS interrupt (
rcar_gen3_phy_usb2_irq) and the role sysfs attribute schedule channel-Quoted 1 line
work (
rcar_gen3_phy_usb2_work), whose handler dereferences the channel (ch->extcon). The channel isdevm-allocated, so it is freed at unbind, but nothing cancels
channel->work:rcar_gen3_phy_usb2_remove()only drops the role sysfs file. A work scheduled just before unbind can run after the channel has been freed, a use-after-free.Cancel the work via devm, registered before the interrupt is requested so that on unbind devm frees the interrupt first (no new work can be
scheduled) and then runs the cancel action before the channel is freed.Fixes c14f8a4032ef ("phy: rcar-gen3-usb2: fix mutex_lock calling in interrupt")Signed-off-by Hyunjung Ko <[email protected]>---
Changes in v2 (thanks to the Sashiko AI review):
- correct the Fixes: tag toc14f8a4032ef, which introducedchannel->workandrcar_gen3_phy_usb2_work(), rather than2b38543c8db1which only added synchronous extcon handlingdrivers/phy/renesas/phy-rcar-gen3-usb2.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)drivers/phy/renesas/phy-rcar-gen3-usb2.c +6
-0@@ -1171,6 +1171,13 @@ static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *chareturn rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable (channel, enable); } +static void rcar_gen3_phy_usb2_cancel_work (void *data) { +struct rcar_gen3_chan *channel = data; + +cancel_work_sync (&channel->work); +} +static int rcar_gen3_phy_usb2_probe (struct platform_device *pdev) {struct device *dev = &pdev->dev;@@ -1277,6 +1284,17 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)return irq; }else if (irq >0 ) {INIT_WORK (&channel->work, rcar_gen3_phy_usb2_work);
drivers/phy/renesas/phy-rcar-gen3-usb2.c static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev) · line 1296
Quoted 10 lines
drivers/phy/renesas/phy-rcar-gen3-usb2.c +10
-0+/* +* Registered before the IRQ so that, on unbind, devm frees the +* IRQ first (no more work can be scheduled) and then cancels any +* work still pending before the channel is freed. +*/ + ret =devm_add_action_or_reset (dev, + rcar_gen3_phy_usb2_cancel_work, + channel); +if (ret <0 ) +return ret;
I have a question,
What happens if this return error? Is it ok to call cancel_work_sync() before scheduling it?
Cheers,
Biju
Quoted 7 lines
drivers/phy/renesas/phy-rcar-gen3-usb2.c +1
-1+ ret =devm_request_irq (dev, irq, rcar_gen3_phy_usb2_irq,IRQF_SHARED ,dev_name (dev), channel);if (ret <0 ) --2.43.0
Hi Biju,
Thanks for the review :)
devm_add_action_or_reset() runs the action once immediately if it
fails to register it, and then returns the error. So on failure rcar_gen3_phy_usb2_cancel_work() is called right away, i.e. cancel_work_sync(&channel->work).
That is safe here: channel->work has already been INIT_WORK()'d on
the line above, and the IRQ that schedules it (rcar_gen3_phy_usb2_irq) is only requested after this call succeeds, so at that point the work has never been queued. cancel_work_sync() on an initialised-but-never-queued work is a no-op -- it just returns false. (Unlike a timer, a work_struct has no uninitialised-object check on the cancel path once INIT_WORK() has run.)
So both paths are fine: either devres registers the cancel and runs
it on unbind, or registration fails, the cancel runs once immediately (a harmless no-op), and probe returns the error.
Thanks,
Hyunjung
2026년 11월 9일 (월) 오후 8:45, Biju Das <[email protected]>님이 작성:
Quoted 80 lines
Hi hyunjungg,
Thanks for the patch.
Quoted 48 lines
-----Original Message-----
From: hyunjungg <[email protected]>
Sent: 09 November 2026 11:31
Subject: [PATCH v2] phy: rcar-gen3-usb2: cancel OTG work on unbind to avoid use-after-freeFrom: Hyunjung Ko <[email protected]>
The OTG role/VBUS interrupt (
rcar_gen3_phy_usb2_irq) and the role sysfs attribute schedule channel-Quoted 1 line
work (
rcar_gen3_phy_usb2_work), whose handler dereferences the channel (ch->extcon). The channel isdevm-allocated, so it is freed at unbind, but nothing cancels
channel->work:rcar_gen3_phy_usb2_remove()only drops the role sysfs file. A work scheduled just before unbind can run after the channel has been freed, a use-after-free.Cancel the work via devm, registered before the interrupt is requested so that on unbind devm frees the interrupt first (no new work can be
scheduled) and then runs the cancel action before the channel is freed.Fixes c14f8a4032ef ("phy: rcar-gen3-usb2: fix mutex_lock calling in interrupt")Signed-off-by Hyunjung Ko <[email protected]>---
Changes in v2 (thanks to the Sashiko AI review):
- correct the Fixes: tag toc14f8a4032ef, which introducedchannel->workandrcar_gen3_phy_usb2_work(), rather than2b38543c8db1which only added synchronous extcon handlingdrivers/phy/renesas/phy-rcar-gen3-usb2.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)drivers/phy/renesas/phy-rcar-gen3-usb2.c +6
-0@@ -1171,6 +1171,13 @@ static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *chareturn rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable (channel, enable); } +static void rcar_gen3_phy_usb2_cancel_work (void *data) { +struct rcar_gen3_chan *channel = data; + +cancel_work_sync (&channel->work); +} +static int rcar_gen3_phy_usb2_probe (struct platform_device *pdev) {struct device *dev = &pdev->dev;@@ -1277,6 +1284,17 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)return irq; }else if (irq >0 ) {INIT_WORK (&channel->work, rcar_gen3_phy_usb2_work);drivers/phy/renesas/phy-rcar-gen3-usb2.c static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev) · line 1296
Quoted 10 lines
drivers/phy/renesas/phy-rcar-gen3-usb2.c +10
-0+/* +* Registered before the IRQ so that, on unbind, devm frees the +* IRQ first (no more work can be scheduled) and then cancels any +* work still pending before the channel is freed. +*/ + ret =devm_add_action_or_reset (dev, + rcar_gen3_phy_usb2_cancel_work, + channel); +if (ret <0 ) +return ret;I have a question,
What happens if this return error? Is it ok to call
cancel_work_sync()before scheduling it?Cheers,
BijuQuoted 7 lines
drivers/phy/renesas/phy-rcar-gen3-usb2.c +1
-1+ ret =devm_request_irq (dev, irq, rcar_gen3_phy_usb2_irq,IRQF_SHARED ,dev_name (dev), channel);if (ret <0 ) --2.43.0
Manivannan Sadhasivam
reviewerQuoted 45 lines · hyunjungg
From: Hyunjung Ko <[email protected]>
The OTG role/VBUS interrupt (
rcar_gen3_phy_usb2_irq) and the role sysfs attribute schedulechannel->work(rcar_gen3_phy_usb2_work), whose handler dereferences the channel (ch->extcon). The channel is devm-allocated, so it is freed at unbind, but nothing cancelschannel->work:rcar_gen3_phy_usb2_remove()only drops the role sysfs file. A work scheduled just before unbind can run after the channel has been freed, a use-after-free.Cancel the work via devm, registered before the interrupt is requested so that on unbind devm frees the interrupt first (no new work can be scheduled) and then runs the cancel action before the channel is freed.
Fixes c14f8a4032ef ("phy: rcar-gen3-usb2: fix mutex_lock calling in interrupt")Signed-off-by Hyunjung Ko <[email protected]>---
Changes in v2 (thanks to the Sashiko AI review):
- correct the Fixes: tag toc14f8a4032ef, which introducedchannel->workandrcar_gen3_phy_usb2_work(), rather than2b38543c8db1which only added synchronous extcon handlingdrivers/phy/renesas/phy-rcar-gen3-usb2.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)drivers/phy/renesas/phy-rcar-gen3-usb2.c +7
-0@@ -1171,6 +1171,13 @@ static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *chareturn rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable (channel, enable); } +static void rcar_gen3_phy_usb2_cancel_work (void *data) +{ +struct rcar_gen3_chan *channel = data; + +cancel_work_sync (&channel->work); +} +static int rcar_gen3_phy_usb2_probe (struct platform_device *pdev) {struct device *dev = &pdev->dev;@@ -1277,6 +1284,17 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)return irq;
drivers/phy/renesas/phy-rcar-gen3-usb2.c static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev) · line 1294
Quoted 10 lines
drivers/phy/renesas/phy-rcar-gen3-usb2.c +8
-0}else if (irq >0 ) {INIT_WORK (&channel->work, rcar_gen3_phy_usb2_work); +/* +* Registered before the IRQ so that, on unbind, devm frees the +* IRQ first (no more work can be scheduled) and then cancels any +* work still pending before the channel is freed. +*/ + ret =devm_add_action_or_reset (dev, + rcar_gen3_phy_usb2_cancel_work, + channel);
Please use devm_work_autocancel().
- Mani