Skip to content

Commit f26c08b

Browse files
Hillf Dantonaxboe
authored andcommitted
io_uring: fix file leak on error path of io ctx creation
Put file as part of error handling when setting up io ctx to fix memory leaks like the following one. BUG: memory leak unreferenced object 0xffff888101ea2200 (size 256): comm "syz-executor355", pid 8470, jiffies 4294953658 (age 32.400s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 20 59 03 01 81 88 ff ff 80 87 a8 10 81 88 ff ff Y.............. backtrace: [<000000002e0a7c5f>] kmem_cache_zalloc include/linux/slab.h:654 [inline] [<000000002e0a7c5f>] __alloc_file+0x1f/0x130 fs/file_table.c:101 [<000000001a55b73a>] alloc_empty_file+0x69/0x120 fs/file_table.c:151 [<00000000fb22349e>] alloc_file+0x33/0x1b0 fs/file_table.c:193 [<000000006e1465bb>] alloc_file_pseudo+0xb2/0x140 fs/file_table.c:233 [<000000007118092a>] anon_inode_getfile fs/anon_inodes.c:91 [inline] [<000000007118092a>] anon_inode_getfile+0xaa/0x120 fs/anon_inodes.c:74 [<000000002ae99012>] io_uring_get_fd fs/io_uring.c:9198 [inline] [<000000002ae99012>] io_uring_create fs/io_uring.c:9377 [inline] [<000000002ae99012>] io_uring_setup+0x1125/0x1630 fs/io_uring.c:9411 [<000000008280baad>] do_syscall_64+0x2d/0x70 arch/x86/entry/common.c:46 [<00000000685d8cf0>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 Reported-by: [email protected] Fixes: 0f21220 ("io_uring: don't rely on weak ->files references") Cc: Pavel Begunkov <[email protected]> Signed-off-by: Hillf Danton <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent e8c954d commit f26c08b

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

fs/io_uring.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9183,6 +9183,7 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
91839183
{
91849184
struct file *file;
91859185
int ret;
9186+
int fd;
91869187

91879188
#if defined(CONFIG_UNIX)
91889189
ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
@@ -9194,25 +9195,27 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
91949195
ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
91959196
if (ret < 0)
91969197
goto err;
9198+
fd = ret;
91979199

91989200
file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
91999201
O_RDWR | O_CLOEXEC);
92009202
if (IS_ERR(file)) {
9201-
err_fd:
9202-
put_unused_fd(ret);
9203+
put_unused_fd(fd);
92039204
ret = PTR_ERR(file);
92049205
goto err;
92059206
}
92069207

92079208
#if defined(CONFIG_UNIX)
92089209
ctx->ring_sock->file = file;
92099210
#endif
9210-
if (unlikely(io_uring_add_task_file(ctx, file))) {
9211-
file = ERR_PTR(-ENOMEM);
9212-
goto err_fd;
9211+
ret = io_uring_add_task_file(ctx, file);
9212+
if (ret) {
9213+
fput(file);
9214+
put_unused_fd(fd);
9215+
goto err;
92139216
}
9214-
fd_install(ret, file);
9215-
return ret;
9217+
fd_install(fd, file);
9218+
return fd;
92169219
err:
92179220
#if defined(CONFIG_UNIX)
92189221
sock_release(ctx->ring_sock);

0 commit comments

Comments
 (0)