All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Rosen Penev" <rosenp@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex
Date: Sun, 06 Dec 2026 03:07:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-252106@kernel.org> (raw)
In-Reply-To: <e9975a8ccbb9de751a58992b60e943d100020b45.1796524906.git.rosenp@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] usb: apple-mfi-fastcharge: NULL dereference in mfi_fc_resume()
  during probe
- [Medium] usb: apple-mfi-fastcharge: unbind races mfi_fc_resume() via
  devm cleanup
- [Low] usb: apple-mfi-fastcharge: missing Fixes tag for charge_type
  race fix
--

commit b8bcac8cd75839545033e3e164816c2f61377563
Author: Rosen Penev <rosenp@gmail.com>

> usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex
>
> Concurrent writes to charge_type from userspace can race: both
> threads may read the old value, send their USB command, and write
> the software state, leaving the cached charge_type mismatched with
> the actual hardware state.
>
> Add a per-device mutex and hold it across the USB control transfer
> in set_charge_type, so the cached value always matches the last
> current sent to the device. Update the cache only after the transfer
> succeeds, which removes the need to roll it back on error. Take the
> same lock in get_property and in the resume path. Initialize it with
> devm_mutex_init() and use guard(mutex) for scoped locking.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

[Severity: Low]
This fixes a concurrency bug on mfi->charge_type that has been present
since the driver was added.  Should this include:

  Fixes: 249fa8217b84 ("USB: Add driver to control USB fast charge for iOS devices")

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index 85cf970e5f5eb..fb4b975a62e94 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c

[ ... ]

> @@ -176,11 +179,16 @@ static int mfi_fc_probe(struct usb_device *udev)
>  	struct power_supply_desc *battery_desc;
>  	struct power_supply *battery;
>  	struct mfi_device *mfi;
> +	int ret;
>  
>  	mfi = devm_kzalloc_obj(&udev->dev, *mfi);
>  	if (!mfi)
>  		return -ENOMEM;
>  
> +	ret = devm_mutex_init(&udev->dev, &mfi->lock);
> +	if (ret)
> +		return ret;

[Severity: Medium]
Can driver unbind race with mfi_fc_resume() and trigger a use-after-free
on mfi->lock?

During unbind while the device remains attached (such as sysfs unbind or
rmmod), usb_unbind_device() drops the runtime PM usage count so udev can
runtime-suspend while udev->dev.driver is still set.  A concurrent
lockless runtime resume, such as writing a negative delay to
power/autosuspend, can then observe udev->dev.driver before
device_unbind_cleanup() clears it:

CPU0 (unbind)                         CPU1 (autosuspend_store)
__device_release_driver()
  usb_unbind_device()
    usb_autosuspend_device()
      ... -> usb_runtime_suspend()
                                      pm_runtime_set_autosuspend_delay()
                                        rpm_resume() -> usb_resume_device()
                                          reads udev->dev.driver
                                          usb_generic_driver_resume()
  device_unbind_cleanup()
    devres_release_all()
      mutex_destroy(&mfi->lock)
      kfree(mfi)
                                          mfi_fc_resume()
                                            guard(mutex)(&mfi->lock)
                                              <-- use-after-free
    device_set_driver(dev, NULL)
    dev_set_drvdata(dev, NULL)

Because devres_release_all() destroys mfi->lock and frees mfi before
dev->driver and drvdata are cleared and without a pm_runtime_barrier(),
mfi_fc_resume() can lock freed memory, dereference a NULL mfi, or have
mfi->lock destroyed while sleeping in usb_control_msg().

[ ... ]

> @@ -212,6 +220,8 @@ static int mfi_fc_resume(struct usb_device *udev, pm_message_t message)
>  {
>  	struct mfi_device *mfi = dev_get_drvdata(&udev->dev);
>  
> +	guard(mutex)(&mfi->lock);

[Severity: High]
Will guard(mutex)(&mfi->lock) dereference a NULL mfi when probing a
runtime-suspended device?

really_probe() binds udev->dev.driver via device_set_driver() before
calling usb_probe_device(), which calls usb_autoresume_device() (both
directly and via usb_generic_driver_probe() -> usb_set_configuration())
before mfi_fc_probe() runs.  If the device is runtime-suspended at probe
time, usb_resume_device() invokes mfi_fc_resume() while
dev_get_drvdata(&udev->dev) is still NULL, causing a NULL pointer
dereference in mutex_lock().

Returning 0 early when !mfi avoids the crash.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=8

  reply	other threads:[~2026-12-06  3:07 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
2026-12-06  2:44 ` [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
2026-12-06  3:00   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 02/12] usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get() Rosen Penev
2026-12-06  2:44 ` [PATCH 03/12] usb: apple-mfi-fastcharge: Fix usb_control_msg() return check and timeout Rosen Penev
2026-12-06  2:44 ` [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY Rosen Penev
2026-12-06  3:04   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 05/12] usb: apple-mfi-fastcharge: Constify power_supply_property array Rosen Penev
2026-12-06  2:44 ` [PATCH 06/12] usb: apple-mfi-fastcharge: Use topology-based device name Rosen Penev
2026-12-06  2:44 ` [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume Rosen Penev
2026-12-06  3:05   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
2026-12-06  3:07   ` sashiko-bot [this message]
2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
2026-12-06  3:05   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 10/12] usb: apple-mfi-fastcharge: Name the vendor power request Rosen Penev
2026-12-06  2:44 ` [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy Rosen Penev
2026-12-06  2:58   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev
2026-12-06  3:06   ` sashiko-bot
2026-12-06  3:07 ` [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-252106@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=rosenp@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.