Skip to content

Commit 6884028

Browse files
q2venPaolo Abeni
authored andcommitted
af_unix: Fix memleak of newsk in unix_stream_connect().
When prepare_peercred() fails in unix_stream_connect(), unix_release_sock() is not called for newsk, and the memory is leaked. Let's move prepare_peercred() before unix_create1(). Fixes: fd0a109 ("net, pidfs: prepare for handing out pidfds for reaped sk->sk_peer_pid") Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com> Link: https://patch.msgid.link/20260207232236.2557549-1-kuniyu@google.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent e3998b6 commit 6884028

1 file changed

Lines changed: 3 additions & 8 deletions

File tree

net/unix/af_unix.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,10 +1650,9 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr_unsized *uad
16501650

16511651
timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
16521652

1653-
/* First of all allocate resources.
1654-
* If we will make it after state is locked,
1655-
* we will have to recheck all again in any case.
1656-
*/
1653+
err = prepare_peercred(&peercred);
1654+
if (err)
1655+
goto out;
16571656

16581657
/* create new sock for complete connection */
16591658
newsk = unix_create1(net, NULL, 0, sock->type);
@@ -1662,10 +1661,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr_unsized *uad
16621661
goto out;
16631662
}
16641663

1665-
err = prepare_peercred(&peercred);
1666-
if (err)
1667-
goto out;
1668-
16691664
/* Allocate skb for sending to listening sock */
16701665
skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
16711666
if (!skb) {

0 commit comments

Comments
 (0)