mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: nicholasfvelten@gmail.com
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Nguyen Quang Le Kien <khiemtranzo532001@gmail.com>,
	syzbot+098999e05b6b877c01b3@syzkaller.appspotmail.com
Subject: Re: [PATCH] usb: gadget: f_phonet: don't free the net_device on failed registration
Date: Fri, 20 Nov 2026 11:51:20 +0100	[thread overview]
Message-ID: <2026112007-explicit-nanny-5e06@gregkh> (raw)
In-Reply-To: <20261014-fix-phonet-free-on-failure-v1-1-3c93c48e7527@gmail.com>

On Wed, Oct 14, 2026 at 09:03:57AM -0300, Nicholas Velten via B4 Relay wrote:
> From: Nicholas Velten <nicholasfvelten@gmail.com>
> 
> gphonet_register_netdev() frees the net_device when register_netdev()
> fails. That made sense when the device was registered once at module
> load, but since the conversion to the function interface the device is
> allocated in phonet_alloc_inst(), stored in phonet_opts->net and
> registered from pn_bind(), which only sets phonet_opts->bound on
> success. After a failed registration opts->net therefore points to
> freed memory, and the next bind writes to it in gphonet_set_gadget():
> 
>   BUG: KASAN: slab-use-after-free in pn_bind+0x49a/0x500
>   Write of size 8 at addr ffff888003e785d8 by task gadget_rereg/86
>   Call Trace:
>    pn_bind
>    usb_add_function
>    configfs_composite_bind
>    ...
>   Allocated by task 175:
>    alloc_netdev_mqs
>    phonet_alloc_inst
>    ...
>   Freed by task 86:
>    kfree
>    device_release
>    kobject_put
>    gphonet_register_netdev
>    pn_bind
>    ...
> 
> This is the "slab-use-after-free Write in pn_bind" that syzbot reports:
> its free stack also goes through gphonet_register_netdev() from
> pn_bind(), so the object is freed by the failed bind itself rather than
> by a racing phonet_free_inst().
> 
> Stop freeing the device there; it belongs to the function instance and
> phonet_free_inst() already frees it when it was never registered.
> Dropping the free alone would make the retry hit the BUG_ON() in
> register_netdevice(), because a registration that fails after
> netdev_register_kobject() leaves the device NETREG_UNREGISTERED and it
> cannot be registered again, so also refuse to register a device that is
> not NETREG_UNINITIALIZED, as gether_register_netdev() does for the
> Ethernet functions.
> 
> Reproduced with fail-nth fault injection on the allocations of the
> write() to the gadget's UDC attribute, binding again without injection
> after each failed bind, with a fresh phonet function instance per
> iteration: without this change KASAN reports the use-after-free above at
> the 15th injected allocation; with it, 300 iterations complete with no
> KASAN report, and removing the function instance afterwards frees the
> device without errors.
> 
> Fixes: fcbdf12ebef7 ("usb: gadget: f_phonet: convert to new function interface with backward compatibility")
> Reported-by: syzbot+098999e05b6b877c01b3@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=098999e05b6b877c01b3
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Nicholas Velten <nicholasfvelten@gmail.com>
> ---
> This is the f_phonet counterpart of my u_ether fix for the same retry
> pattern:
> https://lore.kernel.org/all/20261014-fix-gadget-netdev-reregister-v1-1-0fb91999da06@gmail.com/
> Both patches are independent and apply on their own.
> 
> Nguyen, Cc'ing you because your "usb: gadget: f_phonet: fix
> use-after-free in pn_bind" v3 targets the same syzbot report. The lock it
> adds covers a pn_bind() vs phonet_free_inst() race, but the free stack in
> the syzbot report (and in my reproduction) is in pn_bind() itself, via
> gphonet_register_netdev(), so I don't think it addresses this
> use-after-free. The two changes touch different functions and do not
> conflict.
> 
> The reproducer is a small fail-nth program; I can post it if useful.
> Not tested on architectures other than x86_64.
> ---
>  drivers/usb/gadget/function/f_phonet.c | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_phonet.c b/drivers/usb/gadget/function/f_phonet.c
> index b1ee9a7c2..43b10ce36 100644
> --- a/drivers/usb/gadget/function/f_phonet.c
> +++ b/drivers/usb/gadget/function/f_phonet.c
> @@ -722,13 +722,18 @@ void gphonet_set_gadget(struct net_device *net, struct usb_gadget *g)
>  
>  int gphonet_register_netdev(struct net_device *net)
>  {
> -	int status;
> +	/*
> +	 * A register_netdev() that failed after netdev_register_kobject()
> +	 * leaves the device NETREG_UNREGISTERED, and it can never be
> +	 * registered again. pn_bind() retries on every bind until the
> +	 * device is registered, so refuse instead of hitting the BUG_ON()
> +	 * in register_netdevice(). The net_device is owned by the function
> +	 * instance and freed by phonet_free_inst().
> +	 */

Don't write huge comments when they are not needed.

      reply	other threads:[~2026-11-20 10:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14 12:03 Nicholas Velten via B4 Relay
2026-11-20 10:51 ` Greg Kroah-Hartman [this message]

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=2026112007-explicit-nanny-5e06@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=khiemtranzo532001@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nicholasfvelten@gmail.com \
    --cc=syzbot+098999e05b6b877c01b3@syzkaller.appspotmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®