mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michael Grzeschik <mgr@kernel.org>
To: Yizhou Zhao <zhaoyz24@mails.tsinghua.edu.cn>
Cc: v9fs@lists.linux.dev, Eric Van Hensbergen <ericvh@kernel.org>,
	 Latchesar Ionkov <lucho@ionkov.net>,
	Dominique Martinet <asmadeus@codewreck.org>,
	 Christian Schoenebeck <linux_oss@crudebyte.com>,
	linux-kernel@vger.kernel.org,
	 Yuxiang Yang <yangyx22@mails.tsinghua.edu.cn>,
	Ao Wang <wangao@seu.edu.cn>, Xuewei Feng <fengxw06@126.com>,
	 Qi Li <qli01@tsinghua.edu.cn>, Ke Xu <xuke@tsinghua.edu.cn>
Subject: Re: [PATCH] net/9p/usbg: Fix use-after-free on usb9pfs_clear_tx
Date: Tue, 15 Sep 2026 08:51:18 +0200	[thread overview]
Message-ID: <aqjqwG_6zXJYC7nW@line.checkpat.ch> (raw)
In-Reply-To: <20260529081026.77732-1-zhaoyz24@mails.tsinghua.edu.cn>

On Fri, May 29, 2026 at 04:10:25PM +0800, Yizhou Zhao wrote:
> When p9_usbg_close() is called, it invokes usb9pfs_clear_tx() but does
> not clear usb9pfs->client nor usb9pfs->in_req->context afterwards.
> The 9p client core (p9_client_destroy) calls close() and then
> kfree(clnt), so after p9_usbg_close returns the client struct is freed.
> 
> If the USB gadget is later disabled (e.g., host disconnect),
> usb9pfs_disable calls usb9pfs_clear_tx again, which passes the dangling
> usb9pfs->client pointer to p9_client_cb. Inside p9_client_cb,
> p9_req_put(c, req) dereferences the freed client to access its reqs IDR,
> causing a use-after-free.
> 
> Additionally, usb9pfs_clear_tx does not clear usb9pfs->in_req->context
> after calling p9_client_cb, so a second invocation finds a stale request
> pointer and attempts double-completion, further compounding the UAF risk.
> 
> Fix by:
> 1. Clearing usb9pfs->in_req->context to NULL in usb9pfs_clear_tx()
>    before checking usb9pfs->client, so that even if the client is gone
>    the stale request pointer is consumed and cannot cause
>    double-completion.
> 2. Adding a NULL check on usb9pfs->client in usb9pfs_clear_tx() to skip
>    p9_client_cb() when the client has already been destroyed.
> 3. Setting usb9pfs->client = NULL in p9_usbg_close() after calling
>    usb9pfs_clear_tx(), to prevent any subsequent call from dereferencing
>    the freed client pointer.
> 
> Fixes: a3be076dc174 ("net/9p/usbg: Add new usb gadget function transport")
> Reported-by: Yizhou Zhao <zhaoyz24@mails.tsinghua.edu.cn>
> Reported-by: Yuxiang Yang <yangyx22@mails.tsinghua.edu.cn>
> Reported-by: Ao Wang <wangao@seu.edu.cn>
> Reported-by: Xuewei Feng <fengxw06@126.com>
> Reported-by: Qi Li <qli01@tsinghua.edu.cn>
> Reported-by: Ke Xu <xuke@tsinghua.edu.cn>
> Assisted-by: GLM:GLM-5.1
> Signed-off-by: Yizhou Zhao <zhaoyz24@mails.tsinghua.edu.cn>

Acked-by: Michael Grzeschik <mgr@kernel.org>

> ---
> diff --git a/net/9p/trans_usbg.c b/net/9p/trans_usbg.c
> index 1ce7033..c75de5c 100644
> --- a/net/9p/trans_usbg.c
> +++ b/net/9p/trans_usbg.c
> @@ -435,6 +435,11 @@ static void usb9pfs_clear_tx(struct f_usb9pfs *usb9pfs)
>  	if (!req)
>  		return;
>  
> +	usb9pfs->in_req->context = NULL;
> +
> +	if (!usb9pfs->client)
> +		return;
> +
>  	if (!req->t_err)
>  		req->t_err = -ECONNRESET;
>  
> @@ -457,6 +462,7 @@ static void p9_usbg_close(struct p9_client *client)
>  	client->status = Disconnected;
>  
>  	usb9pfs_clear_tx(usb9pfs);
> +	usb9pfs->client = NULL;
>  
>  	opts = container_of(usb9pfs->function.fi,
>  			    struct f_usb9pfs_opts, func_inst);
> 
> --
> 2.43.0
> 

      parent reply	other threads:[~2026-09-15  6:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-29  8:10 Yizhou Zhao
2026-09-13  8:44 ` Dominique Martinet
2026-09-15  6:54   ` Michael Grzeschik
2026-09-15  7:09     ` Dominique Martinet
2026-09-15  6:51 ` Michael Grzeschik [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=aqjqwG_6zXJYC7nW@line.checkpat.ch \
    --to=mgr@kernel.org \
    --cc=asmadeus@codewreck.org \
    --cc=ericvh@kernel.org \
    --cc=fengxw06@126.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux_oss@crudebyte.com \
    --cc=lucho@ionkov.net \
    --cc=qli01@tsinghua.edu.cn \
    --cc=v9fs@lists.linux.dev \
    --cc=wangao@seu.edu.cn \
    --cc=xuke@tsinghua.edu.cn \
    --cc=yangyx22@mails.tsinghua.edu.cn \
    --cc=zhaoyz24@mails.tsinghua.edu.cn \
    /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®