Skip to content

Commit 9df99c3

Browse files
ignatkkuba-moo
authored andcommitted
net: inet6: do not leave a dangling sk pointer in inet6_create()
sock_init_data() attaches the allocated sk pointer to the provided sock object. If inet6_create() fails later, the sk object is released, but the sock object retains the dangling sk pointer, which may cause use-after-free later. Clear the sock sk pointer on error. Signed-off-by: Ignat Korchagin <ignat@cloudflare.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Link: https://patch.msgid.link/20241014153808.51894-8-ignat@cloudflare.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 9365fa5 commit 9df99c3

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

net/ipv6/af_inet6.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,31 +252,29 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol,
252252
*/
253253
inet->inet_sport = htons(inet->inet_num);
254254
err = sk->sk_prot->hash(sk);
255-
if (err) {
256-
sk_common_release(sk);
257-
goto out;
258-
}
255+
if (err)
256+
goto out_sk_release;
259257
}
260258
if (sk->sk_prot->init) {
261259
err = sk->sk_prot->init(sk);
262-
if (err) {
263-
sk_common_release(sk);
264-
goto out;
265-
}
260+
if (err)
261+
goto out_sk_release;
266262
}
267263

268264
if (!kern) {
269265
err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
270-
if (err) {
271-
sk_common_release(sk);
272-
goto out;
273-
}
266+
if (err)
267+
goto out_sk_release;
274268
}
275269
out:
276270
return err;
277271
out_rcu_unlock:
278272
rcu_read_unlock();
279273
goto out;
274+
out_sk_release:
275+
sk_common_release(sk);
276+
sock->sk = NULL;
277+
goto out;
280278
}
281279

282280
static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,

0 commit comments

Comments
 (0)