From nobody Fri Oct 2 07:46:35 2026 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7E3032B10D for ; Tue, 4 Aug 2026 03:33:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785814427; cv=none; b=pcSjbmMw2SfVC0XkoqQZ18UcJCNk2b4d8U2r3rGcPTq5+3Y1OQeGq/0tUVX7mvYU2VbfodC8ay7QO+iGXELW/PhitDm0rLeDL10wsnrWxQEEuta9B2pILBoupLuJ0X5jTuJMKmc9ktRlIxAPmc/xSjcinxMhfeFkEFDfN0ZQbbI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785814427; c=relaxed/simple; bh=JJPT5uu44aQguJzoiNHoy/Ath8/gUc7Bf0ZjaZG+5GY=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=U56SPo54ZwsUNFJDjcIqwlyrpkY44d9Gm7zFBlh7MfsXjOMh7C1mqI7lW/rGykzxy5+CrffF/6Pf7N6vu9ccZ9RnLHD5+qYnuj6ZPmy/Z/GrFoqSWJvTkwnmTYEDoZYwQRVtL2zRU/ytmDA930Ik9XcVMtBZN5eFNIEwgKyTE18= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=F+ES+5hl; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F+ES+5hl" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-c9c26a5fb98so387403a12.0 for ; Mon, 03 Aug 2026 20:33:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785814425; x=1786419225; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=seZC1uxNZsDIE6HCcr0pbelK6/1aquebV/a2t6CIBO4=; b=F+ES+5hl3g5aRc5y40QhB+4mc314BXjM5dWtFkXtfv16J8XOPZmA8o27pioKF+NC7p udWp5jyB5Uo8lmLq/IkHVXbheq0827uqK9zczTqKb8w4X85FSFRRaeGNe/T72PUR/dSn Dlx1dandjn19hZ/nP9Mqd9RHHbSMqXDOg46cza9zTQCc98SaNWxEkwCOz/CLEET6lyp3 GJC9+Ex2XivuHYNk8daWlW1Xx1BBAI/GxqkuKnACmFVSILrweDTQ+lfO2IkJE/iPZsgr Wlz1rGGYejrIZZ5W6id5aVm4FzZnWK/lGlDAVPQSV2zQQ1rxJFNrDjkwn0BNPTtKcHNe 4XsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785814425; x=1786419225; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=seZC1uxNZsDIE6HCcr0pbelK6/1aquebV/a2t6CIBO4=; b=invfQxPxi5RONz5Itphd+aXz4NnJnDVxlRk+ptkJiPVJJhqS1+xyT7IkfNTbyDOqsc WznG/T5kLNASW/FV8X19jEemLX5ZpQtqGwBj9UGWO8vEvICDJhZoe6tqlFB2dzb/JAYa gbae9zb36ocwpvdpflJXqHIErDZDGOGfMcCDhvYeE4dfqoA2qU3YR/ERFM/5BZZUKkw0 Hf+JZtRI/oU/HrBaHw0+tLZrL2MLh2i+Ii+9UFLZoZOg7y5f0h/ItFDawkmXiNjXj+XR SckjnFHK76DUhOSlJvajYuJgTp814PQtLsRV1fftJbPRm6nSWkls4j+jV+idzB4rDjN8 /RqQ== X-Forwarded-Encrypted: i=1; AHgh+Rqdo5BmvJzBQz9c0D4T9BameyG+1xI60FtP+D/0v8KWKqHss2X8eLiYmgaatKgOG4uHAhmfKRsX1N+lNEs=@vger.kernel.org X-Gm-Message-State: AOJu0YxpUz4ejCcFs/nZDkc5UPtcR+vLaryidYKamUPiCB9AtHSgD1P9 9WkgpqYUUqRXkCJchU5kdn3A0LCV9RpC5+flWe6lgTvFVEbo+jKAdohP X-Gm-Gg: AR+sD12lCtjiWKEeg2gAlul+VHUL4mZxCBi1ITZC74alPZb+lrmRf31mQTN68s8DT/a FUKY2l6BfasaPXR9aEjqI7b14HqwXpJ8hKYAGgHqq1i0TwNBA8y3khMuQqc1/VfSeOtkJ5fz0dC M6Zwo/9zX6TymZEmqaqbyVfnGCTYt6bj7NeZzVr7ClZyXSS4j18t8ZTimRlMp5XkiUVFme+2noL JU8IItbA9Jp+QDwOdMgVIJCQrM6W/vhPHc4TIy52XbbXapsQ9m6MPO2I8cdWaKbyxerob7zZV9i WdrZkzY83dN1V96BFNMeFmE9JphinyehOnFyq67tkMf07I4QkwTVSFH5Or4bz8hNGp9o1kodMse 475n6ne8jF5VhZehQpGB6EcN1GF2hOhUDzw/hPnK44Zb0yWhXK+gLbZpnTdzkTdT+YoiGT5N6/x qGIwfK7avK0aKhSHF5axtsYSCctXFTMWHoDDJTg8GRn6eTAo4fqAmFS34WN2BGF9fJ5Sdq88ahK inU+mA754OplSJBAcaM0FVTKjPUPVjuT8ZF X-Received: by 2002:a05:6a21:6906:b0:3bf:b7e8:68ab with SMTP id adf61e73a8af0-3cb6c752d6dmr2124947637.2.1785814424915; Mon, 03 Aug 2026 20:33:44 -0700 (PDT) Received: from ML-GYSUBT565.ECARX.COM.CN ([101.47.164.95]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3153e04506csm46215078eec.17.2026.08.03.20.33.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 20:33:44 -0700 (PDT) From: Nguyen Quang Le Kien To: gregkh@linuxfoundation.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+098999e05b6b877c01b3@syzkaller.appspotmail.com, Nguyen Quang Le Kien Subject: [PATCH] usb: gadget: f_phonet: fix use-after-free in pn_bind Date: Tue, 4 Aug 2026 11:33:41 +0800 Message-Id: <20260804033341.2784711-1-khiemtranzo532001@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" phonet_free_inst() checks opts->bound to decide whether to call gphonet_cleanup() or free_netdev(). If opts->bound is false, free_netdev(opts->net) is called immediately. However, pn_bind() can race with phonet_free_inst() when a configfs entry is removed while binding is in progress. pn_bind() reads opts->bound and calls gphonet_set_gadget(opts->net, ...) before setting opts->bound =3D true. If phonet_free_inst() runs concurrently after the !opts->bound check in pn_bind() but before opts->bound is set, it will free opts->net, causing a use-after-free when pn_bind() subsequently writes to net->dev.parent via gphonet_set_gadget(). Fix this by adding a mutex to f_phonet_opts and holding it in both pn_bind() and phonet_free_inst() when accessing opts->bound and opts->net. This is consistent with how other gadget functions such as f_eem protect their opts->bound flag. Fixes: 00a2430ff07d ("usb: gadget: Gadget directory cleanup - group usb fun= ctions") Reported-by: syzbot+098999e05b6b877c01b3@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=3D098999e05b6b877c01b3 Signed-off-by: Nguyen Quang Le Kien --- drivers/usb/gadget/function/f_phonet.c | 16 ++++++++-------- drivers/usb/gadget/function/u_phonet.h | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/drivers/usb/gadget/function/f_phonet.c b/drivers/usb/gadget/fu= nction/f_phonet.c index b1ee9a7c2..b7eae14e8 100644 --- a/drivers/usb/gadget/function/f_phonet.c +++ b/drivers/usb/gadget/function/f_phonet.c @@ -499,20 +499,17 @@ static int pn_bind(struct usb_configuration *c, struc= t usb_function *f) =20 phonet_opts =3D container_of(f->fi, struct f_phonet_opts, func_inst); =20 - /* - * in drivers/usb/gadget/configfs.c:configfs_composite_bind() - * configurations are bound in sequence with list_for_each_entry, - * in each configuration its functions are bound in sequence - * with list_for_each_entry, so we assume no race condition - * with regard to phonet_opts->bound access - */ + mutex_lock(&phonet_opts->lock); if (!phonet_opts->bound) { gphonet_set_gadget(phonet_opts->net, gadget); status =3D gphonet_register_netdev(phonet_opts->net); - if (status) + if (status) { + mutex_unlock(&phonet_opts->lock); return status; + } phonet_opts->bound =3D true; } + mutex_unlock(&phonet_opts->lock); =20 /* Reserve interface IDs */ status =3D usb_interface_id(c, f); @@ -621,10 +618,12 @@ static void phonet_free_inst(struct usb_function_inst= ance *f) struct f_phonet_opts *opts; =20 opts =3D container_of(f, struct f_phonet_opts, func_inst); + mutex_lock(&opts->lock); if (opts->bound) gphonet_cleanup(opts->net); else free_netdev(opts->net); + mutex_unlock(&opts->lock); kfree(opts); } =20 @@ -636,6 +635,7 @@ static struct usb_function_instance *phonet_alloc_inst(= void) if (!opts) return ERR_PTR(-ENOMEM); =20 + mutex_init(&opts->lock); opts->func_inst.free_func_inst =3D phonet_free_inst; opts->net =3D gphonet_setup_default(); if (IS_ERR(opts->net)) { diff --git a/drivers/usb/gadget/function/u_phonet.h b/drivers/usb/gadget/fu= nction/u_phonet.h index ff62ca22c..4666413fc 100644 --- a/drivers/usb/gadget/function/u_phonet.h +++ b/drivers/usb/gadget/function/u_phonet.h @@ -13,6 +13,7 @@ =20 struct f_phonet_opts { struct usb_function_instance func_inst; + struct mutex lock; bool bound; struct net_device *net; }; --=20 2.34.1