usb: gadget: f_ecm: serialize notification state with a spinlock
PATCH v2 1/1 usb: gadget: f_ecm: serialize notification state with a spinlock
ecm_do_notify() guards re-entry only with an atomic_read() of notify_count, then fills ecm->notify_req, does atomic_inc() and usb_ep_queue(). This check-fill-inc-queue sequence is not atomic and runs from three contexts that share no lock: ecm_notify_complete(), ecm_open()/ecm_close() (netdev up/down via u_ether) and ecm_set_alt().
When the completion handler decrements notify_count to 0 and re-enters ecm_do_notify(), a concurrent ecm_close()/ecm_set_alt() can observe notify_count == 0 in the window between the atomic_dec() and the next atomic_inc(), and queue the same ecm->notify_req a second time. The request is then added to the endpoint queue twice (list_add corruption), and notify_state is updated concurrently.
struct f_ecm has carried a FIXME about this missing locking. The sibling f_ncm already serializes the identical state machine with a per-instance spinlock. Do the same in f_ecm: take ecm->lock across the is_open update and ecm_notify() in open/close/set_alt and across the state update in ecm_notify_complete(), dropping it only around usb_ep_queue().
---
v2:
- Take ecm->lock with guard()/scoped_guard() in ecm_open(), ecm_close(), ecm_set_alt() and ecm_notify_complete() instead of explicit spin_lock()/spin_unlock() pairs (Greg Kroah-Hartman). The lock is still dropped around usb_ep_queue() in ecm_do_notify(), as f_ncm does, because the completion can run before usb_ep_queue() returns. No change to what is protected.
- Sent as a new thread.
v1: https://lore.kernel.org/all/[email protected]/
drivers/usb/gadget/function/f_ecm.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/usb/gadget/function/f_ecm.c +16 -4
@@ -59,9 +59,8 @@ struct f_ecm {atomic_t notify_count;bool is_open; -/* FIXME is_open needs some irq-ish locking -* ... possibly the same as port.ioport -*/ +/* protects the notification state machine */ +spinlock_t lock; };static inline struct f_ecm *func_to_ecm (struct usb_function *f)
@@ -414,7 +413,11 @@ static void ecm_do_notify(struct f_ecm * event->wIndex =cpu_to_le16 (ecm->ctrl_id);atomic_inc (&ecm->notify_count); + +/* ecm_notify_complete() can run before usb_ep_queue() returns */ +spin_unlock (&ecm->lock); status =usb_ep_queue (ecm->notify, req,GFP_ATOMIC ); +spin_lock (&ecm->lock);if (status <0 ) {atomic_dec (&ecm->notify_count);DBG (cdev,"notify --> %d\n" , status);
@@ -438,6 +441,8 @@ static void ecm_notify_complete(struct ustruct usb_composite_dev *cdev = ecm->port.func.config->cdev;struct usb_cdc_notification *event = req->buf; +guard (spinlock)(&ecm->lock); +switch (req->status) {case 0 :/* no fault */
@@ -588,7 +593,8 @@ static int ecm_set_alt(struct usb_functi* follow another (if the first is in flight), and instead * just guarantee that a speed notification is always sent. */ -ecm_notify (ecm); + scoped_guard(spinlock, &ecm->lock) +ecm_notify (ecm); }else goto fail;
@@ -653,6 +659,8 @@ static void ecm_open(struct gether *gethDBG (ecm->port.func.config->cdev,"%s\n" , __func__); +guard (spinlock)(&ecm->lock); + ecm->is_open = true;ecm_notify (ecm); }
@@ -663,6 +671,8 @@ static void ecm_close(struct gether *getDBG (ecm->port.func.config->cdev,"%s\n" , __func__); +guard (spinlock)(&ecm->lock); + ecm->is_open = false;ecm_notify (ecm); }
@@ -942,6 +952,8 @@ static struct usb_function *ecm_alloc(stif (!ecm)return ERR_PTR (-ENOMEM ); +spin_lock_init (&ecm->lock); + opts = container_of(fi, struct f_ecm_opts, func_inst);mutex_lock (&opts->lock); opts->refcnt++;
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] usb: gadget: f_ecm: missing Fixes trailer for notification race fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] usb: gadget: f_ecm: missing Fixes trailer for notification race fix
--
commit 5f8ac3914708382417c9469276b098d970817bbf
Author: Yu Zhang <[email protected]>
Quoted 15 lines
usb: gadget:
f_ecm: serialize notification state with a spinlock
ecm_do_notify()guards re-entry only with anatomic_read()ofnotify_count, then fillsecm->notify_req, doesatomic_inc()andusb_ep_queue(). This check-fill-inc-queue sequence is not atomic and runs from three contexts that share no lock:ecm_notify_complete(), ecm_open()/ecm_close() (netdev up/down viau_ether) andecm_set_alt().When the completion handler decrements
notify_countto 0 and re-entersecm_do_notify(), a concurrent ecm_close()/ecm_set_alt() can observenotify_count== 0 in the window between theatomic_dec()and the nextatomic_inc(), and queue the sameecm->notify_reqa second time. The request is then added to the endpoint queue twice (list_addcorruption), andnotify_stateis updated concurrently.
Quoted 7 lines
struct
f_ecmhas carried a FIXME about this missing locking. The siblingf_ncmalready serializes the identical state machine with a per-instance spinlock. Do the same inf_ecm: takeecm->lockacross theis_openupdate andecm_notify()in open/close/set_alt and across the state update inecm_notify_complete(), dropping it only aroundusb_ep_queue().Signed-off-by Yu Zhang <[email protected]>
[Severity: Low]
This fixes a concurrency bug in the notification state machine present since f_ecm was split out. Should this include a Fixes tag for da741b8c56d6 ("usb ethernet gadget: split CDC Ethernet function")?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1