tcm_alloc() runs when a TCM function is symlinked into a gadget configuration. It searches tpg_instances[] for the function instance and copies the TPG pointer into fu->tpg at that point, under tpg_instances_lock, and fu->tpg is never updated afterwards. The function and the TPG can be set up in either order. If the function is linked into a configuration before the TPG directory is created, fu->tpg stays NULL. Creating and enabling the TPG afterwards lets tcm_bind() succeed, and the first USB request that reaches the target code dereferences the NULL fu->tpg. Likewise, if the TPG is removed (usbg_drop_tpg() detaches the gadget first) and a new one is created and enabled, the next bind keeps using the freed TPG. Store the TPG in struct f_tcm_opts instead: set it in usbg_make_tpg(), clear it in usbg_drop_tpg() and copy it into fu->tpg in tcm_bind(). opts->tpg cannot change while the function is bound: tcm_bind() only runs for an enabled TPG, and usbg_drop_tpg() detaches the gadget before clearing it. No extra locking or reference counting is needed. With fu->tpg no longer set in tcm_alloc(), the tpg_instances[] lookup there serves no purpose: f->fi already identifies the instance for the item's whole lifetime. Drop it, and with it the tpg_instances_lock acquisition. config_usb_cfg_link() calls tcm_alloc() with gi->lock held, so this also removes the gi->lock -> tpg_instances_lock edge from the following lockdep report: WARNING: possible circular locking dependency detected task is trying to acquire lock: (tpg_instances_lock){+.+.}-{4:4}, at: tcm_alloc but task is already holding lock: (&gi->lock){+.+.}-{4:4}, at: config_usb_cfg_link the existing dependency chain (in reverse order) is: -> #2 (&gi->lock): config_usb_cfg_link configfs_symlink -> #1 (&sb->s_type->i_mutex_key#24): configfs_depend_item_unlocked usbg_make_tpg target_fabric_make_tpg configfs_mkdir -> #0 (tpg_instances_lock): tcm_alloc usb_get_function config_usb_cfg_link configfs_symlink Fixes: 4bb8548df632 ("usb: gadget: f_tcm: add configfs support") Cc: stable@vger.kernel.org Reported-by: syzbot+2799146a53c356f8017d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=2799146a53c356f8017d Link: https://syzkaller.appspot.com/ai_job?id=51e1f189-6d91-4321-a6ba-07d4eb3cd8cd Signed-off-by: Slawomir Stepien --- v3: rebased on usb-testing, where patches 1 and 2 of v2 and the g_target removal are already applied. No functional change. v2: https://lore.kernel.org/all/20261123142513.204652-4-sst@poczta.fm/ drivers/usb/gadget/function/f_tcm.c | 23 ++++++++--------------- drivers/usb/gadget/function/u_tcm.h | 6 ++++++ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c index a3c4b62b3ad0..72acd2d998ec 100644 --- a/drivers/usb/gadget/function/f_tcm.c +++ b/drivers/usb/gadget/function/f_tcm.c @@ -1730,6 +1730,7 @@ static struct se_portal_group *usbg_make_tpg(struct se_wwn *wwn, tpg_instances[i].tpg = tpg; tpg->fi = tpg_instances[i].func_inst; + opts->tpg = tpg; mutex_unlock(&tpg_instances_lock); return &tpg->se_tpg; @@ -1770,6 +1771,7 @@ static void usbg_drop_tpg(struct se_portal_group *se_tpg) tpg_instances[i].tpg = NULL; opts = container_of(tpg_instances[i].func_inst, struct f_tcm_opts, func_inst); + opts->tpg = NULL; configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item); } mutex_unlock(&tpg_instances_lock); @@ -2306,6 +2308,11 @@ static int tcm_bind(struct usb_configuration *c, struct usb_function *f) mutex_unlock(&opts->dep_lock); return -ENODEV; } + /* + * tcm_bind() only runs for an enabled TPG, and usbg_drop_tpg() + * detaches the gadget before clearing opts->tpg. + */ + fu->tpg = opts->tpg; mutex_unlock(&opts->dep_lock); us = usb_gstrings_attach(c->cdev, tcm_strings, ARRAY_SIZE(tcm_us_strings)); @@ -2710,22 +2717,10 @@ static void tcm_unbind(struct usb_configuration *c, struct usb_function *f) static struct usb_function *tcm_alloc(struct usb_function_instance *fi) { struct f_uas *fu; - unsigned i; - - mutex_lock(&tpg_instances_lock); - for (i = 0; i < TPG_INSTANCES; ++i) - if (tpg_instances[i].func_inst == fi) - break; - if (i == TPG_INSTANCES) { - mutex_unlock(&tpg_instances_lock); - return ERR_PTR(-ENODEV); - } fu = kzalloc_obj(*fu); - if (!fu) { - mutex_unlock(&tpg_instances_lock); + if (!fu) return ERR_PTR(-ENOMEM); - } fu->function.name = "Target Function"; fu->function.bind = tcm_bind; @@ -2735,12 +2730,10 @@ static struct usb_function *tcm_alloc(struct usb_function_instance *fi) fu->function.setup = tcm_setup; fu->function.disable = tcm_disable; fu->function.free_func = tcm_free; - fu->tpg = tpg_instances[i].tpg; INIT_WORK(&fu->delayed_set_alt, tcm_delayed_set_alt); spin_lock_init(&fu->delayed_set_alt_lock); hash_init(fu->stream_hash); - mutex_unlock(&tpg_instances_lock); return &fu->function; } diff --git a/drivers/usb/gadget/function/u_tcm.h b/drivers/usb/gadget/function/u_tcm.h index cb3b99f76aa0..c882acba8d3d 100644 --- a/drivers/usb/gadget/function/u_tcm.h +++ b/drivers/usb/gadget/function/u_tcm.h @@ -15,14 +15,20 @@ #include +struct usbg_tpg; + /** * @dep_lock: protects @can_attach. * @ready: true once the function instance has been given a name. * @can_attach: true a function can be bound to gadget + * @tpg: target portal group associated with this function instance. Set + * when the TPG is created, before it can be enabled, and cleared after it + * has been detached, so it never changes while the function is bound. * */ struct f_tcm_opts { struct usb_function_instance func_inst; + struct usbg_tpg *tpg; struct mutex dep_lock; bool ready; bool can_attach; base-commit: 7018748a0ac67b08688dee4b26d226952346d38e -- 2.56.1