Skip to content

Commit 46f2a11

Browse files
ignatkkuba-moo
authored andcommitted
af_packet: avoid erroring out after sock_init_data() in packet_create()
After sock_init_data() the allocated sk object is attached to the provided sock object. On error, packet_create() frees the sk object leaving the dangling pointer in the sock object on return. Some other code may try to use this pointer and cause use-after-free. Suggested-by: Eric Dumazet <[email protected]> Signed-off-by: Ignat Korchagin <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Reviewed-by: Willem de Bruijn <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 397006b commit 46f2a11

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

net/packet/af_packet.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,17 +3422,17 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
34223422
if (sock->type == SOCK_PACKET)
34233423
sock->ops = &packet_ops_spkt;
34243424

3425+
po = pkt_sk(sk);
3426+
err = packet_alloc_pending(po);
3427+
if (err)
3428+
goto out_sk_free;
3429+
34253430
sock_init_data(sock, sk);
34263431

3427-
po = pkt_sk(sk);
34283432
init_completion(&po->skb_completion);
34293433
sk->sk_family = PF_PACKET;
34303434
po->num = proto;
34313435

3432-
err = packet_alloc_pending(po);
3433-
if (err)
3434-
goto out2;
3435-
34363436
packet_cached_dev_reset(po);
34373437

34383438
sk->sk_destruct = packet_sock_destruct;
@@ -3464,7 +3464,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
34643464
sock_prot_inuse_add(net, &packet_proto, 1);
34653465

34663466
return 0;
3467-
out2:
3467+
out_sk_free:
34683468
sk_free(sk);
34693469
out:
34703470
return err;

0 commit comments

Comments
 (0)