Rely on the fd exit path machinery. Signed-off-by: Christian Brauner (Amutable) --- drivers/misc/fastrpc.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index 90fd669636ec..24b84019bbd7 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -1804,6 +1805,7 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) DEFINE_DMA_BUF_EXPORT_INFO(exp_info); struct fastrpc_buf *buf = NULL; int err; + const struct fd_slot *fd; if (copy_from_user(&bp, argp, sizeof(bp))) return -EFAULT; @@ -1822,23 +1824,16 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) return err; } - bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); - if (bp.fd < 0) { + fd = fd_prepare(O_ACCMODE); + if (IS_ERR(fd)) { dma_buf_put(buf->dmabuf); return -EINVAL; } + bp.fd = fd_stage(fd, buf->dmabuf->file); - if (copy_to_user(argp, &bp, sizeof(bp))) { - /* - * 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. - */ + /* The staged file is dropped with its descriptor if this faults. */ + if (copy_to_user(argp, &bp, sizeof(bp))) return -EFAULT; - } return 0; } -- 2.53.0