Skip to content

Commit 532c8b5

Browse files
Al Virojgross1
authored andcommitted
xen: fix UAF in dmabuf_exp_from_pages()
[dma_buf_fd() fixes; no preferences regarding the tree it goes through - up to xen folks] As soon as we'd inserted a file reference into descriptor table, another thread could close it. That's fine for the case when all we are doing is returning that descriptor to userland (it's a race, but it's a userland race and there's nothing the kernel can do about it). However, if we follow fd_install() with any kind of access to objects that would be destroyed on close (be it the struct file itself or anything destroyed by its ->release()), we have a UAF. dma_buf_fd() is a combination of reserving a descriptor and fd_install(). gntdev dmabuf_exp_from_pages() calls it and then proceeds to access the objects destroyed on close - starting with gntdev_dmabuf itself. Fix that by doing reserving descriptor before anything else and do fd_install() only when everything had been set up. Fixes: a240d6e ("xen/gntdev: Implement dma-buf export functionality") Signed-off-by: Al Viro <[email protected]> Acked-by: Juergen Gross <[email protected]> Message-ID: <20250712050916.GY1880847@ZenIV> Signed-off-by: Juergen Gross <[email protected]>
1 parent 0df1195 commit 532c8b5

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

drivers/xen/gntdev-dmabuf.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,11 @@ struct gntdev_dmabuf_export_args {
357357
static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
358358
{
359359
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
360-
struct gntdev_dmabuf *gntdev_dmabuf;
361-
int ret;
360+
struct gntdev_dmabuf *gntdev_dmabuf __free(kfree) = NULL;
361+
CLASS(get_unused_fd, ret)(O_CLOEXEC);
362+
363+
if (ret < 0)
364+
return ret;
362365

363366
gntdev_dmabuf = kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL);
364367
if (!gntdev_dmabuf)
@@ -383,32 +386,21 @@ static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
383386
exp_info.priv = gntdev_dmabuf;
384387

385388
gntdev_dmabuf->dmabuf = dma_buf_export(&exp_info);
386-
if (IS_ERR(gntdev_dmabuf->dmabuf)) {
387-
ret = PTR_ERR(gntdev_dmabuf->dmabuf);
388-
gntdev_dmabuf->dmabuf = NULL;
389-
goto fail;
390-
}
391-
392-
ret = dma_buf_fd(gntdev_dmabuf->dmabuf, O_CLOEXEC);
393-
if (ret < 0)
394-
goto fail;
389+
if (IS_ERR(gntdev_dmabuf->dmabuf))
390+
return PTR_ERR(gntdev_dmabuf->dmabuf);
395391

396392
gntdev_dmabuf->fd = ret;
397393
args->fd = ret;
398394

399395
pr_debug("Exporting DMA buffer with fd %d\n", ret);
400396

397+
get_file(gntdev_dmabuf->priv->filp);
401398
mutex_lock(&args->dmabuf_priv->lock);
402399
list_add(&gntdev_dmabuf->next, &args->dmabuf_priv->exp_list);
403400
mutex_unlock(&args->dmabuf_priv->lock);
404-
get_file(gntdev_dmabuf->priv->filp);
405-
return 0;
406401

407-
fail:
408-
if (gntdev_dmabuf->dmabuf)
409-
dma_buf_put(gntdev_dmabuf->dmabuf);
410-
kfree(gntdev_dmabuf);
411-
return ret;
402+
fd_install(take_fd(ret), no_free_ptr(gntdev_dmabuf)->dmabuf->file);
403+
return 0;
412404
}
413405

414406
static struct gntdev_grant_map *

0 commit comments

Comments
 (0)