Skip to content

Commit 9365fa5

Browse files
ignatkkuba-moo
authored andcommitted
net: inet: do not leave a dangling sk pointer in inet_create()
sock_init_data() attaches the allocated sk object to the provided sock object. If inet_create() fails later, the sk object is freed, but the sock object retains the dangling pointer, which may create use-after-free later. Clear the sk pointer in the sock object on error. Signed-off-by: Ignat Korchagin <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent b4fcd63 commit 9365fa5

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

net/ipv4/af_inet.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -376,32 +376,30 @@ static int inet_create(struct net *net, struct socket *sock, int protocol,
376376
inet->inet_sport = htons(inet->inet_num);
377377
/* Add to protocol hash chains. */
378378
err = sk->sk_prot->hash(sk);
379-
if (err) {
380-
sk_common_release(sk);
381-
goto out;
382-
}
379+
if (err)
380+
goto out_sk_release;
383381
}
384382

385383
if (sk->sk_prot->init) {
386384
err = sk->sk_prot->init(sk);
387-
if (err) {
388-
sk_common_release(sk);
389-
goto out;
390-
}
385+
if (err)
386+
goto out_sk_release;
391387
}
392388

393389
if (!kern) {
394390
err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
395-
if (err) {
396-
sk_common_release(sk);
397-
goto out;
398-
}
391+
if (err)
392+
goto out_sk_release;
399393
}
400394
out:
401395
return err;
402396
out_rcu_unlock:
403397
rcu_read_unlock();
404398
goto out;
399+
out_sk_release:
400+
sk_common_release(sk);
401+
sock->sk = NULL;
402+
goto out;
405403
}
406404

407405

0 commit comments

Comments
 (0)