Skip to content

Commit b609915

Browse files
krismangregkh
authored andcommitted
io_uring/net: Avoid msghdr on op_connect/op_bind async data
[ Upstream commit 3979840 ] This fixes a memory leak due to the lack of the cleanup hook for the iovec. The stable backport differs from upstream by dropping the io_connect_bpf_populate hunk, which didn't exist at the time and by fixing the merge conflict due to the introduction of io_bind_file_create. Both IORING_OP_CONNECT and IORING_OP_BIND reuse the msghdr object just to store the sockaddr. Beyond allocating a much larger object than needed, msghdr can also wrap an iovec, which will be recycled unnecessarily. This uses the sockaddr directly. Cc: stable@vger.kernel.org Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de> Link: https://patch.msgid.link/20260602215327.1885109-2-krisman@suse.de Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 275d294 commit b609915

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

io_uring/net.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ int io_socket(struct io_kiocb *req, unsigned int issue_flags)
17711771
int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
17721772
{
17731773
struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1774-
struct io_async_msghdr *io;
1774+
struct sockaddr_storage *addr;
17751775

17761776
if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
17771777
return -EINVAL;
@@ -1780,17 +1780,17 @@ int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
17801780
conn->addr_len = READ_ONCE(sqe->addr2);
17811781
conn->in_progress = conn->seen_econnaborted = false;
17821782

1783-
io = io_msg_alloc_async(req);
1784-
if (unlikely(!io))
1783+
addr = io_uring_alloc_async_data(NULL, req);
1784+
if (unlikely(!addr))
17851785
return -ENOMEM;
17861786

1787-
return move_addr_to_kernel(conn->addr, conn->addr_len, &io->addr);
1787+
return move_addr_to_kernel(conn->addr, conn->addr_len, addr);
17881788
}
17891789

17901790
int io_connect(struct io_kiocb *req, unsigned int issue_flags)
17911791
{
17921792
struct io_connect *connect = io_kiocb_to_cmd(req, struct io_connect);
1793-
struct io_async_msghdr *io = req->async_data;
1793+
struct sockaddr_storage *addr = req->async_data;
17941794
unsigned file_flags;
17951795
int ret;
17961796
bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
@@ -1804,8 +1804,7 @@ int io_connect(struct io_kiocb *req, unsigned int issue_flags)
18041804

18051805
file_flags = force_nonblock ? O_NONBLOCK : 0;
18061806

1807-
ret = __sys_connect_file(req->file, &io->addr, connect->addr_len,
1808-
file_flags);
1807+
ret = __sys_connect_file(req->file, addr, connect->addr_len, file_flags);
18091808
if ((ret == -EAGAIN || ret == -EINPROGRESS || ret == -ECONNABORTED)
18101809
&& force_nonblock) {
18111810
if (ret == -EINPROGRESS) {
@@ -1834,7 +1833,6 @@ int io_connect(struct io_kiocb *req, unsigned int issue_flags)
18341833
out:
18351834
if (ret < 0)
18361835
req_set_fail(req);
1837-
io_req_msg_cleanup(req, issue_flags);
18381836
io_req_set_res(req, ret, 0);
18391837
return IOU_COMPLETE;
18401838
}
@@ -1844,23 +1842,23 @@ int io_connect(struct io_kiocb *req, unsigned int issue_flags)
18441842
* which in turn end up in mnt_want_write() which will grab the fs
18451843
* percpu start write sem. This can trigger a lockdep warning.
18461844
*/
1847-
static int io_bind_file_create(const struct io_async_msghdr *io, int addr_len)
1845+
static int io_bind_file_create(const struct sockaddr_storage *addr, int addr_len)
18481846
{
18491847
const struct sockaddr_un *sun;
18501848

1851-
if (io->addr.ss_family != AF_UNIX)
1849+
if (addr->ss_family != AF_UNIX)
18521850
return 0;
18531851
if (addr_len <= offsetof(struct sockaddr_un, sun_path))
18541852
return 0;
1855-
sun = (const struct sockaddr_un *) &io->addr;
1853+
sun = (const struct sockaddr_un *) addr;
18561854
return sun->sun_path[0] != '\0';
18571855
}
18581856

18591857
int io_bind_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
18601858
{
18611859
struct io_bind *bind = io_kiocb_to_cmd(req, struct io_bind);
18621860
struct sockaddr __user *uaddr;
1863-
struct io_async_msghdr *io;
1861+
struct sockaddr_storage *addr;
18641862
int ret;
18651863

18661864
if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
@@ -1869,29 +1867,31 @@ int io_bind_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
18691867
uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
18701868
bind->addr_len = READ_ONCE(sqe->addr2);
18711869

1872-
io = io_msg_alloc_async(req);
1873-
if (unlikely(!io))
1870+
addr = io_uring_alloc_async_data(NULL, req);
1871+
if (unlikely(!addr))
18741872
return -ENOMEM;
1875-
ret = move_addr_to_kernel(uaddr, bind->addr_len, &io->addr);
1873+
1874+
ret = move_addr_to_kernel(uaddr, bind->addr_len, addr);
18761875
if (unlikely(ret))
18771876
return ret;
1878-
if (io_bind_file_create(io, bind->addr_len))
1877+
if (io_bind_file_create(addr, bind->addr_len))
18791878
req->flags |= REQ_F_FORCE_ASYNC;
18801879
return 0;
18811880
}
18821881

1882+
18831883
int io_bind(struct io_kiocb *req, unsigned int issue_flags)
18841884
{
18851885
struct io_bind *bind = io_kiocb_to_cmd(req, struct io_bind);
1886-
struct io_async_msghdr *io = req->async_data;
1886+
struct sockaddr_storage *addr = req->async_data;
18871887
struct socket *sock;
18881888
int ret;
18891889

18901890
sock = sock_from_file(req->file);
18911891
if (unlikely(!sock))
18921892
return -ENOTSOCK;
18931893

1894-
ret = __sys_bind_socket(sock, &io->addr, bind->addr_len);
1894+
ret = __sys_bind_socket(sock, addr, bind->addr_len);
18951895
if (ret < 0)
18961896
req_set_fail(req);
18971897
io_req_set_res(req, ret, 0);

io_uring/opdef.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ const struct io_issue_def io_issue_defs[] = {
207207
.unbound_nonreg_file = 1,
208208
.pollout = 1,
209209
#if defined(CONFIG_NET)
210-
.async_size = sizeof(struct io_async_msghdr),
210+
.async_size = sizeof(struct sockaddr_storage),
211211
.prep = io_connect_prep,
212212
.issue = io_connect,
213213
#else
@@ -504,7 +504,7 @@ const struct io_issue_def io_issue_defs[] = {
504504
.needs_file = 1,
505505
.prep = io_bind_prep,
506506
.issue = io_bind,
507-
.async_size = sizeof(struct io_async_msghdr),
507+
.async_size = sizeof(struct sockaddr_storage),
508508
#else
509509
.prep = io_eopnotsupp_prep,
510510
#endif

0 commit comments

Comments
 (0)