Linux USB
 help / color / mirror / Atom feed
* [PATCH usb v4] usb: gadget: f_fs: fix NULL dereference in ffs_dmabuf_detach()
@ 2026-05-13 23:10 Giorgi Kobakhia
  2026-05-22  9:14 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Giorgi Kobakhia @ 2026-05-13 23:10 UTC (permalink / raw)
  To: gregkh, linux-usb; +Cc: Giorgi Kobakhia

ffs_dmabuf_detach() dereferences ffs->gadget->dev.parent without
checking whether the FunctionFS instance is still bound to UDC.

This can be triggered by mounting FunctionFS, binding it to UDC
instance. Then unbind would make ffs->gadget = NULL. Calling
ffs_epfile_ioctl with code FUNCTIONFS_DMABUF_DETACH ends up in
ffs_dmabuf_detach, dereferencing ffs->gadget.

Crash Log:
Oops: general protection fault
KASAN: null-ptr-deref in range [0x00000000000000b0-0x00000000000000b7]
RIP: 0010:ffs_epfile_ioctl+0x3f1/0x25d0

Fix this by checking ffs->gadget under ffs->mutex, taking a reference
to the parent device before dropping the lock, and returning -ENODEV
if the gadget has already been removed.

Fixes: 7b07a2a7ca02 ("usb: gadget: functionfs: Add DMABUF import interface")
Signed-off-by: Giorgi Kobakhia <mrkobush@gmail.com>
---
 drivers/usb/gadget/function/f_fs.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index 002c3441bea3..67c8e65a5aa6 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -1553,14 +1553,27 @@ static int ffs_dmabuf_detach(struct file *file, int fd)
 {
 	struct ffs_epfile *epfile = file->private_data;
 	struct ffs_data *ffs = epfile->ffs;
-	struct device *dev = ffs->gadget->dev.parent;
+	struct device *dev;
 	struct ffs_dmabuf_priv *priv, *tmp;
 	struct dma_buf *dmabuf;
 	int ret = -EPERM;
 
+	mutex_lock(&ffs->mutex);
+	if (!ffs->gadget) {
+		mutex_unlock(&ffs->mutex);
+		return -ENODEV;
+	}
+	dev = get_device(ffs->gadget->dev.parent);
+	mutex_unlock(&ffs->mutex);
+
+	if (!dev)
+		return -ENODEV;
+
 	dmabuf = dma_buf_get(fd);
-	if (IS_ERR(dmabuf))
-		return PTR_ERR(dmabuf);
+	if (IS_ERR(dmabuf)) {
+		ret = PTR_ERR(dmabuf);
+		goto out_put_dev;
+	}
 
 	mutex_lock(&epfile->dmabufs_mutex);
 
@@ -1585,6 +1598,8 @@ static int ffs_dmabuf_detach(struct file *file, int fd)
 	mutex_unlock(&epfile->dmabufs_mutex);
 	dma_buf_put(dmabuf);
 
+out_put_dev:
+	put_device(dev);
 	return ret;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH usb v4] usb: gadget: f_fs: fix NULL dereference in ffs_dmabuf_detach()
  2026-05-13 23:10 [PATCH usb v4] usb: gadget: f_fs: fix NULL dereference in ffs_dmabuf_detach() Giorgi Kobakhia
@ 2026-05-22  9:14 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2026-05-22  9:14 UTC (permalink / raw)
  To: Giorgi Kobakhia; +Cc: linux-usb

On Wed, May 13, 2026 at 04:10:41PM -0700, Giorgi Kobakhia wrote:
> ffs_dmabuf_detach() dereferences ffs->gadget->dev.parent without
> checking whether the FunctionFS instance is still bound to UDC.
> 
> This can be triggered by mounting FunctionFS, binding it to UDC
> instance. Then unbind would make ffs->gadget = NULL. Calling
> ffs_epfile_ioctl with code FUNCTIONFS_DMABUF_DETACH ends up in
> ffs_dmabuf_detach, dereferencing ffs->gadget.
> 
> Crash Log:
> Oops: general protection fault
> KASAN: null-ptr-deref in range [0x00000000000000b0-0x00000000000000b7]
> RIP: 0010:ffs_epfile_ioctl+0x3f1/0x25d0
> 
> Fix this by checking ffs->gadget under ffs->mutex, taking a reference
> to the parent device before dropping the lock, and returning -ENODEV
> if the gadget has already been removed.
> 
> Fixes: 7b07a2a7ca02 ("usb: gadget: functionfs: Add DMABUF import interface")
> Signed-off-by: Giorgi Kobakhia <mrkobush@gmail.com>
> ---
>  drivers/usb/gadget/function/f_fs.c | 21 ++++++++++++++++++---
>  1 file changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
> index 002c3441bea3..67c8e65a5aa6 100644
> --- a/drivers/usb/gadget/function/f_fs.c
> +++ b/drivers/usb/gadget/function/f_fs.c
> @@ -1553,14 +1553,27 @@ static int ffs_dmabuf_detach(struct file *file, int fd)
>  {
>  	struct ffs_epfile *epfile = file->private_data;
>  	struct ffs_data *ffs = epfile->ffs;
> -	struct device *dev = ffs->gadget->dev.parent;
> +	struct device *dev;
>  	struct ffs_dmabuf_priv *priv, *tmp;
>  	struct dma_buf *dmabuf;
>  	int ret = -EPERM;
>  
> +	mutex_lock(&ffs->mutex);
> +	if (!ffs->gadget) {
> +		mutex_unlock(&ffs->mutex);
> +		return -ENODEV;
> +	}
> +	dev = get_device(ffs->gadget->dev.parent);
> +	mutex_unlock(&ffs->mutex);
> +
> +	if (!dev)
> +		return -ENODEV;
> +
>  	dmabuf = dma_buf_get(fd);
> -	if (IS_ERR(dmabuf))
> -		return PTR_ERR(dmabuf);
> +	if (IS_ERR(dmabuf)) {
> +		ret = PTR_ERR(dmabuf);
> +		goto out_put_dev;
> +	}
>  
>  	mutex_lock(&epfile->dmabufs_mutex);
>  
> @@ -1585,6 +1598,8 @@ static int ffs_dmabuf_detach(struct file *file, int fd)
>  	mutex_unlock(&epfile->dmabufs_mutex);
>  	dma_buf_put(dmabuf);
>  
> +out_put_dev:
> +	put_device(dev);
>  	return ret;
>  }
>  
> -- 
> 2.43.0
> 

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- This looks like a new version of a previously submitted patch, but you
  did not list below the --- line any changes from the previous version.
  Please read the section entitled "The canonical patch format" in the
  kernel file, Documentation/process/submitting-patches.rst for what
  needs to be done here to properly describe this.

- You have marked a patch with a "Fixes:" tag for a commit that is in an
  older released kernel, yet you do not have a cc: stable line in the
  signed-off-by area at all, which means that the patch will not be
  applied to any older kernel releases.  To properly fix this, please
  follow the documented rules in the
  Documentation/process/stable-kernel-rules.rst file for how to resolve
  this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-05-22  9:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-13 23:10 [PATCH usb v4] usb: gadget: f_fs: fix NULL dereference in ffs_dmabuf_detach() Giorgi Kobakhia
2026-05-22  9:14 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox