Skip to contentSkip to conversation

usb: gadget: f_fs: fix NULL dereference in ffs_dmabuf_detach()

PATCH In review Giorgi Kobakhia 2 participants 2 messages May 13 – May 22 USB

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 usb: gadget: f_fs: fix NULL dereference in ffs_dmabuf_detach()

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")
---
 drivers/usb/gadget/function/f_fs.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/usb/gadget/function/f_fs.c +18 -3
index 002c3441bea3..67c8e65a5aa6 100644
@@ -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;
 }
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 70 lines · 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")
---
 drivers/usb/gadget/function/f_fs.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/usb/gadget/function/f_fs.c +18 -3
index 002c3441bea3..67c8e65a5aa6 100644
@@ -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;
 }
 
Signature · 1 line
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