From: Ignat Korchagin <ignat@cloudflare.com>
To: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
Marcel Holtmann <marcel@holtmann.org>,
Johan Hedberg <johan.hedberg@gmail.com>,
Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
Oliver Hartkopp <socketcan@hartkopp.net>,
Marc Kleine-Budde <mkl@pengutronix.de>,
Alexander Aring <alex.aring@gmail.com>,
Stefan Schmidt <stefan@datenfreihafen.org>,
Miquel Raynal <miquel.raynal@bootlin.com>,
David Ahern <dsahern@kernel.org>,
Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
linux-bluetooth@vger.kernel.org, linux-can@vger.kernel.org,
linux-wpan@vger.kernel.org
Cc: kernel-team@cloudflare.com, kuniyu@amazon.com,
alibuda@linux.alibaba.com, Ignat Korchagin <ignat@cloudflare.com>
Subject: [PATCH net-next v3 1/9] af_packet: avoid erroring out after sock_init_data() in packet_create()
Date: Mon, 14 Oct 2024 16:38:00 +0100 [thread overview]
Message-ID: <20241014153808.51894-2-ignat@cloudflare.com> (raw)
In-Reply-To: <20241014153808.51894-1-ignat@cloudflare.com>
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 <edumazet@google.com>
Signed-off-by: Ignat Korchagin <ignat@cloudflare.com>
---
net/packet/af_packet.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index f8942062f776..99ae27d1e4dc 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -3421,17 +3421,17 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
if (sock->type == SOCK_PACKET)
sock->ops = &packet_ops_spkt;
+ po = pkt_sk(sk);
+ err = packet_alloc_pending(po);
+ if (err)
+ goto out_sk_free;
+
sock_init_data(sock, sk);
- po = pkt_sk(sk);
init_completion(&po->skb_completion);
sk->sk_family = PF_PACKET;
po->num = proto;
- err = packet_alloc_pending(po);
- if (err)
- goto out2;
-
packet_cached_dev_reset(po);
sk->sk_destruct = packet_sock_destruct;
@@ -3463,7 +3463,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
sock_prot_inuse_add(net, &packet_proto, 1);
return 0;
-out2:
+out_sk_free:
sk_free(sk);
out:
return err;
--
2.39.5
next prev parent reply other threads:[~2024-10-14 15:38 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-14 15:37 [PATCH net-next v3 0/9] do not leave dangling sk pointers in pf->create functions Ignat Korchagin
2024-10-14 15:38 ` Ignat Korchagin [this message]
2024-10-14 21:14 ` [PATCH net-next v3 1/9] af_packet: avoid erroring out after sock_init_data() in packet_create() Kuniyuki Iwashima
2024-10-15 0:52 ` Willem de Bruijn
2024-10-15 8:01 ` Eric Dumazet
2024-10-14 15:38 ` [PATCH net-next v3 2/9] Bluetooth: L2CAP: do not leave dangling sk pointer on error in l2cap_sock_create() Ignat Korchagin
2024-10-14 21:23 ` Kuniyuki Iwashima
2024-10-15 8:03 ` Eric Dumazet
2024-10-14 15:38 ` [PATCH net-next v3 3/9] Bluetooth: RFCOMM: avoid leaving dangling sk pointer in rfcomm_sock_alloc() Ignat Korchagin
2024-10-14 21:29 ` Kuniyuki Iwashima
2024-10-15 8:04 ` Eric Dumazet
2024-10-14 15:38 ` [PATCH net-next v3 4/9] net: af_can: do not leave a dangling sk pointer in can_create() Ignat Korchagin
2024-10-14 21:32 ` Kuniyuki Iwashima
2024-10-15 6:21 ` Marc Kleine-Budde
2024-10-14 15:38 ` [PATCH net-next v3 5/9] net: ieee802154: do not leave a dangling sk pointer in ieee802154_create() Ignat Korchagin
2024-10-14 21:35 ` Kuniyuki Iwashima
2024-10-15 8:05 ` Eric Dumazet
2024-10-14 15:38 ` [PATCH net-next v3 6/9] net: inet: do not leave a dangling sk pointer in inet_create() Ignat Korchagin
2024-10-14 21:37 ` Kuniyuki Iwashima
2024-10-15 8:05 ` Eric Dumazet
2024-10-14 15:38 ` [PATCH net-next v3 7/9] net: inet6: do not leave a dangling sk pointer in inet6_create() Ignat Korchagin
2024-10-14 21:38 ` Kuniyuki Iwashima
2024-10-15 8:11 ` Eric Dumazet
2024-10-14 15:38 ` [PATCH net-next v3 8/9] net: warn, if pf->create does not clear sock->sk on error Ignat Korchagin
2024-10-14 21:40 ` Kuniyuki Iwashima
2024-10-15 8:06 ` Eric Dumazet
2024-10-14 15:38 ` [PATCH net-next v3 9/9] Revert "net: do not leave a dangling sk pointer, when socket creation fails" Ignat Korchagin
2024-10-14 21:42 ` Kuniyuki Iwashima
2024-10-15 8:06 ` Eric Dumazet
2024-10-16 1:50 ` [PATCH net-next v3 0/9] do not leave dangling sk pointers in pf->create functions patchwork-bot+netdevbpf
2024-10-16 19:05 ` patchwork-bot+bluetooth
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241014153808.51894-2-ignat@cloudflare.com \
--to=ignat@cloudflare.com \
--cc=alex.aring@gmail.com \
--cc=alibuda@linux.alibaba.com \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@google.com \
--cc=johan.hedberg@gmail.com \
--cc=kernel-team@cloudflare.com \
--cc=kuba@kernel.org \
--cc=kuniyu@amazon.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-wpan@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
--cc=miquel.raynal@bootlin.com \
--cc=mkl@pengutronix.de \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=socketcan@hartkopp.net \
--cc=stefan@datenfreihafen.org \
--cc=willemdebruijn.kernel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).