Skip to contentSkip to conversation

misc: fastrpc: avoid double fput() on failed usercopy

PATCH In review Mathias Krause 3 participants 5 messages Jan 27, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc: fastrpc: avoid double fput() on failed usercopy

If the copy back to userland fails for the FASTRPC_IOCTL_ALLOC_DMA_BUFF ioctl(), we shouldn't assume that 'buf->dmabuf' is still valid. In fact, dma_buf_fd() called fd_install() before, i.e. "consumed" one reference, leaving us with none.

Calling dma_buf_put() will therefore put a reference we no longer own, leading to a valid file descritor table entry for an already released 'file' object which is a straight use-after-free.

Simply avoid calling dma_buf_put() and rely on the process exit code to do the necessary cleanup, if needed, i.e. if the file descriptor is still valid.

Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
 drivers/misc/fastrpc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/misc/fastrpc.c +8 -1
index 4ccbf43e6bfa..aa1682b94a23 100644
@@ -1288,7 +1288,14 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
 	}
 
 	if (copy_to_user(argp, &bp, sizeof(bp))) {
-		dma_buf_put(buf->dmabuf);
+		/*
+		 * The usercopy failed, but we can't do much about it, as
+		 * dma_buf_fd() already called fd_install() and made the
+		 * file descriptor accessible for the current process. It
+		 * might already be closed and dmabuf no longer valid when
+		 * we reach this point. Therefore "leak" the fd and rely on
+		 * the process exit path to do any required cleanup.
+		 */
 		return -EFAULT;
 	}
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 29 lines · Mathias Krause

If the copy back to userland fails for the FASTRPC_IOCTL_ALLOC_DMA_BUFF ioctl(), we shouldn't assume that 'buf->dmabuf' is still valid. In fact, dma_buf_fd() called fd_install() before, i.e. "consumed" one reference, leaving us with none.

Calling dma_buf_put() will therefore put a reference we no longer own, leading to a valid file descritor table entry for an already released 'file' object which is a straight use-after-free.

Simply avoid calling dma_buf_put() and rely on the process exit code to do the necessary cleanup, if needed, i.e. if the file descriptor is still valid.

Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
 drivers/misc/fastrpc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/misc/fastrpc.c +1 -1
index 4ccbf43e6bfa..aa1682b94a23 100644
@@ -1288,7 +1288,14 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
 	}
 
 	if (copy_to_user(argp, &bp, sizeof(bp))) {
-		dma_buf_put(buf->dmabuf);
+		/*

drivers/misc/fastrpc.c static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) · line 1301

Quoted 10 lines
drivers/misc/fastrpc.c +7 -0
+		 * The usercopy failed, but we can't do much about it, as
+		 * dma_buf_fd() already called fd_install() and made the
+		 * file descriptor accessible for the current process. It
+		 * might already be closed and dmabuf no longer valid when
+		 * we reach this point. Therefore "leak" the fd and rely on
+		 * the process exit path to do any required cleanup.
+		 */
 		return -EFAULT;
 	}
 

This feels wrong. How do all other dma buf users handle this?

And you forgot to cc: the dmabuf developers, I think get_maintainers.pl should have caught them on this patch.

thanks,

greg k-h

#2 lore raw
Quoted 43 lines · Greg Kroah-Hartman
Quoted 29 lines · Mathias Krause

If the copy back to userland fails for the FASTRPC_IOCTL_ALLOC_DMA_BUFF ioctl(), we shouldn't assume that 'buf->dmabuf' is still valid. In fact, dma_buf_fd() called fd_install() before, i.e. "consumed" one reference, leaving us with none.

Calling dma_buf_put() will therefore put a reference we no longer own, leading to a valid file descritor table entry for an already released 'file' object which is a straight use-after-free.

Simply avoid calling dma_buf_put() and rely on the process exit code to do the necessary cleanup, if needed, i.e. if the file descriptor is still valid.

Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
 drivers/misc/fastrpc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/misc/fastrpc.c +1 -1
index 4ccbf43e6bfa..aa1682b94a23 100644
@@ -1288,7 +1288,14 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
 	}
 
 	if (copy_to_user(argp, &bp, sizeof(bp))) {
-		dma_buf_put(buf->dmabuf);
+		/*

drivers/misc/fastrpc.c static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) · line 1301

Quoted 10 lines
drivers/misc/fastrpc.c +7 -0
+		 * The usercopy failed, but we can't do much about it, as
+		 * dma_buf_fd() already called fd_install() and made the
+		 * file descriptor accessible for the current process. It
+		 * might already be closed and dmabuf no longer valid when
+		 * we reach this point. Therefore "leak" the fd and rely on
+		 * the process exit path to do any required cleanup.
+		 */
 		return -EFAULT;
 	}
 

This feels wrong. How do all other dma buf users handle this?

Quoted 2 lines

And you forgot to cc: the dmabuf developers, I think get_maintainers.pl should have caught them on this patch.

Odd, it didn't, not your fault, my apologies.

DMA BUFFER maintainers, what happened to the MAINTAINERS regex that caused the above patch to not catch you all?

thanks,

greg k-h

#3 lore raw

Am 27.01.22 um 14:26 schrieb Greg Kroah-Hartman:

Quoted 43 lines · Greg Kroah-Hartman
Quoted 42 lines · Mathias Krause
Quoted 29 lines

If the copy back to userland fails for the FASTRPC_IOCTL_ALLOC_DMA_BUFF ioctl(), we shouldn't assume that 'buf->dmabuf' is still valid. In fact, dma_buf_fd() called fd_install() before, i.e. "consumed" one reference, leaving us with none.

Calling dma_buf_put() will therefore put a reference we no longer own, leading to a valid file descritor table entry for an already released 'file' object which is a straight use-after-free.

Simply avoid calling dma_buf_put() and rely on the process exit code to do the necessary cleanup, if needed, i.e. if the file descriptor is still valid.

Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
  drivers/misc/fastrpc.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/misc/fastrpc.c +1 -1
index 4ccbf43e6bfa..aa1682b94a23 100644
@@ -1288,7 +1288,14 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
  	}
 
  	if (copy_to_user(argp, &bp, sizeof(bp))) {
-		dma_buf_put(buf->dmabuf);
+		/*

drivers/misc/fastrpc.c static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) · line 1301

Quoted 10 lines
drivers/misc/fastrpc.c +7 -0
+		 * The usercopy failed, but we can't do much about it, as
+		 * dma_buf_fd() already called fd_install() and made the
+		 * file descriptor accessible for the current process. It
+		 * might already be closed and dmabuf no longer valid when
+		 * we reach this point. Therefore "leak" the fd and rely on
+		 * the process exit path to do any required cleanup.
+		 */
  		return -EFAULT;
  	}
 

This feels wrong. How do all other dma buf users handle this?

Quoted 6 lines
Quoted 2 lines

And you forgot to cc: the dmabuf developers, I think get_maintainers.pl should have caught them on this patch.

Odd, it didn't, not your fault, my apologies.

DMA BUFFER maintainers, what happened to the MAINTAINERS regex that caused the above patch to not catch you all?

That worked as expected: \bdma_(?:buf|fence|resv)\b

We are only automatically getting CCed when any of the dma_buf, dma_fence or dma_resv structures are mentioned.

We could remove the trailing \b and match the function names as well.

Regards,
Christian.

Quoted 4 lines

thanks,

greg k-h

#4 lore raw
In reply to Greg Kroah-Hartman

Am 27.01.22 um 14:24 schrieb Greg Kroah-Hartman:

Quoted 27 lines · Mathias Krause
Quoted 26 lines

If the copy back to userland fails for the FASTRPC_IOCTL_ALLOC_DMA_BUFF ioctl(), we shouldn't assume that 'buf->dmabuf' is still valid. In fact, dma_buf_fd() called fd_install() before, i.e. "consumed" one reference, leaving us with none.

Calling dma_buf_put() will therefore put a reference we no longer own, leading to a valid file descritor table entry for an already released 'file' object which is a straight use-after-free.

Simply avoid calling dma_buf_put() and rely on the process exit code to do the necessary cleanup, if needed, i.e. if the file descriptor is still valid.

Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
 drivers/misc/fastrpc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/misc/fastrpc.c +0 -0
index 4ccbf43e6bfa..aa1682b94a23 100644
@@ -1288,7 +1288,14 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
 	}
 
Quoted 15 lines

drivers/misc/fastrpc.c static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) · line 1301

Quoted 13 lines
drivers/misc/fastrpc.c +8 -1
 	if (copy_to_user(argp, &bp, sizeof(bp))) {
-		dma_buf_put(buf->dmabuf);
+		/*
+		 * The usercopy failed, but we can't do much about it, as
+		 * dma_buf_fd() already called fd_install() and made the
+		 * file descriptor accessible for the current process. It
+		 * might already be closed and dmabuf no longer valid when
+		 * we reach this point. Therefore "leak" the fd and rely on
+		 * the process exit path to do any required cleanup.
+		 */
 		return -EFAULT;
 	}
 

This feels wrong. How do all other dma buf users handle this?

Other dma_buf_fd() users don't wrap the returned fd in another structure and simply return the fd, i.e. have no error path on the way out of the kernel.

Thanks,
Mathias

Quoted 6 lines

And you forgot to cc: the dmabuf developers, I think get_maintainers.pl should have caught them on this patch.

thanks,

greg k-h