Unbinding the tcpci_maxim driver triggers a use-after-free in regulator_disable() if the driver has acquired the vbus regulator: Unable to handle kernel paging request at virtual address 006b6b6b6b6b6bb7 pc : regulator_lock_recursive+0x20/0x218 lr : regulator_lock_dependent+0x198/0x2c0 Call trace: regulator_lock_recursive+0x20/0x218 (P) regulator_lock_dependent+0x198/0x2c0 regulator_disable+0x4c/0x100 max_tcpci_set_vbus+0x70/0x108 [tcpci_maxim] tcpci_set_vbus+0x44/0x100 [tcpci] tcpm_reset_port+0x13c/0x288 [tcpm] tcpm_unregister_port+0x70/0x120 [tcpm] tcpci_unregister_port+0x24/0x68 [tcpci] max_tcpci_unregister_tcpci_port+0x18/0x30 [tcpci_maxim] devm_action_release+0x1c/0x30 max_tcpci_set_vbus() obtains chip->vbus_reg lazily via devm_regulator_get_exclusive(). When the regulator is acquired after max_tcpci_probe() has queued max_tcpci_unregister_tcpci_port() with devm_add_action_or_reset(), devres releases resources in reverse order and frees the regulator handle before max_tcpci_unregister_tcpci_port() unregisters the port and max_tcpci_set_vbus() accesses chip->vbus_reg. Switch to regulator_get_exclusive() and register a devres cleanup action for regulator_put() before tcpci_register_port(), so the port is unregistered and tcpm_reset_port() disables VBUS before the regulator handle is released. Fixes: 0c8935642cb0 ("usb: typec: tcpm/tcpci_maxim: deprecate WAR for setting charger mode") Cc: stable@vger.kernel.org Acked-by: Heikki Krogerus Signed-off-by: André Draszik --- drivers/usb/typec/tcpm/tcpci_maxim_core.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpci_maxim_core.c b/drivers/usb/typec/tcpm/tcpci_maxim_core.c index 2bb306b6b1f9..2e6a8906fb32 100644 --- a/drivers/usb/typec/tcpm/tcpci_maxim_core.c +++ b/drivers/usb/typec/tcpm/tcpci_maxim_core.c @@ -209,8 +209,7 @@ static void process_rx(struct max_tcpci_chip *chip, u16 status) static int get_vbus_regulator_handle(struct max_tcpci_chip *chip) { if (IS_ERR_OR_NULL(chip->vbus_reg)) { - chip->vbus_reg = devm_regulator_get_exclusive(chip->dev, - "vbus"); + chip->vbus_reg = regulator_get_exclusive(chip->dev, "vbus"); if (IS_ERR_OR_NULL(chip->vbus_reg)) { dev_err(chip->dev, "Failed to get vbus regulator handle\n"); @@ -221,6 +220,14 @@ static int get_vbus_regulator_handle(struct max_tcpci_chip *chip) return 0; } +static void max_tcpci_put_vbus_regulator(void *data) +{ + struct max_tcpci_chip *chip = data; + + if (!IS_ERR_OR_NULL(chip->vbus_reg)) + regulator_put(chip->vbus_reg); +} + static int max_tcpci_set_vbus(struct tcpci *tcpci, struct tcpci_data *tdata, bool source, bool sink) { struct max_tcpci_chip *chip = tdata_to_max_tcpci(tdata); @@ -514,6 +521,11 @@ static int max_tcpci_probe(struct i2c_client *client) chip->data.cable_comm_capable = true; chip->data.attempt_vconn_swap_discovery = max_tcpci_attempt_vconn_swap_discovery; + ret = devm_add_action_or_reset(&client->dev, + max_tcpci_put_vbus_regulator, chip); + if (ret) + return ret; + max_tcpci_init_regs(chip); chip->tcpci = tcpci_register_port(chip->dev, &chip->data); if (IS_ERR(chip->tcpci)) -- 2.56.1.839.g22a8290db9-goog